gdb.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "user_util.h"
  20. #include "tt.h"
  21. #include "sysdep/thread.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. static void config_gdb_cb(void *arg)
  98. {
  99. struct gdb_data *data = arg;
  100. void *task;
  101. int pid;
  102. data->err = -1;
  103. if(debugger_pid != -1) exit_debugger_cb(NULL);
  104. if(!strncmp(data->str, "pid,", strlen("pid,"))){
  105. data->str += strlen("pid,");
  106. pid = strtoul(data->str, NULL, 0);
  107. task = cpu_tasks[0].task;
  108. debugger_pid = attach_debugger(TASK_EXTERN_PID(task), pid, 0);
  109. if(debugger_pid != -1){
  110. data->err = 0;
  111. gdb_pid = pid;
  112. }
  113. return;
  114. }
  115. data->err = 0;
  116. debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
  117. init_proxy(debugger_pid, 0, 0);
  118. }
  119. int gdb_config(char *str)
  120. {
  121. struct gdb_data data;
  122. if(*str++ != '=') return(-1);
  123. data.str = str;
  124. initial_thread_cb(config_gdb_cb, &data);
  125. return(data.err);
  126. }
  127. void remove_gdb_cb(void *unused)
  128. {
  129. exit_debugger_cb(NULL);
  130. }
  131. int gdb_remove(int unused)
  132. {
  133. initial_thread_cb(remove_gdb_cb, NULL);
  134. return 0;
  135. }
  136. void signal_usr1(int sig)
  137. {
  138. if(debugger_pid != -1){
  139. printf("The debugger is already running\n");
  140. return;
  141. }
  142. debugger_pid = start_debugger(linux_prog, 0, 0, &debugger_fd);
  143. init_proxy(debugger_pid, 0, 0);
  144. }
  145. int init_ptrace_proxy(int idle_pid, int startup, int stop)
  146. {
  147. int pid, status;
  148. pid = start_debugger(linux_prog, startup, stop, &debugger_fd);
  149. status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
  150. if(pid < 0){
  151. cont(idle_pid);
  152. return(-1);
  153. }
  154. init_proxy(pid, 1, status);
  155. return(pid);
  156. }
  157. int attach_debugger(int idle_pid, int pid, int stop)
  158. {
  159. int status = 0, err;
  160. err = attach(pid);
  161. if(err < 0){
  162. printf("Failed to attach pid %d, errno = %d\n", pid, -err);
  163. return(-1);
  164. }
  165. if(stop) status = wait_for_stop(idle_pid, SIGSTOP, PTRACE_CONT, NULL);
  166. init_proxy(pid, 1, status);
  167. return(pid);
  168. }
  169. #ifdef notdef /* Put this back in when it does something useful */
  170. static int __init uml_gdb_init_setup(char *line, int *add)
  171. {
  172. gdb_init = uml_strdup(line);
  173. return 0;
  174. }
  175. __uml_setup("gdb=", uml_gdb_init_setup,
  176. "gdb=<channel description>\n\n"
  177. );
  178. #endif
  179. static int __init uml_gdb_pid_setup(char *line, int *add)
  180. {
  181. gdb_pid = strtoul(line, NULL, 0);
  182. *add = 0;
  183. return 0;
  184. }
  185. __uml_setup("gdb-pid=", uml_gdb_pid_setup,
  186. "gdb-pid=<pid>\n"
  187. " gdb-pid is used to attach an external debugger to UML. This may be\n"
  188. " an already-running gdb or a debugger-like process like strace.\n\n"
  189. );
  190. #else
  191. int debugger_signal(int status, pid_t pid){ return(0); }
  192. void child_signal(pid_t pid, int status){ }
  193. int init_ptrace_proxy(int idle_pid, int startup, int stop)
  194. {
  195. printf("debug requested when CONFIG_PT_PROXY is off\n");
  196. kill_child_dead(idle_pid);
  197. exit(1);
  198. }
  199. void signal_usr1(int sig)
  200. {
  201. printf("debug requested when CONFIG_PT_PROXY is off\n");
  202. }
  203. int attach_debugger(int idle_pid, int pid, int stop)
  204. {
  205. printf("attach_debugger called when CONFIG_PT_PROXY "
  206. "is off\n");
  207. return(-1);
  208. }
  209. int config_gdb(char *str)
  210. {
  211. return(-1);
  212. }
  213. int remove_gdb(void)
  214. {
  215. return(-1);
  216. }
  217. int init_parent_proxy(int pid)
  218. {
  219. return(-1);
  220. }
  221. void debugger_parent_signal(int status, int pid)
  222. {
  223. }
  224. #endif
  225. /*
  226. * Overrides for Emacs so that we follow Linus's tabbing style.
  227. * Emacs will notice this stuff at the end of the file and automatically
  228. * adjust the settings for this buffer only. This must remain at the end
  229. * of the file.
  230. * ---------------------------------------------------------------------------
  231. * Local variables:
  232. * c-file-style: "linux"
  233. * End:
  234. */