blob: 5924df1134e8b863dcca9e07257099511c3de5d1 [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
40#include "ptrace.h"
Petr Machata9294d822012-02-07 12:35:58 +010041#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010042#include "proc.h"
Petr Machata92822542012-03-28 00:31:14 +020043#include "linux-gnu/trace.h"
Petr Machata64262602012-01-07 03:41:36 +010044#include "backend.h"
Petr Machata000e3112012-01-03 17:03:39 +010045#include "type.h"
Petr Machatacd972582012-01-07 03:02:07 +010046#include "events.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020047
48/* If the system headers did not provide the constants, hard-code the normal
49 values. */
50#ifndef PTRACE_EVENT_FORK
51
52#define PTRACE_OLDSETOPTIONS 21
53#define PTRACE_SETOPTIONS 0x4200
54#define PTRACE_GETEVENTMSG 0x4201
55
56/* options set using PTRACE_SETOPTIONS */
57#define PTRACE_O_TRACESYSGOOD 0x00000001
58#define PTRACE_O_TRACEFORK 0x00000002
59#define PTRACE_O_TRACEVFORK 0x00000004
60#define PTRACE_O_TRACECLONE 0x00000008
61#define PTRACE_O_TRACEEXEC 0x00000010
62#define PTRACE_O_TRACEVFORKDONE 0x00000020
63#define PTRACE_O_TRACEEXIT 0x00000040
64
65/* Wait extended result codes for the above trace options. */
66#define PTRACE_EVENT_FORK 1
67#define PTRACE_EVENT_VFORK 2
68#define PTRACE_EVENT_CLONE 3
69#define PTRACE_EVENT_EXEC 4
70#define PTRACE_EVENT_VFORK_DONE 5
71#define PTRACE_EVENT_EXIT 6
72
73#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010074
Juan Cespedesf1350522008-12-16 18:19:58 +010075void
Petr Machatacec06ec2012-04-10 13:31:55 +020076trace_fail_warning(pid_t pid)
77{
78 /* This was adapted from GDB. */
79#ifdef HAVE_LIBSELINUX
80 static int checked = 0;
81 if (checked)
82 return;
83 checked = 1;
84
85 /* -1 is returned for errors, 0 if it has no effect, 1 if
86 * PTRACE_ATTACH is forbidden. */
87 if (security_get_boolean_active("deny_ptrace") == 1)
88 fprintf(stderr,
89"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
90"tracing other processes. You can disable this process attach protection by\n"
91"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
92#endif /* HAVE_LIBSELINUX */
93}
94
95void
96trace_me(void)
97{
Petr Machata26627682011-07-08 18:15:32 +020098 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020099 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200101 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100102 exit(1);
103 }
104}
105
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100106/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100107 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100108 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100109int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100110wait_for_proc(pid_t pid)
111{
Petr Machatac805c622012-03-02 00:10:37 +0100112 /* man ptrace: PTRACE_ATTACH attaches to the process specified
113 in pid. The child is sent a SIGSTOP, but will not
114 necessarily have stopped by the completion of this call;
115 use wait() to wait for the child to stop. */
116 if (waitpid(pid, NULL, __WALL) != pid) {
117 perror ("trace_pid: waitpid");
118 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100119 }
120
Petr Machatac805c622012-03-02 00:10:37 +0100121 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100122}
123
Juan Cespedesf1350522008-12-16 18:19:58 +0100124int
Petr Machatacec06ec2012-04-10 13:31:55 +0200125trace_pid(pid_t pid)
126{
Petr Machata26627682011-07-08 18:15:32 +0200127 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200128 /* This shouldn't emit error messages, as there are legitimate
129 * reasons that the PID can't be attached: like it may have
130 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200131 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100132 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100133
Petr Machatac805c622012-03-02 00:10:37 +0100134 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100135}
136
Juan Cespedesf1350522008-12-16 18:19:58 +0100137void
Petr Machata3ed2a422012-04-06 17:18:55 +0200138trace_set_options(struct Process *proc)
139{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100140 if (proc->tracesysgood & 0x80)
141 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200142
Petr Machata3ed2a422012-04-06 17:18:55 +0200143 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200144 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200145
Juan Cespedes1e583132009-04-07 18:17:11 +0200146 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
147 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
148 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200149 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
150 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100151 perror("PTRACE_SETOPTIONS");
152 return;
153 }
154 proc->tracesysgood |= 0x80;
155}
156
Juan Cespedesf1350522008-12-16 18:19:58 +0100157void
158untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200159 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200160 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100161}
162
Juan Cespedesf1350522008-12-16 18:19:58 +0100163void
Petr Machatac897cb72012-05-05 01:26:58 +0200164continue_after_signal(pid_t pid, int signum)
165{
166 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
167 pid, signum);
168 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200169}
170
171static enum ecb_status
172event_for_pid(Event * event, void * data)
173{
174 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
175 return ecb_yield;
176 return ecb_cont;
177}
178
179static int
180have_events_for(pid_t pid)
181{
182 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
183}
184
185void
186continue_process(pid_t pid)
187{
188 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200189
190 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200191 the queue for this process. Otherwise just wait for the
192 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200193 if (!have_events_for(pid))
194 /* We always trace syscalls to control fork(),
195 * clone(), execve()... */
196 ptrace(PTRACE_SYSCALL, pid, 0, 0);
197 else
198 debug(DEBUG_PROCESS,
199 "putting off the continue, events in que.");
200}
201
Petr Machata98f09922011-07-09 10:55:29 +0200202static struct pid_task *
203get_task_info(struct pid_set * pids, pid_t pid)
204{
Petr Machata750ca8c2011-10-06 14:29:34 +0200205 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200206 size_t i;
207 for (i = 0; i < pids->count; ++i)
208 if (pids->tasks[i].pid == pid)
209 return &pids->tasks[i];
210
211 return NULL;
212}
213
214static struct pid_task *
215add_task_info(struct pid_set * pids, pid_t pid)
216{
217 if (pids->count == pids->alloc) {
218 size_t ns = (2 * pids->alloc) ?: 4;
219 struct pid_task * n = realloc(pids->tasks,
220 sizeof(*pids->tasks) * ns);
221 if (n == NULL)
222 return NULL;
223 pids->tasks = n;
224 pids->alloc = ns;
225 }
226 struct pid_task * task_info = &pids->tasks[pids->count++];
227 memset(task_info, 0, sizeof(*task_info));
228 task_info->pid = pid;
229 return task_info;
230}
231
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232static enum callback_status
233task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200234{
235 enum process_status st = process_status(task->pid);
236 if (data != NULL)
237 *(enum process_status *)data = st;
238
239 /* If the task is already stopped, don't worry about it.
240 * Likewise if it managed to become a zombie or terminate in
241 * the meantime. This can happen when the whole thread group
242 * is terminating. */
243 switch (st) {
244 case ps_invalid:
245 case ps_tracing_stop:
246 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100247 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200248 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200249 case ps_stop:
250 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200252 }
Petr Machata36d19822011-10-21 16:03:45 +0200253
254 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200255}
256
257/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100258static enum callback_status
259task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200260{
261 struct pid_set * pids = data;
262 struct pid_task * task_info = get_task_info(pids, task->pid);
263 if (task_info != NULL
264 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200266
267 return task_stopped(task, NULL);
268}
269
Petr Machata366c2f42012-02-09 19:34:36 +0100270static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200271
Petr Machata2b46cfc2012-02-18 11:17:29 +0100272static enum callback_status
273task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200274{
275 if (task->event_handler != NULL
276 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 return CBS_STOP;
278 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200279}
280
281static int
Petr Machata74132a42012-03-16 02:46:18 +0100282is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200283{
Petr Machata74132a42012-03-16 02:46:18 +0100284 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200285}
286
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287static enum callback_status
288send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200289{
290 Process * leader = task->leader;
291 struct pid_set * pids = data;
292
293 /* Look for pre-existing task record, or add new. */
294 struct pid_task * task_info = get_task_info(pids, task->pid);
295 if (task_info == NULL)
296 task_info = add_task_info(pids, task->pid);
297 if (task_info == NULL) {
298 perror("send_sigstop: add_task_info");
299 destroy_event_handler(leader);
300 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100301 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200302 }
303
304 /* This task still has not been attached to. It should be
305 stopped by the kernel. */
306 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100307 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200308
309 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200310 * if we sent the SIGSTOP already, which happens when we are
311 * handling "onexit" and inherited the handler from breakpoint
312 * re-enablement. */
313 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100314 if (task_stopped(task, &st) == CBS_CONT)
315 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200316 if (task_info->sigstopped) {
317 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200319 task_info->delivered = 0;
320 }
321
Petr Machatacbe29c62011-09-27 02:27:58 +0200322 /* Also don't attempt to stop the process if it's a parent of
323 * vforked process. We set up event handler specially to hint
324 * us. In that case parent is in D state, which we use to
325 * weed out unnecessary looping. */
326 if (st == ps_sleeping
327 && is_vfork_parent (task)) {
328 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100329 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200330 }
331
Petr Machata98f09922011-07-09 10:55:29 +0200332 if (task_kill(task->pid, SIGSTOP) >= 0) {
333 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
334 task_info->sigstopped = 1;
335 } else
336 fprintf(stderr,
337 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
338
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200340}
341
Petr Machata73894bd2011-08-20 23:47:34 +0200342/* On certain kernels, detaching right after a singlestep causes the
343 tracee to be killed with a SIGTRAP (that even though the singlestep
344 was properly caught by waitpid. The ugly workaround is to put a
345 breakpoint where IP points and let the process continue. After
346 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200347static void
Petr Machata73894bd2011-08-20 23:47:34 +0200348ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200349{
350 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100351 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200352 if (sbp != NULL)
353 enable_breakpoint(proc, sbp);
354 else
Petr Machata9df15012012-02-20 12:49:46 +0100355 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200356 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200357}
358
359static void
Petr Machata98f09922011-07-09 10:55:29 +0200360process_stopping_done(struct process_stopping_handler * self, Process * leader)
361{
362 debug(DEBUG_PROCESS, "process stopping done %d",
363 self->task_enabling_breakpoint->pid);
364 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200365 if (!self->exiting) {
366 for (i = 0; i < self->pids.count; ++i)
367 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100368 && (self->pids.tasks[i].delivered
369 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200370 continue_process(self->pids.tasks[i].pid);
371 continue_process(self->task_enabling_breakpoint->pid);
372 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200373 }
374
375 if (self->exiting) {
376 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200377 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200378 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200379 } else {
380 switch ((self->ugly_workaround_p)(self)) {
381 case CBS_FAIL:
382 /* xxx handle me */
383 case CBS_STOP:
384 break;
385 case CBS_CONT:
386 goto ugly_workaround;
387 }
Petr Machata590c8082011-08-20 22:45:26 +0200388 }
389}
390
391/* Before we detach, we need to make sure that task's IP is on the
392 * edge of an instruction. So for tasks that have a breakpoint event
393 * in the queue, we adjust the instruction pointer, just like
394 * continue_after_breakpoint does. */
395static enum ecb_status
396undo_breakpoint(Event * event, void * data)
397{
398 if (event != NULL
399 && event->proc->leader == data
400 && event->type == EVENT_BREAKPOINT)
401 set_instruction_pointer(event->proc, event->e_un.brk_addr);
402 return ecb_cont;
403}
404
Petr Machata2b46cfc2012-02-18 11:17:29 +0100405static enum callback_status
406untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200407{
408 if (task != data)
409 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100410 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200411}
412
Petr Machata2b46cfc2012-02-18 11:17:29 +0100413static enum callback_status
414remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200415{
416 /* Don't untrace leader just yet. */
417 if (task != data)
418 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100419 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200420}
421
Petr Machata86d38282012-04-24 18:09:01 +0200422static enum callback_status
423retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
424{
425 breakpoint_on_retract(bp, proc);
426 return CBS_CONT;
427}
428
Petr Machata590c8082011-08-20 22:45:26 +0200429static void
430detach_process(Process * leader)
431{
432 each_qd_event(&undo_breakpoint, leader);
433 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200434 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200435
436 /* Now untrace the process, if it was attached to by -p. */
437 struct opt_p_t * it;
438 for (it = opt_p; it != NULL; it = it->next) {
439 Process * proc = pid2proc(it->pid);
440 if (proc == NULL)
441 continue;
442 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100443 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200444 break;
445 }
446 }
Petr Machata74132a42012-03-16 02:46:18 +0100447 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200448 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200449 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200450}
451
452static void
453handle_stopping_event(struct pid_task * task_info, Event ** eventp)
454{
455 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200456 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200457 task_info->got_event = 1;
458
459 Event * event = *eventp;
460
461 /* In every state, sink SIGSTOP events for tasks that it was
462 * sent to. */
463 if (task_info != NULL
464 && event->type == EVENT_SIGNAL
465 && event->e_un.signum == SIGSTOP) {
466 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
467 if (task_info->sigstopped
468 && !task_info->delivered) {
469 task_info->delivered = 1;
470 *eventp = NULL; // sink the event
471 } else
472 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
473 "sigstopped=%d and delivered=%d\n",
474 task_info->pid, task_info->sigstopped,
475 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100476 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100477}
478
Petr Machata98f09922011-07-09 10:55:29 +0200479/* Some SIGSTOPs may have not been delivered to their respective tasks
480 * yet. They are still in the queue. If we have seen an event for
481 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200482 * caught by ltrace. We don't mind that the process is after
483 * breakpoint (and therefore potentially doesn't have aligned IP),
484 * because the signal will be delivered without the process actually
485 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200486static void
487continue_for_sigstop_delivery(struct pid_set * pids)
488{
489 size_t i;
490 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200491 if (pids->tasks[i].pid != 0
492 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200493 && !pids->tasks[i].delivered
494 && pids->tasks[i].got_event) {
495 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
496 pids->tasks[i].pid);
497 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
498 }
499 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100500}
501
Petr Machata98f09922011-07-09 10:55:29 +0200502static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200503event_exit_p(Event * event)
504{
505 return event != NULL && (event->type == EVENT_EXIT
506 || event->type == EVENT_EXIT_SIGNAL);
507}
508
509static int
Petr Machata98f09922011-07-09 10:55:29 +0200510event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200511{
Petr Machata750ca8c2011-10-06 14:29:34 +0200512 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200513 || event->type == EVENT_NONE;
514}
515
516static int
517await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
518 Event * event)
519{
520 /* If we still didn't get our SIGSTOP, continue the process
521 * and carry on. */
522 if (event != NULL && !event_exit_or_none_p(event)
523 && task_info != NULL && task_info->sigstopped) {
524 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
525 task_info->pid);
526 /* We should get the signal the first thing
527 * after this, so it should be OK to continue
528 * even if we are over a breakpoint. */
529 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
530
531 } else {
532 /* If all SIGSTOPs were delivered, uninstall the
533 * handler and continue everyone. */
534 /* XXX I suspect that we should check tasks that are
535 * still around. Is things are now, there should be a
536 * race between waiting for everyone to stop and one
537 * of the tasks exiting. */
538 int all_clear = 1;
539 size_t i;
540 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200541 if (pids->tasks[i].pid != 0
542 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200543 && !pids->tasks[i].delivered) {
544 all_clear = 0;
545 break;
546 }
547 return all_clear;
548 }
549
550 return 0;
551}
552
Petr Machata590c8082011-08-20 22:45:26 +0200553static int
554all_stops_accountable(struct pid_set * pids)
555{
556 size_t i;
557 for (i = 0; i < pids->count; ++i)
558 if (pids->tasks[i].pid != 0
559 && !pids->tasks[i].got_event
560 && !have_events_for(pids->tasks[i].pid))
561 return 0;
562 return 1;
563}
564
Petr Machataa266acb2012-04-12 23:50:23 +0200565/* The protocol is: 0 for success, negative for failure, positive if
566 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200567int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200568 int (*add_cb)(void *addr, void *data),
569 void *add_cb_data);
570
571#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
572int
Petr Machata9e1e9692012-04-19 02:29:38 +0200573arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200574 int (*add_cb)(void *addr, void *data),
575 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100576{
Petr Machataa266acb2012-04-12 23:50:23 +0200577 return 1;
578}
579#endif
580
Petr Machata42748ac2012-04-19 04:24:25 +0200581static Event *process_stopping_on_event(struct event_handler *super,
582 Event *event);
583
584static void
585remove_atomic_breakpoints(struct Process *proc)
586{
Petr Machata24a47042012-04-19 18:15:08 +0200587 struct process_stopping_handler *self
588 = (void *)proc->leader->event_handler;
589 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200590 assert(self->super.on_event == process_stopping_on_event);
591
592 int ct = sizeof(self->atomic_skip_bp_addrs)
593 / sizeof(*self->atomic_skip_bp_addrs);
594 int i;
595 for (i = 0; i < ct; ++i)
596 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200597 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200598 self->atomic_skip_bp_addrs[i] = 0;
599 }
600}
601
602static void
603atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
604{
605 remove_atomic_breakpoints(proc);
606}
607
Petr Machataa266acb2012-04-12 23:50:23 +0200608static int
609atomic_singlestep_add_bp(void *addr, void *data)
610{
611 struct process_stopping_handler *self = data;
612 struct Process *proc = self->task_enabling_breakpoint;
613
Petr Machata42748ac2012-04-19 04:24:25 +0200614 int ct = sizeof(self->atomic_skip_bp_addrs)
615 / sizeof(*self->atomic_skip_bp_addrs);
616 int i;
617 for (i = 0; i < ct; ++i)
618 if (self->atomic_skip_bp_addrs[i] == 0) {
619 self->atomic_skip_bp_addrs[i] = addr;
620 static struct bp_callbacks cbs = {
621 .on_hit = atomic_singlestep_bp_on_hit,
622 };
623 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200624 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200625 breakpoint_set_callbacks(bp, &cbs);
626 return 0;
627 }
Petr Machataa266acb2012-04-12 23:50:23 +0200628
Petr Machata42748ac2012-04-19 04:24:25 +0200629 assert(!"Too many atomic singlestep breakpoints!");
630 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200631}
632
633static int
634singlestep(struct process_stopping_handler *self)
635{
636 struct Process *proc = self->task_enabling_breakpoint;
637
638 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
639 self->breakpoint_being_enabled,
640 &atomic_singlestep_add_bp, self);
641
642 /* Propagate failure and success. */
643 if (status <= 0)
644 return status;
645
646 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100647 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200648 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100649 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200650 return -1;
651 }
652 return 0;
653}
654
655static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200656post_singlestep(struct process_stopping_handler *self,
657 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200658{
659 continue_for_sigstop_delivery(&self->pids);
660
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200661 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200662 *eventp = NULL; // handled
663
Petr Machata42748ac2012-04-19 04:24:25 +0200664 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200665
Petr Machata42748ac2012-04-19 04:24:25 +0200666 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200667 self->breakpoint_being_enabled = NULL;
668}
669
670static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200671singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200672{
673 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200674 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200675 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataa266acb2012-04-12 23:50:23 +0200676 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200677 sbp->addr, get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200678 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100679}
680
Petr Machata9e1e9692012-04-19 02:29:38 +0200681static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200682pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200683{
684 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200685 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200686 ptrace(PTRACE_CONT, teb->pid, 0, 0);
687}
688
689static void
690pt_singlestep(struct process_stopping_handler *self)
691{
692 if (singlestep(self) < 0)
693 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200694}
695
696static void
697disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200698 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200699{
700 struct Process *teb = self->task_enabling_breakpoint;
701 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200702 if (self->breakpoint_being_enabled->enabled)
703 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200704 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200705 self->state = psh_singlestep;
706}
707
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200708void
709linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
710{
Petr Machata9e1e9692012-04-19 02:29:38 +0200711 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200712}
713
714void
715linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
716{
717 disable_and(self, &pt_continue);
718}
719
Petr Machata98f09922011-07-09 10:55:29 +0200720/* This event handler is installed when we are in the process of
721 * stopping the whole thread group to do the pointer re-enablement for
722 * one of the threads. We pump all events to the queue for later
723 * processing while we wait for all the threads to stop. When this
724 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
725 * re-enable, and continue everyone. */
726static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100727process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200728{
729 struct process_stopping_handler * self = (void *)super;
730 Process * task = event->proc;
731 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200732 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200733
734 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200735 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200736 task->pid, event->type, self->state);
737
738 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
739 if (task_info == NULL)
740 fprintf(stderr, "new task??? %d\n", task->pid);
741 handle_stopping_event(task_info, &event);
742
743 int state = self->state;
744 int event_to_queue = !event_exit_or_none_p(event);
745
Petr Machata18c97072011-10-06 14:30:11 +0200746 /* Deactivate the entry if the task exits. */
747 if (event_exit_p(event) && task_info != NULL)
748 task_info->pid = 0;
749
Petr Machata43d2fe52011-11-02 13:25:49 +0100750 /* Always handle sysrets. Whether sysret occurred and what
751 * sys it rets from may need to be determined based on process
752 * stack, so we need to keep that in sync with reality. Note
753 * that we don't continue the process after the sysret is
754 * handled. See continue_after_syscall. */
755 if (event != NULL && event->type == EVENT_SYSRET) {
756 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
757 event_to_queue = 0;
758 task_info->sysret = 1;
759 }
760
Petr Machata98f09922011-07-09 10:55:29 +0200761 switch (state) {
762 case psh_stopping:
763 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100764 if (each_task(leader, NULL, &task_blocked,
765 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200766 (self->on_all_stopped)(self);
767 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200768 }
769 break;
770
Petr Machata06986d52011-11-02 13:22:46 +0100771 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200772 /* In singlestep state, breakpoint signifies that we
773 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200774 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200775
Petr Machata42748ac2012-04-19 04:24:25 +0200776 /* If this was caused by a real breakpoint, as
777 * opposed to a singlestep, assume that it's
778 * an artificial breakpoint installed for some
779 * reason for the re-enablement. In that case
780 * handle it. */
781 if (event->type == EVENT_BREAKPOINT) {
782 target_address_t ip
783 = get_instruction_pointer(task);
784 struct breakpoint *other
785 = address2bpstruct(leader, ip);
786 if (other != NULL)
787 breakpoint_on_hit(other, task);
788 }
789
Petr Machata36f40e72012-03-28 02:07:19 +0200790 /* If we got SIGNAL instead of BREAKPOINT,
791 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200792 if (event->type == EVENT_SIGNAL) {
793 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200794 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200795 singlestep_error(self);
796 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200797 goto psh_sinking;
798 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200799 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200800 } else {
801 switch ((self->keep_stepping_p)(self)) {
802 case CBS_FAIL:
803 /* XXX handle me */
804 case CBS_STOP:
805 break;
806 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200807 /* Sink singlestep event. */
808 if (event->type == EVENT_BREAKPOINT)
809 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200810 goto do_singlestep;
811 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200812 }
813
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200814 /* Re-enable the breakpoint that we are
815 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200816 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200817 if (sbp->enabled)
818 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200819
Petr Machataa266acb2012-04-12 23:50:23 +0200820 post_singlestep(self, &event);
821 goto psh_sinking;
822 }
823 break;
Petr Machata98f09922011-07-09 10:55:29 +0200824
Petr Machataa266acb2012-04-12 23:50:23 +0200825 psh_sinking:
826 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200827 case psh_sinking:
828 if (await_sigstop_delivery(&self->pids, task_info, event))
829 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200830 break;
831
832 case psh_ugly_workaround:
833 if (event == NULL)
834 break;
835 if (event->type == EVENT_BREAKPOINT) {
836 undo_breakpoint(event, leader);
837 if (task == teb)
838 self->task_enabling_breakpoint = NULL;
839 }
840 if (self->task_enabling_breakpoint == NULL
841 && all_stops_accountable(&self->pids)) {
842 undo_breakpoint(event, leader);
843 detach_process(leader);
844 event = NULL; // handled
845 }
Petr Machata98f09922011-07-09 10:55:29 +0200846 }
847
848 if (event != NULL && event_to_queue) {
849 enque_event(event);
850 event = NULL; // sink the event
851 }
852
853 return event;
854}
855
856static void
Petr Machata366c2f42012-02-09 19:34:36 +0100857process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200858{
859 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200860 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100861}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100862
Petr Machata36f40e72012-03-28 02:07:19 +0200863static enum callback_status
864no(struct process_stopping_handler *self)
865{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200866 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200867}
868
Petr Machatac1aa9582012-03-27 23:54:01 +0200869int
870process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200871 void (*as)(struct process_stopping_handler *),
872 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200873 (struct process_stopping_handler *),
874 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200875 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200876{
Petr Machata9ace5742012-04-14 02:29:57 +0200877 debug(DEBUG_FUNCTION,
878 "process_install_stopping_handler: pid=%d", proc->pid);
879
Petr Machatac1aa9582012-03-27 23:54:01 +0200880 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
881 if (handler == NULL)
882 return -1;
883
Petr Machata36f40e72012-03-28 02:07:19 +0200884 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200885 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200886 if (ks == NULL)
887 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200888 if (uw == NULL)
889 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200890
Petr Machatac1aa9582012-03-27 23:54:01 +0200891 handler->super.on_event = process_stopping_on_event;
892 handler->super.destroy = process_stopping_destroy;
893 handler->task_enabling_breakpoint = proc;
894 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200895 handler->on_all_stopped = as;
896 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200897 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200898
899 install_event_handler(proc->leader, &handler->super);
900
901 if (each_task(proc->leader, NULL, &send_sigstop,
902 &handler->pids) != NULL) {
903 destroy_event_handler(proc);
904 return -1;
905 }
906
907 /* And deliver the first fake event, in case all the
908 * conditions are already fulfilled. */
909 Event ev = {
910 .type = EVENT_NONE,
911 .proc = proc,
912 };
913 process_stopping_on_event(&handler->super, &ev);
914
915 return 0;
916}
917
Juan Cespedesf1350522008-12-16 18:19:58 +0100918void
Petr Machatabc373262012-02-07 23:31:15 +0100919continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200920{
Petr Machata9ace5742012-04-14 02:29:57 +0200921 debug(DEBUG_PROCESS,
922 "continue_after_breakpoint: pid=%d, addr=%p",
923 proc->pid, sbp->addr);
924
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200925 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200926
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100927 if (sbp->enabled == 0) {
928 continue_process(proc->pid);
929 } else {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500930#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100931 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200932 continue_process(proc->pid);
933#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200934 if (process_install_stopping_handler
935 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200936 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200937 /* Carry on not bothering to re-enable. */
938 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200939 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200940#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100941 }
942}
943
Petr Machata602330f2011-07-09 11:15:34 +0200944/**
945 * Ltrace exit. When we are about to exit, we have to go through all
946 * the processes, stop them all, remove all the breakpoints, and then
947 * detach the processes that we attached to using -p. If we left the
948 * other tasks running, they might hit stray return breakpoints and
949 * produce artifacts, so we better stop everyone, even if it's a bit
950 * of extra work.
951 */
952struct ltrace_exiting_handler
953{
Petr Machata366c2f42012-02-09 19:34:36 +0100954 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200955 struct pid_set pids;
956};
957
Petr Machata602330f2011-07-09 11:15:34 +0200958static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100959ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200960{
961 struct ltrace_exiting_handler * self = (void *)super;
962 Process * task = event->proc;
963 Process * leader = task->leader;
964
Petr Machata9ace5742012-04-14 02:29:57 +0200965 debug(DEBUG_PROCESS,
966 "ltrace_exiting_on_event: pid %d; event type %d",
967 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200968
969 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
970 handle_stopping_event(task_info, &event);
971
Petr Machata590c8082011-08-20 22:45:26 +0200972 if (event != NULL && event->type == EVENT_BREAKPOINT)
973 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200974
975 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200976 && all_stops_accountable(&self->pids))
977 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200978
979 /* Sink all non-exit events. We are about to exit, so we
980 * don't bother with queuing them. */
981 if (event_exit_or_none_p(event))
982 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200983
Petr Machata13d5df72011-08-19 23:15:15 +0200984 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200985}
986
987static void
Petr Machata366c2f42012-02-09 19:34:36 +0100988ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200989{
990 struct ltrace_exiting_handler * self = (void *)super;
991 free(self->pids.tasks);
992}
993
994static int
995ltrace_exiting_install_handler(Process * proc)
996{
997 /* Only install to leader. */
998 if (proc->leader != proc)
999 return 0;
1000
1001 /* Perhaps we are already installed, if the user passed
1002 * several -p options that are tasks of one process. */
1003 if (proc->event_handler != NULL
1004 && proc->event_handler->on_event == &ltrace_exiting_on_event)
1005 return 0;
1006
Petr Machata590c8082011-08-20 22:45:26 +02001007 /* If stopping handler is already present, let it do the
1008 * work. */
1009 if (proc->event_handler != NULL) {
1010 assert(proc->event_handler->on_event
1011 == &process_stopping_on_event);
1012 struct process_stopping_handler * other
1013 = (void *)proc->event_handler;
1014 other->exiting = 1;
1015 return 0;
1016 }
1017
Petr Machata602330f2011-07-09 11:15:34 +02001018 struct ltrace_exiting_handler * handler
1019 = calloc(sizeof(*handler), 1);
1020 if (handler == NULL) {
1021 perror("malloc exiting handler");
1022 fatal:
1023 /* XXXXXXXXXXXXXXXXXXX fixme */
1024 return -1;
1025 }
1026
Petr Machata602330f2011-07-09 11:15:34 +02001027 handler->super.on_event = ltrace_exiting_on_event;
1028 handler->super.destroy = ltrace_exiting_destroy;
1029 install_event_handler(proc->leader, &handler->super);
1030
Petr Machata74132a42012-03-16 02:46:18 +01001031 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001032 &handler->pids) != NULL)
1033 goto fatal;
1034
1035 return 0;
1036}
1037
Petr Machatacbe29c62011-09-27 02:27:58 +02001038/*
1039 * When the traced process vforks, it's suspended until the child
1040 * process calls _exit or exec*. In the meantime, the two share the
1041 * address space.
1042 *
1043 * The child process should only ever call _exit or exec*, but we
1044 * can't count on that (it's not the role of ltrace to policy, but to
1045 * observe). In any case, we will _at least_ have to deal with
1046 * removal of vfork return breakpoint (which we have to smuggle back
1047 * in, so that the parent can see it, too), and introduction of exec*
1048 * return breakpoint. Since we already have both breakpoint actions
1049 * to deal with, we might as well support it all.
1050 *
1051 * The gist is that we pretend that the child is in a thread group
1052 * with its parent, and handle it as a multi-threaded case, with the
1053 * exception that we know that the parent is blocked, and don't
1054 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001055 */
1056
Petr Machata134a1082011-09-27 20:25:58 +02001057struct process_vfork_handler
1058{
Petr Machata366c2f42012-02-09 19:34:36 +01001059 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +02001060 void * bp_addr;
1061};
1062
Petr Machatacbe29c62011-09-27 02:27:58 +02001063static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001064process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001065{
Petr Machata9ace5742012-04-14 02:29:57 +02001066 debug(DEBUG_PROCESS,
1067 "process_vfork_on_event: pid %d; event type %d",
1068 event->proc->pid, event->type);
1069
Petr Machatacbe29c62011-09-27 02:27:58 +02001070 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001071 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001072 assert(self != NULL);
1073
1074 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001075 case EVENT_BREAKPOINT:
1076 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001077 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001078 self->bp_addr = event->e_un.brk_addr;
1079 break;
1080
Petr Machatacbe29c62011-09-27 02:27:58 +02001081 case EVENT_EXIT:
1082 case EVENT_EXIT_SIGNAL:
1083 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001084 /* Smuggle back in the vfork return breakpoint, so
1085 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001086 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001087 sbp = dict_find_entry(event->proc->leader->breakpoints,
1088 self->bp_addr);
1089 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001090 assert(sbp->libsym == NULL);
1091 /* We don't mind failing that, it's not a big
1092 * deal to not display one extra vfork return. */
1093 insert_breakpoint(event->proc->parent,
1094 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001095 }
1096
Petr Machataba9911f2011-09-27 21:09:47 +02001097 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001098
1099 /* Remove the leader that we artificially set up
1100 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001101 change_process_leader(event->proc, event->proc);
1102 destroy_event_handler(event->proc);
1103
Petr Machatacbe29c62011-09-27 02:27:58 +02001104 default:
1105 ;
1106 }
1107
1108 return event;
1109}
1110
1111void
1112continue_after_vfork(Process * proc)
1113{
1114 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001115 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001116 if (handler == NULL) {
1117 perror("malloc vfork handler");
1118 /* Carry on not bothering to treat the process as
1119 * necessary. */
1120 continue_process(proc->parent->pid);
1121 return;
1122 }
1123
1124 /* We must set up custom event handler, so that we see
1125 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001126 handler->super.on_event = process_vfork_on_event;
1127 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001128
1129 /* Make sure that the child is sole thread. */
1130 assert(proc->leader == proc);
1131 assert(proc->next == NULL || proc->next->leader != proc);
1132
1133 /* Make sure that the child's parent is properly set up. */
1134 assert(proc->parent != NULL);
1135 assert(proc->parent->leader != NULL);
1136
1137 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001138}
1139
Petr Machata9d29b3e2011-11-09 16:46:56 +01001140static int
1141is_mid_stopping(Process *proc)
1142{
1143 return proc != NULL
1144 && proc->event_handler != NULL
1145 && proc->event_handler->on_event == &process_stopping_on_event;
1146}
1147
Petr Machata43d2fe52011-11-02 13:25:49 +01001148void
1149continue_after_syscall(Process * proc, int sysnum, int ret_p)
1150{
1151 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001152 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1153 debug(DEBUG_PROCESS,
1154 "continue_after_syscall: don't continue %d",
1155 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001156 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001157 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001158 continue_process(proc->pid);
1159}
1160
Petr Machata602330f2011-07-09 11:15:34 +02001161/* If ltrace gets SIGINT, the processes directly or indirectly run by
1162 * ltrace get it too. We just have to wait long enough for the signal
1163 * to be delivered and the process terminated, which we notice and
1164 * exit ltrace, too. So there's not much we need to do there. We
1165 * want to keep tracing those processes as usual, in case they just
1166 * SIG_IGN the SIGINT to do their shutdown etc.
1167 *
1168 * For processes ran on the background, we want to install an exit
1169 * handler that stops all the threads, removes all breakpoints, and
1170 * detaches.
1171 */
1172void
Petr Machataffe4cd22012-04-11 18:01:44 +02001173os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001174{
1175 struct opt_p_t * it;
1176 for (it = opt_p; it != NULL; it = it->next) {
1177 Process * proc = pid2proc(it->pid);
1178 if (proc == NULL || proc->leader == NULL)
1179 continue;
1180 if (ltrace_exiting_install_handler(proc->leader) < 0)
1181 fprintf(stderr,
1182 "Couldn't install exiting handler for %d.\n",
1183 proc->pid);
1184 }
1185}
1186
Petr Machataffe4cd22012-04-11 18:01:44 +02001187int
1188os_ltrace_exiting_sighandler(void)
1189{
1190 extern int linux_in_waitpid;
1191 if (linux_in_waitpid) {
1192 os_ltrace_exiting();
1193 return 1;
1194 }
1195 return 0;
1196}
1197
Joe Damatodfa3fa32010-11-08 15:47:35 -08001198size_t
1199umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1200
1201 union {
1202 long a;
1203 char c[sizeof(long)];
1204 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001205 int started = 0;
1206 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001207
1208 while (offset < len) {
1209 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1210 if (a.a == -1 && errno) {
1211 if (started && errno == EIO)
1212 return bytes_read;
1213 else
1214 return -1;
1215 }
1216 started = 1;
1217
1218 if (len - offset >= sizeof(long)) {
1219 memcpy(laddr + offset, &a.c[0], sizeof(long));
1220 bytes_read += sizeof(long);
1221 }
1222 else {
1223 memcpy(laddr + offset, &a.c[0], len - offset);
1224 bytes_read += (len - offset);
1225 }
1226 offset += sizeof(long);
1227 }
1228
1229 return bytes_read;
1230}