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 2757e78ffbSPhilippe Charnier #ifndef lint 2857e78ffbSPhilippe Charnier static const char rcsid[] = 297f3dea24SPeter Wemm "$FreeBSD$"; 3057e78ffbSPhilippe Charnier #endif /* not lint */ 3157e78ffbSPhilippe Charnier 3223e200d5SJohn-Mark Gurney #include <err.h> 33eb3beef5SJohan Karlsson #include <stdint.h> 34a79fe607SDoug Rabson #include <stdio.h> 3523e200d5SJohn-Mark Gurney #include <stdlib.h> 36a79fe607SDoug Rabson #include <unistd.h> 37a79fe607SDoug Rabson #include <sys/types.h> 38a79fe607SDoug Rabson #include <sys/param.h> 39a79fe607SDoug Rabson #include <sys/module.h> 40a79fe607SDoug Rabson #include <sys/linker.h> 41a79fe607SDoug Rabson 42fb2ef615SMarcel Moolenaar #if defined(__alpha__) 43fb2ef615SMarcel Moolenaar #define POINTER_WIDTH 18 44fb2ef615SMarcel Moolenaar #else 45fb2ef615SMarcel Moolenaar #define POINTER_WIDTH 10 46fb2ef615SMarcel Moolenaar #endif 47fb2ef615SMarcel Moolenaar 482830148aSJohn-Mark Gurney static void 492830148aSJohn-Mark Gurney printmod(int modid) 50a79fe607SDoug Rabson { 51a79fe607SDoug Rabson struct module_stat stat; 52a79fe607SDoug Rabson 53a79fe607SDoug Rabson stat.version = sizeof(struct module_stat); 54a79fe607SDoug Rabson if (modstat(modid, &stat) < 0) 5557e78ffbSPhilippe Charnier warn("can't stat module id %d", modid); 56a79fe607SDoug Rabson else 5761c3dd35SPaul Saab printf("\t\t%2d %s\n", stat.id, stat.name); 58a79fe607SDoug Rabson } 59a79fe607SDoug Rabson 60a79fe607SDoug Rabson static void printfile(int fileid, int verbose) 61a79fe607SDoug Rabson { 62a79fe607SDoug Rabson struct kld_file_stat stat; 63a79fe607SDoug Rabson int modid; 64a79fe607SDoug Rabson 65a79fe607SDoug Rabson stat.version = sizeof(struct kld_file_stat); 66a79fe607SDoug Rabson if (kldstat(fileid, &stat) < 0) 6757e78ffbSPhilippe Charnier warn("can't stat file id %d", fileid); 68a79fe607SDoug Rabson else 69eb3beef5SJohan Karlsson printf("%2d %4d %p %-8jx %s\n", 70eb3beef5SJohan Karlsson stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 71eb3beef5SJohan Karlsson stat.name); 72a79fe607SDoug Rabson 73a79fe607SDoug Rabson if (verbose) { 74a79fe607SDoug Rabson printf("\tContains modules:\n"); 7561c3dd35SPaul Saab printf("\t\tId Name\n"); 76a79fe607SDoug Rabson for (modid = kldfirstmod(fileid); modid > 0; 77a79fe607SDoug Rabson modid = modfnext(modid)) 78a79fe607SDoug Rabson printmod(modid); 79a79fe607SDoug Rabson } 80a79fe607SDoug Rabson } 81a79fe607SDoug Rabson 82a79fe607SDoug Rabson static void 832830148aSJohn-Mark Gurney usage(void) 84a79fe607SDoug Rabson { 8557e78ffbSPhilippe Charnier fprintf(stderr, "usage: kldstat [-v] [-i id] [-n name]\n"); 86a79fe607SDoug Rabson exit(1); 87a79fe607SDoug Rabson } 88a79fe607SDoug Rabson 892830148aSJohn-Mark Gurney int 902830148aSJohn-Mark Gurney main(int argc, char** argv) 91a79fe607SDoug Rabson { 92a79fe607SDoug Rabson int c; 93a79fe607SDoug Rabson int verbose = 0; 94a79fe607SDoug Rabson int fileid = 0; 957f107457SJohan Karlsson char* filename = NULL; 96a79fe607SDoug Rabson 972830148aSJohn-Mark Gurney while ((c = getopt(argc, argv, "i:n:v")) != -1) 98a79fe607SDoug Rabson switch (c) { 99a79fe607SDoug Rabson case 'i': 100a79fe607SDoug Rabson fileid = atoi(optarg); 101a79fe607SDoug Rabson break; 102a79fe607SDoug Rabson case 'n': 103a79fe607SDoug Rabson filename = optarg; 104a79fe607SDoug Rabson break; 1052830148aSJohn-Mark Gurney case 'v': 1062830148aSJohn-Mark Gurney verbose = 1; 1072830148aSJohn-Mark Gurney break; 108a79fe607SDoug Rabson default: 109a79fe607SDoug Rabson usage(); 110a79fe607SDoug Rabson } 111a79fe607SDoug Rabson argc -= optind; 112a79fe607SDoug Rabson argv += optind; 113a79fe607SDoug Rabson 114a79fe607SDoug Rabson if (argc != 0) 115a79fe607SDoug Rabson usage(); 116a79fe607SDoug Rabson 1177f107457SJohan Karlsson if (filename != NULL) { 1182830148aSJohn-Mark Gurney if ((fileid = kldfind(filename)) < 0) 11957e78ffbSPhilippe Charnier err(1, "can't find file %s", filename); 120a79fe607SDoug Rabson } 121a79fe607SDoug Rabson 122fb2ef615SMarcel Moolenaar printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1237f107457SJohan Karlsson if (fileid != 0) 124a79fe607SDoug Rabson printfile(fileid, verbose); 125a79fe607SDoug Rabson else 126a79fe607SDoug Rabson for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 127a79fe607SDoug Rabson printfile(fileid, verbose); 128a79fe607SDoug Rabson 129a79fe607SDoug Rabson return 0; 130a79fe607SDoug Rabson } 131