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