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