blob: 809714f1394c85795e13bdcca4fae02f5e75c8fa [file] [log] [blame]
Petr Machatacec06ec2012-04-10 13:31:55 +02001#include "config.h"
2
3#include <asm/unistd.h>
4#include <sys/types.h>
5#include <sys/wait.h>
6#include <assert.h>
7#include <errno.h>
Petr Machatad3dc17b2012-03-21 03:23:25 +01008#include <error.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010010#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010011#include <string.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +010012#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013
Petr Machatacec06ec2012-04-10 13:31:55 +020014#ifdef HAVE_LIBSELINUX
15# include <selinux/selinux.h>
16#endif
17
18#include "ptrace.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020019#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010020#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010021#include "proc.h"
Petr Machata92822542012-03-28 00:31:14 +020022#include "linux-gnu/trace.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020023
24/* If the system headers did not provide the constants, hard-code the normal
25 values. */
26#ifndef PTRACE_EVENT_FORK
27
28#define PTRACE_OLDSETOPTIONS 21
29#define PTRACE_SETOPTIONS 0x4200
30#define PTRACE_GETEVENTMSG 0x4201
31
32/* options set using PTRACE_SETOPTIONS */
33#define PTRACE_O_TRACESYSGOOD 0x00000001
34#define PTRACE_O_TRACEFORK 0x00000002
35#define PTRACE_O_TRACEVFORK 0x00000004
36#define PTRACE_O_TRACECLONE 0x00000008
37#define PTRACE_O_TRACEEXEC 0x00000010
38#define PTRACE_O_TRACEVFORKDONE 0x00000020
39#define PTRACE_O_TRACEEXIT 0x00000040
40
41/* Wait extended result codes for the above trace options. */
42#define PTRACE_EVENT_FORK 1
43#define PTRACE_EVENT_VFORK 2
44#define PTRACE_EVENT_CLONE 3
45#define PTRACE_EVENT_EXEC 4
46#define PTRACE_EVENT_VFORK_DONE 5
47#define PTRACE_EVENT_EXIT 6
48
49#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010050
Luis Machado55c5feb2008-03-12 15:56:01 +010051#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020052extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010053int
Juan Cespedesa8909f72009-04-28 20:02:41 +020054umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010055 return arch_umovelong (proc, addr, result, info);
56}
57#else
58/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010059int
Juan Cespedesa8909f72009-04-28 20:02:41 +020060umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010061 long pointed_to;
62
63 errno = 0;
64 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
65 if (pointed_to == -1 && errno)
66 return -errno;
67
68 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050069 if (info) {
70 switch(info->type) {
71 case ARGTYPE_INT:
72 *result &= 0x00000000ffffffffUL;
73 default:
74 break;
75 };
76 }
Luis Machado55c5feb2008-03-12 15:56:01 +010077 return 0;
78}
79#endif
80
Juan Cespedesf1350522008-12-16 18:19:58 +010081void
Petr Machatacec06ec2012-04-10 13:31:55 +020082trace_fail_warning(pid_t pid)
83{
84 /* This was adapted from GDB. */
85#ifdef HAVE_LIBSELINUX
86 static int checked = 0;
87 if (checked)
88 return;
89 checked = 1;
90
91 /* -1 is returned for errors, 0 if it has no effect, 1 if
92 * PTRACE_ATTACH is forbidden. */
93 if (security_get_boolean_active("deny_ptrace") == 1)
94 fprintf(stderr,
95"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
96"tracing other processes. You can disable this process attach protection by\n"
97"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
98#endif /* HAVE_LIBSELINUX */
99}
100
101void
102trace_me(void)
103{
Petr Machata26627682011-07-08 18:15:32 +0200104 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100106 perror("PTRACE_TRACEME");
Petr Machatacec06ec2012-04-10 13:31:55 +0200107 trace_fail_warning(getpid());
Juan Cespedes5e01f651998-03-08 22:31:44 +0100108 exit(1);
109 }
110}
111
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100112/* There's a (hopefully) brief period of time after the child process
Petr Machatac805c622012-03-02 00:10:37 +0100113 * forks when we can't trace it yet. Here we wait for kernel to
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100114 * prepare the process. */
Petr Machatac805c622012-03-02 00:10:37 +0100115int
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100116wait_for_proc(pid_t pid)
117{
Petr Machatac805c622012-03-02 00:10:37 +0100118 /* man ptrace: PTRACE_ATTACH attaches to the process specified
119 in pid. The child is sent a SIGSTOP, but will not
120 necessarily have stopped by the completion of this call;
121 use wait() to wait for the child to stop. */
122 if (waitpid(pid, NULL, __WALL) != pid) {
123 perror ("trace_pid: waitpid");
124 return -1;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100125 }
126
Petr Machatac805c622012-03-02 00:10:37 +0100127 return 0;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100128}
129
Juan Cespedesf1350522008-12-16 18:19:58 +0100130int
Petr Machatacec06ec2012-04-10 13:31:55 +0200131trace_pid(pid_t pid)
132{
Petr Machata26627682011-07-08 18:15:32 +0200133 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Petr Machatacec06ec2012-04-10 13:31:55 +0200134 /* This shouldn't emit error messages, as there are legitimate
135 * reasons that the PID can't be attached: like it may have
136 * already ended. */
137 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0)
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100138 return -1;
Petr Machata89a53602007-01-25 18:05:44 +0100139
Petr Machatac805c622012-03-02 00:10:37 +0100140 return wait_for_proc(pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100141}
142
Juan Cespedesf1350522008-12-16 18:19:58 +0100143void
Petr Machata3ed2a422012-04-06 17:18:55 +0200144trace_set_options(struct Process *proc)
145{
Ian Wienand9a2ad352006-02-20 22:44:45 +0100146 if (proc->tracesysgood & 0x80)
147 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200148
Petr Machata3ed2a422012-04-06 17:18:55 +0200149 pid_t pid = proc->pid;
Petr Machata26627682011-07-08 18:15:32 +0200150 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200151
Juan Cespedes1e583132009-04-07 18:17:11 +0200152 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
153 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
154 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200155 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
156 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100157 perror("PTRACE_SETOPTIONS");
158 return;
159 }
160 proc->tracesysgood |= 0x80;
161}
162
Juan Cespedesf1350522008-12-16 18:19:58 +0100163void
164untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200165 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100166 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100167}
168
Juan Cespedesf1350522008-12-16 18:19:58 +0100169void
170continue_after_signal(pid_t pid, int signum) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200171 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
Petr Machata98f09922011-07-09 10:55:29 +0200172 ptrace(PTRACE_SYSCALL, pid, 0, signum);
173}
174
175static enum ecb_status
176event_for_pid(Event * event, void * data)
177{
178 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
179 return ecb_yield;
180 return ecb_cont;
181}
182
183static int
184have_events_for(pid_t pid)
185{
186 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
187}
188
189void
190continue_process(pid_t pid)
191{
192 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200193
194 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200195 the queue for this process. Otherwise just wait for the
196 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200197 if (!have_events_for(pid))
198 /* We always trace syscalls to control fork(),
199 * clone(), execve()... */
200 ptrace(PTRACE_SYSCALL, pid, 0, 0);
201 else
202 debug(DEBUG_PROCESS,
203 "putting off the continue, events in que.");
204}
205
Petr Machata98f09922011-07-09 10:55:29 +0200206static struct pid_task *
207get_task_info(struct pid_set * pids, pid_t pid)
208{
Petr Machata750ca8c2011-10-06 14:29:34 +0200209 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200210 size_t i;
211 for (i = 0; i < pids->count; ++i)
212 if (pids->tasks[i].pid == pid)
213 return &pids->tasks[i];
214
215 return NULL;
216}
217
218static struct pid_task *
219add_task_info(struct pid_set * pids, pid_t pid)
220{
221 if (pids->count == pids->alloc) {
222 size_t ns = (2 * pids->alloc) ?: 4;
223 struct pid_task * n = realloc(pids->tasks,
224 sizeof(*pids->tasks) * ns);
225 if (n == NULL)
226 return NULL;
227 pids->tasks = n;
228 pids->alloc = ns;
229 }
230 struct pid_task * task_info = &pids->tasks[pids->count++];
231 memset(task_info, 0, sizeof(*task_info));
232 task_info->pid = pid;
233 return task_info;
234}
235
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236static enum callback_status
237task_stopped(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200238{
239 enum process_status st = process_status(task->pid);
240 if (data != NULL)
241 *(enum process_status *)data = st;
242
243 /* If the task is already stopped, don't worry about it.
244 * Likewise if it managed to become a zombie or terminate in
245 * the meantime. This can happen when the whole thread group
246 * is terminating. */
247 switch (st) {
248 case ps_invalid:
249 case ps_tracing_stop:
250 case ps_zombie:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 return CBS_CONT;
Petr Machataffe4cd22012-04-11 18:01:44 +0200252 case ps_sleeping:
Petr Machata36d19822011-10-21 16:03:45 +0200253 case ps_stop:
254 case ps_other:
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 return CBS_STOP;
Petr Machatacbe29c62011-09-27 02:27:58 +0200256 }
Petr Machata36d19822011-10-21 16:03:45 +0200257
258 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200259}
260
261/* Task is blocked if it's stopped, or if it's a vfork parent. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100262static enum callback_status
263task_blocked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200264{
265 struct pid_set * pids = data;
266 struct pid_task * task_info = get_task_info(pids, task->pid);
267 if (task_info != NULL
268 && task_info->vforked)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200270
271 return task_stopped(task, NULL);
272}
273
Petr Machata366c2f42012-02-09 19:34:36 +0100274static Event *process_vfork_on_event(struct event_handler *super, Event *event);
Petr Machatacbe29c62011-09-27 02:27:58 +0200275
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276static enum callback_status
277task_vforked(struct Process *task, void *data)
Petr Machatacbe29c62011-09-27 02:27:58 +0200278{
279 if (task->event_handler != NULL
280 && task->event_handler->on_event == &process_vfork_on_event)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100281 return CBS_STOP;
282 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200283}
284
285static int
Petr Machata74132a42012-03-16 02:46:18 +0100286is_vfork_parent(struct Process *task)
Petr Machatacbe29c62011-09-27 02:27:58 +0200287{
Petr Machata74132a42012-03-16 02:46:18 +0100288 return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
Petr Machatacbe29c62011-09-27 02:27:58 +0200289}
290
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291static enum callback_status
292send_sigstop(struct Process *task, void *data)
Petr Machata98f09922011-07-09 10:55:29 +0200293{
294 Process * leader = task->leader;
295 struct pid_set * pids = data;
296
297 /* Look for pre-existing task record, or add new. */
298 struct pid_task * task_info = get_task_info(pids, task->pid);
299 if (task_info == NULL)
300 task_info = add_task_info(pids, task->pid);
301 if (task_info == NULL) {
302 perror("send_sigstop: add_task_info");
303 destroy_event_handler(leader);
304 /* Signal failure upwards. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 return CBS_STOP;
Petr Machata98f09922011-07-09 10:55:29 +0200306 }
307
308 /* This task still has not been attached to. It should be
309 stopped by the kernel. */
310 if (task->state == STATE_BEING_CREATED)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100311 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200312
313 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200314 * if we sent the SIGSTOP already, which happens when we are
315 * handling "onexit" and inherited the handler from breakpoint
316 * re-enablement. */
317 enum process_status st;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 if (task_stopped(task, &st) == CBS_CONT)
319 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200320 if (task_info->sigstopped) {
321 if (!task_info->delivered)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200323 task_info->delivered = 0;
324 }
325
Petr Machatacbe29c62011-09-27 02:27:58 +0200326 /* Also don't attempt to stop the process if it's a parent of
327 * vforked process. We set up event handler specially to hint
328 * us. In that case parent is in D state, which we use to
329 * weed out unnecessary looping. */
330 if (st == ps_sleeping
331 && is_vfork_parent (task)) {
332 task_info->vforked = 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333 return CBS_CONT;
Petr Machatacbe29c62011-09-27 02:27:58 +0200334 }
335
Petr Machata98f09922011-07-09 10:55:29 +0200336 if (task_kill(task->pid, SIGSTOP) >= 0) {
337 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
338 task_info->sigstopped = 1;
339 } else
340 fprintf(stderr,
341 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
342
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 return CBS_CONT;
Petr Machata98f09922011-07-09 10:55:29 +0200344}
345
Petr Machata73894bd2011-08-20 23:47:34 +0200346/* On certain kernels, detaching right after a singlestep causes the
347 tracee to be killed with a SIGTRAP (that even though the singlestep
348 was properly caught by waitpid. The ugly workaround is to put a
349 breakpoint where IP points and let the process continue. After
350 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200351static void
Petr Machata73894bd2011-08-20 23:47:34 +0200352ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200353{
354 void * ip = get_instruction_pointer(proc);
Petr Machatabc373262012-02-07 23:31:15 +0100355 struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
Petr Machata590c8082011-08-20 22:45:26 +0200356 if (sbp != NULL)
357 enable_breakpoint(proc, sbp);
358 else
Petr Machata9df15012012-02-20 12:49:46 +0100359 insert_breakpoint(proc, ip, NULL);
Petr Machata73894bd2011-08-20 23:47:34 +0200360 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200361}
362
363static void
Petr Machata98f09922011-07-09 10:55:29 +0200364process_stopping_done(struct process_stopping_handler * self, Process * leader)
365{
366 debug(DEBUG_PROCESS, "process stopping done %d",
367 self->task_enabling_breakpoint->pid);
368 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200369 if (!self->exiting) {
370 for (i = 0; i < self->pids.count; ++i)
371 if (self->pids.tasks[i].pid != 0
Petr Machata43d2fe52011-11-02 13:25:49 +0100372 && (self->pids.tasks[i].delivered
373 || self->pids.tasks[i].sysret))
Petr Machata590c8082011-08-20 22:45:26 +0200374 continue_process(self->pids.tasks[i].pid);
375 continue_process(self->task_enabling_breakpoint->pid);
376 destroy_event_handler(leader);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200377 }
378
379 if (self->exiting) {
380 ugly_workaround:
Petr Machata590c8082011-08-20 22:45:26 +0200381 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200382 ugly_workaround(self->task_enabling_breakpoint);
Petr Machatacb9a28d2012-03-28 11:11:32 +0200383 } else {
384 switch ((self->ugly_workaround_p)(self)) {
385 case CBS_FAIL:
386 /* xxx handle me */
387 case CBS_STOP:
388 break;
389 case CBS_CONT:
390 goto ugly_workaround;
391 }
Petr Machata590c8082011-08-20 22:45:26 +0200392 }
393}
394
395/* Before we detach, we need to make sure that task's IP is on the
396 * edge of an instruction. So for tasks that have a breakpoint event
397 * in the queue, we adjust the instruction pointer, just like
398 * continue_after_breakpoint does. */
399static enum ecb_status
400undo_breakpoint(Event * event, void * data)
401{
402 if (event != NULL
403 && event->proc->leader == data
404 && event->type == EVENT_BREAKPOINT)
405 set_instruction_pointer(event->proc, event->e_un.brk_addr);
406 return ecb_cont;
407}
408
Petr Machata2b46cfc2012-02-18 11:17:29 +0100409static enum callback_status
410untrace_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200411{
412 if (task != data)
413 untrace_pid(task->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100414 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200415}
416
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417static enum callback_status
418remove_task(struct Process *task, void *data)
Petr Machata590c8082011-08-20 22:45:26 +0200419{
420 /* Don't untrace leader just yet. */
421 if (task != data)
422 remove_process(task);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100423 return CBS_CONT;
Petr Machata590c8082011-08-20 22:45:26 +0200424}
425
426static void
427detach_process(Process * leader)
428{
429 each_qd_event(&undo_breakpoint, leader);
430 disable_all_breakpoints(leader);
431
432 /* Now untrace the process, if it was attached to by -p. */
433 struct opt_p_t * it;
434 for (it = opt_p; it != NULL; it = it->next) {
435 Process * proc = pid2proc(it->pid);
436 if (proc == NULL)
437 continue;
438 if (proc->leader == leader) {
Petr Machata74132a42012-03-16 02:46:18 +0100439 each_task(leader, NULL, &untrace_task, NULL);
Petr Machata590c8082011-08-20 22:45:26 +0200440 break;
441 }
442 }
Petr Machata74132a42012-03-16 02:46:18 +0100443 each_task(leader, NULL, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200444 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200445 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200446}
447
448static void
449handle_stopping_event(struct pid_task * task_info, Event ** eventp)
450{
451 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200452 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200453 task_info->got_event = 1;
454
455 Event * event = *eventp;
456
457 /* In every state, sink SIGSTOP events for tasks that it was
458 * sent to. */
459 if (task_info != NULL
460 && event->type == EVENT_SIGNAL
461 && event->e_un.signum == SIGSTOP) {
462 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
463 if (task_info->sigstopped
464 && !task_info->delivered) {
465 task_info->delivered = 1;
466 *eventp = NULL; // sink the event
467 } else
468 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
469 "sigstopped=%d and delivered=%d\n",
470 task_info->pid, task_info->sigstopped,
471 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100472 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100473}
474
Petr Machata98f09922011-07-09 10:55:29 +0200475/* Some SIGSTOPs may have not been delivered to their respective tasks
476 * yet. They are still in the queue. If we have seen an event for
477 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200478 * caught by ltrace. We don't mind that the process is after
479 * breakpoint (and therefore potentially doesn't have aligned IP),
480 * because the signal will be delivered without the process actually
481 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200482static void
483continue_for_sigstop_delivery(struct pid_set * pids)
484{
485 size_t i;
486 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200487 if (pids->tasks[i].pid != 0
488 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200489 && !pids->tasks[i].delivered
490 && pids->tasks[i].got_event) {
491 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
492 pids->tasks[i].pid);
493 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
494 }
495 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100496}
497
Petr Machata98f09922011-07-09 10:55:29 +0200498static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200499event_exit_p(Event * event)
500{
501 return event != NULL && (event->type == EVENT_EXIT
502 || event->type == EVENT_EXIT_SIGNAL);
503}
504
505static int
Petr Machata98f09922011-07-09 10:55:29 +0200506event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200507{
Petr Machata750ca8c2011-10-06 14:29:34 +0200508 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200509 || event->type == EVENT_NONE;
510}
511
512static int
513await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
514 Event * event)
515{
516 /* If we still didn't get our SIGSTOP, continue the process
517 * and carry on. */
518 if (event != NULL && !event_exit_or_none_p(event)
519 && task_info != NULL && task_info->sigstopped) {
520 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
521 task_info->pid);
522 /* We should get the signal the first thing
523 * after this, so it should be OK to continue
524 * even if we are over a breakpoint. */
525 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
526
527 } else {
528 /* If all SIGSTOPs were delivered, uninstall the
529 * handler and continue everyone. */
530 /* XXX I suspect that we should check tasks that are
531 * still around. Is things are now, there should be a
532 * race between waiting for everyone to stop and one
533 * of the tasks exiting. */
534 int all_clear = 1;
535 size_t i;
536 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200537 if (pids->tasks[i].pid != 0
538 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200539 && !pids->tasks[i].delivered) {
540 all_clear = 0;
541 break;
542 }
543 return all_clear;
544 }
545
546 return 0;
547}
548
Petr Machata590c8082011-08-20 22:45:26 +0200549static int
550all_stops_accountable(struct pid_set * pids)
551{
552 size_t i;
553 for (i = 0; i < pids->count; ++i)
554 if (pids->tasks[i].pid != 0
555 && !pids->tasks[i].got_event
556 && !have_events_for(pids->tasks[i].pid))
557 return 0;
558 return 1;
559}
560
Petr Machataa266acb2012-04-12 23:50:23 +0200561/* The protocol is: 0 for success, negative for failure, positive if
562 * default singlestep is to be used. */
Petr Machata9e1e9692012-04-19 02:29:38 +0200563int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200564 int (*add_cb)(void *addr, void *data),
565 void *add_cb_data);
566
567#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
568int
Petr Machata9e1e9692012-04-19 02:29:38 +0200569arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
Petr Machataa266acb2012-04-12 23:50:23 +0200570 int (*add_cb)(void *addr, void *data),
571 void *add_cb_data)
Petr Machata06986d52011-11-02 13:22:46 +0100572{
Petr Machataa266acb2012-04-12 23:50:23 +0200573 return 1;
574}
575#endif
576
Petr Machata42748ac2012-04-19 04:24:25 +0200577static Event *process_stopping_on_event(struct event_handler *super,
578 Event *event);
579
580static void
581remove_atomic_breakpoints(struct Process *proc)
582{
Petr Machata24a47042012-04-19 18:15:08 +0200583 struct process_stopping_handler *self
584 = (void *)proc->leader->event_handler;
585 assert(self != NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200586 assert(self->super.on_event == process_stopping_on_event);
587
588 int ct = sizeof(self->atomic_skip_bp_addrs)
589 / sizeof(*self->atomic_skip_bp_addrs);
590 int i;
591 for (i = 0; i < ct; ++i)
592 if (self->atomic_skip_bp_addrs[i] != 0) {
Petr Machata622581f2012-04-23 19:40:40 +0200593 delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
Petr Machata42748ac2012-04-19 04:24:25 +0200594 self->atomic_skip_bp_addrs[i] = 0;
595 }
596}
597
598static void
599atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc)
600{
601 remove_atomic_breakpoints(proc);
602}
603
Petr Machataa266acb2012-04-12 23:50:23 +0200604static int
605atomic_singlestep_add_bp(void *addr, void *data)
606{
607 struct process_stopping_handler *self = data;
608 struct Process *proc = self->task_enabling_breakpoint;
609
Petr Machata42748ac2012-04-19 04:24:25 +0200610 int ct = sizeof(self->atomic_skip_bp_addrs)
611 / sizeof(*self->atomic_skip_bp_addrs);
612 int i;
613 for (i = 0; i < ct; ++i)
614 if (self->atomic_skip_bp_addrs[i] == 0) {
615 self->atomic_skip_bp_addrs[i] = addr;
616 static struct bp_callbacks cbs = {
617 .on_hit = atomic_singlestep_bp_on_hit,
618 };
619 struct breakpoint *bp
Petr Machata622581f2012-04-23 19:40:40 +0200620 = insert_breakpoint(proc, addr, NULL);
Petr Machata42748ac2012-04-19 04:24:25 +0200621 breakpoint_set_callbacks(bp, &cbs);
622 return 0;
623 }
Petr Machataa266acb2012-04-12 23:50:23 +0200624
Petr Machata42748ac2012-04-19 04:24:25 +0200625 assert(!"Too many atomic singlestep breakpoints!");
626 abort();
Petr Machataa266acb2012-04-12 23:50:23 +0200627}
628
629static int
630singlestep(struct process_stopping_handler *self)
631{
632 struct Process *proc = self->task_enabling_breakpoint;
633
634 int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
635 self->breakpoint_being_enabled,
636 &atomic_singlestep_add_bp, self);
637
638 /* Propagate failure and success. */
639 if (status <= 0)
640 return status;
641
642 /* Otherwise do the default action: singlestep. */
Petr Machata06986d52011-11-02 13:22:46 +0100643 debug(1, "PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200644 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
Petr Machata06986d52011-11-02 13:22:46 +0100645 perror("PTRACE_SINGLESTEP");
Petr Machataa266acb2012-04-12 23:50:23 +0200646 return -1;
647 }
648 return 0;
649}
650
651static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200652post_singlestep(struct process_stopping_handler *self,
653 struct Event **eventp)
Petr Machataa266acb2012-04-12 23:50:23 +0200654{
655 continue_for_sigstop_delivery(&self->pids);
656
Petr Machataf1fd7cd2012-04-19 03:05:58 +0200657 if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
Petr Machataa266acb2012-04-12 23:50:23 +0200658 *eventp = NULL; // handled
659
Petr Machata42748ac2012-04-19 04:24:25 +0200660 struct Process *proc = self->task_enabling_breakpoint;
Petr Machataa266acb2012-04-12 23:50:23 +0200661
Petr Machata42748ac2012-04-19 04:24:25 +0200662 remove_atomic_breakpoints(proc);
Petr Machataa266acb2012-04-12 23:50:23 +0200663 self->breakpoint_being_enabled = NULL;
664}
665
666static void
Petr Machata9e1e9692012-04-19 02:29:38 +0200667singlestep_error(struct process_stopping_handler *self)
Petr Machataa266acb2012-04-12 23:50:23 +0200668{
669 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata9e1e9692012-04-19 02:29:38 +0200670 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200671 fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
Petr Machataa266acb2012-04-12 23:50:23 +0200672 teb->pid, sbp->libsym != NULL ? sbp->libsym->name : NULL,
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200673 sbp->addr, get_instruction_pointer(teb));
Petr Machataa266acb2012-04-12 23:50:23 +0200674 delete_breakpoint(teb->leader, sbp->addr);
Petr Machata06986d52011-11-02 13:22:46 +0100675}
676
Petr Machata9e1e9692012-04-19 02:29:38 +0200677static void
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200678pt_continue(struct process_stopping_handler *self)
Petr Machatac1aa9582012-03-27 23:54:01 +0200679{
680 struct Process *teb = self->task_enabling_breakpoint;
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200681 debug(1, "PTRACE_CONT");
Petr Machata9e1e9692012-04-19 02:29:38 +0200682 ptrace(PTRACE_CONT, teb->pid, 0, 0);
683}
684
685static void
686pt_singlestep(struct process_stopping_handler *self)
687{
688 if (singlestep(self) < 0)
689 singlestep_error(self);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200690}
691
692static void
693disable_and(struct process_stopping_handler *self,
Petr Machata9e1e9692012-04-19 02:29:38 +0200694 void (*do_this)(struct process_stopping_handler *self))
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200695{
696 struct Process *teb = self->task_enabling_breakpoint;
697 debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
Petr Machatac1aa9582012-03-27 23:54:01 +0200698 if (self->breakpoint_being_enabled->enabled)
699 disable_breakpoint(teb, self->breakpoint_being_enabled);
Petr Machata9e1e9692012-04-19 02:29:38 +0200700 (do_this)(self);
Petr Machatac1aa9582012-03-27 23:54:01 +0200701 self->state = psh_singlestep;
702}
703
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200704void
705linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
706{
Petr Machata9e1e9692012-04-19 02:29:38 +0200707 disable_and(self, &pt_singlestep);
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200708}
709
710void
711linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
712{
713 disable_and(self, &pt_continue);
714}
715
Petr Machata98f09922011-07-09 10:55:29 +0200716/* This event handler is installed when we are in the process of
717 * stopping the whole thread group to do the pointer re-enablement for
718 * one of the threads. We pump all events to the queue for later
719 * processing while we wait for all the threads to stop. When this
720 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
721 * re-enable, and continue everyone. */
722static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100723process_stopping_on_event(struct event_handler *super, Event *event)
Petr Machata98f09922011-07-09 10:55:29 +0200724{
725 struct process_stopping_handler * self = (void *)super;
726 Process * task = event->proc;
727 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200728 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200729
730 debug(DEBUG_PROCESS,
Petr Machata9ace5742012-04-14 02:29:57 +0200731 "process_stopping_on_event: pid %d; event type %d; state %d",
Petr Machata98f09922011-07-09 10:55:29 +0200732 task->pid, event->type, self->state);
733
734 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
735 if (task_info == NULL)
736 fprintf(stderr, "new task??? %d\n", task->pid);
737 handle_stopping_event(task_info, &event);
738
739 int state = self->state;
740 int event_to_queue = !event_exit_or_none_p(event);
741
Petr Machata18c97072011-10-06 14:30:11 +0200742 /* Deactivate the entry if the task exits. */
743 if (event_exit_p(event) && task_info != NULL)
744 task_info->pid = 0;
745
Petr Machata43d2fe52011-11-02 13:25:49 +0100746 /* Always handle sysrets. Whether sysret occurred and what
747 * sys it rets from may need to be determined based on process
748 * stack, so we need to keep that in sync with reality. Note
749 * that we don't continue the process after the sysret is
750 * handled. See continue_after_syscall. */
751 if (event != NULL && event->type == EVENT_SYSRET) {
752 debug(1, "%d LT_EV_SYSRET", event->proc->pid);
753 event_to_queue = 0;
754 task_info->sysret = 1;
755 }
756
Petr Machata98f09922011-07-09 10:55:29 +0200757 switch (state) {
758 case psh_stopping:
759 /* If everyone is stopped, singlestep. */
Petr Machata74132a42012-03-16 02:46:18 +0100760 if (each_task(leader, NULL, &task_blocked,
761 &self->pids) == NULL) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200762 (self->on_all_stopped)(self);
763 state = self->state;
Petr Machata98f09922011-07-09 10:55:29 +0200764 }
765 break;
766
Petr Machata06986d52011-11-02 13:22:46 +0100767 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200768 /* In singlestep state, breakpoint signifies that we
769 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200770 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200771
Petr Machata42748ac2012-04-19 04:24:25 +0200772 /* If this was caused by a real breakpoint, as
773 * opposed to a singlestep, assume that it's
774 * an artificial breakpoint installed for some
775 * reason for the re-enablement. In that case
776 * handle it. */
777 if (event->type == EVENT_BREAKPOINT) {
778 target_address_t ip
779 = get_instruction_pointer(task);
780 struct breakpoint *other
781 = address2bpstruct(leader, ip);
782 if (other != NULL)
783 breakpoint_on_hit(other, task);
784 }
785
Petr Machata36f40e72012-03-28 02:07:19 +0200786 /* If we got SIGNAL instead of BREAKPOINT,
787 * then this is not singlestep at all. */
Petr Machatacb9a28d2012-03-28 11:11:32 +0200788 if (event->type == EVENT_SIGNAL) {
789 do_singlestep:
Petr Machataa266acb2012-04-12 23:50:23 +0200790 if (singlestep(self) < 0) {
Petr Machata9e1e9692012-04-19 02:29:38 +0200791 singlestep_error(self);
792 post_singlestep(self, &event);
Petr Machataa266acb2012-04-12 23:50:23 +0200793 goto psh_sinking;
794 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200795 break;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200796 } else {
797 switch ((self->keep_stepping_p)(self)) {
798 case CBS_FAIL:
799 /* XXX handle me */
800 case CBS_STOP:
801 break;
802 case CBS_CONT:
Petr Machata949a56a2012-04-03 00:32:03 +0200803 /* Sink singlestep event. */
804 if (event->type == EVENT_BREAKPOINT)
805 event = NULL;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200806 goto do_singlestep;
807 }
Petr Machatad5d93c42011-10-21 16:41:10 +0200808 }
809
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200810 /* Re-enable the breakpoint that we are
811 * stepping over. */
Petr Machatac1aa9582012-03-27 23:54:01 +0200812 struct breakpoint *sbp = self->breakpoint_being_enabled;
Petr Machata590c8082011-08-20 22:45:26 +0200813 if (sbp->enabled)
814 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200815
Petr Machataa266acb2012-04-12 23:50:23 +0200816 post_singlestep(self, &event);
817 goto psh_sinking;
818 }
819 break;
Petr Machata98f09922011-07-09 10:55:29 +0200820
Petr Machataa266acb2012-04-12 23:50:23 +0200821 psh_sinking:
822 state = self->state = psh_sinking;
Petr Machata98f09922011-07-09 10:55:29 +0200823 case psh_sinking:
824 if (await_sigstop_delivery(&self->pids, task_info, event))
825 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200826 break;
827
828 case psh_ugly_workaround:
829 if (event == NULL)
830 break;
831 if (event->type == EVENT_BREAKPOINT) {
832 undo_breakpoint(event, leader);
833 if (task == teb)
834 self->task_enabling_breakpoint = NULL;
835 }
836 if (self->task_enabling_breakpoint == NULL
837 && all_stops_accountable(&self->pids)) {
838 undo_breakpoint(event, leader);
839 detach_process(leader);
840 event = NULL; // handled
841 }
Petr Machata98f09922011-07-09 10:55:29 +0200842 }
843
844 if (event != NULL && event_to_queue) {
845 enque_event(event);
846 event = NULL; // sink the event
847 }
848
849 return event;
850}
851
852static void
Petr Machata366c2f42012-02-09 19:34:36 +0100853process_stopping_destroy(struct event_handler *super)
Petr Machata98f09922011-07-09 10:55:29 +0200854{
855 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200856 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100857}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100858
Petr Machata36f40e72012-03-28 02:07:19 +0200859static enum callback_status
860no(struct process_stopping_handler *self)
861{
Petr Machatacb9a28d2012-03-28 11:11:32 +0200862 return CBS_STOP;
Petr Machata36f40e72012-03-28 02:07:19 +0200863}
864
Petr Machatac1aa9582012-03-27 23:54:01 +0200865int
866process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp,
Petr Machata36f40e72012-03-28 02:07:19 +0200867 void (*as)(struct process_stopping_handler *),
868 enum callback_status (*ks)
Petr Machatacb9a28d2012-03-28 11:11:32 +0200869 (struct process_stopping_handler *),
870 enum callback_status (*uw)
Petr Machata36f40e72012-03-28 02:07:19 +0200871 (struct process_stopping_handler *))
Petr Machatac1aa9582012-03-27 23:54:01 +0200872{
Petr Machata9ace5742012-04-14 02:29:57 +0200873 debug(DEBUG_FUNCTION,
874 "process_install_stopping_handler: pid=%d", proc->pid);
875
Petr Machatac1aa9582012-03-27 23:54:01 +0200876 struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
877 if (handler == NULL)
878 return -1;
879
Petr Machata36f40e72012-03-28 02:07:19 +0200880 if (as == NULL)
Petr Machata1e2a4dd2012-04-15 04:35:48 +0200881 as = &linux_ptrace_disable_and_singlestep;
Petr Machata36f40e72012-03-28 02:07:19 +0200882 if (ks == NULL)
883 ks = &no;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200884 if (uw == NULL)
885 uw = &no;
Petr Machata36f40e72012-03-28 02:07:19 +0200886
Petr Machatac1aa9582012-03-27 23:54:01 +0200887 handler->super.on_event = process_stopping_on_event;
888 handler->super.destroy = process_stopping_destroy;
889 handler->task_enabling_breakpoint = proc;
890 handler->breakpoint_being_enabled = sbp;
Petr Machata36f40e72012-03-28 02:07:19 +0200891 handler->on_all_stopped = as;
892 handler->keep_stepping_p = ks;
Petr Machatacb9a28d2012-03-28 11:11:32 +0200893 handler->ugly_workaround_p = uw;
Petr Machatac1aa9582012-03-27 23:54:01 +0200894
895 install_event_handler(proc->leader, &handler->super);
896
897 if (each_task(proc->leader, NULL, &send_sigstop,
898 &handler->pids) != NULL) {
899 destroy_event_handler(proc);
900 return -1;
901 }
902
903 /* And deliver the first fake event, in case all the
904 * conditions are already fulfilled. */
905 Event ev = {
906 .type = EVENT_NONE,
907 .proc = proc,
908 };
909 process_stopping_on_event(&handler->super, &ev);
910
911 return 0;
912}
913
Juan Cespedesf1350522008-12-16 18:19:58 +0100914void
Petr Machatabc373262012-02-07 23:31:15 +0100915continue_after_breakpoint(Process *proc, struct breakpoint *sbp)
Petr Machata26627682011-07-08 18:15:32 +0200916{
Petr Machata9ace5742012-04-14 02:29:57 +0200917 debug(DEBUG_PROCESS,
918 "continue_after_breakpoint: pid=%d, addr=%p",
919 proc->pid, sbp->addr);
920
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200921 set_instruction_pointer(proc, sbp->addr);
Petr Machatac1aa9582012-03-27 23:54:01 +0200922
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100923 if (sbp->enabled == 0) {
924 continue_process(proc->pid);
925 } else {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500926#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100927 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200928 continue_process(proc->pid);
929#else
Petr Machatacb9a28d2012-03-28 11:11:32 +0200930 if (process_install_stopping_handler
931 (proc, sbp, NULL, NULL, NULL) < 0) {
Petr Machatac1aa9582012-03-27 23:54:01 +0200932 perror("process_stopping_handler_create");
Petr Machata98f09922011-07-09 10:55:29 +0200933 /* Carry on not bothering to re-enable. */
934 continue_process(proc->pid);
Petr Machata98f09922011-07-09 10:55:29 +0200935 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200936#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100937 }
938}
939
Petr Machata602330f2011-07-09 11:15:34 +0200940/**
941 * Ltrace exit. When we are about to exit, we have to go through all
942 * the processes, stop them all, remove all the breakpoints, and then
943 * detach the processes that we attached to using -p. If we left the
944 * other tasks running, they might hit stray return breakpoints and
945 * produce artifacts, so we better stop everyone, even if it's a bit
946 * of extra work.
947 */
948struct ltrace_exiting_handler
949{
Petr Machata366c2f42012-02-09 19:34:36 +0100950 struct event_handler super;
Petr Machata602330f2011-07-09 11:15:34 +0200951 struct pid_set pids;
952};
953
Petr Machata602330f2011-07-09 11:15:34 +0200954static Event *
Petr Machata366c2f42012-02-09 19:34:36 +0100955ltrace_exiting_on_event(struct event_handler *super, Event *event)
Petr Machata602330f2011-07-09 11:15:34 +0200956{
957 struct ltrace_exiting_handler * self = (void *)super;
958 Process * task = event->proc;
959 Process * leader = task->leader;
960
Petr Machata9ace5742012-04-14 02:29:57 +0200961 debug(DEBUG_PROCESS,
962 "ltrace_exiting_on_event: pid %d; event type %d",
963 task->pid, event->type);
Petr Machata602330f2011-07-09 11:15:34 +0200964
965 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
966 handle_stopping_event(task_info, &event);
967
Petr Machata590c8082011-08-20 22:45:26 +0200968 if (event != NULL && event->type == EVENT_BREAKPOINT)
969 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200970
971 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200972 && all_stops_accountable(&self->pids))
973 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200974
975 /* Sink all non-exit events. We are about to exit, so we
976 * don't bother with queuing them. */
977 if (event_exit_or_none_p(event))
978 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200979
Petr Machata13d5df72011-08-19 23:15:15 +0200980 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200981}
982
983static void
Petr Machata366c2f42012-02-09 19:34:36 +0100984ltrace_exiting_destroy(struct event_handler *super)
Petr Machata602330f2011-07-09 11:15:34 +0200985{
986 struct ltrace_exiting_handler * self = (void *)super;
987 free(self->pids.tasks);
988}
989
990static int
991ltrace_exiting_install_handler(Process * proc)
992{
993 /* Only install to leader. */
994 if (proc->leader != proc)
995 return 0;
996
997 /* Perhaps we are already installed, if the user passed
998 * several -p options that are tasks of one process. */
999 if (proc->event_handler != NULL
1000 && proc->event_handler->on_event == &ltrace_exiting_on_event)
1001 return 0;
1002
Petr Machata590c8082011-08-20 22:45:26 +02001003 /* If stopping handler is already present, let it do the
1004 * work. */
1005 if (proc->event_handler != NULL) {
1006 assert(proc->event_handler->on_event
1007 == &process_stopping_on_event);
1008 struct process_stopping_handler * other
1009 = (void *)proc->event_handler;
1010 other->exiting = 1;
1011 return 0;
1012 }
1013
Petr Machata602330f2011-07-09 11:15:34 +02001014 struct ltrace_exiting_handler * handler
1015 = calloc(sizeof(*handler), 1);
1016 if (handler == NULL) {
1017 perror("malloc exiting handler");
1018 fatal:
1019 /* XXXXXXXXXXXXXXXXXXX fixme */
1020 return -1;
1021 }
1022
Petr Machata602330f2011-07-09 11:15:34 +02001023 handler->super.on_event = ltrace_exiting_on_event;
1024 handler->super.destroy = ltrace_exiting_destroy;
1025 install_event_handler(proc->leader, &handler->super);
1026
Petr Machata74132a42012-03-16 02:46:18 +01001027 if (each_task(proc->leader, NULL, &send_sigstop,
Petr Machata602330f2011-07-09 11:15:34 +02001028 &handler->pids) != NULL)
1029 goto fatal;
1030
1031 return 0;
1032}
1033
Petr Machatacbe29c62011-09-27 02:27:58 +02001034/*
1035 * When the traced process vforks, it's suspended until the child
1036 * process calls _exit or exec*. In the meantime, the two share the
1037 * address space.
1038 *
1039 * The child process should only ever call _exit or exec*, but we
1040 * can't count on that (it's not the role of ltrace to policy, but to
1041 * observe). In any case, we will _at least_ have to deal with
1042 * removal of vfork return breakpoint (which we have to smuggle back
1043 * in, so that the parent can see it, too), and introduction of exec*
1044 * return breakpoint. Since we already have both breakpoint actions
1045 * to deal with, we might as well support it all.
1046 *
1047 * The gist is that we pretend that the child is in a thread group
1048 * with its parent, and handle it as a multi-threaded case, with the
1049 * exception that we know that the parent is blocked, and don't
1050 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +02001051 */
1052
Petr Machata134a1082011-09-27 20:25:58 +02001053struct process_vfork_handler
1054{
Petr Machata366c2f42012-02-09 19:34:36 +01001055 struct event_handler super;
Petr Machata134a1082011-09-27 20:25:58 +02001056 void * bp_addr;
1057};
1058
Petr Machatacbe29c62011-09-27 02:27:58 +02001059static Event *
Petr Machata366c2f42012-02-09 19:34:36 +01001060process_vfork_on_event(struct event_handler *super, Event *event)
Petr Machatacbe29c62011-09-27 02:27:58 +02001061{
Petr Machata9ace5742012-04-14 02:29:57 +02001062 debug(DEBUG_PROCESS,
1063 "process_vfork_on_event: pid %d; event type %d",
1064 event->proc->pid, event->type);
1065
Petr Machatacbe29c62011-09-27 02:27:58 +02001066 struct process_vfork_handler * self = (void *)super;
Petr Machatabc373262012-02-07 23:31:15 +01001067 struct breakpoint *sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +02001068 assert(self != NULL);
1069
1070 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +02001071 case EVENT_BREAKPOINT:
1072 /* Remember the vfork return breakpoint. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001073 if (self->bp_addr == 0)
Petr Machata134a1082011-09-27 20:25:58 +02001074 self->bp_addr = event->e_un.brk_addr;
1075 break;
1076
Petr Machatacbe29c62011-09-27 02:27:58 +02001077 case EVENT_EXIT:
1078 case EVENT_EXIT_SIGNAL:
1079 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +02001080 /* Smuggle back in the vfork return breakpoint, so
1081 * that our parent can trip over it once again. */
Petr Machata77e8fa82012-04-19 18:36:32 +02001082 if (self->bp_addr != 0) {
Petr Machata134a1082011-09-27 20:25:58 +02001083 sbp = dict_find_entry(event->proc->leader->breakpoints,
1084 self->bp_addr);
1085 if (sbp != NULL)
Petr Machata77e8fa82012-04-19 18:36:32 +02001086 assert(sbp->libsym == NULL);
1087 /* We don't mind failing that, it's not a big
1088 * deal to not display one extra vfork return. */
1089 insert_breakpoint(event->proc->parent,
1090 self->bp_addr, NULL);
Petr Machata134a1082011-09-27 20:25:58 +02001091 }
1092
Petr Machataba9911f2011-09-27 21:09:47 +02001093 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001094
1095 /* Remove the leader that we artificially set up
1096 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +02001097 change_process_leader(event->proc, event->proc);
1098 destroy_event_handler(event->proc);
1099
Petr Machatacbe29c62011-09-27 02:27:58 +02001100 default:
1101 ;
1102 }
1103
1104 return event;
1105}
1106
1107void
1108continue_after_vfork(Process * proc)
1109{
1110 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +02001111 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +02001112 if (handler == NULL) {
1113 perror("malloc vfork handler");
1114 /* Carry on not bothering to treat the process as
1115 * necessary. */
1116 continue_process(proc->parent->pid);
1117 return;
1118 }
1119
1120 /* We must set up custom event handler, so that we see
1121 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +02001122 handler->super.on_event = process_vfork_on_event;
1123 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +02001124
1125 /* Make sure that the child is sole thread. */
1126 assert(proc->leader == proc);
1127 assert(proc->next == NULL || proc->next->leader != proc);
1128
1129 /* Make sure that the child's parent is properly set up. */
1130 assert(proc->parent != NULL);
1131 assert(proc->parent->leader != NULL);
1132
1133 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +02001134}
1135
Petr Machata9d29b3e2011-11-09 16:46:56 +01001136static int
1137is_mid_stopping(Process *proc)
1138{
1139 return proc != NULL
1140 && proc->event_handler != NULL
1141 && proc->event_handler->on_event == &process_stopping_on_event;
1142}
1143
Petr Machata43d2fe52011-11-02 13:25:49 +01001144void
1145continue_after_syscall(Process * proc, int sysnum, int ret_p)
1146{
1147 /* Don't continue if we are mid-stopping. */
Petr Machata9d29b3e2011-11-09 16:46:56 +01001148 if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1149 debug(DEBUG_PROCESS,
1150 "continue_after_syscall: don't continue %d",
1151 proc->pid);
Petr Machata43d2fe52011-11-02 13:25:49 +01001152 return;
Petr Machata9d29b3e2011-11-09 16:46:56 +01001153 }
Petr Machata43d2fe52011-11-02 13:25:49 +01001154 continue_process(proc->pid);
1155}
1156
Petr Machata602330f2011-07-09 11:15:34 +02001157/* If ltrace gets SIGINT, the processes directly or indirectly run by
1158 * ltrace get it too. We just have to wait long enough for the signal
1159 * to be delivered and the process terminated, which we notice and
1160 * exit ltrace, too. So there's not much we need to do there. We
1161 * want to keep tracing those processes as usual, in case they just
1162 * SIG_IGN the SIGINT to do their shutdown etc.
1163 *
1164 * For processes ran on the background, we want to install an exit
1165 * handler that stops all the threads, removes all breakpoints, and
1166 * detaches.
1167 */
1168void
Petr Machataffe4cd22012-04-11 18:01:44 +02001169os_ltrace_exiting(void)
Petr Machata602330f2011-07-09 11:15:34 +02001170{
1171 struct opt_p_t * it;
1172 for (it = opt_p; it != NULL; it = it->next) {
1173 Process * proc = pid2proc(it->pid);
1174 if (proc == NULL || proc->leader == NULL)
1175 continue;
1176 if (ltrace_exiting_install_handler(proc->leader) < 0)
1177 fprintf(stderr,
1178 "Couldn't install exiting handler for %d.\n",
1179 proc->pid);
1180 }
1181}
1182
Petr Machataffe4cd22012-04-11 18:01:44 +02001183int
1184os_ltrace_exiting_sighandler(void)
1185{
1186 extern int linux_in_waitpid;
1187 if (linux_in_waitpid) {
1188 os_ltrace_exiting();
1189 return 1;
1190 }
1191 return 0;
1192}
1193
Joe Damatodfa3fa32010-11-08 15:47:35 -08001194size_t
1195umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
1196
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}
1227
Steve Fink7bafff02006-08-07 04:50:42 +02001228/* Read a series of bytes starting at the process's memory address
1229 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1230 have been read.
1231*/
Juan Cespedesf1350522008-12-16 18:19:58 +01001232int
Juan Cespedesa8909f72009-04-28 20:02:41 +02001233umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001234 union {
1235 long a;
1236 char c[sizeof(long)];
1237 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -08001238 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001239 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001240
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001241 while (offset < len) {
1242 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1243 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +02001244 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001245 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001246 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001247 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001248 return 0;
1249 }
1250 }
1251 offset += sizeof(long);
1252 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +01001253 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +01001254 return 0;
1255}