vserver 1.9.3
[linux-2.6.git] / arch / um / kernel / tt / tracer.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <signal.h>
11 #include <errno.h>
12 #include <sched.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/ptrace.h>
16 #include <sys/time.h>
17 #include <sys/wait.h>
18 #include "user.h"
19 #include "sysdep/ptrace.h"
20 #include "sigcontext.h"
21 #include "sysdep/sigcontext.h"
22 #include "os.h"
23 #include "signal_user.h"
24 #include "user_util.h"
25 #include "mem_user.h"
26 #include "process.h"
27 #include "kern_util.h"
28 #include "frame.h"
29 #include "chan_user.h"
30 #include "ptrace_user.h"
31 #include "mode.h"
32 #include "tt.h"
33
34 static int tracer_winch[2];
35
36 int is_tracer_winch(int pid, int fd, void *data)
37 {
38         if(pid != tracing_pid)
39                 return(0);
40
41         register_winch_irq(tracer_winch[0], fd, -1, data);
42         return(1);
43 }
44
45 static void tracer_winch_handler(int sig)
46 {
47         int n;
48         char c = 1;
49
50         n = os_write_file(tracer_winch[1], &c, sizeof(c));
51         if(n != sizeof(c))
52                 printk("tracer_winch_handler - write failed, err = %d\n", -n);
53 }
54
55 /* Called only by the tracing thread during initialization */
56
57 static void setup_tracer_winch(void)
58 {
59         int err;
60
61         err = os_pipe(tracer_winch, 1, 1);
62         if(err < 0){
63                 printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err);
64                 return;
65         }
66         signal(SIGWINCH, tracer_winch_handler);
67 }
68
69 void attach_process(int pid)
70 {
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");
77 }
78
79 void tracer_panic(char *format, ...)
80 {
81         va_list ap;
82
83         va_start(ap, format);
84         vprintf(format, ap);
85         printf("\n");
86         while(1) pause();
87 }
88
89 static void tracer_segv(int sig, struct sigcontext sc)
90 {
91         printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
92                SC_FAULT_ADDR(&sc), SC_IP(&sc));
93         while(1)
94                 pause();
95 }
96
97 /* Changed early in boot, and then only read */
98 int debug = 0;
99 int debug_stop = 1;
100 int debug_parent = 0;
101 int honeypot = 0;
102
103 static int signal_tramp(void *arg)
104 {
105         int (*proc)(void *);
106
107         if(honeypot && munmap((void *) (host_task_size - 0x10000000),
108                               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());
119         proc = arg;
120         return((*proc)(NULL));
121 }
122
123 static void sleeping_process_signal(int pid, int sig)
124 {
125         switch(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.
128          */
129         case SIGCONT:
130         case SIGTSTP:
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);
135                 break;
136
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 
142          * PTRACE_SYSCALL it.
143          */
144         case SIGTRAP:
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",
148                                      pid, errno);
149                 break;
150         case SIGSTOP:
151                 break;
152         default:
153                 tracer_panic("sleeping process %d got unexpected "
154                              "signal : %d\n", pid, sig);
155                 break;
156         }
157 }
158
159 /* Accessed only by the tracing thread */
160 int debugger_pid = -1;
161 int debugger_parent = -1;
162 int debugger_fd = -1;
163 int gdb_pid = -1;
164
165 struct {
166         int pid;
167         int signal;
168         unsigned long addr;
169         struct timeval time;
170 } signal_record[1024][32];
171
172 int signal_index[32];
173 int nsignals = 0;
174 int debug_trace = 0;
175 extern int io_nsignals, io_count, intr_count;
176
177 extern void signal_usr1(int sig);
178
179 int tracing_pid = -1;
180
181 int tracer(int (*init_proc)(void *), void *sp)
182 {
183         void *task = NULL;
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;
187
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);
193
194         pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
195         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
196         if(n < 0){
197                 printf("waitpid on idle thread failed, errno = %d\n", errno);
198                 exit(1);
199         }
200         if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
201                 printf("Failed to continue idle thread, errno = %d\n", errno);
202                 exit(1);
203         }
204
205         signal(SIGSEGV, (sighandler_t) tracer_segv);
206         signal(SIGUSR1, signal_usr1);
207         if(debug_trace){
208                 printf("Tracing thread pausing to be attached\n");
209                 stop();
210         }
211         if(debug){
212                 if(gdb_pid != -1) 
213                         debugger_pid = attach_debugger(pid, gdb_pid, 1);
214                 else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
215                 if(debug_parent){
216                         debugger_parent = os_process_parent(debugger_pid);
217                         init_parent_proxy(debugger_parent);
218                         err = attach(debugger_parent);
219                         if(err){
220                                 printf("Failed to attach debugger parent %d, "
221                                        "errno = %d\n", debugger_parent, -err);
222                                 debugger_parent = -1;
223                         }
224                         else {
225                                 if(ptrace(PTRACE_SYSCALL, debugger_parent, 
226                                           0, 0) < 0){
227                                         printf("Failed to continue debugger "
228                                                "parent, errno = %d\n", errno);
229                                         debugger_parent = -1;
230                                 }
231                         }
232                 }
233         }
234         set_cmdline("(tracing thread)");
235         while(1){
236                 CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
237                 if(pid <= 0){
238                         if(errno != ECHILD){
239                                 printf("wait failed - errno = %d\n", errno);
240                         }
241                         continue;
242                 }
243                 if(pid == debugger_pid){
244                         int cont = 0;
245
246                         if(WIFEXITED(status) || WIFSIGNALED(status))
247                                 debugger_pid = -1;
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;
251                         continue;
252                 }
253                 else if(pid == debugger_parent){
254                         debugger_parent_signal(status, pid);
255                         continue;
256                 }
257                 nsignals++;
258                 if(WIFEXITED(status)) ;
259 #ifdef notdef
260                 {
261                         printf("Child %d exited with status %d\n", pid, 
262                                WEXITSTATUS(status));
263                 }
264 #endif
265                 else if(WIFSIGNALED(status)){
266                         sig = WTERMSIG(status);
267                         if(sig != 9){
268                                 printf("Child %d exited with signal %d\n", pid,
269                                        sig);
270                         }
271                 }
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;
277                                 last_index = 1023;
278                         }
279                         else last_index = signal_index[proc_id] - 1;
280                         if(((sig == SIGPROF) || (sig == SIGVTALRM) || 
281                             (sig == SIGALRM)) &&
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;
290                         
291                         if(proc_id == -1){
292                                 sleeping_process_signal(pid, sig);
293                                 continue;
294                         }
295
296                         task = cpu_tasks[proc_id].task;
297                         tracing = is_tracing(task);
298                         old_tracing = tracing;
299
300                         switch(sig){
301                         case SIGUSR1:
302                                 sig = 0;
303                                 op = do_proc_op(task, proc_id);
304                                 switch(op){
305                                 case OP_TRACE_ON:
306                                         arch_leave_kernel(task, pid);
307                                         tracing = 1;
308                                         break;
309                                 case OP_REBOOT:
310                                 case OP_HALT:
311                                         unmap_physmem();
312                                         kmalloc_ok = 0;
313                                         ptrace(PTRACE_KILL, pid, 0, 0);
314                                         return(op == OP_REBOOT);
315                                 case OP_NONE:
316                                         printf("Detaching pid %d\n", pid);
317                                         detach(pid, SIGSTOP);
318                                         continue;
319                                 default:
320                                         break;
321                                 }
322                                 /* OP_EXEC switches host processes on us,
323                                  * we want to continue the new one.
324                                  */
325                                 pid = cpu_tasks[proc_id].pid;
326                                 break;
327                         case SIGTRAP:
328                                 if(!tracing && (debugger_pid != -1)){
329                                         child_signal(pid, status);
330                                         continue;
331                                 }
332                                 tracing = 0;
333                                 if(do_syscall(task, pid))
334                                         sig = SIGUSR2;
335                                 else clear_singlestep(task);
336                                 break;
337                         case SIGPROF:
338                                 if(tracing) sig = 0;
339                                 break;
340                         case SIGCHLD:
341                         case SIGHUP:
342                                 sig = 0;
343                                 break;
344                         case SIGSEGV:
345                         case SIGIO:
346                         case SIGALRM:
347                         case SIGVTALRM:
348                         case SIGFPE:
349                         case SIGBUS:
350                         case SIGILL:
351                         case SIGWINCH:
352                         default:
353                                 tracing = 0;
354                                 break;
355                         }
356                         set_tracing(task, tracing);
357
358                         if(!tracing && old_tracing)
359                                 arch_enter_kernel(task, pid);
360
361                         if(!tracing && (debugger_pid != -1) && (sig != 0) &&
362                                 (sig != SIGALRM) && (sig != SIGVTALRM) &&
363                                 (sig != SIGSEGV) && (sig != SIGTRAP) &&
364                                 (sig != SIGUSR2) && (sig != SIGIO) &&
365                                 (sig != SIGFPE)){
366                                 child_signal(pid, status);
367                                 continue;
368                         }
369
370                         if(tracing){
371                                 if(singlestepping_tt(task))
372                                         cont_type = PTRACE_SINGLESTEP;
373                                 else cont_type = PTRACE_SYSCALL;
374                         }
375                         else cont_type = PTRACE_CONT;
376
377                         if((cont_type == PTRACE_CONT) && 
378                            (debugger_pid != -1) && strace)
379                                 cont_type = PTRACE_SYSCALL;
380
381                         if(ptrace(cont_type, pid, 0, sig) != 0){
382                                 tracer_panic("ptrace failed to continue "
383                                              "process - errno = %d\n", 
384                                              errno);
385                         }
386                 }
387         }
388         return(0);
389 }
390
391 static int __init uml_debug_setup(char *line, int *add)
392 {
393         char *next;
394
395         debug = 1;
396         *add = 0;
397         if(*line != '=') return(0);
398         line++;
399
400         while(line != NULL){
401                 next = strchr(line, ',');
402                 if(next) *next++ = '\0';
403                 
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);
407
408                 line = next;
409         }
410         return(0);
411 }
412
413 __uml_setup("debug", uml_debug_setup,
414 "debug\n"
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"
418 );
419
420 static int __init uml_debugtrace_setup(char *line, int *add)
421 {
422         debug_trace = 1;
423         return 0;
424 }
425 __uml_setup("debugtrace", uml_debugtrace_setup,
426 "debugtrace\n"
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"
431 );
432
433 static int __init uml_honeypot_setup(char *line, int *add)
434 {
435         jail_setup("", add);
436         honeypot = 1;
437         return 0;
438 }
439 __uml_setup("honeypot", uml_honeypot_setup, 
440 "honeypot\n"
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"
444 );
445
446 /*
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
450  * of the file.
451  * ---------------------------------------------------------------------------
452  * Local variables:
453  * c-file-style: "linux"
454  * End:
455  */