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