2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/audit.h"
7 #include "linux/ptrace.h"
8 #include "linux/sched.h"
9 #include "asm/uaccess.h"
13 #include "skas_ptrace.h"
15 static inline void set_singlestepping(struct task_struct
*child
, int on
)
18 child
->ptrace
|= PT_DTRACE
;
20 child
->ptrace
&= ~PT_DTRACE
;
21 child
->thread
.singlestep_syscall
= 0;
23 #ifdef SUBARCH_SET_SINGLESTEPPING
24 SUBARCH_SET_SINGLESTEPPING(child
, on
);
29 * Called by kernel/ptrace.c when detaching..
31 void ptrace_disable(struct task_struct
*child
)
33 set_singlestepping(child
,0);
36 extern int peek_user(struct task_struct
* child
, long addr
, long data
);
37 extern int poke_user(struct task_struct
* child
, long addr
, long data
);
39 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
42 unsigned long __user
*p
= (void __user
*)(unsigned long)data
;
45 /* read word at location addr. */
48 ret
= generic_ptrace_peekdata(child
, addr
, data
);
51 /* read the word at location addr in the USER area. */
53 ret
= peek_user(child
, addr
, data
);
56 /* write the word at location addr. */
59 ret
= generic_ptrace_pokedata(child
, addr
, data
);
62 /* write the word at location addr in the USER area */
64 ret
= poke_user(child
, addr
, data
);
67 /* continue and stop at next (return from) syscall */
69 /* restart after signal. */
72 if (!valid_signal(data
))
75 set_singlestepping(child
, 0);
76 if (request
== PTRACE_SYSCALL
)
77 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
78 else clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
79 child
->exit_code
= data
;
80 wake_up_process(child
);
86 * make the child exit. Best I can do is send it a sigkill.
87 * perhaps it should be put in the status that it wants to
92 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
95 set_singlestepping(child
, 0);
96 child
->exit_code
= SIGKILL
;
97 wake_up_process(child
);
101 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
103 if (!valid_signal(data
))
105 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
106 set_singlestepping(child
, 1);
107 child
->exit_code
= data
;
108 /* give it a chance to run. */
109 wake_up_process(child
);
114 #ifdef PTRACE_GETREGS
115 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
116 if (!access_ok(VERIFY_WRITE
, p
, MAX_REG_OFFSET
)) {
120 for ( i
= 0; i
< MAX_REG_OFFSET
; i
+= sizeof(long) ) {
121 __put_user(getreg(child
, i
), p
);
128 #ifdef PTRACE_SETREGS
129 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
130 unsigned long tmp
= 0;
131 if (!access_ok(VERIFY_READ
, p
, MAX_REG_OFFSET
)) {
135 for ( i
= 0; i
< MAX_REG_OFFSET
; i
+= sizeof(long) ) {
137 putreg(child
, i
, tmp
);
144 #ifdef PTRACE_GETFPREGS
145 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
146 ret
= get_fpregs(data
, child
);
149 #ifdef PTRACE_SETFPREGS
150 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
151 ret
= set_fpregs(data
, child
);
154 #ifdef PTRACE_GETFPXREGS
155 case PTRACE_GETFPXREGS
: /* Get the child FPU state. */
156 ret
= get_fpxregs(data
, child
);
159 #ifdef PTRACE_SETFPXREGS
160 case PTRACE_SETFPXREGS
: /* Set the child FPU state. */
161 ret
= set_fpxregs(data
, child
);
164 case PTRACE_GET_THREAD_AREA
:
165 ret
= ptrace_get_thread_area(child
, addr
,
166 (struct user_desc __user
*) data
);
169 case PTRACE_SET_THREAD_AREA
:
170 ret
= ptrace_set_thread_area(child
, addr
,
171 (struct user_desc __user
*) data
);
174 case PTRACE_FAULTINFO
: {
176 * Take the info from thread->arch->faultinfo,
177 * but transfer max. sizeof(struct ptrace_faultinfo).
178 * On i386, ptrace_faultinfo is smaller!
180 ret
= copy_to_user(p
, &child
->thread
.arch
.faultinfo
,
181 sizeof(struct ptrace_faultinfo
));
189 struct ptrace_ldt ldt
;
191 if (copy_from_user(&ldt
, p
, sizeof(ldt
))) {
197 * This one is confusing, so just punt and return -EIO for
204 #ifdef CONFIG_PROC_MM
205 case PTRACE_SWITCH_MM
: {
206 struct mm_struct
*old
= child
->mm
;
207 struct mm_struct
*new = proc_mm_get_mm(data
);
214 atomic_inc(&new->mm_users
);
216 child
->active_mm
= new;
222 #ifdef PTRACE_ARCH_PRCTL
223 case PTRACE_ARCH_PRCTL
:
224 /* XXX Calls ptrace on the host - needs some SMP thinking */
225 ret
= arch_prctl(child
, data
, (void *) addr
);
229 ret
= ptrace_request(child
, request
, addr
, data
);
236 void send_sigtrap(struct task_struct
*tsk
, struct uml_pt_regs
*regs
,
241 memset(&info
, 0, sizeof(info
));
242 info
.si_signo
= SIGTRAP
;
243 info
.si_code
= TRAP_BRKPT
;
246 info
.si_addr
= UPT_IS_USER(regs
) ? (void __user
*) UPT_IP(regs
) : NULL
;
248 /* Send us the fakey SIGTRAP */
249 force_sig_info(SIGTRAP
, &info
, tsk
);
253 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
254 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
256 void syscall_trace(struct uml_pt_regs
*regs
, int entryexit
)
258 int is_singlestep
= (current
->ptrace
& PT_DTRACE
) && entryexit
;
261 if (unlikely(current
->audit_context
)) {
263 audit_syscall_entry(HOST_AUDIT_ARCH
,
264 UPT_SYSCALL_NR(regs
),
265 UPT_SYSCALL_ARG1(regs
),
266 UPT_SYSCALL_ARG2(regs
),
267 UPT_SYSCALL_ARG3(regs
),
268 UPT_SYSCALL_ARG4(regs
));
269 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs
)),
270 UPT_SYSCALL_RET(regs
));
273 /* Fake a debug trap */
275 send_sigtrap(current
, regs
, 0);
277 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
280 if (!(current
->ptrace
& PT_PTRACED
))
284 * the 0x80 provides a way for the tracing parent to distinguish
285 * between a syscall stop and SIGTRAP delivery
287 tracesysgood
= (current
->ptrace
& PT_TRACESYSGOOD
);
288 ptrace_notify(SIGTRAP
| (tracesysgood
? 0x80 : 0));
290 if (entryexit
) /* force do_signal() --> is_syscall() */
291 set_thread_flag(TIF_SIGPENDING
);
294 * this isn't the same as continuing with a signal, but it will do
295 * for normal use. strace only continues with a signal if the
296 * stopping signal is not SIGTRAP. -brl
298 if (current
->exit_code
) {
299 send_sig(current
->exit_code
, current
, 1);
300 current
->exit_code
= 0;