libltrace.c revision 642626096a694c6af279d25d2b1b2fba5b10ddfb
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2009 Juan Cespedes
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include "config.h"
23
24#include <sys/param.h>
25#include <sys/wait.h>
26#include <errno.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
33#include "common.h"
34#include "proc.h"
35#include "read_config_file.h"
36#include "backend.h"
37
38char *command = NULL;
39
40int exiting = 0;		/* =1 if a SIGINT or SIGTERM has been received */
41
42static enum callback_status
43stop_non_p_processes(Process *proc, void *data)
44{
45	int stop = 1;
46
47	struct opt_p_t *it;
48	for (it = opt_p; it != NULL; it = it->next) {
49		Process * p_proc = pid2proc(it->pid);
50		if (p_proc == NULL) {
51			printf("stop_non_p_processes: %d terminated?\n", it->pid);
52			continue;
53		}
54		if (p_proc == proc || p_proc->leader == proc->leader) {
55			stop = 0;
56			break;
57		}
58	}
59
60	if (stop) {
61		debug(2, "Sending SIGSTOP to process %u", proc->pid);
62		kill(proc->pid, SIGSTOP);
63	}
64
65	return CBS_CONT;
66}
67
68static void
69signal_alarm(int sig) {
70	signal(SIGALRM, SIG_DFL);
71	each_process(NULL, &stop_non_p_processes, NULL);
72}
73
74static void
75signal_exit(int sig)
76{
77	debug(1, "Received interrupt signal; exiting...");
78	if (exiting != 0)
79		return;
80
81	exiting = 1 + !!os_ltrace_exiting_sighandler();
82
83	signal(SIGINT, SIG_IGN);
84	signal(SIGTERM, SIG_IGN);
85	signal(SIGALRM, signal_alarm);
86	//alarm(1);
87}
88
89static void
90normal_exit(void) {
91	output_line(0, 0);
92	if (options.summary) {
93		show_summary();
94	}
95	if (options.output) {
96		fclose(options.output);
97		options.output = NULL;
98	}
99}
100
101void
102ltrace_init(int argc, char **argv) {
103	struct opt_p_t *opt_p_tmp;
104
105	atexit(normal_exit);
106	signal(SIGINT, signal_exit);	/* Detach processes when interrupted */
107	signal(SIGTERM, signal_exit);	/*  ... or killed */
108
109	argv = process_options(argc, argv);
110	init_global_config();
111	while (opt_F) {
112		/* If filename begins with ~, expand it to the user's home */
113		/* directory. This does not correctly handle ~yoda, but that */
114		/* isn't as bad as it seems because the shell will normally */
115		/* be doing the expansion for us; only the hardcoded */
116		/* ~/.ltrace.conf should ever use this code. */
117		if (opt_F->filename[0] == '~') {
118			char path[PATH_MAX];
119			char *home_dir = getenv("HOME");
120			if (home_dir) {
121				strncpy(path, home_dir, PATH_MAX - 1);
122				path[PATH_MAX - 1] = '\0';
123				strncat(path, opt_F->filename + 1,
124						PATH_MAX - strlen(path) - 1);
125				read_config_file(path);
126			}
127		} else {
128			read_config_file(opt_F->filename);
129		}
130		opt_F = opt_F->next;
131	}
132	if (command) {
133		/* Check that the binary ABI is supported before
134		 * calling execute_program.  */
135		struct ltelf lte = {};
136		open_elf(&lte, command);
137
138		pid_t pid = execute_program(command, argv);
139		struct Process *proc = open_program(command, pid);
140		if (proc == NULL) {
141			fprintf(stderr, "couldn't open program '%s': %s\n",
142				command, strerror(errno));
143			exit(EXIT_FAILURE);
144		}
145
146		trace_set_options(proc);
147		continue_process(pid);
148	}
149	opt_p_tmp = opt_p;
150	while (opt_p_tmp) {
151		open_pid(opt_p_tmp->pid);
152		opt_p_tmp = opt_p_tmp->next;
153	}
154}
155
156static int num_ltrace_callbacks[EVENT_MAX];
157static callback_func * ltrace_callbacks[EVENT_MAX];
158
159void
160ltrace_add_callback(callback_func func, Event_type type) {
161	ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
162	ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
163}
164
165static void
166dispatch_callbacks(Event * ev) {
167	int i;
168	/* Ignoring case 1: signal into a dying tracer */
169	if (ev->type==EVENT_SIGNAL &&
170			exiting && ev->e_un.signum == SIGSTOP) {
171		return;
172	}
173	/* Ignoring case 2: process being born before a clone event */
174	if (ev->proc && ev->proc->state == STATE_IGNORED) {
175		return;
176	}
177	for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
178		ltrace_callbacks[ev->type][i](ev);
179	}
180}
181
182void
183ltrace_main(void) {
184	Event * ev;
185	while (1) {
186		ev = next_event();
187		dispatch_callbacks(ev);
188		handle_event(ev);
189	}
190}
191