2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
8 #include "linux/errno.h"
9 #include "linux/smp_lock.h"
10 #include "linux/security.h"
11 #include "linux/ptrace.h"
13 #include "linux/proc_mm.h"
15 #include "asm/ptrace.h"
16 #include "asm/uaccess.h"
17 #include "kern_util.h"
18 #include "ptrace_user.h"
21 * Called by kernel/ptrace.c when detaching..
23 void ptrace_disable(struct task_struct *child)
27 extern long do_mmap2(struct task_struct *task, unsigned long addr,
28 unsigned long len, unsigned long prot,
29 unsigned long flags, unsigned long fd,
32 int sys_ptrace(long request, long pid, long addr, long data)
34 struct task_struct *child;
39 if (request == PTRACE_TRACEME) {
40 /* are we already being traced? */
41 if (current->ptrace & PT_PTRACED)
44 ret = security_ptrace(current->parent, current);
48 /* set the ptrace bit in the process flags. */
49 current->ptrace |= PT_PTRACED;
54 read_lock(&tasklist_lock);
55 child = find_task_by_pid(pid);
57 get_task_struct(child);
58 read_unlock(&tasklist_lock);
61 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
65 if (pid == 1) /* you may not mess with init */
68 if (request == PTRACE_ATTACH) {
69 ret = ptrace_attach(child);
73 ret = ptrace_check_attach(child, request == PTRACE_KILL);
78 /* when I and D space are separate, these will need to be fixed. */
79 case PTRACE_PEEKTEXT: /* read word at location addr. */
80 case PTRACE_PEEKDATA: {
85 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
86 if (copied != sizeof(tmp))
88 ret = put_user(tmp,(unsigned long *) data);
92 /* read the word at location addr in the USER area. */
93 case PTRACE_PEEKUSR: {
97 if ((addr & 3) || addr < 0)
100 tmp = 0; /* Default return condition */
101 if(addr < FRAME_SIZE_OFFSET){
102 tmp = getreg(child, addr);
104 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
105 (addr <= offsetof(struct user, u_debugreg[7]))){
106 addr -= offsetof(struct user, u_debugreg[0]);
108 tmp = child->thread.arch.debugregs[addr];
110 ret = put_user(tmp, (unsigned long *) data);
114 /* when I and D space are separate, this will have to be fixed. */
115 case PTRACE_POKETEXT: /* write the word at location addr. */
116 case PTRACE_POKEDATA:
118 if (access_process_vm(child, addr, &data, sizeof(data),
124 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
126 if ((addr & 3) || addr < 0)
129 if (addr < FRAME_SIZE_OFFSET) {
130 ret = putreg(child, addr, data);
133 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
134 (addr <= offsetof(struct user, u_debugreg[7]))){
135 addr -= offsetof(struct user, u_debugreg[0]);
137 if((addr == 4) || (addr == 5)) break;
138 child->thread.arch.debugregs[addr] = data;
144 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
145 case PTRACE_CONT: { /* restart after signal. */
147 if ((unsigned long) data > _NSIG)
149 if (request == PTRACE_SYSCALL) {
150 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
153 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
155 child->exit_code = data;
156 wake_up_process(child);
162 * make the child exit. Best I can do is send it a sigkill.
163 * perhaps it should be put in the status that it wants to
168 if (child->state == TASK_ZOMBIE) /* already dead */
170 child->exit_code = SIGKILL;
171 wake_up_process(child);
175 case PTRACE_SINGLESTEP: { /* set the trap flag. */
177 if ((unsigned long) data > _NSIG)
179 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
180 child->ptrace |= PT_DTRACE;
181 child->exit_code = data;
182 /* give it a chance to run. */
183 wake_up_process(child);
189 /* detach a process that was attached. */
190 ret = ptrace_detach(child, data);
193 #ifdef PTRACE_GETREGS
194 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
195 if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
196 FRAME_SIZE_OFFSET)) {
200 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
201 __put_user(getreg(child, i), (unsigned long *) data);
202 data += sizeof(long);
208 #ifdef PTRACE_SETREGS
209 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
210 unsigned long tmp = 0;
211 if (!access_ok(VERIFY_READ, (unsigned *)data,
212 FRAME_SIZE_OFFSET)) {
216 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
217 __get_user(tmp, (unsigned long *) data);
218 putreg(child, i, tmp);
219 data += sizeof(long);
225 #ifdef PTRACE_GETFPREGS
226 case PTRACE_GETFPREGS: /* Get the child FPU state. */
227 ret = get_fpregs(data, child);
230 #ifdef PTRACE_SETFPREGS
231 case PTRACE_SETFPREGS: /* Set the child FPU state. */
232 ret = set_fpregs(data, child);
235 #ifdef PTRACE_GETFPXREGS
236 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
237 ret = get_fpxregs(data, child);
240 #ifdef PTRACE_SETFPXREGS
241 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
242 ret = set_fpxregs(data, child);
245 case PTRACE_FAULTINFO: {
246 struct ptrace_faultinfo fault;
248 fault = ((struct ptrace_faultinfo)
249 { .is_write = child->thread.err,
250 .addr = child->thread.cr2 });
251 ret = copy_to_user((unsigned long *) data, &fault,
257 case PTRACE_SIGPENDING:
258 ret = copy_to_user((unsigned long *) data,
259 &child->pending.signal,
260 sizeof(child->pending.signal));
264 struct ptrace_ldt ldt;
266 if(copy_from_user(&ldt, (unsigned long *) data,
272 /* This one is confusing, so just punt and return -EIO for
278 #ifdef CONFIG_PROC_MM
279 case PTRACE_SWITCH_MM: {
280 struct mm_struct *old = child->mm;
281 struct mm_struct *new = proc_mm_get_mm(data);
288 atomic_inc(&new->mm_users);
290 child->active_mm = new;
301 put_task_struct(child);
307 void syscall_trace(void)
309 if (!test_thread_flag(TIF_SYSCALL_TRACE))
311 if (!(current->ptrace & PT_PTRACED))
314 /* the 0x80 provides a way for the tracing parent to distinguish
315 between a syscall stop and SIGTRAP delivery */
316 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
318 current->state = TASK_STOPPED;
319 notify_parent(current, SIGCHLD);
323 * this isn't the same as continuing with a signal, but it will do
324 * for normal use. strace only continues with a signal if the
325 * stopping signal is not SIGTRAP. -brl
327 if (current->exit_code) {
328 send_sig(current->exit_code, current, 1);
329 current->exit_code = 0;
334 * Overrides for Emacs so that we follow Linus's tabbing style.
335 * Emacs will notice this stuff at the end of the file and automatically
336 * adjust the settings for this buffer only. This must remain at the end
338 * ---------------------------------------------------------------------------
340 * c-file-style: "linux"