blob: 8a79583995a863df46c8b8074b73f98a6bbf8f29 [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>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
Juan Cespedesf7281232009-06-25 16:11:21 +020013#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010014
Juan Cespedes393f1d02009-05-07 11:13:54 +020015static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010016
Petr Machata69a03e62011-07-09 11:29:12 +020017/* A queue of events that we missed while enabling the
18 * breakpoint in one of tasks. */
19static Event * delayed_events = NULL;
20static Event * end_delayed_events = NULL;
21
Petr Machatacebb8842011-07-09 11:14:11 +020022static enum pcb_status
23first (Process * proc, void * data)
24{
25 return pcb_stop;
26}
27
Petr Machata69a03e62011-07-09 11:29:12 +020028void
29enque_event(Event * event)
30{
31 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
32 event->proc->pid, event->type);
33 Event * ne = malloc(sizeof(*ne));
34 if (ne == NULL) {
35 perror("event will be missed: malloc");
36 return;
37 }
38
39 *ne = *event;
40 ne->next = NULL;
41 if (end_delayed_events == NULL) {
42 assert(delayed_events == NULL);
43 end_delayed_events = delayed_events = ne;
44 }
45 else {
46 assert(delayed_events != NULL);
47 end_delayed_events = end_delayed_events->next = ne;
48 }
49}
50
51Event *
52each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
53{
54 Event * prev = delayed_events;
55 Event * event;
56 for (event = prev; event != NULL; ) {
57 switch ((*pred)(event, data)) {
58 case ecb_cont:
59 prev = event;
60 event = event->next;
61 continue;
62
63 case ecb_deque:
64 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
65 event->type,
66 event->proc != NULL ? event->proc->pid : -1);
67 /*
68 printf("dequeuing event %d for %d\n", event->type,
69 event->proc != NULL ? event->proc->pid : -1) ;
70 */
71 if (end_delayed_events == event)
72 end_delayed_events = prev;
73 if (delayed_events == event)
74 delayed_events = event->next;
75 else
76 prev->next = event->next;
77 if (delayed_events == NULL)
78 end_delayed_events = NULL;
79 /* fall-through */
80
81 case ecb_yield:
82 return event;
83 }
84 }
85
86 return NULL;
87}
88
89static enum ecb_status
90event_process_not_reenabling(Event * event, void * data)
91{
Petr Machata4007d742011-07-09 11:29:42 +020092 if (event->proc == NULL
93 || event->proc->leader == NULL
94 || event->proc->leader->event_handler == NULL)
95 return ecb_deque;
96 else
97 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +020098}
99
100static Event *
101next_qd_event(void)
102{
103 return each_qd_event(&event_process_not_reenabling, NULL);
104}
105
Juan Cespedes393f1d02009-05-07 11:13:54 +0200106Event *
Petr Machata26627682011-07-08 18:15:32 +0200107next_event(void)
108{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100109 pid_t pid;
110 int status;
111 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200112 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100113
Juan Cespedescd8976d2009-05-14 13:47:58 +0200114 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200115 Event * ev;
116 if ((ev = next_qd_event()) != NULL) {
117 event = *ev;
118 free(ev);
119 return &event;
120 }
121
Petr Machatacebb8842011-07-09 11:14:11 +0200122 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200123 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200124 exit(0);
125 }
Juan Cespedes6be80282009-05-28 19:06:00 +0200126 pid = waitpid(-1, &status, __WALL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 if (pid == -1) {
128 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200129 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200132 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200133 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200134 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100135 }
136 perror("wait");
137 exit(1);
138 }
139 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200140 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200141 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200142 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200143 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200144 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100145 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200146 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200147 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200148 if (event.proc->breakpoints_enabled == -1)
Ian Wienand9a2ad352006-02-20 22:44:45 +0100149 trace_set_options(event.proc, event.proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200150 Process *leader = event.proc->leader;
151 if (leader == event.proc) {
152 if (event.proc->breakpoints_enabled == -1) {
153 event.type = EVENT_NONE;
154 enable_all_breakpoints(event.proc);
155 continue_process(event.proc->pid);
156 debug(DEBUG_EVENT,
157 "event: NONE: pid=%d (enabling breakpoints)",
158 pid);
159 return &event;
160 } else if (!event.proc->libdl_hooked) {
161 /* debug struct may not have been written yet.. */
162 if (linkmap_init(event.proc, &main_lte) == 0) {
163 event.proc->libdl_hooked = 1;
164 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800165 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100166 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800167
Petr Machataf2d2ba52011-07-09 11:03:26 +0200168 /* The process should be stopped after the waitpid call. But
169 * when the whole thread group is terminated, we see
170 * individual tasks spontaneously transitioning from 't' to
171 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
172 * not even be available anymore, so we can't check in
173 * advance. So we just drop the error checking around ptrace
174 * calls. We check for termination ex post when it fails,
175 * suppress the event, and let the event loop collect the
176 * termination in the next iteration. */
177#define CHECK_PROCESS_TERMINATED \
178 do { \
179 int errno_save = errno; \
180 switch (process_stopped(pid)) \
181 case 0: \
182 case -1: { \
183 debug(DEBUG_EVENT, \
184 "process not stopped, is it terminating?"); \
185 event.type = EVENT_NONE; \
186 continue_process(event.proc->pid); \
187 return &event; \
188 } \
189 errno = errno_save; \
190 } while (0)
191
Petr Machata9a5420c2011-07-09 11:21:23 +0200192 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200193
Petr Machataf2d2ba52011-07-09 11:03:26 +0200194 /* Check for task termination now, before we have a need to
195 * call CHECK_PROCESS_TERMINATED later. That would suppress
196 * the event that we are processing. */
197 if (WIFSIGNALED(status)) {
198 event.type = EVENT_EXIT_SIGNAL;
199 event.e_un.signum = WTERMSIG(status);
200 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
201 return &event;
202 }
203 if (WIFEXITED(status)) {
204 event.type = EVENT_EXIT;
205 event.e_un.ret_val = WEXITSTATUS(status);
206 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
207 return &event;
208 }
209
Petr Machata6901b7e2011-07-09 11:00:33 +0200210 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
211 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200212 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200213 if (errno != 0)
214 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100215 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100218 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200219 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100220 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200221 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100222 return &event;
223 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200224 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100225 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200226 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100227 return &event;
228 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200229 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100230 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200231 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100232 return &event;
233 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200234 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100235 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200236 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100237 return &event;
238 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200239 CHECK_PROCESS_TERMINATED;
240 if (errno != 0)
241 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100242 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200243 if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) {
244 unsigned long data;
245 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200246 event.type = EVENT_CLONE;
Juan Cespedes1e583132009-04-07 18:17:11 +0200247 event.e_un.newpid = data;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200248 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d", pid, (int)data);
Juan Cespedes1e583132009-04-07 18:17:11 +0200249 return &event;
250 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200251 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200252 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200253 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200254 return &event;
255 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100256 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200257 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200258 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200259 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100260 return &event;
261 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200262
263 stop_signal = WSTOPSIG(status);
264
265 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200266 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
267 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200268
Petr Machata6901b7e2011-07-09 11:00:33 +0200269 N.B. see comments in GDB's infrun.c for details. I've
270 actually seen this on an Itanium machine on RHEL 5, I don't
271 remember the exact kernel version anymore. ia64-sigill.s
272 in the test suite tests this. Petr Machata 2011-06-08. */
273 void * break_address
274 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
275 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200276 && leader != NULL
277 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200278 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200279
280 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200281 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200282 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200283 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200284 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100285 return &event;
286 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200287
Juan Cespedes427b7812009-07-06 23:05:30 +0200288 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200289 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200290
Petr Machata6901b7e2011-07-09 11:00:33 +0200291 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200292 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200293
Juan Cespedes5e01f651998-03-08 22:31:44 +0100294 return &event;
295}