blob: 01670497a25a3c408682e44837fdd78b6e33ea20 [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"
Petr Machata9294d822012-02-07 12:35:58 +010015#include "breakpoint.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010016
Juan Cespedes393f1d02009-05-07 11:13:54 +020017static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Petr Machata69a03e62011-07-09 11:29:12 +020019/* A queue of events that we missed while enabling the
20 * breakpoint in one of tasks. */
21static Event * delayed_events = NULL;
22static Event * end_delayed_events = NULL;
23
Petr Machatacebb8842011-07-09 11:14:11 +020024static enum pcb_status
25first (Process * proc, void * data)
26{
27 return pcb_stop;
28}
29
Petr Machata69a03e62011-07-09 11:29:12 +020030void
31enque_event(Event * event)
32{
33 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
34 event->proc->pid, event->type);
35 Event * ne = malloc(sizeof(*ne));
36 if (ne == NULL) {
37 perror("event will be missed: malloc");
38 return;
39 }
40
41 *ne = *event;
42 ne->next = NULL;
43 if (end_delayed_events == NULL) {
44 assert(delayed_events == NULL);
45 end_delayed_events = delayed_events = ne;
46 }
47 else {
48 assert(delayed_events != NULL);
49 end_delayed_events = end_delayed_events->next = ne;
50 }
51}
52
53Event *
54each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
55{
56 Event * prev = delayed_events;
57 Event * event;
58 for (event = prev; event != NULL; ) {
59 switch ((*pred)(event, data)) {
60 case ecb_cont:
61 prev = event;
62 event = event->next;
63 continue;
64
65 case ecb_deque:
66 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
67 event->type,
68 event->proc != NULL ? event->proc->pid : -1);
69 /*
70 printf("dequeuing event %d for %d\n", event->type,
71 event->proc != NULL ? event->proc->pid : -1) ;
72 */
73 if (end_delayed_events == event)
74 end_delayed_events = prev;
75 if (delayed_events == event)
76 delayed_events = event->next;
77 else
78 prev->next = event->next;
79 if (delayed_events == NULL)
80 end_delayed_events = NULL;
81 /* fall-through */
82
83 case ecb_yield:
84 return event;
85 }
86 }
87
88 return NULL;
89}
90
91static enum ecb_status
92event_process_not_reenabling(Event * event, void * data)
93{
Petr Machata4007d742011-07-09 11:29:42 +020094 if (event->proc == NULL
95 || event->proc->leader == NULL
96 || event->proc->leader->event_handler == NULL)
97 return ecb_deque;
98 else
99 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +0200100}
101
102static Event *
103next_qd_event(void)
104{
105 return each_qd_event(&event_process_not_reenabling, NULL);
106}
107
Petr Machataffe4cd22012-04-11 18:01:44 +0200108int linux_in_waitpid = 0;
109
Juan Cespedes393f1d02009-05-07 11:13:54 +0200110Event *
Petr Machata26627682011-07-08 18:15:32 +0200111next_event(void)
112{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100113 pid_t pid;
114 int status;
115 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200116 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100117
Juan Cespedescd8976d2009-05-14 13:47:58 +0200118 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200119 Event * ev;
120 if ((ev = next_qd_event()) != NULL) {
121 event = *ev;
122 free(ev);
123 return &event;
124 }
125
Petr Machatacebb8842011-07-09 11:14:11 +0200126 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200127 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200128 exit(0);
129 }
Petr Machataffe4cd22012-04-11 18:01:44 +0200130
131 linux_in_waitpid = 1;
Juan Cespedes6be80282009-05-28 19:06:00 +0200132 pid = waitpid(-1, &status, __WALL);
Petr Machataffe4cd22012-04-11 18:01:44 +0200133 linux_in_waitpid = 0;
134
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100135 if (pid == -1) {
136 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200137 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100138 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200140 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200141 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200142 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143 }
144 perror("wait");
145 exit(1);
146 }
147 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200148 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100149 /* Work around (presumably) a bug on some kernels,
150 * where we are seeing a waitpid event even though the
151 * process is still reported to be running. Wait for
152 * the tracing stop to propagate. But don't get stuck
153 * here forever.
154 *
155 * We need the process in T, because there's a lot of
156 * ptracing going on all over the place, and these
157 * calls fail when the process is not in T.
158 *
159 * N.B. This was observed on RHEL 5 Itanium, but I'm
160 * turning this on globally, to save some poor soul
161 * down the road (which could well be me a year from
162 * now) the pain of figuring this out all over again.
163 * Petr Machata 2011-11-22. */
164 int i = 0;
165 for (; i < 100 && process_status(pid) != ps_tracing_stop; ++i) {
166 debug(2, "waiting for %d to stop", pid);
167 usleep(10000);
168 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200169 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200170 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200171 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200172 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100173 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200174 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200175 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200176 Process *leader = event.proc->leader;
177 if (leader == event.proc) {
Petr Machata02648a12012-02-07 13:44:54 +0100178 if (!event.proc->libdl_hooked) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200179 /* debug struct may not have been written yet.. */
180 if (linkmap_init(event.proc, &main_lte) == 0) {
181 event.proc->libdl_hooked = 1;
182 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800183 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100184 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800185
Petr Machataf2d2ba52011-07-09 11:03:26 +0200186 /* The process should be stopped after the waitpid call. But
187 * when the whole thread group is terminated, we see
188 * individual tasks spontaneously transitioning from 't' to
189 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
190 * not even be available anymore, so we can't check in
191 * advance. So we just drop the error checking around ptrace
192 * calls. We check for termination ex post when it fails,
193 * suppress the event, and let the event loop collect the
194 * termination in the next iteration. */
195#define CHECK_PROCESS_TERMINATED \
196 do { \
197 int errno_save = errno; \
198 switch (process_stopped(pid)) \
199 case 0: \
200 case -1: { \
201 debug(DEBUG_EVENT, \
202 "process not stopped, is it terminating?"); \
203 event.type = EVENT_NONE; \
204 continue_process(event.proc->pid); \
205 return &event; \
206 } \
207 errno = errno_save; \
208 } while (0)
209
Petr Machata9a5420c2011-07-09 11:21:23 +0200210 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200211
Petr Machataf2d2ba52011-07-09 11:03:26 +0200212 /* Check for task termination now, before we have a need to
213 * call CHECK_PROCESS_TERMINATED later. That would suppress
214 * the event that we are processing. */
215 if (WIFSIGNALED(status)) {
216 event.type = EVENT_EXIT_SIGNAL;
217 event.e_un.signum = WTERMSIG(status);
218 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
219 return &event;
220 }
221 if (WIFEXITED(status)) {
222 event.type = EVENT_EXIT;
223 event.e_un.ret_val = WEXITSTATUS(status);
224 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
225 return &event;
226 }
227
Petr Machata6901b7e2011-07-09 11:00:33 +0200228 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
229 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200230 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200231 if (errno != 0)
232 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100233 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200234
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100235 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100236 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200237 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100238 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200239 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100240 return &event;
241 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200242 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100243 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200244 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100245 return &event;
246 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200247 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100248 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200249 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100250 return &event;
251 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200252 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100253 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200254 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100255 return &event;
256 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200257 CHECK_PROCESS_TERMINATED;
258 if (errno != 0)
259 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100260 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200261 if (WIFSTOPPED(status)) {
262 int what = status >> 16;
263 if (what == PTRACE_EVENT_VFORK
264 || what == PTRACE_EVENT_FORK
265 || what == PTRACE_EVENT_CLONE) {
266 unsigned long data;
267 event.type = what == PTRACE_EVENT_VFORK
268 ? EVENT_VFORK : EVENT_CLONE;
269 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
270 event.e_un.newpid = data;
271 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
272 pid, (int)data);
273 return &event;
274 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200275 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200276 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200277 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200278 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200279 return &event;
280 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100281 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200282 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200283 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200284 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100285 return &event;
286 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200287
288 stop_signal = WSTOPSIG(status);
289
290 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200291 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
292 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200293
Petr Machata6901b7e2011-07-09 11:00:33 +0200294 N.B. see comments in GDB's infrun.c for details. I've
295 actually seen this on an Itanium machine on RHEL 5, I don't
296 remember the exact kernel version anymore. ia64-sigill.s
297 in the test suite tests this. Petr Machata 2011-06-08. */
298 void * break_address
299 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
300 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200301 && leader != NULL
302 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200303 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200304
305 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200306 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200307 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200308 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200309 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100310 return &event;
311 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200312
Juan Cespedes427b7812009-07-06 23:05:30 +0200313 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200314 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200315
Petr Machata6901b7e2011-07-09 11:00:33 +0200316 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200317 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200318
Juan Cespedes5e01f651998-03-08 22:31:44 +0100319 return &event;
320}