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)
50 n = os_write_file(tracer_winch[1], &c, sizeof(c));
52 printk("tracer_winch_handler - write failed, err = %d\n", -n);
55 /* Called only by the tracing thread during initialization */
57 static void setup_tracer_winch(void)
61 err = os_pipe(tracer_winch, 1, 1);
63 printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err);
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, signal = %d, "
134 "errno = %d\n", pid, sig, errno);
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 CATCH_EINTR(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 CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
239 printf("wait failed - errno = %d\n", errno);
243 if(pid == debugger_pid){
246 if(WIFEXITED(status) || WIFSIGNALED(status))
248 /* XXX Figure out how to deal with gdb and SMP */
249 else cont = debugger_signal(status, cpu_tasks[0].pid);
250 if(cont == PTRACE_SYSCALL) strace = 1;
253 else if(pid == debugger_parent){
254 debugger_parent_signal(status, pid);
258 if(WIFEXITED(status)) ;
261 printf("Child %d exited with status %d\n", pid,
262 WEXITSTATUS(status));
265 else if(WIFSIGNALED(status)){
266 sig = WTERMSIG(status);
268 printf("Child %d exited with signal %d\n", pid,
272 else if(WIFSTOPPED(status)){
273 proc_id = pid_to_processor_id(pid);
274 sig = WSTOPSIG(status);
275 if(signal_index[proc_id] == 1024){
276 signal_index[proc_id] = 0;
279 else last_index = signal_index[proc_id] - 1;
280 if(((sig == SIGPROF) || (sig == SIGVTALRM) ||
282 (signal_record[proc_id][last_index].signal == sig)&&
283 (signal_record[proc_id][last_index].pid == pid))
284 signal_index[proc_id] = last_index;
285 signal_record[proc_id][signal_index[proc_id]].pid = pid;
286 gettimeofday(&signal_record[proc_id][signal_index[proc_id]].time, NULL);
287 eip = ptrace(PTRACE_PEEKUSER, pid, PT_IP_OFFSET, 0);
288 signal_record[proc_id][signal_index[proc_id]].addr = eip;
289 signal_record[proc_id][signal_index[proc_id]++].signal = sig;
292 sleeping_process_signal(pid, sig);
296 task = cpu_tasks[proc_id].task;
297 tracing = is_tracing(task);
298 old_tracing = tracing;
303 op = do_proc_op(task, proc_id);
306 arch_leave_kernel(task, pid);
313 ptrace(PTRACE_KILL, pid, 0, 0);
314 return(op == OP_REBOOT);
316 printf("Detaching pid %d\n", pid);
317 detach(pid, SIGSTOP);
322 /* OP_EXEC switches host processes on us,
323 * we want to continue the new one.
325 pid = cpu_tasks[proc_id].pid;
328 if(!tracing && (debugger_pid != -1)){
329 child_signal(pid, status);
333 if(do_syscall(task, pid))
335 else clear_singlestep(task);
356 set_tracing(task, tracing);
358 if(!tracing && old_tracing)
359 arch_enter_kernel(task, pid);
361 if(!tracing && (debugger_pid != -1) && (sig != 0) &&
362 (sig != SIGALRM) && (sig != SIGVTALRM) &&
363 (sig != SIGSEGV) && (sig != SIGTRAP) &&
364 (sig != SIGUSR2) && (sig != SIGIO) &&
366 child_signal(pid, status);
371 if(singlestepping_tt(task))
372 cont_type = PTRACE_SINGLESTEP;
373 else cont_type = PTRACE_SYSCALL;
375 else cont_type = PTRACE_CONT;
377 if((cont_type == PTRACE_CONT) &&
378 (debugger_pid != -1) && strace)
379 cont_type = PTRACE_SYSCALL;
381 if(ptrace(cont_type, pid, 0, sig) != 0){
382 tracer_panic("ptrace failed to continue "
383 "process - errno = %d\n",
391 static int __init uml_debug_setup(char *line, int *add)
397 if(*line != '=') return(0);
401 next = strchr(line, ',');
402 if(next) *next++ = '\0';
404 if(!strcmp(line, "go")) debug_stop = 0;
405 else if(!strcmp(line, "parent")) debug_parent = 1;
406 else printf("Unknown debug option : '%s'\n", line);
413 __uml_setup("debug", uml_debug_setup,
415 " Starts up the kernel under the control of gdb. See the \n"
416 " kernel debugging tutorial and the debugging session pages\n"
417 " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
420 static int __init uml_debugtrace_setup(char *line, int *add)
425 __uml_setup("debugtrace", uml_debugtrace_setup,
427 " Causes the tracing thread to pause until it is attached by a\n"
428 " debugger and continued. This is mostly for debugging crashes\n"
429 " early during boot, and should be pretty much obsoleted by\n"
430 " the debug switch.\n\n"
433 static int __init uml_honeypot_setup(char *line, int *add)
439 __uml_setup("honeypot", uml_honeypot_setup,
441 " This makes UML put process stacks in the same location as they are\n"
442 " on the host, allowing expoits such as stack smashes to work against\n"
443 " UML. This implies 'jail'.\n\n"
447 * Overrides for Emacs so that we follow Linus's tabbing style.
448 * Emacs will notice this stuff at the end of the file and automatically
449 * adjust the settings for this buffer only. This must remain at the end
451 * ---------------------------------------------------------------------------
453 * c-file-style: "linux"