blob: 21b327b183ec5eb7946de2ec3ed2a491fb2c9ccf [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"
Petr Machatacd972582012-01-07 03:02:07 +010017#include "events.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Juan Cespedes393f1d02009-05-07 11:13:54 +020019static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010020
Petr Machata69a03e62011-07-09 11:29:12 +020021/* A queue of events that we missed while enabling the
22 * breakpoint in one of tasks. */
23static Event * delayed_events = NULL;
24static Event * end_delayed_events = NULL;
25
Petr Machata2b46cfc2012-02-18 11:17:29 +010026static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +020027first (Process * proc, void * data)
28{
Petr Machata2b46cfc2012-02-18 11:17:29 +010029 return CBS_STOP;
Petr Machatacebb8842011-07-09 11:14:11 +020030}
31
Petr Machata69a03e62011-07-09 11:29:12 +020032void
33enque_event(Event * event)
34{
35 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
36 event->proc->pid, event->type);
37 Event * ne = malloc(sizeof(*ne));
38 if (ne == NULL) {
39 perror("event will be missed: malloc");
40 return;
41 }
42
43 *ne = *event;
44 ne->next = NULL;
45 if (end_delayed_events == NULL) {
46 assert(delayed_events == NULL);
47 end_delayed_events = delayed_events = ne;
48 }
49 else {
50 assert(delayed_events != NULL);
51 end_delayed_events = end_delayed_events->next = ne;
52 }
53}
54
55Event *
56each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
57{
58 Event * prev = delayed_events;
59 Event * event;
60 for (event = prev; event != NULL; ) {
61 switch ((*pred)(event, data)) {
62 case ecb_cont:
63 prev = event;
64 event = event->next;
65 continue;
66
67 case ecb_deque:
68 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
69 event->type,
70 event->proc != NULL ? event->proc->pid : -1);
71 /*
72 printf("dequeuing event %d for %d\n", event->type,
73 event->proc != NULL ? event->proc->pid : -1) ;
74 */
75 if (end_delayed_events == event)
76 end_delayed_events = prev;
77 if (delayed_events == event)
78 delayed_events = event->next;
79 else
80 prev->next = event->next;
81 if (delayed_events == NULL)
82 end_delayed_events = NULL;
83 /* fall-through */
84
85 case ecb_yield:
86 return event;
87 }
88 }
89
90 return NULL;
91}
92
93static enum ecb_status
94event_process_not_reenabling(Event * event, void * data)
95{
Petr Machata4007d742011-07-09 11:29:42 +020096 if (event->proc == NULL
97 || event->proc->leader == NULL
98 || event->proc->leader->event_handler == NULL)
99 return ecb_deque;
100 else
101 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +0200102}
103
104static Event *
105next_qd_event(void)
106{
107 return each_qd_event(&event_process_not_reenabling, NULL);
108}
109
Petr Machataffe4cd22012-04-11 18:01:44 +0200110int linux_in_waitpid = 0;
111
Juan Cespedes393f1d02009-05-07 11:13:54 +0200112Event *
Petr Machata26627682011-07-08 18:15:32 +0200113next_event(void)
114{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100115 pid_t pid;
116 int status;
117 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200118 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119
Juan Cespedescd8976d2009-05-14 13:47:58 +0200120 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200121 Event * ev;
122 if ((ev = next_qd_event()) != NULL) {
123 event = *ev;
124 free(ev);
125 return &event;
126 }
127
Petr Machatacebb8842011-07-09 11:14:11 +0200128 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200129 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200130 exit(0);
131 }
Petr Machataffe4cd22012-04-11 18:01:44 +0200132
133 linux_in_waitpid = 1;
Juan Cespedes6be80282009-05-28 19:06:00 +0200134 pid = waitpid(-1, &status, __WALL);
Petr Machataffe4cd22012-04-11 18:01:44 +0200135 linux_in_waitpid = 0;
136
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 if (pid == -1) {
138 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200139 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100140 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200142 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200143 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200144 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100145 }
146 perror("wait");
147 exit(1);
148 }
149 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200150 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100151 /* Work around (presumably) a bug on some kernels,
152 * where we are seeing a waitpid event even though the
153 * process is still reported to be running. Wait for
154 * the tracing stop to propagate. But don't get stuck
155 * here forever.
156 *
157 * We need the process in T, because there's a lot of
158 * ptracing going on all over the place, and these
159 * calls fail when the process is not in T.
160 *
161 * N.B. This was observed on RHEL 5 Itanium, but I'm
162 * turning this on globally, to save some poor soul
163 * down the road (which could well be me a year from
164 * now) the pain of figuring this out all over again.
165 * Petr Machata 2011-11-22. */
166 int i = 0;
167 for (; i < 100 && process_status(pid) != ps_tracing_stop; ++i) {
168 debug(2, "waiting for %d to stop", pid);
169 usleep(10000);
170 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200171 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200172 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200173 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200174 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100175 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200176 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200177 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200178 Process *leader = event.proc->leader;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800179
Petr Machataf2d2ba52011-07-09 11:03:26 +0200180 /* The process should be stopped after the waitpid call. But
181 * when the whole thread group is terminated, we see
182 * individual tasks spontaneously transitioning from 't' to
183 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
184 * not even be available anymore, so we can't check in
185 * advance. So we just drop the error checking around ptrace
186 * calls. We check for termination ex post when it fails,
187 * suppress the event, and let the event loop collect the
188 * termination in the next iteration. */
189#define CHECK_PROCESS_TERMINATED \
190 do { \
191 int errno_save = errno; \
192 switch (process_stopped(pid)) \
193 case 0: \
194 case -1: { \
195 debug(DEBUG_EVENT, \
196 "process not stopped, is it terminating?"); \
197 event.type = EVENT_NONE; \
198 continue_process(event.proc->pid); \
199 return &event; \
200 } \
201 errno = errno_save; \
202 } while (0)
203
Petr Machata9a5420c2011-07-09 11:21:23 +0200204 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200205
Petr Machataf2d2ba52011-07-09 11:03:26 +0200206 /* Check for task termination now, before we have a need to
207 * call CHECK_PROCESS_TERMINATED later. That would suppress
208 * the event that we are processing. */
209 if (WIFSIGNALED(status)) {
210 event.type = EVENT_EXIT_SIGNAL;
211 event.e_un.signum = WTERMSIG(status);
212 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
213 return &event;
214 }
215 if (WIFEXITED(status)) {
216 event.type = EVENT_EXIT;
217 event.e_un.ret_val = WEXITSTATUS(status);
218 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
219 return &event;
220 }
221
Petr Machata6901b7e2011-07-09 11:00:33 +0200222 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
223 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200224 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200225 if (errno != 0)
226 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100227 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200228
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100230 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200231 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100232 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200233 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100234 return &event;
235 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200236 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100237 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200238 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100239 return &event;
240 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200241 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100242 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200243 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100244 return &event;
245 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200246 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100247 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200248 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100249 return &event;
250 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200251 CHECK_PROCESS_TERMINATED;
252 if (errno != 0)
253 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100254 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200255 if (WIFSTOPPED(status)) {
256 int what = status >> 16;
257 if (what == PTRACE_EVENT_VFORK
258 || what == PTRACE_EVENT_FORK
259 || what == PTRACE_EVENT_CLONE) {
260 unsigned long data;
261 event.type = what == PTRACE_EVENT_VFORK
262 ? EVENT_VFORK : EVENT_CLONE;
263 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
264 event.e_un.newpid = data;
265 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
266 pid, (int)data);
267 return &event;
268 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200269 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200270 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200271 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200272 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200273 return &event;
274 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100275 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200276 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200277 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200278 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279 return &event;
280 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200281
282 stop_signal = WSTOPSIG(status);
283
284 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200285 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
286 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200287
Petr Machata6901b7e2011-07-09 11:00:33 +0200288 N.B. see comments in GDB's infrun.c for details. I've
289 actually seen this on an Itanium machine on RHEL 5, I don't
290 remember the exact kernel version anymore. ia64-sigill.s
291 in the test suite tests this. Petr Machata 2011-06-08. */
292 void * break_address
293 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
294 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200295 && leader != NULL
296 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200297 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200298
299 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200300 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200301 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200302 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200303 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304 return &event;
305 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200306
Juan Cespedes427b7812009-07-06 23:05:30 +0200307 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200308 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200309
Petr Machata6901b7e2011-07-09 11:00:33 +0200310 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200311 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200312
Juan Cespedes5e01f651998-03-08 22:31:44 +0100313 return &event;
314}
Petr Machatacd972582012-01-07 03:02:07 +0100315
316static enum ecb_status
317event_for_proc(struct Event *event, void *data)
318{
319 if (event->proc == data)
320 return ecb_deque;
321 else
322 return ecb_cont;
323}
324
325void
326delete_events_for(struct Process *proc)
327{
328 struct Event *event;
329 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
330 free(event);
331}