blob: bac09327f04e6afeaa9a838dc4c2a6cdf4f82b7c [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 Cespedesa8909f72009-04-28 20:02:41 +0200125 Process *proc;
Juan Cespedese74c80d2009-02-11 11:32:31 +0100126
Juan Cespedescd8976d2009-05-14 13:47:58 +0200127 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
128
Juan Cespedese74c80d2009-02-11 11:32:31 +0100129 proc = pid2proc(pid);
Petr Machata98f09922011-07-09 10:55:29 +0200130 ptrace(PTRACE_SYSCALL, pid, 0, signum);
131}
132
133static enum ecb_status
134event_for_pid(Event * event, void * data)
135{
136 if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
137 return ecb_yield;
138 return ecb_cont;
139}
140
141static int
142have_events_for(pid_t pid)
143{
144 return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
145}
146
147void
148continue_process(pid_t pid)
149{
150 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
Petr Machata98f09922011-07-09 10:55:29 +0200151
152 /* Only really continue the process if there are no events in
153 the queue for this process. Otherwise just for the other
154 events to arrive. */
155 if (!have_events_for(pid))
156 /* We always trace syscalls to control fork(),
157 * clone(), execve()... */
158 ptrace(PTRACE_SYSCALL, pid, 0, 0);
159 else
160 debug(DEBUG_PROCESS,
161 "putting off the continue, events in que.");
162}
163
164/**
165 * This is used for bookkeeping related to PIDs that the event
Petr Machata750ca8c2011-10-06 14:29:34 +0200166 * handlers work with.
167 */
Petr Machata98f09922011-07-09 10:55:29 +0200168struct pid_task {
Petr Machata750ca8c2011-10-06 14:29:34 +0200169 pid_t pid; /* This may be 0 for tasks that exited
170 * mid-handling. */
Petr Machata98f09922011-07-09 10:55:29 +0200171 int sigstopped;
172 int got_event;
173 int delivered;
174} * pids;
175
176struct pid_set {
177 struct pid_task * tasks;
178 size_t count;
179 size_t alloc;
180};
181
182/**
183 * Breakpoint re-enablement. When we hit a breakpoint, we must
184 * disable it, single-step, and re-enable it. That single-step can be
185 * done only by one task in a task group, while others are stopped,
186 * otherwise the processes would race for who sees the breakpoint
187 * disabled and who doesn't. The following is to keep track of it
188 * all.
189 */
190struct process_stopping_handler
191{
192 Event_Handler super;
193
194 /* The task that is doing the re-enablement. */
195 Process * task_enabling_breakpoint;
196
197 /* The pointer being re-enabled. */
198 Breakpoint * breakpoint_being_enabled;
199
200 enum {
201 /* We are waiting for everyone to land in t/T. */
202 psh_stopping = 0,
203
204 /* We are doing the PTRACE_SINGLESTEP. */
205 psh_singlestep,
206
207 /* We are waiting for all the SIGSTOPs to arrive so
208 * that we can sink them. */
209 psh_sinking,
Petr Machata46d66ab2011-08-20 05:29:25 +0200210
211 /* This is for tracking the ugly workaround. */
212 psh_ugly_workaround,
Petr Machata98f09922011-07-09 10:55:29 +0200213 } state;
214
215 struct pid_set pids;
216};
217
218static enum pcb_status
219task_stopped(Process * task, void * data)
220{
Petr Machata98f09922011-07-09 10:55:29 +0200221 /* If the task is already stopped, don't worry about it.
222 * Likewise if it managed to become a zombie or terminate in
223 * the meantime. This can happen when the whole thread group
224 * is terminating. */
Petr Machata617ff0b2011-10-06 14:23:24 +0200225 switch (process_status(task->pid)) {
226 case ps_invalid:
227 case ps_tracing_stop:
228 case ps_zombie:
Petr Machata98f09922011-07-09 10:55:29 +0200229 return pcb_cont;
Petr Machata617ff0b2011-10-06 14:23:24 +0200230 default:
231 return pcb_stop;
232 }
Petr Machata98f09922011-07-09 10:55:29 +0200233}
234
235static struct pid_task *
236get_task_info(struct pid_set * pids, pid_t pid)
237{
Petr Machata750ca8c2011-10-06 14:29:34 +0200238 assert(pid != 0);
Petr Machata98f09922011-07-09 10:55:29 +0200239 size_t i;
240 for (i = 0; i < pids->count; ++i)
241 if (pids->tasks[i].pid == pid)
242 return &pids->tasks[i];
243
244 return NULL;
245}
246
247static struct pid_task *
248add_task_info(struct pid_set * pids, pid_t pid)
249{
250 if (pids->count == pids->alloc) {
251 size_t ns = (2 * pids->alloc) ?: 4;
252 struct pid_task * n = realloc(pids->tasks,
253 sizeof(*pids->tasks) * ns);
254 if (n == NULL)
255 return NULL;
256 pids->tasks = n;
257 pids->alloc = ns;
258 }
259 struct pid_task * task_info = &pids->tasks[pids->count++];
260 memset(task_info, 0, sizeof(*task_info));
261 task_info->pid = pid;
262 return task_info;
263}
264
265static enum pcb_status
266send_sigstop(Process * task, void * data)
267{
268 Process * leader = task->leader;
269 struct pid_set * pids = data;
270
271 /* Look for pre-existing task record, or add new. */
272 struct pid_task * task_info = get_task_info(pids, task->pid);
273 if (task_info == NULL)
274 task_info = add_task_info(pids, task->pid);
275 if (task_info == NULL) {
276 perror("send_sigstop: add_task_info");
277 destroy_event_handler(leader);
278 /* Signal failure upwards. */
279 return pcb_stop;
280 }
281
282 /* This task still has not been attached to. It should be
283 stopped by the kernel. */
284 if (task->state == STATE_BEING_CREATED)
285 return pcb_cont;
286
287 /* Don't bother sending SIGSTOP if we are already stopped, or
288 * if we sent the SIGSTOP already, which happens when we
289 * inherit the handler from breakpoint re-enablement. */
290 if (task_stopped(task, NULL) == pcb_cont)
291 return pcb_cont;
292 if (task_info->sigstopped) {
293 if (!task_info->delivered)
294 return pcb_cont;
295 task_info->delivered = 0;
296 }
297
298 if (task_kill(task->pid, SIGSTOP) >= 0) {
299 debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
300 task_info->sigstopped = 1;
301 } else
302 fprintf(stderr,
303 "Warning: couldn't send SIGSTOP to %d\n", task->pid);
304
305 return pcb_cont;
306}
307
308static void
309process_stopping_done(struct process_stopping_handler * self, Process * leader)
310{
311 debug(DEBUG_PROCESS, "process stopping done %d",
312 self->task_enabling_breakpoint->pid);
313 size_t i;
314 for (i = 0; i < self->pids.count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200315 if (self->pids.tasks[i].pid != 0
316 && self->pids.tasks[i].delivered)
Petr Machata98f09922011-07-09 10:55:29 +0200317 continue_process(self->pids.tasks[i].pid);
318 continue_process(self->task_enabling_breakpoint->pid);
319 destroy_event_handler(leader);
320}
321
322static void
323handle_stopping_event(struct pid_task * task_info, Event ** eventp)
324{
325 /* Mark all events, so that we know whom to SIGCONT later. */
326 if (task_info != NULL && task_info->sigstopped)
327 task_info->got_event = 1;
328
329 Event * event = *eventp;
330
331 /* In every state, sink SIGSTOP events for tasks that it was
332 * sent to. */
333 if (task_info != NULL
334 && event->type == EVENT_SIGNAL
335 && event->e_un.signum == SIGSTOP) {
336 debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
337 if (task_info->sigstopped
338 && !task_info->delivered) {
339 task_info->delivered = 1;
340 *eventp = NULL; // sink the event
341 } else
342 fprintf(stderr, "suspicious: %d got SIGSTOP, but "
343 "sigstopped=%d and delivered=%d\n",
344 task_info->pid, task_info->sigstopped,
345 task_info->delivered);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100346 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100347}
348
Petr Machata98f09922011-07-09 10:55:29 +0200349/* Some SIGSTOPs may have not been delivered to their respective tasks
350 * yet. They are still in the queue. If we have seen an event for
351 * that process, continue it, so that the SIGSTOP can be delivered and
352 * caught by ltrace. */
353static void
354continue_for_sigstop_delivery(struct pid_set * pids)
355{
356 size_t i;
357 for (i = 0; i < pids->count; ++i) {
Petr Machata750ca8c2011-10-06 14:29:34 +0200358 if (pids->tasks[i].pid != 0
359 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200360 && !pids->tasks[i].delivered
361 && pids->tasks[i].got_event) {
362 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
363 pids->tasks[i].pid);
364 ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
365 }
366 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100367}
368
Petr Machata98f09922011-07-09 10:55:29 +0200369static int
Petr Machata750ca8c2011-10-06 14:29:34 +0200370event_exit_p(Event * event)
371{
372 return event != NULL && (event->type == EVENT_EXIT
373 || event->type == EVENT_EXIT_SIGNAL);
374}
375
376static int
Petr Machata98f09922011-07-09 10:55:29 +0200377event_exit_or_none_p(Event * event)
Petr Machataf789c9c2011-07-09 10:54:27 +0200378{
Petr Machata750ca8c2011-10-06 14:29:34 +0200379 return event == NULL || event_exit_p(event)
Petr Machata98f09922011-07-09 10:55:29 +0200380 || event->type == EVENT_NONE;
381}
382
383static int
384await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
385 Event * event)
386{
387 /* If we still didn't get our SIGSTOP, continue the process
388 * and carry on. */
389 if (event != NULL && !event_exit_or_none_p(event)
390 && task_info != NULL && task_info->sigstopped) {
391 debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
392 task_info->pid);
393 /* We should get the signal the first thing
394 * after this, so it should be OK to continue
395 * even if we are over a breakpoint. */
396 ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
397
398 } else {
399 /* If all SIGSTOPs were delivered, uninstall the
400 * handler and continue everyone. */
401 /* XXX I suspect that we should check tasks that are
402 * still around. Is things are now, there should be a
403 * race between waiting for everyone to stop and one
404 * of the tasks exiting. */
405 int all_clear = 1;
406 size_t i;
407 for (i = 0; i < pids->count; ++i)
Petr Machata750ca8c2011-10-06 14:29:34 +0200408 if (pids->tasks[i].pid != 0
409 && pids->tasks[i].sigstopped
Petr Machata98f09922011-07-09 10:55:29 +0200410 && !pids->tasks[i].delivered) {
411 all_clear = 0;
412 break;
413 }
414 return all_clear;
415 }
416
417 return 0;
418}
419
420/* This event handler is installed when we are in the process of
421 * stopping the whole thread group to do the pointer re-enablement for
422 * one of the threads. We pump all events to the queue for later
423 * processing while we wait for all the threads to stop. When this
424 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
425 * re-enable, and continue everyone. */
426static Event *
427process_stopping_on_event(Event_Handler * super, Event * event)
428{
429 struct process_stopping_handler * self = (void *)super;
430 Process * task = event->proc;
431 Process * leader = task->leader;
Petr Machatae21264e2011-10-06 14:30:33 +0200432 Breakpoint * sbp = self->breakpoint_being_enabled;
433 Process * teb = self->task_enabling_breakpoint;
Petr Machata98f09922011-07-09 10:55:29 +0200434
435 debug(DEBUG_PROCESS,
436 "pid %d; event type %d; state %d",
437 task->pid, event->type, self->state);
438
439 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
440 if (task_info == NULL)
441 fprintf(stderr, "new task??? %d\n", task->pid);
442 handle_stopping_event(task_info, &event);
443
444 int state = self->state;
445 int event_to_queue = !event_exit_or_none_p(event);
446
Petr Machata18c97072011-10-06 14:30:11 +0200447 /* Deactivate the entry if the task exits. */
448 if (event_exit_p(event) && task_info != NULL)
449 task_info->pid = 0;
450
Petr Machata98f09922011-07-09 10:55:29 +0200451 switch (state) {
452 case psh_stopping:
453 /* If everyone is stopped, singlestep. */
454 if (each_task(leader, &task_stopped, NULL) == NULL) {
455 debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
Petr Machatae21264e2011-10-06 14:30:33 +0200456 teb->pid);
457 if (sbp->enabled)
458 disable_breakpoint(teb, sbp);
459 if (ptrace(PTRACE_SINGLESTEP, teb->pid, 0, 0))
Petr Machata750ca8c2011-10-06 14:29:34 +0200460 perror("PTRACE_SINGLESTEP");
Petr Machata98f09922011-07-09 10:55:29 +0200461 self->state = state = psh_singlestep;
462 }
463 break;
464
465 case psh_singlestep: {
466 /* In singlestep state, breakpoint signifies that we
467 * have now stepped, and can re-enable the breakpoint. */
Petr Machatae21264e2011-10-06 14:30:33 +0200468 if (event != NULL && task == teb) {
Petr Machata98f09922011-07-09 10:55:29 +0200469 /* Essentially we don't care what event caused
470 * the thread to stop. We can do the
471 * re-enablement now. */
Petr Machatae21264e2011-10-06 14:30:33 +0200472 enable_breakpoint(teb, sbp);
Petr Machata98f09922011-07-09 10:55:29 +0200473
474 continue_for_sigstop_delivery(&self->pids);
475
476 self->breakpoint_being_enabled = NULL;
477 self->state = state = psh_sinking;
478
479 if (event->type == EVENT_BREAKPOINT)
480 event = NULL; // handled
481 } else
482 break;
483 }
484
485 /* fall-through */
486
487 case psh_sinking:
488 if (await_sigstop_delivery(&self->pids, task_info, event))
489 process_stopping_done(self, leader);
490 }
491
492 if (event != NULL && event_to_queue) {
493 enque_event(event);
494 event = NULL; // sink the event
495 }
496
497 return event;
498}
499
500static void
501process_stopping_destroy(Event_Handler * super)
502{
503 struct process_stopping_handler * self = (void *)super;
504 if (self->breakpoint_being_enabled != NULL)
505 enable_breakpoint(self->task_enabling_breakpoint,
506 self->breakpoint_being_enabled);
507 free(self->pids.tasks);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100508}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100509
Juan Cespedesf1350522008-12-16 18:19:58 +0100510void
Petr Machata26627682011-07-08 18:15:32 +0200511continue_after_breakpoint(Process *proc, Breakpoint *sbp)
512{
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200513 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100514 if (sbp->enabled == 0) {
Petr Machatae21264e2011-10-06 14:30:33 +0200515 if (sbp->enabled)
516 disable_breakpoint(proc, sbp);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100517 continue_process(proc->pid);
518 } else {
Petr Machata26627682011-07-08 18:15:32 +0200519 debug(DEBUG_PROCESS,
520 "continue_after_breakpoint: pid=%d, addr=%p",
521 proc->pid, sbp->addr);
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500522#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100523 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200524 continue_process(proc->pid);
525#else
Petr Machata98f09922011-07-09 10:55:29 +0200526 struct process_stopping_handler * handler
527 = calloc(sizeof(*handler), 1);
528 if (handler == NULL) {
529 perror("malloc breakpoint disable handler");
530 fatal:
531 /* Carry on not bothering to re-enable. */
532 continue_process(proc->pid);
533 return;
534 }
535
536 handler->super.on_event = process_stopping_on_event;
537 handler->super.destroy = process_stopping_destroy;
538 handler->task_enabling_breakpoint = proc;
539 handler->breakpoint_being_enabled = sbp;
540 install_event_handler(proc->leader, &handler->super);
541
542 if (each_task(proc->leader, &send_sigstop,
543 &handler->pids) != NULL)
544 goto fatal;
545
546 /* And deliver the first fake event, in case all the
547 * conditions are already fulfilled. */
548 Event ev;
549 ev.type = EVENT_NONE;
550 ev.proc = proc;
551 process_stopping_on_event(&handler->super, &ev);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200552#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100553 }
554}
555
Petr Machata602330f2011-07-09 11:15:34 +0200556/**
557 * Ltrace exit. When we are about to exit, we have to go through all
558 * the processes, stop them all, remove all the breakpoints, and then
559 * detach the processes that we attached to using -p. If we left the
560 * other tasks running, they might hit stray return breakpoints and
561 * produce artifacts, so we better stop everyone, even if it's a bit
562 * of extra work.
563 */
564struct ltrace_exiting_handler
565{
566 Event_Handler super;
567 struct pid_set pids;
Petr Machata13d5df72011-08-19 23:15:15 +0200568 int state;
569 Process * task_enabling_breakpoint;
Petr Machata602330f2011-07-09 11:15:34 +0200570};
571
572static enum pcb_status
573remove_task(Process * task, void * data)
574{
575 /* Don't untrace leader just yet. */
576 if (task != data)
577 remove_process(task);
578 return pcb_cont;
579}
580
581static enum pcb_status
582untrace_task(Process * task, void * data)
583{
584 untrace_pid(task->pid);
585 return pcb_cont;
586}
587
Petr Machata68abfea2011-08-19 22:11:04 +0200588/* Before we detach, we need to make sure that task's IP is on the
589 * edge of an instruction. So for tasks that have a breakpoint event
590 * in the queue, we adjust the instruction pointer, just like
591 * continue_after_breakpoint does. */
592static enum ecb_status
593undo_breakpoint(Event * event, void * data)
594{
Petr Machata13d5df72011-08-19 23:15:15 +0200595 if (event != NULL
596 && event->proc->leader == data
Petr Machata68abfea2011-08-19 22:11:04 +0200597 && event->type == EVENT_BREAKPOINT) {
598 fprintf(stderr, " + %p ", get_instruction_pointer(event->proc));
599 set_instruction_pointer(event->proc, event->e_un.brk_addr);
600 fprintf(stderr, "-> %p\n", get_instruction_pointer(event->proc));
601 }
602 return ecb_cont;
603}
604
Petr Machata46d66ab2011-08-20 05:29:25 +0200605static void
606ugly_workaround(Process * proc, int cont)
607{
608 void * ip = get_instruction_pointer(proc);
609 fprintf(stderr, "activate workaround %d %p\n", proc->pid, ip);
610 Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
611 if (sbp != NULL)
612 enable_breakpoint(proc, sbp);
613 else
614 insert_breakpoint(proc, ip, NULL, 1);
615 if (cont)
616 ptrace(PTRACE_CONT, proc->pid, 0, 0);
617}
618
Petr Machata602330f2011-07-09 11:15:34 +0200619static Event *
620ltrace_exiting_on_event(Event_Handler * super, Event * event)
621{
622 struct ltrace_exiting_handler * self = (void *)super;
623 Process * task = event->proc;
624 Process * leader = task->leader;
625
626 debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
627
628 struct pid_task * task_info = get_task_info(&self->pids, task->pid);
629 handle_stopping_event(task_info, &event);
630
Petr Machata4b9f4d92011-08-20 04:07:05 +0200631 if (event != NULL && event->type == EVENT_BREAKPOINT) {
632 if (self->state == psh_singlestep
633 && self->task_enabling_breakpoint == event->proc) {
Petr Machata46d66ab2011-08-20 05:29:25 +0200634 ugly_workaround(event->proc, 1);
635 self->state = psh_ugly_workaround;
636 } else {
Petr Machata4b9f4d92011-08-20 04:07:05 +0200637 undo_breakpoint(event, leader);
Petr Machata46d66ab2011-08-20 05:29:25 +0200638 if (self->state == psh_ugly_workaround) {
639 self->task_enabling_breakpoint = NULL;
640 self->state = psh_sinking;
641 }
642 }
Petr Machata4b9f4d92011-08-20 04:07:05 +0200643 }
644
645 if (await_sigstop_delivery(&self->pids, task_info, event)
646 && (self->task_enabling_breakpoint == NULL
Petr Machata46d66ab2011-08-20 05:29:25 +0200647 /* NB: psh_ugly_workaround > psh_singlestep */
648 || self->state < psh_singlestep)) {
Petr Machata602330f2011-07-09 11:15:34 +0200649 debug(DEBUG_PROCESS, "all SIGSTOPs delivered %d", leader->pid);
Petr Machata68abfea2011-08-19 22:11:04 +0200650 each_qd_event(&undo_breakpoint, leader);
Petr Machata602330f2011-07-09 11:15:34 +0200651 disable_all_breakpoints(leader);
652
653 /* Now untrace the process, if it was attached to by -p. */
654 struct opt_p_t * it;
655 for (it = opt_p; it != NULL; it = it->next) {
656 Process * proc = pid2proc(it->pid);
657 if (proc == NULL)
658 continue;
659 if (proc->leader == leader) {
660 each_task(leader, &untrace_task, NULL);
661 break;
662 }
663 }
664
665 each_task(leader, &remove_task, leader);
666 destroy_event_handler(leader);
667 remove_task(leader, NULL);
668 return NULL;
669 }
670
671 /* Sink all non-exit events. We are about to exit, so we
672 * don't bother with queuing them. */
673 if (event_exit_or_none_p(event))
674 return event;
Petr Machata13d5df72011-08-19 23:15:15 +0200675
Petr Machata13d5df72011-08-19 23:15:15 +0200676 return NULL;
Petr Machata602330f2011-07-09 11:15:34 +0200677}
678
679static void
680ltrace_exiting_destroy(Event_Handler * super)
681{
682 struct ltrace_exiting_handler * self = (void *)super;
683 free(self->pids.tasks);
684}
685
686static int
687ltrace_exiting_install_handler(Process * proc)
688{
689 /* Only install to leader. */
690 if (proc->leader != proc)
691 return 0;
692
693 /* Perhaps we are already installed, if the user passed
694 * several -p options that are tasks of one process. */
695 if (proc->event_handler != NULL
696 && proc->event_handler->on_event == &ltrace_exiting_on_event)
697 return 0;
698
699 struct ltrace_exiting_handler * handler
700 = calloc(sizeof(*handler), 1);
701 if (handler == NULL) {
702 perror("malloc exiting handler");
703 fatal:
704 /* XXXXXXXXXXXXXXXXXXX fixme */
705 return -1;
706 }
707
708 /* If we are in the middle of breakpoint, extract the
709 * pid-state information from that handler so that we can take
710 * over the SIGSTOP handling. */
711 if (proc->event_handler != NULL) {
712 debug(DEBUG_PROCESS, "taking over breakpoint handling");
713 assert(proc->event_handler->on_event
714 == &process_stopping_on_event);
715 struct process_stopping_handler * other
716 = (void *)proc->event_handler;
Petr Machata13d5df72011-08-19 23:15:15 +0200717
Petr Machata13d5df72011-08-19 23:15:15 +0200718 handler->task_enabling_breakpoint
719 = other->task_enabling_breakpoint;
Petr Machata46d66ab2011-08-20 05:29:25 +0200720 if (other->state == psh_sinking) {
721 ugly_workaround(other->task_enabling_breakpoint, 0);
722 handler->state = psh_ugly_workaround;
723 } else {
724 handler->state = other->state;
725 }
Petr Machata13d5df72011-08-19 23:15:15 +0200726
Petr Machata602330f2011-07-09 11:15:34 +0200727 size_t i;
728 for (i = 0; i < other->pids.count; ++i) {
729 struct pid_task * oti = &other->pids.tasks[i];
Petr Machata750ca8c2011-10-06 14:29:34 +0200730 if (oti->pid == 0)
731 continue;
732
Petr Machata602330f2011-07-09 11:15:34 +0200733 struct pid_task * task_info
734 = add_task_info(&handler->pids, oti->pid);
735 if (task_info == NULL) {
736 perror("ltrace_exiting_install_handler"
737 ":add_task_info");
738 goto fatal;
739 }
740 /* Copy over the state. */
741 *task_info = *oti;
742 }
743
744 /* And destroy the original handler. */
745 destroy_event_handler(proc);
746 }
747
748 handler->super.on_event = ltrace_exiting_on_event;
749 handler->super.destroy = ltrace_exiting_destroy;
750 install_event_handler(proc->leader, &handler->super);
751
752 if (each_task(proc->leader, &send_sigstop,
753 &handler->pids) != NULL)
754 goto fatal;
755
756 return 0;
757}
758
759/* If ltrace gets SIGINT, the processes directly or indirectly run by
760 * ltrace get it too. We just have to wait long enough for the signal
761 * to be delivered and the process terminated, which we notice and
762 * exit ltrace, too. So there's not much we need to do there. We
763 * want to keep tracing those processes as usual, in case they just
764 * SIG_IGN the SIGINT to do their shutdown etc.
765 *
766 * For processes ran on the background, we want to install an exit
767 * handler that stops all the threads, removes all breakpoints, and
768 * detaches.
769 */
770void
771ltrace_exiting(void)
772{
773 struct opt_p_t * it;
774 for (it = opt_p; it != NULL; it = it->next) {
775 Process * proc = pid2proc(it->pid);
776 if (proc == NULL || proc->leader == NULL)
777 continue;
778 if (ltrace_exiting_install_handler(proc->leader) < 0)
779 fprintf(stderr,
780 "Couldn't install exiting handler for %d.\n",
781 proc->pid);
782 }
783}
784
Joe Damatodfa3fa32010-11-08 15:47:35 -0800785size_t
786umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
787
788 union {
789 long a;
790 char c[sizeof(long)];
791 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800792 int started = 0;
793 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800794
795 while (offset < len) {
796 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
797 if (a.a == -1 && errno) {
798 if (started && errno == EIO)
799 return bytes_read;
800 else
801 return -1;
802 }
803 started = 1;
804
805 if (len - offset >= sizeof(long)) {
806 memcpy(laddr + offset, &a.c[0], sizeof(long));
807 bytes_read += sizeof(long);
808 }
809 else {
810 memcpy(laddr + offset, &a.c[0], len - offset);
811 bytes_read += (len - offset);
812 }
813 offset += sizeof(long);
814 }
815
816 return bytes_read;
817}
818
Steve Fink7bafff02006-08-07 04:50:42 +0200819/* Read a series of bytes starting at the process's memory address
820 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
821 have been read.
822*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100823int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200824umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100825 union {
826 long a;
827 char c[sizeof(long)];
828 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800829 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100830 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100831
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100832 while (offset < len) {
833 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
834 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200835 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100836 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100837 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100838 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100839 return 0;
840 }
841 }
842 offset += sizeof(long);
843 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100844 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100845 return 0;
846}