| Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame^] | 1 | /* |
| 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 |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 |
| 23 | * 02110-1301 USA |
| 24 | */ |
| 25 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 26 | #include "config.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 27 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 29 | #include <assert.h> |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <getopt.h> |
| 33 | #include <limits.h> |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 34 | #include <stdio.h> |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 38 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 39 | #include "common.h" |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 40 | #include "filter.h" |
| 41 | #include "glob.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 42 | |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 43 | #ifndef SYSCONFDIR |
| 44 | #define SYSCONFDIR "/etc" |
| 45 | #endif |
| 46 | |
| Olaf Hering | e948e58 | 2006-10-12 23:53:44 +0200 | [diff] [blame] | 47 | #define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf" |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 48 | #define USER_CONFIG_FILE "~/.ltrace.conf" |
| 49 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 50 | struct options_t options = { |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 51 | .align = DEFAULT_ALIGN, /* alignment column for results */ |
| 52 | .user = NULL, /* username to run command as */ |
| 53 | .syscalls = 0, /* display syscalls */ |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 54 | #ifdef USE_DEMANGLE |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 55 | .demangle = 0, /* Demangle low-level symbol names */ |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 56 | #endif |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 57 | .indent = 0, /* indent output according to program flow */ |
| 58 | .output = NULL, /* output to a specific file */ |
| Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 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 */ |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 66 | static char *progname; /* Program name (`ltrace') */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 67 | int opt_i = 0; /* instruction pointer */ |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 68 | int opt_r = 0; /* print relative timestamp */ |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 69 | int opt_t = 0; /* print absolute timestamp */ |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 70 | int opt_T = 0; /* show the time spent inside each call */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 71 | |
| 72 | /* List of pids given to option -p: */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 73 | struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 74 | |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 75 | /* List of filenames give to option -F: */ |
| 76 | struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */ |
| 77 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 78 | static void |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 79 | err_usage(void) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 80 | fprintf(stderr, "Try `%s --help' for more information.\n", progname); |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | static void |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 85 | usage(void) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 86 | fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 87 | "Trace library calls of a given program.\n\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 88 | " -a, --align=COLUMN align return values in a secific column.\n" |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 89 | " -A ARRAYLEN maximum number of array elements to print.\n" |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 90 | " -b, --no-signals don't print signals.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 91 | " -c count time and calls, and report a summary on exit.\n" |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 92 | # ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 93 | " -C, --demangle decode low-level symbol names into user-level names.\n" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 94 | # endif |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 95 | " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n" |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 96 | " -Dh, --debug=help show help on debugging.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 97 | " -e expr modify which events to trace.\n" |
| Juan Cespedes | c4e53a9 | 2009-05-06 20:36:42 +0200 | [diff] [blame] | 98 | " -f trace children (fork() and clone()).\n" |
| Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 99 | " -F, --config=FILE load alternate configuration file (may be repeated).\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 100 | " -h, --help display this help and exit.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 101 | " -i print instruction pointer at time of library call.\n" |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 102 | " -l, --library=FILE only trace symbols implemented by this library.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 103 | " -L do NOT display library calls.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 105 | " -o, --output=FILE write the trace output to that file.\n" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 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" |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | " -V, --version output version information and exit.\n" |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 114 | #if defined(HAVE_LIBUNWIND) |
| 115 | " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n" |
| 116 | #endif /* defined(HAVE_LIBUNWIND) */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 117 | " -x NAME treat the global NAME like a library subroutine.\n" |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 118 | "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 119 | progname); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 122 | static void |
| 123 | usage_debug(void) { |
| 124 | fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname); |
| 125 | fprintf(stdout, |
| 126 | "\n" |
| 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" |
| 132 | "\n" |
| 133 | "Debugging options are mixed using bitwise-or.\n" |
| 134 | "Note that the meanings and values are subject to change.\n" |
| 135 | ); |
| 136 | } |
| 137 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 138 | static char * |
| 139 | search_for_command(char *filename) { |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 140 | static char pathname[PATH_MAX]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 141 | char *path; |
| 142 | int m, n; |
| 143 | |
| 144 | if (strchr(filename, '/')) { |
| 145 | return filename; |
| 146 | } |
| 147 | for (path = getenv("PATH"); path && *path; path += m) { |
| 148 | if (strchr(path, ':')) { |
| 149 | n = strchr(path, ':') - path; |
| 150 | m = n + 1; |
| 151 | } else { |
| 152 | m = n = strlen(path); |
| 153 | } |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 154 | if (n + strlen(filename) + 1 >= PATH_MAX) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 155 | fprintf(stderr, "Error: filename too long.\n"); |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 156 | exit(1); |
| 157 | } |
| 158 | strncpy(pathname, path, n); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 159 | if (n && pathname[n - 1] != '/') { |
| 160 | pathname[n++] = '/'; |
| 161 | } |
| 162 | strcpy(pathname + n, filename); |
| 163 | if (!access(pathname, X_OK)) { |
| 164 | return pathname; |
| 165 | } |
| 166 | } |
| 167 | return filename; |
| 168 | } |
| 169 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 170 | static void |
| 171 | guess_cols(void) { |
| 172 | struct winsize ws; |
| 173 | char *c; |
| 174 | |
| 175 | options.align = DEFAULT_ALIGN; |
| 176 | c = getenv("COLUMNS"); |
| 177 | if (c && *c) { |
| 178 | char *endptr; |
| 179 | int cols; |
| 180 | cols = strtol(c, &endptr, 0); |
| 181 | if (cols > 0 && !*endptr) { |
| 182 | options.align = cols * 5 / 8; |
| 183 | } |
| 184 | } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { |
| 185 | options.align = ws.ws_col * 5 / 8; |
| Juan Cespedes | 43739a6 | 2009-04-07 13:10:08 +0200 | [diff] [blame] | 186 | } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { |
| 187 | options.align = ws.ws_col * 5 / 8; |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 191 | static int |
| 192 | compile_libname(const char *expr, const char *a_lib, int lib_re_p, |
| 193 | struct filter_lib_matcher *matcher) |
| 194 | { |
| 195 | if (strcmp(a_lib, "MAIN") == 0) { |
| 196 | filter_lib_matcher_main_init(matcher); |
| 197 | } else { |
| 198 | /* Add ^ and $ to the library expression as well. */ |
| 199 | char lib[strlen(a_lib) + 3]; |
| 200 | sprintf(lib, "^%s$", a_lib); |
| 201 | |
| 202 | enum filter_lib_matcher_type type |
| 203 | = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME; |
| 204 | |
| 205 | regex_t lib_re; |
| 206 | int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0); |
| 207 | if (status != 0) { |
| 208 | char buf[100]; |
| 209 | regerror(status, &lib_re, buf, sizeof buf); |
| 210 | fprintf(stderr, "Rule near '%s' will be ignored: %s.\n", |
| 211 | expr, buf); |
| 212 | return -1; |
| 213 | } |
| 214 | filter_lib_matcher_name_init(matcher, type, lib_re); |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 219 | static void |
| 220 | add_filter_rule(struct filter *filt, const char *expr, |
| 221 | enum filter_rule_type type, |
| Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 222 | const char *a_sym, int sym_re_p, |
| 223 | const char *a_lib, int lib_re_p) |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 224 | { |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 225 | struct filter_rule *rule = malloc(sizeof(*rule)); |
| 226 | struct filter_lib_matcher *matcher = malloc(sizeof(*matcher)); |
| 227 | |
| 228 | if (rule == NULL || matcher == NULL) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 229 | fprintf(stderr, "Rule near '%s' will be ignored: %s.\n", |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 230 | expr, strerror(errno)); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 231 | fail: |
| 232 | free(rule); |
| 233 | free(matcher); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | regex_t symbol_re; |
| Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 238 | { |
| 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); |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 244 | int status = (sym_re_p ? regcomp : globcomp) |
| 245 | (&symbol_re, sym, 0); |
| Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 246 | if (status != 0) { |
| 247 | char buf[100]; |
| 248 | regerror(status, &symbol_re, buf, sizeof buf); |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 249 | fprintf(stderr, "Rule near '%s' will be ignored: %s.\n", |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 250 | expr, buf); |
| Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 251 | goto fail; |
| 252 | } |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 255 | if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) { |
| 256 | regfree(&symbol_re); |
| 257 | goto fail; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 260 | filter_rule_init(rule, type, matcher, symbol_re); |
| Petr Machata | e0973cb | 2012-04-01 19:36:41 +0200 | [diff] [blame] | 261 | filter_add_rule(filt, rule); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 265 | grok_libname_pattern(char **libnamep, char **libendp) |
| 266 | { |
| 267 | char *libname = *libnamep; |
| 268 | char *libend = *libendp; |
| 269 | |
| 270 | if (libend[0] != '/') |
| 271 | return 0; |
| 272 | |
| 273 | *libend-- = 0; |
| 274 | if (libname != libend && libname[0] == '/') |
| 275 | ++libname; |
| 276 | else |
| 277 | fprintf(stderr, "Unmatched '/' in library name.\n"); |
| 278 | |
| 279 | *libendp = libend; |
| 280 | *libnamep = libname; |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | static int |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 285 | parse_filter(struct filter *filt, char *expr, int operators) |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 286 | { |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 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 *@*. */ |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 290 | |
| 291 | enum filter_rule_type type = FR_ADD; |
| 292 | |
| 293 | while (*expr != 0) { |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 294 | size_t s = strcspn(expr, "-+@" + (operators ? 0 : 2)); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 295 | char *symname = expr; |
| 296 | char *libname; |
| 297 | char *next = expr + s + 1; |
| 298 | enum filter_rule_type this_type = type; |
| 299 | |
| 300 | if (expr[s] == 0) { |
| 301 | libname = "*"; |
| 302 | expr = next - 1; |
| 303 | |
| Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 304 | } else if (expr[s] == '-' || expr[s] == '+') { |
| 305 | type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD; |
| 306 | expr[s] = 0; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 307 | libname = "*"; |
| 308 | expr = next; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 309 | |
| 310 | } else { |
| 311 | assert(expr[s] == '@'); |
| 312 | expr[s] = 0; |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 313 | s = strcspn(next, "-+" + (operators ? 0 : 2)); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 314 | if (s == 0) { |
| 315 | libname = "*"; |
| 316 | expr = next; |
| 317 | } else if (next[s] == 0) { |
| 318 | expr = next + s; |
| 319 | libname = next; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 320 | } else { |
| Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 321 | assert(next[s] == '-' || next[s] == '+'); |
| 322 | type = next[s] == '-' ? FR_SUBTRACT : FR_ADD; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 323 | next[s] = 0; |
| 324 | expr = next + s + 1; |
| 325 | libname = next; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | assert(*libname != 0); |
| 330 | char *symend = symname + strlen(symname) - 1; |
| 331 | char *libend = libname + strlen(libname) - 1; |
| 332 | int sym_is_re = 0; |
| 333 | int lib_is_re = 0; |
| 334 | |
| 335 | /* |
| 336 | * /xxx/@... and ...@/xxx/ means that xxx are regular |
| 337 | * expressions. They are globs otherwise. |
| 338 | * |
| 339 | * /xxx@yyy/ is the same as /xxx/@/yyy/ |
| 340 | * |
| 341 | * @/xxx matches library path name |
| 342 | * @.xxx matches library relative path name |
| 343 | */ |
| 344 | if (symname[0] == '/') { |
| 345 | if (symname != symend && symend[0] == '/') { |
| 346 | ++symname; |
| 347 | *symend-- = 0; |
| 348 | sym_is_re = 1; |
| 349 | |
| 350 | } else { |
| 351 | sym_is_re = 1; |
| 352 | lib_is_re = 1; |
| 353 | ++symname; |
| 354 | |
| 355 | /* /XXX@YYY/ is the same as |
| 356 | * /XXX/@/YYY/. */ |
| 357 | if (libend[0] != '/') |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 358 | fprintf(stderr, "Unmatched '/'" |
| 359 | " in symbol name.\n"); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 360 | else |
| 361 | *libend-- = 0; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* If libname ends in '/', then we expect '/' in the |
| 366 | * beginning too. Otherwise the initial '/' is part |
| 367 | * of absolute file name. */ |
| Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 368 | if (!lib_is_re) |
| 369 | lib_is_re = grok_libname_pattern(&libname, &libend); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 370 | |
| 371 | if (*symname == 0) /* /@AA/ */ |
| 372 | symname = "*"; |
| 373 | if (*libname == 0) /* /aa@/ */ |
| 374 | libname = "*"; |
| 375 | |
| 376 | add_filter_rule(filt, expr, this_type, |
| 377 | symname, sym_is_re, |
| 378 | libname, lib_is_re); |
| 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static struct filter * |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 385 | recursive_parse_chain(char *expr, int operators) |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 386 | { |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 387 | struct filter *filt = malloc(sizeof(*filt)); |
| 388 | if (filt == NULL) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 389 | fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n", |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 390 | expr, strerror(errno)); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | |
| Petr Machata | 35c8814 | 2012-04-04 00:54:43 +0200 | [diff] [blame] | 394 | filter_init(filt); |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 395 | if (parse_filter(filt, expr, operators) < 0) { |
| Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 396 | fprintf(stderr, "Filter '%s' will be ignored.\n", expr); |
| 397 | free(filt); |
| 398 | filt = NULL; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 399 | } |
| Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 400 | |
| 401 | return filt; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 404 | static struct filter ** |
| 405 | slist_chase_end(struct filter **begin) |
| 406 | { |
| 407 | for (; *begin != NULL; begin = &(*begin)->next) |
| 408 | ; |
| 409 | return begin; |
| 410 | } |
| 411 | |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 412 | static void |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 413 | parse_filter_chain(const char *expr, struct filter **retp) |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 414 | { |
| 415 | char *str = strdup(expr); |
| 416 | if (str == NULL) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 417 | fprintf(stderr, "Filter '%s' will be ignored: %s.\n", |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 418 | expr, strerror(errno)); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 419 | return; |
| 420 | } |
| Petr Machata | 25f319e | 2012-04-23 23:45:21 +0200 | [diff] [blame] | 421 | /* Support initial '!' for backward compatibility. */ |
| 422 | if (str[0] == '!') |
| 423 | str[0] = '-'; |
| 424 | |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 425 | *slist_chase_end(retp) = recursive_parse_chain(str, 1); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 428 | char ** |
| Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 429 | process_options(int argc, char **argv) |
| 430 | { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 431 | progname = argv[0]; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 432 | options.output = stderr; |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 433 | options.no_signals = 0; |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 434 | #if defined(HAVE_LIBUNWIND) |
| 435 | options.bt_depth = -1; |
| 436 | #endif /* defined(HAVE_LIBUNWIND) */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 437 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 438 | guess_cols(); |
| 439 | |
| Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 440 | int libcalls = 1; |
| 441 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 442 | while (1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 443 | int c; |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 444 | char *p; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 445 | int option_index = 0; |
| 446 | static struct option long_options[] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 447 | {"align", 1, 0, 'a'}, |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 448 | {"config", 1, 0, 'F'}, |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 449 | {"debug", 1, 0, 'D'}, |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 450 | # ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 451 | {"demangle", 0, 0, 'C'}, |
| Juan Cespedes | 8e3e082 | 1998-09-24 13:49:55 +0200 | [diff] [blame] | 452 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 453 | {"indent", 1, 0, 'n'}, |
| 454 | {"help", 0, 0, 'h'}, |
| 455 | {"library", 1, 0, 'l'}, |
| 456 | {"output", 1, 0, 'o'}, |
| 457 | {"version", 0, 0, 'V'}, |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 458 | {"no-signals", 0, 0, 'b'}, |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 459 | #if defined(HAVE_LIBUNWIND) |
| 460 | {"where", 1, 0, 'w'}, |
| 461 | #endif /* defined(HAVE_LIBUNWIND) */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 462 | {0, 0, 0, 0} |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 463 | }; |
| Petr Machata | 796267f | 2012-04-04 19:09:37 +0200 | [diff] [blame] | 464 | c = getopt_long(argc, argv, "+cfhiLrStTVb" |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 465 | # ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 466 | "C" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 467 | # endif |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 468 | #if defined(HAVE_LIBUNWIND) |
| 469 | "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options, |
| 470 | #else /* !defined(HAVE_LIBUNWIND) */ |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 471 | "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options, |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 472 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 473 | &option_index); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 474 | if (c == -1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 475 | break; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 476 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 477 | switch (c) { |
| 478 | case 'a': |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 479 | options.align = atoi(optarg); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 480 | break; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 481 | case 'A': |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 482 | options.arraylen = atoi(optarg); |
| Steve Fink | 1150bc4 | 2006-08-07 06:04:43 +0200 | [diff] [blame] | 483 | break; |
| Joe Damato | 535e738 | 2010-11-08 15:47:43 -0800 | [diff] [blame] | 484 | case 'b': |
| 485 | options.no_signals = 1; |
| 486 | break; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 487 | case 'c': |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 488 | options.summary++; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 489 | break; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 490 | #ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 491 | case 'C': |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 492 | options.demangle++; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 493 | break; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 494 | #endif |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 495 | case 'D': |
| 496 | if (optarg[0]=='h') { |
| 497 | usage_debug(); |
| 498 | exit(0); |
| 499 | } |
| 500 | options.debug = strtoul(optarg,&p,8); |
| 501 | if (*p) { |
| 502 | fprintf(stderr, "%s: --debug requires an octal argument\n", progname); |
| 503 | err_usage(); |
| 504 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 505 | break; |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 506 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 507 | case 'e': |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 508 | parse_filter_chain(optarg, &options.plt_filter); |
| Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 509 | break; |
| 510 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 511 | case 'f': |
| Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 512 | options.follow = 1; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 513 | break; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 514 | case 'F': |
| 515 | { |
| 516 | struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t)); |
| 517 | if (!tmp) { |
| 518 | perror("ltrace: malloc"); |
| 519 | exit(1); |
| 520 | } |
| 521 | tmp->filename = strdup(optarg); |
| 522 | tmp->next = opt_F; |
| 523 | opt_F = tmp; |
| 524 | break; |
| 525 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 526 | case 'h': |
| 527 | usage(); |
| 528 | exit(0); |
| 529 | case 'i': |
| 530 | opt_i++; |
| 531 | break; |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 532 | |
| 533 | case 'l': { |
| 534 | size_t patlen = strlen(optarg); |
| 535 | char buf[patlen + 2]; |
| 536 | sprintf(buf, "@%s", optarg); |
| 537 | *slist_chase_end(&options.export_filter) |
| 538 | = recursive_parse_chain(buf, 0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 539 | break; |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 542 | case 'L': |
| Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 543 | libcalls = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 544 | break; |
| Petr Machata | 5bddfcc | 2012-05-03 18:40:20 +0200 | [diff] [blame] | 545 | case 'n': { |
| 546 | char *endptr; |
| 547 | long int l = strtol(optarg, &endptr, 0); |
| 548 | /* Arbitrary cut-off. Nobody needs to indent |
| 549 | * more than, say, 8, anyway. */ |
| 550 | if (l < 0 || l > 20 || *optarg == 0 || *endptr != 0) { |
| 551 | fprintf(stderr, "Invalid argument to -n: '%s'." |
| 552 | " Use integer 0..20.\n", optarg); |
| 553 | exit(1); |
| 554 | } |
| 555 | options.indent = (int)l; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 556 | break; |
| Petr Machata | 5bddfcc | 2012-05-03 18:40:20 +0200 | [diff] [blame] | 557 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 558 | case 'o': |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 559 | options.output = fopen(optarg, "w"); |
| 560 | if (!options.output) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 561 | fprintf(stderr, |
| 562 | "can't open %s for writing: %s\n", |
| 563 | optarg, strerror(errno)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 564 | exit(1); |
| 565 | } |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 566 | setvbuf(options.output, (char *)NULL, _IOLBF, 0); |
| 567 | fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 568 | break; |
| 569 | case 'p': |
| 570 | { |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 571 | struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 572 | if (!tmp) { |
| 573 | perror("ltrace: malloc"); |
| 574 | exit(1); |
| 575 | } |
| 576 | tmp->pid = atoi(optarg); |
| 577 | tmp->next = opt_p; |
| 578 | opt_p = tmp; |
| 579 | break; |
| 580 | } |
| 581 | case 'r': |
| 582 | opt_r++; |
| 583 | break; |
| 584 | case 's': |
| Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 585 | options.strlen = atoi(optarg); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 586 | break; |
| 587 | case 'S': |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 588 | options.syscalls = 1; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 589 | break; |
| 590 | case 't': |
| 591 | opt_t++; |
| 592 | break; |
| 593 | case 'T': |
| 594 | opt_T++; |
| 595 | break; |
| 596 | case 'u': |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 597 | options.user = optarg; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 598 | break; |
| 599 | case 'V': |
| 600 | printf("ltrace version " PACKAGE_VERSION ".\n" |
| Juan Cespedes | 1423f08 | 2009-04-07 00:45:33 +0200 | [diff] [blame] | 601 | "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n" |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 602 | "This is free software; see the GNU General Public Licence\n" |
| 603 | "version 2 or later for copying conditions. There is NO warranty.\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 604 | exit(0); |
| Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 605 | break; |
| Joe Damato | 535e738 | 2010-11-08 15:47:43 -0800 | [diff] [blame] | 606 | #if defined(HAVE_LIBUNWIND) |
| 607 | case 'w': |
| 608 | options.bt_depth = atoi(optarg); |
| Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 609 | break; |
| Joe Damato | 535e738 | 2010-11-08 15:47:43 -0800 | [diff] [blame] | 610 | #endif /* defined(HAVE_LIBUNWIND) */ |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 611 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 612 | case 'x': |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 613 | parse_filter_chain(optarg, &options.static_filter); |
| 614 | break; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 615 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 616 | default: |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 617 | err_usage(); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 618 | } |
| 619 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 620 | argc -= optind; |
| 621 | argv += optind; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 622 | |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 623 | if (!opt_F) { |
| 624 | opt_F = malloc(sizeof(struct opt_F_t)); |
| 625 | opt_F->next = malloc(sizeof(struct opt_F_t)); |
| 626 | opt_F->next->next = NULL; |
| 627 | opt_F->filename = USER_CONFIG_FILE; |
| 628 | opt_F->next->filename = SYSTEM_CONFIG_FILE; |
| 629 | } |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 630 | /* Reverse the config file list since it was built by |
| 631 | * prepending, and it would make more sense to process the |
| 632 | * files in the order they were given. Probably it would make |
| 633 | * more sense to keep a tail pointer instead? */ |
| 634 | { |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 635 | struct opt_F_t *egg = NULL; |
| 636 | struct opt_F_t *chicken; |
| 637 | while (opt_F) { |
| 638 | chicken = opt_F->next; |
| 639 | opt_F->next = egg; |
| 640 | egg = opt_F; |
| 641 | opt_F = chicken; |
| 642 | } |
| 643 | opt_F = egg; |
| Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 646 | /* If neither -e, nor -l, nor -L are used, set default -e. |
| 647 | * Use @MAIN for now, as that's what ltrace used to have in |
| 648 | * the past. XXX Maybe we should make this "*" instead. */ |
| 649 | if (libcalls |
| 650 | && options.plt_filter == NULL |
| 651 | && options.export_filter == NULL) { |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 652 | parse_filter_chain("@MAIN", &options.plt_filter); |
| Petr Machata | 0367389 | 2012-04-03 13:51:09 +0200 | [diff] [blame] | 653 | options.hide_caller = 1; |
| 654 | } |
| Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 655 | if (!libcalls && options.plt_filter != NULL) { |
| 656 | fprintf(stderr, |
| 657 | "%s: Option -L can't be used with -e or -l.\n", |
| 658 | progname); |
| 659 | err_usage(); |
| 660 | } |
| Petr Machata | e0973cb | 2012-04-01 19:36:41 +0200 | [diff] [blame] | 661 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 662 | if (!opt_p && argc < 1) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 663 | fprintf(stderr, "%s: too few arguments\n", progname); |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 664 | err_usage(); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 665 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 666 | if (opt_r && opt_t) { |
| Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 667 | fprintf(stderr, |
| 668 | "%s: Options -r and -t can't be used together\n", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 669 | progname); |
| Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 670 | err_usage(); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 671 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 672 | if (argc > 0) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 673 | command = search_for_command(argv[0]); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 674 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 675 | return &argv[0]; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 676 | } |