xref: /onnv-gate/usr/src/cmd/modload/modinfo.c (revision 9268:e20fd2462007)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9268SJerry.Gilliam@Sun.COM  * Common Development and Distribution License (the "License").
6*9268SJerry.Gilliam@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9268SJerry.Gilliam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <inttypes.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stddef.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static int wide;
360Sstevel@tonic-gate static int count = 0;
370Sstevel@tonic-gate static int first_mod = 1;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * When printing module load addresses on 32-bit kernels, the 8 hex
410Sstevel@tonic-gate  * character field width is obviously adequate. On sparcv9 kernels
420Sstevel@tonic-gate  * solely by virtue of the choice of code model enabled by the separate
430Sstevel@tonic-gate  * kernel context, the text addresses are currently in the lower 4G
440Sstevel@tonic-gate  * address range, and so -still- fit in 8 hex characters.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * However, amd64 kernels live at the top of the 64-bit address space, and
470Sstevel@tonic-gate  * so as to be honest about the addresses (and since this is a tool for
480Sstevel@tonic-gate  * humans to parse), we have to print out a 16 hex character address.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * We assume that we will print out all 16 hex characters on future
510Sstevel@tonic-gate  * 64-bit kernel ports too.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate static const char header[] =
540Sstevel@tonic-gate 	" Id "
550Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
560Sstevel@tonic-gate 	"        "
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate 	"Loadaddr   Size Info Rev Module Name\n";
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static char *cheader  =
610Sstevel@tonic-gate 	" Id    Loadcnt Module Name                            State\n";
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void usage();
650Sstevel@tonic-gate static void print_info(struct modinfo *mi);
660Sstevel@tonic-gate static void print_cinfo(struct modinfo *mi);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * These functions are in modsubr.c
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate void fatal(char *fmt, ...);
720Sstevel@tonic-gate void error(char *fmt, ...);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Display information of all loaded modules
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate int
main(int argc,char * argv[])780Sstevel@tonic-gate main(int argc, char *argv[])
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	struct modinfo modinfo;
810Sstevel@tonic-gate 	int info_all = 1;
820Sstevel@tonic-gate 	int id;
830Sstevel@tonic-gate 	int opt;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	id = -1;	/* assume we're getting all loaded modules */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i:wc")) != EOF) {
880Sstevel@tonic-gate 		switch (opt) {
890Sstevel@tonic-gate 		case 'i':
900Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &id) != 1)
910Sstevel@tonic-gate 				fatal("Invalid id %s\n", optarg);
920Sstevel@tonic-gate 			if (id == -1)
930Sstevel@tonic-gate 				id = 0;
940Sstevel@tonic-gate 			info_all = 0;
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		case 'w':
970Sstevel@tonic-gate 			wide++;
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 		case 'c':
1000Sstevel@tonic-gate 			count++;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		case '?':
1030Sstevel@tonic-gate 		default:
1040Sstevel@tonic-gate 			usage();
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * Next id of -1 means we're getting info on all modules.
1120Sstevel@tonic-gate 	 */
1130Sstevel@tonic-gate 	modinfo.mi_id = modinfo.mi_nextid = id;
1140Sstevel@tonic-gate 	modinfo.mi_info = (info_all) ? MI_INFO_ALL : MI_INFO_ONE;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (count)
1170Sstevel@tonic-gate 		modinfo.mi_info |= MI_INFO_CNT;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	do {
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * Get module information.
1220Sstevel@tonic-gate 		 * If modinfo.mi_nextid == -1, get info about the
1230Sstevel@tonic-gate 		 * next installed module with id > "id."
1240Sstevel@tonic-gate 		 * Otherwise, get info about the module with id == "id."
1250Sstevel@tonic-gate 		 */
1260Sstevel@tonic-gate 		if (modctl(MODINFO, id, &modinfo) < 0) {
1270Sstevel@tonic-gate 			if (!info_all)
1280Sstevel@tonic-gate 				error("can't get module information");
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if (first_mod) {
1330Sstevel@tonic-gate 			first_mod = 0;
134*9268SJerry.Gilliam@Sun.COM 			(void) printf("%s", count ? cheader : header);
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 		if (count)
1370Sstevel@tonic-gate 			print_cinfo(&modinfo);
1380Sstevel@tonic-gate 		else
1390Sstevel@tonic-gate 			print_info(&modinfo);
1400Sstevel@tonic-gate 		/*
1410Sstevel@tonic-gate 		 * If we're getting info about all modules, the next one
1420Sstevel@tonic-gate 		 * we want is the one with an id greater than this one.
1430Sstevel@tonic-gate 		 */
1440Sstevel@tonic-gate 		id = modinfo.mi_id;
1450Sstevel@tonic-gate 	} while (info_all);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (0);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Display loadcounts.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static void
print_cinfo(struct modinfo * mi)1540Sstevel@tonic-gate print_cinfo(struct modinfo *mi)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	(void) printf("%3d %10d %-32s", mi->mi_id, mi->mi_loadcnt, mi->mi_name);
1570Sstevel@tonic-gate 	(void) printf(" %s/%s\n",
158*9268SJerry.Gilliam@Sun.COM 	    mi->mi_state & MI_LOADED ? "LOADED" : "UNLOADED",
159*9268SJerry.Gilliam@Sun.COM 	    mi->mi_state & MI_INSTALLED ? "INSTALLED" : "UNINSTALLED");
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Display info about a loaded module.
1640Sstevel@tonic-gate  *
165*9268SJerry.Gilliam@Sun.COM  * The sparc kernel resides in its own address space, with modules
166*9268SJerry.Gilliam@Sun.COM  * loaded at low addresses.  The low 32-bits of a module's base
167*9268SJerry.Gilliam@Sun.COM  * address is sufficient but does put a cap at 4gb here.
168*9268SJerry.Gilliam@Sun.COM  * The x86 64-bit kernel is loaded in high memory with the full
169*9268SJerry.Gilliam@Sun.COM  * address provided.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate static void
print_info(struct modinfo * mi)1720Sstevel@tonic-gate print_info(struct modinfo *mi)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	int n, p0;
1750Sstevel@tonic-gate 	char namebuf[256];
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	for (n = 0; n < MODMAXLINK; n++) {
1780Sstevel@tonic-gate 		if (n > 0 && mi->mi_msinfo[n].msi_linkinfo[0] == '\0')
1790Sstevel@tonic-gate 			break;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		(void) printf("%3d ", mi->mi_id);
1820Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
1830Sstevel@tonic-gate 		(void) printf("%16lx ", (uintptr_t)mi->mi_base);
184*9268SJerry.Gilliam@Sun.COM #elif defined(_LP64)
1850Sstevel@tonic-gate 		(void) printf("%8lx ", (uintptr_t)mi->mi_base);
186*9268SJerry.Gilliam@Sun.COM #else
187*9268SJerry.Gilliam@Sun.COM 		(void) printf("%8x ", (uintptr_t)mi->mi_base);
1880Sstevel@tonic-gate #endif
189*9268SJerry.Gilliam@Sun.COM #if defined(_LP64)
190*9268SJerry.Gilliam@Sun.COM 		(void) printf("%6lx ", mi->mi_size);
191*9268SJerry.Gilliam@Sun.COM #else
1920Sstevel@tonic-gate 		(void) printf("%6x ", mi->mi_size);
193*9268SJerry.Gilliam@Sun.COM #endif
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		p0 = mi->mi_msinfo[n].msi_p0;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		if (p0 != -1)
1980Sstevel@tonic-gate 			(void) printf("%3d ", p0);
1990Sstevel@tonic-gate 		else
2000Sstevel@tonic-gate 			(void) printf("  - ");
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		(void) printf("  %d  ", mi->mi_rev);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		mi->mi_name[MODMAXNAMELEN - 1] = '\0';
2050Sstevel@tonic-gate 		mi->mi_msinfo[n].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		if (wide) {
2080Sstevel@tonic-gate 			(void) printf("%s (%s)\n", mi->mi_name,
2090Sstevel@tonic-gate 			    mi->mi_msinfo[n].msi_linkinfo);
2100Sstevel@tonic-gate 		} else {
2110Sstevel@tonic-gate 			/* snprintf(3c) will always append a null character */
2120Sstevel@tonic-gate 			(void) snprintf(namebuf, sizeof (namebuf), "%s (%s)",
2130Sstevel@tonic-gate 			    mi->mi_name, mi->mi_msinfo[n].msi_linkinfo);
2140Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
2150Sstevel@tonic-gate 			(void) printf("%.43s\n", namebuf);
2160Sstevel@tonic-gate #else
2170Sstevel@tonic-gate 			(void) printf("%.51s\n", namebuf);
2180Sstevel@tonic-gate #endif
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static void
usage()2240Sstevel@tonic-gate usage()
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	fatal("usage:  modinfo [-w] [-c] [-i module-id]\n");
2270Sstevel@tonic-gate }
228