tracer.c 11 KB

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