1*4c06356bSdh /*
2*4c06356bSdh  * CDDL HEADER START
3*4c06356bSdh  *
4*4c06356bSdh  * The contents of this file are subject to the terms of the
5*4c06356bSdh  * Common Development and Distribution License (the "License").
6*4c06356bSdh  * You may not use this file except in compliance with the License.
7*4c06356bSdh  *
8*4c06356bSdh  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4c06356bSdh  * or http://www.opensolaris.org/os/licensing.
10*4c06356bSdh  * See the License for the specific language governing permissions
11*4c06356bSdh  * and limitations under the License.
12*4c06356bSdh  *
13*4c06356bSdh  * When distributing Covered Code, include this CDDL HEADER in each
14*4c06356bSdh  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4c06356bSdh  * If applicable, add the following below this CDDL HEADER, with the
16*4c06356bSdh  * fields enclosed by brackets "[]" replaced with your own identifying
17*4c06356bSdh  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4c06356bSdh  *
19*4c06356bSdh  * CDDL HEADER END
20*4c06356bSdh  */
21*4c06356bSdh /*
22*4c06356bSdh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*4c06356bSdh  * Use is subject to license terms.
24*4c06356bSdh  */
25*4c06356bSdh 
26*4c06356bSdh #include <mdb/mdb_modapi.h>
27*4c06356bSdh #include <sys/bitset.h>
28*4c06356bSdh 
29*4c06356bSdh #include "bitset.h"		/* XXX work out ifdef in include file... */
30*4c06356bSdh 
31*4c06356bSdh void
bitset_help(void)32*4c06356bSdh bitset_help(void)
33*4c06356bSdh {
34*4c06356bSdh 	mdb_printf("Print the bitset at the address given\n");
35*4c06356bSdh }
36*4c06356bSdh 
37*4c06356bSdh static void
bitset_free(bitset_t * bs)38*4c06356bSdh bitset_free(bitset_t *bs)
39*4c06356bSdh {
40*4c06356bSdh 	if (bs == NULL)
41*4c06356bSdh 		return;
42*4c06356bSdh 	if (bs->bs_set && bs->bs_words)
43*4c06356bSdh 		mdb_free(bs->bs_set, bs->bs_words * sizeof (ulong_t));
44*4c06356bSdh 	mdb_free(bs, sizeof (*bs));
45*4c06356bSdh }
46*4c06356bSdh 
47*4c06356bSdh static bitset_t *
bitset_get(uintptr_t bsaddr)48*4c06356bSdh bitset_get(uintptr_t bsaddr)
49*4c06356bSdh {
50*4c06356bSdh 	bitset_t	*bs;
51*4c06356bSdh 
52*4c06356bSdh 	bs = mdb_zalloc(sizeof (*bs), UM_SLEEP);
53*4c06356bSdh 	if (mdb_vread(bs, sizeof (*bs), bsaddr) == -1) {
54*4c06356bSdh 		mdb_warn("couldn't read bitset 0x%p", bsaddr);
55*4c06356bSdh 		bitset_free(bs);
56*4c06356bSdh 		return (NULL);
57*4c06356bSdh 	}
58*4c06356bSdh 
59*4c06356bSdh 	bsaddr = (uintptr_t)bs->bs_set;
60*4c06356bSdh 	bs->bs_set = mdb_alloc(bs->bs_words * sizeof (ulong_t), UM_SLEEP);
61*4c06356bSdh 	if (mdb_vread(bs->bs_set,
62*4c06356bSdh 	    bs->bs_words * sizeof (ulong_t), bsaddr) == -1) {
63*4c06356bSdh 		mdb_warn("couldn't read bitset bs_set 0x%p", bsaddr);
64*4c06356bSdh 		bitset_free(bs);
65*4c06356bSdh 		return (NULL);
66*4c06356bSdh 	}
67*4c06356bSdh 	return (bs);
68*4c06356bSdh 
69*4c06356bSdh }
70*4c06356bSdh 
71*4c06356bSdh static int
bitset_highbit(bitset_t * bs)72*4c06356bSdh bitset_highbit(bitset_t *bs)
73*4c06356bSdh {
74*4c06356bSdh 	int	high;
75*4c06356bSdh 	int	i;
76*4c06356bSdh 
77*4c06356bSdh 	if ((bs->bs_set == NULL) || (bs->bs_words == 0))
78*4c06356bSdh 		return (-1);
79*4c06356bSdh 
80*4c06356bSdh 	/* move backwards through words */
81*4c06356bSdh 	for (i = bs->bs_words; i >= 0; i--)
82*4c06356bSdh 		if (bs->bs_set[i])
83*4c06356bSdh 			break;
84*4c06356bSdh 	if (i < 0)
85*4c06356bSdh 		return (-1);
86*4c06356bSdh 
87*4c06356bSdh 	/* move backwards through bits */
88*4c06356bSdh 	high = i << BT_ULSHIFT;
89*4c06356bSdh 	for (i = BT_NBIPUL - 1; i; i--)
90*4c06356bSdh 		if (BT_TEST(bs->bs_set, high + i))
91*4c06356bSdh 			break;
92*4c06356bSdh 	return (high + i + 1);
93*4c06356bSdh }
94*4c06356bSdh 
95*4c06356bSdh static int
pow10(int exp)96*4c06356bSdh pow10(int exp)
97*4c06356bSdh {
98*4c06356bSdh 	int	res;
99*4c06356bSdh 
100*4c06356bSdh 	for (res = 1; exp; exp--)
101*4c06356bSdh 		res *= 10;
102*4c06356bSdh 	return (res);
103*4c06356bSdh }
104*4c06356bSdh 
105*4c06356bSdh static int
log10(int val)106*4c06356bSdh log10(int val)
107*4c06356bSdh {
108*4c06356bSdh 	int	res = 0;
109*4c06356bSdh 
110*4c06356bSdh 	do {
111*4c06356bSdh 		res++;
112*4c06356bSdh 		val /= 10;
113*4c06356bSdh 	} while (val);
114*4c06356bSdh 	return (res);
115*4c06356bSdh }
116*4c06356bSdh 
117*4c06356bSdh /*
118*4c06356bSdh  * The following prints a bitset with a 'ruler' that look like this
119*4c06356bSdh  *
120*4c06356bSdh  *              11111111112222222222333333333344444444445555555555666666666677
121*4c06356bSdh  *    012345678901234567890123456789012345678901234567890123456789012345678901
122*4c06356bSdh  * xx:........................................................................
123*4c06356bSdh  *                                11111111111111111111111111111111111111111111
124*4c06356bSdh  *    777777778888888888999999999900000000001111111111222222222233333333334444
125*4c06356bSdh  *    234567890123456789012345678901234567890123456789012345678901234567890123
126*4c06356bSdh  *    ........................................................................
127*4c06356bSdh  *    111111111111111111111111111111111111111111111111111111112222222222222222
128*4c06356bSdh  *    444444555555555566666666667777777777888888888899999999990000000000111111
129*4c06356bSdh  *    456789012345678901234567890123456789012345678901234567890123456789012345
130*4c06356bSdh  *    ........................................................................
131*4c06356bSdh  *    2222222222
132*4c06356bSdh  *    1111222222
133*4c06356bSdh  *    6789012345
134*4c06356bSdh  *    ..........
135*4c06356bSdh  *
136*4c06356bSdh  * to identify individual bits that are set.
137*4c06356bSdh  */
138*4c06356bSdh static void
bitset_print(bitset_t * bs,char * label,int width)139*4c06356bSdh bitset_print(bitset_t *bs, char *label, int width)
140*4c06356bSdh {
141*4c06356bSdh 	int	val_start;
142*4c06356bSdh 	int	val_max;
143*4c06356bSdh 	int	label_width;
144*4c06356bSdh 	int	ruler_width;
145*4c06356bSdh 	int	v, vm, vi;
146*4c06356bSdh 	int	nl, l;
147*4c06356bSdh 	int	i;
148*4c06356bSdh 	int	p;
149*4c06356bSdh 	char	c;
150*4c06356bSdh 
151*4c06356bSdh 	val_start = 0;
152*4c06356bSdh 	val_max = bitset_highbit(bs) + 1;
153*4c06356bSdh 	if (val_max <= val_start) {
154*4c06356bSdh 		mdb_printf("%s: empty-set", label);
155*4c06356bSdh 		return;
156*4c06356bSdh 	}
157*4c06356bSdh 
158*4c06356bSdh 	label_width = strlen(label) + 1;
159*4c06356bSdh 	ruler_width = width - label_width;
160*4c06356bSdh 
161*4c06356bSdh 	for (v = val_start; v < val_max; v = vm) {
162*4c06356bSdh 		if ((v + ruler_width) < val_max)
163*4c06356bSdh 			vm = v + ruler_width;
164*4c06356bSdh 		else
165*4c06356bSdh 			vm = val_max;
166*4c06356bSdh 
167*4c06356bSdh 		nl = log10(vm) - 1;
168*4c06356bSdh 		for (l = nl; l >= 0; l--) {
169*4c06356bSdh 			p = pow10(l);
170*4c06356bSdh 			for (i = 0; i < label_width; i++)
171*4c06356bSdh 				mdb_printf(" ");
172*4c06356bSdh 
173*4c06356bSdh 			for (vi = v; vi < vm; vi++) {
174*4c06356bSdh 				c = '0' + ((vi / p) % 10);
175*4c06356bSdh 				if ((l == nl) && (c == '0'))
176*4c06356bSdh 					c = ' ';
177*4c06356bSdh 				mdb_printf("%c", c);
178*4c06356bSdh 			}
179*4c06356bSdh 
180*4c06356bSdh 			mdb_printf("\n");
181*4c06356bSdh 		}
182*4c06356bSdh 
183*4c06356bSdh 		if (v == val_start) {
184*4c06356bSdh 			mdb_printf("%s:", label);
185*4c06356bSdh 		} else {
186*4c06356bSdh 			for (i = 0; i < label_width; i++)
187*4c06356bSdh 				mdb_printf(" ");
188*4c06356bSdh 		}
189*4c06356bSdh 		for (vi = v; vi < vm; vi++) {
190*4c06356bSdh 			if (BT_TEST(bs->bs_set, vi))
191*4c06356bSdh 				mdb_printf("X");
192*4c06356bSdh 			else
193*4c06356bSdh 				mdb_printf(".");
194*4c06356bSdh 		}
195*4c06356bSdh 		mdb_printf("\n");
196*4c06356bSdh 	}
197*4c06356bSdh }
198*4c06356bSdh 
199*4c06356bSdh /*ARGSUSED*/
200*4c06356bSdh int
bitset(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)201*4c06356bSdh bitset(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
202*4c06356bSdh {
203*4c06356bSdh 	bitset_t	*bs;
204*4c06356bSdh 
205*4c06356bSdh 	bs = bitset_get(addr);
206*4c06356bSdh 	if (bs == NULL)
207*4c06356bSdh 		return (DCMD_ERR);
208*4c06356bSdh 
209*4c06356bSdh 	bitset_print(bs, "label", 80);
210*4c06356bSdh 	bitset_free(bs);
211*4c06356bSdh 	return (DCMD_OK);
212*4c06356bSdh }
213