blob: 4f026ea4a9a871052a21f3fdfa67353981c51783 [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
210 regex_t lib_re;
Petr Machatae0973cb2012-04-01 19:36:41 +0200211 status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
Petr Machata1e4fed22012-04-01 00:45:22 +0200212 if (status != 0) {
213 char buf[100];
214 regerror(status, &lib_re, buf, sizeof buf);
215 error(0, 0, "rule near '%s' will be ignored: %s", expr, buf);
216
217 regfree(&symbol_re);
218 goto fail;
219 }
220
221 filter_lib_matcher_name_init(matcher, lib_re);
222 filter_rule_init(rule, type, matcher, symbol_re);
Petr Machatae0973cb2012-04-01 19:36:41 +0200223 filter_add_rule(filt, rule);
Petr Machata1e4fed22012-04-01 00:45:22 +0200224}
225
226static int
227parse_filter(struct filter *filt, char *expr)
228{
229 fprintf(stderr, "filter '%s'\n", expr);
230
231 /* Filter is a chain of sym@lib rules separated by '-'. If
232 * the filter expression starts with '-', the missing initial
233 * rule is implicitly *@*. */
234
235 enum filter_rule_type type = FR_ADD;
236
237 while (*expr != 0) {
238 size_t s = strcspn(expr, "@-");
239 char *symname = expr;
240 char *libname;
241 char *next = expr + s + 1;
242 enum filter_rule_type this_type = type;
243
244 if (expr[s] == 0) {
245 libname = "*";
246 expr = next - 1;
247
248 } else if (expr[s] == '-') {
249 libname = "*";
250 expr = next;
251 type = FR_SUBTRACT;
252
253 } else {
254 assert(expr[s] == '@');
255 expr[s] = 0;
256 s = strcspn(next, "-");
257 if (s == 0) {
258 libname = "*";
259 expr = next;
260 } else if (next[s] == 0) {
261 expr = next + s;
262 libname = next;
263
264 } else {
265 assert(next[s] == '-');
266 type = FR_SUBTRACT;
267 next[s] = 0;
268 expr = next + s + 1;
269 libname = next;
270 }
271 }
272
273 assert(*libname != 0);
274 char *symend = symname + strlen(symname) - 1;
275 char *libend = libname + strlen(libname) - 1;
276 int sym_is_re = 0;
277 int lib_is_re = 0;
278
279 /*
280 * /xxx/@... and ...@/xxx/ means that xxx are regular
281 * expressions. They are globs otherwise.
282 *
283 * /xxx@yyy/ is the same as /xxx/@/yyy/
284 *
285 * @/xxx matches library path name
286 * @.xxx matches library relative path name
287 */
288 if (symname[0] == '/') {
289 if (symname != symend && symend[0] == '/') {
290 ++symname;
291 *symend-- = 0;
292 sym_is_re = 1;
293
294 } else {
295 sym_is_re = 1;
296 lib_is_re = 1;
297 ++symname;
298
299 /* /XXX@YYY/ is the same as
300 * /XXX/@/YYY/. */
301 if (libend[0] != '/')
302 error(0, 0, "unmatched '/'"
303 " in symbol name");
304 else
305 *libend-- = 0;
306 }
307 }
308
309 /* If libname ends in '/', then we expect '/' in the
310 * beginning too. Otherwise the initial '/' is part
311 * of absolute file name. */
312 if (!lib_is_re && libend[0] == '/') {
313 lib_is_re = 1;
314 *libend-- = 0;
315 if (libname != libend && libname[0] == '/')
316 ++libname;
317 else
318 error(0, 0, "unmatched '/' in library name");
319 }
320
321 if (*symname == 0) /* /@AA/ */
322 symname = "*";
323 if (*libname == 0) /* /aa@/ */
324 libname = "*";
325
326 add_filter_rule(filt, expr, this_type,
327 symname, sym_is_re,
328 libname, lib_is_re);
329 }
330
331 return 0;
332}
333
334static struct filter *
335recursive_parse_chain(char *expr)
336{
337 /* Event expression grammar:
338 *
339 * Chain ::= Filter FilterList
340 * FilterList ::= eps | ',' Filter FilterList
341 * Filter ::= Rule RuleList
342 * RuleList ::= eps | '-' Rule RuleList
343 * Rule ::= eps | Glob Soname
344 * Soname ::= eps | '@' Glob | '@' '/' Filename
345 * Glob ::= '/' Regex '/'
346 */
347
348 struct filter *filt = malloc(sizeof(*filt));
349 if (filt == NULL) {
350 error(0, errno, "(part of) filter will be ignored: '%s'", expr);
351 return NULL;
352 }
353
354 struct filter *next = NULL;
355 char *it;
356 int escape = 0;
357 for (it = expr; ; ++it) {
358 if (*it == 0)
359 goto done;
360
361 if (escape) {
362 escape = 0;
363 continue;
364 }
365
366 if (*it == '\\') {
367 escape = 1;
368
369 } else if (*it == ',') {
370 *it = 0;
371 next = recursive_parse_chain(it + 1);
372 done:
373 filt->next = next;
374 if (parse_filter(filt, expr) < 0) {
375 fprintf(stderr,
376 "Filter '%s' will be ignored.\n", expr);
377 free(filt);
378 filt = next;
379 }
380 return filt;
381 }
382 }
383}
384
385static void
Petr Machatae0973cb2012-04-01 19:36:41 +0200386parse_filter_chain(struct options_t *options, const char *expr)
Petr Machata1e4fed22012-04-01 00:45:22 +0200387{
388 char *str = strdup(expr);
389 if (str == NULL) {
390 error(0, errno, "filter '%s' will be ignored", expr);
391 return;
392 }
393 options->filter = recursive_parse_chain(str);
394}
395
Juan Cespedesf1350522008-12-16 18:19:58 +0100396char **
397process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200398 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100399 options.output = stderr;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700400 options.no_plt = 0;
Joe Damato59e3fb12009-11-06 19:45:10 -0800401 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700402#if defined(HAVE_LIBUNWIND)
403 options.bt_depth = -1;
404#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100405
Juan Cespedesce377d52008-12-16 19:38:10 +0100406 guess_cols();
407
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200409 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200410 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200411 int option_index = 0;
412 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100413 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200414 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200415 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100416# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200418#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 {"indent", 1, 0, 'n'},
420 {"help", 0, 0, 'h'},
421 {"library", 1, 0, 'l'},
422 {"output", 1, 0, 'o'},
423 {"version", 0, 0, 'V'},
Joe Damatofa2aefc2010-10-30 19:56:50 -0700424 {"no-plt", 0, 0, 'g'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800425 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700426#if defined(HAVE_LIBUNWIND)
427 {"where", 1, 0, 'w'},
428#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100429 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200430 };
Joe Damato59e3fb12009-11-06 19:45:10 -0800431 c = getopt_long(argc, argv, "+cfhiLrStTVgb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100432# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100433 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200434# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700435#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 Cespedesc5c744a2009-07-23 18:22:58 +0200438 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700439#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100440 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200442 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100443 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100444 switch (c) {
445 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100446 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100447 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200448 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200449 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200450 break;
Joe Damato535e7382010-11-08 15:47:43 -0800451 case 'b':
452 options.no_signals = 1;
453 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200455 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100457#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100458 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100459 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100460 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200461#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200462 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 Wienand2d45b1a2006-02-20 22:48:07 +0100472 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200473
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100474 case 'e':
Petr Machatae0973cb2012-04-01 19:36:41 +0200475 parse_filter_chain(&options, optarg);
Petr Machata1e4fed22012-04-01 00:45:22 +0200476 break;
477
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100478 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200479 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100480 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200481 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 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700493 case 'g':
494 options.no_plt = 1;
495 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100496 case 'h':
497 usage();
498 exit(0);
499 case 'i':
500 opt_i++;
501 break;
502 case 'l':
Petr Machata2b46cfc2012-02-18 11:17:29 +0100503 assert(!"-l support not yet implemented");
504 abort();
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 break;
506 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100507 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100508 break;
509 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100510 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 break;
512 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100513 options.output = fopen(optarg, "w");
514 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100515 fprintf(stderr,
516 "Can't open %s for output: %s\n",
517 optarg, strerror(errno));
518 exit(1);
519 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100520 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
521 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 break;
523 case 'p':
524 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200525 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100526 if (!tmp) {
527 perror("ltrace: malloc");
528 exit(1);
529 }
530 tmp->pid = atoi(optarg);
531 tmp->next = opt_p;
532 opt_p = tmp;
533 break;
534 }
535 case 'r':
536 opt_r++;
537 break;
538 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200539 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100540 break;
541 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100542 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100543 break;
544 case 't':
545 opt_t++;
546 break;
547 case 'T':
548 opt_T++;
549 break;
550 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100551 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100552 break;
553 case 'V':
554 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200555 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200556 "This is free software; see the GNU General Public Licence\n"
557 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100558 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700559 break;
Joe Damato535e7382010-11-08 15:47:43 -0800560#if defined(HAVE_LIBUNWIND)
561 case 'w':
562 options.bt_depth = atoi(optarg);
Joe Damato59e3fb12009-11-06 19:45:10 -0800563 break;
Joe Damato535e7382010-11-08 15:47:43 -0800564#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100565 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200566#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100567 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200568#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200569 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200570 "architecture: assuming you meant \"-x\"\n");
571#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100572 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200573
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100574 case 'x':
575 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100576 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100577
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100578 /* First, check for duplicate. */
579 while (p && strcmp(p->name, optarg)) {
580 p = p->next;
581 }
582 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100583 break;
584 }
585
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100586 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100587 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 if (!p) {
589 perror("ltrace: malloc");
590 exit(1);
591 }
Joe Damato7b0a7de2010-11-08 15:47:34 -0800592 opt_x_cnt++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100593 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100594 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100595 p->next = opt_x;
Joe Damato7b0a7de2010-11-08 15:47:34 -0800596 p->hash = ~(0UL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100597 opt_x = p;
598 break;
599 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100600
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100601 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200602 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100603 }
604 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100605 argc -= optind;
606 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100607
Juan Cespedesaee09312007-08-31 18:49:48 +0200608 if (!opt_F) {
609 opt_F = malloc(sizeof(struct opt_F_t));
610 opt_F->next = malloc(sizeof(struct opt_F_t));
611 opt_F->next->next = NULL;
612 opt_F->filename = USER_CONFIG_FILE;
613 opt_F->next->filename = SYSTEM_CONFIG_FILE;
614 }
Steve Fink58c73a72006-07-17 23:18:35 +0200615 /* Reverse the config file list since it was built by
616 * prepending, and it would make more sense to process the
617 * files in the order they were given. Probably it would make
618 * more sense to keep a tail pointer instead? */
619 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200620 struct opt_F_t *egg = NULL;
621 struct opt_F_t *chicken;
622 while (opt_F) {
623 chicken = opt_F->next;
624 opt_F->next = egg;
625 egg = opt_F;
626 opt_F = chicken;
627 }
628 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200629 }
630
Petr Machatae0973cb2012-04-01 19:36:41 +0200631 if (options.filter == NULL)
632 parse_filter_chain(&options, "*");
633
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100634 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200635 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200636 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100637 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200638 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100639 fprintf(stderr, "%s: Incompatible options -r and -t\n",
640 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200641 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200642 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100643 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200644 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100645 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200646 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100647}