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"
12 #include "linux/audit.h"
14 #include "linux/proc_mm.h"
16 #include "asm/ptrace.h"
17 #include "asm/uaccess.h"
18 #include "kern_util.h"
19 #include "skas_ptrace.h"
20 #include "sysdep/ptrace.h"
22 static inline void set_singlestepping(struct task_struct *child, int on)
25 child->ptrace |= PT_DTRACE;
27 child->ptrace &= ~PT_DTRACE;
28 child->thread.singlestep_syscall = 0;
30 #ifdef SUBARCH_SET_SINGLESTEPPING
31 SUBARCH_SET_SINGLESTEPPING(child, on);
36 * Called by kernel/ptrace.c when detaching..
38 void ptrace_disable(struct task_struct *child)
40 set_singlestepping(child,0);
43 extern int peek_user(struct task_struct * child, long addr, long data);
44 extern int poke_user(struct task_struct * child, long addr, long data);
46 long sys_ptrace(long request, long pid, long addr, long data)
48 struct task_struct *child;
53 if (request == PTRACE_TRACEME) {
54 /* are we already being traced? */
55 if (current->ptrace & PT_PTRACED)
58 ret = security_ptrace(current->parent, current);
62 /* set the ptrace bit in the process flags. */
63 current->ptrace |= PT_PTRACED;
68 read_lock(&tasklist_lock);
69 child = find_task_by_pid(pid);
71 get_task_struct(child);
72 read_unlock(&tasklist_lock);
75 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
79 if (pid == 1) /* you may not mess with init */
82 if (request == PTRACE_ATTACH) {
83 ret = ptrace_attach(child);
87 #ifdef SUBACH_PTRACE_SPECIAL
88 SUBARCH_PTRACE_SPECIAL(child,request,addr,data);
91 ret = ptrace_check_attach(child, request == PTRACE_KILL);
96 /* when I and D space are separate, these will need to be fixed. */
97 case PTRACE_PEEKTEXT: /* read word at location addr. */
98 case PTRACE_PEEKDATA: {
103 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
104 if (copied != sizeof(tmp))
106 ret = put_user(tmp, (unsigned long __user *) data);
110 /* read the word at location addr in the USER area. */
112 ret = peek_user(child, addr, data);
115 /* when I and D space are separate, this will have to be fixed. */
116 case PTRACE_POKETEXT: /* write the word at location addr. */
117 case PTRACE_POKEDATA:
119 if (access_process_vm(child, addr, &data, sizeof(data),
125 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
126 ret = poke_user(child, addr, data);
129 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
130 case PTRACE_CONT: { /* restart after signal. */
132 if (!valid_signal(data))
135 set_singlestepping(child, 0);
136 if (request == PTRACE_SYSCALL) {
137 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
140 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
142 child->exit_code = data;
143 wake_up_process(child);
149 * make the child exit. Best I can do is send it a sigkill.
150 * perhaps it should be put in the status that it wants to
155 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
158 set_singlestepping(child, 0);
159 child->exit_code = SIGKILL;
160 wake_up_process(child);
164 case PTRACE_SINGLESTEP: { /* set the trap flag. */
166 if (!valid_signal(data))
168 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
169 set_singlestepping(child, 1);
170 child->exit_code = data;
171 /* give it a chance to run. */
172 wake_up_process(child);
178 /* detach a process that was attached. */
179 ret = ptrace_detach(child, data);
182 #ifdef PTRACE_GETREGS
183 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
184 if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
189 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
190 __put_user(getreg(child, i),
191 (unsigned long __user *) data);
192 data += sizeof(long);
198 #ifdef PTRACE_SETREGS
199 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
200 unsigned long tmp = 0;
201 if (!access_ok(VERIFY_READ, (unsigned *)data,
206 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
207 __get_user(tmp, (unsigned long __user *) data);
208 putreg(child, i, tmp);
209 data += sizeof(long);
215 #ifdef PTRACE_GETFPREGS
216 case PTRACE_GETFPREGS: /* Get the child FPU state. */
217 ret = get_fpregs(data, child);
220 #ifdef PTRACE_SETFPREGS
221 case PTRACE_SETFPREGS: /* Set the child FPU state. */
222 ret = set_fpregs(data, child);
225 #ifdef PTRACE_GETFPXREGS
226 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
227 ret = get_fpxregs(data, child);
230 #ifdef PTRACE_SETFPXREGS
231 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
232 ret = set_fpxregs(data, child);
235 case PTRACE_FAULTINFO: {
236 /* Take the info from thread->arch->faultinfo,
237 * but transfer max. sizeof(struct ptrace_faultinfo).
238 * On i386, ptrace_faultinfo is smaller!
240 ret = copy_to_user((unsigned long __user *) data,
241 &child->thread.arch.faultinfo,
242 sizeof(struct ptrace_faultinfo));
250 struct ptrace_ldt ldt;
252 if(copy_from_user(&ldt, (unsigned long __user *) data,
258 /* This one is confusing, so just punt and return -EIO for
265 #ifdef CONFIG_PROC_MM
266 case PTRACE_SWITCH_MM: {
267 struct mm_struct *old = child->mm;
268 struct mm_struct *new = proc_mm_get_mm(data);
275 atomic_inc(&new->mm_users);
277 child->active_mm = new;
284 ret = ptrace_request(child, request, addr, data);
288 put_task_struct(child);
294 void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
299 memset(&info, 0, sizeof(info));
300 info.si_signo = SIGTRAP;
301 info.si_code = TRAP_BRKPT;
304 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
306 /* Send us the fakey SIGTRAP */
307 force_sig_info(SIGTRAP, &info, tsk);
310 /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
311 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
313 void syscall_trace(union uml_pt_regs *regs, int entryexit)
315 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
318 if (unlikely(current->audit_context)) {
320 audit_syscall_entry(current,
322 UPT_SYSCALL_NR(regs),
323 UPT_SYSCALL_ARG1(regs),
324 UPT_SYSCALL_ARG2(regs),
325 UPT_SYSCALL_ARG3(regs),
326 UPT_SYSCALL_ARG4(regs));
327 else audit_syscall_exit(current,
328 AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
329 UPT_SYSCALL_RET(regs));
332 /* Fake a debug trap */
334 send_sigtrap(current, regs, 0);
336 if (!test_thread_flag(TIF_SYSCALL_TRACE))
339 if (!(current->ptrace & PT_PTRACED))
342 /* the 0x80 provides a way for the tracing parent to distinguish
343 between a syscall stop and SIGTRAP delivery */
344 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
345 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
347 if (entryexit) /* force do_signal() --> is_syscall() */
348 set_thread_flag(TIF_SIGPENDING);
350 /* this isn't the same as continuing with a signal, but it will do
351 * for normal use. strace only continues with a signal if the
352 * stopping signal is not SIGTRAP. -brl
354 if (current->exit_code) {
355 send_sig(current->exit_code, current, 1);
356 current->exit_code = 0;