2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * The following copyright applies to the DDB command code:
37 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the author nor the names of any co-contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * $DragonFly: src/sys/kern/kern_ktr.c,v 1.7 2005/12/06 23:37:53 dillon Exp $
68 * Kernel tracepoint facility.
74 #include <sys/param.h>
76 #include <sys/kernel.h>
77 #include <sys/libkern.h>
79 #include <sys/sysctl.h>
81 #include <sys/systm.h>
83 #include <sys/malloc.h>
84 #include <sys/thread2.h>
85 #include <sys/ctype.h>
87 #include <machine/cpu.h>
88 #include <machine/cpufunc.h>
89 #include <machine/specialreg.h>
90 #include <machine/md_var.h>
95 #define KTR_ENTRIES 2048
97 #define KTR_ENTRIES_MASK (KTR_ENTRIES - 1)
99 MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers");
101 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RW, 0, "ktr");
103 static int32_t ktr_cpumask = -1;
104 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
105 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
107 static int ktr_entries = KTR_ENTRIES;
108 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
110 static int ktr_version = KTR_VERSION;
111 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
113 static int ktr_stacktrace = 1;
114 SYSCTL_INT(_debug_ktr, OID_AUTO, stacktrace, CTLFLAG_RD, &ktr_stacktrace, 0, "");
116 static int ktr_resynchronize = 0;
117 SYSCTL_INT(_debug_ktr, OID_AUTO, resynchronize, CTLFLAG_RW, &ktr_resynchronize, 0, "");
120 * Give cpu0 a static buffer so the tracepoint facility can be used during
121 * early boot (note however that we still use a critical section, XXX).
123 static struct ktr_entry ktr_buf0[KTR_ENTRIES];
124 static struct ktr_entry *ktr_buf[MAXCPU] = { &ktr_buf0[0] };
125 static int ktr_idx[MAXCPU];
126 static int ktr_sync_state = 0;
127 static int ktr_sync_count;
128 static int64_t ktr_sync_tsc;
129 struct callout ktr_resync_callout;
133 int ktr_verbose = KTR_VERBOSE;
134 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
135 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
139 ktr_sysinit(void *dummy)
143 for(i = 1; i < ncpus; ++i) {
144 ktr_buf[i] = malloc(KTR_ENTRIES * sizeof(struct ktr_entry),
145 M_KTR, M_WAITOK | M_ZERO);
148 SYSINIT(ktr_sysinit, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL);
153 * Try to resynchronize the TSC's for all cpus. This is really, really nasty.
154 * We have to send an IPIQ message to all remote cpus, wait until they
155 * get into their IPIQ processing code loop, then do an even stricter hard
156 * loop to get the cpus as close to synchronized as we can to get the most
159 * This callback occurs on cpu0.
161 static void ktr_resync_callback(void *dummy);
162 static void ktr_resync_remote(void *dummy);
165 ktr_resyncinit(void *dummy)
167 callout_init(&ktr_resync_callout);
168 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
170 SYSINIT(ktr_resync, SI_SUB_FINISH_SMP+1, SI_ORDER_ANY, ktr_resyncinit, NULL);
172 extern cpumask_t smp_active_mask;
175 * We use a callout callback instead of a systimer because we cannot afford
176 * to preempt anyone to do this, or we might deadlock a spin-lock or
177 * serializer between two cpus.
181 ktr_resync_callback(void *dummy __unused)
185 KKASSERT(mycpu->gd_cpuid == 0);
186 if (ktr_resynchronize == 0)
188 if ((cpu_feature & CPUID_TSC) == 0)
193 ktr_sync_tsc = rdtsc();
194 count = lwkt_send_ipiq_mask(mycpu->gd_other_cpus & smp_active_mask,
195 (ipifunc1_t)ktr_resync_remote, NULL);
196 while (ktr_sync_count != count)
199 ktr_sync_tsc = rdtsc();
203 while (ktr_sync_count != 0) {
204 ktr_sync_tsc = rdtsc();
212 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
215 extern int64_t tsc_offsets[MAXCPU];
218 ktr_resync_remote(void *dummy __unused)
220 volatile int64_t tsc1 = ktr_sync_tsc;
221 volatile int64_t tsc2;
223 KKASSERT(ktr_sync_state == 1);
224 atomic_add_int(&ktr_sync_count, 1);
225 while (ktr_sync_state == 1) {
229 KKASSERT(ktr_sync_state == 2);
232 tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc2;
233 atomic_subtract_int(&ktr_sync_count, 1);
241 ktr_write_entry(struct ktr_info *info, const char *file, int line,
244 struct ktr_entry *entry;
247 cpu = mycpu->gd_cpuid;
250 entry = ktr_buf[cpu] + (ktr_idx[cpu] & KTR_ENTRIES_MASK);
253 if (cpu_feature & CPUID_TSC)
254 entry->ktr_timestamp = rdtsc() - tsc_offsets[cpu];
256 entry->ktr_timestamp = get_approximate_time_t();
257 entry->ktr_info = info;
258 entry->ktr_file = file;
259 entry->ktr_line = line;
260 if (info->kf_data_size > KTR_BUFSIZE)
261 bcopyi(ptr, entry->ktr_data, KTR_BUFSIZE);
263 bcopyi(ptr, entry->ktr_data, info->kf_data_size);
265 cpu_ktr_caller(entry);
268 if (ktr_verbose && info->kf_format) {
270 printf("cpu%d ", cpu);
272 if (ktr_verbose > 1) {
273 printf("%s.%d\t", entry->ktr_file, entry->ktr_line);
275 vprintf(info->kf_format, ptr);
282 ktr_log(struct ktr_info *info, const char *file, int line, ...)
286 if (panicstr == NULL) {
287 __va_start(va, line);
288 ktr_write_entry(info, file, line, va);
294 ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr)
296 if (panicstr == NULL) {
297 ktr_write_entry(info, file, line, ptr);
303 #define NUM_LINES_PER_PAGE 19
310 static int db_ktr_verbose;
311 static int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx);
313 DB_SHOW_COMMAND(ktr, db_ktr_all)
319 struct tstate tstate[MAXCPU];
322 for(i = 0; i < ncpus; i++) {
323 tstate[i].first = -1;
324 tstate[i].cur = ktr_idx[i] & KTR_ENTRIES_MASK;
327 while ((c = *(modif++)) != '\0') {
336 while ((c = *(modif++)) != '\0') {
349 if (printcpu > ncpus - 1) {
350 db_printf("Invalid cpu number\n");
354 * Lopp throug all the buffers and print the content of them, sorted
359 u_int64_t highest_ts;
361 struct ktr_entry *kp;
363 if (a_flag == 1 && cncheckc() != -1)
368 * Find the lowest timestamp
370 for (i = 0, counter = 0; i < ncpus; i++) {
371 if (ktr_buf[i] == NULL)
373 if (printcpu != -1 && printcpu != i)
375 if (tstate[i].cur == -1) {
377 if (counter == ncpus) {
378 db_printf("--- End of trace buffer ---\n");
383 if (ktr_buf[i][tstate[i].cur].ktr_timestamp > highest_ts) {
384 highest_ts = ktr_buf[i][tstate[i].cur].ktr_timestamp;
390 kp = &ktr_buf[i][tstate[i].cur];
391 if (tstate[i].first == -1)
392 tstate[i].first = tstate[i].cur;
393 if (--tstate[i].cur < 0)
394 tstate[i].cur = KTR_ENTRIES - 1;
395 if (tstate[i].first == tstate[i].cur) {
396 db_mach_vtrace(i, kp, tstate[i].cur + 1);
400 if (ktr_buf[i][tstate[i].cur].ktr_info == NULL)
402 if (db_more(&nl) == -1)
404 if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0)
410 db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx)
412 if (kp->ktr_info == NULL)
415 db_printf("cpu%d ", cpu);
417 db_printf("%d: ", idx);
418 if (db_ktr_verbose) {
419 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
420 kp->ktr_file, kp->ktr_line);
422 db_printf("%s\t", kp->ktr_info->kf_name);
423 db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2);
424 if (kp->ktr_info->kf_format) {
425 int32_t *args = kp->ktr_data;
426 db_printf(kp->ktr_info->kf_format,
427 args[0], args[1], args[2], args[3],
428 args[4], args[5], args[6], args[7],
429 args[8], args[9], args[10], args[11]);