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