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