kldstat.c revision 30627
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: kldstat.c,v 1.2 1997/10/19 11:15:44 jmg Exp $ 2732270Scharnier */ 2832270Scharnier 2950476Speter#include <err.h> 3032270Scharnier#include <stdio.h> 3132270Scharnier#include <stdlib.h> 3230573Sjmg#include <unistd.h> 3325540Sdfr#include <sys/types.h> 3430573Sjmg#include <sys/param.h> 3525540Sdfr#include <sys/module.h> 3625540Sdfr#include <sys/linker.h> 3725540Sdfr 3825540Sdfrstatic void 3925540Sdfrprintmod(int modid) 4025540Sdfr{ 4154130Smarcel struct module_stat stat; 4254130Smarcel 4354130Smarcel stat.version = sizeof(struct module_stat); 4454130Smarcel if (modstat(modid, &stat) < 0) 4554130Smarcel warn("Can't stat module id %d", modid); 4654130Smarcel else 4730627Sjmg printf("\t\t%2d %s\n", stat.id, stat.name); 4830627Sjmg} 4925540Sdfr 5025540Sdfrstatic void printfile(int fileid, int verbose) 5125540Sdfr{ 5225540Sdfr struct kld_file_stat stat; 5325540Sdfr int modid; 5432270Scharnier 5525540Sdfr stat.version = sizeof(struct kld_file_stat); 5625540Sdfr if (kldstat(fileid, &stat) < 0) 5725540Sdfr warn("Can't stat file id %d", fileid); 5825540Sdfr else 5925540Sdfr printf("%2d %4d %p %-8x %s\n", 6025540Sdfr stat.id, stat.refs, stat.address, stat.size, stat.name); 6125540Sdfr 6225540Sdfr if (verbose) { 6325540Sdfr printf("\tContains modules:\n"); 6425540Sdfr printf("\t\tId Name\n"); 6525540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 6632270Scharnier modid = modfnext(modid)) 6725540Sdfr printmod(modid); 6830573Sjmg } 6925540Sdfr} 7025540Sdfr 7125540Sdfrstatic void 7225540Sdfrusage(void) 7325540Sdfr{ 7425540Sdfr fprintf(stderr, "usage: kldstat [-v]\n"); 7525540Sdfr exit(1); 7625540Sdfr} 7725540Sdfr 7825540Sdfrint 7925540Sdfrmain(int argc, char** argv) 8025540Sdfr{ 8130627Sjmg int c; 8225540Sdfr int verbose = 0; 8332270Scharnier int fileid = 0; 8425540Sdfr char* filename = 0; 8525540Sdfr 8625540Sdfr while ((c = getopt(argc, argv, "i:n:v")) != -1) 8730627Sjmg switch (c) { 8830627Sjmg case 'i': 8925540Sdfr fileid = atoi(optarg); 9025540Sdfr break; 9125540Sdfr case 'n': 9225540Sdfr filename = optarg; 9325540Sdfr break; 9425540Sdfr case 'v': 9530627Sjmg verbose = 1; 9625540Sdfr break; 9725540Sdfr default: 9825540Sdfr usage(); 9925540Sdfr } 10025540Sdfr argc -= optind; 10125540Sdfr argv += optind; 10225540Sdfr 10330627Sjmg if (argc != 0) 10430627Sjmg usage(); 10530627Sjmg 10625540Sdfr if (filename) { 10725540Sdfr if ((fileid = kldfind(filename)) < 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)) 11630627Sjmg printfile(fileid, verbose); 11732270Scharnier 11825540Sdfr return 0; 11925540Sdfr} 12054130Smarcel