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);
63 if (pid == 1) /* you may not mess with init */
66 if (request == PTRACE_ATTACH) {
67 ret = ptrace_attach(child);
71 ret = ptrace_check_attach(child, request == PTRACE_KILL);
76 /* when I and D space are separate, these will need to be fixed. */
77 case PTRACE_PEEKTEXT: /* read word at location addr. */
78 case PTRACE_PEEKDATA: {
83 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
84 if (copied != sizeof(tmp))
86 ret = put_user(tmp,(unsigned long *) data);
90 /* read the word at location addr in the USER area. */
91 case PTRACE_PEEKUSR: {
95 if ((addr & 3) || addr < 0)
98 tmp = 0; /* Default return condition */
99 if(addr < FRAME_SIZE_OFFSET){
100 tmp = getreg(child, addr);
102 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
103 (addr <= offsetof(struct user, u_debugreg[7]))){
104 addr -= offsetof(struct user, u_debugreg[0]);
106 tmp = child->thread.arch.debugregs[addr];
108 ret = put_user(tmp, (unsigned long *) data);
112 /* when I and D space are separate, this will have to be fixed. */
113 case PTRACE_POKETEXT: /* write the word at location addr. */
114 case PTRACE_POKEDATA:
116 if (access_process_vm(child, addr, &data, sizeof(data),
122 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
124 if ((addr & 3) || addr < 0)
127 if (addr < FRAME_SIZE_OFFSET) {
128 ret = putreg(child, addr, data);
131 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
132 (addr <= offsetof(struct user, u_debugreg[7]))){
133 addr -= offsetof(struct user, u_debugreg[0]);
135 if((addr == 4) || (addr == 5)) break;
136 child->thread.arch.debugregs[addr] = data;
142 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
143 case PTRACE_CONT: { /* restart after signal. */
145 if ((unsigned long) data > _NSIG)
147 if (request == PTRACE_SYSCALL) {
148 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
151 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
153 child->exit_code = data;
154 wake_up_process(child);
160 * make the child exit. Best I can do is send it a sigkill.
161 * perhaps it should be put in the status that it wants to
166 if (child->state == TASK_ZOMBIE) /* already dead */
168 child->exit_code = SIGKILL;
169 wake_up_process(child);
173 case PTRACE_SINGLESTEP: { /* set the trap flag. */
175 if ((unsigned long) data > _NSIG)
177 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
178 child->ptrace |= PT_DTRACE;
179 child->exit_code = data;
180 /* give it a chance to run. */
181 wake_up_process(child);
187 /* detach a process that was attached. */
188 ret = ptrace_detach(child, data);
191 #ifdef PTRACE_GETREGS
192 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
193 if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
194 FRAME_SIZE_OFFSET)) {
198 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
199 __put_user(getreg(child, i), (unsigned long *) data);
200 data += sizeof(long);
206 #ifdef PTRACE_SETREGS
207 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
208 unsigned long tmp = 0;
209 if (!access_ok(VERIFY_READ, (unsigned *)data,
210 FRAME_SIZE_OFFSET)) {
214 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
215 __get_user(tmp, (unsigned long *) data);
216 putreg(child, i, tmp);
217 data += sizeof(long);
223 #ifdef PTRACE_GETFPREGS
224 case PTRACE_GETFPREGS: /* Get the child FPU state. */
225 ret = get_fpregs(data, child);
228 #ifdef PTRACE_SETFPREGS
229 case PTRACE_SETFPREGS: /* Set the child FPU state. */
230 ret = set_fpregs(data, child);
233 #ifdef PTRACE_GETFPXREGS
234 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
235 ret = get_fpxregs(data, child);
238 #ifdef PTRACE_SETFPXREGS
239 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
240 ret = set_fpxregs(data, child);
243 case PTRACE_FAULTINFO: {
244 struct ptrace_faultinfo fault;
246 fault = ((struct ptrace_faultinfo)
247 { .is_write = child->thread.err,
248 .addr = child->thread.cr2 });
249 ret = copy_to_user((unsigned long *) data, &fault,
255 case PTRACE_SIGPENDING:
256 ret = copy_to_user((unsigned long *) data,
257 &child->pending.signal,
258 sizeof(child->pending.signal));
262 struct ptrace_ldt ldt;
264 if(copy_from_user(&ldt, (unsigned long *) data,
270 /* This one is confusing, so just punt and return -EIO for
276 #ifdef CONFIG_PROC_MM
277 case PTRACE_SWITCH_MM: {
278 struct mm_struct *old = child->mm;
279 struct mm_struct *new = proc_mm_get_mm(data);
286 atomic_inc(&new->mm_users);
288 child->active_mm = new;
299 put_task_struct(child);
305 void syscall_trace(union uml_pt_regs *regs, int entryexit)
307 if (unlikely(current->audit_context)) {
309 audit_syscall_entry(current, regs->orig_eax,
310 regs->ebx, regs->ecx,
311 regs->edx, regs->esi);
313 audit_syscall_exit(current, regs->eax);
316 if (!test_thread_flag(TIF_SYSCALL_TRACE))
318 if (!(current->ptrace & PT_PTRACED))
321 /* the 0x80 provides a way for the tracing parent to distinguish
322 between a syscall stop and SIGTRAP delivery */
323 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
327 * this isn't the same as continuing with a signal, but it will do
328 * for normal use. strace only continues with a signal if the
329 * stopping signal is not SIGTRAP. -brl
331 if (current->exit_code) {
332 send_sig(current->exit_code, current, 1);
333 current->exit_code = 0;
338 * Overrides for Emacs so that we follow Linus's tabbing style.
339 * Emacs will notice this stuff at the end of the file and automatically
340 * adjust the settings for this buffer only. This must remain at the end
342 * ---------------------------------------------------------------------------
344 * c-file-style: "linux"