blob: 6c3258e2fdeb6aa3f1258d63f3b5558137edcc86 [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>
Petr Machata88070a82013-10-24 14:43:44 +020030#include <inttypes.h>
Petr Machatab420a222013-10-15 10:46:28 +020031#include <stdbool.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010032#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010033#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010034#include <string.h>
Petr Machatab420a222013-10-15 10:46:28 +020035#include <sys/types.h>
36#include <sys/wait.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010037#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010038
Petr Machatacec06ec2012-04-10 13:31:55 +020039#ifdef HAVE_LIBSELINUX
40# include <selinux/selinux.h>
41#endif
42
Petr Machataaf766ab2012-11-07 21:59:13 +010043#include "linux-gnu/trace-defs.h"
Petr Machatab420a222013-10-15 10:46:28 +020044#include "linux-gnu/trace.h"
Petr Machata64262602012-01-07 03:41:36 +010045#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020046#include "breakpoint.h"
47#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010048#include "events.h"
Petr Machatab420a222013-10-15 10:46:28 +020049#include "ltrace-elf.h"
Petr Machataba1664b2012-04-28 14:59:05 +020050#include "options.h"
51#include "proc.h"
52#include "ptrace.h"
53#include "type.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020054
Juan Cespedesf1350522008-12-16 18:19:58 +010055void
Petr Machatacec06ec2012-04-10 13:31:55 +020056trace_fail_warning(pid_t pid)
57{
58 /* This was adapted from GDB. */
59#ifdef HAVE_LIBSELINUX
60 static int checked = 0;
61 if (checked)
62 return;
63 checked = 1;
64
65 /* -1 is returned for errors, 0 if it has no effect, 1 if
66 * PTRACE_ATTACH is forbidden. */
67 if (security_get_boolean_active("deny_ptrace") == 1)
68 fprintf(stderr,
69"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
70"tracing other processes. You can disable this process attach protection by\n"
71"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
72#endif /* HAVE_LIBSELINUX */
73}
74
75void
76trace_me(void)
77{
Petr Machata26627682011-07-08 18:15:32 +020078 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020079 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010080 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +020081 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +010082 exit(1);
83 }
84}
85
Petr Machatab4f9e0c2012-02-07 01:57:59 +010086/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +010087 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +010088 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +010089int
Petr Machatab4f9e0c2012-02-07 01:57:59 +010090wait_for_proc(pid_t pid)
91{
Petr Machatac805c622012-03-02 00:10:37 +010092 /* man ptrace: PTRACE_ATTACH attaches to the process specified
93 in pid. The child is sent a SIGSTOP, but will not
94 necessarily have stopped by the completion of this call;
95 use wait() to wait for the child to stop. */
96 if (waitpid(pid, NULL, __WALL) != pid) {
97 perror ("trace_pid: waitpid");
98 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010099 }
100
Petr Machatac805c622012-03-02 00:10:37 +0100101 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100102}
103
Juan Cespedesf1350522008-12-16 18:19:58 +0100104int
Petr Machatacec06ec2012-04-10 13:31:55 +0200105trace_pid(pid_t pid)
106{
Petr Machata26627682011-07-08 18:15:32 +0200107 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200108 /* This shouldn't emit error messages, as there are legitimate
109 * reasons that the PID can't be attached: like it may have
110 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200111 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100112 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100113
Petr Machatac805c622012-03-02 00:10:37 +0100114 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100115}
116
Juan Cespedesf1350522008-12-16 18:19:58 +0100117void
Petr Machata929bd572012-12-17 03:20:34 +0100118trace_set_options(struct process *proc)
Petr Machata3ed2a422012-04-06 17:18:55 +0200119{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100120 if (proc->tracesysgood & 0x80)
121 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200122
Petr Machata3ed2a422012-04-06 17:18:55 +0200123 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200124 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200125
Juan Cespedes1e583132009-04-07 18:17:11 +0200126 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
127 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
128 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200129 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
130 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100131 perror("PTRACE_SETOPTIONS");
132 return;
133 }
134 proc->tracesysgood |= 0x80;
135}
136
Juan Cespedesf1350522008-12-16 18:19:58 +0100137void
138untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200139 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200140 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100141}
142
Juan Cespedesf1350522008-12-16 18:19:58 +0100143void
Petr Machatac897cb72012-05-05 01:26:58 +0200144continue_after_signal(pid_t pid, int signum)
145{
146 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
147 pid, signum);
148 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200149}
150
151static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200152event_for_pid(Event *event, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200153{
154 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
Petr Machata71f25e22012-12-17 04:01:23 +0100155 return ECB_YIELD;
156 return ECB_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200157}
158
159static int
160have_events_for(pid_t pid)
161{
162 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
163}
164
165void
166continue_process(pid_t pid)
167{
168 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200169
170 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200171 the queue for this process. Otherwise just wait for the
172 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200173 if (!have_events_for(pid))
174 /* We always trace syscalls to control fork(),
175 * clone(), execve()... */
176 ptrace(PTRACE_SYSCALL, pid, 0, 0);
177 else
178 debug(DEBUG_PROCESS,
179 "putting off the continue, events in que.");
180}
181
Petr Machata98f09922011-07-09 10:55:29 +0200182static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200183get_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200184{
Petr Machata750ca8c2011-10-06 14:29:34 +0200185 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200186 size_t i;
187 for (i = 0; i < pids->count; ++i)
188 if (pids->tasks[i].pid == pid)
189 return &pids->tasks[i];
190
191 return NULL;
192}
193
194static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200195add_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200196{
197 if (pids->count == pids->alloc) {
198 size_t ns = (2 * pids->alloc) ?: 4;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200199 struct pid_task *n = realloc(pids->tasks,
200 sizeof(*pids->tasks) * ns);
Petr Machata98f09922011-07-09 10:55:29 +0200201 if (n == NULL)
202 return NULL;
203 pids->tasks = n;
204 pids->alloc = ns;
205 }
206 struct pid_task * task_info = &pids->tasks[pids->count++];
207 memset(task_info, 0, sizeof(*task_info));
208 task_info->pid = pid;
209 return task_info;
210}
211
Petr Machata2b46cfc2012-02-18 11:17:29 +0100212static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100213task_stopped(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200214{
215 enum process_status st = process_status(task->pid);
216 if (data != NULL)
217 *(enum process_status *)data = st;
218
219 /* If the task is already stopped, don't worry about it.
220 * Likewise if it managed to become a zombie or terminate in
221 * the meantime. This can happen when the whole thread group
222 * is terminating. */
223 switch (st) {
Petr Machata6dfc5442012-12-17 03:38:36 +0100224 case PS_INVALID:
225 case PS_TRACING_STOP:
226 case PS_ZOMBIE:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227 return CBS_CONT;
Petr Machata6dfc5442012-12-17 03:38:36 +0100228 case PS_SLEEPING:
229 case PS_STOP:
230 case PS_OTHER:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200232 }
Petr Machata36d19822011-10-21 16:03:45 +0200233
234 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200235}
236
237/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100238static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100239task_blocked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200240{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200241 struct pid_set *pids = data;
242 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +0200243 if (task_info != NULL
244 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200246
247 return task_stopped(task, NULL);
248}
249
Petr Machata366c2f42012-02-09 19:34:36 +0100250static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100253task_vforked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200254{
255 if (task->event_handler != NULL
256 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257 return CBS_STOP;
258 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200259}
260
261static int
Petr Machata929bd572012-12-17 03:20:34 +0100262is_vfork_parent(struct process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200263{
Petr Machata74132a42012-03-16 02:46:18 +0100264 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200265}
266
Petr Machata2b46cfc2012-02-18 11:17:29 +0100267static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100268send_sigstop(struct process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200269{
Petr Machata929bd572012-12-17 03:20:34 +0100270 struct process *leader = task->leader;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200271 struct pid_set *pids = data;
Petr Machata98f09922011-07-09 10:55:29 +0200272
273 /* Look for pre-existing task record, or add new. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200274 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200275 if (task_info == NULL)
276 task_info = add_task_info(pids, task->pid);
277 if (task_info == NULL) {
278 perror("send_sigstop: add_task_info");
279 destroy_event_handler(leader);
280 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100281 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200282 }
283
284 /* This task still has not been attached to. It should be
285 stopped by the kernel. */
286 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200288
289 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200290 * if we sent the SIGSTOP already, which happens when we are
291 * handling "onexit" and inherited the handler from breakpoint
292 * re-enablement. */
293 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294 if (task_stopped(task, &st) == CBS_CONT)
295 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200296 if (task_info->sigstopped) {
297 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200299 task_info->delivered = 0;
300 }
301
Petr Machatacbe29c62011-09-27 02:27:58 +0200302 /* Also don't attempt to stop the process if it's a parent of
303 * vforked process. We set up event handler specially to hint
304 * us. In that case parent is in D state, which we use to
305 * weed out unnecessary looping. */
Petr Machata6dfc5442012-12-17 03:38:36 +0100306 if (st == PS_SLEEPING
307 && is_vfork_parent(task)) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200308 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200310 }
311
Petr Machata98f09922011-07-09 10:55:29 +0200312 if (task_kill(task->pid, SIGSTOP) >= 0) {
313 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
314 task_info->sigstopped = 1;
315 } else
316 fprintf(stderr,
317 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
318
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200320}
321
Petr Machata73894bd2011-08-20 23:47:34 +0200322/* On certain kernels, detaching right after a singlestep causes the
323 tracee to be killed with a SIGTRAP (that even though the singlestep
324 was properly caught by waitpid. The ugly workaround is to put a
325 breakpoint where IP points and let the process continue. After
326 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200327static void
Petr Machata929bd572012-12-17 03:20:34 +0100328ugly_workaround(struct process *proc)
Petr Machata590c8082011-08-20 22:45:26 +0200329{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100330 arch_addr_t ip = get_instruction_pointer(proc);
Petr Machata98ff3092013-03-08 22:11:36 +0100331 struct breakpoint *found;
332 if (DICT_FIND_VAL(proc->leader->breakpoints, &ip, &found) < 0) {
Petr Machata02a796e2013-10-11 17:24:30 +0200333 insert_breakpoint_at(proc, ip, NULL);
Petr Machata98ff3092013-03-08 22:11:36 +0100334 } else {
335 assert(found != NULL);
336 enable_breakpoint(proc, found);
337 }
Petr Machata73894bd2011-08-20 23:47:34 +0200338 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200339}
340
341static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200342process_stopping_done(struct process_stopping_handler *self,
Petr Machata929bd572012-12-17 03:20:34 +0100343 struct process *leader)
Petr Machata98f09922011-07-09 10:55:29 +0200344{
345 debug(DEBUG_PROCESS, "process stopping done %d",
346 self->task_enabling_breakpoint->pid);
Petr Machata8ae36732012-04-30 01:19:09 +0200347
Petr Machata590c8082011-08-20 22:45:26 +0200348 if (!self->exiting) {
Petr Machata8ae36732012-04-30 01:19:09 +0200349 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200350 for (i = 0; i < self->pids.count; ++i)
351 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100352 && (self->pids.tasks[i].delivered
353 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200354 continue_process(self->pids.tasks[i].pid);
355 continue_process(self->task_enabling_breakpoint->pid);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200356 }
357
358 if (self->exiting) {
359 ugly_workaround:
Petr Machata96cb8e32012-12-17 03:49:41 +0100360 self->state = PSH_UGLY_WORKAROUND;
Petr Machata73894bd2011-08-20 23:47:34 +0200361 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200362 } else {
363 switch ((self->ugly_workaround_p)(self)) {
364 case CBS_FAIL:
365 /* xxx handle me */
366 case CBS_STOP:
367 break;
368 case CBS_CONT:
369 goto ugly_workaround;
370 }
Petr Machata8ae36732012-04-30 01:19:09 +0200371 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200372 }
373}
374
375/* Before we detach, we need to make sure that task's IP is on the
376 * edge of an instruction. So for tasks that have a breakpoint event
377 * in the queue, we adjust the instruction pointer, just like
378 * continue_after_breakpoint does. */
379static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200380undo_breakpoint(Event *event, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200381{
382 if (event != NULL
383 && event->proc->leader == data
384 && event->type == EVENT_BREAKPOINT)
385 set_instruction_pointer(event->proc, event->e_un.brk_addr);
Petr Machata71f25e22012-12-17 04:01:23 +0100386 return ECB_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200387}
388
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100390untrace_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200391{
392 if (task != data)
393 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200395}
396
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100398remove_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200399{
400 /* Don't untrace leader just yet. */
401 if (task != data)
402 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100403 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200404}
405
Petr Machata86d38282012-04-24 18:09:01 +0200406static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100407retract_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata86d38282012-04-24 18:09:01 +0200408{
409 breakpoint_on_retract(bp, proc);
410 return CBS_CONT;
411}
412
Petr Machata590c8082011-08-20 22:45:26 +0200413static void
Petr Machata929bd572012-12-17 03:20:34 +0100414detach_process(struct process *leader)
Petr Machata590c8082011-08-20 22:45:26 +0200415{
416 each_qd_event(&undo_breakpoint, leader);
417 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200418 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200419
420 /* Now untrace the process, if it was attached to by -p. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200421 struct opt_p_t *it;
Petr Machata590c8082011-08-20 22:45:26 +0200422 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +0100423 struct process *proc = pid2proc(it->pid);
Petr Machata590c8082011-08-20 22:45:26 +0200424 if (proc == NULL)
425 continue;
426 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100427 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200428 break;
429 }
430 }
Petr Machata74132a42012-03-16 02:46:18 +0100431 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200432 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200433 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200434}
435
436static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200437handle_stopping_event(struct pid_task *task_info, Event **eventp)
Petr Machata98f09922011-07-09 10:55:29 +0200438{
439 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200440 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200441 task_info->got_event = 1;
442
Petr Machata6c2e57a2012-10-27 01:50:22 +0200443 Event *event = *eventp;
Petr Machata98f09922011-07-09 10:55:29 +0200444
445 /* In every state, sink SIGSTOP events for tasks that it was
446 * sent to. */
447 if (task_info != NULL
448 && event->type == EVENT_SIGNAL
449 && event->e_un.signum == SIGSTOP) {
450 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
451 if (task_info->sigstopped
452 && !task_info->delivered) {
453 task_info->delivered = 1;
454 *eventp = NULL; // sink the event
455 } else
456 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
457 "sigstopped=%d and delivered=%d\n",
458 task_info->pid, task_info->sigstopped,
459 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100460 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100461}
462
Petr Machata98f09922011-07-09 10:55:29 +0200463/* Some SIGSTOPs may have not been delivered to their respective tasks
464 * yet. They are still in the queue. If we have seen an event for
465 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200466 * caught by ltrace. We don't mind that the process is after
467 * breakpoint (and therefore potentially doesn't have aligned IP),
468 * because the signal will be delivered without the process actually
469 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200470static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200471continue_for_sigstop_delivery(struct pid_set *pids)
Petr Machata98f09922011-07-09 10:55:29 +0200472{
473 size_t i;
474 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200475 if (pids->tasks[i].pid != 0
476 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200477 && !pids->tasks[i].delivered
478 && pids->tasks[i].got_event) {
479 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
480 pids->tasks[i].pid);
481 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
482 }
483 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100484}
485
Petr Machata98f09922011-07-09 10:55:29 +0200486static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200487event_exit_p(Event *event)
Petr Machata750ca8c2011-10-06 14:29:34 +0200488{
489 return event != NULL && (event->type == EVENT_EXIT
490 || event->type == EVENT_EXIT_SIGNAL);
491}
492
493static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200494event_exit_or_none_p(Event *event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200495{
Petr Machata750ca8c2011-10-06 14:29:34 +0200496 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200497 || event->type == EVENT_NONE;
498}
499
500static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200501await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
502 Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200503{
504 /* If we still didn't get our SIGSTOP, continue the process
505 * and carry on. */
506 if (event != NULL && !event_exit_or_none_p(event)
507 && task_info != NULL && task_info->sigstopped) {
508 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
509 task_info->pid);
510 /* We should get the signal the first thing
511 * after this, so it should be OK to continue
512 * even if we are over a breakpoint. */
513 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
514
515 } else {
516 /* If all SIGSTOPs were delivered, uninstall the
517 * handler and continue everyone. */
518 /* XXX I suspect that we should check tasks that are
519 * still around. Is things are now, there should be a
520 * race between waiting for everyone to stop and one
521 * of the tasks exiting. */
522 int all_clear = 1;
523 size_t i;
524 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200525 if (pids->tasks[i].pid != 0
526 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200527 && !pids->tasks[i].delivered) {
528 all_clear = 0;
529 break;
530 }
531 return all_clear;
532 }
533
534 return 0;
535}
536
Petr Machata590c8082011-08-20 22:45:26 +0200537static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200538all_stops_accountable(struct pid_set *pids)
Petr Machata590c8082011-08-20 22:45:26 +0200539{
540 size_t i;
541 for (i = 0; i < pids->count; ++i)
542 if (pids->tasks[i].pid != 0
543 && !pids->tasks[i].got_event
544 && !have_events_for(pids->tasks[i].pid))
545 return 0;
546 return 1;
547}
548
Petr Machata693dfad2013-01-14 22:10:51 +0100549#ifndef ARCH_HAVE_SW_SINGLESTEP
550enum sw_singlestep_status
551arch_sw_singlestep(struct process *proc, struct breakpoint *bp,
552 int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
553 struct sw_singlestep_data *data)
Petr Machata06986d52011-11-02 13:22:46 +0100554{
Petr Machata693dfad2013-01-14 22:10:51 +0100555 return SWS_HW;
Petr Machataa266acb2012-04-12 23:50:23 +0200556}
557#endif
558
Petr Machata42748ac2012-04-19 04:24:25 +0200559static Event *process_stopping_on_event(struct event_handler *super,
560 Event *event);
561
562static void
Petr Machata693dfad2013-01-14 22:10:51 +0100563remove_sw_breakpoints(struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200564{
Petr Machata24a47042012-04-19 18:15:08 +0200565 struct process_stopping_handler *self
566 = (void *)proc->leader->event_handler;
567 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200568 assert(self->super.on_event == process_stopping_on_event);
569
Petr Machata6e5e2de2013-01-21 22:25:34 +0100570 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200571 int i;
572 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100573 if (self->sws_bps[i] != NULL) {
Petr Machatab9440772013-10-14 10:36:26 +0200574 delete_breakpoint_at(proc, self->sws_bps[i]->addr);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100575 self->sws_bps[i] = NULL;
Petr Machata42748ac2012-04-19 04:24:25 +0200576 }
577}
578
579static void
Petr Machata693dfad2013-01-14 22:10:51 +0100580sw_singlestep_bp_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200581{
Petr Machata693dfad2013-01-14 22:10:51 +0100582 remove_sw_breakpoints(proc);
Petr Machata42748ac2012-04-19 04:24:25 +0200583}
584
Petr Machata693dfad2013-01-14 22:10:51 +0100585struct sw_singlestep_data {
586 struct process_stopping_handler *self;
587};
588
Petr Machataa266acb2012-04-12 23:50:23 +0200589static int
Petr Machata693dfad2013-01-14 22:10:51 +0100590sw_singlestep_add_bp(arch_addr_t addr, struct sw_singlestep_data *data)
Petr Machataa266acb2012-04-12 23:50:23 +0200591{
Petr Machata693dfad2013-01-14 22:10:51 +0100592 struct process_stopping_handler *self = data->self;
Petr Machata929bd572012-12-17 03:20:34 +0100593 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200594
Petr Machata6e5e2de2013-01-21 22:25:34 +0100595 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200596 int i;
597 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100598 if (self->sws_bps[i] == NULL) {
Petr Machata42748ac2012-04-19 04:24:25 +0200599 static struct bp_callbacks cbs = {
Petr Machata693dfad2013-01-14 22:10:51 +0100600 .on_hit = sw_singlestep_bp_on_hit,
Petr Machata42748ac2012-04-19 04:24:25 +0200601 };
602 struct breakpoint *bp
Petr Machata02a796e2013-10-11 17:24:30 +0200603 = insert_breakpoint_at(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200604 breakpoint_set_callbacks(bp, &cbs);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100605 self->sws_bps[i] = bp;
Petr Machata42748ac2012-04-19 04:24:25 +0200606 return 0;
607 }
Petr Machataa266acb2012-04-12 23:50:23 +0200608
Petr Machata693dfad2013-01-14 22:10:51 +0100609 assert(!"Too many sw singlestep breakpoints!");
Petr Machata42748ac2012-04-19 04:24:25 +0200610 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200611}
612
613static int
614singlestep(struct process_stopping_handler *self)
615{
Petr Machata6e5e2de2013-01-21 22:25:34 +0100616 size_t i;
617 for (i = 0; i < sizeof(self->sws_bps) / sizeof(*self->sws_bps); ++i)
618 self->sws_bps[i] = NULL;
Petr Machataa266acb2012-04-12 23:50:23 +0200619
Petr Machata693dfad2013-01-14 22:10:51 +0100620 struct sw_singlestep_data data = { self };
Petr Machata643c6462013-01-15 17:11:43 +0100621 switch (arch_sw_singlestep(self->task_enabling_breakpoint,
622 self->breakpoint_being_enabled,
623 &sw_singlestep_add_bp, &data)) {
624 case SWS_HW:
625 /* Otherwise do the default action: singlestep. */
626 debug(1, "PTRACE_SINGLESTEP");
Petr Machata6e5e2de2013-01-21 22:25:34 +0100627 if (ptrace(PTRACE_SINGLESTEP,
628 self->task_enabling_breakpoint->pid, 0, 0)) {
Petr Machata643c6462013-01-15 17:11:43 +0100629 perror("PTRACE_SINGLESTEP");
630 return -1;
631 }
632 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200633
Petr Machata643c6462013-01-15 17:11:43 +0100634 case SWS_OK:
635 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200636
Petr Machata643c6462013-01-15 17:11:43 +0100637 case SWS_FAIL:
Petr Machataa266acb2012-04-12 23:50:23 +0200638 return -1;
639 }
Petr Machata643c6462013-01-15 17:11:43 +0100640 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200641}
642
643static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200644post_singlestep(struct process_stopping_handler *self,
645 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200646{
647 continue_for_sigstop_delivery(&self->pids);
648
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200649 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200650 *eventp = NULL; // handled
651
Petr Machata929bd572012-12-17 03:20:34 +0100652 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200653
Petr Machata693dfad2013-01-14 22:10:51 +0100654 remove_sw_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200655 self->breakpoint_being_enabled = NULL;
656}
657
658static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200659singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200660{
Petr Machata929bd572012-12-17 03:20:34 +0100661 struct process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200662 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200663 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataba1664b2012-04-28 14:59:05 +0200664 teb->pid, breakpoint_name(sbp), sbp->addr,
665 get_instruction_pointer(teb));
Petr Machatab9440772013-10-14 10:36:26 +0200666 delete_breakpoint_at(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100667}
668
Petr Machata9e1e9692012-04-19 02:29:38 +0200669static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200670pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200671{
Petr Machata929bd572012-12-17 03:20:34 +0100672 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200673 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200674 ptrace(PTRACE_CONT, teb->pid, 0, 0);
675}
676
677static void
678pt_singlestep(struct process_stopping_handler *self)
679{
680 if (singlestep(self) < 0)
681 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200682}
683
684static void
685disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200686 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200687{
Petr Machata929bd572012-12-17 03:20:34 +0100688 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200689 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200690 if (self->breakpoint_being_enabled->enabled)
691 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200692 (do_this)(self);
Petr Machata96cb8e32012-12-17 03:49:41 +0100693 self->state = PSH_SINGLESTEP;
Petr Machatac1aa9582012-03-27 23:54:01 +0200694}
695
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200696void
697linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
698{
Petr Machata9e1e9692012-04-19 02:29:38 +0200699 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200700}
701
702void
703linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
704{
705 disable_and(self, &pt_continue);
706}
707
Petr Machata98f09922011-07-09 10:55:29 +0200708/* This event handler is installed when we are in the process of
709 * stopping the whole thread group to do the pointer re-enablement for
710 * one of the threads. We pump all events to the queue for later
711 * processing while we wait for all the threads to stop. When this
712 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
713 * re-enable, and continue everyone. */
714static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100715process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200716{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200717 struct process_stopping_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100718 struct process *task = event->proc;
719 struct process *leader = task->leader;
720 struct process *teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200721
722 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200723 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200724 task->pid, event->type, self->state);
725
Petr Machata6c2e57a2012-10-27 01:50:22 +0200726 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200727 if (task_info == NULL)
728 fprintf(stderr, "new task??? %d\n", task->pid);
729 handle_stopping_event(task_info, &event);
730
731 int state = self->state;
732 int event_to_queue = !event_exit_or_none_p(event);
733
Petr Machata18c97072011-10-06 14:30:11 +0200734 /* Deactivate the entry if the task exits. */
735 if (event_exit_p(event) && task_info != NULL)
736 task_info->pid = 0;
737
Petr Machata43d2fe52011-11-02 13:25:49 +0100738 /* Always handle sysrets. Whether sysret occurred and what
739 * sys it rets from may need to be determined based on process
740 * stack, so we need to keep that in sync with reality. Note
741 * that we don't continue the process after the sysret is
742 * handled. See continue_after_syscall. */
743 if (event != NULL && event->type == EVENT_SYSRET) {
744 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
745 event_to_queue = 0;
Petr Machata754ce882013-01-08 23:41:47 +0100746 if (task_info != NULL)
747 task_info->sysret = 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100748 }
749
Petr Machata98f09922011-07-09 10:55:29 +0200750 switch (state) {
Petr Machata96cb8e32012-12-17 03:49:41 +0100751 case PSH_STOPPING:
Petr Machata98f09922011-07-09 10:55:29 +0200752 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100753 if (each_task(leader, NULL, &task_blocked,
754 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200755 (self->on_all_stopped)(self);
756 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200757 }
758 break;
759
Petr Machata96cb8e32012-12-17 03:49:41 +0100760 case PSH_SINGLESTEP:
Petr Machata98f09922011-07-09 10:55:29 +0200761 /* In singlestep state, breakpoint signifies that we
762 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200763 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200764
Petr Machata42748ac2012-04-19 04:24:25 +0200765 /* If this was caused by a real breakpoint, as
766 * opposed to a singlestep, assume that it's
767 * an artificial breakpoint installed for some
768 * reason for the re-enablement. In that case
769 * handle it. */
770 if (event->type == EVENT_BREAKPOINT) {
Petr Machatabac2da52012-05-29 00:42:59 +0200771 arch_addr_t ip
Petr Machata42748ac2012-04-19 04:24:25 +0200772 = get_instruction_pointer(task);
773 struct breakpoint *other
774 = address2bpstruct(leader, ip);
775 if (other != NULL)
776 breakpoint_on_hit(other, task);
777 }
778
Petr Machata36f40e72012-03-28 02:07:19 +0200779 /* If we got SIGNAL instead of BREAKPOINT,
780 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200781 if (event->type == EVENT_SIGNAL) {
782 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200783 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200784 singlestep_error(self);
785 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200786 goto psh_sinking;
787 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200788 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200789 } else {
790 switch ((self->keep_stepping_p)(self)) {
791 case CBS_FAIL:
792 /* XXX handle me */
793 case CBS_STOP:
794 break;
795 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200796 /* Sink singlestep event. */
797 if (event->type == EVENT_BREAKPOINT)
798 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200799 goto do_singlestep;
800 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200801 }
802
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200803 /* Re-enable the breakpoint that we are
804 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200805 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200806 if (sbp->enabled)
807 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200808
Petr Machataa266acb2012-04-12 23:50:23 +0200809 post_singlestep(self, &event);
810 goto psh_sinking;
811 }
812 break;
Petr Machata98f09922011-07-09 10:55:29 +0200813
Petr Machataa266acb2012-04-12 23:50:23 +0200814 psh_sinking:
Petr Machata96cb8e32012-12-17 03:49:41 +0100815 state = self->state = PSH_SINKING;
816 /* Fall through. */
817 case PSH_SINKING:
Petr Machata98f09922011-07-09 10:55:29 +0200818 if (await_sigstop_delivery(&self->pids, task_info, event))
819 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200820 break;
821
Petr Machata96cb8e32012-12-17 03:49:41 +0100822 case PSH_UGLY_WORKAROUND:
Petr Machata590c8082011-08-20 22:45:26 +0200823 if (event == NULL)
824 break;
825 if (event->type == EVENT_BREAKPOINT) {
826 undo_breakpoint(event, leader);
827 if (task == teb)
828 self->task_enabling_breakpoint = NULL;
829 }
830 if (self->task_enabling_breakpoint == NULL
831 && all_stops_accountable(&self->pids)) {
832 undo_breakpoint(event, leader);
833 detach_process(leader);
834 event = NULL; // handled
835 }
Petr Machata98f09922011-07-09 10:55:29 +0200836 }
837
838 if (event != NULL && event_to_queue) {
839 enque_event(event);
840 event = NULL; // sink the event
841 }
842
843 return event;
844}
845
846static void
Petr Machata366c2f42012-02-09 19:34:36 +0100847process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200848{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200849 struct process_stopping_handler *self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200850 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100851}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100852
Petr Machata36f40e72012-03-28 02:07:19 +0200853static enum callback_status
854no(struct process_stopping_handler *self)
855{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200856 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200857}
858
Petr Machatac1aa9582012-03-27 23:54:01 +0200859int
Petr Machata929bd572012-12-17 03:20:34 +0100860process_install_stopping_handler(struct process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200861 void (*as)(struct process_stopping_handler *),
862 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200863 (struct process_stopping_handler *),
864 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200865 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200866{
Petr Machata9ace5742012-04-14 02:29:57 +0200867 debug(DEBUG_FUNCTION,
868 "process_install_stopping_handler: pid=%d", proc->pid);
869
Petr Machatac1aa9582012-03-27 23:54:01 +0200870 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
871 if (handler == NULL)
872 return -1;
873
Petr Machata36f40e72012-03-28 02:07:19 +0200874 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200875 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200876 if (ks == NULL)
877 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200878 if (uw == NULL)
879 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200880
Petr Machatac1aa9582012-03-27 23:54:01 +0200881 handler->super.on_event = process_stopping_on_event;
882 handler->super.destroy = process_stopping_destroy;
883 handler->task_enabling_breakpoint = proc;
884 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200885 handler->on_all_stopped = as;
886 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200887 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200888
889 install_event_handler(proc->leader, &handler->super);
890
891 if (each_task(proc->leader, NULL, &send_sigstop,
892 &handler->pids) != NULL) {
893 destroy_event_handler(proc);
894 return -1;
895 }
896
897 /* And deliver the first fake event, in case all the
898 * conditions are already fulfilled. */
899 Event ev = {
900 .type = EVENT_NONE,
901 .proc = proc,
902 };
903 process_stopping_on_event(&handler->super, &ev);
904
905 return 0;
906}
907
Juan Cespedesf1350522008-12-16 18:19:58 +0100908void
Petr Machata929bd572012-12-17 03:20:34 +0100909continue_after_breakpoint(struct process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200910{
Petr Machata9ace5742012-04-14 02:29:57 +0200911 debug(DEBUG_PROCESS,
912 "continue_after_breakpoint: pid=%d, addr=%p",
913 proc->pid, sbp->addr);
914
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200915 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200916
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100917 if (sbp->enabled == 0) {
918 continue_process(proc->pid);
Petr Machataa9d6a672013-03-05 17:10:37 +0100919 } else if (process_install_stopping_handler
920 (proc, sbp, NULL, NULL, NULL) < 0) {
921 perror("process_stopping_handler_create");
922 /* Carry on not bothering to re-enable. */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200923 continue_process(proc->pid);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100924 }
925}
926
Petr Machata602330f2011-07-09 11:15:34 +0200927/**
928 * Ltrace exit. When we are about to exit, we have to go through all
929 * the processes, stop them all, remove all the breakpoints, and then
930 * detach the processes that we attached to using -p. If we left the
931 * other tasks running, they might hit stray return breakpoints and
932 * produce artifacts, so we better stop everyone, even if it's a bit
933 * of extra work.
934 */
935struct ltrace_exiting_handler
936{
Petr Machata366c2f42012-02-09 19:34:36 +0100937 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200938 struct pid_set pids;
939};
940
Petr Machata602330f2011-07-09 11:15:34 +0200941static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100942ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200943{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200944 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100945 struct process *task = event->proc;
946 struct process *leader = task->leader;
Petr Machata602330f2011-07-09 11:15:34 +0200947
Petr Machata9ace5742012-04-14 02:29:57 +0200948 debug(DEBUG_PROCESS,
949 "ltrace_exiting_on_event: pid %d; event type %d",
950 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200951
Petr Machata6c2e57a2012-10-27 01:50:22 +0200952 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata602330f2011-07-09 11:15:34 +0200953 handle_stopping_event(task_info, &event);
954
Petr Machata590c8082011-08-20 22:45:26 +0200955 if (event != NULL && event->type == EVENT_BREAKPOINT)
956 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200957
958 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200959 && all_stops_accountable(&self->pids))
960 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200961
962 /* Sink all non-exit events. We are about to exit, so we
963 * don't bother with queuing them. */
964 if (event_exit_or_none_p(event))
965 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200966
Petr Machata13d5df72011-08-19 23:15:15 +0200967 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200968}
969
970static void
Petr Machata366c2f42012-02-09 19:34:36 +0100971ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200972{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200973 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata602330f2011-07-09 11:15:34 +0200974 free(self->pids.tasks);
975}
976
977static int
Petr Machata929bd572012-12-17 03:20:34 +0100978ltrace_exiting_install_handler(struct process *proc)
Petr Machata602330f2011-07-09 11:15:34 +0200979{
980 /* Only install to leader. */
981 if (proc->leader != proc)
982 return 0;
983
984 /* Perhaps we are already installed, if the user passed
985 * several -p options that are tasks of one process. */
986 if (proc->event_handler != NULL
987 && proc->event_handler->on_event == &ltrace_exiting_on_event)
988 return 0;
989
Petr Machata590c8082011-08-20 22:45:26 +0200990 /* If stopping handler is already present, let it do the
991 * work. */
992 if (proc->event_handler != NULL) {
993 assert(proc->event_handler->on_event
994 == &process_stopping_on_event);
Petr Machata6c2e57a2012-10-27 01:50:22 +0200995 struct process_stopping_handler *other
Petr Machata590c8082011-08-20 22:45:26 +0200996 = (void *)proc->event_handler;
997 other->exiting = 1;
998 return 0;
999 }
1000
Petr Machata6c2e57a2012-10-27 01:50:22 +02001001 struct ltrace_exiting_handler *handler
Petr Machata602330f2011-07-09 11:15:34 +02001002 = calloc(sizeof(*handler), 1);
1003 if (handler == NULL) {
1004 perror("malloc exiting handler");
1005 fatal:
1006 /* XXXXXXXXXXXXXXXXXXX fixme */
1007 return -1;
1008 }
1009
Petr Machata602330f2011-07-09 11:15:34 +02001010 handler->super.on_event = ltrace_exiting_on_event;
1011 handler->super.destroy = ltrace_exiting_destroy;
1012 install_event_handler(proc->leader, &handler->super);
1013
Petr Machata74132a42012-03-16 02:46:18 +01001014 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001015 &handler->pids) != NULL)
1016 goto fatal;
1017
1018 return 0;
1019}
1020
Petr Machatacbe29c62011-09-27 02:27:58 +02001021/*
1022 * When the traced process vforks, it's suspended until the child
1023 * process calls _exit or exec*. In the meantime, the two share the
1024 * address space.
1025 *
1026 * The child process should only ever call _exit or exec*, but we
1027 * can't count on that (it's not the role of ltrace to policy, but to
1028 * observe). In any case, we will _at least_ have to deal with
1029 * removal of vfork return breakpoint (which we have to smuggle back
1030 * in, so that the parent can see it, too), and introduction of exec*
1031 * return breakpoint. Since we already have both breakpoint actions
1032 * to deal with, we might as well support it all.
1033 *
1034 * The gist is that we pretend that the child is in a thread group
1035 * with its parent, and handle it as a multi-threaded case, with the
1036 * exception that we know that the parent is blocked, and don't
1037 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001038 */
1039
Petr Machata134a1082011-09-27 20:25:58 +02001040struct process_vfork_handler
1041{
Petr Machata366c2f42012-02-09 19:34:36 +01001042 struct event_handler super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001043 int vfork_bp_refd:1;
Petr Machata134a1082011-09-27 20:25:58 +02001044};
1045
Petr Machatacbe29c62011-09-27 02:27:58 +02001046static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001047process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001048{
Petr Machata9ace5742012-04-14 02:29:57 +02001049 debug(DEBUG_PROCESS,
1050 "process_vfork_on_event: pid %d; event type %d",
1051 event->proc->pid, event->type);
1052
Petr Machata6c2e57a2012-10-27 01:50:22 +02001053 struct process_vfork_handler *self = (void *)super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001054 struct process *proc = event->proc;
Petr Machatacbe29c62011-09-27 02:27:58 +02001055 assert(self != NULL);
1056
1057 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001058 case EVENT_BREAKPOINT:
Petr Machata7fc98ee2013-01-29 18:01:32 +01001059 /* We turn on the vfork return breakpoint (which
1060 * should be the one that we have tripped over just
1061 * now) one extra time, so that the vfork parent hits
1062 * it as well. */
1063 if (!self->vfork_bp_refd) {
1064 struct breakpoint *sbp = NULL;
1065 DICT_FIND_VAL(proc->leader->breakpoints,
1066 &event->e_un.brk_addr, &sbp);
1067 assert(sbp != NULL);
1068 breakpoint_turn_on(sbp, proc->leader);
1069 self->vfork_bp_refd = 1;
1070 }
Petr Machata134a1082011-09-27 20:25:58 +02001071 break;
1072
Petr Machatacbe29c62011-09-27 02:27:58 +02001073 case EVENT_EXIT:
1074 case EVENT_EXIT_SIGNAL:
1075 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001076 /* Remove the leader that we artificially set up
1077 * earlier. */
Petr Machata7fc98ee2013-01-29 18:01:32 +01001078 change_process_leader(proc, proc);
1079 destroy_event_handler(proc);
1080 continue_process(proc->parent->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +02001081
Petr Machatacbe29c62011-09-27 02:27:58 +02001082 default:
1083 ;
1084 }
1085
1086 return event;
1087}
1088
1089void
Petr Machata929bd572012-12-17 03:20:34 +01001090continue_after_vfork(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +02001091{
1092 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata6c2e57a2012-10-27 01:50:22 +02001093 struct process_vfork_handler *handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001094 if (handler == NULL) {
1095 perror("malloc vfork handler");
1096 /* Carry on not bothering to treat the process as
1097 * necessary. */
1098 continue_process(proc->parent->pid);
1099 return;
1100 }
1101
1102 /* We must set up custom event handler, so that we see
1103 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001104 handler->super.on_event = process_vfork_on_event;
1105 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001106
1107 /* Make sure that the child is sole thread. */
1108 assert(proc->leader == proc);
1109 assert(proc->next == NULL || proc->next->leader != proc);
1110
1111 /* Make sure that the child's parent is properly set up. */
1112 assert(proc->parent != NULL);
1113 assert(proc->parent->leader != NULL);
1114
1115 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001116}
1117
Petr Machata9d29b3e2011-11-09 16:46:56 +01001118static int
Petr Machata929bd572012-12-17 03:20:34 +01001119is_mid_stopping(struct process *proc)
Petr Machata9d29b3e2011-11-09 16:46:56 +01001120{
1121 return proc != NULL
1122 && proc->event_handler != NULL
1123 && proc->event_handler->on_event == &process_stopping_on_event;
1124}
1125
Petr Machata43d2fe52011-11-02 13:25:49 +01001126void
Petr Machata929bd572012-12-17 03:20:34 +01001127continue_after_syscall(struct process *proc, int sysnum, int ret_p)
Petr Machata43d2fe52011-11-02 13:25:49 +01001128{
1129 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001130 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1131 debug(DEBUG_PROCESS,
1132 "continue_after_syscall: don't continue %d",
1133 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001134 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001135 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001136 continue_process(proc->pid);
1137}
1138
Petr Machata057caa52013-01-30 23:28:47 +01001139void
1140continue_after_exec(struct process *proc)
1141{
1142 continue_process(proc->pid);
1143
1144 /* After the exec, we expect to hit the first executable
1145 * instruction.
1146 *
1147 * XXX TODO It would be nice to have this removed, but then we
1148 * need to do that also for initial call to wait_for_proc in
1149 * execute_program. In that case we could generate a
1150 * EVENT_FIRST event or something, or maybe this could somehow
1151 * be rolled into EVENT_NEW. */
1152 wait_for_proc(proc->pid);
1153 continue_process(proc->pid);
1154}
1155
Petr Machata602330f2011-07-09 11:15:34 +02001156/* If ltrace gets SIGINT, the processes directly or indirectly run by
1157 * ltrace get it too. We just have to wait long enough for the signal
1158 * to be delivered and the process terminated, which we notice and
1159 * exit ltrace, too. So there's not much we need to do there. We
1160 * want to keep tracing those processes as usual, in case they just
1161 * SIG_IGN the SIGINT to do their shutdown etc.
1162 *
1163 * For processes ran on the background, we want to install an exit
1164 * handler that stops all the threads, removes all breakpoints, and
1165 * detaches.
1166 */
1167void
Petr Machataffe4cd22012-04-11 18:01:44 +02001168os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001169{
Petr Machata6c2e57a2012-10-27 01:50:22 +02001170 struct opt_p_t *it;
Petr Machata602330f2011-07-09 11:15:34 +02001171 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +01001172 struct process *proc = pid2proc(it->pid);
Petr Machata602330f2011-07-09 11:15:34 +02001173 if (proc == NULL || proc->leader == NULL)
1174 continue;
1175 if (ltrace_exiting_install_handler(proc->leader) < 0)
1176 fprintf(stderr,
1177 "Couldn't install exiting handler for %d.\n",
1178 proc->pid);
1179 }
1180}
1181
Petr Machataffe4cd22012-04-11 18:01:44 +02001182int
1183os_ltrace_exiting_sighandler(void)
1184{
1185 extern int linux_in_waitpid;
1186 if (linux_in_waitpid) {
1187 os_ltrace_exiting();
1188 return 1;
1189 }
1190 return 0;
1191}
1192
Joe Damatodfa3fa32010-11-08 15:47:35 -08001193size_t
Petr Machata929bd572012-12-17 03:20:34 +01001194umovebytes(struct process *proc, void *addr, void *laddr, size_t len)
1195{
Joe Damatodfa3fa32010-11-08 15:47:35 -08001196
1197 union {
1198 long a;
1199 char c[sizeof(long)];
1200 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001201 int started = 0;
1202 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001203
1204 while (offset < len) {
1205 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1206 if (a.a == -1 && errno) {
1207 if (started && errno == EIO)
1208 return bytes_read;
1209 else
1210 return -1;
1211 }
1212 started = 1;
1213
1214 if (len - offset >= sizeof(long)) {
1215 memcpy(laddr + offset, &a.c[0], sizeof(long));
1216 bytes_read += sizeof(long);
1217 }
1218 else {
1219 memcpy(laddr + offset, &a.c[0], len - offset);
1220 bytes_read += (len - offset);
1221 }
1222 offset += sizeof(long);
1223 }
1224
1225 return bytes_read;
1226}
Petr Machatab420a222013-10-15 10:46:28 +02001227
1228struct irelative_name_data_t {
1229 GElf_Addr addr;
1230 const char *found_name;
1231};
1232
1233static enum callback_status
1234irelative_name_cb(GElf_Sym *symbol, const char *name, void *d)
1235{
1236 struct irelative_name_data_t *data = d;
1237
1238 if (symbol->st_value == data->addr) {
1239 bool is_ifunc = false;
1240#ifdef STT_GNU_IFUNC
1241 is_ifunc = GELF_ST_TYPE(symbol->st_info) == STT_GNU_IFUNC;
1242#endif
1243 data->found_name = name;
1244
1245 /* Keep looking, unless we found the actual IFUNC
1246 * symbol. What we matched may have been a symbol
1247 * denoting the resolver function, which would have
1248 * the same address. */
1249 return CBS_STOP_IF(is_ifunc);
1250 }
1251
1252 return CBS_CONT;
1253}
1254
Petr Machatab061bae2013-10-25 23:50:18 +02001255char *
Petr Machata54bb64c2013-11-04 20:00:43 +01001256linux_elf_find_irelative_name(struct ltelf *lte, GElf_Addr addr)
Petr Machatab420a222013-10-15 10:46:28 +02001257{
Petr Machata54bb64c2013-11-04 20:00:43 +01001258 struct irelative_name_data_t data = { addr, NULL };
1259 if (addr != 0
Petr Machatab420a222013-10-15 10:46:28 +02001260 && elf_each_symbol(lte, 0,
1261 irelative_name_cb, &data).status < 0)
Petr Machatab061bae2013-10-25 23:50:18 +02001262 return NULL;
Petr Machatab420a222013-10-15 10:46:28 +02001263
1264 const char *name;
1265 if (data.found_name != NULL) {
1266 name = data.found_name;
1267 } else {
1268#define NAME "IREL."
1269 /* NAME\0 + 0x + digits. */
1270 char *tmp_name = alloca(sizeof NAME + 2 + 16);
Petr Machata54bb64c2013-11-04 20:00:43 +01001271 sprintf(tmp_name, NAME "%#" PRIx64, (uint64_t) addr);
Petr Machatab420a222013-10-15 10:46:28 +02001272 name = tmp_name;
1273#undef NAME
1274 }
1275
Petr Machatab061bae2013-10-25 23:50:18 +02001276 return strdup(name);
1277}
Petr Machatab420a222013-10-15 10:46:28 +02001278
Petr Machatab061bae2013-10-25 23:50:18 +02001279enum plt_status
1280linux_elf_add_plt_entry_irelative(struct process *proc, struct ltelf *lte,
1281 GElf_Rela *rela, size_t ndx,
1282 struct library_symbol **ret)
1283
1284{
Petr Machata54bb64c2013-11-04 20:00:43 +01001285 char *name = linux_elf_find_irelative_name(lte, rela->r_addend);
Petr Machatab061bae2013-10-25 23:50:18 +02001286 int i = default_elf_add_plt_entry(proc, lte, name, rela, ndx, ret);
1287 free(name);
1288 return i < 0 ? PLT_FAIL : PLT_OK;
Petr Machatab420a222013-10-15 10:46:28 +02001289}