add slstat to the list of subdirectories.
[unix-history] / usr.sbin / trpt / trpt.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1983, 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
141aeac8
GW
41static char sccsid[] = "From: @(#)trpt.c 5.14 (Berkeley) 7/1/91";
42static const char rcsid[] =
43 "$Id$";
15637ed4
RG
44#endif /* not lint */
45
46#include <sys/param.h>
47#if BSD >= 199103
48#define NEWVM
49#endif
50#ifndef NEWVM
51#include <machine/pte.h>
52#include <sys/vmmac.h>
53#endif
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#define PRUREQUESTS
57#include <sys/protosw.h>
58#include <sys/file.h>
59
60#include <net/route.h>
61#include <net/if.h>
62
63#include <netinet/in.h>
64#include <netinet/in_systm.h>
65#include <netinet/ip.h>
66#include <netinet/in_pcb.h>
67#include <netinet/ip_var.h>
68#include <netinet/tcp.h>
69#define TCPSTATES
70#include <netinet/tcp_fsm.h>
71#include <netinet/tcp_seq.h>
72#define TCPTIMERS
73#include <netinet/tcp_timer.h>
74#include <netinet/tcp_var.h>
75#include <netinet/tcpip.h>
76#define TANAMES
77#include <netinet/tcp_debug.h>
78
79#include <arpa/inet.h>
80
81#include <stdio.h>
82#include <errno.h>
83#include <nlist.h>
84#include <paths.h>
85
141aeac8
GW
86struct tcp_debug tcp_debug[TCP_NDEBUG];
87int tcp_debx;
88
15637ed4
RG
89struct nlist nl[] = {
90#define N_TCP_DEBUG 0
91 { "_tcp_debug" },
92#define N_TCP_DEBX 1
93 { "_tcp_debx" },
94#ifndef NEWVM
95#define N_SYSMAP 2
96 { "_Sysmap" },
97#define N_SYSSIZE 3
98 { "_Syssize" },
99#endif
100 { "" },
101};
102
103#ifndef NEWVM
104static struct pte *Sysmap;
105#endif
106static caddr_t tcp_pcbs[TCP_NDEBUG];
107static n_time ntime;
108static int aflag, kflag, memf, follow, sflag, tflag;
109
110main(argc, argv)
111 int argc;
112 char **argv;
113{
114 extern char *optarg;
115 extern int optind;
116 int ch, i, jflag, npcbs, numeric();
117 char *system, *core, *malloc();
118 off_t lseek();
119
120 jflag = npcbs = 0;
121 while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
122 switch (ch) {
123 case 'a':
124 ++aflag;
125 break;
126 case 'f':
127 ++follow;
128 setlinebuf(stdout);
129 break;
130 case 'j':
131 ++jflag;
132 break;
133 case 'p':
134 if (npcbs >= TCP_NDEBUG) {
135 fputs("trpt: too many pcb's specified\n",
136 stderr);
137 exit(1);
138 }
139 (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
140 break;
141 case 's':
142 ++sflag;
143 break;
144 case 't':
145 ++tflag;
146 break;
147 case '?':
148 default:
149 (void)fprintf(stderr,
150"usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
151 exit(1);
152 }
153 argc -= optind;
154 argv += optind;
155
156 core = _PATH_KMEM;
157 if (argc > 0) {
158 system = *argv;
159 argc--, argv++;
160 if (argc > 0) {
161 core = *argv;
162 argc--, argv++;
163 ++kflag;
164 }
165 }
166 else
167 system = _PATH_UNIX;
168
169 if (nlist(system, nl) < 0 || !nl[0].n_value) {
170 fprintf(stderr, "trpt: %s: no namelist\n", system);
171 exit(1);
172 }
173 if ((memf = open(core, O_RDONLY)) < 0) {
174 perror(core);
175 exit(2);
176 }
177 if (kflag) {
178#ifdef NEWVM
179 fputs("trpt: can't do core files yet\n", stderr);
180 exit(1);
181#else
182 off_t off;
183
184 Sysmap = (struct pte *)
185 malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
186 if (!Sysmap) {
187 fputs("trpt: can't get memory for Sysmap.\n", stderr);
188 exit(1);
189 }
190 off = nl[N_SYSMAP].n_value & ~KERNBASE;
191 (void)lseek(memf, off, L_SET);
192 (void)read(memf, (char *)Sysmap,
193 (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
194#endif
195 }
196 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
197 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
198 sizeof(tcp_debx)) {
199 perror("trpt: tcp_debx");
200 exit(3);
201 }
202 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
203 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
204 sizeof(tcp_debug)) {
205 perror("trpt: tcp_debug");
206 exit(3);
207 }
208 /*
209 * If no control blocks have been specified, figure
210 * out how many distinct one we have and summarize
211 * them in tcp_pcbs for sorting the trace records
212 * below.
213 */
214 if (!npcbs) {
215 for (i = 0; i < TCP_NDEBUG; i++) {
216 register struct tcp_debug *td = &tcp_debug[i];
217 register int j;
218
219 if (td->td_tcb == 0)
220 continue;
221 for (j = 0; j < npcbs; j++)
222 if (tcp_pcbs[j] == td->td_tcb)
223 break;
224 if (j >= npcbs)
225 tcp_pcbs[npcbs++] = td->td_tcb;
226 }
227 if (!npcbs)
228 exit(0);
229 }
230 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
231 if (jflag) {
232 for (i = 0;;) {
233 printf("%x", (int)tcp_pcbs[i]);
234 if (++i == npcbs)
235 break;
236 fputs(", ", stdout);
237 }
238 putchar('\n');
239 }
240 else for (i = 0; i < npcbs; i++) {
241 printf("\n%x:\n", (int)tcp_pcbs[i]);
242 dotrace(tcp_pcbs[i]);
243 }
244 exit(0);
245}
246
247dotrace(tcpcb)
248 register caddr_t tcpcb;
249{
250 register struct tcp_debug *td;
251 register int i;
252 int prev_debx = tcp_debx;
253
254again: if (--tcp_debx < 0)
255 tcp_debx = TCP_NDEBUG - 1;
256 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
257 td = &tcp_debug[i];
258 if (tcpcb && td->td_tcb != tcpcb)
259 continue;
260 ntime = ntohl(td->td_time);
261 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
262 &td->td_ti, td->td_req);
263 if (i == tcp_debx)
264 goto done;
265 }
266 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
267 td = &tcp_debug[i];
268 if (tcpcb && td->td_tcb != tcpcb)
269 continue;
270 ntime = ntohl(td->td_time);
271 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
272 &td->td_ti, td->td_req);
273 }
274done: if (follow) {
275 prev_debx = tcp_debx + 1;
276 if (prev_debx >= TCP_NDEBUG)
277 prev_debx = 0;
278 do {
279 sleep(1);
280 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
281 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
282 sizeof(tcp_debx)) {
283 perror("trpt: tcp_debx");
284 exit(3);
285 }
286 } while (tcp_debx == prev_debx);
287 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
288 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
289 sizeof(tcp_debug)) {
290 perror("trpt: tcp_debug");
291 exit(3);
292 }
293 goto again;
294 }
295}
296
297/*
298 * Tcp debug routines
299 */
300/*ARGSUSED*/
301tcp_trace(act, ostate, atp, tp, ti, req)
302 short act, ostate;
303 struct tcpcb *atp, *tp;
304 struct tcpiphdr *ti;
305 int req;
306{
307 tcp_seq seq, ack;
308 int flags, len, win, timer;
309
310 printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
311 tanames[act]);
312 switch (act) {
313 case TA_INPUT:
314 case TA_OUTPUT:
315 case TA_DROP:
316 if (aflag) {
317 printf("(src=%s,%u, ",
318 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
319 printf("dst=%s,%u)",
320 inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
321 }
322 seq = ti->ti_seq;
323 ack = ti->ti_ack;
324 len = ti->ti_len;
325 win = ti->ti_win;
326 if (act == TA_OUTPUT) {
327 seq = ntohl(seq);
328 ack = ntohl(ack);
329 len = ntohs(len);
330 win = ntohs(win);
331 }
332 if (act == TA_OUTPUT)
333 len -= sizeof(struct tcphdr);
334 if (len)
335 printf("[%lx..%lx)", seq, seq + len);
336 else
337 printf("%lx", seq);
338 printf("@%lx", ack);
339 if (win)
340 printf("(win=%x)", win);
341 flags = ti->ti_flags;
342 if (flags) {
343 register char *cp = "<";
344#define pf(flag, string) { \
345 if (ti->ti_flags&flag) { \
346 (void)printf("%s%s", cp, string); \
347 cp = ","; \
348 } \
349}
350 pf(TH_SYN, "SYN");
351 pf(TH_ACK, "ACK");
352 pf(TH_FIN, "FIN");
353 pf(TH_RST, "RST");
354 pf(TH_PUSH, "PUSH");
355 pf(TH_URG, "URG");
356 printf(">");
357 }
358 break;
359 case TA_USER:
360 timer = req >> 8;
361 req &= 0xff;
362 printf("%s", prurequests[req]);
363 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
364 printf("<%s>", tcptimers[timer]);
365 break;
366 }
367 printf(" -> %s", tcpstates[tp->t_state]);
368 /* print out internal state of tp !?! */
369 printf("\n");
370 if (sflag) {
371 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
372 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
373 tp->snd_max);
374 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
375 tp->snd_wl2, tp->snd_wnd);
376 }
377 /* print out timers? */
378 if (tflag) {
379 register char *cp = "\t";
380 register int i;
381
382 for (i = 0; i < TCPT_NTIMERS; i++) {
383 if (tp->t_timer[i] == 0)
384 continue;
385 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
386 if (i == TCPT_REXMT)
387 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
388 cp = ", ";
389 }
390 if (*cp != '\t')
391 putchar('\n');
392 }
393}
394
395numeric(c1, c2)
396 caddr_t *c1, *c2;
397{
398 return(*c1 - *c2);
399}
400
401klseek(fd, base, off)
402 int fd, off;
403 off_t base;
404{
405 off_t lseek();
406
407#ifndef NEWVM
408 if (kflag) { /* get kernel pte */
409 base &= ~KERNBASE;
410 base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
411 }
412#endif
413 (void)lseek(fd, base, off);
414}