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