blob: ecff5d1b63875de6a4afe60a36fb300dcbc7a070 [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01002#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01003#include <string.h>
4#include <errno.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +01005#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01006#include <sys/types.h>
Petr Machata89a53602007-01-25 18:05:44 +01007#include <sys/wait.h>
Juan Cespedes5c3fe062004-06-14 18:08:37 +02008#include "ptrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <asm/unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020010#include <assert.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010011
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020013
14/* If the system headers did not provide the constants, hard-code the normal
15 values. */
16#ifndef PTRACE_EVENT_FORK
17
18#define PTRACE_OLDSETOPTIONS 21
19#define PTRACE_SETOPTIONS 0x4200
20#define PTRACE_GETEVENTMSG 0x4201
21
22/* options set using PTRACE_SETOPTIONS */
23#define PTRACE_O_TRACESYSGOOD 0x00000001
24#define PTRACE_O_TRACEFORK 0x00000002
25#define PTRACE_O_TRACEVFORK 0x00000004
26#define PTRACE_O_TRACECLONE 0x00000008
27#define PTRACE_O_TRACEEXEC 0x00000010
28#define PTRACE_O_TRACEVFORKDONE 0x00000020
29#define PTRACE_O_TRACEEXIT 0x00000040
30
31/* Wait extended result codes for the above trace options. */
32#define PTRACE_EVENT_FORK 1
33#define PTRACE_EVENT_VFORK 2
34#define PTRACE_EVENT_CLONE 3
35#define PTRACE_EVENT_EXEC 4
36#define PTRACE_EVENT_VFORK_DONE 5
37#define PTRACE_EVENT_EXIT 6
38
39#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010040
Luis Machado55c5feb2008-03-12 15:56:01 +010041#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020042extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010043int
Juan Cespedesa8909f72009-04-28 20:02:41 +020044umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010045 return arch_umovelong (proc, addr, result, info);
46}
47#else
48/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010049int
Juan Cespedesa8909f72009-04-28 20:02:41 +020050umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010051 long pointed_to;
52
53 errno = 0;
54 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
55 if (pointed_to == -1 && errno)
56 return -errno;
57
58 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050059 if (info) {
60 switch(info->type) {
61 case ARGTYPE_INT:
62 *result &= 0x00000000ffffffffUL;
63 default:
64 break;
65 };
66 }
Luis Machado55c5feb2008-03-12 15:56:01 +010067 return 0;
68}
69#endif
70
Juan Cespedesf1350522008-12-16 18:19:58 +010071void
72trace_me(void) {
Petr Machata26627682011-07-08 18:15:32 +020073 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 perror("PTRACE_TRACEME");
76 exit(1);
77 }
78}
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080int
81trace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +020082 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +010083 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010084 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010085 }
Petr Machata89a53602007-01-25 18:05:44 +010086
Juan Cespedes714ee9d2009-04-07 13:28:54 +020087 /* man ptrace: PTRACE_ATTACH attaches to the process specified
88 in pid. The child is sent a SIGSTOP, but will not
89 necessarily have stopped by the completion of this call;
90 use wait() to wait for the child to stop. */
Petr Machata9a5420c2011-07-09 11:21:23 +020091 if (waitpid (pid, NULL, __WALL) != pid) {
Juan Cespedes714ee9d2009-04-07 13:28:54 +020092 perror ("trace_pid: waitpid");
Petr Machata9a5420c2011-07-09 11:21:23 +020093 return -1;
Juan Cespedes714ee9d2009-04-07 13:28:54 +020094 }
95
Juan Cespedes273ea6d1998-03-14 23:02:40 +010096 return 0;
97}
98
Juan Cespedesf1350522008-12-16 18:19:58 +010099void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200100trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100101 if (proc->tracesysgood & 0x80)
102 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200103
Petr Machata26627682011-07-08 18:15:32 +0200104 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200105
Juan Cespedes1e583132009-04-07 18:17:11 +0200106 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
107 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
108 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200109 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
110 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100111 perror("PTRACE_SETOPTIONS");
112 return;
113 }
114 proc->tracesysgood |= 0x80;
115}
116
Juan Cespedesf1350522008-12-16 18:19:58 +0100117void
118untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200119 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100120 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100121}
122
Juan Cespedesf1350522008-12-16 18:19:58 +0100123void
124continue_after_signal(pid_t pid, int signum) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200125 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
Petr Machata98f09922011-07-09 10:55:29 +0200126 ptrace(PTRACE_SYSCALL, pid, 0, signum);
127}
128
129static enum ecb_status
130event_for_pid(Event * event, void * data)
131{
132 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
133 return ecb_yield;
134 return ecb_cont;
135}
136
137static int
138have_events_for(pid_t pid)
139{
140 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
141}
142
143void
144continue_process(pid_t pid)
145{
146 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200147
148 /* Only really continue the process if there are no events in
Petr Machata36d19822011-10-21 16:03:45 +0200149 the queue for this process. Otherwise just wait for the
150 other events to arrive. */
Petr Machata98f09922011-07-09 10:55:29 +0200151 if (!have_events_for(pid))
152 /* We always trace syscalls to control fork(),
153 * clone(), execve()... */
154 ptrace(PTRACE_SYSCALL, pid, 0, 0);
155 else
156 debug(DEBUG_PROCESS,
157 "putting off the continue, events in que.");
158}
159
160/**
161 * This is used for bookkeeping related to PIDs that the event
Petr Machata750ca8c2011-10-06 14:29:34 +0200162 * handlers work with.
163 */
Petr Machata98f09922011-07-09 10:55:29 +0200164struct pid_task {
Petr Machata750ca8c2011-10-06 14:29:34 +0200165 pid_t pid; /* This may be 0 for tasks that exited
166 * mid-handling. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200167 int sigstopped : 1;
168 int got_event : 1;
169 int delivered : 1;
170 int vforked : 1;
Petr Machata98f09922011-07-09 10:55:29 +0200171} * pids;
172
173struct pid_set {
174 struct pid_task * tasks;
175 size_t count;
176 size_t alloc;
177};
178
179/**
180 * Breakpoint re-enablement. When we hit a breakpoint, we must
181 * disable it, single-step, and re-enable it. That single-step can be
182 * done only by one task in a task group, while others are stopped,
183 * otherwise the processes would race for who sees the breakpoint
184 * disabled and who doesn't. The following is to keep track of it
185 * all.
186 */
187struct process_stopping_handler
188{
189 Event_Handler super;
190
191 /* The task that is doing the re-enablement. */
192 Process * task_enabling_breakpoint;
193
194 /* The pointer being re-enabled. */
195 Breakpoint * breakpoint_being_enabled;
196
197 enum {
198 /* We are waiting for everyone to land in t/T. */
199 psh_stopping = 0,
200
201 /* We are doing the PTRACE_SINGLESTEP. */
202 psh_singlestep,
203
204 /* We are waiting for all the SIGSTOPs to arrive so
205 * that we can sink them. */
206 psh_sinking,
Petr Machata46d66ab2011-08-20 05:29:25 +0200207
208 /* This is for tracking the ugly workaround. */
209 psh_ugly_workaround,
Petr Machata98f09922011-07-09 10:55:29 +0200210 } state;
211
Petr Machata590c8082011-08-20 22:45:26 +0200212 int exiting;
213
Petr Machata98f09922011-07-09 10:55:29 +0200214 struct pid_set pids;
215};
216
Petr Machata98f09922011-07-09 10:55:29 +0200217static struct pid_task *
218get_task_info(struct pid_set * pids, pid_t pid)
219{
Petr Machata750ca8c2011-10-06 14:29:34 +0200220 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200221 size_t i;
222 for (i = 0; i < pids->count; ++i)
223 if (pids->tasks[i].pid == pid)
224 return &pids->tasks[i];
225
226 return NULL;
227}
228
229static struct pid_task *
230add_task_info(struct pid_set * pids, pid_t pid)
231{
232 if (pids->count == pids->alloc) {
233 size_t ns = (2 * pids->alloc) ?: 4;
234 struct pid_task * n = realloc(pids->tasks,
235 sizeof(*pids->tasks) * ns);
236 if (n == NULL)
237 return NULL;
238 pids->tasks = n;
239 pids->alloc = ns;
240 }
241 struct pid_task * task_info = &pids->tasks[pids->count++];
242 memset(task_info, 0, sizeof(*task_info));
243 task_info->pid = pid;
244 return task_info;
245}
246
247static enum pcb_status
Petr Machatacbe29c62011-09-27 02:27:58 +0200248task_stopped(Process * task, void * data)
249{
250 enum process_status st = process_status(task->pid);
251 if (data != NULL)
252 *(enum process_status *)data = st;
253
254 /* If the task is already stopped, don't worry about it.
255 * Likewise if it managed to become a zombie or terminate in
256 * the meantime. This can happen when the whole thread group
257 * is terminating. */
258 switch (st) {
259 case ps_invalid:
260 case ps_tracing_stop:
261 case ps_zombie:
Petr Machata36d19822011-10-21 16:03:45 +0200262 case ps_sleeping:
Petr Machatacbe29c62011-09-27 02:27:58 +0200263 return pcb_cont;
Petr Machata36d19822011-10-21 16:03:45 +0200264 case ps_stop:
265 case ps_other:
Petr Machatacbe29c62011-09-27 02:27:58 +0200266 return pcb_stop;
267 }
Petr Machata36d19822011-10-21 16:03:45 +0200268
269 abort ();
Petr Machatacbe29c62011-09-27 02:27:58 +0200270}
271
272/* Task is blocked if it's stopped, or if it's a vfork parent. */
273static enum pcb_status
274task_blocked(Process * task, void * data)
275{
276 struct pid_set * pids = data;
277 struct pid_task * task_info = get_task_info(pids, task->pid);
278 if (task_info != NULL
279 && task_info->vforked)
280 return pcb_cont;
281
282 return task_stopped(task, NULL);
283}
284
285static Event * process_vfork_on_event(Event_Handler * super, Event * event);
286
287static enum pcb_status
288task_vforked(Process * task, void * data)
289{
290 if (task->event_handler != NULL
291 && task->event_handler->on_event == &process_vfork_on_event)
292 return pcb_stop;
293 return pcb_cont;
294}
295
296static int
297is_vfork_parent(Process * task)
298{
299 return each_task(task->leader, &task_vforked, NULL) != NULL;
300}
301
302static enum pcb_status
Petr Machata98f09922011-07-09 10:55:29 +0200303send_sigstop(Process * task, void * data)
304{
305 Process * leader = task->leader;
306 struct pid_set * pids = data;
307
308 /* Look for pre-existing task record, or add new. */
309 struct pid_task * task_info = get_task_info(pids, task->pid);
310 if (task_info == NULL)
311 task_info = add_task_info(pids, task->pid);
312 if (task_info == NULL) {
313 perror("send_sigstop: add_task_info");
314 destroy_event_handler(leader);
315 /* Signal failure upwards. */
316 return pcb_stop;
317 }
318
319 /* This task still has not been attached to. It should be
320 stopped by the kernel. */
321 if (task->state == STATE_BEING_CREATED)
322 return pcb_cont;
323
324 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200325 * if we sent the SIGSTOP already, which happens when we are
326 * handling "onexit" and inherited the handler from breakpoint
327 * re-enablement. */
328 enum process_status st;
329 if (task_stopped(task, &st) == pcb_cont)
Petr Machata98f09922011-07-09 10:55:29 +0200330 return pcb_cont;
331 if (task_info->sigstopped) {
332 if (!task_info->delivered)
333 return pcb_cont;
334 task_info->delivered = 0;
335 }
336
Petr Machatacbe29c62011-09-27 02:27:58 +0200337 /* Also don't attempt to stop the process if it's a parent of
338 * vforked process. We set up event handler specially to hint
339 * us. In that case parent is in D state, which we use to
340 * weed out unnecessary looping. */
341 if (st == ps_sleeping
342 && is_vfork_parent (task)) {
343 task_info->vforked = 1;
344 return pcb_cont;
345 }
346
Petr Machata98f09922011-07-09 10:55:29 +0200347 if (task_kill(task->pid, SIGSTOP) >= 0) {
348 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
349 task_info->sigstopped = 1;
350 } else
351 fprintf(stderr,
352 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
353
354 return pcb_cont;
355}
356
Petr Machata73894bd2011-08-20 23:47:34 +0200357/* On certain kernels, detaching right after a singlestep causes the
358 tracee to be killed with a SIGTRAP (that even though the singlestep
359 was properly caught by waitpid. The ugly workaround is to put a
360 breakpoint where IP points and let the process continue. After
361 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200362static void
Petr Machata73894bd2011-08-20 23:47:34 +0200363ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200364{
365 void * ip = get_instruction_pointer(proc);
366 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
367 if (sbp != NULL)
368 enable_breakpoint(proc, sbp);
369 else
370 insert_breakpoint(proc, ip, NULL, 1);
Petr Machata73894bd2011-08-20 23:47:34 +0200371 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200372}
373
374static void
Petr Machata98f09922011-07-09 10:55:29 +0200375process_stopping_done(struct process_stopping_handler * self, Process * leader)
376{
377 debug(DEBUG_PROCESS, "process stopping done %d",
378 self->task_enabling_breakpoint->pid);
379 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200380 if (!self->exiting) {
381 for (i = 0; i < self->pids.count; ++i)
382 if (self->pids.tasks[i].pid != 0
383 && self->pids.tasks[i].delivered)
384 continue_process(self->pids.tasks[i].pid);
385 continue_process(self->task_enabling_breakpoint->pid);
386 destroy_event_handler(leader);
387 } else {
388 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200389 ugly_workaround(self->task_enabling_breakpoint);
Petr Machata590c8082011-08-20 22:45:26 +0200390 }
391}
392
393/* Before we detach, we need to make sure that task's IP is on the
394 * edge of an instruction. So for tasks that have a breakpoint event
395 * in the queue, we adjust the instruction pointer, just like
396 * continue_after_breakpoint does. */
397static enum ecb_status
398undo_breakpoint(Event * event, void * data)
399{
400 if (event != NULL
401 && event->proc->leader == data
402 && event->type == EVENT_BREAKPOINT)
403 set_instruction_pointer(event->proc, event->e_un.brk_addr);
404 return ecb_cont;
405}
406
407static enum pcb_status
408untrace_task(Process * task, void * data)
409{
410 if (task != data)
411 untrace_pid(task->pid);
412 return pcb_cont;
413}
414
415static enum pcb_status
416remove_task(Process * task, void * data)
417{
418 /* Don't untrace leader just yet. */
419 if (task != data)
420 remove_process(task);
421 return pcb_cont;
422}
423
424static void
425detach_process(Process * leader)
426{
427 each_qd_event(&undo_breakpoint, leader);
428 disable_all_breakpoints(leader);
429
430 /* Now untrace the process, if it was attached to by -p. */
431 struct opt_p_t * it;
432 for (it = opt_p; it != NULL; it = it->next) {
433 Process * proc = pid2proc(it->pid);
434 if (proc == NULL)
435 continue;
436 if (proc->leader == leader) {
437 each_task(leader, &untrace_task, NULL);
438 break;
439 }
440 }
441 each_task(leader, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200442 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200443 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200444}
445
446static void
447handle_stopping_event(struct pid_task * task_info, Event ** eventp)
448{
449 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200450 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200451 task_info->got_event = 1;
452
453 Event * event = *eventp;
454
455 /* In every state, sink SIGSTOP events for tasks that it was
456 * sent to. */
457 if (task_info != NULL
458 && event->type == EVENT_SIGNAL
459 && event->e_un.signum == SIGSTOP) {
460 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
461 if (task_info->sigstopped
462 && !task_info->delivered) {
463 task_info->delivered = 1;
464 *eventp = NULL; // sink the event
465 } else
466 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
467 "sigstopped=%d and delivered=%d\n",
468 task_info->pid, task_info->sigstopped,
469 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100470 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100471}
472
Petr Machata98f09922011-07-09 10:55:29 +0200473/* Some SIGSTOPs may have not been delivered to their respective tasks
474 * yet. They are still in the queue. If we have seen an event for
475 * that process, continue it, so that the SIGSTOP can be delivered and
Petr Machata36d19822011-10-21 16:03:45 +0200476 * caught by ltrace. We don't mind that the process is after
477 * breakpoint (and therefore potentially doesn't have aligned IP),
478 * because the signal will be delivered without the process actually
479 * starting. */
Petr Machata98f09922011-07-09 10:55:29 +0200480static void
481continue_for_sigstop_delivery(struct pid_set * pids)
482{
483 size_t i;
484 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200485 if (pids->tasks[i].pid != 0
486 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200487 && !pids->tasks[i].delivered
488 && pids->tasks[i].got_event) {
489 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
490 pids->tasks[i].pid);
491 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
492 }
493 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100494}
495
Petr Machata98f09922011-07-09 10:55:29 +0200496static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200497event_exit_p(Event * event)
498{
499 return event != NULL && (event->type == EVENT_EXIT
500 || event->type == EVENT_EXIT_SIGNAL);
501}
502
503static int
Petr Machata98f09922011-07-09 10:55:29 +0200504event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200505{
Petr Machata750ca8c2011-10-06 14:29:34 +0200506 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200507 || event->type == EVENT_NONE;
508}
509
510static int
511await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
512 Event * event)
513{
514 /* If we still didn't get our SIGSTOP, continue the process
515 * and carry on. */
516 if (event != NULL && !event_exit_or_none_p(event)
517 && task_info != NULL && task_info->sigstopped) {
518 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
519 task_info->pid);
520 /* We should get the signal the first thing
521 * after this, so it should be OK to continue
522 * even if we are over a breakpoint. */
523 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
524
525 } else {
526 /* If all SIGSTOPs were delivered, uninstall the
527 * handler and continue everyone. */
528 /* XXX I suspect that we should check tasks that are
529 * still around. Is things are now, there should be a
530 * race between waiting for everyone to stop and one
531 * of the tasks exiting. */
532 int all_clear = 1;
533 size_t i;
534 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200535 if (pids->tasks[i].pid != 0
536 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200537 && !pids->tasks[i].delivered) {
538 all_clear = 0;
539 break;
540 }
541 return all_clear;
542 }
543
544 return 0;
545}
546
Petr Machata590c8082011-08-20 22:45:26 +0200547static int
548all_stops_accountable(struct pid_set * pids)
549{
550 size_t i;
551 for (i = 0; i < pids->count; ++i)
552 if (pids->tasks[i].pid != 0
553 && !pids->tasks[i].got_event
554 && !have_events_for(pids->tasks[i].pid))
555 return 0;
556 return 1;
557}
558
Petr Machata98f09922011-07-09 10:55:29 +0200559/* This event handler is installed when we are in the process of
560 * stopping the whole thread group to do the pointer re-enablement for
561 * one of the threads. We pump all events to the queue for later
562 * processing while we wait for all the threads to stop. When this
563 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
564 * re-enable, and continue everyone. */
565static Event *
566process_stopping_on_event(Event_Handler * super, Event * event)
567{
568 struct process_stopping_handler * self = (void *)super;
569 Process * task = event->proc;
570 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200571 Breakpoint * sbp = self->breakpoint_being_enabled;
572 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200573
574 debug(DEBUG_PROCESS,
575 "pid %d; event type %d; state %d",
576 task->pid, event->type, self->state);
577
578 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
579 if (task_info == NULL)
580 fprintf(stderr, "new task??? %d\n", task->pid);
581 handle_stopping_event(task_info, &event);
582
583 int state = self->state;
584 int event_to_queue = !event_exit_or_none_p(event);
585
Petr Machata18c97072011-10-06 14:30:11 +0200586 /* Deactivate the entry if the task exits. */
587 if (event_exit_p(event) && task_info != NULL)
588 task_info->pid = 0;
589
Petr Machata98f09922011-07-09 10:55:29 +0200590 switch (state) {
591 case psh_stopping:
592 /* If everyone is stopped, singlestep. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200593 if (each_task(leader, &task_blocked, &self->pids) == NULL) {
Petr Machata98f09922011-07-09 10:55:29 +0200594 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200595 teb->pid);
596 if (sbp->enabled)
597 disable_breakpoint(teb, sbp);
598 if (ptrace(PTRACE_SINGLESTEP, teb->pid, 0, 0))
Petr Machata750ca8c2011-10-06 14:29:34 +0200599 perror("PTRACE_SINGLESTEP");
Petr Machata98f09922011-07-09 10:55:29 +0200600 self->state = state = psh_singlestep;
601 }
602 break;
603
604 case psh_singlestep: {
605 /* In singlestep state, breakpoint signifies that we
606 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200607 if (event != NULL && task == teb) {
Petr Machata98f09922011-07-09 10:55:29 +0200608 /* Essentially we don't care what event caused
609 * the thread to stop. We can do the
610 * re-enablement now. */
Petr Machata590c8082011-08-20 22:45:26 +0200611 if (sbp->enabled)
612 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200613
614 continue_for_sigstop_delivery(&self->pids);
615
616 self->breakpoint_being_enabled = NULL;
617 self->state = state = psh_sinking;
618
619 if (event->type == EVENT_BREAKPOINT)
620 event = NULL; // handled
621 } else
622 break;
623 }
624
625 /* fall-through */
626
627 case psh_sinking:
628 if (await_sigstop_delivery(&self->pids, task_info, event))
629 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200630 break;
631
632 case psh_ugly_workaround:
633 if (event == NULL)
634 break;
635 if (event->type == EVENT_BREAKPOINT) {
636 undo_breakpoint(event, leader);
637 if (task == teb)
638 self->task_enabling_breakpoint = NULL;
639 }
640 if (self->task_enabling_breakpoint == NULL
641 && all_stops_accountable(&self->pids)) {
642 undo_breakpoint(event, leader);
643 detach_process(leader);
644 event = NULL; // handled
645 }
Petr Machata98f09922011-07-09 10:55:29 +0200646 }
647
648 if (event != NULL && event_to_queue) {
649 enque_event(event);
650 event = NULL; // sink the event
651 }
652
653 return event;
654}
655
656static void
657process_stopping_destroy(Event_Handler * super)
658{
659 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200660 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100661}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100662
Juan Cespedesf1350522008-12-16 18:19:58 +0100663void
Petr Machata26627682011-07-08 18:15:32 +0200664continue_after_breakpoint(Process *proc, Breakpoint *sbp)
665{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200666 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100667 if (sbp->enabled == 0) {
668 continue_process(proc->pid);
669 } else {
Petr Machata26627682011-07-08 18:15:32 +0200670 debug(DEBUG_PROCESS,
671 "continue_after_breakpoint: pid=%d, addr=%p",
672 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500673#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100674 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200675 continue_process(proc->pid);
676#else
Petr Machata98f09922011-07-09 10:55:29 +0200677 struct process_stopping_handler * handler
678 = calloc(sizeof(*handler), 1);
679 if (handler == NULL) {
680 perror("malloc breakpoint disable handler");
681 fatal:
682 /* Carry on not bothering to re-enable. */
683 continue_process(proc->pid);
684 return;
685 }
686
687 handler->super.on_event = process_stopping_on_event;
688 handler->super.destroy = process_stopping_destroy;
689 handler->task_enabling_breakpoint = proc;
690 handler->breakpoint_being_enabled = sbp;
691 install_event_handler(proc->leader, &handler->super);
692
693 if (each_task(proc->leader, &send_sigstop,
694 &handler->pids) != NULL)
695 goto fatal;
696
697 /* And deliver the first fake event, in case all the
698 * conditions are already fulfilled. */
699 Event ev;
700 ev.type = EVENT_NONE;
701 ev.proc = proc;
702 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200703#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100704 }
705}
706
Petr Machata602330f2011-07-09 11:15:34 +0200707/**
708 * Ltrace exit. When we are about to exit, we have to go through all
709 * the processes, stop them all, remove all the breakpoints, and then
710 * detach the processes that we attached to using -p. If we left the
711 * other tasks running, they might hit stray return breakpoints and
712 * produce artifacts, so we better stop everyone, even if it's a bit
713 * of extra work.
714 */
715struct ltrace_exiting_handler
716{
717 Event_Handler super;
718 struct pid_set pids;
719};
720
Petr Machata602330f2011-07-09 11:15:34 +0200721static Event *
722ltrace_exiting_on_event(Event_Handler * super, Event * event)
723{
724 struct ltrace_exiting_handler * self = (void *)super;
725 Process * task = event->proc;
726 Process * leader = task->leader;
727
728 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
729
730 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
731 handle_stopping_event(task_info, &event);
732
Petr Machata590c8082011-08-20 22:45:26 +0200733 if (event != NULL && event->type == EVENT_BREAKPOINT)
734 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200735
736 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200737 && all_stops_accountable(&self->pids))
738 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200739
740 /* Sink all non-exit events. We are about to exit, so we
741 * don't bother with queuing them. */
742 if (event_exit_or_none_p(event))
743 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200744
Petr Machata13d5df72011-08-19 23:15:15 +0200745 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200746}
747
748static void
749ltrace_exiting_destroy(Event_Handler * super)
750{
751 struct ltrace_exiting_handler * self = (void *)super;
752 free(self->pids.tasks);
753}
754
755static int
756ltrace_exiting_install_handler(Process * proc)
757{
758 /* Only install to leader. */
759 if (proc->leader != proc)
760 return 0;
761
762 /* Perhaps we are already installed, if the user passed
763 * several -p options that are tasks of one process. */
764 if (proc->event_handler != NULL
765 && proc->event_handler->on_event == &ltrace_exiting_on_event)
766 return 0;
767
Petr Machata590c8082011-08-20 22:45:26 +0200768 /* If stopping handler is already present, let it do the
769 * work. */
770 if (proc->event_handler != NULL) {
771 assert(proc->event_handler->on_event
772 == &process_stopping_on_event);
773 struct process_stopping_handler * other
774 = (void *)proc->event_handler;
775 other->exiting = 1;
776 return 0;
777 }
778
Petr Machata602330f2011-07-09 11:15:34 +0200779 struct ltrace_exiting_handler * handler
780 = calloc(sizeof(*handler), 1);
781 if (handler == NULL) {
782 perror("malloc exiting handler");
783 fatal:
784 /* XXXXXXXXXXXXXXXXXXX fixme */
785 return -1;
786 }
787
Petr Machata602330f2011-07-09 11:15:34 +0200788 handler->super.on_event = ltrace_exiting_on_event;
789 handler->super.destroy = ltrace_exiting_destroy;
790 install_event_handler(proc->leader, &handler->super);
791
792 if (each_task(proc->leader, &send_sigstop,
793 &handler->pids) != NULL)
794 goto fatal;
795
796 return 0;
797}
798
Petr Machatacbe29c62011-09-27 02:27:58 +0200799/*
800 * When the traced process vforks, it's suspended until the child
801 * process calls _exit or exec*. In the meantime, the two share the
802 * address space.
803 *
804 * The child process should only ever call _exit or exec*, but we
805 * can't count on that (it's not the role of ltrace to policy, but to
806 * observe). In any case, we will _at least_ have to deal with
807 * removal of vfork return breakpoint (which we have to smuggle back
808 * in, so that the parent can see it, too), and introduction of exec*
809 * return breakpoint. Since we already have both breakpoint actions
810 * to deal with, we might as well support it all.
811 *
812 * The gist is that we pretend that the child is in a thread group
813 * with its parent, and handle it as a multi-threaded case, with the
814 * exception that we know that the parent is blocked, and don't
815 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200816 */
817
Petr Machata134a1082011-09-27 20:25:58 +0200818struct process_vfork_handler
819{
820 Event_Handler super;
821 void * bp_addr;
822};
823
Petr Machatacbe29c62011-09-27 02:27:58 +0200824static Event *
825process_vfork_on_event(Event_Handler * super, Event * event)
826{
827 struct process_vfork_handler * self = (void *)super;
Petr Machata134a1082011-09-27 20:25:58 +0200828 Breakpoint * sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200829 assert(self != NULL);
830
831 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200832 case EVENT_BREAKPOINT:
833 /* Remember the vfork return breakpoint. */
834 if (self->bp_addr == NULL)
835 self->bp_addr = event->e_un.brk_addr;
836 break;
837
Petr Machatacbe29c62011-09-27 02:27:58 +0200838 case EVENT_EXIT:
839 case EVENT_EXIT_SIGNAL:
840 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200841 /* Smuggle back in the vfork return breakpoint, so
842 * that our parent can trip over it once again. */
843 if (self->bp_addr != NULL) {
844 sbp = dict_find_entry(event->proc->leader->breakpoints,
845 self->bp_addr);
846 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +0200847 insert_breakpoint(event->proc->parent,
848 self->bp_addr,
849 sbp->libsym, 1);
Petr Machata134a1082011-09-27 20:25:58 +0200850 }
851
Petr Machataba9911f2011-09-27 21:09:47 +0200852 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200853
854 /* Remove the leader that we artificially set up
855 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200856 change_process_leader(event->proc, event->proc);
857 destroy_event_handler(event->proc);
858
Petr Machatacbe29c62011-09-27 02:27:58 +0200859 default:
860 ;
861 }
862
863 return event;
864}
865
866void
867continue_after_vfork(Process * proc)
868{
869 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200870 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +0200871 if (handler == NULL) {
872 perror("malloc vfork handler");
873 /* Carry on not bothering to treat the process as
874 * necessary. */
875 continue_process(proc->parent->pid);
876 return;
877 }
878
879 /* We must set up custom event handler, so that we see
880 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +0200881 handler->super.on_event = process_vfork_on_event;
882 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +0200883
884 /* Make sure that the child is sole thread. */
885 assert(proc->leader == proc);
886 assert(proc->next == NULL || proc->next->leader != proc);
887
888 /* Make sure that the child's parent is properly set up. */
889 assert(proc->parent != NULL);
890 assert(proc->parent->leader != NULL);
891
892 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +0200893}
894
Petr Machata602330f2011-07-09 11:15:34 +0200895/* If ltrace gets SIGINT, the processes directly or indirectly run by
896 * ltrace get it too. We just have to wait long enough for the signal
897 * to be delivered and the process terminated, which we notice and
898 * exit ltrace, too. So there's not much we need to do there. We
899 * want to keep tracing those processes as usual, in case they just
900 * SIG_IGN the SIGINT to do their shutdown etc.
901 *
902 * For processes ran on the background, we want to install an exit
903 * handler that stops all the threads, removes all breakpoints, and
904 * detaches.
905 */
906void
907ltrace_exiting(void)
908{
909 struct opt_p_t * it;
910 for (it = opt_p; it != NULL; it = it->next) {
911 Process * proc = pid2proc(it->pid);
912 if (proc == NULL || proc->leader == NULL)
913 continue;
914 if (ltrace_exiting_install_handler(proc->leader) < 0)
915 fprintf(stderr,
916 "Couldn't install exiting handler for %d.\n",
917 proc->pid);
918 }
919}
920
Joe Damatodfa3fa32010-11-08 15:47:35 -0800921size_t
922umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
923
924 union {
925 long a;
926 char c[sizeof(long)];
927 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800928 int started = 0;
929 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800930
931 while (offset < len) {
932 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
933 if (a.a == -1 && errno) {
934 if (started && errno == EIO)
935 return bytes_read;
936 else
937 return -1;
938 }
939 started = 1;
940
941 if (len - offset >= sizeof(long)) {
942 memcpy(laddr + offset, &a.c[0], sizeof(long));
943 bytes_read += sizeof(long);
944 }
945 else {
946 memcpy(laddr + offset, &a.c[0], len - offset);
947 bytes_read += (len - offset);
948 }
949 offset += sizeof(long);
950 }
951
952 return bytes_read;
953}
954
Steve Fink7bafff02006-08-07 04:50:42 +0200955/* Read a series of bytes starting at the process's memory address
956 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
957 have been read.
958*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100959int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200960umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100961 union {
962 long a;
963 char c[sizeof(long)];
964 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800965 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100966 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100967
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100968 while (offset < len) {
969 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
970 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200971 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100972 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100973 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100974 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100975 return 0;
976 }
977 }
978 offset += sizeof(long);
979 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100980 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100981 return 0;
982}