2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2009 Juan Cespedes
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.
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.
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
24 #include <sys/param.h>
35 #include "read_config_file.h"
40 int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */
42 static enum callback_status
43 stop_non_p_processes(Process *proc, void *data)
48 for (it = opt_p; it != NULL; it = it->next) {
49 Process * p_proc = pid2proc(it->pid);
51 printf("stop_non_p_processes: %d terminated?\n", it->pid);
54 if (p_proc == proc || p_proc->leader == proc->leader) {
61 debug(2, "Sending SIGSTOP to process %u", proc->pid);
62 kill(proc->pid, SIGSTOP);
69 signal_alarm(int sig) {
70 signal(SIGALRM, SIG_DFL);
71 each_process(NULL, &stop_non_p_processes, NULL);
80 exiting = 1 + !!os_ltrace_exiting_sighandler();
82 signal(SIGINT, SIG_IGN);
83 signal(SIGTERM, SIG_IGN);
84 signal(SIGALRM, signal_alarm);
91 if (options.summary) {
95 fclose(options.output);
96 options.output = NULL;
101 ltrace_init(int argc, char **argv) {
102 struct opt_p_t *opt_p_tmp;
105 signal(SIGINT, signal_exit); /* Detach processes when interrupted */
106 signal(SIGTERM, signal_exit); /* ... or killed */
108 argv = process_options(argc, argv);
109 init_global_config();
111 /* If filename begins with ~, expand it to the user's home */
112 /* directory. This does not correctly handle ~yoda, but that */
113 /* isn't as bad as it seems because the shell will normally */
114 /* be doing the expansion for us; only the hardcoded */
115 /* ~/.ltrace.conf should ever use this code. */
116 if (opt_F->filename[0] == '~') {
118 char *home_dir = getenv("HOME");
120 strncpy(path, home_dir, PATH_MAX - 1);
121 path[PATH_MAX - 1] = '\0';
122 strncat(path, opt_F->filename + 1,
123 PATH_MAX - strlen(path) - 1);
124 read_config_file(path);
127 read_config_file(opt_F->filename);
130 struct opt_F_t *next = opt_F->next;
131 if (opt_F->own_filename)
132 free(opt_F->filename);
137 /* Check that the binary ABI is supported before
138 * calling execute_program. */
139 struct ltelf lte = {};
140 open_elf(<e, command);
143 pid_t pid = execute_program(command, argv);
144 struct Process *proc = open_program(command, pid);
146 fprintf(stderr, "couldn't open program '%s': %s\n",
147 command, strerror(errno));
151 trace_set_options(proc);
152 continue_process(pid);
156 open_pid(opt_p_tmp->pid);
157 opt_p_tmp = opt_p_tmp->next;
161 static int num_ltrace_callbacks[EVENT_MAX];
162 static callback_func * ltrace_callbacks[EVENT_MAX];
165 ltrace_add_callback(callback_func func, Event_type type) {
166 ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
167 ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
171 dispatch_callbacks(Event * ev) {
173 /* Ignoring case 1: signal into a dying tracer */
174 if (ev->type==EVENT_SIGNAL &&
175 exiting && ev->e_un.signum == SIGSTOP) {
178 /* Ignoring case 2: process being born before a clone event */
179 if (ev->proc && ev->proc->state == STATE_IGNORED) {
182 for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
183 ltrace_callbacks[ev->type][i](ev);
192 dispatch_callbacks(ev);