options.c revision 1cd999a32728f41208dc30f38a125d7bb7063625
1#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#ifndef VERSION
6# define VERSION "???"
7#endif
8
9#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <fcntl.h>
13#include <errno.h>
14#include <limits.h>
15
16#if HAVE_GETOPT_H
17#include <getopt.h>
18#endif
19
20#include "ltrace.h"
21#include "options.h"
22#include "defs.h"
23
24#define MAX_LIBRARY		30
25char *library[MAX_LIBRARY];
26int library_num = 0;
27static char *progname;		/* Program name (`ltrace') */
28FILE * output;
29int opt_a = DEFAULT_ACOLUMN;	/* default alignment column for results */
30int opt_d = 0;			/* debug */
31int opt_i = 0;			/* instruction pointer */
32int opt_s = DEFAULT_STRLEN;	/* default maximum # of bytes printed in strings */
33int opt_S = 0;			/* display syscalls */
34int opt_L = 1;			/* display library calls */
35int opt_f = 0;			/* trace child processes as they are created */
36char * opt_u = NULL;		/* username to run command as */
37int opt_r = 0;			/* print relative timestamp */
38int opt_t = 0;			/* print absolute timestamp */
39#if HAVE_LIBIBERTY
40int opt_C = 0;			/* Demangle low-level symbol names into user-level names */
41#endif
42int opt_n = 0;			/* indent trace output according to program flow */
43
44/* List of pids given to option -p: */
45struct opt_p_t * opt_p = NULL;	/* attach to process with a given pid */
46
47/* List of function names given to option -e: */
48struct opt_e_t * opt_e = NULL;
49int opt_e_enable=1;
50
51static void usage(void)
52{
53#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
54	fprintf(stdout, "Usage: %s [command [arg ...]]\n"
55"Trace library calls of a given program.\n\n", progname);
56#else
57	fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
58"Trace library calls of a given program.\n\n"
59
60# if HAVE_GETOPT_LONG
61"  -d, --debug         print debugging info.\n"
62# else
63"  -d                  print debugging info.\n"
64# endif
65"  -f                  follow forks.\n"
66"  -i                  print instruction pointer at time of library call.\n"
67"  -L                  do NOT display library calls.\n"
68"  -S                  display system calls.\n"
69"  -r                  print relative timestamps.\n"
70"  -t, -tt, -ttt       print absolute timestamps.\n"
71#  if HAVE_GETOPT_LONG
72"  -l, --library=FILE  print library calls from this library only.\n"
73#  else
74"  -l FILE             print library calls from this library only.\n"
75#  endif
76# if HAVE_LIBIBERTY
77#  if HAVE_GETOPT_LONG
78"  -C, --demangle      decode low-level symbol names into user-level names.\n"
79#  else
80"  -C                  decode low-level symbol names into user-level names.\n"
81#  endif
82# endif
83# if HAVE_GETOPT_LONG
84"  -a, --align=COLUMN  align return values in a secific column.\n"
85# else
86"  -a COLUMN           align return values in a secific column.\n"
87# endif
88"  -s STRLEN           specify the maximum string size to print.\n"
89# if HAVE_GETOPT_LONG
90"  -o, --output=FILE   write the trace output to that file.\n"
91# else
92"  -o FILE             write the trace output to that file.\n"
93# endif
94"  -u USERNAME         run command with the userid, groupid of username.\n"
95"  -p PID              attach to the process with the process ID pid.\n"
96"  -e expr             modify which events to trace.\n"
97# if HAVE_GETOPT_LONG
98"  -n, --indent=NR     indent output by NR spaces for each call level nesting.\n"
99# else
100"  -n NR               indent output by NR spaces for each call level nesting.\n"
101# endif
102# if HAVE_GETOPT_LONG
103"  -h, --help          display this help and exit.\n"
104# else
105"  -h                  display this help and exit.\n"
106# endif
107# if HAVE_GETOPT_LONG
108"  -V, --version       output version information and exit.\n"
109# else
110"  -V                  output version information and exit.\n"
111# endif
112"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
113		, progname);
114#endif
115}
116
117static char * search_for_command(char * filename)
118{
119	static char pathname[1024];
120	char *path;
121	int m, n;
122
123	if (strchr(filename, '/')) {
124		return filename;
125	}
126	for (path = getenv("PATH"); path && *path; path += m) {
127		if (strchr(path, ':')) {
128			n = strchr(path, ':') - path;
129			m = n + 1;
130		} else {
131			m = n = strlen(path);
132		}
133		strncpy(pathname, path, n);     /* Possible buffer overrun */
134		if (n && pathname[n - 1] != '/') {
135			pathname[n++] = '/';
136		}
137		strcpy(pathname + n, filename);
138		if (!access(pathname, X_OK)) {
139			return pathname;
140		}
141	}
142	return filename;
143}
144
145char ** process_options(int argc, char **argv)
146{
147	progname = argv[0];
148	output = stderr;
149
150#if HAVE_GETOPT || HAVE_GETOPT_LONG
151	while(1) {
152		int c;
153#if HAVE_GETOPT_LONG
154		int option_index = 0;
155		static struct option long_options[] = {
156			{ "align", 1, 0, 'a'},
157			{ "debug", 0, 0, 'd'},
158# if HAVE_LIBIBERTY
159			{ "demangle", 0, 0, 'C'},
160#endif
161			{ "indent", 0, 0, 'n'},
162			{ "library", 0, 0, 'l'},
163			{ "help", 0, 0, 'h'},
164			{ "output", 1, 0, 'o'},
165			{ "version", 0, 0, 'V'},
166			{ 0, 0, 0, 0}
167		};
168		c = getopt_long(argc, argv, "+dfiLSrthV"
169# if HAVE_LIBIBERTY
170			"C"
171# endif
172			"a:s:o:u:p:e:n:l:", long_options, &option_index);
173#else
174		c = getopt(argc, argv, "+dfiLSrthV"
175# if HAVE_LIBIBERTY
176			"C"
177# endif
178			"a:s:o:u:p:e:n:l:");
179#endif
180		if (c==-1) {
181			break;
182		}
183		switch(c) {
184			case 'l':	if (library_num == MAX_LIBRARY) {
185						fprintf(stderr, "Too many libraries.  Maximum is %i.\n", MAX_LIBRARY);
186						exit(1);
187					}
188					library[library_num++] = optarg;
189					break;
190			case 'd':	opt_d++;
191					break;
192			case 'f':	opt_f = 1;
193					break;
194			case 'i':	opt_i++;
195					break;
196			case 'L':	opt_L = 0;
197					break;
198			case 'S':	opt_S = 1;
199					break;
200			case 'r':	opt_r++;
201					break;
202			case 't':	opt_t++;
203					break;
204#if HAVE_LIBIBERTY
205			case 'C':	opt_C++;
206					break;
207#endif
208			case 'a':	opt_a = atoi(optarg);
209					break;
210			case 's':	opt_s = atoi(optarg);
211					break;
212			case 'n':	opt_n = atoi(optarg);
213					break;
214			case 'h':	usage();
215					exit(0);
216			case 'o':	output = fopen(optarg, "w");
217					if (!output) {
218						fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
219						exit(1);
220					}
221					setvbuf(output, (char *)NULL, _IOLBF, 0);
222					fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
223					break;
224			case 'u':	opt_u = optarg;
225					break;
226			case 'p':
227				{
228					struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
229					if (!tmp) {
230						perror("ltrace: malloc");
231						exit(1);
232					}
233					tmp->pid = atoi(optarg);
234					tmp->next = opt_p;
235					opt_p = tmp;
236					break;
237				}
238			case 'e':
239				{
240					char * str_e = strdup(optarg);
241					if (!str_e) {
242						perror("ltrace: strdup");
243						exit(1);
244					}
245					if (str_e[0]=='!') {
246						opt_e_enable=0;
247						str_e++;
248					}
249					while(*str_e) {
250						struct opt_e_t * tmp;
251						char *str2 = strchr(str_e,',');
252						if (str2) {
253							*str2 = '\0';
254						}
255						tmp = malloc(sizeof(struct opt_e_t));
256						if (!tmp) {
257							perror("ltrace: malloc");
258							exit(1);
259						}
260						tmp->name = str_e;
261						tmp->next = opt_e;
262						opt_e = tmp;
263						if (str2) {
264							str_e = str2+1;
265						} else {
266							break;
267						}
268					}
269					break;
270				}
271			case 'V':	printf("ltrace version " VERSION ".\n"
272"Copyright (C) 1997-1999 Juan Cespedes <cespedes@debian.org>.\n"
273"This is free software; see the GNU General Public Licence\n"
274"version 2 or later for copying conditions.  There is NO warranty.\n");
275					exit(0);
276
277			default:
278#if HAVE_GETOPT_LONG
279				fprintf(stderr, "Try `%s --help' for more information\n", progname);
280#else
281				fprintf(stderr, "Try `%s -h' for more information\n", progname);
282#endif
283					exit(1);
284		}
285	}
286	argc -= optind; argv += optind;
287#endif
288
289	if (!opt_p && argc<1) {
290		fprintf(stderr, "%s: too few arguments\n", progname);
291#if HAVE_GETOPT_LONG
292		fprintf(stderr, "Try `%s --help' for more information\n", progname);
293#elif HAVE_GETOPT
294		fprintf(stderr, "Try `%s -h' for more information\n", progname);
295#endif
296		exit(1);
297	}
298	if (opt_r && opt_t) {
299		fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
300		exit(1);
301	}
302	if (argc>0) {
303		command = search_for_command(argv[0]);
304	}
305	return &argv[0];
306}
307