proc.c revision 2b46cfc1127d390eddd9593fe5ce5399c1f68130
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 int
21process_bare_init(struct Process *proc, const char *filename, pid_t pid)
22{
23	fprintf(stderr, "process_bare_init %s %d\n", filename, pid);
24	memset(proc, 0, sizeof(*proc));
25
26	proc->filename = strdup(filename);
27	if (proc->filename == NULL) {
28	fail:
29		free(proc->filename);
30		if (proc->breakpoints != NULL)
31			dict_clear(proc->breakpoints);
32		return -1;
33	}
34
35	/* Add process so that we know who the leader is.  */
36	proc->pid = pid;
37	add_process(proc);
38	if (proc->leader == NULL)
39		goto fail;
40
41	if (proc->leader == proc) {
42		proc->breakpoints = dict_init(dict_key2hash_int,
43					      dict_key_cmp_int);
44		if (proc->breakpoints == NULL)
45			goto fail;
46	} else {
47		proc->breakpoints = NULL;
48	}
49
50#if defined(HAVE_LIBUNWIND)
51	proc->unwind_priv = _UPT_create(pid);
52	proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
53#endif /* defined(HAVE_LIBUNWIND) */
54
55	return 0;
56}
57
58static void
59process_bare_destroy(struct Process *proc)
60{
61	free(proc->filename);
62	dict_clear(proc->breakpoints);
63	remove_process(proc);
64}
65
66int
67process_init(struct Process *proc, const char *filename, pid_t pid, int enable)
68{
69	fprintf(stderr, "process_init %s %d enable=%d\n", filename, pid, enable);
70	if (process_bare_init(proc, filename, pid) < 0) {
71		error(0, errno, "init process %d", pid);
72		return -1;
73	}
74
75	if (proc->leader == proc && breakpoints_init(proc, enable) < 0) {
76		fprintf(stderr, "failed to init breakpoints %d\n",
77			proc->pid);
78		process_bare_destroy(proc);
79		return -1;
80	}
81
82	return 0;
83}
84
85struct Process *
86open_program(const char *filename, pid_t pid, int enable)
87{
88	fprintf(stderr, "open_program %s %d enable=%d\n",
89		filename, pid, enable);
90	assert(pid != 0);
91	struct Process *proc = malloc(sizeof(*proc));
92	if (proc == NULL
93	    || process_init(proc, filename, pid, enable) < 0) {
94		free(proc);
95		return NULL;
96	}
97	return proc;
98}
99
100struct clone_single_bp_data {
101	struct Process *old_proc;
102	struct Process *new_proc;
103	int error;
104};
105
106struct find_symbol_data {
107	struct library_symbol *old_libsym;
108	struct library_symbol *found_libsym;
109};
110
111static enum callback_status
112find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
113{
114	struct find_symbol_data *fs = u;
115	fs->found_libsym
116		= library_each_symbol(lib, NULL, library_symbol_equal_cb,
117				      fs->old_libsym);
118	return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
119}
120
121static void
122clone_single_bp(void *key, void *value, void *u)
123{
124	target_address_t addr = (target_address_t)key;
125	struct breakpoint *bp = value;
126	struct clone_single_bp_data *data = u;
127
128	/* Find library and symbol that this symbol was linked to.  */
129	struct library_symbol *libsym = bp->libsym;
130	struct library *lib = NULL;
131	if (libsym != NULL) {
132		struct find_symbol_data f_data = {
133			.old_libsym = libsym,
134		};
135		lib = proc_each_library(data->old_proc, NULL,
136					find_sym_in_lib, &f_data);
137		assert(lib != NULL);
138		libsym = f_data.found_libsym;
139	}
140
141	/* LIB and LIBSYM now hold the new library and symbol that
142	 * correspond to the original breakpoint.  Now we can do the
143	 * clone itself.  */
144	struct breakpoint *clone = malloc(sizeof(*clone));
145	if (clone == NULL
146	    || breakpoint_init(clone, data->new_proc, addr,
147			       libsym, bp->cbs) < 0) {
148		data->error = -1;
149		return;
150	}
151}
152
153int
154process_clone(struct Process *retp, struct Process *proc, pid_t pid)
155{
156	if (process_bare_init(retp, proc->filename, pid) < 0) {
157	fail:
158		error(0, errno, "clone process %d->%d", proc->pid, pid);
159		return -1;
160	}
161
162	/* For non-leader processes, that's all we need to do.  */
163	if (proc->leader != proc)
164		return 0;
165
166	/* Clone symbols first so that we can clone and relink
167	 * breakpoints.  */
168	struct library *lib;
169	struct library **nlibp = &retp->libraries;
170	for (lib = proc->libraries; lib != NULL; lib = lib->next) {
171		*nlibp = malloc(sizeof(**nlibp));
172		if (*nlibp == NULL
173		    || library_clone(*nlibp, lib) < 0) {
174		fail2:
175			process_bare_destroy(retp);
176
177			/* Error when cloning.  Unroll what was done.  */
178			for (lib = retp->libraries; lib != NULL; ) {
179				struct library *next = lib->next;
180				library_destroy(lib);
181				free(lib);
182				lib = next;
183			}
184			goto fail;
185		}
186
187		nlibp = &(*nlibp)->next;
188	}
189
190	/* Now clone breakpoints.  Symbol relinking is done in
191	 * clone_single_bp.  */
192	struct clone_single_bp_data data = {
193		.old_proc = proc,
194		.new_proc = retp,
195		.error = 0,
196	};
197	dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
198
199	if (data.error < 0)
200		goto fail2;
201
202	return 0;
203}
204
205static int
206open_one_pid(pid_t pid)
207{
208	Process *proc;
209	char *filename;
210	debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
211
212	/* Get the filename first.  Should the trace_pid fail, we can
213	 * easily free it, untracing is more work.  */
214	if ((filename = pid2name(pid)) == NULL
215	    || trace_pid(pid) < 0) {
216		free(filename);
217		return -1;
218	}
219
220	proc = open_program(filename, pid, 0);
221	if (proc == NULL)
222		return -1;
223	trace_set_options(proc, pid);
224
225	return 0;
226}
227
228static enum callback_status
229start_one_pid(Process * proc, void * data)
230{
231	continue_process(proc->pid);
232	return CBS_CONT;
233}
234
235void
236open_pid(pid_t pid)
237{
238	debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
239	/* If we are already tracing this guy, we should be seeing all
240	 * his children via normal tracing route.  */
241	if (pid2proc(pid) != NULL)
242		return;
243
244	/* First, see if we can attach the requested PID itself.  */
245	if (open_one_pid(pid)) {
246		fprintf(stderr, "Cannot attach to pid %u: %s\n",
247			pid, strerror(errno));
248		trace_fail_warning(pid);
249		return;
250	}
251
252	/* Now attach to all tasks that belong to that PID.  There's a
253	 * race between process_tasks and open_one_pid.  So when we
254	 * fail in open_one_pid below, we just do another round.
255	 * Chances are that by then that PID will have gone away, and
256	 * that's why we have seen the failure.  The processes that we
257	 * manage to open_one_pid are stopped, so we should eventually
258	 * reach a point where process_tasks doesn't give any new
259	 * processes (because there's nobody left to produce
260	 * them).  */
261	size_t old_ntasks = 0;
262	int have_all;
263	while (1) {
264		pid_t *tasks;
265		size_t ntasks;
266		size_t i;
267
268		if (process_tasks(pid, &tasks, &ntasks) < 0) {
269			fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
270				pid, strerror(errno));
271			break;
272		}
273
274		have_all = 1;
275		for (i = 0; i < ntasks; ++i)
276			if (pid2proc(tasks[i]) == NULL
277			    && open_one_pid(tasks[i]))
278				have_all = 0;
279
280		free(tasks);
281
282		if (have_all && old_ntasks == ntasks)
283			break;
284		old_ntasks = ntasks;
285	}
286
287	/* Done.  Now initialize breakpoints and then continue
288	 * everyone.  */
289	Process * leader;
290	leader = pid2proc(pid)->leader;
291	enable_all_breakpoints(leader);
292
293	each_task(pid2proc(pid)->leader, start_one_pid, NULL);
294}
295
296static enum callback_status
297find_proc(Process * proc, void * data)
298{
299	pid_t pid = (pid_t)(uintptr_t)data;
300	return proc->pid == pid ? CBS_STOP : CBS_CONT;
301}
302
303Process *
304pid2proc(pid_t pid) {
305	return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
306}
307
308static Process * list_of_processes = NULL;
309
310static void
311unlist_process(Process * proc)
312{
313	Process *tmp;
314
315	if (list_of_processes == proc) {
316		list_of_processes = list_of_processes->next;
317		return;
318	}
319
320	for (tmp = list_of_processes; ; tmp = tmp->next) {
321		/* If the following assert fails, the process wasn't
322		 * in the list.  */
323		assert(tmp->next != NULL);
324
325		if (tmp->next == proc) {
326			tmp->next = tmp->next->next;
327			return;
328		}
329	}
330}
331
332struct Process *
333each_process(struct Process *it,
334	     enum callback_status(*cb)(struct Process *proc, void *data),
335	     void *data)
336{
337	if (it == NULL)
338		it = list_of_processes;
339	for (; it != NULL; ) {
340		/* Callback might call remove_process.  */
341		Process * next = it->next;
342		switch ((*cb)(it, data)) {
343		case CBS_STOP:
344			return it;
345		case CBS_CONT:
346			break;
347		}
348		it = next;
349	}
350	return NULL;
351}
352
353Process *
354each_task(struct Process *it,
355	  enum callback_status(*cb)(struct Process *proc, void *data),
356	  void *data)
357{
358	if (it != NULL) {
359		Process * leader = it->leader;
360		for (; it != NULL && it->leader == leader; ) {
361			/* Callback might call remove_process.  */
362			Process * next = it->next;
363			switch ((*cb)(it, data)) {
364			case CBS_STOP:
365				return it;
366			case CBS_CONT:
367				break;
368			}
369			it = next;
370		}
371	}
372	return NULL;
373}
374
375void
376add_process(Process * proc)
377{
378	fprintf(stderr, "add_process %d\n", proc->pid);
379	Process ** leaderp = &list_of_processes;
380	if (proc->pid) {
381		pid_t tgid = process_leader(proc->pid);
382		fprintf(stderr, " + leader is %d\n", tgid);
383		if (tgid == 0)
384			/* Must have been terminated before we managed
385			 * to fully attach.  */
386			return;
387		if (tgid == proc->pid)
388			proc->leader = proc;
389		else {
390			Process * leader = pid2proc(tgid);
391			proc->leader = leader;
392			if (leader != NULL)
393				leaderp = &leader->next;
394		}
395	}
396	proc->next = *leaderp;
397	*leaderp = proc;
398}
399
400void
401change_process_leader(Process * proc, Process * leader)
402{
403	Process ** leaderp = &list_of_processes;
404	if (proc->leader == leader)
405		return;
406
407	assert(leader != NULL);
408	unlist_process(proc);
409	if (proc != leader)
410		leaderp = &leader->next;
411
412	proc->leader = leader;
413	proc->next = *leaderp;
414	*leaderp = proc;
415}
416
417static enum callback_status
418clear_leader(struct Process *proc, void *data)
419{
420	debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
421	      proc->pid, proc->leader->pid);
422	proc->leader = NULL;
423	return CBS_CONT;
424}
425
426static enum ecb_status
427event_for_proc(Event * event, void * data)
428{
429	if (event->proc == data)
430		return ecb_deque;
431	else
432		return ecb_cont;
433}
434
435static void
436delete_events_for(Process * proc)
437{
438	Event * event;
439	while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
440		free(event);
441}
442
443void
444remove_process(Process *proc)
445{
446	debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
447
448	if (proc->leader == proc)
449		each_task(proc, &clear_leader, NULL);
450
451	unlist_process(proc);
452	delete_events_for(proc);
453	free(proc);
454}
455
456void
457install_event_handler(Process *proc, struct event_handler *handler)
458{
459	debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
460	assert(proc->event_handler == NULL);
461	proc->event_handler = handler;
462}
463
464void
465destroy_event_handler(Process * proc)
466{
467	struct event_handler *handler = proc->event_handler;
468	debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
469	assert(handler != NULL);
470	if (handler->destroy != NULL)
471		handler->destroy(handler);
472	free(handler);
473	proc->event_handler = NULL;
474}
475
476static enum callback_status
477breakpoint_for_symbol(struct library_symbol *libsym, void *data)
478{
479	struct Process *proc = data;
480	fprintf(stderr, "  %s@%p\n", libsym->name, libsym->enter_addr);
481
482	if (insert_breakpoint(proc, libsym->enter_addr, libsym, 1) == NULL)
483		return CBS_STOP;
484
485	return CBS_CONT;
486}
487
488void
489proc_add_library(struct Process *proc, struct library *lib)
490{
491	assert(lib->next == NULL);
492	lib->next = proc->libraries;
493	proc->libraries = lib;
494	fprintf(stderr, "=== Added library %s@%p to %d:\n",
495		lib->name, lib->base, proc->pid);
496
497	struct library_symbol *libsym = NULL;
498	while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
499					     proc)) != NULL) {
500		error(0, errno, "insert breakpoint for %s", libsym->name);
501		libsym = libsym->next;
502	}
503}
504
505int
506proc_remove_library(struct Process *proc, struct library *lib)
507{
508	struct library **libp;
509	for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
510		if (*libp == lib) {
511			*libp = lib->next;
512			return 0;
513		}
514	return -1;
515}
516
517struct library *
518proc_each_library(struct Process *proc, struct library *it,
519		  enum callback_status (*cb)(struct Process *proc,
520					     struct library *lib, void *data),
521		  void *data)
522{
523	if (it == NULL)
524		it = proc->libraries;
525
526	while (it != NULL) {
527		struct library *next = it->next;
528
529		switch (cb(proc, it, data)) {
530		case CBS_STOP:
531			return it;
532		case CBS_CONT:
533			break;
534		}
535
536		it = next;
537	}
538
539	return NULL;
540}
541