ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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(0);
43 }
44
45 static void tracer_winch_handler(int sig)
46 {
47         char c = 1;
48
49         if(write(tracer_winch[1], &c, sizeof(c)) != sizeof(c))
50                 printk("tracer_winch_handler - write failed, errno = %d\n",
51                        errno);
52 }
53
54 /* Called only by the tracing thread during initialization */
55
56 static void setup_tracer_winch(void)
57 {
58         int err;
59
60         err = os_pipe(tracer_winch, 1, 1);
61         if(err){
62                 printk("setup_tracer_winch : os_pipe failed, errno = %d\n", 
63                        -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, errno = %d\n", pid,
134                                      sig);
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, sig);
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         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                 if((pid = waitpid(-1, &status, WUNTRACED)) <= 0){
237                         if(errno != ECHILD){
238                                 printf("wait failed - errno = %d\n", errno);
239                         }
240                         continue;
241                 }
242                 if(pid == debugger_pid){
243                         int cont = 0;
244
245                         if(WIFEXITED(status) || WIFSIGNALED(status))
246                                 debugger_pid = -1;
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;
250                         continue;
251                 }
252                 else if(pid == debugger_parent){
253                         debugger_parent_signal(status, pid);
254                         continue;
255                 }
256                 nsignals++;
257                 if(WIFEXITED(status)) ;
258 #ifdef notdef
259                 {
260                         printf("Child %d exited with status %d\n", pid, 
261                                WEXITSTATUS(status));
262                 }
263 #endif
264                 else if(WIFSIGNALED(status)){
265                         sig = WTERMSIG(status);
266                         if(sig != 9){
267                                 printf("Child %d exited with signal %d\n", pid,
268                                        sig);
269                         }
270                 }
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;
276                                 last_index = 1023;
277                         }
278                         else last_index = signal_index[proc_id] - 1;
279                         if(((sig == SIGPROF) || (sig == SIGVTALRM) || 
280                             (sig == SIGALRM)) &&
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;
289                         
290                         if(proc_id == -1){
291                                 sleeping_process_signal(pid, sig);
292                                 continue;
293                         }
294
295                         task = cpu_tasks[proc_id].task;
296                         tracing = is_tracing(task);
297                         old_tracing = tracing;
298
299                         switch(sig){
300                         case SIGUSR1:
301                                 sig = 0;
302                                 op = do_proc_op(task, proc_id);
303                                 switch(op){
304                                 case OP_TRACE_ON:
305                                         arch_leave_kernel(task, pid);
306                                         tracing = 1;
307                                         break;
308                                 case OP_REBOOT:
309                                 case OP_HALT:
310                                         unmap_physmem();
311                                         kmalloc_ok = 0;
312                                         ptrace(PTRACE_KILL, pid, 0, 0);
313                                         return(op == OP_REBOOT);
314                                 case OP_NONE:
315                                         printf("Detaching pid %d\n", pid);
316                                         detach(pid, SIGSTOP);
317                                         continue;
318                                 default:
319                                         break;
320                                 }
321                                 /* OP_EXEC switches host processes on us,
322                                  * we want to continue the new one.
323                                  */
324                                 pid = cpu_tasks[proc_id].pid;
325                                 break;
326                         case SIGTRAP:
327                                 if(!tracing && (debugger_pid != -1)){
328                                         child_signal(pid, status);
329                                         continue;
330                                 }
331                                 tracing = 0;
332                                 if(do_syscall(task, pid)) sig = SIGUSR2;
333                                 else clear_singlestep(task);
334                                 break;
335                         case SIGPROF:
336                                 if(tracing) sig = 0;
337                                 break;
338                         case SIGCHLD:
339                         case SIGHUP:
340                                 sig = 0;
341                                 break;
342                         case SIGSEGV:
343                         case SIGIO:
344                         case SIGALRM:
345                         case SIGVTALRM:
346                         case SIGFPE:
347                         case SIGBUS:
348                         case SIGILL:
349                         case SIGWINCH:
350                         default:
351                                 tracing = 0;
352                                 break;
353                         }
354                         set_tracing(task, tracing);
355
356                         if(!tracing && old_tracing)
357                                 arch_enter_kernel(task, pid);
358
359                         if(!tracing && (debugger_pid != -1) && (sig != 0) &&
360                                 (sig != SIGALRM) && (sig != SIGVTALRM) &&
361                                 (sig != SIGSEGV) && (sig != SIGTRAP) &&
362                                 (sig != SIGUSR2) && (sig != SIGIO) &&
363                                 (sig != SIGFPE)){
364                                 child_signal(pid, status);
365                                 continue;
366                         }
367
368                         if(tracing){
369                                 if(singlestepping_tt(task))
370                                         cont_type = PTRACE_SINGLESTEP;
371                                 else cont_type = PTRACE_SYSCALL;
372                         }
373                         else cont_type = PTRACE_CONT;
374
375                         if((cont_type == PTRACE_CONT) && 
376                            (debugger_pid != -1) && strace)
377                                 cont_type = PTRACE_SYSCALL;
378
379                         if(ptrace(cont_type, pid, 0, sig) != 0){
380                                 tracer_panic("ptrace failed to continue "
381                                              "process - errno = %d\n", 
382                                              errno);
383                         }
384                 }
385         }
386         return(0);
387 }
388
389 static int __init uml_debug_setup(char *line, int *add)
390 {
391         char *next;
392
393         debug = 1;
394         *add = 0;
395         if(*line != '=') return(0);
396         line++;
397
398         while(line != NULL){
399                 next = strchr(line, ',');
400                 if(next) *next++ = '\0';
401                 
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);
405
406                 line = next;
407         }
408         return(0);
409 }
410
411 __uml_setup("debug", uml_debug_setup,
412 "debug\n"
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"
416 );
417
418 static int __init uml_debugtrace_setup(char *line, int *add)
419 {
420         debug_trace = 1;
421         return 0;
422 }
423 __uml_setup("debugtrace", uml_debugtrace_setup,
424 "debugtrace\n"
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"
429 );
430
431 static int __init uml_honeypot_setup(char *line, int *add)
432 {
433         jail_setup("", add);
434         honeypot = 1;
435         return 0;
436 }
437 __uml_setup("honeypot", uml_honeypot_setup, 
438 "honeypot\n"
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"
442 );
443
444 /*
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
448  * of the file.
449  * ---------------------------------------------------------------------------
450  * Local variables:
451  * c-file-style: "linux"
452  * End:
453  */