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