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