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> 31eb3beef5SJohan Karlsson #include <stdint.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 40fb2ef615SMarcel Moolenaar #if defined(__alpha__) 41fb2ef615SMarcel Moolenaar #define POINTER_WIDTH 18 42fb2ef615SMarcel Moolenaar #else 43fb2ef615SMarcel Moolenaar #define POINTER_WIDTH 10 44fb2ef615SMarcel Moolenaar #endif 45fb2ef615SMarcel Moolenaar 462830148aSJohn-Mark Gurney static void 472830148aSJohn-Mark Gurney printmod(int modid) 48a79fe607SDoug Rabson { 49a79fe607SDoug Rabson struct module_stat stat; 50a79fe607SDoug Rabson 51a79fe607SDoug Rabson stat.version = sizeof(struct module_stat); 52a79fe607SDoug Rabson if (modstat(modid, &stat) < 0) 5357e78ffbSPhilippe Charnier warn("can't stat module id %d", modid); 54a79fe607SDoug Rabson else 5561c3dd35SPaul Saab printf("\t\t%2d %s\n", stat.id, stat.name); 56a79fe607SDoug Rabson } 57a79fe607SDoug Rabson 58a79fe607SDoug Rabson static void printfile(int fileid, int verbose) 59a79fe607SDoug Rabson { 60a79fe607SDoug Rabson struct kld_file_stat stat; 61a79fe607SDoug Rabson int modid; 62a79fe607SDoug Rabson 63a79fe607SDoug Rabson stat.version = sizeof(struct kld_file_stat); 64a79fe607SDoug Rabson if (kldstat(fileid, &stat) < 0) 6557e78ffbSPhilippe Charnier warn("can't stat file id %d", fileid); 66a79fe607SDoug Rabson else 67eb3beef5SJohan Karlsson printf("%2d %4d %p %-8jx %s\n", 68eb3beef5SJohan Karlsson stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 69eb3beef5SJohan Karlsson stat.name); 70a79fe607SDoug Rabson 71a79fe607SDoug Rabson if (verbose) { 72a79fe607SDoug Rabson printf("\tContains modules:\n"); 7361c3dd35SPaul Saab printf("\t\tId Name\n"); 74a79fe607SDoug Rabson for (modid = kldfirstmod(fileid); modid > 0; 75a79fe607SDoug Rabson modid = modfnext(modid)) 76a79fe607SDoug Rabson printmod(modid); 77a79fe607SDoug Rabson } 78a79fe607SDoug Rabson } 79a79fe607SDoug Rabson 80a79fe607SDoug Rabson static void 812830148aSJohn-Mark Gurney usage(void) 82a79fe607SDoug Rabson { 8357e78ffbSPhilippe Charnier fprintf(stderr, "usage: kldstat [-v] [-i id] [-n name]\n"); 84a79fe607SDoug Rabson exit(1); 85a79fe607SDoug Rabson } 86a79fe607SDoug Rabson 872830148aSJohn-Mark Gurney int 882830148aSJohn-Mark Gurney main(int argc, char** argv) 89a79fe607SDoug Rabson { 90a79fe607SDoug Rabson int c; 91a79fe607SDoug Rabson int verbose = 0; 92a79fe607SDoug Rabson int fileid = 0; 937f107457SJohan Karlsson char* filename = NULL; 949c4c393aSJuli Mallett char* p; 95a79fe607SDoug Rabson 962830148aSJohn-Mark Gurney while ((c = getopt(argc, argv, "i:n:v")) != -1) 97a79fe607SDoug Rabson switch (c) { 98a79fe607SDoug Rabson case 'i': 999c4c393aSJuli Mallett fileid = (int)strtoul(optarg, &p, 10); 1009c4c393aSJuli Mallett if (*p != '\0') 1019c4c393aSJuli Mallett usage(); 102a79fe607SDoug Rabson break; 103a79fe607SDoug Rabson case 'n': 104a79fe607SDoug Rabson filename = optarg; 105a79fe607SDoug Rabson break; 1062830148aSJohn-Mark Gurney case 'v': 1072830148aSJohn-Mark Gurney verbose = 1; 1082830148aSJohn-Mark Gurney break; 109a79fe607SDoug Rabson default: 110a79fe607SDoug Rabson usage(); 111a79fe607SDoug Rabson } 112a79fe607SDoug Rabson argc -= optind; 113a79fe607SDoug Rabson argv += optind; 114a79fe607SDoug Rabson 115a79fe607SDoug Rabson if (argc != 0) 116a79fe607SDoug Rabson usage(); 117a79fe607SDoug Rabson 1187f107457SJohan Karlsson if (filename != NULL) { 1192830148aSJohn-Mark Gurney if ((fileid = kldfind(filename)) < 0) 12057e78ffbSPhilippe Charnier err(1, "can't find file %s", filename); 121a79fe607SDoug Rabson } 122a79fe607SDoug Rabson 123fb2ef615SMarcel Moolenaar printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1247f107457SJohan Karlsson if (fileid != 0) 125a79fe607SDoug Rabson printfile(fileid, verbose); 126a79fe607SDoug Rabson else 127a79fe607SDoug Rabson for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 128a79fe607SDoug Rabson printfile(fileid, verbose); 129a79fe607SDoug Rabson 130a79fe607SDoug Rabson return 0; 131a79fe607SDoug Rabson } 132