| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 8 | #include <fcntl.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <limits.h> |
| 11 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 12 | #if HAVE_GETOPT_H |
| 13 | #include <getopt.h> |
| 14 | #endif |
| 15 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | #include "ltrace.h" |
| 17 | #include "options.h" |
| 18 | #include "defs.h" |
| 19 | |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 20 | #ifndef SYSCONFDIR |
| 21 | #define SYSCONFDIR "/etc" |
| 22 | #endif |
| 23 | |
| Olaf Hering | e948e58 | 2006-10-12 23:53:44 +0200 | [diff] [blame] | 24 | #define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf" |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 25 | #define USER_CONFIG_FILE "~/.ltrace.conf" |
| 26 | |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 27 | #define MAX_LIBRARY 30 |
| 28 | char *library[MAX_LIBRARY]; |
| 29 | int library_num = 0; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 30 | static char *progname; /* Program name (`ltrace') */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 31 | FILE *output; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 32 | int opt_a = DEFAULT_ACOLUMN; /* alignment column for results */ |
| 33 | int opt_A = DEFAULT_ARRAYLEN; /* maximum # array elements to print */ |
| 34 | int opt_c = 0; /* Report a summary on program exit */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 35 | int opt_d = 0; /* debug */ |
| 36 | int opt_i = 0; /* instruction pointer */ |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 37 | int opt_s = DEFAULT_STRLEN; /* maximum # of bytes printed in strings */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 38 | int opt_S = 0; /* display syscalls */ |
| 39 | int opt_L = 1; /* display library calls */ |
| 40 | int opt_f = 0; /* trace child processes as they are created */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 41 | char *opt_u = NULL; /* username to run command as */ |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 42 | int opt_r = 0; /* print relative timestamp */ |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 43 | int opt_t = 0; /* print absolute timestamp */ |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 44 | #ifdef USE_DEMANGLE |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 45 | int opt_C = 0; /* Demangle low-level symbol names */ |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 46 | #endif |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 47 | int opt_n = 0; /* indent output according to program flow */ |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 48 | int opt_T = 0; /* show the time spent inside each call */ |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 49 | int opt_o = 0; /* output to a specific file */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 50 | |
| 51 | /* List of pids given to option -p: */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 53 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 54 | /* List of function names given to option -e: */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 55 | struct opt_e_t *opt_e = NULL; |
| 56 | int opt_e_enable = 1; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 57 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 58 | /* List of global function names given to -x: */ |
| Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 59 | struct opt_x_t *opt_x = NULL; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 60 | |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 61 | /* List of filenames give to option -F: */ |
| 62 | struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */ |
| 63 | |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 64 | #ifdef PLT_REINITALISATION_BP |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 65 | /* Set a break on the routine named here in order to re-initialize breakpoints |
| 66 | after all the PLTs have been initialzed */ |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 67 | char *PLTs_initialized_by_here = PLT_REINITALISATION_BP; |
| 68 | #endif |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 69 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 70 | static void usage(void) |
| 71 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 72 | #if !(HAVE_GETOPT || HAVE_GETOPT_LONG) |
| 73 | fprintf(stdout, "Usage: %s [command [arg ...]]\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 74 | "Trace library calls of a given program.\n\n", progname); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 75 | #else |
| 76 | fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 77 | "Trace library calls of a given program.\n\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 78 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 79 | " -a, --align=COLUMN align return values in a secific column.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 80 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 81 | " -a COLUMN align return values in a secific column.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 82 | # endif |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 83 | " -A ARRAYLEN maximum number of array elements to print.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | " -c count time and calls, and report a summary on exit.\n" |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 85 | # ifdef USE_DEMANGLE |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 86 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 87 | " -C, --demangle decode low-level symbol names into user-level names.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 88 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 89 | " -C decode low-level symbol names into user-level names.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 90 | # endif |
| 91 | # endif |
| 92 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 93 | " -d, --debug print debugging info.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 94 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 95 | " -d print debugging info.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 96 | # endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 97 | " -e expr modify which events to trace.\n" |
| 98 | " -f follow forks.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 99 | # if HAVE_GETOPT_LONG |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 100 | " -F, --config=FILE load alternate configuration file\n" |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 101 | # else |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 102 | " -F FILE load alternate configuration file\n" |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 103 | # endif |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 104 | " (can be repeated).\n" |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 105 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 106 | " -h, --help display this help and exit.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 107 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 108 | " -h display this help and exit.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 109 | # endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | " -i print instruction pointer at time of library call.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 111 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 112 | " -l, --library=FILE print library calls from this library only.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 113 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 114 | " -l FILE print library calls from this library only.\n" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 115 | # endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 116 | " -L do NOT display library calls.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 117 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 118 | " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 119 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 120 | " -n NR indent output by NR spaces for each call level nesting.\n" |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 121 | # endif |
| 122 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 123 | " -o, --output=FILE write the trace output to that file.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 124 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 125 | " -o FILE write the trace output to that file.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 126 | # endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 127 | " -p PID attach to the process with the process ID pid.\n" |
| 128 | " -r print relative timestamps.\n" |
| 129 | " -s STRLEN specify the maximum string size to print.\n" |
| 130 | " -S display system calls.\n" |
| 131 | " -t, -tt, -ttt print absolute timestamps.\n" |
| 132 | " -T show the time spent inside each call.\n" |
| 133 | " -u USERNAME run command with the userid, groupid of username.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 134 | # if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 135 | " -V, --version output version information and exit.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 136 | # else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 137 | " -V output version information and exit.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 138 | # endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 139 | " -x NAME treat the global NAME like a library subroutine.\n" |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 140 | #ifdef PLT_REINITALISATION_BP |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 141 | " -X NAME same as -x; and PLT's will be initialized by here.\n" |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 142 | #endif |
| 143 | "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 144 | progname); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 145 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 148 | static char *search_for_command(char *filename) |
| 149 | { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 150 | static char pathname[PATH_MAX]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 151 | char *path; |
| 152 | int m, n; |
| 153 | |
| 154 | if (strchr(filename, '/')) { |
| 155 | return filename; |
| 156 | } |
| 157 | for (path = getenv("PATH"); path && *path; path += m) { |
| 158 | if (strchr(path, ':')) { |
| 159 | n = strchr(path, ':') - path; |
| 160 | m = n + 1; |
| 161 | } else { |
| 162 | m = n = strlen(path); |
| 163 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 164 | if (n + strlen(filename) + 1 >= PATH_MAX) { |
| 165 | fprintf(stderr, "Error: filename too long\n"); |
| 166 | exit(1); |
| 167 | } |
| 168 | strncpy(pathname, path, n); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 169 | if (n && pathname[n - 1] != '/') { |
| 170 | pathname[n++] = '/'; |
| 171 | } |
| 172 | strcpy(pathname + n, filename); |
| 173 | if (!access(pathname, X_OK)) { |
| 174 | return pathname; |
| 175 | } |
| 176 | } |
| 177 | return filename; |
| 178 | } |
| 179 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 180 | char **process_options(int argc, char **argv) |
| 181 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 182 | progname = argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 183 | output = stderr; |
| 184 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 185 | #if HAVE_GETOPT || HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 186 | while (1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 187 | int c; |
| 188 | #if HAVE_GETOPT_LONG |
| 189 | int option_index = 0; |
| 190 | static struct option long_options[] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 191 | {"align", 1, 0, 'a'}, |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 192 | {"config", 1, 0, 'F'}, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 193 | {"debug", 0, 0, 'd'}, |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 194 | # ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 195 | {"demangle", 0, 0, 'C'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 196 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 197 | {"indent", 1, 0, 'n'}, |
| 198 | {"help", 0, 0, 'h'}, |
| 199 | {"library", 1, 0, 'l'}, |
| 200 | {"output", 1, 0, 'o'}, |
| 201 | {"version", 0, 0, 'V'}, |
| 202 | {0, 0, 0, 0} |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 203 | }; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 204 | c = getopt_long(argc, argv, "+cdfhiLrStTV" |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 205 | # ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 206 | "C" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 207 | # endif |
| Steve Fink | 1150bc4 | 2006-08-07 06:04:43 +0200 | [diff] [blame] | 208 | "a:A:e:F:l:n:o:p:s:u:x:X:", long_options, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 209 | &option_index); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 210 | #else |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 211 | c = getopt(argc, argv, "+cdfhiLrStTV" |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 212 | # ifdef USE_DEMANGLE |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 213 | "C" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 214 | # endif |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 215 | "a:A:e:F:l:n:o:p:s:u:x:X:"); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 216 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 217 | if (c == -1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 218 | break; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 219 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 220 | switch (c) { |
| 221 | case 'a': |
| 222 | opt_a = atoi(optarg); |
| 223 | break; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 224 | case 'A': |
| Steve Fink | 1150bc4 | 2006-08-07 06:04:43 +0200 | [diff] [blame] | 225 | opt_A = atoi(optarg); |
| 226 | break; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 227 | case 'c': |
| 228 | opt_c++; |
| 229 | break; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 230 | #ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 231 | case 'C': |
| 232 | opt_C++; |
| 233 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 234 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 235 | case 'd': |
| 236 | opt_d++; |
| 237 | break; |
| 238 | case 'e': |
| 239 | { |
| 240 | char *str_e = strdup(optarg); |
| 241 | if (!str_e) { |
| 242 | perror("ltrace: strdup"); |
| 243 | exit(1); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 244 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 245 | if (str_e[0] == '!') { |
| 246 | opt_e_enable = 0; |
| 247 | str_e++; |
| 248 | } |
| 249 | while (*str_e) { |
| 250 | struct opt_e_t *tmp; |
| 251 | char *str2 = strchr(str_e, ','); |
| 252 | if (str2) { |
| 253 | *str2 = '\0'; |
| 254 | } |
| 255 | tmp = malloc(sizeof(struct opt_e_t)); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 256 | if (!tmp) { |
| 257 | perror("ltrace: malloc"); |
| 258 | exit(1); |
| 259 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 260 | tmp->name = str_e; |
| 261 | tmp->next = opt_e; |
| 262 | opt_e = tmp; |
| 263 | if (str2) { |
| 264 | str_e = str2 + 1; |
| 265 | } else { |
| 266 | break; |
| 267 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 268 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | case 'f': |
| 272 | opt_f = 1; |
| 273 | break; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 274 | case 'F': |
| 275 | { |
| 276 | struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t)); |
| 277 | if (!tmp) { |
| 278 | perror("ltrace: malloc"); |
| 279 | exit(1); |
| 280 | } |
| 281 | tmp->filename = strdup(optarg); |
| 282 | tmp->next = opt_F; |
| 283 | opt_F = tmp; |
| 284 | break; |
| 285 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 286 | case 'h': |
| 287 | usage(); |
| 288 | exit(0); |
| 289 | case 'i': |
| 290 | opt_i++; |
| 291 | break; |
| 292 | case 'l': |
| 293 | if (library_num == MAX_LIBRARY) { |
| 294 | fprintf(stderr, |
| 295 | "Too many libraries. Maximum is %i.\n", |
| 296 | MAX_LIBRARY); |
| 297 | exit(1); |
| 298 | } |
| 299 | library[library_num++] = optarg; |
| 300 | break; |
| 301 | case 'L': |
| 302 | opt_L = 0; |
| 303 | break; |
| 304 | case 'n': |
| 305 | opt_n = atoi(optarg); |
| 306 | break; |
| 307 | case 'o': |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 308 | opt_o++; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 309 | output = fopen(optarg, "w"); |
| 310 | if (!output) { |
| 311 | fprintf(stderr, |
| 312 | "Can't open %s for output: %s\n", |
| 313 | optarg, strerror(errno)); |
| 314 | exit(1); |
| 315 | } |
| 316 | setvbuf(output, (char *)NULL, _IOLBF, 0); |
| 317 | fcntl(fileno(output), F_SETFD, FD_CLOEXEC); |
| 318 | break; |
| 319 | case 'p': |
| 320 | { |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 321 | struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 322 | if (!tmp) { |
| 323 | perror("ltrace: malloc"); |
| 324 | exit(1); |
| 325 | } |
| 326 | tmp->pid = atoi(optarg); |
| 327 | tmp->next = opt_p; |
| 328 | opt_p = tmp; |
| 329 | break; |
| 330 | } |
| 331 | case 'r': |
| 332 | opt_r++; |
| 333 | break; |
| 334 | case 's': |
| 335 | opt_s = atoi(optarg); |
| 336 | break; |
| 337 | case 'S': |
| 338 | opt_S = 1; |
| 339 | break; |
| 340 | case 't': |
| 341 | opt_t++; |
| 342 | break; |
| 343 | case 'T': |
| 344 | opt_T++; |
| 345 | break; |
| 346 | case 'u': |
| 347 | opt_u = optarg; |
| 348 | break; |
| 349 | case 'V': |
| 350 | printf("ltrace version " PACKAGE_VERSION ".\n" |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 351 | "Copyright (C) 1997-2007 Juan Cespedes <cespedes@debian.org>.\n" |
| 352 | "This is free software; see the GNU General Public Licence\n" |
| 353 | "version 2 or later for copying conditions. There is NO warranty.\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 354 | exit(0); |
| 355 | case 'X': |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 356 | #ifdef PLT_REINITALISATION_BP |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 357 | PLTs_initialized_by_here = optarg; |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 358 | #else |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 359 | fprintf(stderr, "WARNING: \"-X\" not used for this " |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 360 | "architecture: assuming you meant \"-x\"\n"); |
| 361 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 362 | /* Fall Thru */ |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 363 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 364 | case 'x': |
| 365 | { |
| Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 366 | struct opt_x_t *p = opt_x; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 367 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 368 | /* First, check for duplicate. */ |
| 369 | while (p && strcmp(p->name, optarg)) { |
| 370 | p = p->next; |
| 371 | } |
| 372 | if (p) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 376 | /* If not duplicate, add to list. */ |
| Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 377 | p = malloc(sizeof(struct opt_x_t)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 378 | if (!p) { |
| 379 | perror("ltrace: malloc"); |
| 380 | exit(1); |
| 381 | } |
| 382 | p->name = optarg; |
| Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 383 | p->found = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 384 | p->next = opt_x; |
| 385 | opt_x = p; |
| 386 | break; |
| 387 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 388 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 389 | default: |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 390 | #if HAVE_GETOPT_LONG |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 391 | fprintf(stderr, |
| 392 | "Try `%s --help' for more information\n", |
| 393 | progname); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 394 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 395 | fprintf(stderr, "Try `%s -h' for more information\n", |
| 396 | progname); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 397 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 398 | exit(1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 399 | } |
| 400 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 401 | argc -= optind; |
| 402 | argv += optind; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 403 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 404 | |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 405 | if (!opt_F) { |
| 406 | opt_F = malloc(sizeof(struct opt_F_t)); |
| 407 | opt_F->next = malloc(sizeof(struct opt_F_t)); |
| 408 | opt_F->next->next = NULL; |
| 409 | opt_F->filename = USER_CONFIG_FILE; |
| 410 | opt_F->next->filename = SYSTEM_CONFIG_FILE; |
| 411 | } |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 412 | /* Reverse the config file list since it was built by |
| 413 | * prepending, and it would make more sense to process the |
| 414 | * files in the order they were given. Probably it would make |
| 415 | * more sense to keep a tail pointer instead? */ |
| 416 | { |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame^] | 417 | struct opt_F_t *egg = NULL; |
| 418 | struct opt_F_t *chicken; |
| 419 | while (opt_F) { |
| 420 | chicken = opt_F->next; |
| 421 | opt_F->next = egg; |
| 422 | egg = opt_F; |
| 423 | opt_F = chicken; |
| 424 | } |
| 425 | opt_F = egg; |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 428 | if (!opt_p && argc < 1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 429 | fprintf(stderr, "%s: too few arguments\n", progname); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 430 | usage(); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 431 | exit(1); |
| 432 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 433 | if (opt_r && opt_t) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 434 | fprintf(stderr, "%s: Incompatible options -r and -t\n", |
| 435 | progname); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 436 | exit(1); |
| 437 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 438 | if (argc > 0) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 439 | command = search_for_command(argv[0]); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 440 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 441 | return &argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 442 | } |