kldstat.c revision 186398
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 186398 2008-12-22 19:52:21Z thompsa $"); 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 40129866Sdwmalone#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 4154130Smarcel 4230627Sjmgstatic void 4330627Sjmgprintmod(int modid) 4425540Sdfr{ 4525540Sdfr struct module_stat stat; 4625540Sdfr 4725540Sdfr stat.version = sizeof(struct module_stat); 4825540Sdfr if (modstat(modid, &stat) < 0) 4932270Scharnier warn("can't stat module id %d", modid); 5025540Sdfr else 5162327Sps 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) 6132270Scharnier warn("can't stat file id %d", fileid); 6225540Sdfr else 63186398Sthompsa printf("%2d %4d %p %-8jx %s", 64113933Sjohan stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 65186398Sthompsa stat.name); 6625540Sdfr 6725540Sdfr if (verbose) { 68186398Sthompsa printf(" (%s)\n", stat.pathname); 6925540Sdfr printf("\tContains modules:\n"); 7062327Sps printf("\t\tId Name\n"); 7125540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 7225540Sdfr modid = modfnext(modid)) 7325540Sdfr printmod(modid); 74186398Sthompsa } else 75186398Sthompsa printf("\n"); 7625540Sdfr} 7725540Sdfr 7825540Sdfrstatic void 7930627Sjmgusage(void) 8025540Sdfr{ 81146465Sru fprintf(stderr, "usage: kldstat [-v] [-i id] [-n filename]\n"); 82150509Spjd fprintf(stderr, " kldstat [-q] [-m modname]\n"); 8325540Sdfr exit(1); 8425540Sdfr} 8525540Sdfr 8630627Sjmgint 8730627Sjmgmain(int argc, char** argv) 8825540Sdfr{ 8925540Sdfr int c; 9025540Sdfr int verbose = 0; 9125540Sdfr int fileid = 0; 92150497Spjd int quiet = 0; 93113936Sjohan char* filename = NULL; 94145861Sfjoe char* modname = NULL; 95127004Sjmallett char* p; 9625540Sdfr 97150497Spjd while ((c = getopt(argc, argv, "i:m:n:qv")) != -1) 9825540Sdfr switch (c) { 9925540Sdfr case 'i': 100127004Sjmallett fileid = (int)strtoul(optarg, &p, 10); 101127004Sjmallett if (*p != '\0') 102127004Sjmallett usage(); 10325540Sdfr break; 104145861Sfjoe case 'm': 105145861Sfjoe modname = optarg; 106145861Sfjoe break; 10725540Sdfr case 'n': 10825540Sdfr filename = optarg; 10925540Sdfr break; 110150497Spjd case 'q': 111150497Spjd quiet = 1; 112150497Spjd break; 11330627Sjmg case 'v': 11430627Sjmg verbose = 1; 11530627Sjmg break; 11625540Sdfr default: 11725540Sdfr usage(); 11825540Sdfr } 11925540Sdfr argc -= optind; 12025540Sdfr argv += optind; 12125540Sdfr 12225540Sdfr if (argc != 0) 12325540Sdfr usage(); 12425540Sdfr 125145861Sfjoe if (modname != NULL) { 126145861Sfjoe int modid; 127145861Sfjoe struct module_stat stat; 128145861Sfjoe 129150497Spjd if ((modid = modfind(modname)) < 0) { 130150497Spjd if (!quiet) 131150497Spjd warn("can't find module %s", modname); 132150497Spjd return 1; 133150497Spjd } else if (quiet) { 134150497Spjd return 0; 135150497Spjd } 136145861Sfjoe 137145861Sfjoe stat.version = sizeof(struct module_stat); 138145861Sfjoe if (modstat(modid, &stat) < 0) 139145861Sfjoe warn("can't stat module id %d", modid); 140145861Sfjoe else { 141145861Sfjoe printf("Id Refs Name\n"); 142145861Sfjoe printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); 143145861Sfjoe } 144145861Sfjoe 145145861Sfjoe return 0; 146145861Sfjoe } 147145861Sfjoe 148113936Sjohan if (filename != NULL) { 14930627Sjmg if ((fileid = kldfind(filename)) < 0) 15032270Scharnier err(1, "can't find file %s", filename); 15125540Sdfr } 15225540Sdfr 15354130Smarcel printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 154113936Sjohan if (fileid != 0) 15525540Sdfr printfile(fileid, verbose); 15625540Sdfr else 15725540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 15825540Sdfr printfile(fileid, verbose); 15925540Sdfr 16025540Sdfr return 0; 16125540Sdfr} 162