blob: 1d402d072d3ba86d5ff4ad52048463f09e867364 [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
34 .demangle = 0, /* Demangle low-level symbol names */
35#endif
36};
37
Juan Cespedes1cd999a2001-07-03 00:46:04 +020038#define MAX_LIBRARY 30
39char *library[MAX_LIBRARY];
40int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020041static char *progname; /* Program name (`ltrace') */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042FILE *output;
Juan Cespedesaee09312007-08-31 18:49:48 +020043int opt_A = DEFAULT_ARRAYLEN; /* maximum # array elements to print */
44int opt_c = 0; /* Report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010045int opt_d = 0; /* debug */
46int opt_i = 0; /* instruction pointer */
Juan Cespedesaee09312007-08-31 18:49:48 +020047int opt_s = DEFAULT_STRLEN; /* maximum # of bytes printed in strings */
Juan Cespedes5e01f651998-03-08 22:31:44 +010048int opt_f = 0; /* trace child processes as they are created */
Juan Cespedesf666d191998-09-20 23:04:34 +020049int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020050int opt_t = 0; /* print absolute timestamp */
Juan Cespedesaee09312007-08-31 18:49:48 +020051int opt_n = 0; /* indent output according to program flow */
Juan Cespedesd65efa32003-02-03 00:22:30 +010052int opt_T = 0; /* show the time spent inside each call */
Juan Cespedesaee09312007-08-31 18:49:48 +020053int opt_o = 0; /* output to a specific file */
Juan Cespedes5e01f651998-03-08 22:31:44 +010054
55/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010057
Juan Cespedesac3db291998-04-25 14:31:58 +020058/* List of function names given to option -e: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010059struct opt_e_t *opt_e = NULL;
60int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020061
Ian Wienand9a2ad352006-02-20 22:44:45 +010062/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010063struct opt_x_t *opt_x = NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010064
Steve Fink58c73a72006-07-17 23:18:35 +020065/* List of filenames give to option -F: */
66struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
67
Paul Gilliambe320772006-04-24 22:06:23 +020068#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010069/* Set a break on the routine named here in order to re-initialize breakpoints
70 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020071char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
72#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010073
Juan Cespedesf1350522008-12-16 18:19:58 +010074static void
75usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020076#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
77 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020079#else
80 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020082# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020084# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020086# endif
Juan Cespedesaee09312007-08-31 18:49:48 +020087 " -A ARRAYLEN maximum number of array elements to print.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010089# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020090# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020092# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020094# endif
95# endif
96# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020098# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200100# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -e expr modify which events to trace.\n"
102 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100103# if HAVE_GETOPT_LONG
Juan Cespedesce377d52008-12-16 19:38:10 +0100104 " -F, --config=FILE load alternate configuration file (can be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200105# else
Juan Cespedesce377d52008-12-16 19:38:10 +0100106 " -F FILE load alternate configuration file (can be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200107# endif
Steve Fink58c73a72006-07-17 23:18:35 +0200108# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100110# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100112# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100114# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100116# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100118# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200120# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100121 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200122# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200124# endif
125# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200127# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200129# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100130 " -p PID attach to the process with the process ID pid.\n"
131 " -r print relative timestamps.\n"
132 " -s STRLEN specify the maximum string size to print.\n"
133 " -S display system calls.\n"
134 " -t, -tt, -ttt print absolute timestamps.\n"
135 " -T show the time spent inside each call.\n"
136 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200137# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200139# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200141# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100142 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200143#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100144 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200145#endif
146 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200148#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100149}
150
Juan Cespedesf1350522008-12-16 18:19:58 +0100151static char *
152search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100153 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100154 char *path;
155 int m, n;
156
157 if (strchr(filename, '/')) {
158 return filename;
159 }
160 for (path = getenv("PATH"); path && *path; path += m) {
161 if (strchr(path, ':')) {
162 n = strchr(path, ':') - path;
163 m = n + 1;
164 } else {
165 m = n = strlen(path);
166 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100167 if (n + strlen(filename) + 1 >= PATH_MAX) {
168 fprintf(stderr, "Error: filename too long\n");
169 exit(1);
170 }
171 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100172 if (n && pathname[n - 1] != '/') {
173 pathname[n++] = '/';
174 }
175 strcpy(pathname + n, filename);
176 if (!access(pathname, X_OK)) {
177 return pathname;
178 }
179 }
180 return filename;
181}
182
Juan Cespedesce377d52008-12-16 19:38:10 +0100183static void
184guess_cols(void) {
185 struct winsize ws;
186 char *c;
187
188 options.align = DEFAULT_ALIGN;
189 c = getenv("COLUMNS");
190 if (c && *c) {
191 char *endptr;
192 int cols;
193 cols = strtol(c, &endptr, 0);
194 if (cols > 0 && !*endptr) {
195 options.align = cols * 5 / 8;
196 }
197 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
198 options.align = ws.ws_col * 5 / 8;
199 }
200}
201
Juan Cespedesf1350522008-12-16 18:19:58 +0100202char **
203process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200204 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205 output = stderr;
206
Juan Cespedesce377d52008-12-16 19:38:10 +0100207 guess_cols();
208
Juan Cespedesac3db291998-04-25 14:31:58 +0200209#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200211 int c;
212#if HAVE_GETOPT_LONG
213 int option_index = 0;
214 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200216 {"config", 1, 0, 'F'},
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100218# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200220#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 {"indent", 1, 0, 'n'},
222 {"help", 0, 0, 'h'},
223 {"library", 1, 0, 'l'},
224 {"output", 1, 0, 'o'},
225 {"version", 0, 0, 'V'},
226 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200227 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100228 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100229# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200231# endif
Steve Fink1150bc42006-08-07 06:04:43 +0200232 "a:A:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200234#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100235 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100236# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200237 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200238# endif
Juan Cespedesaee09312007-08-31 18:49:48 +0200239 "a:A:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200240#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100241 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200242 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100243 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100244 switch (c) {
245 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100246 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200248 case 'A':
Steve Fink1150bc42006-08-07 06:04:43 +0200249 opt_A = atoi(optarg);
250 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 case 'c':
252 opt_c++;
253 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100254#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100256 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200258#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 case 'd':
260 opt_d++;
261 break;
262 case 'e':
263 {
264 char *str_e = strdup(optarg);
265 if (!str_e) {
266 perror("ltrace: strdup");
267 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100268 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 if (str_e[0] == '!') {
270 opt_e_enable = 0;
271 str_e++;
272 }
273 while (*str_e) {
274 struct opt_e_t *tmp;
275 char *str2 = strchr(str_e, ',');
276 if (str2) {
277 *str2 = '\0';
278 }
279 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100280 if (!tmp) {
281 perror("ltrace: malloc");
282 exit(1);
283 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 tmp->name = str_e;
285 tmp->next = opt_e;
286 opt_e = tmp;
287 if (str2) {
288 str_e = str2 + 1;
289 } else {
290 break;
291 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100292 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 break;
294 }
295 case 'f':
296 opt_f = 1;
297 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200298 case 'F':
299 {
300 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
301 if (!tmp) {
302 perror("ltrace: malloc");
303 exit(1);
304 }
305 tmp->filename = strdup(optarg);
306 tmp->next = opt_F;
307 opt_F = tmp;
308 break;
309 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100310 case 'h':
311 usage();
312 exit(0);
313 case 'i':
314 opt_i++;
315 break;
316 case 'l':
317 if (library_num == MAX_LIBRARY) {
318 fprintf(stderr,
319 "Too many libraries. Maximum is %i.\n",
320 MAX_LIBRARY);
321 exit(1);
322 }
323 library[library_num++] = optarg;
324 break;
325 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100326 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100327 break;
328 case 'n':
329 opt_n = atoi(optarg);
330 break;
331 case 'o':
Juan Cespedesaee09312007-08-31 18:49:48 +0200332 opt_o++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100333 output = fopen(optarg, "w");
334 if (!output) {
335 fprintf(stderr,
336 "Can't open %s for output: %s\n",
337 optarg, strerror(errno));
338 exit(1);
339 }
340 setvbuf(output, (char *)NULL, _IOLBF, 0);
341 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
342 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 Cespedes9c32e6f2008-12-10 11:33:38 +0100375 "Copyright (C) 1997-2008 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}