blob: 693676bb51623bb0a6a6e834428ecb0c40e40d7b [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"
Juan Cespedes5e01f651998-03-08 22:31:44 +010042
Steve Fink58c73a72006-07-17 23:18:35 +020043#ifndef SYSCONFDIR
44#define SYSCONFDIR "/etc"
45#endif
46
Olaf Heringe948e582006-10-12 23:53:44 +020047#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020048#define USER_CONFIG_FILE "~/.ltrace.conf"
49
Juan Cespedesce377d52008-12-16 19:38:10 +010050struct options_t options = {
Juan Cespedesda9b9532009-04-07 15:33:50 +020051 .align = DEFAULT_ALIGN, /* alignment column for results */
52 .user = NULL, /* username to run command as */
53 .syscalls = 0, /* display syscalls */
Juan Cespedesce377d52008-12-16 19:38:10 +010054#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020055 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010056#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020057 .indent = 0, /* indent output according to program flow */
58 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020059 .summary = 0, /* Report a summary on program exit */
60 .debug = 0, /* debug */
61 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
62 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
63 .follow = 0, /* trace child processes */
Juan Cespedesce377d52008-12-16 19:38:10 +010064};
65
Juan Cespedesac3db291998-04-25 14:31:58 +020066static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010067int opt_i = 0; /* instruction pointer */
Juan Cespedesf666d191998-09-20 23:04:34 +020068int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020069int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010070int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010071
72/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010074
Steve Fink58c73a72006-07-17 23:18:35 +020075/* List of filenames give to option -F: */
76struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
77
Juan Cespedesf1350522008-12-16 18:19:58 +010078static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020079err_usage(void) {
Petr Machata20a411d2012-09-25 22:45:26 +020080 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020081 exit(1);
82}
83
84static void
Juan Cespedesf1350522008-12-16 18:19:58 +010085usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020086 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -a, --align=COLUMN align return values in a secific column.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +010089 " -A MAXELTS maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080090 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010092# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020094# endif
Petr Machata4b3bda42013-01-11 23:32:17 +010095 " -D, --debug=MASK enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020096 " -Dh, --debug=help show help on debugging.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +010097 " -e FILTER modify which library calls to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020098 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020099 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -i print instruction pointer at time of library call.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100102 " -l, --library=LIBRARY_PATTERN only trace symbols implemented by this library.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100105 " -o, --output=FILENAME write the trace output to file with given name.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 " -p PID attach to the process with the process ID pid.\n"
107 " -r print relative timestamps.\n"
Petr Machata4b3bda42013-01-11 23:32:17 +0100108 " -s STRSIZE specify the maximum string size to print.\n"
109 " -S trace system calls as well as library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 " -t, -tt, -ttt print absolute timestamps.\n"
111 " -T show the time spent inside each call.\n"
112 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700114#if defined(HAVE_LIBUNWIND)
Petr Machata68818a82012-11-30 21:42:59 +0100115 " -w, --where=NR print backtrace showing NR stack frames at most.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700116#endif /* defined(HAVE_LIBUNWIND) */
Petr Machata4b3bda42013-01-11 23:32:17 +0100117 " -x FILTER modify which static functions to trace.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200118 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120}
121
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200122static void
123usage_debug(void) {
124 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
125 fprintf(stdout,
126 "\n"
127 " number ref. in source description\n"
128 " 1 general Generally helpful progress information\n"
129 " 10 event Shows every event received by a traced process\n"
130 " 20 process Shows actions carried upon a traced processes\n"
131 " 40 function Shows every entry to internal functions\n"
132 "\n"
133 "Debugging options are mixed using bitwise-or.\n"
134 "Note that the meanings and values are subject to change.\n"
135 );
136}
137
Juan Cespedesf1350522008-12-16 18:19:58 +0100138static char *
139search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100140 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 char *path;
142 int m, n;
143
144 if (strchr(filename, '/')) {
145 return filename;
146 }
147 for (path = getenv("PATH"); path && *path; path += m) {
148 if (strchr(path, ':')) {
149 n = strchr(path, ':') - path;
150 m = n + 1;
151 } else {
152 m = n = strlen(path);
153 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100154 if (n + strlen(filename) + 1 >= PATH_MAX) {
Petr Machata20a411d2012-09-25 22:45:26 +0200155 fprintf(stderr, "Error: filename too long.\n");
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100156 exit(1);
157 }
158 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 if (n && pathname[n - 1] != '/') {
160 pathname[n++] = '/';
161 }
162 strcpy(pathname + n, filename);
163 if (!access(pathname, X_OK)) {
164 return pathname;
165 }
166 }
167 return filename;
168}
169
Juan Cespedesce377d52008-12-16 19:38:10 +0100170static void
171guess_cols(void) {
172 struct winsize ws;
173 char *c;
174
175 options.align = DEFAULT_ALIGN;
176 c = getenv("COLUMNS");
177 if (c && *c) {
178 char *endptr;
179 int cols;
180 cols = strtol(c, &endptr, 0);
181 if (cols > 0 && !*endptr) {
182 options.align = cols * 5 / 8;
183 }
184 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
185 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200186 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
187 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100188 }
189}
190
Petr Machata02b96072012-09-25 23:10:14 +0200191static int
192compile_libname(const char *expr, const char *a_lib, int lib_re_p,
193 struct filter_lib_matcher *matcher)
194{
195 if (strcmp(a_lib, "MAIN") == 0) {
196 filter_lib_matcher_main_init(matcher);
197 } else {
198 /* Add ^ and $ to the library expression as well. */
199 char lib[strlen(a_lib) + 3];
200 sprintf(lib, "^%s$", a_lib);
201
202 enum filter_lib_matcher_type type
203 = lib[0] == '/' ? FLM_PATHNAME : FLM_SONAME;
204
205 regex_t lib_re;
206 int status = (lib_re_p ? regcomp : globcomp)(&lib_re, lib, 0);
Petr Machatab6c5c8c2012-12-08 03:23:39 +0100207 if (status != 0) {
Petr Machata02b96072012-09-25 23:10:14 +0200208 char buf[100];
209 regerror(status, &lib_re, buf, sizeof buf);
210 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
211 expr, buf);
212 return -1;
213 }
214 filter_lib_matcher_name_init(matcher, type, lib_re);
215 }
216 return 0;
217}
218
Petr Machata1e4fed22012-04-01 00:45:22 +0200219static void
220add_filter_rule(struct filter *filt, const char *expr,
221 enum filter_rule_type type,
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200222 const char *a_sym, int sym_re_p,
223 const char *a_lib, int lib_re_p)
Petr Machata1e4fed22012-04-01 00:45:22 +0200224{
Petr Machata1e4fed22012-04-01 00:45:22 +0200225 struct filter_rule *rule = malloc(sizeof(*rule));
226 struct filter_lib_matcher *matcher = malloc(sizeof(*matcher));
227
228 if (rule == NULL || matcher == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200229 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200230 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200231 fail:
232 free(rule);
233 free(matcher);
234 return;
235 }
236
237 regex_t symbol_re;
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200238 {
239 /* Add ^ to the start of expression and $ to the end, so that
240 * we match the whole symbol name. Let the user write the "*"
241 * explicitly if they wish. */
242 char sym[strlen(a_sym) + 3];
243 sprintf(sym, "^%s$", a_sym);
Petr Machata02b96072012-09-25 23:10:14 +0200244 int status = (sym_re_p ? regcomp : globcomp)
245 (&symbol_re, sym, 0);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200246 if (status != 0) {
247 char buf[100];
248 regerror(status, &symbol_re, buf, sizeof buf);
Petr Machata20a411d2012-09-25 22:45:26 +0200249 fprintf(stderr, "Rule near '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200250 expr, buf);
Petr Machata5bc4e7f2012-04-06 22:28:24 +0200251 goto fail;
252 }
Petr Machata1e4fed22012-04-01 00:45:22 +0200253 }
254
Petr Machata02b96072012-09-25 23:10:14 +0200255 if (compile_libname(expr, a_lib, lib_re_p, matcher) < 0) {
256 regfree(&symbol_re);
257 goto fail;
Petr Machata1e4fed22012-04-01 00:45:22 +0200258 }
259
Petr Machata1e4fed22012-04-01 00:45:22 +0200260 filter_rule_init(rule, type, matcher, symbol_re);
Petr Machatae0973cb2012-04-01 19:36:41 +0200261 filter_add_rule(filt, rule);
Petr Machata1e4fed22012-04-01 00:45:22 +0200262}
263
264static int
Petr Machata02b96072012-09-25 23:10:14 +0200265grok_libname_pattern(char **libnamep, char **libendp)
266{
267 char *libname = *libnamep;
268 char *libend = *libendp;
269
270 if (libend[0] != '/')
271 return 0;
272
273 *libend-- = 0;
274 if (libname != libend && libname[0] == '/')
275 ++libname;
276 else
277 fprintf(stderr, "Unmatched '/' in library name.\n");
278
279 *libendp = libend;
280 *libnamep = libname;
281 return 1;
282}
283
284static int
Petr Machata51e74ac2012-09-27 23:43:25 +0200285parse_filter(struct filter *filt, char *expr, int operators)
Petr Machata1e4fed22012-04-01 00:45:22 +0200286{
Petr Machata02b96072012-09-25 23:10:14 +0200287 /* Filter is a chain of sym@lib rules separated by '-' or '+'.
288 * If the filter expression starts with '-', the missing
289 * initial rule is implicitly *@*. */
Petr Machata1e4fed22012-04-01 00:45:22 +0200290
291 enum filter_rule_type type = FR_ADD;
292
293 while (*expr != 0) {
Andrey Zonov6bb42012013-02-14 12:32:06 +0100294 size_t s = strcspn(expr, &"-+@"[operators ? 0 : 2]);
Petr Machata1e4fed22012-04-01 00:45:22 +0200295 char *symname = expr;
296 char *libname;
297 char *next = expr + s + 1;
298 enum filter_rule_type this_type = type;
299
300 if (expr[s] == 0) {
301 libname = "*";
302 expr = next - 1;
303
Petr Machata050fa7f2012-04-23 23:44:36 +0200304 } else if (expr[s] == '-' || expr[s] == '+') {
305 type = expr[s] == '-' ? FR_SUBTRACT : FR_ADD;
306 expr[s] = 0;
Petr Machata1e4fed22012-04-01 00:45:22 +0200307 libname = "*";
308 expr = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200309
310 } else {
311 assert(expr[s] == '@');
312 expr[s] = 0;
Andrey Zonov6bb42012013-02-14 12:32:06 +0100313 s = strcspn(next, &"-+"[operators ? 0 : 2]);
Petr Machata1e4fed22012-04-01 00:45:22 +0200314 if (s == 0) {
315 libname = "*";
316 expr = next;
317 } else if (next[s] == 0) {
318 expr = next + s;
319 libname = next;
Petr Machata1e4fed22012-04-01 00:45:22 +0200320 } else {
Petr Machata050fa7f2012-04-23 23:44:36 +0200321 assert(next[s] == '-' || next[s] == '+');
322 type = next[s] == '-' ? FR_SUBTRACT : FR_ADD;
Petr Machata1e4fed22012-04-01 00:45:22 +0200323 next[s] = 0;
324 expr = next + s + 1;
325 libname = next;
326 }
327 }
328
329 assert(*libname != 0);
330 char *symend = symname + strlen(symname) - 1;
331 char *libend = libname + strlen(libname) - 1;
332 int sym_is_re = 0;
333 int lib_is_re = 0;
334
335 /*
336 * /xxx/@... and ...@/xxx/ means that xxx are regular
337 * expressions. They are globs otherwise.
338 *
339 * /xxx@yyy/ is the same as /xxx/@/yyy/
340 *
341 * @/xxx matches library path name
342 * @.xxx matches library relative path name
343 */
344 if (symname[0] == '/') {
345 if (symname != symend && symend[0] == '/') {
346 ++symname;
347 *symend-- = 0;
348 sym_is_re = 1;
349
350 } else {
351 sym_is_re = 1;
352 lib_is_re = 1;
353 ++symname;
354
355 /* /XXX@YYY/ is the same as
356 * /XXX/@/YYY/. */
357 if (libend[0] != '/')
Petr Machata20a411d2012-09-25 22:45:26 +0200358 fprintf(stderr, "Unmatched '/'"
359 " in symbol name.\n");
Petr Machata1e4fed22012-04-01 00:45:22 +0200360 else
361 *libend-- = 0;
362 }
363 }
364
365 /* If libname ends in '/', then we expect '/' in the
366 * beginning too. Otherwise the initial '/' is part
367 * of absolute file name. */
Petr Machata02b96072012-09-25 23:10:14 +0200368 if (!lib_is_re)
369 lib_is_re = grok_libname_pattern(&libname, &libend);
Petr Machata1e4fed22012-04-01 00:45:22 +0200370
371 if (*symname == 0) /* /@AA/ */
372 symname = "*";
373 if (*libname == 0) /* /aa@/ */
374 libname = "*";
375
376 add_filter_rule(filt, expr, this_type,
377 symname, sym_is_re,
378 libname, lib_is_re);
379 }
380
381 return 0;
382}
383
384static struct filter *
Petr Machata51e74ac2012-09-27 23:43:25 +0200385recursive_parse_chain(char *expr, int operators)
Petr Machata1e4fed22012-04-01 00:45:22 +0200386{
Petr Machata1e4fed22012-04-01 00:45:22 +0200387 struct filter *filt = malloc(sizeof(*filt));
388 if (filt == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200389 fprintf(stderr, "(Part of) filter will be ignored: '%s': %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200390 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200391 return NULL;
392 }
393
Petr Machata35c88142012-04-04 00:54:43 +0200394 filter_init(filt);
Petr Machata51e74ac2012-09-27 23:43:25 +0200395 if (parse_filter(filt, expr, operators) < 0) {
Petr Machata050fa7f2012-04-23 23:44:36 +0200396 fprintf(stderr, "Filter '%s' will be ignored.\n", expr);
397 free(filt);
398 filt = NULL;
Petr Machata1e4fed22012-04-01 00:45:22 +0200399 }
Petr Machata050fa7f2012-04-23 23:44:36 +0200400
401 return filt;
Petr Machata1e4fed22012-04-01 00:45:22 +0200402}
403
Petr Machata51e74ac2012-09-27 23:43:25 +0200404static struct filter **
405slist_chase_end(struct filter **begin)
406{
407 for (; *begin != NULL; begin = &(*begin)->next)
408 ;
409 return begin;
410}
411
Petr Machata1e4fed22012-04-01 00:45:22 +0200412static void
Petr Machatab5f80ac2012-04-04 01:46:18 +0200413parse_filter_chain(const char *expr, struct filter **retp)
Petr Machata1e4fed22012-04-01 00:45:22 +0200414{
415 char *str = strdup(expr);
416 if (str == NULL) {
Petr Machata20a411d2012-09-25 22:45:26 +0200417 fprintf(stderr, "Filter '%s' will be ignored: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200418 expr, strerror(errno));
Petr Machata1e4fed22012-04-01 00:45:22 +0200419 return;
420 }
Petr Machata25f319e2012-04-23 23:45:21 +0200421 /* Support initial '!' for backward compatibility. */
422 if (str[0] == '!')
423 str[0] = '-';
424
Petr Machata51e74ac2012-09-27 23:43:25 +0200425 *slist_chase_end(retp) = recursive_parse_chain(str, 1);
Petr Machatadd1ec842012-10-27 00:34:10 +0200426 free(str);
Petr Machata1e4fed22012-04-01 00:45:22 +0200427}
428
Petr Machata68818a82012-11-30 21:42:59 +0100429static int
430parse_int(const char *optarg, char opt, int min, int max)
431{
432 char *endptr;
433 long int l = strtol(optarg, &endptr, 0);
434 if (l < min || (max != 0 && l > max)
435 || *optarg == 0 || *endptr != 0) {
436 const char *fmt = max != 0
437 ? "Invalid argument to -%c: '%s'. Use integer %d..%d.\n"
438 : "Invalid argument to -%c: '%s'. Use integer >=%d.\n";
439 fprintf(stderr, fmt, opt, optarg, min, max);
440 exit(1);
441 }
442 return (int)l;
443}
444
Juan Cespedesf1350522008-12-16 18:19:58 +0100445char **
Petr Machata67fa52f2012-04-05 02:11:39 +0200446process_options(int argc, char **argv)
447{
Juan Cespedesac3db291998-04-25 14:31:58 +0200448 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100449 options.output = stderr;
Joe Damato59e3fb12009-11-06 19:45:10 -0800450 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700451#if defined(HAVE_LIBUNWIND)
452 options.bt_depth = -1;
453#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100454
Juan Cespedesce377d52008-12-16 19:38:10 +0100455 guess_cols();
456
Petr Machata67fa52f2012-04-05 02:11:39 +0200457 int libcalls = 1;
458
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200460 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200461 char *p;
Petr Machata35d07302012-12-09 11:38:56 +0100462#ifdef HAVE_GETOPT_LONG
Juan Cespedesac3db291998-04-25 14:31:58 +0200463 int option_index = 0;
464 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200466 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200467 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100468# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100469 {"demangle", 0, 0, 'C'},
Petr Machata35d07302012-12-09 11:38:56 +0100470# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100471 {"indent", 1, 0, 'n'},
472 {"help", 0, 0, 'h'},
473 {"library", 1, 0, 'l'},
474 {"output", 1, 0, 'o'},
475 {"version", 0, 0, 'V'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800476 {"no-signals", 0, 0, 'b'},
Petr Machata35d07302012-12-09 11:38:56 +0100477# if defined(HAVE_LIBUNWIND)
Joe Damatoab3b72c2010-10-31 00:21:53 -0700478 {"where", 1, 0, 'w'},
Petr Machata35d07302012-12-09 11:38:56 +0100479# endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100480 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200481 };
Joe Damatoab3b72c2010-10-31 00:21:53 -0700482#endif
Petr Machata35d07302012-12-09 11:38:56 +0100483
484 const char *opts = "+"
485#ifdef USE_DEMANGLE
486 "C"
487#endif
488#if defined(HAVE_LIBUNWIND)
489 "w:"
490#endif
491 "cfhiLrStTVba:A:D:e:F:l:n:o:p:s:u:x:X:";
492
493#ifdef HAVE_GETOPT_LONG
494 c = getopt_long(argc, argv, opts, long_options, &option_index);
495#else
496 c = getopt(argc, argv, opts);
497#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100498 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200499 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100500 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 switch (c) {
502 case 'a':
Petr Machata68818a82012-11-30 21:42:59 +0100503 options.align = parse_int(optarg, 'a', 0, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100504 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200505 case 'A':
Petr Machata68818a82012-11-30 21:42:59 +0100506 options.arraylen = parse_int(optarg, 'A', 0, 0);
Steve Fink1150bc42006-08-07 06:04:43 +0200507 break;
Joe Damato535e7382010-11-08 15:47:43 -0800508 case 'b':
509 options.no_signals = 1;
510 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200512 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100514#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100515 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100516 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100517 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200518#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200519 case 'D':
520 if (optarg[0]=='h') {
521 usage_debug();
522 exit(0);
523 }
524 options.debug = strtoul(optarg,&p,8);
525 if (*p) {
526 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
527 err_usage();
528 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100529 break;
Petr Machata1e4fed22012-04-01 00:45:22 +0200530
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100531 case 'e':
Petr Machatab5f80ac2012-04-04 01:46:18 +0200532 parse_filter_chain(optarg, &options.plt_filter);
Petr Machata1e4fed22012-04-01 00:45:22 +0200533 break;
534
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100535 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200536 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100537 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200538 case 'F':
539 {
Petr Machata8a98e6f2012-10-27 00:30:13 +0200540 struct opt_F_t *tmp = malloc(sizeof(*tmp));
541 if (tmp == NULL) {
542 fail:
543 fprintf(stderr, "%s\n",
544 strerror(errno));
545 free(tmp);
Juan Cespedesaee09312007-08-31 18:49:48 +0200546 exit(1);
547 }
548 tmp->filename = strdup(optarg);
Petr Machata8a98e6f2012-10-27 00:30:13 +0200549 if (tmp->filename == NULL)
550 goto fail;
551 tmp->own_filename = 1;
Juan Cespedesaee09312007-08-31 18:49:48 +0200552 tmp->next = opt_F;
553 opt_F = tmp;
554 break;
555 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100556 case 'h':
557 usage();
558 exit(0);
559 case 'i':
560 opt_i++;
561 break;
Petr Machata51e74ac2012-09-27 23:43:25 +0200562
563 case 'l': {
564 size_t patlen = strlen(optarg);
565 char buf[patlen + 2];
566 sprintf(buf, "@%s", optarg);
567 *slist_chase_end(&options.export_filter)
568 = recursive_parse_chain(buf, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100569 break;
Petr Machata51e74ac2012-09-27 23:43:25 +0200570 }
571
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100572 case 'L':
Petr Machata67fa52f2012-04-05 02:11:39 +0200573 libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100574 break;
Petr Machata68818a82012-11-30 21:42:59 +0100575 case 'n':
576 options.indent = parse_int(optarg, 'n', 0, 20);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100577 break;
578 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100579 options.output = fopen(optarg, "w");
580 if (!options.output) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200581 fprintf(stderr,
582 "can't open %s for writing: %s\n",
583 optarg, strerror(errno));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100584 exit(1);
585 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100586 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
587 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 break;
589 case 'p':
590 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200591 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 if (!tmp) {
593 perror("ltrace: malloc");
594 exit(1);
595 }
Petr Machata68818a82012-11-30 21:42:59 +0100596 tmp->pid = parse_int(optarg, 'p', 1, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100597 tmp->next = opt_p;
598 opt_p = tmp;
599 break;
600 }
601 case 'r':
602 opt_r++;
603 break;
604 case 's':
Petr Machata68818a82012-11-30 21:42:59 +0100605 options.strlen = parse_int(optarg, 's', 0, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 break;
607 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100608 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100609 break;
610 case 't':
611 opt_t++;
612 break;
613 case 'T':
614 opt_T++;
615 break;
616 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100617 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100618 break;
619 case 'V':
Petr Machata5cde20b2012-12-15 23:54:54 +0100620 printf("ltrace " PACKAGE_VERSION "\n"
621 "Copyright (C) 2010-2012 Petr Machata, Red Hat Inc.\n"
622 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
623 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
624 "This is free software: you are free to change and redistribute it.\n"
625 "There is NO WARRANTY, to the extent permitted by law.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100626 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700627 break;
Joe Damato535e7382010-11-08 15:47:43 -0800628#if defined(HAVE_LIBUNWIND)
629 case 'w':
Petr Machata68818a82012-11-30 21:42:59 +0100630 options.bt_depth = parse_int(optarg, 'w', 1, 0);
Joe Damato59e3fb12009-11-06 19:45:10 -0800631 break;
Joe Damato535e7382010-11-08 15:47:43 -0800632#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedesac3db291998-04-25 14:31:58 +0200633
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100634 case 'x':
Petr Machatada3edbf2012-04-04 02:20:21 +0200635 parse_filter_chain(optarg, &options.static_filter);
636 break;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100637
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100638 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200639 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100640 }
641 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100642 argc -= optind;
643 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100644
Juan Cespedesaee09312007-08-31 18:49:48 +0200645 if (!opt_F) {
646 opt_F = malloc(sizeof(struct opt_F_t));
647 opt_F->next = malloc(sizeof(struct opt_F_t));
648 opt_F->next->next = NULL;
649 opt_F->filename = USER_CONFIG_FILE;
Petr Machata8a98e6f2012-10-27 00:30:13 +0200650 opt_F->own_filename = 0;
Juan Cespedesaee09312007-08-31 18:49:48 +0200651 opt_F->next->filename = SYSTEM_CONFIG_FILE;
Petr Machata8a98e6f2012-10-27 00:30:13 +0200652 opt_F->next->own_filename = 0;
Juan Cespedesaee09312007-08-31 18:49:48 +0200653 }
Steve Fink58c73a72006-07-17 23:18:35 +0200654 /* Reverse the config file list since it was built by
655 * prepending, and it would make more sense to process the
656 * files in the order they were given. Probably it would make
657 * more sense to keep a tail pointer instead? */
658 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200659 struct opt_F_t *egg = NULL;
660 struct opt_F_t *chicken;
661 while (opt_F) {
662 chicken = opt_F->next;
663 opt_F->next = egg;
664 egg = opt_F;
665 opt_F = chicken;
666 }
667 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200668 }
669
Petr Machata51e74ac2012-09-27 23:43:25 +0200670 /* If neither -e, nor -l, nor -L are used, set default -e.
671 * Use @MAIN for now, as that's what ltrace used to have in
672 * the past. XXX Maybe we should make this "*" instead. */
673 if (libcalls
674 && options.plt_filter == NULL
675 && options.export_filter == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200676 parse_filter_chain("@MAIN", &options.plt_filter);
Petr Machata03673892012-04-03 13:51:09 +0200677 options.hide_caller = 1;
678 }
Petr Machata51e74ac2012-09-27 23:43:25 +0200679 if (!libcalls && options.plt_filter != NULL) {
680 fprintf(stderr,
681 "%s: Option -L can't be used with -e or -l.\n",
682 progname);
683 err_usage();
684 }
Petr Machatae0973cb2012-04-01 19:36:41 +0200685
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100686 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200687 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200688 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100689 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200690 if (opt_r && opt_t) {
Petr Machata20a411d2012-09-25 22:45:26 +0200691 fprintf(stderr,
692 "%s: Options -r and -t can't be used together\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100693 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200694 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200695 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100696 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200697 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100698 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200699 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100700}