2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
11 #include <sys/types.h>
12 #include "ptrace_user.h"
13 #include "uml-config.h"
14 #include "kern_constants.h"
15 #include "chan_user.h"
19 #include "kern_util.h"
21 #include "sysdep/thread.h"
24 extern int debugger_pid
;
25 extern int debugger_fd
;
26 extern int debugger_parent
;
28 int detach(int pid
, int sig
)
30 return(ptrace(PTRACE_DETACH
, pid
, 0, sig
));
37 err
= ptrace(PTRACE_ATTACH
, pid
, 0, 0);
38 if(err
< 0) return(-errno
);
44 return(ptrace(PTRACE_CONT
, pid
, 0, 0));
47 #ifdef UML_CONFIG_PT_PROXY
49 int debugger_signal(int status
, pid_t pid
)
51 return(debugger_proxy(status
, pid
));
54 void child_signal(pid_t pid
, int status
)
56 child_proxy(pid
, status
);
59 static void gdb_announce(char *dev_name
, int dev
)
61 printf("gdb assigned device '%s'\n", dev_name
);
64 static struct chan_opts opts
= {
65 .announce
= gdb_announce
,
66 .xterm_title
= "UML kernel debugger",
72 /* Accessed by the tracing thread, which automatically serializes access */
73 static void *xterm_data
;
76 extern void *xterm_init(char *, int, struct chan_opts
*);
77 extern int xterm_open(int, int, int, void *, char **);
78 extern void xterm_close(int, void *);
80 int open_gdb_chan(void)
82 char stack
[UM_KERN_PAGE_SIZE
], *dummy
;
84 opts
.tramp_stack
= (unsigned long) stack
;
85 xterm_data
= xterm_init("", 0, &opts
);
86 xterm_fd
= xterm_open(1, 1, 1, xterm_data
, &dummy
);
90 static void exit_debugger_cb(void *unused
)
92 if(debugger_pid
!= -1){
97 else kill_child_dead(debugger_pid
);
99 if(debugger_parent
!= -1)
100 detach(debugger_parent
, SIGINT
);
102 if(xterm_data
!= NULL
) xterm_close(xterm_fd
, xterm_data
);
105 static void exit_debugger(void)
107 initial_thread_cb(exit_debugger_cb
, NULL
);
110 __uml_exitcall(exit_debugger
);
117 extern char *linux_prog
;
119 static void config_gdb_cb(void *arg
)
121 struct gdb_data
*data
= arg
;
126 if(debugger_pid
!= -1) exit_debugger_cb(NULL
);
127 if(!strncmp(data
->str
, "pid,", strlen("pid,"))){
128 data
->str
+= strlen("pid,");
129 pid
= strtoul(data
->str
, NULL
, 0);
130 task
= cpu_tasks
[0].task
;
131 debugger_pid
= attach_debugger(TASK_EXTERN_PID(task
), pid
, 0);
132 if(debugger_pid
!= -1){
139 debugger_pid
= start_debugger(linux_prog
, 0, 0, &debugger_fd
);
140 init_proxy(debugger_pid
, 0, 0);
143 int gdb_config(char *str
, char **error_out
)
145 struct gdb_data data
;
147 if(*str
++ != '=') return(-1);
149 initial_thread_cb(config_gdb_cb
, &data
);
153 void remove_gdb_cb(void *unused
)
155 exit_debugger_cb(NULL
);
158 int gdb_remove(int unused
, char **error_out
)
160 initial_thread_cb(remove_gdb_cb
, NULL
);
164 void signal_usr1(int sig
)
166 if(debugger_pid
!= -1){
167 printf("The debugger is already running\n");
170 debugger_pid
= start_debugger(linux_prog
, 0, 0, &debugger_fd
);
171 init_proxy(debugger_pid
, 0, 0);
174 int init_ptrace_proxy(int idle_pid
, int startup
, int stop
)
178 pid
= start_debugger(linux_prog
, startup
, stop
, &debugger_fd
);
179 status
= wait_for_stop(idle_pid
, SIGSTOP
, PTRACE_CONT
, NULL
);
184 init_proxy(pid
, 1, status
);
188 int attach_debugger(int idle_pid
, int pid
, int stop
)
194 printf("Failed to attach pid %d, errno = %d\n", pid
, -err
);
197 if(stop
) status
= wait_for_stop(idle_pid
, SIGSTOP
, PTRACE_CONT
, NULL
);
198 init_proxy(pid
, 1, status
);
202 #ifdef notdef /* Put this back in when it does something useful */
203 static int __init
uml_gdb_init_setup(char *line
, int *add
)
205 gdb_init
= uml_strdup(line
);
209 __uml_setup("gdb=", uml_gdb_init_setup
,
210 "gdb=<channel description>\n\n"
214 static int __init
uml_gdb_pid_setup(char *line
, int *add
)
216 gdb_pid
= strtoul(line
, NULL
, 0);
221 __uml_setup("gdb-pid=", uml_gdb_pid_setup
,
223 " gdb-pid is used to attach an external debugger to UML. This may be\n"
224 " an already-running gdb or a debugger-like process like strace.\n\n"
229 int debugger_signal(int status
, pid_t pid
){ return(0); }
230 void child_signal(pid_t pid
, int status
){ }
231 int init_ptrace_proxy(int idle_pid
, int startup
, int stop
)
233 printf("debug requested when CONFIG_PT_PROXY is off\n");
234 kill_child_dead(idle_pid
);
238 void signal_usr1(int sig
)
240 printf("debug requested when CONFIG_PT_PROXY is off\n");
243 int attach_debugger(int idle_pid
, int pid
, int stop
)
245 printf("attach_debugger called when CONFIG_PT_PROXY "
250 int config_gdb(char *str
)
260 int init_parent_proxy(int pid
)
265 void debugger_parent_signal(int status
, int pid
)
272 * Overrides for Emacs so that we follow Linus's tabbing style.
273 * Emacs will notice this stuff at the end of the file and automatically
274 * adjust the settings for this buffer only. This must remain at the end
276 * ---------------------------------------------------------------------------
278 * c-file-style: "linux"