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.
|
|
|
|
*/
|
|
|
|
|
1998-01-05 07:30:26 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1998-01-05 07:30:26 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
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>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/pioctl.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>
|
1997-12-06 14:39:30 +00:00
|
|
|
#include <unistd.h>
|
1997-12-06 05:23:12 +00:00
|
|
|
|
2001-12-11 23:34:02 +00:00
|
|
|
#include "extern.h"
|
1997-12-06 05:23:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These should really be parameterized -- I don't like having globals,
|
|
|
|
* but this is the easiest way, right now, to deal with them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int pid = 0;
|
|
|
|
int nosigs = 0;
|
2001-08-13 21:59:04 +00:00
|
|
|
FILE *outfile;
|
1997-12-06 05:23:12 +00:00
|
|
|
int Procfd;
|
|
|
|
|
|
|
|
static inline void
|
1998-01-05 07:30:26 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n%s\n",
|
|
|
|
"usage: truss [-S] [-o file] -p pid",
|
|
|
|
" truss [-S] [-o file] command [args]");
|
1997-12-06 05:23:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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 {
|
2001-12-11 23:34:02 +00:00
|
|
|
const char *type;
|
1997-12-06 05:23:12 +00:00
|
|
|
void (*enter_syscall)(int, int);
|
|
|
|
void (*exit_syscall)(int, int);
|
|
|
|
} ex_types[] = {
|
1998-10-03 00:43:05 +00:00
|
|
|
#ifdef __alpha__
|
|
|
|
{ "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
|
|
|
|
#endif
|
|
|
|
#ifdef __i386__
|
1997-12-06 05:23:12 +00:00
|
|
|
{ "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
|
1997-12-06 14:41:41 +00:00
|
|
|
{ "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
|
1997-12-06 05:23:12 +00:00
|
|
|
{ "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
|
1998-10-03 00:43:05 +00:00
|
|
|
#endif
|
1997-12-06 05:23:12 +00:00
|
|
|
{ 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the execution type. This is called after every exec, and when
|
|
|
|
* a process is first monitored. The procfs pseudo-file "etype" has
|
|
|
|
* the execution module type -- see /proc/curproc/etype for an example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct ex_types *
|
2001-12-11 23:34:02 +00:00
|
|
|
set_etype(void) {
|
1997-12-06 05:23:12 +00:00
|
|
|
struct ex_types *funcs;
|
|
|
|
char etype[24];
|
2001-12-11 23:34:02 +00:00
|
|
|
char progt[32];
|
1997-12-06 05:23:12 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
sprintf(etype, "/proc/%d/etype", pid);
|
|
|
|
if ((fd = open(etype, O_RDONLY)) == -1) {
|
2001-12-11 23:34:02 +00:00
|
|
|
strcpy(progt, "FreeBSD a.out");
|
1997-12-06 05:23:12 +00:00
|
|
|
} else {
|
2001-12-11 23:34:02 +00:00
|
|
|
int len = read(fd, progt, sizeof(progt));
|
|
|
|
progt[len-1] = '\0';
|
1997-12-06 14:41:41 +00:00
|
|
|
close(fd);
|
1997-12-06 05:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (funcs = ex_types; funcs->type; funcs++)
|
2001-12-11 23:34:02 +00:00
|
|
|
if (!strcmp(funcs->type, progt))
|
1997-12-06 05:23:12 +00:00
|
|
|
break;
|
|
|
|
|
1998-09-07 05:49:43 +00:00
|
|
|
if (funcs == NULL) {
|
|
|
|
warn("Execution type %s is not supported -- using FreeBSD a.out\n",
|
2001-12-11 23:34:02 +00:00
|
|
|
progt);
|
1998-09-07 05:49:43 +00:00
|
|
|
funcs = &ex_types[0];
|
|
|
|
}
|
1997-12-06 05:23:12 +00:00
|
|
|
return funcs;
|
|
|
|
}
|
|
|
|
|
1998-01-05 07:30:26 +00:00
|
|
|
int
|
1997-12-06 05:23:12 +00:00
|
|
|
main(int ac, char **av) {
|
|
|
|
int c;
|
|
|
|
int i;
|
|
|
|
char **command;
|
|
|
|
struct procfs_status pfs;
|
|
|
|
struct ex_types *funcs;
|
|
|
|
int in_exec = 0;
|
1998-01-05 07:30:26 +00:00
|
|
|
char *fname = NULL;
|
1998-08-24 10:17:20 +00:00
|
|
|
int sigexit = 0;
|
1997-12-06 05:23:12 +00:00
|
|
|
|
2001-08-13 21:59:04 +00:00
|
|
|
outfile = stdout;
|
1998-01-07 06:19:50 +00:00
|
|
|
while ((c = getopt(ac, av, "p:o:S")) != -1) {
|
1997-12-06 05:23:12 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'p': /* specified pid */
|
|
|
|
pid = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'o': /* Specified output file */
|
1998-01-05 07:30:26 +00:00
|
|
|
fname = optarg;
|
1997-12-06 05:23:12 +00:00
|
|
|
break;
|
|
|
|
case 'S': /* Don't trace signals */
|
|
|
|
nosigs = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ac -= optind; av += optind;
|
1997-12-06 17:13:54 +00:00
|
|
|
if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0))
|
1997-12-06 05:23:12 +00:00
|
|
|
usage();
|
|
|
|
|
1998-01-05 07:30:26 +00:00
|
|
|
if (fname != NULL) { /* Use output file */
|
|
|
|
if ((outfile = fopen(fname, "w")) == NULL)
|
|
|
|
errx(1, "cannot open %s", fname);
|
|
|
|
}
|
|
|
|
|
1997-12-06 05:23:12 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pid == 0) { /* Start a command ourselves */
|
|
|
|
command = av;
|
|
|
|
pid = setup_and_wait(command);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
} else {
|
|
|
|
signal(SIGINT, restore_proc);
|
|
|
|
signal(SIGTERM, restore_proc);
|
|
|
|
signal(SIGQUIT, restore_proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, if we started the process, it is stopped waiting to
|
|
|
|
* be woken up, either in exit() or in execve().
|
|
|
|
*/
|
|
|
|
|
|
|
|
Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
|
|
|
|
(nosigs ? 0 : S_SIG));
|
2000-01-10 04:09:05 +00:00
|
|
|
if (Procfd == -1)
|
|
|
|
return 0;
|
|
|
|
|
1997-12-06 05:23:12 +00:00
|
|
|
pfs.why = 0;
|
|
|
|
|
|
|
|
funcs = set_etype();
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do {
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
|
1998-01-05 07:30:26 +00:00
|
|
|
warn("PIOCWAIT top of loop");
|
1997-12-06 05:23:12 +00:00
|
|
|
else {
|
|
|
|
switch(i = pfs.why) {
|
|
|
|
case S_SCE:
|
|
|
|
funcs->enter_syscall(pid, pfs.val);
|
|
|
|
break;
|
|
|
|
case S_SCX:
|
|
|
|
/*
|
|
|
|
* This is so we don't get two messages for an exec -- one
|
|
|
|
* for the S_EXEC, and one for the syscall exit. It also,
|
|
|
|
* conveniently, ensures that the first message printed out
|
|
|
|
* isn't the return-from-syscall used to create the process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (in_exec) {
|
|
|
|
in_exec = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
funcs->exit_syscall(pid, pfs.val);
|
|
|
|
break;
|
|
|
|
case S_SIG:
|
1998-07-06 21:01:54 +00:00
|
|
|
fprintf(outfile, "SIGNAL %lu\n", pfs.val);
|
1998-08-24 10:17:20 +00:00
|
|
|
sigexit = pfs.val;
|
1997-12-06 05:23:12 +00:00
|
|
|
break;
|
|
|
|
case S_EXIT:
|
1998-07-06 21:01:54 +00:00
|
|
|
fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
|
1997-12-06 05:23:12 +00:00
|
|
|
break;
|
|
|
|
case S_EXEC:
|
|
|
|
funcs = set_etype();
|
|
|
|
in_exec = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf (outfile, "Process stopped because of: %d\n", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-01-10 04:09:05 +00:00
|
|
|
if (ioctl(Procfd, PIOCCONT, val) == -1) {
|
|
|
|
if (kill(pid, 0) == -1 && errno == ESRCH)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
warn("PIOCCONT");
|
|
|
|
}
|
1997-12-06 05:23:12 +00:00
|
|
|
} while (pfs.why != S_EXIT);
|
1998-12-21 06:34:50 +00:00
|
|
|
fflush(outfile);
|
1998-08-24 10:17:20 +00:00
|
|
|
if (sigexit) {
|
|
|
|
if (sigexit == SIGQUIT)
|
|
|
|
exit(sigexit);
|
|
|
|
(void) signal(sigexit, SIG_DFL);
|
|
|
|
(void) kill(getpid(), sigexit);
|
|
|
|
}
|
1997-12-06 05:23:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|