#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_history.c,v 1.21 2026/01/04 01:34:29 riastradh Exp $");
#include "opt_ddb.h"
#include "opt_kernhist.h"
#include "opt_syscall_debug.h"
#include "opt_usb.h"
#include "opt_uvmhist.h"
#include "opt_biohist.h"
#include "opt_sysctl.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/cpu.h>
#include <sys/kernhist.h>
#include <sys/kmem.h>
#include <sys/sdt.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#ifdef UVMHIST
#include <uvm/uvm.h>
#endif
#ifdef USB_DEBUG
#include <dev/usb/usbhist.h>
#endif
#ifdef BIOHIST
#include <sys/biohist.h>
#endif
#ifdef SYSCALL_DEBUG
KERNHIST_DECL(scdebughist);
#endif
struct addr_xlt {
const char *addr;
size_t len;
uint32_t offset;
};
struct kern_history_head kern_histories;
bool kernhist_sysctl_ready = 0;
int kernhist_print_enabled = 1;
int sysctl_hist_node;
static int sysctl_kernhist_helper(SYSCTLFN_PROTO);
#ifdef DDB
void kernhist_dump(struct kern_history *, size_t count,
void (*)(const char *, ...) __printflike(1, 2));
static void kernhist_info(struct kern_history *,
void (*)(const char *, ...));
void kernhist_dumpmask(uint32_t);
static void kernhist_dump_histories(struct kern_history *[], size_t count,
void (*)(const char *, ...) __printflike(1, 2));
static void
kernhist_info(struct kern_history *l, void (*pr)(const char *, ...))
{
pr("kernhist '%s': at %p total %u next free %u\n",
l->name, l, l->n, l->f);
}
void
kernhist_dump(struct kern_history *l, size_t count,
void (*pr)(const char *, ...))
{
int lcv;
lcv = l->f;
if (count > l->n)
pr("%s: count %zu > size %u\n", __func__, count, l->n);
else if (count)
lcv = (lcv - count) % l->n;
do {
if (l->e[lcv].fmt)
kernhist_entry_print(&l->e[lcv], pr);
lcv = (lcv + 1) % l->n;
} while (lcv != l->f);
}
static void
kernhist_dump_histories(struct kern_history *hists[], size_t count,
void (*pr)(const char *, ...))
{
struct bintime bt;
int cur[MAXHISTS];
int lcv, hi;
for (lcv = 0; hists[lcv]; lcv++)
cur[lcv] = hists[lcv]->f;
for (;;) {
hi = -1;
bt.sec = 0; bt.frac = 0;
for (lcv = 0; hists[lcv]; lcv++) {
restart:
if (cur[lcv] == -1)
continue;
if (!hists[lcv]->e)
continue;
if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
if (cur[lcv] == hists[lcv]->f)
cur[lcv] = -1;
goto restart;
}
if (bt.sec == 0 ||
bintimecmp(&hists[lcv]->e[cur[lcv]].bt, &bt, <)) {
bt = hists[lcv]->e[cur[lcv]].bt;
hi = lcv;
}
}
if (hi == -1)
break;
kernhist_entry_print(&hists[hi]->e[cur[hi]], pr);
cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
if (cur[hi] == hists[hi]->f)
cur[hi] = -1;
}
}
void
kernhist_dumpmask(uint32_t bitmask)
{
struct kern_history *hists[MAXHISTS + 1];
int i = 0;
#ifdef UVMHIST
if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
hists[i++] = &maphist;
if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
hists[i++] = &pdhist;
if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
hists[i++] = &ubchist;
if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
hists[i++] = &loanhist;
#endif
#ifdef USB_DEBUG
if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
hists[i++] = &usbhist;
#endif
#ifdef SYSCALL_DEBUG
if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
hists[i++] = &scdebughist;
#endif
#ifdef BIOHIST
if ((bitmask & KERNHIST_BIOHIST) || bitmask == 0)
hists[i++] = &biohist;
#endif
hists[i] = NULL;
kernhist_dump_histories(hists, 0, printf);
}
void
kernhist_print(void *addr, size_t count, const char *modif,
void (*pr)(const char *, ...) __printflike(1,2))
{
struct kern_history *h;
LIST_FOREACH(h, &kern_histories, list) {
if (h == addr)
break;
}
if (h == NULL) {
struct kern_history *hists[MAXHISTS + 1];
int i = 0;
#ifdef UVMHIST
hists[i++] = &maphist;
hists[i++] = &pdhist;
hists[i++] = &ubchist;
hists[i++] = &loanhist;
#endif
#ifdef USB_DEBUG
hists[i++] = &usbhist;
#endif
#ifdef SYSCALL_DEBUG
hists[i++] = &scdebughist;
#endif
#ifdef BIOHIST
hists[i++] = &biohist;
#endif
hists[i] = NULL;
if (*modif == 'i') {
int lcv;
for (lcv = 0; hists[lcv]; lcv++)
kernhist_info(hists[lcv], pr);
} else {
kernhist_dump_histories(hists, count, pr);
}
} else {
if (*modif == 'i')
kernhist_info(h, pr);
else
kernhist_dump(h, count, pr);
}
}
#endif
void
sysctl_kernhist_new(struct kern_history *hist)
{
int error;
struct kern_history *h;
const struct sysctlnode *rnode = NULL;
membar_consumer();
if (kernhist_sysctl_ready == 0)
return;
LIST_FOREACH(h, &kern_histories, list) {
if (hist && h != hist)
continue;
if (h->s != 0)
continue;
error = sysctl_createv(NULL, 0, NULL, &rnode,
CTLFLAG_PERMANENT,
CTLTYPE_STRUCT, h->name,
SYSCTL_DESCR("history data"),
sysctl_kernhist_helper, 0, NULL, 0,
CTL_KERN, sysctl_hist_node, CTL_CREATE, CTL_EOL);
if (error == 0)
h->s = rnode->sysctl_num;
if (hist == h)
break;
}
}
void
sysctl_kernhist_init(void)
{
const struct sysctlnode *rnode = NULL;
sysctl_createv(NULL, 0, NULL, &rnode,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "hist",
SYSCTL_DESCR("kernel history tables"),
sysctl_kernhist_helper, 0, NULL, 0,
CTL_KERN, CTL_CREATE, CTL_EOL);
sysctl_hist_node = rnode->sysctl_num;
kernhist_sysctl_ready = 1;
membar_producer();
sysctl_kernhist_new(NULL);
}
static int
find_string(struct addr_xlt table[], size_t *count, const char *string,
size_t len)
{
int i;
for (i = 1; i < *count; i++)
if (string == table[i].addr && len == table[i].len)
return i;
return 0;
}
static void
add_string(struct addr_xlt table[], size_t *count, const char *string,
size_t len)
{
if (find_string(table, count, string, len) == 0) {
table[*count].addr = string;
table[*count].len = len;
table[*count + 1].offset = table[*count].offset + len + 1;
(*count)++;
}
}
static int
sysctl_kernhist_helper(SYSCTLFN_ARGS)
{
struct kern_history *h;
struct kern_history_ent *in_evt;
struct sysctl_history_event *out_evt;
struct sysctl_history *buf;
struct addr_xlt *xlate_t, *xlt;
size_t bufsize, xlate_s;
size_t xlate_c;
const char *strp __diagused;
char *next;
int i, j;
int error;
if (namelen == 1 && name[0] == CTL_QUERY)
return sysctl_query(SYSCTLFN_CALL(rnode));
if (newp)
return SET_ERROR(EPERM);
if (namelen != 1 || name[0] != CTL_EOL)
return SET_ERROR(EINVAL);
LIST_FOREACH(h, &kern_histories, list) {
if (h->s == rnode->sysctl_num)
break;
}
if (h == NULL)
return SET_ERROR(ENOENT);
xlate_s = sizeof(struct addr_xlt) * h->n * 2 + 3;
xlate_t = kmem_alloc(xlate_s, KM_SLEEP);
xlate_c = 0;
xlate_t[0].offset = 1;
add_string(xlate_t, &xlate_c, "?", 0);
add_string(xlate_t, &xlate_c, h->name, h->namelen);
for (i = 0, in_evt = h->e; i < h->n; i++, in_evt++) {
if (in_evt->fn == NULL)
continue;
add_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
add_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
}
bufsize = sizeof(struct sysctl_history) +
h->n * sizeof(struct sysctl_history_event) +
xlate_t[xlate_c].offset;
buf = kmem_alloc(bufsize, KM_SLEEP);
j = find_string(xlate_t, &xlate_c, h->name, h->namelen);
buf->sh_nameoffset = xlate_t[j].offset;
buf->sh_numentries = h->n;
buf->sh_nextfree = h->f;
for (i = 0, in_evt = h->e, out_evt = buf->sh_events; i < h->n;
i++, in_evt++, out_evt++) {
if (in_evt->fn == NULL) {
out_evt->she_funcoffset = 0;
out_evt->she_fmtoffset = 0;
continue;
}
out_evt->she_bintime = in_evt->bt;
out_evt->she_callnumber = in_evt->call;
out_evt->she_cpunum = in_evt->cpunum;
out_evt->she_values[0] = in_evt->v[0];
out_evt->she_values[1] = in_evt->v[1];
out_evt->she_values[2] = in_evt->v[2];
out_evt->she_values[3] = in_evt->v[3];
j = find_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
out_evt->she_funcoffset = xlate_t[j].offset;
j = find_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
out_evt->she_fmtoffset = xlate_t[j].offset;
}
strp = next = (char *)(&buf->sh_events[h->n]);
*next++ = '\0';
for (i = 0, xlt = xlate_t; i < xlate_c; i++, xlt++) {
KASSERTMSG((next - strp) == xlt->offset,
"entry %d at wrong offset %"PRIu32, i, xlt->offset);
memcpy(next, xlt->addr, xlt->len);
next += xlt->len;
*next++ = '\0';
}
error = copyout(buf, oldp, uimin(bufsize, *oldlenp));
if (error == 0 && *oldlenp < bufsize)
error = SET_ERROR(ENOMEM);
*oldlenp = bufsize;
kmem_free(buf, bufsize);
kmem_free(xlate_t, xlate_s);
return error;
}