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> 31a79fe607SDoug Rabson #include <stdio.h> 3223e200d5SJohn-Mark Gurney #include <stdlib.h> 33a79fe607SDoug Rabson #include <unistd.h> 34a79fe607SDoug Rabson #include <sys/types.h> 35a79fe607SDoug Rabson #include <sys/param.h> 36a79fe607SDoug Rabson #include <sys/module.h> 37a79fe607SDoug Rabson #include <sys/linker.h> 38a79fe607SDoug Rabson 392c8aff0aSDavid Malone #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 40fb2ef615SMarcel Moolenaar 412830148aSJohn-Mark Gurney static void 422830148aSJohn-Mark Gurney printmod(int modid) 43a79fe607SDoug Rabson { 44a79fe607SDoug Rabson struct module_stat stat; 45a79fe607SDoug Rabson 46a79fe607SDoug Rabson stat.version = sizeof(struct module_stat); 47a79fe607SDoug Rabson if (modstat(modid, &stat) < 0) 4857e78ffbSPhilippe Charnier warn("can't stat module id %d", modid); 49a79fe607SDoug Rabson else 5061c3dd35SPaul Saab printf("\t\t%2d %s\n", stat.id, stat.name); 51a79fe607SDoug Rabson } 52a79fe607SDoug Rabson 53*35e3987fSKonstantin Belousov static void 54*35e3987fSKonstantin Belousov printfile(int fileid, int verbose) 55a79fe607SDoug Rabson { 56a79fe607SDoug Rabson struct kld_file_stat stat; 57a79fe607SDoug Rabson int modid; 58a79fe607SDoug Rabson 59a79fe607SDoug Rabson stat.version = sizeof(struct kld_file_stat); 60a79fe607SDoug Rabson if (kldstat(fileid, &stat) < 0) 61*35e3987fSKonstantin Belousov err(1, "can't stat file id %d", fileid); 62a79fe607SDoug Rabson else 638e721ea7SChristian Brueffer printf("%2d %4d %p %-8zx %s", 648e721ea7SChristian Brueffer stat.id, stat.refs, stat.address, stat.size, 6597a7a569SAndrew Thompson stat.name); 66a79fe607SDoug Rabson 67a79fe607SDoug Rabson if (verbose) { 6897a7a569SAndrew Thompson printf(" (%s)\n", stat.pathname); 69a79fe607SDoug Rabson printf("\tContains modules:\n"); 7061c3dd35SPaul Saab printf("\t\tId Name\n"); 71a79fe607SDoug Rabson for (modid = kldfirstmod(fileid); modid > 0; 72a79fe607SDoug Rabson modid = modfnext(modid)) 73a79fe607SDoug Rabson printmod(modid); 7497a7a569SAndrew Thompson } else 7597a7a569SAndrew Thompson printf("\n"); 76a79fe607SDoug Rabson } 77a79fe607SDoug Rabson 78a79fe607SDoug Rabson static void 792830148aSJohn-Mark Gurney usage(void) 80a79fe607SDoug Rabson { 819b3eb503SRuslan Ermilov fprintf(stderr, "usage: kldstat [-v] [-i id] [-n filename]\n"); 8209d5b706SPawel Jakub Dawidek fprintf(stderr, " kldstat [-q] [-m modname]\n"); 83a79fe607SDoug Rabson exit(1); 84a79fe607SDoug Rabson } 85a79fe607SDoug Rabson 862830148aSJohn-Mark Gurney int 872830148aSJohn-Mark Gurney main(int argc, char** argv) 88a79fe607SDoug Rabson { 89a79fe607SDoug Rabson int c; 90a79fe607SDoug Rabson int verbose = 0; 91a79fe607SDoug Rabson int fileid = 0; 9290217cddSPawel Jakub Dawidek int quiet = 0; 937f107457SJohan Karlsson char* filename = NULL; 945d6d6d38SMax Khon char* modname = NULL; 959c4c393aSJuli Mallett char* p; 96a79fe607SDoug Rabson 9790217cddSPawel Jakub Dawidek while ((c = getopt(argc, argv, "i:m:n:qv")) != -1) 98a79fe607SDoug Rabson switch (c) { 99a79fe607SDoug Rabson case 'i': 1009c4c393aSJuli Mallett fileid = (int)strtoul(optarg, &p, 10); 1019c4c393aSJuli Mallett if (*p != '\0') 1029c4c393aSJuli Mallett usage(); 103a79fe607SDoug Rabson break; 1045d6d6d38SMax Khon case 'm': 1055d6d6d38SMax Khon modname = optarg; 1065d6d6d38SMax Khon break; 107a79fe607SDoug Rabson case 'n': 108a79fe607SDoug Rabson filename = optarg; 109a79fe607SDoug Rabson break; 11090217cddSPawel Jakub Dawidek case 'q': 11190217cddSPawel Jakub Dawidek quiet = 1; 11290217cddSPawel Jakub Dawidek break; 1132830148aSJohn-Mark Gurney case 'v': 1142830148aSJohn-Mark Gurney verbose = 1; 1152830148aSJohn-Mark Gurney break; 116a79fe607SDoug Rabson default: 117a79fe607SDoug Rabson usage(); 118a79fe607SDoug Rabson } 119a79fe607SDoug Rabson argc -= optind; 120a79fe607SDoug Rabson argv += optind; 121a79fe607SDoug Rabson 122a79fe607SDoug Rabson if (argc != 0) 123a79fe607SDoug Rabson usage(); 124a79fe607SDoug Rabson 1255d6d6d38SMax Khon if (modname != NULL) { 1265d6d6d38SMax Khon int modid; 1275d6d6d38SMax Khon struct module_stat stat; 1285d6d6d38SMax Khon 12990217cddSPawel Jakub Dawidek if ((modid = modfind(modname)) < 0) { 13090217cddSPawel Jakub Dawidek if (!quiet) 13190217cddSPawel Jakub Dawidek warn("can't find module %s", modname); 13290217cddSPawel Jakub Dawidek return 1; 13390217cddSPawel Jakub Dawidek } else if (quiet) { 13490217cddSPawel Jakub Dawidek return 0; 13590217cddSPawel Jakub Dawidek } 1365d6d6d38SMax Khon 1375d6d6d38SMax Khon stat.version = sizeof(struct module_stat); 1385d6d6d38SMax Khon if (modstat(modid, &stat) < 0) 1395d6d6d38SMax Khon warn("can't stat module id %d", modid); 1405d6d6d38SMax Khon else { 1415d6d6d38SMax Khon printf("Id Refs Name\n"); 1425d6d6d38SMax Khon printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); 1435d6d6d38SMax Khon } 1445d6d6d38SMax Khon 1455d6d6d38SMax Khon return 0; 1465d6d6d38SMax Khon } 1475d6d6d38SMax Khon 1487f107457SJohan Karlsson if (filename != NULL) { 1492830148aSJohn-Mark Gurney if ((fileid = kldfind(filename)) < 0) 15057e78ffbSPhilippe Charnier err(1, "can't find file %s", filename); 151a79fe607SDoug Rabson } 152a79fe607SDoug Rabson 153fb2ef615SMarcel Moolenaar printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1547f107457SJohan Karlsson if (fileid != 0) 155a79fe607SDoug Rabson printfile(fileid, verbose); 156a79fe607SDoug Rabson else 157a79fe607SDoug Rabson for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 158a79fe607SDoug Rabson printfile(fileid, verbose); 159a79fe607SDoug Rabson 160a79fe607SDoug Rabson return 0; 161a79fe607SDoug Rabson } 162