kldstat.c revision 30573
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 * 2630573Sjmg * $Id: kldstat.c,v 1.1 1997/05/07 18:19:54 dfr Exp $ 2725540Sdfr */ 2825540Sdfr 2930573Sjmg#include <err.h> 3025540Sdfr#include <stdio.h> 3130573Sjmg#include <stdlib.h> 3225540Sdfr#include <unistd.h> 3325540Sdfr#include <sys/types.h> 3425540Sdfr#include <sys/param.h> 3525540Sdfr#include <sys/module.h> 3625540Sdfr#include <sys/linker.h> 3725540Sdfr 3830573Sjmgextern char *optarg; 3930573Sjmgextern int optind; 4030573Sjmg 4125540Sdfrstatic char* progname; 4225540Sdfr 4325540Sdfrstatic void printmod(int modid) 4425540Sdfr{ 4525540Sdfr struct module_stat stat; 4625540Sdfr 4725540Sdfr stat.version = sizeof(struct module_stat); 4825540Sdfr if (modstat(modid, &stat) < 0) 4930573Sjmg warn("Can't stat module id %d", modid); 5025540Sdfr else 5125540Sdfr printf("\t\t%2d %s\n", stat.id, stat.name); 5225540Sdfr} 5325540Sdfr 5425540Sdfrstatic void printfile(int fileid, int verbose) 5525540Sdfr{ 5625540Sdfr struct kld_file_stat stat; 5725540Sdfr int modid; 5825540Sdfr 5925540Sdfr stat.version = sizeof(struct kld_file_stat); 6025540Sdfr if (kldstat(fileid, &stat) < 0) 6130573Sjmg warn("Can't stat file id %d", fileid); 6225540Sdfr else 6330573Sjmg printf("%2d %4d %p %-8x %s\n", 6425540Sdfr stat.id, stat.refs, stat.address, stat.size, stat.name); 6525540Sdfr 6625540Sdfr if (verbose) { 6725540Sdfr printf("\tContains modules:\n"); 6825540Sdfr printf("\t\tId Name\n"); 6925540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 7025540Sdfr modid = modfnext(modid)) 7125540Sdfr printmod(modid); 7225540Sdfr } 7325540Sdfr} 7425540Sdfr 7525540Sdfrstatic void 7625540Sdfrusage() 7725540Sdfr{ 7825540Sdfr fprintf(stderr, "usage: %s [-v]\n", progname); 7925540Sdfr exit(1); 8025540Sdfr} 8125540Sdfr 8225540Sdfrint main(int argc, char** argv) 8325540Sdfr{ 8425540Sdfr int c; 8525540Sdfr int verbose = 0; 8625540Sdfr int fileid = 0; 8725540Sdfr char* filename = 0; 8825540Sdfr 8925540Sdfr progname = argv[0]; 9025540Sdfr while ((c = getopt(argc, argv, "vi:n:")) != -1) 9125540Sdfr switch (c) { 9225540Sdfr case 'v': 9325540Sdfr verbose = 1; 9425540Sdfr break; 9525540Sdfr case 'i': 9625540Sdfr fileid = atoi(optarg); 9725540Sdfr break; 9825540Sdfr case 'n': 9925540Sdfr filename = optarg; 10025540Sdfr break; 10125540Sdfr default: 10225540Sdfr usage(); 10325540Sdfr } 10425540Sdfr argc -= optind; 10525540Sdfr argv += optind; 10625540Sdfr 10725540Sdfr if (argc != 0) 10825540Sdfr usage(); 10925540Sdfr 11025540Sdfr if (filename) { 11125540Sdfr fileid = kldfind(filename); 11225540Sdfr if (fileid < 0) 11325540Sdfr err(1, "Can't find file %s", filename); 11425540Sdfr } 11525540Sdfr 11625540Sdfr printf("Id Refs Address Size Name\n"); 11725540Sdfr if (fileid) 11825540Sdfr printfile(fileid, verbose); 11925540Sdfr else 12025540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 12125540Sdfr printfile(fileid, verbose); 12225540Sdfr 12325540Sdfr return 0; 12425540Sdfr} 125