diff options
author | mdodd <mdodd@FreeBSD.org> | 2002-08-04 00:46:48 +0000 |
---|---|---|
committer | mdodd <mdodd@FreeBSD.org> | 2002-08-04 00:46:48 +0000 |
commit | 357fc6060d6acc804f9b5950000f1dfe7054417d (patch) | |
tree | 5faa50a8195f6ac5d6ceda995a1b3603dde4a3aa /usr.bin/truss/main.c | |
parent | 8cc0839b134c422ecc8ab8449ce6badd3bc0f792 (diff) | |
download | FreeBSD-src-357fc6060d6acc804f9b5950000f1dfe7054417d.zip FreeBSD-src-357fc6060d6acc804f9b5950000f1dfe7054417d.tar.gz |
Parameterize globals.
PR: bin/25587 (in part)
MFC after: 3 weeks
Diffstat (limited to 'usr.bin/truss/main.c')
-rw-r--r-- | usr.bin/truss/main.c | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/usr.bin/truss/main.c b/usr.bin/truss/main.c index c434d92..503329f 100644 --- a/usr.bin/truss/main.c +++ b/usr.bin/truss/main.c @@ -53,16 +53,14 @@ static const char rcsid[] = #include <string.h> #include <unistd.h> +#include "truss.h" #include "extern.h" /* - * These should really be parameterized -- I don't like having globals, - * but this is the easiest way, right now, to deal with them. + * It's difficult to parameterize this because it must be + * accessible in a signal handler. */ -int pid = 0; -int nosigs = 0; -FILE *outfile; int Procfd; static __inline void @@ -80,8 +78,8 @@ usage(void) */ struct ex_types { const char *type; - void (*enter_syscall)(int, int); - void (*exit_syscall)(int, int); + void (*enter_syscall)(struct trussinfo *, int); + int (*exit_syscall)(struct trussinfo *, int); } ex_types[] = { #ifdef __alpha__ { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, @@ -101,13 +99,13 @@ struct ex_types { */ static struct ex_types * -set_etype(void) { +set_etype(struct trussinfo *trussinfo) { struct ex_types *funcs; char etype[24]; char progt[32]; int fd; - sprintf(etype, "/proc/%d/etype", pid); + sprintf(etype, "/proc/%d/etype", trussinfo->pid); if ((fd = open(etype, O_RDONLY)) == -1) { strcpy(progt, "FreeBSD a.out"); } else { @@ -138,18 +136,25 @@ main(int ac, char **av) { int in_exec = 0; char *fname = NULL; int sigexit = 0; + struct trussinfo *trussinfo; + + /* Initialize the trussinfo struct */ + trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); + if (trussinfo == NULL) + errx(1, "malloc() failed"); + bzero(trussinfo, sizeof(struct trussinfo)); + trussinfo->outfile = stderr; - outfile = stderr; while ((c = getopt(ac, av, "p:o:S")) != -1) { switch (c) { case 'p': /* specified pid */ - pid = atoi(optarg); + trussinfo->pid = atoi(optarg); break; case 'o': /* Specified output file */ fname = optarg; break; case 'S': /* Don't trace signals */ - nosigs = 1; + trussinfo->flags |= NOSIGS; break; default: usage(); @@ -157,11 +162,11 @@ main(int ac, char **av) { } ac -= optind; av += optind; - if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0)) + if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0)) usage(); if (fname != NULL) { /* Use output file */ - if ((outfile = fopen(fname, "w")) == NULL) + if ((trussinfo->outfile = fopen(fname, "w")) == NULL) errx(1, "cannot open %s", fname); } @@ -172,9 +177,9 @@ main(int ac, char **av) { * then we restore the event mask on these same signals. */ - if (pid == 0) { /* Start a command ourselves */ + if (trussinfo->pid == 0) { /* Start a command ourselves */ command = av; - pid = setup_and_wait(command); + trussinfo->pid = setup_and_wait(command); signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); signal(SIGQUIT, SIG_IGN); @@ -190,14 +195,15 @@ main(int ac, char **av) { * 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)); + Procfd = start_tracing( + trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | + ((trussinfo->flags & NOSIGS) ? 0 : S_SIG)); if (Procfd == -1) return 0; pfs.why = 0; - funcs = set_etype(); + funcs = set_etype(trussinfo); /* * At this point, it's a simple loop, waiting for the process to * stop, finding out why, printing out why, and then continuing it. @@ -212,7 +218,7 @@ main(int ac, char **av) { else { switch(i = pfs.why) { case S_SCE: - funcs->enter_syscall(pid, pfs.val); + funcs->enter_syscall(trussinfo, pfs.val); break; case S_SCX: /* @@ -226,32 +232,32 @@ main(int ac, char **av) { in_exec = 0; break; } - funcs->exit_syscall(pid, pfs.val); + funcs->exit_syscall(trussinfo, pfs.val); break; case S_SIG: - fprintf(outfile, "SIGNAL %lu\n", pfs.val); + fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val); sigexit = pfs.val; break; case S_EXIT: - fprintf (outfile, "process exit, rval = %lu\n", pfs.val); + fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val); break; case S_EXEC: - funcs = set_etype(); + funcs = set_etype(trussinfo); in_exec = 1; break; default: - fprintf (outfile, "Process stopped because of: %d\n", i); + fprintf (trussinfo->outfile, "Process stopped because of: %d\n", i); break; } } if (ioctl(Procfd, PIOCCONT, val) == -1) { - if (kill(pid, 0) == -1 && errno == ESRCH) + if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH) break; else warn("PIOCCONT"); } } while (pfs.why != S_EXIT); - fflush(outfile); + fflush(trussinfo->outfile); if (sigexit) { if (sigexit == SIGQUIT) exit(sigexit); |