| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 5 | #include <stdio.h> |
| Juan Cespedes | 23658aa | 1997-08-27 22:27:36 +0200 | [diff] [blame] | 6 | #include <stdlib.h> |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 7 | #include <unistd.h> |
| Juan Cespedes | 23658aa | 1997-08-27 22:27:36 +0200 | [diff] [blame] | 8 | #include <string.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <sys/param.h> |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 11 | #include <signal.h> |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 12 | #include <sys/wait.h> |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 14 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 15 | #include "ltrace.h" |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 16 | #include "output.h" |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 17 | #include "read_config_file.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 18 | #include "options.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 19 | #include "debug.h" |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 20 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 21 | char *command = NULL; |
| 22 | struct process *list_of_processes = NULL; |
| Juan Cespedes | 24c8253 | 1997-06-25 00:02:58 +0200 | [diff] [blame] | 23 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 24 | int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */ |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 25 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | static void signal_alarm(int sig) |
| 27 | { |
| 28 | struct process *tmp = list_of_processes; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 29 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | signal(SIGALRM, SIG_DFL); |
| 31 | while (tmp) { |
| 32 | struct opt_p_t *tmp2 = opt_p; |
| 33 | while (tmp2) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 34 | if (tmp->pid == tmp2->pid) { |
| 35 | tmp = tmp->next; |
| 36 | if (!tmp) { |
| 37 | return; |
| 38 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 39 | tmp2 = opt_p; |
| 40 | continue; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 41 | } |
| 42 | tmp2 = tmp2->next; |
| 43 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | debug(2, "Sending SIGSTOP to process %u\n", tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 45 | kill(tmp->pid, SIGSTOP); |
| 46 | tmp = tmp->next; |
| 47 | } |
| 48 | } |
| 49 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 50 | static void signal_exit(int sig) |
| 51 | { |
| 52 | exiting = 1; |
| 53 | debug(1, "Received interrupt signal; exiting..."); |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 54 | if (opt_o) { |
| 55 | fclose(output); |
| 56 | opt_o = 0; |
| 57 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 58 | signal(SIGINT, SIG_IGN); |
| 59 | signal(SIGTERM, SIG_IGN); |
| 60 | signal(SIGALRM, signal_alarm); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 61 | if (opt_p) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 62 | struct opt_p_t *tmp = opt_p; |
| 63 | while (tmp) { |
| 64 | debug(2, "Sending SIGSTOP to process %u\n", tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 65 | kill(tmp->pid, SIGSTOP); |
| 66 | tmp = tmp->next; |
| 67 | } |
| 68 | } |
| 69 | alarm(1); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 72 | static void normal_exit(void) |
| 73 | { |
| 74 | output_line(0, 0); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 75 | if (opt_c) { |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 76 | show_summary(); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 77 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 78 | if (opt_o) { |
| 79 | fclose(output); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame^] | 80 | opt_o = 0; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 81 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 82 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 83 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | static void guess_cols(void) |
| 85 | { |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 86 | struct winsize ws; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 87 | char *c; |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 88 | |
| 89 | opt_a = DEFAULT_ACOLUMN; |
| 90 | c = getenv("COLUMNS"); |
| 91 | if (c && *c) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 92 | char *endptr; |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 93 | int cols; |
| 94 | cols = strtol(c, &endptr, 0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 95 | if (cols > 0 && !*endptr) { |
| 96 | opt_a = cols * 5 / 8; |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 97 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 98 | } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { |
| 99 | opt_a = ws.ws_col * 5 / 8; |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 103 | int main(int argc, char **argv) |
| 104 | { |
| 105 | struct opt_p_t *opt_p_tmp; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 106 | |
| 107 | atexit(normal_exit); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 108 | signal(SIGINT, signal_exit); /* Detach processes when interrupted */ |
| 109 | signal(SIGTERM, signal_exit); /* ... or killed */ |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 110 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 111 | guess_cols(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 112 | argv = process_options(argc, argv); |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 113 | while (opt_F) { |
| 114 | /* If filename begins with ~, expand it to the user's home */ |
| 115 | /* directory. This does not correctly handle ~yoda, but that */ |
| 116 | /* isn't as bad as it seems because the shell will normally */ |
| 117 | /* be doing the expansion for us; only the hardcoded */ |
| 118 | /* ~/.ltrace.conf should ever use this code. */ |
| 119 | if (opt_F->filename[0] == '~') { |
| 120 | char path[PATH_MAX]; |
| 121 | char *home_dir = getenv("HOME"); |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 122 | if (home_dir) { |
| 123 | strncpy(path, home_dir, PATH_MAX - 1); |
| 124 | path[PATH_MAX - 1] = '\0'; |
| 125 | strncat(path, opt_F->filename + 1, |
| 126 | PATH_MAX - strlen(path) - 1); |
| 127 | read_config_file(path); |
| 128 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 129 | } else { |
| 130 | read_config_file(opt_F->filename); |
| 131 | } |
| 132 | opt_F = opt_F->next; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 133 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 134 | if (opt_e) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 135 | struct opt_e_t *tmp = opt_e; |
| 136 | while (tmp) { |
| 137 | debug(1, "Option -e: %s\n", tmp->name); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 138 | tmp = tmp->next; |
| 139 | } |
| 140 | } |
| 141 | if (command) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 142 | execute_program(open_program(command, 0), argv); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 143 | } |
| 144 | opt_p_tmp = opt_p; |
| 145 | while (opt_p_tmp) { |
| 146 | open_pid(opt_p_tmp->pid, 1); |
| 147 | opt_p_tmp = opt_p_tmp->next; |
| 148 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 149 | while (1) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 150 | process_event(wait_for_something()); |
| 151 | } |
| 152 | } |