]>
git.saurik.com Git - apple/network_cmds.git/blob - trpt.tproj/trpt.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char copyright
[] =
59 "@(#) Copyright (c) 1983, 1988, 1993\n\
60 The Regents of the University of California. All rights reserved.\n";
64 static char sccsid
[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
67 #include <sys/param.h>
72 #include <machine/pte.h>
73 #include <sys/vmmac.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
78 #include <sys/protosw.h>
81 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/tcp.h>
91 #include <netinet/tcp_fsm.h>
92 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcpip.h>
98 #include <netinet/tcp_debug.h>
100 #include <arpa/inet.h>
107 struct tcp_debug tcp_debug
[TCP_NDEBUG
];
110 struct nlist nl
[] = {
111 #define N_TCP_DEBUG 0
125 static struct pte
*Sysmap
;
127 static caddr_t tcp_pcbs
[TCP_NDEBUG
];
129 static int aflag
, kflag
, memf
, follow
, sflag
, tflag
;
137 int ch
, i
, jflag
, npcbs
, numeric();
138 char *system
, *core
, *malloc();
142 while ((ch
= getopt(argc
, argv
, "afjp:st")) != EOF
)
155 if (npcbs
>= TCP_NDEBUG
) {
156 fputs("trpt: too many pcb's specified\n",
160 (void)sscanf(optarg
, "%x", (int *)&tcp_pcbs
[npcbs
++]);
170 (void)fprintf(stderr
,
171 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
187 * Discard setgid privileges if not the running kernel so that
188 * bad guys can't print interesting stuff from kernel memory.
195 if (nlist(system
, nl
) < 0 || !nl
[0].n_value
) {
196 fprintf(stderr
, "trpt: %s: no namelist\n", system
);
199 if ((memf
= open(core
, O_RDONLY
)) < 0) {
205 fputs("trpt: can't do core files yet\n", stderr
);
210 Sysmap
= (struct pte
*)
211 malloc((u_int
)(nl
[N_SYSSIZE
].n_value
* sizeof(struct pte
)));
213 fputs("trpt: can't get memory for Sysmap.\n", stderr
);
216 off
= nl
[N_SYSMAP
].n_value
& ~KERNBASE
;
217 (void)lseek(memf
, off
, L_SET
);
218 (void)read(memf
, (char *)Sysmap
,
219 (int)(nl
[N_SYSSIZE
].n_value
* sizeof(struct pte
)));
222 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBX
].n_value
, L_SET
);
223 if (read(memf
, (char *)&tcp_debx
, sizeof(tcp_debx
)) !=
225 perror("trpt: tcp_debx");
228 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBUG
].n_value
, L_SET
);
229 if (read(memf
, (char *)tcp_debug
, sizeof(tcp_debug
)) !=
231 perror("trpt: tcp_debug");
235 * If no control blocks have been specified, figure
236 * out how many distinct one we have and summarize
237 * them in tcp_pcbs for sorting the trace records
241 for (i
= 0; i
< TCP_NDEBUG
; i
++) {
242 register struct tcp_debug
*td
= &tcp_debug
[i
];
247 for (j
= 0; j
< npcbs
; j
++)
248 if (tcp_pcbs
[j
] == td
->td_tcb
)
251 tcp_pcbs
[npcbs
++] = td
->td_tcb
;
256 qsort(tcp_pcbs
, npcbs
, sizeof(caddr_t
), numeric
);
259 printf("%x", (int)tcp_pcbs
[i
]);
266 else for (i
= 0; i
< npcbs
; i
++) {
267 printf("\n%x:\n", (int)tcp_pcbs
[i
]);
268 dotrace(tcp_pcbs
[i
]);
274 register caddr_t tcpcb
;
276 register struct tcp_debug
*td
;
278 int prev_debx
= tcp_debx
;
280 again
: if (--tcp_debx
< 0)
281 tcp_debx
= TCP_NDEBUG
- 1;
282 for (i
= prev_debx
% TCP_NDEBUG
; i
< TCP_NDEBUG
; i
++) {
284 if (tcpcb
&& td
->td_tcb
!= tcpcb
)
286 ntime
= ntohl(td
->td_time
);
287 tcp_trace(td
->td_act
, td
->td_ostate
, td
->td_tcb
, &td
->td_cb
,
288 &td
->td_ti
.ti_t
, td
->td_req
);
292 for (i
= 0; i
<= tcp_debx
% TCP_NDEBUG
; i
++) {
294 if (tcpcb
&& td
->td_tcb
!= tcpcb
)
296 ntime
= ntohl(td
->td_time
);
297 tcp_trace(td
->td_act
, td
->td_ostate
, td
->td_tcb
, &td
->td_cb
,
298 &td
->td_ti
.ti_t
, td
->td_req
);
301 prev_debx
= tcp_debx
+ 1;
302 if (prev_debx
>= TCP_NDEBUG
)
306 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBX
].n_value
, L_SET
);
307 if (read(memf
, (char *)&tcp_debx
, sizeof(tcp_debx
)) !=
309 perror("trpt: tcp_debx");
312 } while (tcp_debx
== prev_debx
);
313 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBUG
].n_value
, L_SET
);
314 if (read(memf
, (char *)tcp_debug
, sizeof(tcp_debug
)) !=
316 perror("trpt: tcp_debug");
327 tcp_trace(act
, ostate
, atp
, tp
, ti
, req
)
329 struct tcpcb
*atp
, *tp
;
334 int flags
, len
, win
, timer
;
336 printf("%03ld %s:%s ",(ntime
/10) % 1000, tcpstates
[ostate
],
343 printf("(src=%s,%u, ",
344 inet_ntoa(ti
->ti_src
), ntohs(ti
->ti_sport
));
346 inet_ntoa(ti
->ti_dst
), ntohs(ti
->ti_dport
));
352 if (act
== TA_OUTPUT
) {
358 if (act
== TA_OUTPUT
)
359 len
-= sizeof(struct tcphdr
);
361 printf("[%lx..%lx)", seq
, seq
+ len
);
366 printf("(win=%x)", win
);
367 flags
= ti
->ti_flags
;
369 register char *cp
= "<";
370 #define pf(flag, string) { \
371 if (ti->ti_flags&flag) { \
372 (void)printf("%s%s", cp, string); \
388 printf("%s", prurequests
[req
]);
389 if (req
== PRU_SLOWTIMO
|| req
== PRU_FASTTIMO
)
390 printf("<%s>", tcptimers
[timer
]);
393 printf(" -> %s", tcpstates
[tp
->t_state
]);
394 /* print out internal state of tp !?! */
397 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
398 tp
->rcv_nxt
, tp
->rcv_wnd
, tp
->snd_una
, tp
->snd_nxt
,
400 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp
->snd_wl1
,
401 tp
->snd_wl2
, tp
->snd_wnd
);
403 /* print out timers? */
405 register char *cp
= "\t";
408 for (i
= 0; i
< TCPT_NTIMERS
; i
++) {
409 if (tp
->t_timer
[i
] == 0)
411 printf("%s%s=%d", cp
, tcptimers
[i
], tp
->t_timer
[i
]);
413 printf(" (t_rxtshft=%d)", tp
->t_rxtshift
);
427 klseek(fd
, base
, off
)
434 if (kflag
) { /* get kernel pte */
436 base
= ctob(Sysmap
[btop(base
)].pg_pfnum
) + (base
& PGOFSET
);
439 (void)lseek(fd
, base
, off
);