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