aboutsummaryrefslogtreecommitdiff
path: root/options.c
blob: 57a0f8e11721fe3e55b22e14fce6c4813ffa4124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

#if HAVE_GETOPT_H
#define _GNU_SOURCE
#include <getopt.h>
#undef _GNU_SOURCE
#endif

#include "ltrace.h"
#include "options.h"
#include "defs.h"

static char *progname;		/* Program name (`ltrace') */
FILE * output;
int opt_a = DEFAULT_ACOLUMN;	/* default alignment column for results */
int opt_d = 0;			/* debug */
int opt_i = 0;			/* instruction pointer */
int opt_s = DEFAULT_STRLEN;	/* default maximum # of bytes printed in strings */
int opt_S = 0;			/* display syscalls */
int opt_L = 1;			/* display library calls */
int opt_f = 0;			/* trace child processes as they are created */
char * opt_u = NULL;		/* username to run command as */
int opt_r = 0;			/* print relative timestamp */
int opt_t = 0;			/* print absolute timestamp */
#if HAVE_LIBIBERTY
int opt_C = 0;			/* Demangle low-level symbol names into user-level names */
#endif

/* List of pids given to option -p: */
struct opt_p_t * opt_p = NULL;	/* attach to process with a given pid */

/* List of function names given to option -e: */
struct opt_e_t * opt_e = NULL;
int opt_e_enable=1;

static void usage(void)
{
#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
	fprintf(stdout, "Usage: %s [command [arg ...]]\n"
"Trace library calls of a given program.\n\n", progname);
#else
	fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
"Trace library calls of a given program.\n\n"

# if HAVE_GETOPT_LONG
"  -d, --debug         print debugging info.\n"
# else
"  -d                  print debugging info.\n"
# endif
"  -f                  follow forks.\n"
"  -i                  print instruction pointer at time of library call.\n"
"  -L                  do NOT display library calls.\n"
"  -S                  display system calls.\n"
"  -r                  print relative timestamps.\n"
"  -t, -tt, -ttt       print absolute timestamps.\n"
# if HAVE_LIBIBERTY
#  if HAVE_GETOPT_LONG
"  -C, --demangle      decode low-level symbol names into user-level names.\n"
#  else
"  -C                  decode low-level symbol names into user-level names.\n"
#  endif
# endif
# if HAVE_GETOPT_LONG
"  -a, --align=COLUMN  align return values in a secific column.\n"
# else
"  -a COLUMN           align return values in a secific column.\n"
# endif
"  -s STRLEN           specify the maximum string size to print.\n"
# if HAVE_GETOPT_LONG
"  -o, --output=FILE   write the trace output to that file.\n"
# else
"  -o FILE             write the trace output to that file.\n"
# endif
"  -u USERNAME         run command with the userid, groupid of username.\n"
"  -p PID              attach to the process with the process ID pid.\n"
"  -e expr             modify which events to trace.\n"
# if HAVE_GETOPT_LONG
"  -h, --help          display this help and exit.\n"
# else
"  -h                  display this help and exit.\n"
# endif
# if HAVE_GETOPT_LONG
"  -V, --version       output version information and exit.\n"
# else
"  -V                  output version information and exit.\n"
# endif
"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
		, progname);
#endif
}

static char * search_for_command(char * filename)
{
	static char pathname[1024];
	char *path;
	int m, n;

	if (strchr(filename, '/')) {
		return filename;
	}
	for (path = getenv("PATH"); path && *path; path += m) {
		if (strchr(path, ':')) {
			n = strchr(path, ':') - path;
			m = n + 1;
		} else {
			m = n = strlen(path);
		}
		strncpy(pathname, path, n);     /* Possible buffer overrun */
		if (n && pathname[n - 1] != '/') {
			pathname[n++] = '/';
		}
		strcpy(pathname + n, filename);
		if (!access(pathname, X_OK)) {
			return pathname;
		}
	}
	return filename;
}

char ** process_options(int argc, char **argv)
{
	progname = argv[0];
	output = stderr;

#if HAVE_GETOPT || HAVE_GETOPT_LONG
	while(1) {
		int c;
#if HAVE_GETOPT_LONG
		int option_index = 0;
		static struct option long_options[] = {
			{ "align", 1, 0, 'a'},
			{ "debug", 0, 0, 'd'},
# if HAVE_LIBIBERTY
			{ "demangle", 0, 0, 'C'},
#endif
			{ "help", 0, 0, 'h'},
			{ "output", 1, 0, 'o'},
			{ "version", 0, 0, 'V'},
			{ 0, 0, 0, 0}
		};
		c = getopt_long(argc, argv, "+dfiLSrthV"
# if HAVE_LIBIBERTY
			"C"
# endif
			"a:s:o:u:p:e:", long_options, &option_index);
#else
		c = getopt(argc, argv, "+dfiLSrthV"
# if HAVE_LIBIBERTY
			"C"
# endif
			"a:s:o:u:p:e:");
#endif
		if (c==-1) {
			break;
		}
		switch(c) {
			case 'd':	opt_d++;
					break;
			case 'f':	opt_f = 1;
					break;
			case 'i':	opt_i++;
					break;
			case 'L':	opt_L = 0;
					break;
			case 'S':	opt_S = 1;
					break;
			case 'r':	opt_r++;
					break;
			case 't':	opt_t++;
					break;
#if HAVE_LIBIBERTY
			case 'C':	opt_C++;
					break;
#endif
			case 'a':	opt_a = atoi(optarg);
					break;
			case 's':	opt_s = atoi(optarg);
					break;
			case 'h':	usage();
					exit(0);
			case 'o':	output = fopen(optarg, "w");
					if (!output) {
						fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
						exit(1);
					}
					setvbuf(output, (char *)NULL, _IOLBF, 0);
					break;
			case 'u':	opt_u = optarg;
					break;
			case 'p':	
				{
					struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
					if (!tmp) {
						perror("ltrace: malloc");
						exit(1);
					}
					tmp->pid = atoi(optarg);
					tmp->next = opt_p;
					opt_p = tmp;
					break;
				}
			case 'e':
				{
					char * str_e = strdup(optarg);
					if (!str_e) {
						perror("ltrace: strdup");
						exit(1);
					}
					if (str_e[0]=='!') {
						opt_e_enable=0;
						str_e++;
					}
					while(*str_e) {
						struct opt_e_t * tmp;
						char *str2 = strchr(str_e,',');
						if (str2) {
							*str2 = '\0';
						}
						tmp = malloc(sizeof(struct opt_e_t));
						if (!tmp) {
							perror("ltrace: malloc");
							exit(1);
						}
						tmp->name = str_e;
						tmp->next = opt_e;
						opt_e = tmp;
						if (str2) {
							str_e = str2+1;
						} else {
							break;
						}
					}
					break;
				}
			case 'V':	printf("ltrace version "
#ifdef VERSION
					VERSION
#else
					"???"
#endif
					".\n"
"Copyright (C) 1997-1999 Juan Cespedes <cespedes@debian.org>.\n"
"This is free software; see the GNU General Public Licence\n"
"version 2 or later for copying conditions.  There is NO warranty.\n");
					exit(0);

			default:
#if HAVE_GETOPT_LONG
				fprintf(stderr, "Try `%s --help' for more information\n", progname);
#else
				fprintf(stderr, "Try `%s -h' for more information\n", progname);
#endif
					exit(1);
		}
	}
	argc -= optind; argv += optind;
#endif

	if (!opt_p && argc<1) {
		fprintf(stderr, "%s: too few arguments\n", progname);
#if HAVE_GETOPT_LONG
		fprintf(stderr, "Try `%s --help' for more information\n", progname);
#elif HAVE_GETOPT
		fprintf(stderr, "Try `%s -h' for more information\n", progname);
#endif
		exit(1);
	}
	if (opt_r && opt_t) {
		fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
		exit(1);
	}
	if (argc>0) {
		command = search_for_command(argv[0]);
	}
	return &argv[0];
}