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/types.h>
30*0Sstevel@tonic-gate #include <inttypes.h>
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <stddef.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <sys/modctl.h>
36*0Sstevel@tonic-gate #include <sys/errno.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static int wide;
39*0Sstevel@tonic-gate static int count = 0;
40*0Sstevel@tonic-gate static int first_mod = 1;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * When printing module load addresses on 32-bit kernels, the 8 hex
44*0Sstevel@tonic-gate  * character field width is obviously adequate. On sparcv9 kernels
45*0Sstevel@tonic-gate  * solely by virtue of the choice of code model enabled by the separate
46*0Sstevel@tonic-gate  * kernel context, the text addresses are currently in the lower 4G
47*0Sstevel@tonic-gate  * address range, and so -still- fit in 8 hex characters.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * However, amd64 kernels live at the top of the 64-bit address space, and
50*0Sstevel@tonic-gate  * so as to be honest about the addresses (and since this is a tool for
51*0Sstevel@tonic-gate  * humans to parse), we have to print out a 16 hex character address.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * We assume that we will print out all 16 hex characters on future
54*0Sstevel@tonic-gate  * 64-bit kernel ports too.
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static const char header[] =
57*0Sstevel@tonic-gate 	" Id "
58*0Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
59*0Sstevel@tonic-gate 	"        "
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate 	"Loadaddr   Size Info Rev Module Name\n";
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static char *cheader  =
64*0Sstevel@tonic-gate 	" Id    Loadcnt Module Name                            State\n";
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate static void usage();
68*0Sstevel@tonic-gate static void print_info(struct modinfo *mi);
69*0Sstevel@tonic-gate static void print_cinfo(struct modinfo *mi);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * These functions are in modsubr.c
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate void fatal(char *fmt, ...);
75*0Sstevel@tonic-gate void error(char *fmt, ...);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * Display information of all loaded modules
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate int
81*0Sstevel@tonic-gate main(int argc, char *argv[])
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	struct modinfo modinfo;
84*0Sstevel@tonic-gate 	int info_all = 1;
85*0Sstevel@tonic-gate 	int id;
86*0Sstevel@tonic-gate 	int opt;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	id = -1;	/* assume we're getting all loaded modules */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i:wc")) != EOF) {
91*0Sstevel@tonic-gate 		switch (opt) {
92*0Sstevel@tonic-gate 		case 'i':
93*0Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &id) != 1)
94*0Sstevel@tonic-gate 				fatal("Invalid id %s\n", optarg);
95*0Sstevel@tonic-gate 			if (id == -1)
96*0Sstevel@tonic-gate 				id = 0;
97*0Sstevel@tonic-gate 			info_all = 0;
98*0Sstevel@tonic-gate 			break;
99*0Sstevel@tonic-gate 		case 'w':
100*0Sstevel@tonic-gate 			wide++;
101*0Sstevel@tonic-gate 			break;
102*0Sstevel@tonic-gate 		case 'c':
103*0Sstevel@tonic-gate 			count++;
104*0Sstevel@tonic-gate 			break;
105*0Sstevel@tonic-gate 		case '?':
106*0Sstevel@tonic-gate 		default:
107*0Sstevel@tonic-gate 			usage();
108*0Sstevel@tonic-gate 			break;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/*
114*0Sstevel@tonic-gate 	 * Next id of -1 means we're getting info on all modules.
115*0Sstevel@tonic-gate 	 */
116*0Sstevel@tonic-gate 	modinfo.mi_id = modinfo.mi_nextid = id;
117*0Sstevel@tonic-gate 	modinfo.mi_info = (info_all) ? MI_INFO_ALL : MI_INFO_ONE;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (count)
120*0Sstevel@tonic-gate 		modinfo.mi_info |= MI_INFO_CNT;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	do {
123*0Sstevel@tonic-gate 		/*
124*0Sstevel@tonic-gate 		 * Get module information.
125*0Sstevel@tonic-gate 		 * If modinfo.mi_nextid == -1, get info about the
126*0Sstevel@tonic-gate 		 * next installed module with id > "id."
127*0Sstevel@tonic-gate 		 * Otherwise, get info about the module with id == "id."
128*0Sstevel@tonic-gate 		 */
129*0Sstevel@tonic-gate 		if (modctl(MODINFO, id, &modinfo) < 0) {
130*0Sstevel@tonic-gate 			if (!info_all)
131*0Sstevel@tonic-gate 				error("can't get module information");
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		if (first_mod) {
136*0Sstevel@tonic-gate 			first_mod = 0;
137*0Sstevel@tonic-gate 			(void) printf(count ? cheader : header);
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 		if (count)
140*0Sstevel@tonic-gate 			print_cinfo(&modinfo);
141*0Sstevel@tonic-gate 		else
142*0Sstevel@tonic-gate 			print_info(&modinfo);
143*0Sstevel@tonic-gate 		/*
144*0Sstevel@tonic-gate 		 * If we're getting info about all modules, the next one
145*0Sstevel@tonic-gate 		 * we want is the one with an id greater than this one.
146*0Sstevel@tonic-gate 		 */
147*0Sstevel@tonic-gate 		id = modinfo.mi_id;
148*0Sstevel@tonic-gate 	} while (info_all);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	return (0);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate  * Display loadcounts.
155*0Sstevel@tonic-gate  */
156*0Sstevel@tonic-gate static void
157*0Sstevel@tonic-gate print_cinfo(struct modinfo *mi)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	(void) printf("%3d %10d %-32s", mi->mi_id, mi->mi_loadcnt, mi->mi_name);
160*0Sstevel@tonic-gate 	(void) printf(" %s/%s\n",
161*0Sstevel@tonic-gate 		    mi->mi_state & MI_LOADED ? "LOADED" : "UNLOADED",
162*0Sstevel@tonic-gate 		    mi->mi_state & MI_INSTALLED ? "INSTALLED" : "UNINSTALLED");
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate  * Display info about a loaded module.
167*0Sstevel@tonic-gate  *
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate static void
170*0Sstevel@tonic-gate print_info(struct modinfo *mi)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	int n, p0;
173*0Sstevel@tonic-gate 	char namebuf[256];
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	for (n = 0; n < MODMAXLINK; n++) {
176*0Sstevel@tonic-gate 		if (n > 0 && mi->mi_msinfo[n].msi_linkinfo[0] == '\0')
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		(void) printf("%3d ", mi->mi_id);
180*0Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
181*0Sstevel@tonic-gate 		(void) printf("%16lx ", (uintptr_t)mi->mi_base);
182*0Sstevel@tonic-gate #else
183*0Sstevel@tonic-gate 		(void) printf("%8lx ", (uintptr_t)mi->mi_base);
184*0Sstevel@tonic-gate #endif
185*0Sstevel@tonic-gate 		(void) printf("%6x ", mi->mi_size);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		p0 = mi->mi_msinfo[n].msi_p0;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		if (p0 != -1)
190*0Sstevel@tonic-gate 			(void) printf("%3d ", p0);
191*0Sstevel@tonic-gate 		else
192*0Sstevel@tonic-gate 			(void) printf("  - ");
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 		(void) printf("  %d  ", mi->mi_rev);
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		mi->mi_name[MODMAXNAMELEN - 1] = '\0';
197*0Sstevel@tonic-gate 		mi->mi_msinfo[n].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if (wide) {
200*0Sstevel@tonic-gate 			(void) printf("%s (%s)\n", mi->mi_name,
201*0Sstevel@tonic-gate 			    mi->mi_msinfo[n].msi_linkinfo);
202*0Sstevel@tonic-gate 		} else {
203*0Sstevel@tonic-gate 			/* snprintf(3c) will always append a null character */
204*0Sstevel@tonic-gate 			(void) snprintf(namebuf, sizeof (namebuf), "%s (%s)",
205*0Sstevel@tonic-gate 			    mi->mi_name, mi->mi_msinfo[n].msi_linkinfo);
206*0Sstevel@tonic-gate #if defined(_LP64) && !defined(__sparcv9)
207*0Sstevel@tonic-gate 			(void) printf("%.43s\n", namebuf);
208*0Sstevel@tonic-gate #else
209*0Sstevel@tonic-gate 			(void) printf("%.51s\n", namebuf);
210*0Sstevel@tonic-gate #endif
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static void
216*0Sstevel@tonic-gate usage()
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	fatal("usage:  modinfo [-w] [-c] [-i module-id]\n");
219*0Sstevel@tonic-gate }
220