kldstat.c revision 150509
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 150509 2005-09-24 08:20:45Z pjd $"); 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 63113933Sjohan printf("%2d %4d %p %-8jx %s\n", 64113933Sjohan stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 65113933Sjohan stat.name); 6625540Sdfr 6725540Sdfr if (verbose) { 6825540Sdfr printf("\tContains modules:\n"); 6962327Sps printf("\t\tId Name\n"); 7025540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 7125540Sdfr modid = modfnext(modid)) 7225540Sdfr printmod(modid); 7325540Sdfr } 7425540Sdfr} 7525540Sdfr 7625540Sdfrstatic void 7730627Sjmgusage(void) 7825540Sdfr{ 79146465Sru fprintf(stderr, "usage: kldstat [-v] [-i id] [-n filename]\n"); 80150509Spjd fprintf(stderr, " kldstat [-q] [-m modname]\n"); 8125540Sdfr exit(1); 8225540Sdfr} 8325540Sdfr 8430627Sjmgint 8530627Sjmgmain(int argc, char** argv) 8625540Sdfr{ 8725540Sdfr int c; 8825540Sdfr int verbose = 0; 8925540Sdfr int fileid = 0; 90150497Spjd int quiet = 0; 91113936Sjohan char* filename = NULL; 92145861Sfjoe char* modname = NULL; 93127004Sjmallett char* p; 9425540Sdfr 95150497Spjd while ((c = getopt(argc, argv, "i:m:n:qv")) != -1) 9625540Sdfr switch (c) { 9725540Sdfr case 'i': 98127004Sjmallett fileid = (int)strtoul(optarg, &p, 10); 99127004Sjmallett if (*p != '\0') 100127004Sjmallett usage(); 10125540Sdfr break; 102145861Sfjoe case 'm': 103145861Sfjoe modname = optarg; 104145861Sfjoe break; 10525540Sdfr case 'n': 10625540Sdfr filename = optarg; 10725540Sdfr break; 108150497Spjd case 'q': 109150497Spjd quiet = 1; 110150497Spjd break; 11130627Sjmg case 'v': 11230627Sjmg verbose = 1; 11330627Sjmg break; 11425540Sdfr default: 11525540Sdfr usage(); 11625540Sdfr } 11725540Sdfr argc -= optind; 11825540Sdfr argv += optind; 11925540Sdfr 12025540Sdfr if (argc != 0) 12125540Sdfr usage(); 12225540Sdfr 123145861Sfjoe if (modname != NULL) { 124145861Sfjoe int modid; 125145861Sfjoe struct module_stat stat; 126145861Sfjoe 127150497Spjd if ((modid = modfind(modname)) < 0) { 128150497Spjd if (!quiet) 129150497Spjd warn("can't find module %s", modname); 130150497Spjd return 1; 131150497Spjd } else if (quiet) { 132150497Spjd return 0; 133150497Spjd } 134145861Sfjoe 135145861Sfjoe stat.version = sizeof(struct module_stat); 136145861Sfjoe if (modstat(modid, &stat) < 0) 137145861Sfjoe warn("can't stat module id %d", modid); 138145861Sfjoe else { 139145861Sfjoe printf("Id Refs Name\n"); 140145861Sfjoe printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); 141145861Sfjoe } 142145861Sfjoe 143145861Sfjoe return 0; 144145861Sfjoe } 145145861Sfjoe 146113936Sjohan if (filename != NULL) { 14730627Sjmg if ((fileid = kldfind(filename)) < 0) 14832270Scharnier err(1, "can't find file %s", filename); 14925540Sdfr } 15025540Sdfr 15154130Smarcel printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 152113936Sjohan if (fileid != 0) 15325540Sdfr printfile(fileid, verbose); 15425540Sdfr else 15525540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 15625540Sdfr printfile(fileid, verbose); 15725540Sdfr 15825540Sdfr return 0; 15925540Sdfr} 160