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