| 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 | |
| Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 16 | static enum pcb_status |
| 17 | first (Process * proc, void * data) |
| 18 | { |
| 19 | return pcb_stop; |
| 20 | } |
| 21 | |
| Juan Cespedes | 393f1d0 | 2009-05-07 11:13:54 +0200 | [diff] [blame] | 22 | Event * |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 23 | next_event(void) |
| 24 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 25 | pid_t pid; |
| 26 | int status; |
| 27 | int tmp; |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 28 | int stop_signal; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 29 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 30 | debug(DEBUG_FUNCTION, "next_event()"); |
| Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 31 | if (!each_process(NULL, &first, NULL)) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 32 | debug(DEBUG_EVENT, "event: No more traced programs: exiting"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 33 | exit(0); |
| 34 | } |
| Juan Cespedes | 6be8028 | 2009-05-28 19:06:00 +0200 | [diff] [blame] | 35 | pid = waitpid(-1, &status, __WALL); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | if (pid == -1) { |
| 37 | if (errno == ECHILD) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 38 | debug(DEBUG_EVENT, "event: No more traced programs: exiting"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 39 | exit(0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 40 | } else if (errno == EINTR) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 41 | debug(DEBUG_EVENT, "event: none (wait received EINTR?)"); |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 42 | event.type = EVENT_NONE; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 43 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 44 | } |
| 45 | perror("wait"); |
| 46 | exit(1); |
| 47 | } |
| 48 | event.proc = pid2proc(pid); |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 49 | if (!event.proc || event.proc->state == STATE_BEING_CREATED) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 50 | event.type = EVENT_NEW; |
| Juan Cespedes | 2721e6a | 2009-05-21 15:15:40 +0200 | [diff] [blame] | 51 | event.e_un.newpid = pid; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 52 | debug(DEBUG_EVENT, "event: NEW: pid=%d", pid); |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 53 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 54 | } |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 55 | get_arch_dep(event.proc); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 56 | debug(3, "event from pid %u", pid); |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 57 | if (event.proc->breakpoints_enabled == -1) |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 58 | trace_set_options(event.proc, event.proc->pid); |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 59 | Process *leader = event.proc->leader; |
| 60 | if (leader == event.proc) { |
| 61 | if (event.proc->breakpoints_enabled == -1) { |
| 62 | event.type = EVENT_NONE; |
| 63 | enable_all_breakpoints(event.proc); |
| 64 | continue_process(event.proc->pid); |
| 65 | debug(DEBUG_EVENT, |
| 66 | "event: NONE: pid=%d (enabling breakpoints)", |
| 67 | pid); |
| 68 | return &event; |
| 69 | } else if (!event.proc->libdl_hooked) { |
| 70 | /* debug struct may not have been written yet.. */ |
| 71 | if (linkmap_init(event.proc, &main_lte) == 0) { |
| 72 | event.proc->libdl_hooked = 1; |
| 73 | } |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 74 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 75 | } |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 76 | |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 77 | event.proc->instruction_pointer = (void *)(uintptr_t)-1; |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 78 | |
| 79 | event.proc->instruction_pointer = get_instruction_pointer(event.proc); |
| 80 | if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) { |
| 81 | if (errno != 0) |
| 82 | perror("get_instruction_pointer"); |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 83 | } |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 84 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 85 | switch (syscall_p(event.proc, status, &tmp)) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 86 | case 1: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 87 | event.type = EVENT_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 88 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 89 | debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 90 | return &event; |
| 91 | case 2: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 92 | event.type = EVENT_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 93 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 94 | debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 95 | return &event; |
| 96 | case 3: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 97 | event.type = EVENT_ARCH_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 98 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 99 | debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 100 | return &event; |
| 101 | case 4: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 102 | event.type = EVENT_ARCH_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 103 | event.e_un.sysnum = tmp; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 104 | debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 105 | return &event; |
| 106 | case -1: |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 107 | event.type = EVENT_NONE; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 108 | continue_process(event.proc->pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 109 | debug(DEBUG_EVENT, "event: NONE: pid=%d (syscall_p returned -1)", pid); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 110 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 111 | } |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 112 | if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) { |
| 113 | unsigned long data; |
| 114 | ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data); |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 115 | event.type = EVENT_CLONE; |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 116 | event.e_un.newpid = data; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 117 | debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d", pid, (int)data); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 118 | return &event; |
| 119 | } |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 120 | if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 121 | event.type = EVENT_EXEC; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 122 | debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid); |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 123 | return &event; |
| 124 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 125 | if (WIFEXITED(status)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 126 | event.type = EVENT_EXIT; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 127 | event.e_un.ret_val = WEXITSTATUS(status); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 128 | 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] | 129 | return &event; |
| 130 | } |
| 131 | if (WIFSIGNALED(status)) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 132 | event.type = EVENT_EXIT_SIGNAL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 133 | event.e_un.signum = WTERMSIG(status); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 134 | 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] | 135 | return &event; |
| 136 | } |
| 137 | if (!WIFSTOPPED(status)) { |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 138 | /* should never happen */ |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 139 | event.type = EVENT_NONE; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 140 | debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 141 | return &event; |
| 142 | } |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 143 | |
| 144 | stop_signal = WSTOPSIG(status); |
| 145 | |
| 146 | /* On some targets, breakpoints are signalled not using |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 147 | SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT |
| 148 | is not defined on Linux, but check for the others. |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 149 | |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 150 | N.B. see comments in GDB's infrun.c for details. I've |
| 151 | actually seen this on an Itanium machine on RHEL 5, I don't |
| 152 | remember the exact kernel version anymore. ia64-sigill.s |
| 153 | in the test suite tests this. Petr Machata 2011-06-08. */ |
| 154 | void * break_address |
| 155 | = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| 156 | if ((stop_signal == SIGSEGV || stop_signal == SIGILL) |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 157 | && leader != NULL |
| 158 | && address2bpstruct(leader, break_address)) |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 159 | stop_signal = SIGTRAP; |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 160 | |
| 161 | if (stop_signal != (SIGTRAP | event.proc->tracesysgood) |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 162 | && stop_signal != SIGTRAP) { |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 163 | event.type = EVENT_SIGNAL; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 164 | event.e_un.signum = stop_signal; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 165 | debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 166 | return &event; |
| 167 | } |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 168 | |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 169 | /* last case [by exhaustion] */ |
| Juan Cespedes | 8f6d1ec | 2009-05-07 17:50:34 +0200 | [diff] [blame] | 170 | event.type = EVENT_BREAKPOINT; |
| Juan Cespedes | 427b781 | 2009-07-06 23:05:30 +0200 | [diff] [blame] | 171 | |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 172 | event.e_un.brk_addr = break_address; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 173 | debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr); |
| Petr Machata | 6901b7e | 2011-07-09 11:00:33 +0200 | [diff] [blame] | 174 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 175 | return &event; |
| 176 | } |