10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6583Smeem * Common Development and Distribution License (the "License").
6*6583Smeem * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6583Smeem * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr3.2H */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Dump STREAMS module. Could be used anywhere on a stream to
300Sstevel@tonic-gate * print all message headers and data on to the console.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/stream.h>
370Sstevel@tonic-gate #include <sys/stropts.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/strsun.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include <sys/conf.h>
440Sstevel@tonic-gate #include <sys/modctl.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate static char hdr[100]; /* current message header */
470Sstevel@tonic-gate static char hdrpad[100]; /* pad of same length as hdr[] */
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * Raw buffer dumping routine. Displays the contents of the first message in
510Sstevel@tonic-gate * message chain `mp', using the "traditional" dump format.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * For instance, "Hello STREAMS, panicked lately?" would be displayed as:
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * RD 30001dbb240 M_DATA 48656C6C 6F205354 5245414D 532C2070 Hello STREAMS, p
560Sstevel@tonic-gate * 616E6963 6B656420 6C617465 6C793F anicked lately?
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * If the character being displayed is not printable, a '.' is shown.
590Sstevel@tonic-gate */
600Sstevel@tonic-gate
610Sstevel@tonic-gate #define DEDUMP_HEXPERBLK 4
620Sstevel@tonic-gate #define DEDUMP_HEXLEN (sizeof ("11223344") * 4)
630Sstevel@tonic-gate #define DEDUMP_ASCLEN (sizeof ("0123456789ABCDEF") - 1)
640Sstevel@tonic-gate
650Sstevel@tonic-gate static void
dedump_raw(mblk_t * mp)660Sstevel@tonic-gate dedump_raw(mblk_t *mp)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate char hex[DEDUMP_HEXLEN + 1], asc[DEDUMP_ASCLEN + 1];
690Sstevel@tonic-gate int hexi = 0, asci = 0, i = 0;
700Sstevel@tonic-gate uchar_t c;
710Sstevel@tonic-gate char *hdrp = hdr;
720Sstevel@tonic-gate
730Sstevel@tonic-gate hex[DEDUMP_HEXLEN] = '\0';
740Sstevel@tonic-gate
750Sstevel@tonic-gate for (;;) {
760Sstevel@tonic-gate if (i == MBLKL(mp) || (i != 0 && (i % DEDUMP_ASCLEN) == 0)) {
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * We're either out of data or we've filled a complete
790Sstevel@tonic-gate * line. In either case, print out what we've got --
800Sstevel@tonic-gate * but first NUL-terminate asc[] and pad out hex[]
810Sstevel@tonic-gate * with spaces.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate asc[asci] = '\0';
840Sstevel@tonic-gate (void) memset(hex + hexi, ' ', DEDUMP_HEXLEN - hexi);
850Sstevel@tonic-gate (void) printf("%s %s %s\n", hdrp, hex, asc);
860Sstevel@tonic-gate
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * If we're out of data, bail. Otherwise, reset asci
890Sstevel@tonic-gate * and hexi for another lap around. Also, set hdrp to
900Sstevel@tonic-gate * the pad since we only want to show the header once.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate if (i == MBLKL(mp))
930Sstevel@tonic-gate break;
940Sstevel@tonic-gate asci = 0;
950Sstevel@tonic-gate hexi = 0;
960Sstevel@tonic-gate hdrp = hdrpad;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate c = mp->b_rptr[i++];
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate hexi += snprintf(hex + hexi, 3, "%02X", c);
1020Sstevel@tonic-gate if ((i % DEDUMP_HEXPERBLK) == 0)
1030Sstevel@tonic-gate hex[hexi++] = ' ';
1040Sstevel@tonic-gate asc[asci++] = (c >= 32 && c <= 126) ? c : '.';
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static void
dedump_char(mblk_t * mp)1090Sstevel@tonic-gate dedump_char(mblk_t *mp)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate (void) printf("%s 0x%x\n", hdr, *(uchar_t *)mp->b_rptr);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static void
dedump_int(mblk_t * mp)1150Sstevel@tonic-gate dedump_int(mblk_t *mp)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate (void) printf("%s %d\n", hdr, *(int *)mp->b_rptr);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate static void
dedump_ssize(mblk_t * mp)1210Sstevel@tonic-gate dedump_ssize(mblk_t *mp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate (void) printf("%s %ld\n", hdr, *(ssize_t *)mp->b_rptr);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate static void
dedump_cmdblk(mblk_t * mp)127*6583Smeem dedump_cmdblk(mblk_t *mp)
128*6583Smeem {
129*6583Smeem struct cmdblk *cbp = (struct cmdblk *)mp->b_rptr;
130*6583Smeem
131*6583Smeem (void) printf("%s cmd %x cred %p len %u error %d\n", hdr, cbp->cb_cmd,
132*6583Smeem (void *)cbp->cb_cr, cbp->cb_len, cbp->cb_error);
133*6583Smeem }
134*6583Smeem
135*6583Smeem static void
dedump_iocblk(mblk_t * mp)1360Sstevel@tonic-gate dedump_iocblk(mblk_t *mp)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate struct iocblk *ic = (struct iocblk *)mp->b_rptr;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate (void) printf("%s cmd %x cred %p id %u flag %x count %ld rval %d "
1410Sstevel@tonic-gate "err %d\n", hdr, ic->ioc_cmd, (void *)ic->ioc_cr, ic->ioc_id,
1420Sstevel@tonic-gate ic->ioc_flag, ic->ioc_count, ic->ioc_rval, ic->ioc_error);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate static void
dedump_stroptions(mblk_t * mp)1460Sstevel@tonic-gate dedump_stroptions(mblk_t *mp)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate struct stroptions *so = (struct stroptions *)mp->b_rptr;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate (void) printf("%s flag %x readopt %d wroff %u\n", hdr,
1510Sstevel@tonic-gate so->so_flags, so->so_readopt, so->so_wroff);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate (void) printf("%s minpsz %ld maxpsz %ld hiwat %lu lowat %lu\n", hdrpad,
1540Sstevel@tonic-gate so->so_minpsz, so->so_maxpsz, so->so_hiwat, so->so_lowat);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate (void) printf("%s band %u erropt %u maxblk %ld copyopt %u\n", hdrpad,
1570Sstevel@tonic-gate so->so_band, so->so_erropt, so->so_maxblk, so->so_copyopt);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate static void
dedump_copyreq(mblk_t * mp)1610Sstevel@tonic-gate dedump_copyreq(mblk_t *mp)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate struct copyreq *cq = (struct copyreq *)mp->b_rptr;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate (void) printf("%s cmd %x cred %p id %u flag %x priv %p addr %p size "
1660Sstevel@tonic-gate "%lu\n", hdr, cq->cq_cmd, (void *)cq->cq_cr, cq->cq_id, cq->cq_flag,
1670Sstevel@tonic-gate (void *)cq->cq_private, (void *)cq->cq_addr, cq->cq_size);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate static void
dedump_copyresp(mblk_t * mp)1710Sstevel@tonic-gate dedump_copyresp(mblk_t *mp)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate struct copyresp *cp = (struct copyresp *)mp->b_rptr;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate (void) printf("%s cmd %x cred %p id %u flag %x priv %p rval %p\n", hdr,
1760Sstevel@tonic-gate cp->cp_cmd, (void *)cp->cp_cr, cp->cp_id, cp->cp_flag,
1770Sstevel@tonic-gate (void *)cp->cp_private, (void *)cp->cp_rval);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate typedef struct msgfmt {
1810Sstevel@tonic-gate uchar_t m_type;
1820Sstevel@tonic-gate char m_desc[15];
1830Sstevel@tonic-gate void (*m_print)(mblk_t *);
1840Sstevel@tonic-gate } msgfmt_t;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static msgfmt_t msgfmt[256] = {
1870Sstevel@tonic-gate { M_DATA, "M_DATA ", dedump_raw },
1880Sstevel@tonic-gate { M_PROTO, "M_PROTO ", dedump_raw },
1890Sstevel@tonic-gate { M_BREAK, "M_BREAK ", dedump_raw },
1900Sstevel@tonic-gate { M_PASSFP, "M_PASSFP ", dedump_raw },
1910Sstevel@tonic-gate { M_EVENT, "M_EVENT ", dedump_raw },
1920Sstevel@tonic-gate { M_SIG, "M_SIG ", dedump_char },
1930Sstevel@tonic-gate { M_DELAY, "M_DELAY ", dedump_int },
1940Sstevel@tonic-gate { M_CTL, "M_CTL ", dedump_raw },
1950Sstevel@tonic-gate { M_IOCTL, "M_IOCTL ", dedump_iocblk },
1960Sstevel@tonic-gate { M_SETOPTS, "M_SETOPTS ", dedump_stroptions },
1970Sstevel@tonic-gate { M_RSE, "M_RSE ", dedump_raw },
1980Sstevel@tonic-gate { M_IOCACK, "M_IOCACK ", dedump_iocblk },
1990Sstevel@tonic-gate { M_IOCNAK, "M_IOCNAK ", dedump_iocblk },
2000Sstevel@tonic-gate { M_PCPROTO, "M_PCPROTO ", dedump_raw },
2010Sstevel@tonic-gate { M_PCSIG, "M_PCSIG ", dedump_char },
2020Sstevel@tonic-gate { M_READ, "M_READ ", dedump_ssize },
2030Sstevel@tonic-gate { M_FLUSH, "M_FLUSH ", dedump_char },
2040Sstevel@tonic-gate { M_STOP, "M_STOP ", dedump_raw },
2050Sstevel@tonic-gate { M_START, "M_START ", dedump_raw },
2060Sstevel@tonic-gate { M_HANGUP, "M_HANGUP ", dedump_raw },
2070Sstevel@tonic-gate { M_ERROR, "M_ERROR ", dedump_char },
2080Sstevel@tonic-gate { M_COPYIN, "M_COPYIN ", dedump_copyreq },
2090Sstevel@tonic-gate { M_COPYOUT, "M_COPYOUT ", dedump_copyreq },
2100Sstevel@tonic-gate { M_IOCDATA, "M_IOCDATA ", dedump_copyresp },
2110Sstevel@tonic-gate { M_PCRSE, "M_PCRSE ", dedump_raw },
2120Sstevel@tonic-gate { M_STOPI, "M_STOPI ", dedump_raw },
2130Sstevel@tonic-gate { M_STARTI, "M_STARTI ", dedump_raw },
2140Sstevel@tonic-gate { M_PCEVENT, "M_PCEVENT ", dedump_raw },
2150Sstevel@tonic-gate { M_UNHANGUP, "M_UNHANGUP", dedump_raw },
216*6583Smeem { M_CMD, "M_CMD ", dedump_cmdblk },
2170Sstevel@tonic-gate };
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /*ARGSUSED1*/
2200Sstevel@tonic-gate static int
dedumpopen(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * crp)2210Sstevel@tonic-gate dedumpopen(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *crp)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate if (!sflag)
2240Sstevel@tonic-gate return (ENXIO);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if (q->q_ptr)
2270Sstevel@tonic-gate return (0); /* already attached */
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate qprocson(q);
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /*ARGSUSED1*/
2340Sstevel@tonic-gate static int
dedumpclose(queue_t * q,int flag,cred_t * crp)2350Sstevel@tonic-gate dedumpclose(queue_t *q, int flag, cred_t *crp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate qprocsoff(q);
2380Sstevel@tonic-gate return (0);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * Common put procedure for upstream and downstream.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate static int
dedumpput(queue_t * q,mblk_t * mp)2450Sstevel@tonic-gate dedumpput(queue_t *q, mblk_t *mp)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate unsigned char type = DB_TYPE(mp);
2480Sstevel@tonic-gate ssize_t hdrlen;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate hdrlen = snprintf(hdr, sizeof (hdr), "%s %p %10s ",
2510Sstevel@tonic-gate (q->q_flag & QREADR) ? "RD" : "WR", (void *)q, msgfmt[type].m_desc);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate hdrpad[hdrlen] = '\0';
2540Sstevel@tonic-gate msgfmt[type].m_print(mp);
2550Sstevel@tonic-gate hdrpad[hdrlen] = ' ';
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate putnext(q, mp);
2580Sstevel@tonic-gate return (0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate struct module_info dedump_minfo = {
2620Sstevel@tonic-gate 0xaaa, "dedump", 0, INFPSZ, 0, 0
2630Sstevel@tonic-gate };
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate struct qinit dedumprinit = {
2660Sstevel@tonic-gate dedumpput, NULL, dedumpopen, dedumpclose, NULL, &dedump_minfo, NULL
2670Sstevel@tonic-gate };
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate struct qinit dedumpwinit = {
2700Sstevel@tonic-gate dedumpput, NULL, NULL, NULL, NULL, &dedump_minfo, NULL
2710Sstevel@tonic-gate };
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate struct streamtab dedumpinfo = {
2740Sstevel@tonic-gate &dedumprinit, &dedumpwinit, NULL, NULL,
2750Sstevel@tonic-gate };
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static struct fmodsw fsw = {
2780Sstevel@tonic-gate "dedump",
2790Sstevel@tonic-gate &dedumpinfo,
2800Sstevel@tonic-gate D_MP | D_MTPERMOD /* just to serialize printfs */
2810Sstevel@tonic-gate };
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
2840Sstevel@tonic-gate &mod_strmodops, "dump streams module", &fsw
2850Sstevel@tonic-gate };
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate static struct modlinkage modlinkage = {
2880Sstevel@tonic-gate MODREV_1, &modlstrmod, NULL
2890Sstevel@tonic-gate };
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate int
_init(void)2920Sstevel@tonic-gate _init(void)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate int i;
2950Sstevel@tonic-gate msgfmt_t mf;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate * Sort msgfmt[] so that msgfmt[n] describes message type n.
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate for (i = 255; i != 0; i--) {
3010Sstevel@tonic-gate mf = msgfmt[i];
3020Sstevel@tonic-gate msgfmt[i].m_type = i;
3030Sstevel@tonic-gate (void) sprintf(msgfmt[i].m_desc, "M_BOGUS_0x%x", i);
3040Sstevel@tonic-gate msgfmt[i].m_print = dedump_raw;
3050Sstevel@tonic-gate if (mf.m_desc[0] != 0)
3060Sstevel@tonic-gate msgfmt[mf.m_type] = mf;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Fill hdrpad[] with as many spaces as will fit.
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate (void) memset(hdrpad, ' ', sizeof (hdrpad) - 1);
3130Sstevel@tonic-gate hdrpad[sizeof (hdrpad) - 1] = '\0';
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate return (mod_install(&modlinkage));
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate int
_fini(void)3190Sstevel@tonic-gate _fini(void)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate return (mod_remove(&modlinkage));
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3250Sstevel@tonic-gate _info(struct modinfo *modinfop)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
3280Sstevel@tonic-gate }
329