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