xref: /freebsd/sbin/kldstat/kldstat.c (revision 57e78ffb1638a3c292714f0164478cf59bb50bd4)
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 
2757e78ffbSPhilippe Charnier #ifndef lint
2857e78ffbSPhilippe Charnier static const char rcsid[] =
2957e78ffbSPhilippe Charnier 	"$Id$";
3057e78ffbSPhilippe Charnier #endif /* not lint */
3157e78ffbSPhilippe Charnier 
3223e200d5SJohn-Mark Gurney #include <err.h>
33a79fe607SDoug Rabson #include <stdio.h>
3423e200d5SJohn-Mark Gurney #include <stdlib.h>
35a79fe607SDoug Rabson #include <unistd.h>
36a79fe607SDoug Rabson #include <sys/types.h>
37a79fe607SDoug Rabson #include <sys/param.h>
38a79fe607SDoug Rabson #include <sys/module.h>
39a79fe607SDoug Rabson #include <sys/linker.h>
40a79fe607SDoug Rabson 
412830148aSJohn-Mark Gurney static void
422830148aSJohn-Mark Gurney printmod(int modid)
43a79fe607SDoug Rabson {
44a79fe607SDoug Rabson     struct module_stat stat;
45a79fe607SDoug Rabson 
46a79fe607SDoug Rabson     stat.version = sizeof(struct module_stat);
47a79fe607SDoug Rabson     if (modstat(modid, &stat) < 0)
4857e78ffbSPhilippe Charnier 	warn("can't stat module id %d", modid);
49a79fe607SDoug Rabson     else
50a79fe607SDoug Rabson 	printf("\t\t%2d %s\n", stat.id, stat.name);
51a79fe607SDoug Rabson }
52a79fe607SDoug Rabson 
53a79fe607SDoug Rabson static void printfile(int fileid, int verbose)
54a79fe607SDoug Rabson {
55a79fe607SDoug Rabson     struct kld_file_stat stat;
56a79fe607SDoug Rabson     int modid;
57a79fe607SDoug Rabson 
58a79fe607SDoug Rabson     stat.version = sizeof(struct kld_file_stat);
59a79fe607SDoug Rabson     if (kldstat(fileid, &stat) < 0)
6057e78ffbSPhilippe Charnier 	warn("can't stat file id %d", fileid);
61a79fe607SDoug Rabson     else
6223e200d5SJohn-Mark Gurney 	printf("%2d %4d %p %-8x %s\n",
63a79fe607SDoug Rabson 	       stat.id, stat.refs, stat.address, stat.size, stat.name);
64a79fe607SDoug Rabson 
65a79fe607SDoug Rabson     if (verbose) {
66a79fe607SDoug Rabson 	printf("\tContains modules:\n");
67a79fe607SDoug Rabson 	printf("\t\tId Name\n");
68a79fe607SDoug Rabson 	for (modid = kldfirstmod(fileid); modid > 0;
69a79fe607SDoug Rabson 	     modid = modfnext(modid))
70a79fe607SDoug Rabson 	    printmod(modid);
71a79fe607SDoug Rabson     }
72a79fe607SDoug Rabson }
73a79fe607SDoug Rabson 
74a79fe607SDoug Rabson static void
752830148aSJohn-Mark Gurney usage(void)
76a79fe607SDoug Rabson {
7757e78ffbSPhilippe Charnier     fprintf(stderr, "usage: kldstat [-v] [-i id] [-n name]\n");
78a79fe607SDoug Rabson     exit(1);
79a79fe607SDoug Rabson }
80a79fe607SDoug Rabson 
812830148aSJohn-Mark Gurney int
822830148aSJohn-Mark Gurney main(int argc, char** argv)
83a79fe607SDoug Rabson {
84a79fe607SDoug Rabson     int c;
85a79fe607SDoug Rabson     int verbose = 0;
86a79fe607SDoug Rabson     int fileid = 0;
87a79fe607SDoug Rabson     char* filename = 0;
88a79fe607SDoug Rabson 
892830148aSJohn-Mark Gurney     while ((c = getopt(argc, argv, "i:n:v")) != -1)
90a79fe607SDoug Rabson 	switch (c) {
91a79fe607SDoug Rabson 	case 'i':
92a79fe607SDoug Rabson 	    fileid = atoi(optarg);
93a79fe607SDoug Rabson 	    break;
94a79fe607SDoug Rabson 	case 'n':
95a79fe607SDoug Rabson 	    filename = optarg;
96a79fe607SDoug Rabson 	    break;
972830148aSJohn-Mark Gurney 	case 'v':
982830148aSJohn-Mark Gurney 	    verbose = 1;
992830148aSJohn-Mark Gurney 	    break;
100a79fe607SDoug Rabson 	default:
101a79fe607SDoug Rabson 	    usage();
102a79fe607SDoug Rabson 	}
103a79fe607SDoug Rabson     argc -= optind;
104a79fe607SDoug Rabson     argv += optind;
105a79fe607SDoug Rabson 
106a79fe607SDoug Rabson     if (argc != 0)
107a79fe607SDoug Rabson 	usage();
108a79fe607SDoug Rabson 
109a79fe607SDoug Rabson     if (filename) {
1102830148aSJohn-Mark Gurney 	if ((fileid = kldfind(filename)) < 0)
11157e78ffbSPhilippe Charnier 	    err(1, "can't find file %s", filename);
112a79fe607SDoug Rabson     }
113a79fe607SDoug Rabson 
114a79fe607SDoug Rabson     printf("Id Refs Address  Size     Name\n");
115a79fe607SDoug Rabson     if (fileid)
116a79fe607SDoug Rabson 	printfile(fileid, verbose);
117a79fe607SDoug Rabson     else
118a79fe607SDoug Rabson 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
119a79fe607SDoug Rabson 	    printfile(fileid, verbose);
120a79fe607SDoug Rabson 
121a79fe607SDoug Rabson     return 0;
122a79fe607SDoug Rabson }
123