xref: /titanic_52/usr/src/cmd/mdb/common/modules/genunix/streams.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_ks.h>
31 
32 #include <sys/types.h>
33 #include <sys/strsubr.h>
34 #include <sys/stream.h>
35 #include <sys/modctl.h>
36 #include <sys/strft.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 
40 #include "streams.h"
41 
42 typedef struct str_flags {
43 	uint_t strf_flag;
44 	const char *strf_name;
45 	const char *strf_descr;
46 } strflags_t;
47 
48 typedef struct str_types {
49 	const char *strt_name;
50 	int strt_value;
51 	const char *strt_descr;
52 } strtypes_t;
53 
54 typedef struct ftblk_data {
55 	ftblk_t ft_data;	/* Copy of ftblk */
56 	int 	ft_ix;		/* Index in event list */
57 	boolean_t ft_in_evlist;	/* Iterating through evlist */
58 } ftblkdata_t;
59 
60 typedef void qprint_func(queue_t *, queue_t *);
61 typedef void sdprint_func(stdata_t *, stdata_t *);
62 
63 #define	SF(flag)	flag, #flag
64 
65 /*
66  * Queue flags
67  */
68 static const strflags_t qf[] = {
69 	{ SF(QENAB), 		"Queue is already enabled to run"	},
70 	{ SF(QWANTR),		"Someone wants to read Q"		},
71 	{ SF(QWANTW),		"Someone wants to write Q"		},
72 	{ SF(QFULL),		"Q is considered full"			},
73 	{ SF(QREADR),		"This is the reader (first) Q"		},
74 	{ SF(QUSE),		"This queue in use (allocation)"	},
75 	{ SF(QNOENB),		"Don't enable Q via putq"		},
76 	{ SF(QWANTRMQSYNC),	"Want to remove sync stream Q"		},
77 	{ SF(QBACK),		"queue has been back-enabled"		},
78 	{ SF(0x00000200),	"unused (was QHLIST)"			},
79 	{ SF(0x00000400),	"unused (was QUNSAFE)"			},
80 	{ SF(QPAIR),		"per queue-pair syncq"			},
81 	{ SF(QPERQ),		"per queue-instance syncq"		},
82 	{ SF(QPERMOD),		"per module syncq"			},
83 	{ SF(QMTSAFE),		"stream module is MT-safe"		},
84 	{ SF(QMTOUTPERIM),	"Has outer perimeter"			},
85 	{ SF(QINSERVICE),	"service routine executing"		},
86 	{ SF(QWCLOSE),		"will not be enabled"			},
87 	{ SF(QEND),		"last queue in stream"			},
88 	{ SF(QWANTWSYNC),	"Streamhead wants to write Q" 		},
89 	{ SF(QSYNCSTR),		"Q supports Synchronous STREAMS"	},
90 	{ SF(QISDRV),		"the Queue is attached to a driver"	},
91 	{ SF(0x00400000),	"unused (was QHOT)"			},
92 	{ SF(0x00800000),	"unused (was QNEXTHOT)"			},
93 	{ SF(0x01000000),	"unused (was _QNEXTLESS)"		},
94 	{ SF(0x02000000),	"unused"				},
95 	{ SF(_QINSERTING),	"module is inserted with _I_INSERT"	},
96 	{ SF(_QREMOVING)	"module is removed with _I_REMOVE"	},
97 	{ SF(_QASSOCIATED),	"queue is associated with a device"	},
98 	{ SF(0),		NULL }
99 };
100 
101 /*
102  * Syncq flags
103  */
104 static const struct str_flags sqf[] = {
105 	{ SF(SQ_EXCL),		"Exclusive access to inner perimeter"	},
106 	{ SF(SQ_BLOCKED),	"qprocsoff in progress"			},
107 	{ SF(SQ_FROZEN),	"freezestr in progress"			},
108 	{ SF(SQ_WRITER),	"qwriter(OUTER) pending or running"	},
109 	{ SF(SQ_MESSAGES),	"There are messages on syncq"		},
110 	{ SF(SQ_WANTWAKEUP)	"Thread waiting on sq_wait"		},
111 	{ SF(SQ_WANTEXWAKEUP),	"Thread waiting on sq_exwait"		},
112 	{ SF(SQ_EVENTS),	"There are events on syncq"		},
113 	{ SF(0),		NULL					}
114 };
115 
116 /*
117  * Syncq types
118  */
119 static const struct str_flags sqt[] = {
120 	{ SF(SQ_CIPUT),		"Concurrent inner put procedure"	},
121 	{ SF(SQ_CISVC),		"Concurrent inner svc procedure"	},
122 	{ SF(SQ_CIOC),		"Concurrent inner open/close"		},
123 	{ SF(SQ_CICB),		"Concurrent inner callback"		},
124 	{ SF(SQ_COPUT),		"Concurrent outer put procedure"	},
125 	{ SF(SQ_COSVC),		"Concurrent outer svc procedure"	},
126 	{ SF(SQ_COOC),		"Concurrent outer open/close"		},
127 	{ SF(SQ_COCB),		"Concurrent outer callback"		},
128 	{ SF(0),		NULL					}
129 };
130 
131 /*
132  * Stdata flags
133  */
134 static const struct str_flags stdf[] = {
135 	{ SF(IOCWAIT),		"someone is doing an ioctl"		},
136 	{ SF(RSLEEP),		"someone wants to read/recv msg"	},
137 	{ SF(WSLEEP),		"someone wants to write"		},
138 	{ SF(STRPRI),		"an M_PCPROTO is at stream head"	},
139 	{ SF(STRHUP),		"device has vanished"			},
140 	{ SF(STWOPEN),		"waiting for 1st open"			},
141 	{ SF(STPLEX),		"stream is being multiplexed"		},
142 	{ SF(STRISTTY),		"stream is a terminal"			},
143 	{ SF(STRGETINPROG),	"(k)strgetmsg is running"		},
144 	{ SF(IOCWAITNE),	"STR_NOERROR ioctl running"		},
145 	{ SF(STRDERR),		"fatal read error from M_ERROR"		},
146 	{ SF(STWRERR),		"fatal write error from M_ERROR"	},
147 	{ SF(STRDERRNONPERSIST), "nonpersistent read errors"		},
148 	{ SF(STWRERRNONPERSIST), "nonpersistent write errors"		},
149 	{ SF(STRCLOSE),		"wait for a close to complete"		},
150 	{ SF(SNDMREAD),		"used for read notification"		},
151 	{ SF(OLDNDELAY),	"use old NDELAY TTY semantics"		},
152 	{ SF(0x00020000),	"unused"				},
153 	{ SF(0x00040000),	"unused"				},
154 	{ SF(STRTOSTOP),	"block background writes"		},
155 	{ SF(0x00100000),	"unused"				},
156 	{ SF(0x00200000),	"unused"				},
157 	{ SF(STRMOUNT),		"stream is mounted"			},
158 	{ SF(STRNOTATMARK),	"Not at mark (when empty read q)"	},
159 	{ SF(STRDELIM),		"generate delimited messages"		},
160 	{ SF(STRATMARK),	"at mark (due to MSGMARKNEXT)"		},
161 	{ SF(STZCNOTIFY),	"wait for zerocopy mblk to be acked"	},
162 	{ SF(STRPLUMB),		"stream plumbing changes in progress"	},
163 	{ SF(STREOF),  		"End-of-file indication"		},
164 	{ SF(STREOPENFAIL),	"re-open has failed"			},
165 	{ SF(STRMATE),		"this stream is a mate"			},
166 	{ SF(STRHASLINKS),	"there are I_LINKs under this stream"	},
167 	{ SF(0),		NULL					}
168 };
169 
170 static const struct str_flags mbf[] = {
171 	{ SF(MSGMARK), 		"last byte of message is marked"	},
172 	{ SF(MSGNOLOOP),	"don't loop message to write side"	},
173 	{ SF(MSGDELIM),		"message is delimited"			},
174 	{ SF(0x08),		"unused"				},
175 	{ SF(MSGMARKNEXT), 	"Private: b_next's first byte marked"	},
176 	{ SF(MSGNOTMARKNEXT),	"Private: ... not marked"		},
177 	{ SF(MSGHASREF),	"Private: msg has reference to owner"	},
178 	{ SF(0),		NULL					}
179 };
180 
181 #define	M_DATA_T 0xff
182 
183 static const strtypes_t mbt[] = {
184 	{ "M_DATA",	M_DATA_T,	"regular data"			},
185 	{ "M_PROTO",	M_PROTO,	"protocol control"		},
186 	{ "M_MULTIDATA", M_MULTIDATA,	"multidata"			},
187 	{ "M_BREAK",	M_BREAK,	"line break"			},
188 	{ "M_PASSFP",	M_PASSFP,	"pass file pointer"		},
189 	{ "M_EVENT",	M_EVENT,	"Obsoleted: do not use"		},
190 	{ "M_SIG",	M_SIG,		"generate process signal"	},
191 	{ "M_DELAY",	M_DELAY,	"real-time xmit delay"		},
192 	{ "M_CTL",	M_CTL,		"device-specific control message" },
193 	{ "M_IOCTL",	M_IOCTL,	"ioctl; set/get params"		},
194 	{ "M_SETOPTS",	M_SETOPTS,	"set stream head options"	},
195 	{ "M_RSE",	M_RSE,		"reserved for RSE use only"	},
196 	{ "M_IOCACK",	M_IOCACK,	"acknowledge ioctl"		},
197 	{ "M_IOCNAK",	M_IOCNAK,	"negative ioctl acknowledge"	},
198 	{ "M_PCPROTO",	M_PCPROTO,	"priority proto message"	},
199 	{ "M_PCSIG",	M_PCSIG,	"generate process signal"	},
200 	{ "M_READ",	M_READ,		"generate read notification"	},
201 	{ "M_FLUSH",	M_FLUSH,	"flush your queues"		},
202 	{ "M_STOP",	M_STOP,		"stop transmission immediately" },
203 	{ "M_START",	M_START,	"restart transmission after stop" },
204 	{ "M_HANGUP",	M_HANGUP,	"line disconnect"		},
205 	{ "M_ERROR",	M_ERROR,	"send error to stream head"	},
206 	{ "M_COPYIN",	M_COPYIN,	"request to copyin data"	},
207 	{ "M_COPYOUT",	M_COPYOUT,	"request to copyout data"	},
208 	{ "M_IOCDATA",	M_IOCDATA,	"response to M_COPYIN and M_COPYOUT" },
209 	{ "M_PCRSE",	M_PCRSE,	"reserved for RSE use only"	},
210 	{ "M_STOPI",	M_STOPI,	"stop reception immediately"	},
211 	{ "M_STARTI",	M_STARTI,	"restart reception after stop"	},
212 	{ "M_PCEVENT",	M_PCEVENT,	"Obsoleted: do not use"		},
213 	{ "M_UNHANGUP",	M_UNHANGUP,	"line reconnect"		},
214 };
215 
216 /* Allocation flow trace events, starting from 0 */
217 static const char *ftev_alloc[] = {
218 /* 0 */	":allocb",
219 /* 1 */	":esballoc",
220 /* 2 */	":desballoc",
221 /* 3 */	":esballoca",
222 /* 4 */	":desballoca",
223 /* 5 */	":allocbig",
224 /* 6 */	":allocbw",
225 /* 7 */	":bcallocb",
226 /* 8 */	":freeb",
227 /* 9 */	":dupb",
228 /* A */	":copyb",
229 };
230 
231 #define	FTEV_PROC_START FTEV_PUT
232 
233 /* Procedures recorded by flow tracing, starting from 0x100 */
234 static const char *ftev_proc[] = {
235 /* 100 */	":put",
236 /* 101 */	":undefined (0x101)",
237 /* 102 */	":undefined (0x102)",
238 /* 103 */	":undefined (0x103)",
239 /* 104 */	":undefined (0x104)",
240 /* 105 */	":putq",
241 /* 106 */	":getq",
242 /* 107 */	":rmvq",
243 /* 108 */	":insq",
244 /* 109 */	":putbq",
245 /* 10A */	":flushq",
246 /* 10B */	":undefined (0x10b)",
247 /* 10C */ 	":undefined (0x10c)",
248 /* 10D */	":putnext",
249 /* 10E */	":rwnext",
250 };
251 
252 static const char *db_control_types[] = {
253 /* 00 */	"data",
254 /* 01 */	"proto",
255 /* 02 */	"multidata",
256 /* 03 */	"unused (0x03)",
257 /* 04 */	"unused (0x04)",
258 /* 05 */	"unused (0x05)",
259 /* 06 */	"unused (0x06)",
260 /* 07 */	"unused (0x07)",
261 /* 08 */	"break",
262 /* 09 */	"passfp",
263 /* 0a */	"event",
264 /* 0b */	"sig",
265 /* 0c */	"delay",
266 /* 0d */	"ctl",
267 /* 0e */	"ioctl",
268 /* 0f */	"unused",
269 /* 10 */	"setopts",
270 /* 11 */	"rse",
271 };
272 
273 static const char *db_control_hipri_types[] = {
274 /* 81 */	"iocack",
275 /* 82 */	"iocnak",
276 /* 83 */	"pcproto",
277 /* 84 */	"pcsig",
278 /* 85 */	"read",
279 /* 86 */	"flush",
280 /* 87 */	"stop",
281 /* 88 */	"start",
282 /* 89 */	"hangup",
283 /* 8a */	"error",
284 /* 8b */	"copyin",
285 /* 8c */	"copyout",
286 /* 8d */	"iocdata",
287 /* 8e */	"pcrse",
288 /* 8f */	"stopi",
289 /* 90 */	"starti",
290 /* 91 */	"pcevent",
291 /* 92 */	"unhangup",
292 };
293 
294 
295 #define	A_SIZE(a) (sizeof (a) / sizeof (a[0]))
296 
297 static void ft_printevent(ushort_t);
298 
299 static int
300 streams_parse_flag(const strflags_t ftable[], const char *arg, uint32_t *flag)
301 {
302 	int i;
303 
304 	for (i = 0; ftable[i].strf_name != NULL; i++) {
305 		if (strcasecmp(arg, ftable[i].strf_name) == 0) {
306 			*flag |= (1 << i);
307 			return (0);
308 		}
309 	}
310 
311 	return (-1);
312 }
313 
314 static void
315 streams_flag_usage(const strflags_t ftable[])
316 {
317 	int i;
318 
319 	for (i = 0; ftable[i].strf_name != NULL; i++)
320 		mdb_printf("%-14s %s\n",
321 		    ftable[i].strf_name, ftable[i].strf_descr);
322 }
323 
324 static int
325 streams_parse_type(const strtypes_t ftable[], const char *arg, uint32_t *flag)
326 {
327 	int i;
328 
329 	for (i = 0; ftable[i].strt_name != NULL; i++) {
330 		if (strcasecmp(arg, ftable[i].strt_name) == 0) {
331 			*flag = ftable[i].strt_value;
332 			return (0);
333 		}
334 	}
335 
336 	return (-1);
337 }
338 
339 static void
340 streams_type_usage(const strtypes_t ftable[])
341 {
342 	int i;
343 
344 	for (i = 0; ftable[i].strt_name != NULL; i++)
345 		mdb_printf("%-12s %s\n",
346 		    ftable[i].strt_name, ftable[i].strt_descr);
347 }
348 
349 int
350 queue(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
351 {
352 	const int QUEUE_FLGDELT = (int)(sizeof (uintptr_t) * 2 + 15);
353 
354 	char name[MODMAXNAMELEN];
355 	int nblks = 0;
356 	uintptr_t maddr;
357 	mblk_t mblk;
358 	queue_t q;
359 
360 	const char *mod = NULL, *flag = NULL, *not_flag = NULL;
361 	uint_t quiet = FALSE;
362 	uint_t verbose = FALSE;
363 	uint32_t mask = 0, not_mask = 0;
364 	uintptr_t syncq = 0;
365 
366 	if (!(flags & DCMD_ADDRSPEC)) {
367 		if (mdb_walk_dcmd("genunix`queue_cache", "genunix`queue",
368 		    argc, argv) == -1) {
369 			mdb_warn("failed to walk queue cache");
370 			return (DCMD_ERR);
371 		}
372 		return (DCMD_OK);
373 	}
374 
375 	if (flags & DCMD_PIPE_OUT)
376 		quiet = TRUE;
377 
378 	if (mdb_getopts(argc, argv,
379 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
380 	    'q', MDB_OPT_SETBITS, TRUE, &quiet,
381 	    'm', MDB_OPT_STR, &mod,
382 	    'f', MDB_OPT_STR, &flag,
383 	    'F', MDB_OPT_STR, &not_flag,
384 	    's', MDB_OPT_UINTPTR, &syncq,
385 	    NULL) != argc)
386 		return (DCMD_USAGE);
387 
388 	/*
389 	 * If any of the filtering flags is specified, don't print anything
390 	 * except the matching pointer.
391 	 */
392 	if (flag != NULL || not_flag != NULL || mod != NULL || syncq != NULL)
393 		quiet = TRUE;
394 
395 	if (DCMD_HDRSPEC(flags) && !quiet) {
396 		mdb_printf("%?s %-13s %6s %4s\n",
397 		    "ADDR", "MODULE", "FLAGS", "NBLK");
398 	}
399 
400 	if (flag != NULL && streams_parse_flag(qf, flag, &mask) == -1) {
401 		mdb_warn("unrecognized queue flag '%s'\n", flag);
402 		streams_flag_usage(qf);
403 		return (DCMD_USAGE);
404 	}
405 
406 	if (not_flag != NULL &&
407 	    streams_parse_flag(qf, not_flag, &not_mask) == -1) {
408 		mdb_warn("unrecognized queue flag '%s'\n", flag);
409 		streams_flag_usage(qf);
410 		return (DCMD_USAGE);
411 	}
412 
413 	if (mdb_vread(&q, sizeof (q), addr) == -1) {
414 		mdb_warn("couldn't read queue at %p", addr);
415 		return (DCMD_ERR);
416 	}
417 
418 	for (maddr = (uintptr_t)q.q_first; maddr != NULL; nblks++) {
419 		if (mdb_vread(&mblk, sizeof (mblk), maddr) == -1) {
420 			mdb_warn("couldn't read mblk %p for queue %p",
421 			    maddr, addr);
422 			break;
423 		}
424 		maddr = (uintptr_t)mblk.b_next;
425 	}
426 
427 	(void) mdb_qname(&q, name, sizeof (name));
428 
429 	/*
430 	 * If queue doesn't pass filtering criteria, don't print anything and
431 	 * just return.
432 	 */
433 
434 	if (mod != NULL && strcmp(mod, name) != 0)
435 		return (DCMD_OK);
436 
437 	if (mask != 0 && !(q.q_flag & mask))
438 		return (DCMD_OK);
439 
440 	if (not_mask != 0 && (q.q_flag & not_mask))
441 		return (DCMD_OK);
442 
443 	if (syncq != 0 && q.q_syncq != (syncq_t *)syncq)
444 		return (DCMD_OK);
445 
446 	/*
447 	 * Options are specified for filtering, so If any option is specified on
448 	 * the command line, just print address and exit.
449 	 */
450 	if (quiet) {
451 		mdb_printf("%0?p\n", addr);
452 		return (DCMD_OK);
453 	}
454 
455 	mdb_printf("%0?p %-13s %06x %4d %0?p\n",
456 	    addr, name, q.q_flag, nblks, q.q_first);
457 
458 	if (verbose) {
459 		int i, arm = 0;
460 
461 		for (i = 0; qf[i].strf_name != NULL; i++) {
462 			if (!(q.q_flag & (1 << i)))
463 				continue;
464 			if (!arm) {
465 				mdb_printf("%*s|\n%*s+-->  ",
466 				    QUEUE_FLGDELT, "", QUEUE_FLGDELT, "");
467 				arm = 1;
468 			} else
469 				mdb_printf("%*s      ", QUEUE_FLGDELT, "");
470 
471 			mdb_printf("%-12s %s\n",
472 			    qf[i].strf_name, qf[i].strf_descr);
473 		}
474 	}
475 
476 	return (DCMD_OK);
477 }
478 
479 int
480 syncq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
481 {
482 	const int SYNC_FLGDELT = (int)(sizeof (uintptr_t) * 2 + 1);
483 	const int SYNC_TYPDELT = (int)(sizeof (uintptr_t) * 2 + 5);
484 	syncq_t sq;
485 
486 	const char *flag = NULL, *not_flag = NULL;
487 	const char *typ = NULL, *not_typ = NULL;
488 	uint_t verbose = FALSE;
489 	uint_t quiet = FALSE;
490 	uint32_t mask = 0, not_mask = 0;
491 	uint32_t tmask = 0, not_tmask = 0;
492 	uint8_t sqtype = 0;
493 
494 	if (!(flags & DCMD_ADDRSPEC)) {
495 		if (mdb_walk_dcmd("genunix`syncq_cache", "genunix`syncq",
496 		    argc, argv) == -1) {
497 			mdb_warn("failed to walk syncq cache");
498 			return (DCMD_ERR);
499 		}
500 		return (DCMD_OK);
501 	}
502 
503 	if (flags & DCMD_PIPE_OUT)
504 		quiet = TRUE;
505 
506 	if (mdb_getopts(argc, argv,
507 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
508 	    'q', MDB_OPT_SETBITS, TRUE, &quiet,
509 	    'f', MDB_OPT_STR, &flag,
510 	    'F', MDB_OPT_STR, &not_flag,
511 	    't', MDB_OPT_STR, &typ,
512 	    'T', MDB_OPT_STR, &not_typ,
513 	    NULL) != argc)
514 		return (DCMD_USAGE);
515 
516 	/*
517 	 * If any of the filtering flags is specified, don't print anything
518 	 * except the matching pointer.
519 	 */
520 	if (flag != NULL || not_flag != NULL || typ != NULL || not_typ != NULL)
521 		quiet = TRUE;
522 
523 	if (DCMD_HDRSPEC(flags) && !quiet) {
524 		mdb_printf("%?s %s %s %s %s %?s %s %s\n",
525 		    "ADDR", "FLG", "TYP", "CNT", "NQS", "OUTER", "SF", "PRI");
526 	}
527 
528 	if (flag != NULL && streams_parse_flag(sqf, flag, &mask) == -1) {
529 		mdb_warn("unrecognized syncq flag '%s'\n", flag);
530 		streams_flag_usage(sqf);
531 		return (DCMD_USAGE);
532 	}
533 
534 	if (typ != NULL && streams_parse_flag(sqt, typ, &tmask) == -1) {
535 		mdb_warn("unrecognized syncq type '%s'\n", typ);
536 		streams_flag_usage(sqt);
537 		return (DCMD_USAGE);
538 	}
539 
540 	if (not_flag != NULL && streams_parse_flag(sqf, not_flag, &not_mask)
541 	    == -1) {
542 		mdb_warn("unrecognized syncq flag '%s'\n", not_flag);
543 		streams_flag_usage(sqf);
544 		return (DCMD_USAGE);
545 	}
546 
547 	if (not_typ != NULL && streams_parse_flag(sqt, not_typ, &not_tmask)
548 	    == -1) {
549 		mdb_warn("unrecognized syncq type '%s'\n", not_typ);
550 		streams_flag_usage(sqt);
551 		return (DCMD_USAGE);
552 	}
553 
554 	if (mdb_vread(&sq, sizeof (sq), addr) == -1) {
555 		mdb_warn("couldn't read syncq at %p", addr);
556 		return (DCMD_ERR);
557 	}
558 
559 	if (mask != 0 && !(sq.sq_flags & mask))
560 		return (DCMD_OK);
561 
562 	if (not_mask != 0 && (sq.sq_flags & not_mask))
563 		return (DCMD_OK);
564 
565 	sqtype = (sq.sq_type >> 8) & 0xff;
566 
567 	if (tmask != 0 && !(sqtype & tmask))
568 		return (DCMD_OK);
569 
570 	if (not_tmask != 0 && (sqtype & not_tmask))
571 		return (DCMD_OK);
572 
573 	/*
574 	 * Options are specified for filtering, so If any option is specified on
575 	 * the command line, just print address and exit.
576 	 */
577 	if (quiet) {
578 		mdb_printf("%0?p\n", addr);
579 		return (DCMD_OK);
580 	}
581 
582 	mdb_printf("%0?p %02x  %02x  %-3u %-3u %0?p  %1x %-3d\n",
583 	    addr, sq.sq_flags & 0xff, sqtype, sq.sq_count,
584 	    sq.sq_nqueues, sq.sq_outer, sq.sq_svcflags, sq.sq_pri);
585 
586 	if (verbose) {
587 		int i, arm = 0;
588 
589 		for (i = 0; sqf[i].strf_name != NULL; i++) {
590 			if (!(sq.sq_flags & (1 << i)))
591 				continue;
592 			if (!arm) {
593 				mdb_printf("%*s|\n%*s+-->  ",
594 				    SYNC_FLGDELT, "", SYNC_FLGDELT, "");
595 				arm = 1;
596 			} else
597 				mdb_printf("%*s      ", SYNC_FLGDELT, "");
598 
599 			mdb_printf("%-12s %s\n",
600 			    sqf[i].strf_name, sqf[i].strf_descr);
601 		}
602 
603 		for (i = 0; sqt[i].strf_name != NULL; i++) {
604 			if (!(sqtype & (1 << i)))
605 				continue;
606 			if (!arm) {
607 				mdb_printf("%*s|\n%*s+-->  ",
608 				    SYNC_TYPDELT, "", SYNC_TYPDELT, "");
609 				arm = 1;
610 			} else
611 				mdb_printf("%*s      ", SYNC_TYPDELT, "");
612 
613 			mdb_printf("%-12s %s\n",
614 			    sqt[i].strf_name, sqt[i].strf_descr);
615 		}
616 	}
617 
618 	return (DCMD_OK);
619 }
620 
621 int
622 stdata(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
623 {
624 	const int STREAM_FLGDELT = (int)(sizeof (uintptr_t) * 2 + 10);
625 
626 	stdata_t  sd;
627 
628 	const char *flag = NULL, *not_flag = NULL;
629 	uint_t verbose = FALSE;
630 	uint_t quiet = FALSE;
631 	uint32_t mask = 0, not_mask = 0;
632 
633 	if (!(flags & DCMD_ADDRSPEC)) {
634 		if (mdb_walk_dcmd("genunix`stream_head_cache",
635 		    "genunix`stdata", argc, argv) == -1) {
636 			mdb_warn("failed to walk stream head cache");
637 			return (DCMD_ERR);
638 		}
639 		return (DCMD_OK);
640 	}
641 
642 	if (flags & DCMD_PIPE_OUT)
643 		quiet = TRUE;
644 
645 	if (mdb_getopts(argc, argv,
646 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
647 	    'q', MDB_OPT_SETBITS, TRUE, &quiet,
648 	    'f', MDB_OPT_STR, &flag,
649 	    'F', MDB_OPT_STR, &not_flag,
650 	    NULL) != argc)
651 		return (DCMD_USAGE);
652 
653 	/*
654 	 * If any of the filtering flags is specified, don't print anything
655 	 * except the matching pointer.
656 	 */
657 	if (flag != NULL || not_flag != NULL)
658 		quiet = TRUE;
659 
660 	if (DCMD_HDRSPEC(flags) && !quiet) {
661 		mdb_printf("%?s %?s %8s %?s %s %s\n",
662 		    "ADDR", "WRQ", "FLAGS", "VNODE", "N/A", "REF");
663 	}
664 
665 	if (flag != NULL && streams_parse_flag(stdf, flag, &mask) == -1) {
666 		mdb_warn("unrecognized stream flag '%s'\n", flag);
667 		streams_flag_usage(stdf);
668 		return (DCMD_USAGE);
669 	}
670 
671 	if (not_flag != NULL &&
672 	    streams_parse_flag(stdf, not_flag, &not_mask) == -1) {
673 		mdb_warn("unrecognized stream flag '%s'\n", flag);
674 		streams_flag_usage(stdf);
675 		return (DCMD_USAGE);
676 	}
677 
678 	if (mdb_vread(&sd, sizeof (sd), addr) == -1) {
679 		mdb_warn("couldn't read stdata at %p", addr);
680 		return (DCMD_ERR);
681 	}
682 
683 	/*
684 	 * If stream doesn't pass filtering criteria, don't print anything and
685 	 * just return.
686 	 */
687 
688 	if (mask != 0 && !(sd.sd_flag & mask))
689 		return (DCMD_OK);
690 
691 	if (not_mask != 0 && (sd.sd_flag & not_mask))
692 		return (DCMD_OK);
693 
694 	/*
695 	 * Options are specified for filtering, so If any option is specified on
696 	 * the command line, just print address and exit.
697 	 */
698 	if (quiet) {
699 		mdb_printf("%0?p\n", addr);
700 		return (DCMD_OK);
701 	}
702 
703 	mdb_printf("%0?p %0?p %08x %0?p %d/%d %d\n",
704 	    addr, sd.sd_wrq, sd.sd_flag, sd.sd_vnode,
705 	    sd.sd_pushcnt, sd.sd_anchor, sd.sd_refcnt);
706 
707 	if (verbose) {
708 		int i, arm = 0;
709 
710 		for (i = 0; stdf[i].strf_name != NULL; i++) {
711 			if (!(sd.sd_flag & (1 << i)))
712 				continue;
713 			if (!arm) {
714 				mdb_printf("%*s|\n%*s+-->  ",
715 				    STREAM_FLGDELT, "", STREAM_FLGDELT, "");
716 				arm = 1;
717 			} else
718 				mdb_printf("%*s      ", STREAM_FLGDELT, "");
719 
720 			mdb_printf("%-12s %s\n",
721 			    stdf[i].strf_name, stdf[i].strf_descr);
722 		}
723 	}
724 
725 	return (DCMD_OK);
726 }
727 
728 /*ARGSUSED*/
729 static void
730 qprint_syncq(queue_t *addr, queue_t *q)
731 {
732 	mdb_printf("%p\n", q->q_syncq);
733 }
734 
735 /*ARGSUSED*/
736 static void
737 qprint_stream(queue_t *addr, queue_t *q)
738 {
739 	mdb_printf("%p\n", q->q_stream);
740 }
741 
742 static void
743 qprint_wrq(queue_t *addr, queue_t *q)
744 {
745 	mdb_printf("%p\n", ((q)->q_flag & QREADR? (addr)+1: (addr)));
746 }
747 
748 static void
749 qprint_rdq(queue_t *addr, queue_t *q)
750 {
751 	mdb_printf("%p\n", ((q)->q_flag & QREADR? (addr): (addr)-1));
752 }
753 
754 static void
755 qprint_otherq(queue_t *addr, queue_t *q)
756 {
757 	mdb_printf("%p\n", ((q)->q_flag & QREADR? (addr)+1: (addr)-1));
758 }
759 
760 static int
761 q2x(uintptr_t addr, int argc, qprint_func prfunc)
762 {
763 	queue_t q;
764 
765 	if (argc != 0)
766 		return (DCMD_USAGE);
767 
768 	if (mdb_vread(&q, sizeof (q), addr) == -1) {
769 		mdb_warn("couldn't read queue at %p", addr);
770 		return (DCMD_ERR);
771 	}
772 
773 	prfunc((queue_t *)addr, &q);
774 
775 	return (DCMD_OK);
776 }
777 
778 
779 /*ARGSUSED*/
780 int
781 q2syncq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
782 {
783 	return (q2x(addr, argc, qprint_syncq));
784 }
785 
786 /*ARGSUSED*/
787 int
788 q2stream(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
789 {
790 	return (q2x(addr, argc, qprint_stream));
791 }
792 
793 /*ARGSUSED*/
794 int
795 q2rdq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
796 {
797 	return (q2x(addr, argc, qprint_rdq));
798 }
799 
800 /*ARGSUSED*/
801 int
802 q2wrq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
803 {
804 	return (q2x(addr, argc, qprint_wrq));
805 }
806 
807 /*ARGSUSED*/
808 int
809 q2otherq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
810 {
811 	return (q2x(addr, argc, qprint_otherq));
812 }
813 
814 static int
815 sd2x(uintptr_t addr, int argc, sdprint_func prfunc)
816 {
817 	stdata_t sd;
818 
819 	if (argc != 0)
820 		return (DCMD_USAGE);
821 
822 	if (mdb_vread(&sd, sizeof (sd), addr) == -1) {
823 		mdb_warn("couldn't read stream head at %p", addr);
824 		return (DCMD_ERR);
825 	}
826 
827 	prfunc((stdata_t *)addr, &sd);
828 
829 	return (DCMD_OK);
830 }
831 
832 /*ARGSUSED*/
833 static void
834 sdprint_wrq(stdata_t *addr, stdata_t *sd)
835 {
836 	mdb_printf("%p\n", sd->sd_wrq);
837 }
838 
839 static void
840 sdprint_mate(stdata_t *addr, stdata_t *sd)
841 {
842 	mdb_printf("%p\n", sd->sd_mate ? sd->sd_mate : addr);
843 }
844 
845 /*ARGSUSED*/
846 int
847 str2mate(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
848 {
849 	return (sd2x(addr, argc, sdprint_mate));
850 }
851 
852 /*ARGSUSED*/
853 int
854 str2wrq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
855 {
856 	return (sd2x(addr, argc, sdprint_wrq));
857 }
858 
859 
860 /*
861  * If this syncq is a part of the queue pair structure, find the queue for it.
862  */
863 /*ARGSUSED*/
864 int
865 syncq2q(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
866 {
867 	syncq_t sq;
868 	queue_t q;
869 	queue_t *qp;
870 
871 	if (argc != 0)
872 		return (DCMD_USAGE);
873 
874 	if (mdb_vread(&sq, sizeof (sq), addr) == -1) {
875 		mdb_warn("couldn't read syncq at %p", addr);
876 		return (DCMD_ERR);
877 	}
878 
879 	/* Try to find its queue */
880 	qp = (queue_t *)addr - 2;
881 
882 	if ((mdb_vread(&q, sizeof (q), (uintptr_t)qp) == -1) ||
883 	    (q.q_syncq != (syncq_t *)addr)) {
884 		mdb_warn("syncq2q: %p is not part of any queue\n", addr);
885 		return (DCMD_ERR);
886 	} else
887 		mdb_printf("%p\n", qp);
888 
889 	return (DCMD_OK);
890 }
891 
892 
893 int
894 queue_walk_init(mdb_walk_state_t *wsp)
895 {
896 	if (wsp->walk_addr == NULL &&
897 	    mdb_readvar(&wsp->walk_addr, "qhead") == -1) {
898 		mdb_warn("failed to read 'qhead'");
899 		return (WALK_ERR);
900 	}
901 
902 	wsp->walk_data = mdb_alloc(sizeof (queue_t), UM_SLEEP);
903 	return (WALK_NEXT);
904 }
905 
906 int
907 queue_link_step(mdb_walk_state_t *wsp)
908 {
909 	int status;
910 
911 	if (wsp->walk_addr == NULL)
912 		return (WALK_DONE);
913 
914 	if (mdb_vread(wsp->walk_data, sizeof (queue_t), wsp->walk_addr) == -1) {
915 		mdb_warn("failed to read queue at %p", wsp->walk_addr);
916 		return (WALK_DONE);
917 	}
918 
919 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
920 	    wsp->walk_cbdata);
921 
922 	wsp->walk_addr = (uintptr_t)(((queue_t *)wsp->walk_data)->q_link);
923 	return (status);
924 }
925 
926 int
927 queue_next_step(mdb_walk_state_t *wsp)
928 {
929 	int status;
930 
931 	if (wsp->walk_addr == NULL)
932 		return (WALK_DONE);
933 
934 	if (mdb_vread(wsp->walk_data, sizeof (queue_t), wsp->walk_addr) == -1) {
935 		mdb_warn("failed to read queue at %p", wsp->walk_addr);
936 		return (WALK_DONE);
937 	}
938 
939 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
940 	    wsp->walk_cbdata);
941 
942 	wsp->walk_addr = (uintptr_t)(((queue_t *)wsp->walk_data)->q_next);
943 	return (status);
944 }
945 
946 void
947 queue_walk_fini(mdb_walk_state_t *wsp)
948 {
949 	mdb_free(wsp->walk_data, sizeof (queue_t));
950 }
951 
952 int
953 str_walk_init(mdb_walk_state_t *wsp)
954 {
955 	stdata_t s;
956 
957 	if (wsp->walk_addr == NULL) {
958 		mdb_warn("walk must begin at address of stdata_t\n");
959 		return (WALK_ERR);
960 	}
961 
962 	if (mdb_vread(&s, sizeof (s), wsp->walk_addr) == -1) {
963 		mdb_warn("failed to read stdata at %p", wsp->walk_addr);
964 		return (WALK_ERR);
965 	}
966 
967 	wsp->walk_addr = (uintptr_t)s.sd_wrq;
968 	wsp->walk_data = mdb_alloc(sizeof (queue_t) * 2, UM_SLEEP);
969 
970 	return (WALK_NEXT);
971 }
972 
973 int
974 strr_walk_step(mdb_walk_state_t *wsp)
975 {
976 	queue_t *rq = wsp->walk_data, *wq = rq + 1;
977 	int status;
978 
979 	if (wsp->walk_addr == NULL)
980 		return (WALK_DONE);
981 
982 	if (mdb_vread(wsp->walk_data, sizeof (queue_t) * 2,
983 	    wsp->walk_addr - sizeof (queue_t)) == -1) {
984 		mdb_warn("failed to read queue pair at %p",
985 		    wsp->walk_addr - sizeof (queue_t));
986 		return (WALK_DONE);
987 	}
988 
989 	status = wsp->walk_callback(wsp->walk_addr - sizeof (queue_t),
990 	    rq, wsp->walk_cbdata);
991 
992 	if (wq->q_next != NULL)
993 		wsp->walk_addr = (uintptr_t)wq->q_next;
994 	else
995 		wsp->walk_addr = mdb_qwnext(wq);
996 
997 	return (status);
998 }
999 
1000 int
1001 strw_walk_step(mdb_walk_state_t *wsp)
1002 {
1003 	queue_t *rq = wsp->walk_data, *wq = rq + 1;
1004 	int status;
1005 
1006 	if (wsp->walk_addr == NULL)
1007 		return (WALK_DONE);
1008 
1009 	if (mdb_vread(wsp->walk_data, sizeof (queue_t) * 2,
1010 	    wsp->walk_addr - sizeof (queue_t)) == -1) {
1011 		mdb_warn("failed to read queue pair at %p",
1012 		    wsp->walk_addr - sizeof (queue_t));
1013 		return (WALK_DONE);
1014 	}
1015 
1016 	status = wsp->walk_callback(wsp->walk_addr, wq, wsp->walk_cbdata);
1017 
1018 	if (wq->q_next != NULL)
1019 		wsp->walk_addr = (uintptr_t)wq->q_next;
1020 	else
1021 		wsp->walk_addr = mdb_qwnext(wq);
1022 
1023 	return (status);
1024 }
1025 
1026 void
1027 str_walk_fini(mdb_walk_state_t *wsp)
1028 {
1029 	mdb_free(wsp->walk_data, sizeof (queue_t) * 2);
1030 }
1031 
1032 static int
1033 print_qpair(uintptr_t addr, const queue_t *q, uint_t *depth)
1034 {
1035 	static const char box_lid[] =
1036 	    "+-----------------------+-----------------------+\n";
1037 	static const char box_sep[] =
1038 	    "|                       |                       |\n";
1039 
1040 	char wname[32], rname[32], info1[256], *info2;
1041 
1042 	if (*depth != 0) {
1043 		mdb_printf("            |                       ^\n");
1044 		mdb_printf("            v                       |\n");
1045 	} else
1046 		mdb_printf("\n");
1047 
1048 	(void) mdb_qname(_WR(q), wname, sizeof (wname));
1049 	(void) mdb_qname(_RD(q), rname, sizeof (rname));
1050 
1051 	mdb_qinfo(_WR(q), info1, sizeof (info1));
1052 	if ((info2 = strchr(info1, '\n')) != NULL)
1053 		*info2++ = '\0';
1054 	else
1055 		info2 = "";
1056 
1057 	mdb_printf(box_lid);
1058 	mdb_printf("| 0x%-19p | 0x%-19p | %s\n",
1059 	    addr, addr - sizeof (queue_t), info1);
1060 
1061 	mdb_printf("| %<b>%-21s%</b> | %<b>%-21s%</b> |", wname, rname);
1062 	mdb_flush(); /* Account for buffered terminal sequences */
1063 
1064 	mdb_printf(" %s\n", info2);
1065 	mdb_printf(box_sep);
1066 
1067 	mdb_qinfo(_RD(q), info1, sizeof (info1));
1068 	if ((info2 = strchr(info1, '\n')) != NULL)
1069 		*info2++ = '\0';
1070 	else
1071 		info2 = "";
1072 
1073 	mdb_printf("| cnt = 0t%-13lu | cnt = 0t%-13lu | %s\n",
1074 	    _WR(q)->q_count, _RD(q)->q_count, info1);
1075 
1076 	mdb_printf("| flg = 0x%08x      | flg = 0x%08x      | %s\n",
1077 	    _WR(q)->q_flag, _RD(q)->q_flag, info2);
1078 
1079 	mdb_printf(box_lid);
1080 	*depth += 1;
1081 	return (0);
1082 }
1083 
1084 /*ARGSUSED*/
1085 int
1086 stream(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1087 {
1088 	uint_t d = 0;	/* Depth counter for print_qpair */
1089 
1090 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1091 		return (DCMD_USAGE);
1092 
1093 	if (mdb_pwalk("writeq", (mdb_walk_cb_t)print_qpair, &d, addr) == -1) {
1094 		mdb_warn("failed to walk writeq");
1095 		return (DCMD_ERR);
1096 	}
1097 
1098 	return (DCMD_OK);
1099 }
1100 
1101 int
1102 mblk_walk_init(mdb_walk_state_t *wsp)
1103 {
1104 	wsp->walk_data = mdb_alloc(sizeof (mblk_t), UM_SLEEP);
1105 	return (WALK_NEXT);
1106 }
1107 
1108 int
1109 b_cont_step(mdb_walk_state_t *wsp)
1110 {
1111 	int status;
1112 
1113 	if (wsp->walk_addr == NULL)
1114 		return (WALK_DONE);
1115 
1116 	if (mdb_vread(wsp->walk_data, sizeof (mblk_t), wsp->walk_addr) == -1) {
1117 		mdb_warn("failed to read mblk at %p", wsp->walk_addr);
1118 		return (WALK_DONE);
1119 	}
1120 
1121 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
1122 	    wsp->walk_cbdata);
1123 
1124 	wsp->walk_addr = (uintptr_t)(((mblk_t *)wsp->walk_data)->b_cont);
1125 	return (status);
1126 }
1127 
1128 int
1129 b_next_step(mdb_walk_state_t *wsp)
1130 {
1131 	int status;
1132 
1133 	if (wsp->walk_addr == NULL)
1134 		return (WALK_DONE);
1135 
1136 	if (mdb_vread(wsp->walk_data, sizeof (mblk_t), wsp->walk_addr) == -1) {
1137 		mdb_warn("failed to read mblk at %p", wsp->walk_addr);
1138 		return (WALK_DONE);
1139 	}
1140 
1141 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
1142 	    wsp->walk_cbdata);
1143 
1144 	wsp->walk_addr = (uintptr_t)(((mblk_t *)wsp->walk_data)->b_next);
1145 	return (status);
1146 }
1147 
1148 void
1149 mblk_walk_fini(mdb_walk_state_t *wsp)
1150 {
1151 	mdb_free(wsp->walk_data, sizeof (mblk_t));
1152 }
1153 
1154 /* ARGSUSED */
1155 int
1156 mblk2dblk(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1157 {
1158 	mblk_t mb;
1159 
1160 	if (argc != 0)
1161 		return (DCMD_USAGE);
1162 
1163 	if (mdb_vread(&mb, sizeof (mb), addr) == -1) {
1164 		mdb_warn("couldn't read mblk at %p", addr);
1165 		return (DCMD_ERR);
1166 	}
1167 
1168 	mdb_printf("%p\n", mb.b_datap);
1169 	return (DCMD_OK);
1170 }
1171 
1172 
1173 static void
1174 mblk_error(int *error, uintptr_t addr, char *message)
1175 {
1176 	if (!*error)
1177 		mdb_printf("%?lx: ", addr);
1178 	else
1179 		mdb_printf(", ");
1180 	mdb_printf("%s", message);
1181 	*error = 1;
1182 }
1183 
1184 int
1185 mblk_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1186 {
1187 	mblk_t	mb;
1188 	dblk_t	db;
1189 	int	error = 0;
1190 
1191 	if (!(flags & DCMD_ADDRSPEC)) {
1192 		if (mdb_walk_dcmd("streams_mblk", "mblk_verify", argc, argv) ==
1193 		    -1) {
1194 			mdb_warn("can't walk mblk cache");
1195 			return (DCMD_ERR);
1196 		}
1197 		return (DCMD_OK);
1198 	}
1199 
1200 	if (mdb_vread(&mb, sizeof (mblk_t), addr) == -1) {
1201 		mdb_warn("can't read mblk_t at 0x%lx", addr);
1202 		return (DCMD_ERR);
1203 	}
1204 
1205 	if (mdb_vread(&db, sizeof (dblk_t), (uintptr_t)mb.b_datap) == -1) {
1206 		mdb_warn("%?lx: invalid b_datap pointer\n", addr);
1207 		return (DCMD_ERR);
1208 	}
1209 
1210 	if (mb.b_rptr < db.db_base || mb.b_rptr > db.db_lim)
1211 		mblk_error(&error, addr, "b_rptr out of range");
1212 
1213 	if (mb.b_wptr < db.db_base || mb.b_wptr > db.db_lim)
1214 		mblk_error(&error, addr, "b_wptr out of range");
1215 
1216 	if (error)
1217 		mdb_printf("\n");
1218 
1219 	return (error ? DCMD_ERR : DCMD_OK);
1220 }
1221 
1222 int
1223 mblk_prt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1224 {
1225 	const int MBLK_FLGDELT = (int)(sizeof (uintptr_t) * 2 + 15);
1226 	mblk_t mblk;
1227 	dblk_t dblk;
1228 	int b_flag;
1229 	int db_type;
1230 	int mblklen;
1231 	uint64_t len = ~0UL;
1232 	uint64_t glen = ~0UL;
1233 	uint64_t llen = ~0UL;
1234 	uint64_t blen = ~0UL;
1235 	const char *dbtype;
1236 	const char *flag = NULL, *not_flag = NULL;
1237 	const char *typ = NULL, *not_typ = NULL;
1238 	uintptr_t  dbaddr = 0;
1239 	uint32_t tmask = 0, not_tmask = 0;
1240 	uint32_t mask = 0, not_mask = 0;
1241 	uint_t quiet = FALSE;
1242 	uint_t verbose = FALSE;
1243 
1244 	if (!(flags & DCMD_ADDRSPEC)) {
1245 		if (mdb_walk_dcmd("genunix`streams_mblk", "genunix`mblk",
1246 		    argc, argv) == -1) {
1247 			mdb_warn("failed to walk mblk cache");
1248 			return (DCMD_ERR);
1249 		}
1250 		return (DCMD_OK);
1251 	}
1252 
1253 	if (flags & DCMD_PIPE_OUT)
1254 		quiet = TRUE;
1255 
1256 	if (mdb_getopts(argc, argv,
1257 		'v', MDB_OPT_SETBITS, TRUE, &verbose,
1258 		'q', MDB_OPT_SETBITS, TRUE, &quiet,
1259 		'f', MDB_OPT_STR, &flag,
1260 		'F', MDB_OPT_STR, &not_flag,
1261 		't', MDB_OPT_STR, &typ,
1262 		'T', MDB_OPT_STR, &not_typ,
1263 		'l', MDB_OPT_UINT64, &len,
1264 		'L', MDB_OPT_UINT64, &llen,
1265 		'G', MDB_OPT_UINT64, &glen,
1266 		'b', MDB_OPT_UINT64, &blen,
1267 		'd', MDB_OPT_UINTPTR, &dbaddr,
1268 	    NULL) != argc)
1269 		return (DCMD_USAGE);
1270 
1271 	/*
1272 	 * If any of the filtering flags is specified, don't print anything
1273 	 * except the matching pointer.
1274 	 */
1275 	if ((flag != NULL) || (not_flag != NULL) || (typ != NULL) ||
1276 	    (not_typ != NULL) || (len != ~0UL) || (glen != ~0UL) ||
1277 	    (llen != ~0UL) || (blen != ~0UL) || (dbaddr != 0))
1278 		quiet = TRUE;
1279 
1280 	if (flag != NULL && streams_parse_flag(mbf, flag, &mask) == -1) {
1281 		mdb_warn("unrecognized mblk flag '%s'\n", flag);
1282 		streams_flag_usage(mbf);
1283 		return (DCMD_USAGE);
1284 	}
1285 
1286 	if (not_flag != NULL &&
1287 	    streams_parse_flag(mbf, not_flag, &not_mask) == -1) {
1288 		mdb_warn("unrecognized mblk flag '%s'\n", flag);
1289 		streams_flag_usage(mbf);
1290 		return (DCMD_USAGE);
1291 	}
1292 
1293 	if (typ != NULL && streams_parse_type(mbt, typ, &tmask) == -1) {
1294 		mdb_warn("unrecognized dblk type '%s'\n", typ);
1295 		streams_type_usage(mbt);
1296 		return (DCMD_USAGE);
1297 	}
1298 
1299 	if (not_typ != NULL && streams_parse_type(mbt, not_typ, &not_tmask)
1300 	    == -1) {
1301 		mdb_warn("unrecognized dblk type '%s'\n", not_typ);
1302 		streams_type_usage(mbt);
1303 		return (DCMD_USAGE);
1304 	}
1305 
1306 	if (DCMD_HDRSPEC(flags) && !quiet) {
1307 		mdb_printf("%?s %2s %-7s %-5s %-5s %?s %?s\n",
1308 		    "ADDR", "FL", "TYPE", "LEN", "BLEN", "RPTR", "DBLK");
1309 	}
1310 
1311 	if (mdb_vread(&mblk, sizeof (mblk), addr) == -1) {
1312 		mdb_warn("couldn't read mblk at %p", addr);
1313 		return (DCMD_ERR);
1314 	}
1315 	b_flag = mblk.b_flag;
1316 
1317 	if (mask != 0 && !(b_flag & mask))
1318 		return (DCMD_OK);
1319 
1320 	if (not_mask != 0 && (b_flag & not_mask))
1321 		return (DCMD_OK);
1322 
1323 	if (mdb_vread(&dblk, sizeof (dblk), (uintptr_t)(mblk.b_datap)) == -1) {
1324 		mdb_warn("couldn't read dblk at %p/%p", addr, mblk.b_datap);
1325 		return (DCMD_ERR);
1326 	}
1327 	db_type = dblk.db_type;
1328 
1329 	/* M_DATA is 0, so tmask has special value 0xff for it */
1330 	if ((tmask != 0) &&
1331 	    (((tmask == M_DATA_T) && (db_type != M_DATA)) ||
1332 		((tmask != M_DATA_T) && (db_type != tmask))))
1333 		return (DCMD_OK);
1334 
1335 
1336 	if ((not_tmask != 0) &&
1337 	    (((not_tmask == M_DATA_T) && (db_type == M_DATA)) ||
1338 		(db_type == not_tmask)))
1339 		return (DCMD_OK);
1340 
1341 	if (dbaddr != 0 && (uintptr_t)mblk.b_datap != dbaddr)
1342 		return (DCMD_OK);
1343 
1344 	mblklen = MBLKL(&mblk);
1345 
1346 	if ((len != ~0UL) && (len != mblklen))
1347 		return (DCMD_OK);
1348 
1349 	if ((llen != ~0Ul) && (mblklen > (int)llen))
1350 		return (DCMD_OK);
1351 
1352 	if ((glen != ~0Ul) && (mblklen < (int)glen))
1353 		return (DCMD_OK);
1354 
1355 	if ((blen != ~0UL) && (blen != (dblk.db_lim - dblk.db_base)))
1356 		return (DCMD_OK);
1357 
1358 	/*
1359 	 * Options are specified for filtering, so If any option is specified on
1360 	 * the command line, just print address and exit.
1361 	 */
1362 	if (quiet) {
1363 		mdb_printf("%0?p\n", addr);
1364 		return (DCMD_OK);
1365 	}
1366 
1367 	/* Figure out symbolic DB_TYPE */
1368 	if (db_type < A_SIZE(db_control_types)) {
1369 		dbtype = db_control_types[db_type];
1370 	} else {
1371 		/*
1372 		 * Must be a high-priority message -- adjust so that
1373 		 * "QPCTL + 1" corresponds to db_control_hipri_types[0]
1374 		 */
1375 		db_type -= (QPCTL + 1);
1376 		if (db_type >= 0 && db_type < A_SIZE(db_control_hipri_types))
1377 			dbtype = db_control_hipri_types[db_type];
1378 		else
1379 			dbtype = "UNKNOWN";
1380 	}
1381 
1382 	mdb_printf("%0?p %-2x %-7s %-5d %-5d %0?p %0?p\n",
1383 	    addr, b_flag, dbtype, mblklen, dblk.db_lim - dblk.db_base,
1384 	    mblk.b_rptr, mblk.b_datap);
1385 
1386 	if (verbose) {
1387 		int i, arm = 0;
1388 
1389 		for (i = 0; mbf[i].strf_name != NULL; i++) {
1390 			if (!(b_flag & (1 << i)))
1391 				continue;
1392 			if (!arm) {
1393 				mdb_printf("%*s|\n%*s+-->  ",
1394 				    MBLK_FLGDELT, "", MBLK_FLGDELT, "");
1395 				arm = 1;
1396 			} else
1397 				mdb_printf("%*s      ", MBLK_FLGDELT, "");
1398 
1399 			mdb_printf("%-12s %s\n",
1400 			    mbf[i].strf_name, mbf[i].strf_descr);
1401 		}
1402 	}
1403 	return (DCMD_OK);
1404 }
1405 
1406 /*
1407  * Streams flow trace walkers.
1408  */
1409 
1410 int
1411 strftblk_walk_init(mdb_walk_state_t *wsp)
1412 {
1413 	ftblkdata_t *ftd;
1414 	dblk_t	db;
1415 
1416 	/* Get the dblock from the address */
1417 	if (mdb_vread(&db, sizeof (dblk_t), wsp->walk_addr) == -1) {
1418 		mdb_warn("failed to read dblk at %p", wsp->walk_addr);
1419 		return (WALK_ERR);
1420 	}
1421 
1422 	/* Is there any flow trace data? */
1423 	if (db.db_fthdr == NULL) {
1424 		return (WALK_DONE);
1425 	}
1426 
1427 	wsp->walk_addr = (uintptr_t)((char *)db.db_fthdr +
1428 	    offsetof(fthdr_t, first));
1429 
1430 	ftd = mdb_alloc(sizeof (ftblkdata_t), UM_SLEEP);
1431 	ftd->ft_ix = 0;
1432 	ftd->ft_in_evlist = B_FALSE;
1433 	wsp->walk_data = ftd;
1434 
1435 	return (WALK_NEXT);
1436 }
1437 
1438 int
1439 strftblk_step(mdb_walk_state_t *wsp)
1440 {
1441 	ftblkdata_t *ftd;
1442 	ftblk_t *ftbp;
1443 	int status = WALK_NEXT;
1444 
1445 	if (wsp->walk_addr == NULL)
1446 		return (WALK_DONE);
1447 
1448 	ftd = (ftblkdata_t *)wsp->walk_data;
1449 	ftbp = &(ftd->ft_data);
1450 
1451 	if (! ftd->ft_in_evlist) {
1452 		/* Read a new ft block */
1453 		if (mdb_vread(ftbp, sizeof (ftblk_t),
1454 		    wsp->walk_addr) == -1) {
1455 			mdb_warn("failed to read ftblk at %p", wsp->walk_addr);
1456 			return (WALK_ERR);
1457 		}
1458 		/*
1459 		 * Check correctness of the index field.
1460 		 */
1461 		if (ftbp->ix < 0 || ftbp->ix > FTBLK_EVNTS) {
1462 			mdb_warn("ftblk: incorrect index value %i\n", ftbp->ix);
1463 			return (WALK_ERR);
1464 		}
1465 		ftd->ft_ix = 1;
1466 		ftd->ft_in_evlist = B_TRUE;
1467 	}
1468 
1469 	if (ftd->ft_ix > ftbp->ix) {
1470 		ftd->ft_in_evlist = B_FALSE;
1471 		/* End of event list reached - move to the next event block */
1472 		wsp->walk_addr = (uintptr_t)ftbp->nxt;
1473 	} else {
1474 		/* Print event address */
1475 		status = wsp->walk_callback((uintptr_t)((char *)wsp->walk_addr +
1476 		    offsetof(ftblk_t, ev) +
1477 		    (ftd->ft_ix - 1) * sizeof (struct ftevnt)),
1478 		    wsp->walk_data, wsp->walk_cbdata);
1479 		ftd->ft_ix++;
1480 	}
1481 
1482 	return (status);
1483 }
1484 
1485 
1486 void
1487 strftblk_walk_fini(mdb_walk_state_t *wsp)
1488 {
1489 	mdb_free(wsp->walk_data, sizeof (ftblkdata_t));
1490 }
1491 
1492 /*ARGSUSED*/
1493 int
1494 strftevent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1495 {
1496 	struct ftevnt ev;
1497 	char modname[MODMAXNAMELEN];
1498 
1499 	if (!(flags & DCMD_ADDRSPEC))
1500 		return (DCMD_USAGE);
1501 
1502 	if (DCMD_HDRSPEC(flags)) {
1503 		mdb_printf("%?s %?s %s %s %-24s  %s\n",
1504 		    "ADDR", "MID", "EVNT", "DATA", "NAME", "EVENT");
1505 	}
1506 
1507 	if (mdb_vread(&ev, sizeof (ev), addr) == -1) {
1508 		mdb_warn("couldn't read ft event at %p", addr);
1509 		return (DCMD_ERR);
1510 	}
1511 
1512 	mdb_printf("%0?p %0?p %4x %4x",
1513 	    addr, ev.mid, ev.evnt, ev.data);
1514 
1515 	if (ev.evnt & FTEV_QMASK) {
1516 		if (mdb_readstr(modname, sizeof (modname),
1517 		    (uintptr_t)ev.mid) == -1)
1518 			mdb_warn("couldn't read module name at %p", ev.mid);
1519 		mdb_printf(" %-24s", modname);
1520 	} else
1521 		mdb_printf(" %-24a", ev.mid);
1522 
1523 	ft_printevent(ev.evnt);
1524 
1525 	mdb_printf("\n");
1526 
1527 	return (DCMD_OK);
1528 }
1529 
1530 static void
1531 ft_printevent(ushort_t ev)
1532 {
1533 	ushort_t proc_ev = (ev & (FTEV_PROC_START | 0xFF)) - FTEV_PROC_START;
1534 	ushort_t alloc_ev = ev & FTEV_CALLER;
1535 
1536 	/* Get event class first */
1537 	if (ev & FTEV_PROC_START) {
1538 		if (proc_ev >= A_SIZE(ftev_proc))
1539 			mdb_printf("  undefined");
1540 		else
1541 			mdb_printf("  %s", ftev_proc[proc_ev]);
1542 	} else if (alloc_ev >= A_SIZE(ftev_alloc))
1543 		mdb_printf("  undefined");
1544 	else
1545 		mdb_printf("  %s", ftev_alloc[alloc_ev]);
1546 
1547 	/* Print event modifiers, if any */
1548 
1549 	if (ev & FTEV_PS)
1550 		mdb_printf(" | PS");
1551 	if (ev & FTEV_CS)
1552 		mdb_printf(" | CS");
1553 	if (ev & FTEV_ISWR)
1554 		mdb_printf(" | ISWR");
1555 }
1556 
1557 /*
1558  * Help functions for STREAMS debugging facilities.
1559  */
1560 void
1561 queue_help(void)
1562 {
1563 	mdb_printf("Print queue information for a given queue pointer.\n"
1564 	    "\nWithout the address of a \"queue_t\" structure given, print "
1565 	    "information about all\n"
1566 	    "queues in the \"queue_cache\".\n\n"
1567 	    "Options:\n"
1568 	    "	-v:\t\tbe verbose - print symbolic flags falues\n"
1569 	    "	-q:\t\tbe quiet - print queue pointer only\n"
1570 	    "	-f flag:\tprint only queues with flag set\n"
1571 	    "	-F flag:\tprint only queues with flag NOT set\n"
1572 	    "	-m modname:\tprint only queues with specified module name\n"
1573 	    "	-s syncq_addr:\tprint only queues which use specified syncq\n\n"
1574 	    "Available conversions:\n"
1575 	    "	q2rdq:		given a queue addr print read queue pointer\n"
1576 	    "	q2wrq:		given a queue addr print write queue pointer\n"
1577 	    "	q2otherq:	given a queue addr print other queue pointer\n"
1578 	    "	q2syncq:	given a queue addr print syncq pointer"
1579 		" (::help syncq)\n"
1580 	    "	q2stream:	given a queue addr print its stream pointer\n"
1581 		"\t\t(see ::help stream and ::help stdata)\n\n"
1582 	    "To walk q_next pointer of the queue use\n"
1583 	    "	queue_addr::walk qnext\n");
1584 }
1585 
1586 void
1587 syncq_help(void)
1588 {
1589 	mdb_printf("Print syncq information for a given syncq pointer.\n"
1590 	    "\nWithout the address of a \"syncq_t\" structure given, print "
1591 	    "information about all\n"
1592 	    "syncqs in the \"syncq_cache\".\n\n"
1593 	    "Options:\n"
1594 	    "	-v:\t\tbe verbose - print symbolic flags falues\n"
1595 	    "	-q:\t\tbe quiet - print syncq pointer only\n"
1596 	    "	-f flag:\tprint only syncqs with flag set\n"
1597 	    "	-F flag:\tprint only syncqs with flag NOT set\n"
1598 	    "	-t type:\tprint only syncqs with specified type\n"
1599 	    "	-T type:\tprint only syncqs with do NOT have specified type\n\n"
1600 	    "Available conversions:\n"
1601 	    "	syncq2q:\tgiven a syncq addr print queue address of the\n"
1602 	    "\t\t\tenclosing queue, if it is part of a queue\n\n"
1603 	    "See also: \"::help queue\" and \"::help stdata\"\n");
1604 }
1605 
1606 void
1607 stdata_help(void)
1608 {
1609 	mdb_printf("Print stdata information for a given stdata pointer.\n"
1610 	    "\nWithout the address of a \"stdata_t\" structure given, print "
1611 	    "information about all\n"
1612 	    "stream head pointers from the \"stream_head_cache\".\n\n"
1613 	    "Fields printed:\n"
1614 	    "	ADDR:\tstream head address\n"
1615 	    "	WRQ:\twrite queue pointer\n"
1616 	    "	FLAGS:\tstream head flags (use -v to show in symbolic form)\n"
1617 	    "	VNODE:\tstream vnode pointer\n"
1618 	    "	N/A:\tpushcount and anchor positions\n"
1619 	    "	REF:\tstream head reference counter\n\n"
1620 	    "Options:\n"
1621 	    "	-v:\t\tbe verbose - print symbolic flags falues\n"
1622 	    "	-q:\t\tbe quiet - print stdata pointer only\n"
1623 	    "	-f flag:\tprint only stdatas with flag set\n"
1624 	    "	-F flag:\tprint only stdatas with flag NOT set\n\n"
1625 	    "Available conversions:\n"
1626 	    "	str2mate:\tgiven a stream head addr print its mate\n"
1627 	    "	str2wrq:\tgiven a stream head addr print its write queue\n\n"
1628 	    "See also: \"::help queue\" and \"::help syncq\"\n");
1629 }
1630 
1631 void
1632 mblk_help(void)
1633 {
1634 	mdb_printf("Print mblock information for a given mblk pointer.\n"
1635 	    "Without the address, print information about all mblocks.\n\n"
1636 	    "Fields printed:\n"
1637 	    "	ADDR:\tmblk address\n"
1638 	    "	FL:\tFlags\n"
1639 	    "	TYPE:\tType of corresponding dblock\n"
1640 	    "	LEN:\tData length as b_wptr - b_rptr\n"
1641 	    "	BLEN:\tDblock space as db_lim - db_base\n"
1642 	    "	RPTR:\tRead pointer\n"
1643 	    "	DBLK:\tDblock pointer\n\n"
1644 	    "Options:\n"
1645 	    "	-v:\t\tbe verbose - print symbolic flags falues\n"
1646 	    "	-q:\t\tbe quiet - print mblk pointer only\n"
1647 	    "	-d dbaddr:\t\tprint mblks with specified dblk address\n"
1648 	    "	-f flag:\tprint only mblks with flag set\n"
1649 	    "	-F flag:\tprint only mblks with flag NOT set\n"
1650 	    "	-t type:\tprint only mblks of specified db_type\n"
1651 	    "	-T type:\tprint only mblks other then the specified db_type\n"
1652 	    "	-l len:\t\ttprint only mblks with MBLKL == len\n"
1653 	    "	-L len:\t\tprint only mblks with MBLKL <= len \n"
1654 	    "	-G len:\t\tprint only mblks with MBLKL >= len \n"
1655 	    "	-b len:\t\tprint only mblks with db_lim - db_base == len\n"
1656 	    "\n");
1657 }
1658