Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 1 | #include "config.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 2 | |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 3 | #include <sys/param.h> |
| 4 | #include <sys/wait.h> |
| 5 | #include <errno.h> |
| 6 | #include <error.h> |
| 7 | #include <signal.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 10 | #include <string.h> |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 11 | #include <unistd.h> |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 12 | |
| 13 | #include "common.h" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 14 | #include "proc.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 15 | |
| 16 | char *command = NULL; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 17 | |
| 18 | int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */ |
| 19 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 20 | static enum callback_status |
| 21 | stop_non_p_processes(Process *proc, void *data) |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 22 | { |
| 23 | int stop = 1; |
| 24 | |
| 25 | struct opt_p_t *it; |
| 26 | for (it = opt_p; it != NULL; it = it->next) { |
| 27 | Process * p_proc = pid2proc(it->pid); |
| 28 | if (p_proc == NULL) { |
| 29 | printf("stop_non_p_processes: %d terminated?\n", it->pid); |
| 30 | continue; |
| 31 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 32 | if (p_proc == proc || p_proc->leader == proc->leader) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 33 | stop = 0; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (stop) { |
| 39 | debug(2, "Sending SIGSTOP to process %u", proc->pid); |
| 40 | kill(proc->pid, SIGSTOP); |
| 41 | } |
| 42 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 43 | return CBS_CONT; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 46 | static void |
| 47 | signal_alarm(int sig) { |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 48 | signal(SIGALRM, SIG_DFL); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 49 | each_process(NULL, &stop_non_p_processes, NULL); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void |
Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 53 | signal_exit(int sig) |
| 54 | { |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 55 | debug(1, "Received interrupt signal; exiting..."); |
Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 56 | if (exiting != 0) |
| 57 | return; |
| 58 | |
| 59 | exiting = 1 + !!os_ltrace_exiting_sighandler(); |
| 60 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 61 | signal(SIGINT, SIG_IGN); |
| 62 | signal(SIGTERM, SIG_IGN); |
| 63 | signal(SIGALRM, signal_alarm); |
Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 64 | //alarm(1); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void |
| 68 | normal_exit(void) { |
| 69 | output_line(0, 0); |
| 70 | if (options.summary) { |
| 71 | show_summary(); |
| 72 | } |
| 73 | if (options.output) { |
| 74 | fclose(options.output); |
| 75 | options.output = NULL; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | ltrace_init(int argc, char **argv) { |
| 81 | struct opt_p_t *opt_p_tmp; |
| 82 | |
| 83 | atexit(normal_exit); |
| 84 | signal(SIGINT, signal_exit); /* Detach processes when interrupted */ |
| 85 | signal(SIGTERM, signal_exit); /* ... or killed */ |
| 86 | |
| 87 | argv = process_options(argc, argv); |
| 88 | while (opt_F) { |
| 89 | /* If filename begins with ~, expand it to the user's home */ |
| 90 | /* directory. This does not correctly handle ~yoda, but that */ |
| 91 | /* isn't as bad as it seems because the shell will normally */ |
| 92 | /* be doing the expansion for us; only the hardcoded */ |
| 93 | /* ~/.ltrace.conf should ever use this code. */ |
| 94 | if (opt_F->filename[0] == '~') { |
| 95 | char path[PATH_MAX]; |
| 96 | char *home_dir = getenv("HOME"); |
| 97 | if (home_dir) { |
| 98 | strncpy(path, home_dir, PATH_MAX - 1); |
| 99 | path[PATH_MAX - 1] = '\0'; |
| 100 | strncat(path, opt_F->filename + 1, |
| 101 | PATH_MAX - strlen(path) - 1); |
| 102 | read_config_file(path); |
| 103 | } |
| 104 | } else { |
| 105 | read_config_file(opt_F->filename); |
| 106 | } |
| 107 | opt_F = opt_F->next; |
| 108 | } |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 109 | if (command) { |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 110 | /* Check that the binary ABI is supported before |
| 111 | * calling execute_program. */ |
| 112 | struct ltelf lte = {}; |
| 113 | open_elf(<e, command); |
| 114 | |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 115 | pid_t pid = execute_program(command, argv); |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 116 | struct Process *proc = open_program(command, pid); |
Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 117 | if (proc == NULL) |
| 118 | error(EXIT_FAILURE, errno, |
| 119 | "couldn't open program '%s'", command); |
Petr Machata | 79a47ba | 2012-04-06 19:54:55 +0200 | [diff] [blame] | 120 | |
| 121 | trace_set_options(proc); |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 122 | continue_process(pid); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 123 | } |
| 124 | opt_p_tmp = opt_p; |
| 125 | while (opt_p_tmp) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 126 | open_pid(opt_p_tmp->pid); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 127 | opt_p_tmp = opt_p_tmp->next; |
| 128 | } |
| 129 | } |
| 130 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 131 | static int num_ltrace_callbacks[EVENT_MAX]; |
| 132 | static callback_func * ltrace_callbacks[EVENT_MAX]; |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 133 | |
| 134 | void |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 135 | ltrace_add_callback(callback_func func, Event_type type) { |
| 136 | ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func)); |
| 137 | ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func; |
| 138 | } |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 139 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 140 | static void |
| 141 | dispatch_callbacks(Event * ev) { |
| 142 | int i; |
| 143 | /* Ignoring case 1: signal into a dying tracer */ |
| 144 | if (ev->type==EVENT_SIGNAL && |
| 145 | exiting && ev->e_un.signum == SIGSTOP) { |
| 146 | return; |
| 147 | } |
| 148 | /* Ignoring case 2: process being born before a clone event */ |
| 149 | if (ev->proc && ev->proc->state == STATE_IGNORED) { |
| 150 | return; |
| 151 | } |
| 152 | for (i=0; i<num_ltrace_callbacks[ev->type]; i++) { |
| 153 | ltrace_callbacks[ev->type][i](ev); |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 157 | void |
| 158 | ltrace_main(void) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 159 | Event * ev; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 160 | while (1) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 161 | ev = next_event(); |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 162 | dispatch_callbacks(ev); |
Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 163 | handle_event(ev); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 164 | } |
| 165 | } |