blob: 34c7ae239d352b4636eb116e147a3eee49993b3d [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 Cespedesf7281232009-06-25 16:11:21 +020017#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Steve Fink58c73a72006-07-17 23:18:35 +020019#ifndef SYSCONFDIR
20#define SYSCONFDIR "/etc"
21#endif
22
Olaf Heringe948e582006-10-12 23:53:44 +020023#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020024#define USER_CONFIG_FILE "~/.ltrace.conf"
25
Juan Cespedesce377d52008-12-16 19:38:10 +010026struct options_t options = {
Juan Cespedesda9b9532009-04-07 15:33:50 +020027 .align = DEFAULT_ALIGN, /* alignment column for results */
28 .user = NULL, /* username to run command as */
29 .syscalls = 0, /* display syscalls */
30 .libcalls = 1, /* display library calls */
Juan Cespedesce377d52008-12-16 19:38:10 +010031#ifdef USE_DEMANGLE
Juan Cespedesda9b9532009-04-07 15:33:50 +020032 .demangle = 0, /* Demangle low-level symbol names */
Juan Cespedesce377d52008-12-16 19:38:10 +010033#endif
Juan Cespedesda9b9532009-04-07 15:33:50 +020034 .indent = 0, /* indent output according to program flow */
35 .output = NULL, /* output to a specific file */
Juan Cespedescc813cd2009-04-07 15:45:53 +020036 .summary = 0, /* Report a summary on program exit */
37 .debug = 0, /* debug */
38 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */
39 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */
40 .follow = 0, /* trace child processes */
Juan Cespedesce377d52008-12-16 19:38:10 +010041};
42
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020043char *library[MAX_LIBRARIES];
Juan Cespedes1cd999a2001-07-03 00:46:04 +020044int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020045static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010046int opt_i = 0; /* instruction pointer */
Juan Cespedesf666d191998-09-20 23:04:34 +020047int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020048int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd65efa32003-02-03 00:22:30 +010049int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010050
51/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010052struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010053
Juan Cespedesac3db291998-04-25 14:31:58 +020054/* List of function names given to option -e: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055struct opt_e_t *opt_e = NULL;
56int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020057
Ian Wienand9a2ad352006-02-20 22:44:45 +010058/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010059struct opt_x_t *opt_x = NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010060
Steve Fink58c73a72006-07-17 23:18:35 +020061/* List of filenames give to option -F: */
62struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
63
Paul Gilliambe320772006-04-24 22:06:23 +020064#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010065/* Set a break on the routine named here in order to re-initialize breakpoints
66 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020067char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
68#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010069
Juan Cespedesf1350522008-12-16 18:19:58 +010070static void
71usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020072#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
73 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020075#else
76 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020078# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020080# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020082# endif
Juan Cespedesaee09312007-08-31 18:49:48 +020083 " -A ARRAYLEN maximum number of array elements to print.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010085# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020086# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020088# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020090# endif
91# endif
92# if HAVE_GETOPT_LONG
Juan Cespedescd8976d2009-05-14 13:47:58 +020093 " -d, --debug=LEVEL print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020094# else
Juan Cespedescd8976d2009-05-14 13:47:58 +020095 " -d LEVEL print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020096# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +020098 " -f trace children (fork() and clone()).\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010099# if HAVE_GETOPT_LONG
Juan Cespedescc813cd2009-04-07 15:45:53 +0200100 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200101# else
Juan Cespedescc813cd2009-04-07 15:45:53 +0200102 " -F FILE load alternate configuration file (may be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200103# endif
Steve Fink58c73a72006-07-17 23:18:35 +0200104# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100106# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100108# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100110# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100112# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100114# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200116# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200118# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200120# endif
121# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200123# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200125# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 " -p PID attach to the process with the process ID pid.\n"
127 " -r print relative timestamps.\n"
128 " -s STRLEN specify the maximum string size to print.\n"
129 " -S display system calls.\n"
130 " -t, -tt, -ttt print absolute timestamps.\n"
131 " -T show the time spent inside each call.\n"
132 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200133# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200135# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100136 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200137# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200139#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200141#endif
142 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100143 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200144#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100145}
146
Juan Cespedesf1350522008-12-16 18:19:58 +0100147static char *
148search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100149 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100150 char *path;
151 int m, n;
152
153 if (strchr(filename, '/')) {
154 return filename;
155 }
156 for (path = getenv("PATH"); path && *path; path += m) {
157 if (strchr(path, ':')) {
158 n = strchr(path, ':') - path;
159 m = n + 1;
160 } else {
161 m = n = strlen(path);
162 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100163 if (n + strlen(filename) + 1 >= PATH_MAX) {
164 fprintf(stderr, "Error: filename too long\n");
165 exit(1);
166 }
167 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100168 if (n && pathname[n - 1] != '/') {
169 pathname[n++] = '/';
170 }
171 strcpy(pathname + n, filename);
172 if (!access(pathname, X_OK)) {
173 return pathname;
174 }
175 }
176 return filename;
177}
178
Juan Cespedesce377d52008-12-16 19:38:10 +0100179static void
180guess_cols(void) {
181 struct winsize ws;
182 char *c;
183
184 options.align = DEFAULT_ALIGN;
185 c = getenv("COLUMNS");
186 if (c && *c) {
187 char *endptr;
188 int cols;
189 cols = strtol(c, &endptr, 0);
190 if (cols > 0 && !*endptr) {
191 options.align = cols * 5 / 8;
192 }
193 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
194 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200195 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
196 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100197 }
198}
199
Juan Cespedesf1350522008-12-16 18:19:58 +0100200char **
201process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200202 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100203 options.output = stderr;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204
Juan Cespedesce377d52008-12-16 19:38:10 +0100205 guess_cols();
206
Juan Cespedesac3db291998-04-25 14:31:58 +0200207#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200209 int c;
210#if HAVE_GETOPT_LONG
211 int option_index = 0;
212 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200214 {"config", 1, 0, 'F'},
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200215 {"debug", 1, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100216# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200218#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 {"indent", 1, 0, 'n'},
220 {"help", 0, 0, 'h'},
221 {"library", 1, 0, 'l'},
222 {"output", 1, 0, 'o'},
223 {"version", 0, 0, 'V'},
224 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200225 };
Juan Cespedescd8976d2009-05-14 13:47:58 +0200226 c = getopt_long(argc, argv, "+cfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100227# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200229# endif
Juan Cespedescd8976d2009-05-14 13:47:58 +0200230 "a:A:d:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200232#else
Juan Cespedescd8976d2009-05-14 13:47:58 +0200233 c = getopt(argc, argv, "+cfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100234# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200235 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200236# endif
Juan Cespedescd8976d2009-05-14 13:47:58 +0200237 "a:A:d:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200238#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200240 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100241 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100242 switch (c) {
243 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100244 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200246 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200247 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200248 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100249 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200250 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100252#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100253 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100254 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200256#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 case 'd':
Juan Cespedescd8976d2009-05-14 13:47:58 +0200258 options.debug = strtoul(optarg,NULL,0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 break;
260 case 'e':
261 {
262 char *str_e = strdup(optarg);
263 if (!str_e) {
264 perror("ltrace: strdup");
265 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100266 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100267 if (str_e[0] == '!') {
268 opt_e_enable = 0;
269 str_e++;
270 }
271 while (*str_e) {
272 struct opt_e_t *tmp;
273 char *str2 = strchr(str_e, ',');
274 if (str2) {
275 *str2 = '\0';
276 }
277 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100278 if (!tmp) {
279 perror("ltrace: malloc");
280 exit(1);
281 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 tmp->name = str_e;
283 tmp->next = opt_e;
284 opt_e = tmp;
285 if (str2) {
286 str_e = str2 + 1;
287 } else {
288 break;
289 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100290 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 break;
292 }
293 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200294 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200296 case 'F':
297 {
298 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
299 if (!tmp) {
300 perror("ltrace: malloc");
301 exit(1);
302 }
303 tmp->filename = strdup(optarg);
304 tmp->next = opt_F;
305 opt_F = tmp;
306 break;
307 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 case 'h':
309 usage();
310 exit(0);
311 case 'i':
312 opt_i++;
313 break;
314 case 'l':
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200315 if (library_num == MAX_LIBRARIES) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 fprintf(stderr,
317 "Too many libraries. Maximum is %i.\n",
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200318 MAX_LIBRARIES);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 exit(1);
320 }
321 library[library_num++] = optarg;
322 break;
323 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100324 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 break;
326 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100327 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 break;
329 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100330 options.output = fopen(optarg, "w");
331 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100332 fprintf(stderr,
333 "Can't open %s for output: %s\n",
334 optarg, strerror(errno));
335 exit(1);
336 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100337 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
338 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 break;
340 case 'p':
341 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200342 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 if (!tmp) {
344 perror("ltrace: malloc");
345 exit(1);
346 }
347 tmp->pid = atoi(optarg);
348 tmp->next = opt_p;
349 opt_p = tmp;
350 break;
351 }
352 case 'r':
353 opt_r++;
354 break;
355 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200356 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 break;
358 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100359 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 break;
361 case 't':
362 opt_t++;
363 break;
364 case 'T':
365 opt_T++;
366 break;
367 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100368 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 break;
370 case 'V':
371 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200372 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200373 "This is free software; see the GNU General Public Licence\n"
374 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 exit(0);
376 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200377#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100378 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200379#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200380 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200381 "architecture: assuming you meant \"-x\"\n");
382#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200384
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100385 case 'x':
386 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100387 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100388
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 /* First, check for duplicate. */
390 while (p && strcmp(p->name, optarg)) {
391 p = p->next;
392 }
393 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100394 break;
395 }
396
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100397 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100398 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399 if (!p) {
400 perror("ltrace: malloc");
401 exit(1);
402 }
403 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100404 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 p->next = opt_x;
406 opt_x = p;
407 break;
408 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100409
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100410 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200411#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 fprintf(stderr,
413 "Try `%s --help' for more information\n",
414 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200415#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 fprintf(stderr, "Try `%s -h' for more information\n",
417 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200418#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100420 }
421 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422 argc -= optind;
423 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200424#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100425
Juan Cespedesaee09312007-08-31 18:49:48 +0200426 if (!opt_F) {
427 opt_F = malloc(sizeof(struct opt_F_t));
428 opt_F->next = malloc(sizeof(struct opt_F_t));
429 opt_F->next->next = NULL;
430 opt_F->filename = USER_CONFIG_FILE;
431 opt_F->next->filename = SYSTEM_CONFIG_FILE;
432 }
Steve Fink58c73a72006-07-17 23:18:35 +0200433 /* Reverse the config file list since it was built by
434 * prepending, and it would make more sense to process the
435 * files in the order they were given. Probably it would make
436 * more sense to keep a tail pointer instead? */
437 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200438 struct opt_F_t *egg = NULL;
439 struct opt_F_t *chicken;
440 while (opt_F) {
441 chicken = opt_F->next;
442 opt_F->next = egg;
443 egg = opt_F;
444 opt_F = chicken;
445 }
446 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200447 }
448
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100449 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200450 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200451 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100452 exit(1);
453 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200454 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 fprintf(stderr, "%s: Incompatible options -r and -t\n",
456 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200457 exit(1);
458 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200460 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100461 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200462 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100463}