blob: d77fbd0746688298cb2f1cfc11f7e8cbebb8c1e4 [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;
211 status = (lib_re_p ? regcomp : globcomp)(&lib_re, sym, 0);
212 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);
223}
224
225static int
226parse_filter(struct filter *filt, char *expr)
227{
228 fprintf(stderr, "filter '%s'\n", expr);
229
230 /* Filter is a chain of sym@lib rules separated by '-'. If
231 * the filter expression starts with '-', the missing initial
232 * rule is implicitly *@*. */
233
234 enum filter_rule_type type = FR_ADD;
235
236 while (*expr != 0) {
237 size_t s = strcspn(expr, "@-");
238 char *symname = expr;
239 char *libname;
240 char *next = expr + s + 1;
241 enum filter_rule_type this_type = type;
242
243 if (expr[s] == 0) {
244 libname = "*";
245 expr = next - 1;
246
247 } else if (expr[s] == '-') {
248 libname = "*";
249 expr = next;
250 type = FR_SUBTRACT;
251
252 } else {
253 assert(expr[s] == '@');
254 expr[s] = 0;
255 s = strcspn(next, "-");
256 if (s == 0) {
257 libname = "*";
258 expr = next;
259 } else if (next[s] == 0) {
260 expr = next + s;
261 libname = next;
262
263 } else {
264 assert(next[s] == '-');
265 type = FR_SUBTRACT;
266 next[s] = 0;
267 expr = next + s + 1;
268 libname = next;
269 }
270 }
271
272 assert(*libname != 0);
273 char *symend = symname + strlen(symname) - 1;
274 char *libend = libname + strlen(libname) - 1;
275 int sym_is_re = 0;
276 int lib_is_re = 0;
277
278 /*
279 * /xxx/@... and ...@/xxx/ means that xxx are regular
280 * expressions. They are globs otherwise.
281 *
282 * /xxx@yyy/ is the same as /xxx/@/yyy/
283 *
284 * @/xxx matches library path name
285 * @.xxx matches library relative path name
286 */
287 if (symname[0] == '/') {
288 if (symname != symend && symend[0] == '/') {
289 ++symname;
290 *symend-- = 0;
291 sym_is_re = 1;
292
293 } else {
294 sym_is_re = 1;
295 lib_is_re = 1;
296 ++symname;
297
298 /* /XXX@YYY/ is the same as
299 * /XXX/@/YYY/. */
300 if (libend[0] != '/')
301 error(0, 0, "unmatched '/'"
302 " in symbol name");
303 else
304 *libend-- = 0;
305 }
306 }
307
308 /* If libname ends in '/', then we expect '/' in the
309 * beginning too. Otherwise the initial '/' is part
310 * of absolute file name. */
311 if (!lib_is_re && libend[0] == '/') {
312 lib_is_re = 1;
313 *libend-- = 0;
314 if (libname != libend && libname[0] == '/')
315 ++libname;
316 else
317 error(0, 0, "unmatched '/' in library name");
318 }
319
320 if (*symname == 0) /* /@AA/ */
321 symname = "*";
322 if (*libname == 0) /* /aa@/ */
323 libname = "*";
324
325 add_filter_rule(filt, expr, this_type,
326 symname, sym_is_re,
327 libname, lib_is_re);
328 }
329
330 return 0;
331}
332
333static struct filter *
334recursive_parse_chain(char *expr)
335{
336 /* Event expression grammar:
337 *
338 * Chain ::= Filter FilterList
339 * FilterList ::= eps | ',' Filter FilterList
340 * Filter ::= Rule RuleList
341 * RuleList ::= eps | '-' Rule RuleList
342 * Rule ::= eps | Glob Soname
343 * Soname ::= eps | '@' Glob | '@' '/' Filename
344 * Glob ::= '/' Regex '/'
345 */
346
347 struct filter *filt = malloc(sizeof(*filt));
348 if (filt == NULL) {
349 error(0, errno, "(part of) filter will be ignored: '%s'", expr);
350 return NULL;
351 }
352
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
384static void
385parse_chain(struct options_t *options, const char *expr)
386{
387 char *str = strdup(expr);
388 if (str == NULL) {
389 error(0, errno, "filter '%s' will be ignored", expr);
390 return;
391 }
392 options->filter = recursive_parse_chain(str);
393}
394
Juan Cespedesf1350522008-12-16 18:19:58 +0100395char **
396process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200397 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100398 options.output = stderr;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700399 options.no_plt = 0;
Joe Damato59e3fb12009-11-06 19:45:10 -0800400 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700401#if defined(HAVE_LIBUNWIND)
402 options.bt_depth = -1;
403#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100404
Juan Cespedesce377d52008-12-16 19:38:10 +0100405 guess_cols();
406
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100407 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200408 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200409 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200410 int option_index = 0;
411 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200413 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200414 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100415# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200417#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 {"indent", 1, 0, 'n'},
419 {"help", 0, 0, 'h'},
420 {"library", 1, 0, 'l'},
421 {"output", 1, 0, 'o'},
422 {"version", 0, 0, 'V'},
Joe Damatofa2aefc2010-10-30 19:56:50 -0700423 {"no-plt", 0, 0, 'g'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800424 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700425#if defined(HAVE_LIBUNWIND)
426 {"where", 1, 0, 'w'},
427#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100428 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200429 };
Joe Damato59e3fb12009-11-06 19:45:10 -0800430 c = getopt_long(argc, argv, "+cfhiLrStTVgb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100431# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200433# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700434#if defined(HAVE_LIBUNWIND)
435 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
436#else /* !defined(HAVE_LIBUNWIND) */
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200437 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700438#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100440 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200441 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100442 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 switch (c) {
444 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100445 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100446 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200447 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200448 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200449 break;
Joe Damato535e7382010-11-08 15:47:43 -0800450 case 'b':
451 options.no_signals = 1;
452 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100453 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200454 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100456#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100458 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200460#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200461 case 'D':
462 if (optarg[0]=='h') {
463 usage_debug();
464 exit(0);
465 }
466 options.debug = strtoul(optarg,&p,8);
467 if (*p) {
468 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
469 err_usage();
470 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100471 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200472
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100473 case 'e':
Petr Machata1e4fed22012-04-01 00:45:22 +0200474 parse_chain(&options, optarg);
475 break;
476
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100477 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200478 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100479 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200480 case 'F':
481 {
482 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
483 if (!tmp) {
484 perror("ltrace: malloc");
485 exit(1);
486 }
487 tmp->filename = strdup(optarg);
488 tmp->next = opt_F;
489 opt_F = tmp;
490 break;
491 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700492 case 'g':
493 options.no_plt = 1;
494 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100495 case 'h':
496 usage();
497 exit(0);
498 case 'i':
499 opt_i++;
500 break;
501 case 'l':
Petr Machata2b46cfc2012-02-18 11:17:29 +0100502 assert(!"-l support not yet implemented");
503 abort();
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100504 break;
505 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100506 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100507 break;
508 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100509 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 break;
511 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100512 options.output = fopen(optarg, "w");
513 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 fprintf(stderr,
515 "Can't open %s for output: %s\n",
516 optarg, strerror(errno));
517 exit(1);
518 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100519 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
520 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100521 break;
522 case 'p':
523 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200524 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100525 if (!tmp) {
526 perror("ltrace: malloc");
527 exit(1);
528 }
529 tmp->pid = atoi(optarg);
530 tmp->next = opt_p;
531 opt_p = tmp;
532 break;
533 }
534 case 'r':
535 opt_r++;
536 break;
537 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200538 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100539 break;
540 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100541 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100542 break;
543 case 't':
544 opt_t++;
545 break;
546 case 'T':
547 opt_T++;
548 break;
549 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100550 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 break;
552 case 'V':
553 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200554 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200555 "This is free software; see the GNU General Public Licence\n"
556 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100557 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700558 break;
Joe Damato535e7382010-11-08 15:47:43 -0800559#if defined(HAVE_LIBUNWIND)
560 case 'w':
561 options.bt_depth = atoi(optarg);
Joe Damato59e3fb12009-11-06 19:45:10 -0800562 break;
Joe Damato535e7382010-11-08 15:47:43 -0800563#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100564 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200565#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100566 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200567#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200568 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200569 "architecture: assuming you meant \"-x\"\n");
570#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100571 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200572
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100573 case 'x':
574 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100575 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100576
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100577 /* First, check for duplicate. */
578 while (p && strcmp(p->name, optarg)) {
579 p = p->next;
580 }
581 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100582 break;
583 }
584
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100585 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100586 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100587 if (!p) {
588 perror("ltrace: malloc");
589 exit(1);
590 }
Joe Damato7b0a7de2010-11-08 15:47:34 -0800591 opt_x_cnt++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100593 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100594 p->next = opt_x;
Joe Damato7b0a7de2010-11-08 15:47:34 -0800595 p->hash = ~(0UL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100596 opt_x = p;
597 break;
598 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100599
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100600 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200601 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100602 }
603 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 argc -= optind;
605 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100606
Juan Cespedesaee09312007-08-31 18:49:48 +0200607 if (!opt_F) {
608 opt_F = malloc(sizeof(struct opt_F_t));
609 opt_F->next = malloc(sizeof(struct opt_F_t));
610 opt_F->next->next = NULL;
611 opt_F->filename = USER_CONFIG_FILE;
612 opt_F->next->filename = SYSTEM_CONFIG_FILE;
613 }
Steve Fink58c73a72006-07-17 23:18:35 +0200614 /* Reverse the config file list since it was built by
615 * prepending, and it would make more sense to process the
616 * files in the order they were given. Probably it would make
617 * more sense to keep a tail pointer instead? */
618 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200619 struct opt_F_t *egg = NULL;
620 struct opt_F_t *chicken;
621 while (opt_F) {
622 chicken = opt_F->next;
623 opt_F->next = egg;
624 egg = opt_F;
625 opt_F = chicken;
626 }
627 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200628 }
629
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100630 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200631 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200632 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100633 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200634 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100635 fprintf(stderr, "%s: Incompatible options -r and -t\n",
636 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200637 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200638 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100639 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200640 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100641 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200642 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100643}