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