2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/module.h>
40 #include <sys/linker.h>
43 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
45 static int showdata = 0;
50 struct module_stat stat;
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);
58 printf("\t\t%3d %s (%d, %u, 0x%lx)\n", stat.id, stat.name,
59 stat.data.intval, stat.data.uintval, stat.data.ulongval);
61 printf("\t\t%3d %s\n", stat.id, stat.name);
66 printfile(int fileid, int verbose, int humanized)
68 struct kld_file_stat stat;
72 stat.version = sizeof(struct kld_file_stat);
73 if (kldstat(fileid, &stat) < 0) {
74 err(1, "can't stat file id %d", fileid);
77 humanize_number(buf, sizeof(buf), stat.size,
78 "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
80 printf("%2d %4d %*p %5s %s",
81 stat.id, stat.refs, POINTER_WIDTH, stat.address, buf,
84 printf("%2d %4d %*p %8zx %s",
85 stat.id, stat.refs, POINTER_WIDTH, stat.address, stat.size,
91 printf(" (%s)\n", stat.pathname);
92 printf("\tContains modules:\n");
93 printf("\t\t Id Name\n");
94 for (modid = kldfirstmod(fileid); modid > 0;
95 modid = modfnext(modid))
104 fprintf(stderr, "usage: kldstat [-d] [-h] [-q] [-v] [-i id] [-n filename]\n");
105 fprintf(stderr, " kldstat [-d] [-q] [-m modname]\n");
110 main(int argc, char** argv)
117 char* filename = NULL;
118 char* modname = NULL;
121 while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1)
130 fileid = (int)strtoul(optarg, &p, 10);
155 if (modname != NULL) {
157 struct module_stat stat;
159 if ((modid = modfind(modname)) < 0) {
161 warn("can't find module %s", modname);
167 stat.version = sizeof(struct module_stat);
168 if (modstat(modid, &stat) < 0)
169 warn("can't stat module id %d", modid);
172 printf("Id Refs Name data..(int, uint, ulong)\n");
173 printf("%3d %4d %s (%d, %u, 0x%lx)\n", stat.id, stat.refs, stat.name,
174 stat.data.intval, stat.data.uintval, stat.data.ulongval);
176 printf("Id Refs Name\n");
177 printf("%3d %4d %s\n", stat.id, stat.refs, stat.name);
184 if (filename != NULL) {
185 if ((fileid = kldfind(filename)) < 0) {
187 warn("can't find file %s", filename);
195 printf("Id Refs Address%*c %5s Name\n", POINTER_WIDTH - 7, ' ', "Size");
197 printf("Id Refs Address%*c %8s Name\n", POINTER_WIDTH - 7, ' ', "Size");
199 printfile(fileid, verbose, humanized);
201 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
202 printfile(fileid, verbose, humanized);