xref: /freebsd/sbin/kldstat/kldstat.c (revision a79fe60755523cab06086c7a032a326f247b7424)
1a79fe607SDoug Rabson /*-
2a79fe607SDoug Rabson  * Copyright (c) 1997 Doug Rabson
3a79fe607SDoug Rabson  * All rights reserved.
4a79fe607SDoug Rabson  *
5a79fe607SDoug Rabson  * Redistribution and use in source and binary forms, with or without
6a79fe607SDoug Rabson  * modification, are permitted provided that the following conditions
7a79fe607SDoug Rabson  * are met:
8a79fe607SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9a79fe607SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10a79fe607SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11a79fe607SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12a79fe607SDoug Rabson  *    documentation and/or other materials provided with the distribution.
13a79fe607SDoug Rabson  *
14a79fe607SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a79fe607SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a79fe607SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a79fe607SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a79fe607SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a79fe607SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a79fe607SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a79fe607SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a79fe607SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a79fe607SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a79fe607SDoug Rabson  * SUCH DAMAGE.
25a79fe607SDoug Rabson  *
26a79fe607SDoug Rabson  *	$Id$
27a79fe607SDoug Rabson  */
28a79fe607SDoug Rabson 
29a79fe607SDoug Rabson #include <stdio.h>
30a79fe607SDoug Rabson #include <unistd.h>
31a79fe607SDoug Rabson #include <sys/types.h>
32a79fe607SDoug Rabson #include <sys/param.h>
33a79fe607SDoug Rabson #include <sys/module.h>
34a79fe607SDoug Rabson #include <sys/linker.h>
35a79fe607SDoug Rabson 
36a79fe607SDoug Rabson static char* progname;
37a79fe607SDoug Rabson 
38a79fe607SDoug Rabson static void printmod(int modid)
39a79fe607SDoug Rabson {
40a79fe607SDoug Rabson     struct module_stat stat;
41a79fe607SDoug Rabson 
42a79fe607SDoug Rabson     stat.version = sizeof(struct module_stat);
43a79fe607SDoug Rabson     if (modstat(modid, &stat) < 0)
44a79fe607SDoug Rabson 	warn(1, "Can't state module");
45a79fe607SDoug Rabson     else
46a79fe607SDoug Rabson 	printf("\t\t%2d %s\n", stat.id, stat.name);
47a79fe607SDoug Rabson }
48a79fe607SDoug Rabson 
49a79fe607SDoug Rabson static void printfile(int fileid, int verbose)
50a79fe607SDoug Rabson {
51a79fe607SDoug Rabson     struct kld_file_stat stat;
52a79fe607SDoug Rabson     int modid;
53a79fe607SDoug Rabson 
54a79fe607SDoug Rabson     stat.version = sizeof(struct kld_file_stat);
55a79fe607SDoug Rabson     if (kldstat(fileid, &stat) < 0)
56a79fe607SDoug Rabson 	warn(1, "Can't stat file");
57a79fe607SDoug Rabson     else
58a79fe607SDoug Rabson 	printf("%2d %4d %-8x %-8x %s\n",
59a79fe607SDoug Rabson 	       stat.id, stat.refs, stat.address, stat.size, stat.name);
60a79fe607SDoug Rabson 
61a79fe607SDoug Rabson     if (verbose) {
62a79fe607SDoug Rabson 	printf("\tContains modules:\n");
63a79fe607SDoug Rabson 	printf("\t\tId Name\n");
64a79fe607SDoug Rabson 	for (modid = kldfirstmod(fileid); modid > 0;
65a79fe607SDoug Rabson 	     modid = modfnext(modid))
66a79fe607SDoug Rabson 	    printmod(modid);
67a79fe607SDoug Rabson     }
68a79fe607SDoug Rabson }
69a79fe607SDoug Rabson 
70a79fe607SDoug Rabson static void
71a79fe607SDoug Rabson usage()
72a79fe607SDoug Rabson {
73a79fe607SDoug Rabson     fprintf(stderr, "usage: %s [-v]\n", progname);
74a79fe607SDoug Rabson     exit(1);
75a79fe607SDoug Rabson }
76a79fe607SDoug Rabson 
77a79fe607SDoug Rabson int main(int argc, char** argv)
78a79fe607SDoug Rabson {
79a79fe607SDoug Rabson     int c;
80a79fe607SDoug Rabson     int verbose = 0;
81a79fe607SDoug Rabson     int fileid = 0;
82a79fe607SDoug Rabson     char* filename = 0;
83a79fe607SDoug Rabson 
84a79fe607SDoug Rabson     progname = argv[0];
85a79fe607SDoug Rabson     while ((c = getopt(argc, argv, "vi:n:")) != -1)
86a79fe607SDoug Rabson 	switch (c) {
87a79fe607SDoug Rabson 	case 'v':
88a79fe607SDoug Rabson 	    verbose = 1;
89a79fe607SDoug Rabson 	    break;
90a79fe607SDoug Rabson 	case 'i':
91a79fe607SDoug Rabson 	    fileid = atoi(optarg);
92a79fe607SDoug Rabson 	    break;
93a79fe607SDoug Rabson 	case 'n':
94a79fe607SDoug Rabson 	    filename = optarg;
95a79fe607SDoug Rabson 	    break;
96a79fe607SDoug Rabson 	default:
97a79fe607SDoug Rabson 	    usage();
98a79fe607SDoug Rabson 	}
99a79fe607SDoug Rabson     argc -= optind;
100a79fe607SDoug Rabson     argv += optind;
101a79fe607SDoug Rabson 
102a79fe607SDoug Rabson     if (argc != 0)
103a79fe607SDoug Rabson 	usage();
104a79fe607SDoug Rabson 
105a79fe607SDoug Rabson     if (filename) {
106a79fe607SDoug Rabson 	fileid = kldfind(filename);
107a79fe607SDoug Rabson 	if (fileid < 0)
108a79fe607SDoug Rabson 	    err(1, "Can't find file %s", filename);
109a79fe607SDoug Rabson     }
110a79fe607SDoug Rabson 
111a79fe607SDoug Rabson     printf("Id Refs Address  Size     Name\n");
112a79fe607SDoug Rabson     if (fileid)
113a79fe607SDoug Rabson 	printfile(fileid, verbose);
114a79fe607SDoug Rabson     else
115a79fe607SDoug Rabson 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
116a79fe607SDoug Rabson 	    printfile(fileid, verbose);
117a79fe607SDoug Rabson 
118a79fe607SDoug Rabson     return 0;
119a79fe607SDoug Rabson }
120