44#include <sys/cpuset.h>
46#include <sys/kernel.h>
48#include <sys/libkern.h>
50#include <sys/malloc.h>
54#include <sys/sysctl.h>
58#include <machine/cpu.h>
62#include <ddb/db_output.h>
65#ifndef KTR_BOOT_ENTRIES
66#define KTR_BOOT_ENTRIES 1024
70#define KTR_ENTRIES 1024
74#define KTR_ENTRIES_MAX (8 * 1024 * 1024)
81#define KTR_CPUMASK CPUSET_FSET
85#define KTR_TIME get_cyclecount()
89#define KTR_CPU PCPU_GET(cpuid)
94FEATURE(ktr,
"Kernel support for KTR kernel tracing facility");
105static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
112 &
ktr_compile, 0,
"Bitmask of KTR event classes compiled into the kernel");
117 char lktr_cpumask_str[CPUSETBUFSIZ];
123 sizeof(lktr_cpumask_str), req);
124 if (error != 0 || req->newptr == NULL)
133 CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
135 "Bitmask of CPUs on which KTR logging is enabled");
144 if (error || !req->newptr)
155 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
171 if (error || !req->newptr)
178 CTLTYPE_U64 | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
180 "Bitmask of KTR event classes for which logging is enabled");
182#if KTR_ENTRIES > KTR_BOOT_ENTRIES
188ktr_entries_initializer(
void *
dummy __unused)
207SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
208 ktr_entries_initializer, NULL);
216 struct ktr_entry *
buf, *oldbuf;
220 if (error || !req->newptr)
237 buf =
malloc(
sizeof(*
buf) * entries, M_KTR, M_WAITOK | M_ZERO);
250 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
252 "Number of entries in the KTR buffer");
255int ktr_verbose = KTR_VERBOSE;
257SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0,
"");
262char ktr_alq_file[MAXPATHLEN] =
"/tmp/ktr.out";
265int ktr_alq_enabled = 0;
266int ktr_alq_failed = 0;
269SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
270 "Maximum number of entries to write");
271SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
272 "Current number of written entries");
273SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
274 "Number of times we overran the buffer");
275SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
276 "Number of items in the write buffer");
277SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
278 sizeof(ktr_alq_file),
"KTR logging file");
281sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
286 enable = ktr_alq_enabled;
289 if (error || !req->newptr)
295 error =
alq_open(&ktr_alq, (
const char *)ktr_alq_file,
296 req->td->td_ucred, ALQ_DEFAULT_CMODE,
297 sizeof(
struct ktr_entry), ktr_alq_depth);
304 if (ktr_alq_enabled == 0)
314 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
315 sysctl_debug_ktr_alq_enable,
"I",
316 "Enable KTR logging");
321 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
324 struct ktr_entry *entry;
326 struct ale *ale = NULL;
328 int newindex, saveindex;
329#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
341#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
343 if (td->td_pflags & TDP_INKTR)
345 td->td_pflags |= TDP_INKTR;
348 if (ktr_alq_enabled) {
349 if (td->td_critnest == 0 &&
350 (TD_IS_IDLETHREAD(td)) == 0 &&
352 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
354 if ((ale =
alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
359 entry = (
struct ktr_entry *)ale->ae_data;
369 }
while (atomic_cmpset_rel_int(&
ktr_idx, saveindex, newindex) == 0);
373 entry->ktr_cpu = cpu;
374 entry->ktr_thread = curthread;
376 while (strncmp(file,
"../", 3) == 0)
378 entry->ktr_file = file;
379 entry->ktr_line = line;
385 if (ktr_verbose > 1) {
386 printf(
"%s.%d\t", entry->ktr_file,
389 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
393 entry->ktr_desc = format;
394 entry->ktr_parms[0] = arg1;
395 entry->ktr_parms[1] = arg2;
396 entry->ktr_parms[2] = arg3;
397 entry->ktr_parms[3] = arg4;
398 entry->ktr_parms[4] = arg5;
399 entry->ktr_parms[5] = arg6;
401 if (ktr_alq_enabled && ale)
402 alq_post(ktr_alq, ale);
405#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
406 td->td_pflags &= ~TDP_INKTR;
416static struct tstate tstate;
417static int db_ktr_verbose;
418static int db_mach_vtrace(
void);
420DB_SHOW_COMMAND(ktr, db_ktr_all)
426 db_ktr_verbose |= (strchr(modif,
'v') != NULL) ? 2 : 0;
427 db_ktr_verbose |= (strchr(modif,
'V') != NULL) ? 1 : 0;
428 if (strchr(modif,
'a') != NULL) {
431 if (db_mach_vtrace() == 0)
434 while (!db_pager_quit)
435 if (db_mach_vtrace() == 0)
443 struct ktr_entry *kp;
445 if (tstate.cur == tstate.first ||
ktr_buf == NULL) {
446 db_printf(
"--- End of trace buffer ---\n");
452 if (kp->ktr_desc == NULL) {
453 db_printf(
"--- End of trace buffer ---\n");
456 db_printf(
"%d (%p", tstate.cur, kp->ktr_thread);
458 db_printf(
":cpu%d", kp->ktr_cpu);
461 if (db_ktr_verbose >= 1) {
462 db_printf(
" %10.10lld", (
long long)kp->ktr_timestamp);
464 if (db_ktr_verbose >= 2) {
465 db_printf(
" %s.%d", kp->ktr_file, kp->ktr_line);
468 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
469 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
473 if (tstate.first == -1)
474 tstate.first = tstate.cur;
476 if (--tstate.cur < 0)
SYSINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_init, NULL)
SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "Path used to search the init process")
void alq_close(struct alq *alq)
struct ale * alq_get(struct alq *alq, int flags)
int alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode, int size, int count)
TUNABLE_INT("kern.eventtimer.periodic", &want_periodic)
int cpusetobj_strscan(cpuset_t *set, const char *buf)
char * cpusetobj_strprint(char *buf, const cpuset_t *set)
SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel")
static int sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RWTUN|CTLFLAG_MPSAFE|CTLTYPE_STRING, NULL, 0, sysctl_debug_ktr_cpumask, "S", "Bitmask of CPUs on which KTR logging is enabled")
static int sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
FEATURE(ktr, "Kernel support for KTR kernel tracing facility")
void ktr_tracepoint(uint64_t mask, const char *file, int line, const char *format, u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, u_long arg6)
struct ktr_entry * ktr_buf
static MALLOC_DEFINE(M_KTR, "KTR", "KTR")
SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "Version of the KTR interface")
static int sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD|CTLFLAG_MPSAFE, 0, "KTR options")
struct ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES]
static int sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
int sysctl_handle_64(SYSCTL_HANDLER_ARGS)
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
int sysctl_handle_string(SYSCTL_HANDLER_ARGS)
u_char __read_frequently kdb_active
int printf(const char *fmt,...)
int quiesce_all_cpus(const char *wmesg, int prio)