blob: fe2bb4cec6b332ad74ac9a9e0a389a2866a83c5c [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
Juan Cespedesc5c744a2009-07-23 18:22:58 +020071err_usage(void) {
72#if HAVE_GETOPT_LONG
73 fprintf(stderr, "Try `%s --help' for more information\n", progname);
74#else
75 fprintf(stderr, "Try `%s -h' for more information\n", progname);
76#endif
77 exit(1);
78}
79
80static void
Juan Cespedesf1350522008-12-16 18:19:58 +010081usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020082#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
83 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020085#else
86 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020088# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020090# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020092# endif
Juan Cespedesaee09312007-08-31 18:49:48 +020093 " -A ARRAYLEN maximum number of array elements to print.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010095# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020096# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020098# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200100# endif
101# endif
102# if HAVE_GETOPT_LONG
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200103 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200104# else
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200105 " -d LEVEL enable debugging (see -Dh).\n"
106# endif
107# if HAVE_GETOPT_LONG
108 " -Dh, --debug=help show help on debugging.\n"
109# else
110 " -Dh show help on debugging.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200111# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 " -e expr modify which events to trace.\n"
Juan Cespedesc4e53a92009-05-06 20:36:42 +0200113 " -f trace children (fork() and clone()).\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100114# if HAVE_GETOPT_LONG
Juan Cespedescc813cd2009-04-07 15:45:53 +0200115 " -F, --config=FILE load alternate configuration file (may be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200116# else
Juan Cespedescc813cd2009-04-07 15:45:53 +0200117 " -F FILE load alternate configuration file (may be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200118# endif
Steve Fink58c73a72006-07-17 23:18:35 +0200119# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100121# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100123# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100125# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100127# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100129# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100130 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200131# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100132 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200133# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200135# endif
136# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200138# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200140# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 " -p PID attach to the process with the process ID pid.\n"
142 " -r print relative timestamps.\n"
143 " -s STRLEN specify the maximum string size to print.\n"
144 " -S display system calls.\n"
145 " -t, -tt, -ttt print absolute timestamps.\n"
146 " -T show the time spent inside each call.\n"
147 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200148# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100149 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200150# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200152# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100153 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200154#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100155 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200156#endif
157 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200159#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100160}
161
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200162static void
163usage_debug(void) {
164 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname);
165 fprintf(stdout,
166 "\n"
167 " number ref. in source description\n"
168 " 1 general Generally helpful progress information\n"
169 " 10 event Shows every event received by a traced process\n"
170 " 20 process Shows actions carried upon a traced processes\n"
171 " 40 function Shows every entry to internal functions\n"
172 "\n"
173 "Debugging options are mixed using bitwise-or.\n"
174 "Note that the meanings and values are subject to change.\n"
175 );
176}
177
Juan Cespedesf1350522008-12-16 18:19:58 +0100178static char *
179search_for_command(char *filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100180 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100181 char *path;
182 int m, n;
183
184 if (strchr(filename, '/')) {
185 return filename;
186 }
187 for (path = getenv("PATH"); path && *path; path += m) {
188 if (strchr(path, ':')) {
189 n = strchr(path, ':') - path;
190 m = n + 1;
191 } else {
192 m = n = strlen(path);
193 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100194 if (n + strlen(filename) + 1 >= PATH_MAX) {
195 fprintf(stderr, "Error: filename too long\n");
196 exit(1);
197 }
198 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 if (n && pathname[n - 1] != '/') {
200 pathname[n++] = '/';
201 }
202 strcpy(pathname + n, filename);
203 if (!access(pathname, X_OK)) {
204 return pathname;
205 }
206 }
207 return filename;
208}
209
Juan Cespedesce377d52008-12-16 19:38:10 +0100210static void
211guess_cols(void) {
212 struct winsize ws;
213 char *c;
214
215 options.align = DEFAULT_ALIGN;
216 c = getenv("COLUMNS");
217 if (c && *c) {
218 char *endptr;
219 int cols;
220 cols = strtol(c, &endptr, 0);
221 if (cols > 0 && !*endptr) {
222 options.align = cols * 5 / 8;
223 }
224 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
225 options.align = ws.ws_col * 5 / 8;
Juan Cespedes43739a62009-04-07 13:10:08 +0200226 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
227 options.align = ws.ws_col * 5 / 8;
Juan Cespedesce377d52008-12-16 19:38:10 +0100228 }
229}
230
Juan Cespedesf1350522008-12-16 18:19:58 +0100231char **
232process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200233 progname = argv[0];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100234 options.output = stderr;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100235
Juan Cespedesce377d52008-12-16 19:38:10 +0100236 guess_cols();
237
Juan Cespedesac3db291998-04-25 14:31:58 +0200238#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200240 int c;
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200241 char *p;
Juan Cespedesac3db291998-04-25 14:31:58 +0200242#if HAVE_GETOPT_LONG
243 int option_index = 0;
244 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200246 {"config", 1, 0, 'F'},
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200247 {"debug", 1, 0, 'D'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100248# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100249 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200250#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 {"indent", 1, 0, 'n'},
252 {"help", 0, 0, 'h'},
253 {"library", 1, 0, 'l'},
254 {"output", 1, 0, 'o'},
255 {"version", 0, 0, 'V'},
256 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200257 };
Juan Cespedescd8976d2009-05-14 13:47:58 +0200258 c = getopt_long(argc, argv, "+cfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100259# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200261# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200262 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100263 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200264#else
Juan Cespedescd8976d2009-05-14 13:47:58 +0200265 c = getopt(argc, argv, "+cfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100266# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200267 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200268# endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200269 "a:A:D:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200270#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100271 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200272 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100273 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100274 switch (c) {
275 case 'a':
Juan Cespedesce377d52008-12-16 19:38:10 +0100276 options.align = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200278 case 'A':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200279 options.arraylen = atoi(optarg);
Steve Fink1150bc42006-08-07 06:04:43 +0200280 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 case 'c':
Juan Cespedesda9b9532009-04-07 15:33:50 +0200282 options.summary++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100284#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 case 'C':
Juan Cespedesce377d52008-12-16 19:38:10 +0100286 options.demangle++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200288#endif
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200289 case 'D':
290 if (optarg[0]=='h') {
291 usage_debug();
292 exit(0);
293 }
294 options.debug = strtoul(optarg,&p,8);
295 if (*p) {
296 fprintf(stderr, "%s: --debug requires an octal argument\n", progname);
297 err_usage();
298 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 break;
300 case 'e':
301 {
302 char *str_e = strdup(optarg);
303 if (!str_e) {
304 perror("ltrace: strdup");
305 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100306 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100307 if (str_e[0] == '!') {
308 opt_e_enable = 0;
309 str_e++;
310 }
311 while (*str_e) {
312 struct opt_e_t *tmp;
313 char *str2 = strchr(str_e, ',');
314 if (str2) {
315 *str2 = '\0';
316 }
317 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100318 if (!tmp) {
319 perror("ltrace: malloc");
320 exit(1);
321 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100322 tmp->name = str_e;
323 tmp->next = opt_e;
324 opt_e = tmp;
325 if (str2) {
326 str_e = str2 + 1;
327 } else {
328 break;
329 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100330 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 break;
332 }
333 case 'f':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200334 options.follow = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200336 case 'F':
337 {
338 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
339 if (!tmp) {
340 perror("ltrace: malloc");
341 exit(1);
342 }
343 tmp->filename = strdup(optarg);
344 tmp->next = opt_F;
345 opt_F = tmp;
346 break;
347 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 case 'h':
349 usage();
350 exit(0);
351 case 'i':
352 opt_i++;
353 break;
354 case 'l':
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200355 if (library_num == MAX_LIBRARIES) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100356 fprintf(stderr,
357 "Too many libraries. Maximum is %i.\n",
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200358 MAX_LIBRARIES);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100359 exit(1);
360 }
361 library[library_num++] = optarg;
362 break;
363 case 'L':
Juan Cespedesce377d52008-12-16 19:38:10 +0100364 options.libcalls = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 break;
366 case 'n':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100367 options.indent = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 break;
369 case 'o':
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100370 options.output = fopen(optarg, "w");
371 if (!options.output) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 fprintf(stderr,
373 "Can't open %s for output: %s\n",
374 optarg, strerror(errno));
375 exit(1);
376 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100377 setvbuf(options.output, (char *)NULL, _IOLBF, 0);
378 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 break;
380 case 'p':
381 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200382 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 if (!tmp) {
384 perror("ltrace: malloc");
385 exit(1);
386 }
387 tmp->pid = atoi(optarg);
388 tmp->next = opt_p;
389 opt_p = tmp;
390 break;
391 }
392 case 'r':
393 opt_r++;
394 break;
395 case 's':
Juan Cespedescc813cd2009-04-07 15:45:53 +0200396 options.strlen = atoi(optarg);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100397 break;
398 case 'S':
Juan Cespedesce377d52008-12-16 19:38:10 +0100399 options.syscalls = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 break;
401 case 't':
402 opt_t++;
403 break;
404 case 'T':
405 opt_T++;
406 break;
407 case 'u':
Juan Cespedesce377d52008-12-16 19:38:10 +0100408 options.user = optarg;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100409 break;
410 case 'V':
411 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedes1423f082009-04-07 00:45:33 +0200412 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200413 "This is free software; see the GNU General Public Licence\n"
414 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100415 exit(0);
416 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200417#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200419#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200420 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200421 "architecture: assuming you meant \"-x\"\n");
422#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200424
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100425 case 'x':
426 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100427 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100428
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100429 /* First, check for duplicate. */
430 while (p && strcmp(p->name, optarg)) {
431 p = p->next;
432 }
433 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100434 break;
435 }
436
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100438 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 if (!p) {
440 perror("ltrace: malloc");
441 exit(1);
442 }
443 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100444 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100445 p->next = opt_x;
446 opt_x = p;
447 break;
448 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100449
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100450 default:
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200451 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100452 }
453 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 argc -= optind;
455 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200456#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100457
Juan Cespedesaee09312007-08-31 18:49:48 +0200458 if (!opt_F) {
459 opt_F = malloc(sizeof(struct opt_F_t));
460 opt_F->next = malloc(sizeof(struct opt_F_t));
461 opt_F->next->next = NULL;
462 opt_F->filename = USER_CONFIG_FILE;
463 opt_F->next->filename = SYSTEM_CONFIG_FILE;
464 }
Steve Fink58c73a72006-07-17 23:18:35 +0200465 /* Reverse the config file list since it was built by
466 * prepending, and it would make more sense to process the
467 * files in the order they were given. Probably it would make
468 * more sense to keep a tail pointer instead? */
469 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200470 struct opt_F_t *egg = NULL;
471 struct opt_F_t *chicken;
472 while (opt_F) {
473 chicken = opt_F->next;
474 opt_F->next = egg;
475 egg = opt_F;
476 opt_F = chicken;
477 }
478 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200479 }
480
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100481 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200482 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200483 err_usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100484 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200485 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100486 fprintf(stderr, "%s: Incompatible options -r and -t\n",
487 progname);
Juan Cespedesc5c744a2009-07-23 18:22:58 +0200488 err_usage();
Juan Cespedesf666d191998-09-20 23:04:34 +0200489 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100490 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200491 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100492 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200493 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100494}