blob: a49db2856bfc4999a669465bb134f99e8e86a850 [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
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
Juan Cespedesf7281232009-06-25 16:11:21 +020022#include "config.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020023
Petr Machata3ed2a422012-04-06 17:18:55 +020024#include <sys/param.h>
25#include <sys/wait.h>
26#include <errno.h>
Petr Machata3ed2a422012-04-06 17:18:55 +020027#include <signal.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020028#include <stdio.h>
29#include <stdlib.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020030#include <string.h>
Petr Machata3ed2a422012-04-06 17:18:55 +020031#include <unistd.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020032
33#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010034#include "proc.h"
Petr Machata94078ec2012-01-05 18:07:02 +010035#include "read_config_file.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020036
37char *command = NULL;
Juan Cespedesf7281232009-06-25 16:11:21 +020038
39int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */
40
Petr Machata2b46cfc2012-02-18 11:17:29 +010041static enum callback_status
42stop_non_p_processes(Process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +020043{
44 int stop = 1;
45
46 struct opt_p_t *it;
47 for (it = opt_p; it != NULL; it = it->next) {
48 Process * p_proc = pid2proc(it->pid);
49 if (p_proc == NULL) {
50 printf("stop_non_p_processes: %d terminated?\n", it->pid);
51 continue;
52 }
Petr Machata9a5420c2011-07-09 11:21:23 +020053 if (p_proc == proc || p_proc->leader == proc->leader) {
Petr Machatacebb8842011-07-09 11:14:11 +020054 stop = 0;
55 break;
56 }
57 }
58
59 if (stop) {
60 debug(2, "Sending SIGSTOP to process %u", proc->pid);
61 kill(proc->pid, SIGSTOP);
62 }
63
Petr Machata2b46cfc2012-02-18 11:17:29 +010064 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +020065}
66
Juan Cespedesf7281232009-06-25 16:11:21 +020067static void
68signal_alarm(int sig) {
Juan Cespedesf7281232009-06-25 16:11:21 +020069 signal(SIGALRM, SIG_DFL);
Petr Machatacebb8842011-07-09 11:14:11 +020070 each_process(NULL, &stop_non_p_processes, NULL);
Juan Cespedesf7281232009-06-25 16:11:21 +020071}
72
73static void
Petr Machataffe4cd22012-04-11 18:01:44 +020074signal_exit(int sig)
75{
Juan Cespedesf7281232009-06-25 16:11:21 +020076 debug(1, "Received interrupt signal; exiting...");
Petr Machataffe4cd22012-04-11 18:01:44 +020077 if (exiting != 0)
78 return;
79
80 exiting = 1 + !!os_ltrace_exiting_sighandler();
81
Juan Cespedesf7281232009-06-25 16:11:21 +020082 signal(SIGINT, SIG_IGN);
83 signal(SIGTERM, SIG_IGN);
84 signal(SIGALRM, signal_alarm);
Petr Machata602330f2011-07-09 11:15:34 +020085 //alarm(1);
Juan Cespedesf7281232009-06-25 16:11:21 +020086}
87
88static void
89normal_exit(void) {
90 output_line(0, 0);
91 if (options.summary) {
92 show_summary();
93 }
94 if (options.output) {
95 fclose(options.output);
96 options.output = NULL;
97 }
98}
99
100void
101ltrace_init(int argc, char **argv) {
102 struct opt_p_t *opt_p_tmp;
103
104 atexit(normal_exit);
105 signal(SIGINT, signal_exit); /* Detach processes when interrupted */
106 signal(SIGTERM, signal_exit); /* ... or killed */
107
108 argv = process_options(argc, argv);
Petr Machataf83053d2012-01-06 18:52:55 +0100109 init_global_config();
Juan Cespedesf7281232009-06-25 16:11:21 +0200110 while (opt_F) {
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] == '~') {
117 char path[PATH_MAX];
118 char *home_dir = getenv("HOME");
119 if (home_dir) {
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);
125 }
126 } else {
127 read_config_file(opt_F->filename);
128 }
129 opt_F = opt_F->next;
130 }
Juan Cespedesf7281232009-06-25 16:11:21 +0200131 if (command) {
Petr Machata02bd9ec2011-09-21 17:38:59 +0200132 /* Check that the binary ABI is supported before
133 * calling execute_program. */
134 struct ltelf lte = {};
135 open_elf(&lte, command);
136
Petr Machatac805c622012-03-02 00:10:37 +0100137 pid_t pid = execute_program(command, argv);
Petr Machata75934ad2012-04-14 02:28:03 +0200138 struct Process *proc = open_program(command, pid);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200139 if (proc == NULL) {
140 fprintf(stderr, "couldn't open program '%s': %s\n",
141 command, strerror(errno));
142 exit(EXIT_FAILURE);
143 }
Petr Machata79a47ba2012-04-06 19:54:55 +0200144
145 trace_set_options(proc);
Petr Machatac805c622012-03-02 00:10:37 +0100146 continue_process(pid);
Juan Cespedesf7281232009-06-25 16:11:21 +0200147 }
148 opt_p_tmp = opt_p;
149 while (opt_p_tmp) {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200150 open_pid(opt_p_tmp->pid);
Juan Cespedesf7281232009-06-25 16:11:21 +0200151 opt_p_tmp = opt_p_tmp->next;
152 }
153}
154
Juan Cespedes61da3372009-07-03 11:55:44 +0200155static int num_ltrace_callbacks[EVENT_MAX];
156static callback_func * ltrace_callbacks[EVENT_MAX];
Juan Cespedes40dc6352009-06-25 19:54:10 +0200157
158void
Juan Cespedes61da3372009-07-03 11:55:44 +0200159ltrace_add_callback(callback_func func, Event_type type) {
160 ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
161 ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
162}
Juan Cespedes40dc6352009-06-25 19:54:10 +0200163
Juan Cespedes61da3372009-07-03 11:55:44 +0200164static void
165dispatch_callbacks(Event * ev) {
166 int i;
167 /* Ignoring case 1: signal into a dying tracer */
168 if (ev->type==EVENT_SIGNAL &&
169 exiting && ev->e_un.signum == SIGSTOP) {
170 return;
171 }
172 /* Ignoring case 2: process being born before a clone event */
173 if (ev->proc && ev->proc->state == STATE_IGNORED) {
174 return;
175 }
176 for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
177 ltrace_callbacks[ev->type][i](ev);
Juan Cespedes40dc6352009-06-25 19:54:10 +0200178 }
179}
180
Juan Cespedesf7281232009-06-25 16:11:21 +0200181void
182ltrace_main(void) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200183 Event * ev;
Juan Cespedesf7281232009-06-25 16:11:21 +0200184 while (1) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200185 ev = next_event();
Juan Cespedes61da3372009-07-03 11:55:44 +0200186 dispatch_callbacks(ev);
Juan Cespedes03192f82009-07-03 10:16:22 +0200187 handle_event(ev);
Juan Cespedesf7281232009-06-25 16:11:21 +0200188 }
189}