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