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