Home | History | Annotate | Line # | Download | only in kern
kern_history.c revision 1.6.2.1
      1  1.6.2.1  pgoyette /*	$NetBSD: kern_history.c,v 1.6.2.1 2017/01/07 08:56:49 pgoyette Exp $	 */
      2      1.1       mrg 
      3      1.1       mrg /*
      4      1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5      1.1       mrg  * All rights reserved.
      6      1.1       mrg  *
      7      1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8      1.1       mrg  * modification, are permitted provided that the following conditions
      9      1.1       mrg  * are met:
     10      1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11      1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12      1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14      1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15      1.1       mrg  *
     16      1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.1       mrg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.1       mrg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.1       mrg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.1       mrg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.1       mrg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.1       mrg  *
     27      1.1       mrg  * from: NetBSD: uvm_stat.c,v 1.36 2011/02/02 15:13:34 chuck Exp
     28      1.1       mrg  * from: Id: uvm_stat.c,v 1.1.2.3 1997/12/19 15:01:00 mrg Exp
     29      1.1       mrg  */
     30      1.1       mrg 
     31      1.1       mrg /*
     32      1.1       mrg  * subr_kernhist.c
     33      1.1       mrg  */
     34      1.1       mrg 
     35      1.1       mrg #include <sys/cdefs.h>
     36  1.6.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: kern_history.c,v 1.6.2.1 2017/01/07 08:56:49 pgoyette Exp $");
     37      1.1       mrg 
     38      1.5     skrll #include "opt_ddb.h"
     39      1.1       mrg #include "opt_kernhist.h"
     40      1.5     skrll #include "opt_syscall_debug.h"
     41      1.5     skrll #include "opt_usb.h"
     42      1.1       mrg #include "opt_uvmhist.h"
     43  1.6.2.1  pgoyette #include "opt_biohist.h"
     44  1.6.2.1  pgoyette #include "opt_sysctl.h"
     45      1.1       mrg 
     46      1.1       mrg #include <sys/param.h>
     47      1.1       mrg #include <sys/systm.h>
     48      1.1       mrg #include <sys/cpu.h>
     49  1.6.2.1  pgoyette #include <sys/sysctl.h>
     50      1.1       mrg #include <sys/kernhist.h>
     51  1.6.2.1  pgoyette #include <sys/kmem.h>
     52      1.1       mrg 
     53      1.2       mrg #ifdef UVMHIST
     54      1.1       mrg #include <uvm/uvm.h>
     55      1.2       mrg #endif
     56      1.2       mrg 
     57      1.2       mrg #ifdef USB_DEBUG
     58      1.2       mrg #include <dev/usb/usbhist.h>
     59      1.2       mrg #endif
     60      1.2       mrg 
     61  1.6.2.1  pgoyette #ifdef BIOHIST
     62  1.6.2.1  pgoyette #include <sys/biohist.h>
     63  1.6.2.1  pgoyette #endif
     64  1.6.2.1  pgoyette 
     65      1.2       mrg #ifdef SYSCALL_DEBUG
     66      1.2       mrg KERNHIST_DECL(scdebughist);
     67      1.2       mrg #endif
     68      1.1       mrg 
     69  1.6.2.1  pgoyette struct addr_xlt {
     70  1.6.2.1  pgoyette 	const char *addr;
     71  1.6.2.1  pgoyette 	size_t len;
     72  1.6.2.1  pgoyette 	uint32_t offset;
     73  1.6.2.1  pgoyette };
     74  1.6.2.1  pgoyette 
     75      1.1       mrg /*
     76      1.1       mrg  * globals
     77      1.1       mrg  */
     78      1.1       mrg 
     79      1.1       mrg struct kern_history_head kern_histories;
     80      1.1       mrg 
     81      1.1       mrg int kernhist_print_enabled = 1;
     82      1.1       mrg 
     83  1.6.2.1  pgoyette int sysctl_hist_node;
     84  1.6.2.1  pgoyette 
     85      1.1       mrg #ifdef DDB
     86      1.1       mrg 
     87      1.1       mrg /*
     88      1.1       mrg  * prototypes
     89      1.1       mrg  */
     90      1.1       mrg 
     91      1.4     skrll void kernhist_dump(struct kern_history *,
     92      1.4     skrll     void (*)(const char *, ...) __printflike(1, 2));
     93  1.6.2.1  pgoyette void kernhist_dumpmask(uint32_t);
     94      1.4     skrll static void kernhist_dump_histories(struct kern_history *[],
     95      1.4     skrll     void (*)(const char *, ...) __printflike(1, 2));
     96      1.1       mrg 
     97  1.6.2.1  pgoyette static int sysctl_kernhist_helper(SYSCTLFN_PROTO);
     98      1.1       mrg 
     99      1.1       mrg /*
    100      1.1       mrg  * call this from ddb
    101      1.1       mrg  *
    102      1.1       mrg  * expects the system to be quiesced, no locking
    103      1.1       mrg  */
    104      1.1       mrg void
    105      1.4     skrll kernhist_dump(struct kern_history *l, void (*pr)(const char *, ...))
    106      1.1       mrg {
    107      1.1       mrg 	int lcv;
    108      1.1       mrg 
    109      1.1       mrg 	lcv = l->f;
    110      1.1       mrg 	do {
    111      1.1       mrg 		if (l->e[lcv].fmt)
    112      1.4     skrll 			kernhist_entry_print(&l->e[lcv], pr);
    113      1.1       mrg 		lcv = (lcv + 1) % l->n;
    114      1.1       mrg 	} while (lcv != l->f);
    115      1.1       mrg }
    116      1.1       mrg 
    117      1.1       mrg /*
    118      1.1       mrg  * print a merged list of kern_history structures
    119      1.1       mrg  */
    120      1.1       mrg static void
    121      1.4     skrll kernhist_dump_histories(struct kern_history *hists[], void (*pr)(const char *, ...))
    122      1.1       mrg {
    123  1.6.2.1  pgoyette 	struct bintime	bt;
    124      1.1       mrg 	int	cur[MAXHISTS];
    125      1.1       mrg 	int	lcv, hi;
    126      1.1       mrg 
    127      1.1       mrg 	/* find the first of each list */
    128      1.1       mrg 	for (lcv = 0; hists[lcv]; lcv++)
    129      1.1       mrg 		 cur[lcv] = hists[lcv]->f;
    130      1.1       mrg 
    131      1.1       mrg 	/*
    132      1.1       mrg 	 * here we loop "forever", finding the next earliest
    133      1.1       mrg 	 * history entry and printing it.  cur[X] is the current
    134      1.1       mrg 	 * entry to test for the history in hists[X].  if it is
    135      1.1       mrg 	 * -1, then this history is finished.
    136      1.1       mrg 	 */
    137      1.1       mrg 	for (;;) {
    138      1.1       mrg 		hi = -1;
    139  1.6.2.1  pgoyette 		bt.sec = 0; bt.frac = 0;
    140      1.1       mrg 
    141      1.1       mrg 		/* loop over each history */
    142      1.1       mrg 		for (lcv = 0; hists[lcv]; lcv++) {
    143      1.1       mrg restart:
    144      1.1       mrg 			if (cur[lcv] == -1)
    145      1.1       mrg 				continue;
    146      1.2       mrg 			if (!hists[lcv]->e)
    147      1.2       mrg 				continue;
    148      1.1       mrg 
    149      1.1       mrg 			/*
    150      1.1       mrg 			 * if the format is empty, go to the next entry
    151      1.1       mrg 			 * and retry.
    152      1.1       mrg 			 */
    153      1.1       mrg 			if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
    154      1.1       mrg 				cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
    155      1.1       mrg 				if (cur[lcv] == hists[lcv]->f)
    156      1.1       mrg 					cur[lcv] = -1;
    157      1.1       mrg 				goto restart;
    158      1.1       mrg 			}
    159      1.1       mrg 
    160      1.1       mrg 			/*
    161      1.1       mrg 			 * if the time hasn't been set yet, or this entry is
    162  1.6.2.1  pgoyette 			 * earlier than the current bt, set the time and history
    163      1.1       mrg 			 * index.
    164      1.1       mrg 			 */
    165  1.6.2.1  pgoyette 			if (bt.sec == 0 ||
    166  1.6.2.1  pgoyette 			    bintimecmp(&hists[lcv]->e[cur[lcv]].bt, &bt, <)) {
    167  1.6.2.1  pgoyette 				bt = hists[lcv]->e[cur[lcv]].bt;
    168      1.1       mrg 				hi = lcv;
    169      1.1       mrg 			}
    170      1.1       mrg 		}
    171      1.1       mrg 
    172      1.1       mrg 		/* if we didn't find any entries, we must be done */
    173      1.1       mrg 		if (hi == -1)
    174      1.1       mrg 			break;
    175      1.1       mrg 
    176      1.1       mrg 		/* print and move to the next entry */
    177      1.4     skrll 		kernhist_entry_print(&hists[hi]->e[cur[hi]], pr);
    178      1.1       mrg 		cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
    179      1.1       mrg 		if (cur[hi] == hists[hi]->f)
    180      1.1       mrg 			cur[hi] = -1;
    181      1.1       mrg 	}
    182      1.1       mrg }
    183      1.1       mrg 
    184      1.1       mrg /*
    185      1.1       mrg  * call this from ddb.  `bitmask' is from <sys/kernhist.h>.  it
    186      1.1       mrg  * merges the named histories.
    187      1.1       mrg  *
    188      1.1       mrg  * expects the system to be quiesced, no locking
    189      1.1       mrg  */
    190      1.1       mrg void
    191  1.6.2.1  pgoyette kernhist_dumpmask(uint32_t bitmask)	/* XXX only support 32 hists */
    192      1.1       mrg {
    193      1.1       mrg 	struct kern_history *hists[MAXHISTS + 1];
    194      1.1       mrg 	int i = 0;
    195      1.1       mrg 
    196      1.1       mrg #ifdef UVMHIST
    197      1.1       mrg 	if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
    198      1.1       mrg 		hists[i++] = &maphist;
    199      1.1       mrg 
    200      1.1       mrg 	if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
    201      1.1       mrg 		hists[i++] = &pdhist;
    202      1.1       mrg 
    203      1.1       mrg 	if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
    204      1.1       mrg 		hists[i++] = &ubchist;
    205      1.1       mrg 
    206      1.1       mrg 	if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
    207      1.1       mrg 		hists[i++] = &loanhist;
    208      1.1       mrg #endif
    209      1.1       mrg 
    210      1.2       mrg #ifdef USB_DEBUG
    211      1.2       mrg 	if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
    212      1.2       mrg 		hists[i++] = &usbhist;
    213      1.2       mrg #endif
    214      1.2       mrg 
    215      1.2       mrg #ifdef SYSCALL_DEBUG
    216      1.2       mrg 	if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
    217      1.2       mrg 		hists[i++] = &scdebughist;
    218      1.2       mrg #endif
    219      1.2       mrg 
    220  1.6.2.1  pgoyette #ifdef BIOHIST
    221  1.6.2.1  pgoyette 	if ((bitmask & KERNHIST_BIOHIST) || bitmask == 0)
    222  1.6.2.1  pgoyette 		hists[i++] = &biohist;
    223  1.6.2.1  pgoyette #endif
    224  1.6.2.1  pgoyette 
    225      1.1       mrg 	hists[i] = NULL;
    226      1.1       mrg 
    227      1.4     skrll 	kernhist_dump_histories(hists, printf);
    228      1.1       mrg }
    229      1.1       mrg 
    230      1.1       mrg /*
    231      1.1       mrg  * kernhist_print: ddb hook to print kern history
    232      1.1       mrg  */
    233      1.1       mrg void
    234      1.4     skrll kernhist_print(void *addr, void (*pr)(const char *, ...) __printflike(1,2))
    235      1.1       mrg {
    236      1.4     skrll 	struct kern_history *h;
    237      1.4     skrll 
    238      1.4     skrll 	LIST_FOREACH(h, &kern_histories, list) {
    239      1.4     skrll 		if (h == addr)
    240      1.4     skrll 			break;
    241      1.4     skrll 	}
    242      1.4     skrll 
    243      1.4     skrll 	if (h == NULL) {
    244      1.4     skrll 		struct kern_history *hists[MAXHISTS + 1];
    245      1.4     skrll 		int i = 0;
    246      1.4     skrll #ifdef UVMHIST
    247      1.4     skrll 		hists[i++] = &maphist;
    248      1.4     skrll 		hists[i++] = &pdhist;
    249      1.4     skrll 		hists[i++] = &ubchist;
    250      1.4     skrll 		hists[i++] = &loanhist;
    251      1.4     skrll #endif
    252      1.4     skrll #ifdef USB_DEBUG
    253      1.4     skrll 		hists[i++] = &usbhist;
    254      1.4     skrll #endif
    255      1.4     skrll 
    256      1.4     skrll #ifdef SYSCALL_DEBUG
    257      1.4     skrll 		hists[i++] = &scdebughist;
    258      1.4     skrll #endif
    259  1.6.2.1  pgoyette #ifdef BIOHIST
    260  1.6.2.1  pgoyette 		hists[i++] = &biohist;
    261  1.6.2.1  pgoyette #endif
    262      1.4     skrll 		hists[i] = NULL;
    263      1.4     skrll 
    264      1.4     skrll 		kernhist_dump_histories(hists, pr);
    265      1.4     skrll 	} else {
    266      1.4     skrll 		kernhist_dump(h, pr);
    267      1.4     skrll 	}
    268      1.1       mrg }
    269      1.1       mrg 
    270      1.1       mrg #endif
    271  1.6.2.1  pgoyette 
    272  1.6.2.1  pgoyette /*
    273  1.6.2.1  pgoyette  * sysctl interface
    274  1.6.2.1  pgoyette  */
    275  1.6.2.1  pgoyette 
    276  1.6.2.1  pgoyette /*
    277  1.6.2.1  pgoyette  * sysctl_hist_new()
    278  1.6.2.1  pgoyette  *
    279  1.6.2.1  pgoyette  *	Scan the list of histories;  for any history that does not already
    280  1.6.2.1  pgoyette  *	have a sysctl node (under kern.hist) we create a new one and record
    281  1.6.2.1  pgoyette  *	it's node number.
    282  1.6.2.1  pgoyette  */
    283  1.6.2.1  pgoyette static void
    284  1.6.2.1  pgoyette sysctl_hist_new(void)
    285  1.6.2.1  pgoyette {
    286  1.6.2.1  pgoyette 	int error;
    287  1.6.2.1  pgoyette 	struct kern_history *h;
    288  1.6.2.1  pgoyette 	const struct sysctlnode *rnode = NULL;
    289  1.6.2.1  pgoyette 
    290  1.6.2.1  pgoyette 	LIST_FOREACH(h, &kern_histories, list) {
    291  1.6.2.1  pgoyette 		if (h->s != 0)
    292  1.6.2.1  pgoyette 			continue;
    293  1.6.2.1  pgoyette 		error = sysctl_createv(NULL, 0, NULL, &rnode,
    294  1.6.2.1  pgoyette 			    CTLFLAG_PERMANENT,
    295  1.6.2.1  pgoyette 			    CTLTYPE_STRUCT, h->name,
    296  1.6.2.1  pgoyette 			    SYSCTL_DESCR("history data"),
    297  1.6.2.1  pgoyette 			    sysctl_kernhist_helper, 0, NULL, 0,
    298  1.6.2.1  pgoyette 			    CTL_KERN, sysctl_hist_node, CTL_CREATE, CTL_EOL);
    299  1.6.2.1  pgoyette 		if (error == 0)
    300  1.6.2.1  pgoyette 			h->s = rnode->sysctl_num;
    301  1.6.2.1  pgoyette 	}
    302  1.6.2.1  pgoyette }
    303  1.6.2.1  pgoyette 
    304  1.6.2.1  pgoyette /*
    305  1.6.2.1  pgoyette  * sysctl_kerhnist_init()
    306  1.6.2.1  pgoyette  *
    307  1.6.2.1  pgoyette  *	Create the 2nd level "hw.hist" sysctl node
    308  1.6.2.1  pgoyette  */
    309  1.6.2.1  pgoyette void
    310  1.6.2.1  pgoyette sysctl_kernhist_init(void)
    311  1.6.2.1  pgoyette {
    312  1.6.2.1  pgoyette 	const struct sysctlnode *rnode = NULL;
    313  1.6.2.1  pgoyette 
    314  1.6.2.1  pgoyette 	sysctl_createv(NULL, 0, NULL, &rnode,
    315  1.6.2.1  pgoyette 			CTLFLAG_PERMANENT,
    316  1.6.2.1  pgoyette 			CTLTYPE_NODE, "hist",
    317  1.6.2.1  pgoyette 			SYSCTL_DESCR("kernel history tables"),
    318  1.6.2.1  pgoyette 			sysctl_kernhist_helper, 0, NULL, 0,
    319  1.6.2.1  pgoyette 			CTL_KERN, CTL_CREATE, CTL_EOL);
    320  1.6.2.1  pgoyette 	sysctl_hist_node = rnode->sysctl_num;
    321  1.6.2.1  pgoyette 
    322  1.6.2.1  pgoyette 	sysctl_hist_new();
    323  1.6.2.1  pgoyette }
    324  1.6.2.1  pgoyette 
    325  1.6.2.1  pgoyette /*
    326  1.6.2.1  pgoyette  * find_string()
    327  1.6.2.1  pgoyette  *
    328  1.6.2.1  pgoyette  *	Search the address-to-offset translation table for matching an
    329  1.6.2.1  pgoyette  *	address and len, and return the index of the entry we found.  If
    330  1.6.2.1  pgoyette  *	not found, returns index 0 which points to the "?" entry.  (We
    331  1.6.2.1  pgoyette  *	start matching at index 1, ignoring any matches of the "?" entry
    332  1.6.2.1  pgoyette  *	itself.)
    333  1.6.2.1  pgoyette  */
    334  1.6.2.1  pgoyette static int
    335  1.6.2.1  pgoyette find_string(struct addr_xlt table[], size_t *count, const char *string,
    336  1.6.2.1  pgoyette 	    size_t len)
    337  1.6.2.1  pgoyette {
    338  1.6.2.1  pgoyette 	int i;
    339  1.6.2.1  pgoyette 
    340  1.6.2.1  pgoyette 	for (i = 1; i < *count; i++)
    341  1.6.2.1  pgoyette 		if (string == table[i].addr && len == table[i].len)
    342  1.6.2.1  pgoyette 			return i;
    343  1.6.2.1  pgoyette 
    344  1.6.2.1  pgoyette 	return 0;
    345  1.6.2.1  pgoyette }
    346  1.6.2.1  pgoyette 
    347  1.6.2.1  pgoyette /*
    348  1.6.2.1  pgoyette  * add_string()
    349  1.6.2.1  pgoyette  *
    350  1.6.2.1  pgoyette  *	If the string and len are unique, add a new address-to-offset
    351  1.6.2.1  pgoyette  *	entry in the translation table and set the offset of the next
    352  1.6.2.1  pgoyette  *	entry.
    353  1.6.2.1  pgoyette  */
    354  1.6.2.1  pgoyette static void
    355  1.6.2.1  pgoyette add_string(struct addr_xlt table[], size_t *count, const char *string,
    356  1.6.2.1  pgoyette 	   size_t len)
    357  1.6.2.1  pgoyette {
    358  1.6.2.1  pgoyette 
    359  1.6.2.1  pgoyette 	if (find_string(table, count, string, len) == 0) {
    360  1.6.2.1  pgoyette 		table[*count].addr = string;
    361  1.6.2.1  pgoyette 		table[*count].len = len;
    362  1.6.2.1  pgoyette 		table[*count + 1].offset = table[*count].offset + len + 1;
    363  1.6.2.1  pgoyette 		(*count)++;
    364  1.6.2.1  pgoyette 	}
    365  1.6.2.1  pgoyette }
    366  1.6.2.1  pgoyette 
    367  1.6.2.1  pgoyette /*
    368  1.6.2.1  pgoyette  * sysctl_kernhist_helper
    369  1.6.2.1  pgoyette  *
    370  1.6.2.1  pgoyette  *	This helper routine is called for all accesses to the kern.hist
    371  1.6.2.1  pgoyette  *	hierarchy.
    372  1.6.2.1  pgoyette  */
    373  1.6.2.1  pgoyette static int
    374  1.6.2.1  pgoyette sysctl_kernhist_helper(SYSCTLFN_ARGS)
    375  1.6.2.1  pgoyette {
    376  1.6.2.1  pgoyette 	struct kern_history *h;
    377  1.6.2.1  pgoyette 	struct kern_history_ent *in_evt;
    378  1.6.2.1  pgoyette 	struct sysctl_history_event *out_evt;
    379  1.6.2.1  pgoyette 	struct sysctl_history *buf;
    380  1.6.2.1  pgoyette 	struct addr_xlt *xlate_t, *xlt;
    381  1.6.2.1  pgoyette 	size_t bufsize, xlate_s;
    382  1.6.2.1  pgoyette 	size_t xlate_c;
    383  1.6.2.1  pgoyette 	const char *strp;
    384  1.6.2.1  pgoyette 	char *next;
    385  1.6.2.1  pgoyette 	int i, j;
    386  1.6.2.1  pgoyette 	int error;
    387  1.6.2.1  pgoyette 
    388  1.6.2.1  pgoyette 	sysctl_hist_new();	/* make sure we're up to date */
    389  1.6.2.1  pgoyette 
    390  1.6.2.1  pgoyette 	if (namelen == 1 && name[0] == CTL_QUERY)
    391  1.6.2.1  pgoyette 		return sysctl_query(SYSCTLFN_CALL(rnode));
    392  1.6.2.1  pgoyette 
    393  1.6.2.1  pgoyette 	/*
    394  1.6.2.1  pgoyette 	 * Disallow userland updates, verify that we arrived at a
    395  1.6.2.1  pgoyette 	 * valid history rnode
    396  1.6.2.1  pgoyette 	 */
    397  1.6.2.1  pgoyette 	if (newp)
    398  1.6.2.1  pgoyette 		return EPERM;
    399  1.6.2.1  pgoyette 	if (namelen != 1 || name[0] != CTL_EOL)
    400  1.6.2.1  pgoyette 		return EINVAL;
    401  1.6.2.1  pgoyette 
    402  1.6.2.1  pgoyette 	/* Find the correct kernhist for this sysctl node */
    403  1.6.2.1  pgoyette 	LIST_FOREACH(h, &kern_histories, list) {
    404  1.6.2.1  pgoyette 		if (h->s == rnode->sysctl_num)
    405  1.6.2.1  pgoyette 			break;
    406  1.6.2.1  pgoyette 	}
    407  1.6.2.1  pgoyette 	if (h == NULL)
    408  1.6.2.1  pgoyette 		return ENOENT;
    409  1.6.2.1  pgoyette 
    410  1.6.2.1  pgoyette 	/*
    411  1.6.2.1  pgoyette 	 * Worst case is two string pointers per history entry, plus
    412  1.6.2.1  pgoyette 	 * two for the history name and "?" string; allocate an extra
    413  1.6.2.1  pgoyette 	 * entry since we pre-set the "next" entry's offset member.
    414  1.6.2.1  pgoyette 	 */
    415  1.6.2.1  pgoyette 	xlate_s = sizeof(struct addr_xlt) * h->n * 2 + 3;
    416  1.6.2.1  pgoyette 	xlate_t = kmem_alloc(xlate_s, KM_SLEEP);
    417  1.6.2.1  pgoyette 	xlate_c = 0;
    418  1.6.2.1  pgoyette 
    419  1.6.2.1  pgoyette 	/* offset 0 reserved for NULL pointer, ie unused history entry */
    420  1.6.2.1  pgoyette 	xlate_t[0].offset = 1;
    421  1.6.2.1  pgoyette 
    422  1.6.2.1  pgoyette 	/*
    423  1.6.2.1  pgoyette 	 * If the history gets updated and an unexpected string is
    424  1.6.2.1  pgoyette 	 * found later, we'll point it here.  Otherwise, we'd have to
    425  1.6.2.1  pgoyette 	 * repeat this process iteratively, and it could take multiple
    426  1.6.2.1  pgoyette 	 * iterations before terminating.
    427  1.6.2.1  pgoyette 	 */
    428  1.6.2.1  pgoyette 	add_string(xlate_t, &xlate_c, "?", 0);
    429  1.6.2.1  pgoyette 
    430  1.6.2.1  pgoyette 	/* Copy the history name itself to the export structure */
    431  1.6.2.1  pgoyette 	add_string(xlate_t, &xlate_c, h->name, h->namelen);
    432  1.6.2.1  pgoyette 
    433  1.6.2.1  pgoyette 	/*
    434  1.6.2.1  pgoyette 	 * Loop through all used history entries to find the unique
    435  1.6.2.1  pgoyette 	 * fn and fmt strings
    436  1.6.2.1  pgoyette 	 */
    437  1.6.2.1  pgoyette 	for (i = 0, in_evt = h->e; i < h->n; i++, in_evt++) {
    438  1.6.2.1  pgoyette 		if (in_evt->fn == NULL)
    439  1.6.2.1  pgoyette 			continue;
    440  1.6.2.1  pgoyette 		add_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
    441  1.6.2.1  pgoyette 		add_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
    442  1.6.2.1  pgoyette 	}
    443  1.6.2.1  pgoyette 
    444  1.6.2.1  pgoyette 	/* Total buffer size includes header, events, and string table */
    445  1.6.2.1  pgoyette 	bufsize = sizeof(struct sysctl_history) +
    446  1.6.2.1  pgoyette 	    h->n * sizeof(struct sysctl_history_event) +
    447  1.6.2.1  pgoyette 	    xlate_t[xlate_c].offset;
    448  1.6.2.1  pgoyette 	buf = kmem_alloc(bufsize, KM_SLEEP);
    449  1.6.2.1  pgoyette 
    450  1.6.2.1  pgoyette 	/*
    451  1.6.2.1  pgoyette 	 * Copy history header info to the export structure
    452  1.6.2.1  pgoyette 	 */
    453  1.6.2.1  pgoyette 	j = find_string(xlate_t, &xlate_c, h->name, h->namelen);
    454  1.6.2.1  pgoyette 	buf->sh_listentry.shle_nameoffset = xlate_t[j].offset;
    455  1.6.2.1  pgoyette 	buf->sh_listentry.shle_numentries = h->n;
    456  1.6.2.1  pgoyette 	buf->sh_listentry.shle_nextfree = h->f;
    457  1.6.2.1  pgoyette 
    458  1.6.2.1  pgoyette 	/*
    459  1.6.2.1  pgoyette 	 * Loop through the history events again, copying the data to
    460  1.6.2.1  pgoyette 	 * the export structure
    461  1.6.2.1  pgoyette 	 */
    462  1.6.2.1  pgoyette 	for (i = 0, in_evt = h->e, out_evt = buf->sh_events; i < h->n;
    463  1.6.2.1  pgoyette 	    i++, in_evt++, out_evt++) {
    464  1.6.2.1  pgoyette 		if (in_evt->fn == NULL) {	/* skip unused entries */
    465  1.6.2.1  pgoyette 			out_evt->she_funcoffset = 0;
    466  1.6.2.1  pgoyette 			out_evt->she_fmtoffset = 0;
    467  1.6.2.1  pgoyette 			continue;
    468  1.6.2.1  pgoyette 		}
    469  1.6.2.1  pgoyette 		out_evt->she_bintime = in_evt->bt;
    470  1.6.2.1  pgoyette 		out_evt->she_callnumber = in_evt->call;
    471  1.6.2.1  pgoyette 		out_evt->she_cpunum = in_evt->cpunum;
    472  1.6.2.1  pgoyette 		out_evt->she_values[0] = in_evt->v[0];
    473  1.6.2.1  pgoyette 		out_evt->she_values[1] = in_evt->v[1];
    474  1.6.2.1  pgoyette 		out_evt->she_values[2] = in_evt->v[2];
    475  1.6.2.1  pgoyette 		out_evt->she_values[3] = in_evt->v[3];
    476  1.6.2.1  pgoyette 		j = find_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
    477  1.6.2.1  pgoyette 		out_evt->she_funcoffset = xlate_t[j].offset;
    478  1.6.2.1  pgoyette 		j = find_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
    479  1.6.2.1  pgoyette 		out_evt->she_fmtoffset = xlate_t[j].offset;
    480  1.6.2.1  pgoyette 	}
    481  1.6.2.1  pgoyette 
    482  1.6.2.1  pgoyette 	/*
    483  1.6.2.1  pgoyette 	 * Finally, fill the text string area with all the unique
    484  1.6.2.1  pgoyette 	 * strings we found earlier.
    485  1.6.2.1  pgoyette 	 *
    486  1.6.2.1  pgoyette 	 * Skip the initial byte, since we use an offset of 0 to mean
    487  1.6.2.1  pgoyette 	 * a NULL pointer (which means an unused history event).
    488  1.6.2.1  pgoyette 	 */
    489  1.6.2.1  pgoyette 	strp = next = (char *)(&buf->sh_events[h->n]);
    490  1.6.2.1  pgoyette 	*next++ = '\0';
    491  1.6.2.1  pgoyette 
    492  1.6.2.1  pgoyette 	/*
    493  1.6.2.1  pgoyette 	 * Then copy each string into the export structure, making
    494  1.6.2.1  pgoyette 	 * sure to terminate each string with a '\0' character
    495  1.6.2.1  pgoyette 	 */
    496  1.6.2.1  pgoyette 	for (i = 0, xlt = xlate_t; i < xlate_c; i++, xlt++) {
    497  1.6.2.1  pgoyette 		KASSERTMSG((next - strp) == xlt->offset,
    498  1.6.2.1  pgoyette 		    "entry %d at wrong offset %"PRIu32, i, xlt->offset);
    499  1.6.2.1  pgoyette 		memcpy(next, xlt->addr, xlt->len);
    500  1.6.2.1  pgoyette 		next += xlt->len;
    501  1.6.2.1  pgoyette 		*next++ = '\0';
    502  1.6.2.1  pgoyette 	}
    503  1.6.2.1  pgoyette 
    504  1.6.2.1  pgoyette 	/* Copy data to userland */
    505  1.6.2.1  pgoyette 	error = copyout(buf, oldp, min(bufsize, *oldlenp));
    506  1.6.2.1  pgoyette 
    507  1.6.2.1  pgoyette 	/* If copyout was successful but only partial, report ENOMEM */
    508  1.6.2.1  pgoyette 	if (error == 0 && *oldlenp < bufsize)
    509  1.6.2.1  pgoyette 		error = ENOMEM;
    510  1.6.2.1  pgoyette 
    511  1.6.2.1  pgoyette 	*oldlenp = bufsize;	/* inform userland of space requirements */
    512  1.6.2.1  pgoyette 
    513  1.6.2.1  pgoyette 	/* Free up the stuff we allocated */
    514  1.6.2.1  pgoyette 	kmem_free(buf, bufsize);
    515  1.6.2.1  pgoyette 	kmem_free(xlate_t, xlate_s);
    516  1.6.2.1  pgoyette 
    517  1.6.2.1  pgoyette 	return error;
    518  1.6.2.1  pgoyette }
    519