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