proc.c revision 8ead1cde21252e779f29ee4bcd68ae4a1bc86ed9
1#include "config.h"
2
3#if defined(HAVE_LIBUNWIND)
4#include <libunwind.h>
5#include <libunwind-ptrace.h>
6#endif /* defined(HAVE_LIBUNWIND) */
7
8#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
13#include <assert.h>
14#include <error.h>
15
16#include "common.h"
17#include "breakpoint.h"
18#include "proc.h"
19
20#ifndef ARCH_HAVE_PROCESS_DATA
21int
22arch_process_init(struct Process *proc)
23{
24	return 0;
25}
26
27void
28arch_process_destroy(struct Process *proc)
29{
30}
31
32int
33arch_process_clone(struct Process *retp, struct Process *proc)
34{
35	return 0;
36}
37
38int
39arch_process_exec(struct Process *proc)
40{
41	return 0;
42}
43#endif
44
45#ifndef ARCH_HAVE_DYNLINK_DONE
46void
47arch_dynlink_done(struct Process *proc)
48{
49}
50#endif
51
52static void add_process(struct Process *proc, int was_exec);
53
54static int
55process_bare_init(struct Process *proc, const char *filename,
56		  pid_t pid, int was_exec)
57{
58	if (!was_exec) {
59		memset(proc, 0, sizeof(*proc));
60
61		proc->filename = strdup(filename);
62		if (proc->filename == NULL) {
63		fail:
64			free(proc->filename);
65			if (proc->breakpoints != NULL)
66				dict_clear(proc->breakpoints);
67			return -1;
68		}
69	}
70
71	/* Add process so that we know who the leader is.  */
72	proc->pid = pid;
73	add_process(proc, was_exec);
74	if (proc->leader == NULL)
75		goto fail;
76
77	if (proc->leader == proc) {
78		proc->breakpoints = dict_init(target_address_hash,
79					      target_address_cmp);
80		if (proc->breakpoints == NULL)
81			goto fail;
82	} else {
83		proc->breakpoints = NULL;
84	}
85
86#if defined(HAVE_LIBUNWIND)
87	proc->unwind_priv = _UPT_create(pid);
88	proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
89#endif /* defined(HAVE_LIBUNWIND) */
90
91	return 0;
92}
93
94static void
95process_bare_destroy(struct Process *proc, int was_exec)
96{
97	dict_clear(proc->breakpoints);
98	if (!was_exec) {
99		free(proc->filename);
100		remove_process(proc);
101	}
102}
103
104static int
105process_init_main(struct Process *proc)
106{
107	target_address_t entry;
108	target_address_t interp_bias;
109	if (process_get_entry(proc, &entry, &interp_bias) < 0) {
110		fprintf(stderr, "Couldn't get entry points of process %d\n",
111			proc->pid);
112		return -1;
113	}
114
115	if (breakpoints_init(proc) < 0) {
116		fprintf(stderr, "failed to init breakpoints %d\n",
117			proc->pid);
118		return -1;
119	}
120
121	return 0;
122}
123
124int
125process_init(struct Process *proc, const char *filename, pid_t pid)
126{
127	if (process_bare_init(proc, filename, pid, 0) < 0) {
128	fail:
129		error(0, errno, "init process %d", pid);
130		return -1;
131	}
132
133	if (arch_process_init(proc) < 0) {
134		process_bare_destroy(proc, 0);
135		goto fail;
136	}
137
138	if (proc->leader != proc)
139		return 0;
140	if (process_init_main(proc) < 0) {
141		process_bare_destroy(proc, 0);
142		goto fail;
143	}
144	return 0;
145}
146
147static enum callback_status
148destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
149{
150	breakpoint_destroy(bp);
151	free(bp);
152	return CBS_CONT;
153}
154
155static void
156private_process_destroy(struct Process *proc, int keep_filename)
157{
158	if (!keep_filename)
159		free(proc->filename);
160
161	/* Libraries and symbols.  This is only relevant in
162	 * leader.  */
163	struct library *lib;
164	for (lib = proc->libraries; lib != NULL; ) {
165		struct library *next = lib->next;
166		library_destroy(lib);
167		free(lib);
168		lib = next;
169	}
170	proc->libraries = NULL;
171
172	/* Breakpoints.  */
173	if (proc->breakpoints != NULL) {
174		proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
175		dict_clear(proc->breakpoints);
176		proc->breakpoints = NULL;
177	}
178}
179
180void
181process_destroy(struct Process *proc)
182{
183	private_process_destroy(proc, 0);
184	arch_process_destroy(proc);
185}
186
187int
188process_exec(struct Process *proc)
189{
190	/* Call exec first, before we destroy the main state.  */
191	if (arch_process_exec(proc) < 0)
192		return -1;
193
194	private_process_destroy(proc, 1);
195	if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
196		return -1;
197	if (process_init_main(proc) < 0) {
198		process_bare_destroy(proc, 1);
199		return -1;
200	}
201	return 0;
202}
203
204struct Process *
205open_program(const char *filename, pid_t pid)
206{
207	assert(pid != 0);
208	struct Process *proc = malloc(sizeof(*proc));
209	if (proc == NULL || process_init(proc, filename, pid) < 0) {
210		free(proc);
211		return NULL;
212	}
213	return proc;
214}
215
216struct clone_single_bp_data {
217	struct Process *old_proc;
218	struct Process *new_proc;
219	int error;
220};
221
222static void
223clone_single_bp(void *key, void *value, void *u)
224{
225	struct breakpoint *bp = value;
226	struct clone_single_bp_data *data = u;
227
228	data->error = 0;
229	struct breakpoint *clone = malloc(sizeof(*clone));
230	if (clone == NULL
231	    || breakpoint_clone(clone, data->new_proc,
232				bp, data->old_proc) < 0) {
233	fail:
234		free(clone);
235		data->error = -1;
236	}
237	if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
238		breakpoint_destroy(clone);
239		goto fail;
240	}
241}
242
243int
244process_clone(struct Process *retp, struct Process *proc, pid_t pid)
245{
246	if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
247	fail:
248		error(0, errno, "clone process %d->%d", proc->pid, pid);
249		return -1;
250	}
251
252	retp->tracesysgood = proc->tracesysgood;
253	retp->e_machine = proc->e_machine;
254
255	/* For non-leader processes, that's all we need to do.  */
256	if (retp->leader != retp)
257		return 0;
258
259	/* Clone symbols first so that we can clone and relink
260	 * breakpoints.  */
261	struct library *lib;
262	struct library **nlibp = &retp->libraries;
263	for (lib = proc->libraries; lib != NULL; lib = lib->next) {
264		*nlibp = malloc(sizeof(**nlibp));
265		if (*nlibp == NULL
266		    || library_clone(*nlibp, lib) < 0) {
267		fail2:
268			process_bare_destroy(retp, 0);
269
270			/* Error when cloning.  Unroll what was done.  */
271			for (lib = retp->libraries; lib != NULL; ) {
272				struct library *next = lib->next;
273				library_destroy(lib);
274				free(lib);
275				lib = next;
276			}
277			goto fail;
278		}
279
280		nlibp = &(*nlibp)->next;
281	}
282
283	/* Now clone breakpoints.  Symbol relinking is done in
284	 * clone_single_bp.  */
285	struct clone_single_bp_data data = {
286		.old_proc = proc,
287		.new_proc = retp,
288		.error = 0,
289	};
290	dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
291
292	/* And finally the call stack.  */
293	memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
294	retp->callstack_depth = proc->callstack_depth;
295
296	if (data.error < 0)
297		goto fail2;
298
299	if (arch_process_clone(retp, proc) < 0)
300		goto fail2;
301
302	return 0;
303}
304
305static int
306open_one_pid(pid_t pid)
307{
308	Process *proc;
309	char *filename;
310	debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
311
312	/* Get the filename first.  Should the trace_pid fail, we can
313	 * easily free it, untracing is more work.  */
314	if ((filename = pid2name(pid)) == NULL
315	    || trace_pid(pid) < 0) {
316		free(filename);
317		return -1;
318	}
319
320	proc = open_program(filename, pid);
321	if (proc == NULL)
322		return -1;
323	trace_set_options(proc);
324
325	return 0;
326}
327
328static enum callback_status
329start_one_pid(Process * proc, void * data)
330{
331	continue_process(proc->pid);
332	return CBS_CONT;
333}
334
335void
336open_pid(pid_t pid)
337{
338	debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
339	/* If we are already tracing this guy, we should be seeing all
340	 * his children via normal tracing route.  */
341	if (pid2proc(pid) != NULL)
342		return;
343
344	/* First, see if we can attach the requested PID itself.  */
345	if (open_one_pid(pid)) {
346		fprintf(stderr, "Cannot attach to pid %u: %s\n",
347			pid, strerror(errno));
348		trace_fail_warning(pid);
349		return;
350	}
351
352	/* Now attach to all tasks that belong to that PID.  There's a
353	 * race between process_tasks and open_one_pid.  So when we
354	 * fail in open_one_pid below, we just do another round.
355	 * Chances are that by then that PID will have gone away, and
356	 * that's why we have seen the failure.  The processes that we
357	 * manage to open_one_pid are stopped, so we should eventually
358	 * reach a point where process_tasks doesn't give any new
359	 * processes (because there's nobody left to produce
360	 * them).  */
361	size_t old_ntasks = 0;
362	int have_all;
363	while (1) {
364		pid_t *tasks;
365		size_t ntasks;
366		size_t i;
367
368		if (process_tasks(pid, &tasks, &ntasks) < 0) {
369			fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
370				pid, strerror(errno));
371			break;
372		}
373
374		have_all = 1;
375		for (i = 0; i < ntasks; ++i)
376			if (pid2proc(tasks[i]) == NULL
377			    && open_one_pid(tasks[i]))
378				have_all = 0;
379
380		free(tasks);
381
382		if (have_all && old_ntasks == ntasks)
383			break;
384		old_ntasks = ntasks;
385	}
386
387	struct Process *leader = pid2proc(pid)->leader;
388
389	/* XXX Is there a way to figure out whether _start has
390	 * actually already been hit?  */
391	arch_dynlink_done(leader);
392
393	/* Done.  Continue everyone.  */
394	each_task(leader, NULL, start_one_pid, NULL);
395}
396
397static enum callback_status
398find_proc(Process * proc, void * data)
399{
400	pid_t pid = (pid_t)(uintptr_t)data;
401	return proc->pid == pid ? CBS_STOP : CBS_CONT;
402}
403
404Process *
405pid2proc(pid_t pid) {
406	return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
407}
408
409static Process * list_of_processes = NULL;
410
411static void
412unlist_process(Process * proc)
413{
414	Process *tmp;
415
416	if (list_of_processes == proc) {
417		list_of_processes = list_of_processes->next;
418		return;
419	}
420
421	for (tmp = list_of_processes; ; tmp = tmp->next) {
422		/* If the following assert fails, the process wasn't
423		 * in the list.  */
424		assert(tmp->next != NULL);
425
426		if (tmp->next == proc) {
427			tmp->next = tmp->next->next;
428			return;
429		}
430	}
431}
432
433struct Process *
434each_process(struct Process *start_after,
435	     enum callback_status(*cb)(struct Process *proc, void *data),
436	     void *data)
437{
438	struct Process *it = start_after == NULL ? list_of_processes
439		: start_after->next;
440
441	while (it != NULL) {
442		/* Callback might call remove_process.  */
443		struct Process *next = it->next;
444		switch ((*cb)(it, data)) {
445		case CBS_FAIL:
446			/* XXX handle me */
447		case CBS_STOP:
448			return it;
449		case CBS_CONT:
450			break;
451		}
452		it = next;
453	}
454	return NULL;
455}
456
457Process *
458each_task(struct Process *proc, struct Process *start_after,
459	  enum callback_status(*cb)(struct Process *proc, void *data),
460	  void *data)
461{
462	assert(proc != NULL);
463	struct Process *it = start_after == NULL ? proc->leader
464		: start_after->next;
465
466	if (it != NULL) {
467		struct Process *leader = it->leader;
468		while (it != NULL && it->leader == leader) {
469			/* Callback might call remove_process.  */
470			struct Process *next = it->next;
471			switch ((*cb)(it, data)) {
472			case CBS_FAIL:
473				/* XXX handle me */
474			case CBS_STOP:
475				return it;
476			case CBS_CONT:
477				break;
478			}
479			it = next;
480		}
481	}
482	return NULL;
483}
484
485static void
486add_process(struct Process *proc, int was_exec)
487{
488	Process ** leaderp = &list_of_processes;
489	if (proc->pid) {
490		pid_t tgid = process_leader(proc->pid);
491		if (tgid == 0)
492			/* Must have been terminated before we managed
493			 * to fully attach.  */
494			return;
495		if (tgid == proc->pid)
496			proc->leader = proc;
497		else {
498			Process * leader = pid2proc(tgid);
499			proc->leader = leader;
500			if (leader != NULL)
501				leaderp = &leader->next;
502		}
503	}
504
505	if (!was_exec) {
506		proc->next = *leaderp;
507		*leaderp = proc;
508	}
509}
510
511void
512change_process_leader(Process * proc, Process * leader)
513{
514	Process ** leaderp = &list_of_processes;
515	if (proc->leader == leader)
516		return;
517
518	assert(leader != NULL);
519	unlist_process(proc);
520	if (proc != leader)
521		leaderp = &leader->next;
522
523	proc->leader = leader;
524	proc->next = *leaderp;
525	*leaderp = proc;
526}
527
528static enum callback_status
529clear_leader(struct Process *proc, void *data)
530{
531	debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
532	      proc->pid, proc->leader->pid);
533	proc->leader = NULL;
534	return CBS_CONT;
535}
536
537static enum ecb_status
538event_for_proc(Event * event, void * data)
539{
540	if (event->proc == data)
541		return ecb_deque;
542	else
543		return ecb_cont;
544}
545
546static void
547delete_events_for(Process * proc)
548{
549	Event * event;
550	while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
551		free(event);
552}
553
554void
555remove_process(Process *proc)
556{
557	debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
558
559	if (proc->leader == proc)
560		each_task(proc, NULL, &clear_leader, NULL);
561
562	unlist_process(proc);
563	delete_events_for(proc);
564	process_destroy(proc);
565	free(proc);
566}
567
568void
569install_event_handler(Process *proc, struct event_handler *handler)
570{
571	debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
572	assert(proc->event_handler == NULL);
573	proc->event_handler = handler;
574}
575
576void
577destroy_event_handler(Process * proc)
578{
579	struct event_handler *handler = proc->event_handler;
580	debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
581	assert(handler != NULL);
582	if (handler->destroy != NULL)
583		handler->destroy(handler);
584	free(handler);
585	proc->event_handler = NULL;
586}
587
588static enum callback_status
589breakpoint_for_symbol(struct library_symbol *libsym, void *data)
590{
591	struct Process *proc = data;
592	assert(proc->leader == proc);
593
594	/* If there is an artificial breakpoint on the same address,
595	 * its libsym will be NULL, and we can smuggle our libsym
596	 * there.  That artificial breakpoint is there presumably for
597	 * the callbacks, which we don't touch.  If there is a real
598	 * breakpoint, then this is a bug.  ltrace-elf.c should filter
599	 * symbols and ignore extra symbol aliases.
600	 *
601	 * The other direction is more complicated and currently not
602	 * supported.  If a breakpoint has custom callbacks, it might
603	 * be also custom-allocated, and we would really need to swap
604	 * the two: delete the one now in the dictionary, swap values
605	 * around, and put the new breakpoint back in.  */
606	struct breakpoint *bp = dict_find_entry(proc->breakpoints,
607						libsym->enter_addr);
608	if (bp != NULL) {
609		assert(bp->libsym == NULL);
610		bp->libsym = libsym;
611		return CBS_CONT;
612	}
613
614	bp = malloc(sizeof(*bp));
615	if (bp == NULL
616	    || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
617	fail:
618		free(bp);
619		return CBS_FAIL;
620	}
621	if (proc_add_breakpoint(proc, bp) < 0) {
622		breakpoint_destroy(bp);
623		goto fail;
624	}
625
626	if (breakpoint_turn_on(bp, proc) < 0) {
627		proc_remove_breakpoint(proc, bp);
628		breakpoint_destroy(bp);
629		goto fail;
630	}
631
632	return CBS_CONT;
633}
634
635void
636proc_add_library(struct Process *proc, struct library *lib)
637{
638	assert(lib->next == NULL);
639	lib->next = proc->libraries;
640	proc->libraries = lib;
641	debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
642	      lib->soname, lib->base, lib->pathname, proc->pid);
643
644	struct library_symbol *libsym = NULL;
645	while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
646					     proc)) != NULL)
647		error(0, errno, "insert breakpoint for %s", libsym->name);
648}
649
650int
651proc_remove_library(struct Process *proc, struct library *lib)
652{
653	struct library **libp;
654	for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
655		if (*libp == lib) {
656			*libp = lib->next;
657			return 0;
658		}
659	return -1;
660}
661
662struct library *
663proc_each_library(struct Process *proc, struct library *it,
664		  enum callback_status (*cb)(struct Process *proc,
665					     struct library *lib, void *data),
666		  void *data)
667{
668	if (it == NULL)
669		it = proc->libraries;
670
671	while (it != NULL) {
672		struct library *next = it->next;
673
674		switch (cb(proc, it, data)) {
675		case CBS_FAIL:
676			/* XXX handle me */
677		case CBS_STOP:
678			return it;
679		case CBS_CONT:
680			break;
681		}
682
683		it = next;
684	}
685
686	return NULL;
687}
688
689static void
690check_leader(struct Process *proc)
691{
692	/* Only the group leader should be getting the breakpoints and
693	 * thus have ->breakpoint initialized.  */
694	assert(proc->leader != NULL);
695	assert(proc->leader == proc);
696	assert(proc->breakpoints != NULL);
697}
698
699int
700proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
701{
702	debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
703	      proc->pid, breakpoint_name(bp), bp->addr);
704	check_leader(proc);
705
706	/* XXX We might merge bp->libsym instead of the following
707	 * assert, but that's not necessary right now.  Read the
708	 * comment in breakpoint_for_symbol.  */
709	assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
710
711	if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
712		error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
713		      breakpoint_name(bp), bp->addr);
714		return -1;
715	}
716
717	return 0;
718}
719
720void
721proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
722{
723	debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
724	      proc->pid, breakpoint_name(bp), bp->addr);
725	check_leader(proc);
726	struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
727	assert(removed == bp);
728}
729
730/* Dict doesn't support iteration restarts, so here's this contraption
731 * for now.  XXX add restarts to dict.  */
732struct each_breakpoint_data
733{
734	void *start;
735	void *end;
736	struct Process *proc;
737	enum callback_status (*cb)(struct Process *proc,
738				   struct breakpoint *bp,
739				   void *data);
740	void *cb_data;
741};
742
743static void
744each_breakpoint_cb(void *key, void *value, void *d)
745{
746	struct each_breakpoint_data *data = d;
747	if (data->end != NULL)
748		return;
749	if (data->start == key)
750		data->start = NULL;
751
752	if (data->start == NULL) {
753		switch (data->cb(data->proc, value, data->cb_data)) {
754		case CBS_FAIL:
755			/* XXX handle me */
756		case CBS_STOP:
757			data->end = key;
758		case CBS_CONT:
759			return;
760		}
761	}
762}
763
764void *
765proc_each_breakpoint(struct Process *proc, void *start,
766		     enum callback_status (*cb)(struct Process *proc,
767						struct breakpoint *bp,
768						void *data), void *data)
769{
770	struct each_breakpoint_data dd = {
771		.start = start,
772		.proc = proc,
773		.cb = cb,
774		.cb_data = data,
775	};
776	dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
777	return dd.end;
778}
779