1 /**********************************************************************
4 Copyright (C) 1999 Lars Brinkhoff. See the file COPYING for licensing
7 Jeff Dike (jdike@karaya.com) : Modified for integration into uml
8 **********************************************************************/
10 /* XXX This file shouldn't refer to CONFIG_* */
20 #include <sys/types.h>
21 #include <sys/ptrace.h>
22 #include <sys/ioctl.h>
23 #include <asm/unistd.h>
29 #include "user_util.h"
34 static int debugger_wait(debugger_state *debugger, int *status, int options,
35 int (*syscall)(debugger_state *debugger, pid_t child),
36 int (*normal_return)(debugger_state *debugger,
38 int (*wait_return)(debugger_state *debugger,
41 if(debugger->real_wait){
42 debugger->handle_trace = normal_return;
43 syscall_continue(debugger->pid);
44 debugger->real_wait = 0;
47 debugger->wait_status_ptr = status;
48 debugger->wait_options = options;
49 if((debugger->debugee != NULL) && debugger->debugee->event){
50 syscall_continue(debugger->pid);
51 wait_for_stop(debugger->pid, SIGTRAP, PTRACE_SYSCALL,
53 (*wait_return)(debugger, -1);
56 else if(debugger->wait_options & WNOHANG){
57 syscall_cancel(debugger->pid, 0);
58 debugger->handle_trace = syscall;
62 syscall_pause(debugger->pid);
63 debugger->handle_trace = wait_return;
64 debugger->waiting = 1;
70 * Handle debugger trap, i.e. syscall.
73 int debugger_syscall(debugger_state *debugger, pid_t child)
75 long arg1, arg2, arg3, arg4, arg5, result;
78 syscall = get_syscall(debugger->pid, &arg1, &arg2, &arg3, &arg4,
83 /* execve never returns */
84 debugger->handle_trace = debugger_syscall;
88 if(debugger->debugee->pid != 0) arg2 = debugger->debugee->pid;
89 if(!debugger->debugee->in_context)
90 child = debugger->debugee->pid;
91 result = proxy_ptrace(debugger, arg1, arg2, arg3, arg4, child,
93 syscall_cancel(debugger->pid, result);
94 debugger->handle_trace = debugger_syscall;
99 if(!debugger_wait(debugger, (int *) arg2, arg3,
100 debugger_syscall, debugger_normal_return,
106 if(!debugger->debugee->in_context)
107 child = debugger->debugee->pid;
108 if(arg1 == debugger->debugee->pid){
109 result = kill(child, arg2);
110 syscall_cancel(debugger->pid, result);
111 debugger->handle_trace = debugger_syscall;
114 else debugger->handle_trace = debugger_normal_return;
118 debugger->handle_trace = debugger_normal_return;
121 syscall_continue(debugger->pid);
125 /* Used by the tracing thread */
126 static debugger_state parent;
127 static int parent_syscall(debugger_state *debugger, int pid);
129 int init_parent_proxy(int pid)
131 parent = ((debugger_state) { .pid = pid,
133 .wait_status_ptr = NULL,
136 .expecting_child = 0,
137 .handle_trace = parent_syscall,
142 int parent_normal_return(debugger_state *debugger, pid_t unused)
144 debugger->handle_trace = parent_syscall;
145 syscall_continue(debugger->pid);
149 static int parent_syscall(debugger_state *debugger, int pid)
151 long arg1, arg2, arg3, arg4, arg5;
154 syscall = get_syscall(pid, &arg1, &arg2, &arg3, &arg4, &arg5);
156 if((syscall == __NR_waitpid) || (syscall == __NR_wait4)){
157 debugger_wait(&parent, (int *) arg2, arg3, parent_syscall,
158 parent_normal_return, parent_wait_return);
160 else ptrace(PTRACE_SYSCALL, pid, 0, 0);
164 int debugger_normal_return(debugger_state *debugger, pid_t unused)
166 debugger->handle_trace = debugger_syscall;
167 syscall_continue(debugger->pid);
171 void debugger_cancelled_return(debugger_state *debugger, int result)
173 debugger->handle_trace = debugger_syscall;
174 syscall_set_result(debugger->pid, result);
175 syscall_continue(debugger->pid);
178 /* Used by the tracing thread */
179 static debugger_state debugger;
180 static debugee_state debugee;
182 void init_proxy (pid_t debugger_pid, int stopped, int status)
184 debugger.pid = debugger_pid;
185 debugger.handle_trace = debugger_syscall;
186 debugger.debugee = &debugee;
187 debugger.waiting = 0;
188 debugger.real_wait = 0;
189 debugger.expecting_child = 0;
193 debugee.stopped = stopped;
197 debugee.wait_status = status;
198 debugee.in_context = 1;
201 int debugger_proxy(int status, int pid)
205 if(WIFSTOPPED(status)){
206 sig = WSTOPSIG(status);
208 ret = (*debugger.handle_trace)(&debugger, pid);
210 else if(sig == SIGCHLD){
211 if(debugger.expecting_child){
212 ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
213 debugger.expecting_child = 0;
215 else if(debugger.waiting)
216 real_wait_return(&debugger);
218 ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
219 debugger.real_wait = 1;
222 else ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
224 else if(WIFEXITED(status)){
225 tracer_panic("debugger (pid %d) exited with status %d",
226 debugger.pid, WEXITSTATUS(status));
228 else if(WIFSIGNALED(status)){
229 tracer_panic("debugger (pid %d) exited with signal %d",
230 debugger.pid, WTERMSIG(status));
233 tracer_panic("proxy got unknown status (0x%x) on debugger "
234 "(pid %d)", status, debugger.pid);
239 void child_proxy(pid_t pid, int status)
242 debugee.wait_status = status;
244 if(WIFSTOPPED(status)){
246 debugger.expecting_child = 1;
247 kill(debugger.pid, SIGCHLD);
249 else if(WIFEXITED(status) || WIFSIGNALED(status)){
251 debugger.expecting_child = 1;
252 kill(debugger.pid, SIGCHLD);
254 else panic("proxy got unknown status (0x%x) on child (pid %d)",
258 void debugger_parent_signal(int status, int pid)
262 if(WIFSTOPPED(status)){
263 sig = WSTOPSIG(status);
264 if(sig == SIGTRAP) (*parent.handle_trace)(&parent, pid);
265 else ptrace(PTRACE_SYSCALL, pid, 0, sig);
269 void fake_child_exit(void)
273 child_proxy(1, W_EXITCODE(0, 0));
274 while(debugger.waiting == 1){
275 pid = waitpid(debugger.pid, &status, WUNTRACED);
276 if(pid != debugger.pid){
277 printk("fake_child_exit - waitpid failed, "
278 "errno = %d\n", errno);
281 debugger_proxy(status, debugger.pid);
283 pid = waitpid(debugger.pid, &status, WUNTRACED);
284 if(pid != debugger.pid){
285 printk("fake_child_exit - waitpid failed, "
286 "errno = %d\n", errno);
289 if(ptrace(PTRACE_DETACH, debugger.pid, 0, SIGCONT) < 0)
290 printk("fake_child_exit - PTRACE_DETACH failed, errno = %d\n",
294 char gdb_init_string[] =
298 handle SIGWINCH nostop noprint pass \n\
301 int start_debugger(char *prog, int startup, int stop, int *fd_out)
305 slave = open_gdb_chan();
308 char *tempname = NULL;
311 if(setsid() < 0) perror("setsid");
312 if((dup2(slave, 0) < 0) || (dup2(slave, 1) < 0) ||
313 (dup2(slave, 2) < 0)){
314 printk("start_debugger : dup2 failed, errno = %d\n",
318 if(ioctl(0, TIOCSCTTY, 0) < 0){
319 printk("start_debugger : TIOCSCTTY failed, "
320 "errno = %d\n", errno);
323 if(tcsetpgrp (1, os_getpid()) < 0){
324 printk("start_debugger : tcsetpgrp failed, "
325 "errno = %d\n", errno);
330 fd = make_tempfile("/tmp/gdb_init-XXXXXX", &tempname, 0);
332 printk("start_debugger : make_tempfile failed,"
336 os_write_file(fd, gdb_init_string, sizeof(gdb_init_string) - 1);
339 os_write_file(fd, "b start_kernel\n",
340 strlen("b start_kernel\n"));
342 os_write_file(fd, "c\n", strlen("c\n"));
344 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
345 printk("start_debugger : PTRACE_TRACEME failed, "
346 "errno = %d\n", errno);
349 execlp("gdb", "gdb", "--command", tempname, prog, NULL);
350 printk("start_debugger : exec of gdb failed, errno = %d\n",
354 printk("start_debugger : fork for gdb failed, errno = %d\n",
363 * Overrides for Emacs so that we follow Linus's tabbing style.
364 * Emacs will notice this stuff at the end of the file and automatically
365 * adjust the settings for this buffer only. This must remain at the end
367 * ---------------------------------------------------------------------------
369 * c-file-style: "linux"