blob: 64f99d3f63b0d739c06fb2f126255a48c65ba2bc [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
Petr Machata588850f2012-10-26 22:26:54 +02003 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
Petr Machata94078ec2012-01-05 18:07:02 +01004 * 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 Machata7c4651c2012-12-08 03:44:54 +010024#include <limits.h>
Petr Machata3ed2a422012-04-06 17:18:55 +020025#include <sys/param.h>
26#include <sys/wait.h>
27#include <errno.h>
Petr Machata3ed2a422012-04-06 17:18:55 +020028#include <signal.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020029#include <stdio.h>
30#include <stdlib.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020031#include <string.h>
Petr Machata3ed2a422012-04-06 17:18:55 +020032#include <unistd.h>
Juan Cespedesf7281232009-06-25 16:11:21 +020033
34#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010035#include "proc.h"
Petr Machata94078ec2012-01-05 18:07:02 +010036#include "read_config_file.h"
Petr Machata64262602012-01-07 03:41:36 +010037#include "backend.h"
Petr Machatae6c00ae2012-11-23 22:30:06 +010038#include "prototype.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020039
40char *command = NULL;
Juan Cespedesf7281232009-06-25 16:11:21 +020041
42int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */
43
Petr Machata2b46cfc2012-02-18 11:17:29 +010044static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +010045stop_non_p_processes(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +020046{
47 int stop = 1;
48
49 struct opt_p_t *it;
50 for (it = opt_p; it != NULL; it = it->next) {
Petr Machata929bd572012-12-17 03:20:34 +010051 struct process *p_proc = pid2proc(it->pid);
Petr Machatacebb8842011-07-09 11:14:11 +020052 if (p_proc == NULL) {
53 printf("stop_non_p_processes: %d terminated?\n", it->pid);
54 continue;
55 }
Petr Machata9a5420c2011-07-09 11:21:23 +020056 if (p_proc == proc || p_proc->leader == proc->leader) {
Petr Machatacebb8842011-07-09 11:14:11 +020057 stop = 0;
58 break;
59 }
60 }
61
62 if (stop) {
63 debug(2, "Sending SIGSTOP to process %u", proc->pid);
64 kill(proc->pid, SIGSTOP);
65 }
66
Petr Machata2b46cfc2012-02-18 11:17:29 +010067 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +020068}
69
Juan Cespedesf7281232009-06-25 16:11:21 +020070static void
71signal_alarm(int sig) {
Juan Cespedesf7281232009-06-25 16:11:21 +020072 signal(SIGALRM, SIG_DFL);
Petr Machatacebb8842011-07-09 11:14:11 +020073 each_process(NULL, &stop_non_p_processes, NULL);
Juan Cespedesf7281232009-06-25 16:11:21 +020074}
75
76static void
Petr Machataffe4cd22012-04-11 18:01:44 +020077signal_exit(int sig)
78{
Petr Machataffe4cd22012-04-11 18:01:44 +020079 if (exiting != 0)
80 return;
81
82 exiting = 1 + !!os_ltrace_exiting_sighandler();
83
Juan Cespedesf7281232009-06-25 16:11:21 +020084 signal(SIGINT, SIG_IGN);
85 signal(SIGTERM, SIG_IGN);
86 signal(SIGALRM, signal_alarm);
Petr Machata602330f2011-07-09 11:15:34 +020087 //alarm(1);
Juan Cespedesf7281232009-06-25 16:11:21 +020088}
89
90static void
Petr Machataefc94362012-10-27 00:49:05 +020091normal_exit(void)
92{
Juan Cespedesf7281232009-06-25 16:11:21 +020093 if (options.summary) {
94 show_summary();
Petr Machatafdfcf7b2012-11-23 18:40:25 +010095 destroy_global_config();
Juan Cespedesf7281232009-06-25 16:11:21 +020096 }
97 if (options.output) {
98 fclose(options.output);
99 options.output = NULL;
100 }
101}
102
103void
104ltrace_init(int argc, char **argv) {
105 struct opt_p_t *opt_p_tmp;
106
107 atexit(normal_exit);
108 signal(SIGINT, signal_exit); /* Detach processes when interrupted */
109 signal(SIGTERM, signal_exit); /* ... or killed */
110
111 argv = process_options(argc, argv);
Petr Machataf83053d2012-01-06 18:52:55 +0100112 init_global_config();
Petr Machata8a98e6f2012-10-27 00:30:13 +0200113
Juan Cespedesf7281232009-06-25 16:11:21 +0200114 if (command) {
Petr Machata02bd9ec2011-09-21 17:38:59 +0200115 /* Check that the binary ABI is supported before
116 * calling execute_program. */
117 struct ltelf lte = {};
118 open_elf(&lte, command);
Petr Machata588850f2012-10-26 22:26:54 +0200119 do_close_elf(&lte);
Petr Machata02bd9ec2011-09-21 17:38:59 +0200120
Petr Machatac805c622012-03-02 00:10:37 +0100121 pid_t pid = execute_program(command, argv);
Petr Machata929bd572012-12-17 03:20:34 +0100122 struct process *proc = open_program(command, pid);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200123 if (proc == NULL) {
124 fprintf(stderr, "couldn't open program '%s': %s\n",
125 command, strerror(errno));
126 exit(EXIT_FAILURE);
127 }
Petr Machata79a47ba2012-04-06 19:54:55 +0200128
129 trace_set_options(proc);
Petr Machatac805c622012-03-02 00:10:37 +0100130 continue_process(pid);
Juan Cespedesf7281232009-06-25 16:11:21 +0200131 }
132 opt_p_tmp = opt_p;
133 while (opt_p_tmp) {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200134 open_pid(opt_p_tmp->pid);
Juan Cespedesf7281232009-06-25 16:11:21 +0200135 opt_p_tmp = opt_p_tmp->next;
136 }
137}
138
Juan Cespedes61da3372009-07-03 11:55:44 +0200139static int num_ltrace_callbacks[EVENT_MAX];
140static callback_func * ltrace_callbacks[EVENT_MAX];
Juan Cespedes40dc6352009-06-25 19:54:10 +0200141
142void
Juan Cespedes61da3372009-07-03 11:55:44 +0200143ltrace_add_callback(callback_func func, Event_type type) {
144 ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
145 ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
146}
Juan Cespedes40dc6352009-06-25 19:54:10 +0200147
Juan Cespedes61da3372009-07-03 11:55:44 +0200148static void
149dispatch_callbacks(Event * ev) {
150 int i;
151 /* Ignoring case 1: signal into a dying tracer */
152 if (ev->type==EVENT_SIGNAL &&
153 exiting && ev->e_un.signum == SIGSTOP) {
154 return;
155 }
156 /* Ignoring case 2: process being born before a clone event */
157 if (ev->proc && ev->proc->state == STATE_IGNORED) {
158 return;
159 }
160 for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
161 ltrace_callbacks[ev->type][i](ev);
Juan Cespedes40dc6352009-06-25 19:54:10 +0200162 }
163}
164
Juan Cespedesf7281232009-06-25 16:11:21 +0200165void
166ltrace_main(void) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200167 Event * ev;
Juan Cespedesf7281232009-06-25 16:11:21 +0200168 while (1) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200169 ev = next_event();
Juan Cespedes61da3372009-07-03 11:55:44 +0200170 dispatch_callbacks(ev);
Juan Cespedes03192f82009-07-03 10:16:22 +0200171 handle_event(ev);
Juan Cespedesf7281232009-06-25 16:11:21 +0200172 }
173}