kldstat.c revision 30627
19617Stwisti/*-
29617Stwisti * Copyright (c) 1997 Doug Rabson
39617Stwisti * All rights reserved.
49617Stwisti *
59617Stwisti * Redistribution and use in source and binary forms, with or without
69617Stwisti * modification, are permitted provided that the following conditions
79617Stwisti * are met:
89617Stwisti * 1. Redistributions of source code must retain the above copyright
99617Stwisti *    notice, this list of conditions and the following disclaimer.
109617Stwisti * 2. Redistributions in binary form must reproduce the above copyright
119617Stwisti *    notice, this list of conditions and the following disclaimer in the
129617Stwisti *    documentation and/or other materials provided with the distribution.
139617Stwisti *
149617Stwisti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
159617Stwisti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169617Stwisti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179617Stwisti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
189617Stwisti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199617Stwisti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
209617Stwisti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
219617Stwisti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
229617Stwisti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
239617Stwisti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
249617Stwisti * SUCH DAMAGE.
259617Stwisti *
269617Stwisti *	$Id: kldstat.c,v 1.2 1997/10/19 11:15:44 jmg Exp $
279617Stwisti */
289617Stwisti
299617Stwisti#include <err.h>
309617Stwisti#include <stdio.h>
319617Stwisti#include <stdlib.h>
329617Stwisti#include <unistd.h>
339617Stwisti#include <sys/types.h>
349617Stwisti#include <sys/param.h>
359617Stwisti#include <sys/module.h>
369617Stwisti#include <sys/linker.h>
379617Stwisti
389617Stwististatic void
399617Stwistiprintmod(int modid)
409617Stwisti{
419617Stwisti    struct module_stat stat;
429617Stwisti
4310053Sdnsimon    stat.version = sizeof(struct module_stat);
4410053Sdnsimon    if (modstat(modid, &stat) < 0)
459617Stwisti	warn("Can't stat module id %d", modid);
469617Stwisti    else
479617Stwisti	printf("\t\t%2d %s\n", stat.id, stat.name);
489617Stwisti}
499617Stwisti
509617Stwististatic void printfile(int fileid, int verbose)
519617Stwisti{
529617Stwisti    struct kld_file_stat stat;
539617Stwisti    int modid;
549617Stwisti
559617Stwisti    stat.version = sizeof(struct kld_file_stat);
569617Stwisti    if (kldstat(fileid, &stat) < 0)
579617Stwisti	warn("Can't stat file id %d", fileid);
589617Stwisti    else
599617Stwisti	printf("%2d %4d %p %-8x %s\n",
609617Stwisti	       stat.id, stat.refs, stat.address, stat.size, stat.name);
619617Stwisti
629617Stwisti    if (verbose) {
639617Stwisti	printf("\tContains modules:\n");
649617Stwisti	printf("\t\tId Name\n");
659617Stwisti	for (modid = kldfirstmod(fileid); modid > 0;
669617Stwisti	     modid = modfnext(modid))
6711694Sdnsimon	    printmod(modid);
689617Stwisti    }
699617Stwisti}
709617Stwisti
719617Stwististatic void
729617Stwistiusage(void)
739617Stwisti{
749617Stwisti    fprintf(stderr, "usage: kldstat [-v]\n");
759617Stwisti    exit(1);
769617Stwisti}
779617Stwisti
789617Stwistiint
799617Stwistimain(int argc, char** argv)
809617Stwisti{
819617Stwisti    int c;
829617Stwisti    int verbose = 0;
839617Stwisti    int fileid = 0;
849617Stwisti    char* filename = 0;
859617Stwisti
869617Stwisti    while ((c = getopt(argc, argv, "i:n:v")) != -1)
879617Stwisti	switch (c) {
889617Stwisti	case 'i':
899617Stwisti	    fileid = atoi(optarg);
909617Stwisti	    break;
919617Stwisti	case 'n':
929617Stwisti	    filename = optarg;
939617Stwisti	    break;
949617Stwisti	case 'v':
959617Stwisti	    verbose = 1;
969617Stwisti	    break;
979617Stwisti	default:
989617Stwisti	    usage();
999617Stwisti	}
1009617Stwisti    argc -= optind;
1019617Stwisti    argv += optind;
1029617Stwisti
1039617Stwisti    if (argc != 0)
1049617Stwisti	usage();
1059617Stwisti
1069617Stwisti    if (filename) {
1079617Stwisti	if ((fileid = kldfind(filename)) < 0)
1089617Stwisti	    err(1, "Can't find file %s", filename);
1099617Stwisti    }
1109617Stwisti
1119617Stwisti    printf("Id Refs Address  Size     Name\n");
1129617Stwisti    if (fileid)
1139617Stwisti	printfile(fileid, verbose);
1149617Stwisti    else
1159617Stwisti	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
1169617Stwisti	    printfile(fileid, verbose);
1179617Stwisti
1189617Stwisti    return 0;
1199617Stwisti}
1209617Stwisti