Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-drm-fsl-dcu.git] / arch / um / kernel / tt / gdb.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
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"
24
25 extern int debugger_pid;
26 extern int debugger_fd;
27 extern int debugger_parent;
28
29 int detach(int pid, int sig)
30 {
31         return(ptrace(PTRACE_DETACH, pid, 0, sig));
32 }
33
34 int attach(int pid)
35 {
36         int err;
37
38         err = ptrace(PTRACE_ATTACH, pid, 0, 0);
39         if(err < 0) return(-errno);
40         else return(err);
41 }
42
43 int cont(int pid)
44 {
45         return(ptrace(PTRACE_CONT, pid, 0, 0));
46 }
47
48 #ifdef UML_CONFIG_PT_PROXY
49
50 int debugger_signal(int status, pid_t pid)
51 {
52         return(debugger_proxy(status, pid));
53 }
54
55 void child_signal(pid_t pid, int status)
56 {
57         child_proxy(pid, status);
58 }
59
60 static void gdb_announce(char *dev_name, int dev)
61 {
62         printf("gdb assigned device '%s'\n", dev_name);
63 }
64
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,
71 };
72
73 /* Accessed by the tracing thread, which automatically serializes access */
74 static void *xterm_data;
75 static int xterm_fd;
76
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 *);
80
81 int open_gdb_chan(void)
82 {
83         char stack[UM_KERN_PAGE_SIZE], *dummy;
84
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);
89 }
90
91 static void exit_debugger_cb(void *unused)
92 {
93         if(debugger_pid != -1){
94                 if(gdb_pid != -1){
95                         fake_child_exit();
96                         gdb_pid = -1;
97                 }
98                 else kill_child_dead(debugger_pid);
99                 debugger_pid = -1;
100                 if(debugger_parent != -1)
101                         detach(debugger_parent, SIGINT);
102         }
103         if(xterm_data != NULL) xterm_close(xterm_fd, xterm_data);
104 }
105
106 static void exit_debugger(void)
107 {
108         initial_thread_cb(exit_debugger_cb, NULL);
109 }
110
111 __uml_exitcall(exit_debugger);
112
113 struct gdb_data {
114         char *str;
115         int err;
116 };
117
118 static void config_gdb_cb(void *arg)
119 {
120         struct gdb_data *data = arg;
121         void *task;
122         int pid;
123
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;
134                 }
135                 return;
136         }
137         data->err = 0;
138         debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
139         init_proxy(debugger_pid, 0, 0);
140 }
141
142 int gdb_config(char *str, char **error_out)
143 {
144         struct gdb_data data;
145
146         if(*str++ != '=') return(-1);
147         data.str = str;
148         initial_thread_cb(config_gdb_cb, &data);
149         return(data.err);
150 }
151
152 void remove_gdb_cb(void *unused)
153 {
154         exit_debugger_cb(NULL);
155 }
156
157 int gdb_remove(int unused, char **error_out)
158 {
159         initial_thread_cb(remove_gdb_cb, NULL);
160         return 0;
161 }
162
163 void signal_usr1(int sig)
164 {
165         if(debugger_pid != -1){
166                 printf("The debugger is already running\n");
167                 return;
168         }
169         debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
170         init_proxy(debugger_pid, 0, 0);
171 }
172
173 int init_ptrace_proxy(int idle_pid, int startup, int stop)
174 {
175         int pid, status;
176
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);
182         }
183         init_proxy(pid, 1, status);
184         return(pid);
185 }
186
187 int attach_debugger(int idle_pid, int pid, int stop)
188 {
189         int status = 0, err;
190
191         err = attach(pid);
192         if(err < 0){
193                 printf("Failed to attach pid %d, errno = %d\n", pid, -err);
194                 return(-1);
195         }
196         if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
197         init_proxy(pid, 1, status);
198         return(pid);
199 }
200
201 #ifdef notdef /* Put this back in when it does something useful */
202 static int __init uml_gdb_init_setup(char *line, int *add)
203 {
204         gdb_init = uml_strdup(line);
205         return 0;
206 }
207
208 __uml_setup("gdb=", uml_gdb_init_setup, 
209 "gdb=<channel description>\n\n"
210 );
211 #endif
212
213 static int __init uml_gdb_pid_setup(char *line, int *add)
214 {
215         gdb_pid = strtoul(line, NULL, 0);
216         *add = 0;
217         return 0;
218 }
219
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"
224 );
225
226 #else
227
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)
231 {
232         printf("debug requested when CONFIG_PT_PROXY is off\n");
233         kill_child_dead(idle_pid);
234         exit(1);
235 }
236
237 void signal_usr1(int sig)
238 {
239         printf("debug requested when CONFIG_PT_PROXY is off\n");
240 }
241
242 int attach_debugger(int idle_pid, int pid, int stop)
243 {
244         printf("attach_debugger called when CONFIG_PT_PROXY "
245                "is off\n");
246         return(-1);
247 }
248
249 int config_gdb(char *str)
250 {
251         return(-1);
252 }
253
254 int remove_gdb(void)
255 {
256         return(-1);
257 }
258
259 int init_parent_proxy(int pid)
260 {
261         return(-1);
262 }
263
264 void debugger_parent_signal(int status, int pid)
265 {
266 }
267
268 #endif
269
270 /*
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:
279  */