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