Home | History | Annotate | Line # | Download | only in kern
kern_history.c revision 1.2
      1 /*	$NetBSD: kern_history.c,v 1.2 2015/10/29 00:27:08 mrg Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * from: NetBSD: uvm_stat.c,v 1.36 2011/02/02 15:13:34 chuck Exp
     28  * from: Id: uvm_stat.c,v 1.1.2.3 1997/12/19 15:01:00 mrg Exp
     29  */
     30 
     31 /*
     32  * subr_kernhist.c
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: kern_history.c,v 1.2 2015/10/29 00:27:08 mrg Exp $");
     37 
     38 #include "opt_kernhist.h"
     39 #include "opt_ddb.h"
     40 #include "opt_uvmhist.h"
     41 #include "opt_usb.h"
     42 #include "opt_syscall_debug.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/cpu.h>
     47 #include <sys/kernhist.h>
     48 
     49 #ifdef UVMHIST
     50 #include <uvm/uvm.h>
     51 #endif
     52 
     53 #ifdef USB_DEBUG
     54 #include <dev/usb/usbhist.h>
     55 #endif
     56 
     57 #ifdef SYSCALL_DEBUG
     58 KERNHIST_DECL(scdebughist);
     59 #endif
     60 
     61 /*
     62  * globals
     63  */
     64 
     65 struct kern_history_head kern_histories;
     66 
     67 int kernhist_print_enabled = 1;
     68 
     69 #ifdef DDB
     70 
     71 /*
     72  * prototypes
     73  */
     74 
     75 void kernhist_dump(struct kern_history *);
     76 void kernhist_dumpmask(u_int32_t);
     77 static void kernhist_dump_histories(struct kern_history *[]);
     78 
     79 
     80 /*
     81  * call this from ddb
     82  *
     83  * expects the system to be quiesced, no locking
     84  */
     85 void
     86 kernhist_dump(struct kern_history *l)
     87 {
     88 	int lcv;
     89 
     90 	lcv = l->f;
     91 	do {
     92 		if (l->e[lcv].fmt)
     93 			kernhist_entry_print(&l->e[lcv]);
     94 		lcv = (lcv + 1) % l->n;
     95 	} while (lcv != l->f);
     96 }
     97 
     98 /*
     99  * print a merged list of kern_history structures
    100  */
    101 static void
    102 kernhist_dump_histories(struct kern_history *hists[])
    103 {
    104 	struct timeval  tv;
    105 	int	cur[MAXHISTS];
    106 	int	lcv, hi;
    107 
    108 	/* find the first of each list */
    109 	for (lcv = 0; hists[lcv]; lcv++)
    110 		 cur[lcv] = hists[lcv]->f;
    111 
    112 	/*
    113 	 * here we loop "forever", finding the next earliest
    114 	 * history entry and printing it.  cur[X] is the current
    115 	 * entry to test for the history in hists[X].  if it is
    116 	 * -1, then this history is finished.
    117 	 */
    118 	for (;;) {
    119 		hi = -1;
    120 		tv.tv_sec = tv.tv_usec = 0;
    121 
    122 		/* loop over each history */
    123 		for (lcv = 0; hists[lcv]; lcv++) {
    124 restart:
    125 			if (cur[lcv] == -1)
    126 				continue;
    127 			if (!hists[lcv]->e)
    128 				continue;
    129 
    130 			/*
    131 			 * if the format is empty, go to the next entry
    132 			 * and retry.
    133 			 */
    134 			if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
    135 				cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
    136 				if (cur[lcv] == hists[lcv]->f)
    137 					cur[lcv] = -1;
    138 				goto restart;
    139 			}
    140 
    141 			/*
    142 			 * if the time hasn't been set yet, or this entry is
    143 			 * earlier than the current tv, set the time and history
    144 			 * index.
    145 			 */
    146 			if (tv.tv_sec == 0 ||
    147 			    timercmp(&hists[lcv]->e[cur[lcv]].tv, &tv, <)) {
    148 				tv = hists[lcv]->e[cur[lcv]].tv;
    149 				hi = lcv;
    150 			}
    151 		}
    152 
    153 		/* if we didn't find any entries, we must be done */
    154 		if (hi == -1)
    155 			break;
    156 
    157 		/* print and move to the next entry */
    158 		kernhist_entry_print(&hists[hi]->e[cur[hi]]);
    159 		cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
    160 		if (cur[hi] == hists[hi]->f)
    161 			cur[hi] = -1;
    162 	}
    163 }
    164 
    165 /*
    166  * call this from ddb.  `bitmask' is from <sys/kernhist.h>.  it
    167  * merges the named histories.
    168  *
    169  * expects the system to be quiesced, no locking
    170  */
    171 void
    172 kernhist_dumpmask(u_int32_t bitmask)	/* XXX only support 32 hists */
    173 {
    174 	struct kern_history *hists[MAXHISTS + 1];
    175 	int i = 0;
    176 
    177 #ifdef UVMHIST
    178 	if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
    179 		hists[i++] = &maphist;
    180 
    181 	if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
    182 		hists[i++] = &pdhist;
    183 
    184 	if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
    185 		hists[i++] = &ubchist;
    186 
    187 	if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
    188 		hists[i++] = &loanhist;
    189 #endif
    190 
    191 #ifdef USB_DEBUG
    192 	if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
    193 		hists[i++] = &usbhist;
    194 #endif
    195 
    196 #ifdef SYSCALL_DEBUG
    197 	if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
    198 		hists[i++] = &scdebughist;
    199 #endif
    200 
    201 	hists[i] = NULL;
    202 
    203 	kernhist_dump_histories(hists);
    204 }
    205 
    206 /*
    207  * kernhist_print: ddb hook to print kern history
    208  */
    209 void
    210 kernhist_print(void (*pr)(const char *, ...))
    211 {
    212 	kernhist_dump(LIST_FIRST(&kern_histories));
    213 }
    214 
    215 #endif
    216