xref: /freebsd/sbin/kldstat/kldstat.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
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, stat.name,
59 	        stat.data.intval, stat.data.uintval, stat.data.ulongval);
60 	} else {
61 		printf("\t\t%3d %s\n", stat.id, stat.name);
62 	}
63 }
64 
65 static void
66 printfile(int fileid, int verbose, int humanized)
67 {
68     struct kld_file_stat stat;
69     int modid;
70     char buf[5];
71 
72     stat.version = sizeof(struct kld_file_stat);
73     if (kldstat(fileid, &stat) < 0) {
74 	err(1, "can't stat file id %d", fileid);
75     } else {
76 	if (humanized) {
77 		humanize_number(buf, sizeof(buf), stat.size,
78 		    "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
79 
80 		printf("%2d %4d %*p %5s %s",
81 		    stat.id, stat.refs, POINTER_WIDTH, stat.address, buf,
82 		    stat.name);
83 	} else {
84 		printf("%2d %4d %*p %8zx %s",
85 		    stat.id, stat.refs, POINTER_WIDTH, stat.address, stat.size,
86 		    stat.name);
87 	}
88     }
89 
90     if (verbose) {
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))
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", stat.id, stat.refs, stat.name,
174 		        stat.data.intval, stat.data.uintval, stat.data.ulongval);
175 		} else {
176 		    printf("Id  Refs Name\n");
177 		    printf("%3d %4d %s\n", stat.id, stat.refs, stat.name);
178 		}
179 	}
180 
181 	return 0;
182     }
183 
184     if (filename != NULL) {
185 	if ((fileid = kldfind(filename)) < 0) {
186 	    if (!quiet)
187 		warn("can't find file %s", filename);
188 	    return 1;
189 	} else if (quiet) {
190 	    return 0;
191 	}
192     }
193 
194     if (humanized)
195 	    printf("Id Refs Address%*c %5s Name\n", POINTER_WIDTH - 7, ' ', "Size");
196     else
197 	    printf("Id Refs Address%*c %8s Name\n", POINTER_WIDTH - 7, ' ', "Size");
198     if (fileid != 0)
199 	printfile(fileid, verbose, humanized);
200     else
201 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
202 	    printfile(fileid, verbose, humanized);
203 
204     return 0;
205 }
206