1997-12-20 18:40:43 +00:00
|
|
|
/*
|
|
|
|
* Copryight 1997 Sean Eric Fagan
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Sean Eric Fagan
|
|
|
|
* 4. Neither the name of the author may be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-09-07 15:50:43 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1998-01-05 07:30:26 +00:00
|
|
|
|
1997-12-06 05:23:12 +00:00
|
|
|
/*
|
|
|
|
* The main module for truss. Suprisingly simple, but, then, the other
|
|
|
|
* files handle the bulk of the work. And, of course, the kernel has to
|
|
|
|
* do a lot of the work :).
|
|
|
|
*/
|
|
|
|
|
2001-10-22 02:02:00 +00:00
|
|
|
#include <sys/param.h>
|
2003-12-28 01:20:03 +00:00
|
|
|
#include <sys/types.h>
|
2002-10-06 21:46:04 +00:00
|
|
|
#include <sys/time.h>
|
2003-12-28 01:20:03 +00:00
|
|
|
#include <sys/resource.h>
|
2007-04-10 04:03:34 +00:00
|
|
|
#include <sys/sysctl.h>
|
2001-10-22 02:02:00 +00:00
|
|
|
|
2004-07-17 19:19:36 +00:00
|
|
|
#include <ctype.h>
|
1998-01-05 07:30:26 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
1997-12-06 05:23:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-08-06 12:46:14 +00:00
|
|
|
#include <time.h>
|
1997-12-06 14:39:30 +00:00
|
|
|
#include <unistd.h>
|
1997-12-06 05:23:12 +00:00
|
|
|
|
2002-08-04 00:46:48 +00:00
|
|
|
#include "truss.h"
|
2001-12-11 23:34:02 +00:00
|
|
|
#include "extern.h"
|
1997-12-06 05:23:12 +00:00
|
|
|
|
2007-07-28 23:00:42 +00:00
|
|
|
#define MAXARGS 6
|
1997-12-06 05:23:12 +00:00
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
static void
|
1998-01-05 07:30:26 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2005-03-27 12:47:04 +00:00
|
|
|
fprintf(stderr, "%s\n%s\n",
|
2006-01-02 08:36:25 +00:00
|
|
|
"usage: truss [-faedDS] [-o file] [-s strsize] -p pid",
|
|
|
|
" truss [-faedDS] [-o file] [-s strsize] command [args]");
|
2005-03-27 12:47:04 +00:00
|
|
|
exit(1);
|
1997-12-06 05:23:12 +00:00
|
|
|
}
|
|
|
|
|
1998-09-07 05:49:43 +00:00
|
|
|
/*
|
|
|
|
* WARNING! "FreeBSD a.out" must be first, or set_etype will not
|
|
|
|
* work correctly.
|
|
|
|
*/
|
1997-12-06 05:23:12 +00:00
|
|
|
struct ex_types {
|
2005-03-27 12:47:04 +00:00
|
|
|
const char *type;
|
|
|
|
void (*enter_syscall)(struct trussinfo *, int);
|
|
|
|
long (*exit_syscall)(struct trussinfo *, int);
|
1997-12-06 05:23:12 +00:00
|
|
|
} ex_types[] = {
|
2004-06-12 22:49:06 +00:00
|
|
|
#ifdef __amd64__
|
2005-03-27 12:47:04 +00:00
|
|
|
{ "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
|
2008-05-16 15:34:06 +00:00
|
|
|
{ "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
|
|
|
|
{ "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
|
2004-06-12 22:49:06 +00:00
|
|
|
#endif
|
1998-10-03 00:43:05 +00:00
|
|
|
#ifdef __i386__
|
2005-03-27 12:47:04 +00:00
|
|
|
{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
|
|
|
|
{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
|
|
|
|
{ "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
|
|
|
|
{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
|
2002-08-04 17:57:01 +00:00
|
|
|
#endif
|
2002-11-10 00:59:13 +00:00
|
|
|
#ifdef __ia64__
|
2005-03-27 12:47:04 +00:00
|
|
|
{ "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
|
2002-11-10 00:59:13 +00:00
|
|
|
#endif
|
2006-01-05 05:57:47 +00:00
|
|
|
#ifdef __powerpc__
|
|
|
|
{ "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
|
|
|
|
{ "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
|
|
|
|
#endif
|
2002-08-04 17:57:01 +00:00
|
|
|
#ifdef __sparc64__
|
2005-03-27 12:47:04 +00:00
|
|
|
{ "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
|
1998-10-03 00:43:05 +00:00
|
|
|
#endif
|
2005-03-27 12:47:04 +00:00
|
|
|
{ 0, 0, 0 },
|
1997-12-06 05:23:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the execution type. This is called after every exec, and when
|
2007-04-10 04:03:34 +00:00
|
|
|
* a process is first monitored.
|
1997-12-06 05:23:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct ex_types *
|
2005-03-27 12:47:04 +00:00
|
|
|
set_etype(struct trussinfo *trussinfo)
|
|
|
|
{
|
|
|
|
struct ex_types *funcs;
|
|
|
|
char progt[32];
|
2007-04-10 04:03:34 +00:00
|
|
|
|
|
|
|
size_t len = sizeof(progt);
|
|
|
|
int mib[4];
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC;
|
|
|
|
mib[2] = KERN_PROC_SV_NAME;
|
|
|
|
mib[3] = trussinfo->pid;
|
|
|
|
error = sysctl(mib, 4, progt, &len, NULL, 0);
|
|
|
|
if (error != 0)
|
|
|
|
err(2, "can not get etype");
|
2005-03-27 12:47:04 +00:00
|
|
|
|
|
|
|
for (funcs = ex_types; funcs->type; funcs++)
|
|
|
|
if (!strcmp(funcs->type, progt))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (funcs->type == NULL) {
|
|
|
|
funcs = &ex_types[0];
|
|
|
|
warn("execution type %s is not supported -- using %s",
|
|
|
|
progt, funcs->type);
|
|
|
|
}
|
|
|
|
return (funcs);
|
1997-12-06 05:23:12 +00:00
|
|
|
}
|
|
|
|
|
2004-07-17 19:19:36 +00:00
|
|
|
char *
|
|
|
|
strsig(int sig)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
if (sig > 0 && sig < NSIG) {
|
|
|
|
int i;
|
|
|
|
asprintf(&ret, "sig%s", sys_signame[sig]);
|
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
|
|
|
for (i = 0; ret[i] != '\0'; ++i)
|
|
|
|
ret[i] = toupper(ret[i]);
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
1998-01-05 07:30:26 +00:00
|
|
|
int
|
2005-03-27 12:47:04 +00:00
|
|
|
main(int ac, char **av)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int i;
|
|
|
|
char **command;
|
|
|
|
struct ex_types *funcs;
|
2007-06-26 22:42:37 +00:00
|
|
|
int initial_open;
|
2005-03-27 12:53:25 +00:00
|
|
|
char *fname;
|
2005-03-27 12:47:04 +00:00
|
|
|
struct trussinfo *trussinfo;
|
|
|
|
char *signame;
|
|
|
|
|
2005-03-27 12:53:25 +00:00
|
|
|
fname = NULL;
|
|
|
|
initial_open = 1;
|
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
/* Initialize the trussinfo struct */
|
|
|
|
trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
|
|
|
|
if (trussinfo == NULL)
|
|
|
|
errx(1, "malloc() failed");
|
|
|
|
bzero(trussinfo, sizeof(struct trussinfo));
|
2007-04-10 04:03:34 +00:00
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
trussinfo->outfile = stderr;
|
2006-01-02 08:36:25 +00:00
|
|
|
trussinfo->strsize = 32;
|
2007-04-10 04:03:34 +00:00
|
|
|
trussinfo->pr_why = S_NONE;
|
|
|
|
trussinfo->curthread = NULL;
|
|
|
|
SLIST_INIT(&trussinfo->threadlist);
|
2006-01-02 08:36:25 +00:00
|
|
|
while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) {
|
2005-03-27 12:47:04 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'p': /* specified pid */
|
|
|
|
trussinfo->pid = atoi(optarg);
|
2007-06-26 22:42:37 +00:00
|
|
|
/* make sure i don't trace me */
|
|
|
|
if(trussinfo->pid == getpid()) {
|
|
|
|
fprintf(stderr, "attempt to grab self.\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
2005-03-27 12:47:04 +00:00
|
|
|
break;
|
|
|
|
case 'f': /* Follow fork()'s */
|
|
|
|
trussinfo->flags |= FOLLOWFORKS;
|
|
|
|
break;
|
|
|
|
case 'a': /* Print execve() argument strings. */
|
|
|
|
trussinfo->flags |= EXECVEARGS;
|
|
|
|
break;
|
|
|
|
case 'e': /* Print execve() environment strings. */
|
|
|
|
trussinfo->flags |= EXECVEENVS;
|
|
|
|
break;
|
|
|
|
case 'd': /* Absolute timestamps */
|
|
|
|
trussinfo->flags |= ABSOLUTETIMESTAMPS;
|
|
|
|
break;
|
|
|
|
case 'D': /* Relative timestamps */
|
|
|
|
trussinfo->flags |= RELATIVETIMESTAMPS;
|
|
|
|
break;
|
|
|
|
case 'o': /* Specified output file */
|
|
|
|
fname = optarg;
|
|
|
|
break;
|
2006-01-02 08:36:25 +00:00
|
|
|
case 's': /* Specified string size */
|
|
|
|
trussinfo->strsize = atoi(optarg);
|
|
|
|
break;
|
2005-03-27 12:47:04 +00:00
|
|
|
case 'S': /* Don't trace signals */
|
|
|
|
trussinfo->flags |= NOSIGS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ac -= optind; av += optind;
|
|
|
|
if ((trussinfo->pid == 0 && ac == 0) ||
|
|
|
|
(trussinfo->pid != 0 && ac != 0))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (fname != NULL) { /* Use output file */
|
|
|
|
if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
|
|
|
|
errx(1, "cannot open %s", fname);
|
|
|
|
}
|
1997-12-06 05:23:12 +00:00
|
|
|
|
|
|
|
/*
|
2005-03-27 12:47:04 +00:00
|
|
|
* If truss starts the process itself, it will ignore some signals --
|
|
|
|
* they should be passed off to the process, which may or may not
|
|
|
|
* exit. If, however, we are examining an already-running process,
|
|
|
|
* then we restore the event mask on these same signals.
|
1997-12-06 05:23:12 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
if (trussinfo->pid == 0) { /* Start a command ourselves */
|
|
|
|
command = av;
|
|
|
|
trussinfo->pid = setup_and_wait(command);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
} else {
|
2007-04-10 04:03:34 +00:00
|
|
|
start_tracing(trussinfo->pid);
|
2005-03-27 12:47:04 +00:00
|
|
|
signal(SIGINT, restore_proc);
|
|
|
|
signal(SIGTERM, restore_proc);
|
|
|
|
signal(SIGQUIT, restore_proc);
|
1997-12-06 05:23:12 +00:00
|
|
|
}
|
2002-08-04 01:02:52 +00:00
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, if we started the process, it is stopped waiting to
|
|
|
|
* be woken up, either in exit() or in execve().
|
|
|
|
*/
|
|
|
|
|
|
|
|
START_TRACE:
|
2002-08-04 00:46:48 +00:00
|
|
|
funcs = set_etype(trussinfo);
|
2007-04-10 04:03:34 +00:00
|
|
|
|
|
|
|
initial_open = 0;
|
2005-03-27 12:47:04 +00:00
|
|
|
/*
|
|
|
|
* At this point, it's a simple loop, waiting for the process to
|
|
|
|
* stop, finding out why, printing out why, and then continuing it.
|
|
|
|
* All of the grunt work is done in the support routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
|
|
|
|
|
|
|
|
do {
|
- Add decoding of kse_release, kevent, sigprocmask, unmount, socket, getrusage,
rename, __getcwd, shutdown, getrlimit, setrlimit, _umtx_lock, _umtx_unlock,
pathconf, truncate, ftruncate, kill
- Decode more arguments of open, mprot, *stat, and fcntl.
- Convert all constant-macro and bitfield decoding to lookup tables; much
cleaner than previous code.
- Print the timestamp of process exit and signal reception when -d or -D are in
use
- Try six times with 1/2 second delay to debug the child
PR: bin/52190 (updated)
Submitted by: Dan Nelson <dnelson@allantgroup.com>
Approved by: alfred
2006-05-15 21:18:28 +00:00
|
|
|
struct timespec timediff;
|
2007-04-10 04:03:34 +00:00
|
|
|
waitevent(trussinfo);
|
|
|
|
|
|
|
|
switch(i = trussinfo->pr_why) {
|
|
|
|
case S_SCE:
|
|
|
|
funcs->enter_syscall(trussinfo, MAXARGS);
|
|
|
|
clock_gettime(CLOCK_REALTIME,
|
|
|
|
&trussinfo->before);
|
|
|
|
break;
|
|
|
|
case S_SCX:
|
|
|
|
clock_gettime(CLOCK_REALTIME,
|
|
|
|
&trussinfo->after);
|
|
|
|
|
|
|
|
if (trussinfo->curthread->in_fork &&
|
|
|
|
(trussinfo->flags & FOLLOWFORKS)) {
|
|
|
|
int childpid;
|
|
|
|
|
|
|
|
trussinfo->curthread->in_fork = 0;
|
|
|
|
childpid =
|
|
|
|
funcs->exit_syscall(trussinfo,
|
|
|
|
trussinfo->pr_data);
|
2005-03-27 12:47:04 +00:00
|
|
|
|
|
|
|
/*
|
2007-04-10 04:03:34 +00:00
|
|
|
* Fork a new copy of ourself to trace
|
|
|
|
* the child of the original traced
|
|
|
|
* process.
|
2005-03-27 12:47:04 +00:00
|
|
|
*/
|
2007-04-10 04:03:34 +00:00
|
|
|
if (fork() == 0) {
|
|
|
|
trussinfo->pid = childpid;
|
|
|
|
start_tracing(trussinfo->pid);
|
|
|
|
goto START_TRACE;
|
2005-03-27 12:47:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-04-10 04:03:34 +00:00
|
|
|
funcs->exit_syscall(trussinfo, MAXARGS);
|
|
|
|
break;
|
|
|
|
case S_SIG:
|
|
|
|
if (trussinfo->flags & NOSIGS)
|
2005-03-27 12:47:04 +00:00
|
|
|
break;
|
2007-04-10 04:03:34 +00:00
|
|
|
if (trussinfo->flags & FOLLOWFORKS)
|
|
|
|
fprintf(trussinfo->outfile, "%5d: ",
|
|
|
|
trussinfo->pid);
|
|
|
|
if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
|
|
|
|
timespecsubt(&trussinfo->after,
|
|
|
|
&trussinfo->start_time, &timediff);
|
|
|
|
fprintf(trussinfo->outfile, "%ld.%09ld ",
|
|
|
|
(long)timediff.tv_sec,
|
|
|
|
timediff.tv_nsec);
|
|
|
|
}
|
|
|
|
if (trussinfo->flags & RELATIVETIMESTAMPS) {
|
|
|
|
timespecsubt(&trussinfo->after,
|
|
|
|
&trussinfo->before, &timediff);
|
|
|
|
fprintf(trussinfo->outfile, "%ld.%09ld ",
|
|
|
|
(long)timediff.tv_sec,
|
|
|
|
timediff.tv_nsec);
|
|
|
|
}
|
|
|
|
signame = strsig(trussinfo->pr_data);
|
|
|
|
fprintf(trussinfo->outfile,
|
|
|
|
"SIGNAL %u (%s)\n", trussinfo->pr_data,
|
|
|
|
signame == NULL ? "?" : signame);
|
|
|
|
free(signame);
|
|
|
|
break;
|
|
|
|
case S_EXIT:
|
|
|
|
if (trussinfo->flags & FOLLOWFORKS)
|
|
|
|
fprintf(trussinfo->outfile, "%5d: ",
|
|
|
|
trussinfo->pid);
|
|
|
|
if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
|
|
|
|
timespecsubt(&trussinfo->after,
|
|
|
|
&trussinfo->start_time, &timediff);
|
|
|
|
fprintf(trussinfo->outfile, "%ld.%09ld ",
|
|
|
|
(long)timediff.tv_sec,
|
|
|
|
timediff.tv_nsec);
|
|
|
|
}
|
|
|
|
if (trussinfo->flags & RELATIVETIMESTAMPS) {
|
|
|
|
timespecsubt(&trussinfo->after,
|
|
|
|
&trussinfo->before, &timediff);
|
|
|
|
fprintf(trussinfo->outfile, "%ld.%09ld ",
|
|
|
|
(long)timediff.tv_sec, timediff.tv_nsec);
|
|
|
|
}
|
|
|
|
fprintf(trussinfo->outfile,
|
|
|
|
"process exit, rval = %u\n", trussinfo->pr_data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2005-03-27 12:47:04 +00:00
|
|
|
}
|
2007-04-10 04:03:34 +00:00
|
|
|
} while (trussinfo->pr_why != S_EXIT);
|
2005-03-27 12:47:04 +00:00
|
|
|
fflush(trussinfo->outfile);
|
2007-04-10 04:03:34 +00:00
|
|
|
|
2005-03-27 12:47:04 +00:00
|
|
|
return (0);
|
1997-12-06 05:23:12 +00:00
|
|
|
}
|