blob: 5567eb6febb702efc7c0663f5b3675c65f2b7a86 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata693dfad2013-01-14 22:10:51 +01003 * Copyright (C) 2007,2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 * 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>
Petr Machatacec06ec2012-04-10 13:31:55 +020027#include <assert.h>
28#include <errno.h>
Petr Machatab420a222013-10-15 10:46:28 +020029#include <gelf.h>
30#include <stdbool.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>
Petr Machatab420a222013-10-15 10:46:28 +020034#include <sys/types.h>
35#include <sys/wait.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010036#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010037
Petr Machatacec06ec2012-04-10 13:31:55 +020038#ifdef HAVE_LIBSELINUX
39# include <selinux/selinux.h>
40#endif
41
Petr Machataaf766ab2012-11-07 21:59:13 +010042#include "linux-gnu/trace-defs.h"
Petr Machatab420a222013-10-15 10:46:28 +020043#include "linux-gnu/trace.h"
Petr Machata64262602012-01-07 03:41:36 +010044#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020045#include "breakpoint.h"
46#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010047#include "events.h"
Petr Machatab420a222013-10-15 10:46:28 +020048#include "ltrace-elf.h"
Petr Machataba1664b2012-04-28 14:59:05 +020049#include "options.h"
50#include "proc.h"
51#include "ptrace.h"
52#include "type.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020053
Juan Cespedesf1350522008-12-16 18:19:58 +010054void
Petr Machatacec06ec2012-04-10 13:31:55 +020055trace_fail_warning(pid_t pid)
56{
57 /* This was adapted from GDB. */
58#ifdef HAVE_LIBSELINUX
59 static int checked = 0;
60 if (checked)
61 return;
62 checked = 1;
63
64 /* -1 is returned for errors, 0 if it has no effect, 1 if
65 * PTRACE_ATTACH is forbidden. */
66 if (security_get_boolean_active("deny_ptrace") == 1)
67 fprintf(stderr,
68"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
69"tracing other processes. You can disable this process attach protection by\n"
70"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
71#endif /* HAVE_LIBSELINUX */
72}
73
74void
75trace_me(void)
76{
Petr Machata26627682011-07-08 18:15:32 +020077 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020078 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010079 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +020080 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +010081 exit(1);
82 }
83}
84
Petr Machatab4f9e0c2012-02-07 01:57:59 +010085/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +010086 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +010087 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +010088int
Petr Machatab4f9e0c2012-02-07 01:57:59 +010089wait_for_proc(pid_t pid)
90{
Petr Machatac805c622012-03-02 00:10:37 +010091 /* man ptrace: PTRACE_ATTACH attaches to the process specified
92 in pid. The child is sent a SIGSTOP, but will not
93 necessarily have stopped by the completion of this call;
94 use wait() to wait for the child to stop. */
95 if (waitpid(pid, NULL, __WALL) != pid) {
96 perror ("trace_pid: waitpid");
97 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010098 }
99
Petr Machatac805c622012-03-02 00:10:37 +0100100 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100101}
102
Juan Cespedesf1350522008-12-16 18:19:58 +0100103int
Petr Machatacec06ec2012-04-10 13:31:55 +0200104trace_pid(pid_t pid)
105{
Petr Machata26627682011-07-08 18:15:32 +0200106 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200107 /* This shouldn't emit error messages, as there are legitimate
108 * reasons that the PID can't be attached: like it may have
109 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200110 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100111 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100112
Petr Machatac805c622012-03-02 00:10:37 +0100113 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100114}
115
Juan Cespedesf1350522008-12-16 18:19:58 +0100116void
Petr Machata929bd572012-12-17 03:20:34 +0100117trace_set_options(struct process *proc)
Petr Machata3ed2a422012-04-06 17:18:55 +0200118{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100119 if (proc->tracesysgood & 0x80)
120 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200121
Petr Machata3ed2a422012-04-06 17:18:55 +0200122 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200123 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200124
Juan Cespedes1e583132009-04-07 18:17:11 +0200125 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
126 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
127 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200128 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
129 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100130 perror("PTRACE_SETOPTIONS");
131 return;
132 }
133 proc->tracesysgood |= 0x80;
134}
135
Juan Cespedesf1350522008-12-16 18:19:58 +0100136void
137untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200138 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200139 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100140}
141
Juan Cespedesf1350522008-12-16 18:19:58 +0100142void
Petr Machatac897cb72012-05-05 01:26:58 +0200143continue_after_signal(pid_t pid, int signum)
144{
145 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
146 pid, signum);
147 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200148}
149
150static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200151event_for_pid(Event *event, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200152{
153 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
Petr Machata71f25e22012-12-17 04:01:23 +0100154 return ECB_YIELD;
155 return ECB_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200156}
157
158static int
159have_events_for(pid_t pid)
160{
161 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
162}
163
164void
165continue_process(pid_t pid)
166{
167 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200168
169 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200170 the queue for this process. Otherwise just wait for the
171 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200172 if (!have_events_for(pid))
173 /* We always trace syscalls to control fork(),
174 * clone(), execve()... */
175 ptrace(PTRACE_SYSCALL, pid, 0, 0);
176 else
177 debug(DEBUG_PROCESS,
178 "putting off the continue, events in que.");
179}
180
Petr Machata98f09922011-07-09 10:55:29 +0200181static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200182get_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200183{
Petr Machata750ca8c2011-10-06 14:29:34 +0200184 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200185 size_t i;
186 for (i = 0; i < pids->count; ++i)
187 if (pids->tasks[i].pid == pid)
188 return &pids->tasks[i];
189
190 return NULL;
191}
192
193static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200194add_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200195{
196 if (pids->count == pids->alloc) {
197 size_t ns = (2 * pids->alloc) ?: 4;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200198 struct pid_task *n = realloc(pids->tasks,
199 sizeof(*pids->tasks) * ns);
Petr Machata98f09922011-07-09 10:55:29 +0200200 if (n == NULL)
201 return NULL;
202 pids->tasks = n;
203 pids->alloc = ns;
204 }
205 struct pid_task * task_info = &pids->tasks[pids->count++];
206 memset(task_info, 0, sizeof(*task_info));
207 task_info->pid = pid;
208 return task_info;
209}
210
Petr Machata2b46cfc2012-02-18 11:17:29 +0100211static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100212task_stopped(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200213{
214 enum process_status st = process_status(task->pid);
215 if (data != NULL)
216 *(enum process_status *)data = st;
217
218 /* If the task is already stopped, don't worry about it.
219 * Likewise if it managed to become a zombie or terminate in
220 * the meantime. This can happen when the whole thread group
221 * is terminating. */
222 switch (st) {
Petr Machata6dfc5442012-12-17 03:38:36 +0100223 case PS_INVALID:
224 case PS_TRACING_STOP:
225 case PS_ZOMBIE:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 return CBS_CONT;
Petr Machata6dfc5442012-12-17 03:38:36 +0100227 case PS_SLEEPING:
228 case PS_STOP:
229 case PS_OTHER:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100230 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200231 }
Petr Machata36d19822011-10-21 16:03:45 +0200232
233 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200234}
235
236/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100238task_blocked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200239{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200240 struct pid_set *pids = data;
241 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +0200242 if (task_info != NULL
243 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200245
246 return task_stopped(task, NULL);
247}
248
Petr Machata366c2f42012-02-09 19:34:36 +0100249static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200250
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100252task_vforked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200253{
254 if (task->event_handler != NULL
255 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 return CBS_STOP;
257 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200258}
259
260static int
Petr Machata929bd572012-12-17 03:20:34 +0100261is_vfork_parent(struct process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200262{
Petr Machata74132a42012-03-16 02:46:18 +0100263 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200264}
265
Petr Machata2b46cfc2012-02-18 11:17:29 +0100266static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100267send_sigstop(struct process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200268{
Petr Machata929bd572012-12-17 03:20:34 +0100269 struct process *leader = task->leader;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200270 struct pid_set *pids = data;
Petr Machata98f09922011-07-09 10:55:29 +0200271
272 /* Look for pre-existing task record, or add new. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200273 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200274 if (task_info == NULL)
275 task_info = add_task_info(pids, task->pid);
276 if (task_info == NULL) {
277 perror("send_sigstop: add_task_info");
278 destroy_event_handler(leader);
279 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100280 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200281 }
282
283 /* This task still has not been attached to. It should be
284 stopped by the kernel. */
285 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200287
288 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200289 * if we sent the SIGSTOP already, which happens when we are
290 * handling "onexit" and inherited the handler from breakpoint
291 * re-enablement. */
292 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100293 if (task_stopped(task, &st) == CBS_CONT)
294 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200295 if (task_info->sigstopped) {
296 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200298 task_info->delivered = 0;
299 }
300
Petr Machatacbe29c62011-09-27 02:27:58 +0200301 /* Also don't attempt to stop the process if it's a parent of
302 * vforked process. We set up event handler specially to hint
303 * us. In that case parent is in D state, which we use to
304 * weed out unnecessary looping. */
Petr Machata6dfc5442012-12-17 03:38:36 +0100305 if (st == PS_SLEEPING
306 && is_vfork_parent(task)) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200307 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100308 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200309 }
310
Petr Machata98f09922011-07-09 10:55:29 +0200311 if (task_kill(task->pid, SIGSTOP) >= 0) {
312 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
313 task_info->sigstopped = 1;
314 } else
315 fprintf(stderr,
316 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
317
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200319}
320
Petr Machata73894bd2011-08-20 23:47:34 +0200321/* On certain kernels, detaching right after a singlestep causes the
322 tracee to be killed with a SIGTRAP (that even though the singlestep
323 was properly caught by waitpid. The ugly workaround is to put a
324 breakpoint where IP points and let the process continue. After
325 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200326static void
Petr Machata929bd572012-12-17 03:20:34 +0100327ugly_workaround(struct process *proc)
Petr Machata590c8082011-08-20 22:45:26 +0200328{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100329 arch_addr_t ip = get_instruction_pointer(proc);
Petr Machata98ff3092013-03-08 22:11:36 +0100330 struct breakpoint *found;
331 if (DICT_FIND_VAL(proc->leader->breakpoints, &ip, &found) < 0) {
Petr Machata02a796e2013-10-11 17:24:30 +0200332 insert_breakpoint_at(proc, ip, NULL);
Petr Machata98ff3092013-03-08 22:11:36 +0100333 } else {
334 assert(found != NULL);
335 enable_breakpoint(proc, found);
336 }
Petr Machata73894bd2011-08-20 23:47:34 +0200337 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200338}
339
340static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200341process_stopping_done(struct process_stopping_handler *self,
Petr Machata929bd572012-12-17 03:20:34 +0100342 struct process *leader)
Petr Machata98f09922011-07-09 10:55:29 +0200343{
344 debug(DEBUG_PROCESS, "process stopping done %d",
345 self->task_enabling_breakpoint->pid);
Petr Machata8ae36732012-04-30 01:19:09 +0200346
Petr Machata590c8082011-08-20 22:45:26 +0200347 if (!self->exiting) {
Petr Machata8ae36732012-04-30 01:19:09 +0200348 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200349 for (i = 0; i < self->pids.count; ++i)
350 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100351 && (self->pids.tasks[i].delivered
352 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200353 continue_process(self->pids.tasks[i].pid);
354 continue_process(self->task_enabling_breakpoint->pid);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200355 }
356
357 if (self->exiting) {
358 ugly_workaround:
Petr Machata96cb8e32012-12-17 03:49:41 +0100359 self->state = PSH_UGLY_WORKAROUND;
Petr Machata73894bd2011-08-20 23:47:34 +0200360 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200361 } else {
362 switch ((self->ugly_workaround_p)(self)) {
363 case CBS_FAIL:
364 /* xxx handle me */
365 case CBS_STOP:
366 break;
367 case CBS_CONT:
368 goto ugly_workaround;
369 }
Petr Machata8ae36732012-04-30 01:19:09 +0200370 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200371 }
372}
373
374/* Before we detach, we need to make sure that task's IP is on the
375 * edge of an instruction. So for tasks that have a breakpoint event
376 * in the queue, we adjust the instruction pointer, just like
377 * continue_after_breakpoint does. */
378static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200379undo_breakpoint(Event *event, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200380{
381 if (event != NULL
382 && event->proc->leader == data
383 && event->type == EVENT_BREAKPOINT)
384 set_instruction_pointer(event->proc, event->e_un.brk_addr);
Petr Machata71f25e22012-12-17 04:01:23 +0100385 return ECB_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200386}
387
Petr Machata2b46cfc2012-02-18 11:17:29 +0100388static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100389untrace_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200390{
391 if (task != data)
392 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100393 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200394}
395
Petr Machata2b46cfc2012-02-18 11:17:29 +0100396static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100397remove_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200398{
399 /* Don't untrace leader just yet. */
400 if (task != data)
401 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100402 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200403}
404
Petr Machata86d38282012-04-24 18:09:01 +0200405static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100406retract_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata86d38282012-04-24 18:09:01 +0200407{
408 breakpoint_on_retract(bp, proc);
409 return CBS_CONT;
410}
411
Petr Machata590c8082011-08-20 22:45:26 +0200412static void
Petr Machata929bd572012-12-17 03:20:34 +0100413detach_process(struct process *leader)
Petr Machata590c8082011-08-20 22:45:26 +0200414{
415 each_qd_event(&undo_breakpoint, leader);
416 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200417 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200418
419 /* Now untrace the process, if it was attached to by -p. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200420 struct opt_p_t *it;
Petr Machata590c8082011-08-20 22:45:26 +0200421 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +0100422 struct process *proc = pid2proc(it->pid);
Petr Machata590c8082011-08-20 22:45:26 +0200423 if (proc == NULL)
424 continue;
425 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100426 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200427 break;
428 }
429 }
Petr Machata74132a42012-03-16 02:46:18 +0100430 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200431 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200432 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200433}
434
435static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200436handle_stopping_event(struct pid_task *task_info, Event **eventp)
Petr Machata98f09922011-07-09 10:55:29 +0200437{
438 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200439 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200440 task_info->got_event = 1;
441
Petr Machata6c2e57a2012-10-27 01:50:22 +0200442 Event *event = *eventp;
Petr Machata98f09922011-07-09 10:55:29 +0200443
444 /* In every state, sink SIGSTOP events for tasks that it was
445 * sent to. */
446 if (task_info != NULL
447 && event->type == EVENT_SIGNAL
448 && event->e_un.signum == SIGSTOP) {
449 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
450 if (task_info->sigstopped
451 && !task_info->delivered) {
452 task_info->delivered = 1;
453 *eventp = NULL; // sink the event
454 } else
455 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
456 "sigstopped=%d and delivered=%d\n",
457 task_info->pid, task_info->sigstopped,
458 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100459 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100460}
461
Petr Machata98f09922011-07-09 10:55:29 +0200462/* Some SIGSTOPs may have not been delivered to their respective tasks
463 * yet. They are still in the queue. If we have seen an event for
464 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200465 * caught by ltrace. We don't mind that the process is after
466 * breakpoint (and therefore potentially doesn't have aligned IP),
467 * because the signal will be delivered without the process actually
468 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200469static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200470continue_for_sigstop_delivery(struct pid_set *pids)
Petr Machata98f09922011-07-09 10:55:29 +0200471{
472 size_t i;
473 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200474 if (pids->tasks[i].pid != 0
475 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200476 && !pids->tasks[i].delivered
477 && pids->tasks[i].got_event) {
478 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
479 pids->tasks[i].pid);
480 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
481 }
482 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100483}
484
Petr Machata98f09922011-07-09 10:55:29 +0200485static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200486event_exit_p(Event *event)
Petr Machata750ca8c2011-10-06 14:29:34 +0200487{
488 return event != NULL && (event->type == EVENT_EXIT
489 || event->type == EVENT_EXIT_SIGNAL);
490}
491
492static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200493event_exit_or_none_p(Event *event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200494{
Petr Machata750ca8c2011-10-06 14:29:34 +0200495 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200496 || event->type == EVENT_NONE;
497}
498
499static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200500await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
501 Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200502{
503 /* If we still didn't get our SIGSTOP, continue the process
504 * and carry on. */
505 if (event != NULL && !event_exit_or_none_p(event)
506 && task_info != NULL && task_info->sigstopped) {
507 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
508 task_info->pid);
509 /* We should get the signal the first thing
510 * after this, so it should be OK to continue
511 * even if we are over a breakpoint. */
512 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
513
514 } else {
515 /* If all SIGSTOPs were delivered, uninstall the
516 * handler and continue everyone. */
517 /* XXX I suspect that we should check tasks that are
518 * still around. Is things are now, there should be a
519 * race between waiting for everyone to stop and one
520 * of the tasks exiting. */
521 int all_clear = 1;
522 size_t i;
523 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200524 if (pids->tasks[i].pid != 0
525 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200526 && !pids->tasks[i].delivered) {
527 all_clear = 0;
528 break;
529 }
530 return all_clear;
531 }
532
533 return 0;
534}
535
Petr Machata590c8082011-08-20 22:45:26 +0200536static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200537all_stops_accountable(struct pid_set *pids)
Petr Machata590c8082011-08-20 22:45:26 +0200538{
539 size_t i;
540 for (i = 0; i < pids->count; ++i)
541 if (pids->tasks[i].pid != 0
542 && !pids->tasks[i].got_event
543 && !have_events_for(pids->tasks[i].pid))
544 return 0;
545 return 1;
546}
547
Petr Machata693dfad2013-01-14 22:10:51 +0100548#ifndef ARCH_HAVE_SW_SINGLESTEP
549enum sw_singlestep_status
550arch_sw_singlestep(struct process *proc, struct breakpoint *bp,
551 int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
552 struct sw_singlestep_data *data)
Petr Machata06986d52011-11-02 13:22:46 +0100553{
Petr Machata693dfad2013-01-14 22:10:51 +0100554 return SWS_HW;
Petr Machataa266acb2012-04-12 23:50:23 +0200555}
556#endif
557
Petr Machata42748ac2012-04-19 04:24:25 +0200558static Event *process_stopping_on_event(struct event_handler *super,
559 Event *event);
560
561static void
Petr Machata693dfad2013-01-14 22:10:51 +0100562remove_sw_breakpoints(struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200563{
Petr Machata24a47042012-04-19 18:15:08 +0200564 struct process_stopping_handler *self
565 = (void *)proc->leader->event_handler;
566 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200567 assert(self->super.on_event == process_stopping_on_event);
568
Petr Machata6e5e2de2013-01-21 22:25:34 +0100569 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200570 int i;
571 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100572 if (self->sws_bps[i] != NULL) {
Petr Machatab9440772013-10-14 10:36:26 +0200573 delete_breakpoint_at(proc, self->sws_bps[i]->addr);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100574 self->sws_bps[i] = NULL;
Petr Machata42748ac2012-04-19 04:24:25 +0200575 }
576}
577
578static void
Petr Machata693dfad2013-01-14 22:10:51 +0100579sw_singlestep_bp_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200580{
Petr Machata693dfad2013-01-14 22:10:51 +0100581 remove_sw_breakpoints(proc);
Petr Machata42748ac2012-04-19 04:24:25 +0200582}
583
Petr Machata693dfad2013-01-14 22:10:51 +0100584struct sw_singlestep_data {
585 struct process_stopping_handler *self;
586};
587
Petr Machataa266acb2012-04-12 23:50:23 +0200588static int
Petr Machata693dfad2013-01-14 22:10:51 +0100589sw_singlestep_add_bp(arch_addr_t addr, struct sw_singlestep_data *data)
Petr Machataa266acb2012-04-12 23:50:23 +0200590{
Petr Machata693dfad2013-01-14 22:10:51 +0100591 struct process_stopping_handler *self = data->self;
Petr Machata929bd572012-12-17 03:20:34 +0100592 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200593
Petr Machata6e5e2de2013-01-21 22:25:34 +0100594 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200595 int i;
596 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100597 if (self->sws_bps[i] == NULL) {
Petr Machata42748ac2012-04-19 04:24:25 +0200598 static struct bp_callbacks cbs = {
Petr Machata693dfad2013-01-14 22:10:51 +0100599 .on_hit = sw_singlestep_bp_on_hit,
Petr Machata42748ac2012-04-19 04:24:25 +0200600 };
601 struct breakpoint *bp
Petr Machata02a796e2013-10-11 17:24:30 +0200602 = insert_breakpoint_at(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200603 breakpoint_set_callbacks(bp, &cbs);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100604 self->sws_bps[i] = bp;
Petr Machata42748ac2012-04-19 04:24:25 +0200605 return 0;
606 }
Petr Machataa266acb2012-04-12 23:50:23 +0200607
Petr Machata693dfad2013-01-14 22:10:51 +0100608 assert(!"Too many sw singlestep breakpoints!");
Petr Machata42748ac2012-04-19 04:24:25 +0200609 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200610}
611
612static int
613singlestep(struct process_stopping_handler *self)
614{
Petr Machata6e5e2de2013-01-21 22:25:34 +0100615 size_t i;
616 for (i = 0; i < sizeof(self->sws_bps) / sizeof(*self->sws_bps); ++i)
617 self->sws_bps[i] = NULL;
Petr Machataa266acb2012-04-12 23:50:23 +0200618
Petr Machata693dfad2013-01-14 22:10:51 +0100619 struct sw_singlestep_data data = { self };
Petr Machata643c6462013-01-15 17:11:43 +0100620 switch (arch_sw_singlestep(self->task_enabling_breakpoint,
621 self->breakpoint_being_enabled,
622 &sw_singlestep_add_bp, &data)) {
623 case SWS_HW:
624 /* Otherwise do the default action: singlestep. */
625 debug(1, "PTRACE_SINGLESTEP");
Petr Machata6e5e2de2013-01-21 22:25:34 +0100626 if (ptrace(PTRACE_SINGLESTEP,
627 self->task_enabling_breakpoint->pid, 0, 0)) {
Petr Machata643c6462013-01-15 17:11:43 +0100628 perror("PTRACE_SINGLESTEP");
629 return -1;
630 }
631 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200632
Petr Machata643c6462013-01-15 17:11:43 +0100633 case SWS_OK:
634 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200635
Petr Machata643c6462013-01-15 17:11:43 +0100636 case SWS_FAIL:
Petr Machataa266acb2012-04-12 23:50:23 +0200637 return -1;
638 }
Petr Machata643c6462013-01-15 17:11:43 +0100639 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200640}
641
642static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200643post_singlestep(struct process_stopping_handler *self,
644 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200645{
646 continue_for_sigstop_delivery(&self->pids);
647
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200648 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200649 *eventp = NULL; // handled
650
Petr Machata929bd572012-12-17 03:20:34 +0100651 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200652
Petr Machata693dfad2013-01-14 22:10:51 +0100653 remove_sw_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200654 self->breakpoint_being_enabled = NULL;
655}
656
657static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200658singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200659{
Petr Machata929bd572012-12-17 03:20:34 +0100660 struct process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200661 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200662 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataba1664b2012-04-28 14:59:05 +0200663 teb->pid, breakpoint_name(sbp), sbp->addr,
664 get_instruction_pointer(teb));
Petr Machatab9440772013-10-14 10:36:26 +0200665 delete_breakpoint_at(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100666}
667
Petr Machata9e1e9692012-04-19 02:29:38 +0200668static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200669pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200670{
Petr Machata929bd572012-12-17 03:20:34 +0100671 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200672 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200673 ptrace(PTRACE_CONT, teb->pid, 0, 0);
674}
675
676static void
677pt_singlestep(struct process_stopping_handler *self)
678{
679 if (singlestep(self) < 0)
680 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200681}
682
683static void
684disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200685 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200686{
Petr Machata929bd572012-12-17 03:20:34 +0100687 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200688 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200689 if (self->breakpoint_being_enabled->enabled)
690 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200691 (do_this)(self);
Petr Machata96cb8e32012-12-17 03:49:41 +0100692 self->state = PSH_SINGLESTEP;
Petr Machatac1aa9582012-03-27 23:54:01 +0200693}
694
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200695void
696linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
697{
Petr Machata9e1e9692012-04-19 02:29:38 +0200698 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200699}
700
701void
702linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
703{
704 disable_and(self, &pt_continue);
705}
706
Petr Machata98f09922011-07-09 10:55:29 +0200707/* This event handler is installed when we are in the process of
708 * stopping the whole thread group to do the pointer re-enablement for
709 * one of the threads. We pump all events to the queue for later
710 * processing while we wait for all the threads to stop. When this
711 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
712 * re-enable, and continue everyone. */
713static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100714process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200715{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200716 struct process_stopping_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100717 struct process *task = event->proc;
718 struct process *leader = task->leader;
719 struct process *teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200720
721 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200722 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200723 task->pid, event->type, self->state);
724
Petr Machata6c2e57a2012-10-27 01:50:22 +0200725 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200726 if (task_info == NULL)
727 fprintf(stderr, "new task??? %d\n", task->pid);
728 handle_stopping_event(task_info, &event);
729
730 int state = self->state;
731 int event_to_queue = !event_exit_or_none_p(event);
732
Petr Machata18c97072011-10-06 14:30:11 +0200733 /* Deactivate the entry if the task exits. */
734 if (event_exit_p(event) && task_info != NULL)
735 task_info->pid = 0;
736
Petr Machata43d2fe52011-11-02 13:25:49 +0100737 /* Always handle sysrets. Whether sysret occurred and what
738 * sys it rets from may need to be determined based on process
739 * stack, so we need to keep that in sync with reality. Note
740 * that we don't continue the process after the sysret is
741 * handled. See continue_after_syscall. */
742 if (event != NULL && event->type == EVENT_SYSRET) {
743 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
744 event_to_queue = 0;
Petr Machata754ce882013-01-08 23:41:47 +0100745 if (task_info != NULL)
746 task_info->sysret = 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100747 }
748
Petr Machata98f09922011-07-09 10:55:29 +0200749 switch (state) {
Petr Machata96cb8e32012-12-17 03:49:41 +0100750 case PSH_STOPPING:
Petr Machata98f09922011-07-09 10:55:29 +0200751 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100752 if (each_task(leader, NULL, &task_blocked,
753 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200754 (self->on_all_stopped)(self);
755 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200756 }
757 break;
758
Petr Machata96cb8e32012-12-17 03:49:41 +0100759 case PSH_SINGLESTEP:
Petr Machata98f09922011-07-09 10:55:29 +0200760 /* In singlestep state, breakpoint signifies that we
761 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200762 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200763
Petr Machata42748ac2012-04-19 04:24:25 +0200764 /* If this was caused by a real breakpoint, as
765 * opposed to a singlestep, assume that it's
766 * an artificial breakpoint installed for some
767 * reason for the re-enablement. In that case
768 * handle it. */
769 if (event->type == EVENT_BREAKPOINT) {
Petr Machatabac2da52012-05-29 00:42:59 +0200770 arch_addr_t ip
Petr Machata42748ac2012-04-19 04:24:25 +0200771 = get_instruction_pointer(task);
772 struct breakpoint *other
773 = address2bpstruct(leader, ip);
774 if (other != NULL)
775 breakpoint_on_hit(other, task);
776 }
777
Petr Machata36f40e72012-03-28 02:07:19 +0200778 /* If we got SIGNAL instead of BREAKPOINT,
779 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200780 if (event->type == EVENT_SIGNAL) {
781 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200782 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200783 singlestep_error(self);
784 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200785 goto psh_sinking;
786 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200787 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200788 } else {
789 switch ((self->keep_stepping_p)(self)) {
790 case CBS_FAIL:
791 /* XXX handle me */
792 case CBS_STOP:
793 break;
794 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200795 /* Sink singlestep event. */
796 if (event->type == EVENT_BREAKPOINT)
797 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200798 goto do_singlestep;
799 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200800 }
801
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200802 /* Re-enable the breakpoint that we are
803 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200804 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200805 if (sbp->enabled)
806 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200807
Petr Machataa266acb2012-04-12 23:50:23 +0200808 post_singlestep(self, &event);
809 goto psh_sinking;
810 }
811 break;
Petr Machata98f09922011-07-09 10:55:29 +0200812
Petr Machataa266acb2012-04-12 23:50:23 +0200813 psh_sinking:
Petr Machata96cb8e32012-12-17 03:49:41 +0100814 state = self->state = PSH_SINKING;
815 /* Fall through. */
816 case PSH_SINKING:
Petr Machata98f09922011-07-09 10:55:29 +0200817 if (await_sigstop_delivery(&self->pids, task_info, event))
818 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200819 break;
820
Petr Machata96cb8e32012-12-17 03:49:41 +0100821 case PSH_UGLY_WORKAROUND:
Petr Machata590c8082011-08-20 22:45:26 +0200822 if (event == NULL)
823 break;
824 if (event->type == EVENT_BREAKPOINT) {
825 undo_breakpoint(event, leader);
826 if (task == teb)
827 self->task_enabling_breakpoint = NULL;
828 }
829 if (self->task_enabling_breakpoint == NULL
830 && all_stops_accountable(&self->pids)) {
831 undo_breakpoint(event, leader);
832 detach_process(leader);
833 event = NULL; // handled
834 }
Petr Machata98f09922011-07-09 10:55:29 +0200835 }
836
837 if (event != NULL && event_to_queue) {
838 enque_event(event);
839 event = NULL; // sink the event
840 }
841
842 return event;
843}
844
845static void
Petr Machata366c2f42012-02-09 19:34:36 +0100846process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200847{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200848 struct process_stopping_handler *self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200849 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100850}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100851
Petr Machata36f40e72012-03-28 02:07:19 +0200852static enum callback_status
853no(struct process_stopping_handler *self)
854{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200855 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200856}
857
Petr Machatac1aa9582012-03-27 23:54:01 +0200858int
Petr Machata929bd572012-12-17 03:20:34 +0100859process_install_stopping_handler(struct process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200860 void (*as)(struct process_stopping_handler *),
861 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200862 (struct process_stopping_handler *),
863 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200864 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200865{
Petr Machata9ace5742012-04-14 02:29:57 +0200866 debug(DEBUG_FUNCTION,
867 "process_install_stopping_handler: pid=%d", proc->pid);
868
Petr Machatac1aa9582012-03-27 23:54:01 +0200869 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
870 if (handler == NULL)
871 return -1;
872
Petr Machata36f40e72012-03-28 02:07:19 +0200873 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200874 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200875 if (ks == NULL)
876 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200877 if (uw == NULL)
878 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200879
Petr Machatac1aa9582012-03-27 23:54:01 +0200880 handler->super.on_event = process_stopping_on_event;
881 handler->super.destroy = process_stopping_destroy;
882 handler->task_enabling_breakpoint = proc;
883 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200884 handler->on_all_stopped = as;
885 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200886 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200887
888 install_event_handler(proc->leader, &handler->super);
889
890 if (each_task(proc->leader, NULL, &send_sigstop,
891 &handler->pids) != NULL) {
892 destroy_event_handler(proc);
893 return -1;
894 }
895
896 /* And deliver the first fake event, in case all the
897 * conditions are already fulfilled. */
898 Event ev = {
899 .type = EVENT_NONE,
900 .proc = proc,
901 };
902 process_stopping_on_event(&handler->super, &ev);
903
904 return 0;
905}
906
Juan Cespedesf1350522008-12-16 18:19:58 +0100907void
Petr Machata929bd572012-12-17 03:20:34 +0100908continue_after_breakpoint(struct process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200909{
Petr Machata9ace5742012-04-14 02:29:57 +0200910 debug(DEBUG_PROCESS,
911 "continue_after_breakpoint: pid=%d, addr=%p",
912 proc->pid, sbp->addr);
913
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200914 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200915
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100916 if (sbp->enabled == 0) {
917 continue_process(proc->pid);
Petr Machataa9d6a672013-03-05 17:10:37 +0100918 } else if (process_install_stopping_handler
919 (proc, sbp, NULL, NULL, NULL) < 0) {
920 perror("process_stopping_handler_create");
921 /* Carry on not bothering to re-enable. */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200922 continue_process(proc->pid);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100923 }
924}
925
Petr Machata602330f2011-07-09 11:15:34 +0200926/**
927 * Ltrace exit. When we are about to exit, we have to go through all
928 * the processes, stop them all, remove all the breakpoints, and then
929 * detach the processes that we attached to using -p. If we left the
930 * other tasks running, they might hit stray return breakpoints and
931 * produce artifacts, so we better stop everyone, even if it's a bit
932 * of extra work.
933 */
934struct ltrace_exiting_handler
935{
Petr Machata366c2f42012-02-09 19:34:36 +0100936 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200937 struct pid_set pids;
938};
939
Petr Machata602330f2011-07-09 11:15:34 +0200940static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100941ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200942{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200943 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100944 struct process *task = event->proc;
945 struct process *leader = task->leader;
Petr Machata602330f2011-07-09 11:15:34 +0200946
Petr Machata9ace5742012-04-14 02:29:57 +0200947 debug(DEBUG_PROCESS,
948 "ltrace_exiting_on_event: pid %d; event type %d",
949 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200950
Petr Machata6c2e57a2012-10-27 01:50:22 +0200951 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata602330f2011-07-09 11:15:34 +0200952 handle_stopping_event(task_info, &event);
953
Petr Machata590c8082011-08-20 22:45:26 +0200954 if (event != NULL && event->type == EVENT_BREAKPOINT)
955 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200956
957 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200958 && all_stops_accountable(&self->pids))
959 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200960
961 /* Sink all non-exit events. We are about to exit, so we
962 * don't bother with queuing them. */
963 if (event_exit_or_none_p(event))
964 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200965
Petr Machata13d5df72011-08-19 23:15:15 +0200966 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200967}
968
969static void
Petr Machata366c2f42012-02-09 19:34:36 +0100970ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200971{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200972 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata602330f2011-07-09 11:15:34 +0200973 free(self->pids.tasks);
974}
975
976static int
Petr Machata929bd572012-12-17 03:20:34 +0100977ltrace_exiting_install_handler(struct process *proc)
Petr Machata602330f2011-07-09 11:15:34 +0200978{
979 /* Only install to leader. */
980 if (proc->leader != proc)
981 return 0;
982
983 /* Perhaps we are already installed, if the user passed
984 * several -p options that are tasks of one process. */
985 if (proc->event_handler != NULL
986 && proc->event_handler->on_event == &ltrace_exiting_on_event)
987 return 0;
988
Petr Machata590c8082011-08-20 22:45:26 +0200989 /* If stopping handler is already present, let it do the
990 * work. */
991 if (proc->event_handler != NULL) {
992 assert(proc->event_handler->on_event
993 == &process_stopping_on_event);
Petr Machata6c2e57a2012-10-27 01:50:22 +0200994 struct process_stopping_handler *other
Petr Machata590c8082011-08-20 22:45:26 +0200995 = (void *)proc->event_handler;
996 other->exiting = 1;
997 return 0;
998 }
999
Petr Machata6c2e57a2012-10-27 01:50:22 +02001000 struct ltrace_exiting_handler *handler
Petr Machata602330f2011-07-09 11:15:34 +02001001 = calloc(sizeof(*handler), 1);
1002 if (handler == NULL) {
1003 perror("malloc exiting handler");
1004 fatal:
1005 /* XXXXXXXXXXXXXXXXXXX fixme */
1006 return -1;
1007 }
1008
Petr Machata602330f2011-07-09 11:15:34 +02001009 handler->super.on_event = ltrace_exiting_on_event;
1010 handler->super.destroy = ltrace_exiting_destroy;
1011 install_event_handler(proc->leader, &handler->super);
1012
Petr Machata74132a42012-03-16 02:46:18 +01001013 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001014 &handler->pids) != NULL)
1015 goto fatal;
1016
1017 return 0;
1018}
1019
Petr Machatacbe29c62011-09-27 02:27:58 +02001020/*
1021 * When the traced process vforks, it's suspended until the child
1022 * process calls _exit or exec*. In the meantime, the two share the
1023 * address space.
1024 *
1025 * The child process should only ever call _exit or exec*, but we
1026 * can't count on that (it's not the role of ltrace to policy, but to
1027 * observe). In any case, we will _at least_ have to deal with
1028 * removal of vfork return breakpoint (which we have to smuggle back
1029 * in, so that the parent can see it, too), and introduction of exec*
1030 * return breakpoint. Since we already have both breakpoint actions
1031 * to deal with, we might as well support it all.
1032 *
1033 * The gist is that we pretend that the child is in a thread group
1034 * with its parent, and handle it as a multi-threaded case, with the
1035 * exception that we know that the parent is blocked, and don't
1036 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001037 */
1038
Petr Machata134a1082011-09-27 20:25:58 +02001039struct process_vfork_handler
1040{
Petr Machata366c2f42012-02-09 19:34:36 +01001041 struct event_handler super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001042 int vfork_bp_refd:1;
Petr Machata134a1082011-09-27 20:25:58 +02001043};
1044
Petr Machatacbe29c62011-09-27 02:27:58 +02001045static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001046process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001047{
Petr Machata9ace5742012-04-14 02:29:57 +02001048 debug(DEBUG_PROCESS,
1049 "process_vfork_on_event: pid %d; event type %d",
1050 event->proc->pid, event->type);
1051
Petr Machata6c2e57a2012-10-27 01:50:22 +02001052 struct process_vfork_handler *self = (void *)super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001053 struct process *proc = event->proc;
Petr Machatacbe29c62011-09-27 02:27:58 +02001054 assert(self != NULL);
1055
1056 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001057 case EVENT_BREAKPOINT:
Petr Machata7fc98ee2013-01-29 18:01:32 +01001058 /* We turn on the vfork return breakpoint (which
1059 * should be the one that we have tripped over just
1060 * now) one extra time, so that the vfork parent hits
1061 * it as well. */
1062 if (!self->vfork_bp_refd) {
1063 struct breakpoint *sbp = NULL;
1064 DICT_FIND_VAL(proc->leader->breakpoints,
1065 &event->e_un.brk_addr, &sbp);
1066 assert(sbp != NULL);
1067 breakpoint_turn_on(sbp, proc->leader);
1068 self->vfork_bp_refd = 1;
1069 }
Petr Machata134a1082011-09-27 20:25:58 +02001070 break;
1071
Petr Machatacbe29c62011-09-27 02:27:58 +02001072 case EVENT_EXIT:
1073 case EVENT_EXIT_SIGNAL:
1074 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001075 /* Remove the leader that we artificially set up
1076 * earlier. */
Petr Machata7fc98ee2013-01-29 18:01:32 +01001077 change_process_leader(proc, proc);
1078 destroy_event_handler(proc);
1079 continue_process(proc->parent->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +02001080
Petr Machatacbe29c62011-09-27 02:27:58 +02001081 default:
1082 ;
1083 }
1084
1085 return event;
1086}
1087
1088void
Petr Machata929bd572012-12-17 03:20:34 +01001089continue_after_vfork(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +02001090{
1091 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata6c2e57a2012-10-27 01:50:22 +02001092 struct process_vfork_handler *handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001093 if (handler == NULL) {
1094 perror("malloc vfork handler");
1095 /* Carry on not bothering to treat the process as
1096 * necessary. */
1097 continue_process(proc->parent->pid);
1098 return;
1099 }
1100
1101 /* We must set up custom event handler, so that we see
1102 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001103 handler->super.on_event = process_vfork_on_event;
1104 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001105
1106 /* Make sure that the child is sole thread. */
1107 assert(proc->leader == proc);
1108 assert(proc->next == NULL || proc->next->leader != proc);
1109
1110 /* Make sure that the child's parent is properly set up. */
1111 assert(proc->parent != NULL);
1112 assert(proc->parent->leader != NULL);
1113
1114 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001115}
1116
Petr Machata9d29b3e2011-11-09 16:46:56 +01001117static int
Petr Machata929bd572012-12-17 03:20:34 +01001118is_mid_stopping(struct process *proc)
Petr Machata9d29b3e2011-11-09 16:46:56 +01001119{
1120 return proc != NULL
1121 && proc->event_handler != NULL
1122 && proc->event_handler->on_event == &process_stopping_on_event;
1123}
1124
Petr Machata43d2fe52011-11-02 13:25:49 +01001125void
Petr Machata929bd572012-12-17 03:20:34 +01001126continue_after_syscall(struct process *proc, int sysnum, int ret_p)
Petr Machata43d2fe52011-11-02 13:25:49 +01001127{
1128 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001129 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1130 debug(DEBUG_PROCESS,
1131 "continue_after_syscall: don't continue %d",
1132 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001133 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001134 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001135 continue_process(proc->pid);
1136}
1137
Petr Machata057caa52013-01-30 23:28:47 +01001138void
1139continue_after_exec(struct process *proc)
1140{
1141 continue_process(proc->pid);
1142
1143 /* After the exec, we expect to hit the first executable
1144 * instruction.
1145 *
1146 * XXX TODO It would be nice to have this removed, but then we
1147 * need to do that also for initial call to wait_for_proc in
1148 * execute_program. In that case we could generate a
1149 * EVENT_FIRST event or something, or maybe this could somehow
1150 * be rolled into EVENT_NEW. */
1151 wait_for_proc(proc->pid);
1152 continue_process(proc->pid);
1153}
1154
Petr Machata602330f2011-07-09 11:15:34 +02001155/* If ltrace gets SIGINT, the processes directly or indirectly run by
1156 * ltrace get it too. We just have to wait long enough for the signal
1157 * to be delivered and the process terminated, which we notice and
1158 * exit ltrace, too. So there's not much we need to do there. We
1159 * want to keep tracing those processes as usual, in case they just
1160 * SIG_IGN the SIGINT to do their shutdown etc.
1161 *
1162 * For processes ran on the background, we want to install an exit
1163 * handler that stops all the threads, removes all breakpoints, and
1164 * detaches.
1165 */
1166void
Petr Machataffe4cd22012-04-11 18:01:44 +02001167os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001168{
Petr Machata6c2e57a2012-10-27 01:50:22 +02001169 struct opt_p_t *it;
Petr Machata602330f2011-07-09 11:15:34 +02001170 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +01001171 struct process *proc = pid2proc(it->pid);
Petr Machata602330f2011-07-09 11:15:34 +02001172 if (proc == NULL || proc->leader == NULL)
1173 continue;
1174 if (ltrace_exiting_install_handler(proc->leader) < 0)
1175 fprintf(stderr,
1176 "Couldn't install exiting handler for %d.\n",
1177 proc->pid);
1178 }
1179}
1180
Petr Machataffe4cd22012-04-11 18:01:44 +02001181int
1182os_ltrace_exiting_sighandler(void)
1183{
1184 extern int linux_in_waitpid;
1185 if (linux_in_waitpid) {
1186 os_ltrace_exiting();
1187 return 1;
1188 }
1189 return 0;
1190}
1191
Joe Damatodfa3fa32010-11-08 15:47:35 -08001192size_t
Petr Machata929bd572012-12-17 03:20:34 +01001193umovebytes(struct process *proc, void *addr, void *laddr, size_t len)
1194{
Joe Damatodfa3fa32010-11-08 15:47:35 -08001195
1196 union {
1197 long a;
1198 char c[sizeof(long)];
1199 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001200 int started = 0;
1201 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001202
1203 while (offset < len) {
1204 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1205 if (a.a == -1 && errno) {
1206 if (started && errno == EIO)
1207 return bytes_read;
1208 else
1209 return -1;
1210 }
1211 started = 1;
1212
1213 if (len - offset >= sizeof(long)) {
1214 memcpy(laddr + offset, &a.c[0], sizeof(long));
1215 bytes_read += sizeof(long);
1216 }
1217 else {
1218 memcpy(laddr + offset, &a.c[0], len - offset);
1219 bytes_read += (len - offset);
1220 }
1221 offset += sizeof(long);
1222 }
1223
1224 return bytes_read;
1225}
Petr Machatab420a222013-10-15 10:46:28 +02001226
1227struct irelative_name_data_t {
1228 GElf_Addr addr;
1229 const char *found_name;
1230};
1231
1232static enum callback_status
1233irelative_name_cb(GElf_Sym *symbol, const char *name, void *d)
1234{
1235 struct irelative_name_data_t *data = d;
1236
1237 if (symbol->st_value == data->addr) {
1238 bool is_ifunc = false;
1239#ifdef STT_GNU_IFUNC
1240 is_ifunc = GELF_ST_TYPE(symbol->st_info) == STT_GNU_IFUNC;
1241#endif
1242 data->found_name = name;
1243
1244 /* Keep looking, unless we found the actual IFUNC
1245 * symbol. What we matched may have been a symbol
1246 * denoting the resolver function, which would have
1247 * the same address. */
1248 return CBS_STOP_IF(is_ifunc);
1249 }
1250
1251 return CBS_CONT;
1252}
1253
1254enum plt_status
1255linux_elf_add_plt_entry_irelative(struct process *proc, struct ltelf *lte,
1256 GElf_Rela *rela, size_t ndx,
1257 struct library_symbol **ret)
1258
1259{
1260 struct irelative_name_data_t data = { rela->r_addend, NULL };
1261 if (rela->r_addend != 0
1262 && elf_each_symbol(lte, 0,
1263 irelative_name_cb, &data).status < 0)
1264 return -1;
1265
1266 const char *name;
1267 if (data.found_name != NULL) {
1268 name = data.found_name;
1269 } else {
1270#define NAME "IREL."
1271 /* NAME\0 + 0x + digits. */
1272 char *tmp_name = alloca(sizeof NAME + 2 + 16);
1273 sprintf(tmp_name, NAME "%#" PRIx64,
1274 (uint64_t)rela->r_addend);
1275 name = tmp_name;
1276#undef NAME
1277 }
1278
1279 if (default_elf_add_plt_entry(proc, lte, name, rela, ndx, ret) < 0)
1280 return PLT_FAIL;
1281
1282 return PLT_OK;
1283}