blob: 8b73233a4736e9f6e7707cc578836a8ed27d5df5 [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>
Petr Machata1e4fed22012-04-01 00:45:22 +02006#include <fcntl.h>
7#include <getopt.h>
8#include <limits.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +02009#include <stdio.h>
Petr Machata1e4fed22012-04-01 00:45:22 +020010#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
Juan Cespedesf1350522008-12-16 18:19:58 +010053static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020054err_usage(void) {
Petr Machata20a411d2012-09-25 22:45:26 +020055 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020056 exit(1);
57}
58
59static void
Juan Cespedesf1350522008-12-16 18:19:58 +010060usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020061 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +020064 " -A ARRAYLEN maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080065 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010067# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020069# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +020070 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020071 " -Dh, --debug=help show help on debugging.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020073 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020074 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 " -i print instruction pointer at time of library call.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 " -l, --library=FILE print library calls from this library only.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 " -o, --output=FILE write the trace output to that file.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 " -p PID attach to the process with the process ID pid.\n"
82 " -r print relative timestamps.\n"
83 " -s STRLEN specify the maximum string size to print.\n"
84 " -S display system calls.\n"
85 " -t, -tt, -ttt print absolute timestamps.\n"
86 " -T show the time spent inside each call.\n"
87 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -070089#if defined(HAVE_LIBUNWIND)
90 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n"
91#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +020093 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +010095}
96
Juan Cespedesc5c744a2009-07-23 18:22:58 +020097static void
98usage_debug(void) {
99 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
100 fprintf(stdout,
101 "\n"
102 " number ref. in source description\n"
103 " 1 general Generally helpful progress information\n"
104 " 10 event Shows every event received by a traced process\n"
105 " 20 process Shows actions carried upon a traced processes\n"
106 " 40 function Shows every entry to internal functions\n"
107 "\n"
108 "Debugging options are mixed using bitwise-or.\n"
109 "Note that the meanings and values are subject to change.\n"
110 );
111}
112
Juan Cespedesf1350522008-12-16 18:19:58 +0100113static char *
114search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100115 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100116 char *path;
117 int m, n;
118
119 if (strchr(filename, '/')) {
120 return filename;
121 }
122 for (path = getenv("PATH"); path && *path; path += m) {
123 if (strchr(path, ':')) {
124 n = strchr(path, ':') - path;
125 m = n + 1;
126 } else {
127 m = n = strlen(path);
128 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100129 if (n + strlen(filename) + 1 >= PATH_MAX) {
Petr Machata20a411d2012-09-25 22:45:26 +0200130 fprintf(stderr, "Error: filename too long.\n");
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100131 exit(1);
132 }
133 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 if (n && pathname[n - 1] != '/') {
135 pathname[n++] = '/';
136 }
137 strcpy(pathname + n, filename);
138 if (!access(pathname, X_OK)) {
139 return pathname;
140 }
141 }
142 return filename;
143}
144
Juan Cespedesce377d52008-12-16 19:38:10 +0100145static void
146guess_cols(void) {
147 struct winsize ws;
148 char *c;
149
150 options.align = DEFAULT_ALIGN;
151 c = getenv("COLUMNS");
152 if (c && *c) {
153 char *endptr;
154 int cols;
155 cols = strtol(c, &endptr, 0);
156 if (cols > 0 && !*endptr) {
157 options.align = cols * 5 / 8;
158 }
159 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
160 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200161 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
162 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100163 }
164}
165
Petr Machata02b96072012-09-25 23:10:14 +0200166static int
167compile_libname(const char *expr, const char *a_lib, int lib_re_p,
168 struct filter_lib_matcher *matcher)
169{
170 if (strcmp(a_lib, "MAIN") == 0) {
171 filter_lib_matcher_main_init(matcher);
172 } else {
173 /* Add ^ and $ to the library expression as well. */
174 char lib[strlen(a_lib) + 3];
175 sprintf(lib, "^%s$", a_lib);
176
177 enum filter_lib_matcher_type type
178 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
179
180 regex_t lib_re;
181 int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
182 if (status != 0) {
183 char buf[100];
184 regerror(status, &lib_re, buf, sizeof buf);
185 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
186 expr, buf);
187 return -1;
188 }
189 filter_lib_matcher_name_init(matcher, type, lib_re);
190 }
191 return 0;
192}
193
Petr Machata1e4fed22012-04-01 00:45:22 +0200194static void
195add_filter_rule(struct filter *filt, const char *expr,
196 enum filter_rule_type type,
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200197 const char *a_sym, int sym_re_p,
198 const char *a_lib, int lib_re_p)
Petr Machata1e4fed22012-04-01 00:45:22 +0200199{
Petr Machata1e4fed22012-04-01 00:45:22 +0200200 struct filter_rule *rule = malloc(sizeof(*rule));
201 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher));
202
203 if (rule == NULL || matcher == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200204 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200205 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200206 fail:
207 free(rule);
208 free(matcher);
209 return;
210 }
211
212 regex_t symbol_re;
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200213 {
214 /* Add ^ to the start of expression and $ to the end, so that
215 * we match the whole symbol name. Let the user write the "*"
216 * explicitly if they wish. */
217 char sym[strlen(a_sym) + 3];
218 sprintf(sym, "^%s$", a_sym);
Petr Machata02b96072012-09-25 23:10:14 +0200219 int status = (sym_re_p ? regcomp : globcomp)
220 (&symbol_re, sym, 0);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200221 if (status != 0) {
222 char buf[100];
223 regerror(status, &symbol_re, buf, sizeof buf);
Petr Machata20a411d2012-09-25 22:45:26 +0200224 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200225 expr, buf);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200226 goto fail;
227 }
Petr Machata1e4fed22012-04-01 00:45:22 +0200228 }
229
Petr Machata02b96072012-09-25 23:10:14 +0200230 if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) {
231 regfree(&symbol_re);
232 goto fail;
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
Petr Machata02b96072012-09-25 23:10:14 +0200240grok_libname_pattern(char **libnamep, char **libendp)
241{
242 char *libname = *libnamep;
243 char *libend = *libendp;
244
245 if (libend[0] != '/')
246 return 0;
247
248 *libend-- = 0;
249 if (libname != libend && libname[0] == '/')
250 ++libname;
251 else
252 fprintf(stderr, "Unmatched '/' in library name.\n");
253
254 *libendp = libend;
255 *libnamep = libname;
256 return 1;
257}
258
259static int
Petr Machata1e4fed22012-04-01 00:45:22 +0200260parse_filter(struct filter *filt, char *expr)
261{
Petr Machata02b96072012-09-25 23:10:14 +0200262 /* Filter is a chain of sym@lib rules separated by '-' or '+'.
263 * If the filter expression starts with '-', the missing
264 * initial rule is implicitly *@*. */
Petr Machata1e4fed22012-04-01 00:45:22 +0200265
266 enum filter_rule_type type = FR_ADD;
267
268 while (*expr != 0) {
Petr Machata050fa7f2012-04-23 23:44:36 +0200269 size_t s = strcspn(expr, "@-+");
Petr Machata1e4fed22012-04-01 00:45:22 +0200270 char *symname = expr;
271 char *libname;
272 char *next = expr + s + 1;
273 enum filter_rule_type this_type = type;
274
275 if (expr[s] == 0) {
276 libname = "*";
277 expr = next - 1;
278
Petr Machata050fa7f2012-04-23 23:44:36 +0200279 } else if (expr[s] == '-' || expr[s] == '+') {
280 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD;
281 expr[s] = 0;
Petr Machata1e4fed22012-04-01 00:45:22 +0200282 libname = "*";
283 expr = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200284
285 } else {
286 assert(expr[s] == '@');
287 expr[s] = 0;
Petr Machata050fa7f2012-04-23 23:44:36 +0200288 s = strcspn(next, "-+");
Petr Machata1e4fed22012-04-01 00:45:22 +0200289 if (s == 0) {
290 libname = "*";
291 expr = next;
292 } else if (next[s] == 0) {
293 expr = next + s;
294 libname = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200295 } else {
Petr Machata050fa7f2012-04-23 23:44:36 +0200296 assert(next[s] == '-' || next[s] == '+');
297 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD;
Petr Machata1e4fed22012-04-01 00:45:22 +0200298 next[s] = 0;
299 expr = next + s + 1;
300 libname = next;
301 }
302 }
303
304 assert(*libname != 0);
305 char *symend = symname + strlen(symname) - 1;
306 char *libend = libname + strlen(libname) - 1;
307 int sym_is_re = 0;
308 int lib_is_re = 0;
309
310 /*
311 * /xxx/@... and ...@/xxx/ means that xxx are regular
312 * expressions. They are globs otherwise.
313 *
314 * /xxx@yyy/ is the same as /xxx/@/yyy/
315 *
316 * @/xxx matches library path name
317 * @.xxx matches library relative path name
318 */
319 if (symname[0] == '/') {
320 if (symname != symend && symend[0] == '/') {
321 ++symname;
322 *symend-- = 0;
323 sym_is_re = 1;
324
325 } else {
326 sym_is_re = 1;
327 lib_is_re = 1;
328 ++symname;
329
330 /* /XXX@YYY/ is the same as
331 * /XXX/@/YYY/. */
332 if (libend[0] != '/')
Petr Machata20a411d2012-09-25 22:45:26 +0200333 fprintf(stderr, "Unmatched '/'"
334 " in symbol name.\n");
Petr Machata1e4fed22012-04-01 00:45:22 +0200335 else
336 *libend-- = 0;
337 }
338 }
339
340 /* If libname ends in '/', then we expect '/' in the
341 * beginning too. Otherwise the initial '/' is part
342 * of absolute file name. */
Petr Machata02b96072012-09-25 23:10:14 +0200343 if (!lib_is_re)
344 lib_is_re = grok_libname_pattern(&libname, &libend);
Petr Machata1e4fed22012-04-01 00:45:22 +0200345
346 if (*symname == 0) /* /@AA/ */
347 symname = "*";
348 if (*libname == 0) /* /aa@/ */
349 libname = "*";
350
351 add_filter_rule(filt, expr, this_type,
352 symname, sym_is_re,
353 libname, lib_is_re);
354 }
355
356 return 0;
357}
358
359static struct filter *
360recursive_parse_chain(char *expr)
361{
Petr Machata1e4fed22012-04-01 00:45:22 +0200362 struct filter *filt = malloc(sizeof(*filt));
363 if (filt == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200364 fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200365 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200366 return NULL;
367 }
368
Petr Machata35c88142012-04-04 00:54:43 +0200369 filter_init(filt);
Petr Machata050fa7f2012-04-23 23:44:36 +0200370 if (parse_filter(filt, expr) < 0) {
371 fprintf(stderr, "Filter '%s' will be ignored.\n", expr);
372 free(filt);
373 filt = NULL;
Petr Machata1e4fed22012-04-01 00:45:22 +0200374 }
Petr Machata050fa7f2012-04-23 23:44:36 +0200375
376 return filt;
Petr Machata1e4fed22012-04-01 00:45:22 +0200377}
378
379static void
Petr Machatab5f80ac2012-04-04 01:46:18 +0200380parse_filter_chain(const char *expr, struct filter **retp)
Petr Machata1e4fed22012-04-01 00:45:22 +0200381{
382 char *str = strdup(expr);
383 if (str == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200384 fprintf(stderr, "Filter '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200385 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200386 return;
387 }
Petr Machata25f319e2012-04-23 23:45:21 +0200388 /* Support initial '!' for backward compatibility. */
389 if (str[0] == '!')
390 str[0] = '-';
391
Petr Machata391318f2012-04-19 02:28:13 +0200392 struct filter **tailp;
393 for (tailp = retp; *tailp != NULL; tailp = &(*tailp)->next)
394 ;
395 *tailp = recursive_parse_chain(str);
Petr Machata1e4fed22012-04-01 00:45:22 +0200396}
397
Juan Cespedesf1350522008-12-16 18:19:58 +0100398char **
Petr Machata67fa52f2012-04-05 02:11:39 +0200399process_options(int argc, char **argv)
400{
Juan Cespedesac3db291998-04-25 14:31:58 +0200401 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100402 options.output = stderr;
Joe Damato59e3fb12009-11-06 19:45:10 -0800403 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700404#if defined(HAVE_LIBUNWIND)
405 options.bt_depth = -1;
406#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100407
Juan Cespedesce377d52008-12-16 19:38:10 +0100408 guess_cols();
409
Petr Machata67fa52f2012-04-05 02:11:39 +0200410 int libcalls = 1;
411
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200413 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200414 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200415 int option_index = 0;
416 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200418 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200419 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100420# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100421 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200422#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 {"indent", 1, 0, 'n'},
424 {"help", 0, 0, 'h'},
425 {"library", 1, 0, 'l'},
426 {"output", 1, 0, 'o'},
427 {"version", 0, 0, 'V'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800428 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700429#if defined(HAVE_LIBUNWIND)
430 {"where", 1, 0, 'w'},
431#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200433 };
Petr Machata796267f2012-04-04 19:09:37 +0200434 c = getopt_long(argc, argv, "+cfhiLrStTVb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100435# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200437# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700438#if defined(HAVE_LIBUNWIND)
439 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
440#else /* !defined(HAVE_LIBUNWIND) */
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200441 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700442#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100444 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200445 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100446 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100447 switch (c) {
448 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100449 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100450 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200451 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200452 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200453 break;
Joe Damato535e7382010-11-08 15:47:43 -0800454 case 'b':
455 options.no_signals = 1;
456 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200458 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100460#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100461 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100462 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200464#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200465 case 'D':
466 if (optarg[0]=='h') {
467 usage_debug();
468 exit(0);
469 }
470 options.debug = strtoul(optarg,&p,8);
471 if (*p) {
472 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
473 err_usage();
474 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100475 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200476
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100477 case 'e':
Petr Machatab5f80ac2012-04-04 01:46:18 +0200478 parse_filter_chain(optarg, &options.plt_filter);
Petr Machata1e4fed22012-04-01 00:45:22 +0200479 break;
480
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100481 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200482 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200484 case 'F':
485 {
486 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
487 if (!tmp) {
488 perror("ltrace: malloc");
489 exit(1);
490 }
491 tmp->filename = strdup(optarg);
492 tmp->next = opt_F;
493 opt_F = tmp;
494 break;
495 }
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 Machataa16a0c22012-04-17 01:19:25 +0200503 // XXX TODO
504 fprintf(stderr, "-l support not yet implemented\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 break;
506 case 'L':
Petr Machata67fa52f2012-04-05 02:11:39 +0200507 libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100508 break;
Petr Machata5bddfcc2012-05-03 18:40:20 +0200509 case 'n': {
510 char *endptr;
511 long int l = strtol(optarg, &endptr, 0);
512 /* Arbitrary cut-off. Nobody needs to indent
513 * more than, say, 8, anyway. */
514 if (l < 0 || l > 20 || *optarg == 0 || *endptr != 0) {
515 fprintf(stderr, "Invalid argument to -n: '%s'."
516 " Use integer 0..20.\n", optarg);
517 exit(1);
518 }
519 options.indent = (int)l;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100520 break;
Petr Machata5bddfcc2012-05-03 18:40:20 +0200521 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100523 options.output = fopen(optarg, "w");
524 if (!options.output) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200525 fprintf(stderr,
526 "can't open %s for writing: %s\n",
527 optarg, strerror(errno));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100528 exit(1);
529 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100530 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
531 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 break;
533 case 'p':
534 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200535 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100536 if (!tmp) {
537 perror("ltrace: malloc");
538 exit(1);
539 }
540 tmp->pid = atoi(optarg);
541 tmp->next = opt_p;
542 opt_p = tmp;
543 break;
544 }
545 case 'r':
546 opt_r++;
547 break;
548 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200549 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100550 break;
551 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100552 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100553 break;
554 case 't':
555 opt_t++;
556 break;
557 case 'T':
558 opt_T++;
559 break;
560 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100561 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100562 break;
563 case 'V':
564 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200565 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200566 "This is free software; see the GNU General Public Licence\n"
567 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100568 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700569 break;
Joe Damato535e7382010-11-08 15:47:43 -0800570#if defined(HAVE_LIBUNWIND)
571 case 'w':
572 options.bt_depth = atoi(optarg);
Joe Damato59e3fb12009-11-06 19:45:10 -0800573 break;
Joe Damato535e7382010-11-08 15:47:43 -0800574#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedesac3db291998-04-25 14:31:58 +0200575
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576 case 'x':
Petr Machatada3edbf2012-04-04 02:20:21 +0200577 parse_filter_chain(optarg, &options.static_filter);
578 break;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100579
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100580 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200581 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100582 }
583 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100584 argc -= optind;
585 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100586
Juan Cespedesaee09312007-08-31 18:49:48 +0200587 if (!opt_F) {
588 opt_F = malloc(sizeof(struct opt_F_t));
589 opt_F->next = malloc(sizeof(struct opt_F_t));
590 opt_F->next->next = NULL;
591 opt_F->filename = USER_CONFIG_FILE;
592 opt_F->next->filename = SYSTEM_CONFIG_FILE;
593 }
Steve Fink58c73a72006-07-17 23:18:35 +0200594 /* Reverse the config file list since it was built by
595 * prepending, and it would make more sense to process the
596 * files in the order they were given. Probably it would make
597 * more sense to keep a tail pointer instead? */
598 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200599 struct opt_F_t *egg = NULL;
600 struct opt_F_t *chicken;
601 while (opt_F) {
602 chicken = opt_F->next;
603 opt_F->next = egg;
604 egg = opt_F;
605 opt_F = chicken;
606 }
607 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200608 }
609
Petr Machata0b55b582012-04-02 00:38:46 +0200610 /* Set default filter. Use @MAIN for now, as that's what
611 * ltrace used to have in the past. XXX Maybe we should make
612 * this "*" instead. */
Petr Machata67fa52f2012-04-05 02:11:39 +0200613 if (options.plt_filter == NULL && libcalls) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200614 parse_filter_chain("@MAIN", &options.plt_filter);
Petr Machata03673892012-04-03 13:51:09 +0200615 options.hide_caller = 1;
616 }
Petr Machatae0973cb2012-04-01 19:36:41 +0200617
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100618 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200619 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200620 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100621 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200622 if (opt_r && opt_t) {
Petr Machata20a411d2012-09-25 22:45:26 +0200623 fprintf(stderr,
624 "%s: Options -r and -t can't be used together\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100625 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200626 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200627 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100628 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200629 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100630 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200631 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100632}