2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <sys/ptrace.h>
19 #include "sysdep/ptrace.h"
20 #include "sigcontext.h"
21 #include "sysdep/sigcontext.h"
23 #include "signal_user.h"
24 #include "user_util.h"
27 #include "kern_util.h"
29 #include "chan_user.h"
30 #include "ptrace_user.h"
34 static int tracer_winch[2];
36 int is_tracer_winch(int pid, int fd, void *data)
38 if(pid != tracing_pid)
41 register_winch_irq(tracer_winch[0], fd, -1, data);
45 static void tracer_winch_handler(int sig)
49 if(write(tracer_winch[1], &c, sizeof(c)) != sizeof(c))
50 printk("tracer_winch_handler - write failed, errno = %d\n",
54 /* Called only by the tracing thread during initialization */
56 static void setup_tracer_winch(void)
60 err = os_pipe(tracer_winch, 1, 1);
62 printk("setup_tracer_winch : os_pipe failed, errno = %d\n",
66 signal(SIGWINCH, tracer_winch_handler);
69 void attach_process(int pid)
71 if((ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) ||
72 (ptrace(PTRACE_CONT, pid, 0, 0) < 0))
73 tracer_panic("OP_FORK failed to attach pid");
74 wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
75 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
76 tracer_panic("OP_FORK failed to continue process");
79 void tracer_panic(char *format, ...)
89 static void tracer_segv(int sig, struct sigcontext sc)
91 printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
92 SC_FAULT_ADDR(&sc), SC_IP(&sc));
97 /* Changed early in boot, and then only read */
100 int debug_parent = 0;
103 static int signal_tramp(void *arg)
107 if(honeypot && munmap((void *) (host_task_size - 0x10000000),
109 panic("Unmapping stack failed");
110 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0)
111 panic("ptrace PTRACE_TRACEME failed");
112 os_stop_process(os_getpid());
113 change_sig(SIGWINCH, 0);
114 signal(SIGUSR1, SIG_IGN);
115 change_sig(SIGCHLD, 0);
116 signal(SIGSEGV, (__sighandler_t) sig_handler);
117 set_cmdline("(idle thread)");
118 set_init_pid(os_getpid());
120 return((*proc)(NULL));
123 static void sleeping_process_signal(int pid, int sig)
126 /* These two result from UML being ^Z-ed and bg-ed. PTRACE_CONT is
127 * right because the process must be in the kernel already.
131 if(ptrace(PTRACE_CONT, pid, 0, sig) < 0)
132 tracer_panic("sleeping_process_signal : Failed to "
133 "continue pid %d, errno = %d\n", pid,
137 /* This happens when the debugger (e.g. strace) is doing system call
138 * tracing on the kernel. During a context switch, the current task
139 * will be set to the incoming process and the outgoing process will
140 * hop into write and then read. Since it's not the current process
141 * any more, the trace of those will land here. So, we need to just
145 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
146 tracer_panic("sleeping_process_signal : Failed to "
147 "PTRACE_SYSCALL pid %d, errno = %d\n",
153 tracer_panic("sleeping process %d got unexpected "
154 "signal : %d\n", pid, sig);
159 /* Accessed only by the tracing thread */
160 int debugger_pid = -1;
161 int debugger_parent = -1;
162 int debugger_fd = -1;
170 } signal_record[1024][32];
172 int signal_index[32];
175 extern int io_nsignals, io_count, intr_count;
177 extern void signal_usr1(int sig);
179 int tracing_pid = -1;
181 int tracer(int (*init_proc)(void *), void *sp)
184 unsigned long eip = 0;
185 int status, pid = 0, sig = 0, cont_type, tracing = 0, op = 0;
186 int last_index, proc_id = 0, n, err, old_tracing = 0, strace = 0;
188 capture_signal_stack();
189 signal(SIGPIPE, SIG_IGN);
190 setup_tracer_winch();
191 tracing_pid = os_getpid();
192 printf("tracing thread pid = %d\n", tracing_pid);
194 pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
195 n = waitpid(pid, &status, WUNTRACED);
197 printf("waitpid on idle thread failed, errno = %d\n", errno);
200 if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
201 printf("Failed to continue idle thread, errno = %d\n", errno);
205 signal(SIGSEGV, (sighandler_t) tracer_segv);
206 signal(SIGUSR1, signal_usr1);
208 printf("Tracing thread pausing to be attached\n");
213 debugger_pid = attach_debugger(pid, gdb_pid, 1);
214 else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
216 debugger_parent = os_process_parent(debugger_pid);
217 init_parent_proxy(debugger_parent);
218 err = attach(debugger_parent);
220 printf("Failed to attach debugger parent %d, "
221 "errno = %d\n", debugger_parent, err);
222 debugger_parent = -1;
225 if(ptrace(PTRACE_SYSCALL, debugger_parent,
227 printf("Failed to continue debugger "
228 "parent, errno = %d\n", errno);
229 debugger_parent = -1;
234 set_cmdline("(tracing thread)");
236 if((pid = waitpid(-1, &status, WUNTRACED)) <= 0){
238 printf("wait failed - errno = %d\n", errno);
242 if(pid == debugger_pid){
245 if(WIFEXITED(status) || WIFSIGNALED(status))
247 /* XXX Figure out how to deal with gdb and SMP */
248 else cont = debugger_signal(status, cpu_tasks[0].pid);
249 if(cont == PTRACE_SYSCALL) strace = 1;
252 else if(pid == debugger_parent){
253 debugger_parent_signal(status, pid);
257 if(WIFEXITED(status)) ;
260 printf("Child %d exited with status %d\n", pid,
261 WEXITSTATUS(status));
264 else if(WIFSIGNALED(status)){
265 sig = WTERMSIG(status);
267 printf("Child %d exited with signal %d\n", pid,
271 else if(WIFSTOPPED(status)){
272 proc_id = pid_to_processor_id(pid);
273 sig = WSTOPSIG(status);
274 if(signal_index[proc_id] == 1024){
275 signal_index[proc_id] = 0;
278 else last_index = signal_index[proc_id] - 1;
279 if(((sig == SIGPROF) || (sig == SIGVTALRM) ||
281 (signal_record[proc_id][last_index].signal == sig)&&
282 (signal_record[proc_id][last_index].pid == pid))
283 signal_index[proc_id] = last_index;
284 signal_record[proc_id][signal_index[proc_id]].pid = pid;
285 gettimeofday(&signal_record[proc_id][signal_index[proc_id]].time, NULL);
286 eip = ptrace(PTRACE_PEEKUSER, pid, PT_IP_OFFSET, 0);
287 signal_record[proc_id][signal_index[proc_id]].addr = eip;
288 signal_record[proc_id][signal_index[proc_id]++].signal = sig;
291 sleeping_process_signal(pid, sig);
295 task = cpu_tasks[proc_id].task;
296 tracing = is_tracing(task);
297 old_tracing = tracing;
302 op = do_proc_op(task, proc_id);
305 arch_leave_kernel(task, pid);
312 ptrace(PTRACE_KILL, pid, 0, 0);
313 return(op == OP_REBOOT);
315 printf("Detaching pid %d\n", pid);
316 detach(pid, SIGSTOP);
321 /* OP_EXEC switches host processes on us,
322 * we want to continue the new one.
324 pid = cpu_tasks[proc_id].pid;
327 if(!tracing && (debugger_pid != -1)){
328 child_signal(pid, status);
332 if(do_syscall(task, pid)) sig = SIGUSR2;
333 else clear_singlestep(task);
354 set_tracing(task, tracing);
356 if(!tracing && old_tracing)
357 arch_enter_kernel(task, pid);
359 if(!tracing && (debugger_pid != -1) && (sig != 0) &&
360 (sig != SIGALRM) && (sig != SIGVTALRM) &&
361 (sig != SIGSEGV) && (sig != SIGTRAP) &&
362 (sig != SIGUSR2) && (sig != SIGIO) &&
364 child_signal(pid, status);
369 if(singlestepping_tt(task))
370 cont_type = PTRACE_SINGLESTEP;
371 else cont_type = PTRACE_SYSCALL;
373 else cont_type = PTRACE_CONT;
375 if((cont_type == PTRACE_CONT) &&
376 (debugger_pid != -1) && strace)
377 cont_type = PTRACE_SYSCALL;
379 if(ptrace(cont_type, pid, 0, sig) != 0){
380 tracer_panic("ptrace failed to continue "
381 "process - errno = %d\n",
389 static int __init uml_debug_setup(char *line, int *add)
395 if(*line != '=') return(0);
399 next = strchr(line, ',');
400 if(next) *next++ = '\0';
402 if(!strcmp(line, "go")) debug_stop = 0;
403 else if(!strcmp(line, "parent")) debug_parent = 1;
404 else printk("Unknown debug option : '%s'\n", line);
411 __uml_setup("debug", uml_debug_setup,
413 " Starts up the kernel under the control of gdb. See the \n"
414 " kernel debugging tutorial and the debugging session pages\n"
415 " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
418 static int __init uml_debugtrace_setup(char *line, int *add)
423 __uml_setup("debugtrace", uml_debugtrace_setup,
425 " Causes the tracing thread to pause until it is attached by a\n"
426 " debugger and continued. This is mostly for debugging crashes\n"
427 " early during boot, and should be pretty much obsoleted by\n"
428 " the debug switch.\n\n"
431 static int __init uml_honeypot_setup(char *line, int *add)
437 __uml_setup("honeypot", uml_honeypot_setup,
439 " This makes UML put process stacks in the same location as they are\n"
440 " on the host, allowing expoits such as stack smashes to work against\n"
441 " UML. This implies 'jail'.\n\n"
445 * Overrides for Emacs so that we follow Linus's tabbing style.
446 * Emacs will notice this stuff at the end of the file and automatically
447 * adjust the settings for this buffer only. This must remain at the end
449 * ---------------------------------------------------------------------------
451 * c-file-style: "linux"