proxy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**********************************************************************
  2. proxy.c
  3. Copyright (C) 1999 Lars Brinkhoff. See the file COPYING for licensing
  4. terms and conditions.
  5. Jeff Dike (jdike@karaya.com) : Modified for integration into uml
  6. **********************************************************************/
  7. /* XXX This file shouldn't refer to CONFIG_* */
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <termios.h>
  15. #include <sys/wait.h>
  16. #include <sys/types.h>
  17. #include <sys/ioctl.h>
  18. #include <asm/unistd.h>
  19. #include "ptrace_user.h"
  20. #include "ptproxy.h"
  21. #include "sysdep.h"
  22. #include "wait.h"
  23. #include "user.h"
  24. #include "os.h"
  25. #include "tempfile.h"
  26. static int debugger_wait(debugger_state *debugger, int *status, int options,
  27. int (*syscall)(debugger_state *debugger, pid_t child),
  28. int (*normal_return)(debugger_state *debugger,
  29. pid_t unused),
  30. int (*wait_return)(debugger_state *debugger,
  31. pid_t unused))
  32. {
  33. if(debugger->real_wait){
  34. debugger->handle_trace = normal_return;
  35. syscall_continue(debugger->pid);
  36. debugger->real_wait = 0;
  37. return(1);
  38. }
  39. debugger->wait_status_ptr = status;
  40. debugger->wait_options = options;
  41. if((debugger->debugee != NULL) && debugger->debugee->event){
  42. syscall_continue(debugger->pid);
  43. wait_for_stop(debugger->pid, SIGTRAP, PTRACE_SYSCALL,
  44. NULL);
  45. (*wait_return)(debugger, -1);
  46. return(0);
  47. }
  48. else if(debugger->wait_options & WNOHANG){
  49. syscall_cancel(debugger->pid, 0);
  50. debugger->handle_trace = syscall;
  51. return(0);
  52. }
  53. else {
  54. syscall_pause(debugger->pid);
  55. debugger->handle_trace = wait_return;
  56. debugger->waiting = 1;
  57. }
  58. return(1);
  59. }
  60. /*
  61. * Handle debugger trap, i.e. syscall.
  62. */
  63. int debugger_syscall(debugger_state *debugger, pid_t child)
  64. {
  65. long arg1, arg2, arg3, arg4, arg5, result;
  66. int syscall, ret = 0;
  67. syscall = get_syscall(debugger->pid, &arg1, &arg2, &arg3, &arg4,
  68. &arg5);
  69. switch(syscall){
  70. case __NR_execve:
  71. /* execve never returns */
  72. debugger->handle_trace = debugger_syscall;
  73. break;
  74. case __NR_ptrace:
  75. if(debugger->debugee->pid != 0) arg2 = debugger->debugee->pid;
  76. if(!debugger->debugee->in_context)
  77. child = debugger->debugee->pid;
  78. result = proxy_ptrace(debugger, arg1, arg2, arg3, arg4, child,
  79. &ret);
  80. syscall_cancel(debugger->pid, result);
  81. debugger->handle_trace = debugger_syscall;
  82. return(ret);
  83. #ifdef __NR_waitpid
  84. case __NR_waitpid:
  85. #endif
  86. case __NR_wait4:
  87. if(!debugger_wait(debugger, (int *) arg2, arg3,
  88. debugger_syscall, debugger_normal_return,
  89. proxy_wait_return))
  90. return(0);
  91. break;
  92. case __NR_kill:
  93. if(!debugger->debugee->in_context)
  94. child = debugger->debugee->pid;
  95. if(arg1 == debugger->debugee->pid){
  96. result = kill(child, arg2);
  97. syscall_cancel(debugger->pid, result);
  98. debugger->handle_trace = debugger_syscall;
  99. return(0);
  100. }
  101. else debugger->handle_trace = debugger_normal_return;
  102. break;
  103. default:
  104. debugger->handle_trace = debugger_normal_return;
  105. }
  106. syscall_continue(debugger->pid);
  107. return(0);
  108. }
  109. /* Used by the tracing thread */
  110. static debugger_state parent;
  111. static int parent_syscall(debugger_state *debugger, int pid);
  112. int init_parent_proxy(int pid)
  113. {
  114. parent = ((debugger_state) { .pid = pid,
  115. .wait_options = 0,
  116. .wait_status_ptr = NULL,
  117. .waiting = 0,
  118. .real_wait = 0,
  119. .expecting_child = 0,
  120. .handle_trace = parent_syscall,
  121. .debugee = NULL } );
  122. return(0);
  123. }
  124. int parent_normal_return(debugger_state *debugger, pid_t unused)
  125. {
  126. debugger->handle_trace = parent_syscall;
  127. syscall_continue(debugger->pid);
  128. return(0);
  129. }
  130. static int parent_syscall(debugger_state *debugger, int pid)
  131. {
  132. long arg1, arg2, arg3, arg4, arg5;
  133. int syscall;
  134. syscall = get_syscall(pid, &arg1, &arg2, &arg3, &arg4, &arg5);
  135. if((syscall == __NR_wait4)
  136. #ifdef __NR_waitpid
  137. || (syscall == __NR_waitpid)
  138. #endif
  139. ){
  140. debugger_wait(&parent, (int *) arg2, arg3, parent_syscall,
  141. parent_normal_return, parent_wait_return);
  142. }
  143. else ptrace(PTRACE_SYSCALL, pid, 0, 0);
  144. return(0);
  145. }
  146. int debugger_normal_return(debugger_state *debugger, pid_t unused)
  147. {
  148. debugger->handle_trace = debugger_syscall;
  149. syscall_continue(debugger->pid);
  150. return(0);
  151. }
  152. void debugger_cancelled_return(debugger_state *debugger, int result)
  153. {
  154. debugger->handle_trace = debugger_syscall;
  155. syscall_set_result(debugger->pid, result);
  156. syscall_continue(debugger->pid);
  157. }
  158. /* Used by the tracing thread */
  159. static debugger_state debugger;
  160. static debugee_state debugee;
  161. void init_proxy (pid_t debugger_pid, int stopped, int status)
  162. {
  163. debugger.pid = debugger_pid;
  164. debugger.handle_trace = debugger_syscall;
  165. debugger.debugee = &debugee;
  166. debugger.waiting = 0;
  167. debugger.real_wait = 0;
  168. debugger.expecting_child = 0;
  169. debugee.pid = 0;
  170. debugee.traced = 0;
  171. debugee.stopped = stopped;
  172. debugee.event = 0;
  173. debugee.zombie = 0;
  174. debugee.died = 0;
  175. debugee.wait_status = status;
  176. debugee.in_context = 1;
  177. }
  178. int debugger_proxy(int status, int pid)
  179. {
  180. int ret = 0, sig;
  181. if(WIFSTOPPED(status)){
  182. sig = WSTOPSIG(status);
  183. if (sig == SIGTRAP)
  184. ret = (*debugger.handle_trace)(&debugger, pid);
  185. else if(sig == SIGCHLD){
  186. if(debugger.expecting_child){
  187. ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  188. debugger.expecting_child = 0;
  189. }
  190. else if(debugger.waiting)
  191. real_wait_return(&debugger);
  192. else {
  193. ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  194. debugger.real_wait = 1;
  195. }
  196. }
  197. else ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  198. }
  199. else if(WIFEXITED(status)){
  200. tracer_panic("debugger (pid %d) exited with status %d",
  201. debugger.pid, WEXITSTATUS(status));
  202. }
  203. else if(WIFSIGNALED(status)){
  204. tracer_panic("debugger (pid %d) exited with signal %d",
  205. debugger.pid, WTERMSIG(status));
  206. }
  207. else {
  208. tracer_panic("proxy got unknown status (0x%x) on debugger "
  209. "(pid %d)", status, debugger.pid);
  210. }
  211. return(ret);
  212. }
  213. void child_proxy(pid_t pid, int status)
  214. {
  215. debugee.event = 1;
  216. debugee.wait_status = status;
  217. if(WIFSTOPPED(status)){
  218. debugee.stopped = 1;
  219. debugger.expecting_child = 1;
  220. kill(debugger.pid, SIGCHLD);
  221. }
  222. else if(WIFEXITED(status) || WIFSIGNALED(status)){
  223. debugee.zombie = 1;
  224. debugger.expecting_child = 1;
  225. kill(debugger.pid, SIGCHLD);
  226. }
  227. else panic("proxy got unknown status (0x%x) on child (pid %d)",
  228. status, pid);
  229. }
  230. void debugger_parent_signal(int status, int pid)
  231. {
  232. int sig;
  233. if(WIFSTOPPED(status)){
  234. sig = WSTOPSIG(status);
  235. if(sig == SIGTRAP) (*parent.handle_trace)(&parent, pid);
  236. else ptrace(PTRACE_SYSCALL, pid, 0, sig);
  237. }
  238. }
  239. void fake_child_exit(void)
  240. {
  241. int status, pid;
  242. child_proxy(1, W_EXITCODE(0, 0));
  243. while(debugger.waiting == 1){
  244. CATCH_EINTR(pid = waitpid(debugger.pid, &status, WUNTRACED));
  245. if(pid != debugger.pid){
  246. printk("fake_child_exit - waitpid failed, "
  247. "errno = %d\n", errno);
  248. return;
  249. }
  250. debugger_proxy(status, debugger.pid);
  251. }
  252. CATCH_EINTR(pid = waitpid(debugger.pid, &status, WUNTRACED));
  253. if(pid != debugger.pid){
  254. printk("fake_child_exit - waitpid failed, "
  255. "errno = %d\n", errno);
  256. return;
  257. }
  258. if(ptrace(PTRACE_DETACH, debugger.pid, 0, SIGCONT) < 0)
  259. printk("fake_child_exit - PTRACE_DETACH failed, errno = %d\n",
  260. errno);
  261. }
  262. char gdb_init_string[] =
  263. "att 1 \n\
  264. b panic \n\
  265. b stop \n\
  266. handle SIGWINCH nostop noprint pass \n\
  267. ";
  268. int start_debugger(char *prog, int startup, int stop, int *fd_out)
  269. {
  270. int slave, child;
  271. slave = open_gdb_chan();
  272. child = fork();
  273. if(child == 0){
  274. char *tempname = NULL;
  275. int fd;
  276. if(setsid() < 0) perror("setsid");
  277. if((dup2(slave, 0) < 0) || (dup2(slave, 1) < 0) ||
  278. (dup2(slave, 2) < 0)){
  279. printk("start_debugger : dup2 failed, errno = %d\n",
  280. errno);
  281. exit(1);
  282. }
  283. if(ioctl(0, TIOCSCTTY, 0) < 0){
  284. printk("start_debugger : TIOCSCTTY failed, "
  285. "errno = %d\n", errno);
  286. exit(1);
  287. }
  288. if(tcsetpgrp (1, os_getpid()) < 0){
  289. printk("start_debugger : tcsetpgrp failed, "
  290. "errno = %d\n", errno);
  291. #ifdef notdef
  292. exit(1);
  293. #endif
  294. }
  295. fd = make_tempfile("/tmp/gdb_init-XXXXXX", &tempname, 0);
  296. if(fd < 0){
  297. printk("start_debugger : make_tempfile failed,"
  298. "err = %d\n", -fd);
  299. exit(1);
  300. }
  301. os_write_file(fd, gdb_init_string,
  302. sizeof(gdb_init_string) - 1);
  303. if(startup){
  304. if(stop){
  305. os_write_file(fd, "b start_kernel\n",
  306. strlen("b start_kernel\n"));
  307. }
  308. os_write_file(fd, "c\n", strlen("c\n"));
  309. }
  310. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
  311. printk("start_debugger : PTRACE_TRACEME failed, "
  312. "errno = %d\n", errno);
  313. exit(1);
  314. }
  315. execlp("gdb", "gdb", "--command", tempname, prog, NULL);
  316. printk("start_debugger : exec of gdb failed, errno = %d\n",
  317. errno);
  318. }
  319. if(child < 0){
  320. printk("start_debugger : fork for gdb failed, errno = %d\n",
  321. errno);
  322. return(-1);
  323. }
  324. *fd_out = slave;
  325. return(child);
  326. }
  327. /*
  328. * Overrides for Emacs so that we follow Linus's tabbing style.
  329. * Emacs will notice this stuff at the end of the file and automatically
  330. * adjust the settings for this buffer only. This must remain at the end
  331. * of the file.
  332. * ---------------------------------------------------------------------------
  333. * Local variables:
  334. * c-file-style: "linux"
  335. * End:
  336. */