kern_history.c revision 1.6 1 /* $NetBSD: kern_history.c,v 1.6 2016/06/23 07:32:12 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.6 2016/06/23 07:32:12 skrll Exp $");
37
38 #include "opt_ddb.h"
39 #include "opt_kernhist.h"
40 #include "opt_syscall_debug.h"
41 #include "opt_usb.h"
42 #include "opt_uvmhist.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 (*)(const char *, ...) __printflike(1, 2));
77 void kernhist_dumpmask(u_int32_t);
78 static void kernhist_dump_histories(struct kern_history *[],
79 void (*)(const char *, ...) __printflike(1, 2));
80
81
82 /*
83 * call this from ddb
84 *
85 * expects the system to be quiesced, no locking
86 */
87 void
88 kernhist_dump(struct kern_history *l, void (*pr)(const char *, ...))
89 {
90 int lcv;
91
92 lcv = l->f;
93 do {
94 if (l->e[lcv].fmt)
95 kernhist_entry_print(&l->e[lcv], pr);
96 lcv = (lcv + 1) % l->n;
97 } while (lcv != l->f);
98 }
99
100 /*
101 * print a merged list of kern_history structures
102 */
103 static void
104 kernhist_dump_histories(struct kern_history *hists[], void (*pr)(const char *, ...))
105 {
106 struct timeval tv;
107 int cur[MAXHISTS];
108 int lcv, hi;
109
110 /* find the first of each list */
111 for (lcv = 0; hists[lcv]; lcv++)
112 cur[lcv] = hists[lcv]->f;
113
114 /*
115 * here we loop "forever", finding the next earliest
116 * history entry and printing it. cur[X] is the current
117 * entry to test for the history in hists[X]. if it is
118 * -1, then this history is finished.
119 */
120 for (;;) {
121 hi = -1;
122 tv.tv_sec = tv.tv_usec = 0;
123
124 /* loop over each history */
125 for (lcv = 0; hists[lcv]; lcv++) {
126 restart:
127 if (cur[lcv] == -1)
128 continue;
129 if (!hists[lcv]->e)
130 continue;
131
132 /*
133 * if the format is empty, go to the next entry
134 * and retry.
135 */
136 if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
137 cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
138 if (cur[lcv] == hists[lcv]->f)
139 cur[lcv] = -1;
140 goto restart;
141 }
142
143 /*
144 * if the time hasn't been set yet, or this entry is
145 * earlier than the current tv, set the time and history
146 * index.
147 */
148 if (tv.tv_sec == 0 ||
149 timercmp(&hists[lcv]->e[cur[lcv]].tv, &tv, <)) {
150 tv = hists[lcv]->e[cur[lcv]].tv;
151 hi = lcv;
152 }
153 }
154
155 /* if we didn't find any entries, we must be done */
156 if (hi == -1)
157 break;
158
159 /* print and move to the next entry */
160 kernhist_entry_print(&hists[hi]->e[cur[hi]], pr);
161 cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
162 if (cur[hi] == hists[hi]->f)
163 cur[hi] = -1;
164 }
165 }
166
167 /*
168 * call this from ddb. `bitmask' is from <sys/kernhist.h>. it
169 * merges the named histories.
170 *
171 * expects the system to be quiesced, no locking
172 */
173 void
174 kernhist_dumpmask(u_int32_t bitmask) /* XXX only support 32 hists */
175 {
176 struct kern_history *hists[MAXHISTS + 1];
177 int i = 0;
178
179 #ifdef UVMHIST
180 if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
181 hists[i++] = &maphist;
182
183 if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
184 hists[i++] = &pdhist;
185
186 if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
187 hists[i++] = &ubchist;
188
189 if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
190 hists[i++] = &loanhist;
191 #endif
192
193 #ifdef USB_DEBUG
194 if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
195 hists[i++] = &usbhist;
196 #endif
197
198 #ifdef SYSCALL_DEBUG
199 if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
200 hists[i++] = &scdebughist;
201 #endif
202
203 hists[i] = NULL;
204
205 kernhist_dump_histories(hists, printf);
206 }
207
208 /*
209 * kernhist_print: ddb hook to print kern history
210 */
211 void
212 kernhist_print(void *addr, void (*pr)(const char *, ...) __printflike(1,2))
213 {
214 struct kern_history *h;
215
216 LIST_FOREACH(h, &kern_histories, list) {
217 if (h == addr)
218 break;
219 }
220
221 if (h == NULL) {
222 struct kern_history *hists[MAXHISTS + 1];
223 int i = 0;
224 #ifdef UVMHIST
225 hists[i++] = &maphist;
226 hists[i++] = &pdhist;
227 hists[i++] = &ubchist;
228 hists[i++] = &loanhist;
229 #endif
230 #ifdef USB_DEBUG
231 hists[i++] = &usbhist;
232 #endif
233
234 #ifdef SYSCALL_DEBUG
235 hists[i++] = &scdebughist;
236 #endif
237 hists[i] = NULL;
238
239 kernhist_dump_histories(hists, pr);
240 } else {
241 kernhist_dump(h, pr);
242 }
243 }
244
245 #endif
246