2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
18 #include "sysdep/ptrace.h"
19 #include "sigcontext.h"
20 #include "sysdep/sigcontext.h"
24 #include "kern_util.h"
25 #include "chan_user.h"
26 #include "ptrace_user.h"
31 static int tracer_winch
[2];
33 int is_tracer_winch(int pid
, int fd
, void *data
)
35 if(pid
!= os_getpgrp())
38 register_winch_irq(tracer_winch
[0], fd
, -1, data
);
42 static void tracer_winch_handler(int sig
)
47 n
= os_write_file(tracer_winch
[1], &c
, sizeof(c
));
49 printk("tracer_winch_handler - write failed, err = %d\n", -n
);
52 /* Called only by the tracing thread during initialization */
54 static void setup_tracer_winch(void)
58 err
= os_pipe(tracer_winch
, 1, 1);
60 printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err
);
63 signal(SIGWINCH
, tracer_winch_handler
);
66 void attach_process(int pid
)
68 if((ptrace(PTRACE_ATTACH
, pid
, 0, 0) < 0) ||
69 (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0))
70 tracer_panic("OP_FORK failed to attach pid");
71 wait_for_stop(pid
, SIGSTOP
, PTRACE_CONT
, NULL
);
72 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
73 tracer_panic("OP_FORK: PTRACE_SETOPTIONS failed, errno = %d", errno
);
74 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
75 tracer_panic("OP_FORK failed to continue process");
78 void tracer_panic(char *format
, ...)
89 static void tracer_segv(int sig
, struct sigcontext sc
)
92 GET_FAULTINFO_FROM_SC(fi
, &sc
);
93 printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
94 FAULT_ADDRESS(fi
), SC_IP(&sc
));
99 /* Changed early in boot, and then only read */
102 int debug_parent
= 0;
105 static int signal_tramp(void *arg
)
109 if(honeypot
&& munmap((void *) (host_task_size
- 0x10000000),
111 panic("Unmapping stack failed");
112 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0)
113 panic("ptrace PTRACE_TRACEME failed");
114 os_stop_process(os_getpid());
115 change_sig(SIGWINCH
, 0);
116 signal(SIGUSR1
, SIG_IGN
);
117 change_sig(SIGCHLD
, 0);
118 signal(SIGSEGV
, (__sighandler_t
) sig_handler
);
119 set_cmdline("(idle thread)");
120 set_init_pid(os_getpid());
123 return((*proc
)(NULL
));
126 static void sleeping_process_signal(int pid
, int sig
)
129 /* These two result from UML being ^Z-ed and bg-ed. PTRACE_CONT is
130 * right because the process must be in the kernel already.
134 if(ptrace(PTRACE_CONT
, pid
, 0, sig
) < 0)
135 tracer_panic("sleeping_process_signal : Failed to "
136 "continue pid %d, signal = %d, "
137 "errno = %d\n", pid
, sig
, errno
);
140 /* This happens when the debugger (e.g. strace) is doing system call
141 * tracing on the kernel. During a context switch, the current task
142 * will be set to the incoming process and the outgoing process will
143 * hop into write and then read. Since it's not the current process
144 * any more, the trace of those will land here. So, we need to just
147 case (SIGTRAP
+ 0x80):
148 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
149 tracer_panic("sleeping_process_signal : Failed to "
150 "PTRACE_SYSCALL pid %d, errno = %d\n",
156 tracer_panic("sleeping process %d got unexpected "
157 "signal : %d\n", pid
, sig
);
162 /* Accessed only by the tracing thread */
163 int debugger_pid
= -1;
164 int debugger_parent
= -1;
165 int debugger_fd
= -1;
173 } signal_record
[1024][32];
175 int signal_index
[32];
179 extern void signal_usr1(int sig
);
181 int tracing_pid
= -1;
183 int tracer(int (*init_proc
)(void *), void *sp
)
186 int status
, pid
= 0, sig
= 0, cont_type
, tracing
= 0, op
= 0;
187 int proc_id
= 0, n
, err
, old_tracing
= 0, strace
= 0;
188 int local_using_sysemu
= 0;
190 signal(SIGPIPE
, SIG_IGN
);
191 setup_tracer_winch();
192 tracing_pid
= os_getpid();
193 printf("tracing thread pid = %d\n", tracing_pid
);
195 pid
= clone(signal_tramp
, sp
, CLONE_FILES
| SIGCHLD
, init_proc
);
196 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
198 printf("waitpid on idle thread failed, errno = %d\n", errno
);
201 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0) {
202 printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno
);
205 if((ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)){
206 printf("Failed to continue idle thread, errno = %d\n", errno
);
210 signal(SIGSEGV
, (sighandler_t
) tracer_segv
);
211 signal(SIGUSR1
, signal_usr1
);
213 printf("Tracing thread pausing to be attached\n");
218 debugger_pid
= attach_debugger(pid
, gdb_pid
, 1);
219 else debugger_pid
= init_ptrace_proxy(pid
, 1, debug_stop
);
221 debugger_parent
= os_process_parent(debugger_pid
);
222 init_parent_proxy(debugger_parent
);
223 err
= attach(debugger_parent
);
225 printf("Failed to attach debugger parent %d, "
226 "errno = %d\n", debugger_parent
, -err
);
227 debugger_parent
= -1;
230 if(ptrace(PTRACE_SYSCALL
, debugger_parent
,
232 printf("Failed to continue debugger "
233 "parent, errno = %d\n", errno
);
234 debugger_parent
= -1;
239 set_cmdline("(tracing thread)");
241 CATCH_EINTR(pid
= waitpid(-1, &status
, WUNTRACED
));
244 printf("wait failed - errno = %d\n", errno
);
248 if(pid
== debugger_pid
){
251 if(WIFEXITED(status
) || WIFSIGNALED(status
))
253 /* XXX Figure out how to deal with gdb and SMP */
254 else cont
= debugger_signal(status
, cpu_tasks
[0].pid
);
255 if(cont
== PTRACE_SYSCALL
) strace
= 1;
258 else if(pid
== debugger_parent
){
259 debugger_parent_signal(status
, pid
);
263 if(WIFEXITED(status
)) ;
266 printf("Child %d exited with status %d\n", pid
,
267 WEXITSTATUS(status
));
270 else if(WIFSIGNALED(status
)){
271 sig
= WTERMSIG(status
);
273 printf("Child %d exited with signal %d\n", pid
,
277 else if(WIFSTOPPED(status
)){
278 proc_id
= pid_to_processor_id(pid
);
279 sig
= WSTOPSIG(status
);
281 sleeping_process_signal(pid
, sig
);
285 task
= cpu_tasks
[proc_id
].task
;
286 tracing
= is_tracing(task
);
287 old_tracing
= tracing
;
289 /* Assume: no syscall, when coming from user */
296 op
= do_proc_op(task
, proc_id
);
299 * This is called when entering user mode; after
300 * this, we start intercepting syscalls.
302 * In fact, a process is started in kernel mode,
303 * so with is_tracing() == 0 (and that is reset
304 * when executing syscalls, since UML kernel has
305 * the right to do syscalls);
308 arch_leave_kernel(task
, pid
);
315 os_kill_ptraced_process(pid
, 0);
316 /* Now let's reap remaining zombies */
321 } while (errno
!= ECHILD
);
322 return(op
== OP_REBOOT
);
324 printf("Detaching pid %d\n", pid
);
325 detach(pid
, SIGSTOP
);
330 /* OP_EXEC switches host processes on us,
331 * we want to continue the new one.
333 pid
= cpu_tasks
[proc_id
].pid
;
335 case (SIGTRAP
+ 0x80):
336 if(!tracing
&& (debugger_pid
!= -1)){
337 child_signal(pid
, status
& 0x7fff);
341 /* local_using_sysemu has been already set
342 * below, since if we are here, is_tracing() on
343 * the traced task was 1, i.e. the process had
344 * already run through one iteration of the
345 * loop which executed a OP_TRACE_ON request.*/
346 do_syscall(task
, pid
, local_using_sysemu
);
350 if(!tracing
&& (debugger_pid
!= -1)){
351 child_signal(pid
, status
);
376 set_tracing(task
, tracing
);
378 if(!tracing
&& old_tracing
)
379 arch_enter_kernel(task
, pid
);
381 if(!tracing
&& (debugger_pid
!= -1) && (sig
!= 0) &&
382 (sig
!= SIGALRM
) && (sig
!= SIGVTALRM
) &&
383 (sig
!= SIGSEGV
) && (sig
!= SIGTRAP
) &&
384 (sig
!= SIGUSR2
) && (sig
!= SIGIO
) &&
386 child_signal(pid
, status
);
390 local_using_sysemu
= get_using_sysemu();
393 cont_type
= SELECT_PTRACE_OPERATION(local_using_sysemu
,
394 singlestepping(task
));
395 else if((debugger_pid
!= -1) && strace
)
396 cont_type
= PTRACE_SYSCALL
;
398 cont_type
= PTRACE_CONT
;
400 if(ptrace(cont_type
, pid
, 0, sig
) != 0){
401 tracer_panic("ptrace failed to continue "
402 "process - errno = %d\n",
410 static int __init
uml_debug_setup(char *line
, int *add
)
416 if(*line
!= '=') return(0);
420 next
= strchr(line
, ',');
421 if(next
) *next
++ = '\0';
423 if(!strcmp(line
, "go")) debug_stop
= 0;
424 else if(!strcmp(line
, "parent")) debug_parent
= 1;
425 else printf("Unknown debug option : '%s'\n", line
);
432 __uml_setup("debug", uml_debug_setup
,
434 " Starts up the kernel under the control of gdb. See the \n"
435 " kernel debugging tutorial and the debugging session pages\n"
436 " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
439 static int __init
uml_debugtrace_setup(char *line
, int *add
)
444 __uml_setup("debugtrace", uml_debugtrace_setup
,
446 " Causes the tracing thread to pause until it is attached by a\n"
447 " debugger and continued. This is mostly for debugging crashes\n"
448 " early during boot, and should be pretty much obsoleted by\n"
449 " the debug switch.\n\n"
453 * Overrides for Emacs so that we follow Linus's tabbing style.
454 * Emacs will notice this stuff at the end of the file and automatically
455 * adjust the settings for this buffer only. This must remain at the end
457 * ---------------------------------------------------------------------------
459 * c-file-style: "linux"