xref: /freebsd/usr.bin/truss/main.c (revision 1d386b48)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright 1997 Sean Eric Fagan
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Sean Eric Fagan
17  * 4. Neither the name of the author may be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 /*
36  * The main module for truss.  Surprisingly simple, but, then, the other
37  * files handle the bulk of the work.  And, of course, the kernel has to
38  * do a lot of the work :).
39  */
40 
41 #include <sys/ptrace.h>
42 
43 #include <err.h>
44 #include <signal.h>
45 #include <stdbool.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <sysdecode.h>
49 #include <time.h>
50 #include <unistd.h>
51 
52 #include "truss.h"
53 #include "extern.h"
54 #include "syscall.h"
55 
56 static __dead2 void
usage(void)57 usage(void)
58 {
59 	fprintf(stderr, "%s\n%s\n",
60 	    "usage: truss [-cfaedDHS] [-o file] [-s strsize] -p pid",
61 	    "       truss [-cfaedDHS] [-o file] [-s strsize] command [args]");
62 	exit(1);
63 }
64 
65 int
main(int ac,char ** av)66 main(int ac, char **av)
67 {
68 	struct sigaction sa;
69 	struct trussinfo *trussinfo;
70 	char *fname;
71 	char **command;
72 	const char *errstr;
73 	pid_t pid;
74 	int c;
75 
76 	fname = NULL;
77 
78 	/* Initialize the trussinfo struct */
79 	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
80 	if (trussinfo == NULL)
81 		errx(1, "calloc() failed");
82 
83 	pid = 0;
84 	trussinfo->outfile = stderr;
85 	trussinfo->strsize = 32;
86 	trussinfo->curthread = NULL;
87 	LIST_INIT(&trussinfo->proclist);
88 	while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) {
89 		switch (c) {
90 		case 'p':	/* specified pid */
91 			pid = atoi(optarg);
92 			/* make sure i don't trace me */
93 			if (pid == getpid()) {
94 				errx(2, "attempt to grab self.");
95 			}
96 			break;
97 		case 'f': /* Follow fork()'s */
98 			trussinfo->flags |= FOLLOWFORKS;
99 			break;
100 		case 'a': /* Print execve() argument strings. */
101 			trussinfo->flags |= EXECVEARGS;
102 			break;
103 		case 'c': /* Count number of system calls and time. */
104 			trussinfo->flags |= (COUNTONLY | NOSIGS);
105 			break;
106 		case 'e': /* Print execve() environment strings. */
107 			trussinfo->flags |= EXECVEENVS;
108 			break;
109 		case 'd': /* Absolute timestamps */
110 			trussinfo->flags |= ABSOLUTETIMESTAMPS;
111 			break;
112 		case 'D': /* Relative timestamps */
113 			trussinfo->flags |= RELATIVETIMESTAMPS;
114 			break;
115 		case 'o':	/* Specified output file */
116 			fname = optarg;
117 			break;
118 		case 's':	/* Specified string size */
119 			trussinfo->strsize = (int)strtonum(optarg, 0, INT_MAX,
120 			    &errstr);
121 			if (errstr)
122 				errx(1, "maximum string size is %s: %s", errstr, optarg);
123 			break;
124 		case 'S':	/* Don't trace signals */
125 			trussinfo->flags |= NOSIGS;
126 			break;
127 		case 'H':
128 			trussinfo->flags |= DISPLAYTIDS;
129 			break;
130 		default:
131 			usage();
132 		}
133 	}
134 
135 	ac -= optind; av += optind;
136 	if ((pid == 0 && ac == 0) ||
137 	    (pid != 0 && ac != 0))
138 		usage();
139 
140 	if (fname != NULL) { /* Use output file */
141 		/*
142 		 * Set close-on-exec ('e'), so that the output file is not
143 		 * shared with the traced process.
144 		 */
145 		if ((trussinfo->outfile = fopen(fname, "we")) == NULL)
146 			err(1, "cannot open %s", fname);
147 	}
148 
149 	/*
150 	 * If truss starts the process itself, it will ignore some signals --
151 	 * they should be passed off to the process, which may or may not
152 	 * exit.  If, however, we are examining an already-running process,
153 	 * then we restore the event mask on these same signals.
154 	 */
155 	if (pid == 0) {
156 		/* Start a command ourselves */
157 		command = av;
158 		setup_and_wait(trussinfo, command);
159 		signal(SIGINT, SIG_IGN);
160 		signal(SIGTERM, SIG_IGN);
161 		signal(SIGQUIT, SIG_IGN);
162 	} else {
163 		sa.sa_handler = restore_proc;
164 		sa.sa_flags = 0;
165 		sigemptyset(&sa.sa_mask);
166 		sigaction(SIGINT, &sa, NULL);
167 		sigaction(SIGQUIT, &sa, NULL);
168 		sigaction(SIGTERM, &sa, NULL);
169 		start_tracing(trussinfo, pid);
170 	}
171 
172 	/*
173 	 * At this point, if we started the process, it is stopped waiting to
174 	 * be woken up, either in exit() or in execve().
175 	 */
176 	if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) {
177 		/*
178 		 * If we are not able to handle this ABI, detach from the
179 		 * process and exit.  If we just created a new process to
180 		 * run a command, kill the new process rather than letting
181 		 * it run untraced.
182 		 */
183 		if (pid == 0)
184 			kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL);
185 		ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL,
186 		    0);
187 		return (1);
188 	}
189 	ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1,
190 	    0);
191 
192 	/*
193 	 * At this point, it's a simple loop, waiting for the process to
194 	 * stop, finding out why, printing out why, and then continuing it.
195 	 * All of the grunt work is done in the support routines.
196 	 */
197 	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
198 
199 	eventloop(trussinfo);
200 
201 	if (trussinfo->flags & COUNTONLY)
202 		print_summary(trussinfo);
203 
204 	fflush(trussinfo->outfile);
205 
206 	return (0);
207 }
208