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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* 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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <mdb/mdb_modapi.h>
#include <sys/sysmacros.h>
#include <sys/sunddi.h>
#include <sys/damap.h>
#include <sys/damap_impl.h>
#include "damap.h"
void
damap_help(void)
{
mdb_printf("Print the damap at the address given.\n");
mdb_printf("\n");
mdb_printf("EXAMPLE: SCSI: To display the SCSI tgtmap damaps ");
mdb_printf("associated with a scsi HBA driver iport dip:\n");
mdb_printf("\n");
mdb_printf("::devbindings -q <driver_name>\n");
mdb_printf("\n");
mdb_printf("<iport-dip>::print struct dev_info devi_driver_data|");
mdb_printf("::print scsi_hba_tran_t tran_tgtmap|");
mdb_printf("::print impl_scsi_tgtmap_t ");
mdb_printf("tgtmap_dam[0] tgtmap_dam[1]|::damap\n");
}
static char *
local_strdup(const char *s)
{
if (s)
return (strcpy(mdb_alloc(strlen(s) + 1, UM_SLEEP), s));
else
return (NULL);
}
static void
local_strfree(const char *s)
{
if (s)
mdb_free((void *)s, strlen(s) + 1);
}
static void
bitset_free(bitset_t *bs, int embedded)
{
if (bs == NULL)
return;
if (bs->bs_set && bs->bs_words)
mdb_free(bs->bs_set, bs->bs_words * sizeof (ulong_t));
if (!embedded)
mdb_free(bs, sizeof (*bs)); /* not embedded, free */
}
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, 0);
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, 0);
return (NULL);
}
return (bs);
}
static void
damap_free(struct dam *dam, void **kdamda, int kdamda_n)
{
int i;
struct i_ddi_soft_state *ss;
dam_da_t *da;
if (dam) {
/* free in dam_da_t softstate */
ss = (struct i_ddi_soft_state *)dam->dam_da;
if (ss) {
if (ss->n_items && ss->array) {
for (i = 0; i < ss->n_items; i++) {
da = ss->array[i];
if (da == NULL)
continue;
local_strfree(da->da_addr);
mdb_free(da, sizeof (*da));
}
}
mdb_free(ss, sizeof (*ss));
}
/* free dam_active/stable/report_set embedded in dam */
bitset_free(&dam->dam_report_set, 1);
bitset_free(&dam->dam_stable_set, 1);
bitset_free(&dam->dam_active_set, 1);
/* free dam_name */
local_strfree(dam->dam_name);
/* free dam */
mdb_free(dam, sizeof (*dam));
}
if (kdamda)
mdb_free(kdamda, kdamda_n * sizeof (void *));
}
/*
* The dam implementation uses a number of different abstractions. Given a
* pointer to a damap_t, this function make an mdb instantiation of the dam -
* many, but not all, of the different abstractions used in the dam
* implementation are also instantiated in mdb. This means that callers of
* damap_get can perform some (but not all) types of structure pointer
* traversals.
*/
struct dam *
damap_get(uintptr_t damaddr, void ***pkdamda, int *pkdamda_n)
{
/* variables that hold instantiation read from kernel */
struct dam kdam;
char kstring[MAXPATHLEN];
struct i_ddi_soft_state kss;
void **kssarray = NULL;
int array_sz = 0;
/* variables that hold mdb instantiation */
struct dam *dam = NULL;
struct i_ddi_soft_state *ss;
bitset_t *bs;
dam_da_t *da;
int i;
/* read kernel: dam */
if (mdb_vread(&kdam, sizeof (kdam), damaddr) == -1) {
mdb_warn("couldn't read dam 0x%p", damaddr);
goto err;
}
/* read kernel: dam->dam_name */
mdb_readstr(kstring, sizeof (kstring), (uintptr_t)kdam.dam_name);
/* read kernel: dam->dam_da (softstate) */
if (mdb_vread(&kss, sizeof (kss), (uintptr_t)kdam.dam_da) == -1) {
mdb_warn("couldn't read dam dam_da 0x%p",
(uintptr_t)kdam.dam_da);
goto err;
}
/* read kernel ((struct i_ddi_soft_state *)dam->dam_da)->array */
array_sz = kss.n_items * sizeof (void *);
kssarray = mdb_alloc(array_sz, UM_SLEEP);
if (mdb_vread(kssarray, array_sz, (uintptr_t)kss.array) == -1) {
mdb_warn("couldn't read dam dam_da array 0x%p",
(uintptr_t)kss.array);
goto err;
}
/*
* Produce mdb instantiation of kernel data structures.
*
* Structure copy kdam to dam, then clear out pointers in dam (some
* will be filled in by mdb instantiation code below).
*/
dam = mdb_zalloc(sizeof (*dam), UM_SLEEP);
*dam = kdam;
dam->dam_name = NULL;
dam->dam_active_set.bs_set = NULL;
dam->dam_stable_set.bs_set = NULL;
dam->dam_report_set.bs_set = NULL;
dam->dam_da = NULL;
/* dam_addr_hash, dam_taskqp, dam_kstatp left as kernel addresses */
/* fill in dam_name */
dam->dam_name = local_strdup(kstring);
/* fill in dam_active/stable/report_set embedded in the dam */
bs = bitset_get(damaddr + (offsetof(struct dam, dam_active_set)));
if (bs) {
dam->dam_active_set = *bs;
mdb_free(bs, sizeof (*bs));
}
bs = bitset_get(damaddr + (offsetof(struct dam, dam_stable_set)));
if (bs) {
dam->dam_stable_set = *bs;
mdb_free(bs, sizeof (*bs));
}
bs = bitset_get(damaddr + (offsetof(struct dam, dam_report_set)));
if (bs) {
dam->dam_report_set = *bs;
mdb_free(bs, sizeof (*bs));
}
/* fill in dam_da_t softstate */
ss = mdb_zalloc(sizeof (struct i_ddi_soft_state), UM_SLEEP);
*ss = kss;
ss->next = NULL;
ss->array = mdb_zalloc(array_sz, UM_SLEEP);
dam->dam_da = ss;
for (i = 0; i < kss.n_items; i++) {
if (kssarray[i] == NULL)
continue;
da = ss->array[i] = mdb_zalloc(sizeof (*da), UM_SLEEP);
if (mdb_vread(da, sizeof (*da), (uintptr_t)kssarray[i]) == -1) {
mdb_warn("couldn't read dam dam_da %d 0x%p", i,
(uintptr_t)kss.array);
goto err;
}
/* da_nvl, da_ppriv_rpt, da_nvl_rpt left as kernel addresses */
/* read kernel: da->da_addr */
mdb_readstr(kstring, sizeof (kstring), (uintptr_t)da->da_addr);
da->da_addr = local_strdup(kstring);
}
/* return array of kernel dam_da_t pointers associated with each id */
*pkdamda = kssarray;
*pkdamda_n = array_sz / sizeof (void *);
/* return pointer to mdb instantiation of the dam */
return (dam);
err: damap_free(dam, kssarray, array_sz / sizeof (void *));
*pkdamda = NULL;
*pkdamda_n = 0;
return (NULL);
}
/*ARGSUSED*/
static void
damap_print(struct dam *dam, void **kdamda, int kdamda_n)
{
struct i_ddi_soft_state *ss;
dam_da_t *da;
int i;
mdb_printf("%s:\n", dam->dam_name);
ss = (struct i_ddi_soft_state *)dam->dam_da;
if (ss == NULL)
return;
if ((ss->n_items == 0) || (ss->array == NULL))
return;
mdb_printf(" #: %-20s [ASR] ref config-private provider-private\n",
"address");
for (i = 0; i < ss->n_items; i++) {
da = ss->array[i];
if (da == NULL)
continue;
/* Print index and address. */
mdb_printf(" %3d: %-20s [", i, da->da_addr);
/* Print shorthand of Active/Stable/Report set membership */
if (BT_TEST(dam->dam_active_set.bs_set, i))
mdb_printf("A");
else
mdb_printf(".");
if (BT_TEST(dam->dam_stable_set.bs_set, i))
mdb_printf("S");
else
mdb_printf(".");
if (BT_TEST(dam->dam_report_set.bs_set, i))
mdb_printf("R");
else
mdb_printf(".");
/* Print the reference count and priv */
mdb_printf("] %-3d %0?lx %0?lx\n",
da->da_ref, da->da_cfg_priv, da->da_ppriv);
mdb_printf(" %p::print -ta dam_da_t\n", kdamda[i]);
}
}
/*ARGSUSED*/
int
damap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
struct dam *dam;
void **kdamda;
int kdamda_n;
if (!(flags & DCMD_ADDRSPEC)) {
return (DCMD_ERR);
}
dam = damap_get(addr, &kdamda, &kdamda_n);
if (dam == NULL)
return (DCMD_ERR);
damap_print(dam, kdamda, kdamda_n);
damap_free(dam, kdamda, kdamda_n);
return (DCMD_OK);
}
|