blob: 04e55a8c7027b0065699c535e4c8ca5a1ee67b2a [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <string.h>
6#include <stdlib.h>
7#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +02008#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <errno.h>
10#include <limits.h>
Juan Cespedesce377d52008-12-16 19:38:10 +010011#include <sys/ioctl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012
Juan Cespedesac3db291998-04-25 14:31:58 +020013#if HAVE_GETOPT_H
14#include <getopt.h>
15#endif
16
Juan Cespedes5e01f651998-03-08 22:31:44 +010017#include "ltrace.h"
18#include "options.h"
19#include "defs.h"
20
Steve Fink58c73a72006-07-17 23:18:35 +020021#ifndef SYSCONFDIR
22#define SYSCONFDIR "/etc"
23#endif
24
Olaf Heringe948e582006-10-12 23:53:44 +020025#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020026#define USER_CONFIG_FILE "~/.ltrace.conf"
27
Juan Cespedesce377d52008-12-16 19:38:10 +010028struct options_t options = {
29 .align = DEFAULT_ALIGN, /* alignment column for results */
30 .user = NULL, /* username to run command as */
31 .syscalls = 0, /* display syscalls */
32 .libcalls = 1, /* display library calls */
33#ifdef USE_DEMANGLE
Juan Cespedesb65bdc52008-12-16 19:50:16 +010034 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010035#endif
Juan Cespedesb65bdc52008-12-16 19:50:16 +010036 .indent = 0, /* indent output according to program flow */
37 .output = NULL, /* output to a specific file */
Juan Cespedesce377d52008-12-16 19:38:10 +010038};
39
Juan Cespedes1cd999a2001-07-03 00:46:04 +020040#define MAX_LIBRARY 30
41char *library[MAX_LIBRARY];
42int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020043static char *progname; /* Program name (`ltrace') */
Juan Cespedesaee09312007-08-31 18:49:48 +020044int opt_A = DEFAULT_ARRAYLEN; /* maximum # array elements to print */
45int opt_c = 0; /* Report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010046int opt_d = 0; /* debug */
47int opt_i = 0; /* instruction pointer */
Juan Cespedesaee09312007-08-31 18:49:48 +020048int opt_s = DEFAULT_STRLEN; /* maximum # of bytes printed in strings */
Juan Cespedes5e01f651998-03-08 22:31:44 +010049int opt_f = 0; /* trace child processes as they are created */
Juan Cespedesf666d191998-09-20 23:04:34 +020050int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020051int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010052int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010053
54/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010056
Juan Cespedesac3db291998-04-25 14:31:58 +020057/* List of function names given to option -e: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058struct opt_e_t *opt_e = NULL;
59int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020060
Ian Wienand9a2ad352006-02-20 22:44:45 +010061/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010062struct opt_x_t *opt_x = NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010063
Steve Fink58c73a72006-07-17 23:18:35 +020064/* List of filenames give to option -F: */
65struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
66
Paul Gilliambe320772006-04-24 22:06:23 +020067#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010068/* Set a break on the routine named here in order to re-initialize breakpoints
69 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020070char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
71#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010072
Juan Cespedesf1350522008-12-16 18:19:58 +010073static void
74usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020075#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
76 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020078#else
79 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020081# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020083# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020085# endif
Juan Cespedesaee09312007-08-31 18:49:48 +020086 " -A ARRAYLEN maximum number of array elements to print.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010088# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020089# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020091# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020093# endif
94# endif
95# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020097# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010098 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020099# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 " -e expr modify which events to trace.\n"
101 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100102# if HAVE_GETOPT_LONG
Juan Cespedesce377d52008-12-16 19:38:10 +0100103 " -F, --config=FILE load alternate configuration file (can be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200104# else
Juan Cespedesce377d52008-12-16 19:38:10 +0100105 " -F FILE load alternate configuration file (can be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200106# endif
Steve Fink58c73a72006-07-17 23:18:35 +0200107# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100109# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100111# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100113# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100115# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100117# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200119# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200121# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200123# endif
124# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100125 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200126# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200128# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100129 " -p PID attach to the process with the process ID pid.\n"
130 " -r print relative timestamps.\n"
131 " -s STRLEN specify the maximum string size to print.\n"
132 " -S display system calls.\n"
133 " -t, -tt, -ttt print absolute timestamps.\n"
134 " -T show the time spent inside each call.\n"
135 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200136# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200138# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200140# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200142#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100143 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200144#endif
145 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200147#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148}
149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150static char *
151search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100152 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100153 char *path;
154 int m, n;
155
156 if (strchr(filename, '/')) {
157 return filename;
158 }
159 for (path = getenv("PATH"); path && *path; path += m) {
160 if (strchr(path, ':')) {
161 n = strchr(path, ':') - path;
162 m = n + 1;
163 } else {
164 m = n = strlen(path);
165 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100166 if (n + strlen(filename) + 1 >= PATH_MAX) {
167 fprintf(stderr, "Error: filename too long\n");
168 exit(1);
169 }
170 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 if (n && pathname[n - 1] != '/') {
172 pathname[n++] = '/';
173 }
174 strcpy(pathname + n, filename);
175 if (!access(pathname, X_OK)) {
176 return pathname;
177 }
178 }
179 return filename;
180}
181
Juan Cespedesce377d52008-12-16 19:38:10 +0100182static void
183guess_cols(void) {
184 struct winsize ws;
185 char *c;
186
187 options.align = DEFAULT_ALIGN;
188 c = getenv("COLUMNS");
189 if (c && *c) {
190 char *endptr;
191 int cols;
192 cols = strtol(c, &endptr, 0);
193 if (cols > 0 && !*endptr) {
194 options.align = cols * 5 / 8;
195 }
196 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
197 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200198 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
199 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100200 }
201}
202
Juan Cespedesf1350522008-12-16 18:19:58 +0100203char **
204process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200205 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100206 options.output = stderr;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207
Juan Cespedesce377d52008-12-16 19:38:10 +0100208 guess_cols();
209
Juan Cespedesac3db291998-04-25 14:31:58 +0200210#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100211 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200212 int c;
213#if HAVE_GETOPT_LONG
214 int option_index = 0;
215 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200217 {"config", 1, 0, 'F'},
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100219# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200221#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 {"indent", 1, 0, 'n'},
223 {"help", 0, 0, 'h'},
224 {"library", 1, 0, 'l'},
225 {"output", 1, 0, 'o'},
226 {"version", 0, 0, 'V'},
227 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200228 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100229 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100230# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200232# endif
Steve Fink1150bc42006-08-07 06:04:43 +0200233 "a:A:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100234 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200235#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100236 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100237# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200238 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200239# endif
Juan Cespedesaee09312007-08-31 18:49:48 +0200240 "a:A:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200241#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100242 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200243 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100244 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 switch (c) {
246 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100247 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200249 case 'A':
Steve Fink1150bc42006-08-07 06:04:43 +0200250 opt_A = atoi(optarg);
251 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100252 case 'c':
253 opt_c++;
254 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100255#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100256 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100257 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200259#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260 case 'd':
261 opt_d++;
262 break;
263 case 'e':
264 {
265 char *str_e = strdup(optarg);
266 if (!str_e) {
267 perror("ltrace: strdup");
268 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100269 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100270 if (str_e[0] == '!') {
271 opt_e_enable = 0;
272 str_e++;
273 }
274 while (*str_e) {
275 struct opt_e_t *tmp;
276 char *str2 = strchr(str_e, ',');
277 if (str2) {
278 *str2 = '\0';
279 }
280 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100281 if (!tmp) {
282 perror("ltrace: malloc");
283 exit(1);
284 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 tmp->name = str_e;
286 tmp->next = opt_e;
287 opt_e = tmp;
288 if (str2) {
289 str_e = str2 + 1;
290 } else {
291 break;
292 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100293 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 break;
295 }
296 case 'f':
297 opt_f = 1;
298 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200299 case 'F':
300 {
301 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
302 if (!tmp) {
303 perror("ltrace: malloc");
304 exit(1);
305 }
306 tmp->filename = strdup(optarg);
307 tmp->next = opt_F;
308 opt_F = tmp;
309 break;
310 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 case 'h':
312 usage();
313 exit(0);
314 case 'i':
315 opt_i++;
316 break;
317 case 'l':
318 if (library_num == MAX_LIBRARY) {
319 fprintf(stderr,
320 "Too many libraries. Maximum is %i.\n",
321 MAX_LIBRARY);
322 exit(1);
323 }
324 library[library_num++] = optarg;
325 break;
326 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100327 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 break;
329 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100330 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 break;
332 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100333 options.output = fopen(optarg, "w");
334 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 fprintf(stderr,
336 "Can't open %s for output: %s\n",
337 optarg, strerror(errno));
338 exit(1);
339 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100340 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
341 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 break;
343 case 'p':
344 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200345 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 if (!tmp) {
347 perror("ltrace: malloc");
348 exit(1);
349 }
350 tmp->pid = atoi(optarg);
351 tmp->next = opt_p;
352 opt_p = tmp;
353 break;
354 }
355 case 'r':
356 opt_r++;
357 break;
358 case 's':
359 opt_s = atoi(optarg);
360 break;
361 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100362 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100363 break;
364 case 't':
365 opt_t++;
366 break;
367 case 'T':
368 opt_T++;
369 break;
370 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100371 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 break;
373 case 'V':
374 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200375 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200376 "This is free software; see the GNU General Public Licence\n"
377 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100378 exit(0);
379 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200380#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200382#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200383 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200384 "architecture: assuming you meant \"-x\"\n");
385#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200387
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 case 'x':
389 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100390 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100391
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 /* First, check for duplicate. */
393 while (p && strcmp(p->name, optarg)) {
394 p = p->next;
395 }
396 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100397 break;
398 }
399
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100401 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100402 if (!p) {
403 perror("ltrace: malloc");
404 exit(1);
405 }
406 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100407 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 p->next = opt_x;
409 opt_x = p;
410 break;
411 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100412
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100413 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200414#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100415 fprintf(stderr,
416 "Try `%s --help' for more information\n",
417 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200418#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 fprintf(stderr, "Try `%s -h' for more information\n",
420 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200421#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100423 }
424 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100425 argc -= optind;
426 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200427#endif
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 Cespedes5c3fe062004-06-14 18:08:37 +0200454 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100455 exit(1);
456 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200457 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100458 fprintf(stderr, "%s: Incompatible options -r and -t\n",
459 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200460 exit(1);
461 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100462 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200463 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100464 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200465 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100466}