kldstat.c revision 25540
125540Sdfr/*- 225540Sdfr * Copyright (c) 1997 Doug Rabson 325540Sdfr * All rights reserved. 425540Sdfr * 525540Sdfr * Redistribution and use in source and binary forms, with or without 625540Sdfr * modification, are permitted provided that the following conditions 725540Sdfr * are met: 825540Sdfr * 1. Redistributions of source code must retain the above copyright 925540Sdfr * notice, this list of conditions and the following disclaimer. 1025540Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1125540Sdfr * notice, this list of conditions and the following disclaimer in the 1225540Sdfr * documentation and/or other materials provided with the distribution. 1325540Sdfr * 1425540Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1525540Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1625540Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1725540Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1825540Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1925540Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2025540Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2125540Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2225540Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2325540Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2425540Sdfr * SUCH DAMAGE. 2525540Sdfr * 2625540Sdfr * $Id$ 2725540Sdfr */ 2825540Sdfr 2925540Sdfr#include <stdio.h> 3025540Sdfr#include <unistd.h> 3125540Sdfr#include <sys/types.h> 3225540Sdfr#include <sys/param.h> 3325540Sdfr#include <sys/module.h> 3425540Sdfr#include <sys/linker.h> 3525540Sdfr 3625540Sdfrstatic char* progname; 3725540Sdfr 3825540Sdfrstatic void printmod(int modid) 3925540Sdfr{ 4025540Sdfr struct module_stat stat; 4125540Sdfr 4225540Sdfr stat.version = sizeof(struct module_stat); 4325540Sdfr if (modstat(modid, &stat) < 0) 4425540Sdfr warn(1, "Can't state module"); 4525540Sdfr else 4625540Sdfr printf("\t\t%2d %s\n", stat.id, stat.name); 4725540Sdfr} 4825540Sdfr 4925540Sdfrstatic void printfile(int fileid, int verbose) 5025540Sdfr{ 5125540Sdfr struct kld_file_stat stat; 5225540Sdfr int modid; 5325540Sdfr 5425540Sdfr stat.version = sizeof(struct kld_file_stat); 5525540Sdfr if (kldstat(fileid, &stat) < 0) 5625540Sdfr warn(1, "Can't stat file"); 5725540Sdfr else 5825540Sdfr printf("%2d %4d %-8x %-8x %s\n", 5925540Sdfr stat.id, stat.refs, stat.address, stat.size, stat.name); 6025540Sdfr 6125540Sdfr if (verbose) { 6225540Sdfr printf("\tContains modules:\n"); 6325540Sdfr printf("\t\tId Name\n"); 6425540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 6525540Sdfr modid = modfnext(modid)) 6625540Sdfr printmod(modid); 6725540Sdfr } 6825540Sdfr} 6925540Sdfr 7025540Sdfrstatic void 7125540Sdfrusage() 7225540Sdfr{ 7325540Sdfr fprintf(stderr, "usage: %s [-v]\n", progname); 7425540Sdfr exit(1); 7525540Sdfr} 7625540Sdfr 7725540Sdfrint main(int argc, char** argv) 7825540Sdfr{ 7925540Sdfr int c; 8025540Sdfr int verbose = 0; 8125540Sdfr int fileid = 0; 8225540Sdfr char* filename = 0; 8325540Sdfr 8425540Sdfr progname = argv[0]; 8525540Sdfr while ((c = getopt(argc, argv, "vi:n:")) != -1) 8625540Sdfr switch (c) { 8725540Sdfr case 'v': 8825540Sdfr verbose = 1; 8925540Sdfr break; 9025540Sdfr case 'i': 9125540Sdfr fileid = atoi(optarg); 9225540Sdfr break; 9325540Sdfr case 'n': 9425540Sdfr filename = optarg; 9525540Sdfr break; 9625540Sdfr default: 9725540Sdfr usage(); 9825540Sdfr } 9925540Sdfr argc -= optind; 10025540Sdfr argv += optind; 10125540Sdfr 10225540Sdfr if (argc != 0) 10325540Sdfr usage(); 10425540Sdfr 10525540Sdfr if (filename) { 10625540Sdfr fileid = kldfind(filename); 10725540Sdfr if (fileid < 0) 10825540Sdfr err(1, "Can't find file %s", filename); 10925540Sdfr } 11025540Sdfr 11125540Sdfr printf("Id Refs Address Size Name\n"); 11225540Sdfr if (fileid) 11325540Sdfr printfile(fileid, verbose); 11425540Sdfr else 11525540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 11625540Sdfr printfile(fileid, verbose); 11725540Sdfr 11825540Sdfr return 0; 11925540Sdfr} 120