install correct aliases file
[unix-history] / usr / src / usr.sbin / trpt / trpt.c
index dedf6e3..2557290 100644 (file)
@@ -1,12 +1,39 @@
+/*
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)trpt.c     4.7 %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)trpt.c     5.6 (Berkeley) %G%";
+#endif /* not lint */
+
+#include <machine/pte.h>
 
 #include <sys/param.h>
 
 #include <sys/param.h>
+#include <sys/vmmac.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #define PRUREQUESTS
 #include <sys/protosw.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #define PRUREQUESTS
 #include <sys/protosw.h>
+#include <sys/file.h>
 
 #include <net/route.h>
 #include <net/if.h>
 
 #include <net/route.h>
 #include <net/if.h>
@@ -27,98 +54,119 @@ static char sccsid[] = "@(#)trpt.c 4.7 %G%";
 #define        TANAMES
 #include <netinet/tcp_debug.h>
 
 #define        TANAMES
 #include <netinet/tcp_debug.h>
 
+#include <arpa/inet.h>
+
 #include <stdio.h>
 #include <errno.h>
 #include <nlist.h>
 
 #include <stdio.h>
 #include <errno.h>
 #include <nlist.h>
 
-n_time ntime;
-int    sflag;
-int    tflag;
-int    jflag;
-int    aflag;
-int    numeric();
-struct nlist nl[] = {
+struct nlist nl[] = {
+#define        N_TCP_DEBUG     0
        { "_tcp_debug" },
        { "_tcp_debug" },
+#define        N_TCP_DEBX      1
        { "_tcp_debx" },
        { "_tcp_debx" },
-       0
+#define        N_SYSMAP        2
+       { "_Sysmap" },
+#define        N_SYSSIZE       3
+       { "_Syssize" },
+       { "" },
 };
 };
-struct tcp_debug tcp_debug[TCP_NDEBUG];
-caddr_t        tcp_pcbs[TCP_NDEBUG];
-int    tcp_debx;
-char   *ntoa();
+
+static struct pte *Sysmap;
+static caddr_t tcp_pcbs[TCP_NDEBUG];
+static n_time ntime;
+static int aflag, kflag, memf, follow, sflag, tflag;
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
-       int i, mask = 0, npcbs = 0;
-       char *system = "/vmunix", *core = "/dev/kmem";
+       extern char *optarg;
+       extern int optind;
+       int ch, i, jflag, npcbs, numeric();
+       char *system, *core, *malloc();
+       off_t lseek();
 
 
-       argc--, argv++;
-again:
-       if (argc > 0 && !strcmp(*argv, "-a")) {
-               aflag++, 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 tcpcb address\n");
+       jflag = npcbs = 0;
+       while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
+               switch((char)ch) {
+               case 'a':
+                       ++aflag;
+                       break;
+               case 'f':
+                       ++follow;
+                       setlinebuf(stdout);
+                       break;
+               case 'j':
+                       ++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++]);
+                       break;
+               case 's':
+                       ++sflag;
+                       break;
+               case 't':
+                       ++tflag;
+                       break;
+               case '?':
+               default:
+                       fputs("usage: trpt [-afjst] [-p hex-address] [system [core]]\n", stderr);
                        exit(1);
                }
                        exit(1);
                }
-               if (npcbs >= TCP_NDEBUG) {
-                       fprintf(stderr, "-p: too many pcb's specified\n");
-                       exit(1);
-               }
-               sscanf(*argv, "%x", &tcp_pcbs[npcbs++]);
-               argc--, argv++;
-               goto again;
-       }
+       argc -= optind;
+       argv += optind;
+
+       core = "/dev/kmem";
        if (argc > 0) {
                system = *argv;
                argc--, argv++;
        if (argc > 0) {
                system = *argv;
                argc--, argv++;
-               mask++;
-       }
-       if (argc > 0) {
-               core = *argv;
-               argc--, argv++;
-               mask++;
+               if (argc > 0) {
+                       core = *argv;
+                       argc--, argv++;
+                       ++kflag;
+               }
        }
        }
-       (void) nlist(system, nl);
-       if (nl[0].n_value == 0) {
+       else
+               system = "/vmunix";
+
+       if (nlist(system, nl) < 0 || !nl[0].n_value) {
                fprintf(stderr, "trpt: %s: no namelist\n", system);
                exit(1);
        }
                fprintf(stderr, "trpt: %s: no namelist\n", system);
                exit(1);
        }
-       (void) close(0);
-       if (open(core, 0) < 0) {
-               fprintf(stderr, "trpt: "); perror(core);
+       if ((memf = open(core, O_RDONLY)) < 0) {
+               perror(core);
                exit(2);
        }
                exit(2);
        }
-       if (mask) {
-               nl[0].n_value &= 0x7fffffff;
-               nl[1].n_value &= 0x7fffffff;
+       if (kflag) {
+               off_t off;
+
+               Sysmap = (struct pte *)
+                  malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
+               if (!Sysmap) {
+                       fputs("arp: 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)));
        }
        }
-       (void) lseek(0, nl[1].n_value, 0);
-       if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
-               fprintf(stderr, "trpt: "); perror("tcp_debx");
+       (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);
        }
                exit(3);
        }
-       printf("tcp_debx=%d\n", tcp_debx);
-       (void) lseek(0, nl[0].n_value, 0);
-       if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
-               fprintf(stderr, "trpt: "); perror("tcp_debug");
+       (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);
        }
        /*
                exit(3);
        }
        /*
@@ -127,10 +175,10 @@ again:
         * them in tcp_pcbs for sorting the trace records
         * below.
         */
         * them in tcp_pcbs for sorting the trace records
         * below.
         */
-       if (npcbs == 0) {
+       if (!npcbs) {
                for (i = 0; i < TCP_NDEBUG; i++) {
                for (i = 0; i < TCP_NDEBUG; i++) {
-                       register int j;
                        register struct tcp_debug *td = &tcp_debug[i];
                        register struct tcp_debug *td = &tcp_debug[i];
+                       register int j;
 
                        if (td->td_tcb == 0)
                                continue;
 
                        if (td->td_tcb == 0)
                                continue;
@@ -140,21 +188,21 @@ again:
                        if (j >= npcbs)
                                tcp_pcbs[npcbs++] = td->td_tcb;
                }
                        if (j >= npcbs)
                                tcp_pcbs[npcbs++] = td->td_tcb;
                }
+               if (!npcbs)
+                       exit(0);
        }
        }
-       qsort(tcp_pcbs, npcbs, sizeof (caddr_t), numeric);
+       qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
        if (jflag) {
        if (jflag) {
-               char *cp = "";
-
-               for (i = 0; i < npcbs; i++) {
-                       printf("%s%x", cp, tcp_pcbs[i]);
-                       cp = ", ";
+               for (i = 0;;) {
+                       printf("%x", (int)tcp_pcbs[i]);
+                       if (++i == npcbs)
+                               break;
+                       fputs(", ", stdout);
                }
                }
-               if (*cp)
-                       putchar('\n');
-               exit(0);
+               putchar('\n');
        }
        }
-       for (i = 0; i < npcbs; i++) {
-               printf("\n%x:\n", tcp_pcbs[i]);
+       else for (i = 0; i < npcbs; i++) {
+               printf("\n%x:\n", (int)tcp_pcbs[i]);
                dotrace(tcp_pcbs[i]);
        }
        exit(0);
                dotrace(tcp_pcbs[i]);
        }
        exit(0);
@@ -163,18 +211,23 @@ again:
 dotrace(tcpcb)
        register caddr_t tcpcb;
 {
 dotrace(tcpcb)
        register caddr_t tcpcb;
 {
-       register int i;
        register struct tcp_debug *td;
        register struct tcp_debug *td;
+       register int i;
+       int prev_debx = tcp_debx;
 
 
-       for (i = 0; i < tcp_debx % TCP_NDEBUG; i++) {
+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);
                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);
+               if (i == tcp_debx)
+                       goto done;
        }
        }
-       for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
+       for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
                td = &tcp_debug[i];
                if (tcpcb && td->td_tcb != tcpcb)
                        continue;
                td = &tcp_debug[i];
                if (tcpcb && td->td_tcb != tcpcb)
                        continue;
@@ -182,11 +235,33 @@ dotrace(tcpcb)
                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, td->td_tcb, &td->td_cb,
                    &td->td_ti, td->td_req);
        }
+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);
+                       }
+               } 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);
+               }
+               goto again;
+       }
 }
 
 /*
  * Tcp debug routines
  */
 }
 
 /*
  * Tcp debug routines
  */
+/*ARGSUSED*/
 tcp_trace(act, ostate, atp, tp, ti, req)
        short act, ostate;
        struct tcpcb *atp, *tp;
 tcp_trace(act, ostate, atp, tp, ti, req)
        short act, ostate;
        struct tcpcb *atp, *tp;
@@ -194,21 +269,18 @@ tcp_trace(act, ostate, atp, tp, ti, req)
        int req;
 {
        tcp_seq seq, ack;
        int req;
 {
        tcp_seq seq, ack;
-       int len, flags, win, timer;
-       char *cp;
+       int flags, len, win, timer;
 
 
-       ptime(ntime);
-       printf("%s:%s ", tcpstates[ostate], tanames[act]);
+       printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
+           tanames[act]);
        switch (act) {
        switch (act) {
-
        case TA_INPUT:
        case TA_OUTPUT:
        case TA_INPUT:
        case TA_OUTPUT:
-               if (aflag) {
-                       printf("(src=%s,%d, ", ntoa(ti->ti_src),
-                               ntohs(ti->ti_sport));
-                       printf("dst=%s,%d)", ntoa(ti->ti_dst),
-                               ntohs(ti->ti_dport));
-               }
+       case TA_DROP:
+               if (aflag)
+                       printf("(src=%s,%u, dst=%s,%u)", inet_ntoa(ti->ti_src),
+                           ntohs(ti->ti_sport), inet_ntoa(ti->ti_dst),
+                           ntohs(ti->ti_dport));
                seq = ti->ti_seq;
                ack = ti->ti_ack;
                len = ti->ti_len;
                seq = ti->ti_seq;
                ack = ti->ti_ack;
                len = ti->ti_len;
@@ -220,23 +292,31 @@ tcp_trace(act, ostate, atp, tp, ti, req)
                        win = ntohs(win);
                }
                if (act == TA_OUTPUT)
                        win = ntohs(win);
                }
                if (act == TA_OUTPUT)
-                       len -= sizeof (struct tcphdr);
+                       len -= sizeof(struct tcphdr);
                if (len)
                if (len)
-                       printf("[%x..%x)", seq, seq+len);
+                       printf("[%lx..%lx)", seq, seq + len);
                else
                else
-                       printf("%x", seq);
-               printf("@%x", ack);
+                       printf("%lx", seq);
+               printf("@%lx", ack);
                if (win)
                if (win)
-                       printf("(win=%d)", win);
+                       printf("(win=%x)", win);
                flags = ti->ti_flags;
                if (flags) {
                flags = ti->ti_flags;
                if (flags) {
-                       char *cp = "<";
-#define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
-                       pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
+                       register char *cp = "<";
+#define        pf(flag) { \
+       if (ti->ti_flags&flag) \
+               printf("%s%s", cp, "f"); \
+               cp = ","; \
+       }
+                       pf(TH_SYN);
+                       pf(TH_ACK);
+                       pf(TH_FIN);
+                       pf(TH_RST);
+                       pf(TH_PUSH);
+                       pf(TH_URG);
                        printf(">");
                }
                break;
                        printf(">");
                }
                break;
-
        case TA_USER:
                timer = req >> 8;
                req &= 0xff;
        case TA_USER:
                timer = req >> 8;
                req &= 0xff;
@@ -249,15 +329,15 @@ tcp_trace(act, ostate, atp, tp, ti, req)
        /* print out internal state of tp !?! */
        printf("\n");
        if (sflag) {
        /* print out internal state of tp !?! */
        printf("\n");
        if (sflag) {
-               printf("\trcv_nxt %x rcv_wnd %d snd_una %x snd_nxt %x snd_max %x\n",
+               printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
                    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
                    tp->snd_max);
                    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
                    tp->snd_max);
-               printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
+               printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
                    tp->snd_wl2, tp->snd_wnd);
        }
        /* print out timers? */
        if (tflag) {
                    tp->snd_wl2, tp->snd_wnd);
        }
        /* print out timers? */
        if (tflag) {
-               char *cp = "\t";
+               register char *cp = "\t";
                register int i;
 
                for (i = 0; i < TCPT_NTIMERS; i++) {
                register int i;
 
                for (i = 0; i < TCPT_NTIMERS; i++) {
@@ -273,34 +353,21 @@ tcp_trace(act, ostate, atp, tp, ti, req)
        }
 }
 
        }
 }
 
-ptime(ms)
-       int ms;
-{
-
-       printf("%03d ", (ms/10) % 1000);
-}
-
 numeric(c1, c2)
        caddr_t *c1, *c2;
 {
 numeric(c1, c2)
        caddr_t *c1, *c2;
 {
-       
-       return (*c1 - *c2);
+       return(*c1 - *c2);
 }
 
 }
 
-/*
- * Convert network-format internet address
- * to base 256 d.d.d.d representation.
- */
-char *
-ntoa(in)
-       struct in_addr in;
+klseek(fd, base, off)
+       int fd, off;
+       off_t base;
 {
 {
-       static char b[18];
-       register char *p;
+       off_t lseek();
 
 
-       in.s_addr = ntohl(in.s_addr);
-       p = (char *)&in;
-#define        UC(b)   (((int)b)&0xff)
-       sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
-       return (b);
+       if (kflag) {    /* get kernel pte */
+               base &= ~KERNBASE;
+               base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
+       }
+       (void)lseek(fd, base, off);
 }
 }