| 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 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 20 | static char *progname; /* Program name (`ltrace') */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 21 | FILE * output; |
| 22 | int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */ |
| 23 | int opt_d = 0; /* debug */ |
| 24 | int opt_i = 0; /* instruction pointer */ |
| 25 | int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */ |
| 26 | int opt_S = 0; /* display syscalls */ |
| 27 | int opt_L = 1; /* display library calls */ |
| 28 | int opt_f = 0; /* trace child processes as they are created */ |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 29 | char * opt_u = NULL; /* username to run command as */ |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 30 | int opt_r = 0; /* print relative timestamp */ |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 31 | int opt_t = 0; /* print absolute timestamp */ |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 32 | #if HAVE_LIBIBERTY |
| 33 | int opt_C = 0; /* Demangle low-level symbol names into user-level names */ |
| 34 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 35 | |
| 36 | /* List of pids given to option -p: */ |
| 37 | struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */ |
| 38 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 39 | /* List of function names given to option -e: */ |
| 40 | struct opt_e_t * opt_e = NULL; |
| 41 | int opt_e_enable=1; |
| 42 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 43 | static void usage(void) |
| 44 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 45 | #if !(HAVE_GETOPT || HAVE_GETOPT_LONG) |
| 46 | fprintf(stdout, "Usage: %s [command [arg ...]]\n" |
| 47 | "Trace library calls of a given program.\n\n", progname); |
| 48 | #else |
| 49 | fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" |
| 50 | "Trace library calls of a given program.\n\n" |
| 51 | |
| 52 | # if HAVE_GETOPT_LONG |
| 53 | " -d, --debug print debugging info.\n" |
| 54 | # else |
| 55 | " -d print debugging info.\n" |
| 56 | # endif |
| 57 | " -f follow forks.\n" |
| 58 | " -i print instruction pointer at time of library call.\n" |
| 59 | " -L do NOT display library calls.\n" |
| 60 | " -S display system calls.\n" |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 61 | " -r print relative timestamps.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 62 | " -t, -tt, -ttt print absolute timestamps.\n" |
| 63 | # if HAVE_LIBIBERTY |
| 64 | # if HAVE_GETOPT_LONG |
| 65 | " -C, --demangle decode low-level symbol names into user-level names.\n" |
| 66 | # else |
| 67 | " -C decode low-level symbol names into user-level names.\n" |
| 68 | # endif |
| 69 | # endif |
| 70 | # if HAVE_GETOPT_LONG |
| 71 | " -a, --align=COLUMN align return values in a secific column.\n" |
| 72 | # else |
| 73 | " -a COLUMN align return values in a secific column.\n" |
| 74 | # endif |
| 75 | " -s STRLEN specify the maximum string size to print.\n" |
| 76 | # if HAVE_GETOPT_LONG |
| 77 | " -o, --output=FILE write the trace output to that file.\n" |
| 78 | # else |
| 79 | " -o FILE write the trace output to that file.\n" |
| 80 | # endif |
| 81 | " -u USERNAME run command with the userid, groupid of username.\n" |
| 82 | " -p PID attach to the process with the process ID pid.\n" |
| 83 | " -e expr modify which events to trace.\n" |
| 84 | # if HAVE_GETOPT_LONG |
| 85 | " -h, --help display this help and exit.\n" |
| 86 | # else |
| 87 | " -h display this help and exit.\n" |
| 88 | # endif |
| 89 | # if HAVE_GETOPT_LONG |
| 90 | " -V, --version output version information and exit.\n" |
| 91 | # else |
| 92 | " -V output version information and exit.\n" |
| 93 | # endif |
| 94 | "\nReport bugs to Juan Cespedes <cespedes@debian.org>\n" |
| 95 | , progname); |
| 96 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static char * search_for_command(char * filename) |
| 100 | { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 101 | static char pathname[1024]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 102 | char *path; |
| 103 | int m, n; |
| 104 | |
| 105 | if (strchr(filename, '/')) { |
| 106 | return filename; |
| 107 | } |
| 108 | for (path = getenv("PATH"); path && *path; path += m) { |
| 109 | if (strchr(path, ':')) { |
| 110 | n = strchr(path, ':') - path; |
| 111 | m = n + 1; |
| 112 | } else { |
| 113 | m = n = strlen(path); |
| 114 | } |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 115 | strncpy(pathname, path, n); /* Possible buffer overrun */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 116 | if (n && pathname[n - 1] != '/') { |
| 117 | pathname[n++] = '/'; |
| 118 | } |
| 119 | strcpy(pathname + n, filename); |
| 120 | if (!access(pathname, X_OK)) { |
| 121 | return pathname; |
| 122 | } |
| 123 | } |
| 124 | return filename; |
| 125 | } |
| 126 | |
| 127 | char ** process_options(int argc, char **argv) |
| 128 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 129 | progname = argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 130 | output = stderr; |
| 131 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 132 | #if HAVE_GETOPT || HAVE_GETOPT_LONG |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 133 | while(1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 134 | int c; |
| 135 | #if HAVE_GETOPT_LONG |
| 136 | int option_index = 0; |
| 137 | static struct option long_options[] = { |
| 138 | { "align", 1, 0, 'a'}, |
| 139 | { "debug", 0, 0, 'd'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 140 | # if HAVE_LIBIBERTY |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 141 | { "demangle", 0, 0, 'C'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 142 | #endif |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 143 | { "help", 0, 0, 'h'}, |
| 144 | { "output", 1, 0, 'o'}, |
| 145 | { "version", 0, 0, 'V'}, |
| 146 | { 0, 0, 0, 0} |
| 147 | }; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 148 | c = getopt_long(argc, argv, "+dfiLSrthV" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 149 | # if HAVE_LIBIBERTY |
| 150 | "C" |
| 151 | # endif |
| 152 | "a:s:o:u:p:e:", long_options, &option_index); |
| 153 | #else |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 154 | c = getopt(argc, argv, "+dfiLSrthV" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 155 | # if HAVE_LIBIBERTY |
| 156 | "C" |
| 157 | # endif |
| 158 | "a:s:o:u:p:e:"); |
| 159 | #endif |
| 160 | if (c==-1) { |
| 161 | break; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 162 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 163 | switch(c) { |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 164 | case 'd': opt_d++; |
| 165 | break; |
| 166 | case 'f': opt_f = 1; |
| 167 | break; |
| 168 | case 'i': opt_i++; |
| 169 | break; |
| 170 | case 'L': opt_L = 0; |
| 171 | break; |
| 172 | case 'S': opt_S = 1; |
| 173 | break; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 174 | case 'r': opt_r++; |
| 175 | break; |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 176 | case 't': opt_t++; |
| 177 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 178 | #if HAVE_LIBIBERTY |
| 179 | case 'C': opt_C++; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 180 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 181 | #endif |
| 182 | case 'a': opt_a = atoi(optarg); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 183 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 184 | case 's': opt_s = atoi(optarg); |
| 185 | break; |
| 186 | case 'h': usage(); |
| 187 | exit(0); |
| 188 | case 'o': output = fopen(optarg, "w"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 189 | if (!output) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 190 | fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno)); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 191 | exit(1); |
| 192 | } |
| Juan Cespedes | 64c6dfb | 1998-07-14 13:49:47 +0200 | [diff] [blame] | 193 | setvbuf(output, (char *)NULL, _IOLBF, 0); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame^] | 194 | fcntl(fileno(output), F_SETFD, FD_CLOEXEC); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 195 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 196 | case 'u': opt_u = optarg; |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 197 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 198 | case 'p': |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 199 | { |
| 200 | struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t)); |
| 201 | if (!tmp) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 202 | perror("ltrace: malloc"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 203 | exit(1); |
| 204 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 205 | tmp->pid = atoi(optarg); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 206 | tmp->next = opt_p; |
| 207 | opt_p = tmp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 208 | break; |
| 209 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 210 | case 'e': |
| 211 | { |
| 212 | char * str_e = strdup(optarg); |
| 213 | if (!str_e) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 214 | perror("ltrace: strdup"); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 215 | exit(1); |
| 216 | } |
| 217 | if (str_e[0]=='!') { |
| 218 | opt_e_enable=0; |
| 219 | str_e++; |
| 220 | } |
| 221 | while(*str_e) { |
| 222 | struct opt_e_t * tmp; |
| 223 | char *str2 = strchr(str_e,','); |
| 224 | if (str2) { |
| 225 | *str2 = '\0'; |
| 226 | } |
| 227 | tmp = malloc(sizeof(struct opt_e_t)); |
| 228 | if (!tmp) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 229 | perror("ltrace: malloc"); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 230 | exit(1); |
| 231 | } |
| 232 | tmp->name = str_e; |
| 233 | tmp->next = opt_e; |
| 234 | opt_e = tmp; |
| 235 | if (str2) { |
| 236 | str_e = str2+1; |
| 237 | } else { |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | break; |
| 242 | } |
| 243 | case 'V': printf("ltrace version " |
| 244 | #ifdef VERSION |
| 245 | VERSION |
| 246 | #else |
| 247 | "???" |
| 248 | #endif |
| 249 | ".\n" |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 250 | "Copyright (C) 1997-1999 Juan Cespedes <cespedes@debian.org>.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 251 | "This is free software; see the GNU General Public Licence\n" |
| 252 | "version 2 or later for copying conditions. There is NO warranty.\n"); |
| 253 | exit(0); |
| 254 | |
| 255 | default: |
| 256 | #if HAVE_GETOPT_LONG |
| 257 | fprintf(stderr, "Try `%s --help' for more information\n", progname); |
| 258 | #else |
| 259 | fprintf(stderr, "Try `%s -h' for more information\n", progname); |
| 260 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 261 | exit(1); |
| 262 | } |
| 263 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 264 | argc -= optind; argv += optind; |
| 265 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 266 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 267 | if (!opt_p && argc<1) { |
| 268 | fprintf(stderr, "%s: too few arguments\n", progname); |
| 269 | #if HAVE_GETOPT_LONG |
| 270 | fprintf(stderr, "Try `%s --help' for more information\n", progname); |
| 271 | #elif HAVE_GETOPT |
| 272 | fprintf(stderr, "Try `%s -h' for more information\n", progname); |
| 273 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 274 | exit(1); |
| 275 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 276 | if (opt_r && opt_t) { |
| 277 | fprintf(stderr, "%s: Incompatible options -r and -t\n", progname); |
| 278 | exit(1); |
| 279 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 280 | if (argc>0) { |
| 281 | command = search_for_command(argv[0]); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 282 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 283 | return &argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 284 | } |