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