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