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_util.h"
  24. #include "user.h"
  25. #include "os.h"
  26. #include "tempfile.h"
  27. static int debugger_wait(debugger_state *debugger, int *status, int options,
  28. int (*syscall)(debugger_state *debugger, pid_t child),
  29. int (*normal_return)(debugger_state *debugger,
  30. pid_t unused),
  31. int (*wait_return)(debugger_state *debugger,
  32. pid_t unused))
  33. {
  34. if(debugger->real_wait){
  35. debugger->handle_trace = normal_return;
  36. syscall_continue(debugger->pid);
  37. debugger->real_wait = 0;
  38. return(1);
  39. }
  40. debugger->wait_status_ptr = status;
  41. debugger->wait_options = options;
  42. if((debugger->debugee != NULL) && debugger->debugee->event){
  43. syscall_continue(debugger->pid);
  44. wait_for_stop(debugger->pid, SIGTRAP, PTRACE_SYSCALL,
  45. NULL);
  46. (*wait_return)(debugger, -1);
  47. return(0);
  48. }
  49. else if(debugger->wait_options & WNOHANG){
  50. syscall_cancel(debugger->pid, 0);
  51. debugger->handle_trace = syscall;
  52. return(0);
  53. }
  54. else {
  55. syscall_pause(debugger->pid);
  56. debugger->handle_trace = wait_return;
  57. debugger->waiting = 1;
  58. }
  59. return(1);
  60. }
  61. /*
  62. * Handle debugger trap, i.e. syscall.
  63. */
  64. int debugger_syscall(debugger_state *debugger, pid_t child)
  65. {
  66. long arg1, arg2, arg3, arg4, arg5, result;
  67. int syscall, ret = 0;
  68. syscall = get_syscall(debugger->pid, &arg1, &arg2, &arg3, &arg4,
  69. &arg5);
  70. switch(syscall){
  71. case __NR_execve:
  72. /* execve never returns */
  73. debugger->handle_trace = debugger_syscall;
  74. break;
  75. case __NR_ptrace:
  76. if(debugger->debugee->pid != 0) arg2 = debugger->debugee->pid;
  77. if(!debugger->debugee->in_context)
  78. child = debugger->debugee->pid;
  79. result = proxy_ptrace(debugger, arg1, arg2, arg3, arg4, child,
  80. &ret);
  81. syscall_cancel(debugger->pid, result);
  82. debugger->handle_trace = debugger_syscall;
  83. return(ret);
  84. #ifdef __NR_waitpid
  85. case __NR_waitpid:
  86. #endif
  87. case __NR_wait4:
  88. if(!debugger_wait(debugger, (int *) arg2, arg3,
  89. debugger_syscall, debugger_normal_return,
  90. proxy_wait_return))
  91. return(0);
  92. break;
  93. case __NR_kill:
  94. if(!debugger->debugee->in_context)
  95. child = debugger->debugee->pid;
  96. if(arg1 == debugger->debugee->pid){
  97. result = kill(child, arg2);
  98. syscall_cancel(debugger->pid, result);
  99. debugger->handle_trace = debugger_syscall;
  100. return(0);
  101. }
  102. else debugger->handle_trace = debugger_normal_return;
  103. break;
  104. default:
  105. debugger->handle_trace = debugger_normal_return;
  106. }
  107. syscall_continue(debugger->pid);
  108. return(0);
  109. }
  110. /* Used by the tracing thread */
  111. static debugger_state parent;
  112. static int parent_syscall(debugger_state *debugger, int pid);
  113. int init_parent_proxy(int pid)
  114. {
  115. parent = ((debugger_state) { .pid = pid,
  116. .wait_options = 0,
  117. .wait_status_ptr = NULL,
  118. .waiting = 0,
  119. .real_wait = 0,
  120. .expecting_child = 0,
  121. .handle_trace = parent_syscall,
  122. .debugee = NULL } );
  123. return(0);
  124. }
  125. int parent_normal_return(debugger_state *debugger, pid_t unused)
  126. {
  127. debugger->handle_trace = parent_syscall;
  128. syscall_continue(debugger->pid);
  129. return(0);
  130. }
  131. static int parent_syscall(debugger_state *debugger, int pid)
  132. {
  133. long arg1, arg2, arg3, arg4, arg5;
  134. int syscall;
  135. syscall = get_syscall(pid, &arg1, &arg2, &arg3, &arg4, &arg5);
  136. if((syscall == __NR_wait4)
  137. #ifdef __NR_waitpid
  138. || (syscall == __NR_waitpid)
  139. #endif
  140. ){
  141. debugger_wait(&parent, (int *) arg2, arg3, parent_syscall,
  142. parent_normal_return, parent_wait_return);
  143. }
  144. else ptrace(PTRACE_SYSCALL, pid, 0, 0);
  145. return(0);
  146. }
  147. int debugger_normal_return(debugger_state *debugger, pid_t unused)
  148. {
  149. debugger->handle_trace = debugger_syscall;
  150. syscall_continue(debugger->pid);
  151. return(0);
  152. }
  153. void debugger_cancelled_return(debugger_state *debugger, int result)
  154. {
  155. debugger->handle_trace = debugger_syscall;
  156. syscall_set_result(debugger->pid, result);
  157. syscall_continue(debugger->pid);
  158. }
  159. /* Used by the tracing thread */
  160. static debugger_state debugger;
  161. static debugee_state debugee;
  162. void init_proxy (pid_t debugger_pid, int stopped, int status)
  163. {
  164. debugger.pid = debugger_pid;
  165. debugger.handle_trace = debugger_syscall;
  166. debugger.debugee = &debugee;
  167. debugger.waiting = 0;
  168. debugger.real_wait = 0;
  169. debugger.expecting_child = 0;
  170. debugee.pid = 0;
  171. debugee.traced = 0;
  172. debugee.stopped = stopped;
  173. debugee.event = 0;
  174. debugee.zombie = 0;
  175. debugee.died = 0;
  176. debugee.wait_status = status;
  177. debugee.in_context = 1;
  178. }
  179. int debugger_proxy(int status, int pid)
  180. {
  181. int ret = 0, sig;
  182. if(WIFSTOPPED(status)){
  183. sig = WSTOPSIG(status);
  184. if (sig == SIGTRAP)
  185. ret = (*debugger.handle_trace)(&debugger, pid);
  186. else if(sig == SIGCHLD){
  187. if(debugger.expecting_child){
  188. ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  189. debugger.expecting_child = 0;
  190. }
  191. else if(debugger.waiting)
  192. real_wait_return(&debugger);
  193. else {
  194. ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  195. debugger.real_wait = 1;
  196. }
  197. }
  198. else ptrace(PTRACE_SYSCALL, debugger.pid, 0, sig);
  199. }
  200. else if(WIFEXITED(status)){
  201. tracer_panic("debugger (pid %d) exited with status %d",
  202. debugger.pid, WEXITSTATUS(status));
  203. }
  204. else if(WIFSIGNALED(status)){
  205. tracer_panic("debugger (pid %d) exited with signal %d",
  206. debugger.pid, WTERMSIG(status));
  207. }
  208. else {
  209. tracer_panic("proxy got unknown status (0x%x) on debugger "
  210. "(pid %d)", status, debugger.pid);
  211. }
  212. return(ret);
  213. }
  214. void child_proxy(pid_t pid, int status)
  215. {
  216. debugee.event = 1;
  217. debugee.wait_status = status;
  218. if(WIFSTOPPED(status)){
  219. debugee.stopped = 1;
  220. debugger.expecting_child = 1;
  221. kill(debugger.pid, SIGCHLD);
  222. }
  223. else if(WIFEXITED(status) || WIFSIGNALED(status)){
  224. debugee.zombie = 1;
  225. debugger.expecting_child = 1;
  226. kill(debugger.pid, SIGCHLD);
  227. }
  228. else panic("proxy got unknown status (0x%x) on child (pid %d)",
  229. status, pid);
  230. }
  231. void debugger_parent_signal(int status, int pid)
  232. {
  233. int sig;
  234. if(WIFSTOPPED(status)){
  235. sig = WSTOPSIG(status);
  236. if(sig == SIGTRAP) (*parent.handle_trace)(&parent, pid);
  237. else ptrace(PTRACE_SYSCALL, pid, 0, sig);
  238. }
  239. }
  240. void fake_child_exit(void)
  241. {
  242. int status, pid;
  243. child_proxy(1, W_EXITCODE(0, 0));
  244. while(debugger.waiting == 1){
  245. CATCH_EINTR(pid = waitpid(debugger.pid, &status, WUNTRACED));
  246. if(pid != debugger.pid){
  247. printk("fake_child_exit - waitpid failed, "
  248. "errno = %d\n", errno);
  249. return;
  250. }
  251. debugger_proxy(status, debugger.pid);
  252. }
  253. CATCH_EINTR(pid = waitpid(debugger.pid, &status, WUNTRACED));
  254. if(pid != debugger.pid){
  255. printk("fake_child_exit - waitpid failed, "
  256. "errno = %d\n", errno);
  257. return;
  258. }
  259. if(ptrace(PTRACE_DETACH, debugger.pid, 0, SIGCONT) < 0)
  260. printk("fake_child_exit - PTRACE_DETACH failed, errno = %d\n",
  261. errno);
  262. }
  263. char gdb_init_string[] =
  264. "att 1 \n\
  265. b panic \n\
  266. b stop \n\
  267. handle SIGWINCH nostop noprint pass \n\
  268. ";
  269. int start_debugger(char *prog, int startup, int stop, int *fd_out)
  270. {
  271. int slave, child;
  272. slave = open_gdb_chan();
  273. child = fork();
  274. if(child == 0){
  275. char *tempname = NULL;
  276. int fd;
  277. if(setsid() < 0) perror("setsid");
  278. if((dup2(slave, 0) < 0) || (dup2(slave, 1) < 0) ||
  279. (dup2(slave, 2) < 0)){
  280. printk("start_debugger : dup2 failed, errno = %d\n",
  281. errno);
  282. exit(1);
  283. }
  284. if(ioctl(0, TIOCSCTTY, 0) < 0){
  285. printk("start_debugger : TIOCSCTTY failed, "
  286. "errno = %d\n", errno);
  287. exit(1);
  288. }
  289. if(tcsetpgrp (1, os_getpid()) < 0){
  290. printk("start_debugger : tcsetpgrp failed, "
  291. "errno = %d\n", errno);
  292. #ifdef notdef
  293. exit(1);
  294. #endif
  295. }
  296. fd = make_tempfile("/tmp/gdb_init-XXXXXX", &tempname, 0);
  297. if(fd < 0){
  298. printk("start_debugger : make_tempfile failed,"
  299. "err = %d\n", -fd);
  300. exit(1);
  301. }
  302. os_write_file(fd, gdb_init_string, 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. */