xref: /onnv-gate/usr/src/cmd/mdb/common/modules/krtld/krtld.c (revision 0:68f95e015346)
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 2004 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 <sys/machelf.h>
30*0Sstevel@tonic-gate #include <sys/modctl.h>
31*0Sstevel@tonic-gate #include <sys/kobj.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate static uintptr_t module_head; /* Head of kernel modctl list */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate struct modctl_walk_data {
38*0Sstevel@tonic-gate 	uintptr_t mwd_head;
39*0Sstevel@tonic-gate 	struct modctl mwd_modctl;
40*0Sstevel@tonic-gate };
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static int
modctl_walk_init(mdb_walk_state_t * wsp)43*0Sstevel@tonic-gate modctl_walk_init(mdb_walk_state_t *wsp)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	struct modctl_walk_data *mwd = mdb_alloc(
46*0Sstevel@tonic-gate 	    sizeof (struct modctl_walk_data), UM_SLEEP);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	mwd->mwd_head = (wsp->walk_addr == NULL ? module_head : wsp->walk_addr);
49*0Sstevel@tonic-gate 	wsp->walk_data = mwd;
50*0Sstevel@tonic-gate 	wsp->walk_addr = NULL;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	return (WALK_NEXT);
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static int
modctl_walk_step(mdb_walk_state_t * wsp)56*0Sstevel@tonic-gate modctl_walk_step(mdb_walk_state_t *wsp)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	struct modctl_walk_data *mwd = wsp->walk_data;
59*0Sstevel@tonic-gate 	int	status;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if (wsp->walk_addr == mwd->mwd_head)
62*0Sstevel@tonic-gate 		return (WALK_DONE);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
65*0Sstevel@tonic-gate 		wsp->walk_addr = mwd->mwd_head;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	if (mdb_vread(&mwd->mwd_modctl, sizeof (struct modctl),
68*0Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
69*0Sstevel@tonic-gate 		mdb_warn("failed to read modctl at %p", wsp->walk_addr);
70*0Sstevel@tonic-gate 		return (WALK_ERR);
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &mwd->mwd_modctl,
74*0Sstevel@tonic-gate 	    wsp->walk_cbdata);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)mwd->mwd_modctl.mod_next;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	return (status);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static void
modctl_walk_fini(mdb_walk_state_t * wsp)82*0Sstevel@tonic-gate modctl_walk_fini(mdb_walk_state_t *wsp)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (struct modctl_walk_data));
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /*ARGSUSED*/
88*0Sstevel@tonic-gate static int
modctl_format(uintptr_t addr,const void * data,void * private)89*0Sstevel@tonic-gate modctl_format(uintptr_t addr, const void *data, void *private)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	const struct modctl *mcp = (const struct modctl *)data;
92*0Sstevel@tonic-gate 	char name[MAXPATHLEN], bits[6], *bp = &bits[0];
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name),
95*0Sstevel@tonic-gate 	    (uintptr_t)mcp->mod_filename) == -1)
96*0Sstevel@tonic-gate 		(void) strcpy(name, "???");
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if (mcp->mod_busy)
99*0Sstevel@tonic-gate 		*bp++ = 'b';
100*0Sstevel@tonic-gate 	if (mcp->mod_want)
101*0Sstevel@tonic-gate 		*bp++ = 'w';
102*0Sstevel@tonic-gate 	if (mcp->mod_prim)
103*0Sstevel@tonic-gate 		*bp++ = 'p';
104*0Sstevel@tonic-gate 	if (mcp->mod_loaded)
105*0Sstevel@tonic-gate 		*bp++ = 'l';
106*0Sstevel@tonic-gate 	if (mcp->mod_installed)
107*0Sstevel@tonic-gate 		*bp++ = 'i';
108*0Sstevel@tonic-gate 	*bp = '\0';
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	mdb_printf("%?p %?p %6s 0x%02x %3d %s\n",
111*0Sstevel@tonic-gate 	    (uintptr_t)addr, (uintptr_t)mcp->mod_mp, bits, mcp->mod_loadflags,
112*0Sstevel@tonic-gate 	    mcp->mod_ref, name);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	return (WALK_NEXT);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /*ARGSUSED*/
118*0Sstevel@tonic-gate static int
modctls(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)119*0Sstevel@tonic-gate modctls(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	if (argc != 0)
122*0Sstevel@tonic-gate 		return (DCMD_USAGE);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
125*0Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %6s %4s %3s %s%</u>\n",
126*0Sstevel@tonic-gate 		    "MODCTL", "MODULE", "BITS", "FLAGS", "REF", "FILE");
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
130*0Sstevel@tonic-gate 		struct modctl mc;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		(void) mdb_vread(&mc, sizeof (mc), addr);
133*0Sstevel@tonic-gate 		return (modctl_format(addr, &mc, NULL));
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (mdb_walk("modctl", modctl_format, NULL) == -1)
137*0Sstevel@tonic-gate 		return (DCMD_ERR);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	return (DCMD_OK);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate static void
dump_ehdr(const Ehdr * ehdr)143*0Sstevel@tonic-gate dump_ehdr(const Ehdr *ehdr)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	mdb_printf("\nELF Header\n");
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	mdb_printf("  ei_magic:  { 0x%02x, %c, %c, %c }\n",
148*0Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG0], ehdr->e_ident[EI_MAG1],
149*0Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2], ehdr->e_ident[EI_MAG3]);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	mdb_printf("  ei_class:  %-18u      ei_data: %-16u\n",
152*0Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS], ehdr->e_ident[EI_DATA]);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	mdb_printf("  e_machine: %-18hu    e_version: %-16u\n",
155*0Sstevel@tonic-gate 	    ehdr->e_machine, ehdr->e_version);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	mdb_printf("  e_type:    %-18hu\n", ehdr->e_type);
158*0Sstevel@tonic-gate 	mdb_printf("  e_flags:   %-18u\n", ehdr->e_flags);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	mdb_printf("  e_entry:   0x%16lx  e_ehsize:    %8hu  e_shstrndx: %hu\n",
161*0Sstevel@tonic-gate 	    ehdr->e_entry, ehdr->e_ehsize, ehdr->e_shstrndx);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	mdb_printf("  e_shoff:   0x%16lx  e_shentsize: %8hu  e_shnum:    %hu\n",
164*0Sstevel@tonic-gate 	    ehdr->e_shoff, ehdr->e_shentsize, ehdr->e_shnum);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	mdb_printf("  e_phoff:   0x%16lx  e_phentsize: %8hu  e_phnum:    %hu\n",
167*0Sstevel@tonic-gate 	    ehdr->e_phoff, ehdr->e_phentsize, ehdr->e_phnum);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate static void
dump_shdr(const Shdr * shdr,int i)171*0Sstevel@tonic-gate dump_shdr(const Shdr *shdr, int i)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	static const mdb_bitmask_t sh_type_masks[] = {
174*0Sstevel@tonic-gate 		{ "SHT_NULL", 0xffffffff, SHT_NULL },
175*0Sstevel@tonic-gate 		{ "SHT_PROGBITS", 0xffffffff, SHT_PROGBITS },
176*0Sstevel@tonic-gate 		{ "SHT_SYMTAB", 0xffffffff, SHT_SYMTAB },
177*0Sstevel@tonic-gate 		{ "SHT_STRTAB", 0xffffffff, SHT_STRTAB },
178*0Sstevel@tonic-gate 		{ "SHT_RELA", 0xffffffff, SHT_RELA },
179*0Sstevel@tonic-gate 		{ "SHT_HASH", 0xffffffff, SHT_HASH },
180*0Sstevel@tonic-gate 		{ "SHT_DYNAMIC", 0xffffffff, SHT_DYNAMIC },
181*0Sstevel@tonic-gate 		{ "SHT_NOTE", 0xffffffff, SHT_NOTE },
182*0Sstevel@tonic-gate 		{ "SHT_NOBITS", 0xffffffff, SHT_NOBITS },
183*0Sstevel@tonic-gate 		{ "SHT_REL", 0xffffffff, SHT_REL },
184*0Sstevel@tonic-gate 		{ "SHT_SHLIB", 0xffffffff, SHT_SHLIB },
185*0Sstevel@tonic-gate 		{ "SHT_DYNSYM", 0xffffffff, SHT_DYNSYM },
186*0Sstevel@tonic-gate 		{ "SHT_LOSUNW", 0xffffffff, SHT_LOSUNW },
187*0Sstevel@tonic-gate 		{ "SHT_SUNW_COMDAT", 0xffffffff, SHT_SUNW_COMDAT },
188*0Sstevel@tonic-gate 		{ "SHT_SUNW_syminfo", 0xffffffff, SHT_SUNW_syminfo },
189*0Sstevel@tonic-gate 		{ "SHT_SUNW_verdef", 0xffffffff, SHT_SUNW_verdef },
190*0Sstevel@tonic-gate 		{ "SHT_SUNW_verneed", 0xffffffff, SHT_SUNW_verneed },
191*0Sstevel@tonic-gate 		{ "SHT_SUNW_versym", 0xffffffff, SHT_SUNW_versym },
192*0Sstevel@tonic-gate 		{ "SHT_HISUNW", 0xffffffff, SHT_HISUNW },
193*0Sstevel@tonic-gate 		{ "SHT_LOPROC", 0xffffffff, SHT_LOPROC },
194*0Sstevel@tonic-gate 		{ "SHT_HIPROC", 0xffffffff, SHT_HIPROC },
195*0Sstevel@tonic-gate 		{ "SHT_LOUSER", 0xffffffff, SHT_LOUSER },
196*0Sstevel@tonic-gate 		{ "SHT_HIUSER", 0xffffffff, SHT_HIUSER },
197*0Sstevel@tonic-gate 		{ NULL, 0, 0 }
198*0Sstevel@tonic-gate 	};
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	static const mdb_bitmask_t sh_flag_masks[] = {
201*0Sstevel@tonic-gate 		{ "SHF_WRITE", SHF_WRITE, SHF_WRITE },
202*0Sstevel@tonic-gate 		{ "SHF_ALLOC", SHF_ALLOC, SHF_ALLOC },
203*0Sstevel@tonic-gate 		{ "SHF_EXECINSTR", SHF_EXECINSTR, SHF_EXECINSTR },
204*0Sstevel@tonic-gate 		{ "SHF_MASKPROC", SHF_MASKPROC, SHF_MASKPROC },
205*0Sstevel@tonic-gate 		{ NULL, 0, 0 }
206*0Sstevel@tonic-gate 	};
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	mdb_printf("\nSection Header[%d]:\n", i);
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	mdb_printf("    sh_addr:      0x%-16lx  sh_flags:   [ %#lb ]\n",
211*0Sstevel@tonic-gate 	    shdr->sh_addr, shdr->sh_flags, sh_flag_masks);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	mdb_printf("    sh_size:      0x%-16lx  sh_type:    [ %#lb ]\n",
214*0Sstevel@tonic-gate 	    shdr->sh_size, shdr->sh_type, sh_type_masks);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	mdb_printf("    sh_offset:    0x%-16lx  sh_entsize: 0x%lx\n",
217*0Sstevel@tonic-gate 	    shdr->sh_offset, shdr->sh_entsize);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	mdb_printf("    sh_link:      0x%-16lx  sh_info:    0x%lx\n",
220*0Sstevel@tonic-gate 	    shdr->sh_link, shdr->sh_info);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	mdb_printf("    sh_addralign: 0x%-16lx\n", shdr->sh_addralign);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate /*ARGSUSED*/
226*0Sstevel@tonic-gate static int
modhdrs(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)227*0Sstevel@tonic-gate modhdrs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	struct modctl ctl;
230*0Sstevel@tonic-gate 	struct module mod;
231*0Sstevel@tonic-gate 	Shdr *shdrs;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	size_t nbytes;
234*0Sstevel@tonic-gate 	int i;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
237*0Sstevel@tonic-gate 		mdb_warn("expected address of struct modctl before ::\n");
238*0Sstevel@tonic-gate 		return (DCMD_USAGE);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (argc != 0)
242*0Sstevel@tonic-gate 		return (DCMD_USAGE);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	mdb_vread(&ctl, sizeof (struct modctl), addr);
245*0Sstevel@tonic-gate 	mdb_vread(&mod, sizeof (struct module), (uintptr_t)ctl.mod_mp);
246*0Sstevel@tonic-gate 	dump_ehdr(&mod.hdr);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	nbytes = sizeof (Shdr) * mod.hdr.e_shnum;
249*0Sstevel@tonic-gate 	shdrs = mdb_alloc(nbytes, UM_SLEEP | UM_GC);
250*0Sstevel@tonic-gate 	mdb_vread(shdrs, nbytes, (uintptr_t)mod.shdrs);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	for (i = 0; i < mod.hdr.e_shnum; i++)
253*0Sstevel@tonic-gate 		dump_shdr(&shdrs[i], i);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	return (DCMD_OK);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate /*ARGSUSED*/
259*0Sstevel@tonic-gate static int
modinfo_format(uintptr_t addr,const void * data,void * private)260*0Sstevel@tonic-gate modinfo_format(uintptr_t addr, const void *data, void *private)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	const struct modctl *mcp = (const struct modctl *)data;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	struct modlinkage linkage;
265*0Sstevel@tonic-gate 	struct modlmisc lmisc;
266*0Sstevel@tonic-gate 	struct module mod;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	char info[MODMAXLINKINFOLEN];
269*0Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	mod.text_size = 0;
272*0Sstevel@tonic-gate 	mod.data_size = 0;
273*0Sstevel@tonic-gate 	mod.text = NULL;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	linkage.ml_rev = 0;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	info[0] = '\0';
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (mcp->mod_mp != NULL)
280*0Sstevel@tonic-gate 		(void) mdb_vread(&mod, sizeof (mod), (uintptr_t)mcp->mod_mp);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (mcp->mod_linkage != NULL) {
283*0Sstevel@tonic-gate 		(void) mdb_vread(&linkage, sizeof (linkage),
284*0Sstevel@tonic-gate 		    (uintptr_t)mcp->mod_linkage);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 		if (linkage.ml_linkage[0] != NULL) {
287*0Sstevel@tonic-gate 			(void) mdb_vread(&lmisc, sizeof (lmisc),
288*0Sstevel@tonic-gate 			    (uintptr_t)linkage.ml_linkage[0]);
289*0Sstevel@tonic-gate 			mdb_readstr(info, sizeof (info),
290*0Sstevel@tonic-gate 			    (uintptr_t)lmisc.misc_linkinfo);
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mcp->mod_modname) == -1)
295*0Sstevel@tonic-gate 		(void) strcpy(name, "???");
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	mdb_printf("%3d %?p %8lx %3d %s (%s)\n",
298*0Sstevel@tonic-gate 	    mcp->mod_id, mod.text, mod.text_size + mod.data_size,
299*0Sstevel@tonic-gate 	    linkage.ml_rev, name, info[0] != '\0' ? info : "?");
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	return (WALK_NEXT);
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate /*ARGSUSED*/
305*0Sstevel@tonic-gate static int
modinfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)306*0Sstevel@tonic-gate modinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	if (argc != 0)
309*0Sstevel@tonic-gate 		return (DCMD_USAGE);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
312*0Sstevel@tonic-gate 		mdb_printf("%<u>%3s %?s %8s %3s %s%</u>\n",
313*0Sstevel@tonic-gate 		    "ID", "LOADADDR", "SIZE", "REV", "MODULE NAME");
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
317*0Sstevel@tonic-gate 		struct modctl mc;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		(void) mdb_vread(&mc, sizeof (mc), addr);
320*0Sstevel@tonic-gate 		return (modinfo_format(addr, &mc, NULL));
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (mdb_walk("modctl", modinfo_format, NULL) == -1)
324*0Sstevel@tonic-gate 		return (DCMD_ERR);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	return (DCMD_OK);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate /*ARGSUSED*/
330*0Sstevel@tonic-gate static int
ctfinfo_format(uintptr_t addr,const struct modctl * mcp,void * private)331*0Sstevel@tonic-gate ctfinfo_format(uintptr_t addr, const struct modctl *mcp, void *private)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
334*0Sstevel@tonic-gate 	struct module mod;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (mcp->mod_mp == NULL)
337*0Sstevel@tonic-gate 		return (WALK_NEXT); /* module is not loaded */
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)mcp->mod_mp) == -1) {
340*0Sstevel@tonic-gate 		mdb_warn("failed to read module at %p for modctl %p\n",
341*0Sstevel@tonic-gate 		    mcp->mod_mp, addr);
342*0Sstevel@tonic-gate 		return (WALK_NEXT);
343*0Sstevel@tonic-gate 	}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mcp->mod_modname) == -1)
346*0Sstevel@tonic-gate 		(void) mdb_snprintf(name, sizeof (name), "%a", mcp->mod_mp);
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	mdb_printf("%-30s %?p %lu\n", name, mod.ctfdata, (ulong_t)mod.ctfsize);
349*0Sstevel@tonic-gate 	return (WALK_NEXT);
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate /*ARGSUSED*/
353*0Sstevel@tonic-gate static int
ctfinfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)354*0Sstevel@tonic-gate ctfinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
357*0Sstevel@tonic-gate 		return (DCMD_USAGE);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	mdb_printf("%<u>%-30s %?s %s%</u>\n", "MODULE", "CTFDATA", "CTFSIZE");
360*0Sstevel@tonic-gate 	if (mdb_walk("modctl", (mdb_walk_cb_t)ctfinfo_format, NULL) == -1)
361*0Sstevel@tonic-gate 		return (DCMD_ERR);
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	return (DCMD_OK);
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
367*0Sstevel@tonic-gate 	{ "modctl", NULL, "list modctl structures", modctls },
368*0Sstevel@tonic-gate 	{ "modhdrs", ":", "given modctl, dump module ehdr and shdrs", modhdrs },
369*0Sstevel@tonic-gate 	{ "modinfo", NULL, "list module information", modinfo },
370*0Sstevel@tonic-gate 	{ "ctfinfo", NULL, "list module CTF information", ctfinfo },
371*0Sstevel@tonic-gate 	{ NULL }
372*0Sstevel@tonic-gate };
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
375*0Sstevel@tonic-gate 	{ "modctl", "list of modctl structures",
376*0Sstevel@tonic-gate 		modctl_walk_init, modctl_walk_step, modctl_walk_fini },
377*0Sstevel@tonic-gate 	{ NULL }
378*0Sstevel@tonic-gate };
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate static const mdb_modinfo_t krtld_modinfo = { MDB_API_VERSION, dcmds, walkers };
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)383*0Sstevel@tonic-gate _mdb_init(void)
384*0Sstevel@tonic-gate {
385*0Sstevel@tonic-gate 	GElf_Sym sym;
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	if (mdb_lookup_by_name("modules", &sym) == -1) {
388*0Sstevel@tonic-gate 		mdb_warn("failed to lookup 'modules'");
389*0Sstevel@tonic-gate 		return (NULL);
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	module_head = (uintptr_t)sym.st_value;
393*0Sstevel@tonic-gate 	return (&krtld_modinfo);
394*0Sstevel@tonic-gate }
395