| 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; |
| 28 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 29 | if (!list_of_processes) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 30 | debug(1, "No more children"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 31 | exit(0); |
| 32 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 33 | pid = wait(&status); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 34 | if (pid == -1) { |
| 35 | if (errno == ECHILD) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 36 | debug(1, "No more children"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 37 | exit(0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 38 | } else if (errno == EINTR) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 39 | debug(1, "wait received EINTR ?"); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 40 | event.thing = LT_EV_NONE; |
| 41 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 42 | } |
| 43 | perror("wait"); |
| 44 | exit(1); |
| 45 | } |
| 46 | event.proc = pid2proc(pid); |
| 47 | if (!event.proc) { |
| 48 | fprintf(stderr, "signal from wrong pid %u ?!?\n", pid); |
| 49 | exit(1); |
| 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 | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 53 | debug(3, "signal 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); |
| 56 | event.thing = LT_EV_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)) { |
| 66 | case 1: |
| 67 | event.thing = LT_EV_SYSCALL; |
| 68 | event.e_un.sysnum = tmp; |
| 69 | return &event; |
| 70 | case 2: |
| 71 | event.thing = LT_EV_SYSRET; |
| 72 | event.e_un.sysnum = tmp; |
| 73 | return &event; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 74 | } |
| 75 | if (WIFEXITED(status)) { |
| 76 | event.thing = LT_EV_EXIT; |
| 77 | event.e_un.ret_val = WEXITSTATUS(status); |
| 78 | return &event; |
| 79 | } |
| 80 | if (WIFSIGNALED(status)) { |
| 81 | event.thing = LT_EV_EXIT_SIGNAL; |
| 82 | event.e_un.signum = WTERMSIG(status); |
| 83 | return &event; |
| 84 | } |
| 85 | if (!WIFSTOPPED(status)) { |
| 86 | event.thing = LT_EV_UNKNOWN; |
| 87 | return &event; |
| 88 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 89 | if ((WSTOPSIG(status) != (SIGTRAP | event.proc->tracesysgood)) && |
| 90 | (WSTOPSIG(status) != SIGTRAP)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 91 | event.thing = LT_EV_SIGNAL; |
| 92 | event.e_un.signum = WSTOPSIG(status); |
| 93 | return &event; |
| 94 | } |
| 95 | event.thing = LT_EV_BREAKPOINT; |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 96 | if (!event.proc->instruction_pointer) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 97 | event.proc->instruction_pointer = |
| 98 | get_instruction_pointer(event.proc); |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 99 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 100 | event.e_un.brk_addr = |
| 101 | event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 102 | return &event; |
| 103 | } |
| 104 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 105 | static struct process *pid2proc(pid_t pid) |
| 106 | { |
| 107 | struct process *tmp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 108 | |
| 109 | tmp = list_of_processes; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame^] | 110 | while (tmp) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 111 | if (pid == tmp->pid) { |
| 112 | return tmp; |
| 113 | } |
| 114 | tmp = tmp->next; |
| 115 | } |
| 116 | return NULL; |
| 117 | } |