| 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> |
| 8 | #include <errno.h> |
| 9 | #include <limits.h> |
| 10 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 11 | #if HAVE_GETOPT_H |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame^] | 12 | #define _GNU_SOURCE |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 13 | #include <getopt.h> |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame^] | 14 | #undef _GNU_SOURCE |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 15 | #endif |
| 16 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 17 | #include "ltrace.h" |
| 18 | #include "options.h" |
| 19 | #include "defs.h" |
| 20 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 21 | static char *progname; /* Program name (`ltrace') */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 22 | FILE * output; |
| 23 | int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */ |
| 24 | int opt_d = 0; /* debug */ |
| 25 | int opt_i = 0; /* instruction pointer */ |
| 26 | int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */ |
| 27 | int opt_S = 0; /* display syscalls */ |
| 28 | int opt_L = 1; /* display library calls */ |
| 29 | int opt_f = 0; /* trace child processes as they are created */ |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 30 | char * opt_u = NULL; /* username to run command as */ |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 31 | int opt_r = 0; /* print relative timestamp */ |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 32 | int opt_t = 0; /* print absolute timestamp */ |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 33 | #if HAVE_LIBIBERTY |
| 34 | int opt_C = 0; /* Demangle low-level symbol names into user-level names */ |
| 35 | #endif |
| 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 |
| 86 | " -h, --help display this help and exit.\n" |
| 87 | # else |
| 88 | " -h display this help and exit.\n" |
| 89 | # endif |
| 90 | # if HAVE_GETOPT_LONG |
| 91 | " -V, --version output version information and exit.\n" |
| 92 | # else |
| 93 | " -V output version information and exit.\n" |
| 94 | # endif |
| 95 | "\nReport bugs to Juan Cespedes <cespedes@debian.org>\n" |
| 96 | , progname); |
| 97 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static char * search_for_command(char * filename) |
| 101 | { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame^] | 102 | static char pathname[1024]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 103 | char *path; |
| 104 | int m, n; |
| 105 | |
| 106 | if (strchr(filename, '/')) { |
| 107 | return filename; |
| 108 | } |
| 109 | for (path = getenv("PATH"); path && *path; path += m) { |
| 110 | if (strchr(path, ':')) { |
| 111 | n = strchr(path, ':') - path; |
| 112 | m = n + 1; |
| 113 | } else { |
| 114 | m = n = strlen(path); |
| 115 | } |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame^] | 116 | strncpy(pathname, path, n); /* Possible buffer overrun */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 117 | if (n && pathname[n - 1] != '/') { |
| 118 | pathname[n++] = '/'; |
| 119 | } |
| 120 | strcpy(pathname + n, filename); |
| 121 | if (!access(pathname, X_OK)) { |
| 122 | return pathname; |
| 123 | } |
| 124 | } |
| 125 | return filename; |
| 126 | } |
| 127 | |
| 128 | char ** process_options(int argc, char **argv) |
| 129 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 130 | progname = argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 131 | output = stderr; |
| 132 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 133 | #if HAVE_GETOPT || HAVE_GETOPT_LONG |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 134 | while(1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 135 | int c; |
| 136 | #if HAVE_GETOPT_LONG |
| 137 | int option_index = 0; |
| 138 | static struct option long_options[] = { |
| 139 | { "align", 1, 0, 'a'}, |
| 140 | { "debug", 0, 0, 'd'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 141 | # if HAVE_LIBIBERTY |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 142 | { "demangle", 0, 0, 'C'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 143 | #endif |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 144 | { "help", 0, 0, 'h'}, |
| 145 | { "output", 1, 0, 'o'}, |
| 146 | { "version", 0, 0, 'V'}, |
| 147 | { 0, 0, 0, 0} |
| 148 | }; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 149 | c = getopt_long(argc, argv, "+dfiLSrthV" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 150 | # if HAVE_LIBIBERTY |
| 151 | "C" |
| 152 | # endif |
| 153 | "a:s:o:u:p:e:", long_options, &option_index); |
| 154 | #else |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 155 | c = getopt(argc, argv, "+dfiLSrthV" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 156 | # if HAVE_LIBIBERTY |
| 157 | "C" |
| 158 | # endif |
| 159 | "a:s:o:u:p:e:"); |
| 160 | #endif |
| 161 | if (c==-1) { |
| 162 | break; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 163 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 164 | switch(c) { |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 165 | case 'd': opt_d++; |
| 166 | break; |
| 167 | case 'f': opt_f = 1; |
| 168 | break; |
| 169 | case 'i': opt_i++; |
| 170 | break; |
| 171 | case 'L': opt_L = 0; |
| 172 | break; |
| 173 | case 'S': opt_S = 1; |
| 174 | break; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 175 | case 'r': opt_r++; |
| 176 | break; |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 177 | case 't': opt_t++; |
| 178 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 179 | #if HAVE_LIBIBERTY |
| 180 | case 'C': opt_C++; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 181 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 182 | #endif |
| 183 | case 'a': opt_a = atoi(optarg); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 184 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 185 | case 's': opt_s = atoi(optarg); |
| 186 | break; |
| 187 | case 'h': usage(); |
| 188 | exit(0); |
| 189 | case 'o': output = fopen(optarg, "w"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 190 | if (!output) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 191 | 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] | 192 | exit(1); |
| 193 | } |
| Juan Cespedes | 64c6dfb | 1998-07-14 13:49:47 +0200 | [diff] [blame] | 194 | setvbuf(output, (char *)NULL, _IOLBF, 0); |
| 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 | } |