| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 3 | #define _GNU_SOURCE 1 |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 4 | #include <stdlib.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
| 9 | #include <string.h> |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 10 | #include <sys/ptrace.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 12 | #include "common.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 13 | |
| Juan Cespedes | 393f1d0 | 2009-05-07 11:13:54 +0200 | [diff] [blame] | 14 | static Event event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 15 | |
| Juan Cespedes | 393f1d0 | 2009-05-07 11:13:54 +0200 | [diff] [blame] | 16 | Event * |
| Juan Cespedes | e2023f7 | 2009-04-07 12:12:08 +0200 | [diff] [blame] | 17 | next_event(void) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 18 | pid_t pid; |
| 19 | int status; |
| 20 | int tmp; |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 21 | int stop_signal; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 22 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 23 | debug(DEBUG_FUNCTION, "next_event()"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 24 | if (!list_of_processes) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 25 | debug(DEBUG_EVENT, "event: No more traced programs: exiting"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 26 | exit(0); |
| 27 | } |
| Juan Cespedes | 6be8028 | 2009-05-28 19:06:00 +0200 | [diff] [blame] | 28 | pid = waitpid(-1, &status, __WALL); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | if (pid == -1) { |
| 30 | if (errno == ECHILD) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 31 | debug(DEBUG_EVENT, "event: No more traced programs: exiting"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 32 | exit(0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 33 | } else if (errno == EINTR) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 34 | debug(DEBUG_EVENT, "event: none (wait received EINTR?)"); |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 35 | event.type = EVENT_NONE; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 36 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 37 | } |
| 38 | perror("wait"); |
| 39 | exit(1); |
| 40 | } |
| 41 | event.proc = pid2proc(pid); |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 42 | if (!event.proc || event.proc->state == STATE_BEING_CREATED) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 43 | event.type = EVENT_NEW; |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 44 | event.e_un.newpid = pid; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 45 | debug(DEBUG_EVENT, "event: NEW: pid=%d", pid); |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 46 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 47 | } |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 48 | get_arch_dep(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 49 | event.proc->instruction_pointer = NULL; |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 50 | debug(3, "event from pid %u", pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 51 | if (event.proc->breakpoints_enabled == -1) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 52 | enable_all_breakpoints(event.proc); |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 53 | event.type = EVENT_NONE; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 54 | trace_set_options(event.proc, event.proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 55 | continue_process(event.proc->pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 56 | debug(DEBUG_EVENT, "event: NONE: pid=%d (enabling breakpoints)", pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 57 | return &event; |
| 58 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 59 | if (opt_i) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 60 | event.proc->instruction_pointer = |
| Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 61 | get_instruction_pointer(event.proc); |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 62 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 63 | switch (syscall_p(event.proc, status, &tmp)) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 64 | case 1: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 65 | event.type = EVENT_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 66 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 67 | debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 68 | return &event; |
| 69 | case 2: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 70 | event.type = EVENT_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 71 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 72 | debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 73 | return &event; |
| 74 | case 3: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 75 | event.type = EVENT_ARCH_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 76 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 77 | debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 78 | return &event; |
| 79 | case 4: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 80 | event.type = EVENT_ARCH_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 81 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 82 | debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 83 | return &event; |
| 84 | case -1: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 85 | event.type = EVENT_NONE; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 86 | continue_process(event.proc->pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 87 | debug(DEBUG_EVENT, "event: NONE: pid=%d (syscall_p returned -1)", pid); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 88 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 89 | } |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 90 | if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) { |
| 91 | unsigned long data; |
| 92 | ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data); |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 93 | event.type = EVENT_CLONE; |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 94 | event.e_un.newpid = data; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 95 | debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d", pid, (int)data); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 96 | return &event; |
| 97 | } |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 98 | if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 99 | event.type = EVENT_EXEC; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 100 | debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 101 | return &event; |
| 102 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 103 | if (WIFEXITED(status)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 104 | event.type = EVENT_EXIT; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 105 | event.e_un.ret_val = WEXITSTATUS(status); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 106 | debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 107 | return &event; |
| 108 | } |
| 109 | if (WIFSIGNALED(status)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 110 | event.type = EVENT_EXIT_SIGNAL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 111 | event.e_un.signum = WTERMSIG(status); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 112 | debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 113 | return &event; |
| 114 | } |
| 115 | if (!WIFSTOPPED(status)) { |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 116 | /* should never happen */ |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 117 | event.type = EVENT_NONE; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 118 | debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 119 | return &event; |
| 120 | } |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 121 | |
| 122 | stop_signal = WSTOPSIG(status); |
| 123 | |
| 124 | /* On some targets, breakpoints are signalled not using |
| 125 | SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 126 | for these. (TODO: is this true?) */ |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 127 | if (stop_signal == SIGSEGV |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 128 | || stop_signal == SIGILL |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 129 | #ifdef SIGEMT |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 130 | || stop_signal == SIGEMT |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 131 | #endif |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 132 | ) { |
| 133 | if (!event.proc->instruction_pointer) { |
| 134 | event.proc->instruction_pointer = |
| 135 | get_instruction_pointer(event.proc); |
| 136 | } |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 137 | |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 138 | if (address2bpstruct(event.proc, event.proc->instruction_pointer)) |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 139 | stop_signal = SIGTRAP; |
| 140 | } |
| 141 | |
| 142 | if (stop_signal != (SIGTRAP | event.proc->tracesysgood) |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 143 | && stop_signal != SIGTRAP) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 144 | event.type = EVENT_SIGNAL; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 145 | event.e_un.signum = stop_signal; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 146 | debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 147 | return &event; |
| 148 | } |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 149 | |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 150 | /* last case [by exhaustion] */ |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 151 | event.type = EVENT_BREAKPOINT; |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 152 | |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 153 | if (!event.proc->instruction_pointer) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 154 | event.proc->instruction_pointer = |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 155 | get_instruction_pointer(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 156 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 157 | event.e_un.brk_addr = |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 158 | event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 159 | debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 160 | return &event; |
| 161 | } |