xref: /onnv-gate/usr/src/uts/common/io/dedump.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr3.2H 	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Dump STREAMS module.  Could be used anywhere on a stream to
31*0Sstevel@tonic-gate  * print all message headers and data on to the console.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/param.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate #include <sys/stream.h>
38*0Sstevel@tonic-gate #include <sys/stropts.h>
39*0Sstevel@tonic-gate #include <sys/errno.h>
40*0Sstevel@tonic-gate #include <sys/cmn_err.h>
41*0Sstevel@tonic-gate #include <sys/ddi.h>
42*0Sstevel@tonic-gate #include <sys/strsun.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <sys/conf.h>
45*0Sstevel@tonic-gate #include <sys/modctl.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static char	hdr[100];	/* current message header */
48*0Sstevel@tonic-gate static char  	hdrpad[100];	/* pad of same length as hdr[] */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Raw buffer dumping routine.  Displays the contents of the first message in
52*0Sstevel@tonic-gate  * message chain `mp', using the "traditional" dump format.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * For instance, "Hello STREAMS, panicked lately?" would be displayed as:
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * RD 30001dbb240 M_DATA 48656C6C 6F205354 5245414D 532C2070  Hello STREAMS, p
57*0Sstevel@tonic-gate  *                       616E6963 6B656420 6C617465 6C793F    anicked lately?
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * If the character being displayed is not printable, a '.' is shown.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	DEDUMP_HEXPERBLK	4
63*0Sstevel@tonic-gate #define	DEDUMP_HEXLEN		(sizeof ("11223344") * 4)
64*0Sstevel@tonic-gate #define	DEDUMP_ASCLEN		(sizeof ("0123456789ABCDEF") - 1)
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static void
67*0Sstevel@tonic-gate dedump_raw(mblk_t *mp)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	char	hex[DEDUMP_HEXLEN + 1], asc[DEDUMP_ASCLEN + 1];
70*0Sstevel@tonic-gate 	int	hexi = 0, asci = 0, i = 0;
71*0Sstevel@tonic-gate 	uchar_t	c;
72*0Sstevel@tonic-gate 	char	*hdrp = hdr;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	hex[DEDUMP_HEXLEN] = '\0';
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	for (;;) {
77*0Sstevel@tonic-gate 		if (i == MBLKL(mp) || (i != 0 && (i % DEDUMP_ASCLEN) == 0)) {
78*0Sstevel@tonic-gate 			/*
79*0Sstevel@tonic-gate 			 * We're either out of data or we've filled a complete
80*0Sstevel@tonic-gate 			 * line.  In either case, print out what we've got --
81*0Sstevel@tonic-gate 			 * but first NUL-terminate asc[] and pad out hex[]
82*0Sstevel@tonic-gate 			 * with spaces.
83*0Sstevel@tonic-gate 			 */
84*0Sstevel@tonic-gate 			asc[asci] = '\0';
85*0Sstevel@tonic-gate 			(void) memset(hex + hexi, ' ', DEDUMP_HEXLEN - hexi);
86*0Sstevel@tonic-gate 			(void) printf("%s %s %s\n", hdrp, hex, asc);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 			/*
89*0Sstevel@tonic-gate 			 * If we're out of data, bail.  Otherwise, reset asci
90*0Sstevel@tonic-gate 			 * and hexi for another lap around.  Also, set hdrp to
91*0Sstevel@tonic-gate 			 * the pad since we only want to show the header once.
92*0Sstevel@tonic-gate 			 */
93*0Sstevel@tonic-gate 			if (i == MBLKL(mp))
94*0Sstevel@tonic-gate 				break;
95*0Sstevel@tonic-gate 			asci = 0;
96*0Sstevel@tonic-gate 			hexi = 0;
97*0Sstevel@tonic-gate 			hdrp = hdrpad;
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		c = mp->b_rptr[i++];
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 		hexi += snprintf(hex + hexi, 3, "%02X", c);
103*0Sstevel@tonic-gate 		if ((i % DEDUMP_HEXPERBLK) == 0)
104*0Sstevel@tonic-gate 			hex[hexi++] = ' ';
105*0Sstevel@tonic-gate 		asc[asci++] = (c >= 32 && c <= 126) ? c : '.';
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static void
110*0Sstevel@tonic-gate dedump_char(mblk_t *mp)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	(void) printf("%s 0x%x\n", hdr, *(uchar_t *)mp->b_rptr);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static void
116*0Sstevel@tonic-gate dedump_int(mblk_t *mp)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	(void) printf("%s %d\n", hdr, *(int *)mp->b_rptr);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static void
122*0Sstevel@tonic-gate dedump_ssize(mblk_t *mp)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	(void) printf("%s %ld\n", hdr, *(ssize_t *)mp->b_rptr);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate static void
128*0Sstevel@tonic-gate dedump_iocblk(mblk_t *mp)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	struct iocblk *ic = (struct iocblk *)mp->b_rptr;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	(void) printf("%s cmd %x cred %p id %u flag %x count %ld rval %d "
133*0Sstevel@tonic-gate 	    "err %d\n", hdr, ic->ioc_cmd, (void *)ic->ioc_cr, ic->ioc_id,
134*0Sstevel@tonic-gate 	    ic->ioc_flag, ic->ioc_count, ic->ioc_rval, ic->ioc_error);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static void
138*0Sstevel@tonic-gate dedump_stroptions(mblk_t *mp)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	struct stroptions *so = (struct stroptions *)mp->b_rptr;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	(void) printf("%s flag %x readopt %d wroff %u\n", hdr,
143*0Sstevel@tonic-gate 	    so->so_flags, so->so_readopt, so->so_wroff);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	(void) printf("%s minpsz %ld maxpsz %ld hiwat %lu lowat %lu\n", hdrpad,
146*0Sstevel@tonic-gate 	    so->so_minpsz, so->so_maxpsz, so->so_hiwat, so->so_lowat);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	(void) printf("%s band %u erropt %u maxblk %ld copyopt %u\n", hdrpad,
149*0Sstevel@tonic-gate 	    so->so_band, so->so_erropt, so->so_maxblk, so->so_copyopt);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate static void
153*0Sstevel@tonic-gate dedump_copyreq(mblk_t *mp)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	struct copyreq *cq = (struct copyreq *)mp->b_rptr;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	(void) printf("%s cmd %x cred %p id %u flag %x priv %p addr %p size "
158*0Sstevel@tonic-gate 	    "%lu\n", hdr, cq->cq_cmd, (void *)cq->cq_cr, cq->cq_id, cq->cq_flag,
159*0Sstevel@tonic-gate 	    (void *)cq->cq_private, (void *)cq->cq_addr, cq->cq_size);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate static void
163*0Sstevel@tonic-gate dedump_copyresp(mblk_t *mp)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	struct copyresp *cp = (struct copyresp *)mp->b_rptr;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	(void) printf("%s cmd %x cred %p id %u flag %x priv %p rval %p\n", hdr,
168*0Sstevel@tonic-gate 	    cp->cp_cmd, (void *)cp->cp_cr, cp->cp_id, cp->cp_flag,
169*0Sstevel@tonic-gate 	    (void *)cp->cp_private, (void *)cp->cp_rval);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate typedef struct msgfmt {
173*0Sstevel@tonic-gate 	uchar_t	m_type;
174*0Sstevel@tonic-gate 	char	m_desc[15];
175*0Sstevel@tonic-gate 	void	(*m_print)(mblk_t *);
176*0Sstevel@tonic-gate } msgfmt_t;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate static msgfmt_t msgfmt[256] = {
179*0Sstevel@tonic-gate 	{	M_DATA,		"M_DATA    ", 	dedump_raw		},
180*0Sstevel@tonic-gate 	{	M_PROTO,	"M_PROTO   ", 	dedump_raw		},
181*0Sstevel@tonic-gate 	{	M_BREAK,	"M_BREAK   ", 	dedump_raw		},
182*0Sstevel@tonic-gate 	{	M_PASSFP,	"M_PASSFP  ", 	dedump_raw		},
183*0Sstevel@tonic-gate 	{	M_EVENT,	"M_EVENT   ", 	dedump_raw		},
184*0Sstevel@tonic-gate 	{	M_SIG,		"M_SIG     ", 	dedump_char		},
185*0Sstevel@tonic-gate 	{	M_DELAY,	"M_DELAY   ", 	dedump_int		},
186*0Sstevel@tonic-gate 	{	M_CTL,		"M_CTL     ", 	dedump_raw		},
187*0Sstevel@tonic-gate 	{	M_IOCTL,	"M_IOCTL   ", 	dedump_iocblk		},
188*0Sstevel@tonic-gate 	{	M_SETOPTS,	"M_SETOPTS ", 	dedump_stroptions	},
189*0Sstevel@tonic-gate 	{	M_RSE,		"M_RSE     ", 	dedump_raw		},
190*0Sstevel@tonic-gate 	{	M_IOCACK,	"M_IOCACK  ", 	dedump_iocblk		},
191*0Sstevel@tonic-gate 	{	M_IOCNAK,	"M_IOCNAK  ", 	dedump_iocblk		},
192*0Sstevel@tonic-gate 	{	M_PCPROTO,	"M_PCPROTO ", 	dedump_raw		},
193*0Sstevel@tonic-gate 	{	M_PCSIG,	"M_PCSIG   ", 	dedump_char		},
194*0Sstevel@tonic-gate 	{	M_READ,		"M_READ    ", 	dedump_ssize		},
195*0Sstevel@tonic-gate 	{	M_FLUSH,	"M_FLUSH   ", 	dedump_char		},
196*0Sstevel@tonic-gate 	{	M_STOP,		"M_STOP    ", 	dedump_raw		},
197*0Sstevel@tonic-gate 	{	M_START,	"M_START   ", 	dedump_raw		},
198*0Sstevel@tonic-gate 	{	M_HANGUP,	"M_HANGUP  ", 	dedump_raw		},
199*0Sstevel@tonic-gate 	{	M_ERROR,	"M_ERROR   ", 	dedump_char		},
200*0Sstevel@tonic-gate 	{	M_COPYIN,	"M_COPYIN  ", 	dedump_copyreq		},
201*0Sstevel@tonic-gate 	{	M_COPYOUT,	"M_COPYOUT ", 	dedump_copyreq		},
202*0Sstevel@tonic-gate 	{	M_IOCDATA,	"M_IOCDATA ", 	dedump_copyresp		},
203*0Sstevel@tonic-gate 	{	M_PCRSE,	"M_PCRSE   ", 	dedump_raw		},
204*0Sstevel@tonic-gate 	{	M_STOPI,	"M_STOPI   ", 	dedump_raw		},
205*0Sstevel@tonic-gate 	{	M_STARTI,	"M_STARTI  ", 	dedump_raw		},
206*0Sstevel@tonic-gate 	{	M_PCEVENT,	"M_PCEVENT ", 	dedump_raw		},
207*0Sstevel@tonic-gate 	{	M_UNHANGUP,	"M_UNHANGUP", 	dedump_raw		},
208*0Sstevel@tonic-gate };
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /*ARGSUSED1*/
211*0Sstevel@tonic-gate static int
212*0Sstevel@tonic-gate dedumpopen(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *crp)
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	if (!sflag)
215*0Sstevel@tonic-gate 		return (ENXIO);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (q->q_ptr)
218*0Sstevel@tonic-gate 		return (0);		/* already attached */
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	qprocson(q);
221*0Sstevel@tonic-gate 	return (0);
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /*ARGSUSED1*/
225*0Sstevel@tonic-gate static int
226*0Sstevel@tonic-gate dedumpclose(queue_t *q, int flag, cred_t *crp)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	qprocsoff(q);
229*0Sstevel@tonic-gate 	return (0);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate  * Common put procedure for upstream and downstream.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate static int
236*0Sstevel@tonic-gate dedumpput(queue_t *q, mblk_t *mp)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	unsigned char type = DB_TYPE(mp);
239*0Sstevel@tonic-gate 	ssize_t hdrlen;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	hdrlen = snprintf(hdr, sizeof (hdr), "%s %p %10s ",
242*0Sstevel@tonic-gate 	    (q->q_flag & QREADR) ? "RD" : "WR", (void *)q, msgfmt[type].m_desc);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	hdrpad[hdrlen] = '\0';
245*0Sstevel@tonic-gate 	msgfmt[type].m_print(mp);
246*0Sstevel@tonic-gate 	hdrpad[hdrlen] = ' ';
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	putnext(q, mp);
249*0Sstevel@tonic-gate 	return (0);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate struct module_info dedump_minfo = {
253*0Sstevel@tonic-gate 	0xaaa, "dedump", 0, INFPSZ, 0, 0
254*0Sstevel@tonic-gate };
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate struct qinit dedumprinit = {
257*0Sstevel@tonic-gate 	dedumpput, NULL, dedumpopen, dedumpclose, NULL, &dedump_minfo, NULL
258*0Sstevel@tonic-gate };
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate struct qinit dedumpwinit = {
261*0Sstevel@tonic-gate 	dedumpput, NULL, NULL, NULL, NULL, &dedump_minfo, NULL
262*0Sstevel@tonic-gate };
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate struct streamtab dedumpinfo = {
265*0Sstevel@tonic-gate 	&dedumprinit, &dedumpwinit, NULL, NULL,
266*0Sstevel@tonic-gate };
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate static struct fmodsw fsw = {
269*0Sstevel@tonic-gate 	"dedump",
270*0Sstevel@tonic-gate 	&dedumpinfo,
271*0Sstevel@tonic-gate 	D_MP | D_MTPERMOD	/* just to serialize printfs */
272*0Sstevel@tonic-gate };
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
275*0Sstevel@tonic-gate 	&mod_strmodops, "dump streams module", &fsw
276*0Sstevel@tonic-gate };
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
279*0Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
280*0Sstevel@tonic-gate };
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate int
283*0Sstevel@tonic-gate _init(void)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate 	int i;
286*0Sstevel@tonic-gate 	msgfmt_t mf;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	/*
289*0Sstevel@tonic-gate 	 * Sort msgfmt[] so that msgfmt[n] describes message type n.
290*0Sstevel@tonic-gate 	 */
291*0Sstevel@tonic-gate 	for (i = 255; i != 0; i--) {
292*0Sstevel@tonic-gate 		mf = msgfmt[i];
293*0Sstevel@tonic-gate 		msgfmt[i].m_type = i;
294*0Sstevel@tonic-gate 		(void) sprintf(msgfmt[i].m_desc, "M_BOGUS_0x%x", i);
295*0Sstevel@tonic-gate 		msgfmt[i].m_print = dedump_raw;
296*0Sstevel@tonic-gate 		if (mf.m_desc[0] != 0)
297*0Sstevel@tonic-gate 			msgfmt[mf.m_type] = mf;
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/*
301*0Sstevel@tonic-gate 	 * Fill hdrpad[] with as many spaces as will fit.
302*0Sstevel@tonic-gate 	 */
303*0Sstevel@tonic-gate 	(void) memset(hdrpad, ' ', sizeof (hdrpad) - 1);
304*0Sstevel@tonic-gate 	hdrpad[sizeof (hdrpad) - 1] = '\0';
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate int
310*0Sstevel@tonic-gate _fini(void)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate int
316*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
319*0Sstevel@tonic-gate }
320