main.c revision 200462
1191005Sdelphij/*-
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 200462 2009-12-13 03:14:06Z delphij $");
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>
42123916Scracauer#include <sys/types.h>
43104581Smike#include <sys/time.h>
44123916Scracauer#include <sys/resource.h>
45168569Sdelphij#include <sys/sysctl.h>
46191005Sdelphij#include <sys/wait.h>
4785301Sdes
48132306Salfred#include <ctype.h>
4932275Scharnier#include <err.h>
50200462Sdelphij#include <errno.h>
51200462Sdelphij#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"
61192025Sdds#include "syscall.h"
6231567Ssef
63171645Smarcel#define MAXARGS 6
6431567Ssef
65144177Salfredstatic void
6632275Scharnierusage(void)
6732275Scharnier{
68144177Salfred	fprintf(stderr, "%s\n%s\n",
69192025Sdds	    "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid",
70192025Sdds	    "       truss [-cfaedDS] [-o file] [-s strsize] command [args]");
71144177Salfred	exit(1);
7231567Ssef}
7331567Ssef
7438897Ssef/*
7538897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not
7638897Ssef * work correctly.
7738897Ssef */
7831567Ssefstruct ex_types {
79144177Salfred	const char *type;
80144177Salfred	void (*enter_syscall)(struct trussinfo *, int);
81144177Salfred	long (*exit_syscall)(struct trussinfo *, int);
8231567Ssef} ex_types[] = {
83130394Sdwmalone#ifdef __amd64__
84144177Salfred	{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
85179051Sjhb	{ "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
86179051Sjhb	{ "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
87130394Sdwmalone#endif
8839908Ssef#ifdef __i386__
89144177Salfred	{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
90144177Salfred	{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
91144177Salfred	{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
92144177Salfred	{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9339908Ssef#endif
94106716Smarcel#ifdef __ia64__
95144177Salfred	{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
96106716Smarcel#endif
97154047Sgrehan#ifdef __powerpc__
98154047Sgrehan	{ "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
99154047Sgrehan	{ "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
100154047Sgrehan#endif
101101320Sjake#ifdef __sparc64__
102144177Salfred	{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
103101320Sjake#endif
104188628Simp#ifdef __mips__
105188628Simp	{ "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit },
106188628Simp	{ "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit },
107188628Simp	{ "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX
108188628Simp#endif
109144177Salfred	{ 0, 0, 0 },
11031567Ssef};
11131567Ssef
11231567Ssef/*
11331567Ssef * Set the execution type.  This is called after every exec, and when
114168569Sdelphij * a process is first monitored.
11531567Ssef */
11631567Ssef
11731567Ssefstatic struct ex_types *
118144177Salfredset_etype(struct trussinfo *trussinfo)
119144177Salfred{
120144177Salfred	struct ex_types *funcs;
121144177Salfred	char progt[32];
122168569Sdelphij
123168569Sdelphij	size_t len = sizeof(progt);
124168569Sdelphij	int mib[4];
125168569Sdelphij	int error;
12631567Ssef
127168569Sdelphij	mib[0] = CTL_KERN;
128168569Sdelphij	mib[1] = KERN_PROC;
129168569Sdelphij	mib[2] = KERN_PROC_SV_NAME;
130168569Sdelphij	mib[3] = trussinfo->pid;
131168569Sdelphij	error = sysctl(mib, 4, progt, &len, NULL, 0);
132168569Sdelphij	if (error != 0)
133168569Sdelphij		err(2, "can not get etype");
13431567Ssef
135144177Salfred	for (funcs = ex_types; funcs->type; funcs++)
136144177Salfred		if (!strcmp(funcs->type, progt))
137144177Salfred			break;
13831567Ssef
139144177Salfred	if (funcs->type == NULL) {
140144177Salfred		funcs = &ex_types[0];
141144177Salfred		warn("execution type %s is not supported -- using %s",
142144177Salfred		    progt, funcs->type);
143144177Salfred	}
144144177Salfred	return (funcs);
14531567Ssef}
14631567Ssef
147132306Salfredchar *
148132306Salfredstrsig(int sig)
149132306Salfred{
150132306Salfred	char *ret;
151132306Salfred
152132306Salfred	ret = NULL;
153132306Salfred	if (sig > 0 && sig < NSIG) {
154132306Salfred		int i;
155132306Salfred		asprintf(&ret, "sig%s", sys_signame[sig]);
156132306Salfred		if (ret == NULL)
157132306Salfred			return (NULL);
158132306Salfred		for (i = 0; ret[i] != '\0'; ++i)
159132306Salfred			ret[i] = toupper(ret[i]);
160132306Salfred	}
161132306Salfred	return (ret);
162132306Salfred}
163132306Salfred
16432275Scharnierint
165144177Salfredmain(int ac, char **av)
166144177Salfred{
167144177Salfred	int c;
168144177Salfred	int i;
169191005Sdelphij	pid_t childpid;
170191005Sdelphij	int status;
171144177Salfred	char **command;
172144177Salfred	struct ex_types *funcs;
173171055Sdelphij	int initial_open;
174144178Salfred	char *fname;
175144177Salfred	struct trussinfo *trussinfo;
176144177Salfred	char *signame;
17731567Ssef
178144178Salfred	fname = NULL;
179144178Salfred	initial_open = 1;
180144178Salfred
181144177Salfred	/* Initialize the trussinfo struct */
182192153Sdelphij	trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
183144177Salfred	if (trussinfo == NULL)
184192153Sdelphij		errx(1, "calloc() failed");
185192153Sdelphij
186144177Salfred	trussinfo->outfile = stderr;
187153963Sbrian	trussinfo->strsize = 32;
188168569Sdelphij	trussinfo->pr_why = S_NONE;
189168569Sdelphij	trussinfo->curthread = NULL;
190168569Sdelphij	SLIST_INIT(&trussinfo->threadlist);
191192025Sdds	while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) {
192144177Salfred		switch (c) {
193144177Salfred		case 'p':	/* specified pid */
194144177Salfred			trussinfo->pid = atoi(optarg);
195171055Sdelphij			/* make sure i don't trace me */
196171055Sdelphij			if(trussinfo->pid == getpid()) {
197171055Sdelphij				fprintf(stderr, "attempt to grab self.\n");
198171055Sdelphij				exit(2);
199171055Sdelphij			}
200144177Salfred			break;
201144177Salfred		case 'f': /* Follow fork()'s */
202144177Salfred			trussinfo->flags |= FOLLOWFORKS;
203144177Salfred			break;
204144177Salfred		case 'a': /* Print execve() argument strings. */
205144177Salfred			trussinfo->flags |= EXECVEARGS;
206144177Salfred			break;
207192025Sdds		case 'c': /* Count number of system calls and time. */
208192025Sdds			trussinfo->flags |= COUNTONLY;
209192025Sdds			break;
210144177Salfred		case 'e': /* Print execve() environment strings. */
211144177Salfred			trussinfo->flags |= EXECVEENVS;
212144177Salfred			break;
213144177Salfred		case 'd': /* Absolute timestamps */
214144177Salfred			trussinfo->flags |= ABSOLUTETIMESTAMPS;
215144177Salfred			break;
216144177Salfred		case 'D': /* Relative timestamps */
217144177Salfred			trussinfo->flags |= RELATIVETIMESTAMPS;
218144177Salfred			break;
219144177Salfred		case 'o':	/* Specified output file */
220144177Salfred			fname = optarg;
221144177Salfred			break;
222153963Sbrian		case 's':	/* Specified string size */
223153963Sbrian			trussinfo->strsize = atoi(optarg);
224153963Sbrian			break;
225144177Salfred		case 'S':	/* Don't trace signals */
226144177Salfred			trussinfo->flags |= NOSIGS;
227144177Salfred			break;
228144177Salfred		default:
229144177Salfred			usage();
230144177Salfred		}
231144177Salfred	}
23231567Ssef
233144177Salfred	ac -= optind; av += optind;
234144177Salfred	if ((trussinfo->pid == 0 && ac == 0) ||
235144177Salfred	    (trussinfo->pid != 0 && ac != 0))
236144177Salfred		usage();
23731567Ssef
238144177Salfred	if (fname != NULL) { /* Use output file */
239144177Salfred		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
240144177Salfred			errx(1, "cannot open %s", fname);
241144177Salfred	}
24232275Scharnier
243144177Salfred	/*
244144177Salfred	 * If truss starts the process itself, it will ignore some signals --
245144177Salfred	 * they should be passed off to the process, which may or may not
246144177Salfred	 * exit.  If, however, we are examining an already-running process,
247144177Salfred	 * then we restore the event mask on these same signals.
248144177Salfred	 */
24931567Ssef
250144177Salfred	if (trussinfo->pid == 0) {	/* Start a command ourselves */
251144177Salfred		command = av;
252144177Salfred		trussinfo->pid = setup_and_wait(command);
253144177Salfred		signal(SIGINT, SIG_IGN);
254144177Salfred		signal(SIGTERM, SIG_IGN);
255144177Salfred		signal(SIGQUIT, SIG_IGN);
256144177Salfred	} else {
257168569Sdelphij		start_tracing(trussinfo->pid);
258144177Salfred		signal(SIGINT, restore_proc);
259144177Salfred		signal(SIGTERM, restore_proc);
260144177Salfred		signal(SIGQUIT, restore_proc);
261144177Salfred	}
26231567Ssef
26331567Ssef
264144177Salfred	/*
265144177Salfred	 * At this point, if we started the process, it is stopped waiting to
266144177Salfred	 * be woken up, either in exit() or in execve().
267144177Salfred	 */
26831567Ssef
269101283SmdoddSTART_TRACE:
270168569Sdelphij	funcs = set_etype(trussinfo);
271168569Sdelphij
272144178Salfred	initial_open = 0;
273144177Salfred	/*
274144177Salfred	 * At this point, it's a simple loop, waiting for the process to
275144177Salfred	 * stop, finding out why, printing out why, and then continuing it.
276144177Salfred	 * All of the grunt work is done in the support routines.
277144177Salfred	 */
27831567Ssef
279144177Salfred	clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
280101285Smdodd
281144177Salfred	do {
282158630Spav		struct timespec timediff;
283168569Sdelphij		waitevent(trussinfo);
28431567Ssef
285168569Sdelphij		switch(i = trussinfo->pr_why) {
286168569Sdelphij		case S_SCE:
287168569Sdelphij			funcs->enter_syscall(trussinfo, MAXARGS);
288168569Sdelphij			clock_gettime(CLOCK_REALTIME,
289168569Sdelphij			    &trussinfo->before);
290168569Sdelphij			break;
291168569Sdelphij		case S_SCX:
292168569Sdelphij			clock_gettime(CLOCK_REALTIME,
293168569Sdelphij			    &trussinfo->after);
29431567Ssef
295168569Sdelphij			if (trussinfo->curthread->in_fork &&
296168569Sdelphij			    (trussinfo->flags & FOLLOWFORKS)) {
297168569Sdelphij				trussinfo->curthread->in_fork = 0;
298168569Sdelphij				childpid =
299168569Sdelphij				    funcs->exit_syscall(trussinfo,
300168569Sdelphij					trussinfo->pr_data);
301101283Smdodd
302168569Sdelphij				/*
303168569Sdelphij				 * Fork a new copy of ourself to trace
304168569Sdelphij				 * the child of the original traced
305168569Sdelphij				 * process.
306168569Sdelphij				 */
307168569Sdelphij				if (fork() == 0) {
308168569Sdelphij					trussinfo->pid = childpid;
309168569Sdelphij					start_tracing(trussinfo->pid);
310168569Sdelphij					goto START_TRACE;
311144177Salfred				}
312144177Salfred				break;
313168569Sdelphij			}
314168569Sdelphij			funcs->exit_syscall(trussinfo, MAXARGS);
315168569Sdelphij			break;
316168569Sdelphij		case S_SIG:
317168569Sdelphij			if (trussinfo->flags & NOSIGS)
318144177Salfred				break;
319168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
320168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
321168569Sdelphij				    trussinfo->pid);
322168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
323168569Sdelphij				timespecsubt(&trussinfo->after,
324168569Sdelphij				    &trussinfo->start_time, &timediff);
325168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
326168569Sdelphij				    (long)timediff.tv_sec,
327168569Sdelphij				    timediff.tv_nsec);
328144177Salfred			}
329168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
330168569Sdelphij				timespecsubt(&trussinfo->after,
331168569Sdelphij				    &trussinfo->before, &timediff);
332168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
333168569Sdelphij				    (long)timediff.tv_sec,
334168569Sdelphij				    timediff.tv_nsec);
335168569Sdelphij			}
336168569Sdelphij			signame = strsig(trussinfo->pr_data);
337168569Sdelphij			fprintf(trussinfo->outfile,
338168569Sdelphij			    "SIGNAL %u (%s)\n", trussinfo->pr_data,
339168569Sdelphij			    signame == NULL ? "?" : signame);
340168569Sdelphij			free(signame);
341168569Sdelphij			break;
342168569Sdelphij		case S_EXIT:
343192025Sdds			if (trussinfo->flags & COUNTONLY)
344192025Sdds				break;
345168569Sdelphij			if (trussinfo->flags & FOLLOWFORKS)
346168569Sdelphij				fprintf(trussinfo->outfile, "%5d: ",
347168569Sdelphij				    trussinfo->pid);
348168569Sdelphij			if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
349168569Sdelphij				timespecsubt(&trussinfo->after,
350168569Sdelphij				    &trussinfo->start_time, &timediff);
351168569Sdelphij				fprintf(trussinfo->outfile, "%ld.%09ld ",
352168569Sdelphij				    (long)timediff.tv_sec,
353168569Sdelphij				    timediff.tv_nsec);
354168569Sdelphij			}
355168569Sdelphij			if (trussinfo->flags & RELATIVETIMESTAMPS) {
356168569Sdelphij			  timespecsubt(&trussinfo->after,
357168569Sdelphij			      &trussinfo->before, &timediff);
358168569Sdelphij			  fprintf(trussinfo->outfile, "%ld.%09ld ",
359168569Sdelphij			    (long)timediff.tv_sec, timediff.tv_nsec);
360168569Sdelphij			}
361168569Sdelphij			fprintf(trussinfo->outfile,
362168569Sdelphij			    "process exit, rval = %u\n", trussinfo->pr_data);
363168569Sdelphij			break;
364168569Sdelphij		default:
365168569Sdelphij			break;
366144177Salfred		}
367168569Sdelphij	} while (trussinfo->pr_why != S_EXIT);
368191005Sdelphij
369191005Sdelphij	if (trussinfo->flags & FOLLOWFORKS)
370191005Sdelphij		do {
371191005Sdelphij			childpid = wait(&status);
372191005Sdelphij		} while (childpid != -1);
373191005Sdelphij
374192025Sdds 	if (trussinfo->flags & COUNTONLY)
375192025Sdds 		print_summary(trussinfo);
376192025Sdds
377192025Sdds	fflush(trussinfo->outfile);
378192025Sdds
379144177Salfred	return (0);
38031567Ssef}
381