blob: 021192f928f1e8ef4fad669526a9146af0f76872 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedes5e01f651998-03-08 22:31:44 +01003#define _GNU_SOURCE 1
Juan Cespedes1cd999a2001-07-03 00:46:04 +02004#include <stdlib.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <sys/types.h>
6#include <sys/wait.h>
7#include <errno.h>
8#include <signal.h>
9#include <string.h>
Juan Cespedes1e583132009-04-07 18:17:11 +020010#include <sys/ptrace.h>
Petr Machata69a03e62011-07-09 11:29:12 +020011#include <assert.h>
Petr Machata97b20842011-11-22 16:50:36 +010012#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013
Juan Cespedesf7281232009-06-25 16:11:21 +020014#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010015
Juan Cespedes393f1d02009-05-07 11:13:54 +020016static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010017
Petr Machata69a03e62011-07-09 11:29:12 +020018/* A queue of events that we missed while enabling the
19 * breakpoint in one of tasks. */
20static Event * delayed_events = NULL;
21static Event * end_delayed_events = NULL;
22
Petr Machatacebb8842011-07-09 11:14:11 +020023static enum pcb_status
24first (Process * proc, void * data)
25{
26 return pcb_stop;
27}
28
Petr Machata69a03e62011-07-09 11:29:12 +020029void
30enque_event(Event * event)
31{
32 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
33 event->proc->pid, event->type);
34 Event * ne = malloc(sizeof(*ne));
35 if (ne == NULL) {
36 perror("event will be missed: malloc");
37 return;
38 }
39
40 *ne = *event;
41 ne->next = NULL;
42 if (end_delayed_events == NULL) {
43 assert(delayed_events == NULL);
44 end_delayed_events = delayed_events = ne;
45 }
46 else {
47 assert(delayed_events != NULL);
48 end_delayed_events = end_delayed_events->next = ne;
49 }
50}
51
52Event *
53each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
54{
55 Event * prev = delayed_events;
56 Event * event;
57 for (event = prev; event != NULL; ) {
58 switch ((*pred)(event, data)) {
59 case ecb_cont:
60 prev = event;
61 event = event->next;
62 continue;
63
64 case ecb_deque:
65 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
66 event->type,
67 event->proc != NULL ? event->proc->pid : -1);
68 /*
69 printf("dequeuing event %d for %d\n", event->type,
70 event->proc != NULL ? event->proc->pid : -1) ;
71 */
72 if (end_delayed_events == event)
73 end_delayed_events = prev;
74 if (delayed_events == event)
75 delayed_events = event->next;
76 else
77 prev->next = event->next;
78 if (delayed_events == NULL)
79 end_delayed_events = NULL;
80 /* fall-through */
81
82 case ecb_yield:
83 return event;
84 }
85 }
86
87 return NULL;
88}
89
90static enum ecb_status
91event_process_not_reenabling(Event * event, void * data)
92{
Petr Machata4007d742011-07-09 11:29:42 +020093 if (event->proc == NULL
94 || event->proc->leader == NULL
95 || event->proc->leader->event_handler == NULL)
96 return ecb_deque;
97 else
98 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +020099}
100
101static Event *
102next_qd_event(void)
103{
104 return each_qd_event(&event_process_not_reenabling, NULL);
105}
106
Juan Cespedes393f1d02009-05-07 11:13:54 +0200107Event *
Petr Machata26627682011-07-08 18:15:32 +0200108next_event(void)
109{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100110 pid_t pid;
111 int status;
112 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200113 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100114
Juan Cespedescd8976d2009-05-14 13:47:58 +0200115 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200116 Event * ev;
117 if ((ev = next_qd_event()) != NULL) {
118 event = *ev;
119 free(ev);
120 return &event;
121 }
122
Petr Machatacebb8842011-07-09 11:14:11 +0200123 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200124 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200125 exit(0);
126 }
Juan Cespedes6be80282009-05-28 19:06:00 +0200127 pid = waitpid(-1, &status, __WALL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 if (pid == -1) {
129 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200130 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100131 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100132 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200133 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200134 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200135 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 }
137 perror("wait");
138 exit(1);
139 }
140 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200141 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100142 /* Work around (presumably) a bug on some kernels,
143 * where we are seeing a waitpid event even though the
144 * process is still reported to be running. Wait for
145 * the tracing stop to propagate. But don't get stuck
146 * here forever.
147 *
148 * We need the process in T, because there's a lot of
149 * ptracing going on all over the place, and these
150 * calls fail when the process is not in T.
151 *
152 * N.B. This was observed on RHEL 5 Itanium, but I'm
153 * turning this on globally, to save some poor soul
154 * down the road (which could well be me a year from
155 * now) the pain of figuring this out all over again.
156 * Petr Machata 2011-11-22. */
157 int i = 0;
158 for (; i < 100 && process_status(pid) != ps_tracing_stop; ++i) {
159 debug(2, "waiting for %d to stop", pid);
160 usleep(10000);
161 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200162 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200163 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200164 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200165 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100166 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200167 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200168 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200169 if (event.proc->breakpoints_enabled == -1)
Ian Wienand9a2ad352006-02-20 22:44:45 +0100170 trace_set_options(event.proc, event.proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200171 Process *leader = event.proc->leader;
172 if (leader == event.proc) {
173 if (event.proc->breakpoints_enabled == -1) {
174 event.type = EVENT_NONE;
175 enable_all_breakpoints(event.proc);
176 continue_process(event.proc->pid);
177 debug(DEBUG_EVENT,
178 "event: NONE: pid=%d (enabling breakpoints)",
179 pid);
180 return &event;
181 } else if (!event.proc->libdl_hooked) {
182 /* debug struct may not have been written yet.. */
183 if (linkmap_init(event.proc, &main_lte) == 0) {
184 event.proc->libdl_hooked = 1;
185 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800186 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100187 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800188
Petr Machataf2d2ba52011-07-09 11:03:26 +0200189 /* The process should be stopped after the waitpid call. But
190 * when the whole thread group is terminated, we see
191 * individual tasks spontaneously transitioning from 't' to
192 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
193 * not even be available anymore, so we can't check in
194 * advance. So we just drop the error checking around ptrace
195 * calls. We check for termination ex post when it fails,
196 * suppress the event, and let the event loop collect the
197 * termination in the next iteration. */
198#define CHECK_PROCESS_TERMINATED \
199 do { \
200 int errno_save = errno; \
201 switch (process_stopped(pid)) \
202 case 0: \
203 case -1: { \
204 debug(DEBUG_EVENT, \
205 "process not stopped, is it terminating?"); \
206 event.type = EVENT_NONE; \
207 continue_process(event.proc->pid); \
208 return &event; \
209 } \
210 errno = errno_save; \
211 } while (0)
212
Petr Machata9a5420c2011-07-09 11:21:23 +0200213 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200214
Petr Machataf2d2ba52011-07-09 11:03:26 +0200215 /* Check for task termination now, before we have a need to
216 * call CHECK_PROCESS_TERMINATED later. That would suppress
217 * the event that we are processing. */
218 if (WIFSIGNALED(status)) {
219 event.type = EVENT_EXIT_SIGNAL;
220 event.e_un.signum = WTERMSIG(status);
221 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
222 return &event;
223 }
224 if (WIFEXITED(status)) {
225 event.type = EVENT_EXIT;
226 event.e_un.ret_val = WEXITSTATUS(status);
227 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
228 return &event;
229 }
230
Petr Machata6901b7e2011-07-09 11:00:33 +0200231 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
232 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200233 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200234 if (errno != 0)
235 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100236 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200237
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100238 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100239 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200240 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100241 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200242 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100243 return &event;
244 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200245 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100246 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200247 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100248 return &event;
249 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200250 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100251 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200252 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100253 return &event;
254 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200255 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100256 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200257 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100258 return &event;
259 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200260 CHECK_PROCESS_TERMINATED;
261 if (errno != 0)
262 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100263 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200264 if (WIFSTOPPED(status)) {
265 int what = status >> 16;
266 if (what == PTRACE_EVENT_VFORK
267 || what == PTRACE_EVENT_FORK
268 || what == PTRACE_EVENT_CLONE) {
269 unsigned long data;
270 event.type = what == PTRACE_EVENT_VFORK
271 ? EVENT_VFORK : EVENT_CLONE;
272 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
273 event.e_un.newpid = data;
274 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
275 pid, (int)data);
276 return &event;
277 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200278 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200279 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200280 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200281 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200282 return &event;
283 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200285 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200286 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200287 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100288 return &event;
289 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200290
291 stop_signal = WSTOPSIG(status);
292
293 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200294 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
295 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200296
Petr Machata6901b7e2011-07-09 11:00:33 +0200297 N.B. see comments in GDB's infrun.c for details. I've
298 actually seen this on an Itanium machine on RHEL 5, I don't
299 remember the exact kernel version anymore. ia64-sigill.s
300 in the test suite tests this. Petr Machata 2011-06-08. */
301 void * break_address
302 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
303 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200304 && leader != NULL
305 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200306 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200307
308 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200309 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200310 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200311 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200312 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100313 return &event;
314 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200315
Juan Cespedes427b7812009-07-06 23:05:30 +0200316 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200317 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200318
Petr Machata6901b7e2011-07-09 11:00:33 +0200319 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200320 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200321
Juan Cespedes5e01f651998-03-08 22:31:44 +0100322 return &event;
323}