blob: 93dea31c3de05c702e5752183f1f9defc05a4fda [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
2 * This file is part of ltrace.
Petr Machata4b3bda42013-01-11 23:32:17 +01003 * Copyright (C) 2012, 2013 Petr Machata, Red Hat Inc.
Petr Machatae99af272012-10-26 00:29:52 +02004 * Copyright (C) 2009,2010 Joe Damato
5 * Copyright (C) 1998,1999,2002,2003,2004,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 * Copyright (C) 2006 Steve Fink
8 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
Juan Cespedesac3db291998-04-25 14:31:58 +020026#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020027
Juan Cespedesce377d52008-12-16 19:38:10 +010028#include <sys/ioctl.h>
Petr Machata2b46cfc2012-02-18 11:17:29 +010029#include <assert.h>
Petr Machata1e4fed22012-04-01 00:45:22 +020030#include <errno.h>
Petr Machata1e4fed22012-04-01 00:45:22 +020031#include <fcntl.h>
32#include <getopt.h>
33#include <limits.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020034#include <stdio.h>
Petr Machata1e4fed22012-04-01 00:45:22 +020035#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
Juan Cespedesac3db291998-04-25 14:31:58 +020038
Juan Cespedesf7281232009-06-25 16:11:21 +020039#include "common.h"
Petr Machata1e4fed22012-04-01 00:45:22 +020040#include "filter.h"
41#include "glob.h"
Petr Machata3ac8db62012-11-23 18:43:10 +010042#include "demangle.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010043
Steve Fink58c73a72006-07-17 23:18:35 +020044#ifndef SYSCONFDIR
45#define SYSCONFDIR "/etc"
46#endif
47
Olaf Heringe948e582006-10-12 23:53:44 +020048#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020049#define USER_CONFIG_FILE "~/.ltrace.conf"
50
Juan Cespedesce377d52008-12-16 19:38:10 +010051struct options_t options = {
Juan Cespedesda9b9532009-04-07 15:33:50 +020052 .align = DEFAULT_ALIGN, /* alignment column for results */
53 .user = NULL, /* username to run command as */
54 .syscalls = 0, /* display syscalls */
Juan Cespedesce377d52008-12-16 19:38:10 +010055#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020056 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010057#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020058 .indent = 0, /* indent output according to program flow */
59 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020060 .summary = 0, /* Report a summary on program exit */
61 .debug = 0, /* debug */
62 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
63 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
64 .follow = 0, /* trace child processes */
Juan Cespedesce377d52008-12-16 19:38:10 +010065};
66
Juan Cespedesac3db291998-04-25 14:31:58 +020067static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010068int opt_i = 0; /* instruction pointer */
Juan Cespedesf666d191998-09-20 23:04:34 +020069int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020070int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010071int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010072
73/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010075
Steve Fink58c73a72006-07-17 23:18:35 +020076/* List of filenames give to option -F: */
77struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
78
Juan Cespedesf1350522008-12-16 18:19:58 +010079static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020080err_usage(void) {
Petr Machata20a411d2012-09-25 22:45:26 +020081 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020082 exit(1);
83}
84
85static void
Juan Cespedesf1350522008-12-16 18:19:58 +010086usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020087 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -a, --align=COLUMN align return values in a secific column.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +010090 " -A MAXELTS maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080091 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010093# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020095# endif
Petr Machata4b3bda42013-01-11 23:32:17 +010096 " -D, --debug=MASK enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020097 " -Dh, --debug=help show help on debugging.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +010098 " -e FILTER modify which library calls to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020099 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +0200100 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 " -i print instruction pointer at time of library call.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100103 " -l, --library=LIBRARY_PATTERN only trace symbols implemented by this library.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100106 " -o, --output=FILENAME write the trace output to file with given name.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 " -p PID attach to the process with the process ID pid.\n"
108 " -r print relative timestamps.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100109 " -s STRSIZE specify the maximum string size to print.\n"
110 " -S trace system calls as well as library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 " -t, -tt, -ttt print absolute timestamps.\n"
112 " -T show the time spent inside each call.\n"
113 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700115#if defined(HAVE_LIBUNWIND)
Petr Machata68818a82012-11-30 21:42:59 +0100116 " -w, --where=NR print backtrace showing NR stack frames at most.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700117#endif /* defined(HAVE_LIBUNWIND) */
Petr Machata4b3bda42013-01-11 23:32:17 +0100118 " -x FILTER modify which static functions to trace.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200119 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100121}
122
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200123static void
124usage_debug(void) {
125 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
126 fprintf(stdout,
127 "\n"
128 " number ref. in source description\n"
129 " 1 general Generally helpful progress information\n"
130 " 10 event Shows every event received by a traced process\n"
131 " 20 process Shows actions carried upon a traced processes\n"
132 " 40 function Shows every entry to internal functions\n"
133 "\n"
134 "Debugging options are mixed using bitwise-or.\n"
135 "Note that the meanings and values are subject to change.\n"
136 );
137}
138
Juan Cespedesf1350522008-12-16 18:19:58 +0100139static char *
140search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100141 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100142 char *path;
143 int m, n;
144
145 if (strchr(filename, '/')) {
146 return filename;
147 }
148 for (path = getenv("PATH"); path && *path; path += m) {
149 if (strchr(path, ':')) {
150 n = strchr(path, ':') - path;
151 m = n + 1;
152 } else {
153 m = n = strlen(path);
154 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100155 if (n + strlen(filename) + 1 >= PATH_MAX) {
Petr Machata20a411d2012-09-25 22:45:26 +0200156 fprintf(stderr, "Error: filename too long.\n");
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100157 exit(1);
158 }
159 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100160 if (n && pathname[n - 1] != '/') {
161 pathname[n++] = '/';
162 }
163 strcpy(pathname + n, filename);
164 if (!access(pathname, X_OK)) {
165 return pathname;
166 }
167 }
168 return filename;
169}
170
Juan Cespedesce377d52008-12-16 19:38:10 +0100171static void
172guess_cols(void) {
173 struct winsize ws;
174 char *c;
175
176 options.align = DEFAULT_ALIGN;
177 c = getenv("COLUMNS");
178 if (c && *c) {
179 char *endptr;
180 int cols;
181 cols = strtol(c, &endptr, 0);
182 if (cols > 0 && !*endptr) {
183 options.align = cols * 5 / 8;
184 }
185 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
186 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200187 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
188 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100189 }
190}
191
Petr Machata02b96072012-09-25 23:10:14 +0200192static int
193compile_libname(const char *expr, const char *a_lib, int lib_re_p,
194 struct filter_lib_matcher *matcher)
195{
196 if (strcmp(a_lib, "MAIN") == 0) {
197 filter_lib_matcher_main_init(matcher);
198 } else {
199 /* Add ^ and $ to the library expression as well. */
200 char lib[strlen(a_lib) + 3];
201 sprintf(lib, "^%s$", a_lib);
202
203 enum filter_lib_matcher_type type
204 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
205
206 regex_t lib_re;
207 int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
Petr Machatab6c5c8c2012-12-08 03:23:39 +0100208 if (status != 0) {
Petr Machata02b96072012-09-25 23:10:14 +0200209 char buf[100];
210 regerror(status, &lib_re, buf, sizeof buf);
211 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
212 expr, buf);
213 return -1;
214 }
215 filter_lib_matcher_name_init(matcher, type, lib_re);
216 }
217 return 0;
218}
219
Petr Machata1e4fed22012-04-01 00:45:22 +0200220static void
221add_filter_rule(struct filter *filt, const char *expr,
222 enum filter_rule_type type,
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200223 const char *a_sym, int sym_re_p,
224 const char *a_lib, int lib_re_p)
Petr Machata1e4fed22012-04-01 00:45:22 +0200225{
Petr Machata1e4fed22012-04-01 00:45:22 +0200226 struct filter_rule *rule = malloc(sizeof(*rule));
227 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher));
228
229 if (rule == NULL || matcher == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200230 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200231 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200232 fail:
233 free(rule);
234 free(matcher);
235 return;
236 }
237
238 regex_t symbol_re;
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200239 {
240 /* Add ^ to the start of expression and $ to the end, so that
241 * we match the whole symbol name. Let the user write the "*"
242 * explicitly if they wish. */
243 char sym[strlen(a_sym) + 3];
244 sprintf(sym, "^%s$", a_sym);
Petr Machata02b96072012-09-25 23:10:14 +0200245 int status = (sym_re_p ? regcomp : globcomp)
246 (&symbol_re, sym, 0);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200247 if (status != 0) {
248 char buf[100];
249 regerror(status, &symbol_re, buf, sizeof buf);
Petr Machata20a411d2012-09-25 22:45:26 +0200250 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200251 expr, buf);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200252 goto fail;
253 }
Petr Machata1e4fed22012-04-01 00:45:22 +0200254 }
255
Petr Machata02b96072012-09-25 23:10:14 +0200256 if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) {
257 regfree(&symbol_re);
258 goto fail;
Petr Machata1e4fed22012-04-01 00:45:22 +0200259 }
260
Petr Machata1e4fed22012-04-01 00:45:22 +0200261 filter_rule_init(rule, type, matcher, symbol_re);
Petr Machatae0973cb2012-04-01 19:36:41 +0200262 filter_add_rule(filt, rule);
Petr Machata1e4fed22012-04-01 00:45:22 +0200263}
264
265static int
Petr Machata02b96072012-09-25 23:10:14 +0200266grok_libname_pattern(char **libnamep, char **libendp)
267{
268 char *libname = *libnamep;
269 char *libend = *libendp;
270
271 if (libend[0] != '/')
272 return 0;
273
274 *libend-- = 0;
275 if (libname != libend && libname[0] == '/')
276 ++libname;
277 else
278 fprintf(stderr, "Unmatched '/' in library name.\n");
279
280 *libendp = libend;
281 *libnamep = libname;
282 return 1;
283}
284
285static int
Petr Machata51e74ac2012-09-27 23:43:25 +0200286parse_filter(struct filter *filt, char *expr, int operators)
Petr Machata1e4fed22012-04-01 00:45:22 +0200287{
Petr Machata02b96072012-09-25 23:10:14 +0200288 /* Filter is a chain of sym@lib rules separated by '-' or '+'.
289 * If the filter expression starts with '-', the missing
290 * initial rule is implicitly *@*. */
Petr Machata1e4fed22012-04-01 00:45:22 +0200291
292 enum filter_rule_type type = FR_ADD;
293
294 while (*expr != 0) {
Andrey Zonov6bb42012013-02-14 12:32:06 +0100295 size_t s = strcspn(expr, &"-+@"[operators ? 0 : 2]);
Petr Machata1e4fed22012-04-01 00:45:22 +0200296 char *symname = expr;
297 char *libname;
298 char *next = expr + s + 1;
299 enum filter_rule_type this_type = type;
300
301 if (expr[s] == 0) {
302 libname = "*";
303 expr = next - 1;
304
Petr Machata050fa7f2012-04-23 23:44:36 +0200305 } else if (expr[s] == '-' || expr[s] == '+') {
306 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD;
307 expr[s] = 0;
Petr Machata1e4fed22012-04-01 00:45:22 +0200308 libname = "*";
309 expr = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200310
311 } else {
312 assert(expr[s] == '@');
313 expr[s] = 0;
Andrey Zonov6bb42012013-02-14 12:32:06 +0100314 s = strcspn(next, &"-+"[operators ? 0 : 2]);
Petr Machata1e4fed22012-04-01 00:45:22 +0200315 if (s == 0) {
316 libname = "*";
317 expr = next;
318 } else if (next[s] == 0) {
319 expr = next + s;
320 libname = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200321 } else {
Petr Machata050fa7f2012-04-23 23:44:36 +0200322 assert(next[s] == '-' || next[s] == '+');
323 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD;
Petr Machata1e4fed22012-04-01 00:45:22 +0200324 next[s] = 0;
325 expr = next + s + 1;
326 libname = next;
327 }
328 }
329
330 assert(*libname != 0);
331 char *symend = symname + strlen(symname) - 1;
332 char *libend = libname + strlen(libname) - 1;
333 int sym_is_re = 0;
334 int lib_is_re = 0;
335
336 /*
337 * /xxx/@... and ...@/xxx/ means that xxx are regular
338 * expressions. They are globs otherwise.
339 *
340 * /xxx@yyy/ is the same as /xxx/@/yyy/
341 *
342 * @/xxx matches library path name
343 * @.xxx matches library relative path name
344 */
345 if (symname[0] == '/') {
346 if (symname != symend && symend[0] == '/') {
347 ++symname;
348 *symend-- = 0;
349 sym_is_re = 1;
350
351 } else {
352 sym_is_re = 1;
353 lib_is_re = 1;
354 ++symname;
355
356 /* /XXX@YYY/ is the same as
357 * /XXX/@/YYY/. */
358 if (libend[0] != '/')
Petr Machata20a411d2012-09-25 22:45:26 +0200359 fprintf(stderr, "Unmatched '/'"
360 " in symbol name.\n");
Petr Machata1e4fed22012-04-01 00:45:22 +0200361 else
362 *libend-- = 0;
363 }
364 }
365
366 /* If libname ends in '/', then we expect '/' in the
367 * beginning too. Otherwise the initial '/' is part
368 * of absolute file name. */
Petr Machata02b96072012-09-25 23:10:14 +0200369 if (!lib_is_re)
370 lib_is_re = grok_libname_pattern(&libname, &libend);
Petr Machata1e4fed22012-04-01 00:45:22 +0200371
372 if (*symname == 0) /* /@AA/ */
373 symname = "*";
374 if (*libname == 0) /* /aa@/ */
375 libname = "*";
376
377 add_filter_rule(filt, expr, this_type,
378 symname, sym_is_re,
379 libname, lib_is_re);
380 }
381
382 return 0;
383}
384
385static struct filter *
Petr Machata51e74ac2012-09-27 23:43:25 +0200386recursive_parse_chain(char *expr, int operators)
Petr Machata1e4fed22012-04-01 00:45:22 +0200387{
Petr Machata1e4fed22012-04-01 00:45:22 +0200388 struct filter *filt = malloc(sizeof(*filt));
389 if (filt == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200390 fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200391 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200392 return NULL;
393 }
394
Petr Machata35c88142012-04-04 00:54:43 +0200395 filter_init(filt);
Petr Machata51e74ac2012-09-27 23:43:25 +0200396 if (parse_filter(filt, expr, operators) < 0) {
Petr Machata050fa7f2012-04-23 23:44:36 +0200397 fprintf(stderr, "Filter '%s' will be ignored.\n", expr);
398 free(filt);
399 filt = NULL;
Petr Machata1e4fed22012-04-01 00:45:22 +0200400 }
Petr Machata050fa7f2012-04-23 23:44:36 +0200401
402 return filt;
Petr Machata1e4fed22012-04-01 00:45:22 +0200403}
404
Petr Machata51e74ac2012-09-27 23:43:25 +0200405static struct filter **
406slist_chase_end(struct filter **begin)
407{
408 for (; *begin != NULL; begin = &(*begin)->next)
409 ;
410 return begin;
411}
412
Petr Machata1e4fed22012-04-01 00:45:22 +0200413static void
Petr Machatab5f80ac2012-04-04 01:46:18 +0200414parse_filter_chain(const char *expr, struct filter **retp)
Petr Machata1e4fed22012-04-01 00:45:22 +0200415{
416 char *str = strdup(expr);
417 if (str == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200418 fprintf(stderr, "Filter '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200419 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200420 return;
421 }
Petr Machata25f319e2012-04-23 23:45:21 +0200422 /* Support initial '!' for backward compatibility. */
423 if (str[0] == '!')
424 str[0] = '-';
425
Petr Machata51e74ac2012-09-27 23:43:25 +0200426 *slist_chase_end(retp) = recursive_parse_chain(str, 1);
Petr Machatadd1ec842012-10-27 00:34:10 +0200427 free(str);
Petr Machata1e4fed22012-04-01 00:45:22 +0200428}
429
Petr Machata68818a82012-11-30 21:42:59 +0100430static int
431parse_int(const char *optarg, char opt, int min, int max)
432{
433 char *endptr;
434 long int l = strtol(optarg, &endptr, 0);
435 if (l < min || (max != 0 && l > max)
436 || *optarg == 0 || *endptr != 0) {
437 const char *fmt = max != 0
438 ? "Invalid argument to -%c: '%s'. Use integer %d..%d.\n"
439 : "Invalid argument to -%c: '%s'. Use integer >=%d.\n";
440 fprintf(stderr, fmt, opt, optarg, min, max);
441 exit(1);
442 }
443 return (int)l;
444}
445
Juan Cespedesf1350522008-12-16 18:19:58 +0100446char **
Petr Machata67fa52f2012-04-05 02:11:39 +0200447process_options(int argc, char **argv)
448{
Juan Cespedesac3db291998-04-25 14:31:58 +0200449 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100450 options.output = stderr;
Joe Damato59e3fb12009-11-06 19:45:10 -0800451 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700452#if defined(HAVE_LIBUNWIND)
453 options.bt_depth = -1;
454#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100455
Juan Cespedesce377d52008-12-16 19:38:10 +0100456 guess_cols();
457
Petr Machata67fa52f2012-04-05 02:11:39 +0200458 int libcalls = 1;
459
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100460 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200461 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200462 char *p;
Petr Machata35d07302012-12-09 11:38:56 +0100463#ifdef HAVE_GETOPT_LONG
Juan Cespedesac3db291998-04-25 14:31:58 +0200464 int option_index = 0;
465 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100466 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200467 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200468 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100469# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100470 {"demangle", 0, 0, 'C'},
Petr Machata35d07302012-12-09 11:38:56 +0100471# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 {"indent", 1, 0, 'n'},
473 {"help", 0, 0, 'h'},
474 {"library", 1, 0, 'l'},
475 {"output", 1, 0, 'o'},
476 {"version", 0, 0, 'V'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800477 {"no-signals", 0, 0, 'b'},
Petr Machata35d07302012-12-09 11:38:56 +0100478# if defined(HAVE_LIBUNWIND)
Joe Damatoab3b72c2010-10-31 00:21:53 -0700479 {"where", 1, 0, 'w'},
Petr Machata35d07302012-12-09 11:38:56 +0100480# endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100481 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200482 };
Joe Damatoab3b72c2010-10-31 00:21:53 -0700483#endif
Petr Machata35d07302012-12-09 11:38:56 +0100484
485 const char *opts = "+"
486#ifdef USE_DEMANGLE
487 "C"
488#endif
489#if defined(HAVE_LIBUNWIND)
490 "w:"
491#endif
492 "cfhiLrStTVba:A:D:e:F:l:n:o:p:s:u:x:X:";
493
494#ifdef HAVE_GETOPT_LONG
495 c = getopt_long(argc, argv, opts, long_options, &option_index);
496#else
497 c = getopt(argc, argv, opts);
498#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100499 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200500 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100501 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100502 switch (c) {
503 case 'a':
Petr Machata68818a82012-11-30 21:42:59 +0100504 options.align = parse_int(optarg, 'a', 0, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200506 case 'A':
Petr Machata68818a82012-11-30 21:42:59 +0100507 options.arraylen = parse_int(optarg, 'A', 0, 0);
Steve Fink1150bc42006-08-07 06:04:43 +0200508 break;
Joe Damato535e7382010-11-08 15:47:43 -0800509 case 'b':
510 options.no_signals = 1;
511 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200513 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100515#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100516 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100517 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100518 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200519#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200520 case 'D':
521 if (optarg[0]=='h') {
522 usage_debug();
523 exit(0);
524 }
525 options.debug = strtoul(optarg,&p,8);
526 if (*p) {
527 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
528 err_usage();
529 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100530 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200531
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100532 case 'e':
Petr Machatab5f80ac2012-04-04 01:46:18 +0200533 parse_filter_chain(optarg, &options.plt_filter);
Petr Machata1e4fed22012-04-01 00:45:22 +0200534 break;
535
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100536 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200537 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100538 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200539 case 'F':
540 {
Petr Machata8a98e6f2012-10-27 00:30:13 +0200541 struct opt_F_t *tmp = malloc(sizeof(*tmp));
542 if (tmp == NULL) {
543 fail:
544 fprintf(stderr, "%s\n",
545 strerror(errno));
546 free(tmp);
Juan Cespedesaee09312007-08-31 18:49:48 +0200547 exit(1);
548 }
549 tmp->filename = strdup(optarg);
Petr Machata8a98e6f2012-10-27 00:30:13 +0200550 if (tmp->filename == NULL)
551 goto fail;
552 tmp->own_filename = 1;
Juan Cespedesaee09312007-08-31 18:49:48 +0200553 tmp->next = opt_F;
554 opt_F = tmp;
555 break;
556 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100557 case 'h':
558 usage();
559 exit(0);
560 case 'i':
561 opt_i++;
562 break;
Petr Machata51e74ac2012-09-27 23:43:25 +0200563
564 case 'l': {
565 size_t patlen = strlen(optarg);
566 char buf[patlen + 2];
567 sprintf(buf, "@%s", optarg);
568 *slist_chase_end(&options.export_filter)
569 = recursive_parse_chain(buf, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100570 break;
Petr Machata51e74ac2012-09-27 23:43:25 +0200571 }
572
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100573 case 'L':
Petr Machata67fa52f2012-04-05 02:11:39 +0200574 libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100575 break;
Petr Machata68818a82012-11-30 21:42:59 +0100576 case 'n':
577 options.indent = parse_int(optarg, 'n', 0, 20);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100578 break;
579 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100580 options.output = fopen(optarg, "w");
581 if (!options.output) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200582 fprintf(stderr,
583 "can't open %s for writing: %s\n",
584 optarg, strerror(errno));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100585 exit(1);
586 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100587 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
588 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100589 break;
590 case 'p':
591 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200592 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100593 if (!tmp) {
594 perror("ltrace: malloc");
595 exit(1);
596 }
Petr Machata68818a82012-11-30 21:42:59 +0100597 tmp->pid = parse_int(optarg, 'p', 1, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100598 tmp->next = opt_p;
599 opt_p = tmp;
600 break;
601 }
602 case 'r':
603 opt_r++;
604 break;
605 case 's':
Petr Machata68818a82012-11-30 21:42:59 +0100606 options.strlen = parse_int(optarg, 's', 0, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100607 break;
608 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100609 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610 break;
611 case 't':
612 opt_t++;
613 break;
614 case 'T':
615 opt_T++;
616 break;
617 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100618 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100619 break;
620 case 'V':
Petr Machata5cde20b2012-12-15 23:54:54 +0100621 printf("ltrace " PACKAGE_VERSION "\n"
622 "Copyright (C) 2010-2012 Petr Machata, Red Hat Inc.\n"
623 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
624 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
625 "This is free software: you are free to change and redistribute it.\n"
626 "There is NO WARRANTY, to the extent permitted by law.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100627 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700628 break;
Joe Damato535e7382010-11-08 15:47:43 -0800629#if defined(HAVE_LIBUNWIND)
630 case 'w':
Petr Machata68818a82012-11-30 21:42:59 +0100631 options.bt_depth = parse_int(optarg, 'w', 1, 0);
Joe Damato59e3fb12009-11-06 19:45:10 -0800632 break;
Joe Damato535e7382010-11-08 15:47:43 -0800633#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedesac3db291998-04-25 14:31:58 +0200634
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100635 case 'x':
Petr Machatada3edbf2012-04-04 02:20:21 +0200636 parse_filter_chain(optarg, &options.static_filter);
637 break;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100638
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100639 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200640 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100641 }
642 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100643 argc -= optind;
644 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100645
Juan Cespedesaee09312007-08-31 18:49:48 +0200646 if (!opt_F) {
647 opt_F = malloc(sizeof(struct opt_F_t));
648 opt_F->next = malloc(sizeof(struct opt_F_t));
649 opt_F->next->next = NULL;
650 opt_F->filename = USER_CONFIG_FILE;
Petr Machata8a98e6f2012-10-27 00:30:13 +0200651 opt_F->own_filename = 0;
Juan Cespedesaee09312007-08-31 18:49:48 +0200652 opt_F->next->filename = SYSTEM_CONFIG_FILE;
Petr Machata8a98e6f2012-10-27 00:30:13 +0200653 opt_F->next->own_filename = 0;
Juan Cespedesaee09312007-08-31 18:49:48 +0200654 }
Steve Fink58c73a72006-07-17 23:18:35 +0200655 /* Reverse the config file list since it was built by
656 * prepending, and it would make more sense to process the
657 * files in the order they were given. Probably it would make
658 * more sense to keep a tail pointer instead? */
659 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200660 struct opt_F_t *egg = NULL;
661 struct opt_F_t *chicken;
662 while (opt_F) {
663 chicken = opt_F->next;
664 opt_F->next = egg;
665 egg = opt_F;
666 opt_F = chicken;
667 }
668 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200669 }
670
Petr Machata51e74ac2012-09-27 23:43:25 +0200671 /* If neither -e, nor -l, nor -L are used, set default -e.
672 * Use @MAIN for now, as that's what ltrace used to have in
673 * the past. XXX Maybe we should make this "*" instead. */
674 if (libcalls
675 && options.plt_filter == NULL
676 && options.export_filter == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200677 parse_filter_chain("@MAIN", &options.plt_filter);
Petr Machata03673892012-04-03 13:51:09 +0200678 options.hide_caller = 1;
679 }
Petr Machata51e74ac2012-09-27 23:43:25 +0200680 if (!libcalls && options.plt_filter != NULL) {
681 fprintf(stderr,
682 "%s: Option -L can't be used with -e or -l.\n",
683 progname);
684 err_usage();
685 }
Petr Machatae0973cb2012-04-01 19:36:41 +0200686
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100687 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200688 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200689 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100690 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200691 if (opt_r && opt_t) {
Petr Machata20a411d2012-09-25 22:45:26 +0200692 fprintf(stderr,
693 "%s: Options -r and -t can't be used together\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100694 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200695 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200696 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100697 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200698 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100699 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200700 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100701}