trace.c revision 6dfc544b6aa6703d2292be34470aebf874d78452
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2007,2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2002,2003,2004,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#include "config.h"
25
26#include <asm/unistd.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <assert.h>
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#ifdef HAVE_LIBSELINUX
37# include <selinux/selinux.h>
38#endif
39
40#include "linux-gnu/trace.h"
41#include "linux-gnu/trace-defs.h"
42#include "backend.h"
43#include "breakpoint.h"
44#include "debug.h"
45#include "events.h"
46#include "options.h"
47#include "proc.h"
48#include "ptrace.h"
49#include "type.h"
50
51void
52trace_fail_warning(pid_t pid)
53{
54	/* This was adapted from GDB.  */
55#ifdef HAVE_LIBSELINUX
56	static int checked = 0;
57	if (checked)
58		return;
59	checked = 1;
60
61	/* -1 is returned for errors, 0 if it has no effect, 1 if
62	 * PTRACE_ATTACH is forbidden.  */
63	if (security_get_boolean_active("deny_ptrace") == 1)
64		fprintf(stderr,
65"The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n"
66"tracing other processes.  You can disable this process attach protection by\n"
67"issuing 'setsebool deny_ptrace=0' in the superuser context.\n");
68#endif /* HAVE_LIBSELINUX */
69}
70
71void
72trace_me(void)
73{
74	debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
75	if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
76		perror("PTRACE_TRACEME");
77		trace_fail_warning(getpid());
78		exit(1);
79	}
80}
81
82/* There's a (hopefully) brief period of time after the child process
83 * forks when we can't trace it yet.  Here we wait for kernel to
84 * prepare the process.  */
85int
86wait_for_proc(pid_t pid)
87{
88	/* man ptrace: PTRACE_ATTACH attaches to the process specified
89	   in pid.  The child is sent a SIGSTOP, but will not
90	   necessarily have stopped by the completion of this call;
91	   use wait() to wait for the child to stop. */
92	if (waitpid(pid, NULL, __WALL) != pid) {
93		perror ("trace_pid: waitpid");
94		return -1;
95	}
96
97	return 0;
98}
99
100int
101trace_pid(pid_t pid)
102{
103	debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
104	/* This shouldn't emit error messages, as there are legitimate
105	 * reasons that the PID can't be attached: like it may have
106	 * already ended.  */
107	if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
108		return -1;
109
110	return wait_for_proc(pid);
111}
112
113void
114trace_set_options(struct process *proc)
115{
116	if (proc->tracesysgood & 0x80)
117		return;
118
119	pid_t pid = proc->pid;
120	debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
121
122	long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
123		PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
124		PTRACE_O_TRACEEXEC;
125	if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 &&
126	    ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) {
127		perror("PTRACE_SETOPTIONS");
128		return;
129	}
130	proc->tracesysgood |= 0x80;
131}
132
133void
134untrace_pid(pid_t pid) {
135	debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
136	ptrace(PTRACE_DETACH, pid, 0, 0);
137}
138
139void
140continue_after_signal(pid_t pid, int signum)
141{
142	debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d",
143	      pid, signum);
144	ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum);
145}
146
147static enum ecb_status
148event_for_pid(Event *event, void *data)
149{
150	if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data)
151		return ecb_yield;
152	return ecb_cont;
153}
154
155static int
156have_events_for(pid_t pid)
157{
158	return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL;
159}
160
161void
162continue_process(pid_t pid)
163{
164	debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
165
166	/* Only really continue the process if there are no events in
167	   the queue for this process.  Otherwise just wait for the
168	   other events to arrive.  */
169	if (!have_events_for(pid))
170		/* We always trace syscalls to control fork(),
171		 * clone(), execve()... */
172		ptrace(PTRACE_SYSCALL, pid, 0, 0);
173	else
174		debug(DEBUG_PROCESS,
175		      "putting off the continue, events in que.");
176}
177
178static struct pid_task *
179get_task_info(struct pid_set *pids, pid_t pid)
180{
181	assert(pid != 0);
182	size_t i;
183	for (i = 0; i < pids->count; ++i)
184		if (pids->tasks[i].pid == pid)
185			return &pids->tasks[i];
186
187	return NULL;
188}
189
190static struct pid_task *
191add_task_info(struct pid_set *pids, pid_t pid)
192{
193	if (pids->count == pids->alloc) {
194		size_t ns = (2 * pids->alloc) ?: 4;
195		struct pid_task *n = realloc(pids->tasks,
196					     sizeof(*pids->tasks) * ns);
197		if (n == NULL)
198			return NULL;
199		pids->tasks = n;
200		pids->alloc = ns;
201	}
202	struct pid_task * task_info = &pids->tasks[pids->count++];
203	memset(task_info, 0, sizeof(*task_info));
204	task_info->pid = pid;
205	return task_info;
206}
207
208static enum callback_status
209task_stopped(struct process *task, void *data)
210{
211	enum process_status st = process_status(task->pid);
212	if (data != NULL)
213		*(enum process_status *)data = st;
214
215	/* If the task is already stopped, don't worry about it.
216	 * Likewise if it managed to become a zombie or terminate in
217	 * the meantime.  This can happen when the whole thread group
218	 * is terminating.  */
219	switch (st) {
220	case PS_INVALID:
221	case PS_TRACING_STOP:
222	case PS_ZOMBIE:
223		return CBS_CONT;
224	case PS_SLEEPING:
225	case PS_STOP:
226	case PS_OTHER:
227		return CBS_STOP;
228	}
229
230	abort ();
231}
232
233/* Task is blocked if it's stopped, or if it's a vfork parent.  */
234static enum callback_status
235task_blocked(struct process *task, void *data)
236{
237	struct pid_set *pids = data;
238	struct pid_task *task_info = get_task_info(pids, task->pid);
239	if (task_info != NULL
240	    && task_info->vforked)
241		return CBS_CONT;
242
243	return task_stopped(task, NULL);
244}
245
246static Event *process_vfork_on_event(struct event_handler *super, Event *event);
247
248static enum callback_status
249task_vforked(struct process *task, void *data)
250{
251	if (task->event_handler != NULL
252	    && task->event_handler->on_event == &process_vfork_on_event)
253		return CBS_STOP;
254	return CBS_CONT;
255}
256
257static int
258is_vfork_parent(struct process *task)
259{
260	return each_task(task->leader, NULL, &task_vforked, NULL) != NULL;
261}
262
263static enum callback_status
264send_sigstop(struct process *task, void *data)
265{
266	struct 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 CBS_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 CBS_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 are
287	 * handling "onexit" and inherited the handler from breakpoint
288	 * re-enablement.  */
289	enum process_status st;
290	if (task_stopped(task, &st) == CBS_CONT)
291		return CBS_CONT;
292	if (task_info->sigstopped) {
293		if (!task_info->delivered)
294			return CBS_CONT;
295		task_info->delivered = 0;
296	}
297
298	/* Also don't attempt to stop the process if it's a parent of
299	 * vforked process.  We set up event handler specially to hint
300	 * us.  In that case parent is in D state, which we use to
301	 * weed out unnecessary looping.  */
302	if (st == PS_SLEEPING
303	    && is_vfork_parent(task)) {
304		task_info->vforked = 1;
305		return CBS_CONT;
306	}
307
308	if (task_kill(task->pid, SIGSTOP) >= 0) {
309		debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid);
310		task_info->sigstopped = 1;
311	} else
312		fprintf(stderr,
313			"Warning: couldn't send SIGSTOP to %d\n", task->pid);
314
315	return CBS_CONT;
316}
317
318/* On certain kernels, detaching right after a singlestep causes the
319   tracee to be killed with a SIGTRAP (that even though the singlestep
320   was properly caught by waitpid.  The ugly workaround is to put a
321   breakpoint where IP points and let the process continue.  After
322   this the breakpoint can be retracted and the process detached.  */
323static void
324ugly_workaround(struct process *proc)
325{
326	void *ip = get_instruction_pointer(proc);
327	struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip);
328	if (sbp != NULL)
329		enable_breakpoint(proc, sbp);
330	else
331		insert_breakpoint(proc, ip, NULL);
332	ptrace(PTRACE_CONT, proc->pid, 0, 0);
333}
334
335static void
336process_stopping_done(struct process_stopping_handler *self,
337		      struct process *leader)
338{
339	debug(DEBUG_PROCESS, "process stopping done %d",
340	      self->task_enabling_breakpoint->pid);
341
342	if (!self->exiting) {
343		size_t i;
344		for (i = 0; i < self->pids.count; ++i)
345			if (self->pids.tasks[i].pid != 0
346			    && (self->pids.tasks[i].delivered
347				|| self->pids.tasks[i].sysret))
348				continue_process(self->pids.tasks[i].pid);
349		continue_process(self->task_enabling_breakpoint->pid);
350	}
351
352	if (self->exiting) {
353	ugly_workaround:
354		self->state = psh_ugly_workaround;
355		ugly_workaround(self->task_enabling_breakpoint);
356	} else {
357		switch ((self->ugly_workaround_p)(self)) {
358		case CBS_FAIL:
359			/* xxx handle me */
360		case CBS_STOP:
361			break;
362		case CBS_CONT:
363			goto ugly_workaround;
364		}
365		destroy_event_handler(leader);
366	}
367}
368
369/* Before we detach, we need to make sure that task's IP is on the
370 * edge of an instruction.  So for tasks that have a breakpoint event
371 * in the queue, we adjust the instruction pointer, just like
372 * continue_after_breakpoint does.  */
373static enum ecb_status
374undo_breakpoint(Event *event, void *data)
375{
376	if (event != NULL
377	    && event->proc->leader == data
378	    && event->type == EVENT_BREAKPOINT)
379		set_instruction_pointer(event->proc, event->e_un.brk_addr);
380	return ecb_cont;
381}
382
383static enum callback_status
384untrace_task(struct process *task, void *data)
385{
386	if (task != data)
387		untrace_pid(task->pid);
388	return CBS_CONT;
389}
390
391static enum callback_status
392remove_task(struct process *task, void *data)
393{
394	/* Don't untrace leader just yet.  */
395	if (task != data)
396		remove_process(task);
397	return CBS_CONT;
398}
399
400static enum callback_status
401retract_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
402{
403	breakpoint_on_retract(bp, proc);
404	return CBS_CONT;
405}
406
407static void
408detach_process(struct process *leader)
409{
410	each_qd_event(&undo_breakpoint, leader);
411	disable_all_breakpoints(leader);
412	proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL);
413
414	/* Now untrace the process, if it was attached to by -p.  */
415	struct opt_p_t *it;
416	for (it = opt_p; it != NULL; it = it->next) {
417		struct process *proc = pid2proc(it->pid);
418		if (proc == NULL)
419			continue;
420		if (proc->leader == leader) {
421			each_task(leader, NULL, &untrace_task, NULL);
422			break;
423		}
424	}
425	each_task(leader, NULL, &remove_task, leader);
426	destroy_event_handler(leader);
427	remove_task(leader, NULL);
428}
429
430static void
431handle_stopping_event(struct pid_task *task_info, Event **eventp)
432{
433	/* Mark all events, so that we know whom to SIGCONT later.  */
434	if (task_info != NULL)
435		task_info->got_event = 1;
436
437	Event *event = *eventp;
438
439	/* In every state, sink SIGSTOP events for tasks that it was
440	 * sent to.  */
441	if (task_info != NULL
442	    && event->type == EVENT_SIGNAL
443	    && event->e_un.signum == SIGSTOP) {
444		debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid);
445		if (task_info->sigstopped
446		    && !task_info->delivered) {
447			task_info->delivered = 1;
448			*eventp = NULL; // sink the event
449		} else
450			fprintf(stderr, "suspicious: %d got SIGSTOP, but "
451				"sigstopped=%d and delivered=%d\n",
452				task_info->pid, task_info->sigstopped,
453				task_info->delivered);
454	}
455}
456
457/* Some SIGSTOPs may have not been delivered to their respective tasks
458 * yet.  They are still in the queue.  If we have seen an event for
459 * that process, continue it, so that the SIGSTOP can be delivered and
460 * caught by ltrace.  We don't mind that the process is after
461 * breakpoint (and therefore potentially doesn't have aligned IP),
462 * because the signal will be delivered without the process actually
463 * starting.  */
464static void
465continue_for_sigstop_delivery(struct pid_set *pids)
466{
467	size_t i;
468	for (i = 0; i < pids->count; ++i) {
469		if (pids->tasks[i].pid != 0
470		    && pids->tasks[i].sigstopped
471		    && !pids->tasks[i].delivered
472		    && pids->tasks[i].got_event) {
473			debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
474			      pids->tasks[i].pid);
475			ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0);
476		}
477	}
478}
479
480static int
481event_exit_p(Event *event)
482{
483	return event != NULL && (event->type == EVENT_EXIT
484				 || event->type == EVENT_EXIT_SIGNAL);
485}
486
487static int
488event_exit_or_none_p(Event *event)
489{
490	return event == NULL || event_exit_p(event)
491		|| event->type == EVENT_NONE;
492}
493
494static int
495await_sigstop_delivery(struct pid_set *pids, struct pid_task *task_info,
496		       Event *event)
497{
498	/* If we still didn't get our SIGSTOP, continue the process
499	 * and carry on.  */
500	if (event != NULL && !event_exit_or_none_p(event)
501	    && task_info != NULL && task_info->sigstopped) {
502		debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery",
503		      task_info->pid);
504		/* We should get the signal the first thing
505		 * after this, so it should be OK to continue
506		 * even if we are over a breakpoint.  */
507		ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0);
508
509	} else {
510		/* If all SIGSTOPs were delivered, uninstall the
511		 * handler and continue everyone.  */
512		/* XXX I suspect that we should check tasks that are
513		 * still around.  Is things are now, there should be a
514		 * race between waiting for everyone to stop and one
515		 * of the tasks exiting.  */
516		int all_clear = 1;
517		size_t i;
518		for (i = 0; i < pids->count; ++i)
519			if (pids->tasks[i].pid != 0
520			    && pids->tasks[i].sigstopped
521			    && !pids->tasks[i].delivered) {
522				all_clear = 0;
523				break;
524			}
525		return all_clear;
526	}
527
528	return 0;
529}
530
531static int
532all_stops_accountable(struct pid_set *pids)
533{
534	size_t i;
535	for (i = 0; i < pids->count; ++i)
536		if (pids->tasks[i].pid != 0
537		    && !pids->tasks[i].got_event
538		    && !have_events_for(pids->tasks[i].pid))
539			return 0;
540	return 1;
541}
542
543/* The protocol is: 0 for success, negative for failure, positive if
544 * default singlestep is to be used.  */
545int arch_atomic_singlestep(struct process *proc, struct breakpoint *sbp,
546			   int (*add_cb)(void *addr, void *data),
547			   void *add_cb_data);
548
549#ifndef ARCH_HAVE_ATOMIC_SINGLESTEP
550int
551arch_atomic_singlestep(struct process *proc, struct breakpoint *sbp,
552		       int (*add_cb)(void *addr, void *data),
553		       void *add_cb_data)
554{
555	return 1;
556}
557#endif
558
559static Event *process_stopping_on_event(struct event_handler *super,
560					Event *event);
561
562static void
563remove_atomic_breakpoints(struct process *proc)
564{
565	struct process_stopping_handler *self
566		= (void *)proc->leader->event_handler;
567	assert(self != NULL);
568	assert(self->super.on_event == process_stopping_on_event);
569
570	int ct = sizeof(self->atomic_skip_bp_addrs)
571		/ sizeof(*self->atomic_skip_bp_addrs);
572	int i;
573	for (i = 0; i < ct; ++i)
574		if (self->atomic_skip_bp_addrs[i] != 0) {
575			delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]);
576			self->atomic_skip_bp_addrs[i] = 0;
577		}
578}
579
580static void
581atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct process *proc)
582{
583	remove_atomic_breakpoints(proc);
584}
585
586static int
587atomic_singlestep_add_bp(void *addr, void *data)
588{
589	struct process_stopping_handler *self = data;
590	struct process *proc = self->task_enabling_breakpoint;
591
592	int ct = sizeof(self->atomic_skip_bp_addrs)
593		/ sizeof(*self->atomic_skip_bp_addrs);
594	int i;
595	for (i = 0; i < ct; ++i)
596		if (self->atomic_skip_bp_addrs[i] == 0) {
597			self->atomic_skip_bp_addrs[i] = addr;
598			static struct bp_callbacks cbs = {
599				.on_hit = atomic_singlestep_bp_on_hit,
600			};
601			struct breakpoint *bp
602				= insert_breakpoint(proc, addr, NULL);
603			breakpoint_set_callbacks(bp, &cbs);
604			return 0;
605		}
606
607	assert(!"Too many atomic singlestep breakpoints!");
608	abort();
609}
610
611static int
612singlestep(struct process_stopping_handler *self)
613{
614	struct process *proc = self->task_enabling_breakpoint;
615
616	int status = arch_atomic_singlestep(self->task_enabling_breakpoint,
617					    self->breakpoint_being_enabled,
618					    &atomic_singlestep_add_bp, self);
619
620	/* Propagate failure and success.  */
621	if (status <= 0)
622		return status;
623
624	/* Otherwise do the default action: singlestep.  */
625	debug(1, "PTRACE_SINGLESTEP");
626	if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) {
627		perror("PTRACE_SINGLESTEP");
628		return -1;
629	}
630	return 0;
631}
632
633static void
634post_singlestep(struct process_stopping_handler *self,
635		struct Event **eventp)
636{
637	continue_for_sigstop_delivery(&self->pids);
638
639	if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT)
640		*eventp = NULL; // handled
641
642	struct process *proc = self->task_enabling_breakpoint;
643
644	remove_atomic_breakpoints(proc);
645	self->breakpoint_being_enabled = NULL;
646}
647
648static void
649singlestep_error(struct process_stopping_handler *self)
650{
651	struct process *teb = self->task_enabling_breakpoint;
652	struct breakpoint *sbp = self->breakpoint_being_enabled;
653	fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n",
654		teb->pid, breakpoint_name(sbp),	sbp->addr,
655		get_instruction_pointer(teb));
656	delete_breakpoint(teb->leader, sbp->addr);
657}
658
659static void
660pt_continue(struct process_stopping_handler *self)
661{
662	struct process *teb = self->task_enabling_breakpoint;
663	debug(1, "PTRACE_CONT");
664	ptrace(PTRACE_CONT, teb->pid, 0, 0);
665}
666
667static void
668pt_singlestep(struct process_stopping_handler *self)
669{
670	if (singlestep(self) < 0)
671		singlestep_error(self);
672}
673
674static void
675disable_and(struct process_stopping_handler *self,
676	    void (*do_this)(struct process_stopping_handler *self))
677{
678	struct process *teb = self->task_enabling_breakpoint;
679	debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid);
680	if (self->breakpoint_being_enabled->enabled)
681		disable_breakpoint(teb, self->breakpoint_being_enabled);
682	(do_this)(self);
683	self->state = psh_singlestep;
684}
685
686void
687linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self)
688{
689	disable_and(self, &pt_singlestep);
690}
691
692void
693linux_ptrace_disable_and_continue(struct process_stopping_handler *self)
694{
695	disable_and(self, &pt_continue);
696}
697
698/* This event handler is installed when we are in the process of
699 * stopping the whole thread group to do the pointer re-enablement for
700 * one of the threads.  We pump all events to the queue for later
701 * processing while we wait for all the threads to stop.  When this
702 * happens, we let the re-enablement thread to PTRACE_SINGLESTEP,
703 * re-enable, and continue everyone.  */
704static Event *
705process_stopping_on_event(struct event_handler *super, Event *event)
706{
707	struct process_stopping_handler *self = (void *)super;
708	struct process *task = event->proc;
709	struct process *leader = task->leader;
710	struct process *teb = self->task_enabling_breakpoint;
711
712	debug(DEBUG_PROCESS,
713	      "process_stopping_on_event: pid %d; event type %d; state %d",
714	      task->pid, event->type, self->state);
715
716	struct pid_task *task_info = get_task_info(&self->pids, task->pid);
717	if (task_info == NULL)
718		fprintf(stderr, "new task??? %d\n", task->pid);
719	handle_stopping_event(task_info, &event);
720
721	int state = self->state;
722	int event_to_queue = !event_exit_or_none_p(event);
723
724	/* Deactivate the entry if the task exits.  */
725	if (event_exit_p(event) && task_info != NULL)
726		task_info->pid = 0;
727
728	/* Always handle sysrets.  Whether sysret occurred and what
729	 * sys it rets from may need to be determined based on process
730	 * stack, so we need to keep that in sync with reality.  Note
731	 * that we don't continue the process after the sysret is
732	 * handled.  See continue_after_syscall.  */
733	if (event != NULL && event->type == EVENT_SYSRET) {
734		debug(1, "%d LT_EV_SYSRET", event->proc->pid);
735		event_to_queue = 0;
736		task_info->sysret = 1;
737	}
738
739	switch (state) {
740	case psh_stopping:
741		/* If everyone is stopped, singlestep.  */
742		if (each_task(leader, NULL, &task_blocked,
743			      &self->pids) == NULL) {
744			(self->on_all_stopped)(self);
745			state = self->state;
746		}
747		break;
748
749	case psh_singlestep:
750		/* In singlestep state, breakpoint signifies that we
751		 * have now stepped, and can re-enable the breakpoint.  */
752		if (event != NULL && task == teb) {
753
754			/* If this was caused by a real breakpoint, as
755			 * opposed to a singlestep, assume that it's
756			 * an artificial breakpoint installed for some
757			 * reason for the re-enablement.  In that case
758			 * handle it.  */
759			if (event->type == EVENT_BREAKPOINT) {
760				arch_addr_t ip
761					= get_instruction_pointer(task);
762				struct breakpoint *other
763					= address2bpstruct(leader, ip);
764				if (other != NULL)
765					breakpoint_on_hit(other, task);
766			}
767
768			/* If we got SIGNAL instead of BREAKPOINT,
769			 * then this is not singlestep at all.  */
770			if (event->type == EVENT_SIGNAL) {
771			do_singlestep:
772				if (singlestep(self) < 0) {
773					singlestep_error(self);
774					post_singlestep(self, &event);
775					goto psh_sinking;
776				}
777				break;
778			} else {
779				switch ((self->keep_stepping_p)(self)) {
780				case CBS_FAIL:
781					/* XXX handle me */
782				case CBS_STOP:
783					break;
784				case CBS_CONT:
785					/* Sink singlestep event.  */
786					if (event->type == EVENT_BREAKPOINT)
787						event = NULL;
788					goto do_singlestep;
789				}
790			}
791
792			/* Re-enable the breakpoint that we are
793			 * stepping over.  */
794			struct breakpoint *sbp = self->breakpoint_being_enabled;
795			if (sbp->enabled)
796				enable_breakpoint(teb, sbp);
797
798			post_singlestep(self, &event);
799			goto psh_sinking;
800		}
801		break;
802
803	psh_sinking:
804		state = self->state = psh_sinking;
805	case psh_sinking:
806		if (await_sigstop_delivery(&self->pids, task_info, event))
807			process_stopping_done(self, leader);
808		break;
809
810	case psh_ugly_workaround:
811		if (event == NULL)
812			break;
813		if (event->type == EVENT_BREAKPOINT) {
814			undo_breakpoint(event, leader);
815			if (task == teb)
816				self->task_enabling_breakpoint = NULL;
817		}
818		if (self->task_enabling_breakpoint == NULL
819		    && all_stops_accountable(&self->pids)) {
820			undo_breakpoint(event, leader);
821			detach_process(leader);
822			event = NULL; // handled
823		}
824	}
825
826	if (event != NULL && event_to_queue) {
827		enque_event(event);
828		event = NULL; // sink the event
829	}
830
831	return event;
832}
833
834static void
835process_stopping_destroy(struct event_handler *super)
836{
837	struct process_stopping_handler *self = (void *)super;
838	free(self->pids.tasks);
839}
840
841static enum callback_status
842no(struct process_stopping_handler *self)
843{
844	return CBS_STOP;
845}
846
847int
848process_install_stopping_handler(struct process *proc, struct breakpoint *sbp,
849				 void (*as)(struct process_stopping_handler *),
850				 enum callback_status (*ks)
851					 (struct process_stopping_handler *),
852				 enum callback_status (*uw)
853					(struct process_stopping_handler *))
854{
855	debug(DEBUG_FUNCTION,
856	      "process_install_stopping_handler: pid=%d", proc->pid);
857
858	struct process_stopping_handler *handler = calloc(sizeof(*handler), 1);
859	if (handler == NULL)
860		return -1;
861
862	if (as == NULL)
863		as = &linux_ptrace_disable_and_singlestep;
864	if (ks == NULL)
865		ks = &no;
866	if (uw == NULL)
867		uw = &no;
868
869	handler->super.on_event = process_stopping_on_event;
870	handler->super.destroy = process_stopping_destroy;
871	handler->task_enabling_breakpoint = proc;
872	handler->breakpoint_being_enabled = sbp;
873	handler->on_all_stopped = as;
874	handler->keep_stepping_p = ks;
875	handler->ugly_workaround_p = uw;
876
877	install_event_handler(proc->leader, &handler->super);
878
879	if (each_task(proc->leader, NULL, &send_sigstop,
880		      &handler->pids) != NULL) {
881		destroy_event_handler(proc);
882		return -1;
883	}
884
885	/* And deliver the first fake event, in case all the
886	 * conditions are already fulfilled.  */
887	Event ev = {
888		.type = EVENT_NONE,
889		.proc = proc,
890	};
891	process_stopping_on_event(&handler->super, &ev);
892
893	return 0;
894}
895
896void
897continue_after_breakpoint(struct process *proc, struct breakpoint *sbp)
898{
899	debug(DEBUG_PROCESS,
900	      "continue_after_breakpoint: pid=%d, addr=%p",
901	      proc->pid, sbp->addr);
902
903	set_instruction_pointer(proc, sbp->addr);
904
905	if (sbp->enabled == 0) {
906		continue_process(proc->pid);
907	} else {
908#if defined __sparc__  || defined __ia64___
909		/* we don't want to singlestep here */
910		continue_process(proc->pid);
911#else
912		if (process_install_stopping_handler
913		    (proc, sbp, NULL, NULL, NULL) < 0) {
914			perror("process_stopping_handler_create");
915			/* Carry on not bothering to re-enable.  */
916			continue_process(proc->pid);
917		}
918#endif
919	}
920}
921
922/**
923 * Ltrace exit.  When we are about to exit, we have to go through all
924 * the processes, stop them all, remove all the breakpoints, and then
925 * detach the processes that we attached to using -p.  If we left the
926 * other tasks running, they might hit stray return breakpoints and
927 * produce artifacts, so we better stop everyone, even if it's a bit
928 * of extra work.
929 */
930struct ltrace_exiting_handler
931{
932	struct event_handler super;
933	struct pid_set pids;
934};
935
936static Event *
937ltrace_exiting_on_event(struct event_handler *super, Event *event)
938{
939	struct ltrace_exiting_handler *self = (void *)super;
940	struct process *task = event->proc;
941	struct process *leader = task->leader;
942
943	debug(DEBUG_PROCESS,
944	      "ltrace_exiting_on_event: pid %d; event type %d",
945	      task->pid, event->type);
946
947	struct pid_task *task_info = get_task_info(&self->pids, task->pid);
948	handle_stopping_event(task_info, &event);
949
950	if (event != NULL && event->type == EVENT_BREAKPOINT)
951		undo_breakpoint(event, leader);
952
953	if (await_sigstop_delivery(&self->pids, task_info, event)
954	    && all_stops_accountable(&self->pids))
955		detach_process(leader);
956
957	/* Sink all non-exit events.  We are about to exit, so we
958	 * don't bother with queuing them. */
959	if (event_exit_or_none_p(event))
960		return event;
961
962	return NULL;
963}
964
965static void
966ltrace_exiting_destroy(struct event_handler *super)
967{
968	struct ltrace_exiting_handler *self = (void *)super;
969	free(self->pids.tasks);
970}
971
972static int
973ltrace_exiting_install_handler(struct process *proc)
974{
975	/* Only install to leader.  */
976	if (proc->leader != proc)
977		return 0;
978
979	/* Perhaps we are already installed, if the user passed
980	 * several -p options that are tasks of one process.  */
981	if (proc->event_handler != NULL
982	    && proc->event_handler->on_event == &ltrace_exiting_on_event)
983		return 0;
984
985	/* If stopping handler is already present, let it do the
986	 * work.  */
987	if (proc->event_handler != NULL) {
988		assert(proc->event_handler->on_event
989		       == &process_stopping_on_event);
990		struct process_stopping_handler *other
991			= (void *)proc->event_handler;
992		other->exiting = 1;
993		return 0;
994	}
995
996	struct ltrace_exiting_handler *handler
997		= calloc(sizeof(*handler), 1);
998	if (handler == NULL) {
999		perror("malloc exiting handler");
1000	fatal:
1001		/* XXXXXXXXXXXXXXXXXXX fixme */
1002		return -1;
1003	}
1004
1005	handler->super.on_event = ltrace_exiting_on_event;
1006	handler->super.destroy = ltrace_exiting_destroy;
1007	install_event_handler(proc->leader, &handler->super);
1008
1009	if (each_task(proc->leader, NULL, &send_sigstop,
1010		      &handler->pids) != NULL)
1011		goto fatal;
1012
1013	return 0;
1014}
1015
1016/*
1017 * When the traced process vforks, it's suspended until the child
1018 * process calls _exit or exec*.  In the meantime, the two share the
1019 * address space.
1020 *
1021 * The child process should only ever call _exit or exec*, but we
1022 * can't count on that (it's not the role of ltrace to policy, but to
1023 * observe).  In any case, we will _at least_ have to deal with
1024 * removal of vfork return breakpoint (which we have to smuggle back
1025 * in, so that the parent can see it, too), and introduction of exec*
1026 * return breakpoint.  Since we already have both breakpoint actions
1027 * to deal with, we might as well support it all.
1028 *
1029 * The gist is that we pretend that the child is in a thread group
1030 * with its parent, and handle it as a multi-threaded case, with the
1031 * exception that we know that the parent is blocked, and don't
1032 * attempt to stop it.  When the child execs, we undo the setup.
1033 */
1034
1035struct process_vfork_handler
1036{
1037	struct event_handler super;
1038	void *bp_addr;
1039};
1040
1041static Event *
1042process_vfork_on_event(struct event_handler *super, Event *event)
1043{
1044	debug(DEBUG_PROCESS,
1045	      "process_vfork_on_event: pid %d; event type %d",
1046	      event->proc->pid, event->type);
1047
1048	struct process_vfork_handler *self = (void *)super;
1049	struct breakpoint *sbp;
1050	assert(self != NULL);
1051
1052	switch (event->type) {
1053	case EVENT_BREAKPOINT:
1054		/* Remember the vfork return breakpoint.  */
1055		if (self->bp_addr == 0)
1056			self->bp_addr = event->e_un.brk_addr;
1057		break;
1058
1059	case EVENT_EXIT:
1060	case EVENT_EXIT_SIGNAL:
1061	case EVENT_EXEC:
1062		/* Smuggle back in the vfork return breakpoint, so
1063		 * that our parent can trip over it once again.  */
1064		if (self->bp_addr != 0) {
1065			sbp = dict_find_entry(event->proc->leader->breakpoints,
1066					      self->bp_addr);
1067			if (sbp != NULL)
1068				assert(sbp->libsym == NULL);
1069			/* We don't mind failing that, it's not a big
1070			 * deal to not display one extra vfork return.  */
1071			insert_breakpoint(event->proc->parent,
1072					  self->bp_addr, NULL);
1073		}
1074
1075		continue_process(event->proc->parent->pid);
1076
1077		/* Remove the leader that we artificially set up
1078		 * earlier.  */
1079		change_process_leader(event->proc, event->proc);
1080		destroy_event_handler(event->proc);
1081
1082	default:
1083		;
1084	}
1085
1086	return event;
1087}
1088
1089void
1090continue_after_vfork(struct process *proc)
1091{
1092	debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid);
1093	struct process_vfork_handler *handler = calloc(sizeof(*handler), 1);
1094	if (handler == NULL) {
1095		perror("malloc vfork handler");
1096		/* Carry on not bothering to treat the process as
1097		 * necessary.  */
1098		continue_process(proc->parent->pid);
1099		return;
1100	}
1101
1102	/* We must set up custom event handler, so that we see
1103	 * exec/exit events for the task itself.  */
1104	handler->super.on_event = process_vfork_on_event;
1105	install_event_handler(proc, &handler->super);
1106
1107	/* Make sure that the child is sole thread.  */
1108	assert(proc->leader == proc);
1109	assert(proc->next == NULL || proc->next->leader != proc);
1110
1111	/* Make sure that the child's parent is properly set up.  */
1112	assert(proc->parent != NULL);
1113	assert(proc->parent->leader != NULL);
1114
1115	change_process_leader(proc, proc->parent->leader);
1116}
1117
1118static int
1119is_mid_stopping(struct process *proc)
1120{
1121	return proc != NULL
1122		&& proc->event_handler != NULL
1123		&& proc->event_handler->on_event == &process_stopping_on_event;
1124}
1125
1126void
1127continue_after_syscall(struct process *proc, int sysnum, int ret_p)
1128{
1129	/* Don't continue if we are mid-stopping.  */
1130	if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) {
1131		debug(DEBUG_PROCESS,
1132		      "continue_after_syscall: don't continue %d",
1133		      proc->pid);
1134		return;
1135	}
1136	continue_process(proc->pid);
1137}
1138
1139/* If ltrace gets SIGINT, the processes directly or indirectly run by
1140 * ltrace get it too.  We just have to wait long enough for the signal
1141 * to be delivered and the process terminated, which we notice and
1142 * exit ltrace, too.  So there's not much we need to do there.  We
1143 * want to keep tracing those processes as usual, in case they just
1144 * SIG_IGN the SIGINT to do their shutdown etc.
1145 *
1146 * For processes ran on the background, we want to install an exit
1147 * handler that stops all the threads, removes all breakpoints, and
1148 * detaches.
1149 */
1150void
1151os_ltrace_exiting(void)
1152{
1153	struct opt_p_t *it;
1154	for (it = opt_p; it != NULL; it = it->next) {
1155		struct process *proc = pid2proc(it->pid);
1156		if (proc == NULL || proc->leader == NULL)
1157			continue;
1158		if (ltrace_exiting_install_handler(proc->leader) < 0)
1159			fprintf(stderr,
1160				"Couldn't install exiting handler for %d.\n",
1161				proc->pid);
1162	}
1163}
1164
1165int
1166os_ltrace_exiting_sighandler(void)
1167{
1168	extern int linux_in_waitpid;
1169	if (linux_in_waitpid) {
1170		os_ltrace_exiting();
1171		return 1;
1172	}
1173	return 0;
1174}
1175
1176size_t
1177umovebytes(struct process *proc, void *addr, void *laddr, size_t len)
1178{
1179
1180	union {
1181		long a;
1182		char c[sizeof(long)];
1183	} a;
1184	int started = 0;
1185	size_t offset = 0, bytes_read = 0;
1186
1187	while (offset < len) {
1188		a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
1189		if (a.a == -1 && errno) {
1190			if (started && errno == EIO)
1191				return bytes_read;
1192			else
1193				return -1;
1194		}
1195		started = 1;
1196
1197		if (len - offset >= sizeof(long)) {
1198			memcpy(laddr + offset, &a.c[0], sizeof(long));
1199			bytes_read += sizeof(long);
1200		}
1201		else {
1202			memcpy(laddr + offset, &a.c[0], len - offset);
1203			bytes_read += (len - offset);
1204		}
1205		offset += sizeof(long);
1206	}
1207
1208	return bytes_read;
1209}
1210