xref: /freebsd/sbin/kldstat/kldstat.c (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/module.h>
35 #include <sys/linker.h>
36 
37 #include <err.h>
38 #include <libutil.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 
44 #define	PTR_WIDTH ((int)(sizeof(void *) * 2 + 2))
45 
46 static void printmod(int);
47 static void printfile(int, int, int);
48 static void usage(void) __dead2;
49 
50 static int showdata = 0;
51 
52 static void
53 printmod(int modid)
54 {
55 	struct module_stat stat;
56 
57 	bzero(&stat, sizeof(stat));
58 	stat.version = sizeof(struct module_stat);
59 	if (modstat(modid, &stat) < 0) {
60 		warn("can't stat module id %d", modid);
61 		return;
62 	}
63 	if (showdata) {
64 		printf("\t\t%3d %s (%d, %u, 0x%lx)\n", stat.id,
65 		    stat.name, stat.data.intval, stat.data.uintval,
66 		    stat.data.ulongval);
67 	} else
68 		printf("\t\t%3d %s\n", stat.id, stat.name);
69 }
70 
71 static void
72 printfile(int fileid, int verbose, int humanized)
73 {
74 	struct kld_file_stat stat;
75 	int modid;
76 	char buf[5];
77 
78 	stat.version = sizeof(struct kld_file_stat);
79 	if (kldstat(fileid, &stat) < 0)
80 		err(1, "can't stat file id %d", fileid);
81 	if (humanized) {
82 		humanize_number(buf, sizeof(buf), stat.size,
83 		    "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
84 
85 		printf("%2d %4d %*p %5s %s",
86 		    stat.id, stat.refs, PTR_WIDTH, stat.address,
87 		    buf, stat.name);
88 	} else {
89 		printf("%2d %4d %*p %8zx %s",
90 		    stat.id, stat.refs, PTR_WIDTH, stat.address,
91 		    stat.size, stat.name);
92 	}
93 
94 	if (verbose) {
95 		printf(" (%s)\n", stat.pathname);
96 		printf("\tContains modules:\n");
97 		printf("\t\t Id Name\n");
98 		for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid))
99 			printmod(modid);
100 	} else
101 		printf("\n");
102 }
103 
104 static void __dead2
105 usage(void)
106 {
107 	fprintf(stderr, "usage: %1$s [-dhqv] [-i id] [-n filename]\n"
108 	    "       %1$s [-dq] [-m modname]\n", getprogname());
109 	exit(1);
110 }
111 
112 int
113 main(int argc, char *argv[])
114 {
115 	struct module_stat stat;
116 	int humanized = 0;
117 	int verbose = 0;
118 	int fileid = 0;
119 	int quiet = 0;
120 	int c, modid;
121 	char *filename = NULL;
122 	char *modname = NULL;
123 	char *p;
124 
125 	while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1) {
126 		switch (c) {
127 		case 'd':
128 			showdata = 1;
129 			break;
130 		case 'h':
131 			humanized = 1;
132 			break;
133 		case 'i':
134 			fileid = (int)strtoul(optarg, &p, 10);
135 			if (*p != '\0')
136 				usage();
137 			break;
138 		case 'm':
139 			modname = optarg;
140 			break;
141 		case 'n':
142 			filename = optarg;
143 			break;
144 		case 'q':
145 			quiet = 1;
146 			break;
147 		case 'v':
148 			verbose = 1;
149 			break;
150 		default:
151 			usage();
152 		}
153 	}
154 	argc -= optind;
155 	argv += optind;
156 
157 	if (argc != 0)
158 		usage();
159 
160 	if (modname != NULL) {
161 		if ((modid = modfind(modname)) < 0) {
162 			if (!quiet)
163 				warn("can't find module %s", modname);
164 			return (1);
165 		} else if (quiet)
166 			return (0);
167 
168 		stat.version = sizeof(struct module_stat);
169 		if (modstat(modid, &stat) < 0)
170 			warn("can't stat module id %d", modid);
171 		else {
172 			if (showdata) {
173 				printf("Id  Refs Name data..(int, uint, ulong)\n");
174 				printf("%3d %4d %s (%d, %u, 0x%lx)\n",
175 				    stat.id, stat.refs, stat.name,
176 				    stat.data.intval, stat.data.uintval,
177 				    stat.data.ulongval);
178 			} else {
179 				printf("Id  Refs Name\n");
180 				printf("%3d %4d %s\n", stat.id, stat.refs,
181 				    stat.name);
182 			}
183 		}
184 
185 		return (0);
186 	}
187 
188 	if (filename != NULL) {
189 		if ((fileid = kldfind(filename)) < 0) {
190 			if (!quiet)
191 				warn("can't find file %s", filename);
192 			return (1);
193 		} else if (quiet)
194 			return (0);
195 	}
196 
197 	if (humanized) {
198 		printf("Id Refs Address%*c %5s Name\n", PTR_WIDTH - 7,
199 		    ' ', "Size");
200 	} else {
201 		printf("Id Refs Address%*c %8s Name\n", PTR_WIDTH - 7,
202 		    ' ', "Size");
203 	}
204 	if (fileid != 0)
205 		printfile(fileid, verbose, humanized);
206 	else
207 		for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
208 			printfile(fileid, verbose, humanized);
209 
210 	return (0);
211 }
212