| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #define _GNU_SOURCE 1 |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 6 | #include <stdlib.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 7 | #include <sys/types.h> |
| 8 | #include <sys/wait.h> |
| 9 | #include <errno.h> |
| 10 | #include <signal.h> |
| 11 | #include <string.h> |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 12 | #include <sys/ptrace.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 13 | |
| 14 | #include "ltrace.h" |
| 15 | #include "options.h" |
| Juan Cespedes | cde5826 | 2009-05-07 11:09:00 +0200 | [diff] [blame] | 16 | #include "output.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 17 | #include "debug.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 18 | |
| Juan Cespedes | 393f1d0 | 2009-05-07 11:13:54 +0200 | [diff] [blame^] | 19 | static Event event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 20 | |
| Juan Cespedes | 393f1d0 | 2009-05-07 11:13:54 +0200 | [diff] [blame^] | 21 | Event * |
| Juan Cespedes | e2023f7 | 2009-04-07 12:12:08 +0200 | [diff] [blame] | 22 | next_event(void) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 23 | pid_t pid; |
| 24 | int status; |
| 25 | int tmp; |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 26 | int stop_signal; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 27 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 28 | if (!list_of_processes) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 29 | debug(1, "No more children"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 30 | exit(0); |
| 31 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 32 | pid = wait(&status); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 33 | if (pid == -1) { |
| 34 | if (errno == ECHILD) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 35 | debug(1, "No more children"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 36 | exit(0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 37 | } else if (errno == EINTR) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 38 | debug(1, "wait received EINTR ?"); |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 39 | event.thing = EVENT_NONE; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 40 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 41 | } |
| 42 | perror("wait"); |
| 43 | exit(1); |
| 44 | } |
| 45 | event.proc = pid2proc(pid); |
| 46 | if (!event.proc) { |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 47 | output_line(NULL, "event from wrong pid %u ?!?\n", pid); |
| 48 | // exit(1); |
| 49 | event.thing = EVENT_NONE; |
| 50 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 51 | } |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 52 | get_arch_dep(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 53 | event.proc->instruction_pointer = NULL; |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 54 | debug(3, "event from pid %u", pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 55 | if (event.proc->breakpoints_enabled == -1) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 56 | enable_all_breakpoints(event.proc); |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 57 | event.thing = EVENT_NONE; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 58 | trace_set_options(event.proc, event.proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 59 | continue_process(event.proc->pid); |
| 60 | return &event; |
| 61 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 62 | if (opt_i) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 63 | event.proc->instruction_pointer = |
| 64 | get_instruction_pointer(event.proc); |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 65 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | switch (syscall_p(event.proc, status, &tmp)) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 67 | case 1: |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 68 | event.thing = EVENT_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 69 | event.e_un.sysnum = tmp; |
| 70 | return &event; |
| 71 | case 2: |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 72 | event.thing = EVENT_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 73 | event.e_un.sysnum = tmp; |
| 74 | return &event; |
| 75 | case 3: |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 76 | event.thing = EVENT_ARCH_SYSCALL; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 77 | event.e_un.sysnum = tmp; |
| 78 | return &event; |
| 79 | case 4: |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 80 | event.thing = EVENT_ARCH_SYSRET; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 81 | event.e_un.sysnum = tmp; |
| 82 | return &event; |
| 83 | case -1: |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 84 | event.thing = EVENT_NONE; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 85 | continue_process(event.proc->pid); |
| 86 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 87 | } |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 88 | if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) { |
| 89 | unsigned long data; |
| 90 | ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data); |
| 91 | event.thing = EVENT_FORK; |
| 92 | event.e_un.newpid = data; |
| 93 | return &event; |
| 94 | } |
| 95 | /* TODO: check for EVENT_CLONE */ |
| 96 | if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) { |
| 97 | event.thing = EVENT_EXEC; |
| 98 | return &event; |
| 99 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 100 | if (WIFEXITED(status)) { |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 101 | event.thing = EVENT_EXIT; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 102 | event.e_un.ret_val = WEXITSTATUS(status); |
| 103 | return &event; |
| 104 | } |
| 105 | if (WIFSIGNALED(status)) { |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 106 | event.thing = EVENT_EXIT_SIGNAL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 107 | event.e_un.signum = WTERMSIG(status); |
| 108 | return &event; |
| 109 | } |
| 110 | if (!WIFSTOPPED(status)) { |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 111 | event.thing = EVENT_NONE; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 112 | return &event; |
| 113 | } |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 114 | |
| 115 | stop_signal = WSTOPSIG(status); |
| 116 | |
| 117 | /* On some targets, breakpoints are signalled not using |
| 118 | SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check |
| 119 | for these. */ |
| 120 | if (stop_signal == SIGSEGV |
| 121 | || stop_signal == SIGILL |
| 122 | #ifdef SIGEMT |
| 123 | || stop_signal == SIGEMT |
| 124 | #endif |
| 125 | ) { |
| 126 | // If we didn't need to know IP so far, get it now. |
| 127 | void * addr = opt_i |
| 128 | ? event.proc->instruction_pointer |
| 129 | : (event.proc->instruction_pointer = get_instruction_pointer (event.proc)); |
| 130 | |
| 131 | if (address2bpstruct(event.proc, addr)) |
| 132 | stop_signal = SIGTRAP; |
| 133 | } |
| 134 | |
| 135 | if (stop_signal != (SIGTRAP | event.proc->tracesysgood) |
| 136 | && stop_signal != SIGTRAP) { |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 137 | event.thing = EVENT_SIGNAL; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 138 | event.e_un.signum = stop_signal; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 139 | return &event; |
| 140 | } |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 141 | |
| 142 | if (was_exec(event.proc, status)) { |
| 143 | pid_t saved_pid; |
| 144 | |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 145 | event.thing = EVENT_NONE; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 146 | event.e_un.signum = WSTOPSIG(status); |
| 147 | debug(1, "Placing breakpoints for the new program"); |
| 148 | event.proc->mask_32bit = 0; |
| 149 | event.proc->personality = 0; |
| 150 | event.proc->arch_ptr = NULL; |
| 151 | event.proc->filename = pid2name(event.proc->pid); |
| 152 | saved_pid = event.proc->pid; |
| 153 | event.proc->pid = 0; |
| 154 | breakpoints_init(event.proc); |
| 155 | event.proc->pid = saved_pid; |
| 156 | continue_process(event.proc->pid); |
| 157 | return &event; |
| 158 | } |
| 159 | |
| Juan Cespedes | 138d41c | 2009-04-07 00:49:12 +0200 | [diff] [blame] | 160 | event.thing = EVENT_BREAKPOINT; |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 161 | if (!event.proc->instruction_pointer) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 162 | event.proc->instruction_pointer = |
| 163 | get_instruction_pointer(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 164 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 165 | event.e_un.brk_addr = |
| 166 | event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 167 | return &event; |
| 168 | } |