blob: 777ad1b0f8f948860ad9f5bb64d8af45003ca31a [file] [log] [blame]
Juan Cespedesf7281232009-06-25 16:11:21 +02001#include "config.h"
Juan Cespedesf7281232009-06-25 16:11:21 +02002
3#include <stdio.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/param.h>
9#include <signal.h>
10#include <sys/wait.h>
11
12#include "common.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020013
14char *command = NULL;
Juan Cespedesf7281232009-06-25 16:11:21 +020015
16int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */
17
Petr Machatacebb8842011-07-09 11:14:11 +020018static enum pcb_status
19stop_non_p_processes (Process * proc, void * data)
20{
21 int stop = 1;
22
23 struct opt_p_t *it;
24 for (it = opt_p; it != NULL; it = it->next) {
25 Process * p_proc = pid2proc(it->pid);
26 if (p_proc == NULL) {
27 printf("stop_non_p_processes: %d terminated?\n", it->pid);
28 continue;
29 }
Petr Machata9a5420c2011-07-09 11:21:23 +020030 if (p_proc == proc || p_proc->leader == proc->leader) {
Petr Machatacebb8842011-07-09 11:14:11 +020031 stop = 0;
32 break;
33 }
34 }
35
36 if (stop) {
37 debug(2, "Sending SIGSTOP to process %u", proc->pid);
38 kill(proc->pid, SIGSTOP);
39 }
40
41 return pcb_cont;
42}
43
Juan Cespedesf7281232009-06-25 16:11:21 +020044static void
45signal_alarm(int sig) {
Juan Cespedesf7281232009-06-25 16:11:21 +020046 signal(SIGALRM, SIG_DFL);
Petr Machatacebb8842011-07-09 11:14:11 +020047 each_process(NULL, &stop_non_p_processes, NULL);
Juan Cespedesf7281232009-06-25 16:11:21 +020048}
49
50static void
Petr Machataffe4cd22012-04-11 18:01:44 +020051signal_exit(int sig)
52{
Juan Cespedesf7281232009-06-25 16:11:21 +020053 debug(1, "Received interrupt signal; exiting...");
Petr Machataffe4cd22012-04-11 18:01:44 +020054 if (exiting != 0)
55 return;
56
57 exiting = 1 + !!os_ltrace_exiting_sighandler();
58
Juan Cespedesf7281232009-06-25 16:11:21 +020059 signal(SIGINT, SIG_IGN);
60 signal(SIGTERM, SIG_IGN);
61 signal(SIGALRM, signal_alarm);
Petr Machata602330f2011-07-09 11:15:34 +020062 //alarm(1);
Juan Cespedesf7281232009-06-25 16:11:21 +020063}
64
65static void
66normal_exit(void) {
67 output_line(0, 0);
68 if (options.summary) {
69 show_summary();
70 }
71 if (options.output) {
72 fclose(options.output);
73 options.output = NULL;
74 }
75}
76
77void
78ltrace_init(int argc, char **argv) {
79 struct opt_p_t *opt_p_tmp;
80
81 atexit(normal_exit);
82 signal(SIGINT, signal_exit); /* Detach processes when interrupted */
83 signal(SIGTERM, signal_exit); /* ... or killed */
84
85 argv = process_options(argc, argv);
86 while (opt_F) {
87 /* If filename begins with ~, expand it to the user's home */
88 /* directory. This does not correctly handle ~yoda, but that */
89 /* isn't as bad as it seems because the shell will normally */
90 /* be doing the expansion for us; only the hardcoded */
91 /* ~/.ltrace.conf should ever use this code. */
92 if (opt_F->filename[0] == '~') {
93 char path[PATH_MAX];
94 char *home_dir = getenv("HOME");
95 if (home_dir) {
96 strncpy(path, home_dir, PATH_MAX - 1);
97 path[PATH_MAX - 1] = '\0';
98 strncat(path, opt_F->filename + 1,
99 PATH_MAX - strlen(path) - 1);
100 read_config_file(path);
101 }
102 } else {
103 read_config_file(opt_F->filename);
104 }
105 opt_F = opt_F->next;
106 }
107 if (opt_e) {
108 struct opt_e_t *tmp = opt_e;
109 while (tmp) {
110 debug(1, "Option -e: %s\n", tmp->name);
111 tmp = tmp->next;
112 }
113 }
114 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);
119
Petr Machatac7585b62011-07-08 22:58:12 +0200120 open_program(command, execute_program(command, argv), 0);
Juan Cespedesf7281232009-06-25 16:11:21 +0200121 }
122 opt_p_tmp = opt_p;
123 while (opt_p_tmp) {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200124 open_pid(opt_p_tmp->pid);
Juan Cespedesf7281232009-06-25 16:11:21 +0200125 opt_p_tmp = opt_p_tmp->next;
126 }
127}
128
Juan Cespedes61da3372009-07-03 11:55:44 +0200129static int num_ltrace_callbacks[EVENT_MAX];
130static callback_func * ltrace_callbacks[EVENT_MAX];
Juan Cespedes40dc6352009-06-25 19:54:10 +0200131
132void
Juan Cespedes61da3372009-07-03 11:55:44 +0200133ltrace_add_callback(callback_func func, Event_type type) {
134 ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func));
135 ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func;
136}
Juan Cespedes40dc6352009-06-25 19:54:10 +0200137
Juan Cespedes61da3372009-07-03 11:55:44 +0200138static void
139dispatch_callbacks(Event * ev) {
140 int i;
141 /* Ignoring case 1: signal into a dying tracer */
142 if (ev->type==EVENT_SIGNAL &&
143 exiting && ev->e_un.signum == SIGSTOP) {
144 return;
145 }
146 /* Ignoring case 2: process being born before a clone event */
147 if (ev->proc && ev->proc->state == STATE_IGNORED) {
148 return;
149 }
150 for (i=0; i<num_ltrace_callbacks[ev->type]; i++) {
151 ltrace_callbacks[ev->type][i](ev);
Juan Cespedes40dc6352009-06-25 19:54:10 +0200152 }
153}
154
Juan Cespedesf7281232009-06-25 16:11:21 +0200155void
156ltrace_main(void) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200157 Event * ev;
Juan Cespedesf7281232009-06-25 16:11:21 +0200158 while (1) {
Juan Cespedes40dc6352009-06-25 19:54:10 +0200159 ev = next_event();
Juan Cespedes61da3372009-07-03 11:55:44 +0200160 dispatch_callbacks(ev);
Juan Cespedes03192f82009-07-03 10:16:22 +0200161 handle_event(ev);
Juan Cespedesf7281232009-06-25 16:11:21 +0200162 }
163}