32 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
43 #include <sys/cpuset.h>
44 #include <sys/kernel.h>
46 #include <sys/libkern.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
52 #include <machine/cpu.h>
54 #include <machine/ktr.h>
59 #include <ddb/db_output.h>
63 #define KTR_ENTRIES 1024
71 #define KTR_TIME get_cyclecount()
75 #define KTR_CPU PCPU_GET(cpuid)
78 FEATURE(ktr,
"Kernel support for KTR kernel tracing facility");
80 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0,
"KTR options");
85 &
ktr_mask, 0,
"Bitmask of KTR event classes for which logging is enabled");
88 SYSCTL_INT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
89 &
ktr_compile, 0,
"Bitmask of KTR event classes compiled into the kernel");
92 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD,
93 &
ktr_entries, 0,
"Number of entries in the KTR buffer");
96 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
127 char lktr_cpumask_str[CPUSETBUFSIZ];
133 sizeof(lktr_cpumask_str), req);
134 if (error != 0 || req->newptr == NULL)
143 CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
145 "Bitmask of CPUs on which KTR logging is enabled");
157 if (error || !req->newptr)
167 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
171 int ktr_verbose = KTR_VERBOSE;
173 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0,
"");
178 char ktr_alq_file[MAXPATHLEN] =
"/tmp/ktr.out";
181 int ktr_alq_enabled = 0;
182 int ktr_alq_failed = 0;
185 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
186 "Maximum number of entries to write");
187 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
188 "Current number of written entries");
189 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
190 "Number of times we overran the buffer");
191 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
192 "Number of items in the write buffer");
193 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
194 sizeof(ktr_alq_file),
"KTR logging file");
197 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
202 enable = ktr_alq_enabled;
205 if (error || !req->newptr)
211 error =
alq_open(&ktr_alq, (
const char *)ktr_alq_file,
212 req->td->td_ucred, ALQ_DEFAULT_CMODE,
213 sizeof(
struct ktr_entry), ktr_alq_depth);
220 if (ktr_alq_enabled == 0)
230 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
231 "I",
"Enable KTR logging");
236 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
239 struct ktr_entry *entry;
241 struct ale *ale = NULL;
243 int newindex, saveindex;
244 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
256 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
258 if (td->td_pflags & TDP_INKTR)
260 td->td_pflags |= TDP_INKTR;
263 if (ktr_alq_enabled) {
264 if (td->td_critnest == 0 &&
265 (td->td_flags & TDF_IDLETD) == 0 &&
267 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
269 if ((ale =
alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
274 entry = (
struct ktr_entry *)ale->ae_data;
284 }
while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
288 entry->ktr_cpu = cpu;
289 entry->ktr_thread = curthread;
291 while (strncmp(file,
"../", 3) == 0)
293 entry->ktr_file = file;
294 entry->ktr_line = line;
300 if (ktr_verbose > 1) {
301 printf(
"%s.%d\t", entry->ktr_file,
304 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
308 entry->ktr_desc = format;
309 entry->ktr_parms[0] = arg1;
310 entry->ktr_parms[1] = arg2;
311 entry->ktr_parms[2] = arg3;
312 entry->ktr_parms[3] = arg4;
313 entry->ktr_parms[4] = arg5;
314 entry->ktr_parms[5] = arg6;
316 if (ktr_alq_enabled && ale)
317 alq_post(ktr_alq, ale);
320 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
321 td->td_pflags &= ~TDP_INKTR;
331 static struct tstate tstate;
332 static int db_ktr_verbose;
333 static int db_mach_vtrace(
void);
335 DB_SHOW_COMMAND(ktr, db_ktr_all)
341 db_ktr_verbose |= (index(modif,
'v') != NULL) ? 2 : 0;
342 db_ktr_verbose |= (index(modif,
'V') != NULL) ? 1 : 0;
343 if (index(modif,
'a') != NULL) {
346 if (db_mach_vtrace() == 0)
349 while (!db_pager_quit)
350 if (db_mach_vtrace() == 0)
358 struct ktr_entry *kp;
360 if (tstate.cur == tstate.first) {
361 db_printf(
"--- End of trace buffer ---\n");
367 if (kp->ktr_desc == NULL) {
368 db_printf(
"--- End of trace buffer ---\n");
371 db_printf(
"%d (%p", tstate.cur, kp->ktr_thread);
373 db_printf(
":cpu%d", kp->ktr_cpu);
376 if (db_ktr_verbose >= 1) {
377 db_printf(
" %10.10lld", (
long long)kp->ktr_timestamp);
379 if (db_ktr_verbose >= 2) {
380 db_printf(
" %s.%d", kp->ktr_file, kp->ktr_line);
383 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
384 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
388 if (tstate.first == -1)
389 tstate.first = tstate.cur;
391 if (--tstate.cur < 0)
TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str))
static void ktr_cpumask_initializer(void *dummy __unused)
static int sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW|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_clear(SYSCTL_HANDLER_ARGS)
struct ktr_entry ktr_buf[KTR_ENTRIES]
int alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode, int size, int count)
void alq_close(struct alq *alq)
int sysctl_handle_string(SYSCTL_HANDLER_ARGS)
TUNABLE_INT("debug.ktr.mask",&ktr_mask)
SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW,&ktr_mask, 0,"Bitmask of KTR event classes for which logging is enabled")
static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0,"KTR options")
FEATURE(ktr,"Kernel support for KTR kernel tracing facility")
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
struct ale * alq_get(struct alq *alq, int flags)
static char ktr_cpumask_str[CPUSETBUFSIZ]
int cpusetobj_strscan(cpuset_t *set, const char *buf)
int printf(const char *fmt,...)
char * cpusetobj_strprint(char *buf, const cpuset_t *set)
SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0,"Path used to search the init process")
SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY, ktr_cpumask_initializer, NULL)
void ktr_tracepoint(u_int 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)