kldstat.c revision 50476
122347Spst/*-
222347Spst * Copyright (c) 1997 Doug Rabson
329964Sache * All rights reserved.
492914Smarkm *
522347Spst * Redistribution and use in source and binary forms, with or without
622347Spst * modification, are permitted provided that the following conditions
722347Spst * are met:
822347Spst * 1. Redistributions of source code must retain the above copyright
922347Spst *    notice, this list of conditions and the following disclaimer.
1022347Spst * 2. Redistributions in binary form must reproduce the above copyright
1122347Spst *    notice, this list of conditions and the following disclaimer in the
1222347Spst *    documentation and/or other materials provided with the distribution.
1322347Spst *
1422347Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1522347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1622347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1759118Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1859118Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1922347Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2022347Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2122347Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2222347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2389766Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2489766Sache * SUCH DAMAGE.
2589766Sache */
2622347Spst
2722347Spst#ifndef lint
2822347Spststatic const char rcsid[] =
2922347Spst  "$FreeBSD: head/sbin/kldstat/kldstat.c 50476 1999-08-28 00:22:10Z peter $";
3022347Spst#endif /* not lint */
3122347Spst
3222347Spst#include <err.h>
3322347Spst#include <stdio.h>
3422347Spst#include <stdlib.h>
3522347Spst#include <unistd.h>
3622347Spst#include <sys/types.h>
3722347Spst#include <sys/param.h>
3822347Spst#include <sys/module.h>
3922347Spst#include <sys/linker.h>
4022347Spst
4122347Spststatic void
4222347Spstprintmod(int modid)
4322347Spst{
4422347Spst    struct module_stat stat;
4522347Spst
4622347Spst    stat.version = sizeof(struct module_stat);
4722347Spst    if (modstat(modid, &stat) < 0)
4822347Spst	warn("can't stat module id %d", modid);
4922347Spst    else
5022347Spst	printf("\t\t%2d %s\n", stat.id, stat.name);
5122347Spst}
5222347Spst
5322347Spststatic void printfile(int fileid, int verbose)
5422347Spst{
5522347Spst    struct kld_file_stat stat;
5622347Spst    int modid;
5722347Spst
5822347Spst    stat.version = sizeof(struct kld_file_stat);
5922347Spst    if (kldstat(fileid, &stat) < 0)
6022347Spst	warn("can't stat file id %d", fileid);
6122347Spst    else
6222347Spst	printf("%2d %4d %p %-8x %s\n",
6322347Spst	       stat.id, stat.refs, stat.address, stat.size, stat.name);
6422347Spst
6522347Spst    if (verbose) {
6622347Spst	printf("\tContains modules:\n");
6722347Spst	printf("\t\tId Name\n");
6822347Spst	for (modid = kldfirstmod(fileid); modid > 0;
6922347Spst	     modid = modfnext(modid))
7092914Smarkm	    printmod(modid);
71269806Sache    }
72269806Sache}
73269806Sache
7422347Spststatic void
7522347Spstusage(void)
7692914Smarkm{
7722347Spst    fprintf(stderr, "usage: kldstat [-v] [-i id] [-n name]\n");
7822347Spst    exit(1);
7922347Spst}
80
81int
82main(int argc, char** argv)
83{
84    int c;
85    int verbose = 0;
86    int fileid = 0;
87    char* filename = 0;
88
89    while ((c = getopt(argc, argv, "i:n:v")) != -1)
90	switch (c) {
91	case 'i':
92	    fileid = atoi(optarg);
93	    break;
94	case 'n':
95	    filename = optarg;
96	    break;
97	case 'v':
98	    verbose = 1;
99	    break;
100	default:
101	    usage();
102	}
103    argc -= optind;
104    argv += optind;
105
106    if (argc != 0)
107	usage();
108
109    if (filename) {
110	if ((fileid = kldfind(filename)) < 0)
111	    err(1, "can't find file %s", filename);
112    }
113
114    printf("Id Refs Address    Size     Name\n");
115    if (fileid)
116	printfile(fileid, verbose);
117    else
118	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
119	    printfile(fileid, verbose);
120
121    return 0;
122}
123