tracer.c 12 KB

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