proc.c revision 8137e804992f15590105a3db29673674971dff7b
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013,2014 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2009 Juan Cespedes
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include "config.h"
24
25#include <sys/types.h>
26#include <assert.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include "backend.h"
33#include "breakpoint.h"
34#include "debug.h"
35#include "fetch.h"
36#include "options.h"
37#include "proc.h"
38#include "value_dict.h"
39
40#ifndef OS_HAVE_PROCESS_DATA
41int
42os_process_init(struct process *proc)
43{
44	return 0;
45}
46
47void
48os_process_destroy(struct process *proc)
49{
50}
51
52int
53os_process_clone(struct process *retp, struct process *proc)
54{
55	return 0;
56}
57
58int
59os_process_exec(struct process *proc)
60{
61	return 0;
62}
63#endif
64
65#ifndef ARCH_HAVE_PROCESS_DATA
66int
67arch_process_init(struct process *proc)
68{
69	return 0;
70}
71
72void
73arch_process_destroy(struct process *proc)
74{
75}
76
77int
78arch_process_clone(struct process *retp, struct process *proc)
79{
80	return 0;
81}
82
83int
84arch_process_exec(struct process *proc)
85{
86	return 0;
87}
88#endif
89
90#ifndef ARCH_HAVE_DYNLINK_DONE
91void
92arch_dynlink_done(struct process *proc)
93{
94}
95#endif
96
97static int add_process(struct process *proc, int was_exec);
98static void unlist_process(struct process *proc);
99
100static void
101destroy_unwind(struct process *proc)
102{
103#if defined(HAVE_LIBUNWIND)
104	if (proc->unwind_priv != NULL)
105		_UPT_destroy(proc->unwind_priv);
106	if (proc->unwind_as != NULL)
107		unw_destroy_addr_space(proc->unwind_as);
108#endif /* defined(HAVE_LIBUNWIND) */
109
110#if defined(HAVE_LIBDW)
111	if (proc->dwfl != NULL)
112		dwfl_end(proc->dwfl);
113#endif /* defined(HAVE_LIBDW) */
114}
115
116static int
117process_bare_init(struct process *proc, const char *filename,
118		  pid_t pid, int was_exec)
119{
120	if (!was_exec) {
121		memset(proc, 0, sizeof(*proc));
122
123		proc->filename = strdup(filename);
124		if (proc->filename == NULL) {
125		fail:
126			free(proc->filename);
127			if (proc->breakpoints != NULL) {
128				dict_destroy(proc->breakpoints,
129					     NULL, NULL, NULL);
130				free(proc->breakpoints);
131				proc->breakpoints = NULL;
132			}
133			return -1;
134		}
135	}
136
137	/* Add process so that we know who the leader is.  */
138	proc->pid = pid;
139	if (add_process(proc, was_exec) < 0)
140		goto fail;
141	if (proc->leader == NULL) {
142	unlist_and_fail:
143		if (!was_exec)
144			unlist_process(proc);
145		goto fail;
146	}
147
148	if (proc->leader == proc) {
149		proc->breakpoints = malloc(sizeof(*proc->breakpoints));
150		if (proc->breakpoints == NULL)
151			goto unlist_and_fail;
152		DICT_INIT(proc->breakpoints,
153			  arch_addr_t, struct breakpoint *,
154			  arch_addr_hash, arch_addr_eq, NULL);
155	} else {
156		proc->breakpoints = NULL;
157	}
158
159#if defined(HAVE_LIBUNWIND)
160	if (options.bt_depth > 0) {
161		proc->unwind_priv = _UPT_create(pid);
162		proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
163
164		if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
165			fprintf(stderr,
166				"Couldn't initialize unwinding "
167				"for process %d\n", proc->pid);
168			destroy_unwind(proc);
169			proc->unwind_priv = NULL;
170			proc->unwind_as = NULL;
171		}
172	}
173#endif /* defined(HAVE_LIBUNWIND) */
174
175#if defined(HAVE_LIBDW)
176	proc->dwfl = NULL; /* Initialize for leader only on first library.  */
177#endif /* defined(HAVE_LIBDW) */
178
179	return 0;
180}
181
182static void
183process_bare_destroy(struct process *proc, int was_exec)
184{
185	dict_destroy(proc->breakpoints, NULL, NULL, NULL);
186	free(proc->breakpoints);
187	if (!was_exec) {
188		free(proc->filename);
189		unlist_process(proc);
190		destroy_unwind(proc);
191	}
192}
193
194static int
195process_init_main(struct process *proc)
196{
197	if (breakpoints_init(proc) < 0) {
198		fprintf(stderr, "failed to init breakpoints %d\n",
199			proc->pid);
200		return -1;
201	}
202
203	return 0;
204}
205
206int
207process_init(struct process *proc, const char *filename, pid_t pid)
208{
209	if (process_bare_init(proc, filename, pid, 0) < 0) {
210	fail:
211		fprintf(stderr, "failed to initialize process %d: %s\n",
212			pid, strerror(errno));
213		return -1;
214	}
215
216	if (os_process_init(proc) < 0) {
217		process_bare_destroy(proc, 0);
218		goto fail;
219	}
220
221	if (arch_process_init(proc) < 0) {
222		os_process_destroy(proc);
223		process_bare_destroy(proc, 0);
224		goto fail;
225	}
226
227	if (proc->leader != proc) {
228		proc->e_machine = proc->leader->e_machine;
229		proc->e_class = proc->leader->e_class;
230		get_arch_dep(proc);
231	} else if (process_init_main(proc) < 0) {
232		process_bare_destroy(proc, 0);
233		goto fail;
234	}
235	return 0;
236}
237
238static enum callback_status
239destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
240{
241	breakpoint_destroy(bp);
242	free(bp);
243	return CBS_CONT;
244}
245
246// XXX see comment in handle_event.c
247void callstack_pop(struct process *proc);
248
249static void
250private_process_destroy(struct process *proc, int was_exec)
251{
252	/* Pop remaining stack elements.  */
253	while (proc->callstack_depth > 0) {
254		/* When this is called just before a process is
255		 * destroyed, the breakpoints should either have been
256		 * retracted by now, or were killed by exec.  In any
257		 * case, it's safe to pretend that there are no
258		 * breakpoints associated with the stack elements, so
259		 * that stack_pop doesn't attempt to destroy them.  */
260		size_t i = proc->callstack_depth - 1;
261		if (!proc->callstack[i].is_syscall)
262			proc->callstack[i].return_addr = 0;
263
264		callstack_pop(proc);
265	}
266
267	if (!was_exec)
268		free(proc->filename);
269
270	/* Libraries and symbols.  This is only relevant in
271	 * leader.  */
272	struct library *lib;
273	for (lib = proc->libraries; lib != NULL; ) {
274		struct library *next = lib->next;
275		library_destroy(lib);
276		free(lib);
277		lib = next;
278	}
279	proc->libraries = NULL;
280
281	/* Breakpoints.  */
282	if (proc->breakpoints != NULL) {
283		proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
284		dict_destroy(proc->breakpoints, NULL, NULL, NULL);
285		free(proc->breakpoints);
286		proc->breakpoints = NULL;
287	}
288
289	destroy_unwind(proc);
290}
291
292void
293process_destroy(struct process *proc)
294{
295	arch_process_destroy(proc);
296	os_process_destroy(proc);
297	private_process_destroy(proc, 0);
298}
299
300int
301process_exec(struct process *proc)
302{
303	/* Call exec handlers first, before we destroy the main
304	 * state.  */
305	if (arch_process_exec(proc) < 0
306	    || os_process_exec(proc) < 0)
307		return -1;
308
309	private_process_destroy(proc, 1);
310
311	if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
312		return -1;
313	if (process_init_main(proc) < 0) {
314		process_bare_destroy(proc, 1);
315		return -1;
316	}
317	return 0;
318}
319
320struct process *
321open_program(const char *filename, pid_t pid)
322{
323	assert(pid != 0);
324	struct process *proc = malloc(sizeof(*proc));
325	if (proc == NULL || process_init(proc, filename, pid) < 0) {
326		free(proc);
327		return NULL;
328	}
329	return proc;
330}
331
332struct clone_single_bp_data {
333	struct process *old_proc;
334	struct process *new_proc;
335};
336
337static enum callback_status
338clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
339{
340	struct breakpoint *bp = *bpp;
341	struct clone_single_bp_data *data = u;
342
343	struct breakpoint *clone = malloc(sizeof(*clone));
344	if (clone == NULL
345	    || breakpoint_clone(clone, data->new_proc, bp) < 0) {
346	fail:
347		free(clone);
348		return CBS_STOP;
349	}
350	if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
351		breakpoint_destroy(clone);
352		goto fail;
353	}
354	return CBS_CONT;
355}
356
357int
358process_clone(struct process *retp, struct process *proc, pid_t pid)
359{
360	if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
361	fail1:
362		fprintf(stderr, "Failed to clone process %d to %d: %s\n",
363			proc->pid, pid, strerror(errno));
364		return -1;
365	}
366
367	retp->tracesysgood = proc->tracesysgood;
368	retp->e_machine = proc->e_machine;
369	retp->e_class = proc->e_class;
370
371	/* For non-leader processes, that's all we need to do.  */
372	if (retp->leader != retp)
373		return 0;
374
375	/* Clone symbols first so that we can clone and relink
376	 * breakpoints.  */
377	struct library *lib;
378	struct library **nlibp = &retp->libraries;
379	for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
380		*nlibp = malloc(sizeof(**nlibp));
381
382		if (*nlibp == NULL
383		    || library_clone(*nlibp, lib) < 0) {
384			free(*nlibp);
385			*nlibp = NULL;
386
387		fail2:
388			process_bare_destroy(retp, 0);
389
390			/* Error when cloning.  Unroll what was done.  */
391			for (lib = retp->libraries; lib != NULL; ) {
392				struct library *next = lib->next;
393				library_destroy(lib);
394				free(lib);
395				lib = next;
396			}
397			goto fail1;
398		}
399
400		nlibp = &(*nlibp)->next;
401	}
402
403	/* Now clone breakpoints.  Symbol relinking is done in
404	 * clone_single_bp.  */
405	struct clone_single_bp_data data = {
406		.old_proc = proc,
407		.new_proc = retp,
408	};
409	if (DICT_EACH(proc->leader->breakpoints,
410		      arch_addr_t, struct breakpoint *, NULL,
411		      clone_single_bp, &data) != NULL)
412		goto fail2;
413
414	/* And finally the call stack.  */
415	/* XXX clearly the callstack handling should be moved to a
416	 * separate module and this whole business extracted to
417	 * callstack_clone, or callstack_element_clone.  */
418	memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
419	retp->callstack_depth = proc->callstack_depth;
420
421	size_t i;
422	for (i = 0; i < retp->callstack_depth; ++i) {
423		struct callstack_element *elem = &retp->callstack[i];
424		struct fetch_context *ctx = elem->fetch_context;
425		if (ctx != NULL) {
426			struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
427			if (nctx == NULL) {
428				size_t j;
429			fail3:
430				for (j = 0; j < i; ++j) {
431					nctx = retp->callstack[j].fetch_context;
432					fetch_arg_done(nctx);
433					elem->fetch_context = NULL;
434				}
435				goto fail2;
436			}
437			elem->fetch_context = nctx;
438		}
439
440		if (elem->arguments != NULL) {
441			struct value_dict *nargs = malloc(sizeof(*nargs));
442			if (nargs == NULL
443			    || val_dict_clone(nargs, elem->arguments) < 0) {
444				size_t j;
445				for (j = 0; j < i; ++j) {
446					nargs = retp->callstack[j].arguments;
447					val_dict_destroy(nargs);
448					free(nargs);
449					elem->arguments = NULL;
450				}
451
452				/* Pretend that this round went well,
453				 * so that fail3 frees I-th
454				 * fetch_context.  */
455				++i;
456				goto fail3;
457			}
458			elem->arguments = nargs;
459		}
460
461		/* If it's not a syscall, we need to find the
462		 * corresponding library symbol in the cloned
463		 * library.  */
464		if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
465			struct library_symbol *libfunc = elem->c_un.libfunc;
466			int rc = proc_find_symbol(retp, libfunc,
467						  NULL, &elem->c_un.libfunc);
468			assert(rc == 0);
469		}
470	}
471
472	/* At this point, retp is fully initialized, except for OS and
473	 * arch parts, and we can call private_process_destroy.  */
474	if (os_process_clone(retp, proc) < 0) {
475		private_process_destroy(retp, 0);
476		return -1;
477	}
478	if (arch_process_clone(retp, proc) < 0) {
479		os_process_destroy(retp);
480		private_process_destroy(retp, 0);
481		return -1;
482	}
483
484	return 0;
485}
486
487static int
488open_one_pid(pid_t pid)
489{
490	debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
491
492	/* Get the filename first.  Should the trace_pid fail, we can
493	 * easily free it, untracing is more work.  */
494	char *filename = pid2name(pid);
495	if (filename == NULL || trace_pid(pid) < 0) {
496	fail:
497		free(filename);
498		return -1;
499	}
500
501	struct process *proc = open_program(filename, pid);
502	if (proc == NULL)
503		goto fail;
504	free(filename);
505	trace_set_options(proc);
506
507	return 0;
508}
509
510static enum callback_status
511start_one_pid(struct process *proc, void *data)
512{
513	continue_process(proc->pid);
514	return CBS_CONT;
515}
516
517static enum callback_status
518is_main(struct process *proc, struct library *lib, void *data)
519{
520	return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
521}
522
523void
524process_hit_start(struct process *proc)
525{
526	struct process *leader = proc->leader;
527	assert(leader != NULL);
528
529	struct library *mainlib
530		= proc_each_library(leader, NULL, is_main, NULL);
531	assert(mainlib != NULL);
532	linkmap_init(leader, mainlib->dyn_addr);
533	arch_dynlink_done(leader);
534}
535
536void
537open_pid(pid_t pid)
538{
539	debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
540	/* If we are already tracing this guy, we should be seeing all
541	 * his children via normal tracing route.  */
542	if (pid2proc(pid) != NULL)
543		return;
544
545	/* First, see if we can attach the requested PID itself.  */
546	if (open_one_pid(pid) < 0) {
547		fprintf(stderr, "Cannot attach to pid %u: %s\n",
548			pid, strerror(errno));
549		trace_fail_warning(pid);
550		return;
551	}
552
553	/* Now attach to all tasks that belong to that PID.  There's a
554	 * race between process_tasks and open_one_pid.  So when we
555	 * fail in open_one_pid below, we just do another round.
556	 * Chances are that by then that PID will have gone away, and
557	 * that's why we have seen the failure.  The processes that we
558	 * manage to open_one_pid are stopped, so we should eventually
559	 * reach a point where process_tasks doesn't give any new
560	 * processes (because there's nobody left to produce
561	 * them).  */
562	size_t old_ntasks = 0;
563	int have_all;
564	while (1) {
565		pid_t *tasks;
566		size_t ntasks;
567		size_t i;
568
569		if (process_tasks(pid, &tasks, &ntasks) < 0) {
570			fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
571				pid, strerror(errno));
572			break;
573		}
574
575		have_all = 1;
576		for (i = 0; i < ntasks; ++i)
577			if (pid2proc(tasks[i]) == NULL
578			    && open_one_pid(tasks[i]) < 0)
579				have_all = 0;
580
581		free(tasks);
582
583		if (have_all && old_ntasks == ntasks)
584			break;
585		old_ntasks = ntasks;
586	}
587
588	struct process *leader = pid2proc(pid)->leader;
589
590	/* XXX Is there a way to figure out whether _start has
591	 * actually already been hit?  */
592	process_hit_start(leader);
593
594	/* Done.  Continue everyone.  */
595	each_task(leader, NULL, start_one_pid, NULL);
596}
597
598static enum callback_status
599find_proc(struct process *proc, void *data)
600{
601	return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
602}
603
604struct process *
605pid2proc(pid_t pid)
606{
607	return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
608}
609
610static struct process *list_of_processes = NULL;
611
612static void
613unlist_process(struct process *proc)
614{
615	if (list_of_processes == proc) {
616		list_of_processes = list_of_processes->next;
617		return;
618	}
619
620	struct process *tmp;
621	for (tmp = list_of_processes; ; tmp = tmp->next) {
622		/* If the following assert fails, the process wasn't
623		 * in the list.  */
624		assert(tmp->next != NULL);
625
626		if (tmp->next == proc) {
627			tmp->next = tmp->next->next;
628			return;
629		}
630	}
631}
632
633struct process *
634each_process(struct process *start_after,
635	     enum callback_status(*cb)(struct process *proc, void *data),
636	     void *data)
637{
638	struct process *it = start_after == NULL ? list_of_processes
639		: start_after->next;
640
641	while (it != NULL) {
642		/* Callback might call remove_process.  */
643		struct process *next = it->next;
644		switch ((*cb)(it, data)) {
645		case CBS_FAIL:
646			/* XXX handle me */
647		case CBS_STOP:
648			return it;
649		case CBS_CONT:
650			break;
651		}
652		it = next;
653	}
654	return NULL;
655}
656
657struct process *
658each_task(struct process *proc, struct process *start_after,
659	  enum callback_status(*cb)(struct process *proc, void *data),
660	  void *data)
661{
662	assert(proc != NULL);
663	struct process *it = start_after == NULL ? proc->leader
664		: start_after->next;
665
666	if (it != NULL) {
667		struct process *leader = it->leader;
668		while (it != NULL && it->leader == leader) {
669			/* Callback might call remove_process.  */
670			struct process *next = it->next;
671			switch ((*cb)(it, data)) {
672			case CBS_FAIL:
673				/* XXX handle me */
674			case CBS_STOP:
675				return it;
676			case CBS_CONT:
677				break;
678			}
679			it = next;
680		}
681	}
682	return NULL;
683}
684
685static int
686add_process(struct process *proc, int was_exec)
687{
688	struct process **leaderp = &list_of_processes;
689	if (proc->pid) {
690		pid_t tgid = process_leader(proc->pid);
691		if (tgid == 0)
692			/* Must have been terminated before we managed
693			 * to fully attach.  */
694			return -1;
695		if (tgid == proc->pid) {
696			proc->leader = proc;
697		} else {
698			struct process *leader = pid2proc(tgid);
699			proc->leader = leader;
700			if (leader != NULL)
701				leaderp = &leader->next;
702		}
703	}
704
705	if (!was_exec) {
706		proc->next = *leaderp;
707		*leaderp = proc;
708	}
709	return 0;
710}
711
712void
713change_process_leader(struct process *proc, struct process *leader)
714{
715	struct process **leaderp = &list_of_processes;
716	if (proc->leader == leader)
717		return;
718
719	assert(leader != NULL);
720	unlist_process(proc);
721	if (proc != leader)
722		leaderp = &leader->next;
723
724	proc->leader = leader;
725	proc->next = *leaderp;
726	*leaderp = proc;
727}
728
729static enum callback_status
730clear_leader(struct process *proc, void *data)
731{
732	debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
733	      proc->pid, proc->leader->pid);
734	proc->leader = NULL;
735	return CBS_CONT;
736}
737
738void
739remove_process(struct process *proc)
740{
741	debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
742
743	if (proc->leader == proc)
744		each_task(proc, NULL, &clear_leader, NULL);
745
746	unlist_process(proc);
747	process_removed(proc);
748	process_destroy(proc);
749	free(proc);
750}
751
752void
753install_event_handler(struct process *proc, struct event_handler *handler)
754{
755	debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
756	assert(proc->event_handler == NULL);
757	proc->event_handler = handler;
758}
759
760void
761destroy_event_handler(struct process *proc)
762{
763	struct event_handler *handler = proc->event_handler;
764	debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
765	assert(handler != NULL);
766	if (handler->destroy != NULL)
767		handler->destroy(handler);
768	free(handler);
769	proc->event_handler = NULL;
770}
771
772static int
773breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
774{
775	arch_addr_t bp_addr;
776	assert(proc->leader == proc);
777
778	/* Don't enable latent or delayed symbols.  */
779	if (libsym->latent || libsym->delayed) {
780		debug(DEBUG_FUNCTION,
781		      "delayed and/or latent breakpoint pid=%d, %s@%p",
782		      proc->pid, libsym->name, libsym->enter_addr);
783		return 0;
784	}
785
786	bp_addr = sym2addr(proc, libsym);
787
788	/* If there is an artificial breakpoint on the same address,
789	 * its libsym will be NULL, and we can smuggle our libsym
790	 * there.  That artificial breakpoint is there presumably for
791	 * the callbacks, which we don't touch.  If there is a real
792	 * breakpoint, then this is a bug.  ltrace-elf.c should filter
793	 * symbols and ignore extra symbol aliases.
794	 *
795	 * The other direction is more complicated and currently not
796	 * supported.  If a breakpoint has custom callbacks, it might
797	 * be also custom-allocated, and we would really need to swap
798	 * the two: delete the one now in the dictionary, swap values
799	 * around, and put the new breakpoint back in.  */
800	struct breakpoint *bp;
801	if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
802		/* MIPS backend makes duplicate requests.  This is
803		 * likely a bug in the backend.  Currently there's no
804		 * point assigning more than one symbol to a
805		 * breakpoint, because when it hits, we won't know
806		 * what to print out.  But it's easier to fix it here
807		 * before someone who understands MIPS has the time to
808		 * look into it.  So turn the sanity check off on
809		 * MIPS.  References:
810		 *
811		 *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
812		 *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
813		 */
814#ifndef __mips__
815		assert(bp->libsym == NULL);
816		bp->libsym = libsym;
817#endif
818		return 0;
819	}
820
821	bp = malloc(sizeof(*bp));
822	if (bp == NULL
823	    || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
824	fail:
825		free(bp);
826		return -1;
827	}
828	if (proc_add_breakpoint(proc, bp) < 0) {
829		breakpoint_destroy(bp);
830		goto fail;
831	}
832
833	if (breakpoint_turn_on(bp, proc) < 0) {
834		proc_remove_breakpoint(proc, bp);
835		breakpoint_destroy(bp);
836		goto fail;
837	}
838
839	return 0;
840}
841
842static enum callback_status
843cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
844{
845	return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
846}
847
848static int
849proc_activate_latent_symbol(struct process *proc,
850			    struct library_symbol *libsym)
851{
852	assert(libsym->latent);
853	libsym->latent = 0;
854	debug(DEBUG_FUNCTION, "activated latent symbol");
855	return breakpoint_for_symbol(libsym, proc);
856}
857
858int
859proc_activate_delayed_symbol(struct process *proc,
860			     struct library_symbol *libsym)
861{
862	assert(libsym->delayed);
863	libsym->delayed = 0;
864	debug(DEBUG_FUNCTION, "activated delayed symbol");
865	return breakpoint_for_symbol(libsym, proc);
866}
867
868static enum callback_status
869activate_latent_in(struct process *proc, struct library *lib, void *data)
870{
871	struct library_exported_name *exported;
872	for (exported = data; exported != NULL; exported = exported->next) {
873		struct library_symbol *libsym = NULL;
874		while ((libsym = library_each_symbol(lib, libsym,
875						     library_symbol_named_cb,
876						     (void *)exported->name))
877		       != NULL)
878			if (libsym->latent
879			    && proc_activate_latent_symbol(proc, libsym) < 0)
880				return CBS_FAIL;
881	}
882	return CBS_CONT;
883}
884
885void
886proc_add_library(struct process *proc, struct library *lib)
887{
888	assert(lib->next == NULL);
889	lib->next = proc->libraries;
890	proc->libraries = lib;
891	debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
892	      lib->soname, lib->base, lib->pathname, proc->pid);
893
894#if defined(HAVE_LIBDW)
895	if (options.bt_depth > 0) {
896		/* Setup module tracking for libdwfl unwinding.  */
897		struct process *leader = proc->leader;
898		Dwfl *dwfl = leader->dwfl;
899		if (dwfl == NULL) {
900			static const Dwfl_Callbacks proc_callbacks = {
901				.find_elf = dwfl_linux_proc_find_elf,
902				.find_debuginfo = dwfl_standard_find_debuginfo
903			};
904			dwfl = dwfl_begin(&proc_callbacks);
905			if (dwfl == NULL)
906				fprintf(stderr,
907					"Couldn't initialize libdwfl unwinding "
908					"for process %d: %s\n", leader->pid,
909					dwfl_errmsg (-1));
910		}
911
912		if (dwfl != NULL) {
913			dwfl_report_begin_add(dwfl);
914			if (dwfl_report_elf(dwfl, lib->soname,
915					    lib->pathname, -1,
916					    (GElf_Addr) lib->base,
917					    false) == NULL)
918				fprintf(stderr,
919					"dwfl_report_elf %s@%p (%s) %d: %s\n",
920					lib->soname, lib->base, lib->pathname,
921					proc->pid, dwfl_errmsg (-1));
922			dwfl_report_end(dwfl, NULL, NULL);
923
924			if (leader->dwfl == NULL) {
925				int r = dwfl_linux_proc_attach(dwfl,
926							       leader->pid,
927							       true);
928				if (r == 0)
929					leader->dwfl = dwfl;
930				else {
931					const char *msg;
932					dwfl_end(dwfl);
933					if (r < 0)
934						msg = dwfl_errmsg(-1);
935					else
936						msg = strerror(r);
937					fprintf(stderr, "Couldn't initialize "
938						"libdwfl unwinding for "
939						"process %d: %s\n",
940						leader->pid, msg);
941				}
942			}
943		}
944	}
945#endif /* defined(HAVE_LIBDW) */
946
947	/* Insert breakpoints for all active (non-latent) symbols.  */
948	struct library_symbol *libsym = NULL;
949	while ((libsym = library_each_symbol(lib, libsym,
950					     cb_breakpoint_for_symbol,
951					     proc)) != NULL)
952		fprintf(stderr,
953			"Couldn't insert breakpoint for %s to %d: %s.\n",
954			libsym->name, proc->pid, strerror(errno));
955
956	/* Look through export list of the new library and compare it
957	 * with latent symbols of all libraries (including this
958	 * library itself).  */
959	struct library *lib2 = NULL;
960	while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
961					 lib->exported_names)) != NULL)
962		fprintf(stderr,
963			"Couldn't activate latent symbols for %s in %d: %s.\n",
964			lib2->soname, proc->pid, strerror(errno));
965}
966
967int
968proc_remove_library(struct process *proc, struct library *lib)
969{
970	struct library **libp;
971	for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
972		if (*libp == lib) {
973			*libp = lib->next;
974			return 0;
975		}
976	return -1;
977}
978
979struct library *
980proc_each_library(struct process *proc, struct library *it,
981		  enum callback_status (*cb)(struct process *proc,
982					     struct library *lib, void *data),
983		  void *data)
984{
985	if (it == NULL)
986		it = proc->libraries;
987	else
988		it = it->next;
989
990	while (it != NULL) {
991		struct library *next = it->next;
992
993		switch (cb(proc, it, data)) {
994		case CBS_FAIL:
995			/* XXX handle me */
996		case CBS_STOP:
997			return it;
998		case CBS_CONT:
999			break;
1000		}
1001
1002		it = next;
1003	}
1004
1005	return NULL;
1006}
1007
1008static void
1009check_leader(struct process *proc)
1010{
1011	/* Only the group leader should be getting the breakpoints and
1012	 * thus have ->breakpoint initialized.  */
1013	assert(proc->leader != NULL);
1014	assert(proc->leader == proc);
1015	assert(proc->breakpoints != NULL);
1016}
1017
1018int
1019proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
1020{
1021	debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
1022	      proc->pid, breakpoint_name(bp), bp->addr);
1023	check_leader(proc);
1024
1025	/* XXX We might merge bp->libsym instead of the following
1026	 * assert, but that's not necessary right now.  Read the
1027	 * comment in breakpoint_for_symbol.  */
1028	assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
1029
1030	if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
1031		fprintf(stderr,
1032			"couldn't enter breakpoint %s@%p to dictionary: %s\n",
1033			breakpoint_name(bp), bp->addr, strerror(errno));
1034		return -1;
1035	}
1036
1037	return 0;
1038}
1039
1040void
1041proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
1042{
1043	debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
1044	      proc->pid, breakpoint_name(bp), bp->addr);
1045	check_leader(proc);
1046	int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
1047			    NULL, NULL, NULL);
1048	assert(rc == 0);
1049}
1050
1051struct each_breakpoint_data
1052{
1053	struct process *proc;
1054	enum callback_status (*cb)(struct process *proc,
1055				   struct breakpoint *bp,
1056				   void *data);
1057	void *cb_data;
1058};
1059
1060static enum callback_status
1061each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
1062{
1063	struct each_breakpoint_data *data = d;
1064	return data->cb(data->proc, *bpp, data->cb_data);
1065}
1066
1067arch_addr_t *
1068proc_each_breakpoint(struct process *proc, arch_addr_t *start,
1069		     enum callback_status (*cb)(struct process *proc,
1070						struct breakpoint *bp,
1071						void *data), void *data)
1072{
1073	struct each_breakpoint_data dd = {
1074		.proc = proc,
1075		.cb = cb,
1076		.cb_data = data,
1077	};
1078	return DICT_EACH(proc->breakpoints,
1079			 arch_addr_t, struct breakpoint *, start,
1080			 &each_breakpoint_cb, &dd);
1081}
1082
1083int
1084proc_find_symbol(struct process *proc, struct library_symbol *sym,
1085		 struct library **retlib, struct library_symbol **retsym)
1086{
1087	struct library *lib = sym->lib;
1088	assert(lib != NULL);
1089
1090	struct library *flib
1091		= proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1092	if (flib == NULL)
1093		return -1;
1094
1095	struct library_symbol *fsym
1096		= library_each_symbol(flib, NULL, library_symbol_named_cb,
1097				      (char *)sym->name);
1098	if (fsym == NULL)
1099		return -1;
1100
1101	if (retlib != NULL)
1102		*retlib = flib;
1103	if (retsym != NULL)
1104		*retsym = fsym;
1105
1106	return 0;
1107}
1108
1109struct library_symbol *
1110proc_each_symbol(struct process *proc, struct library_symbol *start_after,
1111		 enum callback_status (*cb)(struct library_symbol *, void *),
1112		 void *data)
1113{
1114	struct library *lib;
1115	for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1116	     lib != NULL; lib = lib->next) {
1117		start_after = library_each_symbol(lib, start_after, cb, data);
1118		if (start_after != NULL)
1119			return start_after;
1120	}
1121
1122	return NULL;
1123}
1124
1125#define DEF_READER(NAME, SIZE)						\
1126	int								\
1127	NAME(struct process *proc, arch_addr_t addr,			\
1128	     uint##SIZE##_t *lp)					\
1129	{								\
1130		union {							\
1131			uint##SIZE##_t dst;				\
1132			char buf[0];					\
1133		} u;							\
1134		if (umovebytes(proc, addr, &u.buf, sizeof(u.dst))	\
1135		    != sizeof(u.dst))					\
1136			return -1;					\
1137		*lp = u.dst;						\
1138		return 0;						\
1139	}
1140
1141DEF_READER(proc_read_8, 8)
1142DEF_READER(proc_read_16, 16)
1143DEF_READER(proc_read_32, 32)
1144DEF_READER(proc_read_64, 64)
1145
1146#undef DEF_READER
1147