blob: 6e5eb60dcadb7ebdde0f463695931f26871e0f8c [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2007,2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 1998,2001,2004,2007,2008,2009 Juan Cespedes
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
Juan Cespedesd44c6b81998-09-25 14:48:42 +020022#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020023
Juan Cespedes5e01f651998-03-08 22:31:44 +010024#define _GNU_SOURCE 1
Juan Cespedes1cd999a2001-07-03 00:46:04 +020025#include <stdlib.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010026#include <sys/types.h>
27#include <sys/wait.h>
28#include <errno.h>
29#include <signal.h>
30#include <string.h>
Juan Cespedes1e583132009-04-07 18:17:11 +020031#include <sys/ptrace.h>
Petr Machata69a03e62011-07-09 11:29:12 +020032#include <assert.h>
Petr Machata97b20842011-11-22 16:50:36 +010033#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010034
Petr Machata9294d822012-02-07 12:35:58 +010035#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010036#include "proc.h"
Petr Machata64262602012-01-07 03:41:36 +010037#include "backend.h"
Petr Machatacd972582012-01-07 03:02:07 +010038#include "events.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010039
Juan Cespedes393f1d02009-05-07 11:13:54 +020040static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010041
Petr Machata69a03e62011-07-09 11:29:12 +020042/* A queue of events that we missed while enabling the
43 * breakpoint in one of tasks. */
44static Event * delayed_events = NULL;
45static Event * end_delayed_events = NULL;
46
Petr Machata2b46cfc2012-02-18 11:17:29 +010047static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +020048first (Process * proc, void * data)
49{
Petr Machata2b46cfc2012-02-18 11:17:29 +010050 return CBS_STOP;
Petr Machatacebb8842011-07-09 11:14:11 +020051}
52
Petr Machata69a03e62011-07-09 11:29:12 +020053void
54enque_event(Event * event)
55{
56 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
57 event->proc->pid, event->type);
58 Event * ne = malloc(sizeof(*ne));
59 if (ne == NULL) {
60 perror("event will be missed: malloc");
61 return;
62 }
63
64 *ne = *event;
65 ne->next = NULL;
66 if (end_delayed_events == NULL) {
67 assert(delayed_events == NULL);
68 end_delayed_events = delayed_events = ne;
69 }
70 else {
71 assert(delayed_events != NULL);
72 end_delayed_events = end_delayed_events->next = ne;
73 }
74}
75
76Event *
77each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
78{
79 Event * prev = delayed_events;
80 Event * event;
81 for (event = prev; event != NULL; ) {
82 switch ((*pred)(event, data)) {
83 case ecb_cont:
84 prev = event;
85 event = event->next;
86 continue;
87
88 case ecb_deque:
89 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
90 event->type,
91 event->proc != NULL ? event->proc->pid : -1);
92 /*
93 printf("dequeuing event %d for %d\n", event->type,
94 event->proc != NULL ? event->proc->pid : -1) ;
95 */
96 if (end_delayed_events == event)
97 end_delayed_events = prev;
98 if (delayed_events == event)
99 delayed_events = event->next;
100 else
101 prev->next = event->next;
102 if (delayed_events == NULL)
103 end_delayed_events = NULL;
104 /* fall-through */
105
106 case ecb_yield:
107 return event;
108 }
109 }
110
111 return NULL;
112}
113
114static enum ecb_status
115event_process_not_reenabling(Event * event, void * data)
116{
Petr Machata4007d742011-07-09 11:29:42 +0200117 if (event->proc == NULL
118 || event->proc->leader == NULL
119 || event->proc->leader->event_handler == NULL)
120 return ecb_deque;
121 else
122 return ecb_cont;
Petr Machata69a03e62011-07-09 11:29:12 +0200123}
124
125static Event *
126next_qd_event(void)
127{
128 return each_qd_event(&event_process_not_reenabling, NULL);
129}
130
Petr Machataffe4cd22012-04-11 18:01:44 +0200131int linux_in_waitpid = 0;
132
Juan Cespedes393f1d02009-05-07 11:13:54 +0200133Event *
Petr Machata26627682011-07-08 18:15:32 +0200134next_event(void)
135{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 pid_t pid;
137 int status;
138 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200139 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100140
Juan Cespedescd8976d2009-05-14 13:47:58 +0200141 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200142 Event * ev;
143 if ((ev = next_qd_event()) != NULL) {
144 event = *ev;
145 free(ev);
146 return &event;
147 }
148
Petr Machatacebb8842011-07-09 11:14:11 +0200149 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200150 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200151 exit(0);
152 }
Petr Machataffe4cd22012-04-11 18:01:44 +0200153
154 linux_in_waitpid = 1;
Juan Cespedes6be80282009-05-28 19:06:00 +0200155 pid = waitpid(-1, &status, __WALL);
Petr Machataffe4cd22012-04-11 18:01:44 +0200156 linux_in_waitpid = 0;
157
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 if (pid == -1) {
159 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200160 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100162 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200163 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200164 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200165 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100166 }
167 perror("wait");
168 exit(1);
169 }
170 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200171 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100172 /* Work around (presumably) a bug on some kernels,
173 * where we are seeing a waitpid event even though the
174 * process is still reported to be running. Wait for
175 * the tracing stop to propagate. But don't get stuck
176 * here forever.
177 *
178 * We need the process in T, because there's a lot of
179 * ptracing going on all over the place, and these
180 * calls fail when the process is not in T.
181 *
182 * N.B. This was observed on RHEL 5 Itanium, but I'm
183 * turning this on globally, to save some poor soul
184 * down the road (which could well be me a year from
185 * now) the pain of figuring this out all over again.
186 * Petr Machata 2011-11-22. */
187 int i = 0;
188 for (; i < 100 && process_status(pid) != ps_tracing_stop; ++i) {
189 debug(2, "waiting for %d to stop", pid);
190 usleep(10000);
191 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200192 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200193 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200194 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200195 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100196 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200197 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200198 debug(3, "event from pid %u", pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200199 Process *leader = event.proc->leader;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800200
Petr Machataf2d2ba52011-07-09 11:03:26 +0200201 /* The process should be stopped after the waitpid call. But
202 * when the whole thread group is terminated, we see
203 * individual tasks spontaneously transitioning from 't' to
204 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
205 * not even be available anymore, so we can't check in
206 * advance. So we just drop the error checking around ptrace
207 * calls. We check for termination ex post when it fails,
208 * suppress the event, and let the event loop collect the
209 * termination in the next iteration. */
210#define CHECK_PROCESS_TERMINATED \
211 do { \
212 int errno_save = errno; \
213 switch (process_stopped(pid)) \
214 case 0: \
215 case -1: { \
216 debug(DEBUG_EVENT, \
217 "process not stopped, is it terminating?"); \
218 event.type = EVENT_NONE; \
219 continue_process(event.proc->pid); \
220 return &event; \
221 } \
222 errno = errno_save; \
223 } while (0)
224
Petr Machata9a5420c2011-07-09 11:21:23 +0200225 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200226
Petr Machataf2d2ba52011-07-09 11:03:26 +0200227 /* Check for task termination now, before we have a need to
228 * call CHECK_PROCESS_TERMINATED later. That would suppress
229 * the event that we are processing. */
230 if (WIFSIGNALED(status)) {
231 event.type = EVENT_EXIT_SIGNAL;
232 event.e_un.signum = WTERMSIG(status);
233 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
234 return &event;
235 }
236 if (WIFEXITED(status)) {
237 event.type = EVENT_EXIT;
238 event.e_un.ret_val = WEXITSTATUS(status);
239 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
240 return &event;
241 }
242
Petr Machata6901b7e2011-07-09 11:00:33 +0200243 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
244 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200245 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200246 if (errno != 0)
247 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100248 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200249
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100251 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200252 event.type = EVENT_SYSCALL;
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: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100255 return &event;
256 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200257 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100258 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200259 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100260 return &event;
261 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200262 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100263 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200264 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100265 return &event;
266 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200267 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100268 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200269 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100270 return &event;
271 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200272 CHECK_PROCESS_TERMINATED;
273 if (errno != 0)
274 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100275 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200276 if (WIFSTOPPED(status)) {
277 int what = status >> 16;
278 if (what == PTRACE_EVENT_VFORK
279 || what == PTRACE_EVENT_FORK
280 || what == PTRACE_EVENT_CLONE) {
281 unsigned long data;
282 event.type = what == PTRACE_EVENT_VFORK
283 ? EVENT_VFORK : EVENT_CLONE;
284 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
285 event.e_un.newpid = data;
286 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
287 pid, (int)data);
288 return &event;
289 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200290 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200291 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200292 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200293 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200294 return &event;
295 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100296 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200297 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200298 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200299 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300 return &event;
301 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200302
303 stop_signal = WSTOPSIG(status);
304
305 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200306 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
307 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200308
Petr Machata6901b7e2011-07-09 11:00:33 +0200309 N.B. see comments in GDB's infrun.c for details. I've
310 actually seen this on an Itanium machine on RHEL 5, I don't
311 remember the exact kernel version anymore. ia64-sigill.s
312 in the test suite tests this. Petr Machata 2011-06-08. */
313 void * break_address
314 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
315 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200316 && leader != NULL
317 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200318 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200319
320 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200321 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200322 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200323 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200324 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100325 return &event;
326 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200327
Juan Cespedes427b7812009-07-06 23:05:30 +0200328 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200329 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200330
Petr Machata6901b7e2011-07-09 11:00:33 +0200331 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200332 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200333
Juan Cespedes5e01f651998-03-08 22:31:44 +0100334 return &event;
335}
Petr Machatacd972582012-01-07 03:02:07 +0100336
337static enum ecb_status
338event_for_proc(struct Event *event, void *data)
339{
340 if (event->proc == data)
341 return ecb_deque;
342 else
343 return ecb_cont;
344}
345
346void
347delete_events_for(struct Process *proc)
348{
349 struct Event *event;
350 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
351 free(event);
352}