| 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> |
| 12 | |
| 13 | #include "ltrace.h" |
| 14 | #include "options.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 15 | #include "debug.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | |
| 17 | static struct event event; |
| 18 | |
| 19 | /* This should also update `current_process' */ |
| 20 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 21 | static struct process *pid2proc(int pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 22 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 23 | struct event *wait_for_something(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 | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 30 | if (!list_of_processes) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 31 | debug(1, "No more children"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 32 | exit(0); |
| 33 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 34 | pid = wait(&status); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 35 | if (pid == -1) { |
| 36 | if (errno == ECHILD) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 37 | debug(1, "No more children"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 38 | exit(0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 39 | } else if (errno == EINTR) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 40 | debug(1, "wait received EINTR ?"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 41 | event.thing = LT_EV_NONE; |
| 42 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 43 | } |
| 44 | perror("wait"); |
| 45 | exit(1); |
| 46 | } |
| 47 | event.proc = pid2proc(pid); |
| 48 | if (!event.proc) { |
| 49 | fprintf(stderr, "signal from wrong pid %u ?!?\n", pid); |
| 50 | exit(1); |
| 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 | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 54 | debug(3, "signal 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); |
| 57 | event.thing = LT_EV_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: |
| 68 | event.thing = LT_EV_SYSCALL; |
| 69 | event.e_un.sysnum = tmp; |
| 70 | return &event; |
| 71 | case 2: |
| 72 | event.thing = LT_EV_SYSRET; |
| 73 | event.e_un.sysnum = tmp; |
| 74 | return &event; |
| 75 | case 3: |
| 76 | event.thing = LT_EV_ARCH_SYSCALL; |
| 77 | event.e_un.sysnum = tmp; |
| 78 | return &event; |
| 79 | case 4: |
| 80 | event.thing = LT_EV_ARCH_SYSRET; |
| 81 | event.e_un.sysnum = tmp; |
| 82 | return &event; |
| 83 | case -1: |
| 84 | event.thing = LT_EV_NONE; |
| 85 | continue_process(event.proc->pid); |
| 86 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 87 | } |
| 88 | if (WIFEXITED(status)) { |
| 89 | event.thing = LT_EV_EXIT; |
| 90 | event.e_un.ret_val = WEXITSTATUS(status); |
| 91 | return &event; |
| 92 | } |
| 93 | if (WIFSIGNALED(status)) { |
| 94 | event.thing = LT_EV_EXIT_SIGNAL; |
| 95 | event.e_un.signum = WTERMSIG(status); |
| 96 | return &event; |
| 97 | } |
| 98 | if (!WIFSTOPPED(status)) { |
| 99 | event.thing = LT_EV_UNKNOWN; |
| 100 | return &event; |
| 101 | } |
| Petr Machata | ef46b3e | 2007-05-09 19:21:42 +0200 | [diff] [blame] | 102 | |
| 103 | stop_signal = WSTOPSIG(status); |
| 104 | |
| 105 | /* On some targets, breakpoints are signalled not using |
| 106 | SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check |
| 107 | for these. */ |
| 108 | if (stop_signal == SIGSEGV |
| 109 | || stop_signal == SIGILL |
| 110 | #ifdef SIGEMT |
| 111 | || stop_signal == SIGEMT |
| 112 | #endif |
| 113 | ) { |
| 114 | // If we didn't need to know IP so far, get it now. |
| 115 | void * addr = opt_i |
| 116 | ? event.proc->instruction_pointer |
| 117 | : (event.proc->instruction_pointer = get_instruction_pointer (event.proc)); |
| 118 | |
| 119 | if (address2bpstruct(event.proc, addr)) |
| 120 | stop_signal = SIGTRAP; |
| 121 | } |
| 122 | |
| 123 | if (stop_signal != (SIGTRAP | event.proc->tracesysgood) |
| 124 | && stop_signal != SIGTRAP) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 125 | event.thing = LT_EV_SIGNAL; |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 126 | event.e_un.signum = stop_signal; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 127 | return &event; |
| 128 | } |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 129 | |
| 130 | if (was_exec(event.proc, status)) { |
| 131 | pid_t saved_pid; |
| 132 | |
| 133 | event.thing = LT_EV_NONE; |
| 134 | event.e_un.signum = WSTOPSIG(status); |
| 135 | debug(1, "Placing breakpoints for the new program"); |
| 136 | event.proc->mask_32bit = 0; |
| 137 | event.proc->personality = 0; |
| 138 | event.proc->arch_ptr = NULL; |
| 139 | event.proc->filename = pid2name(event.proc->pid); |
| 140 | saved_pid = event.proc->pid; |
| 141 | event.proc->pid = 0; |
| 142 | breakpoints_init(event.proc); |
| 143 | event.proc->pid = saved_pid; |
| 144 | continue_process(event.proc->pid); |
| 145 | return &event; |
| 146 | } |
| 147 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 148 | event.thing = LT_EV_BREAKPOINT; |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 149 | if (!event.proc->instruction_pointer) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 150 | event.proc->instruction_pointer = |
| 151 | get_instruction_pointer(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 152 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 153 | event.e_un.brk_addr = |
| 154 | event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 155 | return &event; |
| 156 | } |
| 157 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 158 | static struct process *pid2proc(pid_t pid) |
| 159 | { |
| 160 | struct process *tmp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 161 | |
| 162 | tmp = list_of_processes; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 163 | while (tmp) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 164 | if (pid == tmp->pid) { |
| 165 | return tmp; |
| 166 | } |
| 167 | tmp = tmp->next; |
| 168 | } |
| 169 | return NULL; |
| 170 | } |