blob: d3e62a0ef06430ae4e035d755aec1f60cfd05ac0 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +02002
Juan Cespedes5e01f651998-03-08 22:31:44 +01003#include <string.h>
4#include <stdlib.h>
5#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +02006#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01007#include <errno.h>
8#include <limits.h>
Juan Cespedesce377d52008-12-16 19:38:10 +01009#include <sys/ioctl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010010
Juan Cespedesac3db291998-04-25 14:31:58 +020011#include <getopt.h>
Juan Cespedesac3db291998-04-25 14:31:58 +020012
Juan Cespedesf7281232009-06-25 16:11:21 +020013#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010014
Steve Fink58c73a72006-07-17 23:18:35 +020015#ifndef SYSCONFDIR
16#define SYSCONFDIR "/etc"
17#endif
18
Olaf Heringe948e582006-10-12 23:53:44 +020019#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020020#define USER_CONFIG_FILE "~/.ltrace.conf"
21
Juan Cespedesce377d52008-12-16 19:38:10 +010022struct options_t options = {
Juan Cespedesda9b9532009-04-07 15:33:50 +020023 .align = DEFAULT_ALIGN, /* alignment column for results */
24 .user = NULL, /* username to run command as */
25 .syscalls = 0, /* display syscalls */
26 .libcalls = 1, /* display library calls */
Juan Cespedesce377d52008-12-16 19:38:10 +010027#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020028 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010029#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020030 .indent = 0, /* indent output according to program flow */
31 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020032 .summary = 0, /* Report a summary on program exit */
33 .debug = 0, /* debug */
34 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
35 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
36 .follow = 0, /* trace child processes */
Juan Cespedesce377d52008-12-16 19:38:10 +010037};
38
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020039char *library[MAX_LIBRARIES];
Juan Cespedes1cd999a2001-07-03 00:46:04 +020040int library_num = 0;
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
Juan Cespedesac3db291998-04-25 14:31:58 +020050/* List of function names given to option -e: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051struct opt_e_t *opt_e = NULL;
52int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020053
Ian Wienand9a2ad352006-02-20 22:44:45 +010054/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010055struct opt_x_t *opt_x = NULL;
Joe Damato7b0a7de2010-11-08 15:47:34 -080056unsigned int opt_x_cnt = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +010057
Steve Fink58c73a72006-07-17 23:18:35 +020058/* List of filenames give to option -F: */
59struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
60
Paul Gilliambe320772006-04-24 22:06:23 +020061#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010062/* Set a break on the routine named here in order to re-initialize breakpoints
63 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020064char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
65#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010066
Juan Cespedesf1350522008-12-16 18:19:58 +010067static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020068err_usage(void) {
Juan Cespedesc5c744a2009-07-23 18:22:58 +020069 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020070 exit(1);
71}
72
73static void
Juan Cespedesf1350522008-12-16 18:19:58 +010074usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020075 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +020078 " -A ARRAYLEN maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080079 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010081# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020083# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +020084 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020085 " -Dh, --debug=help show help on debugging.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020087 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020088 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Joe Damatofa2aefc2010-10-30 19:56:50 -070089 " -g, --no-plt disable breakpoints on PLT entries.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -i print instruction pointer at time of library call.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -l, --library=FILE print library calls from this library only.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 " -o, --output=FILE write the trace output to that file.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 " -p PID attach to the process with the process ID pid.\n"
97 " -r print relative timestamps.\n"
98 " -s STRLEN specify the maximum string size to print.\n"
99 " -S display system calls.\n"
100 " -t, -tt, -ttt print absolute timestamps.\n"
101 " -T show the time spent inside each call.\n"
102 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 " -V, --version output version information and exit.\n"
Joe Damatoab3b72c2010-10-31 00:21:53 -0700104#if defined(HAVE_LIBUNWIND)
105 " -w=NR, --where=NR print backtrace showing NR stack frames at most.\n"
106#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200108#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200110#endif
111 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100113}
114
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200115static void
116usage_debug(void) {
117 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
118 fprintf(stdout,
119 "\n"
120 " number ref. in source description\n"
121 " 1 general Generally helpful progress information\n"
122 " 10 event Shows every event received by a traced process\n"
123 " 20 process Shows actions carried upon a traced processes\n"
124 " 40 function Shows every entry to internal functions\n"
125 "\n"
126 "Debugging options are mixed using bitwise-or.\n"
127 "Note that the meanings and values are subject to change.\n"
128 );
129}
130
Juan Cespedesf1350522008-12-16 18:19:58 +0100131static char *
132search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100133 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 char *path;
135 int m, n;
136
137 if (strchr(filename, '/')) {
138 return filename;
139 }
140 for (path = getenv("PATH"); path && *path; path += m) {
141 if (strchr(path, ':')) {
142 n = strchr(path, ':') - path;
143 m = n + 1;
144 } else {
145 m = n = strlen(path);
146 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100147 if (n + strlen(filename) + 1 >= PATH_MAX) {
148 fprintf(stderr, "Error: filename too long\n");
149 exit(1);
150 }
151 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 if (n && pathname[n - 1] != '/') {
153 pathname[n++] = '/';
154 }
155 strcpy(pathname + n, filename);
156 if (!access(pathname, X_OK)) {
157 return pathname;
158 }
159 }
160 return filename;
161}
162
Juan Cespedesce377d52008-12-16 19:38:10 +0100163static void
164guess_cols(void) {
165 struct winsize ws;
166 char *c;
167
168 options.align = DEFAULT_ALIGN;
169 c = getenv("COLUMNS");
170 if (c && *c) {
171 char *endptr;
172 int cols;
173 cols = strtol(c, &endptr, 0);
174 if (cols > 0 && !*endptr) {
175 options.align = cols * 5 / 8;
176 }
177 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
178 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200179 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
180 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100181 }
182}
183
Juan Cespedesf1350522008-12-16 18:19:58 +0100184char **
185process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200186 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100187 options.output = stderr;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700188 options.no_plt = 0;
Joe Damato59e3fb12009-11-06 19:45:10 -0800189 options.no_signals = 0;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700190#if defined(HAVE_LIBUNWIND)
191 options.bt_depth = -1;
192#endif /* defined(HAVE_LIBUNWIND) */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100193
Juan Cespedesce377d52008-12-16 19:38:10 +0100194 guess_cols();
195
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200197 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200198 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200199 int option_index = 0;
200 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200202 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200203 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100204# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200206#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 {"indent", 1, 0, 'n'},
208 {"help", 0, 0, 'h'},
209 {"library", 1, 0, 'l'},
210 {"output", 1, 0, 'o'},
211 {"version", 0, 0, 'V'},
Joe Damatofa2aefc2010-10-30 19:56:50 -0700212 {"no-plt", 0, 0, 'g'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800213 {"no-signals", 0, 0, 'b'},
Joe Damatoab3b72c2010-10-31 00:21:53 -0700214#if defined(HAVE_LIBUNWIND)
215 {"where", 1, 0, 'w'},
216#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200218 };
Joe Damato59e3fb12009-11-06 19:45:10 -0800219 c = getopt_long(argc, argv, "+cfhiLrStTVgb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100220# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200222# endif
Joe Damatoab3b72c2010-10-31 00:21:53 -0700223#if defined(HAVE_LIBUNWIND)
224 "a:A:D:e:F:l:n:o:p:s:u:x:X:w:", long_options,
225#else /* !defined(HAVE_LIBUNWIND) */
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200226 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Joe Damatoab3b72c2010-10-31 00:21:53 -0700227#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200230 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100231 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 switch (c) {
233 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100234 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100235 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200236 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200237 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200238 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200240 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100241 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100242#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100244 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200246#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200247 case 'D':
248 if (optarg[0]=='h') {
249 usage_debug();
250 exit(0);
251 }
252 options.debug = strtoul(optarg,&p,8);
253 if (*p) {
254 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
255 err_usage();
256 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 break;
258 case 'e':
259 {
260 char *str_e = strdup(optarg);
261 if (!str_e) {
262 perror("ltrace: strdup");
263 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100264 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 if (str_e[0] == '!') {
266 opt_e_enable = 0;
267 str_e++;
268 }
269 while (*str_e) {
270 struct opt_e_t *tmp;
271 char *str2 = strchr(str_e, ',');
272 if (str2) {
273 *str2 = '\0';
274 }
275 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100276 if (!tmp) {
277 perror("ltrace: malloc");
278 exit(1);
279 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 tmp->name = str_e;
281 tmp->next = opt_e;
282 opt_e = tmp;
283 if (str2) {
284 str_e = str2 + 1;
285 } else {
286 break;
287 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100288 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100289 break;
290 }
291 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200292 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200294 case 'F':
295 {
296 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
297 if (!tmp) {
298 perror("ltrace: malloc");
299 exit(1);
300 }
301 tmp->filename = strdup(optarg);
302 tmp->next = opt_F;
303 opt_F = tmp;
304 break;
305 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700306 case 'g':
307 options.no_plt = 1;
308 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 case 'h':
310 usage();
311 exit(0);
312 case 'i':
313 opt_i++;
314 break;
315 case 'l':
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200316 if (library_num == MAX_LIBRARIES) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100317 fprintf(stderr,
318 "Too many libraries. Maximum is %i.\n",
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200319 MAX_LIBRARIES);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 exit(1);
321 }
322 library[library_num++] = optarg;
323 break;
324 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100325 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 break;
327 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100328 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100329 break;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700330#if defined(HAVE_LIBUNWIND)
331 case 'w':
332 options.bt_depth = atoi(optarg);
333 break;
334#endif /* defined(HAVE_LIBUNWIND) */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100336 options.output = fopen(optarg, "w");
337 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100338 fprintf(stderr,
339 "Can't open %s for output: %s\n",
340 optarg, strerror(errno));
341 exit(1);
342 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100343 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
344 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 break;
346 case 'p':
347 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200348 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 if (!tmp) {
350 perror("ltrace: malloc");
351 exit(1);
352 }
353 tmp->pid = atoi(optarg);
354 tmp->next = opt_p;
355 opt_p = tmp;
356 break;
357 }
358 case 'r':
359 opt_r++;
360 break;
361 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200362 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100363 break;
364 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100365 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100366 break;
367 case 't':
368 opt_t++;
369 break;
370 case 'T':
371 opt_T++;
372 break;
373 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100374 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 break;
376 case 'V':
377 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200378 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200379 "This is free software; see the GNU General Public Licence\n"
380 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700382 break;
Joe Damato59e3fb12009-11-06 19:45:10 -0800383 case 'b':
384 options.no_signals = 1;
385 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200387#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200389#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200390 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200391 "architecture: assuming you meant \"-x\"\n");
392#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200394
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 case 'x':
396 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100397 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100398
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399 /* First, check for duplicate. */
400 while (p && strcmp(p->name, optarg)) {
401 p = p->next;
402 }
403 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100404 break;
405 }
406
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100407 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100408 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100409 if (!p) {
410 perror("ltrace: malloc");
411 exit(1);
412 }
Joe Damato7b0a7de2010-11-08 15:47:34 -0800413 opt_x_cnt++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100414 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100415 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 p->next = opt_x;
Joe Damato7b0a7de2010-11-08 15:47:34 -0800417 p->hash = ~(0UL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 opt_x = p;
419 break;
420 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100421
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200423 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100424 }
425 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 argc -= optind;
427 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100428
Juan Cespedesaee09312007-08-31 18:49:48 +0200429 if (!opt_F) {
430 opt_F = malloc(sizeof(struct opt_F_t));
431 opt_F->next = malloc(sizeof(struct opt_F_t));
432 opt_F->next->next = NULL;
433 opt_F->filename = USER_CONFIG_FILE;
434 opt_F->next->filename = SYSTEM_CONFIG_FILE;
435 }
Steve Fink58c73a72006-07-17 23:18:35 +0200436 /* Reverse the config file list since it was built by
437 * prepending, and it would make more sense to process the
438 * files in the order they were given. Probably it would make
439 * more sense to keep a tail pointer instead? */
440 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200441 struct opt_F_t *egg = NULL;
442 struct opt_F_t *chicken;
443 while (opt_F) {
444 chicken = opt_F->next;
445 opt_F->next = egg;
446 egg = opt_F;
447 opt_F = chicken;
448 }
449 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200450 }
451
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100452 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200453 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200454 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100455 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200456 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 fprintf(stderr, "%s: Incompatible options -r and -t\n",
458 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200459 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200460 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100461 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200462 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100463 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200464 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100465}