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