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