1a79fe607SDoug Rabson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4a79fe607SDoug Rabson * Copyright (c) 1997 Doug Rabson
5a79fe607SDoug Rabson * All rights reserved.
6a79fe607SDoug Rabson *
7a79fe607SDoug Rabson * Redistribution and use in source and binary forms, with or without
8a79fe607SDoug Rabson * modification, are permitted provided that the following conditions
9a79fe607SDoug Rabson * are met:
10a79fe607SDoug Rabson * 1. Redistributions of source code must retain the above copyright
11a79fe607SDoug Rabson * notice, this list of conditions and the following disclaimer.
12a79fe607SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
13a79fe607SDoug Rabson * notice, this list of conditions and the following disclaimer in the
14a79fe607SDoug Rabson * documentation and/or other materials provided with the distribution.
15a79fe607SDoug Rabson *
16a79fe607SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a79fe607SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a79fe607SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a79fe607SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a79fe607SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a79fe607SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a79fe607SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a79fe607SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a79fe607SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a79fe607SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a79fe607SDoug Rabson * SUCH DAMAGE.
27a79fe607SDoug Rabson */
28a79fe607SDoug Rabson
29a79fe607SDoug Rabson #include <sys/types.h>
30a79fe607SDoug Rabson #include <sys/param.h>
31a79fe607SDoug Rabson #include <sys/module.h>
32a79fe607SDoug Rabson #include <sys/linker.h>
33a79fe607SDoug Rabson
3483237efcSHans Petter Selasky #include <err.h>
3583237efcSHans Petter Selasky #include <libutil.h>
3683237efcSHans Petter Selasky #include <stdio.h>
3783237efcSHans Petter Selasky #include <stdlib.h>
3883237efcSHans Petter Selasky #include <strings.h>
3983237efcSHans Petter Selasky #include <unistd.h>
4083237efcSHans Petter Selasky
4183237efcSHans Petter Selasky #define PTR_WIDTH ((int)(sizeof(void *) * 2 + 2))
4283237efcSHans Petter Selasky
4383237efcSHans Petter Selasky static void printmod(int);
4483237efcSHans Petter Selasky static void printfile(int, int, int);
4583237efcSHans Petter Selasky static void usage(void) __dead2;
46fb2ef615SMarcel Moolenaar
47637733b1SJulian Elischer static int showdata = 0;
48637733b1SJulian Elischer
492830148aSJohn-Mark Gurney static void
printmod(int modid)502830148aSJohn-Mark Gurney printmod(int modid)
51a79fe607SDoug Rabson {
52a79fe607SDoug Rabson struct module_stat stat;
53a79fe607SDoug Rabson
54637733b1SJulian Elischer bzero(&stat, sizeof(stat));
55a79fe607SDoug Rabson stat.version = sizeof(struct module_stat);
5683237efcSHans Petter Selasky if (modstat(modid, &stat) < 0) {
5757e78ffbSPhilippe Charnier warn("can't stat module id %d", modid);
5883237efcSHans Petter Selasky return;
5983237efcSHans Petter Selasky }
6083237efcSHans Petter Selasky if (showdata) {
61337c8143SHans Petter Selasky printf("\t\t%3d %s (%d, %u, 0x%lx)\n", stat.id,
62337c8143SHans Petter Selasky stat.name, stat.data.intval, stat.data.uintval,
63337c8143SHans Petter Selasky stat.data.ulongval);
6483237efcSHans Petter Selasky } else
65294b242dSMichael Tuexen printf("\t\t%3d %s\n", stat.id, stat.name);
66a79fe607SDoug Rabson }
67a79fe607SDoug Rabson
6835e3987fSKonstantin Belousov static void
printfile(int fileid,int verbose,int humanized)691b18fc10SEdward Tomasz Napierala printfile(int fileid, int verbose, int humanized)
70a79fe607SDoug Rabson {
71a79fe607SDoug Rabson struct kld_file_stat stat;
72a79fe607SDoug Rabson int modid;
731b18fc10SEdward Tomasz Napierala char buf[5];
74a79fe607SDoug Rabson
75a79fe607SDoug Rabson stat.version = sizeof(struct kld_file_stat);
7683237efcSHans Petter Selasky if (kldstat(fileid, &stat) < 0)
7735e3987fSKonstantin Belousov err(1, "can't stat file id %d", fileid);
781b18fc10SEdward Tomasz Napierala if (humanized) {
791b18fc10SEdward Tomasz Napierala humanize_number(buf, sizeof(buf), stat.size,
801b18fc10SEdward Tomasz Napierala "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
811b18fc10SEdward Tomasz Napierala
826130efe4SMichael Tuexen printf("%2d %4d %*p %5s %s",
8383237efcSHans Petter Selasky stat.id, stat.refs, PTR_WIDTH, stat.address,
84337c8143SHans Petter Selasky buf, stat.name);
851b18fc10SEdward Tomasz Napierala } else {
866130efe4SMichael Tuexen printf("%2d %4d %*p %8zx %s",
8783237efcSHans Petter Selasky stat.id, stat.refs, PTR_WIDTH, stat.address,
88337c8143SHans Petter Selasky stat.size, stat.name);
891b18fc10SEdward Tomasz Napierala }
90a79fe607SDoug Rabson
91a79fe607SDoug Rabson if (verbose) {
9297a7a569SAndrew Thompson printf(" (%s)\n", stat.pathname);
93a79fe607SDoug Rabson printf("\tContains modules:\n");
9461c3dd35SPaul Saab printf("\t\t Id Name\n");
95337c8143SHans Petter Selasky for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid))
96a79fe607SDoug Rabson printmod(modid);
9797a7a569SAndrew Thompson } else
9897a7a569SAndrew Thompson printf("\n");
99a79fe607SDoug Rabson }
100a79fe607SDoug Rabson
10183237efcSHans Petter Selasky static void __dead2
usage(void)1022830148aSJohn-Mark Gurney usage(void)
103a79fe607SDoug Rabson {
10483237efcSHans Petter Selasky fprintf(stderr, "usage: %1$s [-dhqv] [-i id] [-n filename]\n"
10583237efcSHans Petter Selasky " %1$s [-dq] [-m modname]\n", getprogname());
106a79fe607SDoug Rabson exit(1);
107a79fe607SDoug Rabson }
108a79fe607SDoug Rabson
1092830148aSJohn-Mark Gurney int
main(int argc,char * argv[])11083237efcSHans Petter Selasky main(int argc, char *argv[])
111a79fe607SDoug Rabson {
11283237efcSHans Petter Selasky struct module_stat stat;
1131b18fc10SEdward Tomasz Napierala int humanized = 0;
114a79fe607SDoug Rabson int verbose = 0;
115a79fe607SDoug Rabson int fileid = 0;
11690217cddSPawel Jakub Dawidek int quiet = 0;
11783237efcSHans Petter Selasky int c, modid;
1187f107457SJohan Karlsson char *filename = NULL;
1195d6d6d38SMax Khon char *modname = NULL;
1209c4c393aSJuli Mallett char *p;
121a79fe607SDoug Rabson
12283237efcSHans Petter Selasky while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1) {
123a79fe607SDoug Rabson switch (c) {
124637733b1SJulian Elischer case 'd':
125637733b1SJulian Elischer showdata = 1;
126637733b1SJulian Elischer break;
1271b18fc10SEdward Tomasz Napierala case 'h':
1281b18fc10SEdward Tomasz Napierala humanized = 1;
1291b18fc10SEdward Tomasz Napierala break;
130a79fe607SDoug Rabson case 'i':
1319c4c393aSJuli Mallett fileid = (int)strtoul(optarg, &p, 10);
1329c4c393aSJuli Mallett if (*p != '\0')
1339c4c393aSJuli Mallett usage();
134a79fe607SDoug Rabson break;
1355d6d6d38SMax Khon case 'm':
1365d6d6d38SMax Khon modname = optarg;
1375d6d6d38SMax Khon break;
138a79fe607SDoug Rabson case 'n':
139a79fe607SDoug Rabson filename = optarg;
140a79fe607SDoug Rabson break;
14190217cddSPawel Jakub Dawidek case 'q':
14290217cddSPawel Jakub Dawidek quiet = 1;
14390217cddSPawel Jakub Dawidek break;
1442830148aSJohn-Mark Gurney case 'v':
1452830148aSJohn-Mark Gurney verbose = 1;
1462830148aSJohn-Mark Gurney break;
147a79fe607SDoug Rabson default:
148a79fe607SDoug Rabson usage();
149a79fe607SDoug Rabson }
15083237efcSHans Petter Selasky }
151a79fe607SDoug Rabson argc -= optind;
152a79fe607SDoug Rabson argv += optind;
153a79fe607SDoug Rabson
154a79fe607SDoug Rabson if (argc != 0)
155a79fe607SDoug Rabson usage();
156a79fe607SDoug Rabson
1575d6d6d38SMax Khon if (modname != NULL) {
15890217cddSPawel Jakub Dawidek if ((modid = modfind(modname)) < 0) {
15990217cddSPawel Jakub Dawidek if (!quiet)
16090217cddSPawel Jakub Dawidek warn("can't find module %s", modname);
16183237efcSHans Petter Selasky return (1);
16283237efcSHans Petter Selasky } else if (quiet)
16383237efcSHans Petter Selasky return (0);
1645d6d6d38SMax Khon
1655d6d6d38SMax Khon stat.version = sizeof(struct module_stat);
1665d6d6d38SMax Khon if (modstat(modid, &stat) < 0)
1675d6d6d38SMax Khon warn("can't stat module id %d", modid);
1685d6d6d38SMax Khon else {
169637733b1SJulian Elischer if (showdata) {
170637733b1SJulian Elischer printf("Id Refs Name data..(int, uint, ulong)\n");
171337c8143SHans Petter Selasky printf("%3d %4d %s (%d, %u, 0x%lx)\n",
172337c8143SHans Petter Selasky stat.id, stat.refs, stat.name,
173337c8143SHans Petter Selasky stat.data.intval, stat.data.uintval,
174337c8143SHans Petter Selasky stat.data.ulongval);
175637733b1SJulian Elischer } else {
1765d6d6d38SMax Khon printf("Id Refs Name\n");
177337c8143SHans Petter Selasky printf("%3d %4d %s\n", stat.id, stat.refs,
178337c8143SHans Petter Selasky stat.name);
1795d6d6d38SMax Khon }
180637733b1SJulian Elischer }
1815d6d6d38SMax Khon
18283237efcSHans Petter Selasky return (0);
1835d6d6d38SMax Khon }
1845d6d6d38SMax Khon
1857f107457SJohan Karlsson if (filename != NULL) {
1863e8cf495SBaptiste Daroussin if ((fileid = kldfind(filename)) < 0) {
1873e8cf495SBaptiste Daroussin if (!quiet)
1883e8cf495SBaptiste Daroussin warn("can't find file %s", filename);
18983237efcSHans Petter Selasky return (1);
19083237efcSHans Petter Selasky } else if (quiet)
19183237efcSHans Petter Selasky return (0);
192a79fe607SDoug Rabson }
193a79fe607SDoug Rabson
19483237efcSHans Petter Selasky if (humanized) {
19583237efcSHans Petter Selasky printf("Id Refs Address%*c %5s Name\n", PTR_WIDTH - 7,
196337c8143SHans Petter Selasky ' ', "Size");
19783237efcSHans Petter Selasky } else {
19883237efcSHans Petter Selasky printf("Id Refs Address%*c %8s Name\n", PTR_WIDTH - 7,
199337c8143SHans Petter Selasky ' ', "Size");
20083237efcSHans Petter Selasky }
2017f107457SJohan Karlsson if (fileid != 0)
2021b18fc10SEdward Tomasz Napierala printfile(fileid, verbose, humanized);
203a79fe607SDoug Rabson else
204a79fe607SDoug Rabson for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
2051b18fc10SEdward Tomasz Napierala printfile(fileid, verbose, humanized);
206a79fe607SDoug Rabson
20783237efcSHans Petter Selasky return (0);
208a79fe607SDoug Rabson }
209