blob: c65de41df272c563ed6d680849bb32eb55fcab1b [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#define _GNU_SOURCE 1
Juan Cespedes1cd999a2001-07-03 00:46:04 +02006#include <stdlib.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01007#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 Cespedescac15c32003-01-31 18:58:58 +010015#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010016
17static struct event event;
18
19/* This should also update `current_process' */
20
Ian Wienand2d45b1a2006-02-20 22:48:07 +010021static struct process *pid2proc(int pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010022
Ian Wienand2d45b1a2006-02-20 22:48:07 +010023struct event *wait_for_something(void)
24{
Juan Cespedes5e01f651998-03-08 22:31:44 +010025 pid_t pid;
26 int status;
27 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +020028 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +010029
Juan Cespedes28f60191998-04-12 00:04:39 +020030 if (!list_of_processes) {
Juan Cespedescac15c32003-01-31 18:58:58 +010031 debug(1, "No more children");
Juan Cespedes28f60191998-04-12 00:04:39 +020032 exit(0);
33 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010034 pid = wait(&status);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 if (pid == -1) {
36 if (errno == ECHILD) {
Juan Cespedescac15c32003-01-31 18:58:58 +010037 debug(1, "No more children");
Juan Cespedes5e01f651998-03-08 22:31:44 +010038 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 } else if (errno == EINTR) {
Juan Cespedescac15c32003-01-31 18:58:58 +010040 debug(1, "wait received EINTR ?");
Juan Cespedes28f60191998-04-12 00:04:39 +020041 event.thing = LT_EV_NONE;
42 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010043 }
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 Cespedes5c3fe062004-06-14 18:08:37 +020052 get_arch_dep(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010053 event.proc->instruction_pointer = NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +010054 debug(3, "signal from pid %u", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010055 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010056 enable_all_breakpoints(event.proc);
57 event.thing = LT_EV_NONE;
Ian Wienand9a2ad352006-02-20 22:44:45 +010058 trace_set_options(event.proc, event.proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010059 continue_process(event.proc->pid);
60 return &event;
61 }
Juan Cespedes35d70631998-03-15 14:05:40 +010062 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 event.proc->instruction_pointer =
64 get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010065 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 switch (syscall_p(event.proc, status, &tmp)) {
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;
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 }
76 if (WIFEXITED(status)) {
77 event.thing = LT_EV_EXIT;
78 event.e_un.ret_val = WEXITSTATUS(status);
79 return &event;
80 }
81 if (WIFSIGNALED(status)) {
82 event.thing = LT_EV_EXIT_SIGNAL;
83 event.e_un.signum = WTERMSIG(status);
84 return &event;
85 }
86 if (!WIFSTOPPED(status)) {
87 event.thing = LT_EV_UNKNOWN;
88 return &event;
89 }
Petr Machataef46b3e2007-05-09 19:21:42 +020090
91 stop_signal = WSTOPSIG(status);
92
93 /* On some targets, breakpoints are signalled not using
94 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check
95 for these. */
96 if (stop_signal == SIGSEGV
97 || stop_signal == SIGILL
98#ifdef SIGEMT
99 || stop_signal == SIGEMT
100#endif
101 ) {
102 // If we didn't need to know IP so far, get it now.
103 void * addr = opt_i
104 ? event.proc->instruction_pointer
105 : (event.proc->instruction_pointer = get_instruction_pointer (event.proc));
106
107 if (address2bpstruct(event.proc, addr))
108 stop_signal = SIGTRAP;
109 }
110
111 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
112 && stop_signal != SIGTRAP) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100113 event.thing = LT_EV_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200114 event.e_un.signum = stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100115 return &event;
116 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200117
118 if (was_exec(event.proc, status)) {
119 pid_t saved_pid;
120
121 event.thing = LT_EV_NONE;
122 event.e_un.signum = WSTOPSIG(status);
123 debug(1, "Placing breakpoints for the new program");
124 event.proc->mask_32bit = 0;
125 event.proc->personality = 0;
126 event.proc->arch_ptr = NULL;
127 event.proc->filename = pid2name(event.proc->pid);
128 saved_pid = event.proc->pid;
129 event.proc->pid = 0;
130 breakpoints_init(event.proc);
131 event.proc->pid = saved_pid;
132 continue_process(event.proc->pid);
133 return &event;
134 }
135
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 event.thing = LT_EV_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +0100137 if (!event.proc->instruction_pointer) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 event.proc->instruction_pointer =
139 get_instruction_pointer(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +0100140 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 event.e_un.brk_addr =
142 event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143 return &event;
144}
145
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146static struct process *pid2proc(pid_t pid)
147{
148 struct process *tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149
150 tmp = list_of_processes;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 while (tmp) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 if (pid == tmp->pid) {
153 return tmp;
154 }
155 tmp = tmp->next;
156 }
157 return NULL;
158}