options.c revision ab3b72cc5d2d3efb3542192f0d72ff2ea4b082f9
1#include "config.h" 2 3#include <string.h> 4#include <stdlib.h> 5#include <unistd.h> 6#include <fcntl.h> 7#include <errno.h> 8#include <limits.h> 9#include <sys/ioctl.h> 10 11#include <getopt.h> 12 13#include "common.h" 14 15#ifndef SYSCONFDIR 16#define SYSCONFDIR "/etc" 17#endif 18 19#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf" 20#define USER_CONFIG_FILE "~/.ltrace.conf" 21 22struct options_t options = { 23 .align = DEFAULT_ALIGN, /* alignment column for results */ 24 .user = NULL, /* username to run command as */ 25 .syscalls = 0, /* display syscalls */ 26 .libcalls = 1, /* display library calls */ 27#ifdef USE_DEMANGLE 28 .demangle = 0, /* Demangle low-level symbol names */ 29#endif 30 .indent = 0, /* indent output according to program flow */ 31 .output = NULL, /* output to a specific file */ 32 .summary = 0, /* Report a summary on program exit */ 33 .debug = 0, /* debug */ 34 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */ 35 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */ 36 .follow = 0, /* trace child processes */ 37}; 38 39char *library[MAX_LIBRARIES]; 40int library_num = 0; 41static char *progname; /* Program name (`ltrace') */ 42int opt_i = 0; /* instruction pointer */ 43int opt_r = 0; /* print relative timestamp */ 44int opt_t = 0; /* print absolute timestamp */ 45int opt_T = 0; /* show the time spent inside each call */ 46 47/* List of pids given to option -p: */ 48struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */ 49 50/* List of function names given to option -e: */ 51struct opt_e_t *opt_e = NULL; 52int opt_e_enable = 1; 53 54/* List of global function names given to -x: */ 55struct opt_x_t *opt_x = NULL; 56 57/* List of filenames give to option -F: */ 58struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */ 59 60#ifdef PLT_REINITALISATION_BP 61/* Set a break on the routine named here in order to re-initialize breakpoints 62 after all the PLTs have been initialzed */ 63char *PLTs_initialized_by_here = PLT_REINITALISATION_BP; 64#endif 65 66static void 67err_usage(void) { 68 fprintf(stderr, "Try `%s --help' for more information\n", progname); 69 exit(1); 70} 71 72static void 73usage(void) { 74 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" 75 "Trace library calls of a given program.\n\n" 76 " -a, --align=COLUMN align return values in a secific column.\n" 77 " -A ARRAYLEN maximum number of array elements to print.\n" 78 " -b, --no-signals don't print signals.\n" 79 " -c count time and calls, and report a summary on exit.\n" 80# ifdef USE_DEMANGLE 81 " -C, --demangle decode low-level symbol names into user-level names.\n" 82# endif 83 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n" 84 " -Dh, --debug=help show help on debugging.\n" 85 " -e expr modify which events to trace.\n" 86 " -f trace children (fork() and clone()).\n" 87 " -F, --config=FILE load alternate configuration file (may be repeated).\n" 88 " -g, --no-plt disable breakpoints on PLT entries.\n" 89 " -h, --help display this help and exit.\n" 90 " -i print instruction pointer at time of library call.\n" 91 " -l, --library=FILE print library calls from this library only.\n" 92 " -L do NOT display library calls.\n" 93 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" 94 " -o, --output=FILE write the trace output to that file.\n" 95 " -p PID attach to the process with the process ID pid.\n" 96 " -r print relative timestamps.\n" 97 " -s STRLEN specify the maximum string size to print.\n" 98 " -S display system calls.\n" 99 " -t, -tt, -ttt print absolute timestamps.\n" 100 " -T show the time spent inside each call.\n" 101 " -u USERNAME run command with the userid, groupid of username.\n" 102 " -V, --version output version information and exit.\n" 103#if defined(HAVE_LIBUNWIND) 104 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n" 105#endif /* defined(HAVE_LIBUNWIND) */ 106 " -x NAME treat the global NAME like a library subroutine.\n" 107#ifdef PLT_REINITALISATION_BP 108 " -X NAME same as -x; and PLT's will be initialized by here.\n" 109#endif 110 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", 111 progname); 112} 113 114static void 115usage_debug(void) { 116 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname); 117 fprintf(stdout, 118 "\n" 119 " number ref. in source description\n" 120 " 1 general Generally helpful progress information\n" 121 " 10 event Shows every event received by a traced process\n" 122 " 20 process Shows actions carried upon a traced processes\n" 123 " 40 function Shows every entry to internal functions\n" 124 "\n" 125 "Debugging options are mixed using bitwise-or.\n" 126 "Note that the meanings and values are subject to change.\n" 127 ); 128} 129 130static char * 131search_for_command(char *filename) { 132 static char pathname[PATH_MAX]; 133 char *path; 134 int m, n; 135 136 if (strchr(filename, '/')) { 137 return filename; 138 } 139 for (path = getenv("PATH"); path && *path; path += m) { 140 if (strchr(path, ':')) { 141 n = strchr(path, ':') - path; 142 m = n + 1; 143 } else { 144 m = n = strlen(path); 145 } 146 if (n + strlen(filename) + 1 >= PATH_MAX) { 147 fprintf(stderr, "Error: filename too long\n"); 148 exit(1); 149 } 150 strncpy(pathname, path, n); 151 if (n && pathname[n - 1] != '/') { 152 pathname[n++] = '/'; 153 } 154 strcpy(pathname + n, filename); 155 if (!access(pathname, X_OK)) { 156 return pathname; 157 } 158 } 159 return filename; 160} 161 162static void 163guess_cols(void) { 164 struct winsize ws; 165 char *c; 166 167 options.align = DEFAULT_ALIGN; 168 c = getenv("COLUMNS"); 169 if (c && *c) { 170 char *endptr; 171 int cols; 172 cols = strtol(c, &endptr, 0); 173 if (cols > 0 && !*endptr) { 174 options.align = cols * 5 / 8; 175 } 176 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 177 options.align = ws.ws_col * 5 / 8; 178 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 179 options.align = ws.ws_col * 5 / 8; 180 } 181} 182 183char ** 184process_options(int argc, char **argv) { 185 progname = argv[0]; 186 options.output = stderr; 187 options.no_plt = 0; 188 options.no_signals = 0; 189#if defined(HAVE_LIBUNWIND) 190 options.bt_depth = -1; 191#endif /* defined(HAVE_LIBUNWIND) */ 192 193 guess_cols(); 194 195 while (1) { 196 int c; 197 char *p; 198 int option_index = 0; 199 static struct option long_options[] = { 200 {"align", 1, 0, 'a'}, 201 {"config", 1, 0, 'F'}, 202 {"debug", 1, 0, 'D'}, 203# ifdef USE_DEMANGLE 204 {"demangle", 0, 0, 'C'}, 205#endif 206 {"indent", 1, 0, 'n'}, 207 {"help", 0, 0, 'h'}, 208 {"library", 1, 0, 'l'}, 209 {"output", 1, 0, 'o'}, 210 {"version", 0, 0, 'V'}, 211 {"no-plt", 0, 0, 'g'}, 212 {"no-signals", 0, 0, 'b'}, 213#if defined(HAVE_LIBUNWIND) 214 {"where", 1, 0, 'w'}, 215#endif /* defined(HAVE_LIBUNWIND) */ 216 {0, 0, 0, 0} 217 }; 218 c = getopt_long(argc, argv, "+cfhiLrStTVgb" 219# ifdef USE_DEMANGLE 220 "C" 221# endif 222#if defined(HAVE_LIBUNWIND) 223 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options, 224#else /* !defined(HAVE_LIBUNWIND) */ 225 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options, 226#endif 227 &option_index); 228 if (c == -1) { 229 break; 230 } 231 switch (c) { 232 case 'a': 233 options.align = atoi(optarg); 234 break; 235 case 'A': 236 options.arraylen = atoi(optarg); 237 break; 238 case 'c': 239 options.summary++; 240 break; 241#ifdef USE_DEMANGLE 242 case 'C': 243 options.demangle++; 244 break; 245#endif 246 case 'D': 247 if (optarg[0]=='h') { 248 usage_debug(); 249 exit(0); 250 } 251 options.debug = strtoul(optarg,&p,8); 252 if (*p) { 253 fprintf(stderr, "%s: --debug requires an octal argument\n", progname); 254 err_usage(); 255 } 256 break; 257 case 'e': 258 { 259 char *str_e = strdup(optarg); 260 if (!str_e) { 261 perror("ltrace: strdup"); 262 exit(1); 263 } 264 if (str_e[0] == '!') { 265 opt_e_enable = 0; 266 str_e++; 267 } 268 while (*str_e) { 269 struct opt_e_t *tmp; 270 char *str2 = strchr(str_e, ','); 271 if (str2) { 272 *str2 = '\0'; 273 } 274 tmp = malloc(sizeof(struct opt_e_t)); 275 if (!tmp) { 276 perror("ltrace: malloc"); 277 exit(1); 278 } 279 tmp->name = str_e; 280 tmp->next = opt_e; 281 opt_e = tmp; 282 if (str2) { 283 str_e = str2 + 1; 284 } else { 285 break; 286 } 287 } 288 break; 289 } 290 case 'f': 291 options.follow = 1; 292 break; 293 case 'F': 294 { 295 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t)); 296 if (!tmp) { 297 perror("ltrace: malloc"); 298 exit(1); 299 } 300 tmp->filename = strdup(optarg); 301 tmp->next = opt_F; 302 opt_F = tmp; 303 break; 304 } 305 case 'g': 306 options.no_plt = 1; 307 break; 308 case 'h': 309 usage(); 310 exit(0); 311 case 'i': 312 opt_i++; 313 break; 314 case 'l': 315 if (library_num == MAX_LIBRARIES) { 316 fprintf(stderr, 317 "Too many libraries. Maximum is %i.\n", 318 MAX_LIBRARIES); 319 exit(1); 320 } 321 library[library_num++] = optarg; 322 break; 323 case 'L': 324 options.libcalls = 0; 325 break; 326 case 'n': 327 options.indent = atoi(optarg); 328 break; 329#if defined(HAVE_LIBUNWIND) 330 case 'w': 331 options.bt_depth = atoi(optarg); 332 break; 333#endif /* defined(HAVE_LIBUNWIND) */ 334 case 'o': 335 options.output = fopen(optarg, "w"); 336 if (!options.output) { 337 fprintf(stderr, 338 "Can't open %s for output: %s\n", 339 optarg, strerror(errno)); 340 exit(1); 341 } 342 setvbuf(options.output, (char *)NULL, _IOLBF, 0); 343 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC); 344 break; 345 case 'p': 346 { 347 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); 348 if (!tmp) { 349 perror("ltrace: malloc"); 350 exit(1); 351 } 352 tmp->pid = atoi(optarg); 353 tmp->next = opt_p; 354 opt_p = tmp; 355 break; 356 } 357 case 'r': 358 opt_r++; 359 break; 360 case 's': 361 options.strlen = atoi(optarg); 362 break; 363 case 'S': 364 options.syscalls = 1; 365 break; 366 case 't': 367 opt_t++; 368 break; 369 case 'T': 370 opt_T++; 371 break; 372 case 'u': 373 options.user = optarg; 374 break; 375 case 'V': 376 printf("ltrace version " PACKAGE_VERSION ".\n" 377 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n" 378 "This is free software; see the GNU General Public Licence\n" 379 "version 2 or later for copying conditions. There is NO warranty.\n"); 380 exit(0); 381 break; 382 case 'b': 383 options.no_signals = 1; 384 break; 385 case 'X': 386#ifdef PLT_REINITALISATION_BP 387 PLTs_initialized_by_here = optarg; 388#else 389 fprintf(stderr, "WARNING: \"-X\" not used for this " 390 "architecture: assuming you meant \"-x\"\n"); 391#endif 392 /* Fall Thru */ 393 394 case 'x': 395 { 396 struct opt_x_t *p = opt_x; 397 398 /* First, check for duplicate. */ 399 while (p && strcmp(p->name, optarg)) { 400 p = p->next; 401 } 402 if (p) { 403 break; 404 } 405 406 /* If not duplicate, add to list. */ 407 p = malloc(sizeof(struct opt_x_t)); 408 if (!p) { 409 perror("ltrace: malloc"); 410 exit(1); 411 } 412 p->name = optarg; 413 p->found = 0; 414 p->next = opt_x; 415 opt_x = p; 416 break; 417 } 418 419 default: 420 err_usage(); 421 } 422 } 423 argc -= optind; 424 argv += optind; 425 426 if (!opt_F) { 427 opt_F = malloc(sizeof(struct opt_F_t)); 428 opt_F->next = malloc(sizeof(struct opt_F_t)); 429 opt_F->next->next = NULL; 430 opt_F->filename = USER_CONFIG_FILE; 431 opt_F->next->filename = SYSTEM_CONFIG_FILE; 432 } 433 /* Reverse the config file list since it was built by 434 * prepending, and it would make more sense to process the 435 * files in the order they were given. Probably it would make 436 * more sense to keep a tail pointer instead? */ 437 { 438 struct opt_F_t *egg = NULL; 439 struct opt_F_t *chicken; 440 while (opt_F) { 441 chicken = opt_F->next; 442 opt_F->next = egg; 443 egg = opt_F; 444 opt_F = chicken; 445 } 446 opt_F = egg; 447 } 448 449 if (!opt_p && argc < 1) { 450 fprintf(stderr, "%s: too few arguments\n", progname); 451 err_usage(); 452 } 453 if (opt_r && opt_t) { 454 fprintf(stderr, "%s: Incompatible options -r and -t\n", 455 progname); 456 err_usage(); 457 } 458 if (argc > 0) { 459 command = search_for_command(argv[0]); 460 } 461 return &argv[0]; 462} 463