| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #define _GNU_SOURCE 1 |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/wait.h> |
| 4 | #include <errno.h> |
| 5 | #include <signal.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "ltrace.h" |
| 9 | #include "options.h" |
| 10 | #include "output.h" |
| 11 | |
| 12 | static struct event event; |
| 13 | |
| 14 | /* This should also update `current_process' */ |
| 15 | |
| 16 | static struct process * pid2proc(int pid); |
| 17 | |
| 18 | struct event * wait_for_something(void) |
| 19 | { |
| 20 | pid_t pid; |
| 21 | int status; |
| 22 | int tmp; |
| 23 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame^] | 24 | if (!list_of_processes) { |
| 25 | if (opt_d) { |
| 26 | output_line(0, "No more children"); |
| 27 | } |
| 28 | exit(0); |
| 29 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 30 | pid = wait(&status); |
| 31 | if (pid==-1) { |
| 32 | if (errno==ECHILD) { |
| 33 | if (opt_d) { |
| 34 | output_line(0, "No more children"); |
| 35 | } |
| 36 | exit(0); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame^] | 37 | } else if (errno==EINTR) { |
| 38 | if (opt_d) { |
| 39 | output_line(0, "wait received EINTR ?"); |
| 40 | } |
| 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 | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 52 | event.proc->instruction_pointer = NULL; |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 53 | if (opt_d>2) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 54 | output_line(0,"signal from pid %u", pid); |
| 55 | } |
| 56 | if (event.proc->breakpoints_enabled == -1) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 57 | enable_all_breakpoints(event.proc); |
| 58 | event.thing = LT_EV_NONE; |
| 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) { |
| 63 | event.proc->instruction_pointer = get_instruction_pointer(pid); |
| Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 64 | } |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 65 | switch(syscall_p(event.proc, status, &tmp)) { |
| 66 | case 1: event.thing = LT_EV_SYSCALL; |
| 67 | event.e_un.sysnum = tmp; |
| 68 | return &event; |
| 69 | case 2: event.thing = LT_EV_SYSRET; |
| 70 | event.e_un.sysnum = tmp; |
| 71 | return &event; |
| 72 | default: |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 73 | } |
| 74 | if (WIFEXITED(status)) { |
| 75 | event.thing = LT_EV_EXIT; |
| 76 | event.e_un.ret_val = WEXITSTATUS(status); |
| 77 | return &event; |
| 78 | } |
| 79 | if (WIFSIGNALED(status)) { |
| 80 | event.thing = LT_EV_EXIT_SIGNAL; |
| 81 | event.e_un.signum = WTERMSIG(status); |
| 82 | return &event; |
| 83 | } |
| 84 | if (!WIFSTOPPED(status)) { |
| 85 | event.thing = LT_EV_UNKNOWN; |
| 86 | return &event; |
| 87 | } |
| 88 | if (WSTOPSIG(status) != SIGTRAP) { |
| 89 | event.thing = LT_EV_SIGNAL; |
| 90 | event.e_un.signum = WSTOPSIG(status); |
| 91 | return &event; |
| 92 | } |
| 93 | event.thing = LT_EV_BREAKPOINT; |
| Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 94 | if (!event.proc->instruction_pointer) { |
| 95 | event.proc->instruction_pointer = get_instruction_pointer(pid); |
| 96 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 97 | event.e_un.brk_addr = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK; |
| 98 | return &event; |
| 99 | } |
| 100 | |
| 101 | static struct process * pid2proc(int pid) |
| 102 | { |
| 103 | struct process * tmp; |
| 104 | |
| 105 | tmp = list_of_processes; |
| 106 | while(tmp) { |
| 107 | if (pid == tmp->pid) { |
| 108 | return tmp; |
| 109 | } |
| 110 | tmp = tmp->next; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |