main.c revision 144177
131567Ssef/*
231899Ssef * Copryight 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: head/usr.bin/truss/main.c 144177 2005-03-27 12:47:04Z alfred $");
3432275Scharnier
3531899Ssef/*
3631567Ssef * The main module for truss.  Suprisingly 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>
4285301Sdes#include <sys/ioctl.h>
4385301Sdes#include <sys/pioctl.h>
44123916Scracauer#include <sys/types.h>
45104581Smike#include <sys/time.h>
46123916Scracauer#include <sys/resource.h>
4785301Sdes
48132306Salfred#include <ctype.h>
4932275Scharnier#include <err.h>
5032275Scharnier#include <errno.h>
5132275Scharnier#include <fcntl.h>
5232275Scharnier#include <signal.h>
5331567Ssef#include <stdio.h>
5431567Ssef#include <stdlib.h>
5531567Ssef#include <string.h>
56101423Smdodd#include <time.h>
5731579Speter#include <unistd.h>
5831567Ssef
59101282Smdodd#include "truss.h"
6087703Smarkm#include "extern.h"
6131567Ssef
6231567Ssef/*
63101282Smdodd * It's difficult to parameterize this because it must be
64101282Smdodd * accessible in a signal handler.
6531567Ssef */
6631567Ssef
6731567Ssefint Procfd;
6831567Ssef
69144177Salfredstatic void
7032275Scharnierusage(void)
7132275Scharnier{
72144177Salfred	fprintf(stderr, "%s\n%s\n",
73144177Salfred	    "usage: truss [-faedDS] [-o file] -p pid",
74144177Salfred	    "       truss [-faedDS] [-o file] command [args]");
75144177Salfred	exit(1);
7631567Ssef}
7731567Ssef
7838897Ssef/*
7938897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not
8038897Ssef * work correctly.
8138897Ssef */
8231567Ssefstruct ex_types {
83144177Salfred	const char *type;
84144177Salfred	void (*enter_syscall)(struct trussinfo *, int);
85144177Salfred	long (*exit_syscall)(struct trussinfo *, int);
8631567Ssef} ex_types[] = {
8739908Ssef#ifdef __alpha__
88144177Salfred	{ "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
8939908Ssef#endif
90130394Sdwmalone#ifdef __amd64__
91144177Salfred	{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
92130394Sdwmalone#endif
9339908Ssef#ifdef __i386__
94144177Salfred	{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
95144177Salfred	{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
96144177Salfred	{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
97144177Salfred	{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9839908Ssef#endif
99106716Smarcel#ifdef __ia64__
100144177Salfred	{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
101106716Smarcel#endif
102101320Sjake#ifdef __sparc64__
103144177Salfred	{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
104101320Sjake#endif
105144177Salfred	{ 0, 0, 0 },
10631567Ssef};
10731567Ssef
10831567Ssef/*
10931567Ssef * Set the execution type.  This is called after every exec, and when
11031567Ssef * a process is first monitored.  The procfs pseudo-file "etype" has
11131567Ssef * the execution module type -- see /proc/curproc/etype for an example.
11231567Ssef */
11331567Ssef
11431567Ssefstatic struct ex_types *
115144177Salfredset_etype(struct trussinfo *trussinfo)
116144177Salfred{
117144177Salfred	struct ex_types *funcs;
118144177Salfred	char etype[24];
119144177Salfred	char progt[32];
120144177Salfred	int fd;
12131567Ssef
122144177Salfred	sprintf(etype, "/proc/%d/etype", trussinfo->pid);
123144177Salfred	if ((fd = open(etype, O_RDONLY)) == -1) {
124144177Salfred		strcpy(progt, "FreeBSD a.out");
125144177Salfred	} else {
126144177Salfred		int len = read(fd, progt, sizeof(progt));
127144177Salfred		progt[len-1] = '\0';
128144177Salfred		close(fd);
129144177Salfred	}
13031567Ssef
131144177Salfred	for (funcs = ex_types; funcs->type; funcs++)
132144177Salfred		if (!strcmp(funcs->type, progt))
133144177Salfred			break;
13431567Ssef
135144177Salfred	if (funcs->type == NULL) {
136144177Salfred		funcs = &ex_types[0];
137144177Salfred		warn("execution type %s is not supported -- using %s",
138144177Salfred		    progt, funcs->type);
139144177Salfred	}
140144177Salfred	return (funcs);
14131567Ssef}
14231567Ssef
143132306Salfredchar *
144132306Salfredstrsig(int sig)
145132306Salfred{
146132306Salfred	char *ret;
147132306Salfred
148132306Salfred	ret = NULL;
149132306Salfred	if (sig > 0 && sig < NSIG) {
150132306Salfred		int i;
151132306Salfred		asprintf(&ret, "sig%s", sys_signame[sig]);
152132306Salfred		if (ret == NULL)
153132306Salfred			return (NULL);
154132306Salfred		for (i = 0; ret[i] != '\0'; ++i)
155132306Salfred			ret[i] = toupper(ret[i]);
156132306Salfred	}
157132306Salfred	return (ret);
158132306Salfred}
159132306Salfred
16032275Scharnierint
161144177Salfredmain(int ac, char **av)
162144177Salfred{
163144177Salfred	int c;
164144177Salfred	int i;
165144177Salfred	char **command;
166144177Salfred	struct procfs_status pfs;
167144177Salfred	struct ex_types *funcs;
168144177Salfred	int in_exec = 0;
169144177Salfred	char *fname = NULL;
170144177Salfred	int sigexit = 0;
171144177Salfred	struct trussinfo *trussinfo;
172144177Salfred	char *signame;
17331567Ssef
174144177Salfred	/* Initialize the trussinfo struct */
175144177Salfred	trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
176144177Salfred	if (trussinfo == NULL)
177144177Salfred		errx(1, "malloc() failed");
178144177Salfred	bzero(trussinfo, sizeof(struct trussinfo));
179144177Salfred	trussinfo->outfile = stderr;
180101282Smdodd
181144177Salfred	while ((c = getopt(ac, av, "p:o:faedDS")) != -1) {
182144177Salfred		switch (c) {
183144177Salfred		case 'p':	/* specified pid */
184144177Salfred			trussinfo->pid = atoi(optarg);
185144177Salfred			break;
186144177Salfred		case 'f': /* Follow fork()'s */
187144177Salfred			trussinfo->flags |= FOLLOWFORKS;
188144177Salfred			break;
189144177Salfred		case 'a': /* Print execve() argument strings. */
190144177Salfred			trussinfo->flags |= EXECVEARGS;
191144177Salfred			break;
192144177Salfred		case 'e': /* Print execve() environment strings. */
193144177Salfred			trussinfo->flags |= EXECVEENVS;
194144177Salfred			break;
195144177Salfred		case 'd': /* Absolute timestamps */
196144177Salfred			trussinfo->flags |= ABSOLUTETIMESTAMPS;
197144177Salfred			break;
198144177Salfred		case 'D': /* Relative timestamps */
199144177Salfred			trussinfo->flags |= RELATIVETIMESTAMPS;
200144177Salfred			break;
201144177Salfred		case 'o':	/* Specified output file */
202144177Salfred			fname = optarg;
203144177Salfred			break;
204144177Salfred		case 'S':	/* Don't trace signals */
205144177Salfred			trussinfo->flags |= NOSIGS;
206144177Salfred			break;
207144177Salfred		default:
208144177Salfred			usage();
209144177Salfred		}
210144177Salfred	}
21131567Ssef
212144177Salfred	ac -= optind; av += optind;
213144177Salfred	if ((trussinfo->pid == 0 && ac == 0) ||
214144177Salfred	    (trussinfo->pid != 0 && ac != 0))
215144177Salfred		usage();
21631567Ssef
217144177Salfred	if (fname != NULL) { /* Use output file */
218144177Salfred		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
219144177Salfred			errx(1, "cannot open %s", fname);
220144177Salfred	}
22132275Scharnier
222144177Salfred	/*
223144177Salfred	 * If truss starts the process itself, it will ignore some signals --
224144177Salfred	 * they should be passed off to the process, which may or may not
225144177Salfred	 * exit.  If, however, we are examining an already-running process,
226144177Salfred	 * then we restore the event mask on these same signals.
227144177Salfred	 */
22831567Ssef
229144177Salfred	if (trussinfo->pid == 0) {	/* Start a command ourselves */
230144177Salfred		command = av;
231144177Salfred		trussinfo->pid = setup_and_wait(command);
232144177Salfred		signal(SIGINT, SIG_IGN);
233144177Salfred		signal(SIGTERM, SIG_IGN);
234144177Salfred		signal(SIGQUIT, SIG_IGN);
235144177Salfred	} else {
236144177Salfred		signal(SIGINT, restore_proc);
237144177Salfred		signal(SIGTERM, restore_proc);
238144177Salfred		signal(SIGQUIT, restore_proc);
239144177Salfred	}
24031567Ssef
24131567Ssef
242144177Salfred	/*
243144177Salfred	 * At this point, if we started the process, it is stopped waiting to
244144177Salfred	 * be woken up, either in exit() or in execve().
245144177Salfred	 */
24631567Ssef
247101283SmdoddSTART_TRACE:
248144177Salfred	Procfd = start_tracing(
249144177Salfred	    trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
250144177Salfred	    ((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
251144177Salfred	    ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
252144177Salfred	if (Procfd == -1)
253144177Salfred		return (0);
25455707Ssef
255144177Salfred	pfs.why = 0;
25631567Ssef
257144177Salfred	funcs = set_etype(trussinfo);
258144177Salfred	/*
259144177Salfred	 * At this point, it's a simple loop, waiting for the process to
260144177Salfred	 * stop, finding out why, printing out why, and then continuing it.
261144177Salfred	 * All of the grunt work is done in the support routines.
262144177Salfred	 */
26331567Ssef
264144177Salfred	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
265101285Smdodd
266144177Salfred	do {
267144177Salfred		int val = 0;
26831567Ssef
269144177Salfred		if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
270144177Salfred			warn("PIOCWAIT top of loop");
271144177Salfred		else {
272144177Salfred			switch(i = pfs.why) {
273144177Salfred			case S_SCE:
274144177Salfred				funcs->enter_syscall(trussinfo, pfs.val);
275144177Salfred				clock_gettime(CLOCK_REALTIME,
276144177Salfred				    &trussinfo->before);
277144177Salfred				break;
278144177Salfred			case S_SCX:
279144177Salfred				clock_gettime(CLOCK_REALTIME,
280144177Salfred				    &trussinfo->after);
281144177Salfred				/*
282144177Salfred				 * This is so we don't get two messages for
283144177Salfred				 * an exec -- one for the S_EXEC, and one for
284144177Salfred				 * the syscall exit.  It also, conveniently,
285144177Salfred				 * ensures that the first message printed out
286144177Salfred				 * isn't the return-from-syscall used to
287144177Salfred				 * create the process.
288144177Salfred				 */
289144177Salfred				if (in_exec) {
290144177Salfred					in_exec = 0;
291144177Salfred					break;
292144177Salfred				}
29331567Ssef
294144177Salfred				if (trussinfo->in_fork &&
295144177Salfred				    (trussinfo->flags & FOLLOWFORKS)) {
296144177Salfred					int childpid;
297101283Smdodd
298144177Salfred					trussinfo->in_fork = 0;
299144177Salfred					childpid =
300144177Salfred					    funcs->exit_syscall(trussinfo,
301144177Salfred						pfs.val);
302101283Smdodd
303144177Salfred					/*
304144177Salfred					 * Fork a new copy of ourself to trace
305144177Salfred					 * the child of the original traced
306144177Salfred					 * process.
307144177Salfred					 */
308144177Salfred					if (fork() == 0) {
309144177Salfred						trussinfo->pid = childpid;
310144177Salfred						goto START_TRACE;
311144177Salfred					}
312144177Salfred					break;
313144177Salfred				}
314144177Salfred				funcs->exit_syscall(trussinfo, pfs.val);
315144177Salfred				break;
316144177Salfred			case S_SIG:
317144177Salfred				signame = strsig(pfs.val);
318144177Salfred				fprintf(trussinfo->outfile,
319144177Salfred				    "SIGNAL %lu (%s)\n", pfs.val,
320144177Salfred				    signame == NULL ? "?" : signame);
321144177Salfred				free(signame);
322144177Salfred				sigexit = pfs.val;
323144177Salfred				break;
324144177Salfred			case S_EXIT:
325144177Salfred				fprintf(trussinfo->outfile,
326144177Salfred				    "process exit, rval = %lu\n", pfs.val);
327144177Salfred				break;
328144177Salfred			case S_EXEC:
329144177Salfred				funcs = set_etype(trussinfo);
330144177Salfred				in_exec = 1;
331144177Salfred				break;
332144177Salfred			default:
333144177Salfred				fprintf(trussinfo->outfile,
334144177Salfred				    "Process stopped because of:  %d\n", i);
335144177Salfred				break;
336144177Salfred			}
337144177Salfred		}
338144177Salfred		if (ioctl(Procfd, PIOCCONT, val) == -1) {
339144177Salfred			if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
340144177Salfred				break;
341144177Salfred			else
342144177Salfred				warn("PIOCCONT");
343144177Salfred		}
344144177Salfred	} while (pfs.why != S_EXIT);
345144177Salfred	fflush(trussinfo->outfile);
346144177Salfred	if (sigexit) {
347144177Salfred		struct rlimit rlp;
348101283Smdodd
349144177Salfred		rlp.rlim_cur = 0;
350144177Salfred		rlp.rlim_max = 0;
351144177Salfred		setrlimit(RLIMIT_CORE, &rlp);
352144177Salfred		(void) signal(sigexit, SIG_DFL);
353144177Salfred		(void) kill(getpid(), sigexit);
354101283Smdodd	}
355144177Salfred	return (0);
35631567Ssef}
357