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: releng/10.3/sbin/kldstat/kldstat.c 268903 2014-07-20 00:34:54Z bapt $"); 2932270Scharnier 3030573Sjmg#include <err.h> 3125540Sdfr#include <stdio.h> 3230573Sjmg#include <stdlib.h> 3325540Sdfr#include <unistd.h> 3425540Sdfr#include <sys/types.h> 3525540Sdfr#include <sys/param.h> 3625540Sdfr#include <sys/module.h> 3725540Sdfr#include <sys/linker.h> 3825540Sdfr 39129866Sdwmalone#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 4054130Smarcel 4130627Sjmgstatic void 4230627Sjmgprintmod(int modid) 4325540Sdfr{ 4425540Sdfr struct module_stat stat; 4525540Sdfr 4625540Sdfr stat.version = sizeof(struct module_stat); 4725540Sdfr if (modstat(modid, &stat) < 0) 4832270Scharnier warn("can't stat module id %d", modid); 4925540Sdfr else 5062327Sps printf("\t\t%2d %s\n", stat.id, stat.name); 5125540Sdfr} 5225540Sdfr 53215248Skibstatic void 54215248Skibprintfile(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) 61215248Skib err(1, "can't stat file id %d", fileid); 6225540Sdfr else 63207964Sbrueffer printf("%2d %4d %p %-8zx %s", 64207964Sbrueffer stat.id, stat.refs, stat.address, 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{ 81268903Sbapt fprintf(stderr, "usage: kldstat [-q] [-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) { 149268903Sbapt if ((fileid = kldfind(filename)) < 0) { 150268903Sbapt if (!quiet) 151268903Sbapt warn("can't find file %s", filename); 152268903Sbapt return 1; 153268903Sbapt } else if (quiet) { 154268903Sbapt return 0; 155268903Sbapt } 15625540Sdfr } 15725540Sdfr 15854130Smarcel printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 159113936Sjohan if (fileid != 0) 16025540Sdfr printfile(fileid, verbose); 16125540Sdfr else 16225540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 16325540Sdfr printfile(fileid, verbose); 16425540Sdfr 16525540Sdfr return 0; 16625540Sdfr} 167