gdb.c 5.5 KB

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