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);
111 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
115 if (pid == 1) /* you may not mess with init */
118 if (request == PTRACE_ATTACH) {
119 ret = ptrace_attach(child);
123 ret = ptrace_check_attach(child, request == PTRACE_KILL);
128 /* when I and D space are separate, these will need to be fixed. */
129 case PTRACE_PEEKTEXT: /* read word at location addr. */
130 case PTRACE_PEEKDATA: {
134 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
136 if (copied != sizeof(tmp))
138 ret = put_user(tmp,(unsigned long *) data);
142 /* read the word at location addr in the USER area. */
143 case PTRACE_PEEKUSR: {
147 if ((addr & 3) || addr < 0 ||
148 addr > sizeof(struct user) - 3)
151 if (addr < sizeof(struct pt_regs))
152 tmp = get_stack_long(child, addr);
153 else if (addr >= (long) &dummy->fpu &&
154 addr < (long) &dummy->u_fpvalid) {
155 if (!tsk_used_math(child)) {
156 if (addr == (long)&dummy->fpu.fpscr)
161 tmp = ((long *)&child->thread.fpu)
162 [(addr - (long)&dummy->fpu) >> 2];
163 } else if (addr == (long) &dummy->u_fpvalid)
164 tmp = !!tsk_used_math(child);
167 ret = put_user(tmp, (unsigned long *)data);
171 /* when I and D space are separate, this will have to be fixed. */
172 case PTRACE_POKETEXT: /* write the word at location addr. */
173 case PTRACE_POKEDATA:
175 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
180 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
182 if ((addr & 3) || addr < 0 ||
183 addr > sizeof(struct user) - 3)
186 if (addr < sizeof(struct pt_regs))
187 ret = put_stack_long(child, addr, data);
188 else if (addr >= (long) &dummy->fpu &&
189 addr < (long) &dummy->u_fpvalid) {
190 set_stopped_child_used_math(child);
191 ((long *)&child->thread.fpu)
192 [(addr - (long)&dummy->fpu) >> 2] = data;
194 } else if (addr == (long) &dummy->u_fpvalid) {
195 conditional_stopped_child_used_math(data, child);
200 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
201 case PTRACE_CONT: { /* restart after signal. */
203 if (!valid_signal(data))
205 if (request == PTRACE_SYSCALL)
206 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
208 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
209 child->exit_code = data;
210 wake_up_process(child);
216 * make the child exit. Best I can do is send it a sigkill.
217 * perhaps it should be put in the status that it wants to
222 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
224 child->exit_code = SIGKILL;
225 wake_up_process(child);
229 case PTRACE_SINGLESTEP: { /* set the trap flag. */
231 struct pt_regs *dummy = NULL;
234 if (!valid_signal(data))
236 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
237 if ((child->ptrace & PT_DTRACE) == 0) {
238 /* Spurious delayed TF traps may occur */
239 child->ptrace |= PT_DTRACE;
242 pc = get_stack_long(child, (long)&dummy->pc);
244 /* Next scheduling will set up UBC */
245 if (child->thread.ubc_pc == 0)
247 child->thread.ubc_pc = pc;
249 child->exit_code = data;
250 /* give it a chance to run. */
251 wake_up_process(child);
256 case PTRACE_DETACH: /* detach a process that was attached. */
257 ret = ptrace_detach(child, data);
261 case PTRACE_GETDSPREGS: {
265 dp = ((unsigned long) child) + THREAD_SIZE -
266 sizeof(struct pt_dspregs);
267 if (*((int *) (dp - 4)) == SR_FD) {
268 copy_to_user(addr, (void *) dp,
269 sizeof(struct pt_dspregs));
275 case PTRACE_SETDSPREGS: {
280 dp = ((unsigned long) child) + THREAD_SIZE -
281 sizeof(struct pt_dspregs);
282 if (*((int *) (dp - 4)) == SR_FD) {
283 copy_from_user((void *) dp, addr,
284 sizeof(struct pt_dspregs));
291 ret = ptrace_request(child, request, addr, data);
295 put_task_struct(child);
301 asmlinkage void do_syscall_trace(void)
303 struct task_struct *tsk = current;
305 if (!test_thread_flag(TIF_SYSCALL_TRACE))
307 if (!(tsk->ptrace & PT_PTRACED))
309 /* the 0x80 provides a way for the tracing parent to distinguish
310 between a syscall stop and SIGTRAP delivery */
311 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
315 * this isn't the same as continuing with a signal, but it will do
316 * for normal use. strace only continues with a signal if the
317 * stopping signal is not SIGTRAP. -brl
319 if (tsk->exit_code) {
320 send_sig(tsk->exit_code, tsk, 1);