main.c revision 179051
131567Ssef/*
231899Ssef * Copryight 1997 Sean Eric Fagan
331899Ssef *
431899Ssef * Redistribution and use in source and binary forms, with or without
531899Ssef * modification, are permitted provided that the following conditions
631899Ssef * are met:
731899Ssef * 1. Redistributions of source code must retain the above copyright
831899Ssef *    notice, this list of conditions and the following disclaimer.
931899Ssef * 2. Redistributions in binary form must reproduce the above copyright
1031899Ssef *    notice, this list of conditions and the following disclaimer in the
1131899Ssef *    documentation and/or other materials provided with the distribution.
1231899Ssef * 3. All advertising materials mentioning features or use of this software
1331899Ssef *    must display the following acknowledgement:
1431899Ssef *	This product includes software developed by Sean Eric Fagan
1531899Ssef * 4. Neither the name of the author may be used to endorse or promote
1631899Ssef *    products derived from this software without specific prior written
1731899Ssef *    permission.
1831899Ssef *
1931899Ssef * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2031899Ssef * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2131899Ssef * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2231899Ssef * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2331899Ssef * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2431899Ssef * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2531899Ssef * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2631899Ssef * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2731899Ssef * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2831899Ssef * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2931899Ssef * SUCH DAMAGE.
3031899Ssef */
3131899Ssef
32119852Scharnier#include <sys/cdefs.h>
33119852Scharnier__FBSDID("$FreeBSD: head/usr.bin/truss/main.c 179051 2008-05-16 15:34:06Z jhb $");
3432275Scharnier
3531899Ssef/*
3631567Ssef * The main module for truss.  Suprisingly simple, but, then, the other
3731567Ssef * files handle the bulk of the work.  And, of course, the kernel has to
3831567Ssef * do a lot of the work :).
3931567Ssef */
4031567Ssef
4185301Sdes#include <sys/param.h>
42123916Scracauer#include <sys/types.h>
43104581Smike#include <sys/time.h>
44123916Scracauer#include <sys/resource.h>
45168569Sdelphij#include <sys/sysctl.h>
4685301Sdes
47132306Salfred#include <ctype.h>
4832275Scharnier#include <err.h>
4932275Scharnier#include <errno.h>
5032275Scharnier#include <fcntl.h>
5132275Scharnier#include <signal.h>
5231567Ssef#include <stdio.h>
5331567Ssef#include <stdlib.h>
5431567Ssef#include <string.h>
55101423Smdodd#include <time.h>
5631579Speter#include <unistd.h>
5731567Ssef
58101282Smdodd#include "truss.h"
5987703Smarkm#include "extern.h"
6031567Ssef
61171645Smarcel#define MAXARGS 6
6231567Ssef
63144177Salfredstatic void
6432275Scharnierusage(void)
6532275Scharnier{
66144177Salfred	fprintf(stderr, "%s\n%s\n",
67153963Sbrian	    "usage: truss [-faedDS] [-o file] [-s strsize] -p pid",
68153963Sbrian	    "       truss [-faedDS] [-o file] [-s strsize] command [args]");
69144177Salfred	exit(1);
7031567Ssef}
7131567Ssef
7238897Ssef/*
7338897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not
7438897Ssef * work correctly.
7538897Ssef */
7631567Ssefstruct ex_types {
77144177Salfred	const char *type;
78144177Salfred	void (*enter_syscall)(struct trussinfo *, int);
79144177Salfred	long (*exit_syscall)(struct trussinfo *, int);
8031567Ssef} ex_types[] = {
8139908Ssef#ifdef __alpha__
82144177Salfred	{ "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
8339908Ssef#endif
84130394Sdwmalone#ifdef __amd64__
85144177Salfred	{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
86179051Sjhb	{ "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
87179051Sjhb	{ "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
88130394Sdwmalone#endif
8939908Ssef#ifdef __i386__
90144177Salfred	{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
91144177Salfred	{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
92144177Salfred	{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
93144177Salfred	{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9439908Ssef#endif
95106716Smarcel#ifdef __ia64__
96144177Salfred	{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
97106716Smarcel#endif
98154047Sgrehan#ifdef __powerpc__
99154047Sgrehan	{ "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
100154047Sgrehan	{ "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
101154047Sgrehan#endif
102101320Sjake#ifdef __sparc64__
103144177Salfred	{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
104101320Sjake#endif
105144177Salfred	{ 0, 0, 0 },
10631567Ssef};
10731567Ssef
10831567Ssef/*
10931567Ssef * Set the execution type.  This is called after every exec, and when
110168569Sdelphij * a process is first monitored.
11131567Ssef */
11231567Ssef
11331567Ssefstatic struct ex_types *
114144177Salfredset_etype(struct trussinfo *trussinfo)
115144177Salfred{
116144177Salfred	struct ex_types *funcs;
117144177Salfred	char progt[32];
118168569Sdelphij
119168569Sdelphij	size_t len = sizeof(progt);
120168569Sdelphij	int mib[4];
121168569Sdelphij	int error;
12231567Ssef
123168569Sdelphij	mib[0] = CTL_KERN;
124168569Sdelphij	mib[1] = KERN_PROC;
125168569Sdelphij	mib[2] = KERN_PROC_SV_NAME;
126168569Sdelphij	mib[3] = trussinfo->pid;
127168569Sdelphij	error = sysctl(mib, 4, progt, &len, NULL, 0);
128168569Sdelphij	if (error != 0)
129168569Sdelphij		err(2, "can not get etype");
13031567Ssef
131144177Salfred	for (funcs = ex_types; funcs->type; funcs++)
132144177Salfred		if (!strcmp(funcs->type, progt))
133144177Salfred			break;
13431567Ssef
135144177Salfred	if (funcs->type == NULL) {
136144177Salfred		funcs = &ex_types[0];
137144177Salfred		warn("execution type %s is not supported -- using %s",
138144177Salfred		    progt, funcs->type);
139144177Salfred	}
140144177Salfred	return (funcs);
14131567Ssef}
14231567Ssef
143132306Salfredchar *
144132306Salfredstrsig(int sig)
145132306Salfred{
146132306Salfred	char *ret;
147132306Salfred
148132306Salfred	ret = NULL;
149132306Salfred	if (sig > 0 && sig < NSIG) {
150132306Salfred		int i;
151132306Salfred		asprintf(&ret, "sig%s", sys_signame[sig]);
152132306Salfred		if (ret == NULL)
153132306Salfred			return (NULL);
154132306Salfred		for (i = 0; ret[i] != '\0'; ++i)
155132306Salfred			ret[i] = toupper(ret[i]);
156132306Salfred	}
157132306Salfred	return (ret);
158132306Salfred}
159132306Salfred
16032275Scharnierint
161144177Salfredmain(int ac, char **av)
162144177Salfred{
163144177Salfred	int c;
164144177Salfred	int i;
165144177Salfred	char **command;
166144177Salfred	struct ex_types *funcs;
167171055Sdelphij	int initial_open;
168144178Salfred	char *fname;
169144177Salfred	struct trussinfo *trussinfo;
170144177Salfred	char *signame;
17131567Ssef
172144178Salfred	fname = NULL;
173144178Salfred	initial_open = 1;
174144178Salfred
175144177Salfred	/* Initialize the trussinfo struct */
176144177Salfred	trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
177144177Salfred	if (trussinfo == NULL)
178144177Salfred		errx(1, "malloc() failed");
179144177Salfred	bzero(trussinfo, sizeof(struct trussinfo));
180168569Sdelphij
181144177Salfred	trussinfo->outfile = stderr;
182153963Sbrian	trussinfo->strsize = 32;
183168569Sdelphij	trussinfo->pr_why = S_NONE;
184168569Sdelphij	trussinfo->curthread = NULL;
185168569Sdelphij	SLIST_INIT(&trussinfo->threadlist);
186153963Sbrian	while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) {
187144177Salfred		switch (c) {
188144177Salfred		case 'p':	/* specified pid */
189144177Salfred			trussinfo->pid = atoi(optarg);
190171055Sdelphij			/* make sure i don't trace me */
191171055Sdelphij			if(trussinfo->pid == getpid()) {
192171055Sdelphij				fprintf(stderr, "attempt to grab self.\n");
193171055Sdelphij				exit(2);
194171055Sdelphij			}
195144177Salfred			break;
196144177Salfred		case 'f': /* Follow fork()'s */
197144177Salfred			trussinfo->flags |= FOLLOWFORKS;
198144177Salfred			break;
199144177Salfred		case 'a': /* Print execve() argument strings. */
200144177Salfred			trussinfo->flags |= EXECVEARGS;
201144177Salfred			break;
202144177Salfred		case 'e': /* Print execve() environment strings. */
203144177Salfred			trussinfo->flags |= EXECVEENVS;
204144177Salfred			break;
205144177Salfred		case 'd': /* Absolute timestamps */
206144177Salfred			trussinfo->flags |= ABSOLUTETIMESTAMPS;
207144177Salfred			break;
208144177Salfred		case 'D': /* Relative timestamps */
209144177Salfred			trussinfo->flags |= RELATIVETIMESTAMPS;
210144177Salfred			break;
211144177Salfred		case 'o':	/* Specified output file */
212144177Salfred			fname = optarg;
213144177Salfred			break;
214153963Sbrian		case 's':	/* Specified string size */
215153963Sbrian			trussinfo->strsize = atoi(optarg);
216153963Sbrian			break;
217144177Salfred		case 'S':	/* Don't trace signals */
218144177Salfred			trussinfo->flags |= NOSIGS;
219144177Salfred			break;
220144177Salfred		default:
221144177Salfred			usage();
222144177Salfred		}
223144177Salfred	}
22431567Ssef
225144177Salfred	ac -= optind; av += optind;
226144177Salfred	if ((trussinfo->pid == 0 && ac == 0) ||
227144177Salfred	    (trussinfo->pid != 0 && ac != 0))
228144177Salfred		usage();
22931567Ssef
230144177Salfred	if (fname != NULL) { /* Use output file */
231144177Salfred		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
232144177Salfred			errx(1, "cannot open %s", fname);
233144177Salfred	}
23432275Scharnier
235144177Salfred	/*
236144177Salfred	 * If truss starts the process itself, it will ignore some signals --
237144177Salfred	 * they should be passed off to the process, which may or may not
238144177Salfred	 * exit.  If, however, we are examining an already-running process,
239144177Salfred	 * then we restore the event mask on these same signals.
240144177Salfred	 */
24131567Ssef
242144177Salfred	if (trussinfo->pid == 0) {	/* Start a command ourselves */
243144177Salfred		command = av;
244144177Salfred		trussinfo->pid = setup_and_wait(command);
245144177Salfred		signal(SIGINT, SIG_IGN);
246144177Salfred		signal(SIGTERM, SIG_IGN);
247144177Salfred		signal(SIGQUIT, SIG_IGN);
248144177Salfred	} else {
249168569Sdelphij		start_tracing(trussinfo->pid);
250144177Salfred		signal(SIGINT, restore_proc);
251144177Salfred		signal(SIGTERM, restore_proc);
252144177Salfred		signal(SIGQUIT, restore_proc);
253144177Salfred	}
25431567Ssef
25531567Ssef
256144177Salfred	/*
257144177Salfred	 * At this point, if we started the process, it is stopped waiting to
258144177Salfred	 * be woken up, either in exit() or in execve().
259144177Salfred	 */
26031567Ssef
261101283SmdoddSTART_TRACE:
262168569Sdelphij	funcs = set_etype(trussinfo);
263168569Sdelphij
264144178Salfred	initial_open = 0;
265144177Salfred	/*
266144177Salfred	 * At this point, it's a simple loop, waiting for the process to
267144177Salfred	 * stop, finding out why, printing out why, and then continuing it.
268144177Salfred	 * All of the grunt work is done in the support routines.
269144177Salfred	 */
27031567Ssef
271144177Salfred	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
272101285Smdodd
273144177Salfred	do {
274158630Spav		struct timespec timediff;
275168569Sdelphij		waitevent(trussinfo);
27631567Ssef
277168569Sdelphij		switch(i = trussinfo->pr_why) {
278168569Sdelphij		case S_SCE:
279168569Sdelphij			funcs->enter_syscall(trussinfo, MAXARGS);
280168569Sdelphij			clock_gettime(CLOCK_REALTIME,
281168569Sdelphij			    &trussinfo->before);
282168569Sdelphij			break;
283168569Sdelphij		case S_SCX:
284168569Sdelphij			clock_gettime(CLOCK_REALTIME,
285168569Sdelphij			    &trussinfo->after);
28631567Ssef
287168569Sdelphij			if (trussinfo->curthread->in_fork &&
288168569Sdelphij			    (trussinfo->flags & FOLLOWFORKS)) {
289168569Sdelphij				int childpid;
290101283Smdodd
291168569Sdelphij				trussinfo->curthread->in_fork = 0;
292168569Sdelphij				childpid =
293168569Sdelphij				    funcs->exit_syscall(trussinfo,
294168569Sdelphij					trussinfo->pr_data);
295101283Smdodd
296168569Sdelphij				/*
297168569Sdelphij				 * Fork a new copy of ourself to trace
298168569Sdelphij				 * the child of the original traced
299168569Sdelphij				 * process.
300168569Sdelphij				 */
301168569Sdelphij				if (fork() == 0) {
302168569Sdelphij					trussinfo->pid = childpid;
303168569Sdelphij					start_tracing(trussinfo->pid);
304168569Sdelphij					goto START_TRACE;
305144177Salfred				}
306144177Salfred				break;
307168569Sdelphij			}
308168569Sdelphij			funcs->exit_syscall(trussinfo, MAXARGS);
309168569Sdelphij			break;
310168569Sdelphij		case S_SIG:
311168569Sdelphij			if (trussinfo->flags & NOSIGS)
312144177Salfred				break;
313168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
314168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
315168569Sdelphij				    trussinfo->pid);
316168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
317168569Sdelphij				timespecsubt(&trussinfo->after,
318168569Sdelphij				    &trussinfo->start_time, &timediff);
319168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
320168569Sdelphij				    (long)timediff.tv_sec,
321168569Sdelphij				    timediff.tv_nsec);
322144177Salfred			}
323168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
324168569Sdelphij				timespecsubt(&trussinfo->after,
325168569Sdelphij				    &trussinfo->before, &timediff);
326168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
327168569Sdelphij				    (long)timediff.tv_sec,
328168569Sdelphij				    timediff.tv_nsec);
329168569Sdelphij			}
330168569Sdelphij			signame = strsig(trussinfo->pr_data);
331168569Sdelphij			fprintf(trussinfo->outfile,
332168569Sdelphij			    "SIGNAL %u (%s)\n", trussinfo->pr_data,
333168569Sdelphij			    signame == NULL ? "?" : signame);
334168569Sdelphij			free(signame);
335168569Sdelphij			break;
336168569Sdelphij		case S_EXIT:
337168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
338168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
339168569Sdelphij				    trussinfo->pid);
340168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
341168569Sdelphij				timespecsubt(&trussinfo->after,
342168569Sdelphij				    &trussinfo->start_time, &timediff);
343168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
344168569Sdelphij				    (long)timediff.tv_sec,
345168569Sdelphij				    timediff.tv_nsec);
346168569Sdelphij			}
347168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
348168569Sdelphij			  timespecsubt(&trussinfo->after,
349168569Sdelphij			      &trussinfo->before, &timediff);
350168569Sdelphij			  fprintf(trussinfo->outfile, "%ld.%09ld ",
351168569Sdelphij			    (long)timediff.tv_sec, timediff.tv_nsec);
352168569Sdelphij			}
353168569Sdelphij			fprintf(trussinfo->outfile,
354168569Sdelphij			    "process exit, rval = %u\n", trussinfo->pr_data);
355168569Sdelphij			break;
356168569Sdelphij		default:
357168569Sdelphij			break;
358144177Salfred		}
359168569Sdelphij	} while (trussinfo->pr_why != S_EXIT);
360144177Salfred	fflush(trussinfo->outfile);
361168569Sdelphij
362144177Salfred	return (0);
36331567Ssef}
364