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