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 "tt.h" 21#include "sysdep/thread.h" 22#include "os.h" 23 24extern int debugger_pid; 25extern int debugger_fd; 26extern int debugger_parent; 27 28int detach(int pid, int sig) 29{ 30 return(ptrace(PTRACE_DETACH, pid, 0, sig)); 31} 32 33int attach(int pid) 34{ 35 int err; 36 37 err = ptrace(PTRACE_ATTACH, pid, 0, 0); 38 if(err < 0) return(-errno); 39 else return(err); 40} 41 42int cont(int pid) 43{ 44 return(ptrace(PTRACE_CONT, pid, 0, 0)); 45} 46 47#ifdef UML_CONFIG_PT_PROXY 48 49int debugger_signal(int status, pid_t pid) 50{ 51 return(debugger_proxy(status, pid)); 52} 53 54void child_signal(pid_t pid, int status) 55{ 56 child_proxy(pid, status); 57} 58 59static void gdb_announce(char *dev_name, int dev) 60{ 61 printf("gdb assigned device '%s'\n", dev_name); 62} 63 64static struct chan_opts opts = { 65 .announce = gdb_announce, 66 .xterm_title = "UML kernel debugger", 67 .raw = 0, 68 .tramp_stack = 0, 69 .in_kernel = 0, 70}; 71 72/* Accessed by the tracing thread, which automatically serializes access */ 73static void *xterm_data; 74static int xterm_fd; 75 76extern void *xterm_init(char *, int, struct chan_opts *); 77extern int xterm_open(int, int, int, void *, char **); 78extern void xterm_close(int, void *); 79 80int open_gdb_chan(void) 81{ 82 char stack[UM_KERN_PAGE_SIZE], *dummy; 83 84 opts.tramp_stack = (unsigned long) stack; 85 xterm_data = xterm_init("", 0, &opts); 86 xterm_fd = xterm_open(1, 1, 1, xterm_data, &dummy); 87 return(xterm_fd); 88} 89 90static void exit_debugger_cb(void *unused) 91{ 92 if(debugger_pid != -1){ 93 if(gdb_pid != -1){ 94 fake_child_exit(); 95 gdb_pid = -1; 96 } 97 else kill_child_dead(debugger_pid); 98 debugger_pid = -1; 99 if(debugger_parent != -1) 100 detach(debugger_parent, SIGINT); 101 } 102 if(xterm_data != NULL) xterm_close(xterm_fd, xterm_data); 103} 104 105static void exit_debugger(void) 106{ 107 initial_thread_cb(exit_debugger_cb, NULL); 108} 109 110__uml_exitcall(exit_debugger); 111 112struct gdb_data { 113 char *str; 114 int err; 115}; 116 117extern char *linux_prog; 118 119static void config_gdb_cb(void *arg) 120{ 121 struct gdb_data *data = arg; 122 void *task; 123 int pid; 124 125 data->err = -1; 126 if(debugger_pid != -1) exit_debugger_cb(NULL); 127 if(!strncmp(data->str, "pid,", strlen("pid,"))){ 128 data->str += strlen("pid,"); 129 pid = strtoul(data->str, NULL, 0); 130 task = cpu_tasks[0].task; 131 debugger_pid = attach_debugger(TASK_EXTERN_PID(task), pid, 0); 132 if(debugger_pid != -1){ 133 data->err = 0; 134 gdb_pid = pid; 135 } 136 return; 137 } 138 data->err = 0; 139 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd); 140 init_proxy(debugger_pid, 0, 0); 141} 142 143int gdb_config(char *str, char **error_out) 144{ 145 struct gdb_data data; 146 147 if(*str++ != '=') return(-1); 148 data.str = str; 149 initial_thread_cb(config_gdb_cb, &data); 150 return(data.err); 151} 152 153void remove_gdb_cb(void *unused) 154{ 155 exit_debugger_cb(NULL); 156} 157 158int gdb_remove(int unused, char **error_out) 159{ 160 initial_thread_cb(remove_gdb_cb, NULL); 161 return 0; 162} 163 164void signal_usr1(int sig) 165{ 166 if(debugger_pid != -1){ 167 printf("The debugger is already running\n"); 168 return; 169 } 170 debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd); 171 init_proxy(debugger_pid, 0, 0); 172} 173 174int init_ptrace_proxy(int idle_pid, int startup, int stop) 175{ 176 int pid, status; 177 178 pid = start_debugger(linux_prog, startup, stop, &debugger_fd); 179 status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL); 180 if(pid < 0){ 181 cont(idle_pid); 182 return(-1); 183 } 184 init_proxy(pid, 1, status); 185 return(pid); 186} 187 188int attach_debugger(int idle_pid, int pid, int stop) 189{ 190 int status = 0, err; 191 192 err = attach(pid); 193 if(err < 0){ 194 printf("Failed to attach pid %d, errno = %d\n", pid, -err); 195 return(-1); 196 } 197 if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL); 198 init_proxy(pid, 1, status); 199 return(pid); 200} 201 202#ifdef notdef /* Put this back in when it does something useful */ 203static int __init uml_gdb_init_setup(char *line, int *add) 204{ 205 gdb_init = uml_strdup(line); 206 return 0; 207} 208 209__uml_setup("gdb=", uml_gdb_init_setup, 210"gdb=<channel description>\n\n" 211); 212#endif 213 214static int __init uml_gdb_pid_setup(char *line, int *add) 215{ 216 gdb_pid = strtoul(line, NULL, 0); 217 *add = 0; 218 return 0; 219} 220 221__uml_setup("gdb-pid=", uml_gdb_pid_setup, 222"gdb-pid=<pid>\n" 223" gdb-pid is used to attach an external debugger to UML. This may be\n" 224" an already-running gdb or a debugger-like process like strace.\n\n" 225); 226 227#else 228 229int debugger_signal(int status, pid_t pid){ return(0); } 230void child_signal(pid_t pid, int status){ } 231int init_ptrace_proxy(int idle_pid, int startup, int stop) 232{ 233 printf("debug requested when CONFIG_PT_PROXY is off\n"); 234 kill_child_dead(idle_pid); 235 exit(1); 236} 237 238void signal_usr1(int sig) 239{ 240 printf("debug requested when CONFIG_PT_PROXY is off\n"); 241} 242 243int attach_debugger(int idle_pid, int pid, int stop) 244{ 245 printf("attach_debugger called when CONFIG_PT_PROXY " 246 "is off\n"); 247 return(-1); 248} 249 250int config_gdb(char *str) 251{ 252 return(-1); 253} 254 255int remove_gdb(void) 256{ 257 return(-1); 258} 259 260int init_parent_proxy(int pid) 261{ 262 return(-1); 263} 264 265void debugger_parent_signal(int status, int pid) 266{ 267} 268 269#endif 270 271/* 272 * Overrides for Emacs so that we follow Linus's tabbing style. 273 * Emacs will notice this stuff at the end of the file and automatically 274 * adjust the settings for this buffer only. This must remain at the end 275 * of the file. 276 * --------------------------------------------------------------------------- 277 * Local variables: 278 * c-file-style: "linux" 279 * End: 280 */ 281