options.c revision d57c9f8a8515781d687ce57eadc371038cd01526
1#include "config.h" 2 3#include <sys/ioctl.h> 4#include <assert.h> 5#include <errno.h> 6#include <fcntl.h> 7#include <getopt.h> 8#include <limits.h> 9#include <stdio.h> 10#include <stdlib.h> 11#include <string.h> 12#include <unistd.h> 13 14#include "common.h" 15#include "filter.h" 16#include "glob.h" 17 18#ifndef SYSCONFDIR 19#define SYSCONFDIR "/etc" 20#endif 21 22#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf" 23#define USER_CONFIG_FILE "~/.ltrace.conf" 24 25struct options_t options = { 26 .align = DEFAULT_ALIGN, /* alignment column for results */ 27 .user = NULL, /* username to run command as */ 28 .syscalls = 0, /* display syscalls */ 29#ifdef USE_DEMANGLE 30 .demangle = 0, /* Demangle low-level symbol names */ 31#endif 32 .indent = 0, /* indent output according to program flow */ 33 .output = NULL, /* output to a specific file */ 34 .summary = 0, /* Report a summary on program exit */ 35 .debug = 0, /* debug */ 36 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */ 37 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */ 38 .follow = 0, /* trace child processes */ 39}; 40 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 filenames give to option -F: */ 51struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */ 52 53static void 54err_usage(void) { 55 fprintf(stderr, "Try `%s --help' for more information\n", progname); 56 exit(1); 57} 58 59static void 60usage(void) { 61 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" 62 "Trace library calls of a given program.\n\n" 63 " -a, --align=COLUMN align return values in a secific column.\n" 64 " -A ARRAYLEN maximum number of array elements to print.\n" 65 " -b, --no-signals don't print signals.\n" 66 " -c count time and calls, and report a summary on exit.\n" 67# ifdef USE_DEMANGLE 68 " -C, --demangle decode low-level symbol names into user-level names.\n" 69# endif 70 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n" 71 " -Dh, --debug=help show help on debugging.\n" 72 " -e expr modify which events to trace.\n" 73 " -f trace children (fork() and clone()).\n" 74 " -F, --config=FILE load alternate configuration file (may be repeated).\n" 75 " -h, --help display this help and exit.\n" 76 " -i print instruction pointer at time of library call.\n" 77 " -l, --library=FILE print library calls from this library only.\n" 78 " -L do NOT display library calls.\n" 79 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" 80 " -o, --output=FILE write the trace output to that file.\n" 81 " -p PID attach to the process with the process ID pid.\n" 82 " -r print relative timestamps.\n" 83 " -s STRLEN specify the maximum string size to print.\n" 84 " -S display system calls.\n" 85 " -t, -tt, -ttt print absolute timestamps.\n" 86 " -T show the time spent inside each call.\n" 87 " -u USERNAME run command with the userid, groupid of username.\n" 88 " -V, --version output version information and exit.\n" 89#if defined(HAVE_LIBUNWIND) 90 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n" 91#endif /* defined(HAVE_LIBUNWIND) */ 92 " -x NAME treat the global NAME like a library subroutine.\n" 93 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", 94 progname); 95} 96 97static void 98usage_debug(void) { 99 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname); 100 fprintf(stdout, 101 "\n" 102 " number ref. in source description\n" 103 " 1 general Generally helpful progress information\n" 104 " 10 event Shows every event received by a traced process\n" 105 " 20 process Shows actions carried upon a traced processes\n" 106 " 40 function Shows every entry to internal functions\n" 107 "\n" 108 "Debugging options are mixed using bitwise-or.\n" 109 "Note that the meanings and values are subject to change.\n" 110 ); 111} 112 113static char * 114search_for_command(char *filename) { 115 static char pathname[PATH_MAX]; 116 char *path; 117 int m, n; 118 119 if (strchr(filename, '/')) { 120 return filename; 121 } 122 for (path = getenv("PATH"); path && *path; path += m) { 123 if (strchr(path, ':')) { 124 n = strchr(path, ':') - path; 125 m = n + 1; 126 } else { 127 m = n = strlen(path); 128 } 129 if (n + strlen(filename) + 1 >= PATH_MAX) { 130 fprintf(stderr, "Error: filename too long\n"); 131 exit(1); 132 } 133 strncpy(pathname, path, n); 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 145static void 146guess_cols(void) { 147 struct winsize ws; 148 char *c; 149 150 options.align = DEFAULT_ALIGN; 151 c = getenv("COLUMNS"); 152 if (c && *c) { 153 char *endptr; 154 int cols; 155 cols = strtol(c, &endptr, 0); 156 if (cols > 0 && !*endptr) { 157 options.align = cols * 5 / 8; 158 } 159 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 160 options.align = ws.ws_col * 5 / 8; 161 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 162 options.align = ws.ws_col * 5 / 8; 163 } 164} 165 166static void 167add_filter_rule(struct filter *filt, const char *expr, 168 enum filter_rule_type type, 169 const char *a_sym, int sym_re_p, 170 const char *a_lib, int lib_re_p) 171{ 172 struct filter_rule *rule = malloc(sizeof(*rule)); 173 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher)); 174 175 if (rule == NULL || matcher == NULL) { 176 fprintf(stderr, "rule near '%s' will be ignored: %s\n", 177 expr, strerror(errno)); 178 fail: 179 free(rule); 180 free(matcher); 181 return; 182 } 183 184 regex_t symbol_re; 185 int status; 186 { 187 /* Add ^ to the start of expression and $ to the end, so that 188 * we match the whole symbol name. Let the user write the "*" 189 * explicitly if they wish. */ 190 char sym[strlen(a_sym) + 3]; 191 sprintf(sym, "^%s$", a_sym); 192 status = (sym_re_p ? regcomp : globcomp)(&symbol_re, sym, 0); 193 if (status != 0) { 194 char buf[100]; 195 regerror(status, &symbol_re, buf, sizeof buf); 196 fprintf(stderr, "rule near '%s' will be ignored: %s\n", 197 expr, buf); 198 goto fail; 199 } 200 } 201 202 if (strcmp(a_lib, "MAIN") == 0) { 203 filter_lib_matcher_main_init(matcher); 204 } else { 205 /* Add ^ and $ to the library expression as well. */ 206 char lib[strlen(a_lib) + 3]; 207 sprintf(lib, "^%s$", a_lib); 208 209 enum filter_lib_matcher_type type 210 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME; 211 212 regex_t lib_re; 213 status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0); 214 if (status != 0) { 215 char buf[100]; 216 regerror(status, &lib_re, buf, sizeof buf); 217 fprintf(stderr, "rule near '%s' will be ignored: %s\n", 218 expr, buf); 219 220 regfree(&symbol_re); 221 goto fail; 222 } 223 filter_lib_matcher_name_init(matcher, type, lib_re); 224 } 225 226 filter_rule_init(rule, type, matcher, symbol_re); 227 filter_add_rule(filt, rule); 228} 229 230static int 231parse_filter(struct filter *filt, char *expr) 232{ 233 /* Filter is a chain of sym@lib rules separated by '-'. If 234 * the filter expression starts with '-', the missing initial 235 * rule is implicitly *@*. */ 236 237 enum filter_rule_type type = FR_ADD; 238 239 while (*expr != 0) { 240 size_t s = strcspn(expr, "@-+"); 241 char *symname = expr; 242 char *libname; 243 char *next = expr + s + 1; 244 enum filter_rule_type this_type = type; 245 246 if (expr[s] == 0) { 247 libname = "*"; 248 expr = next - 1; 249 250 } else if (expr[s] == '-' || expr[s] == '+') { 251 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD; 252 expr[s] = 0; 253 libname = "*"; 254 expr = next; 255 256 } else { 257 assert(expr[s] == '@'); 258 expr[s] = 0; 259 s = strcspn(next, "-+"); 260 if (s == 0) { 261 libname = "*"; 262 expr = next; 263 } else if (next[s] == 0) { 264 expr = next + s; 265 libname = next; 266 } else { 267 assert(next[s] == '-' || next[s] == '+'); 268 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD; 269 next[s] = 0; 270 expr = next + s + 1; 271 libname = next; 272 } 273 } 274 275 assert(*libname != 0); 276 char *symend = symname + strlen(symname) - 1; 277 char *libend = libname + strlen(libname) - 1; 278 int sym_is_re = 0; 279 int lib_is_re = 0; 280 281 /* 282 * /xxx/@... and ...@/xxx/ means that xxx are regular 283 * expressions. They are globs otherwise. 284 * 285 * /xxx@yyy/ is the same as /xxx/@/yyy/ 286 * 287 * @/xxx matches library path name 288 * @.xxx matches library relative path name 289 */ 290 if (symname[0] == '/') { 291 if (symname != symend && symend[0] == '/') { 292 ++symname; 293 *symend-- = 0; 294 sym_is_re = 1; 295 296 } else { 297 sym_is_re = 1; 298 lib_is_re = 1; 299 ++symname; 300 301 /* /XXX@YYY/ is the same as 302 * /XXX/@/YYY/. */ 303 if (libend[0] != '/') 304 fprintf(stderr, "unmatched '/'" 305 " in symbol name\n"); 306 else 307 *libend-- = 0; 308 } 309 } 310 311 /* If libname ends in '/', then we expect '/' in the 312 * beginning too. Otherwise the initial '/' is part 313 * of absolute file name. */ 314 if (!lib_is_re && libend[0] == '/') { 315 lib_is_re = 1; 316 *libend-- = 0; 317 if (libname != libend && libname[0] == '/') 318 ++libname; 319 else 320 fprintf(stderr, "unmatched '/'" 321 " in library name\n"); 322 } 323 324 if (*symname == 0) /* /@AA/ */ 325 symname = "*"; 326 if (*libname == 0) /* /aa@/ */ 327 libname = "*"; 328 329 add_filter_rule(filt, expr, this_type, 330 symname, sym_is_re, 331 libname, lib_is_re); 332 } 333 334 return 0; 335} 336 337static struct filter * 338recursive_parse_chain(char *expr) 339{ 340 struct filter *filt = malloc(sizeof(*filt)); 341 if (filt == NULL) { 342 fprintf(stderr, "(part of) filter will be ignored: '%s': %s\n", 343 expr, strerror(errno)); 344 return NULL; 345 } 346 347 filter_init(filt); 348 if (parse_filter(filt, expr) < 0) { 349 fprintf(stderr, "Filter '%s' will be ignored.\n", expr); 350 free(filt); 351 filt = NULL; 352 } 353 354 return filt; 355} 356 357static void 358parse_filter_chain(const char *expr, struct filter **retp) 359{ 360 char *str = strdup(expr); 361 if (str == NULL) { 362 fprintf(stderr, "filter '%s' will be ignored: %s\n", 363 expr, strerror(errno)); 364 return; 365 } 366 /* Support initial '!' for backward compatibility. */ 367 if (str[0] == '!') 368 str[0] = '-'; 369 370 struct filter **tailp; 371 for (tailp = retp; *tailp != NULL; tailp = &(*tailp)->next) 372 ; 373 *tailp = recursive_parse_chain(str); 374} 375 376char ** 377process_options(int argc, char **argv) 378{ 379 progname = argv[0]; 380 options.output = stderr; 381 options.no_signals = 0; 382#if defined(HAVE_LIBUNWIND) 383 options.bt_depth = -1; 384#endif /* defined(HAVE_LIBUNWIND) */ 385 386 guess_cols(); 387 388 int libcalls = 1; 389 390 while (1) { 391 int c; 392 char *p; 393 int option_index = 0; 394 static struct option long_options[] = { 395 {"align", 1, 0, 'a'}, 396 {"config", 1, 0, 'F'}, 397 {"debug", 1, 0, 'D'}, 398# ifdef USE_DEMANGLE 399 {"demangle", 0, 0, 'C'}, 400#endif 401 {"indent", 1, 0, 'n'}, 402 {"help", 0, 0, 'h'}, 403 {"library", 1, 0, 'l'}, 404 {"output", 1, 0, 'o'}, 405 {"version", 0, 0, 'V'}, 406 {"no-signals", 0, 0, 'b'}, 407#if defined(HAVE_LIBUNWIND) 408 {"where", 1, 0, 'w'}, 409#endif /* defined(HAVE_LIBUNWIND) */ 410 {0, 0, 0, 0} 411 }; 412 c = getopt_long(argc, argv, "+cfhiLrStTVb" 413# ifdef USE_DEMANGLE 414 "C" 415# endif 416#if defined(HAVE_LIBUNWIND) 417 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options, 418#else /* !defined(HAVE_LIBUNWIND) */ 419 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options, 420#endif 421 &option_index); 422 if (c == -1) { 423 break; 424 } 425 switch (c) { 426 case 'a': 427 options.align = atoi(optarg); 428 break; 429 case 'A': 430 options.arraylen = atoi(optarg); 431 break; 432 case 'b': 433 options.no_signals = 1; 434 break; 435 case 'c': 436 options.summary++; 437 break; 438#ifdef USE_DEMANGLE 439 case 'C': 440 options.demangle++; 441 break; 442#endif 443 case 'D': 444 if (optarg[0]=='h') { 445 usage_debug(); 446 exit(0); 447 } 448 options.debug = strtoul(optarg,&p,8); 449 if (*p) { 450 fprintf(stderr, "%s: --debug requires an octal argument\n", progname); 451 err_usage(); 452 } 453 break; 454 455 case 'e': 456 parse_filter_chain(optarg, &options.plt_filter); 457 break; 458 459 case 'f': 460 options.follow = 1; 461 break; 462 case 'F': 463 { 464 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t)); 465 if (!tmp) { 466 perror("ltrace: malloc"); 467 exit(1); 468 } 469 tmp->filename = strdup(optarg); 470 tmp->next = opt_F; 471 opt_F = tmp; 472 break; 473 } 474 case 'h': 475 usage(); 476 exit(0); 477 case 'i': 478 opt_i++; 479 break; 480 case 'l': 481 // XXX TODO 482 fprintf(stderr, "-l support not yet implemented\n"); 483 break; 484 case 'L': 485 libcalls = 0; 486 break; 487 case 'n': { 488 char *endptr; 489 long int l = strtol(optarg, &endptr, 0); 490 /* Arbitrary cut-off. Nobody needs to indent 491 * more than, say, 8, anyway. */ 492 if (l < 0 || l > 20 || *optarg == 0 || *endptr != 0) { 493 fprintf(stderr, "Invalid argument to -n: '%s'." 494 " Use integer 0..20.\n", optarg); 495 exit(1); 496 } 497 options.indent = (int)l; 498 break; 499 } 500 case 'o': 501 options.output = fopen(optarg, "w"); 502 if (!options.output) { 503 fprintf(stderr, 504 "can't open %s for writing: %s\n", 505 optarg, strerror(errno)); 506 exit(1); 507 } 508 setvbuf(options.output, (char *)NULL, _IOLBF, 0); 509 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC); 510 break; 511 case 'p': 512 { 513 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); 514 if (!tmp) { 515 perror("ltrace: malloc"); 516 exit(1); 517 } 518 tmp->pid = atoi(optarg); 519 tmp->next = opt_p; 520 opt_p = tmp; 521 break; 522 } 523 case 'r': 524 opt_r++; 525 break; 526 case 's': 527 options.strlen = atoi(optarg); 528 break; 529 case 'S': 530 options.syscalls = 1; 531 break; 532 case 't': 533 opt_t++; 534 break; 535 case 'T': 536 opt_T++; 537 break; 538 case 'u': 539 options.user = optarg; 540 break; 541 case 'V': 542 printf("ltrace version " PACKAGE_VERSION ".\n" 543 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n" 544 "This is free software; see the GNU General Public Licence\n" 545 "version 2 or later for copying conditions. There is NO warranty.\n"); 546 exit(0); 547 break; 548#if defined(HAVE_LIBUNWIND) 549 case 'w': 550 options.bt_depth = atoi(optarg); 551 break; 552#endif /* defined(HAVE_LIBUNWIND) */ 553 554 case 'x': 555 parse_filter_chain(optarg, &options.static_filter); 556 break; 557 558 default: 559 err_usage(); 560 } 561 } 562 argc -= optind; 563 argv += optind; 564 565 if (!opt_F) { 566 opt_F = malloc(sizeof(struct opt_F_t)); 567 opt_F->next = malloc(sizeof(struct opt_F_t)); 568 opt_F->next->next = NULL; 569 opt_F->filename = USER_CONFIG_FILE; 570 opt_F->next->filename = SYSTEM_CONFIG_FILE; 571 } 572 /* Reverse the config file list since it was built by 573 * prepending, and it would make more sense to process the 574 * files in the order they were given. Probably it would make 575 * more sense to keep a tail pointer instead? */ 576 { 577 struct opt_F_t *egg = NULL; 578 struct opt_F_t *chicken; 579 while (opt_F) { 580 chicken = opt_F->next; 581 opt_F->next = egg; 582 egg = opt_F; 583 opt_F = chicken; 584 } 585 opt_F = egg; 586 } 587 588 /* Set default filter. Use @MAIN for now, as that's what 589 * ltrace used to have in the past. XXX Maybe we should make 590 * this "*" instead. */ 591 if (options.plt_filter == NULL && libcalls) { 592 parse_filter_chain("@MAIN", &options.plt_filter); 593 options.hide_caller = 1; 594 } 595 596 if (!opt_p && argc < 1) { 597 fprintf(stderr, "%s: too few arguments\n", progname); 598 err_usage(); 599 } 600 if (opt_r && opt_t) { 601 fprintf(stderr, "%s: Incompatible options -r and -t\n", 602 progname); 603 err_usage(); 604 } 605 if (argc > 0) { 606 command = search_for_command(argv[0]); 607 } 608 return &argv[0]; 609} 610