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"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <string.h>
30*0Sstevel@tonic-gate #include <sys/modhash_impl.h>
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
33*0Sstevel@tonic-gate #include <mdb/mdb_ks.h>
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include "modhash.h"
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /* This is passed to the modent callback; allows caller to get context */
38*0Sstevel@tonic-gate typedef struct modent_step_data_s {
39*0Sstevel@tonic-gate struct mod_hash_entry msd_mhe; /* must be first */
40*0Sstevel@tonic-gate int msd_hash_index;
41*0Sstevel@tonic-gate int msd_position; /* entry position in chain */
42*0Sstevel@tonic-gate uintptr_t msd_first_addr; /* first address in chain */
43*0Sstevel@tonic-gate } modent_step_data_t;
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate /* Context for a walk over a modhash (variable length) */
46*0Sstevel@tonic-gate typedef struct hash_walk_s {
47*0Sstevel@tonic-gate modent_step_data_t hwalk_msd; /* current entry data */
48*0Sstevel@tonic-gate mod_hash_t hwalk_hash; /* always last (var. len) */
49*0Sstevel@tonic-gate } hash_walk_t;
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate /* Computes number of bytes to allocate for hash_walk_t structure. */
52*0Sstevel@tonic-gate #define HW_SIZE(n) (sizeof (modent_step_data_t) + MH_SIZE(n))
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate /* Used for decoding hash keys for display */
55*0Sstevel@tonic-gate typedef struct hash_type_entry_s {
56*0Sstevel@tonic-gate const char *hte_type; /* name of hash type for ::modent -t */
57*0Sstevel@tonic-gate const char *hte_comparator; /* name of comparator function */
58*0Sstevel@tonic-gate void (*hte_format)(const mod_hash_key_t, char *, size_t);
59*0Sstevel@tonic-gate } hash_type_entry_t;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate static void format_strhash(const mod_hash_key_t, char *, size_t);
62*0Sstevel@tonic-gate static void format_ptrhash(const mod_hash_key_t, char *, size_t);
63*0Sstevel@tonic-gate static void format_idhash(const mod_hash_key_t, char *, size_t);
64*0Sstevel@tonic-gate static void format_default(const mod_hash_key_t, char *, size_t);
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate static const hash_type_entry_t hte_table[] = {
67*0Sstevel@tonic-gate { "str", "mod_hash_strkey_cmp", format_strhash },
68*0Sstevel@tonic-gate { "ptr", "mod_hash_ptrkey_cmp", format_ptrhash },
69*0Sstevel@tonic-gate { "id", "mod_hash_idkey_cmp", format_idhash },
70*0Sstevel@tonic-gate { NULL, NULL, format_default }
71*0Sstevel@tonic-gate };
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate static int modent_print(uintptr_t, int, uint_t, const hash_type_entry_t *,
74*0Sstevel@tonic-gate boolean_t, uint_t, uint_t);
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate /* The information used during a walk */
77*0Sstevel@tonic-gate typedef struct mod_walk_data_s {
78*0Sstevel@tonic-gate const hash_type_entry_t *mwd_hte; /* pointer to entry type */
79*0Sstevel@tonic-gate int mwd_main_flags; /* ::modhash flags */
80*0Sstevel@tonic-gate int mwd_flags; /* DCMD_* flags for looping */
81*0Sstevel@tonic-gate uint_t mwd_opt_e; /* call-modent mode */
82*0Sstevel@tonic-gate uint_t mwd_opt_c; /* chain head only mode */
83*0Sstevel@tonic-gate uint_t mwd_opt_h; /* hash index output */
84*0Sstevel@tonic-gate boolean_t mwd_opt_k_set; /* key supplied */
85*0Sstevel@tonic-gate boolean_t mwd_opt_v_set; /* value supplied */
86*0Sstevel@tonic-gate uintptr_t mwd_opt_k; /* key */
87*0Sstevel@tonic-gate uintptr_t mwd_opt_v; /* value */
88*0Sstevel@tonic-gate int mwd_maxposn; /* len of longest chain - 1 */
89*0Sstevel@tonic-gate int mwd_maxidx; /* hash idx of longest chain */
90*0Sstevel@tonic-gate uintptr_t mwd_maxaddr; /* addr of 1st elem @ maxidx */
91*0Sstevel@tonic-gate uintptr_t mwd_idxtoprint; /* desired hash pos to print */
92*0Sstevel@tonic-gate uintptr_t mwd_addr; /* 1st elem addr @idxtoprint */
93*0Sstevel@tonic-gate } mod_walk_data_t;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate * Initialize a walk over all the modhashes in the system.
97*0Sstevel@tonic-gate */
98*0Sstevel@tonic-gate int
modhash_walk_init(mdb_walk_state_t * wsp)99*0Sstevel@tonic-gate modhash_walk_init(mdb_walk_state_t *wsp)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate mod_hash_t *mh_head;
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate if (mdb_readvar(&mh_head, "mh_head") == -1) {
104*0Sstevel@tonic-gate mdb_warn("failed to read mh_head");
105*0Sstevel@tonic-gate return (WALK_ERR);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)mh_head;
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate return (WALK_NEXT);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate * Step to the next modhash in the system.
114*0Sstevel@tonic-gate */
115*0Sstevel@tonic-gate int
modhash_walk_step(mdb_walk_state_t * wsp)116*0Sstevel@tonic-gate modhash_walk_step(mdb_walk_state_t *wsp)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate mod_hash_t mh;
119*0Sstevel@tonic-gate int status;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate if (wsp->walk_addr == NULL)
122*0Sstevel@tonic-gate return (WALK_DONE);
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate if (mdb_vread(&mh, sizeof (mh), wsp->walk_addr) == -1) {
125*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_t at %p", wsp->walk_addr);
126*0Sstevel@tonic-gate return (WALK_ERR);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate status = wsp->walk_callback(wsp->walk_addr, &mh, wsp->walk_cbdata);
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)mh.mh_next;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate return (status);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate * Initialize a walk over the entries in a given modhash.
138*0Sstevel@tonic-gate */
139*0Sstevel@tonic-gate int
modent_walk_init(mdb_walk_state_t * wsp)140*0Sstevel@tonic-gate modent_walk_init(mdb_walk_state_t *wsp)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate mod_hash_t mh;
143*0Sstevel@tonic-gate hash_walk_t *hwp;
144*0Sstevel@tonic-gate int retv;
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate if (wsp->walk_addr == NULL) {
147*0Sstevel@tonic-gate mdb_warn("mod_hash_t address required\n");
148*0Sstevel@tonic-gate return (WALK_ERR);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate if (mdb_vread(&mh, sizeof (mh), wsp->walk_addr) == -1) {
152*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_t at %p", wsp->walk_addr);
153*0Sstevel@tonic-gate return (WALK_ERR);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate if (mh.mh_nchains <= 1) {
157*0Sstevel@tonic-gate mdb_warn("impossible number of chains in mod_hash_t at %p",
158*0Sstevel@tonic-gate wsp->walk_addr);
159*0Sstevel@tonic-gate return (WALK_ERR);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate * If the user presents us with a garbage pointer, and thus the number
164*0Sstevel@tonic-gate * of chains is just absurd, we don't want to bail out of mdb. Fail to
165*0Sstevel@tonic-gate * walk instead.
166*0Sstevel@tonic-gate */
167*0Sstevel@tonic-gate hwp = mdb_alloc(HW_SIZE(mh.mh_nchains), UM_NOSLEEP);
168*0Sstevel@tonic-gate if (hwp == NULL) {
169*0Sstevel@tonic-gate mdb_warn("unable to allocate %#x bytes for mod_hash_t at %p",
170*0Sstevel@tonic-gate HW_SIZE(mh.mh_nchains), wsp->walk_addr);
171*0Sstevel@tonic-gate return (WALK_ERR);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate (void) memcpy(&hwp->hwalk_hash, &mh, sizeof (hwp->hwalk_hash));
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate retv = mdb_vread(hwp->hwalk_hash.mh_entries + 1,
177*0Sstevel@tonic-gate (mh.mh_nchains - 1) * sizeof (struct mod_hash_entry *),
178*0Sstevel@tonic-gate wsp->walk_addr + sizeof (mh));
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate if (retv == -1) {
181*0Sstevel@tonic-gate mdb_free(hwp, HW_SIZE(mh.mh_nchains));
182*0Sstevel@tonic-gate mdb_warn("failed to read %#x mod_hash_entry pointers at %p",
183*0Sstevel@tonic-gate mh.mh_nchains - 1, wsp->walk_addr + sizeof (mh));
184*0Sstevel@tonic-gate return (WALK_ERR);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate hwp->hwalk_msd.msd_hash_index = -1;
188*0Sstevel@tonic-gate hwp->hwalk_msd.msd_position = 0;
189*0Sstevel@tonic-gate hwp->hwalk_msd.msd_first_addr = NULL;
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate wsp->walk_addr = NULL;
192*0Sstevel@tonic-gate wsp->walk_data = hwp;
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate return (WALK_NEXT);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /*
198*0Sstevel@tonic-gate * Step to the next entry in the modhash.
199*0Sstevel@tonic-gate */
200*0Sstevel@tonic-gate int
modent_walk_step(mdb_walk_state_t * wsp)201*0Sstevel@tonic-gate modent_walk_step(mdb_walk_state_t *wsp)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate hash_walk_t *hwp = wsp->walk_data;
204*0Sstevel@tonic-gate int status;
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate while (wsp->walk_addr == NULL) {
207*0Sstevel@tonic-gate hwp->hwalk_msd.msd_position = 0;
208*0Sstevel@tonic-gate if (++hwp->hwalk_msd.msd_hash_index >=
209*0Sstevel@tonic-gate hwp->hwalk_hash.mh_nchains)
210*0Sstevel@tonic-gate return (WALK_DONE);
211*0Sstevel@tonic-gate wsp->walk_addr = hwp->hwalk_msd.msd_first_addr =
212*0Sstevel@tonic-gate (uintptr_t)hwp->hwalk_hash.mh_entries[
213*0Sstevel@tonic-gate hwp->hwalk_msd.msd_hash_index];
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate if (mdb_vread(&hwp->hwalk_msd.msd_mhe, sizeof (hwp->hwalk_msd.msd_mhe),
217*0Sstevel@tonic-gate wsp->walk_addr) == -1) {
218*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_entry at %p",
219*0Sstevel@tonic-gate wsp->walk_addr);
220*0Sstevel@tonic-gate return (WALK_ERR);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate status = wsp->walk_callback(wsp->walk_addr, &hwp->hwalk_msd,
224*0Sstevel@tonic-gate wsp->walk_cbdata);
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate hwp->hwalk_msd.msd_position++;
227*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)hwp->hwalk_msd.msd_mhe.mhe_next;
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate return (status);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate * Clean up after walking the entries in a modhash.
234*0Sstevel@tonic-gate */
235*0Sstevel@tonic-gate void
modent_walk_fini(mdb_walk_state_t * wsp)236*0Sstevel@tonic-gate modent_walk_fini(mdb_walk_state_t *wsp)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate hash_walk_t *hwp = wsp->walk_data;
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate mdb_free(hwp, HW_SIZE(hwp->hwalk_hash.mh_nchains));
241*0Sstevel@tonic-gate wsp->walk_data = NULL;
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate * Step to next entry on a hash chain.
246*0Sstevel@tonic-gate */
247*0Sstevel@tonic-gate int
modchain_walk_step(mdb_walk_state_t * wsp)248*0Sstevel@tonic-gate modchain_walk_step(mdb_walk_state_t *wsp)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate struct mod_hash_entry mhe;
251*0Sstevel@tonic-gate int status;
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if (wsp->walk_addr == NULL)
254*0Sstevel@tonic-gate return (WALK_DONE);
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate if (mdb_vread(&mhe, sizeof (mhe), wsp->walk_addr) == -1) {
257*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_entry at %p",
258*0Sstevel@tonic-gate wsp->walk_addr);
259*0Sstevel@tonic-gate return (WALK_ERR);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate status = wsp->walk_callback(wsp->walk_addr, &mhe, wsp->walk_cbdata);
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)mhe.mhe_next;
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate return (status);
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate /*
270*0Sstevel@tonic-gate * This is called by ::modhash (via a callback) when gathering data about the
271*0Sstevel@tonic-gate * entries in a given modhash. It keeps track of the longest chain, finds a
272*0Sstevel@tonic-gate * specific entry (if the user requested one) and prints out a summary of the
273*0Sstevel@tonic-gate * entry or entries.
274*0Sstevel@tonic-gate */
275*0Sstevel@tonic-gate static int
modent_format(uintptr_t addr,const void * data,void * private)276*0Sstevel@tonic-gate modent_format(uintptr_t addr, const void *data, void *private)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate const modent_step_data_t *msd = data;
279*0Sstevel@tonic-gate mod_walk_data_t *mwd = private;
280*0Sstevel@tonic-gate int retv = DCMD_OK;
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate /* If this chain is longest seen, then save start of chain */
283*0Sstevel@tonic-gate if (msd->msd_position > mwd->mwd_maxposn) {
284*0Sstevel@tonic-gate mwd->mwd_maxposn = msd->msd_position;
285*0Sstevel@tonic-gate mwd->mwd_maxidx = msd->msd_hash_index;
286*0Sstevel@tonic-gate mwd->mwd_maxaddr = msd->msd_first_addr;
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate /* If the user specified a particular chain, then ignore others */
290*0Sstevel@tonic-gate if (mwd->mwd_idxtoprint != (uintptr_t)-1) {
291*0Sstevel@tonic-gate /* Save address of *first* entry */
292*0Sstevel@tonic-gate if (mwd->mwd_idxtoprint == msd->msd_hash_index)
293*0Sstevel@tonic-gate mwd->mwd_addr = msd->msd_first_addr;
294*0Sstevel@tonic-gate else
295*0Sstevel@tonic-gate return (retv);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate /* If the user specified a particular key, ignore others. */
299*0Sstevel@tonic-gate if (mwd->mwd_opt_k_set &&
300*0Sstevel@tonic-gate (uintptr_t)msd->msd_mhe.mhe_key != mwd->mwd_opt_k)
301*0Sstevel@tonic-gate return (retv);
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate /* If the user specified a particular value, ignore others. */
304*0Sstevel@tonic-gate if (mwd->mwd_opt_v_set &&
305*0Sstevel@tonic-gate (uintptr_t)msd->msd_mhe.mhe_val != mwd->mwd_opt_v)
306*0Sstevel@tonic-gate return (retv);
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate /* If the user just wants the chain heads, skip intermediate nodes. */
309*0Sstevel@tonic-gate if (mwd->mwd_opt_c && msd->msd_position != 0)
310*0Sstevel@tonic-gate return (retv);
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate /* If the user asked to have the entries printed, then do that. */
313*0Sstevel@tonic-gate if (mwd->mwd_opt_e) {
314*0Sstevel@tonic-gate /* If the output is to a pipeline, just print addresses */
315*0Sstevel@tonic-gate if (mwd->mwd_main_flags & DCMD_PIPE_OUT)
316*0Sstevel@tonic-gate mdb_printf("%p\n", addr);
317*0Sstevel@tonic-gate else
318*0Sstevel@tonic-gate retv = modent_print(addr, msd->msd_hash_index,
319*0Sstevel@tonic-gate mwd->mwd_flags, mwd->mwd_hte, mwd->mwd_opt_h, 0, 0);
320*0Sstevel@tonic-gate mwd->mwd_flags &= ~DCMD_LOOPFIRST;
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate return (retv);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate void
modhash_help(void)326*0Sstevel@tonic-gate modhash_help(void)
327*0Sstevel@tonic-gate {
328*0Sstevel@tonic-gate mdb_printf("Prints information about one or all mod_hash_t databases "
329*0Sstevel@tonic-gate "in the system.\n"
330*0Sstevel@tonic-gate "This command has three basic forms, summarized below.\n\n"
331*0Sstevel@tonic-gate " ::modhash [-t]\n <addr>::modhash\n"
332*0Sstevel@tonic-gate " <addr>::modhash -e [-ch] [-k key] [-v val] [-i index]\n\n"
333*0Sstevel@tonic-gate "In the first form, no address is provided, and a summary of all "
334*0Sstevel@tonic-gate "registered\n"
335*0Sstevel@tonic-gate "hashes in the system is printed; adding the '-t' option shows"
336*0Sstevel@tonic-gate " the hash\n"
337*0Sstevel@tonic-gate "type instead of the limits. In the second form, the address of a"
338*0Sstevel@tonic-gate " mod_hash_t\n"
339*0Sstevel@tonic-gate "is provided, and the output is in a verbose format. The final "
340*0Sstevel@tonic-gate "form prints\n"
341*0Sstevel@tonic-gate "the elements of the hash, optionally selecting just those with a "
342*0Sstevel@tonic-gate "particular\n"
343*0Sstevel@tonic-gate "key, value, and/or hash index, or just the chain heads (-c). "
344*0Sstevel@tonic-gate "The -h option\n"
345*0Sstevel@tonic-gate "shows hash indices instead of addresses.\n");
346*0Sstevel@tonic-gate }
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate int
modhash(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)349*0Sstevel@tonic-gate modhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
350*0Sstevel@tonic-gate {
351*0Sstevel@tonic-gate mod_hash_t mh;
352*0Sstevel@tonic-gate char name[256];
353*0Sstevel@tonic-gate int len;
354*0Sstevel@tonic-gate mod_walk_data_t mwd;
355*0Sstevel@tonic-gate uint_t opt_s = FALSE;
356*0Sstevel@tonic-gate uint_t opt_t = FALSE;
357*0Sstevel@tonic-gate char kfunc[MDB_SYM_NAMLEN];
358*0Sstevel@tonic-gate const hash_type_entry_t *htep;
359*0Sstevel@tonic-gate boolean_t elem_flags;
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate (void) memset(&mwd, 0, sizeof (mwd));
362*0Sstevel@tonic-gate mwd.mwd_main_flags = flags;
363*0Sstevel@tonic-gate mwd.mwd_flags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
364*0Sstevel@tonic-gate mwd.mwd_maxposn = -1;
365*0Sstevel@tonic-gate mwd.mwd_idxtoprint = (uintptr_t)-1;
366*0Sstevel@tonic-gate
367*0Sstevel@tonic-gate len = mdb_getopts(argc, argv,
368*0Sstevel@tonic-gate 's', MDB_OPT_SETBITS, TRUE, &opt_s,
369*0Sstevel@tonic-gate 't', MDB_OPT_SETBITS, TRUE, &opt_t,
370*0Sstevel@tonic-gate 'c', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_c,
371*0Sstevel@tonic-gate 'e', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_e,
372*0Sstevel@tonic-gate 'h', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_h,
373*0Sstevel@tonic-gate 'i', MDB_OPT_UINTPTR, &mwd.mwd_idxtoprint,
374*0Sstevel@tonic-gate 'k', MDB_OPT_UINTPTR_SET, &mwd.mwd_opt_k_set, &mwd.mwd_opt_k,
375*0Sstevel@tonic-gate 'v', MDB_OPT_UINTPTR_SET, &mwd.mwd_opt_v_set, &mwd.mwd_opt_v,
376*0Sstevel@tonic-gate NULL);
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate if (len < argc) {
379*0Sstevel@tonic-gate argv += len;
380*0Sstevel@tonic-gate if (argv->a_type == MDB_TYPE_STRING)
381*0Sstevel@tonic-gate mdb_warn("unexpected argument: %s\n",
382*0Sstevel@tonic-gate argv->a_un.a_str);
383*0Sstevel@tonic-gate else
384*0Sstevel@tonic-gate mdb_warn("unexpected argument(s)\n");
385*0Sstevel@tonic-gate return (DCMD_USAGE);
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate /* true if any element-related flags are set */
389*0Sstevel@tonic-gate elem_flags = mwd.mwd_opt_c || mwd.mwd_opt_e || mwd.mwd_opt_h ||
390*0Sstevel@tonic-gate mwd.mwd_opt_k_set || mwd.mwd_opt_v_set ||
391*0Sstevel@tonic-gate mwd.mwd_idxtoprint != (uintptr_t)-1;
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
394*0Sstevel@tonic-gate mdb_arg_t new_argv[1];
395*0Sstevel@tonic-gate
396*0Sstevel@tonic-gate if (elem_flags) {
397*0Sstevel@tonic-gate /*
398*0Sstevel@tonic-gate * This isn't allowed so that the output doesn't become
399*0Sstevel@tonic-gate * a confusing mix of hash table descriptions and
400*0Sstevel@tonic-gate * element entries.
401*0Sstevel@tonic-gate */
402*0Sstevel@tonic-gate mdb_warn("printing elements from all hashes is not "
403*0Sstevel@tonic-gate "permitted\n");
404*0Sstevel@tonic-gate return (DCMD_USAGE);
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate /* we force short mode here, no matter what he says */
407*0Sstevel@tonic-gate new_argv[0].a_type = MDB_TYPE_STRING;
408*0Sstevel@tonic-gate new_argv[0].a_un.a_str = opt_t ? "-st" : "-s";
409*0Sstevel@tonic-gate if (mdb_walk_dcmd("modhash", "modhash", 1, new_argv) == -1) {
410*0Sstevel@tonic-gate mdb_warn("can't walk mod_hash structures");
411*0Sstevel@tonic-gate return (DCMD_ERR);
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate return (DCMD_OK);
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate if (mwd.mwd_opt_e) {
417*0Sstevel@tonic-gate if (opt_s | opt_t) {
418*0Sstevel@tonic-gate mdb_warn("hash summary options not permitted when "
419*0Sstevel@tonic-gate "displaying elements\n");
420*0Sstevel@tonic-gate return (DCMD_USAGE);
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate } else {
423*0Sstevel@tonic-gate if (elem_flags) {
424*0Sstevel@tonic-gate /*
425*0Sstevel@tonic-gate * This isn't allowed so that the output doesn't become
426*0Sstevel@tonic-gate * a confusing mix of hash table description and
427*0Sstevel@tonic-gate * element entries.
428*0Sstevel@tonic-gate */
429*0Sstevel@tonic-gate mdb_warn("printing elements requires -e\n");
430*0Sstevel@tonic-gate return (DCMD_USAGE);
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate if (mdb_vread(&mh, sizeof (mh), addr) == -1) {
435*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_t at %p", addr);
436*0Sstevel@tonic-gate return (DCMD_ERR);
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate if (mwd.mwd_idxtoprint != (uintptr_t)-1 &&
440*0Sstevel@tonic-gate mwd.mwd_idxtoprint >= mh.mh_nchains) {
441*0Sstevel@tonic-gate mdb_warn("mod_hash chain index %x out of range 0..%x\n",
442*0Sstevel@tonic-gate mwd.mwd_idxtoprint, mh.mh_nchains - 1);
443*0Sstevel@tonic-gate return (DCMD_ERR);
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gate if (DCMD_HDRSPEC(flags) && opt_s) {
447*0Sstevel@tonic-gate if (opt_t != 0) {
448*0Sstevel@tonic-gate mdb_printf("%<u>%?s %6s %5s %?s %s%</u>\n",
449*0Sstevel@tonic-gate "ADDR", "CHAINS", "ELEMS", "TYPE", "NAME");
450*0Sstevel@tonic-gate } else {
451*0Sstevel@tonic-gate mdb_printf("%<u>%?s %6s %5s %6s %6s %s%</u>\n",
452*0Sstevel@tonic-gate "ADDR", "CHAINS", "ELEMS", "MAXLEN", "MAXIDX",
453*0Sstevel@tonic-gate "NAME");
454*0Sstevel@tonic-gate }
455*0Sstevel@tonic-gate }
456*0Sstevel@tonic-gate
457*0Sstevel@tonic-gate len = mdb_readstr(name, sizeof (name), (uintptr_t)mh.mh_name);
458*0Sstevel@tonic-gate if (len < 0)
459*0Sstevel@tonic-gate (void) strcpy(name, "??");
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gate if (mdb_lookup_by_addr((uintptr_t)mh.mh_keycmp, MDB_SYM_EXACT, kfunc,
462*0Sstevel@tonic-gate sizeof (kfunc), NULL) == -1)
463*0Sstevel@tonic-gate kfunc[0] = '\0';
464*0Sstevel@tonic-gate for (htep = hte_table; htep->hte_type != NULL; htep++)
465*0Sstevel@tonic-gate if (strcmp(kfunc, htep->hte_comparator) == 0)
466*0Sstevel@tonic-gate break;
467*0Sstevel@tonic-gate mwd.mwd_hte = htep;
468*0Sstevel@tonic-gate
469*0Sstevel@tonic-gate if (!mwd.mwd_opt_e && !opt_s) {
470*0Sstevel@tonic-gate mdb_printf("mod_hash_t %?p %s%s:\n", addr, name,
471*0Sstevel@tonic-gate len == sizeof (name) ? "..." : "");
472*0Sstevel@tonic-gate mdb_printf("\tKey comparator: %?p %s\n",
473*0Sstevel@tonic-gate mh.mh_keycmp, kfunc);
474*0Sstevel@tonic-gate mdb_printf("\tType: %s\n",
475*0Sstevel@tonic-gate htep->hte_type == NULL ? "unknown" : htep->hte_type);
476*0Sstevel@tonic-gate mdb_printf("\tSleep flag = %s, alloc failed = %#x\n",
477*0Sstevel@tonic-gate mh.mh_sleep ? "true" : "false",
478*0Sstevel@tonic-gate mh.mh_stat.mhs_nomem);
479*0Sstevel@tonic-gate mdb_printf("\tNumber of chains = %#x, elements = %#x\n",
480*0Sstevel@tonic-gate mh.mh_nchains, mh.mh_stat.mhs_nelems);
481*0Sstevel@tonic-gate mdb_printf("\tHits = %#x, misses = %#x, dups = %#x\n",
482*0Sstevel@tonic-gate mh.mh_stat.mhs_hit, mh.mh_stat.mhs_miss,
483*0Sstevel@tonic-gate mh.mh_stat.mhs_coll);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate if (mdb_pwalk("modent", modent_format, &mwd, addr) == -1) {
486*0Sstevel@tonic-gate mdb_warn("can't walk mod_hash entries");
487*0Sstevel@tonic-gate return (DCMD_ERR);
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate if (opt_s) {
490*0Sstevel@tonic-gate const char *tname;
491*0Sstevel@tonic-gate char tbuf[64];
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gate if (htep->hte_type == NULL) {
494*0Sstevel@tonic-gate (void) mdb_snprintf(tbuf, sizeof (tbuf), "%p",
495*0Sstevel@tonic-gate mh.mh_keycmp);
496*0Sstevel@tonic-gate tname = tbuf;
497*0Sstevel@tonic-gate } else {
498*0Sstevel@tonic-gate tname = htep->hte_type;
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate mdb_printf("%?p %6x %5x ", addr, mh.mh_nchains,
501*0Sstevel@tonic-gate mh.mh_stat.mhs_nelems);
502*0Sstevel@tonic-gate if (opt_t != 0) {
503*0Sstevel@tonic-gate mdb_printf("%?s", tname);
504*0Sstevel@tonic-gate } else {
505*0Sstevel@tonic-gate mdb_printf("%6x %6x", mwd.mwd_maxposn + 1,
506*0Sstevel@tonic-gate mwd.mwd_maxidx);
507*0Sstevel@tonic-gate }
508*0Sstevel@tonic-gate mdb_printf(" %s%s\n", name, len == sizeof (name) ? "..." : "");
509*0Sstevel@tonic-gate } else if (!mwd.mwd_opt_e) {
510*0Sstevel@tonic-gate mdb_printf("\tMaximum chain length = %x (at index %x, first "
511*0Sstevel@tonic-gate "entry %p)\n", mwd.mwd_maxposn + 1, mwd.mwd_maxidx,
512*0Sstevel@tonic-gate mwd.mwd_maxaddr);
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate return (DCMD_OK);
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate
517*0Sstevel@tonic-gate static void
format_strhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)518*0Sstevel@tonic-gate format_strhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate int len;
521*0Sstevel@tonic-gate
522*0Sstevel@tonic-gate (void) mdb_snprintf(keystr, keystrlen, "%?p ", key);
523*0Sstevel@tonic-gate len = strlen(keystr);
524*0Sstevel@tonic-gate (void) mdb_readstr(keystr + len, keystrlen - len, (uintptr_t)key);
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate static void
format_ptrhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)528*0Sstevel@tonic-gate format_ptrhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate int len;
531*0Sstevel@tonic-gate
532*0Sstevel@tonic-gate (void) mdb_snprintf(keystr, keystrlen, "%?p ", key);
533*0Sstevel@tonic-gate len = strlen(keystr);
534*0Sstevel@tonic-gate (void) mdb_lookup_by_addr((uintptr_t)key, MDB_SYM_EXACT, keystr + len,
535*0Sstevel@tonic-gate keystrlen - len, NULL);
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate
538*0Sstevel@tonic-gate static void
format_idhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)539*0Sstevel@tonic-gate format_idhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
540*0Sstevel@tonic-gate {
541*0Sstevel@tonic-gate (void) mdb_snprintf(keystr, keystrlen, "%?x", (uint_t)(uintptr_t)key);
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate static void
format_default(const mod_hash_key_t key,char * keystr,size_t keystrlen)545*0Sstevel@tonic-gate format_default(const mod_hash_key_t key, char *keystr, size_t keystrlen)
546*0Sstevel@tonic-gate {
547*0Sstevel@tonic-gate (void) mdb_snprintf(keystr, keystrlen, "%?p", key);
548*0Sstevel@tonic-gate }
549*0Sstevel@tonic-gate
550*0Sstevel@tonic-gate void
modent_help(void)551*0Sstevel@tonic-gate modent_help(void)
552*0Sstevel@tonic-gate {
553*0Sstevel@tonic-gate mdb_printf("Options are mutually exclusive:\n"
554*0Sstevel@tonic-gate " -t <type> print key in symbolic form; <type> is one of str, "
555*0Sstevel@tonic-gate "ptr, or id\n"
556*0Sstevel@tonic-gate " -v print value pointer alone\n"
557*0Sstevel@tonic-gate " -k print key pointer alone\n");
558*0Sstevel@tonic-gate }
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate static int
modent_print(uintptr_t addr,int hidx,uint_t flags,const hash_type_entry_t * htep,boolean_t prtidx,uint_t opt_k,uint_t opt_v)561*0Sstevel@tonic-gate modent_print(uintptr_t addr, int hidx, uint_t flags,
562*0Sstevel@tonic-gate const hash_type_entry_t *htep, boolean_t prtidx, uint_t opt_k,
563*0Sstevel@tonic-gate uint_t opt_v)
564*0Sstevel@tonic-gate {
565*0Sstevel@tonic-gate char keystr[256];
566*0Sstevel@tonic-gate struct mod_hash_entry mhe;
567*0Sstevel@tonic-gate
568*0Sstevel@tonic-gate if (DCMD_HDRSPEC(flags) && opt_k == 0 && opt_v == 0) {
569*0Sstevel@tonic-gate mdb_printf("%<u>%?s %?s %?s%</u>\n",
570*0Sstevel@tonic-gate prtidx ? "HASH_IDX" : "ADDR", "VAL", "KEY");
571*0Sstevel@tonic-gate }
572*0Sstevel@tonic-gate
573*0Sstevel@tonic-gate if (mdb_vread(&mhe, sizeof (mhe), addr) == -1) {
574*0Sstevel@tonic-gate mdb_warn("failed to read mod_hash_entry at %p", addr);
575*0Sstevel@tonic-gate return (DCMD_ERR);
576*0Sstevel@tonic-gate }
577*0Sstevel@tonic-gate
578*0Sstevel@tonic-gate if (opt_k) {
579*0Sstevel@tonic-gate mdb_printf("%p\n", mhe.mhe_key);
580*0Sstevel@tonic-gate } else if (opt_v) {
581*0Sstevel@tonic-gate mdb_printf("%p\n", mhe.mhe_val);
582*0Sstevel@tonic-gate } else {
583*0Sstevel@tonic-gate htep->hte_format(mhe.mhe_key, keystr, sizeof (keystr));
584*0Sstevel@tonic-gate if (prtidx)
585*0Sstevel@tonic-gate mdb_printf("%?x", hidx);
586*0Sstevel@tonic-gate else
587*0Sstevel@tonic-gate mdb_printf("%?p", addr);
588*0Sstevel@tonic-gate mdb_printf(" %?p %s\n", mhe.mhe_val, keystr);
589*0Sstevel@tonic-gate }
590*0Sstevel@tonic-gate
591*0Sstevel@tonic-gate return (DCMD_OK);
592*0Sstevel@tonic-gate }
593*0Sstevel@tonic-gate
594*0Sstevel@tonic-gate /*
595*0Sstevel@tonic-gate * This prints out a single mod_hash element, showing its value and its key.
596*0Sstevel@tonic-gate * The key is decoded based on the type of hash keys in use.
597*0Sstevel@tonic-gate */
598*0Sstevel@tonic-gate int
modent(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)599*0Sstevel@tonic-gate modent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
600*0Sstevel@tonic-gate {
601*0Sstevel@tonic-gate const char *opt_t = NULL;
602*0Sstevel@tonic-gate const hash_type_entry_t *htep;
603*0Sstevel@tonic-gate int len;
604*0Sstevel@tonic-gate uint_t opt_k = 0;
605*0Sstevel@tonic-gate uint_t opt_v = 0;
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
608*0Sstevel@tonic-gate mdb_warn("address of mod_hash_entry must be specified\n");
609*0Sstevel@tonic-gate return (DCMD_ERR);
610*0Sstevel@tonic-gate }
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate len = mdb_getopts(argc, argv,
613*0Sstevel@tonic-gate 't', MDB_OPT_STR, &opt_t,
614*0Sstevel@tonic-gate 'k', MDB_OPT_SETBITS, 1, &opt_k,
615*0Sstevel@tonic-gate 'v', MDB_OPT_SETBITS, 1, &opt_v,
616*0Sstevel@tonic-gate NULL);
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gate /* options are mutually exclusive */
619*0Sstevel@tonic-gate if ((opt_k && opt_v) || (opt_t != NULL && (opt_k || opt_v)) ||
620*0Sstevel@tonic-gate len < argc) {
621*0Sstevel@tonic-gate return (DCMD_USAGE);
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate for (htep = hte_table; htep->hte_type != NULL; htep++)
625*0Sstevel@tonic-gate if (opt_t != NULL && strcmp(opt_t, htep->hte_type) == 0)
626*0Sstevel@tonic-gate break;
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate if (opt_t != NULL && htep->hte_type == NULL) {
629*0Sstevel@tonic-gate mdb_warn("unknown hash type %s\n", opt_t);
630*0Sstevel@tonic-gate return (DCMD_ERR);
631*0Sstevel@tonic-gate }
632*0Sstevel@tonic-gate
633*0Sstevel@tonic-gate return (modent_print(addr, 0, flags, htep, FALSE, opt_k, opt_v));
634*0Sstevel@tonic-gate }
635