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>
23 #include <linux/signal.h>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/processor.h>
30 #include <asm/mmu_context.h>
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
38 * This routine will get a word off of the process kernel stack.
40 static inline int get_stack_long(struct task_struct *task, int offset)
44 stack = (unsigned char *)
45 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
47 - sizeof(struct pt_dspregs)
49 - sizeof(unsigned long);
51 return (*((int *)stack));
55 * This routine will put a word on the process kernel stack.
57 static inline int put_stack_long(struct task_struct *task, int offset,
62 stack = (unsigned char *)
63 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
65 - sizeof(struct pt_dspregs)
67 - sizeof(unsigned long);
69 *(unsigned long *) stack = data;
74 * Called by kernel/ptrace.c when detaching..
76 * Make sure single step bits etc are not set.
78 void ptrace_disable(struct task_struct *child)
83 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
85 struct task_struct *child;
86 struct user * dummy = NULL;
91 if (request == PTRACE_TRACEME) {
92 /* are we already being traced? */
93 if (current->ptrace & PT_PTRACED)
95 ret = security_ptrace(current->parent, current);
98 /* set the ptrace bit in the process flags. */
99 current->ptrace |= PT_PTRACED;
104 read_lock(&tasklist_lock);
105 child = find_task_by_pid(pid);
107 get_task_struct(child);
108 read_unlock(&tasklist_lock);
113 if (pid == 1) /* you may not mess with init */
116 if (request == PTRACE_ATTACH) {
117 ret = ptrace_attach(child);
121 ret = ptrace_check_attach(child, request == PTRACE_KILL);
126 /* when I and D space are separate, these will need to be fixed. */
127 case PTRACE_PEEKTEXT: /* read word at location addr. */
128 case PTRACE_PEEKDATA: {
132 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
134 if (copied != sizeof(tmp))
136 ret = put_user(tmp,(unsigned long *) data);
140 /* read the word at location addr in the USER area. */
141 case PTRACE_PEEKUSR: {
145 if ((addr & 3) || addr < 0 ||
146 addr > sizeof(struct user) - 3)
149 if (addr < sizeof(struct pt_regs))
150 tmp = get_stack_long(child, addr);
151 else if (addr >= (long) &dummy->fpu &&
152 addr < (long) &dummy->u_fpvalid) {
153 if (!tsk_used_math(child)) {
154 if (addr == (long)&dummy->fpu.fpscr)
159 tmp = ((long *)&child->thread.fpu)
160 [(addr - (long)&dummy->fpu) >> 2];
161 } else if (addr == (long) &dummy->u_fpvalid)
162 tmp = !!tsk_used_math(child);
165 ret = put_user(tmp, (unsigned long *)data);
169 /* when I and D space are separate, this will have to be fixed. */
170 case PTRACE_POKETEXT: /* write the word at location addr. */
171 case PTRACE_POKEDATA:
173 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
178 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
180 if ((addr & 3) || addr < 0 ||
181 addr > sizeof(struct user) - 3)
184 if (addr < sizeof(struct pt_regs))
185 ret = put_stack_long(child, addr, data);
186 else if (addr >= (long) &dummy->fpu &&
187 addr < (long) &dummy->u_fpvalid) {
188 set_stopped_child_used_math(child);
189 ((long *)&child->thread.fpu)
190 [(addr - (long)&dummy->fpu) >> 2] = data;
192 } else if (addr == (long) &dummy->u_fpvalid) {
193 conditional_stopped_child_used_math(data, child);
198 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
199 case PTRACE_CONT: { /* restart after signal. */
201 if (!valid_signal(data))
203 if (request == PTRACE_SYSCALL)
204 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
206 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
207 child->exit_code = data;
208 wake_up_process(child);
214 * make the child exit. Best I can do is send it a sigkill.
215 * perhaps it should be put in the status that it wants to
220 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
222 child->exit_code = SIGKILL;
223 wake_up_process(child);
227 case PTRACE_SINGLESTEP: { /* set the trap flag. */
229 struct pt_regs *dummy = NULL;
232 if (!valid_signal(data))
234 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
235 if ((child->ptrace & PT_DTRACE) == 0) {
236 /* Spurious delayed TF traps may occur */
237 child->ptrace |= PT_DTRACE;
240 pc = get_stack_long(child, (long)&dummy->pc);
242 /* Next scheduling will set up UBC */
243 if (child->thread.ubc_pc == 0)
245 child->thread.ubc_pc = pc;
247 child->exit_code = data;
248 /* give it a chance to run. */
249 wake_up_process(child);
254 case PTRACE_DETACH: /* detach a process that was attached. */
255 ret = ptrace_detach(child, data);
259 case PTRACE_GETDSPREGS: {
263 dp = ((unsigned long) child) + THREAD_SIZE -
264 sizeof(struct pt_dspregs);
265 if (*((int *) (dp - 4)) == SR_FD) {
266 copy_to_user(addr, (void *) dp,
267 sizeof(struct pt_dspregs));
273 case PTRACE_SETDSPREGS: {
278 dp = ((unsigned long) child) + THREAD_SIZE -
279 sizeof(struct pt_dspregs);
280 if (*((int *) (dp - 4)) == SR_FD) {
281 copy_from_user((void *) dp, addr,
282 sizeof(struct pt_dspregs));
289 ret = ptrace_request(child, request, addr, data);
293 put_task_struct(child);
299 asmlinkage void do_syscall_trace(void)
301 struct task_struct *tsk = current;
303 if (!test_thread_flag(TIF_SYSCALL_TRACE))
305 if (!(tsk->ptrace & PT_PTRACED))
307 /* the 0x80 provides a way for the tracing parent to distinguish
308 between a syscall stop and SIGTRAP delivery */
309 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
313 * this isn't the same as continuing with a signal, but it will do
314 * for normal use. strace only continues with a signal if the
315 * stopping signal is not SIGTRAP. -brl
317 if (tsk->exit_code) {
318 send_sig(tsk->exit_code, tsk, 1);