]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static char copyright
[] =
60 "@(#) Copyright (c) 1983, 1988, 1993\n\
61 The Regents of the University of California. All rights reserved.\n";
65 static char sccsid
[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
68 #include <sys/param.h>
73 #include <machine/pte.h>
74 #include <sys/vmmac.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
79 #include <sys/protosw.h>
82 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/tcp.h>
92 #include <netinet/tcp_fsm.h>
93 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcpip.h>
99 #include <netinet/tcp_debug.h>
101 #include <arpa/inet.h>
108 struct tcp_debug tcp_debug
[TCP_NDEBUG
];
111 struct nlist nl
[] = {
112 #define N_TCP_DEBUG 0
126 static struct pte
*Sysmap
;
128 static caddr_t tcp_pcbs
[TCP_NDEBUG
];
130 static int aflag
, kflag
, memf
, follow
, sflag
, tflag
;
138 int ch
, i
, jflag
, npcbs
, numeric();
139 char *system
, *core
, *malloc();
143 while ((ch
= getopt(argc
, argv
, "afjp:st")) != EOF
)
156 if (npcbs
>= TCP_NDEBUG
) {
157 fputs("trpt: too many pcb's specified\n",
161 (void)sscanf(optarg
, "%x", (int *)&tcp_pcbs
[npcbs
++]);
171 (void)fprintf(stderr
,
172 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
188 * Discard setgid privileges if not the running kernel so that
189 * bad guys can't print interesting stuff from kernel memory.
196 if (nlist(system
, nl
) < 0 || !nl
[0].n_value
) {
197 fprintf(stderr
, "trpt: %s: no namelist\n", system
);
200 if ((memf
= open(core
, O_RDONLY
)) < 0) {
206 fputs("trpt: can't do core files yet\n", stderr
);
211 Sysmap
= (struct pte
*)
212 malloc((u_int
)(nl
[N_SYSSIZE
].n_value
* sizeof(struct pte
)));
214 fputs("trpt: can't get memory for Sysmap.\n", stderr
);
217 off
= nl
[N_SYSMAP
].n_value
& ~KERNBASE
;
218 (void)lseek(memf
, off
, L_SET
);
219 (void)read(memf
, (char *)Sysmap
,
220 (int)(nl
[N_SYSSIZE
].n_value
* sizeof(struct pte
)));
223 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBX
].n_value
, L_SET
);
224 if (read(memf
, (char *)&tcp_debx
, sizeof(tcp_debx
)) !=
226 perror("trpt: tcp_debx");
229 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBUG
].n_value
, L_SET
);
230 if (read(memf
, (char *)tcp_debug
, sizeof(tcp_debug
)) !=
232 perror("trpt: tcp_debug");
236 * If no control blocks have been specified, figure
237 * out how many distinct one we have and summarize
238 * them in tcp_pcbs for sorting the trace records
242 for (i
= 0; i
< TCP_NDEBUG
; i
++) {
243 register struct tcp_debug
*td
= &tcp_debug
[i
];
248 for (j
= 0; j
< npcbs
; j
++)
249 if (tcp_pcbs
[j
] == td
->td_tcb
)
252 tcp_pcbs
[npcbs
++] = td
->td_tcb
;
257 qsort(tcp_pcbs
, npcbs
, sizeof(caddr_t
), numeric
);
260 printf("%x", (int)tcp_pcbs
[i
]);
267 else for (i
= 0; i
< npcbs
; i
++) {
268 printf("\n%x:\n", (int)tcp_pcbs
[i
]);
269 dotrace(tcp_pcbs
[i
]);
275 register caddr_t tcpcb
;
277 register struct tcp_debug
*td
;
279 int prev_debx
= tcp_debx
;
281 again
: if (--tcp_debx
< 0)
282 tcp_debx
= TCP_NDEBUG
- 1;
283 for (i
= prev_debx
% TCP_NDEBUG
; i
< TCP_NDEBUG
; i
++) {
285 if (tcpcb
&& td
->td_tcb
!= tcpcb
)
287 ntime
= ntohl(td
->td_time
);
288 tcp_trace(td
->td_act
, td
->td_ostate
, td
->td_tcb
, &td
->td_cb
,
289 &td
->td_ti
.ti_t
, td
->td_req
);
293 for (i
= 0; i
<= tcp_debx
% TCP_NDEBUG
; i
++) {
295 if (tcpcb
&& td
->td_tcb
!= tcpcb
)
297 ntime
= ntohl(td
->td_time
);
298 tcp_trace(td
->td_act
, td
->td_ostate
, td
->td_tcb
, &td
->td_cb
,
299 &td
->td_ti
.ti_t
, td
->td_req
);
302 prev_debx
= tcp_debx
+ 1;
303 if (prev_debx
>= TCP_NDEBUG
)
307 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBX
].n_value
, L_SET
);
308 if (read(memf
, (char *)&tcp_debx
, sizeof(tcp_debx
)) !=
310 perror("trpt: tcp_debx");
313 } while (tcp_debx
== prev_debx
);
314 (void)klseek(memf
, (off_t
)nl
[N_TCP_DEBUG
].n_value
, L_SET
);
315 if (read(memf
, (char *)tcp_debug
, sizeof(tcp_debug
)) !=
317 perror("trpt: tcp_debug");
328 tcp_trace(act
, ostate
, atp
, tp
, ti
, req
)
330 struct tcpcb
*atp
, *tp
;
335 int flags
, len
, win
, timer
;
337 printf("%03ld %s:%s ",(ntime
/10) % 1000, tcpstates
[ostate
],
344 printf("(src=%s,%u, ",
345 inet_ntoa(ti
->ti_src
), ntohs(ti
->ti_sport
));
347 inet_ntoa(ti
->ti_dst
), ntohs(ti
->ti_dport
));
353 if (act
== TA_OUTPUT
) {
359 if (act
== TA_OUTPUT
)
360 len
-= sizeof(struct tcphdr
);
362 printf("[%lx..%lx)", seq
, seq
+ len
);
367 printf("(win=%x)", win
);
368 flags
= ti
->ti_flags
;
370 register char *cp
= "<";
371 #define pf(flag, string) { \
372 if (ti->ti_flags&flag) { \
373 (void)printf("%s%s", cp, string); \
389 printf("%s", prurequests
[req
]);
390 if (req
== PRU_SLOWTIMO
|| req
== PRU_FASTTIMO
)
391 printf("<%s>", tcptimers
[timer
]);
394 printf(" -> %s", tcpstates
[tp
->t_state
]);
395 /* print out internal state of tp !?! */
398 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
399 tp
->rcv_nxt
, tp
->rcv_wnd
, tp
->snd_una
, tp
->snd_nxt
,
401 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp
->snd_wl1
,
402 tp
->snd_wl2
, tp
->snd_wnd
);
404 /* print out timers? */
406 register char *cp
= "\t";
409 for (i
= 0; i
< TCPT_NTIMERS
; i
++) {
410 if (tp
->t_timer
[i
] == 0)
412 printf("%s%s=%d", cp
, tcptimers
[i
], tp
->t_timer
[i
]);
414 printf(" (t_rxtshft=%d)", tp
->t_rxtshift
);
428 klseek(fd
, base
, off
)
435 if (kflag
) { /* get kernel pte */
437 base
= ctob(Sysmap
[btop(base
)].pg_pfnum
) + (base
& PGOFSET
);
440 (void)lseek(fd
, base
, off
);