trace.c revision b4f9e0c0413c03ad897d7aad320a4a1cd49c467a
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <sys/wait.h>
8#include "ptrace.h"
9#include <asm/unistd.h>
10#include <assert.h>
11
12#include "common.h"
13
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 */
40
41#ifdef ARCH_HAVE_UMOVELONG
42extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
43int
44umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
45	return arch_umovelong (proc, addr, result, info);
46}
47#else
48/* Read a single long from the process's memory address 'addr' */
49int
50umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
51	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;
59	if (info) {
60		switch(info->type) {
61			case ARGTYPE_INT:
62				*result &= 0x00000000ffffffffUL;
63			default:
64				break;
65		};
66	}
67	return 0;
68}
69#endif
70
71void
72trace_me(void) {
73	debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
74	if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
75		perror("PTRACE_TRACEME");
76		exit(1);
77	}
78}
79
80/* There's a (hopefully) brief period of time after the child process
81 * exec's when we can't trace it yet.  Here we wait for kernel to
82 * prepare the process.  */
83void
84wait_for_proc(pid_t pid)
85{
86	size_t i;
87	for (i = 0; i < 100; ++i) {
88		/* We read from memory address 0, but that shouldn't
89		 * be a problem: the reading will just fail.  We are
90		 * looking for a particular reason of failure.  */
91		if (ptrace(PTRACE_PEEKTEXT, pid, 0, 0) != -1
92		    || errno != ESRCH)
93			return;
94
95		usleep(1000);
96	}
97
98	fprintf(stderr, "\
99I consistently fail to read a word from the freshly launched process.\n\
100I'll now try to proceed with tracing, but this shouldn't be happening.\n");
101}
102
103int
104trace_pid(pid_t pid) {
105	debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
106	if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
107		return -1;
108	}
109
110	/* man ptrace: PTRACE_ATTACH attaches to the process specified
111	   in pid.  The child is sent a SIGSTOP, but will not
112	   necessarily have stopped by the completion of this call;
113	   use wait() to wait for the child to stop. */
114	if (waitpid (pid, NULL, __WALL) != pid) {
115		perror ("trace_pid: waitpid");
116		return -1;
117	}
118
119	return 0;
120}
121
122void
123trace_set_options(Process *proc, pid_t pid) {
124	if (proc->tracesysgood & 0x80)
125		return;
126
127	debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
128
129	long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
130		PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
131		PTRACE_O_TRACEEXEC;
132	if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
133	    ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
134		perror("PTRACE_SETOPTIONS");
135		return;
136	}
137	proc->tracesysgood |= 0x80;
138}
139
140void
141untrace_pid(pid_t pid) {
142	debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
143	ptrace(PTRACE_DETACH, pid, 1, 0);
144}
145
146void
147continue_after_signal(pid_t pid, int signum) {
148	debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
149	ptrace(PTRACE_SYSCALL, pid, 0, signum);
150}
151
152static enum ecb_status
153event_for_pid(Event * event, void * data)
154{
155	if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
156		return ecb_yield;
157	return ecb_cont;
158}
159
160static int
161have_events_for(pid_t pid)
162{
163	return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
164}
165
166void
167continue_process(pid_t pid)
168{
169	debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
170
171	/* Only really continue the process if there are no events in
172	   the queue for this process.  Otherwise just wait for the
173	   other events to arrive.  */
174	if (!have_events_for(pid))
175		/* We always trace syscalls to control fork(),
176		 * clone(), execve()... */
177		ptrace(PTRACE_SYSCALL, pid, 0, 0);
178	else
179		debug(DEBUG_PROCESS,
180		      "putting off the continue, events in que.");
181}
182
183/**
184 * This is used for bookkeeping related to PIDs that the event
185 * handlers work with.
186 */
187struct pid_task {
188	pid_t pid;	/* This may be 0 for tasks that exited
189			 * mid-handling.  */
190	int sigstopped : 1;
191	int got_event : 1;
192	int delivered : 1;
193	int vforked : 1;
194	int sysret : 1;
195} * pids;
196
197struct pid_set {
198	struct pid_task * tasks;
199	size_t count;
200	size_t alloc;
201};
202
203/**
204 * Breakpoint re-enablement.  When we hit a breakpoint, we must
205 * disable it, single-step, and re-enable it.  That single-step can be
206 * done only by one task in a task group, while others are stopped,
207 * otherwise the processes would race for who sees the breakpoint
208 * disabled and who doesn't.  The following is to keep track of it
209 * all.
210 */
211struct process_stopping_handler
212{
213	Event_Handler super;
214
215	/* The task that is doing the re-enablement.  */
216	Process * task_enabling_breakpoint;
217
218	/* The pointer being re-enabled.  */
219	Breakpoint * breakpoint_being_enabled;
220
221	enum {
222		/* We are waiting for everyone to land in t/T.  */
223		psh_stopping = 0,
224
225		/* We are doing the PTRACE_SINGLESTEP.  */
226		psh_singlestep,
227
228		/* We are waiting for all the SIGSTOPs to arrive so
229		 * that we can sink them.  */
230		psh_sinking,
231
232		/* This is for tracking the ugly workaround.  */
233		psh_ugly_workaround,
234	} state;
235
236	int exiting;
237
238	struct pid_set pids;
239};
240
241static struct pid_task *
242get_task_info(struct pid_set * pids, pid_t pid)
243{
244	assert(pid != 0);
245	size_t i;
246	for (i = 0; i < pids->count; ++i)
247		if (pids->tasks[i].pid == pid)
248			return &pids->tasks[i];
249
250	return NULL;
251}
252
253static struct pid_task *
254add_task_info(struct pid_set * pids, pid_t pid)
255{
256	if (pids->count == pids->alloc) {
257		size_t ns = (2 * pids->alloc) ?: 4;
258		struct pid_task * n = realloc(pids->tasks,
259					      sizeof(*pids->tasks) * ns);
260		if (n == NULL)
261			return NULL;
262		pids->tasks = n;
263		pids->alloc = ns;
264	}
265	struct pid_task * task_info = &pids->tasks[pids->count++];
266	memset(task_info, 0, sizeof(*task_info));
267	task_info->pid = pid;
268	return task_info;
269}
270
271static enum pcb_status
272task_stopped(Process * task, void * data)
273{
274	enum process_status st = process_status(task->pid);
275	if (data != NULL)
276		*(enum process_status *)data = st;
277
278	/* If the task is already stopped, don't worry about it.
279	 * Likewise if it managed to become a zombie or terminate in
280	 * the meantime.  This can happen when the whole thread group
281	 * is terminating.  */
282	switch (st) {
283	case ps_invalid:
284	case ps_tracing_stop:
285	case ps_zombie:
286	case ps_sleeping:
287		return pcb_cont;
288	case ps_stop:
289	case ps_other:
290		return pcb_stop;
291	}
292
293	abort ();
294}
295
296/* Task is blocked if it's stopped, or if it's a vfork parent.  */
297static enum pcb_status
298task_blocked(Process * task, void * data)
299{
300	struct pid_set * pids = data;
301	struct pid_task * task_info = get_task_info(pids, task->pid);
302	if (task_info != NULL
303	    && task_info->vforked)
304		return pcb_cont;
305
306	return task_stopped(task, NULL);
307}
308
309static Event * process_vfork_on_event(Event_Handler * super, Event * event);
310
311static enum pcb_status
312task_vforked(Process * task, void * data)
313{
314	if (task->event_handler != NULL
315	    && task->event_handler->on_event == &process_vfork_on_event)
316		return pcb_stop;
317	return pcb_cont;
318}
319
320static int
321is_vfork_parent(Process * task)
322{
323	return each_task(task->leader, &task_vforked, NULL) != NULL;
324}
325
326static enum pcb_status
327send_sigstop(Process * task, void * data)
328{
329	Process * leader = task->leader;
330	struct pid_set * pids = data;
331
332	/* Look for pre-existing task record, or add new.  */
333	struct pid_task * task_info = get_task_info(pids, task->pid);
334	if (task_info == NULL)
335		task_info = add_task_info(pids, task->pid);
336	if (task_info == NULL) {
337		perror("send_sigstop: add_task_info");
338		destroy_event_handler(leader);
339		/* Signal failure upwards.  */
340		return pcb_stop;
341	}
342
343	/* This task still has not been attached to.  It should be
344	   stopped by the kernel.  */
345	if (task->state == STATE_BEING_CREATED)
346		return pcb_cont;
347
348	/* Don't bother sending SIGSTOP if we are already stopped, or
349	 * if we sent the SIGSTOP already, which happens when we are
350	 * handling "onexit" and inherited the handler from breakpoint
351	 * re-enablement.  */
352	enum process_status st;
353	if (task_stopped(task, &st) == pcb_cont)
354		return pcb_cont;
355	if (task_info->sigstopped) {
356		if (!task_info->delivered)
357			return pcb_cont;
358		task_info->delivered = 0;
359	}
360
361	/* Also don't attempt to stop the process if it's a parent of
362	 * vforked process.  We set up event handler specially to hint
363	 * us.  In that case parent is in D state, which we use to
364	 * weed out unnecessary looping.  */
365	if (st == ps_sleeping
366	    && is_vfork_parent (task)) {
367		task_info->vforked = 1;
368		return pcb_cont;
369	}
370
371	if (task_kill(task->pid, SIGSTOP) >= 0) {
372		debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
373		task_info->sigstopped = 1;
374	} else
375		fprintf(stderr,
376			"Warning: couldn't send SIGSTOP to %d\n", task->pid);
377
378	return pcb_cont;
379}
380
381/* On certain kernels, detaching right after a singlestep causes the
382   tracee to be killed with a SIGTRAP (that even though the singlestep
383   was properly caught by waitpid.  The ugly workaround is to put a
384   breakpoint where IP points and let the process continue.  After
385   this the breakpoint can be retracted and the process detached.  */
386static void
387ugly_workaround(Process * proc)
388{
389	void * ip = get_instruction_pointer(proc);
390	Breakpoint * sbp = dict_find_entry(proc->leader->breakpoints, ip);
391	if (sbp != NULL)
392		enable_breakpoint(proc, sbp);
393	else
394		insert_breakpoint(proc, ip, NULL, 1);
395	ptrace(PTRACE_CONT, proc->pid, 0, 0);
396}
397
398static void
399process_stopping_done(struct process_stopping_handler * self, Process * leader)
400{
401	debug(DEBUG_PROCESS, "process stopping done %d",
402	      self->task_enabling_breakpoint->pid);
403	size_t i;
404	if (!self->exiting) {
405		for (i = 0; i < self->pids.count; ++i)
406			if (self->pids.tasks[i].pid != 0
407			    && (self->pids.tasks[i].delivered
408				|| self->pids.tasks[i].sysret))
409				continue_process(self->pids.tasks[i].pid);
410		continue_process(self->task_enabling_breakpoint->pid);
411		destroy_event_handler(leader);
412	} else {
413		self->state = psh_ugly_workaround;
414		ugly_workaround(self->task_enabling_breakpoint);
415	}
416}
417
418/* Before we detach, we need to make sure that task's IP is on the
419 * edge of an instruction.  So for tasks that have a breakpoint event
420 * in the queue, we adjust the instruction pointer, just like
421 * continue_after_breakpoint does.  */
422static enum ecb_status
423undo_breakpoint(Event * event, void * data)
424{
425	if (event != NULL
426	    && event->proc->leader == data
427	    && event->type == EVENT_BREAKPOINT)
428		set_instruction_pointer(event->proc, event->e_un.brk_addr);
429	return ecb_cont;
430}
431
432static enum pcb_status
433untrace_task(Process * task, void * data)
434{
435	if (task != data)
436		untrace_pid(task->pid);
437	return pcb_cont;
438}
439
440static enum pcb_status
441remove_task(Process * task, void * data)
442{
443	/* Don't untrace leader just yet.  */
444	if (task != data)
445		remove_process(task);
446	return pcb_cont;
447}
448
449static void
450detach_process(Process * leader)
451{
452	each_qd_event(&undo_breakpoint, leader);
453	disable_all_breakpoints(leader);
454
455	/* Now untrace the process, if it was attached to by -p.  */
456	struct opt_p_t * it;
457	for (it = opt_p; it != NULL; it = it->next) {
458		Process * proc = pid2proc(it->pid);
459		if (proc == NULL)
460			continue;
461		if (proc->leader == leader) {
462			each_task(leader, &untrace_task, NULL);
463			break;
464		}
465	}
466	each_task(leader, &remove_task, leader);
467	destroy_event_handler(leader);
468	remove_task(leader, NULL);
469}
470
471static void
472handle_stopping_event(struct pid_task * task_info, Event ** eventp)
473{
474	/* Mark all events, so that we know whom to SIGCONT later.  */
475	if (task_info != NULL)
476		task_info->got_event = 1;
477
478	Event * event = *eventp;
479
480	/* In every state, sink SIGSTOP events for tasks that it was
481	 * sent to.  */
482	if (task_info != NULL
483	    && event->type == EVENT_SIGNAL
484	    && event->e_un.signum == SIGSTOP) {
485		debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
486		if (task_info->sigstopped
487		    && !task_info->delivered) {
488			task_info->delivered = 1;
489			*eventp = NULL; // sink the event
490		} else
491			fprintf(stderr, "suspicious: %d got SIGSTOP, but "
492				"sigstopped=%d and delivered=%d\n",
493				task_info->pid, task_info->sigstopped,
494				task_info->delivered);
495	}
496}
497
498/* Some SIGSTOPs may have not been delivered to their respective tasks
499 * yet.  They are still in the queue.  If we have seen an event for
500 * that process, continue it, so that the SIGSTOP can be delivered and
501 * caught by ltrace.  We don't mind that the process is after
502 * breakpoint (and therefore potentially doesn't have aligned IP),
503 * because the signal will be delivered without the process actually
504 * starting.  */
505static void
506continue_for_sigstop_delivery(struct pid_set * pids)
507{
508	size_t i;
509	for (i = 0; i < pids->count; ++i) {
510		if (pids->tasks[i].pid != 0
511		    && pids->tasks[i].sigstopped
512		    && !pids->tasks[i].delivered
513		    && pids->tasks[i].got_event) {
514			debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
515			      pids->tasks[i].pid);
516			ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
517		}
518	}
519}
520
521static int
522event_exit_p(Event * event)
523{
524	return event != NULL && (event->type == EVENT_EXIT
525				 || event->type == EVENT_EXIT_SIGNAL);
526}
527
528static int
529event_exit_or_none_p(Event * event)
530{
531	return event == NULL || event_exit_p(event)
532		|| event->type == EVENT_NONE;
533}
534
535static int
536await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info,
537		       Event * event)
538{
539	/* If we still didn't get our SIGSTOP, continue the process
540	 * and carry on.  */
541	if (event != NULL && !event_exit_or_none_p(event)
542	    && task_info != NULL && task_info->sigstopped) {
543		debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
544		      task_info->pid);
545		/* We should get the signal the first thing
546		 * after this, so it should be OK to continue
547		 * even if we are over a breakpoint.  */
548		ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
549
550	} else {
551		/* If all SIGSTOPs were delivered, uninstall the
552		 * handler and continue everyone.  */
553		/* XXX I suspect that we should check tasks that are
554		 * still around.  Is things are now, there should be a
555		 * race between waiting for everyone to stop and one
556		 * of the tasks exiting.  */
557		int all_clear = 1;
558		size_t i;
559		for (i = 0; i < pids->count; ++i)
560			if (pids->tasks[i].pid != 0
561			    && pids->tasks[i].sigstopped
562			    && !pids->tasks[i].delivered) {
563				all_clear = 0;
564				break;
565			}
566		return all_clear;
567	}
568
569	return 0;
570}
571
572static int
573all_stops_accountable(struct pid_set * pids)
574{
575	size_t i;
576	for (i = 0; i < pids->count; ++i)
577		if (pids->tasks[i].pid != 0
578		    && !pids->tasks[i].got_event
579		    && !have_events_for(pids->tasks[i].pid))
580			return 0;
581	return 1;
582}
583
584static void
585singlestep(Process * proc)
586{
587	debug(1, "PTRACE_SINGLESTEP");
588	if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0))
589		perror("PTRACE_SINGLESTEP");
590}
591
592/* This event handler is installed when we are in the process of
593 * stopping the whole thread group to do the pointer re-enablement for
594 * one of the threads.  We pump all events to the queue for later
595 * processing while we wait for all the threads to stop.  When this
596 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
597 * re-enable, and continue everyone.  */
598static Event *
599process_stopping_on_event(Event_Handler * super, Event * event)
600{
601	struct process_stopping_handler * self = (void *)super;
602	Process * task = event->proc;
603	Process * leader = task->leader;
604	Breakpoint * sbp = self->breakpoint_being_enabled;
605	Process * teb = self->task_enabling_breakpoint;
606
607	debug(DEBUG_PROCESS,
608	      "pid %d; event type %d; state %d",
609	      task->pid, event->type, self->state);
610
611	struct pid_task * task_info = get_task_info(&self->pids, task->pid);
612	if (task_info == NULL)
613		fprintf(stderr, "new task??? %d\n", task->pid);
614	handle_stopping_event(task_info, &event);
615
616	int state = self->state;
617	int event_to_queue = !event_exit_or_none_p(event);
618
619	/* Deactivate the entry if the task exits.  */
620	if (event_exit_p(event) && task_info != NULL)
621		task_info->pid = 0;
622
623	/* Always handle sysrets.  Whether sysret occurred and what
624	 * sys it rets from may need to be determined based on process
625	 * stack, so we need to keep that in sync with reality.  Note
626	 * that we don't continue the process after the sysret is
627	 * handled.  See continue_after_syscall.  */
628	if (event != NULL && event->type == EVENT_SYSRET) {
629		debug(1, "%d LT_EV_SYSRET", event->proc->pid);
630		event_to_queue = 0;
631		task_info->sysret = 1;
632	}
633
634	switch (state) {
635	case psh_stopping:
636		/* If everyone is stopped, singlestep.  */
637		if (each_task(leader, &task_blocked, &self->pids) == NULL) {
638			debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d",
639			      teb->pid);
640			if (sbp->enabled)
641				disable_breakpoint(teb, sbp);
642			singlestep(teb);
643			self->state = state = psh_singlestep;
644		}
645		break;
646
647	case psh_singlestep:
648		/* In singlestep state, breakpoint signifies that we
649		 * have now stepped, and can re-enable the breakpoint.  */
650		if (event != NULL && task == teb) {
651
652			/* This is not the singlestep that we are waiting for.  */
653			if (event->type == EVENT_SIGNAL) {
654				singlestep(task);
655				break;
656			}
657
658			/* Essentially we don't care what event caused
659			 * the thread to stop.  We can do the
660			 * re-enablement now.  */
661			if (sbp->enabled)
662				enable_breakpoint(teb, sbp);
663
664			continue_for_sigstop_delivery(&self->pids);
665
666			self->breakpoint_being_enabled = NULL;
667			self->state = state = psh_sinking;
668
669			if (event->type == EVENT_BREAKPOINT)
670				event = NULL; // handled
671		} else
672			break;
673
674		/* fall-through */
675
676	case psh_sinking:
677		if (await_sigstop_delivery(&self->pids, task_info, event))
678			process_stopping_done(self, leader);
679		break;
680
681	case psh_ugly_workaround:
682		if (event == NULL)
683			break;
684		if (event->type == EVENT_BREAKPOINT) {
685			undo_breakpoint(event, leader);
686			if (task == teb)
687				self->task_enabling_breakpoint = NULL;
688		}
689		if (self->task_enabling_breakpoint == NULL
690		    && all_stops_accountable(&self->pids)) {
691			undo_breakpoint(event, leader);
692			detach_process(leader);
693			event = NULL; // handled
694		}
695	}
696
697	if (event != NULL && event_to_queue) {
698		enque_event(event);
699		event = NULL; // sink the event
700	}
701
702	return event;
703}
704
705static void
706process_stopping_destroy(Event_Handler * super)
707{
708	struct process_stopping_handler * self = (void *)super;
709	free(self->pids.tasks);
710}
711
712void
713continue_after_breakpoint(Process *proc, Breakpoint *sbp)
714{
715	set_instruction_pointer(proc, sbp->addr);
716	if (sbp->enabled == 0) {
717		continue_process(proc->pid);
718	} else {
719		debug(DEBUG_PROCESS,
720		      "continue_after_breakpoint: pid=%d, addr=%p",
721		      proc->pid, sbp->addr);
722#if defined __sparc__  || defined __ia64___ || defined __mips__
723		/* we don't want to singlestep here */
724		continue_process(proc->pid);
725#else
726		struct process_stopping_handler * handler
727			= calloc(sizeof(*handler), 1);
728		if (handler == NULL) {
729			perror("malloc breakpoint disable handler");
730		fatal:
731			/* Carry on not bothering to re-enable.  */
732			continue_process(proc->pid);
733			return;
734		}
735
736		handler->super.on_event = process_stopping_on_event;
737		handler->super.destroy = process_stopping_destroy;
738		handler->task_enabling_breakpoint = proc;
739		handler->breakpoint_being_enabled = sbp;
740		install_event_handler(proc->leader, &handler->super);
741
742		if (each_task(proc->leader, &send_sigstop,
743			      &handler->pids) != NULL)
744			goto fatal;
745
746		/* And deliver the first fake event, in case all the
747		 * conditions are already fulfilled.  */
748		Event ev;
749		ev.type = EVENT_NONE;
750		ev.proc = proc;
751		process_stopping_on_event(&handler->super, &ev);
752#endif
753	}
754}
755
756/**
757 * Ltrace exit.  When we are about to exit, we have to go through all
758 * the processes, stop them all, remove all the breakpoints, and then
759 * detach the processes that we attached to using -p.  If we left the
760 * other tasks running, they might hit stray return breakpoints and
761 * produce artifacts, so we better stop everyone, even if it's a bit
762 * of extra work.
763 */
764struct ltrace_exiting_handler
765{
766	Event_Handler super;
767	struct pid_set pids;
768};
769
770static Event *
771ltrace_exiting_on_event(Event_Handler * super, Event * event)
772{
773	struct ltrace_exiting_handler * self = (void *)super;
774	Process * task = event->proc;
775	Process * leader = task->leader;
776
777	debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type);
778
779	struct pid_task * task_info = get_task_info(&self->pids, task->pid);
780	handle_stopping_event(task_info, &event);
781
782	if (event != NULL && event->type == EVENT_BREAKPOINT)
783		undo_breakpoint(event, leader);
784
785	if (await_sigstop_delivery(&self->pids, task_info, event)
786	    && all_stops_accountable(&self->pids))
787		detach_process(leader);
788
789	/* Sink all non-exit events.  We are about to exit, so we
790	 * don't bother with queuing them. */
791	if (event_exit_or_none_p(event))
792		return event;
793
794	return NULL;
795}
796
797static void
798ltrace_exiting_destroy(Event_Handler * super)
799{
800	struct ltrace_exiting_handler * self = (void *)super;
801	free(self->pids.tasks);
802}
803
804static int
805ltrace_exiting_install_handler(Process * proc)
806{
807	/* Only install to leader.  */
808	if (proc->leader != proc)
809		return 0;
810
811	/* Perhaps we are already installed, if the user passed
812	 * several -p options that are tasks of one process.  */
813	if (proc->event_handler != NULL
814	    && proc->event_handler->on_event == &ltrace_exiting_on_event)
815		return 0;
816
817	/* If stopping handler is already present, let it do the
818	 * work.  */
819	if (proc->event_handler != NULL) {
820		assert(proc->event_handler->on_event
821		       == &process_stopping_on_event);
822		struct process_stopping_handler * other
823			= (void *)proc->event_handler;
824		other->exiting = 1;
825		return 0;
826	}
827
828	struct ltrace_exiting_handler * handler
829		= calloc(sizeof(*handler), 1);
830	if (handler == NULL) {
831		perror("malloc exiting handler");
832	fatal:
833		/* XXXXXXXXXXXXXXXXXXX fixme */
834		return -1;
835	}
836
837	handler->super.on_event = ltrace_exiting_on_event;
838	handler->super.destroy = ltrace_exiting_destroy;
839	install_event_handler(proc->leader, &handler->super);
840
841	if (each_task(proc->leader, &send_sigstop,
842		      &handler->pids) != NULL)
843		goto fatal;
844
845	return 0;
846}
847
848/*
849 * When the traced process vforks, it's suspended until the child
850 * process calls _exit or exec*.  In the meantime, the two share the
851 * address space.
852 *
853 * The child process should only ever call _exit or exec*, but we
854 * can't count on that (it's not the role of ltrace to policy, but to
855 * observe).  In any case, we will _at least_ have to deal with
856 * removal of vfork return breakpoint (which we have to smuggle back
857 * in, so that the parent can see it, too), and introduction of exec*
858 * return breakpoint.  Since we already have both breakpoint actions
859 * to deal with, we might as well support it all.
860 *
861 * The gist is that we pretend that the child is in a thread group
862 * with its parent, and handle it as a multi-threaded case, with the
863 * exception that we know that the parent is blocked, and don't
864 * attempt to stop it.  When the child execs, we undo the setup.
865 */
866
867struct process_vfork_handler
868{
869	Event_Handler super;
870	void * bp_addr;
871};
872
873static Event *
874process_vfork_on_event(Event_Handler * super, Event * event)
875{
876	struct process_vfork_handler * self = (void *)super;
877	Breakpoint * sbp;
878	assert(self != NULL);
879
880	switch (event->type) {
881	case EVENT_BREAKPOINT:
882		/* Remember the vfork return breakpoint.  */
883		if (self->bp_addr == NULL)
884			self->bp_addr = event->e_un.brk_addr;
885		break;
886
887	case EVENT_EXIT:
888	case EVENT_EXIT_SIGNAL:
889	case EVENT_EXEC:
890		/* Smuggle back in the vfork return breakpoint, so
891		 * that our parent can trip over it once again.  */
892		if (self->bp_addr != NULL) {
893			sbp = dict_find_entry(event->proc->leader->breakpoints,
894					      self->bp_addr);
895			if (sbp != NULL)
896				insert_breakpoint(event->proc->parent,
897						  self->bp_addr,
898						  sbp->libsym, 1);
899		}
900
901		continue_process(event->proc->parent->pid);
902
903		/* Remove the leader that we artificially set up
904		 * earlier.  */
905		change_process_leader(event->proc, event->proc);
906		destroy_event_handler(event->proc);
907
908	default:
909		;
910	}
911
912	return event;
913}
914
915void
916continue_after_vfork(Process * proc)
917{
918	debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
919	struct process_vfork_handler * handler = calloc(sizeof(*handler), 1);
920	if (handler == NULL) {
921		perror("malloc vfork handler");
922		/* Carry on not bothering to treat the process as
923		 * necessary.  */
924		continue_process(proc->parent->pid);
925		return;
926	}
927
928	/* We must set up custom event handler, so that we see
929	 * exec/exit events for the task itself.  */
930	handler->super.on_event = process_vfork_on_event;
931	install_event_handler(proc, &handler->super);
932
933	/* Make sure that the child is sole thread.  */
934	assert(proc->leader == proc);
935	assert(proc->next == NULL || proc->next->leader != proc);
936
937	/* Make sure that the child's parent is properly set up.  */
938	assert(proc->parent != NULL);
939	assert(proc->parent->leader != NULL);
940
941	change_process_leader(proc, proc->parent->leader);
942}
943
944static int
945is_mid_stopping(Process *proc)
946{
947	return proc != NULL
948		&& proc->event_handler != NULL
949		&& proc->event_handler->on_event == &process_stopping_on_event;
950}
951
952void
953continue_after_syscall(Process * proc, int sysnum, int ret_p)
954{
955	/* Don't continue if we are mid-stopping.  */
956	if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
957		debug(DEBUG_PROCESS,
958		      "continue_after_syscall: don't continue %d",
959		      proc->pid);
960		return;
961	}
962	continue_process(proc->pid);
963}
964
965/* If ltrace gets SIGINT, the processes directly or indirectly run by
966 * ltrace get it too.  We just have to wait long enough for the signal
967 * to be delivered and the process terminated, which we notice and
968 * exit ltrace, too.  So there's not much we need to do there.  We
969 * want to keep tracing those processes as usual, in case they just
970 * SIG_IGN the SIGINT to do their shutdown etc.
971 *
972 * For processes ran on the background, we want to install an exit
973 * handler that stops all the threads, removes all breakpoints, and
974 * detaches.
975 */
976void
977ltrace_exiting(void)
978{
979	struct opt_p_t * it;
980	for (it = opt_p; it != NULL; it = it->next) {
981		Process * proc = pid2proc(it->pid);
982		if (proc == NULL || proc->leader == NULL)
983			continue;
984		if (ltrace_exiting_install_handler(proc->leader) < 0)
985			fprintf(stderr,
986				"Couldn't install exiting handler for %d.\n",
987				proc->pid);
988	}
989}
990
991size_t
992umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
993
994	union {
995		long a;
996		char c[sizeof(long)];
997	} a;
998	int started = 0;
999	size_t offset = 0, bytes_read = 0;
1000
1001	while (offset < len) {
1002		a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1003		if (a.a == -1 && errno) {
1004			if (started && errno == EIO)
1005				return bytes_read;
1006			else
1007				return -1;
1008		}
1009		started = 1;
1010
1011		if (len - offset >= sizeof(long)) {
1012			memcpy(laddr + offset, &a.c[0], sizeof(long));
1013			bytes_read += sizeof(long);
1014		}
1015		else {
1016			memcpy(laddr + offset, &a.c[0], len - offset);
1017			bytes_read += (len - offset);
1018		}
1019		offset += sizeof(long);
1020	}
1021
1022	return bytes_read;
1023}
1024
1025/* Read a series of bytes starting at the process's memory address
1026   'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
1027   have been read.
1028*/
1029int
1030umovestr(Process *proc, void *addr, int len, void *laddr) {
1031	union {
1032		long a;
1033		char c[sizeof(long)];
1034	} a;
1035	unsigned i;
1036	int offset = 0;
1037
1038	while (offset < len) {
1039		a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1040		for (i = 0; i < sizeof(long); i++) {
1041			if (a.c[i] && offset + (signed)i < len) {
1042				*(char *)(laddr + offset + i) = a.c[i];
1043			} else {
1044				*(char *)(laddr + offset + i) = '\0';
1045				return 0;
1046			}
1047		}
1048		offset += sizeof(long);
1049	}
1050	*(char *)(laddr + offset) = '\0';
1051	return 0;
1052}
1053