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