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