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.6 2005/06/20 20:37:24 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, "");
117 * Give cpu0 a static buffer so the tracepoint facility can be used during
118 * early boot (note however that we still use a critical section, XXX).
120 static struct ktr_entry ktr_buf0[KTR_ENTRIES];
121 static struct ktr_entry *ktr_buf[MAXCPU] = { &ktr_buf0[0] };
122 static int ktr_idx[MAXCPU];
125 int ktr_verbose = KTR_VERBOSE;
126 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
127 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
131 ktr_sysinit(void *dummy)
135 for(i = 1; i < ncpus; ++i) {
136 ktr_buf[i] = malloc(KTR_ENTRIES * sizeof(struct ktr_entry),
137 M_KTR, M_WAITOK | M_ZERO);
141 SYSINIT(announce, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL);
145 ktr_write_entry(struct ktr_info *info, const char *file, int line,
148 struct ktr_entry *entry;
151 cpu = mycpu->gd_cpuid;
154 entry = ktr_buf[cpu] + (ktr_idx[cpu] & KTR_ENTRIES_MASK);
157 if (cpu_feature & CPUID_TSC)
158 entry->ktr_timestamp = rdtsc();
160 entry->ktr_timestamp = get_approximate_time_t();
161 entry->ktr_info = info;
162 entry->ktr_file = file;
163 entry->ktr_line = line;
164 if (info->kf_data_size > KTR_BUFSIZE)
165 bcopyi(ptr, entry->ktr_data, KTR_BUFSIZE);
167 bcopyi(ptr, entry->ktr_data, info->kf_data_size);
169 cpu_ktr_caller(entry);
172 if (ktr_verbose && info->kf_format) {
174 printf("cpu%d ", cpu);
176 if (ktr_verbose > 1) {
177 printf("%s.%d\t", entry->ktr_file, entry->ktr_line);
179 vprintf(info->kf_format, ptr);
186 ktr_log(struct ktr_info *info, const char *file, int line, ...)
190 if (panicstr == NULL) {
191 __va_start(va, line);
192 ktr_write_entry(info, file, line, va);
198 ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr)
200 if (panicstr == NULL) {
201 ktr_write_entry(info, file, line, ptr);
207 #define NUM_LINES_PER_PAGE 19
214 static int db_ktr_verbose;
215 static int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx);
217 DB_SHOW_COMMAND(ktr, db_ktr_all)
223 struct tstate tstate[MAXCPU];
226 for(i = 0; i < ncpus; i++) {
227 tstate[i].first = -1;
228 tstate[i].cur = ktr_idx[i] & KTR_ENTRIES_MASK;
231 while ((c = *(modif++)) != '\0') {
240 while ((c = *(modif++)) != '\0') {
253 if (printcpu > ncpus - 1) {
254 db_printf("Invalid cpu number\n");
258 * Lopp throug all the buffers and print the content of them, sorted
263 u_int64_t highest_ts;
265 struct ktr_entry *kp;
267 if (a_flag == 1 && cncheckc() != -1)
272 * Find the lowest timestamp
274 for (i = 0, counter = 0; i < ncpus; i++) {
275 if (ktr_buf[i] == NULL)
277 if (printcpu != -1 && printcpu != i)
279 if (tstate[i].cur == -1) {
281 if (counter == ncpus) {
282 db_printf("--- End of trace buffer ---\n");
287 if (ktr_buf[i][tstate[i].cur].ktr_timestamp > highest_ts) {
288 highest_ts = ktr_buf[i][tstate[i].cur].ktr_timestamp;
294 kp = &ktr_buf[i][tstate[i].cur];
295 if (tstate[i].first == -1)
296 tstate[i].first = tstate[i].cur;
297 if (--tstate[i].cur < 0)
298 tstate[i].cur = KTR_ENTRIES - 1;
299 if (tstate[i].first == tstate[i].cur) {
300 db_mach_vtrace(i, kp, tstate[i].cur + 1);
304 if (ktr_buf[i][tstate[i].cur].ktr_info == NULL)
306 if (db_more(&nl) == -1)
308 if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0)
314 db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx)
316 if (kp->ktr_info == NULL)
319 db_printf("cpu%d ", cpu);
321 db_printf("%d: ", idx);
322 if (db_ktr_verbose) {
323 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
324 kp->ktr_file, kp->ktr_line);
326 db_printf("%s\t", kp->ktr_info->kf_name);
327 db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2);
328 if (kp->ktr_info->kf_format) {
329 int32_t *args = kp->ktr_data;
330 db_printf(kp->ktr_info->kf_format,
331 args[0], args[1], args[2], args[3],
332 args[4], args[5], args[6], args[7],
333 args[8], args[9], args[10], args[11]);