17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
287c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
297c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static uintptr_t module_head; /* Head of kernel modctl list */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate struct modctl_walk_data {
367c478bd9Sstevel@tonic-gate 	uintptr_t mwd_head;
377c478bd9Sstevel@tonic-gate 	struct modctl mwd_modctl;
387c478bd9Sstevel@tonic-gate };
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int
modctl_walk_init(mdb_walk_state_t * wsp)417c478bd9Sstevel@tonic-gate modctl_walk_init(mdb_walk_state_t *wsp)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	struct modctl_walk_data *mwd = mdb_alloc(
447c478bd9Sstevel@tonic-gate 	    sizeof (struct modctl_walk_data), UM_SLEEP);
457c478bd9Sstevel@tonic-gate 
46*892ad162SToomas Soome 	mwd->mwd_head = (wsp->walk_addr == 0 ? module_head : wsp->walk_addr);
477c478bd9Sstevel@tonic-gate 	wsp->walk_data = mwd;
48*892ad162SToomas Soome 	wsp->walk_addr = 0;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static int
modctl_walk_step(mdb_walk_state_t * wsp)547c478bd9Sstevel@tonic-gate modctl_walk_step(mdb_walk_state_t *wsp)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	struct modctl_walk_data *mwd = wsp->walk_data;
577c478bd9Sstevel@tonic-gate 	int	status;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == mwd->mwd_head)
607c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
617c478bd9Sstevel@tonic-gate 
62*892ad162SToomas Soome 	if (wsp->walk_addr == 0)
637c478bd9Sstevel@tonic-gate 		wsp->walk_addr = mwd->mwd_head;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mwd->mwd_modctl, sizeof (struct modctl),
667c478bd9Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
677c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read modctl at %p", wsp->walk_addr);
687c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &mwd->mwd_modctl,
727c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)mwd->mwd_modctl.mod_next;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (status);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static void
modctl_walk_fini(mdb_walk_state_t * wsp)807c478bd9Sstevel@tonic-gate modctl_walk_fini(mdb_walk_state_t *wsp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (struct modctl_walk_data));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
867c478bd9Sstevel@tonic-gate static int
modctl_format(uintptr_t addr,const void * data,void * private)877c478bd9Sstevel@tonic-gate modctl_format(uintptr_t addr, const void *data, void *private)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	const struct modctl *mcp = (const struct modctl *)data;
907c478bd9Sstevel@tonic-gate 	char name[MAXPATHLEN], bits[6], *bp = &bits[0];
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name),
937c478bd9Sstevel@tonic-gate 	    (uintptr_t)mcp->mod_filename) == -1)
947c478bd9Sstevel@tonic-gate 		(void) strcpy(name, "???");
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (mcp->mod_busy)
977c478bd9Sstevel@tonic-gate 		*bp++ = 'b';
987c478bd9Sstevel@tonic-gate 	if (mcp->mod_want)
997c478bd9Sstevel@tonic-gate 		*bp++ = 'w';
1007c478bd9Sstevel@tonic-gate 	if (mcp->mod_prim)
1017c478bd9Sstevel@tonic-gate 		*bp++ = 'p';
1027c478bd9Sstevel@tonic-gate 	if (mcp->mod_loaded)
1037c478bd9Sstevel@tonic-gate 		*bp++ = 'l';
1047c478bd9Sstevel@tonic-gate 	if (mcp->mod_installed)
1057c478bd9Sstevel@tonic-gate 		*bp++ = 'i';
1067c478bd9Sstevel@tonic-gate 	*bp = '\0';
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	mdb_printf("%?p %?p %6s 0x%02x %3d %s\n",
1097c478bd9Sstevel@tonic-gate 	    (uintptr_t)addr, (uintptr_t)mcp->mod_mp, bits, mcp->mod_loadflags,
1107c478bd9Sstevel@tonic-gate 	    mcp->mod_ref, name);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1167c478bd9Sstevel@tonic-gate static int
modctls(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1177c478bd9Sstevel@tonic-gate modctls(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	if (argc != 0)
1207c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
1237c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %6s %4s %3s %s%</u>\n",
1247c478bd9Sstevel@tonic-gate 		    "MODCTL", "MODULE", "BITS", "FLAGS", "REF", "FILE");
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
1287c478bd9Sstevel@tonic-gate 		struct modctl mc;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		(void) mdb_vread(&mc, sizeof (mc), addr);
1317c478bd9Sstevel@tonic-gate 		return (modctl_format(addr, &mc, NULL));
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (mdb_walk("modctl", modctl_format, NULL) == -1)
1357c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static void
dump_ehdr(const Ehdr * ehdr)1417c478bd9Sstevel@tonic-gate dump_ehdr(const Ehdr *ehdr)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	mdb_printf("\nELF Header\n");
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	mdb_printf("  ei_magic:  { 0x%02x, %c, %c, %c }\n",
1467c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG0], ehdr->e_ident[EI_MAG1],
1477c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2], ehdr->e_ident[EI_MAG3]);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	mdb_printf("  ei_class:  %-18u      ei_data: %-16u\n",
1507c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS], ehdr->e_ident[EI_DATA]);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	mdb_printf("  e_machine: %-18hu    e_version: %-16u\n",
1537c478bd9Sstevel@tonic-gate 	    ehdr->e_machine, ehdr->e_version);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	mdb_printf("  e_type:    %-18hu\n", ehdr->e_type);
1567c478bd9Sstevel@tonic-gate 	mdb_printf("  e_flags:   %-18u\n", ehdr->e_flags);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	mdb_printf("  e_entry:   0x%16lx  e_ehsize:    %8hu  e_shstrndx: %hu\n",
1597c478bd9Sstevel@tonic-gate 	    ehdr->e_entry, ehdr->e_ehsize, ehdr->e_shstrndx);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	mdb_printf("  e_shoff:   0x%16lx  e_shentsize: %8hu  e_shnum:    %hu\n",
1627c478bd9Sstevel@tonic-gate 	    ehdr->e_shoff, ehdr->e_shentsize, ehdr->e_shnum);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	mdb_printf("  e_phoff:   0x%16lx  e_phentsize: %8hu  e_phnum:    %hu\n",
1657c478bd9Sstevel@tonic-gate 	    ehdr->e_phoff, ehdr->e_phentsize, ehdr->e_phnum);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static void
dump_shdr(const Shdr * shdr,int i)1697c478bd9Sstevel@tonic-gate dump_shdr(const Shdr *shdr, int i)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	static const mdb_bitmask_t sh_type_masks[] = {
1727c478bd9Sstevel@tonic-gate 		{ "SHT_NULL", 0xffffffff, SHT_NULL },
1737c478bd9Sstevel@tonic-gate 		{ "SHT_PROGBITS", 0xffffffff, SHT_PROGBITS },
1747c478bd9Sstevel@tonic-gate 		{ "SHT_SYMTAB", 0xffffffff, SHT_SYMTAB },
1757c478bd9Sstevel@tonic-gate 		{ "SHT_STRTAB", 0xffffffff, SHT_STRTAB },
1767c478bd9Sstevel@tonic-gate 		{ "SHT_RELA", 0xffffffff, SHT_RELA },
1777c478bd9Sstevel@tonic-gate 		{ "SHT_HASH", 0xffffffff, SHT_HASH },
1787c478bd9Sstevel@tonic-gate 		{ "SHT_DYNAMIC", 0xffffffff, SHT_DYNAMIC },
1797c478bd9Sstevel@tonic-gate 		{ "SHT_NOTE", 0xffffffff, SHT_NOTE },
1807c478bd9Sstevel@tonic-gate 		{ "SHT_NOBITS", 0xffffffff, SHT_NOBITS },
1817c478bd9Sstevel@tonic-gate 		{ "SHT_REL", 0xffffffff, SHT_REL },
1827c478bd9Sstevel@tonic-gate 		{ "SHT_SHLIB", 0xffffffff, SHT_SHLIB },
1837c478bd9Sstevel@tonic-gate 		{ "SHT_DYNSYM", 0xffffffff, SHT_DYNSYM },
1847c478bd9Sstevel@tonic-gate 		{ "SHT_LOSUNW", 0xffffffff, SHT_LOSUNW },
1857c478bd9Sstevel@tonic-gate 		{ "SHT_SUNW_COMDAT", 0xffffffff, SHT_SUNW_COMDAT },
1867c478bd9Sstevel@tonic-gate 		{ "SHT_SUNW_syminfo", 0xffffffff, SHT_SUNW_syminfo },
1877c478bd9Sstevel@tonic-gate 		{ "SHT_SUNW_verdef", 0xffffffff, SHT_SUNW_verdef },
1887c478bd9Sstevel@tonic-gate 		{ "SHT_SUNW_verneed", 0xffffffff, SHT_SUNW_verneed },
1897c478bd9Sstevel@tonic-gate 		{ "SHT_SUNW_versym", 0xffffffff, SHT_SUNW_versym },
1907c478bd9Sstevel@tonic-gate 		{ "SHT_HISUNW", 0xffffffff, SHT_HISUNW },
1917c478bd9Sstevel@tonic-gate 		{ "SHT_LOPROC", 0xffffffff, SHT_LOPROC },
1927c478bd9Sstevel@tonic-gate 		{ "SHT_HIPROC", 0xffffffff, SHT_HIPROC },
1937c478bd9Sstevel@tonic-gate 		{ "SHT_LOUSER", 0xffffffff, SHT_LOUSER },
1947c478bd9Sstevel@tonic-gate 		{ "SHT_HIUSER", 0xffffffff, SHT_HIUSER },
1957c478bd9Sstevel@tonic-gate 		{ NULL, 0, 0 }
1967c478bd9Sstevel@tonic-gate 	};
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	static const mdb_bitmask_t sh_flag_masks[] = {
1997c478bd9Sstevel@tonic-gate 		{ "SHF_WRITE", SHF_WRITE, SHF_WRITE },
2007c478bd9Sstevel@tonic-gate 		{ "SHF_ALLOC", SHF_ALLOC, SHF_ALLOC },
2017c478bd9Sstevel@tonic-gate 		{ "SHF_EXECINSTR", SHF_EXECINSTR, SHF_EXECINSTR },
2027c478bd9Sstevel@tonic-gate 		{ "SHF_MASKPROC", SHF_MASKPROC, SHF_MASKPROC },
2037c478bd9Sstevel@tonic-gate 		{ NULL, 0, 0 }
2047c478bd9Sstevel@tonic-gate 	};
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	mdb_printf("\nSection Header[%d]:\n", i);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	mdb_printf("    sh_addr:      0x%-16lx  sh_flags:   [ %#lb ]\n",
2097c478bd9Sstevel@tonic-gate 	    shdr->sh_addr, shdr->sh_flags, sh_flag_masks);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	mdb_printf("    sh_size:      0x%-16lx  sh_type:    [ %#lb ]\n",
2127c478bd9Sstevel@tonic-gate 	    shdr->sh_size, shdr->sh_type, sh_type_masks);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	mdb_printf("    sh_offset:    0x%-16lx  sh_entsize: 0x%lx\n",
2157c478bd9Sstevel@tonic-gate 	    shdr->sh_offset, shdr->sh_entsize);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	mdb_printf("    sh_link:      0x%-16lx  sh_info:    0x%lx\n",
2187c478bd9Sstevel@tonic-gate 	    shdr->sh_link, shdr->sh_info);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	mdb_printf("    sh_addralign: 0x%-16lx\n", shdr->sh_addralign);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2247c478bd9Sstevel@tonic-gate static int
modhdrs(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2257c478bd9Sstevel@tonic-gate modhdrs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	struct modctl ctl;
2287c478bd9Sstevel@tonic-gate 	struct module mod;
2297c478bd9Sstevel@tonic-gate 	Shdr *shdrs;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	size_t nbytes;
2327c478bd9Sstevel@tonic-gate 	int i;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2357c478bd9Sstevel@tonic-gate 		mdb_warn("expected address of struct modctl before ::\n");
2367c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (argc != 0)
2407c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	mdb_vread(&ctl, sizeof (struct modctl), addr);
2437c478bd9Sstevel@tonic-gate 	mdb_vread(&mod, sizeof (struct module), (uintptr_t)ctl.mod_mp);
2447c478bd9Sstevel@tonic-gate 	dump_ehdr(&mod.hdr);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	nbytes = sizeof (Shdr) * mod.hdr.e_shnum;
2477c478bd9Sstevel@tonic-gate 	shdrs = mdb_alloc(nbytes, UM_SLEEP | UM_GC);
2487c478bd9Sstevel@tonic-gate 	mdb_vread(shdrs, nbytes, (uintptr_t)mod.shdrs);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	for (i = 0; i < mod.hdr.e_shnum; i++)
2517c478bd9Sstevel@tonic-gate 		dump_shdr(&shdrs[i], i);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2577c478bd9Sstevel@tonic-gate static int
modinfo_format(uintptr_t addr,const void * data,void * private)2587c478bd9Sstevel@tonic-gate modinfo_format(uintptr_t addr, const void *data, void *private)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	const struct modctl *mcp = (const struct modctl *)data;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	struct modlinkage linkage;
2637c478bd9Sstevel@tonic-gate 	struct modlmisc lmisc;
2647c478bd9Sstevel@tonic-gate 	struct module mod;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	char info[MODMAXLINKINFOLEN];
2677c478bd9Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	mod.text_size = 0;
2707c478bd9Sstevel@tonic-gate 	mod.data_size = 0;
2717c478bd9Sstevel@tonic-gate 	mod.text = NULL;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	linkage.ml_rev = 0;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	info[0] = '\0';
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (mcp->mod_mp != NULL)
2787c478bd9Sstevel@tonic-gate 		(void) mdb_vread(&mod, sizeof (mod), (uintptr_t)mcp->mod_mp);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (mcp->mod_linkage != NULL) {
2817c478bd9Sstevel@tonic-gate 		(void) mdb_vread(&linkage, sizeof (linkage),
2827c478bd9Sstevel@tonic-gate 		    (uintptr_t)mcp->mod_linkage);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		if (linkage.ml_linkage[0] != NULL) {
2857c478bd9Sstevel@tonic-gate 			(void) mdb_vread(&lmisc, sizeof (lmisc),
2867c478bd9Sstevel@tonic-gate 			    (uintptr_t)linkage.ml_linkage[0]);
2877c478bd9Sstevel@tonic-gate 			mdb_readstr(info, sizeof (info),
2887c478bd9Sstevel@tonic-gate 			    (uintptr_t)lmisc.misc_linkinfo);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mcp->mod_modname) == -1)
2937c478bd9Sstevel@tonic-gate 		(void) strcpy(name, "???");
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	mdb_printf("%3d %?p %8lx %3d %s (%s)\n",
2967c478bd9Sstevel@tonic-gate 	    mcp->mod_id, mod.text, mod.text_size + mod.data_size,
2977c478bd9Sstevel@tonic-gate 	    linkage.ml_rev, name, info[0] != '\0' ? info : "?");
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3037c478bd9Sstevel@tonic-gate static int
modinfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)3047c478bd9Sstevel@tonic-gate modinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	if (argc != 0)
3077c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
3107c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%3s %?s %8s %3s %s%</u>\n",
3117c478bd9Sstevel@tonic-gate 		    "ID", "LOADADDR", "SIZE", "REV", "MODULE NAME");
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
3157c478bd9Sstevel@tonic-gate 		struct modctl mc;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		(void) mdb_vread(&mc, sizeof (mc), addr);
3187c478bd9Sstevel@tonic-gate 		return (modinfo_format(addr, &mc, NULL));
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (mdb_walk("modctl", modinfo_format, NULL) == -1)
3227c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3287c478bd9Sstevel@tonic-gate static int
ctfinfo_format(uintptr_t addr,const struct modctl * mcp,void * private)3297c478bd9Sstevel@tonic-gate ctfinfo_format(uintptr_t addr, const struct modctl *mcp, void *private)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	char name[MODMAXNAMELEN];
3327c478bd9Sstevel@tonic-gate 	struct module mod;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (mcp->mod_mp == NULL)
3357c478bd9Sstevel@tonic-gate 		return (WALK_NEXT); /* module is not loaded */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)mcp->mod_mp) == -1) {
3387c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read module at %p for modctl %p\n",
3397c478bd9Sstevel@tonic-gate 		    mcp->mod_mp, addr);
3407c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)mcp->mod_modname) == -1)
3447c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(name, sizeof (name), "%a", mcp->mod_mp);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	mdb_printf("%-30s %?p %lu\n", name, mod.ctfdata, (ulong_t)mod.ctfsize);
3477c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3517c478bd9Sstevel@tonic-gate static int
ctfinfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)3527c478bd9Sstevel@tonic-gate ctfinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
3557c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	mdb_printf("%<u>%-30s %?s %s%</u>\n", "MODULE", "CTFDATA", "CTFSIZE");
3587c478bd9Sstevel@tonic-gate 	if (mdb_walk("modctl", (mdb_walk_cb_t)ctfinfo_format, NULL) == -1)
3597c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
3657c478bd9Sstevel@tonic-gate 	{ "modctl", NULL, "list modctl structures", modctls },
3667c478bd9Sstevel@tonic-gate 	{ "modhdrs", ":", "given modctl, dump module ehdr and shdrs", modhdrs },
3677c478bd9Sstevel@tonic-gate 	{ "modinfo", NULL, "list module information", modinfo },
3687c478bd9Sstevel@tonic-gate 	{ "ctfinfo", NULL, "list module CTF information", ctfinfo },
3697c478bd9Sstevel@tonic-gate 	{ NULL }
3707c478bd9Sstevel@tonic-gate };
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
3737c478bd9Sstevel@tonic-gate 	{ "modctl", "list of modctl structures",
3747c478bd9Sstevel@tonic-gate 		modctl_walk_init, modctl_walk_step, modctl_walk_fini },
3757c478bd9Sstevel@tonic-gate 	{ NULL }
3767c478bd9Sstevel@tonic-gate };
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static const mdb_modinfo_t krtld_modinfo = { MDB_API_VERSION, dcmds, walkers };
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)3817c478bd9Sstevel@tonic-gate _mdb_init(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("modules", &sym) == -1) {
3867c478bd9Sstevel@tonic-gate 		mdb_warn("failed to lookup 'modules'");
3877c478bd9Sstevel@tonic-gate 		return (NULL);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	module_head = (uintptr_t)sym.st_value;
3917c478bd9Sstevel@tonic-gate 	return (&krtld_modinfo);
3927c478bd9Sstevel@tonic-gate }
393