kernel.org linux-2.6.10
[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         int pt_syscall_parm, local_using_sysemu;
188
189         capture_signal_stack();
190         signal(SIGPIPE, SIG_IGN);
191         setup_tracer_winch();
192         tracing_pid = os_getpid();
193         printf("tracing thread pid = %d\n", tracing_pid);
194
195         pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
196         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
197         if(n < 0){
198                 printf("waitpid on idle thread failed, errno = %d\n", errno);
199                 exit(1);
200         }
201         if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
202                 printf("Failed to continue idle thread, errno = %d\n", errno);
203                 exit(1);
204         }
205
206         signal(SIGSEGV, (sighandler_t) tracer_segv);
207         signal(SIGUSR1, signal_usr1);
208         if(debug_trace){
209                 printf("Tracing thread pausing to be attached\n");
210                 stop();
211         }
212         if(debug){
213                 if(gdb_pid != -1) 
214                         debugger_pid = attach_debugger(pid, gdb_pid, 1);
215                 else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
216                 if(debug_parent){
217                         debugger_parent = os_process_parent(debugger_pid);
218                         init_parent_proxy(debugger_parent);
219                         err = attach(debugger_parent);
220                         if(err){
221                                 printf("Failed to attach debugger parent %d, "
222                                        "errno = %d\n", debugger_parent, -err);
223                                 debugger_parent = -1;
224                         }
225                         else {
226                                 if(ptrace(PTRACE_SYSCALL, debugger_parent, 
227                                           0, 0) < 0){
228                                         printf("Failed to continue debugger "
229                                                "parent, errno = %d\n", errno);
230                                         debugger_parent = -1;
231                                 }
232                         }
233                 }
234         }
235         set_cmdline("(tracing thread)");
236         while(1){
237                 CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
238                 if(pid <= 0){
239                         if(errno != ECHILD){
240                                 printf("wait failed - errno = %d\n", errno);
241                         }
242                         continue;
243                 }
244                 if(pid == debugger_pid){
245                         int cont = 0;
246
247                         if(WIFEXITED(status) || WIFSIGNALED(status))
248                                 debugger_pid = -1;
249                         /* XXX Figure out how to deal with gdb and SMP */
250                         else cont = debugger_signal(status, cpu_tasks[0].pid);
251                         if(cont == PTRACE_SYSCALL) strace = 1;
252                         continue;
253                 }
254                 else if(pid == debugger_parent){
255                         debugger_parent_signal(status, pid);
256                         continue;
257                 }
258                 nsignals++;
259                 if(WIFEXITED(status)) ;
260 #ifdef notdef
261                 {
262                         printf("Child %d exited with status %d\n", pid, 
263                                WEXITSTATUS(status));
264                 }
265 #endif
266                 else if(WIFSIGNALED(status)){
267                         sig = WTERMSIG(status);
268                         if(sig != 9){
269                                 printf("Child %d exited with signal %d\n", pid,
270                                        sig);
271                         }
272                 }
273                 else if(WIFSTOPPED(status)){
274                         proc_id = pid_to_processor_id(pid);
275                         sig = WSTOPSIG(status);
276                         if(signal_index[proc_id] == 1024){
277                                 signal_index[proc_id] = 0;
278                                 last_index = 1023;
279                         }
280                         else last_index = signal_index[proc_id] - 1;
281                         if(((sig == SIGPROF) || (sig == SIGVTALRM) || 
282                             (sig == SIGALRM)) &&
283                            (signal_record[proc_id][last_index].signal == sig)&&
284                            (signal_record[proc_id][last_index].pid == pid))
285                                 signal_index[proc_id] = last_index;
286                         signal_record[proc_id][signal_index[proc_id]].pid = pid;
287                         gettimeofday(&signal_record[proc_id][signal_index[proc_id]].time, NULL);
288                         eip = ptrace(PTRACE_PEEKUSER, pid, PT_IP_OFFSET, 0);
289                         signal_record[proc_id][signal_index[proc_id]].addr = eip;
290                         signal_record[proc_id][signal_index[proc_id]++].signal = sig;
291                         
292                         if(proc_id == -1){
293                                 sleeping_process_signal(pid, sig);
294                                 continue;
295                         }
296
297                         task = cpu_tasks[proc_id].task;
298                         tracing = is_tracing(task);
299                         old_tracing = tracing;
300
301                         local_using_sysemu = get_using_sysemu();
302                         pt_syscall_parm = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
303
304                         switch(sig){
305                         case SIGUSR1:
306                                 sig = 0;
307                                 op = do_proc_op(task, proc_id);
308                                 switch(op){
309                                 case OP_TRACE_ON:
310                                         arch_leave_kernel(task, pid);
311                                         tracing = 1;
312                                         break;
313                                 case OP_REBOOT:
314                                 case OP_HALT:
315                                         unmap_physmem();
316                                         kmalloc_ok = 0;
317                                         ptrace(PTRACE_KILL, pid, 0, 0);
318                                         return(op == OP_REBOOT);
319                                 case OP_NONE:
320                                         printf("Detaching pid %d\n", pid);
321                                         detach(pid, SIGSTOP);
322                                         continue;
323                                 default:
324                                         break;
325                                 }
326                                 /* OP_EXEC switches host processes on us,
327                                  * we want to continue the new one.
328                                  */
329                                 pid = cpu_tasks[proc_id].pid;
330                                 break;
331                         case SIGTRAP:
332                                 if(!tracing && (debugger_pid != -1)){
333                                         child_signal(pid, status);
334                                         continue;
335                                 }
336                                 tracing = 0;
337                                 if(do_syscall(task, pid, local_using_sysemu))
338                                         sig = SIGUSR2;
339                                 break;
340                         case SIGPROF:
341                                 if(tracing) sig = 0;
342                                 break;
343                         case SIGCHLD:
344                         case SIGHUP:
345                                 sig = 0;
346                                 break;
347                         case SIGSEGV:
348                         case SIGIO:
349                         case SIGALRM:
350                         case SIGVTALRM:
351                         case SIGFPE:
352                         case SIGBUS:
353                         case SIGILL:
354                         case SIGWINCH:
355
356                         default:
357                                 tracing = 0;
358                                 break;
359                         }
360                         set_tracing(task, tracing);
361
362                         if(!tracing && old_tracing)
363                                 arch_enter_kernel(task, pid);
364
365                         if(!tracing && (debugger_pid != -1) && (sig != 0) &&
366                                 (sig != SIGALRM) && (sig != SIGVTALRM) &&
367                                 (sig != SIGSEGV) && (sig != SIGTRAP) &&
368                                 (sig != SIGUSR2) && (sig != SIGIO) &&
369                                 (sig != SIGFPE)){
370                                 child_signal(pid, status);
371                                 continue;
372                         }
373
374                         if(tracing){
375                                 if(singlestepping(task))
376                                         cont_type = PTRACE_SINGLESTEP;
377                                 else cont_type = pt_syscall_parm;
378                         }
379                         else cont_type = PTRACE_CONT;
380
381                         if((cont_type == PTRACE_CONT) && 
382                            (debugger_pid != -1) && strace)
383                                 cont_type = PTRACE_SYSCALL;
384
385                         if(ptrace(cont_type, pid, 0, sig) != 0){
386                                 tracer_panic("ptrace failed to continue "
387                                              "process - errno = %d\n", 
388                                              errno);
389                         }
390                 }
391         }
392         return(0);
393 }
394
395 static int __init uml_debug_setup(char *line, int *add)
396 {
397         char *next;
398
399         debug = 1;
400         *add = 0;
401         if(*line != '=') return(0);
402         line++;
403
404         while(line != NULL){
405                 next = strchr(line, ',');
406                 if(next) *next++ = '\0';
407                 
408                 if(!strcmp(line, "go")) debug_stop = 0;
409                 else if(!strcmp(line, "parent")) debug_parent = 1;
410                 else printf("Unknown debug option : '%s'\n", line);
411
412                 line = next;
413         }
414         return(0);
415 }
416
417 __uml_setup("debug", uml_debug_setup,
418 "debug\n"
419 "    Starts up the kernel under the control of gdb. See the \n"
420 "    kernel debugging tutorial and the debugging session pages\n"
421 "    at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
422 );
423
424 static int __init uml_debugtrace_setup(char *line, int *add)
425 {
426         debug_trace = 1;
427         return 0;
428 }
429 __uml_setup("debugtrace", uml_debugtrace_setup,
430 "debugtrace\n"
431 "    Causes the tracing thread to pause until it is attached by a\n"
432 "    debugger and continued.  This is mostly for debugging crashes\n"
433 "    early during boot, and should be pretty much obsoleted by\n"
434 "    the debug switch.\n\n"
435 );
436
437 static int __init uml_honeypot_setup(char *line, int *add)
438 {
439         jail_setup("", add);
440         honeypot = 1;
441         return 0;
442 }
443 __uml_setup("honeypot", uml_honeypot_setup, 
444 "honeypot\n"
445 "    This makes UML put process stacks in the same location as they are\n"
446 "    on the host, allowing expoits such as stack smashes to work against\n"
447 "    UML.  This implies 'jail'.\n\n"
448 );
449
450 /*
451  * Overrides for Emacs so that we follow Linus's tabbing style.
452  * Emacs will notice this stuff at the end of the file and automatically
453  * adjust the settings for this buffer only.  This must remain at the end
454  * of the file.
455  * ---------------------------------------------------------------------------
456  * Local variables:
457  * c-file-style: "linux"
458  * End:
459  */