xref: /dragonfly/sys/kern/kern_ktr.c (revision ddd22766)
1d3776285SMatthew Dillon /*
2d3776285SMatthew Dillon  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3d3776285SMatthew Dillon  *
4d3776285SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5d3776285SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6d3776285SMatthew Dillon  *
7d3776285SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8d3776285SMatthew Dillon  * modification, are permitted provided that the following conditions
9d3776285SMatthew Dillon  * are met:
10d3776285SMatthew Dillon  *
11d3776285SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12d3776285SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13d3776285SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14d3776285SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15d3776285SMatthew Dillon  *    the documentation and/or other materials provided with the
16d3776285SMatthew Dillon  *    distribution.
17d3776285SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18d3776285SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19d3776285SMatthew Dillon  *    from this software without specific, prior written permission.
20d3776285SMatthew Dillon  *
21d3776285SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22d3776285SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23d3776285SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24d3776285SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25d3776285SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26d3776285SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27d3776285SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28d3776285SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29d3776285SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30d3776285SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31d3776285SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d3776285SMatthew Dillon  * SUCH DAMAGE.
33d3776285SMatthew Dillon  */
34d3776285SMatthew Dillon /*
35d3776285SMatthew Dillon  * The following copyright applies to the DDB command code:
36d3776285SMatthew Dillon  *
3781540c2dSEirik Nygaard  * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
3881540c2dSEirik Nygaard  * All rights reserved.
3981540c2dSEirik Nygaard  *
4081540c2dSEirik Nygaard  * Redistribution and use in source and binary forms, with or without
4181540c2dSEirik Nygaard  * modification, are permitted provided that the following conditions
4281540c2dSEirik Nygaard  * are met:
4381540c2dSEirik Nygaard  * 1. Redistributions of source code must retain the above copyright
4481540c2dSEirik Nygaard  *    notice, this list of conditions and the following disclaimer.
4581540c2dSEirik Nygaard  * 2. Redistributions in binary form must reproduce the above copyright
4681540c2dSEirik Nygaard  *    notice, this list of conditions and the following disclaimer in the
4781540c2dSEirik Nygaard  *    documentation and/or other materials provided with the distribution.
4881540c2dSEirik Nygaard  * 3. Neither the name of the author nor the names of any co-contributors
4981540c2dSEirik Nygaard  *    may be used to endorse or promote products derived from this software
5081540c2dSEirik Nygaard  *    without specific prior written permission.
5181540c2dSEirik Nygaard  *
5281540c2dSEirik Nygaard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
5381540c2dSEirik Nygaard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5481540c2dSEirik Nygaard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5581540c2dSEirik Nygaard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
5681540c2dSEirik Nygaard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5781540c2dSEirik Nygaard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5881540c2dSEirik Nygaard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5981540c2dSEirik Nygaard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6081540c2dSEirik Nygaard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6181540c2dSEirik Nygaard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6281540c2dSEirik Nygaard  * SUCH DAMAGE.
6381540c2dSEirik Nygaard  */
646d22945aSSascha Wildner 
6581540c2dSEirik Nygaard /*
66d3776285SMatthew Dillon  * Kernel tracepoint facility.
6781540c2dSEirik Nygaard  */
6881540c2dSEirik Nygaard 
6981540c2dSEirik Nygaard #include "opt_ddb.h"
7081540c2dSEirik Nygaard #include "opt_ktr.h"
7181540c2dSEirik Nygaard 
7281540c2dSEirik Nygaard #include <sys/param.h>
7381540c2dSEirik Nygaard #include <sys/cons.h>
7481540c2dSEirik Nygaard #include <sys/kernel.h>
7581540c2dSEirik Nygaard #include <sys/libkern.h>
7681540c2dSEirik Nygaard #include <sys/proc.h>
7781540c2dSEirik Nygaard #include <sys/sysctl.h>
78d3776285SMatthew Dillon #include <sys/ktr.h>
7981540c2dSEirik Nygaard #include <sys/systm.h>
8081540c2dSEirik Nygaard #include <sys/time.h>
818e273a1dSEirik Nygaard #include <sys/malloc.h>
82c2751817SMatthew Dillon #include <sys/spinlock.h>
83ce7866b8SMatthew Dillon #include <sys/kbio.h>
84ce7866b8SMatthew Dillon #include <sys/ctype.h>
852fa6678bSSascha Wildner #include <sys/limits.h>
86ce7866b8SMatthew Dillon 
878e273a1dSEirik Nygaard #include <sys/thread2.h>
88c2751817SMatthew Dillon #include <sys/spinlock2.h>
8981540c2dSEirik Nygaard 
9081540c2dSEirik Nygaard #include <machine/cpu.h>
9181540c2dSEirik Nygaard #include <machine/cpufunc.h>
9281540c2dSEirik Nygaard #include <machine/specialreg.h>
9381540c2dSEirik Nygaard #include <machine/md_var.h>
9481540c2dSEirik Nygaard 
9581540c2dSEirik Nygaard #include <ddb/ddb.h>
9681540c2dSEirik Nygaard 
9781540c2dSEirik Nygaard #ifndef KTR_ENTRIES
98d3776285SMatthew Dillon #define	KTR_ENTRIES		2048
996d22945aSSascha Wildner #elif (KTR_ENTRIES & KTR_ENTRIES - 1)
1006d22945aSSascha Wildner #error KTR_ENTRIES must be a power of two
10181540c2dSEirik Nygaard #endif
102d3776285SMatthew Dillon #define KTR_ENTRIES_MASK	(KTR_ENTRIES - 1)
10381540c2dSEirik Nygaard 
10496328d40SMatthew Dillon /*
10562b18938SSepherosa Ziehau  * Used by earlier boot; default value consumes ~64K BSS.
10662b18938SSepherosa Ziehau  *
10762b18938SSepherosa Ziehau  * NOTE:
10862b18938SSepherosa Ziehau  * We use a small value here; this prevents kernel or module loading
10962b18938SSepherosa Ziehau  * failure due to excessive BSS usage if KTR_ENTRIES is large.
11062b18938SSepherosa Ziehau  */
11162b18938SSepherosa Ziehau #if (KTR_ENTRIES < 256)
11262b18938SSepherosa Ziehau #define KTR_ENTRIES_BOOT0	KTR_ENTRIES
11362b18938SSepherosa Ziehau #else
11462b18938SSepherosa Ziehau #define KTR_ENTRIES_BOOT0	256
11562b18938SSepherosa Ziehau #endif
11662b18938SSepherosa Ziehau #define KTR_ENTRIES_BOOT0_MASK	(KTR_ENTRIES_BOOT0 - 1)
11762b18938SSepherosa Ziehau 
11862b18938SSepherosa Ziehau /*
11996328d40SMatthew Dillon  * test logging support.  When ktr_testlogcnt is non-zero each synchronization
1200b698dcaSMatthew Dillon  * interrupt will issue six back-to-back ktr logging messages on cpu 0
12196328d40SMatthew Dillon  * so the user can determine KTR logging overheads.
12296328d40SMatthew Dillon  */
12396328d40SMatthew Dillon #if !defined(KTR_TESTLOG)
12496328d40SMatthew Dillon #define KTR_TESTLOG	KTR_ALL
12596328d40SMatthew Dillon #endif
12696328d40SMatthew Dillon KTR_INFO_MASTER(testlog);
1278cb417faSMatthew Dillon #if KTR_TESTLOG
1282fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, charfmt, 0,
1292fa6678bSSascha Wildner     "charfmt %hhd %hhi %#hho %hhu %#hhx %#hhX\n",
1302fa6678bSSascha Wildner     signed char d1, signed char d2,
1312fa6678bSSascha Wildner     unsigned char d3, unsigned char d4,
1322fa6678bSSascha Wildner     unsigned char d5, unsigned char d6);
1332fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, shortfmt, 1,
1342fa6678bSSascha Wildner     "shortfmt %hd %hi %#ho %hu %#hx %#hX\n",
1352fa6678bSSascha Wildner     short d1, short d2,
1362fa6678bSSascha Wildner     unsigned short d3, unsigned short d4,
1372fa6678bSSascha Wildner     unsigned short d5, unsigned short d6);
1382fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, longfmt, 2,
1392fa6678bSSascha Wildner     "longfmt %ld %li %#lo %lu %#lx %#lX\n",
1402fa6678bSSascha Wildner     long d1, long d2,
1412fa6678bSSascha Wildner     unsigned long d3, unsigned long d4,
1422fa6678bSSascha Wildner     unsigned long d5, unsigned long d6);
1432fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, longlongfmt, 3,
1442fa6678bSSascha Wildner     "longlongfmt %lld %lli %#llo %llu %#llx %#llX\n",
1452fa6678bSSascha Wildner     long long d1, long long d2,
1462fa6678bSSascha Wildner     unsigned long long d3, unsigned long long d4,
1472fa6678bSSascha Wildner     unsigned long long d5, unsigned long long d6);
1482fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, intmaxfmt, 4,
1492fa6678bSSascha Wildner     "intmaxfmt %jd %ji %#jo %ju %#jx %#jX\n",
1502fa6678bSSascha Wildner     intmax_t d1, intmax_t d2,
1512fa6678bSSascha Wildner     uintmax_t d3, uintmax_t d4,
1522fa6678bSSascha Wildner     uintmax_t d5, uintmax_t d6);
1532fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, ptrdifffmt, 5,
1542fa6678bSSascha Wildner     "ptrdifffmt %td %ti\n",
1552fa6678bSSascha Wildner     ptrdiff_t d1, ptrdiff_t d2);
1562fa6678bSSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, sizefmt, 6,
1572fa6678bSSascha Wildner     "sizefmt %zd %zi %#zo %zu %#zx %#zX\n",
1582fa6678bSSascha Wildner     ssize_t d1, ssize_t d2,
1592fa6678bSSascha Wildner     size_t d3, size_t d4,
1602fa6678bSSascha Wildner     size_t d5, size_t d6);
161*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, pingpong, 17, "pingpong");
162*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, pipeline, 18, "pipeline");
163*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, crit_beg, 19, "crit_beg");
164*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, crit_end, 20, "crit_end");
165*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, spin_beg, 21, "spin_beg");
166*ddd22766SSascha Wildner KTR_INFO(KTR_TESTLOG, testlog, spin_end, 22, "spin_end");
1670b698dcaSMatthew Dillon #define logtest_noargs(name)	KTR_LOG(testlog_ ## name)
1688cb417faSMatthew Dillon #endif
16996328d40SMatthew Dillon 
1708e273a1dSEirik Nygaard MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers");
1718e273a1dSEirik Nygaard 
172d3776285SMatthew Dillon SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RW, 0, "ktr");
17381540c2dSEirik Nygaard 
17462b18938SSepherosa Ziehau static int	ktr_entries = KTR_ENTRIES_BOOT0;
175f22af1e9SSascha Wildner SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0,
176f22af1e9SSascha Wildner     "Size of the event buffer");
17762b18938SSepherosa Ziehau static int	ktr_entries_mask = KTR_ENTRIES_BOOT0_MASK;
17881540c2dSEirik Nygaard 
17962b18938SSepherosa Ziehau static int	ktr_version = KTR_VERSION;
18081540c2dSEirik Nygaard SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
18181540c2dSEirik Nygaard 
182af6ff89eSMatthew Dillon static int	ktr_stacktrace = 1;
183af6ff89eSMatthew Dillon SYSCTL_INT(_debug_ktr, OID_AUTO, stacktrace, CTLFLAG_RD, &ktr_stacktrace, 0, "");
184af6ff89eSMatthew Dillon 
185374133e3SMatthew Dillon static int	ktr_resynchronize = 0;
186f22af1e9SSascha Wildner SYSCTL_INT(_debug_ktr, OID_AUTO, resynchronize, CTLFLAG_RW,
187f22af1e9SSascha Wildner     &ktr_resynchronize, 0, "Resynchronize TSC 10 times a second");
188374133e3SMatthew Dillon 
18996328d40SMatthew Dillon #if KTR_TESTLOG
19096328d40SMatthew Dillon static int	ktr_testlogcnt = 0;
19196328d40SMatthew Dillon SYSCTL_INT(_debug_ktr, OID_AUTO, testlogcnt, CTLFLAG_RW, &ktr_testlogcnt, 0, "");
19272d379dfSMatthew Dillon static int	ktr_testipicnt = 0;
19372d379dfSMatthew Dillon static int	ktr_testipicnt_remainder;
19472d379dfSMatthew Dillon SYSCTL_INT(_debug_ktr, OID_AUTO, testipicnt, CTLFLAG_RW, &ktr_testipicnt, 0, "");
195c2751817SMatthew Dillon static int	ktr_testcritcnt = 0;
196c2751817SMatthew Dillon SYSCTL_INT(_debug_ktr, OID_AUTO, testcritcnt, CTLFLAG_RW, &ktr_testcritcnt, 0, "");
197c2751817SMatthew Dillon static int	ktr_testspincnt = 0;
198c2751817SMatthew Dillon SYSCTL_INT(_debug_ktr, OID_AUTO, testspincnt, CTLFLAG_RW, &ktr_testspincnt, 0, "");
19996328d40SMatthew Dillon #endif
20096328d40SMatthew Dillon 
201d3776285SMatthew Dillon /*
202d3776285SMatthew Dillon  * Give cpu0 a static buffer so the tracepoint facility can be used during
203d3776285SMatthew Dillon  * early boot (note however that we still use a critical section, XXX).
204d3776285SMatthew Dillon  */
20562b18938SSepherosa Ziehau static struct	ktr_entry ktr_buf0[KTR_ENTRIES_BOOT0];
206ddca1582SMatthew Dillon 
20702289741SSepherosa Ziehau struct ktr_cpu ktr_cpu[MAXCPU] = {
208ddca1582SMatthew Dillon 	{ .core.ktr_buf = &ktr_buf0[0] }
209ddca1582SMatthew Dillon };
210ddca1582SMatthew Dillon 
211374133e3SMatthew Dillon static int64_t	ktr_sync_tsc;
212374133e3SMatthew Dillon struct callout	ktr_resync_callout;
213374133e3SMatthew Dillon 
21481540c2dSEirik Nygaard #ifdef KTR_VERBOSE
21581540c2dSEirik Nygaard int	ktr_verbose = KTR_VERBOSE;
21681540c2dSEirik Nygaard TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
217f22af1e9SSascha Wildner SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0,
218f22af1e9SSascha Wildner     "Log events to the console as well");
21981540c2dSEirik Nygaard #endif
22081540c2dSEirik Nygaard 
221ba39e2e0SMatthew Dillon static void ktr_resync_callback(void *dummy __unused);
222ba39e2e0SMatthew Dillon 
223c2751817SMatthew Dillon extern int64_t tsc_offsets[];
2240b698dcaSMatthew Dillon 
22581540c2dSEirik Nygaard static void
ktr_sysinit(void * dummy)22681540c2dSEirik Nygaard ktr_sysinit(void *dummy)
22781540c2dSEirik Nygaard {
228ddca1582SMatthew Dillon 	struct ktr_cpu_core *kcpu;
2298e273a1dSEirik Nygaard 	int i;
2308e273a1dSEirik Nygaard 
23162b18938SSepherosa Ziehau 	for (i = 0; i < ncpus; ++i) {
232ddca1582SMatthew Dillon 		kcpu = &ktr_cpu[i].core;
233ddca1582SMatthew Dillon 		kcpu->ktr_buf = kmalloc(KTR_ENTRIES * sizeof(struct ktr_entry),
234d3776285SMatthew Dillon 					M_KTR, M_WAITOK | M_ZERO);
23562b18938SSepherosa Ziehau 		if (i == 0) {
23662b18938SSepherosa Ziehau 			/* Migrate ktrs on CPU0 to the new location */
23762b18938SSepherosa Ziehau 			memcpy(kcpu->ktr_buf, ktr_buf0, sizeof(ktr_buf0));
2388e273a1dSEirik Nygaard 		}
23962b18938SSepherosa Ziehau 	}
24062b18938SSepherosa Ziehau 	cpu_sfence();
24162b18938SSepherosa Ziehau 	ktr_entries = KTR_ENTRIES;
24262b18938SSepherosa Ziehau 	ktr_entries_mask = KTR_ENTRIES_MASK;
24362b18938SSepherosa Ziehau 
244bf0ecf68SMatthew Dillon 	callout_init_mp(&ktr_resync_callout);
245ba39e2e0SMatthew Dillon 	callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
24681540c2dSEirik Nygaard }
247ba39e2e0SMatthew Dillon SYSINIT(ktr_sysinit, SI_BOOT2_KLD, SI_ORDER_ANY, ktr_sysinit, NULL);
248d3776285SMatthew Dillon 
249374133e3SMatthew Dillon /*
250374133e3SMatthew Dillon  * Try to resynchronize the TSC's for all cpus.  This is really, really nasty.
251374133e3SMatthew Dillon  * We have to send an IPIQ message to all remote cpus, wait until they
252374133e3SMatthew Dillon  * get into their IPIQ processing code loop, then do an even stricter hard
253374133e3SMatthew Dillon  * loop to get the cpus as close to synchronized as we can to get the most
254374133e3SMatthew Dillon  * accurate reading.
255374133e3SMatthew Dillon  *
256374133e3SMatthew Dillon  * This callback occurs on cpu0.
257374133e3SMatthew Dillon  */
2588cb417faSMatthew Dillon #if KTR_TESTLOG
25972d379dfSMatthew Dillon static void ktr_pingpong_remote(void *dummy);
260c2751817SMatthew Dillon static void ktr_pipeline_remote(void *dummy);
2618cb417faSMatthew Dillon #endif
262374133e3SMatthew Dillon 
2631918fc5cSSascha Wildner #ifdef _RDTSC_SUPPORTED_
264b4c3db6fSMatthew Dillon 
265b4c3db6fSMatthew Dillon static void ktr_resync_remote(void *dummy);
266374133e3SMatthew Dillon 
267374133e3SMatthew Dillon /*
268374133e3SMatthew Dillon  * We use a callout callback instead of a systimer because we cannot afford
269374133e3SMatthew Dillon  * to preempt anyone to do this, or we might deadlock a spin-lock or
270374133e3SMatthew Dillon  * serializer between two cpus.
271374133e3SMatthew Dillon  */
272374133e3SMatthew Dillon static
273374133e3SMatthew Dillon void
ktr_resync_callback(void * dummy __unused)274374133e3SMatthew Dillon ktr_resync_callback(void *dummy __unused)
275374133e3SMatthew Dillon {
276f697b97dSMatthew Dillon 	struct lwkt_cpusync cs;
277ef4b87eeSSascha Wildner #if KTR_TESTLOG
278374133e3SMatthew Dillon 	int count;
279ef4b87eeSSascha Wildner #endif
280374133e3SMatthew Dillon 
281374133e3SMatthew Dillon 	KKASSERT(mycpu->gd_cpuid == 0);
28296328d40SMatthew Dillon 
28396328d40SMatthew Dillon #if KTR_TESTLOG
28496328d40SMatthew Dillon 	/*
28596328d40SMatthew Dillon 	 * Test logging
28696328d40SMatthew Dillon 	 */
28796328d40SMatthew Dillon 	if (ktr_testlogcnt) {
28896328d40SMatthew Dillon 		--ktr_testlogcnt;
28996328d40SMatthew Dillon 		cpu_disable_intr();
2902fa6678bSSascha Wildner 		KTR_LOG(testlog_charfmt,
2912fa6678bSSascha Wildner 		    (signed char)UCHAR_MAX, (signed char)UCHAR_MAX,
2922fa6678bSSascha Wildner 		    (unsigned char)-1, (unsigned char)-1,
2932fa6678bSSascha Wildner 		    (unsigned char)-1, (unsigned char)-1);
2942fa6678bSSascha Wildner 		KTR_LOG(testlog_shortfmt,
2952fa6678bSSascha Wildner 		    (short)USHRT_MAX, (short)USHRT_MAX,
2962fa6678bSSascha Wildner 		    (unsigned short)-1, (unsigned short)-1,
2972fa6678bSSascha Wildner 		    (unsigned short)-1, (unsigned short)-1);
2982fa6678bSSascha Wildner 		KTR_LOG(testlog_longfmt,
2992fa6678bSSascha Wildner 		    (long)ULONG_MAX, (long)ULONG_MAX,
3002fa6678bSSascha Wildner 		    (unsigned long)-1, (unsigned long)-1,
3012fa6678bSSascha Wildner 		    (unsigned long)-1, (unsigned long)-1);
3022fa6678bSSascha Wildner 		KTR_LOG(testlog_longlongfmt,
3032fa6678bSSascha Wildner 		    (long long)ULLONG_MAX, (long long)ULLONG_MAX,
3042fa6678bSSascha Wildner 		    (unsigned long long)-1, (unsigned long long)-1,
3052fa6678bSSascha Wildner 		    (unsigned long long)-1, (unsigned long long)-1);
3062fa6678bSSascha Wildner 		KTR_LOG(testlog_intmaxfmt,
3072fa6678bSSascha Wildner 		    (intmax_t)UINTMAX_MAX, (intmax_t)UINTMAX_MAX,
3082fa6678bSSascha Wildner 		    (uintmax_t)-1, (uintmax_t)-1,
3092fa6678bSSascha Wildner 		    (uintmax_t)-1, (uintmax_t)-1);
3102fa6678bSSascha Wildner 		KTR_LOG(testlog_ptrdifffmt,
3112fa6678bSSascha Wildner 		    (ptrdiff_t)PTRDIFF_MAX, (ptrdiff_t)PTRDIFF_MAX);
3122fa6678bSSascha Wildner 		KTR_LOG(testlog_sizefmt,
3132fa6678bSSascha Wildner 		    (ssize_t)SIZE_T_MAX, (ssize_t)SIZE_T_MAX,
3142fa6678bSSascha Wildner 		    (size_t)-1, (size_t)-1,
3152fa6678bSSascha Wildner 		    (size_t)-1, (size_t)-1);
31696328d40SMatthew Dillon 		cpu_enable_intr();
31796328d40SMatthew Dillon 	}
31872d379dfSMatthew Dillon 
31972d379dfSMatthew Dillon 	/*
32072d379dfSMatthew Dillon 	 * Test IPI messaging
32172d379dfSMatthew Dillon 	 */
32272d379dfSMatthew Dillon 	if (ktr_testipicnt && ktr_testipicnt_remainder == 0 && ncpus > 1) {
32372d379dfSMatthew Dillon 		ktr_testipicnt_remainder = ktr_testipicnt;
32472d379dfSMatthew Dillon 		ktr_testipicnt = 0;
32572d379dfSMatthew Dillon 		lwkt_send_ipiq_bycpu(1, ktr_pingpong_remote, NULL);
32672d379dfSMatthew Dillon 	}
327c2751817SMatthew Dillon 
328c2751817SMatthew Dillon 	/*
329c2751817SMatthew Dillon 	 * Test critical sections
330c2751817SMatthew Dillon 	 */
331c2751817SMatthew Dillon 	if (ktr_testcritcnt) {
332c2751817SMatthew Dillon 		crit_enter();
333c2751817SMatthew Dillon 		crit_exit();
334c2751817SMatthew Dillon 		logtest_noargs(crit_beg);
335c2751817SMatthew Dillon 		for (count = ktr_testcritcnt; count; --count) {
336c2751817SMatthew Dillon 			crit_enter();
337c2751817SMatthew Dillon 			crit_exit();
338c2751817SMatthew Dillon 		}
339c2751817SMatthew Dillon 		logtest_noargs(crit_end);
340c2751817SMatthew Dillon 		ktr_testcritcnt = 0;
341c2751817SMatthew Dillon 	}
342c2751817SMatthew Dillon 
343c2751817SMatthew Dillon 	/*
344c2751817SMatthew Dillon 	 * Test spinlock sections
345c2751817SMatthew Dillon 	 */
346c2751817SMatthew Dillon 	if (ktr_testspincnt) {
347c2751817SMatthew Dillon 		struct spinlock spin;
348c2751817SMatthew Dillon 
349ba87a4abSSascha Wildner 		spin_init(&spin, "ktrresync");
350287a8577SAlex Hornung 		spin_lock(&spin);
351287a8577SAlex Hornung 		spin_unlock(&spin);
352c2751817SMatthew Dillon 		logtest_noargs(spin_beg);
353c2751817SMatthew Dillon 		for (count = ktr_testspincnt; count; --count) {
354287a8577SAlex Hornung 			spin_lock(&spin);
355287a8577SAlex Hornung 			spin_unlock(&spin);
356d666840aSMatthew Dillon 		}
357d666840aSMatthew Dillon 		logtest_noargs(spin_end);
358c2751817SMatthew Dillon 		ktr_testspincnt = 0;
359c2751817SMatthew Dillon 	}
36096328d40SMatthew Dillon #endif
36196328d40SMatthew Dillon 
36296328d40SMatthew Dillon 	/*
36396328d40SMatthew Dillon 	 * Resynchronize the TSC
36496328d40SMatthew Dillon 	 */
365374133e3SMatthew Dillon 	if (ktr_resynchronize == 0)
366374133e3SMatthew Dillon 		goto done;
367374133e3SMatthew Dillon 	if ((cpu_feature & CPUID_TSC) == 0)
368374133e3SMatthew Dillon 		return;
3690b698dcaSMatthew Dillon 
370374133e3SMatthew Dillon 	crit_enter();
371f697b97dSMatthew Dillon 	lwkt_cpusync_init(&cs, smp_active_mask, ktr_resync_remote,
372f697b97dSMatthew Dillon 			  (void *)(intptr_t)mycpu->gd_cpuid);
373f697b97dSMatthew Dillon 	lwkt_cpusync_interlock(&cs);
374374133e3SMatthew Dillon 	ktr_sync_tsc = rdtsc();
375f697b97dSMatthew Dillon 	lwkt_cpusync_deinterlock(&cs);
376374133e3SMatthew Dillon 	crit_exit();
377374133e3SMatthew Dillon done:
378374133e3SMatthew Dillon 	callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
379374133e3SMatthew Dillon }
380374133e3SMatthew Dillon 
3810b698dcaSMatthew Dillon /*
382f697b97dSMatthew Dillon  * The remote-end of the KTR synchronization protocol runs on all cpus.
383f697b97dSMatthew Dillon  * The one we run on the controlling cpu updates its tsc continuously
384f697b97dSMatthew Dillon  * until the others have finished syncing (theoretically), but we don't
385f697b97dSMatthew Dillon  * loop forever.
386f697b97dSMatthew Dillon  *
387f697b97dSMatthew Dillon  * This is a bit ad-hoc but we need to avoid livelocking inside an IPI
388f697b97dSMatthew Dillon  * callback.  rdtsc() is a synchronizing instruction (I think).
3890b698dcaSMatthew Dillon  */
390374133e3SMatthew Dillon static void
ktr_resync_remote(void * arg)391f697b97dSMatthew Dillon ktr_resync_remote(void *arg)
392374133e3SMatthew Dillon {
393f697b97dSMatthew Dillon 	globaldata_t gd = mycpu;
394f697b97dSMatthew Dillon 	int64_t delta;
395f697b97dSMatthew Dillon 	int i;
396374133e3SMatthew Dillon 
397f697b97dSMatthew Dillon 	if (gd->gd_cpuid == (int)(intptr_t)arg) {
398f697b97dSMatthew Dillon 		for (i = 0; i < 2000; ++i)
399f697b97dSMatthew Dillon 			ktr_sync_tsc = rdtsc();
400f697b97dSMatthew Dillon 	} else {
401f697b97dSMatthew Dillon 		delta = rdtsc() - ktr_sync_tsc;
402f697b97dSMatthew Dillon 		if (tsc_offsets[gd->gd_cpuid] == 0)
403f697b97dSMatthew Dillon 			tsc_offsets[gd->gd_cpuid] = delta;
404f697b97dSMatthew Dillon 		tsc_offsets[gd->gd_cpuid] =
405f697b97dSMatthew Dillon 			(tsc_offsets[gd->gd_cpuid] * 7 + delta) / 8;
406374133e3SMatthew Dillon 	}
407374133e3SMatthew Dillon }
408374133e3SMatthew Dillon 
4098cb417faSMatthew Dillon #if KTR_TESTLOG
4108cb417faSMatthew Dillon 
41172d379dfSMatthew Dillon static
41272d379dfSMatthew Dillon void
ktr_pingpong_remote(void * dummy __unused)41372d379dfSMatthew Dillon ktr_pingpong_remote(void *dummy __unused)
41472d379dfSMatthew Dillon {
415c2751817SMatthew Dillon 	int other_cpu;
416c2751817SMatthew Dillon 
41772d379dfSMatthew Dillon 	logtest_noargs(pingpong);
418c2751817SMatthew Dillon 	other_cpu = 1 - mycpu->gd_cpuid;
41972d379dfSMatthew Dillon 	if (ktr_testipicnt_remainder) {
42072d379dfSMatthew Dillon 		--ktr_testipicnt_remainder;
421c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pingpong_remote, NULL);
422c2751817SMatthew Dillon 	} else {
423c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
424c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
425c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
426c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
427c2751817SMatthew Dillon 		lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
42872d379dfSMatthew Dillon 	}
42972d379dfSMatthew Dillon }
43072d379dfSMatthew Dillon 
431c2751817SMatthew Dillon static
432c2751817SMatthew Dillon void
ktr_pipeline_remote(void * dummy __unused)433c2751817SMatthew Dillon ktr_pipeline_remote(void *dummy __unused)
434c2751817SMatthew Dillon {
435c2751817SMatthew Dillon 	logtest_noargs(pipeline);
436c2751817SMatthew Dillon }
437c2751817SMatthew Dillon 
4388cb417faSMatthew Dillon #endif
4398cb417faSMatthew Dillon 
4401918fc5cSSascha Wildner #else	/* !_RDTSC_SUPPORTED_ */
4410b698dcaSMatthew Dillon 
4420b698dcaSMatthew Dillon /*
4430b698dcaSMatthew Dillon  * The resync callback for UP doesn't do anything other then run the test
4440b698dcaSMatthew Dillon  * log messages.  If test logging is not enabled, don't bother resetting
4450b698dcaSMatthew Dillon  * the callout.
4460b698dcaSMatthew Dillon  */
4470b698dcaSMatthew Dillon static
4480b698dcaSMatthew Dillon void
ktr_resync_callback(void * dummy __unused)4490b698dcaSMatthew Dillon ktr_resync_callback(void *dummy __unused)
4500b698dcaSMatthew Dillon {
4510b698dcaSMatthew Dillon #if KTR_TESTLOG
4520b698dcaSMatthew Dillon 	/*
4530b698dcaSMatthew Dillon 	 * Test logging
4540b698dcaSMatthew Dillon 	 */
4550b698dcaSMatthew Dillon 	if (ktr_testlogcnt) {
4560b698dcaSMatthew Dillon 		--ktr_testlogcnt;
4570b698dcaSMatthew Dillon 		cpu_disable_intr();
4582fa6678bSSascha Wildner 		KTR_LOG(testlog_charfmt,
4592fa6678bSSascha Wildner 		    (signed char)UCHAR_MAX, (signed char)UCHAR_MAX,
4602fa6678bSSascha Wildner 		    (unsigned char)-1, (unsigned char)-1,
4612fa6678bSSascha Wildner 		    (unsigned char)-1, (unsigned char)-1);
4622fa6678bSSascha Wildner 		KTR_LOG(testlog_shortfmt,
4632fa6678bSSascha Wildner 		    (short)USHRT_MAX, (short)USHRT_MAX,
4642fa6678bSSascha Wildner 		    (unsigned short)-1, (unsigned short)-1,
4652fa6678bSSascha Wildner 		    (unsigned short)-1, (unsigned short)-1);
4662fa6678bSSascha Wildner 		KTR_LOG(testlog_longfmt,
4672fa6678bSSascha Wildner 		    (long)ULONG_MAX, (long)ULONG_MAX,
4682fa6678bSSascha Wildner 		    (unsigned long)-1, (unsigned long)-1,
4692fa6678bSSascha Wildner 		    (unsigned long)-1, (unsigned long)-1);
4702fa6678bSSascha Wildner 		KTR_LOG(testlog_longlongfmt,
4712fa6678bSSascha Wildner 		    (long long)ULLONG_MAX, (long long)ULLONG_MAX,
4722fa6678bSSascha Wildner 		    (unsigned long long)-1, (unsigned long long)-1,
4732fa6678bSSascha Wildner 		    (unsigned long long)-1, (unsigned long long)-1);
4742fa6678bSSascha Wildner 		KTR_LOG(testlog_intmaxfmt,
4752fa6678bSSascha Wildner 		    (intmax_t)UINTMAX_MAX, (intmax_t)UINTMAX_MAX,
4762fa6678bSSascha Wildner 		    (uintmax_t)-1, (uintmax_t)-1,
4772fa6678bSSascha Wildner 		    (uintmax_t)-1, (uintmax_t)-1);
4782fa6678bSSascha Wildner 		KTR_LOG(testlog_ptrdifffmt,
4792fa6678bSSascha Wildner 		    (ptrdiff_t)PTRDIFF_MAX, (ptrdiff_t)PTRDIFF_MAX);
4802fa6678bSSascha Wildner 		KTR_LOG(testlog_sizefmt,
4812fa6678bSSascha Wildner 		    (ssize_t)SIZE_T_MAX, (ssize_t)SIZE_T_MAX,
4822fa6678bSSascha Wildner 		    (size_t)-1, (size_t)-1,
4832fa6678bSSascha Wildner 		    (size_t)-1, (size_t)-1);
4840b698dcaSMatthew Dillon 		cpu_enable_intr();
4850b698dcaSMatthew Dillon 	}
4860b698dcaSMatthew Dillon 	callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
4870b698dcaSMatthew Dillon #endif
4880b698dcaSMatthew Dillon }
4890b698dcaSMatthew Dillon 
490374133e3SMatthew Dillon #endif
49181540c2dSEirik Nygaard 
4920b698dcaSMatthew Dillon /*
4935bf48697SAggelos Economopoulos  * Setup the next empty slot and return it to the caller to store the data
4945bf48697SAggelos Economopoulos  * directly.
4950b698dcaSMatthew Dillon  */
4965bf48697SAggelos Economopoulos struct ktr_entry *
ktr_begin_write_entry(struct ktr_info * info,const char * file,int line)4975bf48697SAggelos Economopoulos ktr_begin_write_entry(struct ktr_info *info, const char *file, int line)
49881540c2dSEirik Nygaard {
499ddca1582SMatthew Dillon 	struct ktr_cpu_core *kcpu;
50081540c2dSEirik Nygaard 	struct ktr_entry *entry;
501d3776285SMatthew Dillon 	int cpu;
50281540c2dSEirik Nygaard 
503d3776285SMatthew Dillon 	cpu = mycpu->gd_cpuid;
504ddca1582SMatthew Dillon 	kcpu = &ktr_cpu[cpu].core;
505ab1b4385SMatthew Dillon 	if (panicstr)			/* stop logging during panic */
506ab1b4385SMatthew Dillon 		return NULL;
507ab1b4385SMatthew Dillon 	if (kcpu->ktr_buf == NULL)	/* too early in boot */
5085bf48697SAggelos Economopoulos 		return NULL;
509b4cb74b5SSascha Wildner 
510d3776285SMatthew Dillon 	crit_enter();
51162b18938SSepherosa Ziehau 	entry = kcpu->ktr_buf + (kcpu->ktr_idx & ktr_entries_mask);
512ddca1582SMatthew Dillon 	++kcpu->ktr_idx;
513527fddf7SMatthew Dillon #ifdef _RDTSC_SUPPORTED_
5140b698dcaSMatthew Dillon 	if (cpu_feature & CPUID_TSC) {
515374133e3SMatthew Dillon 		entry->ktr_timestamp = rdtsc() - tsc_offsets[cpu];
516527fddf7SMatthew Dillon 	} else
517527fddf7SMatthew Dillon #endif
518527fddf7SMatthew Dillon 	{
519d3776285SMatthew Dillon 		entry->ktr_timestamp = get_approximate_time_t();
5200b698dcaSMatthew Dillon 	}
521d3776285SMatthew Dillon 	entry->ktr_info = info;
52281540c2dSEirik Nygaard 	entry->ktr_file = file;
52381540c2dSEirik Nygaard 	entry->ktr_line = line;
5240b698dcaSMatthew Dillon 	crit_exit();
5255bf48697SAggelos Economopoulos 	return entry;
5265bf48697SAggelos Economopoulos }
5275bf48697SAggelos Economopoulos 
5285bf48697SAggelos Economopoulos int
ktr_finish_write_entry(struct ktr_info * info,struct ktr_entry * entry)5295bf48697SAggelos Economopoulos ktr_finish_write_entry(struct ktr_info *info, struct ktr_entry *entry)
5305bf48697SAggelos Economopoulos {
531af6ff89eSMatthew Dillon 	if (ktr_stacktrace)
532af6ff89eSMatthew Dillon 		cpu_ktr_caller(entry);
53381540c2dSEirik Nygaard #ifdef KTR_VERBOSE
534d3776285SMatthew Dillon 	if (ktr_verbose && info->kf_format) {
5355bf48697SAggelos Economopoulos 		kprintf("cpu%d ", mycpu->gd_cpuid);
53681540c2dSEirik Nygaard 		if (ktr_verbose > 1) {
5376ea70f76SSascha Wildner 			kprintf("%s.%d\t", entry->ktr_file, entry->ktr_line);
53881540c2dSEirik Nygaard 		}
5395bf48697SAggelos Economopoulos 		return !0;
54081540c2dSEirik Nygaard 	}
54181540c2dSEirik Nygaard #endif
5425bf48697SAggelos Economopoulos 	return 0;
543af6ff89eSMatthew Dillon }
544d3776285SMatthew Dillon 
54581540c2dSEirik Nygaard #ifdef DDB
54681540c2dSEirik Nygaard 
5478e273a1dSEirik Nygaard #define	NUM_LINES_PER_PAGE	19
54881540c2dSEirik Nygaard 
54981540c2dSEirik Nygaard struct tstate {
55081540c2dSEirik Nygaard 	int	cur;
55181540c2dSEirik Nygaard 	int	first;
55281540c2dSEirik Nygaard };
553d3776285SMatthew Dillon 
55481540c2dSEirik Nygaard static	int db_ktr_verbose;
555d3776285SMatthew Dillon static	int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx);
55681540c2dSEirik Nygaard 
DB_SHOW_COMMAND(ktr,db_ktr_all)55781540c2dSEirik Nygaard DB_SHOW_COMMAND(ktr, db_ktr_all)
55881540c2dSEirik Nygaard {
559ddca1582SMatthew Dillon 	struct ktr_cpu_core *kcpu;
5608e273a1dSEirik Nygaard 	int a_flag = 0;
5618e273a1dSEirik Nygaard 	int c;
56281540c2dSEirik Nygaard 	int nl = 0;
5638e273a1dSEirik Nygaard 	int i;
5648e273a1dSEirik Nygaard 	struct tstate tstate[MAXCPU];
5658e273a1dSEirik Nygaard 	int printcpu = -1;
56681540c2dSEirik Nygaard 
5678e273a1dSEirik Nygaard 	for(i = 0; i < ncpus; i++) {
568ddca1582SMatthew Dillon 		kcpu = &ktr_cpu[i].core;
5698e273a1dSEirik Nygaard 		tstate[i].first = -1;
57062b18938SSepherosa Ziehau 		tstate[i].cur = (kcpu->ktr_idx - 1) & ktr_entries_mask;
5718e273a1dSEirik Nygaard 	}
57281540c2dSEirik Nygaard 	db_ktr_verbose = 0;
5738e273a1dSEirik Nygaard 	while ((c = *(modif++)) != '\0') {
5748e273a1dSEirik Nygaard 		if (c == 'v') {
5758e273a1dSEirik Nygaard 			db_ktr_verbose = 1;
5768e273a1dSEirik Nygaard 		}
5778e273a1dSEirik Nygaard 		else if (c == 'a') {
5788e273a1dSEirik Nygaard 			a_flag = 1;
5798e273a1dSEirik Nygaard 		}
5808e273a1dSEirik Nygaard 		else if (c == 'c') {
5818e273a1dSEirik Nygaard 			printcpu = 0;
5828e273a1dSEirik Nygaard 			while ((c = *(modif++)) != '\0') {
5838e273a1dSEirik Nygaard 				if (isdigit(c)) {
5848e273a1dSEirik Nygaard 					printcpu *= 10;
5858e273a1dSEirik Nygaard 					printcpu += c - '0';
5868e273a1dSEirik Nygaard 				}
5878e273a1dSEirik Nygaard 				else {
5888e273a1dSEirik Nygaard 					modif++;
58981540c2dSEirik Nygaard 					break;
59081540c2dSEirik Nygaard 				}
5918e273a1dSEirik Nygaard 			}
5928e273a1dSEirik Nygaard 			modif--;
5938e273a1dSEirik Nygaard 		}
5948e273a1dSEirik Nygaard 	}
5958e273a1dSEirik Nygaard 	if (printcpu > ncpus - 1) {
5968e273a1dSEirik Nygaard 		db_printf("Invalid cpu number\n");
5978e273a1dSEirik Nygaard 		return;
5988e273a1dSEirik Nygaard 	}
5998e273a1dSEirik Nygaard 	/*
600ce7866b8SMatthew Dillon 	 * Loop throug all the buffers and print the content of them, sorted
6018e273a1dSEirik Nygaard 	 * by the timestamp.
6028e273a1dSEirik Nygaard 	 */
60381540c2dSEirik Nygaard 	while (1) {
6048e273a1dSEirik Nygaard 		int counter;
6058e273a1dSEirik Nygaard 		u_int64_t highest_ts;
6068e273a1dSEirik Nygaard 		struct ktr_entry *kp;
607ce7866b8SMatthew Dillon 		int highest_cpu;
608ce7866b8SMatthew Dillon 		int c;
6098e273a1dSEirik Nygaard 
610ce7866b8SMatthew Dillon 		if (a_flag == 1) {
611ce7866b8SMatthew Dillon 			c = cncheckc();
612ce7866b8SMatthew Dillon 			if (c != -1 && c != NOKEY)
6138e273a1dSEirik Nygaard 				return;
614ce7866b8SMatthew Dillon 		}
6158e273a1dSEirik Nygaard 		highest_ts = 0;
6168e273a1dSEirik Nygaard 		highest_cpu = -1;
6178e273a1dSEirik Nygaard 		/*
6188e273a1dSEirik Nygaard 		 * Find the lowest timestamp
6198e273a1dSEirik Nygaard 		 */
6208e273a1dSEirik Nygaard 		for (i = 0, counter = 0; i < ncpus; i++) {
621ddca1582SMatthew Dillon 			kcpu = &ktr_cpu[i].core;
622ddca1582SMatthew Dillon 			if (kcpu->ktr_buf == NULL)
623d3776285SMatthew Dillon 				continue;
6248e273a1dSEirik Nygaard 			if (printcpu != -1 && printcpu != i)
6258e273a1dSEirik Nygaard 				continue;
6268e273a1dSEirik Nygaard 			if (tstate[i].cur == -1) {
6278e273a1dSEirik Nygaard 				counter++;
6288e273a1dSEirik Nygaard 				if (counter == ncpus) {
6298e273a1dSEirik Nygaard 					db_printf("--- End of trace buffer ---\n");
6308e273a1dSEirik Nygaard 					return;
63181540c2dSEirik Nygaard 				}
6328e273a1dSEirik Nygaard 				continue;
6338e273a1dSEirik Nygaard 			}
634ddca1582SMatthew Dillon 			if (kcpu->ktr_buf[tstate[i].cur].ktr_timestamp > highest_ts) {
635ddca1582SMatthew Dillon 				highest_ts = kcpu->ktr_buf[tstate[i].cur].ktr_timestamp;
6368e273a1dSEirik Nygaard 				highest_cpu = i;
6378e273a1dSEirik Nygaard 			}
6388e273a1dSEirik Nygaard 		}
639438c526fSMatthew Dillon 		if (highest_cpu < 0) {
640438c526fSMatthew Dillon 			db_printf("no KTR data available\n");
641438c526fSMatthew Dillon 			break;
642438c526fSMatthew Dillon 		}
6438e273a1dSEirik Nygaard 		i = highest_cpu;
644ddca1582SMatthew Dillon 		kcpu = &ktr_cpu[i].core;
645ddca1582SMatthew Dillon 		kp = &kcpu->ktr_buf[tstate[i].cur];
6468e273a1dSEirik Nygaard 		if (tstate[i].first == -1)
6478e273a1dSEirik Nygaard 			tstate[i].first = tstate[i].cur;
6488e273a1dSEirik Nygaard 		if (--tstate[i].cur < 0)
64962b18938SSepherosa Ziehau 			tstate[i].cur = ktr_entries - 1;
6508e273a1dSEirik Nygaard 		if (tstate[i].first == tstate[i].cur) {
651d3776285SMatthew Dillon 			db_mach_vtrace(i, kp, tstate[i].cur + 1);
6528e273a1dSEirik Nygaard 			tstate[i].cur = -1;
6538e273a1dSEirik Nygaard 			continue;
6548e273a1dSEirik Nygaard 		}
655ddca1582SMatthew Dillon 		if (kcpu->ktr_buf[tstate[i].cur].ktr_info == NULL)
6568e273a1dSEirik Nygaard 			tstate[i].cur = -1;
6578e273a1dSEirik Nygaard 		if (db_more(&nl) == -1)
6588e273a1dSEirik Nygaard 			break;
659d3776285SMatthew Dillon 		if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0)
6608e273a1dSEirik Nygaard 			tstate[i].cur = -1;
66181540c2dSEirik Nygaard 	}
66281540c2dSEirik Nygaard }
66381540c2dSEirik Nygaard 
66481540c2dSEirik Nygaard static int
db_mach_vtrace(int cpu,struct ktr_entry * kp,int idx)665d3776285SMatthew Dillon db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx)
66681540c2dSEirik Nygaard {
667d3776285SMatthew Dillon 	if (kp->ktr_info == NULL)
66881540c2dSEirik Nygaard 		return(0);
669d3776285SMatthew Dillon 	db_printf("cpu%d ", cpu);
6708e273a1dSEirik Nygaard 	db_printf("%d: ", idx);
67181540c2dSEirik Nygaard 	if (db_ktr_verbose) {
67281540c2dSEirik Nygaard 		db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
67381540c2dSEirik Nygaard 		    kp->ktr_file, kp->ktr_line);
67481540c2dSEirik Nygaard 	}
675d3776285SMatthew Dillon 	db_printf("%s\t", kp->ktr_info->kf_name);
676af6ff89eSMatthew Dillon 	db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2);
67781540c2dSEirik Nygaard 	db_printf("\n");
67881540c2dSEirik Nygaard 
67981540c2dSEirik Nygaard 	return(1);
68081540c2dSEirik Nygaard }
68181540c2dSEirik Nygaard 
68281540c2dSEirik Nygaard #endif	/* DDB */
683