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%2d %s (%d, %u, 0x%lx)\n", stat.id, stat.name,
59 stat.data.intval, stat.data.uintval, stat.data.ulongval);
61 printf("\t\t%2d %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, stat.address, buf, stat.name);
83 printf("%2d %4d %p %-8zx %s",
84 stat.id, stat.refs, stat.address, stat.size, stat.name);
89 printf(" (%s)\n", stat.pathname);
90 printf("\tContains modules:\n");
91 printf("\t\tId Name\n");
92 for (modid = kldfirstmod(fileid); modid > 0;
93 modid = modfnext(modid))
102 fprintf(stderr, "usage: kldstat [-d] [-h] [-q] [-v] [-i id] [-n filename]\n");
103 fprintf(stderr, " kldstat [-d] [-q] [-m modname]\n");
108 main(int argc, char** argv)
115 char* filename = NULL;
116 char* modname = NULL;
119 while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1)
128 fileid = (int)strtoul(optarg, &p, 10);
153 if (modname != NULL) {
155 struct module_stat stat;
157 if ((modid = modfind(modname)) < 0) {
159 warn("can't find module %s", modname);
165 stat.version = sizeof(struct module_stat);
166 if (modstat(modid, &stat) < 0)
167 warn("can't stat module id %d", modid);
170 printf("Id Refs Name data..(int, uint, ulong)\n");
171 printf("%3d %4d %s (%d, %u, 0x%lx)\n", stat.id, stat.refs, stat.name,
172 stat.data.intval, stat.data.uintval, stat.data.ulongval);
174 printf("Id Refs Name\n");
175 printf("%3d %4d %s\n", stat.id, stat.refs, stat.name);
182 if (filename != NULL) {
183 if ((fileid = kldfind(filename)) < 0) {
185 warn("can't find file %s", filename);
193 printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
195 printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
197 printfile(fileid, verbose, humanized);
199 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
200 printfile(fileid, verbose, humanized);