blob: ba3806dcf93c7e514f043a05fbc2850ff8dabb12 [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
149 the queue for this process. Otherwise just for the other
150 events to arrive. */
151 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:
262 return pcb_cont;
263 default:
264 return pcb_stop;
265 }
266}
267
268/* Task is blocked if it's stopped, or if it's a vfork parent. */
269static enum pcb_status
270task_blocked(Process * task, void * data)
271{
272 struct pid_set * pids = data;
273 struct pid_task * task_info = get_task_info(pids, task->pid);
274 if (task_info != NULL
275 && task_info->vforked)
276 return pcb_cont;
277
278 return task_stopped(task, NULL);
279}
280
281static Event * process_vfork_on_event(Event_Handler * super, Event * event);
282
283static enum pcb_status
284task_vforked(Process * task, void * data)
285{
286 if (task->event_handler != NULL
287 && task->event_handler->on_event == &process_vfork_on_event)
288 return pcb_stop;
289 return pcb_cont;
290}
291
292static int
293is_vfork_parent(Process * task)
294{
295 return each_task(task->leader, &task_vforked, NULL) != NULL;
296}
297
298static enum pcb_status
Petr Machata98f09922011-07-09 10:55:29 +0200299send_sigstop(Process * task, void * data)
300{
301 Process * leader = task->leader;
302 struct pid_set * pids = data;
303
304 /* Look for pre-existing task record, or add new. */
305 struct pid_task * task_info = get_task_info(pids, task->pid);
306 if (task_info == NULL)
307 task_info = add_task_info(pids, task->pid);
308 if (task_info == NULL) {
309 perror("send_sigstop: add_task_info");
310 destroy_event_handler(leader);
311 /* Signal failure upwards. */
312 return pcb_stop;
313 }
314
315 /* This task still has not been attached to. It should be
316 stopped by the kernel. */
317 if (task->state == STATE_BEING_CREATED)
318 return pcb_cont;
319
320 /* Don't bother sending SIGSTOP if we are already stopped, or
Petr Machatacbe29c62011-09-27 02:27:58 +0200321 * if we sent the SIGSTOP already, which happens when we are
322 * handling "onexit" and inherited the handler from breakpoint
323 * re-enablement. */
324 enum process_status st;
325 if (task_stopped(task, &st) == pcb_cont)
Petr Machata98f09922011-07-09 10:55:29 +0200326 return pcb_cont;
327 if (task_info->sigstopped) {
328 if (!task_info->delivered)
329 return pcb_cont;
330 task_info->delivered = 0;
331 }
332
Petr Machatacbe29c62011-09-27 02:27:58 +0200333 /* Also don't attempt to stop the process if it's a parent of
334 * vforked process. We set up event handler specially to hint
335 * us. In that case parent is in D state, which we use to
336 * weed out unnecessary looping. */
337 if (st == ps_sleeping
338 && is_vfork_parent (task)) {
339 task_info->vforked = 1;
340 return pcb_cont;
341 }
342
Petr Machata98f09922011-07-09 10:55:29 +0200343 if (task_kill(task->pid, SIGSTOP) >= 0) {
344 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
345 task_info->sigstopped = 1;
346 } else
347 fprintf(stderr,
348 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
349
350 return pcb_cont;
351}
352
Petr Machata73894bd2011-08-20 23:47:34 +0200353/* On certain kernels, detaching right after a singlestep causes the
354 tracee to be killed with a SIGTRAP (that even though the singlestep
355 was properly caught by waitpid. The ugly workaround is to put a
356 breakpoint where IP points and let the process continue. After
357 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200358static void
Petr Machata73894bd2011-08-20 23:47:34 +0200359ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200360{
361 void * ip = get_instruction_pointer(proc);
362 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
363 if (sbp != NULL)
364 enable_breakpoint(proc, sbp);
365 else
366 insert_breakpoint(proc, ip, NULL, 1);
Petr Machata73894bd2011-08-20 23:47:34 +0200367 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200368}
369
370static void
Petr Machata98f09922011-07-09 10:55:29 +0200371process_stopping_done(struct process_stopping_handler * self, Process * leader)
372{
373 debug(DEBUG_PROCESS, "process stopping done %d",
374 self->task_enabling_breakpoint->pid);
375 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200376 if (!self->exiting) {
377 for (i = 0; i < self->pids.count; ++i)
378 if (self->pids.tasks[i].pid != 0
379 && self->pids.tasks[i].delivered)
380 continue_process(self->pids.tasks[i].pid);
381 continue_process(self->task_enabling_breakpoint->pid);
382 destroy_event_handler(leader);
383 } else {
384 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200385 ugly_workaround(self->task_enabling_breakpoint);
Petr Machata590c8082011-08-20 22:45:26 +0200386 }
387}
388
389/* Before we detach, we need to make sure that task's IP is on the
390 * edge of an instruction. So for tasks that have a breakpoint event
391 * in the queue, we adjust the instruction pointer, just like
392 * continue_after_breakpoint does. */
393static enum ecb_status
394undo_breakpoint(Event * event, void * data)
395{
396 if (event != NULL
397 && event->proc->leader == data
398 && event->type == EVENT_BREAKPOINT)
399 set_instruction_pointer(event->proc, event->e_un.brk_addr);
400 return ecb_cont;
401}
402
403static enum pcb_status
404untrace_task(Process * task, void * data)
405{
406 if (task != data)
407 untrace_pid(task->pid);
408 return pcb_cont;
409}
410
411static enum pcb_status
412remove_task(Process * task, void * data)
413{
414 /* Don't untrace leader just yet. */
415 if (task != data)
416 remove_process(task);
417 return pcb_cont;
418}
419
420static void
421detach_process(Process * leader)
422{
423 each_qd_event(&undo_breakpoint, leader);
424 disable_all_breakpoints(leader);
425
426 /* Now untrace the process, if it was attached to by -p. */
427 struct opt_p_t * it;
428 for (it = opt_p; it != NULL; it = it->next) {
429 Process * proc = pid2proc(it->pid);
430 if (proc == NULL)
431 continue;
432 if (proc->leader == leader) {
433 each_task(leader, &untrace_task, NULL);
434 break;
435 }
436 }
437 each_task(leader, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200438 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200439 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200440}
441
442static void
443handle_stopping_event(struct pid_task * task_info, Event ** eventp)
444{
445 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200446 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200447 task_info->got_event = 1;
448
449 Event * event = *eventp;
450
451 /* In every state, sink SIGSTOP events for tasks that it was
452 * sent to. */
453 if (task_info != NULL
454 && event->type == EVENT_SIGNAL
455 && event->e_un.signum == SIGSTOP) {
456 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
457 if (task_info->sigstopped
458 && !task_info->delivered) {
459 task_info->delivered = 1;
460 *eventp = NULL; // sink the event
461 } else
462 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
463 "sigstopped=%d and delivered=%d\n",
464 task_info->pid, task_info->sigstopped,
465 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100466 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100467}
468
Petr Machata98f09922011-07-09 10:55:29 +0200469/* Some SIGSTOPs may have not been delivered to their respective tasks
470 * yet. They are still in the queue. If we have seen an event for
471 * that process, continue it, so that the SIGSTOP can be delivered and
472 * caught by ltrace. */
473static void
474continue_for_sigstop_delivery(struct pid_set * pids)
475{
476 size_t i;
477 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200478 if (pids->tasks[i].pid != 0
479 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200480 && !pids->tasks[i].delivered
481 && pids->tasks[i].got_event) {
482 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
483 pids->tasks[i].pid);
484 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
485 }
486 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100487}
488
Petr Machata98f09922011-07-09 10:55:29 +0200489static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200490event_exit_p(Event * event)
491{
492 return event != NULL && (event->type == EVENT_EXIT
493 || event->type == EVENT_EXIT_SIGNAL);
494}
495
496static int
Petr Machata98f09922011-07-09 10:55:29 +0200497event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200498{
Petr Machata750ca8c2011-10-06 14:29:34 +0200499 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200500 || event->type == EVENT_NONE;
501}
502
503static int
504await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
505 Event * event)
506{
507 /* If we still didn't get our SIGSTOP, continue the process
508 * and carry on. */
509 if (event != NULL && !event_exit_or_none_p(event)
510 && task_info != NULL && task_info->sigstopped) {
511 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
512 task_info->pid);
513 /* We should get the signal the first thing
514 * after this, so it should be OK to continue
515 * even if we are over a breakpoint. */
516 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
517
518 } else {
519 /* If all SIGSTOPs were delivered, uninstall the
520 * handler and continue everyone. */
521 /* XXX I suspect that we should check tasks that are
522 * still around. Is things are now, there should be a
523 * race between waiting for everyone to stop and one
524 * of the tasks exiting. */
525 int all_clear = 1;
526 size_t i;
527 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200528 if (pids->tasks[i].pid != 0
529 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200530 && !pids->tasks[i].delivered) {
531 all_clear = 0;
532 break;
533 }
534 return all_clear;
535 }
536
537 return 0;
538}
539
Petr Machata590c8082011-08-20 22:45:26 +0200540static int
541all_stops_accountable(struct pid_set * pids)
542{
543 size_t i;
544 for (i = 0; i < pids->count; ++i)
545 if (pids->tasks[i].pid != 0
546 && !pids->tasks[i].got_event
547 && !have_events_for(pids->tasks[i].pid))
548 return 0;
549 return 1;
550}
551
Petr Machata98f09922011-07-09 10:55:29 +0200552/* This event handler is installed when we are in the process of
553 * stopping the whole thread group to do the pointer re-enablement for
554 * one of the threads. We pump all events to the queue for later
555 * processing while we wait for all the threads to stop. When this
556 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
557 * re-enable, and continue everyone. */
558static Event *
559process_stopping_on_event(Event_Handler * super, Event * event)
560{
561 struct process_stopping_handler * self = (void *)super;
562 Process * task = event->proc;
563 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200564 Breakpoint * sbp = self->breakpoint_being_enabled;
565 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200566
567 debug(DEBUG_PROCESS,
568 "pid %d; event type %d; state %d",
569 task->pid, event->type, self->state);
570
571 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
572 if (task_info == NULL)
573 fprintf(stderr, "new task??? %d\n", task->pid);
574 handle_stopping_event(task_info, &event);
575
576 int state = self->state;
577 int event_to_queue = !event_exit_or_none_p(event);
578
Petr Machata18c97072011-10-06 14:30:11 +0200579 /* Deactivate the entry if the task exits. */
580 if (event_exit_p(event) && task_info != NULL)
581 task_info->pid = 0;
582
Petr Machata98f09922011-07-09 10:55:29 +0200583 switch (state) {
584 case psh_stopping:
585 /* If everyone is stopped, singlestep. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200586 if (each_task(leader, &task_blocked, &self->pids) == NULL) {
Petr Machata98f09922011-07-09 10:55:29 +0200587 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200588 teb->pid);
589 if (sbp->enabled)
590 disable_breakpoint(teb, sbp);
591 if (ptrace(PTRACE_SINGLESTEP, teb->pid, 0, 0))
Petr Machata750ca8c2011-10-06 14:29:34 +0200592 perror("PTRACE_SINGLESTEP");
Petr Machata98f09922011-07-09 10:55:29 +0200593 self->state = state = psh_singlestep;
594 }
595 break;
596
597 case psh_singlestep: {
598 /* In singlestep state, breakpoint signifies that we
599 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200600 if (event != NULL && task == teb) {
Petr Machata98f09922011-07-09 10:55:29 +0200601 /* Essentially we don't care what event caused
602 * the thread to stop. We can do the
603 * re-enablement now. */
Petr Machata590c8082011-08-20 22:45:26 +0200604 if (sbp->enabled)
605 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200606
607 continue_for_sigstop_delivery(&self->pids);
608
609 self->breakpoint_being_enabled = NULL;
610 self->state = state = psh_sinking;
611
612 if (event->type == EVENT_BREAKPOINT)
613 event = NULL; // handled
614 } else
615 break;
616 }
617
618 /* fall-through */
619
620 case psh_sinking:
621 if (await_sigstop_delivery(&self->pids, task_info, event))
622 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200623 break;
624
625 case psh_ugly_workaround:
626 if (event == NULL)
627 break;
628 if (event->type == EVENT_BREAKPOINT) {
629 undo_breakpoint(event, leader);
630 if (task == teb)
631 self->task_enabling_breakpoint = NULL;
632 }
633 if (self->task_enabling_breakpoint == NULL
634 && all_stops_accountable(&self->pids)) {
635 undo_breakpoint(event, leader);
636 detach_process(leader);
637 event = NULL; // handled
638 }
Petr Machata98f09922011-07-09 10:55:29 +0200639 }
640
641 if (event != NULL && event_to_queue) {
642 enque_event(event);
643 event = NULL; // sink the event
644 }
645
646 return event;
647}
648
649static void
650process_stopping_destroy(Event_Handler * super)
651{
652 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200653 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100654}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100655
Juan Cespedesf1350522008-12-16 18:19:58 +0100656void
Petr Machata26627682011-07-08 18:15:32 +0200657continue_after_breakpoint(Process *proc, Breakpoint *sbp)
658{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200659 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100660 if (sbp->enabled == 0) {
661 continue_process(proc->pid);
662 } else {
Petr Machata26627682011-07-08 18:15:32 +0200663 debug(DEBUG_PROCESS,
664 "continue_after_breakpoint: pid=%d, addr=%p",
665 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500666#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100667 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200668 continue_process(proc->pid);
669#else
Petr Machata98f09922011-07-09 10:55:29 +0200670 struct process_stopping_handler * handler
671 = calloc(sizeof(*handler), 1);
672 if (handler == NULL) {
673 perror("malloc breakpoint disable handler");
674 fatal:
675 /* Carry on not bothering to re-enable. */
676 continue_process(proc->pid);
677 return;
678 }
679
680 handler->super.on_event = process_stopping_on_event;
681 handler->super.destroy = process_stopping_destroy;
682 handler->task_enabling_breakpoint = proc;
683 handler->breakpoint_being_enabled = sbp;
684 install_event_handler(proc->leader, &handler->super);
685
686 if (each_task(proc->leader, &send_sigstop,
687 &handler->pids) != NULL)
688 goto fatal;
689
690 /* And deliver the first fake event, in case all the
691 * conditions are already fulfilled. */
692 Event ev;
693 ev.type = EVENT_NONE;
694 ev.proc = proc;
695 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200696#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100697 }
698}
699
Petr Machata602330f2011-07-09 11:15:34 +0200700/**
701 * Ltrace exit. When we are about to exit, we have to go through all
702 * the processes, stop them all, remove all the breakpoints, and then
703 * detach the processes that we attached to using -p. If we left the
704 * other tasks running, they might hit stray return breakpoints and
705 * produce artifacts, so we better stop everyone, even if it's a bit
706 * of extra work.
707 */
708struct ltrace_exiting_handler
709{
710 Event_Handler super;
711 struct pid_set pids;
712};
713
Petr Machata602330f2011-07-09 11:15:34 +0200714static Event *
715ltrace_exiting_on_event(Event_Handler * super, Event * event)
716{
717 struct ltrace_exiting_handler * self = (void *)super;
718 Process * task = event->proc;
719 Process * leader = task->leader;
720
721 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
722
723 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
724 handle_stopping_event(task_info, &event);
725
Petr Machata590c8082011-08-20 22:45:26 +0200726 if (event != NULL && event->type == EVENT_BREAKPOINT)
727 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200728
729 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200730 && all_stops_accountable(&self->pids))
731 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200732
733 /* Sink all non-exit events. We are about to exit, so we
734 * don't bother with queuing them. */
735 if (event_exit_or_none_p(event))
736 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200737
Petr Machata13d5df72011-08-19 23:15:15 +0200738 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200739}
740
741static void
742ltrace_exiting_destroy(Event_Handler * super)
743{
744 struct ltrace_exiting_handler * self = (void *)super;
745 free(self->pids.tasks);
746}
747
748static int
749ltrace_exiting_install_handler(Process * proc)
750{
751 /* Only install to leader. */
752 if (proc->leader != proc)
753 return 0;
754
755 /* Perhaps we are already installed, if the user passed
756 * several -p options that are tasks of one process. */
757 if (proc->event_handler != NULL
758 && proc->event_handler->on_event == &ltrace_exiting_on_event)
759 return 0;
760
Petr Machata590c8082011-08-20 22:45:26 +0200761 /* If stopping handler is already present, let it do the
762 * work. */
763 if (proc->event_handler != NULL) {
764 assert(proc->event_handler->on_event
765 == &process_stopping_on_event);
766 struct process_stopping_handler * other
767 = (void *)proc->event_handler;
768 other->exiting = 1;
769 return 0;
770 }
771
Petr Machata602330f2011-07-09 11:15:34 +0200772 struct ltrace_exiting_handler * handler
773 = calloc(sizeof(*handler), 1);
774 if (handler == NULL) {
775 perror("malloc exiting handler");
776 fatal:
777 /* XXXXXXXXXXXXXXXXXXX fixme */
778 return -1;
779 }
780
Petr Machata602330f2011-07-09 11:15:34 +0200781 handler->super.on_event = ltrace_exiting_on_event;
782 handler->super.destroy = ltrace_exiting_destroy;
783 install_event_handler(proc->leader, &handler->super);
784
785 if (each_task(proc->leader, &send_sigstop,
786 &handler->pids) != NULL)
787 goto fatal;
788
789 return 0;
790}
791
Petr Machatacbe29c62011-09-27 02:27:58 +0200792/*
793 * When the traced process vforks, it's suspended until the child
794 * process calls _exit or exec*. In the meantime, the two share the
795 * address space.
796 *
797 * The child process should only ever call _exit or exec*, but we
798 * can't count on that (it's not the role of ltrace to policy, but to
799 * observe). In any case, we will _at least_ have to deal with
800 * removal of vfork return breakpoint (which we have to smuggle back
801 * in, so that the parent can see it, too), and introduction of exec*
802 * return breakpoint. Since we already have both breakpoint actions
803 * to deal with, we might as well support it all.
804 *
805 * The gist is that we pretend that the child is in a thread group
806 * with its parent, and handle it as a multi-threaded case, with the
807 * exception that we know that the parent is blocked, and don't
808 * attempt to stop it. When the child execs, we undo the setup.
809 *
810 * XXX The parent process could be un-suspended before ltrace gets
811 * child exec/exit event. Make sure this is taken care of.
812 */
813
Petr Machata134a1082011-09-27 20:25:58 +0200814struct process_vfork_handler
815{
816 Event_Handler super;
817 void * bp_addr;
818};
819
Petr Machatacbe29c62011-09-27 02:27:58 +0200820static Event *
821process_vfork_on_event(Event_Handler * super, Event * event)
822{
823 struct process_vfork_handler * self = (void *)super;
Petr Machata134a1082011-09-27 20:25:58 +0200824 Breakpoint * sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200825 assert(self != NULL);
826
827 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200828 case EVENT_BREAKPOINT:
829 /* Remember the vfork return breakpoint. */
830 if (self->bp_addr == NULL)
831 self->bp_addr = event->e_un.brk_addr;
832 break;
833
Petr Machatacbe29c62011-09-27 02:27:58 +0200834 case EVENT_EXIT:
835 case EVENT_EXIT_SIGNAL:
836 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200837 /* Smuggle back in the vfork return breakpoint, so
838 * that our parent can trip over it once again. */
839 if (self->bp_addr != NULL) {
840 sbp = dict_find_entry(event->proc->leader->breakpoints,
841 self->bp_addr);
842 if (sbp != NULL)
843 insert_breakpoint(event->proc->leader,
844 self->bp_addr, sbp->libsym,
845 1);
846 }
847
Petr Machataba9911f2011-09-27 21:09:47 +0200848 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200849
850 /* Remove the leader that we artificially set up
851 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200852 change_process_leader(event->proc, event->proc);
853 destroy_event_handler(event->proc);
854
855 /* XXXXX this could happen in the middle of handling
856 * multi-threaded breakpoint. We must be careful to
857 * undo the effects that we introduced above (vforked
858 * = 1 et.al.). */
859
860 default:
861 ;
862 }
863
864 return event;
865}
866
867void
868continue_after_vfork(Process * proc)
869{
870 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200871 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +0200872 if (handler == NULL) {
873 perror("malloc vfork handler");
874 /* Carry on not bothering to treat the process as
875 * necessary. */
876 continue_process(proc->parent->pid);
877 return;
878 }
879
880 /* We must set up custom event handler, so that we see
881 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +0200882 handler->super.on_event = process_vfork_on_event;
883 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +0200884
885 /* Make sure that the child is sole thread. */
886 assert(proc->leader == proc);
887 assert(proc->next == NULL || proc->next->leader != proc);
888
889 /* Make sure that the child's parent is properly set up. */
890 assert(proc->parent != NULL);
891 assert(proc->parent->leader != NULL);
892
893 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +0200894}
895
Petr Machata602330f2011-07-09 11:15:34 +0200896/* If ltrace gets SIGINT, the processes directly or indirectly run by
897 * ltrace get it too. We just have to wait long enough for the signal
898 * to be delivered and the process terminated, which we notice and
899 * exit ltrace, too. So there's not much we need to do there. We
900 * want to keep tracing those processes as usual, in case they just
901 * SIG_IGN the SIGINT to do their shutdown etc.
902 *
903 * For processes ran on the background, we want to install an exit
904 * handler that stops all the threads, removes all breakpoints, and
905 * detaches.
906 */
907void
908ltrace_exiting(void)
909{
910 struct opt_p_t * it;
911 for (it = opt_p; it != NULL; it = it->next) {
912 Process * proc = pid2proc(it->pid);
913 if (proc == NULL || proc->leader == NULL)
914 continue;
915 if (ltrace_exiting_install_handler(proc->leader) < 0)
916 fprintf(stderr,
917 "Couldn't install exiting handler for %d.\n",
918 proc->pid);
919 }
920}
921
Joe Damatodfa3fa32010-11-08 15:47:35 -0800922size_t
923umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
924
925 union {
926 long a;
927 char c[sizeof(long)];
928 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800929 int started = 0;
930 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800931
932 while (offset < len) {
933 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
934 if (a.a == -1 && errno) {
935 if (started && errno == EIO)
936 return bytes_read;
937 else
938 return -1;
939 }
940 started = 1;
941
942 if (len - offset >= sizeof(long)) {
943 memcpy(laddr + offset, &a.c[0], sizeof(long));
944 bytes_read += sizeof(long);
945 }
946 else {
947 memcpy(laddr + offset, &a.c[0], len - offset);
948 bytes_read += (len - offset);
949 }
950 offset += sizeof(long);
951 }
952
953 return bytes_read;
954}
955
Steve Fink7bafff02006-08-07 04:50:42 +0200956/* Read a series of bytes starting at the process's memory address
957 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
958 have been read.
959*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100960int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200961umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100962 union {
963 long a;
964 char c[sizeof(long)];
965 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800966 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100967 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100968
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100969 while (offset < len) {
970 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
971 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200972 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100973 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100974 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100975 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100976 return 0;
977 }
978 }
979 offset += sizeof(long);
980 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100981 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100982 return 0;
983}