2 * linux/arch/sh/kernel/ptrace.c
4 * Original x86 implementation:
6 * edited by Linus Torvalds
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/slab.h>
22 #include <linux/security.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
29 #include <asm/mmu_context.h>
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
37 * This routine will get a word off of the process kernel stack.
39 static inline int get_stack_long(struct task_struct
*task
, int offset
)
43 stack
= (unsigned char *)
44 task
->thread_info
+ THREAD_SIZE
- sizeof(struct pt_regs
)
46 - sizeof(struct pt_dspregs
)
48 - sizeof(unsigned long);
50 return (*((int *)stack
));
54 * This routine will put a word on the process kernel stack.
56 static inline int put_stack_long(struct task_struct
*task
, int offset
,
61 stack
= (unsigned char *)
62 task
->thread_info
+ THREAD_SIZE
- sizeof(struct pt_regs
)
64 - sizeof(struct pt_dspregs
)
66 - sizeof(unsigned long);
68 *(unsigned long *) stack
= data
;
73 * Called by kernel/ptrace.c when detaching..
75 * Make sure single step bits etc are not set.
77 void ptrace_disable(struct task_struct
*child
)
82 asmlinkage
int sys_ptrace(long request
, long pid
, long addr
, long data
)
84 struct task_struct
*child
;
85 struct user
* dummy
= NULL
;
90 if (request
== PTRACE_TRACEME
) {
91 /* are we already being traced? */
92 if (current
->ptrace
& PT_PTRACED
)
94 ret
= security_ptrace(current
->parent
, current
);
97 /* set the ptrace bit in the process flags. */
98 current
->ptrace
|= PT_PTRACED
;
103 read_lock(&tasklist_lock
);
104 child
= find_task_by_pid(pid
);
106 get_task_struct(child
);
107 read_unlock(&tasklist_lock
);
112 if (pid
== 1) /* you may not mess with init */
115 if (request
== PTRACE_ATTACH
) {
116 ret
= ptrace_attach(child
);
120 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
125 /* when I and D space are separate, these will need to be fixed. */
126 case PTRACE_PEEKTEXT
: /* read word at location addr. */
127 case PTRACE_PEEKDATA
: {
131 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
133 if (copied
!= sizeof(tmp
))
135 ret
= put_user(tmp
,(unsigned long *) data
);
139 /* read the word at location addr in the USER area. */
140 case PTRACE_PEEKUSR
: {
144 if ((addr
& 3) || addr
< 0 ||
145 addr
> sizeof(struct user
) - 3)
148 if (addr
< sizeof(struct pt_regs
))
149 tmp
= get_stack_long(child
, addr
);
150 else if (addr
>= (long) &dummy
->fpu
&&
151 addr
< (long) &dummy
->u_fpvalid
) {
152 if (!tsk_used_math(child
)) {
153 if (addr
== (long)&dummy
->fpu
.fpscr
)
158 tmp
= ((long *)&child
->thread
.fpu
)
159 [(addr
- (long)&dummy
->fpu
) >> 2];
160 } else if (addr
== (long) &dummy
->u_fpvalid
)
161 tmp
= !!tsk_used_math(child
);
164 ret
= put_user(tmp
, (unsigned long *)data
);
168 /* when I and D space are separate, this will have to be fixed. */
169 case PTRACE_POKETEXT
: /* write the word at location addr. */
170 case PTRACE_POKEDATA
:
172 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
177 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
179 if ((addr
& 3) || addr
< 0 ||
180 addr
> sizeof(struct user
) - 3)
183 if (addr
< sizeof(struct pt_regs
))
184 ret
= put_stack_long(child
, addr
, data
);
185 else if (addr
>= (long) &dummy
->fpu
&&
186 addr
< (long) &dummy
->u_fpvalid
) {
187 set_stopped_child_used_math(child
);
188 ((long *)&child
->thread
.fpu
)
189 [(addr
- (long)&dummy
->fpu
) >> 2] = data
;
191 } else if (addr
== (long) &dummy
->u_fpvalid
) {
192 conditional_stopped_child_used_math(data
, child
);
197 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
198 case PTRACE_CONT
: { /* restart after signal. */
200 if ((unsigned long) data
> _NSIG
)
202 if (request
== PTRACE_SYSCALL
)
203 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
205 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
206 child
->exit_code
= data
;
207 wake_up_process(child
);
213 * make the child exit. Best I can do is send it a sigkill.
214 * perhaps it should be put in the status that it wants to
219 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
221 child
->exit_code
= SIGKILL
;
222 wake_up_process(child
);
226 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
228 struct pt_regs
*dummy
= NULL
;
231 if ((unsigned long) data
> _NSIG
)
233 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
234 if ((child
->ptrace
& PT_DTRACE
) == 0) {
235 /* Spurious delayed TF traps may occur */
236 child
->ptrace
|= PT_DTRACE
;
239 pc
= get_stack_long(child
, (long)&dummy
->pc
);
241 /* Next scheduling will set up UBC */
242 if (child
->thread
.ubc_pc
== 0)
244 child
->thread
.ubc_pc
= pc
;
246 child
->exit_code
= data
;
247 /* give it a chance to run. */
248 wake_up_process(child
);
253 case PTRACE_DETACH
: /* detach a process that was attached. */
254 ret
= ptrace_detach(child
, data
);
258 case PTRACE_GETDSPREGS
: {
262 dp
= ((unsigned long) child
) + THREAD_SIZE
-
263 sizeof(struct pt_dspregs
);
264 if (*((int *) (dp
- 4)) == SR_FD
) {
265 copy_to_user(addr
, (void *) dp
,
266 sizeof(struct pt_dspregs
));
272 case PTRACE_SETDSPREGS
: {
277 dp
= ((unsigned long) child
) + THREAD_SIZE
-
278 sizeof(struct pt_dspregs
);
279 if (*((int *) (dp
- 4)) == SR_FD
) {
280 copy_from_user((void *) dp
, addr
,
281 sizeof(struct pt_dspregs
));
288 ret
= ptrace_request(child
, request
, addr
, data
);
292 put_task_struct(child
);
298 asmlinkage
void do_syscall_trace(void)
300 struct task_struct
*tsk
= current
;
302 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
304 if (!(tsk
->ptrace
& PT_PTRACED
))
306 /* the 0x80 provides a way for the tracing parent to distinguish
307 between a syscall stop and SIGTRAP delivery */
308 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
312 * this isn't the same as continuing with a signal, but it will do
313 * for normal use. strace only continues with a signal if the
314 * stopping signal is not SIGTRAP. -brl
316 if (tsk
->exit_code
) {
317 send_sig(tsk
->exit_code
, tsk
, 1);