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 static void config_gdb_cb(void *arg)
120 struct gdb_data *data = arg;
125 if(debugger_pid != -1) exit_debugger_cb(NULL);
126 if(!strncmp(data->str, "pid,", strlen("pid,"))){
127 data->str += strlen("pid,");
128 pid = strtoul(data->str, NULL, 0);
129 task = cpu_tasks[0].task;
130 debugger_pid = attach_debugger(TASK_EXTERN_PID(task), pid, 0);
131 if(debugger_pid != -1){
138 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
139 init_proxy(debugger_pid, 0, 0);
142 int gdb_config(char *str)
144 struct gdb_data data;
146 if(*str++ != '=') return(-1);
148 initial_thread_cb(config_gdb_cb, &data);
152 void remove_gdb_cb(void *unused)
154 exit_debugger_cb(NULL);
157 int gdb_remove(int unused)
159 initial_thread_cb(remove_gdb_cb, NULL);
163 void signal_usr1(int sig)
165 if(debugger_pid != -1){
166 printf("The debugger is already running\n");
169 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
170 init_proxy(debugger_pid, 0, 0);
173 int init_ptrace_proxy(int idle_pid, int startup, int stop)
177 pid = start_debugger(linux_prog, startup, stop, &debugger_fd);
178 status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
183 init_proxy(pid, 1, status);
187 int attach_debugger(int idle_pid, int pid, int stop)
193 printf("Failed to attach pid %d, errno = %d\n", pid, -err);
196 if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
197 init_proxy(pid, 1, status);
201 #ifdef notdef /* Put this back in when it does something useful */
202 static int __init uml_gdb_init_setup(char *line, int *add)
204 gdb_init = uml_strdup(line);
208 __uml_setup("gdb=", uml_gdb_init_setup,
209 "gdb=<channel description>\n\n"
213 static int __init uml_gdb_pid_setup(char *line, int *add)
215 gdb_pid = strtoul(line, NULL, 0);
220 __uml_setup("gdb-pid=", uml_gdb_pid_setup,
222 " gdb-pid is used to attach an external debugger to UML. This may be\n"
223 " an already-running gdb or a debugger-like process like strace.\n\n"
228 int debugger_signal(int status, pid_t pid){ return(0); }
229 void child_signal(pid_t pid, int status){ }
230 int init_ptrace_proxy(int idle_pid, int startup, int stop)
232 printf("debug requested when CONFIG_PT_PROXY is off\n");
233 kill_child_dead(idle_pid);
237 void signal_usr1(int sig)
239 printf("debug requested when CONFIG_PT_PROXY is off\n");
242 int attach_debugger(int idle_pid, int pid, int stop)
244 printf("attach_debugger called when CONFIG_PT_PROXY "
249 int config_gdb(char *str)
259 int init_parent_proxy(int pid)
264 void debugger_parent_signal(int status, int pid)
271 * Overrides for Emacs so that we follow Linus's tabbing style.
272 * Emacs will notice this stuff at the end of the file and automatically
273 * adjust the settings for this buffer only. This must remain at the end
275 * ---------------------------------------------------------------------------
277 * c-file-style: "linux"