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