proc.c revision a2c270e86913ab93c41cdd61055d7c2b71b10fa1
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 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	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, bp) < 0) {
340	fail:
341		free(clone);
342		return CBS_STOP;
343	}
344	if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
345		breakpoint_destroy(clone);
346		goto fail;
347	}
348	return CBS_CONT;
349}
350
351int
352process_clone(struct process *retp, struct process *proc, pid_t pid)
353{
354	if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
355	fail1:
356		fprintf(stderr, "Failed to clone process %d to %d: %s\n",
357			proc->pid, pid, strerror(errno));
358		return -1;
359	}
360
361	retp->tracesysgood = proc->tracesysgood;
362	retp->e_machine = proc->e_machine;
363	retp->e_class = proc->e_class;
364
365	/* For non-leader processes, that's all we need to do.  */
366	if (retp->leader != retp)
367		return 0;
368
369	/* Clone symbols first so that we can clone and relink
370	 * breakpoints.  */
371	struct library *lib;
372	struct library **nlibp = &retp->libraries;
373	for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
374		*nlibp = malloc(sizeof(**nlibp));
375
376		if (*nlibp == NULL
377		    || library_clone(*nlibp, lib) < 0) {
378			free(*nlibp);
379			*nlibp = NULL;
380
381		fail2:
382			process_bare_destroy(retp, 0);
383
384			/* Error when cloning.  Unroll what was done.  */
385			for (lib = retp->libraries; lib != NULL; ) {
386				struct library *next = lib->next;
387				library_destroy(lib);
388				free(lib);
389				lib = next;
390			}
391			goto fail1;
392		}
393
394		nlibp = &(*nlibp)->next;
395	}
396
397	/* Now clone breakpoints.  Symbol relinking is done in
398	 * clone_single_bp.  */
399	struct clone_single_bp_data data = {
400		.old_proc = proc,
401		.new_proc = retp,
402	};
403	if (DICT_EACH(proc->leader->breakpoints,
404		      arch_addr_t, struct breakpoint *, NULL,
405		      clone_single_bp, &data) != NULL)
406		goto fail2;
407
408	/* And finally the call stack.  */
409	/* XXX clearly the callstack handling should be moved to a
410	 * separate module and this whole business extracted to
411	 * callstack_clone, or callstack_element_clone.  */
412	memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
413	retp->callstack_depth = proc->callstack_depth;
414
415	size_t i;
416	for (i = 0; i < retp->callstack_depth; ++i) {
417		struct callstack_element *elem = &retp->callstack[i];
418		struct fetch_context *ctx = elem->fetch_context;
419		if (ctx != NULL) {
420			struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
421			if (nctx == NULL) {
422				size_t j;
423			fail3:
424				for (j = 0; j < i; ++j) {
425					nctx = retp->callstack[j].fetch_context;
426					fetch_arg_done(nctx);
427					elem->fetch_context = NULL;
428				}
429				goto fail2;
430			}
431			elem->fetch_context = nctx;
432		}
433
434		if (elem->arguments != NULL) {
435			struct value_dict *nargs = malloc(sizeof(*nargs));
436			if (nargs == NULL
437			    || val_dict_clone(nargs, elem->arguments) < 0) {
438				size_t j;
439				for (j = 0; j < i; ++j) {
440					nargs = retp->callstack[j].arguments;
441					val_dict_destroy(nargs);
442					free(nargs);
443					elem->arguments = NULL;
444				}
445
446				/* Pretend that this round went well,
447				 * so that fail3 frees I-th
448				 * fetch_context.  */
449				++i;
450				goto fail3;
451			}
452			elem->arguments = nargs;
453		}
454
455		/* If it's not a syscall, we need to find the
456		 * corresponding library symbol in the cloned
457		 * library.  */
458		if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
459			struct library_symbol *libfunc = elem->c_un.libfunc;
460			int rc = proc_find_symbol(retp, libfunc,
461						  NULL, &elem->c_un.libfunc);
462			assert(rc == 0);
463		}
464	}
465
466	/* At this point, retp is fully initialized, except for OS and
467	 * arch parts, and we can call private_process_destroy.  */
468	if (os_process_clone(retp, proc) < 0) {
469		private_process_destroy(retp, 0);
470		return -1;
471	}
472	if (arch_process_clone(retp, proc) < 0) {
473		os_process_destroy(retp);
474		private_process_destroy(retp, 0);
475		return -1;
476	}
477
478	return 0;
479}
480
481static int
482open_one_pid(pid_t pid)
483{
484	debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
485
486	/* Get the filename first.  Should the trace_pid fail, we can
487	 * easily free it, untracing is more work.  */
488	char *filename = pid2name(pid);
489	if (filename == NULL || trace_pid(pid) < 0) {
490	fail:
491		free(filename);
492		return -1;
493	}
494
495	struct process *proc = open_program(filename, pid);
496	if (proc == NULL)
497		goto fail;
498	free(filename);
499	trace_set_options(proc);
500
501	return 0;
502}
503
504static enum callback_status
505start_one_pid(struct process *proc, void *data)
506{
507	continue_process(proc->pid);
508	return CBS_CONT;
509}
510
511void
512open_pid(pid_t pid)
513{
514	debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
515	/* If we are already tracing this guy, we should be seeing all
516	 * his children via normal tracing route.  */
517	if (pid2proc(pid) != NULL)
518		return;
519
520	/* First, see if we can attach the requested PID itself.  */
521	if (open_one_pid(pid)) {
522		fprintf(stderr, "Cannot attach to pid %u: %s\n",
523			pid, strerror(errno));
524		trace_fail_warning(pid);
525		return;
526	}
527
528	/* Now attach to all tasks that belong to that PID.  There's a
529	 * race between process_tasks and open_one_pid.  So when we
530	 * fail in open_one_pid below, we just do another round.
531	 * Chances are that by then that PID will have gone away, and
532	 * that's why we have seen the failure.  The processes that we
533	 * manage to open_one_pid are stopped, so we should eventually
534	 * reach a point where process_tasks doesn't give any new
535	 * processes (because there's nobody left to produce
536	 * them).  */
537	size_t old_ntasks = 0;
538	int have_all;
539	while (1) {
540		pid_t *tasks;
541		size_t ntasks;
542		size_t i;
543
544		if (process_tasks(pid, &tasks, &ntasks) < 0) {
545			fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
546				pid, strerror(errno));
547			break;
548		}
549
550		have_all = 1;
551		for (i = 0; i < ntasks; ++i)
552			if (pid2proc(tasks[i]) == NULL
553			    && open_one_pid(tasks[i]))
554				have_all = 0;
555
556		free(tasks);
557
558		if (have_all && old_ntasks == ntasks)
559			break;
560		old_ntasks = ntasks;
561	}
562
563	struct process *leader = pid2proc(pid)->leader;
564
565	/* XXX Is there a way to figure out whether _start has
566	 * actually already been hit?  */
567	arch_dynlink_done(leader);
568
569	/* Done.  Continue everyone.  */
570	each_task(leader, NULL, start_one_pid, NULL);
571}
572
573static enum callback_status
574find_proc(struct process *proc, void *data)
575{
576	pid_t pid = (pid_t)(uintptr_t)data;
577	return proc->pid == pid ? CBS_STOP : CBS_CONT;
578}
579
580struct process *
581pid2proc(pid_t pid)
582{
583	return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
584}
585
586static struct process *list_of_processes = NULL;
587
588static void
589unlist_process(struct process *proc)
590{
591	if (list_of_processes == proc) {
592		list_of_processes = list_of_processes->next;
593		return;
594	}
595
596	struct process *tmp;
597	for (tmp = list_of_processes; ; tmp = tmp->next) {
598		/* If the following assert fails, the process wasn't
599		 * in the list.  */
600		assert(tmp->next != NULL);
601
602		if (tmp->next == proc) {
603			tmp->next = tmp->next->next;
604			return;
605		}
606	}
607}
608
609struct process *
610each_process(struct process *start_after,
611	     enum callback_status(*cb)(struct process *proc, void *data),
612	     void *data)
613{
614	struct process *it = start_after == NULL ? list_of_processes
615		: start_after->next;
616
617	while (it != NULL) {
618		/* Callback might call remove_process.  */
619		struct process *next = it->next;
620		switch ((*cb)(it, data)) {
621		case CBS_FAIL:
622			/* XXX handle me */
623		case CBS_STOP:
624			return it;
625		case CBS_CONT:
626			break;
627		}
628		it = next;
629	}
630	return NULL;
631}
632
633struct process *
634each_task(struct process *proc, struct process *start_after,
635	  enum callback_status(*cb)(struct process *proc, void *data),
636	  void *data)
637{
638	assert(proc != NULL);
639	struct process *it = start_after == NULL ? proc->leader
640		: start_after->next;
641
642	if (it != NULL) {
643		struct process *leader = it->leader;
644		while (it != NULL && it->leader == leader) {
645			/* Callback might call remove_process.  */
646			struct process *next = it->next;
647			switch ((*cb)(it, data)) {
648			case CBS_FAIL:
649				/* XXX handle me */
650			case CBS_STOP:
651				return it;
652			case CBS_CONT:
653				break;
654			}
655			it = next;
656		}
657	}
658	return NULL;
659}
660
661static int
662add_process(struct process *proc, int was_exec)
663{
664	struct process **leaderp = &list_of_processes;
665	if (proc->pid) {
666		pid_t tgid = process_leader(proc->pid);
667		if (tgid == 0)
668			/* Must have been terminated before we managed
669			 * to fully attach.  */
670			return -1;
671		if (tgid == proc->pid) {
672			proc->leader = proc;
673		} else {
674			struct process *leader = pid2proc(tgid);
675			proc->leader = leader;
676			if (leader != NULL)
677				leaderp = &leader->next;
678		}
679	}
680
681	if (!was_exec) {
682		proc->next = *leaderp;
683		*leaderp = proc;
684	}
685	return 0;
686}
687
688void
689change_process_leader(struct process *proc, struct process *leader)
690{
691	struct process **leaderp = &list_of_processes;
692	if (proc->leader == leader)
693		return;
694
695	assert(leader != NULL);
696	unlist_process(proc);
697	if (proc != leader)
698		leaderp = &leader->next;
699
700	proc->leader = leader;
701	proc->next = *leaderp;
702	*leaderp = proc;
703}
704
705static enum callback_status
706clear_leader(struct process *proc, void *data)
707{
708	debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
709	      proc->pid, proc->leader->pid);
710	proc->leader = NULL;
711	return CBS_CONT;
712}
713
714void
715remove_process(struct process *proc)
716{
717	debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
718
719	if (proc->leader == proc)
720		each_task(proc, NULL, &clear_leader, NULL);
721
722	unlist_process(proc);
723	process_removed(proc);
724	process_destroy(proc);
725	free(proc);
726}
727
728void
729install_event_handler(struct process *proc, struct event_handler *handler)
730{
731	debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
732	assert(proc->event_handler == NULL);
733	proc->event_handler = handler;
734}
735
736void
737destroy_event_handler(struct process *proc)
738{
739	struct event_handler *handler = proc->event_handler;
740	debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
741	assert(handler != NULL);
742	if (handler->destroy != NULL)
743		handler->destroy(handler);
744	free(handler);
745	proc->event_handler = NULL;
746}
747
748static int
749breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
750{
751	arch_addr_t bp_addr;
752	assert(proc->leader == proc);
753
754	/* Don't enable latent or delayed symbols.  */
755	if (libsym->latent || libsym->delayed) {
756		debug(DEBUG_FUNCTION,
757		      "delayed and/or latent breakpoint pid=%d, %s@%p",
758		      proc->pid, libsym->name, libsym->enter_addr);
759		return 0;
760	}
761
762	bp_addr = sym2addr(proc, libsym);
763
764	/* If there is an artificial breakpoint on the same address,
765	 * its libsym will be NULL, and we can smuggle our libsym
766	 * there.  That artificial breakpoint is there presumably for
767	 * the callbacks, which we don't touch.  If there is a real
768	 * breakpoint, then this is a bug.  ltrace-elf.c should filter
769	 * symbols and ignore extra symbol aliases.
770	 *
771	 * The other direction is more complicated and currently not
772	 * supported.  If a breakpoint has custom callbacks, it might
773	 * be also custom-allocated, and we would really need to swap
774	 * the two: delete the one now in the dictionary, swap values
775	 * around, and put the new breakpoint back in.  */
776	struct breakpoint *bp;
777	if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
778		/* MIPS backend makes duplicate requests.  This is
779		 * likely a bug in the backend.  Currently there's no
780		 * point assigning more than one symbol to a
781		 * breakpoint, because when it hits, we won't know
782		 * what to print out.  But it's easier to fix it here
783		 * before someone who understands MIPS has the time to
784		 * look into it.  So turn the sanity check off on
785		 * MIPS.  References:
786		 *
787		 *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
788		 *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
789		 */
790#ifndef __mips__
791		assert(bp->libsym == NULL);
792		bp->libsym = libsym;
793#endif
794		return 0;
795	}
796
797	bp = malloc(sizeof(*bp));
798	if (bp == NULL
799	    || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
800	fail:
801		free(bp);
802		return -1;
803	}
804	if (proc_add_breakpoint(proc, bp) < 0) {
805		breakpoint_destroy(bp);
806		goto fail;
807	}
808
809	if (breakpoint_turn_on(bp, proc) < 0) {
810		proc_remove_breakpoint(proc, bp);
811		breakpoint_destroy(bp);
812		goto fail;
813	}
814
815	return 0;
816}
817
818static enum callback_status
819cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
820{
821	return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
822}
823
824static int
825proc_activate_latent_symbol(struct process *proc,
826			    struct library_symbol *libsym)
827{
828	assert(libsym->latent);
829	libsym->latent = 0;
830	debug(DEBUG_FUNCTION, "activated latent symbol");
831	return breakpoint_for_symbol(libsym, proc);
832}
833
834int
835proc_activate_delayed_symbol(struct process *proc,
836			     struct library_symbol *libsym)
837{
838	assert(libsym->delayed);
839	libsym->delayed = 0;
840	debug(DEBUG_FUNCTION, "activated delayed symbol");
841	return breakpoint_for_symbol(libsym, proc);
842}
843
844static enum callback_status
845activate_latent_in(struct process *proc, struct library *lib, void *data)
846{
847	struct library_exported_name *exported;
848	for (exported = data; exported != NULL; exported = exported->next) {
849		struct library_symbol *libsym = NULL;
850		while ((libsym = library_each_symbol(lib, libsym,
851						     library_symbol_named_cb,
852						     (void *)exported->name))
853		       != NULL)
854			if (libsym->latent
855			    && proc_activate_latent_symbol(proc, libsym) < 0)
856				return CBS_FAIL;
857	}
858	return CBS_CONT;
859}
860
861void
862proc_add_library(struct process *proc, struct library *lib)
863{
864	assert(lib->next == NULL);
865	lib->next = proc->libraries;
866	proc->libraries = lib;
867	debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
868	      lib->soname, lib->base, lib->pathname, proc->pid);
869
870	/* Insert breakpoints for all active (non-latent) symbols.  */
871	struct library_symbol *libsym = NULL;
872	while ((libsym = library_each_symbol(lib, libsym,
873					     cb_breakpoint_for_symbol,
874					     proc)) != NULL)
875		fprintf(stderr,
876			"Couldn't insert breakpoint for %s to %d: %s.\n",
877			libsym->name, proc->pid, strerror(errno));
878
879	/* Look through export list of the new library and compare it
880	 * with latent symbols of all libraries (including this
881	 * library itself).  */
882	struct library *lib2 = NULL;
883	while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
884					 lib->exported_names)) != NULL)
885		fprintf(stderr,
886			"Couldn't activate latent symbols for %s in %d: %s.\n",
887			lib2->soname, proc->pid, strerror(errno));
888}
889
890int
891proc_remove_library(struct process *proc, struct library *lib)
892{
893	struct library **libp;
894	for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
895		if (*libp == lib) {
896			*libp = lib->next;
897			return 0;
898		}
899	return -1;
900}
901
902struct library *
903proc_each_library(struct process *proc, struct library *it,
904		  enum callback_status (*cb)(struct process *proc,
905					     struct library *lib, void *data),
906		  void *data)
907{
908	if (it == NULL)
909		it = proc->libraries;
910	else
911		it = it->next;
912
913	while (it != NULL) {
914		struct library *next = it->next;
915
916		switch (cb(proc, it, data)) {
917		case CBS_FAIL:
918			/* XXX handle me */
919		case CBS_STOP:
920			return it;
921		case CBS_CONT:
922			break;
923		}
924
925		it = next;
926	}
927
928	return NULL;
929}
930
931static void
932check_leader(struct process *proc)
933{
934	/* Only the group leader should be getting the breakpoints and
935	 * thus have ->breakpoint initialized.  */
936	assert(proc->leader != NULL);
937	assert(proc->leader == proc);
938	assert(proc->breakpoints != NULL);
939}
940
941int
942proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
943{
944	debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
945	      proc->pid, breakpoint_name(bp), bp->addr);
946	check_leader(proc);
947
948	/* XXX We might merge bp->libsym instead of the following
949	 * assert, but that's not necessary right now.  Read the
950	 * comment in breakpoint_for_symbol.  */
951	assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
952
953	if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
954		fprintf(stderr,
955			"couldn't enter breakpoint %s@%p to dictionary: %s\n",
956			breakpoint_name(bp), bp->addr, strerror(errno));
957		return -1;
958	}
959
960	return 0;
961}
962
963void
964proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
965{
966	debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
967	      proc->pid, breakpoint_name(bp), bp->addr);
968	check_leader(proc);
969	int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
970			    NULL, NULL, NULL);
971	assert(rc == 0);
972}
973
974struct each_breakpoint_data
975{
976	struct process *proc;
977	enum callback_status (*cb)(struct process *proc,
978				   struct breakpoint *bp,
979				   void *data);
980	void *cb_data;
981};
982
983static enum callback_status
984each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
985{
986	struct each_breakpoint_data *data = d;
987	return data->cb(data->proc, *bpp, data->cb_data);
988}
989
990void *
991proc_each_breakpoint(struct process *proc, void *start,
992		     enum callback_status (*cb)(struct process *proc,
993						struct breakpoint *bp,
994						void *data), void *data)
995{
996	struct each_breakpoint_data dd = {
997		.proc = proc,
998		.cb = cb,
999		.cb_data = data,
1000	};
1001	return DICT_EACH(proc->breakpoints,
1002			 arch_addr_t, struct breakpoint *, start,
1003			 &each_breakpoint_cb, &dd);
1004}
1005
1006int
1007proc_find_symbol(struct process *proc, struct library_symbol *sym,
1008		 struct library **retlib, struct library_symbol **retsym)
1009{
1010	struct library *lib = sym->lib;
1011	assert(lib != NULL);
1012
1013	struct library *flib
1014		= proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1015	if (flib == NULL)
1016		return -1;
1017
1018	struct library_symbol *fsym
1019		= library_each_symbol(flib, NULL, library_symbol_named_cb,
1020				      (char *)sym->name);
1021	if (fsym == NULL)
1022		return -1;
1023
1024	if (retlib != NULL)
1025		*retlib = flib;
1026	if (retsym != NULL)
1027		*retsym = fsym;
1028
1029	return 0;
1030}
1031
1032struct library_symbol *
1033proc_each_symbol(struct process *proc, struct library_symbol *start_after,
1034		 enum callback_status (*cb)(struct library_symbol *, void *),
1035		 void *data)
1036{
1037	struct library *lib;
1038	for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1039	     lib != NULL; lib = lib->next) {
1040		start_after = library_each_symbol(lib, start_after, cb, data);
1041		if (start_after != NULL)
1042			return start_after;
1043	}
1044
1045	return NULL;
1046}
1047
1048#define DEF_READER(NAME, SIZE)						\
1049	int								\
1050	NAME(struct process *proc, arch_addr_t addr,			\
1051	     uint##SIZE##_t *lp)					\
1052	{								\
1053		union {							\
1054			uint##SIZE##_t dst;				\
1055			char buf[0];					\
1056		} u;							\
1057		if (umovebytes(proc, addr, &u.buf, sizeof(u.dst))	\
1058		    != sizeof(u.dst))					\
1059			return -1;					\
1060		*lp = u.dst;						\
1061		return 0;						\
1062	}
1063
1064DEF_READER(proc_read_8, 8)
1065DEF_READER(proc_read_16, 16)
1066DEF_READER(proc_read_32, 32)
1067DEF_READER(proc_read_64, 64)
1068
1069#undef DEF_READER
1070