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