Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 94773bf | 2014-01-14 16:01:35 +0100 | [diff] [blame] | 3 | * Copyright (C) 2012, 2013, 2014 Petr Machata, Red Hat Inc. |
Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 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 | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 31 | #include <assert.h> |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 32 | #include <errno.h> |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <getopt.h> |
| 35 | #include <limits.h> |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 40 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 41 | #include "common.h" |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 42 | #include "filter.h" |
| 43 | #include "glob.h" |
Petr Machata | 3ac8db6 | 2012-11-23 18:43:10 +0100 | [diff] [blame] | 44 | #include "demangle.h" |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 45 | |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 46 | struct options_t options = { |
Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 47 | .align = DEFAULT_ALIGN, /* alignment column for results */ |
| 48 | .user = NULL, /* username to run command as */ |
| 49 | .syscalls = 0, /* display syscalls */ |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 50 | #ifdef USE_DEMANGLE |
Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 51 | .demangle = 0, /* Demangle low-level symbol names */ |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 52 | #endif |
Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 53 | .indent = 0, /* indent output according to program flow */ |
| 54 | .output = NULL, /* output to a specific file */ |
Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 55 | .summary = 0, /* Report a summary on program exit */ |
| 56 | .debug = 0, /* debug */ |
| 57 | .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */ |
| 58 | .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */ |
| 59 | .follow = 0, /* trace child processes */ |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 62 | static char *progname; /* Program name (`ltrace') */ |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 63 | int opt_i = 0; /* instruction pointer */ |
Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 64 | int opt_r = 0; /* print relative timestamp */ |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 65 | int opt_t = 0; /* print absolute timestamp */ |
Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 66 | int opt_T = 0; /* show the time spent inside each call */ |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 67 | |
| 68 | /* List of pids given to option -p: */ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 69 | 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] | 70 | |
Petr Machata | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 71 | /* Vector of struct opt_F_t. */ |
| 72 | struct vect opt_F; |
Steve Fink | 58c73a7 | 2006-07-17 23:18:35 +0200 | [diff] [blame] | 73 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 74 | static void |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 75 | err_usage(void) { |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 76 | fprintf(stderr, "Try `%s --help' for more information.\n", progname); |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 77 | exit(1); |
| 78 | } |
| 79 | |
| 80 | static void |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 81 | usage(void) { |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 82 | fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 83 | "Trace library calls of a given program.\n\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | " -a, --align=COLUMN align return values in a secific column.\n" |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 85 | " -A MAXELTS maximum number of array elements to print.\n" |
Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 86 | " -b, --no-signals don't print signals.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 87 | " -c count time and calls, and report a summary on exit.\n" |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 88 | # ifdef USE_DEMANGLE |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 89 | " -C, --demangle decode low-level symbol names into user-level names.\n" |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 90 | # endif |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 91 | " -D, --debug=MASK enable debugging (see -Dh or --debug=help).\n" |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 92 | " -Dh, --debug=help show help on debugging.\n" |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 93 | " -e FILTER modify which library calls to trace.\n" |
Juan Cespedes | c4e53a9 | 2009-05-06 20:36:42 +0200 | [diff] [blame] | 94 | " -f trace children (fork() and clone()).\n" |
Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 95 | " -F, --config=FILE load alternate configuration file (may be repeated).\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 96 | " -h, --help display this help and exit.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 97 | " -i print instruction pointer at time of library call.\n" |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 98 | " -l, --library=LIBRARY_PATTERN only trace symbols implemented by this library.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 99 | " -L do NOT display library calls.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 100 | " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 101 | " -o, --output=FILENAME write the trace output to file with given name.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 102 | " -p PID attach to the process with the process ID pid.\n" |
| 103 | " -r print relative timestamps.\n" |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 104 | " -s STRSIZE specify the maximum string size to print.\n" |
| 105 | " -S trace system calls as well as library calls.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 106 | " -t, -tt, -ttt print absolute timestamps.\n" |
| 107 | " -T show the time spent inside each call.\n" |
| 108 | " -u USERNAME run command with the userid, groupid of username.\n" |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 109 | " -V, --version output version information and exit.\n" |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 110 | #if defined(HAVE_UNWINDER) |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 111 | " -w, --where=NR print backtrace showing NR stack frames at most.\n" |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 112 | #endif /* defined(HAVE_UNWINDER) */ |
Petr Machata | 4b3bda4 | 2013-01-11 23:32:17 +0100 | [diff] [blame] | 113 | " -x FILTER modify which static functions to trace.\n" |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 114 | "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 115 | progname); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 118 | static void |
| 119 | usage_debug(void) { |
| 120 | fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname); |
| 121 | fprintf(stdout, |
| 122 | "\n" |
| 123 | " number ref. in source description\n" |
| 124 | " 1 general Generally helpful progress information\n" |
| 125 | " 10 event Shows every event received by a traced process\n" |
| 126 | " 20 process Shows actions carried upon a traced processes\n" |
| 127 | " 40 function Shows every entry to internal functions\n" |
| 128 | "\n" |
| 129 | "Debugging options are mixed using bitwise-or.\n" |
| 130 | "Note that the meanings and values are subject to change.\n" |
Petr Machata | 94773bf | 2014-01-14 16:01:35 +0100 | [diff] [blame] | 131 | "Also note that these values are used inconsistently in ltrace, and the\n" |
| 132 | "only debuglevel that you can rely on is -D77 that will show everything.\n" |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 133 | ); |
| 134 | } |
| 135 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 136 | static char * |
| 137 | search_for_command(char *filename) { |
Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 138 | static char pathname[PATH_MAX]; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 139 | char *path; |
| 140 | int m, n; |
| 141 | |
| 142 | if (strchr(filename, '/')) { |
| 143 | return filename; |
| 144 | } |
| 145 | for (path = getenv("PATH"); path && *path; path += m) { |
| 146 | if (strchr(path, ':')) { |
| 147 | n = strchr(path, ':') - path; |
| 148 | m = n + 1; |
| 149 | } else { |
| 150 | m = n = strlen(path); |
| 151 | } |
Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 152 | if (n + strlen(filename) + 1 >= PATH_MAX) { |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 153 | fprintf(stderr, "Error: filename too long.\n"); |
Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 154 | exit(1); |
| 155 | } |
| 156 | strncpy(pathname, path, n); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 157 | if (n && pathname[n - 1] != '/') { |
| 158 | pathname[n++] = '/'; |
| 159 | } |
| 160 | strcpy(pathname + n, filename); |
| 161 | if (!access(pathname, X_OK)) { |
| 162 | return pathname; |
| 163 | } |
| 164 | } |
| 165 | return filename; |
| 166 | } |
| 167 | |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 168 | static void |
| 169 | guess_cols(void) { |
| 170 | struct winsize ws; |
| 171 | char *c; |
| 172 | |
| 173 | options.align = DEFAULT_ALIGN; |
| 174 | c = getenv("COLUMNS"); |
| 175 | if (c && *c) { |
| 176 | char *endptr; |
| 177 | int cols; |
| 178 | cols = strtol(c, &endptr, 0); |
| 179 | if (cols > 0 && !*endptr) { |
| 180 | options.align = cols * 5 / 8; |
| 181 | } |
| 182 | } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { |
| 183 | options.align = ws.ws_col * 5 / 8; |
Juan Cespedes | 43739a6 | 2009-04-07 13:10:08 +0200 | [diff] [blame] | 184 | } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { |
| 185 | options.align = ws.ws_col * 5 / 8; |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 189 | static int |
| 190 | compile_libname(const char *expr, const char *a_lib, int lib_re_p, |
| 191 | struct filter_lib_matcher *matcher) |
| 192 | { |
| 193 | if (strcmp(a_lib, "MAIN") == 0) { |
| 194 | filter_lib_matcher_main_init(matcher); |
| 195 | } else { |
| 196 | /* Add ^ and $ to the library expression as well. */ |
| 197 | char lib[strlen(a_lib) + 3]; |
| 198 | sprintf(lib, "^%s$", a_lib); |
| 199 | |
| 200 | enum filter_lib_matcher_type type |
| 201 | = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME; |
| 202 | |
| 203 | regex_t lib_re; |
| 204 | int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0); |
Petr Machata | b6c5c8c | 2012-12-08 03:23:39 +0100 | [diff] [blame] | 205 | if (status != 0) { |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 206 | char buf[100]; |
| 207 | regerror(status, &lib_re, buf, sizeof buf); |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 208 | fprintf(stderr, "Couldn't compile '%s': %s.\n", |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 209 | expr, buf); |
| 210 | return -1; |
| 211 | } |
| 212 | filter_lib_matcher_name_init(matcher, type, lib_re); |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 217 | static int |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 218 | add_filter_rule(struct filter *filt, const char *expr, |
| 219 | enum filter_rule_type type, |
Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 220 | const char *a_sym, int sym_re_p, |
| 221 | const char *a_lib, int lib_re_p) |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 222 | { |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 223 | struct filter_rule *rule = malloc(sizeof(*rule)); |
| 224 | struct filter_lib_matcher *matcher = malloc(sizeof(*matcher)); |
| 225 | |
| 226 | if (rule == NULL || matcher == NULL) { |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 227 | fail: |
| 228 | free(rule); |
| 229 | free(matcher); |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 230 | return -1; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | regex_t symbol_re; |
Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 234 | { |
| 235 | /* Add ^ to the start of expression and $ to the end, so that |
| 236 | * we match the whole symbol name. Let the user write the "*" |
| 237 | * explicitly if they wish. */ |
| 238 | char sym[strlen(a_sym) + 3]; |
| 239 | sprintf(sym, "^%s$", a_sym); |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 240 | int status = (sym_re_p ? regcomp : globcomp) |
| 241 | (&symbol_re, sym, 0); |
Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 242 | if (status != 0) { |
| 243 | char buf[100]; |
| 244 | regerror(status, &symbol_re, buf, sizeof buf); |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 245 | fprintf(stderr, "Couldn't compile '%s': %s.\n", |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 246 | expr, buf); |
Petr Machata | 5bc4e7f | 2012-04-06 22:28:24 +0200 | [diff] [blame] | 247 | goto fail; |
| 248 | } |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 251 | if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) { |
| 252 | regfree(&symbol_re); |
| 253 | goto fail; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 256 | filter_rule_init(rule, type, matcher, symbol_re); |
Petr Machata | e0973cb | 2012-04-01 19:36:41 +0200 | [diff] [blame] | 257 | filter_add_rule(filt, rule); |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 258 | return 0; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static int |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 262 | grok_libname_pattern(char **libnamep, char **libendp) |
| 263 | { |
| 264 | char *libname = *libnamep; |
| 265 | char *libend = *libendp; |
| 266 | |
| 267 | if (libend[0] != '/') |
| 268 | return 0; |
| 269 | |
| 270 | *libend-- = 0; |
| 271 | if (libname != libend && libname[0] == '/') |
| 272 | ++libname; |
| 273 | else |
| 274 | fprintf(stderr, "Unmatched '/' in library name.\n"); |
| 275 | |
| 276 | *libendp = libend; |
| 277 | *libnamep = libname; |
| 278 | return 1; |
| 279 | } |
| 280 | |
| 281 | static int |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 282 | parse_filter(struct filter *filt, char *expr, int operators) |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 283 | { |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 284 | /* Filter is a chain of sym@lib rules separated by '-' or '+'. |
| 285 | * If the filter expression starts with '-', the missing |
| 286 | * initial rule is implicitly *@*. */ |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 287 | |
| 288 | enum filter_rule_type type = FR_ADD; |
| 289 | |
| 290 | while (*expr != 0) { |
Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 291 | size_t s = strcspn(expr, &"-+@"[operators ? 0 : 2]); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 292 | char *symname = expr; |
| 293 | char *libname; |
| 294 | char *next = expr + s + 1; |
| 295 | enum filter_rule_type this_type = type; |
| 296 | |
| 297 | if (expr[s] == 0) { |
| 298 | libname = "*"; |
| 299 | expr = next - 1; |
| 300 | |
Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 301 | } else if (expr[s] == '-' || expr[s] == '+') { |
| 302 | type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD; |
| 303 | expr[s] = 0; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 304 | libname = "*"; |
| 305 | expr = next; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 306 | |
| 307 | } else { |
| 308 | assert(expr[s] == '@'); |
| 309 | expr[s] = 0; |
Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 310 | s = strcspn(next, &"-+"[operators ? 0 : 2]); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 311 | if (s == 0) { |
| 312 | libname = "*"; |
| 313 | expr = next; |
| 314 | } else if (next[s] == 0) { |
| 315 | expr = next + s; |
| 316 | libname = next; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 317 | } else { |
Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 318 | assert(next[s] == '-' || next[s] == '+'); |
| 319 | type = next[s] == '-' ? FR_SUBTRACT : FR_ADD; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 320 | next[s] = 0; |
| 321 | expr = next + s + 1; |
| 322 | libname = next; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | assert(*libname != 0); |
| 327 | char *symend = symname + strlen(symname) - 1; |
| 328 | char *libend = libname + strlen(libname) - 1; |
| 329 | int sym_is_re = 0; |
| 330 | int lib_is_re = 0; |
| 331 | |
| 332 | /* |
| 333 | * /xxx/@... and ...@/xxx/ means that xxx are regular |
| 334 | * expressions. They are globs otherwise. |
| 335 | * |
| 336 | * /xxx@yyy/ is the same as /xxx/@/yyy/ |
| 337 | * |
| 338 | * @/xxx matches library path name |
| 339 | * @.xxx matches library relative path name |
| 340 | */ |
| 341 | if (symname[0] == '/') { |
| 342 | if (symname != symend && symend[0] == '/') { |
| 343 | ++symname; |
| 344 | *symend-- = 0; |
| 345 | sym_is_re = 1; |
| 346 | |
| 347 | } else { |
| 348 | sym_is_re = 1; |
| 349 | lib_is_re = 1; |
| 350 | ++symname; |
| 351 | |
| 352 | /* /XXX@YYY/ is the same as |
| 353 | * /XXX/@/YYY/. */ |
| 354 | if (libend[0] != '/') |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 355 | fprintf(stderr, "Unmatched '/'" |
| 356 | " in symbol name.\n"); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 357 | else |
| 358 | *libend-- = 0; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* If libname ends in '/', then we expect '/' in the |
| 363 | * beginning too. Otherwise the initial '/' is part |
| 364 | * of absolute file name. */ |
Petr Machata | 02b9607 | 2012-09-25 23:10:14 +0200 | [diff] [blame] | 365 | if (!lib_is_re) |
| 366 | lib_is_re = grok_libname_pattern(&libname, &libend); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 367 | |
| 368 | if (*symname == 0) /* /@AA/ */ |
| 369 | symname = "*"; |
| 370 | if (*libname == 0) /* /aa@/ */ |
| 371 | libname = "*"; |
| 372 | |
Petr Machata | a43b2a0 | 2013-03-11 21:21:22 -0400 | [diff] [blame] | 373 | add_filter_rule(filt, expr, this_type, |
| 374 | symname, sym_is_re, |
| 375 | libname, lib_is_re); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static struct filter * |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 382 | recursive_parse_chain(const char *orig, char *expr, int operators) |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 383 | { |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 384 | struct filter *filt = malloc(sizeof(*filt)); |
| 385 | if (filt == NULL) { |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 386 | fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n", |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 387 | expr, strerror(errno)); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
| 390 | |
Petr Machata | 35c8814 | 2012-04-04 00:54:43 +0200 | [diff] [blame] | 391 | filter_init(filt); |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 392 | if (parse_filter(filt, expr, operators) < 0) { |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 393 | fprintf(stderr, "Filter '%s' will be ignored.\n", orig); |
Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 394 | free(filt); |
| 395 | filt = NULL; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 396 | } |
Petr Machata | 050fa7f | 2012-04-23 23:44:36 +0200 | [diff] [blame] | 397 | |
| 398 | return filt; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 401 | static struct filter ** |
| 402 | slist_chase_end(struct filter **begin) |
| 403 | { |
| 404 | for (; *begin != NULL; begin = &(*begin)->next) |
| 405 | ; |
| 406 | return begin; |
| 407 | } |
| 408 | |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 409 | static void |
Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 410 | parse_filter_chain(const char *expr, struct filter **retp) |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 411 | { |
| 412 | char *str = strdup(expr); |
| 413 | if (str == NULL) { |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 414 | fprintf(stderr, "Filter '%s' will be ignored: %s.\n", |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 415 | expr, strerror(errno)); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 416 | return; |
| 417 | } |
Petr Machata | 25f319e | 2012-04-23 23:45:21 +0200 | [diff] [blame] | 418 | /* Support initial '!' for backward compatibility. */ |
| 419 | if (str[0] == '!') |
| 420 | str[0] = '-'; |
| 421 | |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 422 | *slist_chase_end(retp) = recursive_parse_chain(expr, str, 1); |
Petr Machata | dd1ec84 | 2012-10-27 00:34:10 +0200 | [diff] [blame] | 423 | free(str); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 426 | static int |
| 427 | parse_int(const char *optarg, char opt, int min, int max) |
| 428 | { |
| 429 | char *endptr; |
| 430 | long int l = strtol(optarg, &endptr, 0); |
| 431 | if (l < min || (max != 0 && l > max) |
| 432 | || *optarg == 0 || *endptr != 0) { |
| 433 | const char *fmt = max != 0 |
| 434 | ? "Invalid argument to -%c: '%s'. Use integer %d..%d.\n" |
| 435 | : "Invalid argument to -%c: '%s'. Use integer >=%d.\n"; |
| 436 | fprintf(stderr, fmt, opt, optarg, min, max); |
| 437 | exit(1); |
| 438 | } |
| 439 | return (int)l; |
| 440 | } |
| 441 | |
Petr Machata | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 442 | int |
| 443 | parse_colon_separated_list(const char *paths, struct vect *vec) |
| 444 | { |
| 445 | /* PATHS contains a colon-separated list of directories and |
| 446 | * files to load. It's modeled after shell PATH variable, |
| 447 | * which doesn't allow escapes. PYTHONPATH in CPython behaves |
| 448 | * the same way. So let's follow suit, it makes things easier |
| 449 | * to us. */ |
| 450 | |
| 451 | char *clone = strdup(paths); |
| 452 | if (clone == NULL) { |
| 453 | fprintf(stderr, "Couldn't parse argument %s: %s.\n", |
| 454 | paths, strerror(errno)); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | /* It's undesirable to use strtok, because we want the string |
| 459 | * "a::b" to have three elements. */ |
| 460 | char *tok = clone - 1; |
| 461 | char *end = clone + strlen(clone); |
| 462 | while (tok < end) { |
| 463 | ++tok; |
| 464 | size_t len = strcspn(tok, ":"); |
| 465 | tok[len] = 0; |
| 466 | |
| 467 | struct opt_F_t arg = { |
| 468 | .pathname = tok, |
| 469 | .own_pathname = tok == clone, |
| 470 | }; |
| 471 | if (VECT_PUSHBACK(vec, &arg) < 0) |
| 472 | /* Presumably this is not a deal-breaker. */ |
| 473 | fprintf(stderr, "Couldn't store component of %s: %s.\n", |
| 474 | paths, strerror(errno)); |
| 475 | |
| 476 | tok += len; |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | opt_F_destroy(struct opt_F_t *entry) |
| 484 | { |
| 485 | if (entry == NULL) |
| 486 | return; |
| 487 | if (entry->own_pathname) |
| 488 | free(entry->pathname); |
| 489 | } |
| 490 | |
| 491 | enum opt_F_kind |
| 492 | opt_F_get_kind(struct opt_F_t *entry) |
| 493 | { |
| 494 | if (entry->kind == OPT_F_UNKNOWN) { |
| 495 | struct stat st; |
| 496 | if (lstat(entry->pathname, &st) < 0) { |
| 497 | fprintf(stderr, "Couldn't stat %s: %s\n", |
| 498 | entry->pathname, strerror(errno)); |
| 499 | entry->kind = OPT_F_BROKEN; |
| 500 | } else if (S_ISDIR(st.st_mode)) { |
| 501 | entry->kind = OPT_F_DIR; |
Petr Machata | e7be390 | 2012-12-05 01:21:03 +0100 | [diff] [blame] | 502 | } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
Petr Machata | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 503 | entry->kind = OPT_F_FILE; |
| 504 | } else { |
| 505 | fprintf(stderr, "%s is neither a regular file, " |
| 506 | "nor a directory.\n", entry->pathname); |
| 507 | entry->kind = OPT_F_BROKEN; |
| 508 | } |
| 509 | } |
| 510 | assert(entry->kind != OPT_F_UNKNOWN); |
| 511 | return entry->kind; |
| 512 | } |
| 513 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 514 | char ** |
Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 515 | process_options(int argc, char **argv) |
| 516 | { |
Petr Machata | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 517 | VECT_INIT(&opt_F, struct opt_F_t); |
| 518 | |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 519 | progname = argv[0]; |
Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 520 | options.output = stderr; |
Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 521 | options.no_signals = 0; |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 522 | #if defined(HAVE_UNWINDER) |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 523 | options.bt_depth = -1; |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 524 | #endif /* defined(HAVE_UNWINDER) */ |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 525 | |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 526 | guess_cols(); |
| 527 | |
Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 528 | int libcalls = 1; |
| 529 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 530 | while (1) { |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 531 | int c; |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 532 | char *p; |
Petr Machata | 35d0730 | 2012-12-09 11:38:56 +0100 | [diff] [blame] | 533 | #ifdef HAVE_GETOPT_LONG |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 534 | int option_index = 0; |
| 535 | static struct option long_options[] = { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 536 | {"align", 1, 0, 'a'}, |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 537 | {"config", 1, 0, 'F'}, |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 538 | {"debug", 1, 0, 'D'}, |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 539 | # ifdef USE_DEMANGLE |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 540 | {"demangle", 0, 0, 'C'}, |
Petr Machata | 35d0730 | 2012-12-09 11:38:56 +0100 | [diff] [blame] | 541 | # endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 542 | {"indent", 1, 0, 'n'}, |
| 543 | {"help", 0, 0, 'h'}, |
| 544 | {"library", 1, 0, 'l'}, |
| 545 | {"output", 1, 0, 'o'}, |
| 546 | {"version", 0, 0, 'V'}, |
Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 547 | {"no-signals", 0, 0, 'b'}, |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 548 | # if defined(HAVE_UNWINDER) |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 549 | {"where", 1, 0, 'w'}, |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 550 | # endif /* defined(HAVE_UNWINDER) */ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 551 | {0, 0, 0, 0} |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 552 | }; |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 553 | #endif |
Petr Machata | 35d0730 | 2012-12-09 11:38:56 +0100 | [diff] [blame] | 554 | |
| 555 | const char *opts = "+" |
| 556 | #ifdef USE_DEMANGLE |
| 557 | "C" |
| 558 | #endif |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 559 | #if defined(HAVE_UNWINDER) |
Petr Machata | 35d0730 | 2012-12-09 11:38:56 +0100 | [diff] [blame] | 560 | "w:" |
| 561 | #endif |
Petr Machata | 5da68e9 | 2014-02-28 12:37:15 +0100 | [diff] [blame] | 562 | "cfhiLrStTVba:A:D:e:F:l:n:o:p:s:u:x:"; |
Petr Machata | 35d0730 | 2012-12-09 11:38:56 +0100 | [diff] [blame] | 563 | |
| 564 | #ifdef HAVE_GETOPT_LONG |
| 565 | c = getopt_long(argc, argv, opts, long_options, &option_index); |
| 566 | #else |
| 567 | c = getopt(argc, argv, opts); |
| 568 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 569 | if (c == -1) { |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 570 | break; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 571 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 572 | switch (c) { |
| 573 | case 'a': |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 574 | options.align = parse_int(optarg, 'a', 0, 0); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 575 | break; |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 576 | case 'A': |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 577 | options.arraylen = parse_int(optarg, 'A', 0, 0); |
Steve Fink | 1150bc4 | 2006-08-07 06:04:43 +0200 | [diff] [blame] | 578 | break; |
Joe Damato | 535e738 | 2010-11-08 15:47:43 -0800 | [diff] [blame] | 579 | case 'b': |
| 580 | options.no_signals = 1; |
| 581 | break; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 582 | case 'c': |
Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 583 | options.summary++; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 584 | break; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 585 | #ifdef USE_DEMANGLE |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 586 | case 'C': |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 587 | options.demangle++; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 588 | break; |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 589 | #endif |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 590 | case 'D': |
| 591 | if (optarg[0]=='h') { |
| 592 | usage_debug(); |
| 593 | exit(0); |
| 594 | } |
| 595 | options.debug = strtoul(optarg,&p,8); |
| 596 | if (*p) { |
| 597 | fprintf(stderr, "%s: --debug requires an octal argument\n", progname); |
| 598 | err_usage(); |
| 599 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 600 | break; |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 601 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 602 | case 'e': |
Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 603 | parse_filter_chain(optarg, &options.plt_filter); |
Petr Machata | 1e4fed2 | 2012-04-01 00:45:22 +0200 | [diff] [blame] | 604 | break; |
| 605 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 606 | case 'f': |
Juan Cespedes | cc813cd | 2009-04-07 15:45:53 +0200 | [diff] [blame] | 607 | options.follow = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 608 | break; |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 609 | case 'F': |
Petr Machata | 418584d | 2012-12-04 17:53:56 +0100 | [diff] [blame] | 610 | parse_colon_separated_list(optarg, &opt_F); |
| 611 | break; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 612 | case 'h': |
| 613 | usage(); |
| 614 | exit(0); |
| 615 | case 'i': |
| 616 | opt_i++; |
| 617 | break; |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 618 | |
| 619 | case 'l': { |
| 620 | size_t patlen = strlen(optarg); |
| 621 | char buf[patlen + 2]; |
| 622 | sprintf(buf, "@%s", optarg); |
| 623 | *slist_chase_end(&options.export_filter) |
Petr Machata | ec44669 | 2013-01-06 17:14:09 +0100 | [diff] [blame] | 624 | = recursive_parse_chain(buf, buf, 0); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 625 | break; |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 628 | case 'L': |
Petr Machata | 67fa52f | 2012-04-05 02:11:39 +0200 | [diff] [blame] | 629 | libcalls = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 630 | break; |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 631 | case 'n': |
| 632 | options.indent = parse_int(optarg, 'n', 0, 20); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 633 | break; |
| 634 | case 'o': |
Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 635 | options.output = fopen(optarg, "w"); |
| 636 | if (!options.output) { |
Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 637 | fprintf(stderr, |
| 638 | "can't open %s for writing: %s\n", |
| 639 | optarg, strerror(errno)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 640 | exit(1); |
| 641 | } |
Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 642 | setvbuf(options.output, (char *)NULL, _IOLBF, 0); |
| 643 | fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 644 | break; |
| 645 | case 'p': |
| 646 | { |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 647 | struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 648 | if (!tmp) { |
| 649 | perror("ltrace: malloc"); |
| 650 | exit(1); |
| 651 | } |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 652 | tmp->pid = parse_int(optarg, 'p', 1, 0); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 653 | tmp->next = opt_p; |
| 654 | opt_p = tmp; |
| 655 | break; |
| 656 | } |
| 657 | case 'r': |
| 658 | opt_r++; |
| 659 | break; |
| 660 | case 's': |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 661 | options.strlen = parse_int(optarg, 's', 0, 0); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 662 | break; |
| 663 | case 'S': |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 664 | options.syscalls = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 665 | break; |
| 666 | case 't': |
| 667 | opt_t++; |
| 668 | break; |
| 669 | case 'T': |
| 670 | opt_T++; |
| 671 | break; |
| 672 | case 'u': |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 673 | options.user = optarg; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 674 | break; |
| 675 | case 'V': |
Petr Machata | 5cde20b | 2012-12-15 23:54:54 +0100 | [diff] [blame] | 676 | printf("ltrace " PACKAGE_VERSION "\n" |
Petr Machata | 3992a0b | 2013-10-23 00:44:52 +0200 | [diff] [blame] | 677 | "Copyright (C) 2010-2013 Petr Machata, Red Hat Inc.\n" |
Petr Machata | 5cde20b | 2012-12-15 23:54:54 +0100 | [diff] [blame] | 678 | "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n" |
| 679 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 680 | "This is free software: you are free to change and redistribute it.\n" |
| 681 | "There is NO WARRANTY, to the extent permitted by law.\n"); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 682 | exit(0); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 683 | break; |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 684 | #if defined(HAVE_UNWINDER) |
Joe Damato | 535e738 | 2010-11-08 15:47:43 -0800 | [diff] [blame] | 685 | case 'w': |
Petr Machata | 68818a8 | 2012-11-30 21:42:59 +0100 | [diff] [blame] | 686 | options.bt_depth = parse_int(optarg, 'w', 1, 0); |
Joe Damato | 59e3fb1 | 2009-11-06 19:45:10 -0800 | [diff] [blame] | 687 | break; |
Mark Wielaard | dfefa9f | 2014-01-07 21:00:44 +0100 | [diff] [blame] | 688 | #endif /* defined(HAVE_UNWINDER) */ |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 689 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 690 | case 'x': |
Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 691 | parse_filter_chain(optarg, &options.static_filter); |
| 692 | break; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 693 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 694 | default: |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 695 | err_usage(); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 696 | } |
| 697 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 698 | argc -= optind; |
| 699 | argv += optind; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 700 | |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 701 | /* If neither -e, nor -l, nor -L are used, set default -e. |
| 702 | * Use @MAIN for now, as that's what ltrace used to have in |
| 703 | * the past. XXX Maybe we should make this "*" instead. */ |
| 704 | if (libcalls |
| 705 | && options.plt_filter == NULL |
| 706 | && options.export_filter == NULL) { |
Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 707 | parse_filter_chain("@MAIN", &options.plt_filter); |
Petr Machata | 0367389 | 2012-04-03 13:51:09 +0200 | [diff] [blame] | 708 | options.hide_caller = 1; |
| 709 | } |
Petr Machata | 51e74ac | 2012-09-27 23:43:25 +0200 | [diff] [blame] | 710 | if (!libcalls && options.plt_filter != NULL) { |
| 711 | fprintf(stderr, |
| 712 | "%s: Option -L can't be used with -e or -l.\n", |
| 713 | progname); |
| 714 | err_usage(); |
| 715 | } |
Petr Machata | e0973cb | 2012-04-01 19:36:41 +0200 | [diff] [blame] | 716 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 717 | if (!opt_p && argc < 1) { |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 718 | fprintf(stderr, "%s: too few arguments\n", progname); |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 719 | err_usage(); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 720 | } |
Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 721 | if (opt_r && opt_t) { |
Petr Machata | 20a411d | 2012-09-25 22:45:26 +0200 | [diff] [blame] | 722 | fprintf(stderr, |
| 723 | "%s: Options -r and -t can't be used together\n", |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 724 | progname); |
Juan Cespedes | c5c744a | 2009-07-23 18:22:58 +0200 | [diff] [blame] | 725 | err_usage(); |
Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 726 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 727 | if (argc > 0) { |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 728 | command = search_for_command(argv[0]); |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 729 | } |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 730 | return &argv[0]; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 731 | } |