[PATCH] uml: return hotplug errors to host
[linux-2.6/kvm.git] / arch / um / kernel / tt / gdb.c
blob8eba8f7dca683cefaebb04f51be819a3f8362e68
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <signal.h>
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"
16 #include "init.h"
17 #include "user.h"
18 #include "debug.h"
19 #include "kern_util.h"
20 #include "user_util.h"
21 #include "tt.h"
22 #include "sysdep/thread.h"
23 #include "os.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));
34 int attach(int pid)
36 int err;
38 err = ptrace(PTRACE_ATTACH, pid, 0, 0);
39 if(err < 0) return(-errno);
40 else return(err);
43 int cont(int pid)
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",
68 .raw = 0,
69 .tramp_stack = 0,
70 .in_kernel = 0,
73 /* Accessed by the tracing thread, which automatically serializes access */
74 static void *xterm_data;
75 static int xterm_fd;
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);
88 return(xterm_fd);
91 static void exit_debugger_cb(void *unused)
93 if(debugger_pid != -1){
94 if(gdb_pid != -1){
95 fake_child_exit();
96 gdb_pid = -1;
98 else kill_child_dead(debugger_pid);
99 debugger_pid = -1;
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);
113 struct gdb_data {
114 char *str;
115 int err;
118 static void config_gdb_cb(void *arg)
120 struct gdb_data *data = arg;
121 void *task;
122 int pid;
124 data->err = -1;
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){
132 data->err = 0;
133 gdb_pid = pid;
135 return;
137 data->err = 0;
138 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
139 init_proxy(debugger_pid, 0, 0);
142 int gdb_config(char *str, char **error_out)
144 struct gdb_data data;
146 if(*str++ != '=') return(-1);
147 data.str = str;
148 initial_thread_cb(config_gdb_cb, &data);
149 return(data.err);
152 void remove_gdb_cb(void *unused)
154 exit_debugger_cb(NULL);
157 int gdb_remove(int unused, char **error_out)
159 initial_thread_cb(remove_gdb_cb, NULL);
160 return 0;
163 void signal_usr1(int sig)
165 if(debugger_pid != -1){
166 printf("The debugger is already running\n");
167 return;
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)
175 int pid, status;
177 pid = start_debugger(linux_prog, startup, stop, &debugger_fd);
178 status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
179 if(pid < 0){
180 cont(idle_pid);
181 return(-1);
183 init_proxy(pid, 1, status);
184 return(pid);
187 int attach_debugger(int idle_pid, int pid, int stop)
189 int status = 0, err;
191 err = attach(pid);
192 if(err < 0){
193 printf("Failed to attach pid %d, errno = %d\n", pid, -err);
194 return(-1);
196 if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
197 init_proxy(pid, 1, status);
198 return(pid);
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);
205 return 0;
208 __uml_setup("gdb=", uml_gdb_init_setup,
209 "gdb=<channel description>\n\n"
211 #endif
213 static int __init uml_gdb_pid_setup(char *line, int *add)
215 gdb_pid = strtoul(line, NULL, 0);
216 *add = 0;
217 return 0;
220 __uml_setup("gdb-pid=", uml_gdb_pid_setup,
221 "gdb-pid=<pid>\n"
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"
226 #else
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);
234 exit(1);
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 "
245 "is off\n");
246 return(-1);
249 int config_gdb(char *str)
251 return(-1);
254 int remove_gdb(void)
256 return(-1);
259 int init_parent_proxy(int pid)
261 return(-1);
264 void debugger_parent_signal(int status, int pid)
268 #endif
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
274 * of the file.
275 * ---------------------------------------------------------------------------
276 * Local variables:
277 * c-file-style: "linux"
278 * End: