libltrace.c 4.03 KB
Newer Older
1 2
/*
 * This file is part of ltrace.
3
 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2009 Juan Cespedes
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

Juan Cespedes's avatar
Juan Cespedes committed
22 23
#include "config.h"

24 25 26
#include <sys/param.h>
#include <sys/wait.h>
#include <errno.h>
27 28
#include <limits.h>
#include <locale.h>
29
#include <signal.h>
Juan Cespedes's avatar
Juan Cespedes committed
30 31 32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
33
#include <unistd.h>
Juan Cespedes's avatar
Juan Cespedes committed
34

35
#include "backend.h"
Juan Cespedes's avatar
Juan Cespedes committed
36
#include "common.h"
37
#include "proc.h"
38
#include "prototype.h"
39 40
#include "read_config_file.h"
#include "summary.h"
Juan Cespedes's avatar
Juan Cespedes committed
41 42 43 44 45

char *command = NULL;

int exiting = 0;		/* =1 if a SIGINT or SIGTERM has been received */

46
static enum callback_status
47
stop_non_p_processes(struct process *proc, void *data)
48 49
{
	int stop = 1;
Juan Cespedes's avatar
Juan Cespedes committed
50

51 52
	struct opt_p_t *it;
	for (it = opt_p; it != NULL; it = it->next) {
53
		struct process *p_proc = pid2proc(it->pid);
54 55 56
		if (p_proc == NULL) {
			printf("stop_non_p_processes: %d terminated?\n", it->pid);
			continue;
Juan Cespedes's avatar
Juan Cespedes committed
57
		}
58
		if (p_proc == proc || p_proc->leader == proc->leader) {
59 60 61 62 63 64 65 66
			stop = 0;
			break;
		}
	}

	if (stop) {
		debug(2, "Sending SIGSTOP to process %u", proc->pid);
		kill(proc->pid, SIGSTOP);
Juan Cespedes's avatar
Juan Cespedes committed
67
	}
68

69
	return CBS_CONT;
70 71 72 73 74 75
}

static void
signal_alarm(int sig) {
	signal(SIGALRM, SIG_DFL);
	each_process(NULL, &stop_non_p_processes, NULL);
Juan Cespedes's avatar
Juan Cespedes committed
76 77 78
}

static void
79 80 81 82 83 84 85
signal_exit(int sig)
{
	if (exiting != 0)
		return;

	exiting = 1 + !!os_ltrace_exiting_sighandler();

Juan Cespedes's avatar
Juan Cespedes committed
86 87 88
	signal(SIGINT, SIG_IGN);
	signal(SIGTERM, SIG_IGN);
	signal(SIGALRM, signal_alarm);
89
	//alarm(1);
Juan Cespedes's avatar
Juan Cespedes committed
90 91 92
}

static void
93 94
normal_exit(void)
{
95
	if (options.summary)
Juan Cespedes's avatar
Juan Cespedes committed
96 97 98 99 100 101 102 103
		show_summary();
	if (options.output) {
		fclose(options.output);
		options.output = NULL;
	}
}

void
104 105 106 107
ltrace_init(int argc, char **argv)
{
	setlocale(LC_ALL, "");

Juan Cespedes's avatar
Juan Cespedes committed
108 109 110 111 112 113 114
	struct opt_p_t *opt_p_tmp;

	atexit(normal_exit);
	signal(SIGINT, signal_exit);	/* Detach processes when interrupted */
	signal(SIGTERM, signal_exit);	/*  ... or killed */

	argv = process_options(argc, argv);
115
	init_global_config();
116

Juan Cespedes's avatar
Juan Cespedes committed
117
	if (command) {
118 119
		/* Check that the binary ABI is supported before
		 * calling execute_program.  */
120 121 122 123 124 125 126
		{
			struct ltelf lte;
			if (ltelf_init(&lte, command) == 0)
				ltelf_destroy(&lte);
			else
				exit(EXIT_FAILURE);
		}
127

128
		pid_t pid = execute_program(command, argv);
129
		struct process *proc = open_program(command, pid);
130 131 132 133 134
		if (proc == NULL) {
			fprintf(stderr, "couldn't open program '%s': %s\n",
				command, strerror(errno));
			exit(EXIT_FAILURE);
		}
135 136

		trace_set_options(proc);
137
		continue_process(pid);
Juan Cespedes's avatar
Juan Cespedes committed
138 139 140
	}
	opt_p_tmp = opt_p;
	while (opt_p_tmp) {
Juan Cespedes's avatar
Juan Cespedes committed
141
		open_pid(opt_p_tmp->pid);
Juan Cespedes's avatar
Juan Cespedes committed
142 143 144 145
		opt_p_tmp = opt_p_tmp->next;
	}
}

146 147
static int num_ltrace_callbacks[EVENT_MAX];
static callback_func * ltrace_callbacks[EVENT_MAX];
148 149

void
150 151 152 153
ltrace_add_callback(callback_func func, Event_type type) {
	ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
	ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
static void
dispatch_callbacks(Event * ev) {
	int i;
	/* Ignoring case 1: signal into a dying tracer */
	if (ev->type==EVENT_SIGNAL && 
			exiting && ev->e_un.signum == SIGSTOP) {
		return;
	}
	/* Ignoring case 2: process being born before a clone event */
	if (ev->proc && ev->proc->state == STATE_IGNORED) {
		return;
	}
	for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
		ltrace_callbacks[ev->type][i](ev);
169 170 171
	}
}

Juan Cespedes's avatar
Juan Cespedes committed
172 173
void
ltrace_main(void) {
174
	Event * ev;
Juan Cespedes's avatar
Juan Cespedes committed
175
	while (1) {
176
		ev = next_event();
177
		dispatch_callbacks(ev);
178
		handle_event(ev);
Juan Cespedes's avatar
Juan Cespedes committed
179 180
	}
}