2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
18 #include "sysdep/ptrace.h"
19 #include "sigcontext.h"
20 #include "sysdep/sigcontext.h"
22 #include "signal_user.h"
23 #include "user_util.h"
26 #include "kern_util.h"
27 #include "chan_user.h"
28 #include "ptrace_user.h"
33 static int tracer_winch
[2];
35 int is_tracer_winch(int pid
, int fd
, void *data
)
37 if(pid
!= os_getpgrp())
40 register_winch_irq(tracer_winch
[0], fd
, -1, data
);
44 static void tracer_winch_handler(int sig
)
49 n
= os_write_file(tracer_winch
[1], &c
, sizeof(c
));
51 printk("tracer_winch_handler - write failed, err = %d\n", -n
);
54 /* Called only by the tracing thread during initialization */
56 static void setup_tracer_winch(void)
60 err
= os_pipe(tracer_winch
, 1, 1);
62 printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err
);
65 signal(SIGWINCH
, tracer_winch_handler
);
68 void attach_process(int pid
)
70 if((ptrace(PTRACE_ATTACH
, pid
, 0, 0) < 0) ||
71 (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0))
72 tracer_panic("OP_FORK failed to attach pid");
73 wait_for_stop(pid
, SIGSTOP
, PTRACE_CONT
, NULL
);
74 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
75 tracer_panic("OP_FORK: PTRACE_SETOPTIONS failed, errno = %d", errno
);
76 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
77 tracer_panic("OP_FORK failed to continue process");
80 void tracer_panic(char *format
, ...)
91 static void tracer_segv(int sig
, struct sigcontext sc
)
94 GET_FAULTINFO_FROM_SC(fi
, &sc
);
95 printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
96 FAULT_ADDRESS(fi
), SC_IP(&sc
));
101 /* Changed early in boot, and then only read */
104 int debug_parent
= 0;
107 static int signal_tramp(void *arg
)
111 if(honeypot
&& munmap((void *) (host_task_size
- 0x10000000),
113 panic("Unmapping stack failed");
114 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0)
115 panic("ptrace PTRACE_TRACEME failed");
116 os_stop_process(os_getpid());
117 change_sig(SIGWINCH
, 0);
118 signal(SIGUSR1
, SIG_IGN
);
119 change_sig(SIGCHLD
, 0);
120 signal(SIGSEGV
, (__sighandler_t
) sig_handler
);
121 set_cmdline("(idle thread)");
122 set_init_pid(os_getpid());
125 return((*proc
)(NULL
));
128 static void sleeping_process_signal(int pid
, int sig
)
131 /* These two result from UML being ^Z-ed and bg-ed. PTRACE_CONT is
132 * right because the process must be in the kernel already.
136 if(ptrace(PTRACE_CONT
, pid
, 0, sig
) < 0)
137 tracer_panic("sleeping_process_signal : Failed to "
138 "continue pid %d, signal = %d, "
139 "errno = %d\n", pid
, sig
, errno
);
142 /* This happens when the debugger (e.g. strace) is doing system call
143 * tracing on the kernel. During a context switch, the current task
144 * will be set to the incoming process and the outgoing process will
145 * hop into write and then read. Since it's not the current process
146 * any more, the trace of those will land here. So, we need to just
149 case (SIGTRAP
+ 0x80):
150 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
151 tracer_panic("sleeping_process_signal : Failed to "
152 "PTRACE_SYSCALL pid %d, errno = %d\n",
158 tracer_panic("sleeping process %d got unexpected "
159 "signal : %d\n", pid
, sig
);
164 /* Accessed only by the tracing thread */
165 int debugger_pid
= -1;
166 int debugger_parent
= -1;
167 int debugger_fd
= -1;
175 } signal_record
[1024][32];
177 int signal_index
[32];
180 extern int io_nsignals
, io_count
, intr_count
;
182 extern void signal_usr1(int sig
);
184 int tracing_pid
= -1;
186 int tracer(int (*init_proc
)(void *), void *sp
)
189 int status
, pid
= 0, sig
= 0, cont_type
, tracing
= 0, op
= 0;
190 int proc_id
= 0, n
, err
, old_tracing
= 0, strace
= 0;
191 int local_using_sysemu
= 0;
192 #ifdef UML_CONFIG_SYSCALL_DEBUG
193 unsigned long eip
= 0;
196 signal(SIGPIPE
, SIG_IGN
);
197 setup_tracer_winch();
198 tracing_pid
= os_getpid();
199 printf("tracing thread pid = %d\n", tracing_pid
);
201 pid
= clone(signal_tramp
, sp
, CLONE_FILES
| SIGCHLD
, init_proc
);
202 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
204 printf("waitpid on idle thread failed, errno = %d\n", errno
);
207 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0) {
208 printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno
);
211 if((ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)){
212 printf("Failed to continue idle thread, errno = %d\n", errno
);
216 signal(SIGSEGV
, (sighandler_t
) tracer_segv
);
217 signal(SIGUSR1
, signal_usr1
);
219 printf("Tracing thread pausing to be attached\n");
224 debugger_pid
= attach_debugger(pid
, gdb_pid
, 1);
225 else debugger_pid
= init_ptrace_proxy(pid
, 1, debug_stop
);
227 debugger_parent
= os_process_parent(debugger_pid
);
228 init_parent_proxy(debugger_parent
);
229 err
= attach(debugger_parent
);
231 printf("Failed to attach debugger parent %d, "
232 "errno = %d\n", debugger_parent
, -err
);
233 debugger_parent
= -1;
236 if(ptrace(PTRACE_SYSCALL
, debugger_parent
,
238 printf("Failed to continue debugger "
239 "parent, errno = %d\n", errno
);
240 debugger_parent
= -1;
245 set_cmdline("(tracing thread)");
247 CATCH_EINTR(pid
= waitpid(-1, &status
, WUNTRACED
));
250 printf("wait failed - errno = %d\n", errno
);
254 if(pid
== debugger_pid
){
257 if(WIFEXITED(status
) || WIFSIGNALED(status
))
259 /* XXX Figure out how to deal with gdb and SMP */
260 else cont
= debugger_signal(status
, cpu_tasks
[0].pid
);
261 if(cont
== PTRACE_SYSCALL
) strace
= 1;
264 else if(pid
== debugger_parent
){
265 debugger_parent_signal(status
, pid
);
269 if(WIFEXITED(status
)) ;
272 printf("Child %d exited with status %d\n", pid
,
273 WEXITSTATUS(status
));
276 else if(WIFSIGNALED(status
)){
277 sig
= WTERMSIG(status
);
279 printf("Child %d exited with signal %d\n", pid
,
283 else if(WIFSTOPPED(status
)){
284 proc_id
= pid_to_processor_id(pid
);
285 sig
= WSTOPSIG(status
);
286 #ifdef UML_CONFIG_SYSCALL_DEBUG
287 if(signal_index
[proc_id
] == 1024){
288 signal_index
[proc_id
] = 0;
291 else last_index
= signal_index
[proc_id
] - 1;
292 if(((sig
== SIGPROF
) || (sig
== SIGVTALRM
) ||
294 (signal_record
[proc_id
][last_index
].signal
== sig
)&&
295 (signal_record
[proc_id
][last_index
].pid
== pid
))
296 signal_index
[proc_id
] = last_index
;
297 signal_record
[proc_id
][signal_index
[proc_id
]].pid
= pid
;
298 gettimeofday(&signal_record
[proc_id
][signal_index
[proc_id
]].time
, NULL
);
299 eip
= ptrace(PTRACE_PEEKUSR
, pid
, PT_IP_OFFSET
, 0);
300 signal_record
[proc_id
][signal_index
[proc_id
]].addr
= eip
;
301 signal_record
[proc_id
][signal_index
[proc_id
]++].signal
= sig
;
304 sleeping_process_signal(pid
, sig
);
308 task
= cpu_tasks
[proc_id
].task
;
309 tracing
= is_tracing(task
);
310 old_tracing
= tracing
;
312 /* Assume: no syscall, when coming from user */
319 op
= do_proc_op(task
, proc_id
);
322 * This is called when entering user mode; after
323 * this, we start intercepting syscalls.
325 * In fact, a process is started in kernel mode,
326 * so with is_tracing() == 0 (and that is reset
327 * when executing syscalls, since UML kernel has
328 * the right to do syscalls);
331 arch_leave_kernel(task
, pid
);
338 os_kill_ptraced_process(pid
, 0);
339 /* Now let's reap remaining zombies */
344 } while (errno
!= ECHILD
);
345 return(op
== OP_REBOOT
);
347 printf("Detaching pid %d\n", pid
);
348 detach(pid
, SIGSTOP
);
353 /* OP_EXEC switches host processes on us,
354 * we want to continue the new one.
356 pid
= cpu_tasks
[proc_id
].pid
;
358 case (SIGTRAP
+ 0x80):
359 if(!tracing
&& (debugger_pid
!= -1)){
360 child_signal(pid
, status
& 0x7fff);
364 /* local_using_sysemu has been already set
365 * below, since if we are here, is_tracing() on
366 * the traced task was 1, i.e. the process had
367 * already run through one iteration of the
368 * loop which executed a OP_TRACE_ON request.*/
369 do_syscall(task
, pid
, local_using_sysemu
);
373 if(!tracing
&& (debugger_pid
!= -1)){
374 child_signal(pid
, status
);
399 set_tracing(task
, tracing
);
401 if(!tracing
&& old_tracing
)
402 arch_enter_kernel(task
, pid
);
404 if(!tracing
&& (debugger_pid
!= -1) && (sig
!= 0) &&
405 (sig
!= SIGALRM
) && (sig
!= SIGVTALRM
) &&
406 (sig
!= SIGSEGV
) && (sig
!= SIGTRAP
) &&
407 (sig
!= SIGUSR2
) && (sig
!= SIGIO
) &&
409 child_signal(pid
, status
);
413 local_using_sysemu
= get_using_sysemu();
416 cont_type
= SELECT_PTRACE_OPERATION(local_using_sysemu
,
417 singlestepping(task
));
418 else if((debugger_pid
!= -1) && strace
)
419 cont_type
= PTRACE_SYSCALL
;
421 cont_type
= PTRACE_CONT
;
423 if(ptrace(cont_type
, pid
, 0, sig
) != 0){
424 tracer_panic("ptrace failed to continue "
425 "process - errno = %d\n",
433 static int __init
uml_debug_setup(char *line
, int *add
)
439 if(*line
!= '=') return(0);
443 next
= strchr(line
, ',');
444 if(next
) *next
++ = '\0';
446 if(!strcmp(line
, "go")) debug_stop
= 0;
447 else if(!strcmp(line
, "parent")) debug_parent
= 1;
448 else printf("Unknown debug option : '%s'\n", line
);
455 __uml_setup("debug", uml_debug_setup
,
457 " Starts up the kernel under the control of gdb. See the \n"
458 " kernel debugging tutorial and the debugging session pages\n"
459 " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
462 static int __init
uml_debugtrace_setup(char *line
, int *add
)
467 __uml_setup("debugtrace", uml_debugtrace_setup
,
469 " Causes the tracing thread to pause until it is attached by a\n"
470 " debugger and continued. This is mostly for debugging crashes\n"
471 " early during boot, and should be pretty much obsoleted by\n"
472 " the debug switch.\n\n"
476 * Overrides for Emacs so that we follow Linus's tabbing style.
477 * Emacs will notice this stuff at the end of the file and automatically
478 * adjust the settings for this buffer only. This must remain at the end
480 * ---------------------------------------------------------------------------
482 * c-file-style: "linux"