xref: /freebsd/sys/kern/uipc_debug.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Debugger routines relating to sockets, protocols, etc, for use in DDB.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_ddb.h"
37 
38 #include <sys/param.h>
39 #include <sys/domain.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 
45 #ifdef DDB
46 #include <ddb/ddb.h>
47 
48 static void
49 db_print_sotype(short so_type)
50 {
51 
52 	switch (so_type) {
53 	case SOCK_STREAM:
54 		db_printf("SOCK_STREAM");
55 		break;
56 
57 	case SOCK_DGRAM:
58 		db_printf("SOCK_DGRAM");
59 		break;
60 
61 	case SOCK_RAW:
62 		db_printf("SOCK_RAW");
63 		break;
64 
65 	case SOCK_RDM:
66 		db_printf("SOCK_RDM");
67 		break;
68 
69 	case SOCK_SEQPACKET:
70 		db_printf("SOCK_SEQPACKET");
71 		break;
72 
73 	default:
74 		db_printf("unknown");
75 		break;
76 	}
77 }
78 
79 static void
80 db_print_sooptions(int so_options)
81 {
82 	int comma;
83 
84 	comma = 0;
85 	if (so_options & SO_DEBUG) {
86 		db_printf("%sSO_DEBUG", comma ? ", " : "");
87 		comma = 1;
88 	}
89 	if (so_options & SO_ACCEPTCONN) {
90 		db_printf("%sSO_ACCEPTCONN", comma ? ", " : "");
91 		comma = 1;
92 	}
93 	if (so_options & SO_REUSEADDR) {
94 		db_printf("%sSO_REUSEADDR", comma ? ", " : "");
95 		comma = 1;
96 	}
97 	if (so_options & SO_KEEPALIVE) {
98 		db_printf("%sSO_KEEPALIVE", comma ? ", " : "");
99 		comma = 1;
100 	}
101 	if (so_options & SO_DONTROUTE) {
102 		db_printf("%sSO_DONTROUTE", comma ? ", " : "");
103 		comma = 1;
104 	}
105 	if (so_options & SO_BROADCAST) {
106 		db_printf("%sSO_BROADCAST", comma ? ", " : "");
107 		comma = 1;
108 	}
109 	if (so_options & SO_USELOOPBACK) {
110 		db_printf("%sSO_USELOOPBACK", comma ? ", " : "");
111 		comma = 1;
112 	}
113 	if (so_options & SO_LINGER) {
114 		db_printf("%sSO_LINGER", comma ? ", " : "");
115 		comma = 1;
116 	}
117 	if (so_options & SO_OOBINLINE) {
118 		db_printf("%sSO_OOBINLINE", comma ? ", " : "");
119 		comma = 1;
120 	}
121 	if (so_options & SO_REUSEPORT) {
122 		db_printf("%sSO_REUSEPORT", comma ? ", " : "");
123 		comma = 1;
124 	}
125 	if (so_options & SO_REUSEPORT_LB) {
126 		db_printf("%sSO_REUSEPORT_LB", comma ? ", " : "");
127 		comma = 1;
128 	}
129 	if (so_options & SO_TIMESTAMP) {
130 		db_printf("%sSO_TIMESTAMP", comma ? ", " : "");
131 		comma = 1;
132 	}
133 	if (so_options & SO_NOSIGPIPE) {
134 		db_printf("%sSO_NOSIGPIPE", comma ? ", " : "");
135 		comma = 1;
136 	}
137 	if (so_options & SO_ACCEPTFILTER) {
138 		db_printf("%sSO_ACCEPTFILTER", comma ? ", " : "");
139 		comma = 1;
140 	}
141 	if (so_options & SO_BINTIME) {
142 		db_printf("%sSO_BINTIME", comma ? ", " : "");
143 		comma = 1;
144 	}
145 	if (so_options & SO_NO_OFFLOAD) {
146 		db_printf("%sSO_NO_OFFLOAD", comma ? ", " : "");
147 		comma = 1;
148 	}
149 	if (so_options & SO_NO_DDP) {
150 		db_printf("%sSO_NO_DDP", comma ? ", " : "");
151 		comma = 1;
152 	}
153 }
154 
155 static void
156 db_print_sostate(short so_state)
157 {
158 	int comma;
159 
160 	comma = 0;
161 	if (so_state & SS_NOFDREF) {
162 		db_printf("%sSS_NOFDREF", comma ? ", " : "");
163 		comma = 1;
164 	}
165 	if (so_state & SS_ISCONNECTED) {
166 		db_printf("%sSS_ISCONNECTED", comma ? ", " : "");
167 		comma = 1;
168 	}
169 	if (so_state & SS_ISCONNECTING) {
170 		db_printf("%sSS_ISCONNECTING", comma ? ", " : "");
171 		comma = 1;
172 	}
173 	if (so_state & SS_ISDISCONNECTING) {
174 		db_printf("%sSS_ISDISCONNECTING", comma ? ", " : "");
175 		comma = 1;
176 	}
177 	if (so_state & SS_NBIO) {
178 		db_printf("%sSS_NBIO", comma ? ", " : "");
179 		comma = 1;
180 	}
181 	if (so_state & SS_ASYNC) {
182 		db_printf("%sSS_ASYNC", comma ? ", " : "");
183 		comma = 1;
184 	}
185 	if (so_state & SS_ISCONFIRMING) {
186 		db_printf("%sSS_ISCONFIRMING", comma ? ", " : "");
187 		comma = 1;
188 	}
189 	if (so_state & SS_PROTOREF) {
190 		db_printf("%sSS_PROTOREF", comma ? ", " : "");
191 		comma = 1;
192 	}
193 }
194 
195 static void
196 db_print_soqstate(int so_qstate)
197 {
198 	int comma;
199 
200 	comma = 0;
201 	if (so_qstate & SQ_INCOMP) {
202 		db_printf("%sSQ_INCOMP", comma ? ", " : "");
203 		comma = 1;
204 	}
205 	if (so_qstate & SQ_COMP) {
206 		db_printf("%sSQ_COMP", comma ? ", " : "");
207 		comma = 1;
208 	}
209 }
210 
211 static void
212 db_print_sbstate(short sb_state)
213 {
214 	int comma;
215 
216 	comma = 0;
217 	if (sb_state & SBS_CANTSENDMORE) {
218 		db_printf("%sSBS_CANTSENDMORE", comma ? ", " : "");
219 		comma = 1;
220 	}
221 	if (sb_state & SBS_CANTRCVMORE) {
222 		db_printf("%sSBS_CANTRCVMORE", comma ? ", " : "");
223 		comma = 1;
224 	}
225 	if (sb_state & SBS_RCVATMARK) {
226 		db_printf("%sSBS_RCVATMARK", comma ? ", " : "");
227 		comma = 1;
228 	}
229 }
230 
231 static void
232 db_print_indent(int indent)
233 {
234 	int i;
235 
236 	for (i = 0; i < indent; i++)
237 		db_printf(" ");
238 }
239 
240 static void
241 db_print_domain(struct domain *d, const char *domain_name, int indent)
242 {
243 
244 	db_print_indent(indent);
245 	db_printf("%s at %p\n", domain_name, d);
246 
247 	indent += 2;
248 
249 	db_print_indent(indent);
250 	db_printf("dom_family: %d   ", d->dom_family);
251 	db_printf("dom_name: %s\n", d->dom_name);
252 
253 	db_print_indent(indent);
254 	db_printf("dom_init: %p   ", d->dom_init);
255 	db_printf("dom_externalize: %p   ", d->dom_externalize);
256 	db_printf("dom_dispose: %p\n", d->dom_dispose);
257 
258 	db_print_indent(indent);
259 	db_printf("dom_protosw: %p   ", d->dom_protosw);
260 	db_printf("dom_next: %p\n", d->dom_next);
261 
262 	db_print_indent(indent);
263 	db_printf("dom_rtattach: %p   ", d->dom_rtattach);
264 
265 	db_print_indent(indent);
266 	db_printf("dom_ifattach: %p   ", d->dom_ifattach);
267 	db_printf("dom_ifdetach: %p\n", d->dom_ifdetach);
268 }
269 
270 static void
271 db_print_prflags(short pr_flags)
272 {
273 	int comma;
274 
275 	comma = 0;
276 	if (pr_flags & PR_ATOMIC) {
277 		db_printf("%sPR_ATOMIC", comma ? ", " : "");
278 		comma = 1;
279 	}
280 	if (pr_flags & PR_ADDR) {
281 		db_printf("%sPR_ADDR", comma ? ", " : "");
282 		comma = 1;
283 	}
284 	if (pr_flags & PR_CONNREQUIRED) {
285 		db_printf("%sPR_CONNREQUIRED", comma ? ", " : "");
286 		comma = 1;
287 	}
288 	if (pr_flags & PR_WANTRCVD) {
289 		db_printf("%sPR_WANTRCVD", comma ? ", " : "");
290 		comma = 1;
291 	}
292 	if (pr_flags & PR_RIGHTS) {
293 		db_printf("%sPR_RIGHTS", comma ? ", " : "");
294 		comma = 1;
295 	}
296 	if (pr_flags & PR_IMPLOPCL) {
297 		db_printf("%sPR_IMPLOPCL", comma ? ", " : "");
298 		comma = 1;
299 	}
300 	if (pr_flags & PR_LASTHDR) {
301 		db_printf("%sPR_LASTHDR", comma ? ", " : "");
302 		comma = 1;
303 	}
304 }
305 
306 static void
307 db_print_protosw(struct protosw *pr, const char *prname, int indent)
308 {
309 
310 	db_print_indent(indent);
311 	db_printf("%s at %p\n", prname, pr);
312 
313 	indent += 2;
314 
315 	db_print_indent(indent);
316 	db_printf("pr_type: %d   ", pr->pr_type);
317 	db_printf("pr_domain: %p\n", pr->pr_domain);
318 	if (pr->pr_domain != NULL)
319 		db_print_domain(pr->pr_domain, "pr_domain", indent);
320 
321 	db_print_indent(indent);
322 	db_printf("pr_protocol: %d\n", pr->pr_protocol);
323 
324 	db_print_indent(indent);
325 	db_printf("pr_flags: %d (", pr->pr_flags);
326 	db_print_prflags(pr->pr_flags);
327 	db_printf(")\n");
328 
329 	db_print_indent(indent);
330 	db_printf("pr_input: %p   ", pr->pr_input);
331 	db_printf("pr_output: %p   ", pr->pr_output);
332 	db_printf("pr_ctlinput: %p\n", pr->pr_ctlinput);
333 
334 	db_print_indent(indent);
335 	db_printf("pr_ctloutput: %p   ", pr->pr_ctloutput);
336 	db_printf("pr_init: %p\n", pr->pr_init);
337 
338 	db_print_indent(indent);
339 	db_printf("pr_fasttimo: %p   ", pr->pr_fasttimo);
340 	db_printf("pr_slowtimo: %p   ", pr->pr_slowtimo);
341 	db_printf("pr_drain: %p\n", pr->pr_drain);
342 }
343 
344 static void
345 db_print_sbflags(short sb_flags)
346 {
347 	int comma;
348 
349 	comma = 0;
350 	if (sb_flags & SB_WAIT) {
351 		db_printf("%sSB_WAIT", comma ? ", " : "");
352 		comma = 1;
353 	}
354 	if (sb_flags & SB_SEL) {
355 		db_printf("%sSB_SEL", comma ? ", " : "");
356 		comma = 1;
357 	}
358 	if (sb_flags & SB_ASYNC) {
359 		db_printf("%sSB_ASYNC", comma ? ", " : "");
360 		comma = 1;
361 	}
362 	if (sb_flags & SB_UPCALL) {
363 		db_printf("%sSB_UPCALL", comma ? ", " : "");
364 		comma = 1;
365 	}
366 	if (sb_flags & SB_NOINTR) {
367 		db_printf("%sSB_NOINTR", comma ? ", " : "");
368 		comma = 1;
369 	}
370 	if (sb_flags & SB_AIO) {
371 		db_printf("%sSB_AIO", comma ? ", " : "");
372 		comma = 1;
373 	}
374 	if (sb_flags & SB_KNOTE) {
375 		db_printf("%sSB_KNOTE", comma ? ", " : "");
376 		comma = 1;
377 	}
378 	if (sb_flags & SB_AUTOSIZE) {
379 		db_printf("%sSB_AUTOSIZE", comma ? ", " : "");
380 		comma = 1;
381 	}
382 }
383 
384 static void
385 db_print_sockbuf(struct sockbuf *sb, const char *sockbufname, int indent)
386 {
387 
388 	db_print_indent(indent);
389 	db_printf("%s at %p\n", sockbufname, sb);
390 
391 	indent += 2;
392 
393 	db_print_indent(indent);
394 	db_printf("sb_state: 0x%x (", sb->sb_state);
395 	db_print_sbstate(sb->sb_state);
396 	db_printf(")\n");
397 
398 	db_print_indent(indent);
399 	db_printf("sb_mb: %p   ", sb->sb_mb);
400 	db_printf("sb_mbtail: %p   ", sb->sb_mbtail);
401 	db_printf("sb_lastrecord: %p\n", sb->sb_lastrecord);
402 
403 	db_print_indent(indent);
404 	db_printf("sb_sndptr: %p   ", sb->sb_sndptr);
405 	db_printf("sb_sndptroff: %u\n", sb->sb_sndptroff);
406 
407 	db_print_indent(indent);
408 	db_printf("sb_acc: %u   ", sb->sb_acc);
409 	db_printf("sb_ccc: %u   ", sb->sb_ccc);
410 	db_printf("sb_hiwat: %u   ", sb->sb_hiwat);
411 	db_printf("sb_mbcnt: %u   ", sb->sb_mbcnt);
412 	db_printf("sb_mbmax: %u\n", sb->sb_mbmax);
413 
414 	db_print_indent(indent);
415 	db_printf("sb_mcnt: %u   ", sb->sb_mcnt);
416 	db_printf("sb_ccnt: %u   ", sb->sb_ccnt);
417 	db_printf("sb_ctl: %u   ", sb->sb_ctl);
418 	db_printf("sb_lowat: %d   ", sb->sb_lowat);
419 	db_printf("sb_timeo: %jd\n", sb->sb_timeo);
420 
421 	db_print_indent(indent);
422 	db_printf("sb_flags: 0x%x (", sb->sb_flags);
423 	db_print_sbflags(sb->sb_flags);
424 	db_printf(")\n");
425 
426 	db_print_indent(indent);
427 	db_printf("sb_aiojobq first: %p\n", TAILQ_FIRST(&sb->sb_aiojobq));
428 }
429 
430 static void
431 db_print_socket(struct socket *so, const char *socketname, int indent)
432 {
433 
434 	db_print_indent(indent);
435 	db_printf("%s at %p\n", socketname, so);
436 
437 	indent += 2;
438 
439 	db_print_indent(indent);
440 	db_printf("so_count: %d   ", so->so_count);
441 	db_printf("so_type: %d (", so->so_type);
442 	db_print_sotype(so->so_type);
443 	db_printf(")\n");
444 
445 	db_print_indent(indent);
446 	db_printf("so_options: 0x%x (", so->so_options);
447 	db_print_sooptions(so->so_options);
448 	db_printf(")\n");
449 
450 	db_print_indent(indent);
451 	db_printf("so_linger: %d   ", so->so_linger);
452 	db_printf("so_state: 0x%x (", so->so_state);
453 	db_print_sostate(so->so_state);
454 	db_printf(")\n");
455 
456 	db_print_indent(indent);
457 	db_printf("so_pcb: %p   ", so->so_pcb);
458 	db_printf("so_proto: %p\n", so->so_proto);
459 
460 	if (so->so_proto != NULL)
461 		db_print_protosw(so->so_proto, "so_proto", indent);
462 
463 	db_print_indent(indent);
464 	if (so->so_options & SO_ACCEPTCONN) {
465 		db_printf("sol_incomp first: %p   ",
466 		    TAILQ_FIRST(&so->sol_incomp));
467 		db_printf("sol_comp first: %p\n", TAILQ_FIRST(&so->sol_comp));
468 		db_printf("sol_qlen: %d   ", so->sol_qlen);
469 		db_printf("sol_incqlen: %d   ", so->sol_incqlen);
470 		db_printf("sol_qlimit: %d   ", so->sol_qlimit);
471 	} else {
472 		db_printf("so_qstate: 0x%x (", so->so_qstate);
473 		db_print_soqstate(so->so_qstate);
474 		db_printf(")   ");
475 		db_printf("so_listen: %p   ", so->so_listen);
476 		/* so_list skipped */
477 		db_printf("so_timeo: %d   ", so->so_timeo);
478 		db_printf("so_error: %d\n", so->so_error);
479 
480 		db_print_indent(indent);
481 		db_printf("so_sigio: %p   ", so->so_sigio);
482 		db_printf("so_oobmark: %lu\n", so->so_oobmark);
483 
484 		db_print_sockbuf(&so->so_rcv, "so_rcv", indent);
485 		db_print_sockbuf(&so->so_snd, "so_snd", indent);
486 	}
487 }
488 
489 DB_SHOW_COMMAND(socket, db_show_socket)
490 {
491 	struct socket *so;
492 
493 	if (!have_addr) {
494 		db_printf("usage: show socket <addr>\n");
495 		return;
496 	}
497 	so = (struct socket *)addr;
498 
499 	db_print_socket(so, "socket", 0);
500 }
501 
502 DB_SHOW_COMMAND(sockbuf, db_show_sockbuf)
503 {
504 	struct sockbuf *sb;
505 
506 	if (!have_addr) {
507 		db_printf("usage: show sockbuf <addr>\n");
508 		return;
509 	}
510 	sb = (struct sockbuf *)addr;
511 
512 	db_print_sockbuf(sb, "sockbuf", 0);
513 }
514 
515 DB_SHOW_COMMAND(protosw, db_show_protosw)
516 {
517 	struct protosw *pr;
518 
519 	if (!have_addr) {
520 		db_printf("usage: show protosw <addr>\n");
521 		return;
522 	}
523 	pr = (struct protosw *)addr;
524 
525 	db_print_protosw(pr, "protosw", 0);
526 }
527 
528 DB_SHOW_COMMAND(domain, db_show_domain)
529 {
530 	struct domain *d;
531 
532 	if (!have_addr) {
533 		db_printf("usage: show protosw <addr>\n");
534 		return;
535 	}
536 	d = (struct domain *)addr;
537 
538 	db_print_domain(d, "domain", 0);
539 }
540 #endif
541