tracer.c 12 KB

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