add -f to follow a trace as it continues
[unix-history] / usr / src / usr.sbin / trpt / trpt.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
9a3a7863 13#ifndef lint
89586647 14static char sccsid[] = "@(#)trpt.c 5.2 (Berkeley) %G%";
5ff67f98 15#endif not lint
9a3a7863
BJ
16
17#include <sys/param.h>
18#include <sys/socket.h>
19#include <sys/socketvar.h>
20#define PRUREQUESTS
21#include <sys/protosw.h>
94a2d2a7 22
9a3a7863 23#include <net/route.h>
9a3a7863 24#include <net/if.h>
94a2d2a7
SL
25
26#include <netinet/in.h>
27#include <netinet/in_pcb.h>
28#include <netinet/in_systm.h>
29#include <netinet/ip.h>
30#include <netinet/ip_var.h>
31#include <netinet/tcp.h>
9a3a7863 32#define TCPSTATES
94a2d2a7
SL
33#include <netinet/tcp_fsm.h>
34#include <netinet/tcp_seq.h>
9a3a7863 35#define TCPTIMERS
94a2d2a7
SL
36#include <netinet/tcp_timer.h>
37#include <netinet/tcp_var.h>
38#include <netinet/tcpip.h>
9a3a7863 39#define TANAMES
94a2d2a7 40#include <netinet/tcp_debug.h>
9a3a7863 41
7d3cb675
SL
42#include <arpa/inet.h>
43
eb0fbeab 44#include <stdio.h>
94a2d2a7 45#include <errno.h>
9a3a7863
BJ
46#include <nlist.h>
47
48n_time ntime;
49int sflag;
175d35a6
SL
50int tflag;
51int jflag;
49173cdd 52int aflag;
89586647 53int follow;
175d35a6 54int numeric();
9a3a7863
BJ
55struct nlist nl[] = {
56 { "_tcp_debug" },
57 { "_tcp_debx" },
58 0
59};
60struct tcp_debug tcp_debug[TCP_NDEBUG];
175d35a6 61caddr_t tcp_pcbs[TCP_NDEBUG];
9a3a7863
BJ
62int tcp_debx;
63
64main(argc, argv)
65 int argc;
66 char **argv;
67{
175d35a6 68 int i, mask = 0, npcbs = 0;
eb0fbeab 69 char *system = "/vmunix", *core = "/dev/kmem";
9a3a7863
BJ
70
71 argc--, argv++;
72again:
49173cdd
SL
73 if (argc > 0 && !strcmp(*argv, "-a")) {
74 aflag++, argc--, argv++;
75 goto again;
76 }
89586647
MK
77 if (argc > 0 && !strcmp(*argv, "-f")) {
78 follow++, argc--, argv++;
79 goto again;
80 }
9a3a7863
BJ
81 if (argc > 0 && !strcmp(*argv, "-s")) {
82 sflag++, argc--, argv++;
83 goto again;
84 }
175d35a6
SL
85 if (argc > 0 && !strcmp(*argv, "-t")) {
86 tflag++, argc--, argv++;
87 goto again;
88 }
89 if (argc > 0 && !strcmp(*argv, "-j")) {
90 jflag++, argc--, argv++;
91 goto again;
92 }
eb0fbeab 93 if (argc > 0 && !strcmp(*argv, "-p")) {
175d35a6 94 argc--, argv++;
eb0fbeab
SL
95 if (argc < 1) {
96 fprintf(stderr, "-p: missing tcpcb address\n");
97 exit(1);
98 }
175d35a6
SL
99 if (npcbs >= TCP_NDEBUG) {
100 fprintf(stderr, "-p: too many pcb's specified\n");
101 exit(1);
102 }
103 sscanf(*argv, "%x", &tcp_pcbs[npcbs++]);
eb0fbeab
SL
104 argc--, argv++;
105 goto again;
106 }
107 if (argc > 0) {
108 system = *argv;
109 argc--, argv++;
110 mask++;
111 }
112 if (argc > 0) {
113 core = *argv;
114 argc--, argv++;
115 mask++;
116 }
117 (void) nlist(system, nl);
9a3a7863 118 if (nl[0].n_value == 0) {
eb0fbeab 119 fprintf(stderr, "trpt: %s: no namelist\n", system);
9a3a7863
BJ
120 exit(1);
121 }
eb0fbeab
SL
122 (void) close(0);
123 if (open(core, 0) < 0) {
124 fprintf(stderr, "trpt: "); perror(core);
125 exit(2);
126 }
127 if (mask) {
9a3a7863
BJ
128 nl[0].n_value &= 0x7fffffff;
129 nl[1].n_value &= 0x7fffffff;
130 }
eb0fbeab
SL
131 (void) lseek(0, nl[1].n_value, 0);
132 if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
133 fprintf(stderr, "trpt: "); perror("tcp_debx");
134 exit(3);
135 }
eb0fbeab
SL
136 (void) lseek(0, nl[0].n_value, 0);
137 if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
138 fprintf(stderr, "trpt: "); perror("tcp_debug");
139 exit(3);
140 }
175d35a6
SL
141 /*
142 * If no control blocks have been specified, figure
143 * out how many distinct one we have and summarize
144 * them in tcp_pcbs for sorting the trace records
145 * below.
146 */
147 if (npcbs == 0) {
148 for (i = 0; i < TCP_NDEBUG; i++) {
149 register int j;
150 register struct tcp_debug *td = &tcp_debug[i];
151
152 if (td->td_tcb == 0)
153 continue;
154 for (j = 0; j < npcbs; j++)
155 if (tcp_pcbs[j] == td->td_tcb)
156 break;
157 if (j >= npcbs)
158 tcp_pcbs[npcbs++] = td->td_tcb;
159 }
160 }
161 qsort(tcp_pcbs, npcbs, sizeof (caddr_t), numeric);
162 if (jflag) {
163 char *cp = "";
10ce0f6a 164
175d35a6
SL
165 for (i = 0; i < npcbs; i++) {
166 printf("%s%x", cp, tcp_pcbs[i]);
167 cp = ", ";
168 }
169 if (*cp)
170 putchar('\n');
171 exit(0);
172 }
173 for (i = 0; i < npcbs; i++) {
174 printf("\n%x:\n", tcp_pcbs[i]);
175 dotrace(tcp_pcbs[i]);
176 }
177 exit(0);
178}
179
180dotrace(tcpcb)
181 register caddr_t tcpcb;
182{
183 register int i;
184 register struct tcp_debug *td;
89586647 185 int prev_debx = tcp_debx;
175d35a6 186
89586647
MK
187again:
188 if (--tcp_debx < 0)
189 tcp_debx = TCP_NDEBUG - 1;
190 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
175d35a6 191 td = &tcp_debug[i];
eb0fbeab
SL
192 if (tcpcb && td->td_tcb != tcpcb)
193 continue;
10ce0f6a 194 ntime = ntohl(td->td_time);
9a3a7863
BJ
195 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
196 &td->td_ti, td->td_req);
89586647
MK
197 if (i == tcp_debx)
198 goto done;
9a3a7863 199 }
89586647 200 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
175d35a6 201 td = &tcp_debug[i];
eb0fbeab
SL
202 if (tcpcb && td->td_tcb != tcpcb)
203 continue;
10ce0f6a 204 ntime = ntohl(td->td_time);
9a3a7863
BJ
205 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
206 &td->td_ti, td->td_req);
207 }
89586647
MK
208done:
209 if (follow) {
210 prev_debx = tcp_debx + 1;
211 if (prev_debx >= TCP_NDEBUG)
212 prev_debx = 0;
213 do {
214 sleep(1);
215 (void) lseek(0, nl[1].n_value, 0);
216 if (read(0, &tcp_debx, sizeof(tcp_debx)) != sizeof(tcp_debx)) {
217 fprintf(stderr, "trpt: "); perror("tcp_debx");
218 exit(3);
219 }
220 } while (tcp_debx == prev_debx);
221 (void) lseek(0, nl[0].n_value, 0);
222 if (read(0, tcp_debug, sizeof(tcp_debug)) != sizeof(tcp_debug)) {
223 fprintf(stderr, "trpt: "); perror("tcp_debug");
224 exit(3);
225 }
226 goto again;
227 }
9a3a7863
BJ
228}
229
230/*
231 * Tcp debug routines
232 */
233tcp_trace(act, ostate, atp, tp, ti, req)
234 short act, ostate;
235 struct tcpcb *atp, *tp;
236 struct tcpiphdr *ti;
237 int req;
238{
239 tcp_seq seq, ack;
10ce0f6a 240 int len, flags, win, timer;
9a3a7863
BJ
241 char *cp;
242
243 ptime(ntime);
175d35a6 244 printf("%s:%s ", tcpstates[ostate], tanames[act]);
9a3a7863
BJ
245 switch (act) {
246
247 case TA_INPUT:
248 case TA_OUTPUT:
3cd6f35b 249 case TA_DROP:
49173cdd 250 if (aflag) {
7d3cb675 251 printf("(src=%s,%d, ", inet_ntoa(ti->ti_src),
49173cdd 252 ntohs(ti->ti_sport));
7d3cb675 253 printf("dst=%s,%d)", inet_ntoa(ti->ti_dst),
49173cdd
SL
254 ntohs(ti->ti_dport));
255 }
9a3a7863
BJ
256 seq = ti->ti_seq;
257 ack = ti->ti_ack;
258 len = ti->ti_len;
259 win = ti->ti_win;
9a3a7863
BJ
260 if (act == TA_OUTPUT) {
261 seq = ntohl(seq);
262 ack = ntohl(ack);
263 len = ntohs(len);
264 win = ntohs(win);
265 }
9a3a7863
BJ
266 if (act == TA_OUTPUT)
267 len -= sizeof (struct tcphdr);
268 if (len)
269 printf("[%x..%x)", seq, seq+len);
270 else
271 printf("%x", seq);
272 printf("@%x", ack);
273 if (win)
23c416d7 274 printf("(win=%x)", win);
9a3a7863
BJ
275 flags = ti->ti_flags;
276 if (flags) {
277 char *cp = "<";
278#define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
ed06dc42 279 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
9a3a7863
BJ
280 printf(">");
281 }
282 break;
283
284 case TA_USER:
10ce0f6a
SL
285 timer = req >> 8;
286 req &= 0xff;
287 printf("%s", prurequests[req]);
288 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
289 printf("<%s>", tcptimers[timer]);
9a3a7863
BJ
290 break;
291 }
292 printf(" -> %s", tcpstates[tp->t_state]);
293 /* print out internal state of tp !?! */
294 printf("\n");
295 if (sflag) {
49282770 296 printf("\trcv_nxt %x rcv_wnd %x snd_una %x snd_nxt %x snd_max %x\n",
eb0fbeab
SL
297 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
298 tp->snd_max);
299 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
300 tp->snd_wl2, tp->snd_wnd);
9a3a7863 301 }
175d35a6
SL
302 /* print out timers? */
303 if (tflag) {
304 char *cp = "\t";
305 register int i;
306
307 for (i = 0; i < TCPT_NTIMERS; i++) {
308 if (tp->t_timer[i] == 0)
309 continue;
310 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
311 if (i == TCPT_REXMT)
312 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
313 cp = ", ";
314 }
315 if (*cp != '\t')
316 putchar('\n');
317 }
9a3a7863
BJ
318}
319
320ptime(ms)
321 int ms;
322{
323
324 printf("%03d ", (ms/10) % 1000);
325}
175d35a6
SL
326
327numeric(c1, c2)
328 caddr_t *c1, *c2;
329{
330
331 return (*c1 - *c2);
332}