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/ioctl.h>
22 #include <asm/unistd.h>
23 #include "ptrace_user.h"
33 static int debugger_wait(debugger_state
*debugger
, int *status
, int options
,
34 int (*syscall
)(debugger_state
*debugger
, pid_t child
),
35 int (*normal_return
)(debugger_state
*debugger
,
37 int (*wait_return
)(debugger_state
*debugger
,
40 if(debugger
->real_wait
){
41 debugger
->handle_trace
= normal_return
;
42 syscall_continue(debugger
->pid
);
43 debugger
->real_wait
= 0;
46 debugger
->wait_status_ptr
= status
;
47 debugger
->wait_options
= options
;
48 if((debugger
->debugee
!= NULL
) && debugger
->debugee
->event
){
49 syscall_continue(debugger
->pid
);
50 wait_for_stop(debugger
->pid
, SIGTRAP
, PTRACE_SYSCALL
,
52 (*wait_return
)(debugger
, -1);
55 else if(debugger
->wait_options
& WNOHANG
){
56 syscall_cancel(debugger
->pid
, 0);
57 debugger
->handle_trace
= syscall
;
61 syscall_pause(debugger
->pid
);
62 debugger
->handle_trace
= wait_return
;
63 debugger
->waiting
= 1;
69 * Handle debugger trap, i.e. syscall.
72 int debugger_syscall(debugger_state
*debugger
, pid_t child
)
74 long arg1
, arg2
, arg3
, arg4
, arg5
, result
;
77 syscall
= get_syscall(debugger
->pid
, &arg1
, &arg2
, &arg3
, &arg4
,
82 /* execve never returns */
83 debugger
->handle_trace
= debugger_syscall
;
87 if(debugger
->debugee
->pid
!= 0) arg2
= debugger
->debugee
->pid
;
88 if(!debugger
->debugee
->in_context
)
89 child
= debugger
->debugee
->pid
;
90 result
= proxy_ptrace(debugger
, arg1
, arg2
, arg3
, arg4
, child
,
92 syscall_cancel(debugger
->pid
, result
);
93 debugger
->handle_trace
= debugger_syscall
;
100 if(!debugger_wait(debugger
, (int *) arg2
, arg3
,
101 debugger_syscall
, debugger_normal_return
,
107 if(!debugger
->debugee
->in_context
)
108 child
= debugger
->debugee
->pid
;
109 if(arg1
== debugger
->debugee
->pid
){
110 result
= kill(child
, arg2
);
111 syscall_cancel(debugger
->pid
, result
);
112 debugger
->handle_trace
= debugger_syscall
;
115 else debugger
->handle_trace
= debugger_normal_return
;
119 debugger
->handle_trace
= debugger_normal_return
;
122 syscall_continue(debugger
->pid
);
126 /* Used by the tracing thread */
127 static debugger_state parent
;
128 static int parent_syscall(debugger_state
*debugger
, int pid
);
130 int init_parent_proxy(int pid
)
132 parent
= ((debugger_state
) { .pid
= pid
,
134 .wait_status_ptr
= NULL
,
137 .expecting_child
= 0,
138 .handle_trace
= parent_syscall
,
143 int parent_normal_return(debugger_state
*debugger
, pid_t unused
)
145 debugger
->handle_trace
= parent_syscall
;
146 syscall_continue(debugger
->pid
);
150 static int parent_syscall(debugger_state
*debugger
, int pid
)
152 long arg1
, arg2
, arg3
, arg4
, arg5
;
155 syscall
= get_syscall(pid
, &arg1
, &arg2
, &arg3
, &arg4
, &arg5
);
157 if((syscall
== __NR_wait4
)
159 || (syscall
== __NR_waitpid
)
162 debugger_wait(&parent
, (int *) arg2
, arg3
, parent_syscall
,
163 parent_normal_return
, parent_wait_return
);
165 else ptrace(PTRACE_SYSCALL
, pid
, 0, 0);
169 int debugger_normal_return(debugger_state
*debugger
, pid_t unused
)
171 debugger
->handle_trace
= debugger_syscall
;
172 syscall_continue(debugger
->pid
);
176 void debugger_cancelled_return(debugger_state
*debugger
, int result
)
178 debugger
->handle_trace
= debugger_syscall
;
179 syscall_set_result(debugger
->pid
, result
);
180 syscall_continue(debugger
->pid
);
183 /* Used by the tracing thread */
184 static debugger_state debugger
;
185 static debugee_state debugee
;
187 void init_proxy (pid_t debugger_pid
, int stopped
, int status
)
189 debugger
.pid
= debugger_pid
;
190 debugger
.handle_trace
= debugger_syscall
;
191 debugger
.debugee
= &debugee
;
192 debugger
.waiting
= 0;
193 debugger
.real_wait
= 0;
194 debugger
.expecting_child
= 0;
198 debugee
.stopped
= stopped
;
202 debugee
.wait_status
= status
;
203 debugee
.in_context
= 1;
206 int debugger_proxy(int status
, int pid
)
210 if(WIFSTOPPED(status
)){
211 sig
= WSTOPSIG(status
);
213 ret
= (*debugger
.handle_trace
)(&debugger
, pid
);
215 else if(sig
== SIGCHLD
){
216 if(debugger
.expecting_child
){
217 ptrace(PTRACE_SYSCALL
, debugger
.pid
, 0, sig
);
218 debugger
.expecting_child
= 0;
220 else if(debugger
.waiting
)
221 real_wait_return(&debugger
);
223 ptrace(PTRACE_SYSCALL
, debugger
.pid
, 0, sig
);
224 debugger
.real_wait
= 1;
227 else ptrace(PTRACE_SYSCALL
, debugger
.pid
, 0, sig
);
229 else if(WIFEXITED(status
)){
230 tracer_panic("debugger (pid %d) exited with status %d",
231 debugger
.pid
, WEXITSTATUS(status
));
233 else if(WIFSIGNALED(status
)){
234 tracer_panic("debugger (pid %d) exited with signal %d",
235 debugger
.pid
, WTERMSIG(status
));
238 tracer_panic("proxy got unknown status (0x%x) on debugger "
239 "(pid %d)", status
, debugger
.pid
);
244 void child_proxy(pid_t pid
, int status
)
247 debugee
.wait_status
= status
;
249 if(WIFSTOPPED(status
)){
251 debugger
.expecting_child
= 1;
252 kill(debugger
.pid
, SIGCHLD
);
254 else if(WIFEXITED(status
) || WIFSIGNALED(status
)){
256 debugger
.expecting_child
= 1;
257 kill(debugger
.pid
, SIGCHLD
);
259 else panic("proxy got unknown status (0x%x) on child (pid %d)",
263 void debugger_parent_signal(int status
, int pid
)
267 if(WIFSTOPPED(status
)){
268 sig
= WSTOPSIG(status
);
269 if(sig
== SIGTRAP
) (*parent
.handle_trace
)(&parent
, pid
);
270 else ptrace(PTRACE_SYSCALL
, pid
, 0, sig
);
274 void fake_child_exit(void)
278 child_proxy(1, W_EXITCODE(0, 0));
279 while(debugger
.waiting
== 1){
280 CATCH_EINTR(pid
= waitpid(debugger
.pid
, &status
, WUNTRACED
));
281 if(pid
!= debugger
.pid
){
282 printk("fake_child_exit - waitpid failed, "
283 "errno = %d\n", errno
);
286 debugger_proxy(status
, debugger
.pid
);
288 CATCH_EINTR(pid
= waitpid(debugger
.pid
, &status
, WUNTRACED
));
289 if(pid
!= debugger
.pid
){
290 printk("fake_child_exit - waitpid failed, "
291 "errno = %d\n", errno
);
294 if(ptrace(PTRACE_DETACH
, debugger
.pid
, 0, SIGCONT
) < 0)
295 printk("fake_child_exit - PTRACE_DETACH failed, errno = %d\n",
299 char gdb_init_string
[] =
303 handle SIGWINCH nostop noprint pass \n\
306 int start_debugger(char *prog
, int startup
, int stop
, int *fd_out
)
310 slave
= open_gdb_chan();
313 char *tempname
= NULL
;
316 if(setsid() < 0) perror("setsid");
317 if((dup2(slave
, 0) < 0) || (dup2(slave
, 1) < 0) ||
318 (dup2(slave
, 2) < 0)){
319 printk("start_debugger : dup2 failed, errno = %d\n",
323 if(ioctl(0, TIOCSCTTY
, 0) < 0){
324 printk("start_debugger : TIOCSCTTY failed, "
325 "errno = %d\n", errno
);
328 if(tcsetpgrp (1, os_getpid()) < 0){
329 printk("start_debugger : tcsetpgrp failed, "
330 "errno = %d\n", errno
);
335 fd
= make_tempfile("/tmp/gdb_init-XXXXXX", &tempname
, 0);
337 printk("start_debugger : make_tempfile failed,"
341 os_write_file(fd
, gdb_init_string
,
342 sizeof(gdb_init_string
) - 1);
345 os_write_file(fd
, "b start_kernel\n",
346 strlen("b start_kernel\n"));
348 os_write_file(fd
, "c\n", strlen("c\n"));
350 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
351 printk("start_debugger : PTRACE_TRACEME failed, "
352 "errno = %d\n", errno
);
355 execlp("gdb", "gdb", "--command", tempname
, prog
, NULL
);
356 printk("start_debugger : exec of gdb failed, errno = %d\n",
360 printk("start_debugger : fork for gdb failed, errno = %d\n",
369 * Overrides for Emacs so that we follow Linus's tabbing style.
370 * Emacs will notice this stuff at the end of the file and automatically
371 * adjust the settings for this buffer only. This must remain at the end
373 * ---------------------------------------------------------------------------
375 * c-file-style: "linux"