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 */
27 * Get contents of register REGNO in task TASK.
29 inline long get_reg(struct task_struct
*task
, unsigned int regno
)
31 /* USP is a special case, it's not in the pt_regs struct but
32 * in the tasks thread struct
36 return task
->thread
.usp
;
37 else if (regno
< PT_MAX
)
38 return ((unsigned long *)user_regs(task
->thread_info
))[regno
];
44 * Write contents of register REGNO in task TASK.
46 inline int put_reg(struct task_struct
*task
, unsigned int regno
,
50 task
->thread
.usp
= data
;
51 else if (regno
< PT_MAX
)
52 ((unsigned long *)user_regs(task
->thread_info
))[regno
] = data
;
59 * Called by kernel/ptrace.c when detaching.
61 * Make sure the single step bit is not set.
64 ptrace_disable(struct task_struct
*child
)
66 /* Todo - pending singlesteps? */
70 * Note that this implementation of ptrace behaves differently from vanilla
71 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
72 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
73 * ignored. Instead, the data variable is expected to point at a location
74 * (in user space) where the result of the ptrace call is written (instead of
78 sys_ptrace(long request
, long pid
, long addr
, long data
)
80 struct task_struct
*child
;
82 unsigned long __user
*datap
= (unsigned long __user
*)data
;
87 if (request
== PTRACE_TRACEME
) {
88 if (current
->ptrace
& PT_PTRACED
)
91 current
->ptrace
|= PT_PTRACED
;
97 read_lock(&tasklist_lock
);
98 child
= find_task_by_pid(pid
);
101 get_task_struct(child
);
103 read_unlock(&tasklist_lock
);
110 if (pid
== 1) /* Leave the init process alone! */
113 if (request
== PTRACE_ATTACH
) {
114 ret
= ptrace_attach(child
);
118 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
123 /* Read word at location address. */
124 case PTRACE_PEEKTEXT
:
125 case PTRACE_PEEKDATA
: {
129 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
132 if (copied
!= sizeof(tmp
))
135 ret
= put_user(tmp
,datap
);
139 /* Read the word at location address in the USER area. */
140 case PTRACE_PEEKUSR
: {
144 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
147 tmp
= get_reg(child
, addr
>> 2);
148 ret
= put_user(tmp
, datap
);
152 /* Write the word at location address. */
153 case PTRACE_POKETEXT
:
154 case PTRACE_POKEDATA
:
157 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
163 /* Write the word at location address in the USER area. */
166 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
171 if (addr
== PT_DCCR
) {
172 /* don't allow the tracing process to change stuff like
173 * interrupt enable, kernel/user bit, dma enables etc.
176 data
|= get_reg(child
, PT_DCCR
) & ~DCCR_MASK
;
178 if (put_reg(child
, addr
, data
))
187 if ((unsigned long) data
> _NSIG
)
190 if (request
== PTRACE_SYSCALL
) {
191 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
194 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
197 child
->exit_code
= data
;
199 /* TODO: make sure any pending breakpoint is killed */
200 wake_up_process(child
);
205 /* Make the child exit by sending it a sigkill. */
209 if (child
->state
== TASK_ZOMBIE
)
212 child
->exit_code
= SIGKILL
;
214 /* TODO: make sure any pending breakpoint is killed */
215 wake_up_process(child
);
218 /* Set the trap flag. */
219 case PTRACE_SINGLESTEP
:
222 if ((unsigned long) data
> _NSIG
)
225 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
227 /* TODO: set some clever breakpoint mechanism... */
229 child
->exit_code
= data
;
230 wake_up_process(child
);
235 ret
= ptrace_detach(child
, data
);
238 /* Get all GP registers from the child. */
239 case PTRACE_GETREGS
: {
243 for (i
= 0; i
<= PT_MAX
; i
++) {
244 tmp
= get_reg(child
, i
);
246 if (put_user(tmp
, datap
)) {
251 data
+= sizeof(long);
258 /* Set all GP registers in the child. */
259 case PTRACE_SETREGS
: {
263 for (i
= 0; i
<= PT_MAX
; i
++) {
264 if (get_user(tmp
, datap
)) {
271 tmp
|= get_reg(child
, PT_DCCR
) & ~DCCR_MASK
;
274 put_reg(child
, i
, tmp
);
275 data
+= sizeof(long);
283 ret
= ptrace_request(child
, request
, addr
, data
);
287 put_task_struct(child
);
293 void do_syscall_trace(void)
295 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
298 if (!(current
->ptrace
& PT_PTRACED
))
301 /* the 0x80 provides a way for the tracing parent to distinguish
302 between a syscall stop and SIGTRAP delivery */
303 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
307 * This isn't the same as continuing with a signal, but it will do for
310 if (current
->exit_code
) {
311 send_sig(current
->exit_code
, current
, 1);
312 current
->exit_code
= 0;