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"
20 #include "user_util.h"
22 #include "sysdep/thread.h"
25 extern int debugger_pid;
26 extern int debugger_fd;
27 extern int debugger_parent;
29 int detach(int pid, int sig)
31 return(ptrace(PTRACE_DETACH, pid, 0, sig));
38 err = ptrace(PTRACE_ATTACH, pid, 0, 0);
39 if(err < 0) return(-errno);
45 return(ptrace(PTRACE_CONT, pid, 0, 0));
48 #ifdef UML_CONFIG_PT_PROXY
50 int debugger_signal(int status, pid_t pid)
52 return(debugger_proxy(status, pid));
55 void child_signal(pid_t pid, int status)
57 child_proxy(pid, status);
60 static void gdb_announce(char *dev_name, int dev)
62 printf("gdb assigned device '%s'\n", dev_name);
65 static struct chan_opts opts = {
66 .announce = gdb_announce,
67 .xterm_title = "UML kernel debugger",
73 /* Accessed by the tracing thread, which automatically serializes access */
74 static void *xterm_data;
77 extern void *xterm_init(char *, int, struct chan_opts *);
78 extern int xterm_open(int, int, int, void *, char **);
79 extern void xterm_close(int, void *);
81 int open_gdb_chan(void)
83 char stack[UM_KERN_PAGE_SIZE], *dummy;
85 opts.tramp_stack = (unsigned long) stack;
86 xterm_data = xterm_init("", 0, &opts);
87 xterm_fd = xterm_open(1, 1, 1, xterm_data, &dummy);
91 static void exit_debugger_cb(void *unused)
93 if(debugger_pid != -1){
98 else kill_child_dead(debugger_pid);
100 if(debugger_parent != -1)
101 detach(debugger_parent, SIGINT);
103 if(xterm_data != NULL) xterm_close(xterm_fd, xterm_data);
106 static void exit_debugger(void)
108 initial_thread_cb(exit_debugger_cb, NULL);
111 __uml_exitcall(exit_debugger);
118 extern char *linux_prog;
120 static void config_gdb_cb(void *arg)
122 struct gdb_data *data = arg;
127 if(debugger_pid != -1) exit_debugger_cb(NULL);
128 if(!strncmp(data->str, "pid,", strlen("pid,"))){
129 data->str += strlen("pid,");
130 pid = strtoul(data->str, NULL, 0);
131 task = cpu_tasks[0].task;
132 debugger_pid = attach_debugger(TASK_EXTERN_PID(task), pid, 0);
133 if(debugger_pid != -1){
140 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
141 init_proxy(debugger_pid, 0, 0);
144 int gdb_config(char *str, char **error_out)
146 struct gdb_data data;
148 if(*str++ != '=') return(-1);
150 initial_thread_cb(config_gdb_cb, &data);
154 void remove_gdb_cb(void *unused)
156 exit_debugger_cb(NULL);
159 int gdb_remove(int unused, char **error_out)
161 initial_thread_cb(remove_gdb_cb, NULL);
165 void signal_usr1(int sig)
167 if(debugger_pid != -1){
168 printf("The debugger is already running\n");
171 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
172 init_proxy(debugger_pid, 0, 0);
175 int init_ptrace_proxy(int idle_pid, int startup, int stop)
179 pid = start_debugger(linux_prog, startup, stop, &debugger_fd);
180 status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
185 init_proxy(pid, 1, status);
189 int attach_debugger(int idle_pid, int pid, int stop)
195 printf("Failed to attach pid %d, errno = %d\n", pid, -err);
198 if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
199 init_proxy(pid, 1, status);
203 #ifdef notdef /* Put this back in when it does something useful */
204 static int __init uml_gdb_init_setup(char *line, int *add)
206 gdb_init = uml_strdup(line);
210 __uml_setup("gdb=", uml_gdb_init_setup,
211 "gdb=<channel description>\n\n"
215 static int __init uml_gdb_pid_setup(char *line, int *add)
217 gdb_pid = strtoul(line, NULL, 0);
222 __uml_setup("gdb-pid=", uml_gdb_pid_setup,
224 " gdb-pid is used to attach an external debugger to UML. This may be\n"
225 " an already-running gdb or a debugger-like process like strace.\n\n"
230 int debugger_signal(int status, pid_t pid){ return(0); }
231 void child_signal(pid_t pid, int status){ }
232 int init_ptrace_proxy(int idle_pid, int startup, int stop)
234 printf("debug requested when CONFIG_PT_PROXY is off\n");
235 kill_child_dead(idle_pid);
239 void signal_usr1(int sig)
241 printf("debug requested when CONFIG_PT_PROXY is off\n");
244 int attach_debugger(int idle_pid, int pid, int stop)
246 printf("attach_debugger called when CONFIG_PT_PROXY "
251 int config_gdb(char *str)
261 int init_parent_proxy(int pid)
266 void debugger_parent_signal(int status, int pid)
273 * Overrides for Emacs so that we follow Linus's tabbing style.
274 * Emacs will notice this stuff at the end of the file and automatically
275 * adjust the settings for this buffer only. This must remain at the end
277 * ---------------------------------------------------------------------------
279 * c-file-style: "linux"