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