1191005Sdelphij/*-
2204977Simp * Copyright 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$");
3432275Scharnier
3531899Ssef/*
36222103Sbcr * The main module for truss.  Surprisingly 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>
46191005Sdelphij#include <sys/wait.h>
4785301Sdes
4832275Scharnier#include <err.h>
49200462Sdelphij#include <errno.h>
50200462Sdelphij#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"
60192025Sdds#include "syscall.h"
6131567Ssef
62241162Szont#define	MAXARGS	6
6331567Ssef
64144177Salfredstatic void
6532275Scharnierusage(void)
6632275Scharnier{
67144177Salfred	fprintf(stderr, "%s\n%s\n",
68192025Sdds	    "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid",
69192025Sdds	    "       truss [-cfaedDS] [-o file] [-s strsize] command [args]");
70144177Salfred	exit(1);
7131567Ssef}
7231567Ssef
7338897Ssef/*
7438897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not
7538897Ssef * work correctly.
7638897Ssef */
7731567Ssefstruct ex_types {
78144177Salfred	const char *type;
79144177Salfred	void (*enter_syscall)(struct trussinfo *, int);
80144177Salfred	long (*exit_syscall)(struct trussinfo *, int);
8131567Ssef} ex_types[] = {
82130394Sdwmalone#ifdef __amd64__
83144177Salfred	{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
84179051Sjhb	{ "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
85179051Sjhb	{ "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
86130394Sdwmalone#endif
8739908Ssef#ifdef __i386__
88144177Salfred	{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
89144177Salfred	{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
90144177Salfred	{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
91144177Salfred	{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9239908Ssef#endif
93106716Smarcel#ifdef __ia64__
94144177Salfred	{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
95106716Smarcel#endif
96154047Sgrehan#ifdef __powerpc__
97154047Sgrehan	{ "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
98154047Sgrehan	{ "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
99211935Snwhitehorn#ifdef __powerpc64__
100211935Snwhitehorn	{ "FreeBSD ELF64", powerpc64_syscall_entry, powerpc64_syscall_exit },
101154047Sgrehan#endif
102211935Snwhitehorn#endif
103101320Sjake#ifdef __sparc64__
104144177Salfred	{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
105101320Sjake#endif
106188628Simp#ifdef __mips__
107188628Simp	{ "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit },
108188628Simp	{ "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit },
109188628Simp	{ "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX
110188628Simp#endif
111144177Salfred	{ 0, 0, 0 },
11231567Ssef};
11331567Ssef
11431567Ssef/*
11531567Ssef * Set the execution type.  This is called after every exec, and when
116241162Szont * a process is first monitored.
11731567Ssef */
11831567Ssef
11931567Ssefstatic struct ex_types *
120144177Salfredset_etype(struct trussinfo *trussinfo)
121144177Salfred{
122144177Salfred	struct ex_types *funcs;
123241162Szont	size_t len;
124241162Szont	int error;
125241162Szont	int mib[4];
126144177Salfred	char progt[32];
12731567Ssef
128241162Szont	len = sizeof(progt);
129168569Sdelphij	mib[0] = CTL_KERN;
130168569Sdelphij	mib[1] = KERN_PROC;
131168569Sdelphij	mib[2] = KERN_PROC_SV_NAME;
132168569Sdelphij	mib[3] = trussinfo->pid;
133168569Sdelphij	error = sysctl(mib, 4, progt, &len, NULL, 0);
134168569Sdelphij	if (error != 0)
135168569Sdelphij		err(2, "can not get etype");
13631567Ssef
137144177Salfred	for (funcs = ex_types; funcs->type; funcs++)
138241162Szont		if (strcmp(funcs->type, progt) == 0)
139144177Salfred			break;
14031567Ssef
141144177Salfred	if (funcs->type == NULL) {
142144177Salfred		funcs = &ex_types[0];
143144177Salfred		warn("execution type %s is not supported -- using %s",
144144177Salfred		    progt, funcs->type);
145144177Salfred	}
146144177Salfred	return (funcs);
14731567Ssef}
14831567Ssef
149132306Salfredchar *
150132306Salfredstrsig(int sig)
151132306Salfred{
152132306Salfred	char *ret;
153132306Salfred
154132306Salfred	ret = NULL;
155132306Salfred	if (sig > 0 && sig < NSIG) {
156218285Sjilles		asprintf(&ret, "SIG%s", sys_signame[sig]);
157132306Salfred		if (ret == NULL)
158132306Salfred			return (NULL);
159132306Salfred	}
160132306Salfred	return (ret);
161132306Salfred}
162132306Salfred
16332275Scharnierint
164144177Salfredmain(int ac, char **av)
165144177Salfred{
166241162Szont	struct timespec timediff;
167241162Szont	struct sigaction sa;
168144177Salfred	struct ex_types *funcs;
169241162Szont	struct trussinfo *trussinfo;
170144178Salfred	char *fname;
171144177Salfred	char *signame;
172241162Szont	char **command;
173241162Szont	pid_t childpid;
174241162Szont	int c, initial_open, status;
17531567Ssef
176144178Salfred	fname = NULL;
177144178Salfred	initial_open = 1;
178144178Salfred
179144177Salfred	/* Initialize the trussinfo struct */
180192153Sdelphij	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
181144177Salfred	if (trussinfo == NULL)
182192153Sdelphij		errx(1, "calloc() failed");
183192153Sdelphij
184144177Salfred	trussinfo->outfile = stderr;
185153963Sbrian	trussinfo->strsize = 32;
186168569Sdelphij	trussinfo->pr_why = S_NONE;
187168569Sdelphij	trussinfo->curthread = NULL;
188168569Sdelphij	SLIST_INIT(&trussinfo->threadlist);
189192025Sdds	while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) {
190144177Salfred		switch (c) {
191144177Salfred		case 'p':	/* specified pid */
192144177Salfred			trussinfo->pid = atoi(optarg);
193171055Sdelphij			/* make sure i don't trace me */
194241162Szont			if (trussinfo->pid == getpid()) {
195171055Sdelphij				fprintf(stderr, "attempt to grab self.\n");
196171055Sdelphij				exit(2);
197171055Sdelphij			}
198144177Salfred			break;
199144177Salfred		case 'f': /* Follow fork()'s */
200144177Salfred			trussinfo->flags |= FOLLOWFORKS;
201144177Salfred			break;
202144177Salfred		case 'a': /* Print execve() argument strings. */
203144177Salfred			trussinfo->flags |= EXECVEARGS;
204144177Salfred			break;
205192025Sdds		case 'c': /* Count number of system calls and time. */
206192025Sdds			trussinfo->flags |= COUNTONLY;
207192025Sdds			break;
208144177Salfred		case 'e': /* Print execve() environment strings. */
209144177Salfred			trussinfo->flags |= EXECVEENVS;
210144177Salfred			break;
211144177Salfred		case 'd': /* Absolute timestamps */
212144177Salfred			trussinfo->flags |= ABSOLUTETIMESTAMPS;
213144177Salfred			break;
214144177Salfred		case 'D': /* Relative timestamps */
215144177Salfred			trussinfo->flags |= RELATIVETIMESTAMPS;
216144177Salfred			break;
217144177Salfred		case 'o':	/* Specified output file */
218144177Salfred			fname = optarg;
219144177Salfred			break;
220153963Sbrian		case 's':	/* Specified string size */
221153963Sbrian			trussinfo->strsize = atoi(optarg);
222153963Sbrian			break;
223241162Szont		case 'S':	/* Don't trace signals */
224144177Salfred			trussinfo->flags |= NOSIGS;
225144177Salfred			break;
226144177Salfred		default:
227144177Salfred			usage();
228144177Salfred		}
229144177Salfred	}
23031567Ssef
231144177Salfred	ac -= optind; av += optind;
232144177Salfred	if ((trussinfo->pid == 0 && ac == 0) ||
233144177Salfred	    (trussinfo->pid != 0 && ac != 0))
234144177Salfred		usage();
23531567Ssef
236144177Salfred	if (fname != NULL) { /* Use output file */
237144177Salfred		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
238246142Smjg			err(1, "cannot open %s", fname);
239215235Sjh		/*
240215235Sjh		 * Set FD_CLOEXEC, so that the output file is not shared with
241215235Sjh		 * the traced process.
242215235Sjh		 */
243215235Sjh		if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) ==
244215235Sjh		    -1)
245215235Sjh			warn("fcntl()");
246144177Salfred	}
24732275Scharnier
248144177Salfred	/*
249144177Salfred	 * If truss starts the process itself, it will ignore some signals --
250144177Salfred	 * they should be passed off to the process, which may or may not
251144177Salfred	 * exit.  If, however, we are examining an already-running process,
252144177Salfred	 * then we restore the event mask on these same signals.
253144177Salfred	 */
25431567Ssef
255144177Salfred	if (trussinfo->pid == 0) {	/* Start a command ourselves */
256144177Salfred		command = av;
257144177Salfred		trussinfo->pid = setup_and_wait(command);
258144177Salfred		signal(SIGINT, SIG_IGN);
259144177Salfred		signal(SIGTERM, SIG_IGN);
260144177Salfred		signal(SIGQUIT, SIG_IGN);
261144177Salfred	} else {
262241162Szont		sa.sa_handler = restore_proc;
263241162Szont		sa.sa_flags = 0;
264241162Szont		sigemptyset(&sa.sa_mask);
265241162Szont		sigaction(SIGINT, &sa, NULL);
266241162Szont		sigaction(SIGQUIT, &sa, NULL);
267241162Szont		sigaction(SIGTERM, &sa, NULL);
268168569Sdelphij		start_tracing(trussinfo->pid);
269144177Salfred	}
27031567Ssef
27131567Ssef
272144177Salfred	/*
273144177Salfred	 * At this point, if we started the process, it is stopped waiting to
274144177Salfred	 * be woken up, either in exit() or in execve().
275144177Salfred	 */
27631567Ssef
277101283SmdoddSTART_TRACE:
278168569Sdelphij	funcs = set_etype(trussinfo);
279168569Sdelphij
280144178Salfred	initial_open = 0;
281144177Salfred	/*
282144177Salfred	 * At this point, it's a simple loop, waiting for the process to
283144177Salfred	 * stop, finding out why, printing out why, and then continuing it.
284144177Salfred	 * All of the grunt work is done in the support routines.
285144177Salfred	 */
28631567Ssef
287144177Salfred	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
288101285Smdodd
289144177Salfred	do {
290168569Sdelphij		waitevent(trussinfo);
29131567Ssef
292241162Szont		switch (trussinfo->pr_why) {
293168569Sdelphij		case S_SCE:
294168569Sdelphij			funcs->enter_syscall(trussinfo, MAXARGS);
295168569Sdelphij			clock_gettime(CLOCK_REALTIME,
296241162Szont			    &trussinfo->curthread->before);
297168569Sdelphij			break;
298168569Sdelphij		case S_SCX:
299168569Sdelphij			clock_gettime(CLOCK_REALTIME,
300241162Szont			    &trussinfo->curthread->after);
30131567Ssef
302168569Sdelphij			if (trussinfo->curthread->in_fork &&
303168569Sdelphij			    (trussinfo->flags & FOLLOWFORKS)) {
304168569Sdelphij				trussinfo->curthread->in_fork = 0;
305241162Szont				childpid = funcs->exit_syscall(trussinfo,
306241162Szont				    trussinfo->pr_data);
307101283Smdodd
308168569Sdelphij				/*
309168569Sdelphij				 * Fork a new copy of ourself to trace
310168569Sdelphij				 * the child of the original traced
311168569Sdelphij				 * process.
312168569Sdelphij				 */
313168569Sdelphij				if (fork() == 0) {
314168569Sdelphij					trussinfo->pid = childpid;
315168569Sdelphij					start_tracing(trussinfo->pid);
316168569Sdelphij					goto START_TRACE;
317144177Salfred				}
318144177Salfred				break;
319168569Sdelphij			}
320168569Sdelphij			funcs->exit_syscall(trussinfo, MAXARGS);
321168569Sdelphij			break;
322168569Sdelphij		case S_SIG:
323168569Sdelphij			if (trussinfo->flags & NOSIGS)
324144177Salfred				break;
325168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
326168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
327168569Sdelphij				    trussinfo->pid);
328168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
329241162Szont				timespecsubt(&trussinfo->curthread->after,
330168569Sdelphij				    &trussinfo->start_time, &timediff);
331168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
332168569Sdelphij				    (long)timediff.tv_sec,
333168569Sdelphij				    timediff.tv_nsec);
334144177Salfred			}
335168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
336241162Szont				timespecsubt(&trussinfo->curthread->after,
337241162Szont				    &trussinfo->curthread->before, &timediff);
338168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
339168569Sdelphij				    (long)timediff.tv_sec,
340168569Sdelphij				    timediff.tv_nsec);
341168569Sdelphij			}
342168569Sdelphij			signame = strsig(trussinfo->pr_data);
343168569Sdelphij			fprintf(trussinfo->outfile,
344168569Sdelphij			    "SIGNAL %u (%s)\n", trussinfo->pr_data,
345168569Sdelphij			    signame == NULL ? "?" : signame);
346168569Sdelphij			free(signame);
347168569Sdelphij			break;
348168569Sdelphij		case S_EXIT:
349192025Sdds			if (trussinfo->flags & COUNTONLY)
350192025Sdds				break;
351168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
352168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
353168569Sdelphij				    trussinfo->pid);
354168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
355241162Szont				timespecsubt(&trussinfo->curthread->after,
356168569Sdelphij				    &trussinfo->start_time, &timediff);
357168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
358168569Sdelphij				    (long)timediff.tv_sec,
359168569Sdelphij				    timediff.tv_nsec);
360168569Sdelphij			}
361168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
362241162Szont				timespecsubt(&trussinfo->curthread->after,
363241162Szont				    &trussinfo->curthread->before, &timediff);
364241162Szont				fprintf(trussinfo->outfile, "%ld.%09ld ",
365241162Szont				    (long)timediff.tv_sec, timediff.tv_nsec);
366168569Sdelphij			}
367168569Sdelphij			fprintf(trussinfo->outfile,
368168569Sdelphij			    "process exit, rval = %u\n", trussinfo->pr_data);
369168569Sdelphij			break;
370168569Sdelphij		default:
371168569Sdelphij			break;
372144177Salfred		}
373241162Szont	} while (trussinfo->pr_why != S_EXIT &&
374241162Szont	    trussinfo->pr_why != S_DETACHED);
375191005Sdelphij
376241162Szont	if (trussinfo->flags & FOLLOWFORKS) {
377191005Sdelphij		do {
378191005Sdelphij			childpid = wait(&status);
379191005Sdelphij		} while (childpid != -1);
380241162Szont	}
381191005Sdelphij
382241162Szont	if (trussinfo->flags & COUNTONLY)
383241162Szont		print_summary(trussinfo);
384192025Sdds
385192025Sdds	fflush(trussinfo->outfile);
386192025Sdds
387144177Salfred	return (0);
38831567Ssef}
389