xref: /freebsd/usr.bin/truss/main.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1d5303c80SXin LI /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
40a6c71f8SWarner Losh  * Copyright 1997 Sean Eric Fagan
509d64da3SSean Eric Fagan  *
609d64da3SSean Eric Fagan  * Redistribution and use in source and binary forms, with or without
709d64da3SSean Eric Fagan  * modification, are permitted provided that the following conditions
809d64da3SSean Eric Fagan  * are met:
909d64da3SSean Eric Fagan  * 1. Redistributions of source code must retain the above copyright
1009d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer.
1109d64da3SSean Eric Fagan  * 2. Redistributions in binary form must reproduce the above copyright
1209d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer in the
1309d64da3SSean Eric Fagan  *    documentation and/or other materials provided with the distribution.
1409d64da3SSean Eric Fagan  * 3. All advertising materials mentioning features or use of this software
1509d64da3SSean Eric Fagan  *    must display the following acknowledgement:
1609d64da3SSean Eric Fagan  *	This product includes software developed by Sean Eric Fagan
1709d64da3SSean Eric Fagan  * 4. Neither the name of the author may be used to endorse or promote
1809d64da3SSean Eric Fagan  *    products derived from this software without specific prior written
1909d64da3SSean Eric Fagan  *    permission.
2009d64da3SSean Eric Fagan  *
2109d64da3SSean Eric Fagan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2209d64da3SSean Eric Fagan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2309d64da3SSean Eric Fagan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2409d64da3SSean Eric Fagan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2509d64da3SSean Eric Fagan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2609d64da3SSean Eric Fagan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2709d64da3SSean Eric Fagan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2809d64da3SSean Eric Fagan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2909d64da3SSean Eric Fagan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3009d64da3SSean Eric Fagan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3109d64da3SSean Eric Fagan  * SUCH DAMAGE.
3209d64da3SSean Eric Fagan  */
3309d64da3SSean Eric Fagan 
34b956c13cSPhilippe Charnier #include <sys/cdefs.h>
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>
459289f547SJohn 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 
56*31dddc6aSAlex Richardson static __dead2 void
usage(void)573cf51049SPhilippe Charnier usage(void)
583cf51049SPhilippe Charnier {
593cf51049SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
60fe7f211cSEd Maste 	    "usage: truss [-cfaedDHS] [-o file] [-s strsize] -p pid",
61fe7f211cSEd Maste 	    "       truss [-cfaedDHS] [-o file] [-s strsize] command [args]");
62bbeaf6c0SSean Eric Fagan 	exit(1);
63bbeaf6c0SSean Eric Fagan }
64bbeaf6c0SSean Eric Fagan 
653cf51049SPhilippe Charnier int
main(int ac,char ** av)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;
72c1745bf5SJohn Baldwin 	const char *errstr;
732b75c8adSJohn Baldwin 	pid_t pid;
742b75c8adSJohn Baldwin 	int c;
75bbeaf6c0SSean Eric Fagan 
7672aa911aSAlfred Perlstein 	fname = NULL;
7772aa911aSAlfred Perlstein 
78ec0bed25SMatthew N. Dodd 	/* Initialize the trussinfo struct */
79216fa4c6SXin LI 	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
80ec0bed25SMatthew N. Dodd 	if (trussinfo == NULL)
81216fa4c6SXin LI 		errx(1, "calloc() failed");
825d2d083cSXin LI 
832b75c8adSJohn Baldwin 	pid = 0;
84ec0bed25SMatthew N. Dodd 	trussinfo->outfile = stderr;
850cf21b4fSBrian Somers 	trussinfo->strsize = 32;
865d2d083cSXin LI 	trussinfo->curthread = NULL;
872b75c8adSJohn Baldwin 	LIST_INIT(&trussinfo->proclist);
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 */
119*31dddc6aSAlex Richardson 			trussinfo->strsize = (int)strtonum(optarg, 0, INT_MAX,
120*31dddc6aSAlex Richardson 			    &errstr);
121c1745bf5SJohn Baldwin 			if (errstr)
122c1745bf5SJohn Baldwin 				errx(1, "maximum string size is %s: %s", errstr, optarg);
1230cf21b4fSBrian Somers 			break;
124bbeaf6c0SSean Eric Fagan 		case 'S':	/* Don't trace signals */
125ec0bed25SMatthew N. Dodd 			trussinfo->flags |= NOSIGS;
126bbeaf6c0SSean Eric Fagan 			break;
127d70876fdSJohn Baldwin 		case 'H':
128d70876fdSJohn Baldwin 			trussinfo->flags |= DISPLAYTIDS;
129d70876fdSJohn Baldwin 			break;
130bbeaf6c0SSean Eric Fagan 		default:
131bbeaf6c0SSean Eric Fagan 			usage();
132bbeaf6c0SSean Eric Fagan 		}
133bbeaf6c0SSean Eric Fagan 	}
134bbeaf6c0SSean Eric Fagan 
135bbeaf6c0SSean Eric Fagan 	ac -= optind; av += optind;
1362b75c8adSJohn Baldwin 	if ((pid == 0 && ac == 0) ||
1372b75c8adSJohn Baldwin 	    (pid != 0 && ac != 0))
138bbeaf6c0SSean Eric Fagan 		usage();
139bbeaf6c0SSean Eric Fagan 
1403cf51049SPhilippe Charnier 	if (fname != NULL) { /* Use output file */
141ecbb6d34SJaakko Heinonen 		/*
142c2b51d44SMateusz Guzik 		 * Set close-on-exec ('e'), so that the output file is not
143c2b51d44SMateusz Guzik 		 * shared with the traced process.
144ecbb6d34SJaakko Heinonen 		 */
145c2b51d44SMateusz Guzik 		if ((trussinfo->outfile = fopen(fname, "we")) == NULL)
146c2b51d44SMateusz Guzik 			err(1, "cannot open %s", fname);
147e04c3786SJaakko Heinonen 	}
1483cf51049SPhilippe Charnier 
149bbeaf6c0SSean Eric Fagan 	/*
150bbeaf6c0SSean Eric Fagan 	 * If truss starts the process itself, it will ignore some signals --
151bbeaf6c0SSean Eric Fagan 	 * they should be passed off to the process, which may or may not
152bbeaf6c0SSean Eric Fagan 	 * exit.  If, however, we are examining an already-running process,
153bbeaf6c0SSean Eric Fagan 	 * then we restore the event mask on these same signals.
154bbeaf6c0SSean Eric Fagan 	 */
1552b75c8adSJohn Baldwin 	if (pid == 0) {
1562b75c8adSJohn Baldwin 		/* Start a command ourselves */
157bbeaf6c0SSean Eric Fagan 		command = av;
1582b75c8adSJohn Baldwin 		setup_and_wait(trussinfo, command);
159bbeaf6c0SSean Eric Fagan 		signal(SIGINT, SIG_IGN);
160bbeaf6c0SSean Eric Fagan 		signal(SIGTERM, SIG_IGN);
161bbeaf6c0SSean Eric Fagan 		signal(SIGQUIT, SIG_IGN);
162bbeaf6c0SSean Eric Fagan 	} else {
163896fc463SAndrey Zonov 		sa.sa_handler = restore_proc;
164896fc463SAndrey Zonov 		sa.sa_flags = 0;
165896fc463SAndrey Zonov 		sigemptyset(&sa.sa_mask);
166896fc463SAndrey Zonov 		sigaction(SIGINT, &sa, NULL);
167896fc463SAndrey Zonov 		sigaction(SIGQUIT, &sa, NULL);
168896fc463SAndrey Zonov 		sigaction(SIGTERM, &sa, NULL);
1692b75c8adSJohn Baldwin 		start_tracing(trussinfo, pid);
170bbeaf6c0SSean Eric Fagan 	}
171bbeaf6c0SSean Eric Fagan 
172bbeaf6c0SSean Eric Fagan 	/*
173bbeaf6c0SSean Eric Fagan 	 * At this point, if we started the process, it is stopped waiting to
174bbeaf6c0SSean Eric Fagan 	 * be woken up, either in exit() or in execve().
175bbeaf6c0SSean Eric Fagan 	 */
1762b75c8adSJohn Baldwin 	if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) {
1772b75c8adSJohn Baldwin 		/*
1782b75c8adSJohn Baldwin 		 * If we are not able to handle this ABI, detach from the
1792b75c8adSJohn Baldwin 		 * process and exit.  If we just created a new process to
1802b75c8adSJohn Baldwin 		 * run a command, kill the new process rather than letting
1812b75c8adSJohn Baldwin 		 * it run untraced.
1822b75c8adSJohn Baldwin 		 */
1832b75c8adSJohn Baldwin 		if (pid == 0)
1842b75c8adSJohn Baldwin 			kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL);
1852b75c8adSJohn Baldwin 		ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL,
1862b75c8adSJohn Baldwin 		    0);
1872b75c8adSJohn Baldwin 		return (1);
1882b75c8adSJohn Baldwin 	}
1892b75c8adSJohn Baldwin 	ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1,
1902b75c8adSJohn Baldwin 	    0);
191bbeaf6c0SSean Eric Fagan 
192bbeaf6c0SSean Eric Fagan 	/*
193bbeaf6c0SSean Eric Fagan 	 * At this point, it's a simple loop, waiting for the process to
194bbeaf6c0SSean Eric Fagan 	 * stop, finding out why, printing out why, and then continuing it.
195bbeaf6c0SSean Eric Fagan 	 * All of the grunt work is done in the support routines.
196bbeaf6c0SSean Eric Fagan 	 */
197203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
1980d0bd00eSMatthew N. Dodd 
1992b75c8adSJohn Baldwin 	eventloop(trussinfo);
200d5303c80SXin LI 
201ee3b0f6eSDiomidis Spinellis 	if (trussinfo->flags & COUNTONLY)
202ee3b0f6eSDiomidis Spinellis 		print_summary(trussinfo);
203ee3b0f6eSDiomidis Spinellis 
204ee3b0f6eSDiomidis Spinellis 	fflush(trussinfo->outfile);
205ee3b0f6eSDiomidis Spinellis 
2065321ae86SAlfred Perlstein 	return (0);
207bbeaf6c0SSean Eric Fagan }
208