libltrace.c revision c00837c2928da53a3515b107399b742ea157e78a
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 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 <limits.h>
28#include <locale.h>
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "common.h"
36#include "proc.h"
37#include "read_config_file.h"
38#include "backend.h"
39#include "prototype.h"
40
41char *command = NULL;
42
43int exiting = 0;		/* =1 if a SIGINT or SIGTERM has been received */
44
45static enum callback_status
46stop_non_p_processes(struct process *proc, void *data)
47{
48	int stop = 1;
49
50	struct opt_p_t *it;
51	for (it = opt_p; it != NULL; it = it->next) {
52		struct process *p_proc = pid2proc(it->pid);
53		if (p_proc == NULL) {
54			printf("stop_non_p_processes: %d terminated?\n", it->pid);
55			continue;
56		}
57		if (p_proc == proc || p_proc->leader == proc->leader) {
58			stop = 0;
59			break;
60		}
61	}
62
63	if (stop) {
64		debug(2, "Sending SIGSTOP to process %u", proc->pid);
65		kill(proc->pid, SIGSTOP);
66	}
67
68	return CBS_CONT;
69}
70
71static void
72signal_alarm(int sig) {
73	signal(SIGALRM, SIG_DFL);
74	each_process(NULL, &stop_non_p_processes, NULL);
75}
76
77static void
78signal_exit(int sig)
79{
80	if (exiting != 0)
81		return;
82
83	exiting = 1 + !!os_ltrace_exiting_sighandler();
84
85	signal(SIGINT, SIG_IGN);
86	signal(SIGTERM, SIG_IGN);
87	signal(SIGALRM, signal_alarm);
88	//alarm(1);
89}
90
91static void
92normal_exit(void)
93{
94	if (options.summary)
95		show_summary();
96	if (options.output) {
97		fclose(options.output);
98		options.output = NULL;
99	}
100}
101
102void
103ltrace_init(int argc, char **argv)
104{
105	setlocale(LC_ALL, "");
106
107	struct opt_p_t *opt_p_tmp;
108
109	atexit(normal_exit);
110	signal(SIGINT, signal_exit);	/* Detach processes when interrupted */
111	signal(SIGTERM, signal_exit);	/*  ... or killed */
112
113	argv = process_options(argc, argv);
114	init_global_config();
115
116	if (command) {
117		/* Check that the binary ABI is supported before
118		 * calling execute_program.  */
119		struct ltelf lte;
120		ltelf_init(&lte, command);
121		ltelf_destroy(&lte);
122
123		pid_t pid = execute_program(command, argv);
124		struct process *proc = open_program(command, pid);
125		if (proc == NULL) {
126			fprintf(stderr, "couldn't open program '%s': %s\n",
127				command, strerror(errno));
128			exit(EXIT_FAILURE);
129		}
130
131		trace_set_options(proc);
132		continue_process(pid);
133	}
134	opt_p_tmp = opt_p;
135	while (opt_p_tmp) {
136		open_pid(opt_p_tmp->pid);
137		opt_p_tmp = opt_p_tmp->next;
138	}
139}
140
141static int num_ltrace_callbacks[EVENT_MAX];
142static callback_func * ltrace_callbacks[EVENT_MAX];
143
144void
145ltrace_add_callback(callback_func func, Event_type type) {
146	ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
147	ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
148}
149
150static void
151dispatch_callbacks(Event * ev) {
152	int i;
153	/* Ignoring case 1: signal into a dying tracer */
154	if (ev->type==EVENT_SIGNAL &&
155			exiting && ev->e_un.signum == SIGSTOP) {
156		return;
157	}
158	/* Ignoring case 2: process being born before a clone event */
159	if (ev->proc && ev->proc->state == STATE_IGNORED) {
160		return;
161	}
162	for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
163		ltrace_callbacks[ev->type][i](ev);
164	}
165}
166
167void
168ltrace_main(void) {
169	Event * ev;
170	while (1) {
171		ev = next_event();
172		dispatch_callbacks(ev);
173		handle_event(ev);
174	}
175}
176