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