blob: 51c2caeb5ba35ae440af8e32774fccdd4635e498 [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
Petr Machataba1664b2012-04-28 14:59:05 +020025#include <sys/ptrace.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010026#include <sys/types.h>
27#include <sys/wait.h>
Petr Machataba1664b2012-04-28 14:59:05 +020028#include <assert.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010029#include <errno.h>
30#include <signal.h>
Petr Machataba1664b2012-04-28 14:59:05 +020031#include <stdio.h>
32#include <stdlib.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010033#include <string.h>
Petr Machata97b20842011-11-22 16:50:36 +010034#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010035
Petr Machata64262602012-01-07 03:41:36 +010036#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020037#include "breakpoint.h"
38#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010039#include "events.h"
Petr Machataba1664b2012-04-28 14:59:05 +020040#include "proc.h"
Petr Machataaf766ab2012-11-07 21:59:13 +010041#include "linux-gnu/trace-defs.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010042
Juan Cespedes393f1d02009-05-07 11:13:54 +020043static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010044
Petr Machata69a03e62011-07-09 11:29:12 +020045/* A queue of events that we missed while enabling the
46 * breakpoint in one of tasks. */
47static Event * delayed_events = NULL;
48static Event * end_delayed_events = NULL;
49
Petr Machata2b46cfc2012-02-18 11:17:29 +010050static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +010051first(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +020052{
Petr Machata2b46cfc2012-02-18 11:17:29 +010053 return CBS_STOP;
Petr Machatacebb8842011-07-09 11:14:11 +020054}
55
Petr Machata69a03e62011-07-09 11:29:12 +020056void
57enque_event(Event * event)
58{
59 debug(DEBUG_FUNCTION, "%d: queuing event %d for later",
60 event->proc->pid, event->type);
61 Event * ne = malloc(sizeof(*ne));
62 if (ne == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +020063 fprintf(stderr, "event will be missed: %s\n", strerror(errno));
Petr Machata69a03e62011-07-09 11:29:12 +020064 return;
65 }
66
67 *ne = *event;
68 ne->next = NULL;
69 if (end_delayed_events == NULL) {
70 assert(delayed_events == NULL);
71 end_delayed_events = delayed_events = ne;
72 }
73 else {
74 assert(delayed_events != NULL);
75 end_delayed_events = end_delayed_events->next = ne;
76 }
77}
78
79Event *
80each_qd_event(enum ecb_status (*pred)(Event *, void *), void * data)
81{
82 Event * prev = delayed_events;
83 Event * event;
84 for (event = prev; event != NULL; ) {
85 switch ((*pred)(event, data)) {
Petr Machata71f25e22012-12-17 04:01:23 +010086 case ECB_CONT:
Petr Machata69a03e62011-07-09 11:29:12 +020087 prev = event;
88 event = event->next;
89 continue;
90
Petr Machata71f25e22012-12-17 04:01:23 +010091 case ECB_DEQUE:
Petr Machata69a03e62011-07-09 11:29:12 +020092 debug(DEBUG_FUNCTION, "dequeuing event %d for %d",
93 event->type,
94 event->proc != NULL ? event->proc->pid : -1);
95 /*
96 printf("dequeuing event %d for %d\n", event->type,
97 event->proc != NULL ? event->proc->pid : -1) ;
98 */
99 if (end_delayed_events == event)
100 end_delayed_events = prev;
101 if (delayed_events == event)
102 delayed_events = event->next;
103 else
104 prev->next = event->next;
105 if (delayed_events == NULL)
106 end_delayed_events = NULL;
107 /* fall-through */
108
Petr Machata71f25e22012-12-17 04:01:23 +0100109 case ECB_YIELD:
Petr Machata69a03e62011-07-09 11:29:12 +0200110 return event;
111 }
112 }
113
114 return NULL;
115}
116
117static enum ecb_status
118event_process_not_reenabling(Event * event, void * data)
119{
Petr Machata4007d742011-07-09 11:29:42 +0200120 if (event->proc == NULL
121 || event->proc->leader == NULL
122 || event->proc->leader->event_handler == NULL)
Petr Machata71f25e22012-12-17 04:01:23 +0100123 return ECB_DEQUE;
Petr Machata4007d742011-07-09 11:29:42 +0200124 else
Petr Machata71f25e22012-12-17 04:01:23 +0100125 return ECB_CONT;
Petr Machata69a03e62011-07-09 11:29:12 +0200126}
127
128static Event *
129next_qd_event(void)
130{
131 return each_qd_event(&event_process_not_reenabling, NULL);
132}
133
Petr Machataffe4cd22012-04-11 18:01:44 +0200134int linux_in_waitpid = 0;
135
Juan Cespedes393f1d02009-05-07 11:13:54 +0200136Event *
Petr Machata26627682011-07-08 18:15:32 +0200137next_event(void)
138{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139 pid_t pid;
140 int status;
141 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +0200142 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143
Juan Cespedescd8976d2009-05-14 13:47:58 +0200144 debug(DEBUG_FUNCTION, "next_event()");
Petr Machata69a03e62011-07-09 11:29:12 +0200145 Event * ev;
146 if ((ev = next_qd_event()) != NULL) {
147 event = *ev;
148 free(ev);
149 return &event;
150 }
151
Petr Machatacebb8842011-07-09 11:14:11 +0200152 if (!each_process(NULL, &first, NULL)) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200153 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes28f60191998-04-12 00:04:39 +0200154 exit(0);
155 }
Petr Machataffe4cd22012-04-11 18:01:44 +0200156
157 linux_in_waitpid = 1;
Juan Cespedes6be80282009-05-28 19:06:00 +0200158 pid = waitpid(-1, &status, __WALL);
Petr Machataffe4cd22012-04-11 18:01:44 +0200159 linux_in_waitpid = 0;
160
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100161 if (pid == -1) {
162 if (errno == ECHILD) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200163 debug(DEBUG_EVENT, "event: No more traced programs: exiting");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100164 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165 } else if (errno == EINTR) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200166 debug(DEBUG_EVENT, "event: none (wait received EINTR?)");
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200167 event.type = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +0200168 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 }
170 perror("wait");
171 exit(1);
172 }
173 event.proc = pid2proc(pid);
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200174 if (!event.proc || event.proc->state == STATE_BEING_CREATED) {
Petr Machata97b20842011-11-22 16:50:36 +0100175 /* Work around (presumably) a bug on some kernels,
176 * where we are seeing a waitpid event even though the
177 * process is still reported to be running. Wait for
178 * the tracing stop to propagate. But don't get stuck
179 * here forever.
180 *
181 * We need the process in T, because there's a lot of
182 * ptracing going on all over the place, and these
183 * calls fail when the process is not in T.
184 *
185 * N.B. This was observed on RHEL 5 Itanium, but I'm
186 * turning this on globally, to save some poor soul
187 * down the road (which could well be me a year from
188 * now) the pain of figuring this out all over again.
189 * Petr Machata 2011-11-22. */
190 int i = 0;
Petr Machata6dfc5442012-12-17 03:38:36 +0100191 for (; i < 100 && process_status(pid) != PS_TRACING_STOP; ++i) {
Petr Machata97b20842011-11-22 16:50:36 +0100192 debug(2, "waiting for %d to stop", pid);
193 usleep(10000);
194 }
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200195 event.type = EVENT_NEW;
Juan Cespedes2721e6a2009-05-21 15:15:40 +0200196 event.e_un.newpid = pid;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200197 debug(DEBUG_EVENT, "event: NEW: pid=%d", pid);
Juan Cespedesa8909f72009-04-28 20:02:41 +0200198 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
Petr Machataba36f0a2013-11-04 19:56:42 +0100200
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200201 get_arch_dep(event.proc);
Juan Cespedes1e583132009-04-07 18:17:11 +0200202 debug(3, "event from pid %u", pid);
Petr Machata929bd572012-12-17 03:20:34 +0100203 struct process *leader = event.proc->leader;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800204
Petr Machataf2d2ba52011-07-09 11:03:26 +0200205 /* The process should be stopped after the waitpid call. But
206 * when the whole thread group is terminated, we see
207 * individual tasks spontaneously transitioning from 't' to
208 * 'R' and 'Z'. Calls to ptrace fail and /proc/pid/status may
209 * not even be available anymore, so we can't check in
210 * advance. So we just drop the error checking around ptrace
211 * calls. We check for termination ex post when it fails,
212 * suppress the event, and let the event loop collect the
213 * termination in the next iteration. */
214#define CHECK_PROCESS_TERMINATED \
215 do { \
216 int errno_save = errno; \
217 switch (process_stopped(pid)) \
218 case 0: \
219 case -1: { \
220 debug(DEBUG_EVENT, \
221 "process not stopped, is it terminating?"); \
222 event.type = EVENT_NONE; \
223 continue_process(event.proc->pid); \
224 return &event; \
225 } \
226 errno = errno_save; \
227 } while (0)
228
Petr Machata9a5420c2011-07-09 11:21:23 +0200229 event.proc->instruction_pointer = (void *)(uintptr_t)-1;
Petr Machata6901b7e2011-07-09 11:00:33 +0200230
Petr Machataf2d2ba52011-07-09 11:03:26 +0200231 /* Check for task termination now, before we have a need to
232 * call CHECK_PROCESS_TERMINATED later. That would suppress
233 * the event that we are processing. */
234 if (WIFSIGNALED(status)) {
235 event.type = EVENT_EXIT_SIGNAL;
236 event.e_un.signum = WTERMSIG(status);
237 debug(DEBUG_EVENT, "event: EXIT_SIGNAL: pid=%d, signum=%d", pid, event.e_un.signum);
238 return &event;
239 }
240 if (WIFEXITED(status)) {
241 event.type = EVENT_EXIT;
242 event.e_un.ret_val = WEXITSTATUS(status);
243 debug(DEBUG_EVENT, "event: EXIT: pid=%d, status=%d", pid, event.e_un.ret_val);
244 return &event;
245 }
246
Petr Machata6901b7e2011-07-09 11:00:33 +0200247 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
248 if (event.proc->instruction_pointer == (void *)(uintptr_t)-1) {
Petr Machataf2d2ba52011-07-09 11:03:26 +0200249 CHECK_PROCESS_TERMINATED;
Petr Machata6901b7e2011-07-09 11:00:33 +0200250 if (errno != 0)
251 perror("get_instruction_pointer");
Juan Cespedesf0fdae91998-03-11 00:03:00 +0100252 }
Petr Machata6901b7e2011-07-09 11:00:33 +0200253
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +0100255 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200256 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100257 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200258 debug(DEBUG_EVENT, "event: SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100259 return &event;
260 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200261 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100262 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200263 debug(DEBUG_EVENT, "event: SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100264 return &event;
265 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200266 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +0100267 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200268 debug(DEBUG_EVENT, "event: ARCH_SYSCALL: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100269 return &event;
270 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200271 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +0100272 event.e_un.sysnum = tmp;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200273 debug(DEBUG_EVENT, "event: ARCH_SYSRET: pid=%d, sysnum=%d", pid, tmp);
Juan Cespedes63184be2008-12-10 13:30:12 +0100274 return &event;
275 case -1:
Petr Machataf2d2ba52011-07-09 11:03:26 +0200276 CHECK_PROCESS_TERMINATED;
277 if (errno != 0)
278 perror("syscall_p");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200280 if (WIFSTOPPED(status)) {
281 int what = status >> 16;
282 if (what == PTRACE_EVENT_VFORK
283 || what == PTRACE_EVENT_FORK
284 || what == PTRACE_EVENT_CLONE) {
285 unsigned long data;
286 event.type = what == PTRACE_EVENT_VFORK
287 ? EVENT_VFORK : EVENT_CLONE;
288 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
289 event.e_un.newpid = data;
290 debug(DEBUG_EVENT, "event: CLONE: pid=%d, newpid=%d",
291 pid, (int)data);
292 return &event;
293 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200294 }
Juan Cespedes1e583132009-04-07 18:17:11 +0200295 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200296 event.type = EVENT_EXEC;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200297 debug(DEBUG_EVENT, "event: EXEC: pid=%d", pid);
Juan Cespedes1e583132009-04-07 18:17:11 +0200298 return &event;
299 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300 if (!WIFSTOPPED(status)) {
Juan Cespedes427b7812009-07-06 23:05:30 +0200301 /* should never happen */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200302 event.type = EVENT_NONE;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200303 debug(DEBUG_EVENT, "event: NONE: pid=%d (wait error?)", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304 return &event;
305 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200306
307 stop_signal = WSTOPSIG(status);
308
309 /* On some targets, breakpoints are signalled not using
Petr Machata6901b7e2011-07-09 11:00:33 +0200310 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. SIGEMT
311 is not defined on Linux, but check for the others.
Petr Machataef46b3e2007-05-09 19:21:42 +0200312
Petr Machata6901b7e2011-07-09 11:00:33 +0200313 N.B. see comments in GDB's infrun.c for details. I've
314 actually seen this on an Itanium machine on RHEL 5, I don't
315 remember the exact kernel version anymore. ia64-sigill.s
316 in the test suite tests this. Petr Machata 2011-06-08. */
Petr Machataba36f0a2013-11-04 19:56:42 +0100317 arch_addr_t break_address
Petr Machata6901b7e2011-07-09 11:00:33 +0200318 = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
319 if ((stop_signal == SIGSEGV || stop_signal == SIGILL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200320 && leader != NULL
321 && address2bpstruct(leader, break_address))
Petr Machataef46b3e2007-05-09 19:21:42 +0200322 stop_signal = SIGTRAP;
Petr Machataef46b3e2007-05-09 19:21:42 +0200323
324 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
Juan Cespedes427b7812009-07-06 23:05:30 +0200325 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200326 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200327 event.e_un.signum = stop_signal;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200328 debug(DEBUG_EVENT, "event: SIGNAL: pid=%d, signum=%d", pid, stop_signal);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100329 return &event;
330 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200331
Juan Cespedes427b7812009-07-06 23:05:30 +0200332 /* last case [by exhaustion] */
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200333 event.type = EVENT_BREAKPOINT;
Juan Cespedes427b7812009-07-06 23:05:30 +0200334
Petr Machata6901b7e2011-07-09 11:00:33 +0200335 event.e_un.brk_addr = break_address;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200336 debug(DEBUG_EVENT, "event: BREAKPOINT: pid=%d, addr=%p", pid, event.e_un.brk_addr);
Petr Machata6901b7e2011-07-09 11:00:33 +0200337
Juan Cespedes5e01f651998-03-08 22:31:44 +0100338 return &event;
339}
Petr Machatacd972582012-01-07 03:02:07 +0100340
341static enum ecb_status
342event_for_proc(struct Event *event, void *data)
343{
344 if (event->proc == data)
Petr Machata71f25e22012-12-17 04:01:23 +0100345 return ECB_DEQUE;
Petr Machatacd972582012-01-07 03:02:07 +0100346 else
Petr Machata71f25e22012-12-17 04:01:23 +0100347 return ECB_CONT;
Petr Machatacd972582012-01-07 03:02:07 +0100348}
349
350void
Petr Machata929bd572012-12-17 03:20:34 +0100351delete_events_for(struct process *proc)
Petr Machatacd972582012-01-07 03:02:07 +0100352{
353 struct Event *event;
354 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
355 free(event);
356}