1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/modctl.h>
#include <sys/errno.h>
static int wide;
static int count = 0;
static int first_mod = 1;
/*
* When printing module load addresses on 32-bit kernels, the 8 hex
* character field width is obviously adequate. On sparcv9 kernels
* solely by virtue of the choice of code model enabled by the separate
* kernel context, the text addresses are currently in the lower 4G
* address range, and so -still- fit in 8 hex characters.
*
* However, amd64 kernels live at the top of the 64-bit address space, and
* so as to be honest about the addresses (and since this is a tool for
* humans to parse), we have to print out a 16 hex character address.
*
* We assume that we will print out all 16 hex characters on future
* 64-bit kernel ports too.
*/
static const char header[] =
" Id "
#if defined(_LP64) && !defined(__sparcv9)
" "
#endif
"Loadaddr Size Info Rev Module Name\n";
static char *cheader =
" Id Loadcnt Module Name State\n";
static void usage();
static void print_info(struct modinfo *mi);
static void print_cinfo(struct modinfo *mi);
/*
* These functions are in modsubr.c
*/
void fatal(char *fmt, ...);
void error(char *fmt, ...);
/*
* Display information of all loaded modules
*/
int
main(int argc, char *argv[])
{
struct modinfo modinfo;
int info_all = 1;
int id;
int opt;
id = -1; /* assume we're getting all loaded modules */
while ((opt = getopt(argc, argv, "i:wc")) != EOF) {
switch (opt) {
case 'i':
if (sscanf(optarg, "%d", &id) != 1)
fatal("Invalid id %s\n", optarg);
if (id == -1)
id = 0;
info_all = 0;
break;
case 'w':
wide++;
break;
case 'c':
count++;
break;
case '?':
default:
usage();
break;
}
}
/*
* Next id of -1 means we're getting info on all modules.
*/
modinfo.mi_id = modinfo.mi_nextid = id;
modinfo.mi_info = (info_all) ? MI_INFO_ALL : MI_INFO_ONE;
if (count)
modinfo.mi_info |= MI_INFO_CNT;
do {
/*
* Get module information.
* If modinfo.mi_nextid == -1, get info about the
* next installed module with id > "id."
* Otherwise, get info about the module with id == "id."
*/
if (modctl(MODINFO, id, &modinfo) < 0) {
if (!info_all)
error("can't get module information");
break;
}
if (first_mod) {
first_mod = 0;
(void) printf(count ? cheader : header);
}
if (count)
print_cinfo(&modinfo);
else
print_info(&modinfo);
/*
* If we're getting info about all modules, the next one
* we want is the one with an id greater than this one.
*/
id = modinfo.mi_id;
} while (info_all);
return (0);
}
/*
* Display loadcounts.
*/
static void
print_cinfo(struct modinfo *mi)
{
(void) printf("%3d %10d %-32s", mi->mi_id, mi->mi_loadcnt, mi->mi_name);
(void) printf(" %s/%s\n",
mi->mi_state & MI_LOADED ? "LOADED" : "UNLOADED",
mi->mi_state & MI_INSTALLED ? "INSTALLED" : "UNINSTALLED");
}
/*
* Display info about a loaded module.
*
*/
static void
print_info(struct modinfo *mi)
{
int n, p0;
char namebuf[256];
for (n = 0; n < MODMAXLINK; n++) {
if (n > 0 && mi->mi_msinfo[n].msi_linkinfo[0] == '\0')
break;
(void) printf("%3d ", mi->mi_id);
#if defined(_LP64) && !defined(__sparcv9)
(void) printf("%16lx ", (uintptr_t)mi->mi_base);
#else
(void) printf("%8lx ", (uintptr_t)mi->mi_base);
#endif
(void) printf("%6x ", mi->mi_size);
p0 = mi->mi_msinfo[n].msi_p0;
if (p0 != -1)
(void) printf("%3d ", p0);
else
(void) printf(" - ");
(void) printf(" %d ", mi->mi_rev);
mi->mi_name[MODMAXNAMELEN - 1] = '\0';
mi->mi_msinfo[n].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
if (wide) {
(void) printf("%s (%s)\n", mi->mi_name,
mi->mi_msinfo[n].msi_linkinfo);
} else {
/* snprintf(3c) will always append a null character */
(void) snprintf(namebuf, sizeof (namebuf), "%s (%s)",
mi->mi_name, mi->mi_msinfo[n].msi_linkinfo);
#if defined(_LP64) && !defined(__sparcv9)
(void) printf("%.43s\n", namebuf);
#else
(void) printf("%.51s\n", namebuf);
#endif
}
}
}
static void
usage()
{
fatal("usage: modinfo [-w] [-c] [-i module-id]\n");
}
|