2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2009,2010 Joe Damato
5 * Copyright (C) 1998,1999,2002,2003,2004,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 * Copyright (C) 2006 Steve Fink
8 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <sys/ioctl.h>
44 #define SYSCONFDIR "/etc"
47 #define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
48 #define USER_CONFIG_FILE "~/.ltrace.conf"
50 struct options_t options = {
51 .align = DEFAULT_ALIGN, /* alignment column for results */
52 .user = NULL, /* username to run command as */
53 .syscalls = 0, /* display syscalls */
55 .demangle = 0, /* Demangle low-level symbol names */
57 .indent = 0, /* indent output according to program flow */
58 .output = NULL, /* output to a specific file */
59 .summary = 0, /* Report a summary on program exit */
60 .debug = 0, /* debug */
61 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
62 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
63 .follow = 0, /* trace child processes */
66 static char *progname; /* Program name (`ltrace') */
67 int opt_i = 0; /* instruction pointer */
68 int opt_r = 0; /* print relative timestamp */
69 int opt_t = 0; /* print absolute timestamp */
70 int opt_T = 0; /* show the time spent inside each call */
72 /* List of pids given to option -p: */
73 struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
75 /* List of filenames give to option -F: */
76 struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
80 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
86 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
87 "Trace library calls of a given program.\n\n"
88 " -a, --align=COLUMN align return values in a secific column.\n"
89 " -A ARRAYLEN maximum number of array elements to print.\n"
90 " -b, --no-signals don't print signals.\n"
91 " -c count time and calls, and report a summary on exit.\n"
93 " -C, --demangle decode low-level symbol names into user-level names.\n"
95 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
96 " -Dh, --debug=help show help on debugging.\n"
97 " -e expr modify which events to trace.\n"
98 " -f trace children (fork() and clone()).\n"
99 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
100 " -h, --help display this help and exit.\n"
101 " -i print instruction pointer at time of library call.\n"
102 " -l, --library=FILE only trace symbols implemented by this library.\n"
103 " -L do NOT display library calls.\n"
104 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
105 " -o, --output=FILE write the trace output to that file.\n"
106 " -p PID attach to the process with the process ID pid.\n"
107 " -r print relative timestamps.\n"
108 " -s STRLEN specify the maximum string size to print.\n"
109 " -S display system calls.\n"
110 " -t, -tt, -ttt print absolute timestamps.\n"
111 " -T show the time spent inside each call.\n"
112 " -u USERNAME run command with the userid, groupid of username.\n"
113 " -V, --version output version information and exit.\n"
114 #if defined(HAVE_LIBUNWIND)
115 " -w, --where=NR print backtrace showing NR stack frames at most.\n"
116 #endif /* defined(HAVE_LIBUNWIND) */
117 " -x NAME treat the global NAME like a library subroutine.\n"
118 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
124 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
127 " number ref. in source description\n"
128 " 1 general Generally helpful progress information\n"
129 " 10 event Shows every event received by a traced process\n"
130 " 20 process Shows actions carried upon a traced processes\n"
131 " 40 function Shows every entry to internal functions\n"
133 "Debugging options are mixed using bitwise-or.\n"
134 "Note that the meanings and values are subject to change.\n"
139 search_for_command(char *filename) {
140 static char pathname[PATH_MAX];
144 if (strchr(filename, '/')) {
147 for (path = getenv("PATH"); path && *path; path += m) {
148 if (strchr(path, ':')) {
149 n = strchr(path, ':') - path;
152 m = n = strlen(path);
154 if (n + strlen(filename) + 1 >= PATH_MAX) {
155 fprintf(stderr, "Error: filename too long.\n");
158 strncpy(pathname, path, n);
159 if (n && pathname[n - 1] != '/') {
162 strcpy(pathname + n, filename);
163 if (!access(pathname, X_OK)) {
175 options.align = DEFAULT_ALIGN;
176 c = getenv("COLUMNS");
180 cols = strtol(c, &endptr, 0);
181 if (cols > 0 && !*endptr) {
182 options.align = cols * 5 / 8;
184 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
185 options.align = ws.ws_col * 5 / 8;
186 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
187 options.align = ws.ws_col * 5 / 8;
192 compile_libname(const char *expr, const char *a_lib, int lib_re_p,
193 struct filter_lib_matcher *matcher)
195 if (strcmp(a_lib, "MAIN") == 0) {
196 filter_lib_matcher_main_init(matcher);
198 /* Add ^ and $ to the library expression as well. */
199 char lib[strlen(a_lib) + 3];
200 sprintf(lib, "^%s$", a_lib);
202 enum filter_lib_matcher_type type
203 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
206 int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
207 if (status != REG_NOERROR) {
209 regerror(status, &lib_re, buf, sizeof buf);
210 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
214 filter_lib_matcher_name_init(matcher, type, lib_re);
220 add_filter_rule(struct filter *filt, const char *expr,
221 enum filter_rule_type type,
222 const char *a_sym, int sym_re_p,
223 const char *a_lib, int lib_re_p)
225 struct filter_rule *rule = malloc(sizeof(*rule));
226 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher));
228 if (rule == NULL || matcher == NULL) {
229 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
230 expr, strerror(errno));
239 /* Add ^ to the start of expression and $ to the end, so that
240 * we match the whole symbol name. Let the user write the "*"
241 * explicitly if they wish. */
242 char sym[strlen(a_sym) + 3];
243 sprintf(sym, "^%s$", a_sym);
244 int status = (sym_re_p ? regcomp : globcomp)
245 (&symbol_re, sym, 0);
248 regerror(status, &symbol_re, buf, sizeof buf);
249 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
255 if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) {
260 filter_rule_init(rule, type, matcher, symbol_re);
261 filter_add_rule(filt, rule);
265 grok_libname_pattern(char **libnamep, char **libendp)
267 char *libname = *libnamep;
268 char *libend = *libendp;
270 if (libend[0] != '/')
274 if (libname != libend && libname[0] == '/')
277 fprintf(stderr, "Unmatched '/' in library name.\n");
285 parse_filter(struct filter *filt, char *expr, int operators)
287 /* Filter is a chain of sym@lib rules separated by '-' or '+'.
288 * If the filter expression starts with '-', the missing
289 * initial rule is implicitly *@*. */
291 enum filter_rule_type type = FR_ADD;
294 size_t s = strcspn(expr, "-+@" + (operators ? 0 : 2));
295 char *symname = expr;
297 char *next = expr + s + 1;
298 enum filter_rule_type this_type = type;
304 } else if (expr[s] == '-' || expr[s] == '+') {
305 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD;
311 assert(expr[s] == '@');
313 s = strcspn(next, "-+" + (operators ? 0 : 2));
317 } else if (next[s] == 0) {
321 assert(next[s] == '-' || next[s] == '+');
322 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD;
329 assert(*libname != 0);
330 char *symend = symname + strlen(symname) - 1;
331 char *libend = libname + strlen(libname) - 1;
336 * /xxx/@... and ...@/xxx/ means that xxx are regular
337 * expressions. They are globs otherwise.
339 * /xxx@yyy/ is the same as /xxx/@/yyy/
341 * @/xxx matches library path name
342 * @.xxx matches library relative path name
344 if (symname[0] == '/') {
345 if (symname != symend && symend[0] == '/') {
355 /* /XXX@YYY/ is the same as
357 if (libend[0] != '/')
358 fprintf(stderr, "Unmatched '/'"
359 " in symbol name.\n");
365 /* If libname ends in '/', then we expect '/' in the
366 * beginning too. Otherwise the initial '/' is part
367 * of absolute file name. */
369 lib_is_re = grok_libname_pattern(&libname, &libend);
371 if (*symname == 0) /* /@AA/ */
373 if (*libname == 0) /* /aa@/ */
376 add_filter_rule(filt, expr, this_type,
384 static struct filter *
385 recursive_parse_chain(char *expr, int operators)
387 struct filter *filt = malloc(sizeof(*filt));
389 fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n",
390 expr, strerror(errno));
395 if (parse_filter(filt, expr, operators) < 0) {
396 fprintf(stderr, "Filter '%s' will be ignored.\n", expr);
404 static struct filter **
405 slist_chase_end(struct filter **begin)
407 for (; *begin != NULL; begin = &(*begin)->next)
413 parse_filter_chain(const char *expr, struct filter **retp)
415 char *str = strdup(expr);
417 fprintf(stderr, "Filter '%s' will be ignored: %s.\n",
418 expr, strerror(errno));
421 /* Support initial '!' for backward compatibility. */
425 *slist_chase_end(retp) = recursive_parse_chain(str, 1);
430 parse_int(const char *optarg, char opt, int min, int max)
433 long int l = strtol(optarg, &endptr, 0);
434 if (l < min || (max != 0 && l > max)
435 || *optarg == 0 || *endptr != 0) {
436 const char *fmt = max != 0
437 ? "Invalid argument to -%c: '%s'. Use integer %d..%d.\n"
438 : "Invalid argument to -%c: '%s'. Use integer >=%d.\n";
439 fprintf(stderr, fmt, opt, optarg, min, max);
446 process_options(int argc, char **argv)
449 options.output = stderr;
450 options.no_signals = 0;
451 #if defined(HAVE_LIBUNWIND)
452 options.bt_depth = -1;
453 #endif /* defined(HAVE_LIBUNWIND) */
462 int option_index = 0;
463 static struct option long_options[] = {
464 {"align", 1, 0, 'a'},
465 {"config", 1, 0, 'F'},
466 {"debug", 1, 0, 'D'},
468 {"demangle", 0, 0, 'C'},
470 {"indent", 1, 0, 'n'},
472 {"library", 1, 0, 'l'},
473 {"output", 1, 0, 'o'},
474 {"version", 0, 0, 'V'},
475 {"no-signals", 0, 0, 'b'},
476 #if defined(HAVE_LIBUNWIND)
477 {"where", 1, 0, 'w'},
478 #endif /* defined(HAVE_LIBUNWIND) */
481 c = getopt_long(argc, argv, "+cfhiLrStTVb"
485 #if defined(HAVE_LIBUNWIND)
486 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
487 #else /* !defined(HAVE_LIBUNWIND) */
488 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
496 options.align = parse_int(optarg, 'a', 0, 0);
499 options.arraylen = parse_int(optarg, 'A', 0, 0);
502 options.no_signals = 1;
513 if (optarg[0]=='h') {
517 options.debug = strtoul(optarg,&p,8);
519 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
525 parse_filter_chain(optarg, &options.plt_filter);
533 struct opt_F_t *tmp = malloc(sizeof(*tmp));
536 fprintf(stderr, "%s\n",
541 tmp->filename = strdup(optarg);
542 if (tmp->filename == NULL)
544 tmp->own_filename = 1;
557 size_t patlen = strlen(optarg);
558 char buf[patlen + 2];
559 sprintf(buf, "@%s", optarg);
560 *slist_chase_end(&options.export_filter)
561 = recursive_parse_chain(buf, 0);
569 options.indent = parse_int(optarg, 'n', 0, 20);
572 options.output = fopen(optarg, "w");
573 if (!options.output) {
575 "can't open %s for writing: %s\n",
576 optarg, strerror(errno));
579 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
580 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
584 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
586 perror("ltrace: malloc");
589 tmp->pid = parse_int(optarg, 'p', 1, 0);
598 options.strlen = parse_int(optarg, 's', 0, 0);
601 options.syscalls = 1;
610 options.user = optarg;
613 printf("ltrace version " PACKAGE_VERSION ".\n"
614 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
615 "This is free software; see the GNU General Public Licence\n"
616 "version 2 or later for copying conditions. There is NO warranty.\n");
619 #if defined(HAVE_LIBUNWIND)
621 options.bt_depth = parse_int(optarg, 'w', 1, 0);
623 #endif /* defined(HAVE_LIBUNWIND) */
626 parse_filter_chain(optarg, &options.static_filter);
637 opt_F = malloc(sizeof(struct opt_F_t));
638 opt_F->next = malloc(sizeof(struct opt_F_t));
639 opt_F->next->next = NULL;
640 opt_F->filename = USER_CONFIG_FILE;
641 opt_F->own_filename = 0;
642 opt_F->next->filename = SYSTEM_CONFIG_FILE;
643 opt_F->next->own_filename = 0;
645 /* Reverse the config file list since it was built by
646 * prepending, and it would make more sense to process the
647 * files in the order they were given. Probably it would make
648 * more sense to keep a tail pointer instead? */
650 struct opt_F_t *egg = NULL;
651 struct opt_F_t *chicken;
653 chicken = opt_F->next;
661 /* If neither -e, nor -l, nor -L are used, set default -e.
662 * Use @MAIN for now, as that's what ltrace used to have in
663 * the past. XXX Maybe we should make this "*" instead. */
665 && options.plt_filter == NULL
666 && options.export_filter == NULL) {
667 parse_filter_chain("@MAIN", &options.plt_filter);
668 options.hide_caller = 1;
670 if (!libcalls && options.plt_filter != NULL) {
672 "%s: Option -L can't be used with -e or -l.\n",
677 if (!opt_p && argc < 1) {
678 fprintf(stderr, "%s: too few arguments\n", progname);
681 if (opt_r && opt_t) {
683 "%s: Options -r and -t can't be used together\n",
688 command = search_for_command(argv[0]);