blob: 67a0128cc65a13dd0110751424d94db6727f3d01 [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 */
Juan Cespedesce377d52008-12-16 19:38:10 +010029#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020030 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010031#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020032 .indent = 0, /* indent output according to program flow */
33 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020034 .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 Cespedesce377d52008-12-16 19:38:10 +010039};
40
Juan Cespedesac3db291998-04-25 14:31:58 +020041static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010042int opt_i = 0; /* instruction pointer */
Juan Cespedesf666d191998-09-20 23:04:34 +020043int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020044int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010045int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010046
47/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010049
Steve Fink58c73a72006-07-17 23:18:35 +020050/* List of filenames give to option -F: */
51struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
52
Paul Gilliambe320772006-04-24 22:06:23 +020053#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010054/* Set a break on the routine named here in order to re-initialize breakpoints
55 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020056char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
57#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010058
Juan Cespedesf1350522008-12-16 18:19:58 +010059static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020060err_usage(void) {
Juan Cespedesc5c744a2009-07-23 18:22:58 +020061 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020062 exit(1);
63}
64
65static void
Juan Cespedesf1350522008-12-16 18:19:58 +010066usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020067 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +020070 " -A ARRAYLEN maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080071 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010073# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020075# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +020076 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020077 " -Dh, --debug=help show help on debugging.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020079 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020080 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Joe Damatofa2aefc2010-10-30 19:56:50 -070081 " -g, --no-plt disable breakpoints on PLT entries.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 " -i print instruction pointer at time of library call.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -l, --library=FILE print library calls from this library only.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 " -o, --output=FILE write the trace output to that file.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -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 Wienand2d45b1a2006-02-20 22:48:07 +010095 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -070096#if defined(HAVE_LIBUNWIND)
97 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n"
98#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200100#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200102#endif
103 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100105}
106
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200107static void
108usage_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 Cespedesf1350522008-12-16 18:19:58 +0100123static char *
124search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100125 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100126 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 Cespedesf1bfe202002-03-27 00:22:23 +0100139 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 Cespedes5e01f651998-03-08 22:31:44 +0100144 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 Cespedesce377d52008-12-16 19:38:10 +0100155static void
156guess_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 Cespedes43739a62009-04-07 13:10:08 +0200171 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
172 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100173 }
174}
175
Petr Machata1e4fed22012-04-01 00:45:22 +0200176static void
177add_filter_rule(struct filter *filt, const char *expr,
178 enum filter_rule_type type,
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200179 const char *a_sym, int sym_re_p,
180 const char *a_lib, int lib_re_p)
Petr Machata1e4fed22012-04-01 00:45:22 +0200181{
Petr Machata1e4fed22012-04-01 00:45:22 +0200182 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;
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200194 int status;
195 {
196 /* Add ^ to the start of expression and $ to the end, so that
197 * we match the whole symbol name. Let the user write the "*"
198 * explicitly if they wish. */
199 char sym[strlen(a_sym) + 3];
200 sprintf(sym, "^%s$", a_sym);
201 status = (sym_re_p ? regcomp : globcomp)(&symbol_re, sym, 0);
202 if (status != 0) {
203 char buf[100];
204 regerror(status, &symbol_re, buf, sizeof buf);
205 error(0, 0, "rule near '%s' will be ignored: %s",
206 expr, buf);
207 goto fail;
208 }
Petr Machata1e4fed22012-04-01 00:45:22 +0200209 }
210
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200211 if (strcmp(a_lib, "MAIN") == 0) {
Petr Machata0b55b582012-04-02 00:38:46 +0200212 filter_lib_matcher_main_init(matcher);
213 } else {
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200214 /* Add ^ and $ to the library expression as well. */
215 char lib[strlen(a_lib) + 3];
216 sprintf(lib, "^%s$", a_lib);
217
Petr Machata0b55b582012-04-02 00:38:46 +0200218 enum filter_lib_matcher_type type
219 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
Petr Machata1e4fed22012-04-01 00:45:22 +0200220
Petr Machata0b55b582012-04-02 00:38:46 +0200221 regex_t lib_re;
222 status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
223 if (status != 0) {
224 char buf[100];
225 regerror(status, &lib_re, buf, sizeof buf);
226 error(0, 0, "rule near '%s' will be ignored: %s",
227 expr, buf);
228
229 regfree(&symbol_re);
230 goto fail;
231 }
232 filter_lib_matcher_name_init(matcher, type, lib_re);
Petr Machata1e4fed22012-04-01 00:45:22 +0200233 }
234
Petr Machata1e4fed22012-04-01 00:45:22 +0200235 filter_rule_init(rule, type, matcher, symbol_re);
Petr Machatae0973cb2012-04-01 19:36:41 +0200236 filter_add_rule(filt, rule);
Petr Machata1e4fed22012-04-01 00:45:22 +0200237}
238
239static int
240parse_filter(struct filter *filt, char *expr)
241{
Petr Machata1e4fed22012-04-01 00:45:22 +0200242 /* Filter is a chain of sym@lib rules separated by '-'. If
243 * the filter expression starts with '-', the missing initial
244 * rule is implicitly *@*. */
245
246 enum filter_rule_type type = FR_ADD;
247
248 while (*expr != 0) {
Petr Machata050fa7f2012-04-23 23:44:36 +0200249 size_t s = strcspn(expr, "@-+");
Petr Machata1e4fed22012-04-01 00:45:22 +0200250 char *symname = expr;
251 char *libname;
252 char *next = expr + s + 1;
253 enum filter_rule_type this_type = type;
254
255 if (expr[s] == 0) {
256 libname = "*";
257 expr = next - 1;
258
Petr Machata050fa7f2012-04-23 23:44:36 +0200259 } else if (expr[s] == '-' || expr[s] == '+') {
260 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD;
261 expr[s] = 0;
Petr Machata1e4fed22012-04-01 00:45:22 +0200262 libname = "*";
263 expr = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200264
265 } else {
266 assert(expr[s] == '@');
267 expr[s] = 0;
Petr Machata050fa7f2012-04-23 23:44:36 +0200268 s = strcspn(next, "-+");
Petr Machata1e4fed22012-04-01 00:45:22 +0200269 if (s == 0) {
270 libname = "*";
271 expr = next;
272 } else if (next[s] == 0) {
273 expr = next + s;
274 libname = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200275 } else {
Petr Machata050fa7f2012-04-23 23:44:36 +0200276 assert(next[s] == '-' || next[s] == '+');
277 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD;
Petr Machata1e4fed22012-04-01 00:45:22 +0200278 next[s] = 0;
279 expr = next + s + 1;
280 libname = next;
281 }
282 }
283
284 assert(*libname != 0);
285 char *symend = symname + strlen(symname) - 1;
286 char *libend = libname + strlen(libname) - 1;
287 int sym_is_re = 0;
288 int lib_is_re = 0;
289
290 /*
291 * /xxx/@... and ...@/xxx/ means that xxx are regular
292 * expressions. They are globs otherwise.
293 *
294 * /xxx@yyy/ is the same as /xxx/@/yyy/
295 *
296 * @/xxx matches library path name
297 * @.xxx matches library relative path name
298 */
299 if (symname[0] == '/') {
300 if (symname != symend && symend[0] == '/') {
301 ++symname;
302 *symend-- = 0;
303 sym_is_re = 1;
304
305 } else {
306 sym_is_re = 1;
307 lib_is_re = 1;
308 ++symname;
309
310 /* /XXX@YYY/ is the same as
311 * /XXX/@/YYY/. */
312 if (libend[0] != '/')
313 error(0, 0, "unmatched '/'"
314 " in symbol name");
315 else
316 *libend-- = 0;
317 }
318 }
319
320 /* If libname ends in '/', then we expect '/' in the
321 * beginning too. Otherwise the initial '/' is part
322 * of absolute file name. */
323 if (!lib_is_re && libend[0] == '/') {
324 lib_is_re = 1;
325 *libend-- = 0;
326 if (libname != libend && libname[0] == '/')
327 ++libname;
328 else
329 error(0, 0, "unmatched '/' in library name");
330 }
331
332 if (*symname == 0) /* /@AA/ */
333 symname = "*";
334 if (*libname == 0) /* /aa@/ */
335 libname = "*";
336
337 add_filter_rule(filt, expr, this_type,
338 symname, sym_is_re,
339 libname, lib_is_re);
340 }
341
342 return 0;
343}
344
345static struct filter *
346recursive_parse_chain(char *expr)
347{
Petr Machata1e4fed22012-04-01 00:45:22 +0200348 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
Petr Machata35c88142012-04-04 00:54:43 +0200354 filter_init(filt);
Petr Machata050fa7f2012-04-23 23:44:36 +0200355 if (parse_filter(filt, expr) < 0) {
356 fprintf(stderr, "Filter '%s' will be ignored.\n", expr);
357 free(filt);
358 filt = NULL;
Petr Machata1e4fed22012-04-01 00:45:22 +0200359 }
Petr Machata050fa7f2012-04-23 23:44:36 +0200360
361 return filt;
Petr Machata1e4fed22012-04-01 00:45:22 +0200362}
363
364static void
Petr Machatab5f80ac2012-04-04 01:46:18 +0200365parse_filter_chain(const char *expr, struct filter **retp)
Petr Machata1e4fed22012-04-01 00:45:22 +0200366{
367 char *str = strdup(expr);
368 if (str == NULL) {
369 error(0, errno, "filter '%s' will be ignored", expr);
370 return;
371 }
Petr Machata391318f2012-04-19 02:28:13 +0200372 struct filter **tailp;
373 for (tailp = retp; *tailp != NULL; tailp = &(*tailp)->next)
374 ;
375 *tailp = recursive_parse_chain(str);
Petr Machata1e4fed22012-04-01 00:45:22 +0200376}
377
Juan Cespedesf1350522008-12-16 18:19:58 +0100378char **
Petr Machata67fa52f2012-04-05 02:11:39 +0200379process_options(int argc, char **argv)
380{
Juan Cespedesac3db291998-04-25 14:31:58 +0200381 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100382 options.output = stderr;
Joe Damato59e3fb12009-11-06 19:45:10 -0800383 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700384#if defined(HAVE_LIBUNWIND)
385 options.bt_depth = -1;
386#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100387
Juan Cespedesce377d52008-12-16 19:38:10 +0100388 guess_cols();
389
Petr Machata67fa52f2012-04-05 02:11:39 +0200390 int libcalls = 1;
391
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200393 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200394 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200395 int option_index = 0;
396 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100397 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200398 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200399 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100400# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200402#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100403 {"indent", 1, 0, 'n'},
404 {"help", 0, 0, 'h'},
405 {"library", 1, 0, 'l'},
406 {"output", 1, 0, 'o'},
407 {"version", 0, 0, 'V'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800408 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700409#if defined(HAVE_LIBUNWIND)
410 {"where", 1, 0, 'w'},
411#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200413 };
Petr Machata796267f2012-04-04 19:09:37 +0200414 c = getopt_long(argc, argv, "+cfhiLrStTVb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100415# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200417# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700418#if defined(HAVE_LIBUNWIND)
419 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
420#else /* !defined(HAVE_LIBUNWIND) */
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200421 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700422#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100424 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200425 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100426 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100427 switch (c) {
428 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100429 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100430 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200431 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200432 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200433 break;
Joe Damato535e7382010-11-08 15:47:43 -0800434 case 'b':
435 options.no_signals = 1;
436 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200438 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100440#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100442 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200444#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200445 case 'D':
446 if (optarg[0]=='h') {
447 usage_debug();
448 exit(0);
449 }
450 options.debug = strtoul(optarg,&p,8);
451 if (*p) {
452 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
453 err_usage();
454 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200456
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 case 'e':
Petr Machatab5f80ac2012-04-04 01:46:18 +0200458 parse_filter_chain(optarg, &options.plt_filter);
Petr Machata1e4fed22012-04-01 00:45:22 +0200459 break;
460
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100461 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200462 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200464 case 'F':
465 {
466 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
467 if (!tmp) {
468 perror("ltrace: malloc");
469 exit(1);
470 }
471 tmp->filename = strdup(optarg);
472 tmp->next = opt_F;
473 opt_F = tmp;
474 break;
475 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100476 case 'h':
477 usage();
478 exit(0);
479 case 'i':
480 opt_i++;
481 break;
482 case 'l':
Petr Machataa16a0c22012-04-17 01:19:25 +0200483 // XXX TODO
484 fprintf(stderr, "-l support not yet implemented\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100485 break;
486 case 'L':
Petr Machata67fa52f2012-04-05 02:11:39 +0200487 libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100488 break;
489 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100490 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100491 break;
492 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100493 options.output = fopen(optarg, "w");
494 if (!options.output) {
Petr Machata51268152012-04-17 00:27:18 +0200495 error(0, errno,
496 "can't open %s for writing", optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100497 exit(1);
498 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100499 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
500 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 break;
502 case 'p':
503 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200504 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 if (!tmp) {
506 perror("ltrace: malloc");
507 exit(1);
508 }
509 tmp->pid = atoi(optarg);
510 tmp->next = opt_p;
511 opt_p = tmp;
512 break;
513 }
514 case 'r':
515 opt_r++;
516 break;
517 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200518 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100519 break;
520 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100521 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 break;
523 case 't':
524 opt_t++;
525 break;
526 case 'T':
527 opt_T++;
528 break;
529 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100530 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100531 break;
532 case 'V':
533 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200534 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200535 "This is free software; see the GNU General Public Licence\n"
536 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100537 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700538 break;
Joe Damato535e7382010-11-08 15:47:43 -0800539#if defined(HAVE_LIBUNWIND)
540 case 'w':
541 options.bt_depth = atoi(optarg);
Joe Damato59e3fb12009-11-06 19:45:10 -0800542 break;
Joe Damato535e7382010-11-08 15:47:43 -0800543#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100544 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200545#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100546 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200547#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200548 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200549 "architecture: assuming you meant \"-x\"\n");
550#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200552
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100553 case 'x':
Petr Machatada3edbf2012-04-04 02:20:21 +0200554 parse_filter_chain(optarg, &options.static_filter);
555 break;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100556
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100557 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200558 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100559 }
560 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 argc -= optind;
562 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100563
Juan Cespedesaee09312007-08-31 18:49:48 +0200564 if (!opt_F) {
565 opt_F = malloc(sizeof(struct opt_F_t));
566 opt_F->next = malloc(sizeof(struct opt_F_t));
567 opt_F->next->next = NULL;
568 opt_F->filename = USER_CONFIG_FILE;
569 opt_F->next->filename = SYSTEM_CONFIG_FILE;
570 }
Steve Fink58c73a72006-07-17 23:18:35 +0200571 /* Reverse the config file list since it was built by
572 * prepending, and it would make more sense to process the
573 * files in the order they were given. Probably it would make
574 * more sense to keep a tail pointer instead? */
575 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200576 struct opt_F_t *egg = NULL;
577 struct opt_F_t *chicken;
578 while (opt_F) {
579 chicken = opt_F->next;
580 opt_F->next = egg;
581 egg = opt_F;
582 opt_F = chicken;
583 }
584 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200585 }
586
Petr Machata0b55b582012-04-02 00:38:46 +0200587 /* Set default filter. Use @MAIN for now, as that's what
588 * ltrace used to have in the past. XXX Maybe we should make
589 * this "*" instead. */
Petr Machata67fa52f2012-04-05 02:11:39 +0200590 if (options.plt_filter == NULL && libcalls) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200591 parse_filter_chain("@MAIN", &options.plt_filter);
Petr Machata03673892012-04-03 13:51:09 +0200592 options.hide_caller = 1;
593 }
Petr Machatae0973cb2012-04-01 19:36:41 +0200594
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100595 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200596 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200597 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100598 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200599 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100600 fprintf(stderr, "%s: Incompatible options -r and -t\n",
601 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200602 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200603 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200605 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100606 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200607 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100608}