summaryrefslogtreecommitdiffstats
path: root/usr.bin/truss/main.c
blob: 9c1589b2e7e9bb03c236491f0485d4d330430ce2 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*-
 * Copryight 1997 Sean Eric Fagan
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Sean Eric Fagan
 * 4. Neither the name of the author may be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * The main module for truss.  Suprisingly simple, but, then, the other
 * files handle the bulk of the work.  And, of course, the kernel has to
 * do a lot of the work :).
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "truss.h"
#include "extern.h"

#define MAXARGS 6

static void
usage(void)
{
	fprintf(stderr, "%s\n%s\n",
	    "usage: truss [-faedDS] [-o file] [-s strsize] -p pid",
	    "       truss [-faedDS] [-o file] [-s strsize] command [args]");
	exit(1);
}

/*
 * WARNING! "FreeBSD a.out" must be first, or set_etype will not
 * work correctly.
 */
struct ex_types {
	const char *type;
	void (*enter_syscall)(struct trussinfo *, int);
	long (*exit_syscall)(struct trussinfo *, int);
} ex_types[] = {
#ifdef __amd64__
	{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
	{ "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
	{ "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
#endif
#ifdef __i386__
	{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
	{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
	{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
	{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
#endif
#ifdef __ia64__
	{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
#endif
#ifdef __powerpc__
	{ "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
	{ "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
#endif
#ifdef __sparc64__
	{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
#endif
#ifdef __mips__
	{ "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit },
	{ "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit },
	{ "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX
#endif
	{ 0, 0, 0 },
};

/*
 * Set the execution type.  This is called after every exec, and when
 * a process is first monitored. 
 */

static struct ex_types *
set_etype(struct trussinfo *trussinfo)
{
	struct ex_types *funcs;
	char progt[32];
	
	size_t len = sizeof(progt);
	int mib[4];
	int error;

	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_SV_NAME;
	mib[3] = trussinfo->pid;
	error = sysctl(mib, 4, progt, &len, NULL, 0);
	if (error != 0)
		err(2, "can not get etype");

	for (funcs = ex_types; funcs->type; funcs++)
		if (!strcmp(funcs->type, progt))
			break;

	if (funcs->type == NULL) {
		funcs = &ex_types[0];
		warn("execution type %s is not supported -- using %s",
		    progt, funcs->type);
	}
	return (funcs);
}

char *
strsig(int sig)
{
	char *ret;

	ret = NULL;
	if (sig > 0 && sig < NSIG) {
		int i;
		asprintf(&ret, "sig%s", sys_signame[sig]);
		if (ret == NULL)
			return (NULL);
		for (i = 0; ret[i] != '\0'; ++i)
			ret[i] = toupper(ret[i]);
	}
	return (ret);
}

int
main(int ac, char **av)
{
	int c;
	int i;
	pid_t childpid;
	int status;
	char **command;
	struct ex_types *funcs;
	int initial_open;
	char *fname;
	struct trussinfo *trussinfo;
	char *signame;

	fname = NULL;
	initial_open = 1;

	/* Initialize the trussinfo struct */
	trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
	if (trussinfo == NULL)
		errx(1, "malloc() failed");
	bzero(trussinfo, sizeof(struct trussinfo));
	
	trussinfo->outfile = stderr;
	trussinfo->strsize = 32;
	trussinfo->pr_why = S_NONE;
	trussinfo->curthread = NULL;
	SLIST_INIT(&trussinfo->threadlist);
	while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) {
		switch (c) {
		case 'p':	/* specified pid */
			trussinfo->pid = atoi(optarg);
			/* make sure i don't trace me */
			if(trussinfo->pid == getpid()) {
				fprintf(stderr, "attempt to grab self.\n");
				exit(2);
			}
			break;
		case 'f': /* Follow fork()'s */
			trussinfo->flags |= FOLLOWFORKS;
			break;
		case 'a': /* Print execve() argument strings. */
			trussinfo->flags |= EXECVEARGS;
			break;
		case 'e': /* Print execve() environment strings. */
			trussinfo->flags |= EXECVEENVS;
			break;
		case 'd': /* Absolute timestamps */
			trussinfo->flags |= ABSOLUTETIMESTAMPS;
			break;
		case 'D': /* Relative timestamps */
			trussinfo->flags |= RELATIVETIMESTAMPS;
			break;
		case 'o':	/* Specified output file */
			fname = optarg;
			break;
		case 's':	/* Specified string size */
			trussinfo->strsize = atoi(optarg);
			break;
		case 'S':	/* Don't trace signals */ 
			trussinfo->flags |= NOSIGS;
			break;
		default:
			usage();
		}
	}

	ac -= optind; av += optind;
	if ((trussinfo->pid == 0 && ac == 0) ||
	    (trussinfo->pid != 0 && ac != 0))
		usage();

	if (fname != NULL) { /* Use output file */
		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
			errx(1, "cannot open %s", fname);
	}

	/*
	 * If truss starts the process itself, it will ignore some signals --
	 * they should be passed off to the process, which may or may not
	 * exit.  If, however, we are examining an already-running process,
	 * then we restore the event mask on these same signals.
	 */

	if (trussinfo->pid == 0) {	/* Start a command ourselves */
		command = av;
		trussinfo->pid = setup_and_wait(command);
		signal(SIGINT, SIG_IGN);
		signal(SIGTERM, SIG_IGN);
		signal(SIGQUIT, SIG_IGN);
	} else {
		start_tracing(trussinfo->pid);
		signal(SIGINT, restore_proc);
		signal(SIGTERM, restore_proc);
		signal(SIGQUIT, restore_proc);
	}


	/*
	 * At this point, if we started the process, it is stopped waiting to
	 * be woken up, either in exit() or in execve().
	 */

START_TRACE:
	funcs = set_etype(trussinfo);

	initial_open = 0;
	/*
	 * At this point, it's a simple loop, waiting for the process to
	 * stop, finding out why, printing out why, and then continuing it.
	 * All of the grunt work is done in the support routines.
	 */

	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);

	do {
		struct timespec timediff;
		waitevent(trussinfo);

		switch(i = trussinfo->pr_why) {
		case S_SCE:
			funcs->enter_syscall(trussinfo, MAXARGS);
			clock_gettime(CLOCK_REALTIME,
			    &trussinfo->before);
			break;
		case S_SCX:
			clock_gettime(CLOCK_REALTIME,
			    &trussinfo->after);

			if (trussinfo->curthread->in_fork &&
			    (trussinfo->flags & FOLLOWFORKS)) {
				trussinfo->curthread->in_fork = 0;
				childpid =
				    funcs->exit_syscall(trussinfo,
					trussinfo->pr_data);

				/*
				 * Fork a new copy of ourself to trace
				 * the child of the original traced
				 * process.
				 */
				if (fork() == 0) {
					trussinfo->pid = childpid;
					start_tracing(trussinfo->pid);
					goto START_TRACE;
				}
				break;
			}
			funcs->exit_syscall(trussinfo, MAXARGS);
			break;
		case S_SIG:
			if (trussinfo->flags & NOSIGS)
				break;
			if (trussinfo->flags & FOLLOWFORKS)
				fprintf(trussinfo->outfile, "%5d: ",
				    trussinfo->pid);
			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
				timespecsubt(&trussinfo->after,
				    &trussinfo->start_time, &timediff);
				fprintf(trussinfo->outfile, "%ld.%09ld ",
				    (long)timediff.tv_sec,
				    timediff.tv_nsec);
			}
			if (trussinfo->flags & RELATIVETIMESTAMPS) {
				timespecsubt(&trussinfo->after,
				    &trussinfo->before, &timediff);
				fprintf(trussinfo->outfile, "%ld.%09ld ",
				    (long)timediff.tv_sec,
				    timediff.tv_nsec);
			}
			signame = strsig(trussinfo->pr_data);
			fprintf(trussinfo->outfile,
			    "SIGNAL %u (%s)\n", trussinfo->pr_data,
			    signame == NULL ? "?" : signame);
			free(signame);
			break;
		case S_EXIT:
			if (trussinfo->flags & FOLLOWFORKS)
				fprintf(trussinfo->outfile, "%5d: ",
				    trussinfo->pid);
			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
				timespecsubt(&trussinfo->after,
				    &trussinfo->start_time, &timediff);
				fprintf(trussinfo->outfile, "%ld.%09ld ",
				    (long)timediff.tv_sec,
				    timediff.tv_nsec);
			}
			if (trussinfo->flags & RELATIVETIMESTAMPS) {
			  timespecsubt(&trussinfo->after,
			      &trussinfo->before, &timediff);
			  fprintf(trussinfo->outfile, "%ld.%09ld ",
			    (long)timediff.tv_sec, timediff.tv_nsec);
			}
			fprintf(trussinfo->outfile,
			    "process exit, rval = %u\n", trussinfo->pr_data);
			break;
		default:
			break;
		}
	} while (trussinfo->pr_why != S_EXIT);
	fflush(trussinfo->outfile);

	if (trussinfo->flags & FOLLOWFORKS)
		do {
			childpid = wait(&status);
		} while (childpid != -1);

	return (0);
}
OpenPOWER on IntegriCloud