blob: 9c376f38b207e45606e1dd9229f9bb9a19e7c180 [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"
Petr Machata366c2f42012-02-09 19:34:36 +010016#include "proc.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010017
Juan Cespedes393f1d02009-05-07 11:13:54 +020018static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010019
Petr Machata69a03e62011-07-09 11:29:12 +020020/* A queue of events that we missed while enabling the
21 * breakpoint in one of tasks. */
22static Event * delayed_events = NULL;
23static Event * end_delayed_events = NULL;
24
Petr Machatacebb8842011-07-09 11:14:11 +020025static enum pcb_status
26first (Process * proc, void * data)
27{
28 return pcb_stop;
29}
30
Petr Machata69a03e62011-07-09 11:29:12 +020031void
32enque_event(Event * event)
33{
34 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
35 event->proc->pid, event->type);
36 Event * ne = malloc(sizeof(*ne));
37 if (ne == NULL) {
38 perror("event will be missed: malloc");
39 return;
40 }
41
42 *ne = *event;
43 ne->next = NULL;
44 if (end_delayed_events == NULL) {
45 assert(delayed_events == NULL);
46 end_delayed_events = delayed_events = ne;
47 }
48 else {
49 assert(delayed_events != NULL);
50 end_delayed_events = end_delayed_events->next = ne;
51 }
52}
53
54Event *
55each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
56{
57 Event * prev = delayed_events;
58 Event * event;
59 for (event = prev; event != NULL; ) {
60 switch ((*pred)(event, data)) {
61 case ecb_cont:
62 prev = event;
63 event = event->next;
64 continue;
65
66 case ecb_deque:
67 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
68 event->type,
69 event->proc != NULL ? event->proc->pid : -1);
70 /*
71 printf("dequeuing event %d for %d\n", event->type,
72 event->proc != NULL ? event->proc->pid : -1) ;
73 */
74 if (end_delayed_events == event)
75 end_delayed_events = prev;
76 if (delayed_events == event)
77 delayed_events = event->next;
78 else
79 prev->next = event->next;
80 if (delayed_events == NULL)
81 end_delayed_events = NULL;
82 /* fall-through */
83
84 case ecb_yield:
85 return event;
86 }
87 }
88
89 return NULL;
90}
91
92static enum ecb_status
93event_process_not_reenabling(Event * event, void * data)
94{
Petr Machata4007d742011-07-09 11:29:42 +020095 if (event->proc == NULL
96 || event->proc->leader == NULL
97 || event->proc->leader->event_handler == NULL)
98 return ecb_deque;
99 else
100 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +0200101}
102
103static Event *
104next_qd_event(void)
105{
106 return each_qd_event(&event_process_not_reenabling, NULL);
107}
108
Petr Machataffe4cd22012-04-11 18:01:44 +0200109int linux_in_waitpid = 0;
110
Juan Cespedes393f1d02009-05-07 11:13:54 +0200111Event *
Petr Machata26627682011-07-08 18:15:32 +0200112next_event(void)
113{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100114 pid_t pid;
115 int status;
116 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200117 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100118
Juan Cespedescd8976d2009-05-14 13:47:58 +0200119 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200120 Event * ev;
121 if ((ev = next_qd_event()) != NULL) {
122 event = *ev;
123 free(ev);
124 return &event;
125 }
126
Petr Machatacebb8842011-07-09 11:14:11 +0200127 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200128 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200129 exit(0);
130 }
Petr Machataffe4cd22012-04-11 18:01:44 +0200131
132 linux_in_waitpid = 1;
Juan Cespedes6be80282009-05-28 19:06:00 +0200133 pid = waitpid(-1, &status, __WALL);
Petr Machataffe4cd22012-04-11 18:01:44 +0200134 linux_in_waitpid = 0;
135
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100136 if (pid == -1) {
137 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200138 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200141 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200142 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200143 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100144 }
145 perror("wait");
146 exit(1);
147 }
148 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200149 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100150 /* Work around (presumably) a bug on some kernels,
151 * where we are seeing a waitpid event even though the
152 * process is still reported to be running. Wait for
153 * the tracing stop to propagate. But don't get stuck
154 * here forever.
155 *
156 * We need the process in T, because there's a lot of
157 * ptracing going on all over the place, and these
158 * calls fail when the process is not in T.
159 *
160 * N.B. This was observed on RHEL 5 Itanium, but I'm
161 * turning this on globally, to save some poor soul
162 * down the road (which could well be me a year from
163 * now) the pain of figuring this out all over again.
164 * Petr Machata 2011-11-22. */
165 int i = 0;
166 for (; i < 100 && process_status(pid) != ps_tracing_stop; ++i) {
167 debug(2, "waiting for %d to stop", pid);
168 usleep(10000);
169 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200170 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200171 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200172 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200173 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100174 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200175 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200176 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200177 Process *leader = event.proc->leader;
178 if (leader == event.proc) {
Petr Machata02648a12012-02-07 13:44:54 +0100179 if (!event.proc->libdl_hooked) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200180 /* debug struct may not have been written yet.. */
181 if (linkmap_init(event.proc, &main_lte) == 0) {
182 event.proc->libdl_hooked = 1;
183 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800184 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100185 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800186
Petr Machataf2d2ba52011-07-09 11:03:26 +0200187 /* The process should be stopped after the waitpid call. But
188 * when the whole thread group is terminated, we see
189 * individual tasks spontaneously transitioning from 't' to
190 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
191 * not even be available anymore, so we can't check in
192 * advance. So we just drop the error checking around ptrace
193 * calls. We check for termination ex post when it fails,
194 * suppress the event, and let the event loop collect the
195 * termination in the next iteration. */
196#define CHECK_PROCESS_TERMINATED \
197 do { \
198 int errno_save = errno; \
199 switch (process_stopped(pid)) \
200 case 0: \
201 case -1: { \
202 debug(DEBUG_EVENT, \
203 "process not stopped, is it terminating?"); \
204 event.type = EVENT_NONE; \
205 continue_process(event.proc->pid); \
206 return &event; \
207 } \
208 errno = errno_save; \
209 } while (0)
210
Petr Machata9a5420c2011-07-09 11:21:23 +0200211 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200212
Petr Machataf2d2ba52011-07-09 11:03:26 +0200213 /* Check for task termination now, before we have a need to
214 * call CHECK_PROCESS_TERMINATED later. That would suppress
215 * the event that we are processing. */
216 if (WIFSIGNALED(status)) {
217 event.type = EVENT_EXIT_SIGNAL;
218 event.e_un.signum = WTERMSIG(status);
219 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
220 return &event;
221 }
222 if (WIFEXITED(status)) {
223 event.type = EVENT_EXIT;
224 event.e_un.ret_val = WEXITSTATUS(status);
225 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
226 return &event;
227 }
228
Petr Machata6901b7e2011-07-09 11:00:33 +0200229 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
230 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200231 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200232 if (errno != 0)
233 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100234 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200235
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100236 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100237 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200238 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100239 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200240 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100241 return &event;
242 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200243 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100244 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200245 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100246 return &event;
247 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200248 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100249 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200250 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100251 return &event;
252 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200253 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100254 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200255 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100256 return &event;
257 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200258 CHECK_PROCESS_TERMINATED;
259 if (errno != 0)
260 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100261 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200262 if (WIFSTOPPED(status)) {
263 int what = status >> 16;
264 if (what == PTRACE_EVENT_VFORK
265 || what == PTRACE_EVENT_FORK
266 || what == PTRACE_EVENT_CLONE) {
267 unsigned long data;
268 event.type = what == PTRACE_EVENT_VFORK
269 ? EVENT_VFORK : EVENT_CLONE;
270 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
271 event.e_un.newpid = data;
272 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
273 pid, (int)data);
274 return &event;
275 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200276 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200277 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200278 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200279 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200280 return &event;
281 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200283 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200284 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200285 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100286 return &event;
287 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200288
289 stop_signal = WSTOPSIG(status);
290
291 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200292 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
293 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200294
Petr Machata6901b7e2011-07-09 11:00:33 +0200295 N.B. see comments in GDB's infrun.c for details. I've
296 actually seen this on an Itanium machine on RHEL 5, I don't
297 remember the exact kernel version anymore. ia64-sigill.s
298 in the test suite tests this. Petr Machata 2011-06-08. */
299 void * break_address
300 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
301 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200302 && leader != NULL
303 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200304 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200305
306 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200307 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200308 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200309 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200310 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100311 return &event;
312 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200313
Juan Cespedes427b7812009-07-06 23:05:30 +0200314 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200315 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200316
Petr Machata6901b7e2011-07-09 11:00:33 +0200317 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200318 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200319
Juan Cespedes5e01f651998-03-08 22:31:44 +0100320 return &event;
321}