events.c revision 97b208405bcbf2fc75a70fba7d094740f590cb90
1#include "config.h"
2
3#define	_GNU_SOURCE	1
4#include <stdlib.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <errno.h>
8#include <signal.h>
9#include <string.h>
10#include <sys/ptrace.h>
11#include <assert.h>
12#include <unistd.h>
13
14#include "common.h"
15
16static Event event;
17
18/* 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
23static enum pcb_status
24first (Process * proc, void * data)
25{
26	return pcb_stop;
27}
28
29void
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{
93	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;
99}
100
101static Event *
102next_qd_event(void)
103{
104	return each_qd_event(&event_process_not_reenabling, NULL);
105}
106
107Event *
108next_event(void)
109{
110	pid_t pid;
111	int status;
112	int tmp;
113	int stop_signal;
114
115	debug(DEBUG_FUNCTION, "next_event()");
116	Event * ev;
117	if ((ev = next_qd_event()) != NULL) {
118		event = *ev;
119		free(ev);
120		return &event;
121	}
122
123	if (!each_process(NULL, &first, NULL)) {
124		debug(DEBUG_EVENT, "event: No more traced programs: exiting");
125		exit(0);
126	}
127	pid = waitpid(-1, &status, __WALL);
128	if (pid == -1) {
129		if (errno == ECHILD) {
130			debug(DEBUG_EVENT, "event: No more traced programs: exiting");
131			exit(0);
132		} else if (errno == EINTR) {
133			debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
134			event.type = EVENT_NONE;
135			return &event;
136		}
137		perror("wait");
138		exit(1);
139	}
140	event.proc = pid2proc(pid);
141	if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
142		/* 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		}
162		event.type = EVENT_NEW;
163		event.e_un.newpid = pid;
164		debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
165		return &event;
166	}
167	get_arch_dep(event.proc);
168	debug(3, "event from pid %u", pid);
169	if (event.proc->breakpoints_enabled == -1)
170		trace_set_options(event.proc, event.proc->pid);
171	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			}
186		}
187	}
188
189	/* 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
213	event.proc->instruction_pointer = (void *)(uintptr_t)-1;
214
215	/* 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
231	event.proc->instruction_pointer = get_instruction_pointer(event.proc);
232	if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
233		CHECK_PROCESS_TERMINATED;
234		if (errno != 0)
235			perror("get_instruction_pointer");
236	}
237
238	switch (syscall_p(event.proc, status, &tmp)) {
239		case 1:
240			event.type = EVENT_SYSCALL;
241			event.e_un.sysnum = tmp;
242			debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
243			return &event;
244		case 2:
245			event.type = EVENT_SYSRET;
246			event.e_un.sysnum = tmp;
247			debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
248			return &event;
249		case 3:
250			event.type = EVENT_ARCH_SYSCALL;
251			event.e_un.sysnum = tmp;
252			debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
253			return &event;
254		case 4:
255			event.type = EVENT_ARCH_SYSRET;
256			event.e_un.sysnum = tmp;
257			debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
258			return &event;
259		case -1:
260			CHECK_PROCESS_TERMINATED;
261			if (errno != 0)
262				perror("syscall_p");
263	}
264	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		}
278	}
279	if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
280		event.type = EVENT_EXEC;
281		debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
282		return &event;
283	}
284	if (!WIFSTOPPED(status)) {
285		/* should never happen */
286		event.type = EVENT_NONE;
287		debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
288		return &event;
289	}
290
291	stop_signal = WSTOPSIG(status);
292
293	/* On some targets, breakpoints are signalled not using
294	   SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT.  SIGEMT
295	   is not defined on Linux, but check for the others.
296
297	   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)
304	    && leader != NULL
305	    && address2bpstruct(leader, break_address))
306			stop_signal = SIGTRAP;
307
308	if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
309			&& stop_signal != SIGTRAP) {
310		event.type = EVENT_SIGNAL;
311		event.e_un.signum = stop_signal;
312		debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
313		return &event;
314	}
315
316	/* last case [by exhaustion] */
317	event.type = EVENT_BREAKPOINT;
318
319	event.e_un.brk_addr = break_address;
320	debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
321
322	return &event;
323}
324