proc.c revision ba1664b062414481d0f37d06bb01a19874c8d481
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2009 Juan Cespedes
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include "config.h"
24
25#include <sys/types.h>
26#include <assert.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#if defined(HAVE_LIBUNWIND)
33#include <libunwind.h>
34#include <libunwind-ptrace.h>
35#endif /* defined(HAVE_LIBUNWIND) */
36
37#include "backend.h"
38#include "breakpoint.h"
39#include "debug.h"
40#include "fetch.h"
41#include "proc.h"
42#include "value_dict.h"
43
44#ifndef ARCH_HAVE_PROCESS_DATA
45int
46arch_process_init(struct Process *proc)
47{
48	return 0;
49}
50
51void
52arch_process_destroy(struct Process *proc)
53{
54}
55
56int
57arch_process_clone(struct Process *retp, struct Process *proc)
58{
59	return 0;
60}
61
62int
63arch_process_exec(struct Process *proc)
64{
65	return 0;
66}
67#endif
68
69#ifndef ARCH_HAVE_DYNLINK_DONE
70void
71arch_dynlink_done(struct Process *proc)
72{
73}
74#endif
75
76static void add_process(struct Process *proc, int was_exec);
77static void unlist_process(struct Process *proc);
78
79static int
80process_bare_init(struct Process *proc, const char *filename,
81		  pid_t pid, int was_exec)
82{
83	if (!was_exec) {
84		memset(proc, 0, sizeof(*proc));
85
86		proc->filename = strdup(filename);
87		if (proc->filename == NULL) {
88		fail:
89			free(proc->filename);
90			if (proc->breakpoints != NULL)
91				dict_clear(proc->breakpoints);
92			return -1;
93		}
94	}
95
96	/* Add process so that we know who the leader is.  */
97	proc->pid = pid;
98	add_process(proc, was_exec);
99	if (proc->leader == NULL)
100		goto fail;
101
102	if (proc->leader == proc) {
103		proc->breakpoints = dict_init(target_address_hash,
104					      target_address_cmp);
105		if (proc->breakpoints == NULL)
106			goto fail;
107	} else {
108		proc->breakpoints = NULL;
109	}
110
111#if defined(HAVE_LIBUNWIND)
112	proc->unwind_priv = _UPT_create(pid);
113	proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
114#endif /* defined(HAVE_LIBUNWIND) */
115
116	return 0;
117}
118
119static void
120process_bare_destroy(struct Process *proc, int was_exec)
121{
122	dict_clear(proc->breakpoints);
123	if (!was_exec) {
124		free(proc->filename);
125		unlist_process(proc);
126	}
127}
128
129static int
130process_init_main(struct Process *proc)
131{
132	if (breakpoints_init(proc) < 0) {
133		fprintf(stderr, "failed to init breakpoints %d\n",
134			proc->pid);
135		return -1;
136	}
137
138	return 0;
139}
140
141int
142process_init(struct Process *proc, const char *filename, pid_t pid)
143{
144	if (process_bare_init(proc, filename, pid, 0) < 0) {
145	fail:
146		fprintf(stderr, "failed to initialize process %d: %s\n",
147			pid, strerror(errno));
148		return -1;
149	}
150
151	if (arch_process_init(proc) < 0) {
152		process_bare_destroy(proc, 0);
153		goto fail;
154	}
155
156	if (proc->leader != proc)
157		return 0;
158	if (process_init_main(proc) < 0) {
159		process_bare_destroy(proc, 0);
160		goto fail;
161	}
162	return 0;
163}
164
165static enum callback_status
166destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
167{
168	breakpoint_destroy(bp);
169	free(bp);
170	return CBS_CONT;
171}
172
173static void
174private_process_destroy(struct Process *proc, int keep_filename)
175{
176	if (!keep_filename)
177		free(proc->filename);
178
179	/* Libraries and symbols.  This is only relevant in
180	 * leader.  */
181	struct library *lib;
182	for (lib = proc->libraries; lib != NULL; ) {
183		struct library *next = lib->next;
184		library_destroy(lib);
185		free(lib);
186		lib = next;
187	}
188	proc->libraries = NULL;
189
190	/* Breakpoints.  */
191	if (proc->breakpoints != NULL) {
192		proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
193		dict_clear(proc->breakpoints);
194		proc->breakpoints = NULL;
195	}
196}
197
198void
199process_destroy(struct Process *proc)
200{
201	private_process_destroy(proc, 0);
202	arch_process_destroy(proc);
203}
204
205int
206process_exec(struct Process *proc)
207{
208	/* Call exec first, before we destroy the main state.  */
209	if (arch_process_exec(proc) < 0)
210		return -1;
211
212	private_process_destroy(proc, 1);
213	if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
214		return -1;
215	if (process_init_main(proc) < 0) {
216		process_bare_destroy(proc, 1);
217		return -1;
218	}
219	return 0;
220}
221
222struct Process *
223open_program(const char *filename, pid_t pid)
224{
225	assert(pid != 0);
226	struct Process *proc = malloc(sizeof(*proc));
227	if (proc == NULL || process_init(proc, filename, pid) < 0) {
228		free(proc);
229		return NULL;
230	}
231	return proc;
232}
233
234struct clone_single_bp_data {
235	struct Process *old_proc;
236	struct Process *new_proc;
237	int error;
238};
239
240static void
241clone_single_bp(void *key, void *value, void *u)
242{
243	struct breakpoint *bp = value;
244	struct clone_single_bp_data *data = u;
245
246	data->error = 0;
247	struct breakpoint *clone = malloc(sizeof(*clone));
248	if (clone == NULL
249	    || breakpoint_clone(clone, data->new_proc,
250				bp, data->old_proc) < 0) {
251	fail:
252		free(clone);
253		data->error = -1;
254	}
255	if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
256		breakpoint_destroy(clone);
257		goto fail;
258	}
259}
260
261int
262process_clone(struct Process *retp, struct Process *proc, pid_t pid)
263{
264	if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
265	fail1:
266		fprintf(stderr, "failed to clone process %d->%d : %s\n",
267			proc->pid, pid, strerror(errno));
268		return -1;
269	}
270
271	retp->tracesysgood = proc->tracesysgood;
272	retp->e_machine = proc->e_machine;
273
274	/* For non-leader processes, that's all we need to do.  */
275	if (retp->leader != retp)
276		return 0;
277
278	/* Clone symbols first so that we can clone and relink
279	 * breakpoints.  */
280	struct library *lib;
281	struct library **nlibp = &retp->libraries;
282	for (lib = proc->libraries; lib != NULL; lib = lib->next) {
283		*nlibp = malloc(sizeof(**nlibp));
284		if (*nlibp == NULL
285		    || library_clone(*nlibp, lib) < 0) {
286		fail2:
287			process_bare_destroy(retp, 0);
288
289			/* Error when cloning.  Unroll what was done.  */
290			for (lib = retp->libraries; lib != NULL; ) {
291				struct library *next = lib->next;
292				library_destroy(lib);
293				free(lib);
294				lib = next;
295			}
296			goto fail1;
297		}
298
299		nlibp = &(*nlibp)->next;
300	}
301
302	/* Now clone breakpoints.  Symbol relinking is done in
303	 * clone_single_bp.  */
304	struct clone_single_bp_data data = {
305		.old_proc = proc,
306		.new_proc = retp,
307		.error = 0,
308	};
309	dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
310	if (data.error < 0)
311		goto fail2;
312
313	/* And finally the call stack.  */
314	memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
315	retp->callstack_depth = proc->callstack_depth;
316
317	size_t i;
318	for (i = 0; i < retp->callstack_depth; ++i) {
319		struct fetch_context *ctx = retp->callstack[i].fetch_context;
320		if (ctx != NULL) {
321			struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
322			if (nctx == NULL) {
323				size_t j;
324			fail3:
325				for (j = 0; j < i; ++j) {
326					nctx = retp->callstack[i].fetch_context;
327					fetch_arg_done(nctx);
328					retp->callstack[i].fetch_context = NULL;
329				}
330				goto fail2;
331			}
332			retp->callstack[i].fetch_context = nctx;
333		}
334
335		struct value_dict *args = retp->callstack[i].arguments;
336		if (args != NULL) {
337			struct value_dict *nargs = malloc(sizeof(*nargs));
338			if (nargs == NULL
339			    || val_dict_clone(nargs, args) < 0) {
340				size_t j;
341			fail4:
342				for (j = 0; j < i; ++j) {
343					nargs = retp->callstack[i].arguments;
344					val_dict_destroy(nargs);
345					free(nargs);
346					retp->callstack[i].arguments = NULL;
347				}
348
349				/* Pretend that this round went well,
350				 * so that fail3 frees I-th
351				 * fetch_context.  */
352				++i;
353				goto fail3;
354			}
355			retp->callstack[i].arguments = nargs;
356		}
357	}
358
359	if (arch_process_clone(retp, proc) < 0)
360		goto fail4;
361
362	return 0;
363}
364
365static int
366open_one_pid(pid_t pid)
367{
368	Process *proc;
369	char *filename;
370	debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
371
372	/* Get the filename first.  Should the trace_pid fail, we can
373	 * easily free it, untracing is more work.  */
374	if ((filename = pid2name(pid)) == NULL
375	    || trace_pid(pid) < 0) {
376		free(filename);
377		return -1;
378	}
379
380	proc = open_program(filename, pid);
381	if (proc == NULL)
382		return -1;
383	trace_set_options(proc);
384
385	return 0;
386}
387
388static enum callback_status
389start_one_pid(Process * proc, void * data)
390{
391	continue_process(proc->pid);
392	return CBS_CONT;
393}
394
395void
396open_pid(pid_t pid)
397{
398	debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
399	/* If we are already tracing this guy, we should be seeing all
400	 * his children via normal tracing route.  */
401	if (pid2proc(pid) != NULL)
402		return;
403
404	/* First, see if we can attach the requested PID itself.  */
405	if (open_one_pid(pid)) {
406		fprintf(stderr, "Cannot attach to pid %u: %s\n",
407			pid, strerror(errno));
408		trace_fail_warning(pid);
409		return;
410	}
411
412	/* Now attach to all tasks that belong to that PID.  There's a
413	 * race between process_tasks and open_one_pid.  So when we
414	 * fail in open_one_pid below, we just do another round.
415	 * Chances are that by then that PID will have gone away, and
416	 * that's why we have seen the failure.  The processes that we
417	 * manage to open_one_pid are stopped, so we should eventually
418	 * reach a point where process_tasks doesn't give any new
419	 * processes (because there's nobody left to produce
420	 * them).  */
421	size_t old_ntasks = 0;
422	int have_all;
423	while (1) {
424		pid_t *tasks;
425		size_t ntasks;
426		size_t i;
427
428		if (process_tasks(pid, &tasks, &ntasks) < 0) {
429			fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
430				pid, strerror(errno));
431			break;
432		}
433
434		have_all = 1;
435		for (i = 0; i < ntasks; ++i)
436			if (pid2proc(tasks[i]) == NULL
437			    && open_one_pid(tasks[i]))
438				have_all = 0;
439
440		free(tasks);
441
442		if (have_all && old_ntasks == ntasks)
443			break;
444		old_ntasks = ntasks;
445	}
446
447	struct Process *leader = pid2proc(pid)->leader;
448
449	/* XXX Is there a way to figure out whether _start has
450	 * actually already been hit?  */
451	arch_dynlink_done(leader);
452
453	/* Done.  Continue everyone.  */
454	each_task(leader, NULL, start_one_pid, NULL);
455}
456
457static enum callback_status
458find_proc(Process * proc, void * data)
459{
460	pid_t pid = (pid_t)(uintptr_t)data;
461	return proc->pid == pid ? CBS_STOP : CBS_CONT;
462}
463
464Process *
465pid2proc(pid_t pid) {
466	return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
467}
468
469static Process * list_of_processes = NULL;
470
471static void
472unlist_process(Process * proc)
473{
474	Process *tmp;
475
476	if (list_of_processes == proc) {
477		list_of_processes = list_of_processes->next;
478		return;
479	}
480
481	for (tmp = list_of_processes; ; tmp = tmp->next) {
482		/* If the following assert fails, the process wasn't
483		 * in the list.  */
484		assert(tmp->next != NULL);
485
486		if (tmp->next == proc) {
487			tmp->next = tmp->next->next;
488			return;
489		}
490	}
491}
492
493struct Process *
494each_process(struct Process *start_after,
495	     enum callback_status(*cb)(struct Process *proc, void *data),
496	     void *data)
497{
498	struct Process *it = start_after == NULL ? list_of_processes
499		: start_after->next;
500
501	while (it != NULL) {
502		/* Callback might call remove_process.  */
503		struct Process *next = it->next;
504		switch ((*cb)(it, data)) {
505		case CBS_FAIL:
506			/* XXX handle me */
507		case CBS_STOP:
508			return it;
509		case CBS_CONT:
510			break;
511		}
512		it = next;
513	}
514	return NULL;
515}
516
517Process *
518each_task(struct Process *proc, struct Process *start_after,
519	  enum callback_status(*cb)(struct Process *proc, void *data),
520	  void *data)
521{
522	assert(proc != NULL);
523	struct Process *it = start_after == NULL ? proc->leader
524		: start_after->next;
525
526	if (it != NULL) {
527		struct Process *leader = it->leader;
528		while (it != NULL && it->leader == leader) {
529			/* Callback might call remove_process.  */
530			struct Process *next = it->next;
531			switch ((*cb)(it, data)) {
532			case CBS_FAIL:
533				/* XXX handle me */
534			case CBS_STOP:
535				return it;
536			case CBS_CONT:
537				break;
538			}
539			it = next;
540		}
541	}
542	return NULL;
543}
544
545static void
546add_process(struct Process *proc, int was_exec)
547{
548	Process ** leaderp = &list_of_processes;
549	if (proc->pid) {
550		pid_t tgid = process_leader(proc->pid);
551		if (tgid == 0)
552			/* Must have been terminated before we managed
553			 * to fully attach.  */
554			return;
555		if (tgid == proc->pid)
556			proc->leader = proc;
557		else {
558			Process * leader = pid2proc(tgid);
559			proc->leader = leader;
560			if (leader != NULL)
561				leaderp = &leader->next;
562		}
563	}
564
565	if (!was_exec) {
566		proc->next = *leaderp;
567		*leaderp = proc;
568	}
569}
570
571void
572change_process_leader(Process * proc, Process * leader)
573{
574	Process ** leaderp = &list_of_processes;
575	if (proc->leader == leader)
576		return;
577
578	assert(leader != NULL);
579	unlist_process(proc);
580	if (proc != leader)
581		leaderp = &leader->next;
582
583	proc->leader = leader;
584	proc->next = *leaderp;
585	*leaderp = proc;
586}
587
588static enum callback_status
589clear_leader(struct Process *proc, void *data)
590{
591	debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
592	      proc->pid, proc->leader->pid);
593	proc->leader = NULL;
594	return CBS_CONT;
595}
596
597void
598remove_process(Process *proc)
599{
600	debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
601
602	if (proc->leader == proc)
603		each_task(proc, NULL, &clear_leader, NULL);
604
605	unlist_process(proc);
606	process_removed(proc);
607	process_destroy(proc);
608	free(proc);
609}
610
611void
612install_event_handler(Process *proc, struct event_handler *handler)
613{
614	debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
615	assert(proc->event_handler == NULL);
616	proc->event_handler = handler;
617}
618
619void
620destroy_event_handler(Process * proc)
621{
622	struct event_handler *handler = proc->event_handler;
623	debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
624	assert(handler != NULL);
625	if (handler->destroy != NULL)
626		handler->destroy(handler);
627	free(handler);
628	proc->event_handler = NULL;
629}
630
631static enum callback_status
632breakpoint_for_symbol(struct library_symbol *libsym, void *data)
633{
634	struct Process *proc = data;
635	assert(proc->leader == proc);
636
637	/* If there is an artificial breakpoint on the same address,
638	 * its libsym will be NULL, and we can smuggle our libsym
639	 * there.  That artificial breakpoint is there presumably for
640	 * the callbacks, which we don't touch.  If there is a real
641	 * breakpoint, then this is a bug.  ltrace-elf.c should filter
642	 * symbols and ignore extra symbol aliases.
643	 *
644	 * The other direction is more complicated and currently not
645	 * supported.  If a breakpoint has custom callbacks, it might
646	 * be also custom-allocated, and we would really need to swap
647	 * the two: delete the one now in the dictionary, swap values
648	 * around, and put the new breakpoint back in.  */
649	struct breakpoint *bp = dict_find_entry(proc->breakpoints,
650						libsym->enter_addr);
651	if (bp != NULL) {
652		assert(bp->libsym == NULL);
653		bp->libsym = libsym;
654		return CBS_CONT;
655	}
656
657	bp = malloc(sizeof(*bp));
658	if (bp == NULL
659	    || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
660	fail:
661		free(bp);
662		return CBS_FAIL;
663	}
664	if (proc_add_breakpoint(proc, bp) < 0) {
665		breakpoint_destroy(bp);
666		goto fail;
667	}
668
669	if (breakpoint_turn_on(bp, proc) < 0) {
670		proc_remove_breakpoint(proc, bp);
671		breakpoint_destroy(bp);
672		goto fail;
673	}
674
675	return CBS_CONT;
676}
677
678void
679proc_add_library(struct Process *proc, struct library *lib)
680{
681	assert(lib->next == NULL);
682	lib->next = proc->libraries;
683	proc->libraries = lib;
684	debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
685	      lib->soname, lib->base, lib->pathname, proc->pid);
686
687	struct library_symbol *libsym = NULL;
688	while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
689					     proc)) != NULL)
690		fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
691			libsym->name, proc->pid, strerror(errno));
692}
693
694int
695proc_remove_library(struct Process *proc, struct library *lib)
696{
697	struct library **libp;
698	for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
699		if (*libp == lib) {
700			*libp = lib->next;
701			return 0;
702		}
703	return -1;
704}
705
706struct library *
707proc_each_library(struct Process *proc, struct library *it,
708		  enum callback_status (*cb)(struct Process *proc,
709					     struct library *lib, void *data),
710		  void *data)
711{
712	if (it == NULL)
713		it = proc->libraries;
714
715	while (it != NULL) {
716		struct library *next = it->next;
717
718		switch (cb(proc, it, data)) {
719		case CBS_FAIL:
720			/* XXX handle me */
721		case CBS_STOP:
722			return it;
723		case CBS_CONT:
724			break;
725		}
726
727		it = next;
728	}
729
730	return NULL;
731}
732
733static void
734check_leader(struct Process *proc)
735{
736	/* Only the group leader should be getting the breakpoints and
737	 * thus have ->breakpoint initialized.  */
738	assert(proc->leader != NULL);
739	assert(proc->leader == proc);
740	assert(proc->breakpoints != NULL);
741}
742
743int
744proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
745{
746	debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
747	      proc->pid, breakpoint_name(bp), bp->addr);
748	check_leader(proc);
749
750	/* XXX We might merge bp->libsym instead of the following
751	 * assert, but that's not necessary right now.  Read the
752	 * comment in breakpoint_for_symbol.  */
753	assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
754
755	if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
756		fprintf(stderr,
757			"couldn't enter breakpoint %s@%p to dictionary: %s\n",
758			breakpoint_name(bp), bp->addr, strerror(errno));
759		return -1;
760	}
761
762	return 0;
763}
764
765void
766proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
767{
768	debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
769	      proc->pid, breakpoint_name(bp), bp->addr);
770	check_leader(proc);
771	struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
772	assert(removed == bp);
773}
774
775/* Dict doesn't support iteration restarts, so here's this contraption
776 * for now.  XXX add restarts to dict.  */
777struct each_breakpoint_data
778{
779	void *start;
780	void *end;
781	struct Process *proc;
782	enum callback_status (*cb)(struct Process *proc,
783				   struct breakpoint *bp,
784				   void *data);
785	void *cb_data;
786};
787
788static void
789each_breakpoint_cb(void *key, void *value, void *d)
790{
791	struct each_breakpoint_data *data = d;
792	if (data->end != NULL)
793		return;
794	if (data->start == key)
795		data->start = NULL;
796
797	if (data->start == NULL) {
798		switch (data->cb(data->proc, value, data->cb_data)) {
799		case CBS_FAIL:
800			/* XXX handle me */
801		case CBS_STOP:
802			data->end = key;
803		case CBS_CONT:
804			return;
805		}
806	}
807}
808
809void *
810proc_each_breakpoint(struct Process *proc, void *start,
811		     enum callback_status (*cb)(struct Process *proc,
812						struct breakpoint *bp,
813						void *data), void *data)
814{
815	struct each_breakpoint_data dd = {
816		.start = start,
817		.proc = proc,
818		.cb = cb,
819		.cb_data = data,
820	};
821	dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
822	return dd.end;
823}
824