main.c revision 144178
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 144178 2005-03-27 12:53:25Z 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;
168144178Salfred	int in_exec, sigexit, initial_open;
169144178Salfred	char *fname;
170144177Salfred	struct trussinfo *trussinfo;
171144177Salfred	char *signame;
17231567Ssef
173144178Salfred	in_exec = 0;
174144178Salfred	sigexit = 0;
175144178Salfred	fname = NULL;
176144178Salfred	initial_open = 1;
177144178Salfred
178144177Salfred	/* Initialize the trussinfo struct */
179144177Salfred	trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
180144177Salfred	if (trussinfo == NULL)
181144177Salfred		errx(1, "malloc() failed");
182144177Salfred	bzero(trussinfo, sizeof(struct trussinfo));
183144177Salfred	trussinfo->outfile = stderr;
184101282Smdodd
185144177Salfred	while ((c = getopt(ac, av, "p:o:faedDS")) != -1) {
186144177Salfred		switch (c) {
187144177Salfred		case 'p':	/* specified pid */
188144177Salfred			trussinfo->pid = atoi(optarg);
189144177Salfred			break;
190144177Salfred		case 'f': /* Follow fork()'s */
191144177Salfred			trussinfo->flags |= FOLLOWFORKS;
192144177Salfred			break;
193144177Salfred		case 'a': /* Print execve() argument strings. */
194144177Salfred			trussinfo->flags |= EXECVEARGS;
195144177Salfred			break;
196144177Salfred		case 'e': /* Print execve() environment strings. */
197144177Salfred			trussinfo->flags |= EXECVEENVS;
198144177Salfred			break;
199144177Salfred		case 'd': /* Absolute timestamps */
200144177Salfred			trussinfo->flags |= ABSOLUTETIMESTAMPS;
201144177Salfred			break;
202144177Salfred		case 'D': /* Relative timestamps */
203144177Salfred			trussinfo->flags |= RELATIVETIMESTAMPS;
204144177Salfred			break;
205144177Salfred		case 'o':	/* Specified output file */
206144177Salfred			fname = optarg;
207144177Salfred			break;
208144177Salfred		case 'S':	/* Don't trace signals */
209144177Salfred			trussinfo->flags |= NOSIGS;
210144177Salfred			break;
211144177Salfred		default:
212144177Salfred			usage();
213144177Salfred		}
214144177Salfred	}
21531567Ssef
216144177Salfred	ac -= optind; av += optind;
217144177Salfred	if ((trussinfo->pid == 0 && ac == 0) ||
218144177Salfred	    (trussinfo->pid != 0 && ac != 0))
219144177Salfred		usage();
22031567Ssef
221144177Salfred	if (fname != NULL) { /* Use output file */
222144177Salfred		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
223144177Salfred			errx(1, "cannot open %s", fname);
224144177Salfred	}
22532275Scharnier
226144177Salfred	/*
227144177Salfred	 * If truss starts the process itself, it will ignore some signals --
228144177Salfred	 * they should be passed off to the process, which may or may not
229144177Salfred	 * exit.  If, however, we are examining an already-running process,
230144177Salfred	 * then we restore the event mask on these same signals.
231144177Salfred	 */
23231567Ssef
233144177Salfred	if (trussinfo->pid == 0) {	/* Start a command ourselves */
234144177Salfred		command = av;
235144177Salfred		trussinfo->pid = setup_and_wait(command);
236144177Salfred		signal(SIGINT, SIG_IGN);
237144177Salfred		signal(SIGTERM, SIG_IGN);
238144177Salfred		signal(SIGQUIT, SIG_IGN);
239144177Salfred	} else {
240144177Salfred		signal(SIGINT, restore_proc);
241144177Salfred		signal(SIGTERM, restore_proc);
242144177Salfred		signal(SIGQUIT, restore_proc);
243144177Salfred	}
24431567Ssef
24531567Ssef
246144177Salfred	/*
247144177Salfred	 * At this point, if we started the process, it is stopped waiting to
248144177Salfred	 * be woken up, either in exit() or in execve().
249144177Salfred	 */
25031567Ssef
251101283SmdoddSTART_TRACE:
252144177Salfred	Procfd = start_tracing(
253144178Salfred	    trussinfo->pid, initial_open,
254144178Salfred	    S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
255144177Salfred	    ((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
256144177Salfred	    ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
257144178Salfred	initial_open = 0;
258144177Salfred	if (Procfd == -1)
259144177Salfred		return (0);
26055707Ssef
261144177Salfred	pfs.why = 0;
26231567Ssef
263144177Salfred	funcs = set_etype(trussinfo);
264144177Salfred	/*
265144177Salfred	 * At this point, it's a simple loop, waiting for the process to
266144177Salfred	 * stop, finding out why, printing out why, and then continuing it.
267144177Salfred	 * All of the grunt work is done in the support routines.
268144177Salfred	 */
26931567Ssef
270144177Salfred	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
271101285Smdodd
272144177Salfred	do {
273144177Salfred		int val = 0;
27431567Ssef
275144177Salfred		if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
276144177Salfred			warn("PIOCWAIT top of loop");
277144177Salfred		else {
278144177Salfred			switch(i = pfs.why) {
279144177Salfred			case S_SCE:
280144177Salfred				funcs->enter_syscall(trussinfo, pfs.val);
281144177Salfred				clock_gettime(CLOCK_REALTIME,
282144177Salfred				    &trussinfo->before);
283144177Salfred				break;
284144177Salfred			case S_SCX:
285144177Salfred				clock_gettime(CLOCK_REALTIME,
286144177Salfred				    &trussinfo->after);
287144177Salfred				/*
288144177Salfred				 * This is so we don't get two messages for
289144177Salfred				 * an exec -- one for the S_EXEC, and one for
290144177Salfred				 * the syscall exit.  It also, conveniently,
291144177Salfred				 * ensures that the first message printed out
292144177Salfred				 * isn't the return-from-syscall used to
293144177Salfred				 * create the process.
294144177Salfred				 */
295144177Salfred				if (in_exec) {
296144177Salfred					in_exec = 0;
297144177Salfred					break;
298144177Salfred				}
29931567Ssef
300144177Salfred				if (trussinfo->in_fork &&
301144177Salfred				    (trussinfo->flags & FOLLOWFORKS)) {
302144177Salfred					int childpid;
303101283Smdodd
304144177Salfred					trussinfo->in_fork = 0;
305144177Salfred					childpid =
306144177Salfred					    funcs->exit_syscall(trussinfo,
307144177Salfred						pfs.val);
308101283Smdodd
309144177Salfred					/*
310144177Salfred					 * Fork a new copy of ourself to trace
311144177Salfred					 * the child of the original traced
312144177Salfred					 * process.
313144177Salfred					 */
314144177Salfred					if (fork() == 0) {
315144177Salfred						trussinfo->pid = childpid;
316144177Salfred						goto START_TRACE;
317144177Salfred					}
318144177Salfred					break;
319144177Salfred				}
320144177Salfred				funcs->exit_syscall(trussinfo, pfs.val);
321144177Salfred				break;
322144177Salfred			case S_SIG:
323144177Salfred				signame = strsig(pfs.val);
324144177Salfred				fprintf(trussinfo->outfile,
325144177Salfred				    "SIGNAL %lu (%s)\n", pfs.val,
326144177Salfred				    signame == NULL ? "?" : signame);
327144177Salfred				free(signame);
328144177Salfred				sigexit = pfs.val;
329144177Salfred				break;
330144177Salfred			case S_EXIT:
331144177Salfred				fprintf(trussinfo->outfile,
332144177Salfred				    "process exit, rval = %lu\n", pfs.val);
333144177Salfred				break;
334144177Salfred			case S_EXEC:
335144177Salfred				funcs = set_etype(trussinfo);
336144177Salfred				in_exec = 1;
337144177Salfred				break;
338144177Salfred			default:
339144177Salfred				fprintf(trussinfo->outfile,
340144177Salfred				    "Process stopped because of:  %d\n", i);
341144177Salfred				break;
342144177Salfred			}
343144177Salfred		}
344144177Salfred		if (ioctl(Procfd, PIOCCONT, val) == -1) {
345144177Salfred			if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
346144177Salfred				break;
347144177Salfred			else
348144177Salfred				warn("PIOCCONT");
349144177Salfred		}
350144177Salfred	} while (pfs.why != S_EXIT);
351144177Salfred	fflush(trussinfo->outfile);
352144177Salfred	if (sigexit) {
353144177Salfred		struct rlimit rlp;
354101283Smdodd
355144177Salfred		rlp.rlim_cur = 0;
356144177Salfred		rlp.rlim_max = 0;
357144177Salfred		setrlimit(RLIMIT_CORE, &rlp);
358144177Salfred		(void) signal(sigexit, SIG_DFL);
359144177Salfred		(void) kill(getpid(), sigexit);
360101283Smdodd	}
361144177Salfred	return (0);
36231567Ssef}
363