#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/modctl.h>
#include <sys/errno.h>
static int wide;
static int count = 0;
static int first_mod = 1;
static const char header[] =
" Id "
#if defined(_LP64) && !defined(__sparcv9)
" "
#endif
"Loadaddr Size Info Rev Module Name\n";
static char *cheader =
" Id Loadcnt Module Name State\n";
static void usage();
static void print_info(struct modinfo *mi);
static void print_cinfo(struct modinfo *mi);
void fatal(char *fmt, ...);
void error(char *fmt, ...);
int
main(int argc, char *argv[])
{
struct modinfo modinfo;
int info_all = 1;
int id;
int opt;
id = -1;
while ((opt = getopt(argc, argv, "i:wc")) != EOF) {
switch (opt) {
case 'i':
if (sscanf(optarg, "%d", &id) != 1)
fatal("Invalid id %s\n", optarg);
if (id == -1)
id = 0;
info_all = 0;
break;
case 'w':
wide++;
break;
case 'c':
count++;
break;
case '?':
default:
usage();
break;
}
}
modinfo.mi_id = modinfo.mi_nextid = id;
modinfo.mi_info = (info_all) ? MI_INFO_ALL : MI_INFO_ONE;
if (count)
modinfo.mi_info |= MI_INFO_CNT;
do {
if (modctl(MODINFO, id, &modinfo) < 0) {
if (!info_all)
error("can't get module information");
break;
}
if (first_mod) {
first_mod = 0;
(void) printf("%s", count ? cheader : header);
}
if (count)
print_cinfo(&modinfo);
else
print_info(&modinfo);
id = modinfo.mi_id;
} while (info_all);
return (0);
}
static void
print_cinfo(struct modinfo *mi)
{
(void) printf("%3d %10d %-32s", mi->mi_id, mi->mi_loadcnt, mi->mi_name);
(void) printf(" %s/%s\n",
mi->mi_state & MI_LOADED ? "LOADED" : "UNLOADED",
mi->mi_state & MI_INSTALLED ? "INSTALLED" : "UNINSTALLED");
}
static void
print_info(struct modinfo *mi)
{
int n, p0;
char namebuf[256];
for (n = 0; n < MODMAXLINK; n++) {
if (n > 0 && mi->mi_msinfo[n].msi_linkinfo[0] == '\0')
break;
(void) printf("%3d ", mi->mi_id);
#if defined(_LP64) && !defined(__sparcv9)
(void) printf("%16lx ", (uintptr_t)mi->mi_base);
#elif defined(_LP64)
(void) printf("%8lx ", (uintptr_t)mi->mi_base);
#else
(void) printf("%8x ", (uintptr_t)mi->mi_base);
#endif
#if defined(_LP64)
(void) printf("%6lx ", mi->mi_size);
#else
(void) printf("%6x ", mi->mi_size);
#endif
p0 = mi->mi_msinfo[n].msi_p0;
if (p0 != -1)
(void) printf("%3d ", p0);
else
(void) printf(" - ");
(void) printf(" %d ", mi->mi_rev);
mi->mi_name[MODMAXNAMELEN - 1] = '\0';
mi->mi_msinfo[n].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
if (wide) {
(void) printf("%s (%s)\n", mi->mi_name,
mi->mi_msinfo[n].msi_linkinfo);
} else {
(void) snprintf(namebuf, sizeof (namebuf), "%s (%s)",
mi->mi_name, mi->mi_msinfo[n].msi_linkinfo);
#if defined(_LP64) && !defined(__sparcv9)
(void) printf("%.43s\n", namebuf);
#else
(void) printf("%.51s\n", namebuf);
#endif
}
}
}
static void
usage()
{
fatal("usage: modinfo [-w] [-c] [-i module-id]\n");
}