kldstat.c revision 215248
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 215248 2010-11-13 16:49:07Z kib $"); 2932270Scharnier 3030573Sjmg#include <err.h> 31113933Sjohan#include <stdio.h> 3225540Sdfr#include <stdlib.h> 3330573Sjmg#include <unistd.h> 3425540Sdfr#include <sys/types.h> 3525540Sdfr#include <sys/param.h> 3625540Sdfr#include <sys/module.h> 3725540Sdfr#include <sys/linker.h> 3825540Sdfr 3925540Sdfr#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 40129866Sdwmalone 4154130Smarcelstatic void 4230627Sjmgprintmod(int modid) 4330627Sjmg{ 4425540Sdfr struct module_stat stat; 4525540Sdfr 4625540Sdfr stat.version = sizeof(struct module_stat); 4725540Sdfr if (modstat(modid, &stat) < 0) 4825540Sdfr warn("can't stat module id %d", modid); 4932270Scharnier else 5025540Sdfr printf("\t\t%2d %s\n", stat.id, stat.name); 5162327Sps} 5225540Sdfr 5325540Sdfrstatic void 5425540Sdfrprintfile(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 err(1, "can't stat file id %d", fileid); 6225540Sdfr else 63113933Sjohan printf("%2d %4d %p %-8zx %s", 64113933Sjohan stat.id, stat.refs, stat.address, stat.size, 65113933Sjohan stat.name); 6625540Sdfr 6725540Sdfr if (verbose) { 6825540Sdfr printf(" (%s)\n", stat.pathname); 6962327Sps printf("\tContains modules:\n"); 7025540Sdfr printf("\t\tId Name\n"); 7125540Sdfr for (modid = kldfirstmod(fileid); modid > 0; 7225540Sdfr modid = modfnext(modid)) 7325540Sdfr printmod(modid); 7425540Sdfr } else 7525540Sdfr printf("\n"); 7625540Sdfr} 7730627Sjmg 7825540Sdfrstatic void 79146465Sruusage(void) 80146465Sru{ 8125540Sdfr fprintf(stderr, "usage: kldstat [-v] [-i id] [-n filename]\n"); 8225540Sdfr fprintf(stderr, " kldstat [-q] [-m modname]\n"); 8325540Sdfr exit(1); 8430627Sjmg} 8530627Sjmg 8625540Sdfrint 8725540Sdfrmain(int argc, char** argv) 8825540Sdfr{ 8925540Sdfr int c; 90150497Spjd int verbose = 0; 91113936Sjohan int fileid = 0; 92145861Sfjoe int quiet = 0; 93127004Sjmallett char* filename = NULL; 9425540Sdfr char* modname = NULL; 95150497Spjd char* p; 9625540Sdfr 9725540Sdfr while ((c = getopt(argc, argv, "i:m:n:qv")) != -1) 98127004Sjmallett switch (c) { 99127004Sjmallett case 'i': 100127004Sjmallett fileid = (int)strtoul(optarg, &p, 10); 10125540Sdfr if (*p != '\0') 102145861Sfjoe usage(); 103145861Sfjoe break; 104145861Sfjoe case 'm': 10525540Sdfr modname = optarg; 10625540Sdfr break; 10725540Sdfr case 'n': 108150497Spjd filename = optarg; 109150497Spjd break; 110150497Spjd case 'q': 11130627Sjmg quiet = 1; 11230627Sjmg break; 11330627Sjmg case 'v': 11425540Sdfr verbose = 1; 11525540Sdfr break; 11625540Sdfr default: 11725540Sdfr usage(); 11825540Sdfr } 11925540Sdfr argc -= optind; 12025540Sdfr argv += optind; 12125540Sdfr 12225540Sdfr if (argc != 0) 123145861Sfjoe usage(); 124145861Sfjoe 125145861Sfjoe if (modname != NULL) { 126145861Sfjoe int modid; 127150497Spjd struct module_stat stat; 128150497Spjd 129150497Spjd if ((modid = modfind(modname)) < 0) { 130150497Spjd if (!quiet) 131150497Spjd warn("can't find module %s", modname); 132150497Spjd return 1; 133150497Spjd } else if (quiet) { 134145861Sfjoe return 0; 135145861Sfjoe } 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; 146113936Sjohan } 14730627Sjmg 14832270Scharnier if (filename != NULL) { 14925540Sdfr if ((fileid = kldfind(filename)) < 0) 15025540Sdfr err(1, "can't find file %s", filename); 15154130Smarcel } 152113936Sjohan 15325540Sdfr printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 15425540Sdfr if (fileid != 0) 15525540Sdfr printfile(fileid, verbose); 15625540Sdfr else 15725540Sdfr for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 15825540Sdfr printfile(fileid, verbose); 15925540Sdfr 160 return 0; 161} 162