xref: /titanic_41/usr/src/cmd/modload/modinfo.c (revision 2e107de79998f3036decec2454002940afb9a6ff)
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
5*2e107de7SJerry Gilliam  * Common Development and Distribution License (the "License").
6*2e107de7SJerry Gilliam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*2e107de7SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <inttypes.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stddef.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static int wide;
367c478bd9Sstevel@tonic-gate static int count = 0;
377c478bd9Sstevel@tonic-gate static int first_mod = 1;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * When printing module load addresses on 32-bit kernels, the 8 hex
417c478bd9Sstevel@tonic-gate  * character field width is obviously adequate. On sparcv9 kernels
427c478bd9Sstevel@tonic-gate  * solely by virtue of the choice of code model enabled by the separate
437c478bd9Sstevel@tonic-gate  * kernel context, the text addresses are currently in the lower 4G
447c478bd9Sstevel@tonic-gate  * address range, and so -still- fit in 8 hex characters.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * However, amd64 kernels live at the top of the 64-bit address space, and
477c478bd9Sstevel@tonic-gate  * so as to be honest about the addresses (and since this is a tool for
487c478bd9Sstevel@tonic-gate  * humans to parse), we have to print out a 16 hex character address.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * We assume that we will print out all 16 hex characters on future
517c478bd9Sstevel@tonic-gate  * 64-bit kernel ports too.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static const char header[] =
547c478bd9Sstevel@tonic-gate 	" Id "
557c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
567c478bd9Sstevel@tonic-gate 	"        "
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 	"Loadaddr   Size Info Rev Module Name\n";
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static char *cheader  =
617c478bd9Sstevel@tonic-gate 	" Id    Loadcnt Module Name                            State\n";
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static void usage();
657c478bd9Sstevel@tonic-gate static void print_info(struct modinfo *mi);
667c478bd9Sstevel@tonic-gate static void print_cinfo(struct modinfo *mi);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * These functions are in modsubr.c
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate void fatal(char *fmt, ...);
727c478bd9Sstevel@tonic-gate void error(char *fmt, ...);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Display information of all loaded modules
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])787c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	struct modinfo modinfo;
817c478bd9Sstevel@tonic-gate 	int info_all = 1;
827c478bd9Sstevel@tonic-gate 	int id;
837c478bd9Sstevel@tonic-gate 	int opt;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	id = -1;	/* assume we're getting all loaded modules */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i:wc")) != EOF) {
887c478bd9Sstevel@tonic-gate 		switch (opt) {
897c478bd9Sstevel@tonic-gate 		case 'i':
907c478bd9Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &id) != 1)
917c478bd9Sstevel@tonic-gate 				fatal("Invalid id %s\n", optarg);
927c478bd9Sstevel@tonic-gate 			if (id == -1)
937c478bd9Sstevel@tonic-gate 				id = 0;
947c478bd9Sstevel@tonic-gate 			info_all = 0;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case 'w':
977c478bd9Sstevel@tonic-gate 			wide++;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case 'c':
1007c478bd9Sstevel@tonic-gate 			count++;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		case '?':
1037c478bd9Sstevel@tonic-gate 		default:
1047c478bd9Sstevel@tonic-gate 			usage();
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Next id of -1 means we're getting info on all modules.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	modinfo.mi_id = modinfo.mi_nextid = id;
1147c478bd9Sstevel@tonic-gate 	modinfo.mi_info = (info_all) ? MI_INFO_ALL : MI_INFO_ONE;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (count)
1177c478bd9Sstevel@tonic-gate 		modinfo.mi_info |= MI_INFO_CNT;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	do {
1207c478bd9Sstevel@tonic-gate 		/*
1217c478bd9Sstevel@tonic-gate 		 * Get module information.
1227c478bd9Sstevel@tonic-gate 		 * If modinfo.mi_nextid == -1, get info about the
1237c478bd9Sstevel@tonic-gate 		 * next installed module with id > "id."
1247c478bd9Sstevel@tonic-gate 		 * Otherwise, get info about the module with id == "id."
1257c478bd9Sstevel@tonic-gate 		 */
1267c478bd9Sstevel@tonic-gate 		if (modctl(MODINFO, id, &modinfo) < 0) {
1277c478bd9Sstevel@tonic-gate 			if (!info_all)
1287c478bd9Sstevel@tonic-gate 				error("can't get module information");
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (first_mod) {
1337c478bd9Sstevel@tonic-gate 			first_mod = 0;
134*2e107de7SJerry Gilliam 			(void) printf("%s", count ? cheader : header);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		if (count)
1377c478bd9Sstevel@tonic-gate 			print_cinfo(&modinfo);
1387c478bd9Sstevel@tonic-gate 		else
1397c478bd9Sstevel@tonic-gate 			print_info(&modinfo);
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		 * If we're getting info about all modules, the next one
1427c478bd9Sstevel@tonic-gate 		 * we want is the one with an id greater than this one.
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		id = modinfo.mi_id;
1457c478bd9Sstevel@tonic-gate 	} while (info_all);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (0);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Display loadcounts.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate static void
print_cinfo(struct modinfo * mi)1547c478bd9Sstevel@tonic-gate print_cinfo(struct modinfo *mi)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	(void) printf("%3d %10d %-32s", mi->mi_id, mi->mi_loadcnt, mi->mi_name);
1577c478bd9Sstevel@tonic-gate 	(void) printf(" %s/%s\n",
1587c478bd9Sstevel@tonic-gate 	    mi->mi_state & MI_LOADED ? "LOADED" : "UNLOADED",
1597c478bd9Sstevel@tonic-gate 	    mi->mi_state & MI_INSTALLED ? "INSTALLED" : "UNINSTALLED");
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Display info about a loaded module.
1647c478bd9Sstevel@tonic-gate  *
165*2e107de7SJerry Gilliam  * The sparc kernel resides in its own address space, with modules
166*2e107de7SJerry Gilliam  * loaded at low addresses.  The low 32-bits of a module's base
167*2e107de7SJerry Gilliam  * address is sufficient but does put a cap at 4gb here.
168*2e107de7SJerry Gilliam  * The x86 64-bit kernel is loaded in high memory with the full
169*2e107de7SJerry Gilliam  * address provided.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static void
print_info(struct modinfo * mi)1727c478bd9Sstevel@tonic-gate print_info(struct modinfo *mi)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	int n, p0;
1757c478bd9Sstevel@tonic-gate 	char namebuf[256];
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (n = 0; n < MODMAXLINK; n++) {
1787c478bd9Sstevel@tonic-gate 		if (n > 0 && mi->mi_msinfo[n].msi_linkinfo[0] == '\0')
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		(void) printf("%3d ", mi->mi_id);
1827c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
1837c478bd9Sstevel@tonic-gate 		(void) printf("%16lx ", (uintptr_t)mi->mi_base);
184*2e107de7SJerry Gilliam #elif defined(_LP64)
1857c478bd9Sstevel@tonic-gate 		(void) printf("%8lx ", (uintptr_t)mi->mi_base);
186*2e107de7SJerry Gilliam #else
187*2e107de7SJerry Gilliam 		(void) printf("%8x ", (uintptr_t)mi->mi_base);
1887c478bd9Sstevel@tonic-gate #endif
189*2e107de7SJerry Gilliam #if defined(_LP64)
190*2e107de7SJerry Gilliam 		(void) printf("%6lx ", mi->mi_size);
191*2e107de7SJerry Gilliam #else
1927c478bd9Sstevel@tonic-gate 		(void) printf("%6x ", mi->mi_size);
193*2e107de7SJerry Gilliam #endif
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		p0 = mi->mi_msinfo[n].msi_p0;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (p0 != -1)
1987c478bd9Sstevel@tonic-gate 			(void) printf("%3d ", p0);
1997c478bd9Sstevel@tonic-gate 		else
2007c478bd9Sstevel@tonic-gate 			(void) printf("  - ");
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		(void) printf("  %d  ", mi->mi_rev);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		mi->mi_name[MODMAXNAMELEN - 1] = '\0';
2057c478bd9Sstevel@tonic-gate 		mi->mi_msinfo[n].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		if (wide) {
2087c478bd9Sstevel@tonic-gate 			(void) printf("%s (%s)\n", mi->mi_name,
2097c478bd9Sstevel@tonic-gate 			    mi->mi_msinfo[n].msi_linkinfo);
2107c478bd9Sstevel@tonic-gate 		} else {
2117c478bd9Sstevel@tonic-gate 			/* snprintf(3c) will always append a null character */
2127c478bd9Sstevel@tonic-gate 			(void) snprintf(namebuf, sizeof (namebuf), "%s (%s)",
2137c478bd9Sstevel@tonic-gate 			    mi->mi_name, mi->mi_msinfo[n].msi_linkinfo);
2147c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
2157c478bd9Sstevel@tonic-gate 			(void) printf("%.43s\n", namebuf);
2167c478bd9Sstevel@tonic-gate #else
2177c478bd9Sstevel@tonic-gate 			(void) printf("%.51s\n", namebuf);
2187c478bd9Sstevel@tonic-gate #endif
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
usage()2247c478bd9Sstevel@tonic-gate usage()
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	fatal("usage:  modinfo [-w] [-c] [-i module-id]\n");
2277c478bd9Sstevel@tonic-gate }
228