tracer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <stdarg.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11. #include <sched.h>
  12. #include <string.h>
  13. #include <sys/mman.h>
  14. #include <sys/time.h>
  15. #include <sys/wait.h>
  16. #include "user.h"
  17. #include "sysdep/ptrace.h"
  18. #include "sigcontext.h"
  19. #include "sysdep/sigcontext.h"
  20. #include "os.h"
  21. #include "mem_user.h"
  22. #include "process.h"
  23. #include "kern_util.h"
  24. #include "chan_user.h"
  25. #include "ptrace_user.h"
  26. #include "irq_user.h"
  27. #include "mode.h"
  28. #include "tt.h"
  29. static int tracer_winch[2];
  30. int is_tracer_winch(int pid, int fd, void *data)
  31. {
  32. if(pid != os_getpgrp())
  33. return(0);
  34. register_winch_irq(tracer_winch[0], fd, -1, data);
  35. return(1);
  36. }
  37. static void tracer_winch_handler(int sig)
  38. {
  39. int n;
  40. char c = 1;
  41. n = os_write_file(tracer_winch[1], &c, sizeof(c));
  42. if(n != sizeof(c))
  43. printk("tracer_winch_handler - write failed, err = %d\n", -n);
  44. }
  45. /* Called only by the tracing thread during initialization */
  46. static void setup_tracer_winch(void)
  47. {
  48. int err;
  49. err = os_pipe(tracer_winch, 1, 1);
  50. if(err < 0){
  51. printk("setup_tracer_winch : os_pipe failed, err = %d\n", -err);
  52. return;
  53. }
  54. signal(SIGWINCH, tracer_winch_handler);
  55. }
  56. void attach_process(int pid)
  57. {
  58. if((ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) ||
  59. (ptrace(PTRACE_CONT, pid, 0, 0) < 0))
  60. tracer_panic("OP_FORK failed to attach pid");
  61. wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
  62. if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  63. tracer_panic("OP_FORK: PTRACE_SETOPTIONS failed, errno = %d", errno);
  64. if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
  65. tracer_panic("OP_FORK failed to continue process");
  66. }
  67. void tracer_panic(char *format, ...)
  68. {
  69. va_list ap;
  70. va_start(ap, format);
  71. vprintf(format, ap);
  72. va_end(ap);
  73. printf("\n");
  74. while(1) pause();
  75. }
  76. static void tracer_segv(int sig, struct sigcontext sc)
  77. {
  78. struct faultinfo fi;
  79. GET_FAULTINFO_FROM_SC(fi, &sc);
  80. printf("Tracing thread segfault at address 0x%lx, ip 0x%lx\n",
  81. FAULT_ADDRESS(fi), SC_IP(&sc));
  82. while(1)
  83. pause();
  84. }
  85. /* Changed early in boot, and then only read */
  86. int debug = 0;
  87. int debug_stop = 1;
  88. int debug_parent = 0;
  89. int honeypot = 0;
  90. static int signal_tramp(void *arg)
  91. {
  92. int (*proc)(void *);
  93. if(honeypot && munmap((void *) (host_task_size - 0x10000000),
  94. 0x10000000))
  95. panic("Unmapping stack failed");
  96. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0)
  97. panic("ptrace PTRACE_TRACEME failed");
  98. os_stop_process(os_getpid());
  99. change_sig(SIGWINCH, 0);
  100. signal(SIGUSR1, SIG_IGN);
  101. change_sig(SIGCHLD, 0);
  102. signal(SIGSEGV, (__sighandler_t) sig_handler);
  103. set_cmdline("(idle thread)");
  104. set_init_pid(os_getpid());
  105. init_irq_signals(0);
  106. proc = arg;
  107. return((*proc)(NULL));
  108. }
  109. static void sleeping_process_signal(int pid, int sig)
  110. {
  111. switch(sig){
  112. /* These two result from UML being ^Z-ed and bg-ed. PTRACE_CONT is
  113. * right because the process must be in the kernel already.
  114. */
  115. case SIGCONT:
  116. case SIGTSTP:
  117. if(ptrace(PTRACE_CONT, pid, 0, sig) < 0)
  118. tracer_panic("sleeping_process_signal : Failed to "
  119. "continue pid %d, signal = %d, "
  120. "errno = %d\n", pid, sig, errno);
  121. break;
  122. /* This happens when the debugger (e.g. strace) is doing system call
  123. * tracing on the kernel. During a context switch, the current task
  124. * will be set to the incoming process and the outgoing process will
  125. * hop into write and then read. Since it's not the current process
  126. * any more, the trace of those will land here. So, we need to just
  127. * PTRACE_SYSCALL it.
  128. */
  129. case (SIGTRAP + 0x80):
  130. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  131. tracer_panic("sleeping_process_signal : Failed to "
  132. "PTRACE_SYSCALL pid %d, errno = %d\n",
  133. pid, errno);
  134. break;
  135. case SIGSTOP:
  136. break;
  137. default:
  138. tracer_panic("sleeping process %d got unexpected "
  139. "signal : %d\n", pid, sig);
  140. break;
  141. }
  142. }
  143. /* Accessed only by the tracing thread */
  144. int debugger_pid = -1;
  145. int debugger_parent = -1;
  146. int debugger_fd = -1;
  147. int gdb_pid = -1;
  148. struct {
  149. int pid;
  150. int signal;
  151. unsigned long addr;
  152. struct timeval time;
  153. } signal_record[1024][32];
  154. int signal_index[32];
  155. int nsignals = 0;
  156. int debug_trace = 0;
  157. extern void signal_usr1(int sig);
  158. int tracing_pid = -1;
  159. int tracer(int (*init_proc)(void *), void *sp)
  160. {
  161. void *task = NULL;
  162. int status, pid = 0, sig = 0, cont_type, tracing = 0, op = 0;
  163. int proc_id = 0, n, err, old_tracing = 0, strace = 0;
  164. int local_using_sysemu = 0;
  165. signal(SIGPIPE, SIG_IGN);
  166. setup_tracer_winch();
  167. tracing_pid = os_getpid();
  168. printf("tracing thread pid = %d\n", tracing_pid);
  169. pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
  170. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  171. if(n < 0){
  172. printf("waitpid on idle thread failed, errno = %d\n", errno);
  173. exit(1);
  174. }
  175. if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0) {
  176. printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno);
  177. exit(1);
  178. }
  179. if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
  180. printf("Failed to continue idle thread, errno = %d\n", errno);
  181. exit(1);
  182. }
  183. signal(SIGSEGV, (sighandler_t) tracer_segv);
  184. signal(SIGUSR1, signal_usr1);
  185. if(debug_trace){
  186. printf("Tracing thread pausing to be attached\n");
  187. stop();
  188. }
  189. if(debug){
  190. if(gdb_pid != -1)
  191. debugger_pid = attach_debugger(pid, gdb_pid, 1);
  192. else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
  193. if(debug_parent){
  194. debugger_parent = os_process_parent(debugger_pid);
  195. init_parent_proxy(debugger_parent);
  196. err = attach(debugger_parent);
  197. if(err){
  198. printf("Failed to attach debugger parent %d, "
  199. "errno = %d\n", debugger_parent, -err);
  200. debugger_parent = -1;
  201. }
  202. else {
  203. if(ptrace(PTRACE_SYSCALL, debugger_parent,
  204. 0, 0) < 0){
  205. printf("Failed to continue debugger "
  206. "parent, errno = %d\n", errno);
  207. debugger_parent = -1;
  208. }
  209. }
  210. }
  211. }
  212. set_cmdline("(tracing thread)");
  213. while(1){
  214. CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
  215. if(pid <= 0){
  216. if(errno != ECHILD){
  217. printf("wait failed - errno = %d\n", errno);
  218. }
  219. continue;
  220. }
  221. if(pid == debugger_pid){
  222. int cont = 0;
  223. if(WIFEXITED(status) || WIFSIGNALED(status))
  224. debugger_pid = -1;
  225. /* XXX Figure out how to deal with gdb and SMP */
  226. else cont = debugger_signal(status, cpu_tasks[0].pid);
  227. if(cont == PTRACE_SYSCALL) strace = 1;
  228. continue;
  229. }
  230. else if(pid == debugger_parent){
  231. debugger_parent_signal(status, pid);
  232. continue;
  233. }
  234. nsignals++;
  235. if(WIFEXITED(status)) ;
  236. #ifdef notdef
  237. {
  238. printf("Child %d exited with status %d\n", pid,
  239. WEXITSTATUS(status));
  240. }
  241. #endif
  242. else if(WIFSIGNALED(status)){
  243. sig = WTERMSIG(status);
  244. if(sig != 9){
  245. printf("Child %d exited with signal %d\n", pid,
  246. sig);
  247. }
  248. }
  249. else if(WIFSTOPPED(status)){
  250. proc_id = pid_to_processor_id(pid);
  251. sig = WSTOPSIG(status);
  252. if(proc_id == -1){
  253. sleeping_process_signal(pid, sig);
  254. continue;
  255. }
  256. task = cpu_tasks[proc_id].task;
  257. tracing = is_tracing(task);
  258. old_tracing = tracing;
  259. /* Assume: no syscall, when coming from user */
  260. if ( tracing )
  261. do_sigtrap(task);
  262. switch(sig){
  263. case SIGUSR1:
  264. sig = 0;
  265. op = do_proc_op(task, proc_id);
  266. switch(op){
  267. /*
  268. * This is called when entering user mode; after
  269. * this, we start intercepting syscalls.
  270. *
  271. * In fact, a process is started in kernel mode,
  272. * so with is_tracing() == 0 (and that is reset
  273. * when executing syscalls, since UML kernel has
  274. * the right to do syscalls);
  275. */
  276. case OP_TRACE_ON:
  277. arch_leave_kernel(task, pid);
  278. tracing = 1;
  279. break;
  280. case OP_REBOOT:
  281. case OP_HALT:
  282. unmap_physmem();
  283. kmalloc_ok = 0;
  284. os_kill_ptraced_process(pid, 0);
  285. /* Now let's reap remaining zombies */
  286. errno = 0;
  287. do {
  288. waitpid(-1, &status,
  289. WUNTRACED);
  290. } while (errno != ECHILD);
  291. return(op == OP_REBOOT);
  292. case OP_NONE:
  293. printf("Detaching pid %d\n", pid);
  294. detach(pid, SIGSTOP);
  295. continue;
  296. default:
  297. break;
  298. }
  299. /* OP_EXEC switches host processes on us,
  300. * we want to continue the new one.
  301. */
  302. pid = cpu_tasks[proc_id].pid;
  303. break;
  304. case (SIGTRAP + 0x80):
  305. if(!tracing && (debugger_pid != -1)){
  306. child_signal(pid, status & 0x7fff);
  307. continue;
  308. }
  309. tracing = 0;
  310. /* local_using_sysemu has been already set
  311. * below, since if we are here, is_tracing() on
  312. * the traced task was 1, i.e. the process had
  313. * already run through one iteration of the
  314. * loop which executed a OP_TRACE_ON request.*/
  315. do_syscall(task, pid, local_using_sysemu);
  316. sig = SIGUSR2;
  317. break;
  318. case SIGTRAP:
  319. if(!tracing && (debugger_pid != -1)){
  320. child_signal(pid, status);
  321. continue;
  322. }
  323. tracing = 0;
  324. break;
  325. case SIGPROF:
  326. if(tracing) sig = 0;
  327. break;
  328. case SIGCHLD:
  329. case SIGHUP:
  330. sig = 0;
  331. break;
  332. case SIGSEGV:
  333. case SIGIO:
  334. case SIGALRM:
  335. case SIGVTALRM:
  336. case SIGFPE:
  337. case SIGBUS:
  338. case SIGILL:
  339. case SIGWINCH:
  340. default:
  341. tracing = 0;
  342. break;
  343. }
  344. set_tracing(task, tracing);
  345. if(!tracing && old_tracing)
  346. arch_enter_kernel(task, pid);
  347. if(!tracing && (debugger_pid != -1) && (sig != 0) &&
  348. (sig != SIGALRM) && (sig != SIGVTALRM) &&
  349. (sig != SIGSEGV) && (sig != SIGTRAP) &&
  350. (sig != SIGUSR2) && (sig != SIGIO) &&
  351. (sig != SIGFPE)){
  352. child_signal(pid, status);
  353. continue;
  354. }
  355. local_using_sysemu = get_using_sysemu();
  356. if(tracing)
  357. cont_type = SELECT_PTRACE_OPERATION(local_using_sysemu,
  358. singlestepping(task));
  359. else if((debugger_pid != -1) && strace)
  360. cont_type = PTRACE_SYSCALL;
  361. else
  362. cont_type = PTRACE_CONT;
  363. if(ptrace(cont_type, pid, 0, sig) != 0){
  364. tracer_panic("ptrace failed to continue "
  365. "process - errno = %d\n",
  366. errno);
  367. }
  368. }
  369. }
  370. return(0);
  371. }
  372. static int __init uml_debug_setup(char *line, int *add)
  373. {
  374. char *next;
  375. debug = 1;
  376. *add = 0;
  377. if(*line != '=') return(0);
  378. line++;
  379. while(line != NULL){
  380. next = strchr(line, ',');
  381. if(next) *next++ = '\0';
  382. if(!strcmp(line, "go")) debug_stop = 0;
  383. else if(!strcmp(line, "parent")) debug_parent = 1;
  384. else printf("Unknown debug option : '%s'\n", line);
  385. line = next;
  386. }
  387. return(0);
  388. }
  389. __uml_setup("debug", uml_debug_setup,
  390. "debug\n"
  391. " Starts up the kernel under the control of gdb. See the \n"
  392. " kernel debugging tutorial and the debugging session pages\n"
  393. " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
  394. );
  395. static int __init uml_debugtrace_setup(char *line, int *add)
  396. {
  397. debug_trace = 1;
  398. return 0;
  399. }
  400. __uml_setup("debugtrace", uml_debugtrace_setup,
  401. "debugtrace\n"
  402. " Causes the tracing thread to pause until it is attached by a\n"
  403. " debugger and continued. This is mostly for debugging crashes\n"
  404. " early during boot, and should be pretty much obsoleted by\n"
  405. " the debug switch.\n\n"
  406. );
  407. /*
  408. * Overrides for Emacs so that we follow Linus's tabbing style.
  409. * Emacs will notice this stuff at the end of the file and automatically
  410. * adjust the settings for this buffer only. This must remain at the end
  411. * of the file.
  412. * ---------------------------------------------------------------------------
  413. * Local variables:
  414. * c-file-style: "linux"
  415. * End:
  416. */