xref: /original-bsd/usr.sbin/trpt/trpt.c (revision 67e03b52)
1 /*
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)trpt.c	5.14 (Berkeley) 07/01/91";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #if BSD >= 199103
20 #define NEWVM
21 #endif
22 #ifndef NEWVM
23 #include <machine/pte.h>
24 #include <sys/vmmac.h>
25 #endif
26 #include <sys/socket.h>
27 #include <sys/socketvar.h>
28 #define PRUREQUESTS
29 #include <sys/protosw.h>
30 #include <sys/file.h>
31 
32 #include <net/route.h>
33 #include <net/if.h>
34 
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/in_pcb.h>
39 #include <netinet/ip_var.h>
40 #include <netinet/tcp.h>
41 #define TCPSTATES
42 #include <netinet/tcp_fsm.h>
43 #include <netinet/tcp_seq.h>
44 #define	TCPTIMERS
45 #include <netinet/tcp_timer.h>
46 #include <netinet/tcp_var.h>
47 #include <netinet/tcpip.h>
48 #define	TANAMES
49 #include <netinet/tcp_debug.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <stdio.h>
54 #include <errno.h>
55 #include <nlist.h>
56 #include <paths.h>
57 
58 struct nlist nl[] = {
59 #define	N_TCP_DEBUG	0
60 	{ "_tcp_debug" },
61 #define	N_TCP_DEBX	1
62 	{ "_tcp_debx" },
63 #ifndef NEWVM
64 #define	N_SYSMAP	2
65 	{ "_Sysmap" },
66 #define	N_SYSSIZE	3
67 	{ "_Syssize" },
68 #endif
69 	{ "" },
70 };
71 
72 #ifndef NEWVM
73 static struct pte *Sysmap;
74 #endif
75 static caddr_t tcp_pcbs[TCP_NDEBUG];
76 static n_time ntime;
77 static int aflag, kflag, memf, follow, sflag, tflag;
78 
79 main(argc, argv)
80 	int argc;
81 	char **argv;
82 {
83 	extern char *optarg;
84 	extern int optind;
85 	int ch, i, jflag, npcbs, numeric();
86 	char *system, *core, *malloc();
87 	off_t lseek();
88 
89 	jflag = npcbs = 0;
90 	while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
91 		switch (ch) {
92 		case 'a':
93 			++aflag;
94 			break;
95 		case 'f':
96 			++follow;
97 			setlinebuf(stdout);
98 			break;
99 		case 'j':
100 			++jflag;
101 			break;
102 		case 'p':
103 			if (npcbs >= TCP_NDEBUG) {
104 				fputs("trpt: too many pcb's specified\n",
105 				    stderr);
106 				exit(1);
107 			}
108 			(void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
109 			break;
110 		case 's':
111 			++sflag;
112 			break;
113 		case 't':
114 			++tflag;
115 			break;
116 		case '?':
117 		default:
118 			(void)fprintf(stderr,
119 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
120 			exit(1);
121 		}
122 	argc -= optind;
123 	argv += optind;
124 
125 	core = _PATH_KMEM;
126 	if (argc > 0) {
127 		system = *argv;
128 		argc--, argv++;
129 		if (argc > 0) {
130 			core = *argv;
131 			argc--, argv++;
132 			++kflag;
133 		}
134 	}
135 	else
136 		system = _PATH_UNIX;
137 
138 	if (nlist(system, nl) < 0 || !nl[0].n_value) {
139 		fprintf(stderr, "trpt: %s: no namelist\n", system);
140 		exit(1);
141 	}
142 	if ((memf = open(core, O_RDONLY)) < 0) {
143 		perror(core);
144 		exit(2);
145 	}
146 	if (kflag) {
147 #ifdef NEWVM
148 		fputs("trpt: can't do core files yet\n", stderr);
149 		exit(1);
150 #else
151 		off_t off;
152 
153 		Sysmap = (struct pte *)
154 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
155 		if (!Sysmap) {
156 			fputs("trpt: can't get memory for Sysmap.\n", stderr);
157 			exit(1);
158 		}
159 		off = nl[N_SYSMAP].n_value & ~KERNBASE;
160 		(void)lseek(memf, off, L_SET);
161 		(void)read(memf, (char *)Sysmap,
162 		    (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
163 #endif
164 	}
165 	(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
166 	if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
167 	    sizeof(tcp_debx)) {
168 		perror("trpt: tcp_debx");
169 		exit(3);
170 	}
171 	(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
172 	if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
173 	    sizeof(tcp_debug)) {
174 		perror("trpt: tcp_debug");
175 		exit(3);
176 	}
177 	/*
178 	 * If no control blocks have been specified, figure
179 	 * out how many distinct one we have and summarize
180 	 * them in tcp_pcbs for sorting the trace records
181 	 * below.
182 	 */
183 	if (!npcbs) {
184 		for (i = 0; i < TCP_NDEBUG; i++) {
185 			register struct tcp_debug *td = &tcp_debug[i];
186 			register int j;
187 
188 			if (td->td_tcb == 0)
189 				continue;
190 			for (j = 0; j < npcbs; j++)
191 				if (tcp_pcbs[j] == td->td_tcb)
192 					break;
193 			if (j >= npcbs)
194 				tcp_pcbs[npcbs++] = td->td_tcb;
195 		}
196 		if (!npcbs)
197 			exit(0);
198 	}
199 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
200 	if (jflag) {
201 		for (i = 0;;) {
202 			printf("%x", (int)tcp_pcbs[i]);
203 			if (++i == npcbs)
204 				break;
205 			fputs(", ", stdout);
206 		}
207 		putchar('\n');
208 	}
209 	else for (i = 0; i < npcbs; i++) {
210 		printf("\n%x:\n", (int)tcp_pcbs[i]);
211 		dotrace(tcp_pcbs[i]);
212 	}
213 	exit(0);
214 }
215 
216 dotrace(tcpcb)
217 	register caddr_t tcpcb;
218 {
219 	register struct tcp_debug *td;
220 	register int i;
221 	int prev_debx = tcp_debx;
222 
223 again:	if (--tcp_debx < 0)
224 		tcp_debx = TCP_NDEBUG - 1;
225 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
226 		td = &tcp_debug[i];
227 		if (tcpcb && td->td_tcb != tcpcb)
228 			continue;
229 		ntime = ntohl(td->td_time);
230 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
231 		    &td->td_ti, td->td_req);
232 		if (i == tcp_debx)
233 			goto done;
234 	}
235 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
236 		td = &tcp_debug[i];
237 		if (tcpcb && td->td_tcb != tcpcb)
238 			continue;
239 		ntime = ntohl(td->td_time);
240 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
241 		    &td->td_ti, td->td_req);
242 	}
243 done:	if (follow) {
244 		prev_debx = tcp_debx + 1;
245 		if (prev_debx >= TCP_NDEBUG)
246 			prev_debx = 0;
247 		do {
248 			sleep(1);
249 			(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
250 			if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
251 			    sizeof(tcp_debx)) {
252 				perror("trpt: tcp_debx");
253 				exit(3);
254 			}
255 		} while (tcp_debx == prev_debx);
256 		(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
257 		if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
258 		    sizeof(tcp_debug)) {
259 			perror("trpt: tcp_debug");
260 			exit(3);
261 		}
262 		goto again;
263 	}
264 }
265 
266 /*
267  * Tcp debug routines
268  */
269 /*ARGSUSED*/
270 tcp_trace(act, ostate, atp, tp, ti, req)
271 	short act, ostate;
272 	struct tcpcb *atp, *tp;
273 	struct tcpiphdr *ti;
274 	int req;
275 {
276 	tcp_seq seq, ack;
277 	int flags, len, win, timer;
278 
279 	printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
280 	    tanames[act]);
281 	switch (act) {
282 	case TA_INPUT:
283 	case TA_OUTPUT:
284 	case TA_DROP:
285 		if (aflag) {
286 			printf("(src=%s,%u, ",
287 			    inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
288 			printf("dst=%s,%u)",
289 			    inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
290 		}
291 		seq = ti->ti_seq;
292 		ack = ti->ti_ack;
293 		len = ti->ti_len;
294 		win = ti->ti_win;
295 		if (act == TA_OUTPUT) {
296 			seq = ntohl(seq);
297 			ack = ntohl(ack);
298 			len = ntohs(len);
299 			win = ntohs(win);
300 		}
301 		if (act == TA_OUTPUT)
302 			len -= sizeof(struct tcphdr);
303 		if (len)
304 			printf("[%lx..%lx)", seq, seq + len);
305 		else
306 			printf("%lx", seq);
307 		printf("@%lx", ack);
308 		if (win)
309 			printf("(win=%x)", win);
310 		flags = ti->ti_flags;
311 		if (flags) {
312 			register char *cp = "<";
313 #define	pf(flag, string) { \
314 	if (ti->ti_flags&flag) { \
315 		(void)printf("%s%s", cp, string); \
316 		cp = ","; \
317 	} \
318 }
319 			pf(TH_SYN, "SYN");
320 			pf(TH_ACK, "ACK");
321 			pf(TH_FIN, "FIN");
322 			pf(TH_RST, "RST");
323 			pf(TH_PUSH, "PUSH");
324 			pf(TH_URG, "URG");
325 			printf(">");
326 		}
327 		break;
328 	case TA_USER:
329 		timer = req >> 8;
330 		req &= 0xff;
331 		printf("%s", prurequests[req]);
332 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
333 			printf("<%s>", tcptimers[timer]);
334 		break;
335 	}
336 	printf(" -> %s", tcpstates[tp->t_state]);
337 	/* print out internal state of tp !?! */
338 	printf("\n");
339 	if (sflag) {
340 		printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
341 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
342 		    tp->snd_max);
343 		printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
344 		    tp->snd_wl2, tp->snd_wnd);
345 	}
346 	/* print out timers? */
347 	if (tflag) {
348 		register char *cp = "\t";
349 		register int i;
350 
351 		for (i = 0; i < TCPT_NTIMERS; i++) {
352 			if (tp->t_timer[i] == 0)
353 				continue;
354 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
355 			if (i == TCPT_REXMT)
356 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
357 			cp = ", ";
358 		}
359 		if (*cp != '\t')
360 			putchar('\n');
361 	}
362 }
363 
364 numeric(c1, c2)
365 	caddr_t *c1, *c2;
366 {
367 	return(*c1 - *c2);
368 }
369 
370 klseek(fd, base, off)
371 	int fd, off;
372 	off_t base;
373 {
374 	off_t lseek();
375 
376 #ifndef NEWVM
377 	if (kflag) {	/* get kernel pte */
378 		base &= ~KERNBASE;
379 		base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
380 	}
381 #endif
382 	(void)lseek(fd, base, off);
383 }
384