kern_history.c revision 1.1.36.1 1 /* $NetBSD: kern_history.c,v 1.1.36.1 2015/12/27 12:10:05 skrll 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.1.36.1 2015/12/27 12:10:05 skrll 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 #include "usb.h"
50 #if NUSB == 0
51 #undef USB_DEBUG
52 #endif
53
54 #ifdef UVMHIST
55 #include <uvm/uvm.h>
56 #endif
57
58 #ifdef USB_DEBUG
59 #include <dev/usb/usbhist.h>
60 #endif
61
62 #ifdef SYSCALL_DEBUG
63 KERNHIST_DECL(scdebughist);
64 #endif
65
66 /*
67 * globals
68 */
69
70 struct kern_history_head kern_histories;
71
72 int kernhist_print_enabled = 1;
73
74 #ifdef DDB
75
76 /*
77 * prototypes
78 */
79
80 void kernhist_dump(struct kern_history *);
81 void kernhist_dumpmask(u_int32_t);
82 static void kernhist_dump_histories(struct kern_history *[]);
83
84
85 /*
86 * call this from ddb
87 *
88 * expects the system to be quiesced, no locking
89 */
90 void
91 kernhist_dump(struct kern_history *l)
92 {
93 int lcv;
94
95 lcv = l->f;
96 do {
97 if (l->e[lcv].fmt)
98 kernhist_entry_print(&l->e[lcv]);
99 lcv = (lcv + 1) % l->n;
100 } while (lcv != l->f);
101 }
102
103 /*
104 * print a merged list of kern_history structures
105 */
106 static void
107 kernhist_dump_histories(struct kern_history *hists[])
108 {
109 struct timeval tv;
110 int cur[MAXHISTS];
111 int lcv, hi;
112
113 /* find the first of each list */
114 for (lcv = 0; hists[lcv]; lcv++)
115 cur[lcv] = hists[lcv]->f;
116
117 /*
118 * here we loop "forever", finding the next earliest
119 * history entry and printing it. cur[X] is the current
120 * entry to test for the history in hists[X]. if it is
121 * -1, then this history is finished.
122 */
123 for (;;) {
124 hi = -1;
125 tv.tv_sec = tv.tv_usec = 0;
126
127 /* loop over each history */
128 for (lcv = 0; hists[lcv]; lcv++) {
129 restart:
130 if (cur[lcv] == -1)
131 continue;
132 if (!hists[lcv]->e)
133 continue;
134
135 /*
136 * if the format is empty, go to the next entry
137 * and retry.
138 */
139 if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
140 cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
141 if (cur[lcv] == hists[lcv]->f)
142 cur[lcv] = -1;
143 goto restart;
144 }
145
146 /*
147 * if the time hasn't been set yet, or this entry is
148 * earlier than the current tv, set the time and history
149 * index.
150 */
151 if (tv.tv_sec == 0 ||
152 timercmp(&hists[lcv]->e[cur[lcv]].tv, &tv, <)) {
153 tv = hists[lcv]->e[cur[lcv]].tv;
154 hi = lcv;
155 }
156 }
157
158 /* if we didn't find any entries, we must be done */
159 if (hi == -1)
160 break;
161
162 /* print and move to the next entry */
163 kernhist_entry_print(&hists[hi]->e[cur[hi]]);
164 cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
165 if (cur[hi] == hists[hi]->f)
166 cur[hi] = -1;
167 }
168 }
169
170 /*
171 * call this from ddb. `bitmask' is from <sys/kernhist.h>. it
172 * merges the named histories.
173 *
174 * expects the system to be quiesced, no locking
175 */
176 void
177 kernhist_dumpmask(u_int32_t bitmask) /* XXX only support 32 hists */
178 {
179 struct kern_history *hists[MAXHISTS + 1];
180 int i = 0;
181
182 #ifdef UVMHIST
183 if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
184 hists[i++] = &maphist;
185
186 if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
187 hists[i++] = &pdhist;
188
189 if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
190 hists[i++] = &ubchist;
191
192 if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
193 hists[i++] = &loanhist;
194 #endif
195
196 #ifdef USB_DEBUG
197 if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
198 hists[i++] = &usbhist;
199 #endif
200
201 #ifdef SYSCALL_DEBUG
202 if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
203 hists[i++] = &scdebughist;
204 #endif
205
206 hists[i] = NULL;
207
208 kernhist_dump_histories(hists);
209 }
210
211 /*
212 * kernhist_print: ddb hook to print kern history
213 */
214 void
215 kernhist_print(void (*pr)(const char *, ...))
216 {
217 kernhist_dump(LIST_FIRST(&kern_histories));
218 }
219
220 #endif
221