1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <err.h> 33 #include <libutil.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <unistd.h> 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/module.h> 40 #include <sys/linker.h> 41 #include <strings.h> 42 43 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 44 45 static int showdata = 0; 46 47 static void 48 printmod(int modid) 49 { 50 struct module_stat stat; 51 52 bzero(&stat, sizeof(stat)); 53 stat.version = sizeof(struct module_stat); 54 if (modstat(modid, &stat) < 0) 55 warn("can't stat module id %d", modid); 56 else 57 if (showdata) { 58 printf("\t\t%2d %s (%d, %u, 0x%lx)\n", stat.id, stat.name, 59 stat.data.intval, stat.data.uintval, stat.data.ulongval); 60 } else { 61 printf("\t\t%2d %s\n", stat.id, stat.name); 62 } 63 } 64 65 static void 66 printfile(int fileid, int verbose, int humanized) 67 { 68 struct kld_file_stat stat; 69 int modid; 70 char buf[5]; 71 72 stat.version = sizeof(struct kld_file_stat); 73 if (kldstat(fileid, &stat) < 0) { 74 err(1, "can't stat file id %d", fileid); 75 } else { 76 if (humanized) { 77 humanize_number(buf, sizeof(buf), stat.size, 78 "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE); 79 80 printf("%2d %4d %p %5s %s", 81 stat.id, stat.refs, stat.address, buf, stat.name); 82 } else { 83 printf("%2d %4d %p %8zx %s", 84 stat.id, stat.refs, stat.address, stat.size, stat.name); 85 } 86 } 87 88 if (verbose) { 89 printf(" (%s)\n", stat.pathname); 90 printf("\tContains modules:\n"); 91 printf("\t\tId Name\n"); 92 for (modid = kldfirstmod(fileid); modid > 0; 93 modid = modfnext(modid)) 94 printmod(modid); 95 } else 96 printf("\n"); 97 } 98 99 static void 100 usage(void) 101 { 102 fprintf(stderr, "usage: kldstat [-d] [-h] [-q] [-v] [-i id] [-n filename]\n"); 103 fprintf(stderr, " kldstat [-d] [-q] [-m modname]\n"); 104 exit(1); 105 } 106 107 int 108 main(int argc, char** argv) 109 { 110 int c; 111 int humanized = 0; 112 int verbose = 0; 113 int fileid = 0; 114 int quiet = 0; 115 char* filename = NULL; 116 char* modname = NULL; 117 char* p; 118 119 while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1) 120 switch (c) { 121 case 'd': 122 showdata = 1; 123 break; 124 case 'h': 125 humanized = 1; 126 break; 127 case 'i': 128 fileid = (int)strtoul(optarg, &p, 10); 129 if (*p != '\0') 130 usage(); 131 break; 132 case 'm': 133 modname = optarg; 134 break; 135 case 'n': 136 filename = optarg; 137 break; 138 case 'q': 139 quiet = 1; 140 break; 141 case 'v': 142 verbose = 1; 143 break; 144 default: 145 usage(); 146 } 147 argc -= optind; 148 argv += optind; 149 150 if (argc != 0) 151 usage(); 152 153 if (modname != NULL) { 154 int modid; 155 struct module_stat stat; 156 157 if ((modid = modfind(modname)) < 0) { 158 if (!quiet) 159 warn("can't find module %s", modname); 160 return 1; 161 } else if (quiet) { 162 return 0; 163 } 164 165 stat.version = sizeof(struct module_stat); 166 if (modstat(modid, &stat) < 0) 167 warn("can't stat module id %d", modid); 168 else { 169 if (showdata) { 170 printf("Id Refs Name data..(int, uint, ulong)\n"); 171 printf("%3d %4d %s (%d, %u, 0x%lx)\n", stat.id, stat.refs, stat.name, 172 stat.data.intval, stat.data.uintval, stat.data.ulongval); 173 } else { 174 printf("Id Refs Name\n"); 175 printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); 176 } 177 } 178 179 return 0; 180 } 181 182 if (filename != NULL) { 183 if ((fileid = kldfind(filename)) < 0) { 184 if (!quiet) 185 warn("can't find file %s", filename); 186 return 1; 187 } else if (quiet) { 188 return 0; 189 } 190 } 191 192 if (humanized) 193 printf("Id Refs Address%*c %5s Name\n", POINTER_WIDTH - 7, ' ', "Size"); 194 else 195 printf("Id Refs Address%*c %8s Name\n", POINTER_WIDTH - 7, ' ', "Size"); 196 if (fileid != 0) 197 printfile(fileid, verbose, humanized); 198 else 199 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) 200 printfile(fileid, verbose, humanized); 201 202 return 0; 203 } 204