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