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