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>
13 #include <linux/signal.h>
14 #include <linux/security.h>
16 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
20 #include <asm/processor.h>
23 * Determines which bits in DCCR the user has access to.
24 * 1 = access, 0 = no access.
26 #define DCCR_MASK 0x0000001f /* XNZVC */
29 * Get contents of register REGNO in task TASK.
31 inline long get_reg(struct task_struct *task, unsigned int regno)
33 /* USP is a special case, it's not in the pt_regs struct but
34 * in the tasks thread struct
38 return task->thread.usp;
39 else if (regno < PT_MAX)
40 return ((unsigned long *)user_regs(task->thread_info))[regno];
46 * Write contents of register REGNO in task TASK.
48 inline int put_reg(struct task_struct *task, unsigned int regno,
52 task->thread.usp = data;
53 else if (regno < PT_MAX)
54 ((unsigned long *)user_regs(task->thread_info))[regno] = data;
61 * Called by kernel/ptrace.c when detaching.
63 * Make sure the single step bit is not set.
66 ptrace_disable(struct task_struct *child)
68 /* Todo - pending singlesteps? */
72 * Note that this implementation of ptrace behaves differently from vanilla
73 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
74 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
75 * ignored. Instead, the data variable is expected to point at a location
76 * (in user space) where the result of the ptrace call is written (instead of
80 sys_ptrace(long request, long pid, long addr, long data)
82 struct task_struct *child;
84 unsigned long __user *datap = (unsigned long __user *)data;
89 if (request == PTRACE_TRACEME) {
90 /* are we already being traced? */
91 if (current->ptrace & PT_PTRACED)
93 ret = security_ptrace(current->parent, current);
96 /* set the ptrace bit in the process flags. */
97 current->ptrace |= PT_PTRACED;
103 read_lock(&tasklist_lock);
104 child = find_task_by_pid(pid);
107 get_task_struct(child);
109 read_unlock(&tasklist_lock);
116 if (pid == 1) /* Leave the init process alone! */
119 if (request == PTRACE_ATTACH) {
120 ret = ptrace_attach(child);
124 ret = ptrace_check_attach(child, request == PTRACE_KILL);
129 /* Read word at location address. */
130 case PTRACE_PEEKTEXT:
131 case PTRACE_PEEKDATA: {
135 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
138 if (copied != sizeof(tmp))
141 ret = put_user(tmp,datap);
145 /* Read the word at location address in the USER area. */
146 case PTRACE_PEEKUSR: {
150 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
153 tmp = get_reg(child, addr >> 2);
154 ret = put_user(tmp, datap);
158 /* Write the word at location address. */
159 case PTRACE_POKETEXT:
160 case PTRACE_POKEDATA:
163 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
169 /* Write the word at location address in the USER area. */
172 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
177 if (addr == PT_DCCR) {
178 /* don't allow the tracing process to change stuff like
179 * interrupt enable, kernel/user bit, dma enables etc.
182 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
184 if (put_reg(child, addr, data))
193 if (!valid_signal(data))
196 if (request == PTRACE_SYSCALL) {
197 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
200 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
203 child->exit_code = data;
205 /* TODO: make sure any pending breakpoint is killed */
206 wake_up_process(child);
211 /* Make the child exit by sending it a sigkill. */
215 if (child->exit_state == EXIT_ZOMBIE)
218 child->exit_code = SIGKILL;
220 /* TODO: make sure any pending breakpoint is killed */
221 wake_up_process(child);
224 /* Set the trap flag. */
225 case PTRACE_SINGLESTEP:
228 if (!valid_signal(data))
231 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
233 /* TODO: set some clever breakpoint mechanism... */
235 child->exit_code = data;
236 wake_up_process(child);
241 ret = ptrace_detach(child, data);
244 /* Get all GP registers from the child. */
245 case PTRACE_GETREGS: {
249 for (i = 0; i <= PT_MAX; i++) {
250 tmp = get_reg(child, i);
252 if (put_user(tmp, datap)) {
257 data += sizeof(long);
264 /* Set all GP registers in the child. */
265 case PTRACE_SETREGS: {
269 for (i = 0; i <= PT_MAX; i++) {
270 if (get_user(tmp, datap)) {
277 tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
280 put_reg(child, i, tmp);
281 data += sizeof(long);
289 ret = ptrace_request(child, request, addr, data);
293 put_task_struct(child);
299 void do_syscall_trace(void)
301 if (!test_thread_flag(TIF_SYSCALL_TRACE))
304 if (!(current->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 for
316 if (current->exit_code) {
317 send_sig(current->exit_code, current, 1);
318 current->exit_code = 0;