blob: dc0cd02422ea1c3a927e543be26890098e4f23e9 [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>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <assert.h>
30#include <errno.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010031#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010032#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010033#include <string.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010034#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010035
Petr Machatacec06ec2012-04-10 13:31:55 +020036#ifdef HAVE_LIBSELINUX
37# include <selinux/selinux.h>
38#endif
39
Petr Machata92822542012-03-28 00:31:14 +020040#include "linux-gnu/trace.h"
Petr Machataaf766ab2012-11-07 21:59:13 +010041#include "linux-gnu/trace-defs.h"
Petr Machata64262602012-01-07 03:41:36 +010042#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020043#include "breakpoint.h"
44#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010045#include "events.h"
Petr Machataba1664b2012-04-28 14:59:05 +020046#include "options.h"
47#include "proc.h"
48#include "ptrace.h"
49#include "type.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020050
Juan Cespedesf1350522008-12-16 18:19:58 +010051void
Petr Machatacec06ec2012-04-10 13:31:55 +020052trace_fail_warning(pid_t pid)
53{
54 /* This was adapted from GDB. */
55#ifdef HAVE_LIBSELINUX
56 static int checked = 0;
57 if (checked)
58 return;
59 checked = 1;
60
61 /* -1 is returned for errors, 0 if it has no effect, 1 if
62 * PTRACE_ATTACH is forbidden. */
63 if (security_get_boolean_active("deny_ptrace") == 1)
64 fprintf(stderr,
65"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
66"tracing other processes. You can disable this process attach protection by\n"
67"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
68#endif /* HAVE_LIBSELINUX */
69}
70
71void
72trace_me(void)
73{
Petr Machata26627682011-07-08 18:15:32 +020074 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Petr Machatac897cb72012-05-05 01:26:58 +020075 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010076 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +020077 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +010078 exit(1);
79 }
80}
81
Petr Machatab4f9e0c2012-02-07 01:57:59 +010082/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +010083 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +010084 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +010085int
Petr Machatab4f9e0c2012-02-07 01:57:59 +010086wait_for_proc(pid_t pid)
87{
Petr Machatac805c622012-03-02 00:10:37 +010088 /* man ptrace: PTRACE_ATTACH attaches to the process specified
89 in pid. The child is sent a SIGSTOP, but will not
90 necessarily have stopped by the completion of this call;
91 use wait() to wait for the child to stop. */
92 if (waitpid(pid, NULL, __WALL) != pid) {
93 perror ("trace_pid: waitpid");
94 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010095 }
96
Petr Machatac805c622012-03-02 00:10:37 +010097 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010098}
99
Juan Cespedesf1350522008-12-16 18:19:58 +0100100int
Petr Machatacec06ec2012-04-10 13:31:55 +0200101trace_pid(pid_t pid)
102{
Petr Machata26627682011-07-08 18:15:32 +0200103 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200104 /* This shouldn't emit error messages, as there are legitimate
105 * reasons that the PID can't be attached: like it may have
106 * already ended. */
Petr Machatac897cb72012-05-05 01:26:58 +0200107 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100108 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100109
Petr Machatac805c622012-03-02 00:10:37 +0100110 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100111}
112
Juan Cespedesf1350522008-12-16 18:19:58 +0100113void
Petr Machata929bd572012-12-17 03:20:34 +0100114trace_set_options(struct process *proc)
Petr Machata3ed2a422012-04-06 17:18:55 +0200115{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100116 if (proc->tracesysgood & 0x80)
117 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200118
Petr Machata3ed2a422012-04-06 17:18:55 +0200119 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200120 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200121
Juan Cespedes1e583132009-04-07 18:17:11 +0200122 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
123 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
124 PTRACE_O_TRACEEXEC;
Petr Machatac897cb72012-05-05 01:26:58 +0200125 if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
126 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100127 perror("PTRACE_SETOPTIONS");
128 return;
129 }
130 proc->tracesysgood |= 0x80;
131}
132
Juan Cespedesf1350522008-12-16 18:19:58 +0100133void
134untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200135 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Petr Machatac897cb72012-05-05 01:26:58 +0200136 ptrace(PTRACE_DETACH, pid, 0, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139void
Petr Machatac897cb72012-05-05 01:26:58 +0200140continue_after_signal(pid_t pid, int signum)
141{
142 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
143 pid, signum);
144 ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
Petr Machata98f09922011-07-09 10:55:29 +0200145}
146
147static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200148event_for_pid(Event *event, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200149{
150 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
Petr Machata71f25e22012-12-17 04:01:23 +0100151 return ECB_YIELD;
152 return ECB_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200153}
154
155static int
156have_events_for(pid_t pid)
157{
158 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
159}
160
161void
162continue_process(pid_t pid)
163{
164 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200165
166 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200167 the queue for this process. Otherwise just wait for the
168 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200169 if (!have_events_for(pid))
170 /* We always trace syscalls to control fork(),
171 * clone(), execve()... */
172 ptrace(PTRACE_SYSCALL, pid, 0, 0);
173 else
174 debug(DEBUG_PROCESS,
175 "putting off the continue, events in que.");
176}
177
Petr Machata98f09922011-07-09 10:55:29 +0200178static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200179get_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200180{
Petr Machata750ca8c2011-10-06 14:29:34 +0200181 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200182 size_t i;
183 for (i = 0; i < pids->count; ++i)
184 if (pids->tasks[i].pid == pid)
185 return &pids->tasks[i];
186
187 return NULL;
188}
189
190static struct pid_task *
Petr Machata6c2e57a2012-10-27 01:50:22 +0200191add_task_info(struct pid_set *pids, pid_t pid)
Petr Machata98f09922011-07-09 10:55:29 +0200192{
193 if (pids->count == pids->alloc) {
194 size_t ns = (2 * pids->alloc) ?: 4;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200195 struct pid_task *n = realloc(pids->tasks,
196 sizeof(*pids->tasks) * ns);
Petr Machata98f09922011-07-09 10:55:29 +0200197 if (n == NULL)
198 return NULL;
199 pids->tasks = n;
200 pids->alloc = ns;
201 }
202 struct pid_task * task_info = &pids->tasks[pids->count++];
203 memset(task_info, 0, sizeof(*task_info));
204 task_info->pid = pid;
205 return task_info;
206}
207
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100209task_stopped(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200210{
211 enum process_status st = process_status(task->pid);
212 if (data != NULL)
213 *(enum process_status *)data = st;
214
215 /* If the task is already stopped, don't worry about it.
216 * Likewise if it managed to become a zombie or terminate in
217 * the meantime. This can happen when the whole thread group
218 * is terminating. */
219 switch (st) {
Petr Machata6dfc5442012-12-17 03:38:36 +0100220 case PS_INVALID:
221 case PS_TRACING_STOP:
222 case PS_ZOMBIE:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223 return CBS_CONT;
Petr Machata6dfc5442012-12-17 03:38:36 +0100224 case PS_SLEEPING:
225 case PS_STOP:
226 case PS_OTHER:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200228 }
Petr Machata36d19822011-10-21 16:03:45 +0200229
230 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200231}
232
233/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100235task_blocked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200236{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200237 struct pid_set *pids = data;
238 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machatacbe29c62011-09-27 02:27:58 +0200239 if (task_info != NULL
240 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200242
243 return task_stopped(task, NULL);
244}
245
Petr Machata366c2f42012-02-09 19:34:36 +0100246static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200247
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100249task_vforked(struct process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200250{
251 if (task->event_handler != NULL
252 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 return CBS_STOP;
254 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200255}
256
257static int
Petr Machata929bd572012-12-17 03:20:34 +0100258is_vfork_parent(struct process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200259{
Petr Machata74132a42012-03-16 02:46:18 +0100260 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200261}
262
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100264send_sigstop(struct process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200265{
Petr Machata929bd572012-12-17 03:20:34 +0100266 struct process *leader = task->leader;
Petr Machata6c2e57a2012-10-27 01:50:22 +0200267 struct pid_set *pids = data;
Petr Machata98f09922011-07-09 10:55:29 +0200268
269 /* Look for pre-existing task record, or add new. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200270 struct pid_task *task_info = get_task_info(pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200271 if (task_info == NULL)
272 task_info = add_task_info(pids, task->pid);
273 if (task_info == NULL) {
274 perror("send_sigstop: add_task_info");
275 destroy_event_handler(leader);
276 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200278 }
279
280 /* This task still has not been attached to. It should be
281 stopped by the kernel. */
282 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100283 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200284
285 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200286 * if we sent the SIGSTOP already, which happens when we are
287 * handling "onexit" and inherited the handler from breakpoint
288 * re-enablement. */
289 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 if (task_stopped(task, &st) == CBS_CONT)
291 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200292 if (task_info->sigstopped) {
293 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200295 task_info->delivered = 0;
296 }
297
Petr Machatacbe29c62011-09-27 02:27:58 +0200298 /* Also don't attempt to stop the process if it's a parent of
299 * vforked process. We set up event handler specially to hint
300 * us. In that case parent is in D state, which we use to
301 * weed out unnecessary looping. */
Petr Machata6dfc5442012-12-17 03:38:36 +0100302 if (st == PS_SLEEPING
303 && is_vfork_parent(task)) {
Petr Machatacbe29c62011-09-27 02:27:58 +0200304 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200306 }
307
Petr Machata98f09922011-07-09 10:55:29 +0200308 if (task_kill(task->pid, SIGSTOP) >= 0) {
309 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
310 task_info->sigstopped = 1;
311 } else
312 fprintf(stderr,
313 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
314
Petr Machata2b46cfc2012-02-18 11:17:29 +0100315 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200316}
317
Petr Machata73894bd2011-08-20 23:47:34 +0200318/* On certain kernels, detaching right after a singlestep causes the
319 tracee to be killed with a SIGTRAP (that even though the singlestep
320 was properly caught by waitpid. The ugly workaround is to put a
321 breakpoint where IP points and let the process continue. After
322 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200323static void
Petr Machata929bd572012-12-17 03:20:34 +0100324ugly_workaround(struct process *proc)
Petr Machata590c8082011-08-20 22:45:26 +0200325{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100326 arch_addr_t ip = get_instruction_pointer(proc);
Petr Machata98ff3092013-03-08 22:11:36 +0100327 struct breakpoint *found;
328 if (DICT_FIND_VAL(proc->leader->breakpoints, &ip, &found) < 0) {
Petr Machata9df15012012-02-20 12:49:46 +0100329 insert_breakpoint(proc, ip, NULL);
Petr Machata98ff3092013-03-08 22:11:36 +0100330 } else {
331 assert(found != NULL);
332 enable_breakpoint(proc, found);
333 }
Petr Machata73894bd2011-08-20 23:47:34 +0200334 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200335}
336
337static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200338process_stopping_done(struct process_stopping_handler *self,
Petr Machata929bd572012-12-17 03:20:34 +0100339 struct process *leader)
Petr Machata98f09922011-07-09 10:55:29 +0200340{
341 debug(DEBUG_PROCESS, "process stopping done %d",
342 self->task_enabling_breakpoint->pid);
Petr Machata8ae36732012-04-30 01:19:09 +0200343
Petr Machata590c8082011-08-20 22:45:26 +0200344 if (!self->exiting) {
Petr Machata8ae36732012-04-30 01:19:09 +0200345 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200346 for (i = 0; i < self->pids.count; ++i)
347 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100348 && (self->pids.tasks[i].delivered
349 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200350 continue_process(self->pids.tasks[i].pid);
351 continue_process(self->task_enabling_breakpoint->pid);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200352 }
353
354 if (self->exiting) {
355 ugly_workaround:
Petr Machata96cb8e32012-12-17 03:49:41 +0100356 self->state = PSH_UGLY_WORKAROUND;
Petr Machata73894bd2011-08-20 23:47:34 +0200357 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200358 } else {
359 switch ((self->ugly_workaround_p)(self)) {
360 case CBS_FAIL:
361 /* xxx handle me */
362 case CBS_STOP:
363 break;
364 case CBS_CONT:
365 goto ugly_workaround;
366 }
Petr Machata8ae36732012-04-30 01:19:09 +0200367 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200368 }
369}
370
371/* Before we detach, we need to make sure that task's IP is on the
372 * edge of an instruction. So for tasks that have a breakpoint event
373 * in the queue, we adjust the instruction pointer, just like
374 * continue_after_breakpoint does. */
375static enum ecb_status
Petr Machata6c2e57a2012-10-27 01:50:22 +0200376undo_breakpoint(Event *event, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200377{
378 if (event != NULL
379 && event->proc->leader == data
380 && event->type == EVENT_BREAKPOINT)
381 set_instruction_pointer(event->proc, event->e_un.brk_addr);
Petr Machata71f25e22012-12-17 04:01:23 +0100382 return ECB_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200383}
384
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100386untrace_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200387{
388 if (task != data)
389 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100390 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200391}
392
Petr Machata2b46cfc2012-02-18 11:17:29 +0100393static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100394remove_task(struct process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200395{
396 /* Don't untrace leader just yet. */
397 if (task != data)
398 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100399 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200400}
401
Petr Machata86d38282012-04-24 18:09:01 +0200402static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100403retract_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata86d38282012-04-24 18:09:01 +0200404{
405 breakpoint_on_retract(bp, proc);
406 return CBS_CONT;
407}
408
Petr Machata590c8082011-08-20 22:45:26 +0200409static void
Petr Machata929bd572012-12-17 03:20:34 +0100410detach_process(struct process *leader)
Petr Machata590c8082011-08-20 22:45:26 +0200411{
412 each_qd_event(&undo_breakpoint, leader);
413 disable_all_breakpoints(leader);
Petr Machata86d38282012-04-24 18:09:01 +0200414 proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200415
416 /* Now untrace the process, if it was attached to by -p. */
Petr Machata6c2e57a2012-10-27 01:50:22 +0200417 struct opt_p_t *it;
Petr Machata590c8082011-08-20 22:45:26 +0200418 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +0100419 struct process *proc = pid2proc(it->pid);
Petr Machata590c8082011-08-20 22:45:26 +0200420 if (proc == NULL)
421 continue;
422 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100423 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200424 break;
425 }
426 }
Petr Machata74132a42012-03-16 02:46:18 +0100427 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200428 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200429 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200430}
431
432static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200433handle_stopping_event(struct pid_task *task_info, Event **eventp)
Petr Machata98f09922011-07-09 10:55:29 +0200434{
435 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200436 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200437 task_info->got_event = 1;
438
Petr Machata6c2e57a2012-10-27 01:50:22 +0200439 Event *event = *eventp;
Petr Machata98f09922011-07-09 10:55:29 +0200440
441 /* In every state, sink SIGSTOP events for tasks that it was
442 * sent to. */
443 if (task_info != NULL
444 && event->type == EVENT_SIGNAL
445 && event->e_un.signum == SIGSTOP) {
446 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
447 if (task_info->sigstopped
448 && !task_info->delivered) {
449 task_info->delivered = 1;
450 *eventp = NULL; // sink the event
451 } else
452 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
453 "sigstopped=%d and delivered=%d\n",
454 task_info->pid, task_info->sigstopped,
455 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100456 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100457}
458
Petr Machata98f09922011-07-09 10:55:29 +0200459/* Some SIGSTOPs may have not been delivered to their respective tasks
460 * yet. They are still in the queue. If we have seen an event for
461 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200462 * caught by ltrace. We don't mind that the process is after
463 * breakpoint (and therefore potentially doesn't have aligned IP),
464 * because the signal will be delivered without the process actually
465 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200466static void
Petr Machata6c2e57a2012-10-27 01:50:22 +0200467continue_for_sigstop_delivery(struct pid_set *pids)
Petr Machata98f09922011-07-09 10:55:29 +0200468{
469 size_t i;
470 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200471 if (pids->tasks[i].pid != 0
472 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200473 && !pids->tasks[i].delivered
474 && pids->tasks[i].got_event) {
475 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
476 pids->tasks[i].pid);
477 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
478 }
479 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100480}
481
Petr Machata98f09922011-07-09 10:55:29 +0200482static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200483event_exit_p(Event *event)
Petr Machata750ca8c2011-10-06 14:29:34 +0200484{
485 return event != NULL && (event->type == EVENT_EXIT
486 || event->type == EVENT_EXIT_SIGNAL);
487}
488
489static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200490event_exit_or_none_p(Event *event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200491{
Petr Machata750ca8c2011-10-06 14:29:34 +0200492 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200493 || event->type == EVENT_NONE;
494}
495
496static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200497await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
498 Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200499{
500 /* If we still didn't get our SIGSTOP, continue the process
501 * and carry on. */
502 if (event != NULL && !event_exit_or_none_p(event)
503 && task_info != NULL && task_info->sigstopped) {
504 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
505 task_info->pid);
506 /* We should get the signal the first thing
507 * after this, so it should be OK to continue
508 * even if we are over a breakpoint. */
509 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
510
511 } else {
512 /* If all SIGSTOPs were delivered, uninstall the
513 * handler and continue everyone. */
514 /* XXX I suspect that we should check tasks that are
515 * still around. Is things are now, there should be a
516 * race between waiting for everyone to stop and one
517 * of the tasks exiting. */
518 int all_clear = 1;
519 size_t i;
520 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200521 if (pids->tasks[i].pid != 0
522 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200523 && !pids->tasks[i].delivered) {
524 all_clear = 0;
525 break;
526 }
527 return all_clear;
528 }
529
530 return 0;
531}
532
Petr Machata590c8082011-08-20 22:45:26 +0200533static int
Petr Machata6c2e57a2012-10-27 01:50:22 +0200534all_stops_accountable(struct pid_set *pids)
Petr Machata590c8082011-08-20 22:45:26 +0200535{
536 size_t i;
537 for (i = 0; i < pids->count; ++i)
538 if (pids->tasks[i].pid != 0
539 && !pids->tasks[i].got_event
540 && !have_events_for(pids->tasks[i].pid))
541 return 0;
542 return 1;
543}
544
Petr Machata693dfad2013-01-14 22:10:51 +0100545#ifndef ARCH_HAVE_SW_SINGLESTEP
546enum sw_singlestep_status
547arch_sw_singlestep(struct process *proc, struct breakpoint *bp,
548 int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
549 struct sw_singlestep_data *data)
Petr Machata06986d52011-11-02 13:22:46 +0100550{
Petr Machata693dfad2013-01-14 22:10:51 +0100551 return SWS_HW;
Petr Machataa266acb2012-04-12 23:50:23 +0200552}
553#endif
554
Petr Machata42748ac2012-04-19 04:24:25 +0200555static Event *process_stopping_on_event(struct event_handler *super,
556 Event *event);
557
558static void
Petr Machata693dfad2013-01-14 22:10:51 +0100559remove_sw_breakpoints(struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200560{
Petr Machata24a47042012-04-19 18:15:08 +0200561 struct process_stopping_handler *self
562 = (void *)proc->leader->event_handler;
563 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200564 assert(self->super.on_event == process_stopping_on_event);
565
Petr Machata6e5e2de2013-01-21 22:25:34 +0100566 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200567 int i;
568 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100569 if (self->sws_bps[i] != NULL) {
570 delete_breakpoint(proc, self->sws_bps[i]->addr);
571 self->sws_bps[i] = NULL;
Petr Machata42748ac2012-04-19 04:24:25 +0200572 }
573}
574
575static void
Petr Machata693dfad2013-01-14 22:10:51 +0100576sw_singlestep_bp_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machata42748ac2012-04-19 04:24:25 +0200577{
Petr Machata693dfad2013-01-14 22:10:51 +0100578 remove_sw_breakpoints(proc);
Petr Machata42748ac2012-04-19 04:24:25 +0200579}
580
Petr Machata693dfad2013-01-14 22:10:51 +0100581struct sw_singlestep_data {
582 struct process_stopping_handler *self;
583};
584
Petr Machataa266acb2012-04-12 23:50:23 +0200585static int
Petr Machata693dfad2013-01-14 22:10:51 +0100586sw_singlestep_add_bp(arch_addr_t addr, struct sw_singlestep_data *data)
Petr Machataa266acb2012-04-12 23:50:23 +0200587{
Petr Machata693dfad2013-01-14 22:10:51 +0100588 struct process_stopping_handler *self = data->self;
Petr Machata929bd572012-12-17 03:20:34 +0100589 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200590
Petr Machata6e5e2de2013-01-21 22:25:34 +0100591 int ct = sizeof(self->sws_bps) / sizeof(*self->sws_bps);
Petr Machata42748ac2012-04-19 04:24:25 +0200592 int i;
593 for (i = 0; i < ct; ++i)
Petr Machata6e5e2de2013-01-21 22:25:34 +0100594 if (self->sws_bps[i] == NULL) {
Petr Machata42748ac2012-04-19 04:24:25 +0200595 static struct bp_callbacks cbs = {
Petr Machata693dfad2013-01-14 22:10:51 +0100596 .on_hit = sw_singlestep_bp_on_hit,
Petr Machata42748ac2012-04-19 04:24:25 +0200597 };
598 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200599 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200600 breakpoint_set_callbacks(bp, &cbs);
Petr Machata6e5e2de2013-01-21 22:25:34 +0100601 self->sws_bps[i] = bp;
Petr Machata42748ac2012-04-19 04:24:25 +0200602 return 0;
603 }
Petr Machataa266acb2012-04-12 23:50:23 +0200604
Petr Machata693dfad2013-01-14 22:10:51 +0100605 assert(!"Too many sw singlestep breakpoints!");
Petr Machata42748ac2012-04-19 04:24:25 +0200606 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200607}
608
609static int
610singlestep(struct process_stopping_handler *self)
611{
Petr Machata6e5e2de2013-01-21 22:25:34 +0100612 size_t i;
613 for (i = 0; i < sizeof(self->sws_bps) / sizeof(*self->sws_bps); ++i)
614 self->sws_bps[i] = NULL;
Petr Machataa266acb2012-04-12 23:50:23 +0200615
Petr Machata693dfad2013-01-14 22:10:51 +0100616 struct sw_singlestep_data data = { self };
Petr Machata643c6462013-01-15 17:11:43 +0100617 switch (arch_sw_singlestep(self->task_enabling_breakpoint,
618 self->breakpoint_being_enabled,
619 &sw_singlestep_add_bp, &data)) {
620 case SWS_HW:
621 /* Otherwise do the default action: singlestep. */
622 debug(1, "PTRACE_SINGLESTEP");
Petr Machata6e5e2de2013-01-21 22:25:34 +0100623 if (ptrace(PTRACE_SINGLESTEP,
624 self->task_enabling_breakpoint->pid, 0, 0)) {
Petr Machata643c6462013-01-15 17:11:43 +0100625 perror("PTRACE_SINGLESTEP");
626 return -1;
627 }
628 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200629
Petr Machata643c6462013-01-15 17:11:43 +0100630 case SWS_OK:
631 return 0;
Petr Machataa266acb2012-04-12 23:50:23 +0200632
Petr Machata643c6462013-01-15 17:11:43 +0100633 case SWS_FAIL:
Petr Machataa266acb2012-04-12 23:50:23 +0200634 return -1;
635 }
Petr Machata643c6462013-01-15 17:11:43 +0100636 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200637}
638
639static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200640post_singlestep(struct process_stopping_handler *self,
641 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200642{
643 continue_for_sigstop_delivery(&self->pids);
644
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200645 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200646 *eventp = NULL; // handled
647
Petr Machata929bd572012-12-17 03:20:34 +0100648 struct process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200649
Petr Machata693dfad2013-01-14 22:10:51 +0100650 remove_sw_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200651 self->breakpoint_being_enabled = NULL;
652}
653
654static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200655singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200656{
Petr Machata929bd572012-12-17 03:20:34 +0100657 struct process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200658 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200659 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataba1664b2012-04-28 14:59:05 +0200660 teb->pid, breakpoint_name(sbp), sbp->addr,
661 get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200662 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100663}
664
Petr Machata9e1e9692012-04-19 02:29:38 +0200665static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200666pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200667{
Petr Machata929bd572012-12-17 03:20:34 +0100668 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200669 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200670 ptrace(PTRACE_CONT, teb->pid, 0, 0);
671}
672
673static void
674pt_singlestep(struct process_stopping_handler *self)
675{
676 if (singlestep(self) < 0)
677 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200678}
679
680static void
681disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200682 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200683{
Petr Machata929bd572012-12-17 03:20:34 +0100684 struct process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200685 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200686 if (self->breakpoint_being_enabled->enabled)
687 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200688 (do_this)(self);
Petr Machata96cb8e32012-12-17 03:49:41 +0100689 self->state = PSH_SINGLESTEP;
Petr Machatac1aa9582012-03-27 23:54:01 +0200690}
691
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200692void
693linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
694{
Petr Machata9e1e9692012-04-19 02:29:38 +0200695 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200696}
697
698void
699linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
700{
701 disable_and(self, &pt_continue);
702}
703
Petr Machata98f09922011-07-09 10:55:29 +0200704/* This event handler is installed when we are in the process of
705 * stopping the whole thread group to do the pointer re-enablement for
706 * one of the threads. We pump all events to the queue for later
707 * processing while we wait for all the threads to stop. When this
708 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
709 * re-enable, and continue everyone. */
710static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100711process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200712{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200713 struct process_stopping_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100714 struct process *task = event->proc;
715 struct process *leader = task->leader;
716 struct process *teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200717
718 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200719 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200720 task->pid, event->type, self->state);
721
Petr Machata6c2e57a2012-10-27 01:50:22 +0200722 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200723 if (task_info == NULL)
724 fprintf(stderr, "new task??? %d\n", task->pid);
725 handle_stopping_event(task_info, &event);
726
727 int state = self->state;
728 int event_to_queue = !event_exit_or_none_p(event);
729
Petr Machata18c97072011-10-06 14:30:11 +0200730 /* Deactivate the entry if the task exits. */
731 if (event_exit_p(event) && task_info != NULL)
732 task_info->pid = 0;
733
Petr Machata43d2fe52011-11-02 13:25:49 +0100734 /* Always handle sysrets. Whether sysret occurred and what
735 * sys it rets from may need to be determined based on process
736 * stack, so we need to keep that in sync with reality. Note
737 * that we don't continue the process after the sysret is
738 * handled. See continue_after_syscall. */
739 if (event != NULL && event->type == EVENT_SYSRET) {
740 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
741 event_to_queue = 0;
Petr Machata754ce882013-01-08 23:41:47 +0100742 if (task_info != NULL)
743 task_info->sysret = 1;
Petr Machata43d2fe52011-11-02 13:25:49 +0100744 }
745
Petr Machata98f09922011-07-09 10:55:29 +0200746 switch (state) {
Petr Machata96cb8e32012-12-17 03:49:41 +0100747 case PSH_STOPPING:
Petr Machata98f09922011-07-09 10:55:29 +0200748 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100749 if (each_task(leader, NULL, &task_blocked,
750 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200751 (self->on_all_stopped)(self);
752 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200753 }
754 break;
755
Petr Machata96cb8e32012-12-17 03:49:41 +0100756 case PSH_SINGLESTEP:
Petr Machata98f09922011-07-09 10:55:29 +0200757 /* In singlestep state, breakpoint signifies that we
758 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200759 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200760
Petr Machata42748ac2012-04-19 04:24:25 +0200761 /* If this was caused by a real breakpoint, as
762 * opposed to a singlestep, assume that it's
763 * an artificial breakpoint installed for some
764 * reason for the re-enablement. In that case
765 * handle it. */
766 if (event->type == EVENT_BREAKPOINT) {
Petr Machatabac2da52012-05-29 00:42:59 +0200767 arch_addr_t ip
Petr Machata42748ac2012-04-19 04:24:25 +0200768 = get_instruction_pointer(task);
769 struct breakpoint *other
770 = address2bpstruct(leader, ip);
771 if (other != NULL)
772 breakpoint_on_hit(other, task);
773 }
774
Petr Machata36f40e72012-03-28 02:07:19 +0200775 /* If we got SIGNAL instead of BREAKPOINT,
776 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200777 if (event->type == EVENT_SIGNAL) {
778 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200779 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200780 singlestep_error(self);
781 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200782 goto psh_sinking;
783 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200784 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200785 } else {
786 switch ((self->keep_stepping_p)(self)) {
787 case CBS_FAIL:
788 /* XXX handle me */
789 case CBS_STOP:
790 break;
791 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200792 /* Sink singlestep event. */
793 if (event->type == EVENT_BREAKPOINT)
794 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200795 goto do_singlestep;
796 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200797 }
798
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200799 /* Re-enable the breakpoint that we are
800 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200801 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200802 if (sbp->enabled)
803 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200804
Petr Machataa266acb2012-04-12 23:50:23 +0200805 post_singlestep(self, &event);
806 goto psh_sinking;
807 }
808 break;
Petr Machata98f09922011-07-09 10:55:29 +0200809
Petr Machataa266acb2012-04-12 23:50:23 +0200810 psh_sinking:
Petr Machata96cb8e32012-12-17 03:49:41 +0100811 state = self->state = PSH_SINKING;
812 /* Fall through. */
813 case PSH_SINKING:
Petr Machata98f09922011-07-09 10:55:29 +0200814 if (await_sigstop_delivery(&self->pids, task_info, event))
815 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200816 break;
817
Petr Machata96cb8e32012-12-17 03:49:41 +0100818 case PSH_UGLY_WORKAROUND:
Petr Machata590c8082011-08-20 22:45:26 +0200819 if (event == NULL)
820 break;
821 if (event->type == EVENT_BREAKPOINT) {
822 undo_breakpoint(event, leader);
823 if (task == teb)
824 self->task_enabling_breakpoint = NULL;
825 }
826 if (self->task_enabling_breakpoint == NULL
827 && all_stops_accountable(&self->pids)) {
828 undo_breakpoint(event, leader);
829 detach_process(leader);
830 event = NULL; // handled
831 }
Petr Machata98f09922011-07-09 10:55:29 +0200832 }
833
834 if (event != NULL && event_to_queue) {
835 enque_event(event);
836 event = NULL; // sink the event
837 }
838
839 return event;
840}
841
842static void
Petr Machata366c2f42012-02-09 19:34:36 +0100843process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200844{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200845 struct process_stopping_handler *self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200846 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100847}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100848
Petr Machata36f40e72012-03-28 02:07:19 +0200849static enum callback_status
850no(struct process_stopping_handler *self)
851{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200852 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200853}
854
Petr Machatac1aa9582012-03-27 23:54:01 +0200855int
Petr Machata929bd572012-12-17 03:20:34 +0100856process_install_stopping_handler(struct process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200857 void (*as)(struct process_stopping_handler *),
858 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200859 (struct process_stopping_handler *),
860 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200861 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200862{
Petr Machata9ace5742012-04-14 02:29:57 +0200863 debug(DEBUG_FUNCTION,
864 "process_install_stopping_handler: pid=%d", proc->pid);
865
Petr Machatac1aa9582012-03-27 23:54:01 +0200866 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
867 if (handler == NULL)
868 return -1;
869
Petr Machata36f40e72012-03-28 02:07:19 +0200870 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200871 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200872 if (ks == NULL)
873 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200874 if (uw == NULL)
875 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200876
Petr Machatac1aa9582012-03-27 23:54:01 +0200877 handler->super.on_event = process_stopping_on_event;
878 handler->super.destroy = process_stopping_destroy;
879 handler->task_enabling_breakpoint = proc;
880 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200881 handler->on_all_stopped = as;
882 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200883 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200884
885 install_event_handler(proc->leader, &handler->super);
886
887 if (each_task(proc->leader, NULL, &send_sigstop,
888 &handler->pids) != NULL) {
889 destroy_event_handler(proc);
890 return -1;
891 }
892
893 /* And deliver the first fake event, in case all the
894 * conditions are already fulfilled. */
895 Event ev = {
896 .type = EVENT_NONE,
897 .proc = proc,
898 };
899 process_stopping_on_event(&handler->super, &ev);
900
901 return 0;
902}
903
Juan Cespedesf1350522008-12-16 18:19:58 +0100904void
Petr Machata929bd572012-12-17 03:20:34 +0100905continue_after_breakpoint(struct process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200906{
Petr Machata9ace5742012-04-14 02:29:57 +0200907 debug(DEBUG_PROCESS,
908 "continue_after_breakpoint: pid=%d, addr=%p",
909 proc->pid, sbp->addr);
910
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200911 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200912
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100913 if (sbp->enabled == 0) {
914 continue_process(proc->pid);
Petr Machataa9d6a672013-03-05 17:10:37 +0100915 } else if (process_install_stopping_handler
916 (proc, sbp, NULL, NULL, NULL) < 0) {
917 perror("process_stopping_handler_create");
918 /* Carry on not bothering to re-enable. */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200919 continue_process(proc->pid);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100920 }
921}
922
Petr Machata602330f2011-07-09 11:15:34 +0200923/**
924 * Ltrace exit. When we are about to exit, we have to go through all
925 * the processes, stop them all, remove all the breakpoints, and then
926 * detach the processes that we attached to using -p. If we left the
927 * other tasks running, they might hit stray return breakpoints and
928 * produce artifacts, so we better stop everyone, even if it's a bit
929 * of extra work.
930 */
931struct ltrace_exiting_handler
932{
Petr Machata366c2f42012-02-09 19:34:36 +0100933 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200934 struct pid_set pids;
935};
936
Petr Machata602330f2011-07-09 11:15:34 +0200937static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100938ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200939{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200940 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata929bd572012-12-17 03:20:34 +0100941 struct process *task = event->proc;
942 struct process *leader = task->leader;
Petr Machata602330f2011-07-09 11:15:34 +0200943
Petr Machata9ace5742012-04-14 02:29:57 +0200944 debug(DEBUG_PROCESS,
945 "ltrace_exiting_on_event: pid %d; event type %d",
946 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200947
Petr Machata6c2e57a2012-10-27 01:50:22 +0200948 struct pid_task *task_info = get_task_info(&self->pids, task->pid);
Petr Machata602330f2011-07-09 11:15:34 +0200949 handle_stopping_event(task_info, &event);
950
Petr Machata590c8082011-08-20 22:45:26 +0200951 if (event != NULL && event->type == EVENT_BREAKPOINT)
952 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200953
954 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200955 && all_stops_accountable(&self->pids))
956 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200957
958 /* Sink all non-exit events. We are about to exit, so we
959 * don't bother with queuing them. */
960 if (event_exit_or_none_p(event))
961 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200962
Petr Machata13d5df72011-08-19 23:15:15 +0200963 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200964}
965
966static void
Petr Machata366c2f42012-02-09 19:34:36 +0100967ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200968{
Petr Machata6c2e57a2012-10-27 01:50:22 +0200969 struct ltrace_exiting_handler *self = (void *)super;
Petr Machata602330f2011-07-09 11:15:34 +0200970 free(self->pids.tasks);
971}
972
973static int
Petr Machata929bd572012-12-17 03:20:34 +0100974ltrace_exiting_install_handler(struct process *proc)
Petr Machata602330f2011-07-09 11:15:34 +0200975{
976 /* Only install to leader. */
977 if (proc->leader != proc)
978 return 0;
979
980 /* Perhaps we are already installed, if the user passed
981 * several -p options that are tasks of one process. */
982 if (proc->event_handler != NULL
983 && proc->event_handler->on_event == &ltrace_exiting_on_event)
984 return 0;
985
Petr Machata590c8082011-08-20 22:45:26 +0200986 /* If stopping handler is already present, let it do the
987 * work. */
988 if (proc->event_handler != NULL) {
989 assert(proc->event_handler->on_event
990 == &process_stopping_on_event);
Petr Machata6c2e57a2012-10-27 01:50:22 +0200991 struct process_stopping_handler *other
Petr Machata590c8082011-08-20 22:45:26 +0200992 = (void *)proc->event_handler;
993 other->exiting = 1;
994 return 0;
995 }
996
Petr Machata6c2e57a2012-10-27 01:50:22 +0200997 struct ltrace_exiting_handler *handler
Petr Machata602330f2011-07-09 11:15:34 +0200998 = calloc(sizeof(*handler), 1);
999 if (handler == NULL) {
1000 perror("malloc exiting handler");
1001 fatal:
1002 /* XXXXXXXXXXXXXXXXXXX fixme */
1003 return -1;
1004 }
1005
Petr Machata602330f2011-07-09 11:15:34 +02001006 handler->super.on_event = ltrace_exiting_on_event;
1007 handler->super.destroy = ltrace_exiting_destroy;
1008 install_event_handler(proc->leader, &handler->super);
1009
Petr Machata74132a42012-03-16 02:46:18 +01001010 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001011 &handler->pids) != NULL)
1012 goto fatal;
1013
1014 return 0;
1015}
1016
Petr Machatacbe29c62011-09-27 02:27:58 +02001017/*
1018 * When the traced process vforks, it's suspended until the child
1019 * process calls _exit or exec*. In the meantime, the two share the
1020 * address space.
1021 *
1022 * The child process should only ever call _exit or exec*, but we
1023 * can't count on that (it's not the role of ltrace to policy, but to
1024 * observe). In any case, we will _at least_ have to deal with
1025 * removal of vfork return breakpoint (which we have to smuggle back
1026 * in, so that the parent can see it, too), and introduction of exec*
1027 * return breakpoint. Since we already have both breakpoint actions
1028 * to deal with, we might as well support it all.
1029 *
1030 * The gist is that we pretend that the child is in a thread group
1031 * with its parent, and handle it as a multi-threaded case, with the
1032 * exception that we know that the parent is blocked, and don't
1033 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001034 */
1035
Petr Machata134a1082011-09-27 20:25:58 +02001036struct process_vfork_handler
1037{
Petr Machata366c2f42012-02-09 19:34:36 +01001038 struct event_handler super;
Petr Machatad7e4ca82012-11-28 03:38:47 +01001039 arch_addr_t bp_addr;
Petr Machata134a1082011-09-27 20:25:58 +02001040};
1041
Petr Machatacbe29c62011-09-27 02:27:58 +02001042static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001043process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001044{
Petr Machata9ace5742012-04-14 02:29:57 +02001045 debug(DEBUG_PROCESS,
1046 "process_vfork_on_event: pid %d; event type %d",
1047 event->proc->pid, event->type);
1048
Petr Machata6c2e57a2012-10-27 01:50:22 +02001049 struct process_vfork_handler *self = (void *)super;
Petr Machatacbe29c62011-09-27 02:27:58 +02001050 assert(self != NULL);
1051
1052 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001053 case EVENT_BREAKPOINT:
1054 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001055 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001056 self->bp_addr = event->e_un.brk_addr;
1057 break;
1058
Petr Machatacbe29c62011-09-27 02:27:58 +02001059 case EVENT_EXIT:
1060 case EVENT_EXIT_SIGNAL:
1061 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001062 /* Smuggle back in the vfork return breakpoint, so
1063 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001064 if (self->bp_addr != 0) {
Petr Machata98ff3092013-03-08 22:11:36 +01001065 struct breakpoint *found;
1066 if (DICT_FIND_VAL(event->proc->leader->breakpoints,
1067 &self->bp_addr, &found) == 0)
1068 assert(found->libsym == NULL);
Petr Machata77e8fa82012-04-19 18:36:32 +02001069 /* We don't mind failing that, it's not a big
1070 * deal to not display one extra vfork return. */
1071 insert_breakpoint(event->proc->parent,
1072 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001073 }
1074
Petr Machataba9911f2011-09-27 21:09:47 +02001075 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001076
1077 /* Remove the leader that we artificially set up
1078 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001079 change_process_leader(event->proc, event->proc);
1080 destroy_event_handler(event->proc);
1081
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}