fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / um / kernel / tt / tracer.c
index 6a6322a..b919535 100644 (file)
@@ -12,7 +12,6 @@
 #include <sched.h>
 #include <string.h>
 #include <sys/mman.h>
-#include <sys/ptrace.h>
 #include <sys/time.h>
 #include <sys/wait.h>
 #include "user.h"
 #include "sigcontext.h"
 #include "sysdep/sigcontext.h"
 #include "os.h"
-#include "signal_user.h"
 #include "user_util.h"
 #include "mem_user.h"
 #include "process.h"
 #include "kern_util.h"
-#include "frame.h"
 #include "chan_user.h"
 #include "ptrace_user.h"
+#include "irq_user.h"
 #include "mode.h"
 #include "tt.h"
 
@@ -35,20 +33,21 @@ static int tracer_winch[2];
 
 int is_tracer_winch(int pid, int fd, void *data)
 {
-       if(pid != tracing_pid)
+       if(pid != os_getpgrp())
                return(0);
 
        register_winch_irq(tracer_winch[0], fd, -1, data);
-       return(0);
+       return(1);
 }
 
 static void tracer_winch_handler(int sig)
 {
+       int n;
        char c = 1;
 
-       if(write(tracer_winch[1], &c, sizeof(c)) != sizeof(c))
-               printk("tracer_winch_handler - write failed, errno = %d\n",
-                      errno);
+       n = os_write_file(tracer_winch[1], &c, sizeof(c));
+       if(n != sizeof(c))
+               printk("tracer_winch_handler - write failed, err = %d\n", -n);
 }
 
 /* Called only by the tracing thread during initialization */
@@ -58,9 +57,8 @@ static void setup_tracer_winch(void)
        int err;
 
        err = os_pipe(tracer_winch, 1, 1);
-       if(err){
-               printk("setup_tracer_winch : os_pipe failed, errno = %d\n", 
-                      -err);
+       if(err < 0){
+               printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err);
                return;
        }
        signal(SIGWINCH, tracer_winch_handler);
@@ -72,6 +70,8 @@ void attach_process(int pid)
           (ptrace(PTRACE_CONT, pid, 0, 0) < 0))
                tracer_panic("OP_FORK failed to attach pid");
        wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
+       if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
+               tracer_panic("OP_FORK: PTRACE_SETOPTIONS failed, errno = %d", errno);
        if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
                tracer_panic("OP_FORK failed to continue process");
 }
@@ -82,14 +82,17 @@ void tracer_panic(char *format, ...)
 
        va_start(ap, format);
        vprintf(format, ap);
+       va_end(ap);
        printf("\n");
        while(1) pause();
 }
 
 static void tracer_segv(int sig, struct sigcontext sc)
 {
+        struct faultinfo fi;
+        GET_FAULTINFO_FROM_SC(fi, &sc);
        printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
-              SC_FAULT_ADDR(&sc), SC_IP(&sc));
+               FAULT_ADDRESS(fi), SC_IP(&sc));
        while(1)
                pause();
 }
@@ -116,6 +119,7 @@ static int signal_tramp(void *arg)
        signal(SIGSEGV, (__sighandler_t) sig_handler);
        set_cmdline("(idle thread)");
        set_init_pid(os_getpid());
+       init_irq_signals(0);
        proc = arg;
        return((*proc)(NULL));
 }
@@ -130,8 +134,8 @@ static void sleeping_process_signal(int pid, int sig)
        case SIGTSTP:
                if(ptrace(PTRACE_CONT, pid, 0, sig) < 0)
                        tracer_panic("sleeping_process_signal : Failed to "
-                                    "continue pid %d, errno = %d\n", pid,
-                                    sig);
+                                    "continue pid %d, signal = %d, "
+                                    "errno = %d\n", pid, sig, errno);
                break;
 
        /* This happens when the debugger (e.g. strace) is doing system call 
@@ -141,11 +145,11 @@ static void sleeping_process_signal(int pid, int sig)
         * any more, the trace of those will land here.  So, we need to just 
         * PTRACE_SYSCALL it.
         */
-       case SIGTRAP:
+       case (SIGTRAP + 0x80):
                if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
                        tracer_panic("sleeping_process_signal : Failed to "
                                     "PTRACE_SYSCALL pid %d, errno = %d\n",
-                                    pid, sig);
+                                    pid, errno);
                break;
        case SIGSTOP:
                break;
@@ -172,7 +176,6 @@ struct {
 int signal_index[32];
 int nsignals = 0;
 int debug_trace = 0;
-extern int io_nsignals, io_count, intr_count;
 
 extern void signal_usr1(int sig);
 
@@ -181,22 +184,25 @@ int tracing_pid = -1;
 int tracer(int (*init_proc)(void *), void *sp)
 {
        void *task = NULL;
-       unsigned long eip = 0;
        int status, pid = 0, sig = 0, cont_type, tracing = 0, op = 0;
-       int last_index, proc_id = 0, n, err, old_tracing = 0, strace = 0;
+       int proc_id = 0, n, err, old_tracing = 0, strace = 0;
+       int local_using_sysemu = 0;
 
-       capture_signal_stack();
        signal(SIGPIPE, SIG_IGN);
        setup_tracer_winch();
        tracing_pid = os_getpid();
        printf("tracing thread pid = %d\n", tracing_pid);
 
        pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
-       n = waitpid(pid, &status, WUNTRACED);
+       CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
        if(n < 0){
                printf("waitpid on idle thread failed, errno = %d\n", errno);
                exit(1);
        }
+       if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0) {
+               printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno);
+               exit(1);
+       }
        if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
                printf("Failed to continue idle thread, errno = %d\n", errno);
                exit(1);
@@ -218,7 +224,7 @@ int tracer(int (*init_proc)(void *), void *sp)
                        err = attach(debugger_parent);
                        if(err){
                                printf("Failed to attach debugger parent %d, "
-                                      "errno = %d\n", debugger_parent, err);
+                                      "errno = %d\n", debugger_parent, -err);
                                debugger_parent = -1;
                        }
                        else {
@@ -233,7 +239,8 @@ int tracer(int (*init_proc)(void *), void *sp)
        }
        set_cmdline("(tracing thread)");
        while(1){
-               if((pid = waitpid(-1, &status, WUNTRACED)) <= 0){
+               CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
+               if(pid <= 0){
                        if(errno != ECHILD){
                                printf("wait failed - errno = %d\n", errno);
                        }
@@ -271,22 +278,6 @@ int tracer(int (*init_proc)(void *), void *sp)
                else if(WIFSTOPPED(status)){
                        proc_id = pid_to_processor_id(pid);
                        sig = WSTOPSIG(status);
-                       if(signal_index[proc_id] == 1024){
-                               signal_index[proc_id] = 0;
-                               last_index = 1023;
-                       }
-                       else last_index = signal_index[proc_id] - 1;
-                       if(((sig == SIGPROF) || (sig == SIGVTALRM) || 
-                           (sig == SIGALRM)) &&
-                          (signal_record[proc_id][last_index].signal == sig)&&
-                          (signal_record[proc_id][last_index].pid == pid))
-                               signal_index[proc_id] = last_index;
-                       signal_record[proc_id][signal_index[proc_id]].pid = pid;
-                       gettimeofday(&signal_record[proc_id][signal_index[proc_id]].time, NULL);
-                       eip = ptrace(PTRACE_PEEKUSER, pid, PT_IP_OFFSET, 0);
-                       signal_record[proc_id][signal_index[proc_id]].addr = eip;
-                       signal_record[proc_id][signal_index[proc_id]++].signal = sig;
-                       
                        if(proc_id == -1){
                                sleeping_process_signal(pid, sig);
                                continue;
@@ -296,11 +287,24 @@ int tracer(int (*init_proc)(void *), void *sp)
                        tracing = is_tracing(task);
                        old_tracing = tracing;
 
+                       /* Assume: no syscall, when coming from user */
+                       if ( tracing )
+                               do_sigtrap(task);
+
                        switch(sig){
                        case SIGUSR1:
                                sig = 0;
                                op = do_proc_op(task, proc_id);
                                switch(op){
+                               /*
+                                * This is called when entering user mode; after
+                                * this, we start intercepting syscalls.
+                                *
+                                * In fact, a process is started in kernel mode,
+                                * so with is_tracing() == 0 (and that is reset
+                                * when executing syscalls, since UML kernel has
+                                * the right to do syscalls);
+                                */
                                case OP_TRACE_ON:
                                        arch_leave_kernel(task, pid);
                                        tracing = 1;
@@ -309,7 +313,13 @@ int tracer(int (*init_proc)(void *), void *sp)
                                case OP_HALT:
                                        unmap_physmem();
                                        kmalloc_ok = 0;
-                                       ptrace(PTRACE_KILL, pid, 0, 0);
+                                       os_kill_ptraced_process(pid, 0);
+                                       /* Now let's reap remaining zombies */
+                                       errno = 0;
+                                       do {
+                                               waitpid(-1, &status,
+                                                       WUNTRACED);
+                                       } while (errno != ECHILD);
                                        return(op == OP_REBOOT);
                                case OP_NONE:
                                        printf("Detaching pid %d\n", pid);
@@ -323,14 +333,26 @@ int tracer(int (*init_proc)(void *), void *sp)
                                 */
                                pid = cpu_tasks[proc_id].pid;
                                break;
+                       case (SIGTRAP + 0x80):
+                               if(!tracing && (debugger_pid != -1)){
+                                       child_signal(pid, status & 0x7fff);
+                                       continue;
+                               }
+                               tracing = 0;
+                               /* local_using_sysemu has been already set
+                                * below, since if we are here, is_tracing() on
+                                * the traced task was 1, i.e. the process had
+                                * already run through one iteration of the
+                                * loop which executed a OP_TRACE_ON request.*/
+                               do_syscall(task, pid, local_using_sysemu);
+                               sig = SIGUSR2;
+                               break;
                        case SIGTRAP:
                                if(!tracing && (debugger_pid != -1)){
                                        child_signal(pid, status);
                                        continue;
                                }
                                tracing = 0;
-                               if(do_syscall(task, pid)) sig = SIGUSR2;
-                               else clear_singlestep(task);
                                break;
                        case SIGPROF:
                                if(tracing) sig = 0;
@@ -347,6 +369,7 @@ int tracer(int (*init_proc)(void *), void *sp)
                        case SIGBUS:
                        case SIGILL:
                        case SIGWINCH:
+
                        default:
                                tracing = 0;
                                break;
@@ -365,16 +388,15 @@ int tracer(int (*init_proc)(void *), void *sp)
                                continue;
                        }
 
-                       if(tracing){
-                               if(singlestepping_tt(task))
-                                       cont_type = PTRACE_SINGLESTEP;
-                               else cont_type = PTRACE_SYSCALL;
-                       }
-                       else cont_type = PTRACE_CONT;
+                       local_using_sysemu = get_using_sysemu();
 
-                       if((cont_type == PTRACE_CONT) && 
-                          (debugger_pid != -1) && strace)
+                       if(tracing)
+                               cont_type = SELECT_PTRACE_OPERATION(local_using_sysemu,
+                                                                   singlestepping(task));
+                       else if((debugger_pid != -1) && strace)
                                cont_type = PTRACE_SYSCALL;
+                       else
+                               cont_type = PTRACE_CONT;
 
                        if(ptrace(cont_type, pid, 0, sig) != 0){
                                tracer_panic("ptrace failed to continue "
@@ -401,7 +423,7 @@ static int __init uml_debug_setup(char *line, int *add)
                
                if(!strcmp(line, "go")) debug_stop = 0;
                else if(!strcmp(line, "parent")) debug_parent = 1;
-               else printk("Unknown debug option : '%s'\n", line);
+               else printf("Unknown debug option : '%s'\n", line);
 
                line = next;
        }
@@ -428,19 +450,6 @@ __uml_setup("debugtrace", uml_debugtrace_setup,
 "    the debug switch.\n\n"
 );
 
-static int __init uml_honeypot_setup(char *line, int *add)
-{
-       jail_setup("", add);
-       honeypot = 1;
-       return 0;
-}
-__uml_setup("honeypot", uml_honeypot_setup, 
-"honeypot\n"
-"    This makes UML put process stacks in the same location as they are\n"
-"    on the host, allowing expoits such as stack smashes to work against\n"
-"    UML.  This implies 'jail'.\n\n"
-);
-
 /*
  * Overrides for Emacs so that we follow Linus's tabbing style.
  * Emacs will notice this stuff at the end of the file and automatically