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