blob: 0abb545882364a208c6d3eb8701d2c16c9d4966c [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 Machatafec0b3b2013-11-04 20:22:06 +010049#include "fetch.h"
Petr Machatab420a222013-10-15 10:46:28 +020050#include "ltrace-elf.h"
Petr Machataba1664b2012-04-28 14:59:05 +020051#include "options.h"
52#include "proc.h"
Petr Machatafec0b3b2013-11-04 20:22:06 +010053#include "prototype.h"
Petr Machataba1664b2012-04-28 14:59:05 +020054#include "ptrace.h"
55#include "type.h"
Petr Machatafec0b3b2013-11-04 20:22:06 +010056#include "value.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020057
Juan Cespedesf1350522008-12-16 18:19:58 +010058void
Petr Machatacec06ec2012-04-10 13:31:55 +020059trace_fail_warning(pid_t pid)
60{
61 /* This was adapted from GDB. */
62#ifdef HAVE_LIBSELINUX
63 static int checked = 0;
64 if (checked)
65 return;
66 checked = 1;
67
68 /* -1 is returned for errors, 0 if it has no effect, 1 if
69 * PTRACE_ATTACH is forbidden. */
70 if (security_get_boolean_active("deny_ptrace") == 1)
71 fprintf(stderr,
72"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
73"tracing other processes. You can disable this process attach protection by\n"
74"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
75#endif /* HAVE_LIBSELINUX */
76}
77
78void
79trace_me(void)
80{
Petr Machata26627682011-07-08 18:15:32 +020081 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020082 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010083 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +020084 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +010085 exit(1);
86 }
87}
88
Petr Machatab4f9e0c2012-02-07 01:57:59 +010089/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +010090 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +010091 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +010092int
Petr Machatab4f9e0c2012-02-07 01:57:59 +010093wait_for_proc(pid_t pid)
94{
Petr Machatac805c622012-03-02 00:10:37 +010095 /* man ptrace: PTRACE_ATTACH attaches to the process specified
96 in pid. The child is sent a SIGSTOP, but will not
97 necessarily have stopped by the completion of this call;
98 use wait() to wait for the child to stop. */
99 if (waitpid(pid, NULL, __WALL) != pid) {
100 perror ("trace_pid: waitpid");
101 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100102 }
103
Petr Machatac805c622012-03-02 00:10:37 +0100104 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100105}
106
Juan Cespedesf1350522008-12-16 18:19:58 +0100107int
Petr Machatacec06ec2012-04-10 13:31:55 +0200108trace_pid(pid_t pid)
109{
Petr Machata26627682011-07-08 18:15:32 +0200110 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200111 /* This shouldn't emit error messages, as there are legitimate
112 * reasons that the PID can't be attached: like it may have
113 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200114 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100115 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100116
Petr Machatac805c622012-03-02 00:10:37 +0100117 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100118}
119
Juan Cespedesf1350522008-12-16 18:19:58 +0100120void
Petr Machata929bd572012-12-17 03:20:34 +0100121trace_set_options(struct process *proc)
Petr Machata3ed2a422012-04-06 17:18:55 +0200122{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100123 if (proc->tracesysgood & 0x80)
124 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200125
Petr Machata3ed2a422012-04-06 17:18:55 +0200126 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200127 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200128
Juan Cespedes1e583132009-04-07 18:17:11 +0200129 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
130 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
131 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200132 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
133 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100134 perror("PTRACE_SETOPTIONS");
135 return;
136 }
137 proc->tracesysgood |= 0x80;
138}
139
Juan Cespedesf1350522008-12-16 18:19:58 +0100140void
141untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200142 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200143 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100144}
145
Juan Cespedesf1350522008-12-16 18:19:58 +0100146void
Petr Machatac897cb72012-05-05 01:26:58 +0200147continue_after_signal(pid_t pid, int signum)
148{
149 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
150 pid, signum);
151 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200152}
153
154static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200155event_for_pid(Event *event, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200156{
157 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
Petr Machata71f25e22012-12-17 04:01:23 +0100158 return ECB_YIELD;
159 return ECB_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200160}
161
162static int
163have_events_for(pid_t pid)
164{
165 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
166}
167
168void
169continue_process(pid_t pid)
170{
171 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200172
173 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200174 the queue for this process. Otherwise just wait for the
175 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200176 if (!have_events_for(pid))
177 /* We always trace syscalls to control fork(),
178 * clone(), execve()... */
179 ptrace(PTRACE_SYSCALL, pid, 0, 0);
180 else
181 debug(DEBUG_PROCESS,
182 "putting off the continue, events in que.");
183}
184
Petr Machata98f09922011-07-09 10:55:29 +0200185static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200186get_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200187{
Petr Machata750ca8c2011-10-06 14:29:34 +0200188 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200189 size_t i;
190 for (i = 0; i < pids->count; ++i)
191 if (pids->tasks[i].pid == pid)
192 return &pids->tasks[i];
193
194 return NULL;
195}
196
197static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200198add_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200199{
200 if (pids->count == pids->alloc) {
201 size_t ns = (2 * pids->alloc) ?: 4;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200202 struct pid_task *n = realloc(pids->tasks,
203 sizeof(*pids->tasks) * ns);
Petr Machata98f09922011-07-09 10:55:29 +0200204 if (n == NULL)
205 return NULL;
206 pids->tasks = n;
207 pids->alloc = ns;
208 }
209 struct pid_task * task_info = &pids->tasks[pids->count++];
210 memset(task_info, 0, sizeof(*task_info));
211 task_info->pid = pid;
212 return task_info;
213}
214
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100216task_stopped(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200217{
218 enum process_status st = process_status(task->pid);
219 if (data != NULL)
220 *(enum process_status *)data = st;
221
222 /* If the task is already stopped, don't worry about it.
223 * Likewise if it managed to become a zombie or terminate in
224 * the meantime. This can happen when the whole thread group
225 * is terminating. */
226 switch (st) {
Petr Machata6dfc5442012-12-17 03:38:36 +0100227 case PS_INVALID:
228 case PS_TRACING_STOP:
229 case PS_ZOMBIE:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100230 return CBS_CONT;
Petr Machata6dfc5442012-12-17 03:38:36 +0100231 case PS_SLEEPING:
232 case PS_STOP:
233 case PS_OTHER:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200235 }
Petr Machata36d19822011-10-21 16:03:45 +0200236
237 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200238}
239
240/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100242task_blocked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200243{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200244 struct pid_set *pids = data;
245 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +0200246 if (task_info != NULL
247 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200249
250 return task_stopped(task, NULL);
251}
252
Petr Machata366c2f42012-02-09 19:34:36 +0100253static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200254
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100256task_vforked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200257{
258 if (task->event_handler != NULL
259 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260 return CBS_STOP;
261 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200262}
263
264static int
Petr Machata929bd572012-12-17 03:20:34 +0100265is_vfork_parent(struct process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200266{
Petr Machata74132a42012-03-16 02:46:18 +0100267 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200268}
269
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100271send_sigstop(struct process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200272{
Petr Machata929bd572012-12-17 03:20:34 +0100273 struct process *leader = task->leader;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200274 struct pid_set *pids = data;
Petr Machata98f09922011-07-09 10:55:29 +0200275
276 /* Look for pre-existing task record, or add new. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200277 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200278 if (task_info == NULL)
279 task_info = add_task_info(pids, task->pid);
280 if (task_info == NULL) {
281 perror("send_sigstop: add_task_info");
282 destroy_event_handler(leader);
283 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200285 }
286
287 /* This task still has not been attached to. It should be
288 stopped by the kernel. */
289 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200291
292 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200293 * if we sent the SIGSTOP already, which happens when we are
294 * handling "onexit" and inherited the handler from breakpoint
295 * re-enablement. */
296 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297 if (task_stopped(task, &st) == CBS_CONT)
298 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200299 if (task_info->sigstopped) {
300 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100301 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200302 task_info->delivered = 0;
303 }
304
Petr Machatacbe29c62011-09-27 02:27:58 +0200305 /* Also don't attempt to stop the process if it's a parent of
306 * vforked process. We set up event handler specially to hint
307 * us. In that case parent is in D state, which we use to
308 * weed out unnecessary looping. */
Petr Machata6dfc5442012-12-17 03:38:36 +0100309 if (st == PS_SLEEPING
310 && is_vfork_parent(task)) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200311 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200313 }
314
Petr Machata98f09922011-07-09 10:55:29 +0200315 if (task_kill(task->pid, SIGSTOP) >= 0) {
316 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
317 task_info->sigstopped = 1;
318 } else
319 fprintf(stderr,
320 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
321
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200323}
324
Petr Machata73894bd2011-08-20 23:47:34 +0200325/* On certain kernels, detaching right after a singlestep causes the
326 tracee to be killed with a SIGTRAP (that even though the singlestep
327 was properly caught by waitpid. The ugly workaround is to put a
328 breakpoint where IP points and let the process continue. After
329 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200330static void
Petr Machata929bd572012-12-17 03:20:34 +0100331ugly_workaround(struct process *proc)
Petr Machata590c8082011-08-20 22:45:26 +0200332{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100333 arch_addr_t ip = get_instruction_pointer(proc);
Petr Machata98ff3092013-03-08 22:11:36 +0100334 struct breakpoint *found;
335 if (DICT_FIND_VAL(proc->leader->breakpoints, &ip, &found) < 0) {
Petr Machata02a796e2013-10-11 17:24:30 +0200336 insert_breakpoint_at(proc, ip, NULL);
Petr Machata98ff3092013-03-08 22:11:36 +0100337 } else {
338 assert(found != NULL);
339 enable_breakpoint(proc, found);
340 }
Petr Machata73894bd2011-08-20 23:47:34 +0200341 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200342}
343
344static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200345process_stopping_done(struct process_stopping_handler *self,
Petr Machata929bd572012-12-17 03:20:34 +0100346 struct process *leader)
Petr Machata98f09922011-07-09 10:55:29 +0200347{
348 debug(DEBUG_PROCESS, "process stopping done %d",
349 self->task_enabling_breakpoint->pid);
Petr Machata8ae36732012-04-30 01:19:09 +0200350
Petr Machata590c8082011-08-20 22:45:26 +0200351 if (!self->exiting) {
Petr Machata8ae36732012-04-30 01:19:09 +0200352 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200353 for (i = 0; i < self->pids.count; ++i)
354 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100355 && (self->pids.tasks[i].delivered
356 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200357 continue_process(self->pids.tasks[i].pid);
358 continue_process(self->task_enabling_breakpoint->pid);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200359 }
360
361 if (self->exiting) {
362 ugly_workaround:
Petr Machata96cb8e32012-12-17 03:49:41 +0100363 self->state = PSH_UGLY_WORKAROUND;
Petr Machata73894bd2011-08-20 23:47:34 +0200364 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200365 } else {
366 switch ((self->ugly_workaround_p)(self)) {
367 case CBS_FAIL:
368 /* xxx handle me */
369 case CBS_STOP:
370 break;
371 case CBS_CONT:
372 goto ugly_workaround;
373 }
Petr Machata8ae36732012-04-30 01:19:09 +0200374 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200375 }
376}
377
378/* Before we detach, we need to make sure that task's IP is on the
379 * edge of an instruction. So for tasks that have a breakpoint event
380 * in the queue, we adjust the instruction pointer, just like
381 * continue_after_breakpoint does. */
382static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200383undo_breakpoint(Event *event, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200384{
385 if (event != NULL
386 && event->proc->leader == data
387 && event->type == EVENT_BREAKPOINT)
388 set_instruction_pointer(event->proc, event->e_un.brk_addr);
Petr Machata71f25e22012-12-17 04:01:23 +0100389 return ECB_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200390}
391
Petr Machata2b46cfc2012-02-18 11:17:29 +0100392static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100393untrace_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200394{
395 if (task != data)
396 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200398}
399
Petr Machata2b46cfc2012-02-18 11:17:29 +0100400static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100401remove_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200402{
403 /* Don't untrace leader just yet. */
404 if (task != data)
405 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100406 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200407}
408
Petr Machata86d38282012-04-24 18:09:01 +0200409static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100410retract_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata86d38282012-04-24 18:09:01 +0200411{
412 breakpoint_on_retract(bp, proc);
413 return CBS_CONT;
414}
415
Petr Machata590c8082011-08-20 22:45:26 +0200416static void
Petr Machata929bd572012-12-17 03:20:34 +0100417detach_process(struct process *leader)
Petr Machata590c8082011-08-20 22:45:26 +0200418{
419 each_qd_event(&undo_breakpoint, leader);
420 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200421 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200422
423 /* Now untrace the process, if it was attached to by -p. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200424 struct opt_p_t *it;
Petr Machata590c8082011-08-20 22:45:26 +0200425 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +0100426 struct process *proc = pid2proc(it->pid);
Petr Machata590c8082011-08-20 22:45:26 +0200427 if (proc == NULL)
428 continue;
429 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100430 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200431 break;
432 }
433 }
Petr Machata74132a42012-03-16 02:46:18 +0100434 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200435 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200436 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200437}
438
439static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200440handle_stopping_event(struct pid_task *task_info, Event **eventp)
Petr Machata98f09922011-07-09 10:55:29 +0200441{
442 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200443 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200444 task_info->got_event = 1;
445
Petr Machata6c2e57a2012-10-27 01:50:22 +0200446 Event *event = *eventp;
Petr Machata98f09922011-07-09 10:55:29 +0200447
448 /* In every state, sink SIGSTOP events for tasks that it was
449 * sent to. */
450 if (task_info != NULL
451 && event->type == EVENT_SIGNAL
452 && event->e_un.signum == SIGSTOP) {
453 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
454 if (task_info->sigstopped
455 && !task_info->delivered) {
456 task_info->delivered = 1;
457 *eventp = NULL; // sink the event
458 } else
459 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
460 "sigstopped=%d and delivered=%d\n",
461 task_info->pid, task_info->sigstopped,
462 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100463 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100464}
465
Petr Machata98f09922011-07-09 10:55:29 +0200466/* Some SIGSTOPs may have not been delivered to their respective tasks
467 * yet. They are still in the queue. If we have seen an event for
468 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200469 * caught by ltrace. We don't mind that the process is after
470 * breakpoint (and therefore potentially doesn't have aligned IP),
471 * because the signal will be delivered without the process actually
472 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200473static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200474continue_for_sigstop_delivery(struct pid_set *pids)
Petr Machata98f09922011-07-09 10:55:29 +0200475{
476 size_t i;
477 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200478 if (pids->tasks[i].pid != 0
479 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200480 && !pids->tasks[i].delivered
481 && pids->tasks[i].got_event) {
482 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
483 pids->tasks[i].pid);
484 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
485 }
486 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100487}
488
Petr Machata98f09922011-07-09 10:55:29 +0200489static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200490event_exit_p(Event *event)
Petr Machata750ca8c2011-10-06 14:29:34 +0200491{
492 return event != NULL && (event->type == EVENT_EXIT
493 || event->type == EVENT_EXIT_SIGNAL);
494}
495
496static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200497event_exit_or_none_p(Event *event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200498{
Petr Machata750ca8c2011-10-06 14:29:34 +0200499 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200500 || event->type == EVENT_NONE;
501}
502
503static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200504await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
505 Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200506{
507 /* If we still didn't get our SIGSTOP, continue the process
508 * and carry on. */
509 if (event != NULL && !event_exit_or_none_p(event)
510 && task_info != NULL && task_info->sigstopped) {
511 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
512 task_info->pid);
513 /* We should get the signal the first thing
514 * after this, so it should be OK to continue
515 * even if we are over a breakpoint. */
516 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
517
518 } else {
519 /* If all SIGSTOPs were delivered, uninstall the
520 * handler and continue everyone. */
521 /* XXX I suspect that we should check tasks that are
522 * still around. Is things are now, there should be a
523 * race between waiting for everyone to stop and one
524 * of the tasks exiting. */
525 int all_clear = 1;
526 size_t i;
527 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200528 if (pids->tasks[i].pid != 0
529 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200530 && !pids->tasks[i].delivered) {
531 all_clear = 0;
532 break;
533 }
534 return all_clear;
535 }
536
537 return 0;
538}
539
Petr Machata590c8082011-08-20 22:45:26 +0200540static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200541all_stops_accountable(struct pid_set *pids)
Petr Machata590c8082011-08-20 22:45:26 +0200542{
543 size_t i;
544 for (i = 0; i < pids->count; ++i)
545 if (pids->tasks[i].pid != 0
546 && !pids->tasks[i].got_event
547 && !have_events_for(pids->tasks[i].pid))
548 return 0;
549 return 1;
550}
551
Petr Machata693dfad2013-01-14 22:10:51 +0100552#ifndef ARCH_HAVE_SW_SINGLESTEP
553enum sw_singlestep_status
554arch_sw_singlestep(struct process *proc, struct breakpoint *bp,
555 int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
556 struct sw_singlestep_data *data)
Petr Machata06986d52011-11-02 13:22:46 +0100557{
Petr Machata693dfad2013-01-14 22:10:51 +0100558 return SWS_HW;
Petr Machataa266acb2012-04-12 23:50:23 +0200559}
560#endif
561
Petr Machata42748ac2012-04-19 04:24:25 +0200562static Event *process_stopping_on_event(struct event_handler *super,
563 Event *event);
564
565static void
Petr Machata693dfad2013-01-14 22:10:51 +0100566remove_sw_breakpoints(struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200567{
Petr Machata24a47042012-04-19 18:15:08 +0200568 struct process_stopping_handler *self
569 = (void *)proc->leader->event_handler;
570 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200571 assert(self->super.on_event == process_stopping_on_event);
572
Petr Machata6e5e2de2013-01-21 22:25:34 +0100573 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200574 int i;
575 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100576 if (self->sws_bps[i] != NULL) {
Petr Machatab9440772013-10-14 10:36:26 +0200577 delete_breakpoint_at(proc, self->sws_bps[i]->addr);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100578 self->sws_bps[i] = NULL;
Petr Machata42748ac2012-04-19 04:24:25 +0200579 }
580}
581
582static void
Petr Machata693dfad2013-01-14 22:10:51 +0100583sw_singlestep_bp_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200584{
Petr Machata693dfad2013-01-14 22:10:51 +0100585 remove_sw_breakpoints(proc);
Petr Machata42748ac2012-04-19 04:24:25 +0200586}
587
Petr Machata693dfad2013-01-14 22:10:51 +0100588struct sw_singlestep_data {
589 struct process_stopping_handler *self;
590};
591
Petr Machataa266acb2012-04-12 23:50:23 +0200592static int
Petr Machata693dfad2013-01-14 22:10:51 +0100593sw_singlestep_add_bp(arch_addr_t addr, struct sw_singlestep_data *data)
Petr Machataa266acb2012-04-12 23:50:23 +0200594{
Petr Machata693dfad2013-01-14 22:10:51 +0100595 struct process_stopping_handler *self = data->self;
Petr Machata929bd572012-12-17 03:20:34 +0100596 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200597
Petr Machata6e5e2de2013-01-21 22:25:34 +0100598 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200599 int i;
600 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100601 if (self->sws_bps[i] == NULL) {
Petr Machata42748ac2012-04-19 04:24:25 +0200602 static struct bp_callbacks cbs = {
Petr Machata693dfad2013-01-14 22:10:51 +0100603 .on_hit = sw_singlestep_bp_on_hit,
Petr Machata42748ac2012-04-19 04:24:25 +0200604 };
605 struct breakpoint *bp
Petr Machata02a796e2013-10-11 17:24:30 +0200606 = insert_breakpoint_at(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200607 breakpoint_set_callbacks(bp, &cbs);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100608 self->sws_bps[i] = bp;
Petr Machata42748ac2012-04-19 04:24:25 +0200609 return 0;
610 }
Petr Machataa266acb2012-04-12 23:50:23 +0200611
Petr Machata693dfad2013-01-14 22:10:51 +0100612 assert(!"Too many sw singlestep breakpoints!");
Petr Machata42748ac2012-04-19 04:24:25 +0200613 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200614}
615
616static int
617singlestep(struct process_stopping_handler *self)
618{
Petr Machata6e5e2de2013-01-21 22:25:34 +0100619 size_t i;
620 for (i = 0; i < sizeof(self->sws_bps) / sizeof(*self->sws_bps); ++i)
621 self->sws_bps[i] = NULL;
Petr Machataa266acb2012-04-12 23:50:23 +0200622
Petr Machata693dfad2013-01-14 22:10:51 +0100623 struct sw_singlestep_data data = { self };
Petr Machata643c6462013-01-15 17:11:43 +0100624 switch (arch_sw_singlestep(self->task_enabling_breakpoint,
625 self->breakpoint_being_enabled,
626 &sw_singlestep_add_bp, &data)) {
627 case SWS_HW:
628 /* Otherwise do the default action: singlestep. */
629 debug(1, "PTRACE_SINGLESTEP");
Petr Machata6e5e2de2013-01-21 22:25:34 +0100630 if (ptrace(PTRACE_SINGLESTEP,
631 self->task_enabling_breakpoint->pid, 0, 0)) {
Petr Machata643c6462013-01-15 17:11:43 +0100632 perror("PTRACE_SINGLESTEP");
633 return -1;
634 }
635 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200636
Petr Machata643c6462013-01-15 17:11:43 +0100637 case SWS_OK:
638 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200639
Petr Machata643c6462013-01-15 17:11:43 +0100640 case SWS_FAIL:
Petr Machataa266acb2012-04-12 23:50:23 +0200641 return -1;
642 }
Petr Machata643c6462013-01-15 17:11:43 +0100643 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200644}
645
646static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200647post_singlestep(struct process_stopping_handler *self,
648 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200649{
650 continue_for_sigstop_delivery(&self->pids);
651
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200652 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200653 *eventp = NULL; // handled
654
Petr Machata929bd572012-12-17 03:20:34 +0100655 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200656
Petr Machata693dfad2013-01-14 22:10:51 +0100657 remove_sw_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200658 self->breakpoint_being_enabled = NULL;
659}
660
661static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200662singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200663{
Petr Machata929bd572012-12-17 03:20:34 +0100664 struct process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200665 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200666 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataba1664b2012-04-28 14:59:05 +0200667 teb->pid, breakpoint_name(sbp), sbp->addr,
668 get_instruction_pointer(teb));
Petr Machatab9440772013-10-14 10:36:26 +0200669 delete_breakpoint_at(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100670}
671
Petr Machata9e1e9692012-04-19 02:29:38 +0200672static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200673pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200674{
Petr Machata929bd572012-12-17 03:20:34 +0100675 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200676 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200677 ptrace(PTRACE_CONT, teb->pid, 0, 0);
678}
679
680static void
681pt_singlestep(struct process_stopping_handler *self)
682{
683 if (singlestep(self) < 0)
684 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200685}
686
687static void
688disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200689 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200690{
Petr Machata929bd572012-12-17 03:20:34 +0100691 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200692 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200693 if (self->breakpoint_being_enabled->enabled)
694 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200695 (do_this)(self);
Petr Machata96cb8e32012-12-17 03:49:41 +0100696 self->state = PSH_SINGLESTEP;
Petr Machatac1aa9582012-03-27 23:54:01 +0200697}
698
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200699void
700linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
701{
Petr Machata9e1e9692012-04-19 02:29:38 +0200702 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200703}
704
705void
706linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
707{
708 disable_and(self, &pt_continue);
709}
710
Petr Machata98f09922011-07-09 10:55:29 +0200711/* This event handler is installed when we are in the process of
712 * stopping the whole thread group to do the pointer re-enablement for
713 * one of the threads. We pump all events to the queue for later
714 * processing while we wait for all the threads to stop. When this
715 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
716 * re-enable, and continue everyone. */
717static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100718process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200719{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200720 struct process_stopping_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100721 struct process *task = event->proc;
722 struct process *leader = task->leader;
723 struct process *teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200724
725 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200726 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200727 task->pid, event->type, self->state);
728
Petr Machata6c2e57a2012-10-27 01:50:22 +0200729 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200730 if (task_info == NULL)
731 fprintf(stderr, "new task??? %d\n", task->pid);
732 handle_stopping_event(task_info, &event);
733
734 int state = self->state;
735 int event_to_queue = !event_exit_or_none_p(event);
736
Petr Machata18c97072011-10-06 14:30:11 +0200737 /* Deactivate the entry if the task exits. */
738 if (event_exit_p(event) && task_info != NULL)
739 task_info->pid = 0;
740
Petr Machata43d2fe52011-11-02 13:25:49 +0100741 /* Always handle sysrets. Whether sysret occurred and what
742 * sys it rets from may need to be determined based on process
743 * stack, so we need to keep that in sync with reality. Note
744 * that we don't continue the process after the sysret is
745 * handled. See continue_after_syscall. */
746 if (event != NULL && event->type == EVENT_SYSRET) {
747 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
748 event_to_queue = 0;
Petr Machata754ce882013-01-08 23:41:47 +0100749 if (task_info != NULL)
750 task_info->sysret = 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100751 }
752
Petr Machata98f09922011-07-09 10:55:29 +0200753 switch (state) {
Petr Machata96cb8e32012-12-17 03:49:41 +0100754 case PSH_STOPPING:
Petr Machata98f09922011-07-09 10:55:29 +0200755 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100756 if (each_task(leader, NULL, &task_blocked,
757 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200758 (self->on_all_stopped)(self);
759 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200760 }
761 break;
762
Petr Machata96cb8e32012-12-17 03:49:41 +0100763 case PSH_SINGLESTEP:
Petr Machata98f09922011-07-09 10:55:29 +0200764 /* In singlestep state, breakpoint signifies that we
765 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200766 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200767
Petr Machata42748ac2012-04-19 04:24:25 +0200768 /* If this was caused by a real breakpoint, as
769 * opposed to a singlestep, assume that it's
770 * an artificial breakpoint installed for some
771 * reason for the re-enablement. In that case
772 * handle it. */
773 if (event->type == EVENT_BREAKPOINT) {
Petr Machatabac2da52012-05-29 00:42:59 +0200774 arch_addr_t ip
Petr Machata42748ac2012-04-19 04:24:25 +0200775 = get_instruction_pointer(task);
776 struct breakpoint *other
777 = address2bpstruct(leader, ip);
778 if (other != NULL)
779 breakpoint_on_hit(other, task);
780 }
781
Petr Machata36f40e72012-03-28 02:07:19 +0200782 /* If we got SIGNAL instead of BREAKPOINT,
783 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200784 if (event->type == EVENT_SIGNAL) {
785 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200786 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200787 singlestep_error(self);
788 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200789 goto psh_sinking;
790 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200791 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200792 } else {
793 switch ((self->keep_stepping_p)(self)) {
794 case CBS_FAIL:
795 /* XXX handle me */
796 case CBS_STOP:
797 break;
798 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200799 /* Sink singlestep event. */
800 if (event->type == EVENT_BREAKPOINT)
801 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200802 goto do_singlestep;
803 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200804 }
805
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200806 /* Re-enable the breakpoint that we are
807 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200808 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200809 if (sbp->enabled)
810 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200811
Petr Machataa266acb2012-04-12 23:50:23 +0200812 post_singlestep(self, &event);
813 goto psh_sinking;
814 }
815 break;
Petr Machata98f09922011-07-09 10:55:29 +0200816
Petr Machataa266acb2012-04-12 23:50:23 +0200817 psh_sinking:
Petr Machata96cb8e32012-12-17 03:49:41 +0100818 state = self->state = PSH_SINKING;
819 /* Fall through. */
820 case PSH_SINKING:
Petr Machata98f09922011-07-09 10:55:29 +0200821 if (await_sigstop_delivery(&self->pids, task_info, event))
822 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200823 break;
824
Petr Machata96cb8e32012-12-17 03:49:41 +0100825 case PSH_UGLY_WORKAROUND:
Petr Machata590c8082011-08-20 22:45:26 +0200826 if (event == NULL)
827 break;
828 if (event->type == EVENT_BREAKPOINT) {
829 undo_breakpoint(event, leader);
830 if (task == teb)
831 self->task_enabling_breakpoint = NULL;
832 }
833 if (self->task_enabling_breakpoint == NULL
834 && all_stops_accountable(&self->pids)) {
835 undo_breakpoint(event, leader);
836 detach_process(leader);
837 event = NULL; // handled
838 }
Petr Machata98f09922011-07-09 10:55:29 +0200839 }
840
841 if (event != NULL && event_to_queue) {
842 enque_event(event);
843 event = NULL; // sink the event
844 }
845
846 return event;
847}
848
849static void
Petr Machata366c2f42012-02-09 19:34:36 +0100850process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200851{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200852 struct process_stopping_handler *self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200853 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100854}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100855
Petr Machata36f40e72012-03-28 02:07:19 +0200856static enum callback_status
857no(struct process_stopping_handler *self)
858{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200859 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200860}
861
Petr Machatac1aa9582012-03-27 23:54:01 +0200862int
Petr Machata929bd572012-12-17 03:20:34 +0100863process_install_stopping_handler(struct process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200864 void (*as)(struct process_stopping_handler *),
865 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200866 (struct process_stopping_handler *),
867 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200868 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200869{
Petr Machata9ace5742012-04-14 02:29:57 +0200870 debug(DEBUG_FUNCTION,
871 "process_install_stopping_handler: pid=%d", proc->pid);
872
Petr Machatac1aa9582012-03-27 23:54:01 +0200873 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
874 if (handler == NULL)
875 return -1;
876
Petr Machata36f40e72012-03-28 02:07:19 +0200877 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200878 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200879 if (ks == NULL)
880 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200881 if (uw == NULL)
882 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200883
Petr Machatac1aa9582012-03-27 23:54:01 +0200884 handler->super.on_event = process_stopping_on_event;
885 handler->super.destroy = process_stopping_destroy;
886 handler->task_enabling_breakpoint = proc;
887 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200888 handler->on_all_stopped = as;
889 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200890 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200891
892 install_event_handler(proc->leader, &handler->super);
893
894 if (each_task(proc->leader, NULL, &send_sigstop,
895 &handler->pids) != NULL) {
896 destroy_event_handler(proc);
897 return -1;
898 }
899
900 /* And deliver the first fake event, in case all the
901 * conditions are already fulfilled. */
902 Event ev = {
903 .type = EVENT_NONE,
904 .proc = proc,
905 };
906 process_stopping_on_event(&handler->super, &ev);
907
908 return 0;
909}
910
Juan Cespedesf1350522008-12-16 18:19:58 +0100911void
Petr Machata929bd572012-12-17 03:20:34 +0100912continue_after_breakpoint(struct process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200913{
Petr Machata9ace5742012-04-14 02:29:57 +0200914 debug(DEBUG_PROCESS,
915 "continue_after_breakpoint: pid=%d, addr=%p",
916 proc->pid, sbp->addr);
917
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200918 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200919
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100920 if (sbp->enabled == 0) {
921 continue_process(proc->pid);
Petr Machataa9d6a672013-03-05 17:10:37 +0100922 } else if (process_install_stopping_handler
923 (proc, sbp, NULL, NULL, NULL) < 0) {
924 perror("process_stopping_handler_create");
925 /* Carry on not bothering to re-enable. */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200926 continue_process(proc->pid);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100927 }
928}
929
Petr Machata602330f2011-07-09 11:15:34 +0200930/**
931 * Ltrace exit. When we are about to exit, we have to go through all
932 * the processes, stop them all, remove all the breakpoints, and then
933 * detach the processes that we attached to using -p. If we left the
934 * other tasks running, they might hit stray return breakpoints and
935 * produce artifacts, so we better stop everyone, even if it's a bit
936 * of extra work.
937 */
938struct ltrace_exiting_handler
939{
Petr Machata366c2f42012-02-09 19:34:36 +0100940 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200941 struct pid_set pids;
942};
943
Petr Machata602330f2011-07-09 11:15:34 +0200944static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100945ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200946{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200947 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100948 struct process *task = event->proc;
949 struct process *leader = task->leader;
Petr Machata602330f2011-07-09 11:15:34 +0200950
Petr Machata9ace5742012-04-14 02:29:57 +0200951 debug(DEBUG_PROCESS,
952 "ltrace_exiting_on_event: pid %d; event type %d",
953 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200954
Petr Machata6c2e57a2012-10-27 01:50:22 +0200955 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata602330f2011-07-09 11:15:34 +0200956 handle_stopping_event(task_info, &event);
957
Petr Machata590c8082011-08-20 22:45:26 +0200958 if (event != NULL && event->type == EVENT_BREAKPOINT)
959 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200960
961 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200962 && all_stops_accountable(&self->pids))
963 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200964
965 /* Sink all non-exit events. We are about to exit, so we
966 * don't bother with queuing them. */
967 if (event_exit_or_none_p(event))
968 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200969
Petr Machata13d5df72011-08-19 23:15:15 +0200970 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200971}
972
973static void
Petr Machata366c2f42012-02-09 19:34:36 +0100974ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200975{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200976 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata602330f2011-07-09 11:15:34 +0200977 free(self->pids.tasks);
978}
979
980static int
Petr Machata929bd572012-12-17 03:20:34 +0100981ltrace_exiting_install_handler(struct process *proc)
Petr Machata602330f2011-07-09 11:15:34 +0200982{
983 /* Only install to leader. */
984 if (proc->leader != proc)
985 return 0;
986
987 /* Perhaps we are already installed, if the user passed
988 * several -p options that are tasks of one process. */
989 if (proc->event_handler != NULL
990 && proc->event_handler->on_event == &ltrace_exiting_on_event)
991 return 0;
992
Petr Machata590c8082011-08-20 22:45:26 +0200993 /* If stopping handler is already present, let it do the
994 * work. */
995 if (proc->event_handler != NULL) {
996 assert(proc->event_handler->on_event
997 == &process_stopping_on_event);
Petr Machata6c2e57a2012-10-27 01:50:22 +0200998 struct process_stopping_handler *other
Petr Machata590c8082011-08-20 22:45:26 +0200999 = (void *)proc->event_handler;
1000 other->exiting = 1;
1001 return 0;
1002 }
1003
Petr Machata6c2e57a2012-10-27 01:50:22 +02001004 struct ltrace_exiting_handler *handler
Petr Machata602330f2011-07-09 11:15:34 +02001005 = calloc(sizeof(*handler), 1);
1006 if (handler == NULL) {
1007 perror("malloc exiting handler");
1008 fatal:
1009 /* XXXXXXXXXXXXXXXXXXX fixme */
1010 return -1;
1011 }
1012
Petr Machata602330f2011-07-09 11:15:34 +02001013 handler->super.on_event = ltrace_exiting_on_event;
1014 handler->super.destroy = ltrace_exiting_destroy;
1015 install_event_handler(proc->leader, &handler->super);
1016
Petr Machata74132a42012-03-16 02:46:18 +01001017 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001018 &handler->pids) != NULL)
1019 goto fatal;
1020
1021 return 0;
1022}
1023
Petr Machatacbe29c62011-09-27 02:27:58 +02001024/*
1025 * When the traced process vforks, it's suspended until the child
1026 * process calls _exit or exec*. In the meantime, the two share the
1027 * address space.
1028 *
1029 * The child process should only ever call _exit or exec*, but we
1030 * can't count on that (it's not the role of ltrace to policy, but to
1031 * observe). In any case, we will _at least_ have to deal with
1032 * removal of vfork return breakpoint (which we have to smuggle back
1033 * in, so that the parent can see it, too), and introduction of exec*
1034 * return breakpoint. Since we already have both breakpoint actions
1035 * to deal with, we might as well support it all.
1036 *
1037 * The gist is that we pretend that the child is in a thread group
1038 * with its parent, and handle it as a multi-threaded case, with the
1039 * exception that we know that the parent is blocked, and don't
1040 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001041 */
1042
Petr Machata134a1082011-09-27 20:25:58 +02001043struct process_vfork_handler
1044{
Petr Machata366c2f42012-02-09 19:34:36 +01001045 struct event_handler super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001046 int vfork_bp_refd:1;
Petr Machata134a1082011-09-27 20:25:58 +02001047};
1048
Petr Machatacbe29c62011-09-27 02:27:58 +02001049static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001050process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001051{
Petr Machata9ace5742012-04-14 02:29:57 +02001052 debug(DEBUG_PROCESS,
1053 "process_vfork_on_event: pid %d; event type %d",
1054 event->proc->pid, event->type);
1055
Petr Machata6c2e57a2012-10-27 01:50:22 +02001056 struct process_vfork_handler *self = (void *)super;
Petr Machata7fc98ee2013-01-29 18:01:32 +01001057 struct process *proc = event->proc;
Petr Machatacbe29c62011-09-27 02:27:58 +02001058 assert(self != NULL);
1059
1060 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001061 case EVENT_BREAKPOINT:
Petr Machata7fc98ee2013-01-29 18:01:32 +01001062 /* We turn on the vfork return breakpoint (which
1063 * should be the one that we have tripped over just
1064 * now) one extra time, so that the vfork parent hits
1065 * it as well. */
1066 if (!self->vfork_bp_refd) {
1067 struct breakpoint *sbp = NULL;
1068 DICT_FIND_VAL(proc->leader->breakpoints,
1069 &event->e_un.brk_addr, &sbp);
1070 assert(sbp != NULL);
1071 breakpoint_turn_on(sbp, proc->leader);
1072 self->vfork_bp_refd = 1;
1073 }
Petr Machata134a1082011-09-27 20:25:58 +02001074 break;
1075
Petr Machatacbe29c62011-09-27 02:27:58 +02001076 case EVENT_EXIT:
1077 case EVENT_EXIT_SIGNAL:
1078 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001079 /* Remove the leader that we artificially set up
1080 * earlier. */
Petr Machata7fc98ee2013-01-29 18:01:32 +01001081 change_process_leader(proc, proc);
1082 destroy_event_handler(proc);
1083 continue_process(proc->parent->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +02001084
Petr Machatacbe29c62011-09-27 02:27:58 +02001085 default:
1086 ;
1087 }
1088
1089 return event;
1090}
1091
1092void
Petr Machata929bd572012-12-17 03:20:34 +01001093continue_after_vfork(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +02001094{
1095 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata6c2e57a2012-10-27 01:50:22 +02001096 struct process_vfork_handler *handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001097 if (handler == NULL) {
1098 perror("malloc vfork handler");
1099 /* Carry on not bothering to treat the process as
1100 * necessary. */
1101 continue_process(proc->parent->pid);
1102 return;
1103 }
1104
1105 /* We must set up custom event handler, so that we see
1106 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001107 handler->super.on_event = process_vfork_on_event;
1108 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001109
1110 /* Make sure that the child is sole thread. */
1111 assert(proc->leader == proc);
1112 assert(proc->next == NULL || proc->next->leader != proc);
1113
1114 /* Make sure that the child's parent is properly set up. */
1115 assert(proc->parent != NULL);
1116 assert(proc->parent->leader != NULL);
1117
1118 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001119}
1120
Petr Machata9d29b3e2011-11-09 16:46:56 +01001121static int
Petr Machata929bd572012-12-17 03:20:34 +01001122is_mid_stopping(struct process *proc)
Petr Machata9d29b3e2011-11-09 16:46:56 +01001123{
1124 return proc != NULL
1125 && proc->event_handler != NULL
1126 && proc->event_handler->on_event == &process_stopping_on_event;
1127}
1128
Petr Machata43d2fe52011-11-02 13:25:49 +01001129void
Petr Machata929bd572012-12-17 03:20:34 +01001130continue_after_syscall(struct process *proc, int sysnum, int ret_p)
Petr Machata43d2fe52011-11-02 13:25:49 +01001131{
1132 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001133 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1134 debug(DEBUG_PROCESS,
1135 "continue_after_syscall: don't continue %d",
1136 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001137 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001138 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001139 continue_process(proc->pid);
1140}
1141
Petr Machata057caa52013-01-30 23:28:47 +01001142void
1143continue_after_exec(struct process *proc)
1144{
1145 continue_process(proc->pid);
1146
1147 /* After the exec, we expect to hit the first executable
1148 * instruction.
1149 *
1150 * XXX TODO It would be nice to have this removed, but then we
1151 * need to do that also for initial call to wait_for_proc in
1152 * execute_program. In that case we could generate a
1153 * EVENT_FIRST event or something, or maybe this could somehow
1154 * be rolled into EVENT_NEW. */
1155 wait_for_proc(proc->pid);
1156 continue_process(proc->pid);
1157}
1158
Petr Machata602330f2011-07-09 11:15:34 +02001159/* If ltrace gets SIGINT, the processes directly or indirectly run by
1160 * ltrace get it too. We just have to wait long enough for the signal
1161 * to be delivered and the process terminated, which we notice and
1162 * exit ltrace, too. So there's not much we need to do there. We
1163 * want to keep tracing those processes as usual, in case they just
1164 * SIG_IGN the SIGINT to do their shutdown etc.
1165 *
1166 * For processes ran on the background, we want to install an exit
1167 * handler that stops all the threads, removes all breakpoints, and
1168 * detaches.
1169 */
1170void
Petr Machataffe4cd22012-04-11 18:01:44 +02001171os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001172{
Petr Machata6c2e57a2012-10-27 01:50:22 +02001173 struct opt_p_t *it;
Petr Machata602330f2011-07-09 11:15:34 +02001174 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +01001175 struct process *proc = pid2proc(it->pid);
Petr Machata602330f2011-07-09 11:15:34 +02001176 if (proc == NULL || proc->leader == NULL)
1177 continue;
1178 if (ltrace_exiting_install_handler(proc->leader) < 0)
1179 fprintf(stderr,
1180 "Couldn't install exiting handler for %d.\n",
1181 proc->pid);
1182 }
1183}
1184
Petr Machataffe4cd22012-04-11 18:01:44 +02001185int
1186os_ltrace_exiting_sighandler(void)
1187{
1188 extern int linux_in_waitpid;
1189 if (linux_in_waitpid) {
1190 os_ltrace_exiting();
1191 return 1;
1192 }
1193 return 0;
1194}
1195
Joe Damatodfa3fa32010-11-08 15:47:35 -08001196size_t
Petr Machata929bd572012-12-17 03:20:34 +01001197umovebytes(struct process *proc, void *addr, void *laddr, size_t len)
1198{
Joe Damatodfa3fa32010-11-08 15:47:35 -08001199
1200 union {
1201 long a;
1202 char c[sizeof(long)];
1203 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001204 int started = 0;
1205 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -08001206
1207 while (offset < len) {
1208 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1209 if (a.a == -1 && errno) {
1210 if (started && errno == EIO)
1211 return bytes_read;
1212 else
1213 return -1;
1214 }
1215 started = 1;
1216
1217 if (len - offset >= sizeof(long)) {
1218 memcpy(laddr + offset, &a.c[0], sizeof(long));
1219 bytes_read += sizeof(long);
1220 }
1221 else {
1222 memcpy(laddr + offset, &a.c[0], len - offset);
1223 bytes_read += (len - offset);
1224 }
1225 offset += sizeof(long);
1226 }
1227
1228 return bytes_read;
1229}
Petr Machatab420a222013-10-15 10:46:28 +02001230
1231struct irelative_name_data_t {
1232 GElf_Addr addr;
1233 const char *found_name;
1234};
1235
1236static enum callback_status
1237irelative_name_cb(GElf_Sym *symbol, const char *name, void *d)
1238{
1239 struct irelative_name_data_t *data = d;
1240
1241 if (symbol->st_value == data->addr) {
1242 bool is_ifunc = false;
1243#ifdef STT_GNU_IFUNC
1244 is_ifunc = GELF_ST_TYPE(symbol->st_info) == STT_GNU_IFUNC;
1245#endif
1246 data->found_name = name;
1247
1248 /* Keep looking, unless we found the actual IFUNC
1249 * symbol. What we matched may have been a symbol
1250 * denoting the resolver function, which would have
1251 * the same address. */
1252 return CBS_STOP_IF(is_ifunc);
1253 }
1254
1255 return CBS_CONT;
1256}
1257
Petr Machatab061bae2013-10-25 23:50:18 +02001258char *
Petr Machata54bb64c2013-11-04 20:00:43 +01001259linux_elf_find_irelative_name(struct ltelf *lte, GElf_Addr addr)
Petr Machatab420a222013-10-15 10:46:28 +02001260{
Petr Machata54bb64c2013-11-04 20:00:43 +01001261 struct irelative_name_data_t data = { addr, NULL };
1262 if (addr != 0
Petr Machatab420a222013-10-15 10:46:28 +02001263 && elf_each_symbol(lte, 0,
1264 irelative_name_cb, &data).status < 0)
Petr Machatab061bae2013-10-25 23:50:18 +02001265 return NULL;
Petr Machatab420a222013-10-15 10:46:28 +02001266
1267 const char *name;
1268 if (data.found_name != NULL) {
1269 name = data.found_name;
1270 } else {
1271#define NAME "IREL."
1272 /* NAME\0 + 0x + digits. */
1273 char *tmp_name = alloca(sizeof NAME + 2 + 16);
Petr Machata54bb64c2013-11-04 20:00:43 +01001274 sprintf(tmp_name, NAME "%#" PRIx64, (uint64_t) addr);
Petr Machatab420a222013-10-15 10:46:28 +02001275 name = tmp_name;
1276#undef NAME
1277 }
1278
Petr Machatab061bae2013-10-25 23:50:18 +02001279 return strdup(name);
1280}
Petr Machatab420a222013-10-15 10:46:28 +02001281
Petr Machatab061bae2013-10-25 23:50:18 +02001282enum plt_status
1283linux_elf_add_plt_entry_irelative(struct process *proc, struct ltelf *lte,
1284 GElf_Rela *rela, size_t ndx,
1285 struct library_symbol **ret)
1286
1287{
Petr Machata54bb64c2013-11-04 20:00:43 +01001288 char *name = linux_elf_find_irelative_name(lte, rela->r_addend);
Petr Machatab061bae2013-10-25 23:50:18 +02001289 int i = default_elf_add_plt_entry(proc, lte, name, rela, ndx, ret);
1290 free(name);
1291 return i < 0 ? PLT_FAIL : PLT_OK;
Petr Machatab420a222013-10-15 10:46:28 +02001292}
Petr Machatafec0b3b2013-11-04 20:22:06 +01001293
Petr Machatafa844db2013-11-05 01:55:57 +01001294struct prototype *
1295linux_IFUNC_prototype(void)
Petr Machatafec0b3b2013-11-04 20:22:06 +01001296{
1297 static struct prototype ret;
1298 if (ret.return_info == NULL) {
1299 prototype_init(&ret);
1300 ret.return_info = type_get_voidptr();
1301 ret.own_return_info = 0;
1302 }
1303 return &ret;
1304}
1305
1306int
1307os_library_symbol_init(struct library_symbol *libsym)
1308{
1309 libsym->os = (struct os_library_symbol_data){};
1310 return 0;
1311}
1312
1313void
1314os_library_symbol_destroy(struct library_symbol *libsym)
1315{
1316}
1317
1318int
1319os_library_symbol_clone(struct library_symbol *retp,
1320 struct library_symbol *libsym)
1321{
1322 retp->os = libsym->os;
1323 return 0;
1324}
1325
Petr Machatafa844db2013-11-05 01:55:57 +01001326char *
1327linux_append_IFUNC_to_name(const char *name)
1328{
1329#define S ".IFUNC"
1330 char *tmp_name = malloc(strlen(name) + sizeof S);
1331 if (tmp_name == NULL)
1332 return NULL;
1333 sprintf(tmp_name, "%s%s", name, S);
1334#undef S
1335 return tmp_name;
1336}
1337
Petr Machatafec0b3b2013-11-04 20:22:06 +01001338enum plt_status
1339os_elf_add_func_entry(struct process *proc, struct ltelf *lte,
1340 const GElf_Sym *sym,
1341 arch_addr_t addr, const char *name,
1342 struct library_symbol **ret)
1343{
1344 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC)
1345 return PLT_DEFAULT;
1346
1347 bool ifunc = false;
1348#ifdef STT_GNU_IFUNC
1349 ifunc = GELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC;
1350#endif
1351
1352 if (ifunc) {
Petr Machatafa844db2013-11-05 01:55:57 +01001353 char *tmp_name = linux_append_IFUNC_to_name(name);
Petr Machatafec0b3b2013-11-04 20:22:06 +01001354 struct library_symbol *tmp = malloc(sizeof *tmp);
1355 if (tmp_name == NULL || tmp == NULL) {
1356 fail:
1357 free(tmp_name);
1358 free(tmp);
1359 return PLT_FAIL;
1360 }
Petr Machatafec0b3b2013-11-04 20:22:06 +01001361
1362 if (library_symbol_init(tmp, addr, tmp_name, 1,
1363 LS_TOPLT_NONE) < 0)
1364 goto fail;
Petr Machatafa844db2013-11-05 01:55:57 +01001365 tmp->proto = linux_IFUNC_prototype();
Petr Machatafec0b3b2013-11-04 20:22:06 +01001366 tmp->os.is_ifunc = 1;
1367
1368 *ret = tmp;
1369 return PLT_OK;
1370 }
1371
1372 *ret = NULL;
1373 return PLT_OK;
1374}
1375
1376static enum callback_status
1377libsym_at_address(struct library_symbol *libsym, void *addrp)
1378{
1379 arch_addr_t addr = *(arch_addr_t *)addrp;
1380 return CBS_STOP_IF(addr == libsym->enter_addr);
1381}
1382
1383static void
1384ifunc_ret_hit(struct breakpoint *bp, struct process *proc)
1385{
1386 struct fetch_context *fetch = fetch_arg_init(LT_TOF_FUNCTION, proc,
1387 type_get_voidptr());
1388 if (fetch == NULL)
1389 return;
1390
1391 struct breakpoint *nbp = NULL;
1392 int own_libsym = 0;
Mark Wielaardb882ba72014-01-09 22:56:35 +01001393 struct library_symbol *libsym = NULL;
Petr Machatafec0b3b2013-11-04 20:22:06 +01001394
1395 struct value value;
1396 value_init(&value, proc, NULL, type_get_voidptr(), 0);
1397 size_t sz = value_size(&value, NULL);
1398 union {
1399 uint64_t u64;
1400 uint32_t u32;
1401 arch_addr_t a;
1402 } u;
1403
1404 if (fetch_retval(fetch, LT_TOF_FUNCTIONR, proc,
1405 value.type, &value) < 0
1406 || sz > 8 /* Captures failure as well. */
1407 || value_extract_buf(&value, (void *) &u, NULL) < 0) {
1408 fail:
1409 fprintf(stderr,
1410 "Couldn't trace the function "
1411 "indicated by IFUNC resolver.\n");
1412 goto done;
1413 }
1414
1415 assert(sz == 4 || sz == 8);
1416 /* XXX double casts below: */
1417 if (sz == 4)
1418 u.a = (arch_addr_t)(uintptr_t)u.u32;
1419 else
1420 u.a = (arch_addr_t)(uintptr_t)u.u64;
1421 if (arch_translate_address_dyn(proc, u.a, &u.a) < 0) {
1422 fprintf(stderr, "Couldn't OPD-translate the address returned"
1423 " by the IFUNC resolver.\n");
1424 goto done;
1425 }
1426
1427 assert(bp->os.ret_libsym != NULL);
1428
1429 struct library *lib = bp->os.ret_libsym->lib;
1430 assert(lib != NULL);
1431
1432 /* Look if we already have a symbol with this address.
1433 * Otherwise create a new one. */
Mark Wielaardb882ba72014-01-09 22:56:35 +01001434 libsym = library_each_symbol(lib, NULL, libsym_at_address, &u.a);
Petr Machatafec0b3b2013-11-04 20:22:06 +01001435 if (libsym == NULL) {
1436 libsym = malloc(sizeof *libsym);
1437 char *name = strdup(bp->os.ret_libsym->name);
1438
1439 if (libsym == NULL
1440 || name == NULL
1441 || library_symbol_init(libsym, u.a, name, 1,
1442 LS_TOPLT_NONE) < 0) {
1443 free(libsym);
1444 free(name);
1445 goto fail;
1446 }
1447
1448 /* Snip the .IFUNC token. */
1449 *strrchr(name, '.') = 0;
1450
1451 own_libsym = 1;
1452 library_add_symbol(lib, libsym);
1453 }
1454
1455 nbp = malloc(sizeof *bp);
1456 if (nbp == NULL || breakpoint_init(nbp, proc, u.a, libsym) < 0)
1457 goto fail;
1458
1459 /* If there already is a breakpoint at that address, that is
1460 * suspicious, but whatever. */
1461 struct breakpoint *pre_bp = insert_breakpoint(proc, nbp);
1462 if (pre_bp == NULL)
1463 goto fail;
1464 if (pre_bp == nbp) {
1465 /* PROC took our breakpoint, so these resources are
1466 * not ours anymore. */
1467 nbp = NULL;
1468 own_libsym = 0;
1469 }
1470
1471done:
1472 free(nbp);
1473 if (own_libsym) {
1474 library_symbol_destroy(libsym);
1475 free(libsym);
1476 }
1477 fetch_arg_done(fetch);
1478}
1479
1480static int
1481create_ifunc_ret_bp(struct breakpoint **ret,
1482 struct breakpoint *bp, struct process *proc)
1483{
1484 *ret = create_default_return_bp(proc);
1485 if (*ret == NULL)
1486 return -1;
1487 static struct bp_callbacks cbs = {
1488 .on_hit = ifunc_ret_hit,
1489 };
1490 breakpoint_set_callbacks(*ret, &cbs);
1491
1492 (*ret)->os.ret_libsym = bp->libsym;
1493
1494 return 0;
1495}
1496
1497int
1498os_breakpoint_init(struct process *proc, struct breakpoint *bp)
1499{
1500 if (bp->libsym != NULL && bp->libsym->os.is_ifunc) {
1501 static struct bp_callbacks cbs = {
1502 .get_return_bp = create_ifunc_ret_bp,
1503 };
1504 breakpoint_set_callbacks(bp, &cbs);
1505 }
1506 return 0;
1507}
1508
1509void
1510os_breakpoint_destroy(struct breakpoint *bp)
1511{
1512}
1513
1514int
1515os_breakpoint_clone(struct breakpoint *retp, struct breakpoint *bp)
1516{
1517 return 0;
1518}