blob: f230d1b2158a5d1ed2c814c5126476c082f56487 [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;
198 }
199}
200
Juan Cespedesf1350522008-12-16 18:19:58 +0100201char **
202process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200203 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100204 options.output = stderr;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205
Juan Cespedesce377d52008-12-16 19:38:10 +0100206 guess_cols();
207
Juan Cespedesac3db291998-04-25 14:31:58 +0200208#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200210 int c;
211#if HAVE_GETOPT_LONG
212 int option_index = 0;
213 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200215 {"config", 1, 0, 'F'},
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100217# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200219#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 {"indent", 1, 0, 'n'},
221 {"help", 0, 0, 'h'},
222 {"library", 1, 0, 'l'},
223 {"output", 1, 0, 'o'},
224 {"version", 0, 0, 'V'},
225 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200226 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100227 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100228# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200230# endif
Steve Fink1150bc42006-08-07 06:04:43 +0200231 "a:A:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200233#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100234 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100235# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200236 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200237# endif
Juan Cespedesaee09312007-08-31 18:49:48 +0200238 "a:A:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200239#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100240 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200241 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100242 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 switch (c) {
244 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100245 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100246 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200247 case 'A':
Steve Fink1150bc42006-08-07 06:04:43 +0200248 opt_A = atoi(optarg);
249 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 case 'c':
251 opt_c++;
252 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100253#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100255 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100256 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200257#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 case 'd':
259 opt_d++;
260 break;
261 case 'e':
262 {
263 char *str_e = strdup(optarg);
264 if (!str_e) {
265 perror("ltrace: strdup");
266 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100267 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 if (str_e[0] == '!') {
269 opt_e_enable = 0;
270 str_e++;
271 }
272 while (*str_e) {
273 struct opt_e_t *tmp;
274 char *str2 = strchr(str_e, ',');
275 if (str2) {
276 *str2 = '\0';
277 }
278 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100279 if (!tmp) {
280 perror("ltrace: malloc");
281 exit(1);
282 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 tmp->name = str_e;
284 tmp->next = opt_e;
285 opt_e = tmp;
286 if (str2) {
287 str_e = str2 + 1;
288 } else {
289 break;
290 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100291 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100292 break;
293 }
294 case 'f':
295 opt_f = 1;
296 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200297 case 'F':
298 {
299 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
300 if (!tmp) {
301 perror("ltrace: malloc");
302 exit(1);
303 }
304 tmp->filename = strdup(optarg);
305 tmp->next = opt_F;
306 opt_F = tmp;
307 break;
308 }
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':
316 if (library_num == MAX_LIBRARY) {
317 fprintf(stderr,
318 "Too many libraries. Maximum is %i.\n",
319 MAX_LIBRARY);
320 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;
330 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100331 options.output = fopen(optarg, "w");
332 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100333 fprintf(stderr,
334 "Can't open %s for output: %s\n",
335 optarg, strerror(errno));
336 exit(1);
337 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100338 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
339 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100340 break;
341 case 'p':
342 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200343 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 if (!tmp) {
345 perror("ltrace: malloc");
346 exit(1);
347 }
348 tmp->pid = atoi(optarg);
349 tmp->next = opt_p;
350 opt_p = tmp;
351 break;
352 }
353 case 'r':
354 opt_r++;
355 break;
356 case 's':
357 opt_s = atoi(optarg);
358 break;
359 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100360 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 break;
362 case 't':
363 opt_t++;
364 break;
365 case 'T':
366 opt_T++;
367 break;
368 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100369 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 break;
371 case 'V':
372 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200373 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200374 "This is free software; see the GNU General Public Licence\n"
375 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 exit(0);
377 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200378#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200380#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200381 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200382 "architecture: assuming you meant \"-x\"\n");
383#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200385
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 case 'x':
387 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100388 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100389
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 /* First, check for duplicate. */
391 while (p && strcmp(p->name, optarg)) {
392 p = p->next;
393 }
394 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100395 break;
396 }
397
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100398 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100399 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 if (!p) {
401 perror("ltrace: malloc");
402 exit(1);
403 }
404 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100405 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 p->next = opt_x;
407 opt_x = p;
408 break;
409 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100410
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100411 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200412#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100413 fprintf(stderr,
414 "Try `%s --help' for more information\n",
415 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200416#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 fprintf(stderr, "Try `%s -h' for more information\n",
418 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200419#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100420 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100421 }
422 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 argc -= optind;
424 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200425#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100426
Juan Cespedesaee09312007-08-31 18:49:48 +0200427 if (!opt_F) {
428 opt_F = malloc(sizeof(struct opt_F_t));
429 opt_F->next = malloc(sizeof(struct opt_F_t));
430 opt_F->next->next = NULL;
431 opt_F->filename = USER_CONFIG_FILE;
432 opt_F->next->filename = SYSTEM_CONFIG_FILE;
433 }
Steve Fink58c73a72006-07-17 23:18:35 +0200434 /* Reverse the config file list since it was built by
435 * prepending, and it would make more sense to process the
436 * files in the order they were given. Probably it would make
437 * more sense to keep a tail pointer instead? */
438 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200439 struct opt_F_t *egg = NULL;
440 struct opt_F_t *chicken;
441 while (opt_F) {
442 chicken = opt_F->next;
443 opt_F->next = egg;
444 egg = opt_F;
445 opt_F = chicken;
446 }
447 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200448 }
449
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100450 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200451 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200452 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100453 exit(1);
454 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200455 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 fprintf(stderr, "%s: Incompatible options -r and -t\n",
457 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200458 exit(1);
459 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100460 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200461 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100462 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200463 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100464}