blob: 5337cb0497dfa1fe0c4cbfa7c88a8b16539dcb65 [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 Machata06986d52011-11-02 13:22:46 +0100559static void
560singlestep(Process * proc)
561{
562 debug(1, "PTRACE_SINGLESTEP");
563 if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0))
564 perror("PTRACE_SINGLESTEP");
565}
566
Petr Machata98f09922011-07-09 10:55:29 +0200567/* This event handler is installed when we are in the process of
568 * stopping the whole thread group to do the pointer re-enablement for
569 * one of the threads. We pump all events to the queue for later
570 * processing while we wait for all the threads to stop. When this
571 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
572 * re-enable, and continue everyone. */
573static Event *
574process_stopping_on_event(Event_Handler * super, Event * event)
575{
576 struct process_stopping_handler * self = (void *)super;
577 Process * task = event->proc;
578 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200579 Breakpoint * sbp = self->breakpoint_being_enabled;
580 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200581
582 debug(DEBUG_PROCESS,
583 "pid %d; event type %d; state %d",
584 task->pid, event->type, self->state);
585
586 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
587 if (task_info == NULL)
588 fprintf(stderr, "new task??? %d\n", task->pid);
589 handle_stopping_event(task_info, &event);
590
591 int state = self->state;
592 int event_to_queue = !event_exit_or_none_p(event);
593
Petr Machata18c97072011-10-06 14:30:11 +0200594 /* Deactivate the entry if the task exits. */
595 if (event_exit_p(event) && task_info != NULL)
596 task_info->pid = 0;
597
Petr Machata98f09922011-07-09 10:55:29 +0200598 switch (state) {
599 case psh_stopping:
600 /* If everyone is stopped, singlestep. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200601 if (each_task(leader, &task_blocked, &self->pids) == NULL) {
Petr Machata98f09922011-07-09 10:55:29 +0200602 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200603 teb->pid);
604 if (sbp->enabled)
605 disable_breakpoint(teb, sbp);
Petr Machata06986d52011-11-02 13:22:46 +0100606 singlestep(teb);
Petr Machata98f09922011-07-09 10:55:29 +0200607 self->state = state = psh_singlestep;
608 }
609 break;
610
Petr Machata06986d52011-11-02 13:22:46 +0100611 case psh_singlestep:
Petr Machata98f09922011-07-09 10:55:29 +0200612 /* In singlestep state, breakpoint signifies that we
613 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200614 if (event != NULL && task == teb) {
Petr Machatad5d93c42011-10-21 16:41:10 +0200615
Petr Machata06986d52011-11-02 13:22:46 +0100616 /* This is not the singlestep that we are waiting for. */
Petr Machatad5d93c42011-10-21 16:41:10 +0200617 if (event->type == EVENT_SIGNAL) {
Petr Machata06986d52011-11-02 13:22:46 +0100618 singlestep(task);
Petr Machatad5d93c42011-10-21 16:41:10 +0200619 break;
620 }
621
Petr Machata98f09922011-07-09 10:55:29 +0200622 /* Essentially we don't care what event caused
623 * the thread to stop. We can do the
624 * re-enablement now. */
Petr Machata590c8082011-08-20 22:45:26 +0200625 if (sbp->enabled)
626 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200627
628 continue_for_sigstop_delivery(&self->pids);
629
630 self->breakpoint_being_enabled = NULL;
631 self->state = state = psh_sinking;
632
633 if (event->type == EVENT_BREAKPOINT)
634 event = NULL; // handled
635 } else
636 break;
Petr Machata98f09922011-07-09 10:55:29 +0200637
638 /* fall-through */
639
640 case psh_sinking:
641 if (await_sigstop_delivery(&self->pids, task_info, event))
642 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200643 break;
644
645 case psh_ugly_workaround:
646 if (event == NULL)
647 break;
648 if (event->type == EVENT_BREAKPOINT) {
649 undo_breakpoint(event, leader);
650 if (task == teb)
651 self->task_enabling_breakpoint = NULL;
652 }
653 if (self->task_enabling_breakpoint == NULL
654 && all_stops_accountable(&self->pids)) {
655 undo_breakpoint(event, leader);
656 detach_process(leader);
657 event = NULL; // handled
658 }
Petr Machata98f09922011-07-09 10:55:29 +0200659 }
660
661 if (event != NULL && event_to_queue) {
662 enque_event(event);
663 event = NULL; // sink the event
664 }
665
666 return event;
667}
668
669static void
670process_stopping_destroy(Event_Handler * super)
671{
672 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200673 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100674}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100675
Juan Cespedesf1350522008-12-16 18:19:58 +0100676void
Petr Machata26627682011-07-08 18:15:32 +0200677continue_after_breakpoint(Process *proc, Breakpoint *sbp)
678{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200679 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100680 if (sbp->enabled == 0) {
681 continue_process(proc->pid);
682 } else {
Petr Machata26627682011-07-08 18:15:32 +0200683 debug(DEBUG_PROCESS,
684 "continue_after_breakpoint: pid=%d, addr=%p",
685 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500686#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100687 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200688 continue_process(proc->pid);
689#else
Petr Machata98f09922011-07-09 10:55:29 +0200690 struct process_stopping_handler * handler
691 = calloc(sizeof(*handler), 1);
692 if (handler == NULL) {
693 perror("malloc breakpoint disable handler");
694 fatal:
695 /* Carry on not bothering to re-enable. */
696 continue_process(proc->pid);
697 return;
698 }
699
700 handler->super.on_event = process_stopping_on_event;
701 handler->super.destroy = process_stopping_destroy;
702 handler->task_enabling_breakpoint = proc;
703 handler->breakpoint_being_enabled = sbp;
704 install_event_handler(proc->leader, &handler->super);
705
706 if (each_task(proc->leader, &send_sigstop,
707 &handler->pids) != NULL)
708 goto fatal;
709
710 /* And deliver the first fake event, in case all the
711 * conditions are already fulfilled. */
712 Event ev;
713 ev.type = EVENT_NONE;
714 ev.proc = proc;
715 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200716#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100717 }
718}
719
Petr Machata602330f2011-07-09 11:15:34 +0200720/**
721 * Ltrace exit. When we are about to exit, we have to go through all
722 * the processes, stop them all, remove all the breakpoints, and then
723 * detach the processes that we attached to using -p. If we left the
724 * other tasks running, they might hit stray return breakpoints and
725 * produce artifacts, so we better stop everyone, even if it's a bit
726 * of extra work.
727 */
728struct ltrace_exiting_handler
729{
730 Event_Handler super;
731 struct pid_set pids;
732};
733
Petr Machata602330f2011-07-09 11:15:34 +0200734static Event *
735ltrace_exiting_on_event(Event_Handler * super, Event * event)
736{
737 struct ltrace_exiting_handler * self = (void *)super;
738 Process * task = event->proc;
739 Process * leader = task->leader;
740
741 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
742
743 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
744 handle_stopping_event(task_info, &event);
745
Petr Machata590c8082011-08-20 22:45:26 +0200746 if (event != NULL && event->type == EVENT_BREAKPOINT)
747 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200748
749 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200750 && all_stops_accountable(&self->pids))
751 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200752
753 /* Sink all non-exit events. We are about to exit, so we
754 * don't bother with queuing them. */
755 if (event_exit_or_none_p(event))
756 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200757
Petr Machata13d5df72011-08-19 23:15:15 +0200758 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200759}
760
761static void
762ltrace_exiting_destroy(Event_Handler * super)
763{
764 struct ltrace_exiting_handler * self = (void *)super;
765 free(self->pids.tasks);
766}
767
768static int
769ltrace_exiting_install_handler(Process * proc)
770{
771 /* Only install to leader. */
772 if (proc->leader != proc)
773 return 0;
774
775 /* Perhaps we are already installed, if the user passed
776 * several -p options that are tasks of one process. */
777 if (proc->event_handler != NULL
778 && proc->event_handler->on_event == &ltrace_exiting_on_event)
779 return 0;
780
Petr Machata590c8082011-08-20 22:45:26 +0200781 /* If stopping handler is already present, let it do the
782 * work. */
783 if (proc->event_handler != NULL) {
784 assert(proc->event_handler->on_event
785 == &process_stopping_on_event);
786 struct process_stopping_handler * other
787 = (void *)proc->event_handler;
788 other->exiting = 1;
789 return 0;
790 }
791
Petr Machata602330f2011-07-09 11:15:34 +0200792 struct ltrace_exiting_handler * handler
793 = calloc(sizeof(*handler), 1);
794 if (handler == NULL) {
795 perror("malloc exiting handler");
796 fatal:
797 /* XXXXXXXXXXXXXXXXXXX fixme */
798 return -1;
799 }
800
Petr Machata602330f2011-07-09 11:15:34 +0200801 handler->super.on_event = ltrace_exiting_on_event;
802 handler->super.destroy = ltrace_exiting_destroy;
803 install_event_handler(proc->leader, &handler->super);
804
805 if (each_task(proc->leader, &send_sigstop,
806 &handler->pids) != NULL)
807 goto fatal;
808
809 return 0;
810}
811
Petr Machatacbe29c62011-09-27 02:27:58 +0200812/*
813 * When the traced process vforks, it's suspended until the child
814 * process calls _exit or exec*. In the meantime, the two share the
815 * address space.
816 *
817 * The child process should only ever call _exit or exec*, but we
818 * can't count on that (it's not the role of ltrace to policy, but to
819 * observe). In any case, we will _at least_ have to deal with
820 * removal of vfork return breakpoint (which we have to smuggle back
821 * in, so that the parent can see it, too), and introduction of exec*
822 * return breakpoint. Since we already have both breakpoint actions
823 * to deal with, we might as well support it all.
824 *
825 * The gist is that we pretend that the child is in a thread group
826 * with its parent, and handle it as a multi-threaded case, with the
827 * exception that we know that the parent is blocked, and don't
828 * attempt to stop it. When the child execs, we undo the setup.
Petr Machatacbe29c62011-09-27 02:27:58 +0200829 */
830
Petr Machata134a1082011-09-27 20:25:58 +0200831struct process_vfork_handler
832{
833 Event_Handler super;
834 void * bp_addr;
835};
836
Petr Machatacbe29c62011-09-27 02:27:58 +0200837static Event *
838process_vfork_on_event(Event_Handler * super, Event * event)
839{
840 struct process_vfork_handler * self = (void *)super;
Petr Machata134a1082011-09-27 20:25:58 +0200841 Breakpoint * sbp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200842 assert(self != NULL);
843
844 switch (event->type) {
Petr Machata134a1082011-09-27 20:25:58 +0200845 case EVENT_BREAKPOINT:
846 /* Remember the vfork return breakpoint. */
847 if (self->bp_addr == NULL)
848 self->bp_addr = event->e_un.brk_addr;
849 break;
850
Petr Machatacbe29c62011-09-27 02:27:58 +0200851 case EVENT_EXIT:
852 case EVENT_EXIT_SIGNAL:
853 case EVENT_EXEC:
Petr Machata134a1082011-09-27 20:25:58 +0200854 /* Smuggle back in the vfork return breakpoint, so
855 * that our parent can trip over it once again. */
856 if (self->bp_addr != NULL) {
857 sbp = dict_find_entry(event->proc->leader->breakpoints,
858 self->bp_addr);
859 if (sbp != NULL)
Petr Machata3797cd62011-10-03 19:23:37 +0200860 insert_breakpoint(event->proc->parent,
861 self->bp_addr,
862 sbp->libsym, 1);
Petr Machata134a1082011-09-27 20:25:58 +0200863 }
864
Petr Machataba9911f2011-09-27 21:09:47 +0200865 continue_process(event->proc->parent->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200866
867 /* Remove the leader that we artificially set up
868 * earlier. */
Petr Machatacbe29c62011-09-27 02:27:58 +0200869 change_process_leader(event->proc, event->proc);
870 destroy_event_handler(event->proc);
871
Petr Machatacbe29c62011-09-27 02:27:58 +0200872 default:
873 ;
874 }
875
876 return event;
877}
878
879void
880continue_after_vfork(Process * proc)
881{
882 debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
Petr Machata134a1082011-09-27 20:25:58 +0200883 struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
Petr Machatacbe29c62011-09-27 02:27:58 +0200884 if (handler == NULL) {
885 perror("malloc vfork handler");
886 /* Carry on not bothering to treat the process as
887 * necessary. */
888 continue_process(proc->parent->pid);
889 return;
890 }
891
892 /* We must set up custom event handler, so that we see
893 * exec/exit events for the task itself. */
Petr Machata134a1082011-09-27 20:25:58 +0200894 handler->super.on_event = process_vfork_on_event;
895 install_event_handler(proc, &handler->super);
Petr Machatacbe29c62011-09-27 02:27:58 +0200896
897 /* Make sure that the child is sole thread. */
898 assert(proc->leader == proc);
899 assert(proc->next == NULL || proc->next->leader != proc);
900
901 /* Make sure that the child's parent is properly set up. */
902 assert(proc->parent != NULL);
903 assert(proc->parent->leader != NULL);
904
905 change_process_leader(proc, proc->parent->leader);
Petr Machatacbe29c62011-09-27 02:27:58 +0200906}
907
Petr Machata602330f2011-07-09 11:15:34 +0200908/* If ltrace gets SIGINT, the processes directly or indirectly run by
909 * ltrace get it too. We just have to wait long enough for the signal
910 * to be delivered and the process terminated, which we notice and
911 * exit ltrace, too. So there's not much we need to do there. We
912 * want to keep tracing those processes as usual, in case they just
913 * SIG_IGN the SIGINT to do their shutdown etc.
914 *
915 * For processes ran on the background, we want to install an exit
916 * handler that stops all the threads, removes all breakpoints, and
917 * detaches.
918 */
919void
920ltrace_exiting(void)
921{
922 struct opt_p_t * it;
923 for (it = opt_p; it != NULL; it = it->next) {
924 Process * proc = pid2proc(it->pid);
925 if (proc == NULL || proc->leader == NULL)
926 continue;
927 if (ltrace_exiting_install_handler(proc->leader) < 0)
928 fprintf(stderr,
929 "Couldn't install exiting handler for %d.\n",
930 proc->pid);
931 }
932}
933
Joe Damatodfa3fa32010-11-08 15:47:35 -0800934size_t
935umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
936
937 union {
938 long a;
939 char c[sizeof(long)];
940 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800941 int started = 0;
942 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800943
944 while (offset < len) {
945 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
946 if (a.a == -1 && errno) {
947 if (started && errno == EIO)
948 return bytes_read;
949 else
950 return -1;
951 }
952 started = 1;
953
954 if (len - offset >= sizeof(long)) {
955 memcpy(laddr + offset, &a.c[0], sizeof(long));
956 bytes_read += sizeof(long);
957 }
958 else {
959 memcpy(laddr + offset, &a.c[0], len - offset);
960 bytes_read += (len - offset);
961 }
962 offset += sizeof(long);
963 }
964
965 return bytes_read;
966}
967
Steve Fink7bafff02006-08-07 04:50:42 +0200968/* Read a series of bytes starting at the process's memory address
969 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
970 have been read.
971*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100972int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200973umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100974 union {
975 long a;
976 char c[sizeof(long)];
977 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800978 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100979 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100980
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100981 while (offset < len) {
982 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
983 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200984 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100985 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100986 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100987 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100988 return 0;
989 }
990 }
991 offset += sizeof(long);
992 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100993 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100994 return 0;
995}