xref: /freebsd/sbin/kldstat/kldstat.c (revision 2830148a29f76ad83de6693dad770c5c8240c55e)
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  *
262830148aSJohn-Mark Gurney  *	$Id: kldstat.c,v 1.2 1997/10/19 11:15:44 jmg Exp $
27a79fe607SDoug Rabson  */
28a79fe607SDoug Rabson 
2923e200d5SJohn-Mark Gurney #include <err.h>
30a79fe607SDoug Rabson #include <stdio.h>
3123e200d5SJohn-Mark Gurney #include <stdlib.h>
32a79fe607SDoug Rabson #include <unistd.h>
33a79fe607SDoug Rabson #include <sys/types.h>
34a79fe607SDoug Rabson #include <sys/param.h>
35a79fe607SDoug Rabson #include <sys/module.h>
36a79fe607SDoug Rabson #include <sys/linker.h>
37a79fe607SDoug Rabson 
382830148aSJohn-Mark Gurney static void
392830148aSJohn-Mark Gurney printmod(int modid)
40a79fe607SDoug Rabson {
41a79fe607SDoug Rabson     struct module_stat stat;
42a79fe607SDoug Rabson 
43a79fe607SDoug Rabson     stat.version = sizeof(struct module_stat);
44a79fe607SDoug Rabson     if (modstat(modid, &stat) < 0)
4523e200d5SJohn-Mark Gurney 	warn("Can't stat module id %d", modid);
46a79fe607SDoug Rabson     else
47a79fe607SDoug Rabson 	printf("\t\t%2d %s\n", stat.id, stat.name);
48a79fe607SDoug Rabson }
49a79fe607SDoug Rabson 
50a79fe607SDoug Rabson static void printfile(int fileid, int verbose)
51a79fe607SDoug Rabson {
52a79fe607SDoug Rabson     struct kld_file_stat stat;
53a79fe607SDoug Rabson     int modid;
54a79fe607SDoug Rabson 
55a79fe607SDoug Rabson     stat.version = sizeof(struct kld_file_stat);
56a79fe607SDoug Rabson     if (kldstat(fileid, &stat) < 0)
5723e200d5SJohn-Mark Gurney 	warn("Can't stat file id %d", fileid);
58a79fe607SDoug Rabson     else
5923e200d5SJohn-Mark Gurney 	printf("%2d %4d %p %-8x %s\n",
60a79fe607SDoug Rabson 	       stat.id, stat.refs, stat.address, stat.size, stat.name);
61a79fe607SDoug Rabson 
62a79fe607SDoug Rabson     if (verbose) {
63a79fe607SDoug Rabson 	printf("\tContains modules:\n");
64a79fe607SDoug Rabson 	printf("\t\tId Name\n");
65a79fe607SDoug Rabson 	for (modid = kldfirstmod(fileid); modid > 0;
66a79fe607SDoug Rabson 	     modid = modfnext(modid))
67a79fe607SDoug Rabson 	    printmod(modid);
68a79fe607SDoug Rabson     }
69a79fe607SDoug Rabson }
70a79fe607SDoug Rabson 
71a79fe607SDoug Rabson static void
722830148aSJohn-Mark Gurney usage(void)
73a79fe607SDoug Rabson {
742830148aSJohn-Mark Gurney     fprintf(stderr, "usage: kldstat [-v]\n");
75a79fe607SDoug Rabson     exit(1);
76a79fe607SDoug Rabson }
77a79fe607SDoug Rabson 
782830148aSJohn-Mark Gurney int
792830148aSJohn-Mark Gurney main(int argc, char** argv)
80a79fe607SDoug Rabson {
81a79fe607SDoug Rabson     int c;
82a79fe607SDoug Rabson     int verbose = 0;
83a79fe607SDoug Rabson     int fileid = 0;
84a79fe607SDoug Rabson     char* filename = 0;
85a79fe607SDoug Rabson 
862830148aSJohn-Mark Gurney     while ((c = getopt(argc, argv, "i:n:v")) != -1)
87a79fe607SDoug Rabson 	switch (c) {
88a79fe607SDoug Rabson 	case 'i':
89a79fe607SDoug Rabson 	    fileid = atoi(optarg);
90a79fe607SDoug Rabson 	    break;
91a79fe607SDoug Rabson 	case 'n':
92a79fe607SDoug Rabson 	    filename = optarg;
93a79fe607SDoug Rabson 	    break;
942830148aSJohn-Mark Gurney 	case 'v':
952830148aSJohn-Mark Gurney 	    verbose = 1;
962830148aSJohn-Mark Gurney 	    break;
97a79fe607SDoug Rabson 	default:
98a79fe607SDoug Rabson 	    usage();
99a79fe607SDoug Rabson 	}
100a79fe607SDoug Rabson     argc -= optind;
101a79fe607SDoug Rabson     argv += optind;
102a79fe607SDoug Rabson 
103a79fe607SDoug Rabson     if (argc != 0)
104a79fe607SDoug Rabson 	usage();
105a79fe607SDoug Rabson 
106a79fe607SDoug Rabson     if (filename) {
1072830148aSJohn-Mark Gurney 	if ((fileid = kldfind(filename)) < 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