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>
15 #include <asm/uaccess.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/processor.h>
22 * Determines which bits in DCCR the user has access to.
23 * 1 = access, 0 = no access.
25 #define DCCR_MASK 0x0000001f /* XNZVC */
28 * Get contents of register REGNO in task TASK.
30 inline long get_reg(struct task_struct
*task
, unsigned int regno
)
32 /* USP is a special case, it's not in the pt_regs struct but
33 * in the tasks thread struct
37 return task
->thread
.usp
;
38 else if (regno
< PT_MAX
)
39 return ((unsigned long *)user_regs(task
->thread_info
))[regno
];
45 * Write contents of register REGNO in task TASK.
47 inline int put_reg(struct task_struct
*task
, unsigned int regno
,
51 task
->thread
.usp
= data
;
52 else if (regno
< PT_MAX
)
53 ((unsigned long *)user_regs(task
->thread_info
))[regno
] = data
;
60 * Called by kernel/ptrace.c when detaching.
62 * Make sure the single step bit is not set.
65 ptrace_disable(struct task_struct
*child
)
67 /* Todo - pending singlesteps? */
71 * Note that this implementation of ptrace behaves differently from vanilla
72 * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
73 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
74 * ignored. Instead, the data variable is expected to point at a location
75 * (in user space) where the result of the ptrace call is written (instead of
79 sys_ptrace(long request
, long pid
, long addr
, long data
)
81 struct task_struct
*child
;
83 unsigned long __user
*datap
= (unsigned long __user
*)data
;
88 if (request
== PTRACE_TRACEME
) {
89 if (current
->ptrace
& PT_PTRACED
)
92 current
->ptrace
|= PT_PTRACED
;
98 read_lock(&tasklist_lock
);
99 child
= find_task_by_pid(pid
);
102 get_task_struct(child
);
104 read_unlock(&tasklist_lock
);
111 if (pid
== 1) /* Leave the init process alone! */
114 if (request
== PTRACE_ATTACH
) {
115 ret
= ptrace_attach(child
);
119 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
124 /* Read word at location address. */
125 case PTRACE_PEEKTEXT
:
126 case PTRACE_PEEKDATA
: {
130 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
133 if (copied
!= sizeof(tmp
))
136 ret
= put_user(tmp
,datap
);
140 /* Read the word at location address in the USER area. */
141 case PTRACE_PEEKUSR
: {
145 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
148 tmp
= get_reg(child
, addr
>> 2);
149 ret
= put_user(tmp
, datap
);
153 /* Write the word at location address. */
154 case PTRACE_POKETEXT
:
155 case PTRACE_POKEDATA
:
158 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
164 /* Write the word at location address in the USER area. */
167 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
172 if (addr
== PT_DCCR
) {
173 /* don't allow the tracing process to change stuff like
174 * interrupt enable, kernel/user bit, dma enables etc.
177 data
|= get_reg(child
, PT_DCCR
) & ~DCCR_MASK
;
179 if (put_reg(child
, addr
, data
))
188 if (!valid_signal(data
))
191 if (request
== PTRACE_SYSCALL
) {
192 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
195 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
198 child
->exit_code
= data
;
200 /* TODO: make sure any pending breakpoint is killed */
201 wake_up_process(child
);
206 /* Make the child exit by sending it a sigkill. */
210 if (child
->state
== TASK_ZOMBIE
)
213 child
->exit_code
= SIGKILL
;
215 /* TODO: make sure any pending breakpoint is killed */
216 wake_up_process(child
);
219 /* Set the trap flag. */
220 case PTRACE_SINGLESTEP
:
223 if (!valid_signal(data
))
226 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
228 /* TODO: set some clever breakpoint mechanism... */
230 child
->exit_code
= data
;
231 wake_up_process(child
);
236 ret
= ptrace_detach(child
, data
);
239 /* Get all GP registers from the child. */
240 case PTRACE_GETREGS
: {
244 for (i
= 0; i
<= PT_MAX
; i
++) {
245 tmp
= get_reg(child
, i
);
247 if (put_user(tmp
, datap
)) {
252 data
+= sizeof(long);
259 /* Set all GP registers in the child. */
260 case PTRACE_SETREGS
: {
264 for (i
= 0; i
<= PT_MAX
; i
++) {
265 if (get_user(tmp
, datap
)) {
272 tmp
|= get_reg(child
, PT_DCCR
) & ~DCCR_MASK
;
275 put_reg(child
, i
, tmp
);
276 data
+= sizeof(long);
284 ret
= ptrace_request(child
, request
, addr
, data
);
288 put_task_struct(child
);
294 void do_syscall_trace(void)
296 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
299 if (!(current
->ptrace
& PT_PTRACED
))
302 /* the 0x80 provides a way for the tracing parent to distinguish
303 between a syscall stop and SIGTRAP delivery */
304 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
308 * This isn't the same as continuing with a signal, but it will do for
311 if (current
->exit_code
) {
312 send_sig(current
->exit_code
, current
, 1);
313 current
->exit_code
= 0;