blob: f8a1779971db883f844d17e0aa8db66e7183784d [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 Machata98f09922011-07-09 10:55:29 +0200167 int sigstopped;
168 int got_event;
169 int delivered;
170} * pids;
171
172struct pid_set {
173 struct pid_task * tasks;
174 size_t count;
175 size_t alloc;
176};
177
178/**
179 * Breakpoint re-enablement. When we hit a breakpoint, we must
180 * disable it, single-step, and re-enable it. That single-step can be
181 * done only by one task in a task group, while others are stopped,
182 * otherwise the processes would race for who sees the breakpoint
183 * disabled and who doesn't. The following is to keep track of it
184 * all.
185 */
186struct process_stopping_handler
187{
188 Event_Handler super;
189
190 /* The task that is doing the re-enablement. */
191 Process * task_enabling_breakpoint;
192
193 /* The pointer being re-enabled. */
194 Breakpoint * breakpoint_being_enabled;
195
196 enum {
197 /* We are waiting for everyone to land in t/T. */
198 psh_stopping = 0,
199
200 /* We are doing the PTRACE_SINGLESTEP. */
201 psh_singlestep,
202
203 /* We are waiting for all the SIGSTOPs to arrive so
204 * that we can sink them. */
205 psh_sinking,
Petr Machata46d66ab2011-08-20 05:29:25 +0200206
207 /* This is for tracking the ugly workaround. */
208 psh_ugly_workaround,
Petr Machata98f09922011-07-09 10:55:29 +0200209 } state;
210
Petr Machata590c8082011-08-20 22:45:26 +0200211 int exiting;
212
Petr Machata98f09922011-07-09 10:55:29 +0200213 struct pid_set pids;
214};
215
216static enum pcb_status
217task_stopped(Process * task, void * data)
218{
Petr Machata98f09922011-07-09 10:55:29 +0200219 /* If the task is already stopped, don't worry about it.
220 * Likewise if it managed to become a zombie or terminate in
221 * the meantime. This can happen when the whole thread group
222 * is terminating. */
Petr Machata617ff0b2011-10-06 14:23:24 +0200223 switch (process_status(task->pid)) {
224 case ps_invalid:
225 case ps_tracing_stop:
226 case ps_zombie:
Petr Machata98f09922011-07-09 10:55:29 +0200227 return pcb_cont;
Petr Machata617ff0b2011-10-06 14:23:24 +0200228 default:
229 return pcb_stop;
230 }
Petr Machata98f09922011-07-09 10:55:29 +0200231}
232
233static struct pid_task *
234get_task_info(struct pid_set * pids, pid_t pid)
235{
Petr Machata750ca8c2011-10-06 14:29:34 +0200236 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200237 size_t i;
238 for (i = 0; i < pids->count; ++i)
239 if (pids->tasks[i].pid == pid)
240 return &pids->tasks[i];
241
242 return NULL;
243}
244
245static struct pid_task *
246add_task_info(struct pid_set * pids, pid_t pid)
247{
248 if (pids->count == pids->alloc) {
249 size_t ns = (2 * pids->alloc) ?: 4;
250 struct pid_task * n = realloc(pids->tasks,
251 sizeof(*pids->tasks) * ns);
252 if (n == NULL)
253 return NULL;
254 pids->tasks = n;
255 pids->alloc = ns;
256 }
257 struct pid_task * task_info = &pids->tasks[pids->count++];
258 memset(task_info, 0, sizeof(*task_info));
259 task_info->pid = pid;
260 return task_info;
261}
262
263static enum pcb_status
264send_sigstop(Process * task, void * data)
265{
266 Process * leader = task->leader;
267 struct pid_set * pids = data;
268
269 /* Look for pre-existing task record, or add new. */
270 struct pid_task * task_info = get_task_info(pids, task->pid);
271 if (task_info == NULL)
272 task_info = add_task_info(pids, task->pid);
273 if (task_info == NULL) {
274 perror("send_sigstop: add_task_info");
275 destroy_event_handler(leader);
276 /* Signal failure upwards. */
277 return pcb_stop;
278 }
279
280 /* This task still has not been attached to. It should be
281 stopped by the kernel. */
282 if (task->state == STATE_BEING_CREATED)
283 return pcb_cont;
284
285 /* Don't bother sending SIGSTOP if we are already stopped, or
286 * if we sent the SIGSTOP already, which happens when we
287 * inherit the handler from breakpoint re-enablement. */
288 if (task_stopped(task, NULL) == pcb_cont)
289 return pcb_cont;
290 if (task_info->sigstopped) {
291 if (!task_info->delivered)
292 return pcb_cont;
293 task_info->delivered = 0;
294 }
295
296 if (task_kill(task->pid, SIGSTOP) >= 0) {
297 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
298 task_info->sigstopped = 1;
299 } else
300 fprintf(stderr,
301 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
302
303 return pcb_cont;
304}
305
Petr Machata73894bd2011-08-20 23:47:34 +0200306/* On certain kernels, detaching right after a singlestep causes the
307 tracee to be killed with a SIGTRAP (that even though the singlestep
308 was properly caught by waitpid. The ugly workaround is to put a
309 breakpoint where IP points and let the process continue. After
310 this the breakpoint can be retracted and the process detached. */
Petr Machata98f09922011-07-09 10:55:29 +0200311static void
Petr Machata73894bd2011-08-20 23:47:34 +0200312ugly_workaround(Process * proc)
Petr Machata590c8082011-08-20 22:45:26 +0200313{
314 void * ip = get_instruction_pointer(proc);
315 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
316 if (sbp != NULL)
317 enable_breakpoint(proc, sbp);
318 else
319 insert_breakpoint(proc, ip, NULL, 1);
Petr Machata73894bd2011-08-20 23:47:34 +0200320 ptrace(PTRACE_CONT, proc->pid, 0, 0);
Petr Machata590c8082011-08-20 22:45:26 +0200321}
322
323static void
Petr Machata98f09922011-07-09 10:55:29 +0200324process_stopping_done(struct process_stopping_handler * self, Process * leader)
325{
326 debug(DEBUG_PROCESS, "process stopping done %d",
327 self->task_enabling_breakpoint->pid);
328 size_t i;
Petr Machata590c8082011-08-20 22:45:26 +0200329 if (!self->exiting) {
330 for (i = 0; i < self->pids.count; ++i)
331 if (self->pids.tasks[i].pid != 0
332 && self->pids.tasks[i].delivered)
333 continue_process(self->pids.tasks[i].pid);
334 continue_process(self->task_enabling_breakpoint->pid);
335 destroy_event_handler(leader);
336 } else {
337 self->state = psh_ugly_workaround;
Petr Machata73894bd2011-08-20 23:47:34 +0200338 ugly_workaround(self->task_enabling_breakpoint);
Petr Machata590c8082011-08-20 22:45:26 +0200339 }
340}
341
342/* Before we detach, we need to make sure that task's IP is on the
343 * edge of an instruction. So for tasks that have a breakpoint event
344 * in the queue, we adjust the instruction pointer, just like
345 * continue_after_breakpoint does. */
346static enum ecb_status
347undo_breakpoint(Event * event, void * data)
348{
349 if (event != NULL
350 && event->proc->leader == data
351 && event->type == EVENT_BREAKPOINT)
352 set_instruction_pointer(event->proc, event->e_un.brk_addr);
353 return ecb_cont;
354}
355
356static enum pcb_status
357untrace_task(Process * task, void * data)
358{
359 if (task != data)
360 untrace_pid(task->pid);
361 return pcb_cont;
362}
363
364static enum pcb_status
365remove_task(Process * task, void * data)
366{
367 /* Don't untrace leader just yet. */
368 if (task != data)
369 remove_process(task);
370 return pcb_cont;
371}
372
373static void
374detach_process(Process * leader)
375{
376 each_qd_event(&undo_breakpoint, leader);
377 disable_all_breakpoints(leader);
378
379 /* Now untrace the process, if it was attached to by -p. */
380 struct opt_p_t * it;
381 for (it = opt_p; it != NULL; it = it->next) {
382 Process * proc = pid2proc(it->pid);
383 if (proc == NULL)
384 continue;
385 if (proc->leader == leader) {
386 each_task(leader, &untrace_task, NULL);
387 break;
388 }
389 }
390 each_task(leader, &remove_task, leader);
Petr Machata98f09922011-07-09 10:55:29 +0200391 destroy_event_handler(leader);
Petr Machata590c8082011-08-20 22:45:26 +0200392 remove_task(leader, NULL);
Petr Machata98f09922011-07-09 10:55:29 +0200393}
394
395static void
396handle_stopping_event(struct pid_task * task_info, Event ** eventp)
397{
398 /* Mark all events, so that we know whom to SIGCONT later. */
Petr Machata3c9b6292011-08-20 15:05:41 +0200399 if (task_info != NULL)
Petr Machata98f09922011-07-09 10:55:29 +0200400 task_info->got_event = 1;
401
402 Event * event = *eventp;
403
404 /* In every state, sink SIGSTOP events for tasks that it was
405 * sent to. */
406 if (task_info != NULL
407 && event->type == EVENT_SIGNAL
408 && event->e_un.signum == SIGSTOP) {
409 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
410 if (task_info->sigstopped
411 && !task_info->delivered) {
412 task_info->delivered = 1;
413 *eventp = NULL; // sink the event
414 } else
415 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
416 "sigstopped=%d and delivered=%d\n",
417 task_info->pid, task_info->sigstopped,
418 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100419 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100420}
421
Petr Machata98f09922011-07-09 10:55:29 +0200422/* Some SIGSTOPs may have not been delivered to their respective tasks
423 * yet. They are still in the queue. If we have seen an event for
424 * that process, continue it, so that the SIGSTOP can be delivered and
425 * caught by ltrace. */
426static void
427continue_for_sigstop_delivery(struct pid_set * pids)
428{
429 size_t i;
430 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200431 if (pids->tasks[i].pid != 0
432 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200433 && !pids->tasks[i].delivered
434 && pids->tasks[i].got_event) {
435 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
436 pids->tasks[i].pid);
437 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
438 }
439 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100440}
441
Petr Machata98f09922011-07-09 10:55:29 +0200442static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200443event_exit_p(Event * event)
444{
445 return event != NULL && (event->type == EVENT_EXIT
446 || event->type == EVENT_EXIT_SIGNAL);
447}
448
449static int
Petr Machata98f09922011-07-09 10:55:29 +0200450event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200451{
Petr Machata750ca8c2011-10-06 14:29:34 +0200452 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200453 || event->type == EVENT_NONE;
454}
455
456static int
457await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
458 Event * event)
459{
460 /* If we still didn't get our SIGSTOP, continue the process
461 * and carry on. */
462 if (event != NULL && !event_exit_or_none_p(event)
463 && task_info != NULL && task_info->sigstopped) {
464 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
465 task_info->pid);
466 /* We should get the signal the first thing
467 * after this, so it should be OK to continue
468 * even if we are over a breakpoint. */
469 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
470
471 } else {
472 /* If all SIGSTOPs were delivered, uninstall the
473 * handler and continue everyone. */
474 /* XXX I suspect that we should check tasks that are
475 * still around. Is things are now, there should be a
476 * race between waiting for everyone to stop and one
477 * of the tasks exiting. */
478 int all_clear = 1;
479 size_t i;
480 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200481 if (pids->tasks[i].pid != 0
482 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200483 && !pids->tasks[i].delivered) {
484 all_clear = 0;
485 break;
486 }
487 return all_clear;
488 }
489
490 return 0;
491}
492
Petr Machata590c8082011-08-20 22:45:26 +0200493static int
494all_stops_accountable(struct pid_set * pids)
495{
496 size_t i;
497 for (i = 0; i < pids->count; ++i)
498 if (pids->tasks[i].pid != 0
499 && !pids->tasks[i].got_event
500 && !have_events_for(pids->tasks[i].pid))
501 return 0;
502 return 1;
503}
504
Petr Machata98f09922011-07-09 10:55:29 +0200505/* This event handler is installed when we are in the process of
506 * stopping the whole thread group to do the pointer re-enablement for
507 * one of the threads. We pump all events to the queue for later
508 * processing while we wait for all the threads to stop. When this
509 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
510 * re-enable, and continue everyone. */
511static Event *
512process_stopping_on_event(Event_Handler * super, Event * event)
513{
514 struct process_stopping_handler * self = (void *)super;
515 Process * task = event->proc;
516 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200517 Breakpoint * sbp = self->breakpoint_being_enabled;
518 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200519
520 debug(DEBUG_PROCESS,
521 "pid %d; event type %d; state %d",
522 task->pid, event->type, self->state);
523
524 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
525 if (task_info == NULL)
526 fprintf(stderr, "new task??? %d\n", task->pid);
527 handle_stopping_event(task_info, &event);
528
529 int state = self->state;
530 int event_to_queue = !event_exit_or_none_p(event);
531
Petr Machata18c97072011-10-06 14:30:11 +0200532 /* Deactivate the entry if the task exits. */
533 if (event_exit_p(event) && task_info != NULL)
534 task_info->pid = 0;
535
Petr Machata98f09922011-07-09 10:55:29 +0200536 switch (state) {
537 case psh_stopping:
538 /* If everyone is stopped, singlestep. */
539 if (each_task(leader, &task_stopped, NULL) == NULL) {
540 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200541 teb->pid);
542 if (sbp->enabled)
543 disable_breakpoint(teb, sbp);
544 if (ptrace(PTRACE_SINGLESTEP, teb->pid, 0, 0))
Petr Machata750ca8c2011-10-06 14:29:34 +0200545 perror("PTRACE_SINGLESTEP");
Petr Machata98f09922011-07-09 10:55:29 +0200546 self->state = state = psh_singlestep;
547 }
548 break;
549
550 case psh_singlestep: {
551 /* In singlestep state, breakpoint signifies that we
552 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200553 if (event != NULL && task == teb) {
Petr Machata98f09922011-07-09 10:55:29 +0200554 /* Essentially we don't care what event caused
555 * the thread to stop. We can do the
556 * re-enablement now. */
Petr Machata590c8082011-08-20 22:45:26 +0200557 if (sbp->enabled)
558 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200559
560 continue_for_sigstop_delivery(&self->pids);
561
562 self->breakpoint_being_enabled = NULL;
563 self->state = state = psh_sinking;
564
565 if (event->type == EVENT_BREAKPOINT)
566 event = NULL; // handled
567 } else
568 break;
569 }
570
571 /* fall-through */
572
573 case psh_sinking:
574 if (await_sigstop_delivery(&self->pids, task_info, event))
575 process_stopping_done(self, leader);
Petr Machata590c8082011-08-20 22:45:26 +0200576 break;
577
578 case psh_ugly_workaround:
579 if (event == NULL)
580 break;
581 if (event->type == EVENT_BREAKPOINT) {
582 undo_breakpoint(event, leader);
583 if (task == teb)
584 self->task_enabling_breakpoint = NULL;
585 }
586 if (self->task_enabling_breakpoint == NULL
587 && all_stops_accountable(&self->pids)) {
588 undo_breakpoint(event, leader);
589 detach_process(leader);
590 event = NULL; // handled
591 }
Petr Machata98f09922011-07-09 10:55:29 +0200592 }
593
594 if (event != NULL && event_to_queue) {
595 enque_event(event);
596 event = NULL; // sink the event
597 }
598
599 return event;
600}
601
602static void
603process_stopping_destroy(Event_Handler * super)
604{
605 struct process_stopping_handler * self = (void *)super;
Petr Machata98f09922011-07-09 10:55:29 +0200606 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100607}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100608
Juan Cespedesf1350522008-12-16 18:19:58 +0100609void
Petr Machata26627682011-07-08 18:15:32 +0200610continue_after_breakpoint(Process *proc, Breakpoint *sbp)
611{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200612 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100613 if (sbp->enabled == 0) {
614 continue_process(proc->pid);
615 } else {
Petr Machata26627682011-07-08 18:15:32 +0200616 debug(DEBUG_PROCESS,
617 "continue_after_breakpoint: pid=%d, addr=%p",
618 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500619#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100620 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200621 continue_process(proc->pid);
622#else
Petr Machata98f09922011-07-09 10:55:29 +0200623 struct process_stopping_handler * handler
624 = calloc(sizeof(*handler), 1);
625 if (handler == NULL) {
626 perror("malloc breakpoint disable handler");
627 fatal:
628 /* Carry on not bothering to re-enable. */
629 continue_process(proc->pid);
630 return;
631 }
632
633 handler->super.on_event = process_stopping_on_event;
634 handler->super.destroy = process_stopping_destroy;
635 handler->task_enabling_breakpoint = proc;
636 handler->breakpoint_being_enabled = sbp;
637 install_event_handler(proc->leader, &handler->super);
638
639 if (each_task(proc->leader, &send_sigstop,
640 &handler->pids) != NULL)
641 goto fatal;
642
643 /* And deliver the first fake event, in case all the
644 * conditions are already fulfilled. */
645 Event ev;
646 ev.type = EVENT_NONE;
647 ev.proc = proc;
648 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200649#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100650 }
651}
652
Petr Machata602330f2011-07-09 11:15:34 +0200653/**
654 * Ltrace exit. When we are about to exit, we have to go through all
655 * the processes, stop them all, remove all the breakpoints, and then
656 * detach the processes that we attached to using -p. If we left the
657 * other tasks running, they might hit stray return breakpoints and
658 * produce artifacts, so we better stop everyone, even if it's a bit
659 * of extra work.
660 */
661struct ltrace_exiting_handler
662{
663 Event_Handler super;
664 struct pid_set pids;
665};
666
Petr Machata602330f2011-07-09 11:15:34 +0200667static Event *
668ltrace_exiting_on_event(Event_Handler * super, Event * event)
669{
670 struct ltrace_exiting_handler * self = (void *)super;
671 Process * task = event->proc;
672 Process * leader = task->leader;
673
674 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
675
676 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
677 handle_stopping_event(task_info, &event);
678
Petr Machata590c8082011-08-20 22:45:26 +0200679 if (event != NULL && event->type == EVENT_BREAKPOINT)
680 undo_breakpoint(event, leader);
Petr Machata4b9f4d92011-08-20 04:07:05 +0200681
682 if (await_sigstop_delivery(&self->pids, task_info, event)
Petr Machata590c8082011-08-20 22:45:26 +0200683 && all_stops_accountable(&self->pids))
684 detach_process(leader);
Petr Machata602330f2011-07-09 11:15:34 +0200685
686 /* Sink all non-exit events. We are about to exit, so we
687 * don't bother with queuing them. */
688 if (event_exit_or_none_p(event))
689 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200690
Petr Machata13d5df72011-08-19 23:15:15 +0200691 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200692}
693
694static void
695ltrace_exiting_destroy(Event_Handler * super)
696{
697 struct ltrace_exiting_handler * self = (void *)super;
698 free(self->pids.tasks);
699}
700
701static int
702ltrace_exiting_install_handler(Process * proc)
703{
704 /* Only install to leader. */
705 if (proc->leader != proc)
706 return 0;
707
708 /* Perhaps we are already installed, if the user passed
709 * several -p options that are tasks of one process. */
710 if (proc->event_handler != NULL
711 && proc->event_handler->on_event == &ltrace_exiting_on_event)
712 return 0;
713
Petr Machata590c8082011-08-20 22:45:26 +0200714 /* If stopping handler is already present, let it do the
715 * work. */
716 if (proc->event_handler != NULL) {
717 assert(proc->event_handler->on_event
718 == &process_stopping_on_event);
719 struct process_stopping_handler * other
720 = (void *)proc->event_handler;
721 other->exiting = 1;
722 return 0;
723 }
724
Petr Machata602330f2011-07-09 11:15:34 +0200725 struct ltrace_exiting_handler * handler
726 = calloc(sizeof(*handler), 1);
727 if (handler == NULL) {
728 perror("malloc exiting handler");
729 fatal:
730 /* XXXXXXXXXXXXXXXXXXX fixme */
731 return -1;
732 }
733
Petr Machata602330f2011-07-09 11:15:34 +0200734 handler->super.on_event = ltrace_exiting_on_event;
735 handler->super.destroy = ltrace_exiting_destroy;
736 install_event_handler(proc->leader, &handler->super);
737
738 if (each_task(proc->leader, &send_sigstop,
739 &handler->pids) != NULL)
740 goto fatal;
741
742 return 0;
743}
744
745/* If ltrace gets SIGINT, the processes directly or indirectly run by
746 * ltrace get it too. We just have to wait long enough for the signal
747 * to be delivered and the process terminated, which we notice and
748 * exit ltrace, too. So there's not much we need to do there. We
749 * want to keep tracing those processes as usual, in case they just
750 * SIG_IGN the SIGINT to do their shutdown etc.
751 *
752 * For processes ran on the background, we want to install an exit
753 * handler that stops all the threads, removes all breakpoints, and
754 * detaches.
755 */
756void
757ltrace_exiting(void)
758{
759 struct opt_p_t * it;
760 for (it = opt_p; it != NULL; it = it->next) {
761 Process * proc = pid2proc(it->pid);
762 if (proc == NULL || proc->leader == NULL)
763 continue;
764 if (ltrace_exiting_install_handler(proc->leader) < 0)
765 fprintf(stderr,
766 "Couldn't install exiting handler for %d.\n",
767 proc->pid);
768 }
769}
770
Joe Damatodfa3fa32010-11-08 15:47:35 -0800771size_t
772umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
773
774 union {
775 long a;
776 char c[sizeof(long)];
777 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800778 int started = 0;
779 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800780
781 while (offset < len) {
782 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
783 if (a.a == -1 && errno) {
784 if (started && errno == EIO)
785 return bytes_read;
786 else
787 return -1;
788 }
789 started = 1;
790
791 if (len - offset >= sizeof(long)) {
792 memcpy(laddr + offset, &a.c[0], sizeof(long));
793 bytes_read += sizeof(long);
794 }
795 else {
796 memcpy(laddr + offset, &a.c[0], len - offset);
797 bytes_read += (len - offset);
798 }
799 offset += sizeof(long);
800 }
801
802 return bytes_read;
803}
804
Steve Fink7bafff02006-08-07 04:50:42 +0200805/* Read a series of bytes starting at the process's memory address
806 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
807 have been read.
808*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100809int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200810umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100811 union {
812 long a;
813 char c[sizeof(long)];
814 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800815 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100816 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100817
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100818 while (offset < len) {
819 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
820 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200821 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100822 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100823 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100824 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100825 return 0;
826 }
827 }
828 offset += sizeof(long);
829 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100830 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100831 return 0;
832}