Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 673ff51 | 2013-10-25 23:45:39 +0200 | [diff] [blame] | 3 | * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc. |
Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 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 Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 22 | #include "config.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 23 | |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 24 | #include <sys/param.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <errno.h> |
Petr Machata | c00837c | 2013-11-11 02:24:42 +0100 | [diff] [blame] | 27 | #include <limits.h> |
| 28 | #include <locale.h> |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 32 | #include <string.h> |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 33 | #include <unistd.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 34 | |
Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 35 | #include "backend.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 36 | #include "common.h" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 37 | #include "proc.h" |
Petr Machata | e6c00ae | 2012-11-23 22:30:06 +0100 | [diff] [blame] | 38 | #include "prototype.h" |
Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 39 | #include "read_config_file.h" |
| 40 | #include "summary.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 41 | |
| 42 | char *command = NULL; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 43 | |
| 44 | int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */ |
| 45 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 46 | static enum callback_status |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 47 | stop_non_p_processes(struct process *proc, void *data) |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 48 | { |
| 49 | int stop = 1; |
| 50 | |
| 51 | struct opt_p_t *it; |
| 52 | for (it = opt_p; it != NULL; it = it->next) { |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 53 | struct process *p_proc = pid2proc(it->pid); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 54 | if (p_proc == NULL) { |
| 55 | printf("stop_non_p_processes: %d terminated?\n", it->pid); |
| 56 | continue; |
| 57 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 58 | if (p_proc == proc || p_proc->leader == proc->leader) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 59 | stop = 0; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (stop) { |
| 65 | debug(2, "Sending SIGSTOP to process %u", proc->pid); |
| 66 | kill(proc->pid, SIGSTOP); |
| 67 | } |
| 68 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 69 | return CBS_CONT; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 72 | static void |
| 73 | signal_alarm(int sig) { |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 74 | signal(SIGALRM, SIG_DFL); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 75 | each_process(NULL, &stop_non_p_processes, NULL); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void |
Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 79 | signal_exit(int sig) |
| 80 | { |
Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 81 | if (exiting != 0) |
| 82 | return; |
| 83 | |
| 84 | exiting = 1 + !!os_ltrace_exiting_sighandler(); |
| 85 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 86 | signal(SIGINT, SIG_IGN); |
| 87 | signal(SIGTERM, SIG_IGN); |
| 88 | signal(SIGALRM, signal_alarm); |
Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 89 | //alarm(1); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void |
Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 93 | normal_exit(void) |
| 94 | { |
Petr Machata | 93eb0ee | 2012-12-13 18:21:36 +0100 | [diff] [blame] | 95 | if (options.summary) |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 96 | show_summary(); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 97 | if (options.output) { |
| 98 | fclose(options.output); |
| 99 | options.output = NULL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void |
Petr Machata | c00837c | 2013-11-11 02:24:42 +0100 | [diff] [blame] | 104 | ltrace_init(int argc, char **argv) |
| 105 | { |
| 106 | setlocale(LC_ALL, ""); |
| 107 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 108 | struct opt_p_t *opt_p_tmp; |
| 109 | |
| 110 | atexit(normal_exit); |
| 111 | signal(SIGINT, signal_exit); /* Detach processes when interrupted */ |
| 112 | signal(SIGTERM, signal_exit); /* ... or killed */ |
| 113 | |
| 114 | argv = process_options(argc, argv); |
Petr Machata | f83053d | 2012-01-06 18:52:55 +0100 | [diff] [blame] | 115 | init_global_config(); |
Petr Machata | 8a98e6f | 2012-10-27 00:30:13 +0200 | [diff] [blame] | 116 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 117 | if (command) { |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 118 | /* Check that the binary ABI is supported before |
| 119 | * calling execute_program. */ |
Petr Machata | 0ba3c5e | 2013-11-11 02:27:08 +0100 | [diff] [blame] | 120 | { |
| 121 | struct ltelf lte; |
| 122 | if (ltelf_init(<e, command) == 0) |
| 123 | ltelf_destroy(<e); |
| 124 | else |
| 125 | exit(EXIT_FAILURE); |
| 126 | } |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 127 | |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 128 | pid_t pid = execute_program(command, argv); |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 129 | struct process *proc = open_program(command, pid); |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 130 | if (proc == NULL) { |
| 131 | fprintf(stderr, "couldn't open program '%s': %s\n", |
| 132 | command, strerror(errno)); |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
Petr Machata | 79a47ba | 2012-04-06 19:54:55 +0200 | [diff] [blame] | 135 | |
| 136 | trace_set_options(proc); |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 137 | continue_process(pid); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 138 | } |
| 139 | opt_p_tmp = opt_p; |
| 140 | while (opt_p_tmp) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 141 | open_pid(opt_p_tmp->pid); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 142 | opt_p_tmp = opt_p_tmp->next; |
| 143 | } |
| 144 | } |
| 145 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 146 | static int num_ltrace_callbacks[EVENT_MAX]; |
| 147 | static callback_func * ltrace_callbacks[EVENT_MAX]; |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 148 | |
| 149 | void |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 150 | ltrace_add_callback(callback_func func, Event_type type) { |
| 151 | ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func)); |
| 152 | ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func; |
| 153 | } |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 154 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 155 | static void |
| 156 | dispatch_callbacks(Event * ev) { |
| 157 | int i; |
| 158 | /* Ignoring case 1: signal into a dying tracer */ |
| 159 | if (ev->type==EVENT_SIGNAL && |
| 160 | exiting && ev->e_un.signum == SIGSTOP) { |
| 161 | return; |
| 162 | } |
| 163 | /* Ignoring case 2: process being born before a clone event */ |
| 164 | if (ev->proc && ev->proc->state == STATE_IGNORED) { |
| 165 | return; |
| 166 | } |
| 167 | for (i=0; i<num_ltrace_callbacks[ev->type]; i++) { |
| 168 | ltrace_callbacks[ev->type][i](ev); |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 172 | void |
| 173 | ltrace_main(void) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 174 | Event * ev; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 175 | while (1) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 176 | ev = next_event(); |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 177 | dispatch_callbacks(ev); |
Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 178 | handle_event(ev); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 179 | } |
| 180 | } |