diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-09-08 09:23:18 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-09-08 09:23:18 +0000 |
commit | 16fb6017a5f6c5e49140db451622f61d6942a243 (patch) | |
tree | 698366fc6923bb8edd961998f7dc78fd19f301c2 /usr.sbin | |
parent | 5ad7685892257c6025b202460cb65fdd042ed020 (diff) |
use -lkvm, various other fixes; thorpej
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/trpt/Makefile | 5 | ||||
-rw-r--r-- | usr.sbin/trpt/trpt.8 | 30 | ||||
-rw-r--r-- | usr.sbin/trpt/trpt.c | 312 | ||||
-rw-r--r-- | usr.sbin/trsp/Makefile | 5 | ||||
-rw-r--r-- | usr.sbin/trsp/trsp.8 | 53 | ||||
-rw-r--r-- | usr.sbin/trsp/trsp.c | 360 |
6 files changed, 420 insertions, 345 deletions
diff --git a/usr.sbin/trpt/Makefile b/usr.sbin/trpt/Makefile index 9e013aa195b..6a45e86b843 100644 --- a/usr.sbin/trpt/Makefile +++ b/usr.sbin/trpt/Makefile @@ -1,9 +1,10 @@ -# from: @(#)Makefile 5.3 (Berkeley) 5/11/90 -# $Id: Makefile,v 1.1 1995/10/18 08:48:29 deraadt Exp $ +# $OpenBSD: Makefile,v 1.2 1997/09/08 09:23:14 deraadt Exp $ PROG= trpt MAN= trpt.8 BINGRP= kmem BINMODE=2555 +DPADD= ${LIBKVM} +LDADD= -lkvm .include <bsd.prog.mk> diff --git a/usr.sbin/trpt/trpt.8 b/usr.sbin/trpt/trpt.8 index 3aa39364283..8e1b4790f55 100644 --- a/usr.sbin/trpt/trpt.8 +++ b/usr.sbin/trpt/trpt.8 @@ -1,5 +1,7 @@ -.\" Copyright (c) 1983, 1991 The Regents of the University of California. -.\" All rights reserved. +.\" $OpenBSD: trpt.8,v 1.5 1997/09/08 09:23:14 deraadt Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -29,10 +31,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)trpt.8 6.5 (Berkeley) 3/16/91 -.\" $Id: trpt.8,v 1.4 1997/01/30 08:23:34 deraadt Exp $ +.\" @(#)trpt.8 8.2 (Berkeley) 12/11/93 .\" -.Dd March 16, 1991 +.Dd December 11, 1993 .Dt TRPT 8 .Os BSD 4.2 .Sh NAME @@ -46,9 +47,8 @@ .Op Fl p Ar hex-address .Op Fl s .Op Fl t -.Oo -.Ar system Op Ar core -.Oc +.Op Fl N Ar system +.Op Fl M Ar core .Sh DESCRIPTION .Nm Trpt interrogates the buffer of @@ -92,6 +92,16 @@ sequencing information. in addition to the normal output, print the values for all timers at each point in the trace. +.It Fl M Ar core +Extract values associated with the name list from +.Pa core +instead of the default +.Pa /dev/kmem . +.It Fl N Ar system +Extract the name list from +.Pa system +instead of the default +.Pa /bsd . .El .Pp The recommended use of @@ -119,10 +129,6 @@ many sockets using the debugging option, the option may be useful in checking to see if any trace records are present for the socket in question. -.Pp -If debugging is being performed on a system or -core file other than the default, the last two -arguments may be used to supplant the defaults. .Sh FILES .Bl -tag -width /dev/kmem -compact .It Pa /dev/kmem diff --git a/usr.sbin/trpt/trpt.c b/usr.sbin/trpt/trpt.c index 82fa3a92052..f3f1fd78922 100644 --- a/usr.sbin/trpt/trpt.c +++ b/usr.sbin/trpt/trpt.c @@ -1,7 +1,46 @@ -/* - * Copyright (c) 1983, 1988 Regents of the University of California. +/* $OpenBSD: trpt.c,v 1.6 1997/09/08 09:23:15 deraadt Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. * All rights reserved. * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +/* + * Copyright (c) 1983, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -33,24 +72,16 @@ #ifndef lint char copyright[] = -"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\ - All rights reserved.\n"; +"@(#) Copyright (c) 1983, 1988, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)trpt.c 5.14 (Berkeley) 7/1/91";*/ -static char rcsid[] = "$Id: trpt.c,v 1.5 1997/01/15 23:44:26 millert Exp $"; +static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93"; #endif /* not lint */ #include <sys/param.h> #include <sys/queue.h> -#if BSD >= 199103 -#define NEWVM -#endif -#ifndef NEWVM -#include <machine/pte.h> -#include <sys/vmmac.h> -#endif #include <sys/socket.h> #include <sys/socketvar.h> #define PRUREQUESTS @@ -78,44 +109,51 @@ static char rcsid[] = "$Id: trpt.c,v 1.5 1997/01/15 23:44:26 millert Exp $"; #include <arpa/inet.h> +#include <err.h> #include <stdio.h> #include <errno.h> +#include <kvm.h> #include <nlist.h> #include <paths.h> +#include <limits.h> +#include <stdlib.h> +#include <unistd.h> struct nlist nl[] = { #define N_TCP_DEBUG 0 { "_tcp_debug" }, #define N_TCP_DEBX 1 { "_tcp_debx" }, -#ifndef NEWVM -#define N_SYSMAP 2 - { "_Sysmap" }, -#define N_SYSSIZE 3 - { "_Syssize" }, -#endif - { "" }, + { NULL }, }; -#ifndef NEWVM -static struct pte *Sysmap; -#endif static caddr_t tcp_pcbs[TCP_NDEBUG]; static n_time ntime; -static int aflag, kflag, memf, follow, sflag, tflag; +static int aflag, follow, sflag, tflag; +extern char *__progname; + +int main __P((int, char *[])); +void dotrace __P((caddr_t)); +void tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *, + struct tcpiphdr *, int)); +int numeric __P((const void *, const void *)); +void usage __P((void)); + +kvm_t *kd; + +int main(argc, argv) int argc; - char **argv; + char *argv[]; { - extern char *optarg; - extern int optind; - int ch, i, jflag, npcbs, numeric(); - char *system, *core, *malloc(); - off_t lseek(); + int ch, i, jflag, npcbs; + char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX]; + + system = core = NULL; jflag = npcbs = 0; - while ((ch = getopt(argc, argv, "afjp:st")) != -1) + while ((ch = getopt(argc, argv, "afjp:st")) != -1) { switch (ch) { case 'a': ++aflag; @@ -128,12 +166,12 @@ main(argc, argv) ++jflag; break; case 'p': - if (npcbs >= TCP_NDEBUG) { - fputs("trpt: too many pcb's specified\n", - stderr); - exit(1); - } - (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]); + if (npcbs >= TCP_NDEBUG) + errx(1, "too many pcbs specified"); + errno = 0; + tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16); + if (*cp != '\0' || errno == ERANGE) + errx(1, "invalid address: %s", optarg); break; case 's': ++sflag; @@ -141,86 +179,58 @@ main(argc, argv) case 't': ++tflag; break; + case 'N': + system = optarg; + break; + case 'M': + core = optarg; + break; case '?': default: - (void)fprintf(stderr, -"usage: trpt [-afjst] [-p hex-address] [system [core]]\n"); - exit(1); + usage(); + /* NOTREACHED */ } + } argc -= optind; argv += optind; - core = _PATH_KMEM; - if (argc > 0) { - system = *argv; - argc--, argv++; - if (argc > 0) { - core = *argv; - argc--, argv++; - ++kflag; - } - } - else - system = _PATH_UNIX; + if (argc) + usage(); /* - * Discard setgid priviledges if not the running kernel so that bad + * Discard setgid privileged if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. */ - if (!strcmp(core, _PATH_KMEM) || !strcmp(system, _PATH_UNIX)) { + if (core != NULL || system != NULL) { setegid(getgid()); setgid(getgid()); } - if (nlist(system, nl) < 0 || !nl[0].n_value) { - fprintf(stderr, "trpt: %s: no namelist\n", system); - exit(1); - } - if ((memf = open(core, O_RDONLY)) < 0) { - perror(core); - exit(2); - } - if (kflag) { -#ifdef NEWVM - fputs("trpt: can't do core files yet\n", stderr); - exit(1); -#else - off_t off; - - Sysmap = (struct pte *) - malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte))); - if (!Sysmap) { - fputs("trpt: can't get memory for Sysmap.\n", stderr); - exit(1); - } - off = nl[N_SYSMAP].n_value & ~KERNBASE; - (void)lseek(memf, off, L_SET); - (void)read(memf, (char *)Sysmap, - (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte))); -#endif - } - (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET); - if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) != - sizeof(tcp_debx)) { - perror("trpt: tcp_debx"); - exit(3); - } - (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET); - if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) != - sizeof(tcp_debug)) { - perror("trpt: tcp_debug"); - exit(3); - } + kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf); + if (kd == NULL) + errx(1, "can't open kmem: %s", errbuf); + + if (kvm_nlist(kd, nl)) + errx(2, "%s: no namelist", system ? system : _PATH_UNIX); + + if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx, + sizeof(tcp_debx)) != sizeof(tcp_debx)) + errx(3, "tcp_debx: %s", kvm_geterr(kd)); + + if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug, + sizeof(tcp_debug)) != sizeof(tcp_debug)) + errx(3, "tcp_debug: %s", kvm_geterr(kd)); + /* * If no control blocks have been specified, figure * out how many distinct one we have and summarize * them in tcp_pcbs for sorting the trace records * below. */ - if (!npcbs) { + if (npcbs == 0) { for (i = 0; i < TCP_NDEBUG; i++) { - register struct tcp_debug *td = &tcp_debug[i]; - register int j; + struct tcp_debug *td = &tcp_debug[i]; + int j; if (td->td_tcb == 0) continue; @@ -230,7 +240,7 @@ main(argc, argv) if (j >= npcbs) tcp_pcbs[npcbs++] = td->td_tcb; } - if (!npcbs) + if (npcbs == 0) exit(0); } qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric); @@ -242,30 +252,34 @@ main(argc, argv) fputs(", ", stdout); } putchar('\n'); - } - else for (i = 0; i < npcbs; i++) { - printf("\n%lx:\n", (long)tcp_pcbs[i]); - dotrace(tcp_pcbs[i]); + } else { + for (i = 0; i < npcbs; i++) { + printf("\n%lx:\n", (long)tcp_pcbs[i]); + dotrace(tcp_pcbs[i]); + } } exit(0); } +void dotrace(tcpcb) - register caddr_t tcpcb; + caddr_t tcpcb; { - register struct tcp_debug *td; - register int i; + struct tcp_debug *td; int prev_debx = tcp_debx; + int i; -again: if (--tcp_debx < 0) + again: + if (--tcp_debx < 0) tcp_debx = TCP_NDEBUG - 1; for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) { td = &tcp_debug[i]; if (tcpcb && td->td_tcb != tcpcb) continue; ntime = ntohl(td->td_time); - tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, - &td->td_ti, td->td_req); + tcp_trace(td->td_act, td->td_ostate, + (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti, + td->td_req); if (i == tcp_debx) goto done; } @@ -274,28 +288,27 @@ again: if (--tcp_debx < 0) if (tcpcb && td->td_tcb != tcpcb) continue; ntime = ntohl(td->td_time); - tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, - &td->td_ti, td->td_req); + tcp_trace(td->td_act, td->td_ostate, + (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti, + td->td_req); } -done: if (follow) { + done: + if (follow) { prev_debx = tcp_debx + 1; if (prev_debx >= TCP_NDEBUG) prev_debx = 0; do { sleep(1); - (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET); - if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) != - sizeof(tcp_debx)) { - perror("trpt: tcp_debx"); - exit(3); - } + if (kvm_read(kd, nl[N_TCP_DEBX].n_value, + (char *)&tcp_debx, sizeof(tcp_debx)) != + sizeof(tcp_debx)) + errx(3, "tcp_debx: %s", kvm_geterr(kd)); } while (tcp_debx == prev_debx); - (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET); - if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) != - sizeof(tcp_debug)) { - perror("trpt: tcp_debug"); - exit(3); - } + + if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug, + sizeof(tcp_debug)) != sizeof(tcp_debug)) + errx(3, "tcp_debug: %s", kvm_geterr(kd)); + goto again; } } @@ -304,6 +317,7 @@ done: if (follow) { * Tcp debug routines */ /*ARGSUSED*/ +void tcp_trace(act, ostate, atp, tp, ti, req) short act, ostate; struct tcpcb *atp, *tp; @@ -313,7 +327,7 @@ tcp_trace(act, ostate, atp, tp, ti, req) tcp_seq seq, ack; int flags, len, win, timer; - printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate], + printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate], tanames[act]); switch (act) { case TA_INPUT: @@ -330,18 +344,18 @@ tcp_trace(act, ostate, atp, tp, ti, req) len = ti->ti_len; win = ti->ti_win; if (act == TA_OUTPUT) { - seq = ntohl(seq); - ack = ntohl(ack); - len = ntohs(len); - win = ntohs(win); + NTOHL(seq); + NTOHL(ack); + NTOHS(len); + NTOHS(win); } if (act == TA_OUTPUT) len -= sizeof(struct tcphdr); if (len) - printf("[%lx..%lx)", seq, seq + len); + printf("[%x..%x)", seq, seq + len); else - printf("%lx", seq); - printf("@%lx", ack); + printf("%x", seq); + printf("@%x", ack); if (win) printf("(win=%x)", win); flags = ti->ti_flags; @@ -374,10 +388,10 @@ tcp_trace(act, ostate, atp, tp, ti, req) /* print out internal state of tp !?! */ printf("\n"); if (sflag) { - printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n", + printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n", tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, tp->snd_max); - printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1, + printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1, tp->snd_wl2, tp->snd_wnd); } /* print out timers? */ @@ -398,23 +412,29 @@ tcp_trace(act, ostate, atp, tp, ti, req) } } -numeric(c1, c2) - caddr_t *c1, *c2; +int +numeric(v1, v2) + const void *v1, *v2; { - return(*c1 - *c2); + const caddr_t *c1 = v1; + const caddr_t *c2 = v2; + int rv; + + if (*c1 < *c2) + rv = -1; + else if (*c1 > *c2) + rv = 1; + else + rv = 0; + + return (rv); } -klseek(fd, base, off) - int fd, off; - off_t base; +void +usage() { - off_t lseek(); -#ifndef NEWVM - if (kflag) { /* get kernel pte */ - base &= ~KERNBASE; - base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET); - } -#endif - (void)lseek(fd, base, off); + (void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]" + " [-N system] [-M core]\n", __progname); + exit(1); } diff --git a/usr.sbin/trsp/Makefile b/usr.sbin/trsp/Makefile index 0298a246738..82ed7d15237 100644 --- a/usr.sbin/trsp/Makefile +++ b/usr.sbin/trsp/Makefile @@ -1,10 +1,11 @@ -# from: @(#)Makefile 5.3 (Berkeley) 5/11/90 -# $Id: Makefile,v 1.1 1995/10/18 08:48:30 deraadt Exp $ +# $OpenBSD: Makefile,v 1.2 1997/09/08 09:23:16 deraadt Exp $ PROG= trsp MAN= trsp.8 BINGRP= kmem BINMODE=2555 +DPADD= ${LIBKVM} +LDADD= -lkvm .include <bsd.prog.mk> diff --git a/usr.sbin/trsp/trsp.8 b/usr.sbin/trsp/trsp.8 index 43900219c08..2bff603ee9f 100644 --- a/usr.sbin/trsp/trsp.8 +++ b/usr.sbin/trsp/trsp.8 @@ -1,5 +1,7 @@ -.\" Copyright (c) 1985, 1991 The Regents of the University of California. -.\" All rights reserved. +.\" $OpenBSD: trsp.8,v 1.4 1997/09/08 09:23:16 deraadt Exp $ +.\" +.\" Copyright (c) 1985, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -29,27 +31,26 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)trsp.8 6.4 (Berkeley) 3/16/91 -.\" $Id: trsp.8,v 1.3 1997/01/30 08:23:35 deraadt Exp $ +.\" @(#)trsp.8 8.1 (Berkeley) 6/6/93 .\" -.Dd March 16, 1991 +.Dd June 6, 1993 .Dt TRSP 8 -.Os BSD 4.2 +.Os BSD 4.3 .Sh NAME .Nm trsp .Nd transliterate sequenced packet protocol trace .Sh SYNOPSIS .Nm trsp .Op Fl a -.Op Fl s -.Op Fl t .Op Fl j .Op Fl p Ar hex-address -.Oo -.Ar system Op Ar core -.Oc +.Op Fl s +.Op Fl t +.Op Fl z +.Op Fl N Ar system +.Op Fl M Ar core .Sh DESCRIPTION -.Xr Trpt +.Nm Trsp interrogates the buffer of .Tn SPP trace records created @@ -78,16 +79,28 @@ Just give a list of the protocol control block addresses for which there are trace records. .It Fl p Show only trace records associated with the protocol -control block at the given address, +control block at the given address .Ar hex-address . .It Fl s -in addition to the normal output, +In addition to the normal output, print a detailed description of the packet sequencing information. .It Fl t -in addition to the normal output, +In addition to the normal output, print the values for all timers at each -point in the trace, +point in the trace. +.It Fl z +Clear the in-kernel debugging buffers. +.It Fl M Ar core +Extract values associated with the name list from +.Pa core +instead of the default +.Pa /dev/kmem . +.It Fl N Ar system +Extract the name list from +.Pa system +instead of the default +.Pa /bsd . .El .Pp The recommended use of @@ -111,10 +124,6 @@ many sockets using the debugging option, the option may be useful in checking to see if any trace records are present for the socket in question. -.Pp -If debugging is being performed on a system or -core file other than the default, the last two -arguments may be used to supplant the defaults. .Sh FILES .Bl -tag -width /dev/kmem -compact .It Pa /dev/kmem @@ -124,13 +133,15 @@ default system namelist .El .Sh SEE ALSO .Xr netstat 1 , -.Xr setsockopt 2 +.Xr setsockopt 2 , +.Xr trpt 8 .Sh DIAGNOSTICS .Bl -tag -width Ds .It Sy no namelist When the system image doesn't contain the proper symbols to find the trace buffer; others which should be self explanatory. +.El .Sh BUGS Should also print the data for each input or output, but this is not saved in the trace record. diff --git a/usr.sbin/trsp/trsp.c b/usr.sbin/trsp/trsp.c index ff161d40add..157423fb408 100644 --- a/usr.sbin/trsp/trsp.c +++ b/usr.sbin/trsp/trsp.c @@ -1,7 +1,46 @@ -/* - * Copyright (c) 1985 Regents of the University of California. +/* $OpenBSD: trsp.c,v 1.5 1997/09/08 09:23:17 deraadt Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. * All rights reserved. * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +/* + * Copyright (c) 1985, 1993 + * The Regents of the University of California. All rights reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -33,13 +72,12 @@ #ifndef lint char copyright[] = -"@(#) Copyright (c) 1985 Regents of the University of California.\n\ - All rights reserved.\n"; +"@(#) Copyright (c) 1985, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)trsp.c 6.8 (Berkeley) 3/2/91";*/ -static char rcsid[] = "$Id: trsp.c,v 1.4 1996/12/22 03:29:12 deraadt Exp $"; +static char sccsid[] = "@(#)trsp.c 8.1 (Berkeley) 6/6/93"; #endif /* not lint */ #include <sys/cdefs.h> @@ -68,142 +106,142 @@ static char rcsid[] = "$Id: trsp.c,v 1.4 1996/12/22 03:29:12 deraadt Exp $"; #define SANAMES #include <netns/spp_debug.h> +#include <err.h> +#include <fcntl.h> #include <stdio.h> #include <errno.h> +#include <kvm.h> #include <nlist.h> #include <paths.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> -unsigned long ntime; +u_int32_t ntime; int sflag; int tflag; int jflag; int aflag; int zflag; -int numeric(); struct nlist nl[] = { +#define N_SPP_DEBUG 0 { "_spp_debug" }, +#define N_SPP_DEBX 1 { "_spp_debx" }, - 0 + { NULL }, }; + struct spp_debug spp_debug[SPP_NDEBUG]; caddr_t spp_pcbs[SPP_NDEBUG]; int spp_debx; +kvm_t *kd; + +extern char *__progname; + +int main __P((int, char *[])); +void dotrace __P((caddr_t)); +int numeric __P((const void *, const void *)); +void spp_trace __P((short, short, struct sppcb *, struct sppcb *, + struct spidp *, int)); +void usage __P((void)); + +int main(argc, argv) int argc; - char **argv; + char *argv[]; { - int i, mask = 0, npcbs = 0; - char *system, *core; + int ch, i, npcbs = 0; + char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX]; - system = _PATH_UNIX; - core = _PATH_KMEM; + system = core = NULL; - argc--, argv++; -again: - if (argc > 0 && !strcmp(*argv, "-a")) { - aflag++, argc--, argv++; - goto again; - } - if (argc > 0 && !strcmp(*argv, "-z")) { - zflag++, argc--, argv++; - goto again; - } - if (argc > 0 && !strcmp(*argv, "-s")) { - sflag++, argc--, argv++; - goto again; - } - if (argc > 0 && !strcmp(*argv, "-t")) { - tflag++, argc--, argv++; - goto again; - } - if (argc > 0 && !strcmp(*argv, "-j")) { - jflag++, argc--, argv++; - goto again; - } - if (argc > 0 && !strcmp(*argv, "-p")) { - argc--, argv++; - if (argc < 1) { - fprintf(stderr, "-p: missing sppcb address\n"); - exit(1); - } - if (npcbs >= SPP_NDEBUG) { - fprintf(stderr, "-p: too many pcb's specified\n"); - exit(1); + while ((ch = getopt(argc, argv, "azstjp:N:M:")) != -1) { + switch (ch) { + case 'a': + ++aflag; + break; + case 'z': + ++zflag; + break; + case 's': + ++sflag; + break; + case 't': + ++tflag; + break; + case 'j': + ++jflag; + break; + case 'p': + if (npcbs >= SPP_NDEBUG) + errx(1, "too many pcbs specified"); + errno = 0; + spp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16); + if (*cp != '\0' || errno == ERANGE) + errx(1, "invalid address: %s", optarg); + break; + case 'N': + system = optarg; + break; + case 'M': + core = optarg; + break; + default: + usage(); } - sscanf(*argv, "%x", &spp_pcbs[npcbs++]); - argc--, argv++; - goto again; - } - if (argc > 0) { - system = *argv; - argc--, argv++; - mask++; - } - if (argc > 0) { - core = *argv; - argc--, argv++; - mask++; } + argc -= optind; + argv += optind; + + if (argc) + usage(); + /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. */ - if (!strcmp(system, _PATH_UNIX) || !strcmp(core, _PATH_KMEM)) { + if (core != NULL || system != NULL) { setegid(getgid()); setgid(getgid()); } - (void) nlist(system, nl); - if (nl[0].n_value == 0) { - fprintf(stderr, "trsp: %s: no namelist\n", system); - exit(1); - } - (void) close(0); - if (open(core, 0) < 0) { - fprintf(stderr, "trsp: "); perror(core); - exit(2); - } - if (mask) { - nl[0].n_value &= 0x7fffffff; - nl[1].n_value &= 0x7fffffff; - } - (void) lseek(0, nl[1].n_value, 0); - if (read(0, &spp_debx, sizeof (spp_debx)) != sizeof (spp_debx)) { - fprintf(stderr, "trsp: "); perror("spp_debx"); - exit(3); - } + kd = kvm_openfiles(system, core, NULL, zflag ? O_RDWR : O_RDONLY, + errbuf); + if (kd == NULL) + errx(1, "can't open kmem: %s", errbuf); + + if (kvm_nlist(kd, nl)) + errx(2, "%s: no namelist", system ? system : _PATH_UNIX); + + if (kvm_read(kd, nl[N_SPP_DEBX].n_value, &spp_debx, + sizeof(spp_debx)) != sizeof(spp_debx)) + errx(3, "spp_debx: %s", kvm_geterr(kd)); printf("spp_debx=%d\n", spp_debx); - (void) lseek(0, nl[0].n_value, 0); - if (read(0, spp_debug, sizeof (spp_debug)) != sizeof (spp_debug)) { - fprintf(stderr, "trsp: "); perror("spp_debug"); - exit(3); - } + + if (kvm_read(kd, nl[N_SPP_DEBUG].n_value, spp_debug, + sizeof(spp_debug)) != sizeof(spp_debug)) + errx(3, "spp_debug: %s", kvm_geterr(kd)); + /* * Here, we just want to clear out the old trace data and start over. */ if (zflag) { - char *cp = (char *) spp_debug, - *cplim = cp + sizeof(spp_debug); - (void) close(0); - if (open(core, 2) < 0) { - fprintf(stderr, "trsp: "); perror(core); - exit(2); - } - while(cp < cplim) *cp++ = 0; - (void) lseek(0, nl[0].n_value, 0); - if (write(0, spp_debug, sizeof (spp_debug)) != sizeof (spp_debug)) { - fprintf(stderr, "trsp: "); perror("spp_debug"); - exit(3); - } - (void) lseek(0, nl[1].n_value, 0); spp_debx = 0; - if (write(0, &spp_debx, sizeof (spp_debx)) != sizeof (spp_debx)) { - fprintf(stderr, "trsp: "); perror("spp_debx"); - exit(3); - } + (void) memset(spp_debug, 0, sizeof(spp_debug)); + + if (kvm_write(kd, nl[N_SPP_DEBX].n_value, &spp_debx, + sizeof(spp_debx)) != sizeof(spp_debx)) + errx(4, "write spp_debx: %s\n", kvm_geterr(kd)); + + if (kvm_write(kd, nl[N_SPP_DEBUG].n_value, spp_debug, + sizeof(spp_debug)) != sizeof(spp_debug)) + errx(4, "write spp_debug: %s\n", kvm_geterr(kd)); + exit(0); } + /* * If no control blocks have been specified, figure * out how many distinct one we have and summarize @@ -212,8 +250,8 @@ again: */ if (npcbs == 0) { for (i = 0; i < SPP_NDEBUG; i++) { - register int j; - register struct spp_debug *sd = &spp_debug[i]; + struct spp_debug *sd = &spp_debug[i]; + int j; if (sd->sd_cb == 0) continue; @@ -226,76 +264,84 @@ again: } qsort(spp_pcbs, npcbs, sizeof (caddr_t), numeric); if (jflag) { - char *cp = ""; + cp = ""; for (i = 0; i < npcbs; i++) { - printf("%s%x", cp, spp_pcbs[i]); + printf("%s%lx", cp, (long)spp_pcbs[i]); cp = ", "; } if (*cp) putchar('\n'); - exit(0); - } - for (i = 0; i < npcbs; i++) { - printf("\n%x:\n", spp_pcbs[i]); - dotrace(spp_pcbs[i]); + } else { + for (i = 0; i < npcbs; i++) { + printf("\n%lx:\n", (long)spp_pcbs[i]); + dotrace(spp_pcbs[i]); + } } exit(0); } +void dotrace(sppcb) - register caddr_t sppcb; + caddr_t sppcb; { - register int i; - register struct spp_debug *sd; + struct spp_debug *sd; + int i; for (i = spp_debx % SPP_NDEBUG; i < SPP_NDEBUG; i++) { sd = &spp_debug[i]; if (sppcb && sd->sd_cb != sppcb) continue; ntime = ntohl(sd->sd_time); - spp_trace(sd->sd_act, sd->sd_ostate, sd->sd_cb, &sd->sd_sp, - &sd->sd_si, sd->sd_req); + spp_trace(sd->sd_act, sd->sd_ostate, (struct sppcb *)sd->sd_cb, + &sd->sd_sp, &sd->sd_si, sd->sd_req); } for (i = 0; i < spp_debx % SPP_NDEBUG; i++) { sd = &spp_debug[i]; if (sppcb && sd->sd_cb != sppcb) continue; ntime = ntohl(sd->sd_time); - spp_trace(sd->sd_act, sd->sd_ostate, sd->sd_cb, &sd->sd_sp, - &sd->sd_si, sd->sd_req); + spp_trace(sd->sd_act, sd->sd_ostate, (struct sppcb *)sd->sd_cb, + &sd->sd_sp, &sd->sd_si, sd->sd_req); } } -ptime(ms) - int ms; +int +numeric(v1, v2) + const void *v1, *v2; { + const caddr_t *c1 = v1; + const caddr_t *c2 = v2; + int rv; - printf("%03d ", (ms/10) % 1000); -} + if (*c1 < *c2) + rv = -1; + else if (*c1 > *c2) + rv = 1; + else + rv = 0; -numeric(c1, c2) - caddr_t *c1, *c2; -{ - - return (*c1 - *c2); + return (rv); } +void spp_trace(act, ostate, asp, sp, si, req) short act, ostate; struct sppcb *asp, *sp; struct spidp *si; int req; { - u_short seq, ack, len, alo; - int flags, timer; + u_int16_t seq, ack, len, alo; + int flags; char *cp; - if(ostate >= TCP_NSTATES) ostate = 0; - if(act > SA_DROP) act = SA_DROP; + if (ostate >= TCP_NSTATES) + ostate = 0; + if (act > SA_DROP) + act = SA_DROP; printf("\n"); - ptime(ntime); - printf("%s:%s", tcpstates[ostate], sanames[act]); + printf("%03d %s:%s", (ntime/10) % 1000, tcpstates[ostate], + sanames[act]); if (si != 0) { seq = si->si_seq; @@ -305,22 +351,19 @@ spp_trace(act, ostate, asp, sp, si, req) switch (act) { case SA_RESPOND: case SA_OUTPUT: - seq = ntohs(seq); - ack = ntohs(ack); - alo = ntohs(alo); - len = ntohs(len); + NTOHS(seq); + NTOHS(ack); + NTOHS(alo); + NTOHS(len); case SA_INPUT: case SA_DROP: - if (aflag) { - printf("\n\tsna="); - ns_printhost(&si->si_sna); - printf("\tdna="); - ns_printhost(&si->si_dna); - } + if (aflag) + printf("\n\tsna=%s\tdna=%s", + ns_ntoa(si->si_sna), ns_ntoa(si->si_dna)); printf("\n\t"); #define p1(name, f) { \ printf("%s = %x, ", name, f); \ - } +} p1("seq", seq); p1("ack", ack); p1("alo", alo); @@ -334,7 +377,7 @@ spp_trace(act, ostate, asp, sp, si, req) } \ } if (flags) { - char *cp = "<"; + cp = "<"; pf("SP_SP", SP_SP); pf("SP_SA", SP_SA); pf("SP_OB", SP_OB); @@ -348,10 +391,8 @@ spp_trace(act, ostate, asp, sp, si, req) p2("sid", si->si_sid); p2("did", si->si_did); p2("dt", si->si_dt); - printf("\n\tsna="); - ns_printhost(&si->si_sna); - printf("\tdna="); - ns_printhost(&si->si_dna); + printf("\n\tsna=%s\tdna=%s", ns_ntoa(si->si_sna), + ns_ntoa(si->si_dna)); } } if(act == SA_USER) { @@ -384,7 +425,7 @@ spp_trace(act, ostate, asp, sp, si, req) } flags = sp->s_flags; if (flags || sp->s_oobflags) { - char *cp = "<"; + cp = "<"; pf("ACKNOW", SF_ACKNOW); pf("DELACK", SF_DELACK); pf("HI", SF_HI); @@ -401,8 +442,9 @@ spp_trace(act, ostate, asp, sp, si, req) } /* print out timers? */ if (tflag) { - char *cp = "\t"; - register int i; + int i; + + cp = "\t"; printf("\n\tTIMERS: "); p3("idle", sp->s_idle); @@ -421,17 +463,11 @@ spp_trace(act, ostate, asp, sp, si, req) } } -ns_printhost(p) -register struct ns_addr *p; +void +usage() { - printf("<net:%x%x,host:%4.4x%4.4x%4.4x,port:%x>", - p->x_net.s_net[0], - p->x_net.s_net[1], - p->x_host.s_host[0], - p->x_host.s_host[1], - p->x_host.s_host[2], - p->x_port); - + fprintf(stderr, "usage: %s [-azstj] [-p hex-address]" + " [-N system] [-M core]\n", __progname); + exit(1); } - |