blob: d4e16a8a7367a22accab3a59ce369ed4c68f65c6 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +02002
Juan Cespedesce377d52008-12-16 19:38:10 +01003#include <sys/ioctl.h>
Petr Machata2b46cfc2012-02-18 11:17:29 +01004#include <assert.h>
Petr Machata1e4fed22012-04-01 00:45:22 +02005#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 Cespedesac3db291998-04-25 14:31:58 +020013
Juan Cespedesf7281232009-06-25 16:11:21 +020014#include "common.h"
Petr Machata1e4fed22012-04-01 00:45:22 +020015#include "filter.h"
16#include "glob.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010017
Steve Fink58c73a72006-07-17 23:18:35 +020018#ifndef SYSCONFDIR
19#define SYSCONFDIR "/etc"
20#endif
21
Olaf Heringe948e582006-10-12 23:53:44 +020022#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020023#define USER_CONFIG_FILE "~/.ltrace.conf"
24
Juan Cespedesce377d52008-12-16 19:38:10 +010025struct options_t options = {
Juan Cespedesda9b9532009-04-07 15:33:50 +020026 .align = DEFAULT_ALIGN, /* alignment column for results */
27 .user = NULL, /* username to run command as */
28 .syscalls = 0, /* display syscalls */
29 .libcalls = 1, /* display library calls */
Juan Cespedesce377d52008-12-16 19:38:10 +010030#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020031 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010032#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020033 .indent = 0, /* indent output according to program flow */
34 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020035 .summary = 0, /* Report a summary on program exit */
36 .debug = 0, /* debug */
37 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
38 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
39 .follow = 0, /* trace child processes */
Juan Cespedesce377d52008-12-16 19:38:10 +010040};
41
Juan Cespedesac3db291998-04-25 14:31:58 +020042static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010043int opt_i = 0; /* instruction pointer */
Juan Cespedesf666d191998-09-20 23:04:34 +020044int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020045int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010046int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010047
48/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010050
Ian Wienand9a2ad352006-02-20 22:44:45 +010051/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010052struct opt_x_t *opt_x = NULL;
Joe Damato7b0a7de2010-11-08 15:47:34 -080053unsigned int opt_x_cnt = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +010054
Steve Fink58c73a72006-07-17 23:18:35 +020055/* List of filenames give to option -F: */
56struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
57
Paul Gilliambe320772006-04-24 22:06:23 +020058#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010059/* Set a break on the routine named here in order to re-initialize breakpoints
60 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020061char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
62#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010063
Juan Cespedesf1350522008-12-16 18:19:58 +010064static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020065err_usage(void) {
Juan Cespedesc5c744a2009-07-23 18:22:58 +020066 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020067 exit(1);
68}
69
70static void
Juan Cespedesf1350522008-12-16 18:19:58 +010071usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020072 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +020075 " -A ARRAYLEN maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080076 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010078# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020080# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +020081 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020082 " -Dh, --debug=help show help on debugging.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020084 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020085 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Joe Damatofa2aefc2010-10-30 19:56:50 -070086 " -g, --no-plt disable breakpoints on PLT entries.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -i print instruction pointer at time of library call.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -l, --library=FILE print library calls from this library only.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -o, --output=FILE write the trace output to that file.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -p PID attach to the process with the process ID pid.\n"
94 " -r print relative timestamps.\n"
95 " -s STRLEN specify the maximum string size to print.\n"
96 " -S display system calls.\n"
97 " -t, -tt, -ttt print absolute timestamps.\n"
98 " -T show the time spent inside each call.\n"
99 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700101#if defined(HAVE_LIBUNWIND)
102 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n"
103#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200105#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200107#endif
108 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100110}
111
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200112static void
113usage_debug(void) {
114 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
115 fprintf(stdout,
116 "\n"
117 " number ref. in source description\n"
118 " 1 general Generally helpful progress information\n"
119 " 10 event Shows every event received by a traced process\n"
120 " 20 process Shows actions carried upon a traced processes\n"
121 " 40 function Shows every entry to internal functions\n"
122 "\n"
123 "Debugging options are mixed using bitwise-or.\n"
124 "Note that the meanings and values are subject to change.\n"
125 );
126}
127
Juan Cespedesf1350522008-12-16 18:19:58 +0100128static char *
129search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100130 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100131 char *path;
132 int m, n;
133
134 if (strchr(filename, '/')) {
135 return filename;
136 }
137 for (path = getenv("PATH"); path && *path; path += m) {
138 if (strchr(path, ':')) {
139 n = strchr(path, ':') - path;
140 m = n + 1;
141 } else {
142 m = n = strlen(path);
143 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100144 if (n + strlen(filename) + 1 >= PATH_MAX) {
145 fprintf(stderr, "Error: filename too long\n");
146 exit(1);
147 }
148 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149 if (n && pathname[n - 1] != '/') {
150 pathname[n++] = '/';
151 }
152 strcpy(pathname + n, filename);
153 if (!access(pathname, X_OK)) {
154 return pathname;
155 }
156 }
157 return filename;
158}
159
Juan Cespedesce377d52008-12-16 19:38:10 +0100160static void
161guess_cols(void) {
162 struct winsize ws;
163 char *c;
164
165 options.align = DEFAULT_ALIGN;
166 c = getenv("COLUMNS");
167 if (c && *c) {
168 char *endptr;
169 int cols;
170 cols = strtol(c, &endptr, 0);
171 if (cols > 0 && !*endptr) {
172 options.align = cols * 5 / 8;
173 }
174 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
175 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200176 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
177 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100178 }
179}
180
Petr Machata1e4fed22012-04-01 00:45:22 +0200181static void
182add_filter_rule(struct filter *filt, const char *expr,
183 enum filter_rule_type type,
184 const char *sym, int sym_re_p,
185 const char *lib, int lib_re_p)
186{
187 fprintf(stderr, "add_filter_rule, type = %d\n", type);
188 fprintf(stderr, "+ symname = %s (re=%d)\n", sym, sym_re_p);
189 fprintf(stderr, "+ libname = %s (re=%d)\n", lib, lib_re_p);
190 struct filter_rule *rule = malloc(sizeof(*rule));
191 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher));
192
193 if (rule == NULL || matcher == NULL) {
194 error(0, errno, "rule near '%s' will be ignored", expr);
195 fail:
196 free(rule);
197 free(matcher);
198 return;
199 }
200
201 regex_t symbol_re;
202 int status = (sym_re_p ? regcomp : globcomp)(&symbol_re, sym, 0);
203 if (status != 0) {
204 char buf[100];
205 regerror(status, &symbol_re, buf, sizeof buf);
206 error(0, 0, "rule near '%s' will be ignored: %s", expr, buf);
207 goto fail;
208 }
209
Petr Machata0b55b582012-04-02 00:38:46 +0200210 if (strcmp(lib, "MAIN") == 0) {
211 filter_lib_matcher_main_init(matcher);
212 } else {
213 enum filter_lib_matcher_type type
214 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
Petr Machata1e4fed22012-04-01 00:45:22 +0200215
Petr Machata0b55b582012-04-02 00:38:46 +0200216 regex_t lib_re;
217 status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
218 if (status != 0) {
219 char buf[100];
220 regerror(status, &lib_re, buf, sizeof buf);
221 error(0, 0, "rule near '%s' will be ignored: %s",
222 expr, buf);
223
224 regfree(&symbol_re);
225 goto fail;
226 }
227 filter_lib_matcher_name_init(matcher, type, lib_re);
Petr Machata1e4fed22012-04-01 00:45:22 +0200228 }
229
Petr Machata1e4fed22012-04-01 00:45:22 +0200230 filter_rule_init(rule, type, matcher, symbol_re);
Petr Machatae0973cb2012-04-01 19:36:41 +0200231 filter_add_rule(filt, rule);
Petr Machata1e4fed22012-04-01 00:45:22 +0200232}
233
234static int
235parse_filter(struct filter *filt, char *expr)
236{
237 fprintf(stderr, "filter '%s'\n", expr);
238
239 /* Filter is a chain of sym@lib rules separated by '-'. If
240 * the filter expression starts with '-', the missing initial
241 * rule is implicitly *@*. */
242
243 enum filter_rule_type type = FR_ADD;
244
245 while (*expr != 0) {
246 size_t s = strcspn(expr, "@-");
247 char *symname = expr;
248 char *libname;
249 char *next = expr + s + 1;
250 enum filter_rule_type this_type = type;
251
252 if (expr[s] == 0) {
253 libname = "*";
254 expr = next - 1;
255
256 } else if (expr[s] == '-') {
257 libname = "*";
258 expr = next;
259 type = FR_SUBTRACT;
260
261 } else {
262 assert(expr[s] == '@');
263 expr[s] = 0;
264 s = strcspn(next, "-");
265 if (s == 0) {
266 libname = "*";
267 expr = next;
268 } else if (next[s] == 0) {
269 expr = next + s;
270 libname = next;
271
272 } else {
273 assert(next[s] == '-');
274 type = FR_SUBTRACT;
275 next[s] = 0;
276 expr = next + s + 1;
277 libname = next;
278 }
279 }
280
281 assert(*libname != 0);
282 char *symend = symname + strlen(symname) - 1;
283 char *libend = libname + strlen(libname) - 1;
284 int sym_is_re = 0;
285 int lib_is_re = 0;
286
287 /*
288 * /xxx/@... and ...@/xxx/ means that xxx are regular
289 * expressions. They are globs otherwise.
290 *
291 * /xxx@yyy/ is the same as /xxx/@/yyy/
292 *
293 * @/xxx matches library path name
294 * @.xxx matches library relative path name
295 */
296 if (symname[0] == '/') {
297 if (symname != symend && symend[0] == '/') {
298 ++symname;
299 *symend-- = 0;
300 sym_is_re = 1;
301
302 } else {
303 sym_is_re = 1;
304 lib_is_re = 1;
305 ++symname;
306
307 /* /XXX@YYY/ is the same as
308 * /XXX/@/YYY/. */
309 if (libend[0] != '/')
310 error(0, 0, "unmatched '/'"
311 " in symbol name");
312 else
313 *libend-- = 0;
314 }
315 }
316
317 /* If libname ends in '/', then we expect '/' in the
318 * beginning too. Otherwise the initial '/' is part
319 * of absolute file name. */
320 if (!lib_is_re && libend[0] == '/') {
321 lib_is_re = 1;
322 *libend-- = 0;
323 if (libname != libend && libname[0] == '/')
324 ++libname;
325 else
326 error(0, 0, "unmatched '/' in library name");
327 }
328
329 if (*symname == 0) /* /@AA/ */
330 symname = "*";
331 if (*libname == 0) /* /aa@/ */
332 libname = "*";
333
334 add_filter_rule(filt, expr, this_type,
335 symname, sym_is_re,
336 libname, lib_is_re);
337 }
338
339 return 0;
340}
341
342static struct filter *
343recursive_parse_chain(char *expr)
344{
345 /* Event expression grammar:
346 *
347 * Chain ::= Filter FilterList
348 * FilterList ::= eps | ',' Filter FilterList
349 * Filter ::= Rule RuleList
350 * RuleList ::= eps | '-' Rule RuleList
351 * Rule ::= eps | Glob Soname
352 * Soname ::= eps | '@' Glob | '@' '/' Filename
353 * Glob ::= '/' Regex '/'
354 */
355
356 struct filter *filt = malloc(sizeof(*filt));
357 if (filt == NULL) {
358 error(0, errno, "(part of) filter will be ignored: '%s'", expr);
359 return NULL;
360 }
361
Petr Machata35c88142012-04-04 00:54:43 +0200362 filter_init(filt);
Petr Machata1e4fed22012-04-01 00:45:22 +0200363 struct filter *next = NULL;
364 char *it;
365 int escape = 0;
366 for (it = expr; ; ++it) {
367 if (*it == 0)
368 goto done;
369
370 if (escape) {
371 escape = 0;
372 continue;
373 }
374
375 if (*it == '\\') {
376 escape = 1;
377
378 } else if (*it == ',') {
379 *it = 0;
380 next = recursive_parse_chain(it + 1);
381 done:
382 filt->next = next;
383 if (parse_filter(filt, expr) < 0) {
384 fprintf(stderr,
385 "Filter '%s' will be ignored.\n", expr);
386 free(filt);
387 filt = next;
388 }
389 return filt;
390 }
391 }
392}
393
394static void
Petr Machatab5f80ac2012-04-04 01:46:18 +0200395parse_filter_chain(const char *expr, struct filter **retp)
Petr Machata1e4fed22012-04-01 00:45:22 +0200396{
397 char *str = strdup(expr);
398 if (str == NULL) {
399 error(0, errno, "filter '%s' will be ignored", expr);
400 return;
401 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200402 *retp = recursive_parse_chain(str);
Petr Machata1e4fed22012-04-01 00:45:22 +0200403}
404
Juan Cespedesf1350522008-12-16 18:19:58 +0100405char **
406process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200407 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100408 options.output = stderr;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700409 options.no_plt = 0;
Joe Damato59e3fb12009-11-06 19:45:10 -0800410 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700411#if defined(HAVE_LIBUNWIND)
412 options.bt_depth = -1;
413#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100414
Juan Cespedesce377d52008-12-16 19:38:10 +0100415 guess_cols();
416
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200418 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200419 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200420 int option_index = 0;
421 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200423 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200424 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100425# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200427#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100428 {"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 Damatofa2aefc2010-10-30 19:56:50 -0700433 {"no-plt", 0, 0, 'g'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800434 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700435#if defined(HAVE_LIBUNWIND)
436 {"where", 1, 0, 'w'},
437#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200439 };
Joe Damato59e3fb12009-11-06 19:45:10 -0800440 c = getopt_long(argc, argv, "+cfhiLrStTVgb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100441# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100442 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200443# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700444#if defined(HAVE_LIBUNWIND)
445 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
446#else /* !defined(HAVE_LIBUNWIND) */
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200447 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700448#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100449 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100450 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200451 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100452 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100453 switch (c) {
454 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100455 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200457 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200458 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200459 break;
Joe Damato535e7382010-11-08 15:47:43 -0800460 case 'b':
461 options.no_signals = 1;
462 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200464 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100466#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100467 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100468 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100469 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200470#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200471 case 'D':
472 if (optarg[0]=='h') {
473 usage_debug();
474 exit(0);
475 }
476 options.debug = strtoul(optarg,&p,8);
477 if (*p) {
478 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
479 err_usage();
480 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100481 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200482
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 case 'e':
Petr Machatab5f80ac2012-04-04 01:46:18 +0200484 parse_filter_chain(optarg, &options.plt_filter);
Petr Machata1e4fed22012-04-01 00:45:22 +0200485 break;
486
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100487 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200488 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100489 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200490 case 'F':
491 {
492 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
493 if (!tmp) {
494 perror("ltrace: malloc");
495 exit(1);
496 }
497 tmp->filename = strdup(optarg);
498 tmp->next = opt_F;
499 opt_F = tmp;
500 break;
501 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700502 case 'g':
503 options.no_plt = 1;
504 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 case 'h':
506 usage();
507 exit(0);
508 case 'i':
509 opt_i++;
510 break;
511 case 'l':
Petr Machata2b46cfc2012-02-18 11:17:29 +0100512 assert(!"-l support not yet implemented");
513 abort();
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 break;
515 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100516 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100517 break;
518 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100519 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100520 break;
521 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100522 options.output = fopen(optarg, "w");
523 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100524 fprintf(stderr,
525 "Can't open %s for output: %s\n",
526 optarg, strerror(errno));
527 exit(1);
528 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100529 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
530 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100531 break;
532 case 'p':
533 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200534 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100535 if (!tmp) {
536 perror("ltrace: malloc");
537 exit(1);
538 }
539 tmp->pid = atoi(optarg);
540 tmp->next = opt_p;
541 opt_p = tmp;
542 break;
543 }
544 case 'r':
545 opt_r++;
546 break;
547 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200548 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100549 break;
550 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100551 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100552 break;
553 case 't':
554 opt_t++;
555 break;
556 case 'T':
557 opt_T++;
558 break;
559 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100560 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 break;
562 case 'V':
563 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200564 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200565 "This is free software; see the GNU General Public Licence\n"
566 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100567 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700568 break;
Joe Damato535e7382010-11-08 15:47:43 -0800569#if defined(HAVE_LIBUNWIND)
570 case 'w':
571 options.bt_depth = atoi(optarg);
Joe Damato59e3fb12009-11-06 19:45:10 -0800572 break;
Joe Damato535e7382010-11-08 15:47:43 -0800573#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100574 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200575#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200577#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200578 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200579 "architecture: assuming you meant \"-x\"\n");
580#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100581 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200582
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100583 case 'x':
584 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100585 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100586
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100587 /* First, check for duplicate. */
588 while (p && strcmp(p->name, optarg)) {
589 p = p->next;
590 }
591 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100592 break;
593 }
594
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100595 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100596 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100597 if (!p) {
598 perror("ltrace: malloc");
599 exit(1);
600 }
Joe Damato7b0a7de2010-11-08 15:47:34 -0800601 opt_x_cnt++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100602 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100603 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 p->next = opt_x;
Joe Damato7b0a7de2010-11-08 15:47:34 -0800605 p->hash = ~(0UL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 opt_x = p;
607 break;
608 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100609
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200611 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100612 }
613 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100614 argc -= optind;
615 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100616
Juan Cespedesaee09312007-08-31 18:49:48 +0200617 if (!opt_F) {
618 opt_F = malloc(sizeof(struct opt_F_t));
619 opt_F->next = malloc(sizeof(struct opt_F_t));
620 opt_F->next->next = NULL;
621 opt_F->filename = USER_CONFIG_FILE;
622 opt_F->next->filename = SYSTEM_CONFIG_FILE;
623 }
Steve Fink58c73a72006-07-17 23:18:35 +0200624 /* Reverse the config file list since it was built by
625 * prepending, and it would make more sense to process the
626 * files in the order they were given. Probably it would make
627 * more sense to keep a tail pointer instead? */
628 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200629 struct opt_F_t *egg = NULL;
630 struct opt_F_t *chicken;
631 while (opt_F) {
632 chicken = opt_F->next;
633 opt_F->next = egg;
634 egg = opt_F;
635 opt_F = chicken;
636 }
637 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200638 }
639
Petr Machata0b55b582012-04-02 00:38:46 +0200640 /* Set default filter. Use @MAIN for now, as that's what
641 * ltrace used to have in the past. XXX Maybe we should make
642 * this "*" instead. */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200643 if (options.plt_filter == NULL) {
644 parse_filter_chain("@MAIN", &options.plt_filter);
Petr Machata03673892012-04-03 13:51:09 +0200645 options.hide_caller = 1;
646 }
Petr Machatae0973cb2012-04-01 19:36:41 +0200647
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100648 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200649 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200650 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100651 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200652 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100653 fprintf(stderr, "%s: Incompatible options -r and -t\n",
654 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200655 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200656 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100657 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200658 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100659 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200660 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100661}