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