tracer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 void signal_usr1(int sig);
  159. int tracing_pid = -1;
  160. int tracer(int (*init_proc)(void *), void *sp)
  161. {
  162. void *task = NULL;
  163. int status, pid = 0, sig = 0, cont_type, tracing = 0, op = 0;
  164. int proc_id = 0, n, err, old_tracing = 0, strace = 0;
  165. int local_using_sysemu = 0;
  166. signal(SIGPIPE, SIG_IGN);
  167. setup_tracer_winch();
  168. tracing_pid = os_getpid();
  169. printf("tracing thread pid = %d\n", tracing_pid);
  170. pid = clone(signal_tramp, sp, CLONE_FILES | SIGCHLD, init_proc);
  171. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  172. if(n < 0){
  173. printf("waitpid on idle thread failed, errno = %d\n", errno);
  174. exit(1);
  175. }
  176. if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0) {
  177. printf("Failed to PTRACE_SETOPTIONS for idle thread, errno = %d\n", errno);
  178. exit(1);
  179. }
  180. if((ptrace(PTRACE_CONT, pid, 0, 0) < 0)){
  181. printf("Failed to continue idle thread, errno = %d\n", errno);
  182. exit(1);
  183. }
  184. signal(SIGSEGV, (sighandler_t) tracer_segv);
  185. signal(SIGUSR1, signal_usr1);
  186. if(debug_trace){
  187. printf("Tracing thread pausing to be attached\n");
  188. stop();
  189. }
  190. if(debug){
  191. if(gdb_pid != -1)
  192. debugger_pid = attach_debugger(pid, gdb_pid, 1);
  193. else debugger_pid = init_ptrace_proxy(pid, 1, debug_stop);
  194. if(debug_parent){
  195. debugger_parent = os_process_parent(debugger_pid);
  196. init_parent_proxy(debugger_parent);
  197. err = attach(debugger_parent);
  198. if(err){
  199. printf("Failed to attach debugger parent %d, "
  200. "errno = %d\n", debugger_parent, -err);
  201. debugger_parent = -1;
  202. }
  203. else {
  204. if(ptrace(PTRACE_SYSCALL, debugger_parent,
  205. 0, 0) < 0){
  206. printf("Failed to continue debugger "
  207. "parent, errno = %d\n", errno);
  208. debugger_parent = -1;
  209. }
  210. }
  211. }
  212. }
  213. set_cmdline("(tracing thread)");
  214. while(1){
  215. CATCH_EINTR(pid = waitpid(-1, &status, WUNTRACED));
  216. if(pid <= 0){
  217. if(errno != ECHILD){
  218. printf("wait failed - errno = %d\n", errno);
  219. }
  220. continue;
  221. }
  222. if(pid == debugger_pid){
  223. int cont = 0;
  224. if(WIFEXITED(status) || WIFSIGNALED(status))
  225. debugger_pid = -1;
  226. /* XXX Figure out how to deal with gdb and SMP */
  227. else cont = debugger_signal(status, cpu_tasks[0].pid);
  228. if(cont == PTRACE_SYSCALL) strace = 1;
  229. continue;
  230. }
  231. else if(pid == debugger_parent){
  232. debugger_parent_signal(status, pid);
  233. continue;
  234. }
  235. nsignals++;
  236. if(WIFEXITED(status)) ;
  237. #ifdef notdef
  238. {
  239. printf("Child %d exited with status %d\n", pid,
  240. WEXITSTATUS(status));
  241. }
  242. #endif
  243. else if(WIFSIGNALED(status)){
  244. sig = WTERMSIG(status);
  245. if(sig != 9){
  246. printf("Child %d exited with signal %d\n", pid,
  247. sig);
  248. }
  249. }
  250. else if(WIFSTOPPED(status)){
  251. proc_id = pid_to_processor_id(pid);
  252. sig = WSTOPSIG(status);
  253. if(proc_id == -1){
  254. sleeping_process_signal(pid, sig);
  255. continue;
  256. }
  257. task = cpu_tasks[proc_id].task;
  258. tracing = is_tracing(task);
  259. old_tracing = tracing;
  260. /* Assume: no syscall, when coming from user */
  261. if ( tracing )
  262. do_sigtrap(task);
  263. switch(sig){
  264. case SIGUSR1:
  265. sig = 0;
  266. op = do_proc_op(task, proc_id);
  267. switch(op){
  268. /*
  269. * This is called when entering user mode; after
  270. * this, we start intercepting syscalls.
  271. *
  272. * In fact, a process is started in kernel mode,
  273. * so with is_tracing() == 0 (and that is reset
  274. * when executing syscalls, since UML kernel has
  275. * the right to do syscalls);
  276. */
  277. case OP_TRACE_ON:
  278. arch_leave_kernel(task, pid);
  279. tracing = 1;
  280. break;
  281. case OP_REBOOT:
  282. case OP_HALT:
  283. unmap_physmem();
  284. kmalloc_ok = 0;
  285. os_kill_ptraced_process(pid, 0);
  286. /* Now let's reap remaining zombies */
  287. errno = 0;
  288. do {
  289. waitpid(-1, &status,
  290. WUNTRACED);
  291. } while (errno != ECHILD);
  292. return(op == OP_REBOOT);
  293. case OP_NONE:
  294. printf("Detaching pid %d\n", pid);
  295. detach(pid, SIGSTOP);
  296. continue;
  297. default:
  298. break;
  299. }
  300. /* OP_EXEC switches host processes on us,
  301. * we want to continue the new one.
  302. */
  303. pid = cpu_tasks[proc_id].pid;
  304. break;
  305. case (SIGTRAP + 0x80):
  306. if(!tracing && (debugger_pid != -1)){
  307. child_signal(pid, status & 0x7fff);
  308. continue;
  309. }
  310. tracing = 0;
  311. /* local_using_sysemu has been already set
  312. * below, since if we are here, is_tracing() on
  313. * the traced task was 1, i.e. the process had
  314. * already run through one iteration of the
  315. * loop which executed a OP_TRACE_ON request.*/
  316. do_syscall(task, pid, local_using_sysemu);
  317. sig = SIGUSR2;
  318. break;
  319. case SIGTRAP:
  320. if(!tracing && (debugger_pid != -1)){
  321. child_signal(pid, status);
  322. continue;
  323. }
  324. tracing = 0;
  325. break;
  326. case SIGPROF:
  327. if(tracing) sig = 0;
  328. break;
  329. case SIGCHLD:
  330. case SIGHUP:
  331. sig = 0;
  332. break;
  333. case SIGSEGV:
  334. case SIGIO:
  335. case SIGALRM:
  336. case SIGVTALRM:
  337. case SIGFPE:
  338. case SIGBUS:
  339. case SIGILL:
  340. case SIGWINCH:
  341. default:
  342. tracing = 0;
  343. break;
  344. }
  345. set_tracing(task, tracing);
  346. if(!tracing && old_tracing)
  347. arch_enter_kernel(task, pid);
  348. if(!tracing && (debugger_pid != -1) && (sig != 0) &&
  349. (sig != SIGALRM) && (sig != SIGVTALRM) &&
  350. (sig != SIGSEGV) && (sig != SIGTRAP) &&
  351. (sig != SIGUSR2) && (sig != SIGIO) &&
  352. (sig != SIGFPE)){
  353. child_signal(pid, status);
  354. continue;
  355. }
  356. local_using_sysemu = get_using_sysemu();
  357. if(tracing)
  358. cont_type = SELECT_PTRACE_OPERATION(local_using_sysemu,
  359. singlestepping(task));
  360. else if((debugger_pid != -1) && strace)
  361. cont_type = PTRACE_SYSCALL;
  362. else
  363. cont_type = PTRACE_CONT;
  364. if(ptrace(cont_type, pid, 0, sig) != 0){
  365. tracer_panic("ptrace failed to continue "
  366. "process - errno = %d\n",
  367. errno);
  368. }
  369. }
  370. }
  371. return(0);
  372. }
  373. static int __init uml_debug_setup(char *line, int *add)
  374. {
  375. char *next;
  376. debug = 1;
  377. *add = 0;
  378. if(*line != '=') return(0);
  379. line++;
  380. while(line != NULL){
  381. next = strchr(line, ',');
  382. if(next) *next++ = '\0';
  383. if(!strcmp(line, "go")) debug_stop = 0;
  384. else if(!strcmp(line, "parent")) debug_parent = 1;
  385. else printf("Unknown debug option : '%s'\n", line);
  386. line = next;
  387. }
  388. return(0);
  389. }
  390. __uml_setup("debug", uml_debug_setup,
  391. "debug\n"
  392. " Starts up the kernel under the control of gdb. See the \n"
  393. " kernel debugging tutorial and the debugging session pages\n"
  394. " at http://user-mode-linux.sourceforge.net/ for more information.\n\n"
  395. );
  396. static int __init uml_debugtrace_setup(char *line, int *add)
  397. {
  398. debug_trace = 1;
  399. return 0;
  400. }
  401. __uml_setup("debugtrace", uml_debugtrace_setup,
  402. "debugtrace\n"
  403. " Causes the tracing thread to pause until it is attached by a\n"
  404. " debugger and continued. This is mostly for debugging crashes\n"
  405. " early during boot, and should be pretty much obsoleted by\n"
  406. " the debug switch.\n\n"
  407. );
  408. /*
  409. * Overrides for Emacs so that we follow Linus's tabbing style.
  410. * Emacs will notice this stuff at the end of the file and automatically
  411. * adjust the settings for this buffer only. This must remain at the end
  412. * of the file.
  413. * ---------------------------------------------------------------------------
  414. * Local variables:
  415. * c-file-style: "linux"
  416. * End:
  417. */