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"
22 #include "user_util.h"
25 #include "kern_util.h"
26 #include "chan_user.h"
27 #include "ptrace_user.h"
32 static int tracer_winch[2];
34 int is_tracer_winch(int pid, int fd, void *data)
36 if(pid != os_getpgrp())
39 register_winch_irq(tracer_winch[0], fd, -1, data);
43 static void tracer_winch_handler(int sig)
48 n = os_write_file(tracer_winch[1], &c, sizeof(c));
50 printk("tracer_winch_handler - write failed, err = %d\n", -n);
53 /* Called only by the tracing thread during initialization */
55 static void setup_tracer_winch(void)
59 err = os_pipe(tracer_winch, 1, 1);
61 printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err);
64 signal(SIGWINCH, tracer_winch_handler);
67 void attach_process(int pid)
69 if((ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) ||
70 (ptrace(PTRACE_CONT, pid, 0, 0) < 0))
71 tracer_panic("OP_FORK failed to attach pid");
72 wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
73 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
74 tracer_panic("OP_FORK: PTRACE_SETOPTIONS failed, errno = %d", errno);
75 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
76 tracer_panic("OP_FORK failed to continue process");
79 void tracer_panic(char *format, ...)
90 static void tracer_segv(int sig, struct sigcontext sc)
93 GET_FAULTINFO_FROM_SC(fi, &sc);
94 printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
95 FAULT_ADDRESS(fi), SC_IP(&sc));
100 /* Changed early in boot, and then only read */
103 int debug_parent = 0;
106 static int signal_tramp(void *arg)
110 if(honeypot && munmap((void *) (host_task_size - 0x10000000),
112 panic("Unmapping stack failed");
113 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0)
114 panic("ptrace PTRACE_TRACEME failed");
115 os_stop_process(os_getpid());
116 change_sig(SIGWINCH, 0);
117 signal(SIGUSR1, SIG_IGN);
118 change_sig(SIGCHLD, 0);
119 signal(SIGSEGV, (__sighandler_t) sig_handler);
120 set_cmdline("(idle thread)");
121 set_init_pid(os_getpid());
124 return((*proc)(NULL));
127 static void sleeping_process_signal(int pid, int sig)
130 /* These two result from UML being ^Z-ed and bg-ed. PTRACE_CONT is
131 * right because the process must be in the kernel already.
135 if(ptrace(PTRACE_CONT, pid, 0, sig) < 0)
136 tracer_panic("sleeping_process_signal : Failed to "
137 "continue pid %d, signal = %d, "
138 "errno = %d\n", pid, sig, errno);
141 /* This happens when the debugger (e.g. strace) is doing system call
142 * tracing on the kernel. During a context switch, the current task
143 * will be set to the incoming process and the outgoing process will
144 * hop into write and then read. Since it's not the current process
145 * any more, the trace of those will land here. So, we need to just
148 case (SIGTRAP + 0x80):
149 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
150 tracer_panic("sleeping_process_signal : Failed to "
151 "PTRACE_SYSCALL pid %d, errno = %d\n",
157 tracer_panic("sleeping process %d got unexpected "
158 "signal : %d\n", pid, sig);
163 /* Accessed only by the tracing thread */
164 int debugger_pid = -1;
165 int debugger_parent = -1;
166 int debugger_fd = -1;
174 } signal_record[1024][32];
176 int signal_index[32];
180 extern void signal_usr1(int sig);
182 int tracing_pid = -1;
184 int tracer(int (*init_proc)(void *), void *sp)
187 int status, pid = 0, sig = 0, cont_type, tracing = 0, op = 0;
188 int proc_id = 0, n, err, old_tracing = 0, strace = 0;
189 int local_using_sysemu = 0;
191 signal(SIGPIPE, SIG_IGN);
192 setup_tracer_winch();
193 tracing_pid = os_getpid();
194 printf("tracing thread pid = %d\n", tracing_pid);
196 pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
197 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
199 printf("waitpid on idle thread failed, errno = %d\n", errno);
202 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0) {
203 printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno);
206 if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
207 printf("Failed to continue idle thread, errno = %d\n", errno);
211 signal(SIGSEGV, (sighandler_t) tracer_segv);
212 signal(SIGUSR1, signal_usr1);
214 printf("Tracing thread pausing to be attached\n");
219 debugger_pid = attach_debugger(pid, gdb_pid, 1);
220 else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
222 debugger_parent = os_process_parent(debugger_pid);
223 init_parent_proxy(debugger_parent);
224 err = attach(debugger_parent);
226 printf("Failed to attach debugger parent %d, "
227 "errno = %d\n", debugger_parent, -err);
228 debugger_parent = -1;
231 if(ptrace(PTRACE_SYSCALL, debugger_parent,
233 printf("Failed to continue debugger "
234 "parent, errno = %d\n", errno);
235 debugger_parent = -1;
240 set_cmdline("(tracing thread)");
242 CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
245 printf("wait failed - errno = %d\n", errno);
249 if(pid == debugger_pid){
252 if(WIFEXITED(status) || WIFSIGNALED(status))
254 /* XXX Figure out how to deal with gdb and SMP */
255 else cont = debugger_signal(status, cpu_tasks[0].pid);
256 if(cont == PTRACE_SYSCALL) strace = 1;
259 else if(pid == debugger_parent){
260 debugger_parent_signal(status, pid);
264 if(WIFEXITED(status)) ;
267 printf("Child %d exited with status %d\n", pid,
268 WEXITSTATUS(status));
271 else if(WIFSIGNALED(status)){
272 sig = WTERMSIG(status);
274 printf("Child %d exited with signal %d\n", pid,
278 else if(WIFSTOPPED(status)){
279 proc_id = pid_to_processor_id(pid);
280 sig = WSTOPSIG(status);
282 sleeping_process_signal(pid, sig);
286 task = cpu_tasks[proc_id].task;
287 tracing = is_tracing(task);
288 old_tracing = tracing;
290 /* Assume: no syscall, when coming from user */
297 op = do_proc_op(task, proc_id);
300 * This is called when entering user mode; after
301 * this, we start intercepting syscalls.
303 * In fact, a process is started in kernel mode,
304 * so with is_tracing() == 0 (and that is reset
305 * when executing syscalls, since UML kernel has
306 * the right to do syscalls);
309 arch_leave_kernel(task, pid);
316 os_kill_ptraced_process(pid, 0);
317 /* Now let's reap remaining zombies */
322 } while (errno != ECHILD);
323 return(op == OP_REBOOT);
325 printf("Detaching pid %d\n", pid);
326 detach(pid, SIGSTOP);
331 /* OP_EXEC switches host processes on us,
332 * we want to continue the new one.
334 pid = cpu_tasks[proc_id].pid;
336 case (SIGTRAP + 0x80):
337 if(!tracing && (debugger_pid != -1)){
338 child_signal(pid, status & 0x7fff);
342 /* local_using_sysemu has been already set
343 * below, since if we are here, is_tracing() on
344 * the traced task was 1, i.e. the process had
345 * already run through one iteration of the
346 * loop which executed a OP_TRACE_ON request.*/
347 do_syscall(task, pid, local_using_sysemu);
351 if(!tracing && (debugger_pid != -1)){
352 child_signal(pid, status);
377 set_tracing(task, tracing);
379 if(!tracing && old_tracing)
380 arch_enter_kernel(task, pid);
382 if(!tracing && (debugger_pid != -1) && (sig != 0) &&
383 (sig != SIGALRM) && (sig != SIGVTALRM) &&
384 (sig != SIGSEGV) && (sig != SIGTRAP) &&
385 (sig != SIGUSR2) && (sig != SIGIO) &&
387 child_signal(pid, status);
391 local_using_sysemu = get_using_sysemu();
394 cont_type = SELECT_PTRACE_OPERATION(local_using_sysemu,
395 singlestepping(task));
396 else if((debugger_pid != -1) && strace)
397 cont_type = PTRACE_SYSCALL;
399 cont_type = PTRACE_CONT;
401 if(ptrace(cont_type, pid, 0, sig) != 0){
402 tracer_panic("ptrace failed to continue "
403 "process - errno = %d\n",
411 static int __init uml_debug_setup(char *line, int *add)
417 if(*line != '=') return(0);
421 next = strchr(line, ',');
422 if(next) *next++ = '\0';
424 if(!strcmp(line, "go")) debug_stop = 0;
425 else if(!strcmp(line, "parent")) debug_parent = 1;
426 else printf("Unknown debug option : '%s'\n", line);
433 __uml_setup("debug", uml_debug_setup,
435 " Starts up the kernel under the control of gdb. See the \n"
436 " kernel debugging tutorial and the debugging session pages\n"
437 " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
440 static int __init uml_debugtrace_setup(char *line, int *add)
445 __uml_setup("debugtrace", uml_debugtrace_setup,
447 " Causes the tracing thread to pause until it is attached by a\n"
448 " debugger and continued. This is mostly for debugging crashes\n"
449 " early during boot, and should be pretty much obsoleted by\n"
450 " the debug switch.\n\n"
454 * Overrides for Emacs so that we follow Linus's tabbing style.
455 * Emacs will notice this stuff at the end of the file and automatically
456 * adjust the settings for this buffer only. This must remain at the end
458 * ---------------------------------------------------------------------------
460 * c-file-style: "linux"