xref: /freebsd/usr.bin/truss/main.c (revision d70876fd7e2fe46f9d1fb2212a2687f9684ccb7a)
1d5303c80SXin LI /*-
20a6c71f8SWarner Losh  * Copyright 1997 Sean Eric Fagan
309d64da3SSean Eric Fagan  *
409d64da3SSean Eric Fagan  * Redistribution and use in source and binary forms, with or without
509d64da3SSean Eric Fagan  * modification, are permitted provided that the following conditions
609d64da3SSean Eric Fagan  * are met:
709d64da3SSean Eric Fagan  * 1. Redistributions of source code must retain the above copyright
809d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer.
909d64da3SSean Eric Fagan  * 2. Redistributions in binary form must reproduce the above copyright
1009d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer in the
1109d64da3SSean Eric Fagan  *    documentation and/or other materials provided with the distribution.
1209d64da3SSean Eric Fagan  * 3. All advertising materials mentioning features or use of this software
1309d64da3SSean Eric Fagan  *    must display the following acknowledgement:
1409d64da3SSean Eric Fagan  *	This product includes software developed by Sean Eric Fagan
1509d64da3SSean Eric Fagan  * 4. Neither the name of the author may be used to endorse or promote
1609d64da3SSean Eric Fagan  *    products derived from this software without specific prior written
1709d64da3SSean Eric Fagan  *    permission.
1809d64da3SSean Eric Fagan  *
1909d64da3SSean Eric Fagan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2009d64da3SSean Eric Fagan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2109d64da3SSean Eric Fagan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2209d64da3SSean Eric Fagan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2309d64da3SSean Eric Fagan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2409d64da3SSean Eric Fagan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2509d64da3SSean Eric Fagan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2609d64da3SSean Eric Fagan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2709d64da3SSean Eric Fagan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2809d64da3SSean Eric Fagan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2909d64da3SSean Eric Fagan  * SUCH DAMAGE.
3009d64da3SSean Eric Fagan  */
3109d64da3SSean Eric Fagan 
32b956c13cSPhilippe Charnier #include <sys/cdefs.h>
33b956c13cSPhilippe Charnier __FBSDID("$FreeBSD$");
343cf51049SPhilippe Charnier 
3509d64da3SSean Eric Fagan /*
36b2bf146eSBenedict Reuschling  * The main module for truss.  Surprisingly simple, but, then, the other
37bbeaf6c0SSean Eric Fagan  * files handle the bulk of the work.  And, of course, the kernel has to
38bbeaf6c0SSean Eric Fagan  * do a lot of the work :).
39bbeaf6c0SSean Eric Fagan  */
40bbeaf6c0SSean Eric Fagan 
412b75c8adSJohn Baldwin #include <sys/ptrace.h>
42580e0a2bSDag-Erling Smørgrav 
433cf51049SPhilippe Charnier #include <err.h>
443cf51049SPhilippe Charnier #include <signal.h>
45bbeaf6c0SSean Eric Fagan #include <stdio.h>
46bbeaf6c0SSean Eric Fagan #include <stdlib.h>
47a5f14abfSJohn Baldwin #include <sysdecode.h>
4837169f94SMatthew N. Dodd #include <time.h>
4995c4ef65SPeter Wemm #include <unistd.h>
50bbeaf6c0SSean Eric Fagan 
51ec0bed25SMatthew N. Dodd #include "truss.h"
521be5d704SMark Murray #include "extern.h"
53ee3b0f6eSDiomidis Spinellis #include "syscall.h"
54bbeaf6c0SSean Eric Fagan 
555321ae86SAlfred Perlstein static void
563cf51049SPhilippe Charnier usage(void)
573cf51049SPhilippe Charnier {
583cf51049SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
59ee3b0f6eSDiomidis Spinellis 	    "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid",
60ee3b0f6eSDiomidis Spinellis 	    "       truss [-cfaedDS] [-o file] [-s strsize] command [args]");
61bbeaf6c0SSean Eric Fagan 	exit(1);
62bbeaf6c0SSean Eric Fagan }
63bbeaf6c0SSean Eric Fagan 
64d75300bfSAlfred Perlstein char *
65d75300bfSAlfred Perlstein strsig(int sig)
66d75300bfSAlfred Perlstein {
67f083f689SJohn Baldwin 	static char tmp[64];
68d75300bfSAlfred Perlstein 
69d75300bfSAlfred Perlstein 	if (sig > 0 && sig < NSIG) {
70f083f689SJohn Baldwin 		snprintf(tmp, sizeof(tmp), "SIG%s", sys_signame[sig]);
71f083f689SJohn Baldwin 		return (tmp);
72d75300bfSAlfred Perlstein 	}
73f083f689SJohn Baldwin 	return (NULL);
74d75300bfSAlfred Perlstein }
75d75300bfSAlfred Perlstein 
763cf51049SPhilippe Charnier int
775321ae86SAlfred Perlstein main(int ac, char **av)
785321ae86SAlfred Perlstein {
79896fc463SAndrey Zonov 	struct sigaction sa;
80ec0bed25SMatthew N. Dodd 	struct trussinfo *trussinfo;
8194355cfdSAndrey Zonov 	char *fname;
8294355cfdSAndrey Zonov 	char **command;
832b75c8adSJohn Baldwin 	pid_t pid;
842b75c8adSJohn Baldwin 	int c;
85bbeaf6c0SSean Eric Fagan 
8672aa911aSAlfred Perlstein 	fname = NULL;
8772aa911aSAlfred Perlstein 
88ec0bed25SMatthew N. Dodd 	/* Initialize the trussinfo struct */
89216fa4c6SXin LI 	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
90ec0bed25SMatthew N. Dodd 	if (trussinfo == NULL)
91216fa4c6SXin LI 		errx(1, "calloc() failed");
925d2d083cSXin LI 
932b75c8adSJohn Baldwin 	pid = 0;
94ec0bed25SMatthew N. Dodd 	trussinfo->outfile = stderr;
950cf21b4fSBrian Somers 	trussinfo->strsize = 32;
965d2d083cSXin LI 	trussinfo->curthread = NULL;
972b75c8adSJohn Baldwin 	LIST_INIT(&trussinfo->proclist);
986c61b0f3SBryan Drewery 	init_syscalls();
99*d70876fdSJohn Baldwin 	while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) {
100bbeaf6c0SSean Eric Fagan 		switch (c) {
101bbeaf6c0SSean Eric Fagan 		case 'p':	/* specified pid */
1022b75c8adSJohn Baldwin 			pid = atoi(optarg);
103ef29ac7fSXin LI 			/* make sure i don't trace me */
1042b75c8adSJohn Baldwin 			if (pid == getpid()) {
1052b75c8adSJohn Baldwin 				errx(2, "attempt to grab self.");
106ef29ac7fSXin LI 			}
107bbeaf6c0SSean Eric Fagan 			break;
108c03bfcc8SMatthew N. Dodd 		case 'f': /* Follow fork()'s */
109c03bfcc8SMatthew N. Dodd 			trussinfo->flags |= FOLLOWFORKS;
110c03bfcc8SMatthew N. Dodd 			break;
1119897b203SMatthew N. Dodd 		case 'a': /* Print execve() argument strings. */
1129897b203SMatthew N. Dodd 			trussinfo->flags |= EXECVEARGS;
1139897b203SMatthew N. Dodd 			break;
114ee3b0f6eSDiomidis Spinellis 		case 'c': /* Count number of system calls and time. */
1157096af2eSBryan Drewery 			trussinfo->flags |= (COUNTONLY | NOSIGS);
116ee3b0f6eSDiomidis Spinellis 			break;
1179897b203SMatthew N. Dodd 		case 'e': /* Print execve() environment strings. */
1189897b203SMatthew N. Dodd 			trussinfo->flags |= EXECVEENVS;
1199897b203SMatthew N. Dodd 			break;
1200d0bd00eSMatthew N. Dodd 		case 'd': /* Absolute timestamps */
1210d0bd00eSMatthew N. Dodd 			trussinfo->flags |= ABSOLUTETIMESTAMPS;
1220d0bd00eSMatthew N. Dodd 			break;
1230d0bd00eSMatthew N. Dodd 		case 'D': /* Relative timestamps */
1240d0bd00eSMatthew N. Dodd 			trussinfo->flags |= RELATIVETIMESTAMPS;
1250d0bd00eSMatthew N. Dodd 			break;
126bbeaf6c0SSean Eric Fagan 		case 'o':	/* Specified output file */
1273cf51049SPhilippe Charnier 			fname = optarg;
128bbeaf6c0SSean Eric Fagan 			break;
1290cf21b4fSBrian Somers 		case 's':	/* Specified string size */
1300cf21b4fSBrian Somers 			trussinfo->strsize = atoi(optarg);
1310cf21b4fSBrian Somers 			break;
132bbeaf6c0SSean Eric Fagan 		case 'S':	/* Don't trace signals */
133ec0bed25SMatthew N. Dodd 			trussinfo->flags |= NOSIGS;
134bbeaf6c0SSean Eric Fagan 			break;
135*d70876fdSJohn Baldwin 		case 'H':
136*d70876fdSJohn Baldwin 			trussinfo->flags |= DISPLAYTIDS;
137*d70876fdSJohn Baldwin 			break;
138bbeaf6c0SSean Eric Fagan 		default:
139bbeaf6c0SSean Eric Fagan 			usage();
140bbeaf6c0SSean Eric Fagan 		}
141bbeaf6c0SSean Eric Fagan 	}
142bbeaf6c0SSean Eric Fagan 
143bbeaf6c0SSean Eric Fagan 	ac -= optind; av += optind;
1442b75c8adSJohn Baldwin 	if ((pid == 0 && ac == 0) ||
1452b75c8adSJohn Baldwin 	    (pid != 0 && ac != 0))
146bbeaf6c0SSean Eric Fagan 		usage();
147bbeaf6c0SSean Eric Fagan 
1483cf51049SPhilippe Charnier 	if (fname != NULL) { /* Use output file */
149ecbb6d34SJaakko Heinonen 		/*
150c2b51d44SMateusz Guzik 		 * Set close-on-exec ('e'), so that the output file is not
151c2b51d44SMateusz Guzik 		 * shared with the traced process.
152ecbb6d34SJaakko Heinonen 		 */
153c2b51d44SMateusz Guzik 		if ((trussinfo->outfile = fopen(fname, "we")) == NULL)
154c2b51d44SMateusz Guzik 			err(1, "cannot open %s", fname);
155e04c3786SJaakko Heinonen 	}
1563cf51049SPhilippe Charnier 
157bbeaf6c0SSean Eric Fagan 	/*
158bbeaf6c0SSean Eric Fagan 	 * If truss starts the process itself, it will ignore some signals --
159bbeaf6c0SSean Eric Fagan 	 * they should be passed off to the process, which may or may not
160bbeaf6c0SSean Eric Fagan 	 * exit.  If, however, we are examining an already-running process,
161bbeaf6c0SSean Eric Fagan 	 * then we restore the event mask on these same signals.
162bbeaf6c0SSean Eric Fagan 	 */
1632b75c8adSJohn Baldwin 	if (pid == 0) {
1642b75c8adSJohn Baldwin 		/* Start a command ourselves */
165bbeaf6c0SSean Eric Fagan 		command = av;
1662b75c8adSJohn Baldwin 		setup_and_wait(trussinfo, command);
167bbeaf6c0SSean Eric Fagan 		signal(SIGINT, SIG_IGN);
168bbeaf6c0SSean Eric Fagan 		signal(SIGTERM, SIG_IGN);
169bbeaf6c0SSean Eric Fagan 		signal(SIGQUIT, SIG_IGN);
170bbeaf6c0SSean Eric Fagan 	} else {
171896fc463SAndrey Zonov 		sa.sa_handler = restore_proc;
172896fc463SAndrey Zonov 		sa.sa_flags = 0;
173896fc463SAndrey Zonov 		sigemptyset(&sa.sa_mask);
174896fc463SAndrey Zonov 		sigaction(SIGINT, &sa, NULL);
175896fc463SAndrey Zonov 		sigaction(SIGQUIT, &sa, NULL);
176896fc463SAndrey Zonov 		sigaction(SIGTERM, &sa, NULL);
1772b75c8adSJohn Baldwin 		start_tracing(trussinfo, pid);
178bbeaf6c0SSean Eric Fagan 	}
179bbeaf6c0SSean Eric Fagan 
180bbeaf6c0SSean Eric Fagan 	/*
181bbeaf6c0SSean Eric Fagan 	 * At this point, if we started the process, it is stopped waiting to
182bbeaf6c0SSean Eric Fagan 	 * be woken up, either in exit() or in execve().
183bbeaf6c0SSean Eric Fagan 	 */
1842b75c8adSJohn Baldwin 	if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) {
1852b75c8adSJohn Baldwin 		/*
1862b75c8adSJohn Baldwin 		 * If we are not able to handle this ABI, detach from the
1872b75c8adSJohn Baldwin 		 * process and exit.  If we just created a new process to
1882b75c8adSJohn Baldwin 		 * run a command, kill the new process rather than letting
1892b75c8adSJohn Baldwin 		 * it run untraced.
1902b75c8adSJohn Baldwin 		 */
1912b75c8adSJohn Baldwin 		if (pid == 0)
1922b75c8adSJohn Baldwin 			kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL);
1932b75c8adSJohn Baldwin 		ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL,
1942b75c8adSJohn Baldwin 		    0);
1952b75c8adSJohn Baldwin 		return (1);
1962b75c8adSJohn Baldwin 	}
1972b75c8adSJohn Baldwin 	ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1,
1982b75c8adSJohn Baldwin 	    0);
199bbeaf6c0SSean Eric Fagan 
200bbeaf6c0SSean Eric Fagan 	/*
201bbeaf6c0SSean Eric Fagan 	 * At this point, it's a simple loop, waiting for the process to
202bbeaf6c0SSean Eric Fagan 	 * stop, finding out why, printing out why, and then continuing it.
203bbeaf6c0SSean Eric Fagan 	 * All of the grunt work is done in the support routines.
204bbeaf6c0SSean Eric Fagan 	 */
205203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
2060d0bd00eSMatthew N. Dodd 
2072b75c8adSJohn Baldwin 	eventloop(trussinfo);
208d5303c80SXin LI 
209ee3b0f6eSDiomidis Spinellis 	if (trussinfo->flags & COUNTONLY)
210ee3b0f6eSDiomidis Spinellis 		print_summary(trussinfo);
211ee3b0f6eSDiomidis Spinellis 
212ee3b0f6eSDiomidis Spinellis 	fflush(trussinfo->outfile);
213ee3b0f6eSDiomidis Spinellis 
2145321ae86SAlfred Perlstein 	return (0);
215bbeaf6c0SSean Eric Fagan }
216