xref: /freebsd/sbin/kldstat/kldstat.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <err.h>
33 #include <libutil.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/module.h>
40 #include <sys/linker.h>
41 #include <strings.h>
42 
43 #define	POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
44 
45 static int showdata = 0;
46 
47 static void
48 printmod(int modid)
49 {
50 	struct module_stat stat;
51 
52 	bzero(&stat, sizeof(stat));
53 	stat.version = sizeof(struct module_stat);
54 	if (modstat(modid, &stat) < 0)
55 		warn("can't stat module id %d", modid);
56 	else {
57 		if (showdata)
58 			printf("\t\t%3d %s (%d, %u, 0x%lx)\n", stat.id,
59 			    stat.name, stat.data.intval, stat.data.uintval,
60 			    stat.data.ulongval);
61 		else
62 			printf("\t\t%3d %s\n", stat.id, stat.name);
63 	}
64 }
65 
66 static void
67 printfile(int fileid, int verbose, int humanized)
68 {
69 	struct kld_file_stat stat;
70 	int modid;
71 	char buf[5];
72 
73 	stat.version = sizeof(struct kld_file_stat);
74 	if (kldstat(fileid, &stat) < 0) {
75 		err(1, "can't stat file id %d", fileid);
76 	} else {
77 		if (humanized) {
78 			humanize_number(buf, sizeof(buf), stat.size,
79 			    "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
80 
81 			printf("%2d %4d %*p %5s %s",
82 			    stat.id, stat.refs, POINTER_WIDTH, stat.address,
83 			    buf, stat.name);
84 		} else {
85 			printf("%2d %4d %*p %8zx %s",
86 			    stat.id, stat.refs, POINTER_WIDTH, stat.address,
87 			    stat.size, stat.name);
88 		}
89 	}
90 
91 	if (verbose) {
92 		printf(" (%s)\n", stat.pathname);
93 		printf("\tContains modules:\n");
94 		printf("\t\t Id Name\n");
95 		for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid))
96 			printmod(modid);
97 	} else
98 		printf("\n");
99 }
100 
101 static void
102 usage(void)
103 {
104 	fprintf(stderr, "usage: kldstat [-d] [-h] [-q] [-v] [-i id] [-n filename]\n");
105 	fprintf(stderr, "       kldstat [-d] [-q] [-m modname]\n");
106 	exit(1);
107 }
108 
109 int
110 main(int argc, char** argv)
111 {
112 	int c;
113 	int humanized = 0;
114 	int verbose = 0;
115 	int fileid = 0;
116 	int quiet = 0;
117 	char* filename = NULL;
118 	char* modname = NULL;
119 	char* p;
120 
121 	while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1)
122 		switch (c) {
123 		case 'd':
124 			showdata = 1;
125 			break;
126 		case 'h':
127 			humanized = 1;
128 			break;
129 		case 'i':
130 			fileid = (int)strtoul(optarg, &p, 10);
131 			if (*p != '\0')
132 				usage();
133 			break;
134 		case 'm':
135 			modname = optarg;
136 			break;
137 		case 'n':
138 			filename = optarg;
139 			break;
140 		case 'q':
141 			quiet = 1;
142 			break;
143 		case 'v':
144 			verbose = 1;
145 			break;
146 		default:
147 			usage();
148 		}
149 	argc -= optind;
150 	argv += optind;
151 
152 	if (argc != 0)
153 		usage();
154 
155 	if (modname != NULL) {
156 		int modid;
157 		struct module_stat stat;
158 
159 		if ((modid = modfind(modname)) < 0) {
160 			if (!quiet)
161 				warn("can't find module %s", modname);
162 			return 1;
163 		} else if (quiet) {
164 			return 0;
165 		}
166 
167 		stat.version = sizeof(struct module_stat);
168 		if (modstat(modid, &stat) < 0)
169 			warn("can't stat module id %d", modid);
170 		else {
171 			if (showdata) {
172 				printf("Id  Refs Name data..(int, uint, ulong)\n");
173 				printf("%3d %4d %s (%d, %u, 0x%lx)\n",
174 				    stat.id, stat.refs, stat.name,
175 				    stat.data.intval, stat.data.uintval,
176 				    stat.data.ulongval);
177 			} else {
178 				printf("Id  Refs Name\n");
179 				printf("%3d %4d %s\n", stat.id, stat.refs,
180 				    stat.name);
181 			}
182 		}
183 
184 		return 0;
185 	}
186 
187 	if (filename != NULL) {
188 		if ((fileid = kldfind(filename)) < 0) {
189 			if (!quiet)
190 				warn("can't find file %s", filename);
191 			return 1;
192 		} else if (quiet) {
193 			return 0;
194 		}
195 	}
196 
197 	if (humanized)
198 		printf("Id Refs Address%*c %5s Name\n", POINTER_WIDTH - 7,
199 		    ' ', "Size");
200 	else
201 		printf("Id Refs Address%*c %8s Name\n", POINTER_WIDTH - 7,
202 		    ' ', "Size");
203 	if (fileid != 0)
204 		printfile(fileid, verbose, humanized);
205 	else
206 		for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
207 			printfile(fileid, verbose, humanized);
208 
209 	return 0;
210 }
211