]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_ktr.c
Fix bhyve SVM guest escape.
[FreeBSD/FreeBSD.git] / sys / kern / kern_ktr.c
1 /*-
2  * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * This module holds the global variables used by KTR and the ktr_tracepoint()
29  * function that does the actual tracing.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_ddb.h"
36 #include "opt_ktr.h"
37 #include "opt_alq.h"
38
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/alq.h>
42 #include <sys/cons.h>
43 #include <sys/cpuset.h>
44 #include <sys/kdb.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/libkern.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.h>
52 #include <sys/smp.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/time.h>
56
57 #include <machine/cpu.h>
58
59 #ifdef DDB
60 #include <ddb/ddb.h>
61 #include <ddb/db_output.h>
62 #endif
63
64 #ifndef KTR_BOOT_ENTRIES
65 #define KTR_BOOT_ENTRIES        1024
66 #endif
67
68 #ifndef KTR_ENTRIES
69 #define KTR_ENTRIES     1024
70 #endif
71
72 /* Limit the allocations to something manageable. */
73 #define KTR_ENTRIES_MAX (8 * 1024 * 1024)
74
75 #ifndef KTR_MASK
76 #define KTR_MASK        (0)
77 #endif
78
79 #ifndef KTR_CPUMASK
80 #define KTR_CPUMASK     CPUSET_FSET
81 #endif
82
83 #ifndef KTR_TIME
84 #define KTR_TIME        get_cyclecount()
85 #endif
86
87 #ifndef KTR_CPU
88 #define KTR_CPU         PCPU_GET(cpuid)
89 #endif
90
91 static MALLOC_DEFINE(M_KTR, "KTR", "KTR");
92
93 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
94
95 volatile int    ktr_idx = 0;
96 uint64_t ktr_mask = KTR_MASK;
97 uint64_t ktr_compile = KTR_COMPILE;
98 int     ktr_entries = KTR_BOOT_ENTRIES;
99 int     ktr_version = KTR_VERSION;
100 struct  ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES];
101 struct  ktr_entry *ktr_buf = ktr_buf_init;
102 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK);
103
104 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
105
106 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
107     &ktr_version, 0, "Version of the KTR interface");
108
109 SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
110     &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
111
112 static int
113 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
114 {
115         char lktr_cpumask_str[CPUSETBUFSIZ];
116         cpuset_t imask;
117         int error;
118
119         cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
120         error = sysctl_handle_string(oidp, lktr_cpumask_str,
121             sizeof(lktr_cpumask_str), req);
122         if (error != 0 || req->newptr == NULL)
123                 return (error);
124         if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
125                 return (EINVAL);
126         CPU_COPY(&imask, &ktr_cpumask);
127
128         return (error);
129 }
130 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
131     CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
132     sysctl_debug_ktr_cpumask, "S",
133     "Bitmask of CPUs on which KTR logging is enabled");
134
135 static int
136 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
137 {
138         int clear, error;
139
140         clear = 0;
141         error = sysctl_handle_int(oidp, &clear, 0, req);
142         if (error || !req->newptr)
143                 return (error);
144
145         if (clear) {
146                 bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries);
147                 ktr_idx = 0;
148         }
149
150         return (error);
151 }
152 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
153     sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
154
155 /*
156  * This is a sysctl proc so that it is serialized as !MPSAFE along with
157  * the other ktr sysctl procs.
158  */
159 static int
160 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
161 {
162         uint64_t mask;
163         int error;
164
165         mask = ktr_mask;
166         error = sysctl_handle_64(oidp, &mask, 0, req);
167         if (error || !req->newptr)
168                 return (error);
169         ktr_mask = mask;
170         return (error);
171 }
172
173 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, CTLTYPE_U64 | CTLFLAG_RWTUN, 0, 0,
174     sysctl_debug_ktr_mask, "QU",
175     "Bitmask of KTR event classes for which logging is enabled");
176
177 #if KTR_ENTRIES > KTR_BOOT_ENTRIES
178 /*
179  * A simplified version of sysctl_debug_ktr_entries.
180  * No need to care about SMP, scheduling, etc.
181  */
182 static void
183 ktr_entries_initializer(void *dummy __unused)
184 {
185         uint64_t mask;
186
187         /* Temporarily disable ktr in case malloc() is being traced. */
188         mask = ktr_mask;
189         ktr_mask = 0;
190         ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR,
191             M_WAITOK | M_ZERO);
192         memcpy(ktr_buf, ktr_buf_init + ktr_idx,
193             (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf));
194         if (ktr_idx != 0) {
195                 memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init,
196                     ktr_idx * sizeof(*ktr_buf));
197                 ktr_idx = KTR_BOOT_ENTRIES;
198         }
199         ktr_entries = KTR_ENTRIES;
200         ktr_mask = mask;
201 }
202 SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
203     ktr_entries_initializer, NULL);
204 #endif
205
206 static int
207 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
208 {
209         uint64_t mask;
210         int entries, error;
211         struct ktr_entry *buf, *oldbuf;
212
213         entries = ktr_entries;
214         error = sysctl_handle_int(oidp, &entries, 0, req);
215         if (error || !req->newptr)
216                 return (error);
217         if (entries > KTR_ENTRIES_MAX)
218                 return (ERANGE);
219         /* Disable ktr temporarily. */
220         mask = ktr_mask;
221         ktr_mask = 0;
222         /* Wait for threads to go idle. */
223         if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
224                 ktr_mask = mask;
225                 return (error);
226         }
227         if (ktr_buf != ktr_buf_init)
228                 oldbuf = ktr_buf;
229         else
230                 oldbuf = NULL;
231         /* Allocate a new buffer. */
232         buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO);
233         /* Install the new buffer and restart ktr. */
234         ktr_buf = buf;
235         ktr_entries = entries;
236         ktr_idx = 0;
237         ktr_mask = mask;
238         if (oldbuf != NULL)
239                 free(oldbuf, M_KTR);
240
241         return (error);
242 }
243
244 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
245     sysctl_debug_ktr_entries, "I", "Number of entries in the KTR buffer");
246
247 #ifdef KTR_VERBOSE
248 int     ktr_verbose = KTR_VERBOSE;
249 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
250 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
251 #endif
252
253 #ifdef KTR_ALQ
254 struct alq *ktr_alq;
255 char    ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
256 int     ktr_alq_cnt = 0;
257 int     ktr_alq_depth = KTR_ENTRIES;
258 int     ktr_alq_enabled = 0;
259 int     ktr_alq_failed = 0;
260 int     ktr_alq_max = 0;
261
262 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
263     "Maximum number of entries to write");
264 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
265     "Current number of written entries");
266 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
267     "Number of times we overran the buffer");
268 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
269     "Number of items in the write buffer");
270 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
271     sizeof(ktr_alq_file), "KTR logging file");
272
273 static int
274 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
275 {
276         int error;
277         int enable;
278
279         enable = ktr_alq_enabled;
280
281         error = sysctl_handle_int(oidp, &enable, 0, req);
282         if (error || !req->newptr)
283                 return (error);
284
285         if (enable) {
286                 if (ktr_alq_enabled)
287                         return (0);
288                 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
289                     req->td->td_ucred, ALQ_DEFAULT_CMODE,
290                     sizeof(struct ktr_entry), ktr_alq_depth);
291                 if (error == 0) {
292                         ktr_alq_cnt = 0;
293                         ktr_alq_failed = 0;
294                         ktr_alq_enabled = 1;
295                 }
296         } else {
297                 if (ktr_alq_enabled == 0)
298                         return (0);
299                 ktr_alq_enabled = 0;
300                 alq_close(ktr_alq);
301                 ktr_alq = NULL;
302         }
303
304         return (error);
305 }
306 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
307     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
308     "I", "Enable KTR logging");
309 #endif
310
311 void
312 ktr_tracepoint(uint64_t mask, const char *file, int line, const char *format,
313     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
314     u_long arg6)
315 {
316         struct ktr_entry *entry;
317 #ifdef KTR_ALQ
318         struct ale *ale = NULL;
319 #endif
320         int newindex, saveindex;
321 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
322         struct thread *td;
323 #endif
324         int cpu;
325
326         if (panicstr || kdb_active)
327                 return;
328         if ((ktr_mask & mask) == 0 || ktr_buf == NULL)
329                 return;
330         cpu = KTR_CPU;
331         if (!CPU_ISSET(cpu, &ktr_cpumask))
332                 return;
333 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
334         td = curthread;
335         if (td->td_pflags & TDP_INKTR)
336                 return;
337         td->td_pflags |= TDP_INKTR;
338 #endif
339 #ifdef KTR_ALQ
340         if (ktr_alq_enabled) {
341                 if (td->td_critnest == 0 &&
342                     (TD_IS_IDLETHREAD(td)) == 0 &&
343                     td != ald_thread) {
344                         if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
345                                 goto done;
346                         if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
347                                 ktr_alq_failed++;
348                                 goto done;
349                         }
350                         ktr_alq_cnt++;
351                         entry = (struct ktr_entry *)ale->ae_data;
352                 } else {
353                         goto done;
354                 }
355         } else
356 #endif
357         {
358                 do {
359                         saveindex = ktr_idx;
360                         newindex = (saveindex + 1) % ktr_entries;
361                 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
362                 entry = &ktr_buf[saveindex];
363         }
364         entry->ktr_timestamp = KTR_TIME;
365         entry->ktr_cpu = cpu;
366         entry->ktr_thread = curthread;
367         if (file != NULL)
368                 while (strncmp(file, "../", 3) == 0)
369                         file += 3;
370         entry->ktr_file = file;
371         entry->ktr_line = line;
372 #ifdef KTR_VERBOSE
373         if (ktr_verbose) {
374 #ifdef SMP
375                 printf("cpu%d ", cpu);
376 #endif
377                 if (ktr_verbose > 1) {
378                         printf("%s.%d\t", entry->ktr_file,
379                             entry->ktr_line);
380                 }
381                 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
382                 printf("\n");
383         }
384 #endif
385         entry->ktr_desc = format;
386         entry->ktr_parms[0] = arg1;
387         entry->ktr_parms[1] = arg2;
388         entry->ktr_parms[2] = arg3;
389         entry->ktr_parms[3] = arg4;
390         entry->ktr_parms[4] = arg5;
391         entry->ktr_parms[5] = arg6;
392 #ifdef KTR_ALQ
393         if (ktr_alq_enabled && ale)
394                 alq_post(ktr_alq, ale);
395 done:
396 #endif
397 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
398         td->td_pflags &= ~TDP_INKTR;
399 #endif
400 }
401
402 #ifdef DDB
403
404 struct tstate {
405         int     cur;
406         int     first;
407 };
408 static  struct tstate tstate;
409 static  int db_ktr_verbose;
410 static  int db_mach_vtrace(void);
411
412 DB_SHOW_COMMAND(ktr, db_ktr_all)
413 {
414         
415         tstate.cur = (ktr_idx - 1) % ktr_entries;
416         tstate.first = -1;
417         db_ktr_verbose = 0;
418         db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
419         db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
420         if (strchr(modif, 'a') != NULL) {
421                 db_disable_pager();
422                 while (cncheckc() == -1)
423                         if (db_mach_vtrace() == 0)
424                                 break;
425         } else {
426                 while (!db_pager_quit)
427                         if (db_mach_vtrace() == 0)
428                                 break;
429         }
430 }
431
432 static int
433 db_mach_vtrace(void)
434 {
435         struct ktr_entry        *kp;
436
437         if (tstate.cur == tstate.first || ktr_buf == NULL) {
438                 db_printf("--- End of trace buffer ---\n");
439                 return (0);
440         }
441         kp = &ktr_buf[tstate.cur];
442
443         /* Skip over unused entries. */
444         if (kp->ktr_desc == NULL) {
445                 db_printf("--- End of trace buffer ---\n");
446                 return (0);
447         }
448         db_printf("%d (%p", tstate.cur, kp->ktr_thread);
449 #ifdef SMP
450         db_printf(":cpu%d", kp->ktr_cpu);
451 #endif
452         db_printf(")");
453         if (db_ktr_verbose >= 1) {
454                 db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
455         }
456         if (db_ktr_verbose >= 2) {
457                 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
458         }
459         db_printf(": ");
460         db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
461             kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
462             kp->ktr_parms[5]);
463         db_printf("\n");
464
465         if (tstate.first == -1)
466                 tstate.first = tstate.cur;
467
468         if (--tstate.cur < 0)
469                 tstate.cur = ktr_entries - 1;
470
471         return (1);
472 }
473
474 #endif  /* DDB */