1a79fe607SDoug Rabson /*- 2a79fe607SDoug Rabson * Copyright (c) 1997 Doug Rabson 3a79fe607SDoug Rabson * All rights reserved. 4a79fe607SDoug Rabson * 5a79fe607SDoug Rabson * Redistribution and use in source and binary forms, with or without 6a79fe607SDoug Rabson * modification, are permitted provided that the following conditions 7a79fe607SDoug Rabson * are met: 8a79fe607SDoug Rabson * 1. Redistributions of source code must retain the above copyright 9a79fe607SDoug Rabson * notice, this list of conditions and the following disclaimer. 10a79fe607SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 11a79fe607SDoug Rabson * notice, this list of conditions and the following disclaimer in the 12a79fe607SDoug Rabson * documentation and/or other materials provided with the distribution. 13a79fe607SDoug Rabson * 14a79fe607SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a79fe607SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a79fe607SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a79fe607SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a79fe607SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a79fe607SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a79fe607SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a79fe607SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a79fe607SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a79fe607SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a79fe607SDoug Rabson * SUCH DAMAGE. 25a79fe607SDoug Rabson */ 26a79fe607SDoug Rabson 27c69284caSDavid E. O'Brien #include <sys/cdefs.h> 28c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 2957e78ffbSPhilippe Charnier 3023e200d5SJohn-Mark Gurney #include <err.h> 31*1b18fc10SEdward Tomasz Napierala #include <libutil.h> 32a79fe607SDoug Rabson #include <stdio.h> 3323e200d5SJohn-Mark Gurney #include <stdlib.h> 34a79fe607SDoug Rabson #include <unistd.h> 35a79fe607SDoug Rabson #include <sys/types.h> 36a79fe607SDoug Rabson #include <sys/param.h> 37a79fe607SDoug Rabson #include <sys/module.h> 38a79fe607SDoug Rabson #include <sys/linker.h> 39a79fe607SDoug Rabson 402c8aff0aSDavid Malone #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 41fb2ef615SMarcel Moolenaar 422830148aSJohn-Mark Gurney static void 432830148aSJohn-Mark Gurney printmod(int modid) 44a79fe607SDoug Rabson { 45a79fe607SDoug Rabson struct module_stat stat; 46a79fe607SDoug Rabson 47a79fe607SDoug Rabson stat.version = sizeof(struct module_stat); 48a79fe607SDoug Rabson if (modstat(modid, &stat) < 0) 4957e78ffbSPhilippe Charnier warn("can't stat module id %d", modid); 50a79fe607SDoug Rabson else 5161c3dd35SPaul Saab printf("\t\t%2d %s\n", stat.id, stat.name); 52a79fe607SDoug Rabson } 53a79fe607SDoug Rabson 5435e3987fSKonstantin Belousov static void 55*1b18fc10SEdward Tomasz Napierala printfile(int fileid, int verbose, int humanized) 56a79fe607SDoug Rabson { 57a79fe607SDoug Rabson struct kld_file_stat stat; 58a79fe607SDoug Rabson int modid; 59*1b18fc10SEdward Tomasz Napierala char buf[5]; 60a79fe607SDoug Rabson 61a79fe607SDoug Rabson stat.version = sizeof(struct kld_file_stat); 62*1b18fc10SEdward Tomasz Napierala if (kldstat(fileid, &stat) < 0) { 6335e3987fSKonstantin Belousov err(1, "can't stat file id %d", fileid); 64*1b18fc10SEdward Tomasz Napierala } else { 65*1b18fc10SEdward Tomasz Napierala if (humanized) { 66*1b18fc10SEdward Tomasz Napierala humanize_number(buf, sizeof(buf), stat.size, 67*1b18fc10SEdward Tomasz Napierala "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE); 68*1b18fc10SEdward Tomasz Napierala 69*1b18fc10SEdward Tomasz Napierala printf("%2d %4d %p %5s %s", 70*1b18fc10SEdward Tomasz Napierala stat.id, stat.refs, stat.address, buf, stat.name); 71*1b18fc10SEdward Tomasz Napierala } else { 728e721ea7SChristian Brueffer printf("%2d %4d %p %-8zx %s", 73*1b18fc10SEdward Tomasz Napierala stat.id, stat.refs, stat.address, stat.size, stat.name); 74*1b18fc10SEdward Tomasz Napierala } 75*1b18fc10SEdward Tomasz Napierala } 76a79fe607SDoug Rabson 77a79fe607SDoug Rabson if (verbose) { 7897a7a569SAndrew Thompson printf(" (%s)\n", stat.pathname); 79a79fe607SDoug Rabson printf("\tContains modules:\n"); 8061c3dd35SPaul Saab printf("\t\tId Name\n"); 81a79fe607SDoug Rabson for (modid = kldfirstmod(fileid); modid > 0; 82a79fe607SDoug Rabson modid = modfnext(modid)) 83a79fe607SDoug Rabson printmod(modid); 8497a7a569SAndrew Thompson } else 8597a7a569SAndrew Thompson printf("\n"); 86a79fe607SDoug Rabson } 87a79fe607SDoug Rabson 88a79fe607SDoug Rabson static void 892830148aSJohn-Mark Gurney usage(void) 90a79fe607SDoug Rabson { 91*1b18fc10SEdward Tomasz Napierala fprintf(stderr, "usage: kldstat [-h] [-q] [-v] [-i id] [-n filename]\n"); 9209d5b706SPawel Jakub Dawidek fprintf(stderr, " kldstat [-q] [-m modname]\n"); 93a79fe607SDoug Rabson exit(1); 94a79fe607SDoug Rabson } 95a79fe607SDoug Rabson 962830148aSJohn-Mark Gurney int 972830148aSJohn-Mark Gurney main(int argc, char** argv) 98a79fe607SDoug Rabson { 99a79fe607SDoug Rabson int c; 100*1b18fc10SEdward Tomasz Napierala int humanized = 0; 101a79fe607SDoug Rabson int verbose = 0; 102a79fe607SDoug Rabson int fileid = 0; 10390217cddSPawel Jakub Dawidek int quiet = 0; 1047f107457SJohan Karlsson char* filename = NULL; 1055d6d6d38SMax Khon char* modname = NULL; 1069c4c393aSJuli Mallett char* p; 107a79fe607SDoug Rabson 108*1b18fc10SEdward Tomasz Napierala while ((c = getopt(argc, argv, "hi:m:n:qv")) != -1) 109a79fe607SDoug Rabson switch (c) { 110*1b18fc10SEdward Tomasz Napierala case 'h': 111*1b18fc10SEdward Tomasz Napierala humanized = 1; 112*1b18fc10SEdward Tomasz Napierala break; 113a79fe607SDoug Rabson case 'i': 1149c4c393aSJuli Mallett fileid = (int)strtoul(optarg, &p, 10); 1159c4c393aSJuli Mallett if (*p != '\0') 1169c4c393aSJuli Mallett usage(); 117a79fe607SDoug Rabson break; 1185d6d6d38SMax Khon case 'm': 1195d6d6d38SMax Khon modname = optarg; 1205d6d6d38SMax Khon break; 121a79fe607SDoug Rabson case 'n': 122a79fe607SDoug Rabson filename = optarg; 123a79fe607SDoug Rabson break; 12490217cddSPawel Jakub Dawidek case 'q': 12590217cddSPawel Jakub Dawidek quiet = 1; 12690217cddSPawel Jakub Dawidek break; 1272830148aSJohn-Mark Gurney case 'v': 1282830148aSJohn-Mark Gurney verbose = 1; 1292830148aSJohn-Mark Gurney break; 130a79fe607SDoug Rabson default: 131a79fe607SDoug Rabson usage(); 132a79fe607SDoug Rabson } 133a79fe607SDoug Rabson argc -= optind; 134a79fe607SDoug Rabson argv += optind; 135a79fe607SDoug Rabson 136a79fe607SDoug Rabson if (argc != 0) 137a79fe607SDoug Rabson usage(); 138a79fe607SDoug Rabson 1395d6d6d38SMax Khon if (modname != NULL) { 1405d6d6d38SMax Khon int modid; 1415d6d6d38SMax Khon struct module_stat stat; 1425d6d6d38SMax Khon 14390217cddSPawel Jakub Dawidek if ((modid = modfind(modname)) < 0) { 14490217cddSPawel Jakub Dawidek if (!quiet) 14590217cddSPawel Jakub Dawidek warn("can't find module %s", modname); 14690217cddSPawel Jakub Dawidek return 1; 14790217cddSPawel Jakub Dawidek } else if (quiet) { 14890217cddSPawel Jakub Dawidek return 0; 14990217cddSPawel Jakub Dawidek } 1505d6d6d38SMax Khon 1515d6d6d38SMax Khon stat.version = sizeof(struct module_stat); 1525d6d6d38SMax Khon if (modstat(modid, &stat) < 0) 1535d6d6d38SMax Khon warn("can't stat module id %d", modid); 1545d6d6d38SMax Khon else { 1555d6d6d38SMax Khon printf("Id Refs Name\n"); 1565d6d6d38SMax Khon printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); 1575d6d6d38SMax Khon } 1585d6d6d38SMax Khon 1595d6d6d38SMax Khon return 0; 1605d6d6d38SMax Khon } 1615d6d6d38SMax Khon 1627f107457SJohan Karlsson if (filename != NULL) { 1633e8cf495SBaptiste Daroussin if ((fileid = kldfind(filename)) < 0) { 1643e8cf495SBaptiste Daroussin if (!quiet) 1653e8cf495SBaptiste Daroussin warn("can't find file %s", filename); 1663e8cf495SBaptiste Daroussin return 1; 1673e8cf495SBaptiste Daroussin } else if (quiet) { 1683e8cf495SBaptiste Daroussin return 0; 1693e8cf495SBaptiste Daroussin } 170a79fe607SDoug Rabson } 171a79fe607SDoug Rabson 172*1b18fc10SEdward Tomasz Napierala if (humanized) 173*1b18fc10SEdward Tomasz Napierala printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 174*1b18fc10SEdward Tomasz Napierala else 175fb2ef615SMarcel Moolenaar printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1767f107457SJohan Karlsson if (fileid != 0) 177*1b18fc10SEdward Tomasz Napierala printfile(fileid, verbose, humanized); 178a79fe607SDoug Rabson else 179a79fe607SDoug Rabson for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 180*1b18fc10SEdward Tomasz Napierala printfile(fileid, verbose, humanized); 181a79fe607SDoug Rabson 182a79fe607SDoug Rabson return 0; 183a79fe607SDoug Rabson } 184