blob: d284f69f04ee74b6df33ec09bab8202e3bb4733b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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"
Gennady Sharapov4fef0c12006-01-18 17:42:41 -080023#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25extern int debugger_pid;
26extern int debugger_fd;
27extern int debugger_parent;
28
29int detach(int pid, int sig)
30{
31 return(ptrace(PTRACE_DETACH, pid, 0, sig));
32}
33
34int 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
43int cont(int pid)
44{
45 return(ptrace(PTRACE_CONT, pid, 0, 0));
46}
47
48#ifdef UML_CONFIG_PT_PROXY
49
50int debugger_signal(int status, pid_t pid)
51{
52 return(debugger_proxy(status, pid));
53}
54
55void child_signal(pid_t pid, int status)
56{
57 child_proxy(pid, status);
58}
59
60static void gdb_announce(char *dev_name, int dev)
61{
62 printf("gdb assigned device '%s'\n", dev_name);
63}
64
65static 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 */
74static void *xterm_data;
75static int xterm_fd;
76
77extern void *xterm_init(char *, int, struct chan_opts *);
78extern int xterm_open(int, int, int, void *, char **);
79extern void xterm_close(int, void *);
80
81int 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
91static 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
106static void exit_debugger(void)
107{
108 initial_thread_cb(exit_debugger_cb, NULL);
109}
110
111__uml_exitcall(exit_debugger);
112
113struct gdb_data {
114 char *str;
115 int err;
116};
117
Jeff Dike11100b12007-05-06 14:50:57 -0700118extern char *linux_prog;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void config_gdb_cb(void *arg)
121{
122 struct gdb_data *data = arg;
123 void *task;
124 int pid;
125
126 data->err = -1;
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){
134 data->err = 0;
135 gdb_pid = pid;
136 }
137 return;
138 }
139 data->err = 0;
140 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
141 init_proxy(debugger_pid, 0, 0);
142}
143
Jeff Dikef28169d2007-02-10 01:43:53 -0800144int gdb_config(char *str, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct gdb_data data;
147
148 if(*str++ != '=') return(-1);
149 data.str = str;
150 initial_thread_cb(config_gdb_cb, &data);
151 return(data.err);
152}
153
154void remove_gdb_cb(void *unused)
155{
156 exit_debugger_cb(NULL);
157}
158
Jeff Dikef28169d2007-02-10 01:43:53 -0800159int gdb_remove(int unused, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 initial_thread_cb(remove_gdb_cb, NULL);
Jeff Dike29d56cf2005-06-25 14:55:25 -0700162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165void signal_usr1(int sig)
166{
167 if(debugger_pid != -1){
168 printf("The debugger is already running\n");
169 return;
170 }
171 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
172 init_proxy(debugger_pid, 0, 0);
173}
174
175int init_ptrace_proxy(int idle_pid, int startup, int stop)
176{
177 int pid, status;
178
179 pid = start_debugger(linux_prog, startup, stop, &debugger_fd);
180 status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
181 if(pid < 0){
182 cont(idle_pid);
183 return(-1);
184 }
185 init_proxy(pid, 1, status);
186 return(pid);
187}
188
189int attach_debugger(int idle_pid, int pid, int stop)
190{
191 int status = 0, err;
192
193 err = attach(pid);
194 if(err < 0){
195 printf("Failed to attach pid %d, errno = %d\n", pid, -err);
196 return(-1);
197 }
198 if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
199 init_proxy(pid, 1, status);
200 return(pid);
201}
202
203#ifdef notdef /* Put this back in when it does something useful */
204static int __init uml_gdb_init_setup(char *line, int *add)
205{
206 gdb_init = uml_strdup(line);
207 return 0;
208}
209
210__uml_setup("gdb=", uml_gdb_init_setup,
211"gdb=<channel description>\n\n"
212);
213#endif
214
215static int __init uml_gdb_pid_setup(char *line, int *add)
216{
217 gdb_pid = strtoul(line, NULL, 0);
218 *add = 0;
219 return 0;
220}
221
222__uml_setup("gdb-pid=", uml_gdb_pid_setup,
223"gdb-pid=<pid>\n"
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"
226);
227
228#else
229
230int debugger_signal(int status, pid_t pid){ return(0); }
231void child_signal(pid_t pid, int status){ }
232int init_ptrace_proxy(int idle_pid, int startup, int stop)
233{
234 printf("debug requested when CONFIG_PT_PROXY is off\n");
235 kill_child_dead(idle_pid);
236 exit(1);
237}
238
239void signal_usr1(int sig)
240{
241 printf("debug requested when CONFIG_PT_PROXY is off\n");
242}
243
244int attach_debugger(int idle_pid, int pid, int stop)
245{
246 printf("attach_debugger called when CONFIG_PT_PROXY "
247 "is off\n");
248 return(-1);
249}
250
251int config_gdb(char *str)
252{
253 return(-1);
254}
255
256int remove_gdb(void)
257{
258 return(-1);
259}
260
261int init_parent_proxy(int pid)
262{
263 return(-1);
264}
265
266void debugger_parent_signal(int status, int pid)
267{
268}
269
270#endif
271
272/*
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
276 * of the file.
277 * ---------------------------------------------------------------------------
278 * Local variables:
279 * c-file-style: "linux"
280 * End:
281 */