2 * Copyright (C) 2000-2003, Axis Communications AB.
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
9 #include <linux/smp_lock.h>
10 #include <linux/errno.h>
11 #include <linux/ptrace.h>
12 #include <linux/user.h>
14 #include <asm/uaccess.h>
16 #include <asm/pgtable.h>
17 #include <asm/system.h>
18 #include <asm/processor.h>
21 * Determines which bits in DCCR the user has access to.
22 * 1 = access, 0 = no access.
24 #define DCCR_MASK 0x0000001f /* XNZVC */
26 extern inline long get_reg(struct task_struct *, unsigned int);
27 extern inline long put_reg(struct task_struct *, unsigned int, unsigned long);
30 * Called by kernel/ptrace.c when detaching.
32 * Make sure the single step bit is not set.
35 ptrace_disable(struct task_struct *child)
37 /* Todo - pending singlesteps? */
41 * Note that this implementation of ptrace behaves differently from vanilla
42 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
43 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
44 * ignored. Instead, the data variable is expected to point at a location
45 * (in user space) where the result of the ptrace call is written (instead of
49 sys_ptrace(long request, long pid, long addr, long data)
51 struct task_struct *child;
57 if (request == PTRACE_TRACEME) {
58 if (current->ptrace & PT_PTRACED)
61 current->ptrace |= PT_PTRACED;
67 read_lock(&tasklist_lock);
68 child = find_task_by_pid(pid);
71 get_task_struct(child);
73 read_unlock(&tasklist_lock);
80 if (pid == 1) /* Leave the init process alone! */
83 if (request == PTRACE_ATTACH) {
84 ret = ptrace_attach(child);
90 if (!(child->ptrace & PT_PTRACED))
93 if (child->state != TASK_STOPPED) {
94 if (request != PTRACE_KILL)
98 if (child->parent != current)
102 /* Read word at location address. */
103 case PTRACE_PEEKTEXT:
104 case PTRACE_PEEKDATA: {
108 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
111 if (copied != sizeof(tmp))
114 ret = put_user(tmp,(unsigned long *) data);
118 /* Read the word at location address in the USER area. */
119 case PTRACE_PEEKUSR: {
123 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
126 tmp = 0; /* Default return condition */
129 if (addr < sizeof(struct pt_regs)) {
130 tmp = get_reg(child, addr >> 2);
131 ret = put_user(tmp, (unsigned long *)data);
137 /* Write the word at location address. */
138 case PTRACE_POKETEXT:
139 case PTRACE_POKEDATA:
142 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
148 /* Write the word at location address in the USER area. */
152 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
155 if (addr < sizeof(struct pt_regs)) {
158 if (addr == PT_DCCR) {
160 * Don't allow the tracing process to
161 * change stuff like interrupt enable,
162 * kernel/user bit, etc.
165 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
168 if (put_reg(child, addr, data))
179 if ((unsigned long) data > _NSIG)
182 if (request == PTRACE_SYSCALL) {
183 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
186 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
189 child->exit_code = data;
191 /* TODO: make sure any pending breakpoint is killed */
192 wake_up_process(child);
197 /* Make the child exit by sending it a sigkill. */
201 if (child->state == TASK_ZOMBIE)
204 child->exit_code = SIGKILL;
206 /* TODO: make sure any pending breakpoint is killed */
207 wake_up_process(child);
210 /* Set the trap flag. */
211 case PTRACE_SINGLESTEP:
214 if ((unsigned long) data > _NSIG)
217 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
219 /* TODO: set some clever breakpoint mechanism... */
221 child->exit_code = data;
222 wake_up_process(child);
227 ret = ptrace_detach(child, data);
230 /* Get all GP registers from the child. */
231 case PTRACE_GETREGS: {
235 for (i = 0; i <= PT_MAX; i++) {
236 tmp = get_reg(child, i);
238 if (put_user(tmp, (unsigned long *) data)) {
243 data += sizeof(long);
250 /* Set all GP registers in the child. */
251 case PTRACE_SETREGS: {
255 for (i = 0; i <= PT_MAX; i++) {
256 if (get_user(tmp, (unsigned long *) data)) {
263 tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
266 put_reg(child, i, tmp);
267 data += sizeof(long);
275 ret = ptrace_request(child, request, addr, data);
279 put_task_struct(child);
285 void do_syscall_trace(void)
287 if (!test_thread_flag(TIF_SYSCALL_TRACE))
290 if (!(current->ptrace & PT_PTRACED))
293 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
296 current->state = TASK_STOPPED;
297 notify_parent(current, SIGCHLD);
301 * This isn't the same as continuing with a signal, but it will do for
304 if (current->exit_code) {
305 send_sig(current->exit_code, current, 1);
306 current->exit_code = 0;