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