1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_ddb.h"
35
36 #include <sys/param.h>
37 #include <sys/domain.h>
38 #include <sys/kernel.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42
43 #ifdef DDB
44 #include <ddb/ddb.h>
45
46 static void
db_print_sotype(short so_type)47 db_print_sotype(short so_type)
48 {
49
50 switch (so_type) {
51 case SOCK_STREAM:
52 db_printf("SOCK_STREAM");
53 break;
54
55 case SOCK_DGRAM:
56 db_printf("SOCK_DGRAM");
57 break;
58
59 case SOCK_RAW:
60 db_printf("SOCK_RAW");
61 break;
62
63 case SOCK_RDM:
64 db_printf("SOCK_RDM");
65 break;
66
67 case SOCK_SEQPACKET:
68 db_printf("SOCK_SEQPACKET");
69 break;
70
71 default:
72 db_printf("unknown");
73 break;
74 }
75 }
76
77 static void
db_print_sooptions(int so_options)78 db_print_sooptions(int so_options)
79 {
80 int comma;
81
82 comma = 0;
83 if (so_options & SO_DEBUG) {
84 db_printf("%sSO_DEBUG", comma ? ", " : "");
85 comma = 1;
86 }
87 if (so_options & SO_ACCEPTCONN) {
88 db_printf("%sSO_ACCEPTCONN", comma ? ", " : "");
89 comma = 1;
90 }
91 if (so_options & SO_REUSEADDR) {
92 db_printf("%sSO_REUSEADDR", comma ? ", " : "");
93 comma = 1;
94 }
95 if (so_options & SO_KEEPALIVE) {
96 db_printf("%sSO_KEEPALIVE", comma ? ", " : "");
97 comma = 1;
98 }
99 if (so_options & SO_DONTROUTE) {
100 db_printf("%sSO_DONTROUTE", comma ? ", " : "");
101 comma = 1;
102 }
103 if (so_options & SO_BROADCAST) {
104 db_printf("%sSO_BROADCAST", comma ? ", " : "");
105 comma = 1;
106 }
107 if (so_options & SO_USELOOPBACK) {
108 db_printf("%sSO_USELOOPBACK", comma ? ", " : "");
109 comma = 1;
110 }
111 if (so_options & SO_LINGER) {
112 db_printf("%sSO_LINGER", comma ? ", " : "");
113 comma = 1;
114 }
115 if (so_options & SO_OOBINLINE) {
116 db_printf("%sSO_OOBINLINE", comma ? ", " : "");
117 comma = 1;
118 }
119 if (so_options & SO_REUSEPORT) {
120 db_printf("%sSO_REUSEPORT", comma ? ", " : "");
121 comma = 1;
122 }
123 if (so_options & SO_REUSEPORT_LB) {
124 db_printf("%sSO_REUSEPORT_LB", comma ? ", " : "");
125 comma = 1;
126 }
127 if (so_options & SO_TIMESTAMP) {
128 db_printf("%sSO_TIMESTAMP", comma ? ", " : "");
129 comma = 1;
130 }
131 if (so_options & SO_NOSIGPIPE) {
132 db_printf("%sSO_NOSIGPIPE", comma ? ", " : "");
133 comma = 1;
134 }
135 if (so_options & SO_ACCEPTFILTER) {
136 db_printf("%sSO_ACCEPTFILTER", comma ? ", " : "");
137 comma = 1;
138 }
139 if (so_options & SO_BINTIME) {
140 db_printf("%sSO_BINTIME", comma ? ", " : "");
141 comma = 1;
142 }
143 if (so_options & SO_NO_OFFLOAD) {
144 db_printf("%sSO_NO_OFFLOAD", comma ? ", " : "");
145 comma = 1;
146 }
147 if (so_options & SO_NO_DDP) {
148 db_printf("%sSO_NO_DDP", comma ? ", " : "");
149 comma = 1;
150 }
151 }
152
153 static void
db_print_sostate(short so_state)154 db_print_sostate(short so_state)
155 {
156 int comma;
157
158 comma = 0;
159 if (so_state & SS_ISCONNECTED) {
160 db_printf("%sSS_ISCONNECTED", comma ? ", " : "");
161 comma = 1;
162 }
163 if (so_state & SS_ISCONNECTING) {
164 db_printf("%sSS_ISCONNECTING", comma ? ", " : "");
165 comma = 1;
166 }
167 if (so_state & SS_ISDISCONNECTING) {
168 db_printf("%sSS_ISDISCONNECTING", comma ? ", " : "");
169 comma = 1;
170 }
171 if (so_state & SS_NBIO) {
172 db_printf("%sSS_NBIO", comma ? ", " : "");
173 comma = 1;
174 }
175 if (so_state & SS_ASYNC) {
176 db_printf("%sSS_ASYNC", comma ? ", " : "");
177 comma = 1;
178 }
179 }
180
181 static void
db_print_soqstate(int so_qstate)182 db_print_soqstate(int so_qstate)
183 {
184 int comma;
185
186 comma = 0;
187 if (so_qstate & SQ_INCOMP) {
188 db_printf("%sSQ_INCOMP", comma ? ", " : "");
189 comma = 1;
190 }
191 if (so_qstate & SQ_COMP) {
192 db_printf("%sSQ_COMP", comma ? ", " : "");
193 comma = 1;
194 }
195 }
196
197 static void
db_print_sbstate(short sb_state)198 db_print_sbstate(short sb_state)
199 {
200 int comma;
201
202 comma = 0;
203 if (sb_state & SBS_CANTSENDMORE) {
204 db_printf("%sSBS_CANTSENDMORE", comma ? ", " : "");
205 comma = 1;
206 }
207 if (sb_state & SBS_CANTRCVMORE) {
208 db_printf("%sSBS_CANTRCVMORE", comma ? ", " : "");
209 comma = 1;
210 }
211 if (sb_state & SBS_RCVATMARK) {
212 db_printf("%sSBS_RCVATMARK", comma ? ", " : "");
213 comma = 1;
214 }
215 }
216
217 static void
db_print_indent(int indent)218 db_print_indent(int indent)
219 {
220 int i;
221
222 for (i = 0; i < indent; i++)
223 db_printf(" ");
224 }
225
226 static void
db_print_domain(struct domain * d,const char * domain_name,int indent)227 db_print_domain(struct domain *d, const char *domain_name, int indent)
228 {
229
230 db_print_indent(indent);
231 db_printf("%s at %p\n", domain_name, d);
232
233 indent += 2;
234
235 db_print_indent(indent);
236 db_printf("dom_family: %d ", d->dom_family);
237 db_printf("dom_name: %s\n", d->dom_name);
238
239 db_print_indent(indent);
240 db_printf("dom_protosw: %p ", d->dom_protosw);
241 db_printf("dom_next: %p\n", d->dom_next.sle_next);
242
243 db_print_indent(indent);
244 db_printf("dom_rtattach: %p ", d->dom_rtattach);
245
246 db_print_indent(indent);
247 db_printf("dom_ifattach: %p ", d->dom_ifattach);
248 db_printf("dom_ifdetach: %p\n", d->dom_ifdetach);
249 }
250
251 static void
db_print_prflags(short pr_flags)252 db_print_prflags(short pr_flags)
253 {
254 int comma;
255
256 comma = 0;
257 if (pr_flags & PR_ATOMIC) {
258 db_printf("%sPR_ATOMIC", comma ? ", " : "");
259 comma = 1;
260 }
261 if (pr_flags & PR_ADDR) {
262 db_printf("%sPR_ADDR", comma ? ", " : "");
263 comma = 1;
264 }
265 if (pr_flags & PR_CONNREQUIRED) {
266 db_printf("%sPR_CONNREQUIRED", comma ? ", " : "");
267 comma = 1;
268 }
269 if (pr_flags & PR_WANTRCVD) {
270 db_printf("%sPR_WANTRCVD", comma ? ", " : "");
271 comma = 1;
272 }
273 if (pr_flags & PR_IMPLOPCL) {
274 db_printf("%sPR_IMPLOPCL", comma ? ", " : "");
275 comma = 1;
276 }
277 }
278
279 static void
db_print_protosw(struct protosw * pr,const char * prname,int indent)280 db_print_protosw(struct protosw *pr, const char *prname, int indent)
281 {
282
283 db_print_indent(indent);
284 db_printf("%s at %p\n", prname, pr);
285
286 indent += 2;
287
288 db_print_indent(indent);
289 db_printf("pr_type: %d ", pr->pr_type);
290 db_printf("pr_domain: %p\n", pr->pr_domain);
291 if (pr->pr_domain != NULL)
292 db_print_domain(pr->pr_domain, "pr_domain", indent);
293
294 db_print_indent(indent);
295 db_printf("pr_protocol: %d\n", pr->pr_protocol);
296
297 db_print_indent(indent);
298 db_printf("pr_flags: %d (", pr->pr_flags);
299 db_print_prflags(pr->pr_flags);
300 db_printf(")\n");
301
302 db_print_indent(indent);
303 db_printf("pr_ctloutput: %p ", pr->pr_ctloutput);
304 }
305
306 static void
db_print_sbflags(short sb_flags)307 db_print_sbflags(short sb_flags)
308 {
309 int comma;
310
311 comma = 0;
312 if (sb_flags & SB_WAIT) {
313 db_printf("%sSB_WAIT", comma ? ", " : "");
314 comma = 1;
315 }
316 if (sb_flags & SB_SEL) {
317 db_printf("%sSB_SEL", comma ? ", " : "");
318 comma = 1;
319 }
320 if (sb_flags & SB_ASYNC) {
321 db_printf("%sSB_ASYNC", comma ? ", " : "");
322 comma = 1;
323 }
324 if (sb_flags & SB_UPCALL) {
325 db_printf("%sSB_UPCALL", comma ? ", " : "");
326 comma = 1;
327 }
328 if (sb_flags & SB_AIO) {
329 db_printf("%sSB_AIO", comma ? ", " : "");
330 comma = 1;
331 }
332 if (sb_flags & SB_KNOTE) {
333 db_printf("%sSB_KNOTE", comma ? ", " : "");
334 comma = 1;
335 }
336 if (sb_flags & SB_AUTOSIZE) {
337 db_printf("%sSB_AUTOSIZE", comma ? ", " : "");
338 comma = 1;
339 }
340 }
341
342 static void
db_print_sockbuf(struct sockbuf * sb,const char * sockbufname,int indent)343 db_print_sockbuf(struct sockbuf *sb, const char *sockbufname, int indent)
344 {
345
346 db_print_indent(indent);
347 db_printf("%s at %p\n", sockbufname, sb);
348
349 indent += 2;
350
351 db_print_indent(indent);
352 db_printf("sb_state: 0x%x (", sb->sb_state);
353 db_print_sbstate(sb->sb_state);
354 db_printf(")\n");
355
356 db_print_indent(indent);
357 db_printf("sb_mb: %p ", sb->sb_mb);
358 db_printf("sb_mbtail: %p ", sb->sb_mbtail);
359 db_printf("sb_lastrecord: %p\n", sb->sb_lastrecord);
360
361 db_print_indent(indent);
362 db_printf("sb_sndptr: %p ", sb->sb_sndptr);
363 db_printf("sb_sndptroff: %u\n", sb->sb_sndptroff);
364
365 db_print_indent(indent);
366 db_printf("sb_acc: %u ", sb->sb_acc);
367 db_printf("sb_ccc: %u ", sb->sb_ccc);
368 db_printf("sb_hiwat: %u ", sb->sb_hiwat);
369 db_printf("sb_mbcnt: %u ", sb->sb_mbcnt);
370 db_printf("sb_mbmax: %u\n", sb->sb_mbmax);
371
372 db_print_indent(indent);
373 db_printf("sb_ctl: %u ", sb->sb_ctl);
374 db_printf("sb_lowat: %d ", sb->sb_lowat);
375 db_printf("sb_timeo: %jd\n", sb->sb_timeo);
376
377 db_print_indent(indent);
378 db_printf("sb_flags: 0x%x (", sb->sb_flags);
379 db_print_sbflags(sb->sb_flags);
380 db_printf(")\n");
381
382 db_print_indent(indent);
383 db_printf("sb_aiojobq first: %p\n", TAILQ_FIRST(&sb->sb_aiojobq));
384 }
385
386 static void
db_print_socket(struct socket * so,const char * socketname,int indent)387 db_print_socket(struct socket *so, const char *socketname, int indent)
388 {
389
390 db_print_indent(indent);
391 db_printf("%s at %p\n", socketname, so);
392
393 indent += 2;
394
395 db_print_indent(indent);
396 db_printf("so_count: %d ", so->so_count);
397 db_printf("so_type: %d (", so->so_type);
398 db_print_sotype(so->so_type);
399 db_printf(")\n");
400
401 db_print_indent(indent);
402 db_printf("so_options: 0x%x (", so->so_options);
403 db_print_sooptions(so->so_options);
404 db_printf(")\n");
405
406 db_print_indent(indent);
407 db_printf("so_linger: %d ", so->so_linger);
408 db_printf("so_state: 0x%x (", so->so_state);
409 db_print_sostate(so->so_state);
410 db_printf(")\n");
411
412 db_print_indent(indent);
413 db_printf("so_pcb: %p ", so->so_pcb);
414 db_printf("so_proto: %p\n", so->so_proto);
415
416 if (so->so_proto != NULL)
417 db_print_protosw(so->so_proto, "so_proto", indent);
418
419 db_print_indent(indent);
420 if (so->so_options & SO_ACCEPTCONN) {
421 db_printf("sol_incomp first: %p ",
422 TAILQ_FIRST(&so->sol_incomp));
423 db_printf("sol_comp first: %p\n", TAILQ_FIRST(&so->sol_comp));
424 db_printf("sol_qlen: %d ", so->sol_qlen);
425 db_printf("sol_incqlen: %d ", so->sol_incqlen);
426 db_printf("sol_qlimit: %d ", so->sol_qlimit);
427 } else {
428 db_printf("so_qstate: 0x%x (", so->so_qstate);
429 db_print_soqstate(so->so_qstate);
430 db_printf(") ");
431 db_printf("so_listen: %p ", so->so_listen);
432 /* so_list skipped */
433 db_printf("so_timeo: %d ", so->so_timeo);
434 db_printf("so_error: %d\n", so->so_error);
435
436 db_print_indent(indent);
437 db_printf("so_sigio: %p ", so->so_sigio);
438 db_printf("so_oobmark: %lu\n", so->so_oobmark);
439
440 db_print_sockbuf(&so->so_rcv, "so_rcv", indent);
441 db_print_sockbuf(&so->so_snd, "so_snd", indent);
442 }
443 }
444
DB_SHOW_COMMAND(socket,db_show_socket)445 DB_SHOW_COMMAND(socket, db_show_socket)
446 {
447 struct socket *so;
448
449 if (!have_addr) {
450 db_printf("usage: show socket <addr>\n");
451 return;
452 }
453 so = (struct socket *)addr;
454
455 db_print_socket(so, "socket", 0);
456 }
457
DB_SHOW_COMMAND(sockbuf,db_show_sockbuf)458 DB_SHOW_COMMAND(sockbuf, db_show_sockbuf)
459 {
460 struct sockbuf *sb;
461
462 if (!have_addr) {
463 db_printf("usage: show sockbuf <addr>\n");
464 return;
465 }
466 sb = (struct sockbuf *)addr;
467
468 db_print_sockbuf(sb, "sockbuf", 0);
469 }
470
DB_SHOW_COMMAND(protosw,db_show_protosw)471 DB_SHOW_COMMAND(protosw, db_show_protosw)
472 {
473 struct protosw *pr;
474
475 if (!have_addr) {
476 db_printf("usage: show protosw <addr>\n");
477 return;
478 }
479 pr = (struct protosw *)addr;
480
481 db_print_protosw(pr, "protosw", 0);
482 }
483
DB_SHOW_COMMAND(domain,db_show_domain)484 DB_SHOW_COMMAND(domain, db_show_domain)
485 {
486 struct domain *d;
487
488 if (!have_addr) {
489 db_printf("usage: show protosw <addr>\n");
490 return;
491 }
492 d = (struct domain *)addr;
493
494 db_print_domain(d, "domain", 0);
495 }
496 #endif
497