blob: 929808bdb8cd63a5a4b0ff5f6293dcc1a9f27741 [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) 2010 Joe Damato
5 * Copyright (C) 1998,2002,2003,2004,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
Petr Machatacec06ec2012-04-10 13:31:55 +020024#include "config.h"
25
26#include <asm/unistd.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <assert.h>
30#include <errno.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010031#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010032#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010033#include <string.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010034#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010035
Petr Machatacec06ec2012-04-10 13:31:55 +020036#ifdef HAVE_LIBSELINUX
37# include <selinux/selinux.h>
38#endif
39
Petr Machata92822542012-03-28 00:31:14 +020040#include "linux-gnu/trace.h"
Petr Machata64262602012-01-07 03:41:36 +010041#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020042#include "breakpoint.h"
43#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010044#include "events.h"
Petr Machataba1664b2012-04-28 14:59:05 +020045#include "options.h"
46#include "proc.h"
47#include "ptrace.h"
48#include "type.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020049
50/* If the system headers did not provide the constants, hard-code the normal
51 values. */
52#ifndef PTRACE_EVENT_FORK
53
54#define PTRACE_OLDSETOPTIONS 21
55#define PTRACE_SETOPTIONS 0x4200
56#define PTRACE_GETEVENTMSG 0x4201
57
58/* options set using PTRACE_SETOPTIONS */
59#define PTRACE_O_TRACESYSGOOD 0x00000001
60#define PTRACE_O_TRACEFORK 0x00000002
61#define PTRACE_O_TRACEVFORK 0x00000004
62#define PTRACE_O_TRACECLONE 0x00000008
63#define PTRACE_O_TRACEEXEC 0x00000010
64#define PTRACE_O_TRACEVFORKDONE 0x00000020
65#define PTRACE_O_TRACEEXIT 0x00000040
66
67/* Wait extended result codes for the above trace options. */
68#define PTRACE_EVENT_FORK 1
69#define PTRACE_EVENT_VFORK 2
70#define PTRACE_EVENT_CLONE 3
71#define PTRACE_EVENT_EXEC 4
72#define PTRACE_EVENT_VFORK_DONE 5
73#define PTRACE_EVENT_EXIT 6
74
75#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010076
Juan Cespedesf1350522008-12-16 18:19:58 +010077void
Petr Machatacec06ec2012-04-10 13:31:55 +020078trace_fail_warning(pid_t pid)
79{
80 /* This was adapted from GDB. */
81#ifdef HAVE_LIBSELINUX
82 static int checked = 0;
83 if (checked)
84 return;
85 checked = 1;
86
87 /* -1 is returned for errors, 0 if it has no effect, 1 if
88 * PTRACE_ATTACH is forbidden. */
89 if (security_get_boolean_active("deny_ptrace") == 1)
90 fprintf(stderr,
91"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
92"tracing other processes. You can disable this process attach protection by\n"
93"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
94#endif /* HAVE_LIBSELINUX */
95}
96
97void
98trace_me(void)
99{
Petr Machata26627682011-07-08 18:15:32 +0200100 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +0200101 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100102 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200103 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100104 exit(1);
105 }
106}
107
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100108/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100109 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100110 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100111int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100112wait_for_proc(pid_t pid)
113{
Petr Machatac805c622012-03-02 00:10:37 +0100114 /* man ptrace: PTRACE_ATTACH attaches to the process specified
115 in pid. The child is sent a SIGSTOP, but will not
116 necessarily have stopped by the completion of this call;
117 use wait() to wait for the child to stop. */
118 if (waitpid(pid, NULL, __WALL) != pid) {
119 perror ("trace_pid: waitpid");
120 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100121 }
122
Petr Machatac805c622012-03-02 00:10:37 +0100123 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100124}
125
Juan Cespedesf1350522008-12-16 18:19:58 +0100126int
Petr Machatacec06ec2012-04-10 13:31:55 +0200127trace_pid(pid_t pid)
128{
Petr Machata26627682011-07-08 18:15:32 +0200129 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200130 /* This shouldn't emit error messages, as there are legitimate
131 * reasons that the PID can't be attached: like it may have
132 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200133 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100134 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100135
Petr Machatac805c622012-03-02 00:10:37 +0100136 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139void
Petr Machata3ed2a422012-04-06 17:18:55 +0200140trace_set_options(struct Process *proc)
141{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100142 if (proc->tracesysgood & 0x80)
143 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200144
Petr Machata3ed2a422012-04-06 17:18:55 +0200145 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200146 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200147
Juan Cespedes1e583132009-04-07 18:17:11 +0200148 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
149 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
150 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200151 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
152 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100153 perror("PTRACE_SETOPTIONS");
154 return;
155 }
156 proc->tracesysgood |= 0x80;
157}
158
Juan Cespedesf1350522008-12-16 18:19:58 +0100159void
160untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200161 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200162 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100163}
164
Juan Cespedesf1350522008-12-16 18:19:58 +0100165void
Petr Machatac897cb72012-05-05 01:26:58 +0200166continue_after_signal(pid_t pid, int signum)
167{
168 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
169 pid, signum);
170 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200171}
172
173static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200174event_for_pid(Event *event, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200175{
176 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
177 return ecb_yield;
178 return ecb_cont;
179}
180
181static int
182have_events_for(pid_t pid)
183{
184 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
185}
186
187void
188continue_process(pid_t pid)
189{
190 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200191
192 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200193 the queue for this process. Otherwise just wait for the
194 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200195 if (!have_events_for(pid))
196 /* We always trace syscalls to control fork(),
197 * clone(), execve()... */
198 ptrace(PTRACE_SYSCALL, pid, 0, 0);
199 else
200 debug(DEBUG_PROCESS,
201 "putting off the continue, events in que.");
202}
203
Petr Machata98f09922011-07-09 10:55:29 +0200204static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200205get_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200206{
Petr Machata750ca8c2011-10-06 14:29:34 +0200207 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200208 size_t i;
209 for (i = 0; i < pids->count; ++i)
210 if (pids->tasks[i].pid == pid)
211 return &pids->tasks[i];
212
213 return NULL;
214}
215
216static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200217add_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200218{
219 if (pids->count == pids->alloc) {
220 size_t ns = (2 * pids->alloc) ?: 4;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200221 struct pid_task *n = realloc(pids->tasks,
222 sizeof(*pids->tasks) * ns);
Petr Machata98f09922011-07-09 10:55:29 +0200223 if (n == NULL)
224 return NULL;
225 pids->tasks = n;
226 pids->alloc = ns;
227 }
228 struct pid_task * task_info = &pids->tasks[pids->count++];
229 memset(task_info, 0, sizeof(*task_info));
230 task_info->pid = pid;
231 return task_info;
232}
233
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234static enum callback_status
235task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200236{
237 enum process_status st = process_status(task->pid);
238 if (data != NULL)
239 *(enum process_status *)data = st;
240
241 /* If the task is already stopped, don't worry about it.
242 * Likewise if it managed to become a zombie or terminate in
243 * the meantime. This can happen when the whole thread group
244 * is terminating. */
245 switch (st) {
246 case ps_invalid:
247 case ps_tracing_stop:
248 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200250 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200251 case ps_stop:
252 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200254 }
Petr Machata36d19822011-10-21 16:03:45 +0200255
256 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200257}
258
259/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260static enum callback_status
261task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200262{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200263 struct pid_set *pids = data;
264 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +0200265 if (task_info != NULL
266 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100267 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200268
269 return task_stopped(task, NULL);
270}
271
Petr Machata366c2f42012-02-09 19:34:36 +0100272static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200273
Petr Machata2b46cfc2012-02-18 11:17:29 +0100274static enum callback_status
275task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200276{
277 if (task->event_handler != NULL
278 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279 return CBS_STOP;
280 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200281}
282
283static int
Petr Machata74132a42012-03-16 02:46:18 +0100284is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200285{
Petr Machata74132a42012-03-16 02:46:18 +0100286 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200287}
288
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289static enum callback_status
290send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200291{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200292 struct Process *leader = task->leader;
293 struct pid_set *pids = data;
Petr Machata98f09922011-07-09 10:55:29 +0200294
295 /* Look for pre-existing task record, or add new. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200296 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200297 if (task_info == NULL)
298 task_info = add_task_info(pids, task->pid);
299 if (task_info == NULL) {
300 perror("send_sigstop: add_task_info");
301 destroy_event_handler(leader);
302 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100303 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200304 }
305
306 /* This task still has not been attached to. It should be
307 stopped by the kernel. */
308 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200310
311 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200312 * if we sent the SIGSTOP already, which happens when we are
313 * handling "onexit" and inherited the handler from breakpoint
314 * re-enablement. */
315 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100316 if (task_stopped(task, &st) == CBS_CONT)
317 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200318 if (task_info->sigstopped) {
319 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100320 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200321 task_info->delivered = 0;
322 }
323
Petr Machatacbe29c62011-09-27 02:27:58 +0200324 /* Also don't attempt to stop the process if it's a parent of
325 * vforked process. We set up event handler specially to hint
326 * us. In that case parent is in D state, which we use to
327 * weed out unnecessary looping. */
328 if (st == ps_sleeping
329 && is_vfork_parent (task)) {
330 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200332 }
333
Petr Machata98f09922011-07-09 10:55:29 +0200334 if (task_kill(task->pid, SIGSTOP) >= 0) {
335 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
336 task_info->sigstopped = 1;
337 } else
338 fprintf(stderr,
339 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
340
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200342}
343
Petr Machata73894bd2011-08-20 23:47:34 +0200344/* On certain kernels, detaching right after a singlestep causes the
345 tracee to be killed with a SIGTRAP (that even though the singlestep
346 was properly caught by waitpid. The ugly workaround is to put a
347 breakpoint where IP points and let the process continue. After
348 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200349static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200350ugly_workaround(struct Process *proc)
Petr Machata590c8082011-08-20 22:45:26 +0200351{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200352 void *ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100353 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200354 if (sbp != NULL)
355 enable_breakpoint(proc, sbp);
356 else
Petr Machata9df15012012-02-20 12:49:46 +0100357 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200358 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200359}
360
361static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200362process_stopping_done(struct process_stopping_handler *self,
363 struct Process *leader)
Petr Machata98f09922011-07-09 10:55:29 +0200364{
365 debug(DEBUG_PROCESS, "process stopping done %d",
366 self->task_enabling_breakpoint->pid);
Petr Machata8ae36732012-04-30 01:19:09 +0200367
Petr Machata590c8082011-08-20 22:45:26 +0200368 if (!self->exiting) {
Petr Machata8ae36732012-04-30 01:19:09 +0200369 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200370 for (i = 0; i < self->pids.count; ++i)
371 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100372 && (self->pids.tasks[i].delivered
373 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200374 continue_process(self->pids.tasks[i].pid);
375 continue_process(self->task_enabling_breakpoint->pid);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200376 }
377
378 if (self->exiting) {
379 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200380 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200381 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200382 } else {
383 switch ((self->ugly_workaround_p)(self)) {
384 case CBS_FAIL:
385 /* xxx handle me */
386 case CBS_STOP:
387 break;
388 case CBS_CONT:
389 goto ugly_workaround;
390 }
Petr Machata8ae36732012-04-30 01:19:09 +0200391 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200392 }
393}
394
395/* Before we detach, we need to make sure that task's IP is on the
396 * edge of an instruction. So for tasks that have a breakpoint event
397 * in the queue, we adjust the instruction pointer, just like
398 * continue_after_breakpoint does. */
399static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200400undo_breakpoint(Event *event, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200401{
402 if (event != NULL
403 && event->proc->leader == data
404 && event->type == EVENT_BREAKPOINT)
405 set_instruction_pointer(event->proc, event->e_un.brk_addr);
406 return ecb_cont;
407}
408
Petr Machata2b46cfc2012-02-18 11:17:29 +0100409static enum callback_status
410untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200411{
412 if (task != data)
413 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100414 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200415}
416
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417static enum callback_status
418remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200419{
420 /* Don't untrace leader just yet. */
421 if (task != data)
422 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100423 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200424}
425
Petr Machata86d38282012-04-24 18:09:01 +0200426static enum callback_status
427retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
428{
429 breakpoint_on_retract(bp, proc);
430 return CBS_CONT;
431}
432
Petr Machata590c8082011-08-20 22:45:26 +0200433static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200434detach_process(struct Process *leader)
Petr Machata590c8082011-08-20 22:45:26 +0200435{
436 each_qd_event(&undo_breakpoint, leader);
437 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200438 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200439
440 /* Now untrace the process, if it was attached to by -p. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200441 struct opt_p_t *it;
Petr Machata590c8082011-08-20 22:45:26 +0200442 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata6c2e57a2012-10-27 01:50:22 +0200443 struct Process *proc = pid2proc(it->pid);
Petr Machata590c8082011-08-20 22:45:26 +0200444 if (proc == NULL)
445 continue;
446 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100447 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200448 break;
449 }
450 }
Petr Machata74132a42012-03-16 02:46:18 +0100451 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200452 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200453 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200454}
455
456static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200457handle_stopping_event(struct pid_task *task_info, Event **eventp)
Petr Machata98f09922011-07-09 10:55:29 +0200458{
459 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200460 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200461 task_info->got_event = 1;
462
Petr Machata6c2e57a2012-10-27 01:50:22 +0200463 Event *event = *eventp;
Petr Machata98f09922011-07-09 10:55:29 +0200464
465 /* In every state, sink SIGSTOP events for tasks that it was
466 * sent to. */
467 if (task_info != NULL
468 && event->type == EVENT_SIGNAL
469 && event->e_un.signum == SIGSTOP) {
470 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
471 if (task_info->sigstopped
472 && !task_info->delivered) {
473 task_info->delivered = 1;
474 *eventp = NULL; // sink the event
475 } else
476 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
477 "sigstopped=%d and delivered=%d\n",
478 task_info->pid, task_info->sigstopped,
479 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100480 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100481}
482
Petr Machata98f09922011-07-09 10:55:29 +0200483/* Some SIGSTOPs may have not been delivered to their respective tasks
484 * yet. They are still in the queue. If we have seen an event for
485 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200486 * caught by ltrace. We don't mind that the process is after
487 * breakpoint (and therefore potentially doesn't have aligned IP),
488 * because the signal will be delivered without the process actually
489 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200490static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200491continue_for_sigstop_delivery(struct pid_set *pids)
Petr Machata98f09922011-07-09 10:55:29 +0200492{
493 size_t i;
494 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200495 if (pids->tasks[i].pid != 0
496 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200497 && !pids->tasks[i].delivered
498 && pids->tasks[i].got_event) {
499 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
500 pids->tasks[i].pid);
501 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
502 }
503 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100504}
505
Petr Machata98f09922011-07-09 10:55:29 +0200506static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200507event_exit_p(Event *event)
Petr Machata750ca8c2011-10-06 14:29:34 +0200508{
509 return event != NULL && (event->type == EVENT_EXIT
510 || event->type == EVENT_EXIT_SIGNAL);
511}
512
513static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200514event_exit_or_none_p(Event *event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200515{
Petr Machata750ca8c2011-10-06 14:29:34 +0200516 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200517 || event->type == EVENT_NONE;
518}
519
520static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200521await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
522 Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200523{
524 /* If we still didn't get our SIGSTOP, continue the process
525 * and carry on. */
526 if (event != NULL && !event_exit_or_none_p(event)
527 && task_info != NULL && task_info->sigstopped) {
528 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
529 task_info->pid);
530 /* We should get the signal the first thing
531 * after this, so it should be OK to continue
532 * even if we are over a breakpoint. */
533 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
534
535 } else {
536 /* If all SIGSTOPs were delivered, uninstall the
537 * handler and continue everyone. */
538 /* XXX I suspect that we should check tasks that are
539 * still around. Is things are now, there should be a
540 * race between waiting for everyone to stop and one
541 * of the tasks exiting. */
542 int all_clear = 1;
543 size_t i;
544 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200545 if (pids->tasks[i].pid != 0
546 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200547 && !pids->tasks[i].delivered) {
548 all_clear = 0;
549 break;
550 }
551 return all_clear;
552 }
553
554 return 0;
555}
556
Petr Machata590c8082011-08-20 22:45:26 +0200557static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200558all_stops_accountable(struct pid_set *pids)
Petr Machata590c8082011-08-20 22:45:26 +0200559{
560 size_t i;
561 for (i = 0; i < pids->count; ++i)
562 if (pids->tasks[i].pid != 0
563 && !pids->tasks[i].got_event
564 && !have_events_for(pids->tasks[i].pid))
565 return 0;
566 return 1;
567}
568
Petr Machataa266acb2012-04-12 23:50:23 +0200569/* The protocol is: 0 for success, negative for failure, positive if
570 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200571int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200572 int (*add_cb)(void *addr, void *data),
573 void *add_cb_data);
574
575#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
576int
Petr Machata9e1e9692012-04-19 02:29:38 +0200577arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200578 int (*add_cb)(void *addr, void *data),
579 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100580{
Petr Machataa266acb2012-04-12 23:50:23 +0200581 return 1;
582}
583#endif
584
Petr Machata42748ac2012-04-19 04:24:25 +0200585static Event *process_stopping_on_event(struct event_handler *super,
586 Event *event);
587
588static void
589remove_atomic_breakpoints(struct Process *proc)
590{
Petr Machata24a47042012-04-19 18:15:08 +0200591 struct process_stopping_handler *self
592 = (void *)proc->leader->event_handler;
593 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200594 assert(self->super.on_event == process_stopping_on_event);
595
596 int ct = sizeof(self->atomic_skip_bp_addrs)
597 / sizeof(*self->atomic_skip_bp_addrs);
598 int i;
599 for (i = 0; i < ct; ++i)
600 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200601 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200602 self->atomic_skip_bp_addrs[i] = 0;
603 }
604}
605
606static void
607atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
608{
609 remove_atomic_breakpoints(proc);
610}
611
Petr Machataa266acb2012-04-12 23:50:23 +0200612static int
613atomic_singlestep_add_bp(void *addr, void *data)
614{
615 struct process_stopping_handler *self = data;
616 struct Process *proc = self->task_enabling_breakpoint;
617
Petr Machata42748ac2012-04-19 04:24:25 +0200618 int ct = sizeof(self->atomic_skip_bp_addrs)
619 / sizeof(*self->atomic_skip_bp_addrs);
620 int i;
621 for (i = 0; i < ct; ++i)
622 if (self->atomic_skip_bp_addrs[i] == 0) {
623 self->atomic_skip_bp_addrs[i] = addr;
624 static struct bp_callbacks cbs = {
625 .on_hit = atomic_singlestep_bp_on_hit,
626 };
627 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200628 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200629 breakpoint_set_callbacks(bp, &cbs);
630 return 0;
631 }
Petr Machataa266acb2012-04-12 23:50:23 +0200632
Petr Machata42748ac2012-04-19 04:24:25 +0200633 assert(!"Too many atomic singlestep breakpoints!");
634 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200635}
636
637static int
638singlestep(struct process_stopping_handler *self)
639{
640 struct Process *proc = self->task_enabling_breakpoint;
641
642 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
643 self->breakpoint_being_enabled,
644 &atomic_singlestep_add_bp, self);
645
646 /* Propagate failure and success. */
647 if (status <= 0)
648 return status;
649
650 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100651 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200652 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100653 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200654 return -1;
655 }
656 return 0;
657}
658
659static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200660post_singlestep(struct process_stopping_handler *self,
661 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200662{
663 continue_for_sigstop_delivery(&self->pids);
664
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200665 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200666 *eventp = NULL; // handled
667
Petr Machata42748ac2012-04-19 04:24:25 +0200668 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200669
Petr Machata42748ac2012-04-19 04:24:25 +0200670 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200671 self->breakpoint_being_enabled = NULL;
672}
673
674static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200675singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200676{
677 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200678 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200679 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataba1664b2012-04-28 14:59:05 +0200680 teb->pid, breakpoint_name(sbp), sbp->addr,
681 get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200682 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100683}
684
Petr Machata9e1e9692012-04-19 02:29:38 +0200685static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200686pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200687{
688 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200689 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200690 ptrace(PTRACE_CONT, teb->pid, 0, 0);
691}
692
693static void
694pt_singlestep(struct process_stopping_handler *self)
695{
696 if (singlestep(self) < 0)
697 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200698}
699
700static void
701disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200702 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200703{
704 struct Process *teb = self->task_enabling_breakpoint;
705 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200706 if (self->breakpoint_being_enabled->enabled)
707 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200708 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200709 self->state = psh_singlestep;
710}
711
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200712void
713linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
714{
Petr Machata9e1e9692012-04-19 02:29:38 +0200715 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200716}
717
718void
719linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
720{
721 disable_and(self, &pt_continue);
722}
723
Petr Machata98f09922011-07-09 10:55:29 +0200724/* This event handler is installed when we are in the process of
725 * stopping the whole thread group to do the pointer re-enablement for
726 * one of the threads. We pump all events to the queue for later
727 * processing while we wait for all the threads to stop. When this
728 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
729 * re-enable, and continue everyone. */
730static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100731process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200732{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200733 struct process_stopping_handler *self = (void *)super;
734 struct Process *task = event->proc;
735 struct Process *leader = task->leader;
736 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200737
738 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200739 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200740 task->pid, event->type, self->state);
741
Petr Machata6c2e57a2012-10-27 01:50:22 +0200742 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200743 if (task_info == NULL)
744 fprintf(stderr, "new task??? %d\n", task->pid);
745 handle_stopping_event(task_info, &event);
746
747 int state = self->state;
748 int event_to_queue = !event_exit_or_none_p(event);
749
Petr Machata18c97072011-10-06 14:30:11 +0200750 /* Deactivate the entry if the task exits. */
751 if (event_exit_p(event) && task_info != NULL)
752 task_info->pid = 0;
753
Petr Machata43d2fe52011-11-02 13:25:49 +0100754 /* Always handle sysrets. Whether sysret occurred and what
755 * sys it rets from may need to be determined based on process
756 * stack, so we need to keep that in sync with reality. Note
757 * that we don't continue the process after the sysret is
758 * handled. See continue_after_syscall. */
759 if (event != NULL && event->type == EVENT_SYSRET) {
760 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
761 event_to_queue = 0;
762 task_info->sysret = 1;
763 }
764
Petr Machata98f09922011-07-09 10:55:29 +0200765 switch (state) {
766 case psh_stopping:
767 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100768 if (each_task(leader, NULL, &task_blocked,
769 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200770 (self->on_all_stopped)(self);
771 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200772 }
773 break;
774
Petr Machata06986d52011-11-02 13:22:46 +0100775 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200776 /* In singlestep state, breakpoint signifies that we
777 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200778 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200779
Petr Machata42748ac2012-04-19 04:24:25 +0200780 /* If this was caused by a real breakpoint, as
781 * opposed to a singlestep, assume that it's
782 * an artificial breakpoint installed for some
783 * reason for the re-enablement. In that case
784 * handle it. */
785 if (event->type == EVENT_BREAKPOINT) {
Petr Machatabac2da52012-05-29 00:42:59 +0200786 arch_addr_t ip
Petr Machata42748ac2012-04-19 04:24:25 +0200787 = get_instruction_pointer(task);
788 struct breakpoint *other
789 = address2bpstruct(leader, ip);
790 if (other != NULL)
791 breakpoint_on_hit(other, task);
792 }
793
Petr Machata36f40e72012-03-28 02:07:19 +0200794 /* If we got SIGNAL instead of BREAKPOINT,
795 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200796 if (event->type == EVENT_SIGNAL) {
797 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200798 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200799 singlestep_error(self);
800 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200801 goto psh_sinking;
802 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200803 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200804 } else {
805 switch ((self->keep_stepping_p)(self)) {
806 case CBS_FAIL:
807 /* XXX handle me */
808 case CBS_STOP:
809 break;
810 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200811 /* Sink singlestep event. */
812 if (event->type == EVENT_BREAKPOINT)
813 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200814 goto do_singlestep;
815 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200816 }
817
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200818 /* Re-enable the breakpoint that we are
819 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200820 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200821 if (sbp->enabled)
822 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200823
Petr Machataa266acb2012-04-12 23:50:23 +0200824 post_singlestep(self, &event);
825 goto psh_sinking;
826 }
827 break;
Petr Machata98f09922011-07-09 10:55:29 +0200828
Petr Machataa266acb2012-04-12 23:50:23 +0200829 psh_sinking:
830 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200831 case psh_sinking:
832 if (await_sigstop_delivery(&self->pids, task_info, event))
833 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200834 break;
835
836 case psh_ugly_workaround:
837 if (event == NULL)
838 break;
839 if (event->type == EVENT_BREAKPOINT) {
840 undo_breakpoint(event, leader);
841 if (task == teb)
842 self->task_enabling_breakpoint = NULL;
843 }
844 if (self->task_enabling_breakpoint == NULL
845 && all_stops_accountable(&self->pids)) {
846 undo_breakpoint(event, leader);
847 detach_process(leader);
848 event = NULL; // handled
849 }
Petr Machata98f09922011-07-09 10:55:29 +0200850 }
851
852 if (event != NULL && event_to_queue) {
853 enque_event(event);
854 event = NULL; // sink the event
855 }
856
857 return event;
858}
859
860static void
Petr Machata366c2f42012-02-09 19:34:36 +0100861process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200862{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200863 struct process_stopping_handler *self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200864 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100865}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100866
Petr Machata36f40e72012-03-28 02:07:19 +0200867static enum callback_status
868no(struct process_stopping_handler *self)
869{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200870 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200871}
872
Petr Machatac1aa9582012-03-27 23:54:01 +0200873int
874process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200875 void (*as)(struct process_stopping_handler *),
876 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200877 (struct process_stopping_handler *),
878 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200879 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200880{
Petr Machata9ace5742012-04-14 02:29:57 +0200881 debug(DEBUG_FUNCTION,
882 "process_install_stopping_handler: pid=%d", proc->pid);
883
Petr Machatac1aa9582012-03-27 23:54:01 +0200884 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
885 if (handler == NULL)
886 return -1;
887
Petr Machata36f40e72012-03-28 02:07:19 +0200888 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200889 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200890 if (ks == NULL)
891 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200892 if (uw == NULL)
893 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200894
Petr Machatac1aa9582012-03-27 23:54:01 +0200895 handler->super.on_event = process_stopping_on_event;
896 handler->super.destroy = process_stopping_destroy;
897 handler->task_enabling_breakpoint = proc;
898 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200899 handler->on_all_stopped = as;
900 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200901 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200902
903 install_event_handler(proc->leader, &handler->super);
904
905 if (each_task(proc->leader, NULL, &send_sigstop,
906 &handler->pids) != NULL) {
907 destroy_event_handler(proc);
908 return -1;
909 }
910
911 /* And deliver the first fake event, in case all the
912 * conditions are already fulfilled. */
913 Event ev = {
914 .type = EVENT_NONE,
915 .proc = proc,
916 };
917 process_stopping_on_event(&handler->super, &ev);
918
919 return 0;
920}
921
Juan Cespedesf1350522008-12-16 18:19:58 +0100922void
Petr Machatabc373262012-02-07 23:31:15 +0100923continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200924{
Petr Machata9ace5742012-04-14 02:29:57 +0200925 debug(DEBUG_PROCESS,
926 "continue_after_breakpoint: pid=%d, addr=%p",
927 proc->pid, sbp->addr);
928
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200929 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200930
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100931 if (sbp->enabled == 0) {
932 continue_process(proc->pid);
933 } else {
Edgar E. Iglesiasaddae052012-09-27 17:02:39 +0200934#if defined __sparc__ || defined __ia64___
Ian Wienand9a2ad352006-02-20 22:44:45 +0100935 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200936 continue_process(proc->pid);
937#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200938 if (process_install_stopping_handler
939 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200940 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200941 /* Carry on not bothering to re-enable. */
942 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200943 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200944#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100945 }
946}
947
Petr Machata602330f2011-07-09 11:15:34 +0200948/**
949 * Ltrace exit. When we are about to exit, we have to go through all
950 * the processes, stop them all, remove all the breakpoints, and then
951 * detach the processes that we attached to using -p. If we left the
952 * other tasks running, they might hit stray return breakpoints and
953 * produce artifacts, so we better stop everyone, even if it's a bit
954 * of extra work.
955 */
956struct ltrace_exiting_handler
957{
Petr Machata366c2f42012-02-09 19:34:36 +0100958 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200959 struct pid_set pids;
960};
961
Petr Machata602330f2011-07-09 11:15:34 +0200962static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100963ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200964{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200965 struct ltrace_exiting_handler *self = (void *)super;
966 struct Process *task = event->proc;
967 struct Process *leader = task->leader;
Petr Machata602330f2011-07-09 11:15:34 +0200968
Petr Machata9ace5742012-04-14 02:29:57 +0200969 debug(DEBUG_PROCESS,
970 "ltrace_exiting_on_event: pid %d; event type %d",
971 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200972
Petr Machata6c2e57a2012-10-27 01:50:22 +0200973 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata602330f2011-07-09 11:15:34 +0200974 handle_stopping_event(task_info, &event);
975
Petr Machata590c8082011-08-20 22:45:26 +0200976 if (event != NULL && event->type == EVENT_BREAKPOINT)
977 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200978
979 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200980 && all_stops_accountable(&self->pids))
981 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200982
983 /* Sink all non-exit events. We are about to exit, so we
984 * don't bother with queuing them. */
985 if (event_exit_or_none_p(event))
986 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200987
Petr Machata13d5df72011-08-19 23:15:15 +0200988 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200989}
990
991static void
Petr Machata366c2f42012-02-09 19:34:36 +0100992ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200993{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200994 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata602330f2011-07-09 11:15:34 +0200995 free(self->pids.tasks);
996}
997
998static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200999ltrace_exiting_install_handler(struct Process *proc)
Petr Machata602330f2011-07-09 11:15:34 +02001000{
1001 /* Only install to leader. */
1002 if (proc->leader != proc)
1003 return 0;
1004
1005 /* Perhaps we are already installed, if the user passed
1006 * several -p options that are tasks of one process. */
1007 if (proc->event_handler != NULL
1008 && proc->event_handler->on_event == &ltrace_exiting_on_event)
1009 return 0;
1010
Petr Machata590c8082011-08-20 22:45:26 +02001011 /* If stopping handler is already present, let it do the
1012 * work. */
1013 if (proc->event_handler != NULL) {
1014 assert(proc->event_handler->on_event
1015 == &process_stopping_on_event);
Petr Machata6c2e57a2012-10-27 01:50:22 +02001016 struct process_stopping_handler *other
Petr Machata590c8082011-08-20 22:45:26 +02001017 = (void *)proc->event_handler;
1018 other->exiting = 1;
1019 return 0;
1020 }
1021
Petr Machata6c2e57a2012-10-27 01:50:22 +02001022 struct ltrace_exiting_handler *handler
Petr Machata602330f2011-07-09 11:15:34 +02001023 = calloc(sizeof(*handler), 1);
1024 if (handler == NULL) {
1025 perror("malloc exiting handler");
1026 fatal:
1027 /* XXXXXXXXXXXXXXXXXXX fixme */
1028 return -1;
1029 }
1030
Petr Machata602330f2011-07-09 11:15:34 +02001031 handler->super.on_event = ltrace_exiting_on_event;
1032 handler->super.destroy = ltrace_exiting_destroy;
1033 install_event_handler(proc->leader, &handler->super);
1034
Petr Machata74132a42012-03-16 02:46:18 +01001035 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001036 &handler->pids) != NULL)
1037 goto fatal;
1038
1039 return 0;
1040}
1041
Petr Machatacbe29c62011-09-27 02:27:58 +02001042/*
1043 * When the traced process vforks, it's suspended until the child
1044 * process calls _exit or exec*. In the meantime, the two share the
1045 * address space.
1046 *
1047 * The child process should only ever call _exit or exec*, but we
1048 * can't count on that (it's not the role of ltrace to policy, but to
1049 * observe). In any case, we will _at least_ have to deal with
1050 * removal of vfork return breakpoint (which we have to smuggle back
1051 * in, so that the parent can see it, too), and introduction of exec*
1052 * return breakpoint. Since we already have both breakpoint actions
1053 * to deal with, we might as well support it all.
1054 *
1055 * The gist is that we pretend that the child is in a thread group
1056 * with its parent, and handle it as a multi-threaded case, with the
1057 * exception that we know that the parent is blocked, and don't
1058 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001059 */
1060
Petr Machata134a1082011-09-27 20:25:58 +02001061struct process_vfork_handler
1062{
Petr Machata366c2f42012-02-09 19:34:36 +01001063 struct event_handler super;
Petr Machata6c2e57a2012-10-27 01:50:22 +02001064 void *bp_addr;
Petr Machata134a1082011-09-27 20:25:58 +02001065};
1066
Petr Machatacbe29c62011-09-27 02:27:58 +02001067static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001068process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001069{
Petr Machata9ace5742012-04-14 02:29:57 +02001070 debug(DEBUG_PROCESS,
1071 "process_vfork_on_event: pid %d; event type %d",
1072 event->proc->pid, event->type);
1073
Petr Machata6c2e57a2012-10-27 01:50:22 +02001074 struct process_vfork_handler *self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001075 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001076 assert(self != NULL);
1077
1078 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001079 case EVENT_BREAKPOINT:
1080 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001081 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001082 self->bp_addr = event->e_un.brk_addr;
1083 break;
1084
Petr Machatacbe29c62011-09-27 02:27:58 +02001085 case EVENT_EXIT:
1086 case EVENT_EXIT_SIGNAL:
1087 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001088 /* Smuggle back in the vfork return breakpoint, so
1089 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001090 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001091 sbp = dict_find_entry(event->proc->leader->breakpoints,
1092 self->bp_addr);
1093 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001094 assert(sbp->libsym == NULL);
1095 /* We don't mind failing that, it's not a big
1096 * deal to not display one extra vfork return. */
1097 insert_breakpoint(event->proc->parent,
1098 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001099 }
1100
Petr Machataba9911f2011-09-27 21:09:47 +02001101 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001102
1103 /* Remove the leader that we artificially set up
1104 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001105 change_process_leader(event->proc, event->proc);
1106 destroy_event_handler(event->proc);
1107
Petr Machatacbe29c62011-09-27 02:27:58 +02001108 default:
1109 ;
1110 }
1111
1112 return event;
1113}
1114
1115void
Petr Machata6c2e57a2012-10-27 01:50:22 +02001116continue_after_vfork(struct Process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +02001117{
1118 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata6c2e57a2012-10-27 01:50:22 +02001119 struct process_vfork_handler *handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001120 if (handler == NULL) {
1121 perror("malloc vfork handler");
1122 /* Carry on not bothering to treat the process as
1123 * necessary. */
1124 continue_process(proc->parent->pid);
1125 return;
1126 }
1127
1128 /* We must set up custom event handler, so that we see
1129 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001130 handler->super.on_event = process_vfork_on_event;
1131 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001132
1133 /* Make sure that the child is sole thread. */
1134 assert(proc->leader == proc);
1135 assert(proc->next == NULL || proc->next->leader != proc);
1136
1137 /* Make sure that the child's parent is properly set up. */
1138 assert(proc->parent != NULL);
1139 assert(proc->parent->leader != NULL);
1140
1141 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001142}
1143
Petr Machata9d29b3e2011-11-09 16:46:56 +01001144static int
1145is_mid_stopping(Process *proc)
1146{
1147 return proc != NULL
1148 && proc->event_handler != NULL
1149 && proc->event_handler->on_event == &process_stopping_on_event;
1150}
1151
Petr Machata43d2fe52011-11-02 13:25:49 +01001152void
Petr Machata6c2e57a2012-10-27 01:50:22 +02001153continue_after_syscall(struct Process *proc, int sysnum, int ret_p)
Petr Machata43d2fe52011-11-02 13:25:49 +01001154{
1155 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001156 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1157 debug(DEBUG_PROCESS,
1158 "continue_after_syscall: don't continue %d",
1159 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001160 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001161 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001162 continue_process(proc->pid);
1163}
1164
Petr Machata602330f2011-07-09 11:15:34 +02001165/* If ltrace gets SIGINT, the processes directly or indirectly run by
1166 * ltrace get it too. We just have to wait long enough for the signal
1167 * to be delivered and the process terminated, which we notice and
1168 * exit ltrace, too. So there's not much we need to do there. We
1169 * want to keep tracing those processes as usual, in case they just
1170 * SIG_IGN the SIGINT to do their shutdown etc.
1171 *
1172 * For processes ran on the background, we want to install an exit
1173 * handler that stops all the threads, removes all breakpoints, and
1174 * detaches.
1175 */
1176void
Petr Machataffe4cd22012-04-11 18:01:44 +02001177os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001178{
Petr Machata6c2e57a2012-10-27 01:50:22 +02001179 struct opt_p_t *it;
Petr Machata602330f2011-07-09 11:15:34 +02001180 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata6c2e57a2012-10-27 01:50:22 +02001181 struct Process *proc = pid2proc(it->pid);
Petr Machata602330f2011-07-09 11:15:34 +02001182 if (proc == NULL || proc->leader == NULL)
1183 continue;
1184 if (ltrace_exiting_install_handler(proc->leader) < 0)
1185 fprintf(stderr,
1186 "Couldn't install exiting handler for %d.\n",
1187 proc->pid);
1188 }
1189}
1190
Petr Machataffe4cd22012-04-11 18:01:44 +02001191int
1192os_ltrace_exiting_sighandler(void)
1193{
1194 extern int linux_in_waitpid;
1195 if (linux_in_waitpid) {
1196 os_ltrace_exiting();
1197 return 1;
1198 }
1199 return 0;
1200}
1201
Joe Damatodfa3fa32010-11-08 15:47:35 -08001202size_t
1203umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1204
1205 union {
1206 long a;
1207 char c[sizeof(long)];
1208 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001209 int started = 0;
1210 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001211
1212 while (offset < len) {
1213 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1214 if (a.a == -1 && errno) {
1215 if (started && errno == EIO)
1216 return bytes_read;
1217 else
1218 return -1;
1219 }
1220 started = 1;
1221
1222 if (len - offset >= sizeof(long)) {
1223 memcpy(laddr + offset, &a.c[0], sizeof(long));
1224 bytes_read += sizeof(long);
1225 }
1226 else {
1227 memcpy(laddr + offset, &a.c[0], len - offset);
1228 bytes_read += (len - offset);
1229 }
1230 offset += sizeof(long);
1231 }
1232
1233 return bytes_read;
1234}