kldstat.c revision 127004
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 27114589Sobrien#include <sys/cdefs.h> 28114589Sobrien__FBSDID("$FreeBSD: head/sbin/kldstat/kldstat.c 127004 2004-03-15 03:41:07Z jmallett $"); 2932270Scharnier 3030573Sjmg#include <err.h> 31113933Sjohan#include <stdint.h> 3225540Sdfr#include <stdio.h> 3330573Sjmg#include <stdlib.h> 3425540Sdfr#include <unistd.h> 3525540Sdfr#include <sys/types.h> 3625540Sdfr#include <sys/param.h> 3725540Sdfr#include <sys/module.h> 3825540Sdfr#include <sys/linker.h> 3925540Sdfr 4054130Smarcel#if defined(__alpha__) 4154130Smarcel#define POINTER_WIDTH 18 4254130Smarcel#else 4354130Smarcel#define POINTER_WIDTH 10 4454130Smarcel#endif 4554130Smarcel 4630627Sjmgstatic void 4730627Sjmgprintmod(int modid) 4825540Sdfr{ 4925540Sdfr struct module_stat stat; 5025540Sdfr 5125540Sdfr stat.version = sizeof(struct module_stat); 5225540Sdfr if (modstat(modid, &stat) < 0) 5332270Scharnier warn("can't stat module id %d", modid); 5425540Sdfr else 5562327Sps printf("\t\t%2d %s\n", stat.id, stat.name); 5625540Sdfr} 5725540Sdfr 5825540Sdfrstatic void printfile(int fileid, int verbose) 5925540Sdfr{ 6025540Sdfr struct kld_file_stat stat; 6125540Sdfr int modid; 6225540Sdfr 6325540Sdfr stat.version = sizeof(struct kld_file_stat); 6425540Sdfr if (kldstat(fileid, &stat) < 0) 6532270Scharnier warn("can't stat file id %d", fileid); 6625540Sdfr else 67113933Sjohan printf("%2d %4d %p %-8jx %s\n", 68113933Sjohan stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 69113933Sjohan stat.name); 7025540Sdfr 7125540Sdfr if (verbose) { 7225540Sdfr printf("\tContains modules:\n"); 7362327Sps printf("\t\tId Name\n"); 7425540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 7525540Sdfr modid = modfnext(modid)) 7625540Sdfr printmod(modid); 7725540Sdfr } 7825540Sdfr} 7925540Sdfr 8025540Sdfrstatic void 8130627Sjmgusage(void) 8225540Sdfr{ 8332270Scharnier fprintf(stderr, "usage: kldstat [-v] [-i id] [-n name]\n"); 8425540Sdfr exit(1); 8525540Sdfr} 8625540Sdfr 8730627Sjmgint 8830627Sjmgmain(int argc, char** argv) 8925540Sdfr{ 9025540Sdfr int c; 9125540Sdfr int verbose = 0; 9225540Sdfr int fileid = 0; 93113936Sjohan char* filename = NULL; 94127004Sjmallett char* p; 9525540Sdfr 9630627Sjmg while ((c = getopt(argc, argv, "i:n:v")) != -1) 9725540Sdfr switch (c) { 9825540Sdfr case 'i': 99127004Sjmallett fileid = (int)strtoul(optarg, &p, 10); 100127004Sjmallett if (*p != '\0') 101127004Sjmallett usage(); 10225540Sdfr break; 10325540Sdfr case 'n': 10425540Sdfr filename = optarg; 10525540Sdfr break; 10630627Sjmg case 'v': 10730627Sjmg verbose = 1; 10830627Sjmg break; 10925540Sdfr default: 11025540Sdfr usage(); 11125540Sdfr } 11225540Sdfr argc -= optind; 11325540Sdfr argv += optind; 11425540Sdfr 11525540Sdfr if (argc != 0) 11625540Sdfr usage(); 11725540Sdfr 118113936Sjohan if (filename != NULL) { 11930627Sjmg if ((fileid = kldfind(filename)) < 0) 12032270Scharnier err(1, "can't find file %s", filename); 12125540Sdfr } 12225540Sdfr 12354130Smarcel printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 124113936Sjohan if (fileid != 0) 12525540Sdfr printfile(fileid, verbose); 12625540Sdfr else 12725540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 12825540Sdfr printfile(fileid, verbose); 12925540Sdfr 13025540Sdfr return 0; 13125540Sdfr} 132