blob: 858957243a1fce95a42b30c4fc35187161d4bbc9 [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;
Ian Wienand9a2ad352006-02-20 22:44:45 +010056
Steve Fink58c73a72006-07-17 23:18:35 +020057/* List of filenames give to option -F: */
58struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
59
Paul Gilliambe320772006-04-24 22:06:23 +020060#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010061/* Set a break on the routine named here in order to re-initialize breakpoints
62 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020063char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
64#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010065
Juan Cespedesf1350522008-12-16 18:19:58 +010066static void
Juan Cespedesc5c744a2009-07-23 18:22:58 +020067err_usage(void) {
Juan Cespedesc5c744a2009-07-23 18:22:58 +020068 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +020069 exit(1);
70}
71
72static void
Juan Cespedesf1350522008-12-16 18:19:58 +010073usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020074 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 "Trace library calls of a given program.\n\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +020077 " -A ARRAYLEN maximum number of array elements to print.\n"
Joe Damato59e3fb12009-11-06 19:45:10 -080078 " -b, --no-signals don't print signals.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010080# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020082# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +020083 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesc5c744a2009-07-23 18:22:58 +020084 " -Dh, --debug=help show help on debugging.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020086 " -f trace children (fork() and clone()).\n"
Juan Cespedescc813cd2009-04-07 15:45:53 +020087 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Joe Damatofa2aefc2010-10-30 19:56:50 -070088 " -g, --no-plt disable breakpoints on PLT entries.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -h, --help display this help and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 " -i print instruction pointer at time of library call.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -l, --library=FILE print library calls from this library only.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 " -L do NOT display library calls.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 " -o, --output=FILE write the trace output to that file.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 " -p PID attach to the process with the process ID pid.\n"
96 " -r print relative timestamps.\n"
97 " -s STRLEN specify the maximum string size to print.\n"
98 " -S display system calls.\n"
99 " -t, -tt, -ttt print absolute timestamps.\n"
100 " -T show the time spent inside each call.\n"
101 " -u USERNAME run command with the userid, groupid of username.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 " -V, --version output version information and exit.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200104#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200106#endif
107 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 progname);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100109}
110
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200111static void
112usage_debug(void) {
113 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
114 fprintf(stdout,
115 "\n"
116 " number ref. in source description\n"
117 " 1 general Generally helpful progress information\n"
118 " 10 event Shows every event received by a traced process\n"
119 " 20 process Shows actions carried upon a traced processes\n"
120 " 40 function Shows every entry to internal functions\n"
121 "\n"
122 "Debugging options are mixed using bitwise-or.\n"
123 "Note that the meanings and values are subject to change.\n"
124 );
125}
126
Juan Cespedesf1350522008-12-16 18:19:58 +0100127static char *
128search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100129 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 char *path;
131 int m, n;
132
133 if (strchr(filename, '/')) {
134 return filename;
135 }
136 for (path = getenv("PATH"); path && *path; path += m) {
137 if (strchr(path, ':')) {
138 n = strchr(path, ':') - path;
139 m = n + 1;
140 } else {
141 m = n = strlen(path);
142 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100143 if (n + strlen(filename) + 1 >= PATH_MAX) {
144 fprintf(stderr, "Error: filename too long\n");
145 exit(1);
146 }
147 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148 if (n && pathname[n - 1] != '/') {
149 pathname[n++] = '/';
150 }
151 strcpy(pathname + n, filename);
152 if (!access(pathname, X_OK)) {
153 return pathname;
154 }
155 }
156 return filename;
157}
158
Juan Cespedesce377d52008-12-16 19:38:10 +0100159static void
160guess_cols(void) {
161 struct winsize ws;
162 char *c;
163
164 options.align = DEFAULT_ALIGN;
165 c = getenv("COLUMNS");
166 if (c && *c) {
167 char *endptr;
168 int cols;
169 cols = strtol(c, &endptr, 0);
170 if (cols > 0 && !*endptr) {
171 options.align = cols * 5 / 8;
172 }
173 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
174 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200175 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
176 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100177 }
178}
179
Juan Cespedesf1350522008-12-16 18:19:58 +0100180char **
181process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200182 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100183 options.output = stderr;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700184 options.no_plt = 0;
Joe Damato59e3fb12009-11-06 19:45:10 -0800185 options.no_signals = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186
Juan Cespedesce377d52008-12-16 19:38:10 +0100187 guess_cols();
188
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100189 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200190 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200191 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200192 int option_index = 0;
193 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200195 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200196 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100197# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200199#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 {"indent", 1, 0, 'n'},
201 {"help", 0, 0, 'h'},
202 {"library", 1, 0, 'l'},
203 {"output", 1, 0, 'o'},
204 {"version", 0, 0, 'V'},
Joe Damatofa2aefc2010-10-30 19:56:50 -0700205 {"no-plt", 0, 0, 'g'},
Joe Damato59e3fb12009-11-06 19:45:10 -0800206 {"no-signals", 0, 0, 'b'},
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200208 };
Joe Damato59e3fb12009-11-06 19:45:10 -0800209 c = getopt_long(argc, argv, "+cfhiLrStTVgb"
Juan Cespedesd914a202004-11-10 00:15:33 +0100210# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100211 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200212# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200213 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 &option_index);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200216 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 switch (c) {
219 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100220 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200222 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200223 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200224 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100225 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200226 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100228#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100230 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200232#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200233 case 'D':
234 if (optarg[0]=='h') {
235 usage_debug();
236 exit(0);
237 }
238 options.debug = strtoul(optarg,&p,8);
239 if (*p) {
240 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
241 err_usage();
242 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 break;
244 case 'e':
245 {
246 char *str_e = strdup(optarg);
247 if (!str_e) {
248 perror("ltrace: strdup");
249 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100250 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 if (str_e[0] == '!') {
252 opt_e_enable = 0;
253 str_e++;
254 }
255 while (*str_e) {
256 struct opt_e_t *tmp;
257 char *str2 = strchr(str_e, ',');
258 if (str2) {
259 *str2 = '\0';
260 }
261 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100262 if (!tmp) {
263 perror("ltrace: malloc");
264 exit(1);
265 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100266 tmp->name = str_e;
267 tmp->next = opt_e;
268 opt_e = tmp;
269 if (str2) {
270 str_e = str2 + 1;
271 } else {
272 break;
273 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100274 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 break;
276 }
277 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200278 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200280 case 'F':
281 {
282 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
283 if (!tmp) {
284 perror("ltrace: malloc");
285 exit(1);
286 }
287 tmp->filename = strdup(optarg);
288 tmp->next = opt_F;
289 opt_F = tmp;
290 break;
291 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700292 case 'g':
293 options.no_plt = 1;
294 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 case 'h':
296 usage();
297 exit(0);
298 case 'i':
299 opt_i++;
300 break;
301 case 'l':
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200302 if (library_num == MAX_LIBRARIES) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100303 fprintf(stderr,
304 "Too many libraries. Maximum is %i.\n",
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200305 MAX_LIBRARIES);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 exit(1);
307 }
308 library[library_num++] = optarg;
309 break;
310 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100311 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 break;
313 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100314 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100315 break;
316 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100317 options.output = fopen(optarg, "w");
318 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 fprintf(stderr,
320 "Can't open %s for output: %s\n",
321 optarg, strerror(errno));
322 exit(1);
323 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100324 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
325 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 break;
327 case 'p':
328 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200329 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 if (!tmp) {
331 perror("ltrace: malloc");
332 exit(1);
333 }
334 tmp->pid = atoi(optarg);
335 tmp->next = opt_p;
336 opt_p = tmp;
337 break;
338 }
339 case 'r':
340 opt_r++;
341 break;
342 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200343 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 break;
345 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100346 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 break;
348 case 't':
349 opt_t++;
350 break;
351 case 'T':
352 opt_T++;
353 break;
354 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100355 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100356 break;
357 case 'V':
358 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200359 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200360 "This is free software; see the GNU General Public Licence\n"
361 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 exit(0);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700363 break;
Joe Damato59e3fb12009-11-06 19:45:10 -0800364 case 'b':
365 options.no_signals = 1;
366 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200368#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200370#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200371 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200372 "architecture: assuming you meant \"-x\"\n");
373#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200375
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 case 'x':
377 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100378 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100379
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 /* First, check for duplicate. */
381 while (p && strcmp(p->name, optarg)) {
382 p = p->next;
383 }
384 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100385 break;
386 }
387
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100389 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 if (!p) {
391 perror("ltrace: malloc");
392 exit(1);
393 }
394 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100395 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100396 p->next = opt_x;
397 opt_x = p;
398 break;
399 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100400
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200402 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100403 }
404 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 argc -= optind;
406 argv += optind;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100407
Juan Cespedesaee09312007-08-31 18:49:48 +0200408 if (!opt_F) {
409 opt_F = malloc(sizeof(struct opt_F_t));
410 opt_F->next = malloc(sizeof(struct opt_F_t));
411 opt_F->next->next = NULL;
412 opt_F->filename = USER_CONFIG_FILE;
413 opt_F->next->filename = SYSTEM_CONFIG_FILE;
414 }
Steve Fink58c73a72006-07-17 23:18:35 +0200415 /* Reverse the config file list since it was built by
416 * prepending, and it would make more sense to process the
417 * files in the order they were given. Probably it would make
418 * more sense to keep a tail pointer instead? */
419 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200420 struct opt_F_t *egg = NULL;
421 struct opt_F_t *chicken;
422 while (opt_F) {
423 chicken = opt_F->next;
424 opt_F->next = egg;
425 egg = opt_F;
426 opt_F = chicken;
427 }
428 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200429 }
430
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100431 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200432 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200433 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100434 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200435 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 fprintf(stderr, "%s: Incompatible options -r and -t\n",
437 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200438 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200439 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100440 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200441 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100442 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200443 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100444}