xref: /freebsd/usr.bin/truss/main.c (revision 9289f547a2e9780d2a77a78e31b02b357b2ce810)
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>
45*9289f547SJohn Baldwin #include <stdbool.h>
46bbeaf6c0SSean Eric Fagan #include <stdio.h>
47bbeaf6c0SSean Eric Fagan #include <stdlib.h>
48a5f14abfSJohn Baldwin #include <sysdecode.h>
4937169f94SMatthew N. Dodd #include <time.h>
5095c4ef65SPeter Wemm #include <unistd.h>
51bbeaf6c0SSean Eric Fagan 
52ec0bed25SMatthew N. Dodd #include "truss.h"
531be5d704SMark Murray #include "extern.h"
54ee3b0f6eSDiomidis Spinellis #include "syscall.h"
55bbeaf6c0SSean Eric Fagan 
565321ae86SAlfred Perlstein static void
573cf51049SPhilippe Charnier usage(void)
583cf51049SPhilippe Charnier {
593cf51049SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
60ee3b0f6eSDiomidis Spinellis 	    "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid",
61ee3b0f6eSDiomidis Spinellis 	    "       truss [-cfaedDS] [-o file] [-s strsize] command [args]");
62bbeaf6c0SSean Eric Fagan 	exit(1);
63bbeaf6c0SSean Eric Fagan }
64bbeaf6c0SSean Eric Fagan 
653cf51049SPhilippe Charnier int
665321ae86SAlfred Perlstein main(int ac, char **av)
675321ae86SAlfred Perlstein {
68896fc463SAndrey Zonov 	struct sigaction sa;
69ec0bed25SMatthew N. Dodd 	struct trussinfo *trussinfo;
7094355cfdSAndrey Zonov 	char *fname;
7194355cfdSAndrey Zonov 	char **command;
722b75c8adSJohn Baldwin 	pid_t pid;
732b75c8adSJohn Baldwin 	int c;
74bbeaf6c0SSean Eric Fagan 
7572aa911aSAlfred Perlstein 	fname = NULL;
7672aa911aSAlfred Perlstein 
77ec0bed25SMatthew N. Dodd 	/* Initialize the trussinfo struct */
78216fa4c6SXin LI 	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
79ec0bed25SMatthew N. Dodd 	if (trussinfo == NULL)
80216fa4c6SXin LI 		errx(1, "calloc() failed");
815d2d083cSXin LI 
822b75c8adSJohn Baldwin 	pid = 0;
83ec0bed25SMatthew N. Dodd 	trussinfo->outfile = stderr;
840cf21b4fSBrian Somers 	trussinfo->strsize = 32;
855d2d083cSXin LI 	trussinfo->curthread = NULL;
862b75c8adSJohn Baldwin 	LIST_INIT(&trussinfo->proclist);
876c61b0f3SBryan Drewery 	init_syscalls();
88d70876fdSJohn Baldwin 	while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) {
89bbeaf6c0SSean Eric Fagan 		switch (c) {
90bbeaf6c0SSean Eric Fagan 		case 'p':	/* specified pid */
912b75c8adSJohn Baldwin 			pid = atoi(optarg);
92ef29ac7fSXin LI 			/* make sure i don't trace me */
932b75c8adSJohn Baldwin 			if (pid == getpid()) {
942b75c8adSJohn Baldwin 				errx(2, "attempt to grab self.");
95ef29ac7fSXin LI 			}
96bbeaf6c0SSean Eric Fagan 			break;
97c03bfcc8SMatthew N. Dodd 		case 'f': /* Follow fork()'s */
98c03bfcc8SMatthew N. Dodd 			trussinfo->flags |= FOLLOWFORKS;
99c03bfcc8SMatthew N. Dodd 			break;
1009897b203SMatthew N. Dodd 		case 'a': /* Print execve() argument strings. */
1019897b203SMatthew N. Dodd 			trussinfo->flags |= EXECVEARGS;
1029897b203SMatthew N. Dodd 			break;
103ee3b0f6eSDiomidis Spinellis 		case 'c': /* Count number of system calls and time. */
1047096af2eSBryan Drewery 			trussinfo->flags |= (COUNTONLY | NOSIGS);
105ee3b0f6eSDiomidis Spinellis 			break;
1069897b203SMatthew N. Dodd 		case 'e': /* Print execve() environment strings. */
1079897b203SMatthew N. Dodd 			trussinfo->flags |= EXECVEENVS;
1089897b203SMatthew N. Dodd 			break;
1090d0bd00eSMatthew N. Dodd 		case 'd': /* Absolute timestamps */
1100d0bd00eSMatthew N. Dodd 			trussinfo->flags |= ABSOLUTETIMESTAMPS;
1110d0bd00eSMatthew N. Dodd 			break;
1120d0bd00eSMatthew N. Dodd 		case 'D': /* Relative timestamps */
1130d0bd00eSMatthew N. Dodd 			trussinfo->flags |= RELATIVETIMESTAMPS;
1140d0bd00eSMatthew N. Dodd 			break;
115bbeaf6c0SSean Eric Fagan 		case 'o':	/* Specified output file */
1163cf51049SPhilippe Charnier 			fname = optarg;
117bbeaf6c0SSean Eric Fagan 			break;
1180cf21b4fSBrian Somers 		case 's':	/* Specified string size */
1190cf21b4fSBrian Somers 			trussinfo->strsize = atoi(optarg);
1200cf21b4fSBrian Somers 			break;
121bbeaf6c0SSean Eric Fagan 		case 'S':	/* Don't trace signals */
122ec0bed25SMatthew N. Dodd 			trussinfo->flags |= NOSIGS;
123bbeaf6c0SSean Eric Fagan 			break;
124d70876fdSJohn Baldwin 		case 'H':
125d70876fdSJohn Baldwin 			trussinfo->flags |= DISPLAYTIDS;
126d70876fdSJohn Baldwin 			break;
127bbeaf6c0SSean Eric Fagan 		default:
128bbeaf6c0SSean Eric Fagan 			usage();
129bbeaf6c0SSean Eric Fagan 		}
130bbeaf6c0SSean Eric Fagan 	}
131bbeaf6c0SSean Eric Fagan 
132bbeaf6c0SSean Eric Fagan 	ac -= optind; av += optind;
1332b75c8adSJohn Baldwin 	if ((pid == 0 && ac == 0) ||
1342b75c8adSJohn Baldwin 	    (pid != 0 && ac != 0))
135bbeaf6c0SSean Eric Fagan 		usage();
136bbeaf6c0SSean Eric Fagan 
1373cf51049SPhilippe Charnier 	if (fname != NULL) { /* Use output file */
138ecbb6d34SJaakko Heinonen 		/*
139c2b51d44SMateusz Guzik 		 * Set close-on-exec ('e'), so that the output file is not
140c2b51d44SMateusz Guzik 		 * shared with the traced process.
141ecbb6d34SJaakko Heinonen 		 */
142c2b51d44SMateusz Guzik 		if ((trussinfo->outfile = fopen(fname, "we")) == NULL)
143c2b51d44SMateusz Guzik 			err(1, "cannot open %s", fname);
144e04c3786SJaakko Heinonen 	}
1453cf51049SPhilippe Charnier 
146bbeaf6c0SSean Eric Fagan 	/*
147bbeaf6c0SSean Eric Fagan 	 * If truss starts the process itself, it will ignore some signals --
148bbeaf6c0SSean Eric Fagan 	 * they should be passed off to the process, which may or may not
149bbeaf6c0SSean Eric Fagan 	 * exit.  If, however, we are examining an already-running process,
150bbeaf6c0SSean Eric Fagan 	 * then we restore the event mask on these same signals.
151bbeaf6c0SSean Eric Fagan 	 */
1522b75c8adSJohn Baldwin 	if (pid == 0) {
1532b75c8adSJohn Baldwin 		/* Start a command ourselves */
154bbeaf6c0SSean Eric Fagan 		command = av;
1552b75c8adSJohn Baldwin 		setup_and_wait(trussinfo, command);
156bbeaf6c0SSean Eric Fagan 		signal(SIGINT, SIG_IGN);
157bbeaf6c0SSean Eric Fagan 		signal(SIGTERM, SIG_IGN);
158bbeaf6c0SSean Eric Fagan 		signal(SIGQUIT, SIG_IGN);
159bbeaf6c0SSean Eric Fagan 	} else {
160896fc463SAndrey Zonov 		sa.sa_handler = restore_proc;
161896fc463SAndrey Zonov 		sa.sa_flags = 0;
162896fc463SAndrey Zonov 		sigemptyset(&sa.sa_mask);
163896fc463SAndrey Zonov 		sigaction(SIGINT, &sa, NULL);
164896fc463SAndrey Zonov 		sigaction(SIGQUIT, &sa, NULL);
165896fc463SAndrey Zonov 		sigaction(SIGTERM, &sa, NULL);
1662b75c8adSJohn Baldwin 		start_tracing(trussinfo, pid);
167bbeaf6c0SSean Eric Fagan 	}
168bbeaf6c0SSean Eric Fagan 
169bbeaf6c0SSean Eric Fagan 	/*
170bbeaf6c0SSean Eric Fagan 	 * At this point, if we started the process, it is stopped waiting to
171bbeaf6c0SSean Eric Fagan 	 * be woken up, either in exit() or in execve().
172bbeaf6c0SSean Eric Fagan 	 */
1732b75c8adSJohn Baldwin 	if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) {
1742b75c8adSJohn Baldwin 		/*
1752b75c8adSJohn Baldwin 		 * If we are not able to handle this ABI, detach from the
1762b75c8adSJohn Baldwin 		 * process and exit.  If we just created a new process to
1772b75c8adSJohn Baldwin 		 * run a command, kill the new process rather than letting
1782b75c8adSJohn Baldwin 		 * it run untraced.
1792b75c8adSJohn Baldwin 		 */
1802b75c8adSJohn Baldwin 		if (pid == 0)
1812b75c8adSJohn Baldwin 			kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL);
1822b75c8adSJohn Baldwin 		ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL,
1832b75c8adSJohn Baldwin 		    0);
1842b75c8adSJohn Baldwin 		return (1);
1852b75c8adSJohn Baldwin 	}
1862b75c8adSJohn Baldwin 	ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1,
1872b75c8adSJohn Baldwin 	    0);
188bbeaf6c0SSean Eric Fagan 
189bbeaf6c0SSean Eric Fagan 	/*
190bbeaf6c0SSean Eric Fagan 	 * At this point, it's a simple loop, waiting for the process to
191bbeaf6c0SSean Eric Fagan 	 * stop, finding out why, printing out why, and then continuing it.
192bbeaf6c0SSean Eric Fagan 	 * All of the grunt work is done in the support routines.
193bbeaf6c0SSean Eric Fagan 	 */
194203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
1950d0bd00eSMatthew N. Dodd 
1962b75c8adSJohn Baldwin 	eventloop(trussinfo);
197d5303c80SXin LI 
198ee3b0f6eSDiomidis Spinellis 	if (trussinfo->flags & COUNTONLY)
199ee3b0f6eSDiomidis Spinellis 		print_summary(trussinfo);
200ee3b0f6eSDiomidis Spinellis 
201ee3b0f6eSDiomidis Spinellis 	fflush(trussinfo->outfile);
202ee3b0f6eSDiomidis Spinellis 
2035321ae86SAlfred Perlstein 	return (0);
204bbeaf6c0SSean Eric Fagan }
205