From 81540c2d4cdb70abd3233d60cab10c6f79689701 Mon Sep 17 00:00:00 2001 From: Eirik Nygaard Date: Mon, 20 Sep 2004 20:38:17 +0000 Subject: [PATCH] Add KTR, a facility that logs kernel events to help debugging. You can access the logged information with ddb. If KTR_VERBOSE is defined a string will also be printed with printf() to your console. See ktr(4) and ktr(9) for information on how to use KTR. Obtained from: FreeBSD --- share/man/man4/Makefile | 3 +- share/man/man4/ktr.4 | 148 +++++++++++++++++++++++ share/man/man9/Makefile | 4 +- share/man/man9/ktr.9 | 148 +++++++++++++++++++++++ sys/conf/files | 3 +- sys/conf/options | 10 +- sys/config/LINT | 10 +- sys/i386/conf/LINT | 10 +- sys/kern/kern_ktr.c | 262 ++++++++++++++++++++++++++++++++++++++++ sys/sys/ktr.h | 180 +++++++++++++++++++++++++++ 10 files changed, 771 insertions(+), 7 deletions(-) create mode 100644 share/man/man4/ktr.4 create mode 100644 share/man/man9/ktr.9 create mode 100644 sys/kern/kern_ktr.c create mode 100644 sys/sys/ktr.h diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile index 256ec91ef5..87ddf89942 100644 --- a/share/man/man4/Makefile +++ b/share/man/man4/Makefile @@ -1,6 +1,6 @@ # @(#)Makefile 8.1 (Berkeley) 6/18/93 # $FreeBSD: src/share/man/man4/Makefile,v 1.83.2.66 2003/06/04 17:10:30 sam Exp $ -# $DragonFly: src/share/man/man4/Makefile,v 1.11 2004/09/02 05:45:20 asmodai Exp $ +# $DragonFly: src/share/man/man4/Makefile,v 1.12 2004/09/20 20:38:17 eirikn Exp $ MAN= aac.4 \ adv.4 \ @@ -84,6 +84,7 @@ MAN= aac.4 \ kame.4 \ keyboard.4 \ kld.4 \ + ktr.4 \ kue.4 \ lge.4 \ lo.4 \ diff --git a/share/man/man4/ktr.4 b/share/man/man4/ktr.4 new file mode 100644 index 0000000000..ad99c5578a --- /dev/null +++ b/share/man/man4/ktr.4 @@ -0,0 +1,148 @@ +.\" Copyright (c) 2001 John H. Baldwin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD: src/share/man/man4/ktr.4,v 1.9 2003/09/08 19:57:19 ru Exp $ +.\" $DragonFly: src/share/man/man4/ktr.4,v 1.1 2004/09/20 20:38:17 eirikn Exp $ +.\" +.Dd February 16, 2001 +.Dt KTR 4 +.Os +.Sh NAME +.Nm ktr +.Nd kernel tracing facility +.Sh SYNOPSIS +.Cd options KTR +.Cd options KTR_COMPILE=(KTR_LOCK|KTR_INTR|KTR_PROC) +.Cd options KTR_CPUMASK=0x3 +.Cd options KTR_ENTRIES=8192 +.Cd options KTR_MASK=(KTR_INTR|KTR_PROC) +.Cd options KTR_VERBOSE +.Sh DESCRIPTION +The +.Nm +facility allows kernel events to be logged while the kernel executes so that +they can be examined later when debugging. +The only mandatory option to enable +.Nm +is +.Dq Li options KTR . +.Pp +The +.Dv KTR_ENTRIES +option sets the size of the buffer of events. +It should be a power of two. +The size of the buffer in the currently running kernel can be found via the +read-only sysctl +.Va debug.ktr.entries . +By default the buffer contains 1024 entries. +.Ss Event Masking +Event levels can be enabled or disabled to trim excessive and overly verbose +logging. +First, a mask of events is specified at compile time via the +.Dv KTR_COMPILE +option to limit which events are actually compiled into the kernel. +The default value for this option is for all events to be enabled. +.Pp +Secondly, the actual events logged while the kernel runs can be further +masked via the run time event mask. +The +.Dv KTR_MASK +option sets the default value of the run time event mask. +The runtime event mask can also be set by the +.Xr loader 8 +via the +.Va debug.ktr.mask +environment variable. +It can also be examined and set after booting via the +.Va debug.ktr.mask +sysctl. +By default the run time mask is set to log only +.Dv KTR_GEN +events. +The definitions of the event mask bits can be found in +.In sys/ktr.h . +.Pp +Furthermore, there is a CPU event mask whose default value can be changed via +the +.Dv KTR_CPUMASK +option. +A CPU must have the bit corresponding to its logical id set in this bitmask +for events that occur on it to be logged. +This mask can be set by the +.Xr loader 8 +via the +.Va debug.ktr.cpumask +environment variable. +It can also be examined and set after booting via the +.Va debug.ktr.cpumask +sysctl. +By default events on all CPUs are enabled. +.Ss Verbose Mode +By default, events are only logged to the internal buffer for examination +later, but if the verbose flag is set then they are dumped to the kernel +console as well. +This flag can also be set from the loader via the +.Va debug.ktr.verbose +environment variable, or it can be examined and set after booting via the +.Va debug.ktr.verbose +sysctl. +If the flag is set to zero, which is the default, then verbose output is +disabled. +If the flag is set to one, then the contents of the log message and the CPU +number are printed to the kernel console. +If the flag is greater than one, then the filename and line number of the +event are output to the console in addition to the log message and the CPU +number. +The +.Dv KTR_VERBOSE +option sets the flag to one. +.Ss Examining the Events +The KTR buffer can be examined from within +.Xr ddb 4 +via the +.Ic show ktr Op Cm /v +command. +This command displays the contents of the trace buffer one page at a time. +At the +.Dq Li --more-- +prompt, the Enter key displays one more entry and prompts again. +The spacebar displays another page of entries. +Any other key quits. +By default the timestamp, filename, and line number are not displayed with +each log entry. +If the +.Cm /v +modifier is specified, then they are displayed in addition to the normal +output. +Note that the events are displayed in reverse chronological order. +That is, the most recent events are displayed first. +.Sh SEE ALSO +.Xr ktr 9 +.Sh HISTORY +The KTR kernel tracing facility first appeared in +.Bsx 3.0 +and was imported into +.Fx 5.0 +and +.Dx 1.1 . diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index 02c15d19d2..4a04160d2d 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/share/man/man9/Makefile,v 1.60.2.26 2003/06/13 01:04:17 hmp Exp $ -# $DragonFly: src/share/man/man9/Makefile,v 1.13 2004/07/27 14:50:56 hmp Exp $ +# $DragonFly: src/share/man/man9/Makefile,v 1.14 2004/09/20 20:38:17 eirikn Exp $ MAN= DECLARE_MODULE.9 DELAY.9 DEV_MODULE.9 KASSERT.9 MD5.9 \ MODULE_DEPEND.9 MODULE_VERSION.9 SPLASSERT.9 SYSCALL_MODULE.9 \ @@ -22,7 +22,7 @@ MAN= DECLARE_MODULE.9 DELAY.9 DEV_MODULE.9 KASSERT.9 MD5.9 \ ieee80211.9 ieee80211_crypto.9 ieee80211_input.9 ieee80211_ioctl.9 \ ieee80211_node.9 ieee80211_output.9 ieee80211_proto.9 \ ieee80211_radiotap.9 \ - ifnet.9 inittodr.9 intro.9 kernacc.9 lock.9 \ + ifnet.9 inittodr.9 intro.9 kernacc.9 ktr.9 lock.9 \ malloc.9 make_dev.9 mbuf.9 microseq.9 mi_switch.9 module.9 namei.9 \ pci.9 panic.9 physio.9 posix4.9 psignal.9 \ resettodr.9 \ diff --git a/share/man/man9/ktr.9 b/share/man/man9/ktr.9 new file mode 100644 index 0000000000..5aa381c950 --- /dev/null +++ b/share/man/man9/ktr.9 @@ -0,0 +1,148 @@ +.\" Copyright (c) 2001 John H. Baldwin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD: src/share/man/man9/ktr.9,v 1.7 2003/09/08 19:57:21 ru Exp $ +.\" $DragonFly: src/share/man/man9/ktr.9,v 1.1 2004/09/20 20:38:17 eirikn Exp $ +.\" +.Dd February 15, 2001 +.Dt KTR 9 +.Os +.Sh NAME +.Nm CTR0 , CTR1 , CTR2 , CTR3 , CTR4 , CTR5 , CTR6 +.Nd kernel tracing facility +.Sh SYNOPSIS +.In sys/param.h +.In sys/ktr.h +.Vt "extern int ktr_cpumask" ; +.Vt "extern int ktr_entries" ; +.Vt "extern int ktr_extend" ; +.Vt "extern int ktr_mask" ; +.Vt "extern int ktr_verbose" ; +.Vt "extern struct ktr_entry ktr_buf[]" ; +.Ft void +.Fn CTR0 "u_int mask" "char *format" +.Ft void +.Fn CTR1 "u_int mask" "char *format" "arg1" +.Ft void +.Fn CTR2 "u_int mask" "char *format" "arg1" "arg2" +.Ft void +.Fn CTR3 "u_int mask" "char *format" "arg1" "arg2" "arg3" +.Ft void +.Fn CTR4 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" +.Ft void +.Fn CTR5 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" +.Ft void +.Fn CTR6 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" "arg6" +.Sh DESCRIPTION +KTR provides a circular buffer of events that can be logged in a +.Xr printf 9 +style +fashion. +These events can then be dumped either via +.Xr ddb 4 +or +.Xr gdb 1 . +.Pp +Events are created and logged in the kernel via the +.Dv CTR Ns Ar x +macros. +The first parameter is a mask of event types +.Pq Dv KTR_* +defined in +.In sys/ktr.h . +The event will be logged only if any of the event types specified in +.Fa mask +are enabled in the global event mask stored in +.Va ktr_mask . +The +.Fa format +argument is a +.Xr printf 9 +style format string used to build the text of the event log message. +Following the +.Fa format +string are zero to five arguments referenced by +.Fa format . +Note that the different macros differ only in the number of arguments each +one takes, as indicated by its name. +Each event is logged with a timestamp in addition to the log message. +.Pp +The +.Va ktr_entries +variable contains the number of entries in the +.Va ktr_buf +array. +These variables are mostly useful for post-mortem crash dump tools to locate +the base of the circular trace buffer and its length. +.Pp +The +.Va ktr_mask +variable contains the run time mask of events to log. +.Pp +The CPU event mask is stored in the +.Va ktr_cpumask +variable. +.Pp +The +.Va ktr_verbose +variable stores the verbose flag that controls whether events are logged to +the console in addition to the event buffer. +.Sh EXAMPLES +This example demonstrates the use of tracepoints at the +.Dv KTR_PROC +logging level. +.Bd -literal -offset indent +void +mi_switch() +{ + ... + /* + * Pick a new current process and record its start time. + */ + ... + CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d, %s)", p, p->p_pid, + p->p_comm); + ... + cpu_switch(); + ... + CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d, %s)", p, p->p_pid, + p->p_comm); + ... +} +.Ed +.Sh SEE ALSO +.Xr ktr 4 +.Sh HISTORY +The KTR kernel tracing facility first appeared in +.Bsx 3.0 +and was imported into +.Fx 5.0 +and +.Dx 1.1 . +.Sh BUGS +Currently there is one global buffer shared among all CPUs. +It might be profitable at some point in time to use per-CPU buffers instead +so that if one CPU halts or starts spinning, then the log messages it +emitted just prior to halting or spinning will not be drowned out by events +from the other CPUs. diff --git a/sys/conf/files b/sys/conf/files index 1feeace964..2e2a3671df 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/conf/files,v 1.340.2.137 2003/06/04 17:10:30 sam Exp $ -# $DragonFly: src/sys/conf/files,v 1.73 2004/09/19 22:32:47 joerg Exp $ +# $DragonFly: src/sys/conf/files,v 1.74 2004/09/20 20:38:17 eirikn Exp $ # # The long compile-with and dependency lines are required because of # limitations in config: backslash-newline doesn't work in strings, and @@ -631,6 +631,7 @@ kern/kern_exit.c standard kern/kern_fork.c standard kern/kern_jail.c standard kern/kern_kthread.c standard +kern/kern_ktr.c optional ktr kern/kern_ktrace.c standard kern/kern_lock.c standard kern/kern_lockf.c standard diff --git a/sys/conf/options b/sys/conf/options index ed73172d74..ec6fdf082a 100644 --- a/sys/conf/options +++ b/sys/conf/options @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/conf/options,v 1.191.2.53 2003/06/04 17:56:58 sam Exp $ -# $DragonFly: src/sys/conf/options,v 1.24 2004/09/06 13:52:24 joerg Exp $ +# $DragonFly: src/sys/conf/options,v 1.25 2004/09/20 20:38:17 eirikn Exp $ # # On the handling of kernel options # @@ -511,6 +511,14 @@ HIFN_DEBUG opt_hifn.h HIFN_RNDTEST opt_hifn.h HIFN_NO_RNG opt_hifn.h +# KTR options +KTR opt_global.h +KTR_MASK opt_ktr.h +KTR_CPUMASK opt_ktr.h +KTR_COMPILE opt_global.h +KTR_ENTRIES opt_global.h +KTR_VERBOSE opt_ktr.h + # ed driver ED_NO_MIIBUS opt_ed.h diff --git a/sys/config/LINT b/sys/config/LINT index b0a26e73ef..9f9f130038 100644 --- a/sys/config/LINT +++ b/sys/config/LINT @@ -3,7 +3,7 @@ # as much of the source tree as it can. # # $FreeBSD: src/sys/i386/conf/LINT,v 1.749.2.144 2003/06/04 17:56:59 sam Exp $ -# $DragonFly: src/sys/config/LINT,v 1.36 2004/09/19 22:32:47 joerg Exp $ +# $DragonFly: src/sys/config/LINT,v 1.37 2004/09/20 20:38:17 eirikn Exp $ # # NB: You probably don't want to try running a kernel built from this # file. Instead, you should start from GENERIC, and add options from @@ -2786,3 +2786,11 @@ options SPX_HACK options TIMER_FREQ="((14318182+6)/12)" options VFS_BIO_DEBUG options XBONEHACK + +options KTR +options KTR_MASK=1 +options KTR_CPUMASK=1 +options KTR_COMPILE=(KTR_ALL) # Every trace class, see sys/ktr.h for + # the different class numbers +options KTR_ENTRIES=1024 +options KTR_VERBOSE=1 diff --git a/sys/i386/conf/LINT b/sys/i386/conf/LINT index 1d81b2094e..ee13123fb9 100644 --- a/sys/i386/conf/LINT +++ b/sys/i386/conf/LINT @@ -3,7 +3,7 @@ # as much of the source tree as it can. # # $FreeBSD: src/sys/i386/conf/LINT,v 1.749.2.144 2003/06/04 17:56:59 sam Exp $ -# $DragonFly: src/sys/i386/conf/Attic/LINT,v 1.36 2004/09/19 22:32:47 joerg Exp $ +# $DragonFly: src/sys/i386/conf/Attic/LINT,v 1.37 2004/09/20 20:38:17 eirikn Exp $ # # NB: You probably don't want to try running a kernel built from this # file. Instead, you should start from GENERIC, and add options from @@ -2786,3 +2786,11 @@ options SPX_HACK options TIMER_FREQ="((14318182+6)/12)" options VFS_BIO_DEBUG options XBONEHACK + +options KTR +options KTR_MASK=1 +options KTR_CPUMASK=1 +options KTR_COMPILE=(KTR_ALL) # Every trace class, see sys/ktr.h for + # the different class numbers +options KTR_ENTRIES=1024 +options KTR_VERBOSE=1 diff --git a/sys/kern/kern_ktr.c b/sys/kern/kern_ktr.c new file mode 100644 index 0000000000..d64bafe5bf --- /dev/null +++ b/sys/kern/kern_ktr.c @@ -0,0 +1,262 @@ +/*- + * Copyright (c) 2004 Eirik Nygaard + * All rights reserved. + * Copyright (c) 2000 John Baldwin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * This module holds the global variables used by KTR and the ktr_tracepoint() + * function that does the actual tracing. + */ + +/* + * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_ktr.c,v 1.43 2003/09/10 01:09:32 jhb Exp $ + * $DragonFly: src/sys/kern/kern_ktr.c,v 1.1 2004/09/20 20:38:17 eirikn Exp $ + */ + +#include "opt_ddb.h" +#include "opt_ktr.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#ifndef KTR_ENTRIES +#define KTR_ENTRIES 1024 +#endif + +#ifndef KTR_MASK +#define KTR_MASK (KTR_ALL) +#endif + +#ifndef KTR_CPUMASK +#define KTR_CPUMASK (~0) +#endif + +#ifndef KTR_TIME +#define KTR_TIME ktr_getts() +#endif + +#ifndef KTR_CPU +#define KTR_CPU mycpu->gd_cpuid; +#endif + +SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options"); + +int ktr_cpumask = KTR_CPUMASK; +TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask); +SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, ""); + +int ktr_mask = KTR_MASK; +TUNABLE_INT("debug.ktr.mask", &ktr_mask); +SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, ""); + +int ktr_entries = KTR_ENTRIES; +SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, ""); + +int ktr_version = KTR_VERSION; +SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, ""); + +volatile int ktr_idx = 0; +struct ktr_entry ktr_buf[KTR_ENTRIES]; +struct lwkt_token ktr_token; + +#ifdef KTR_VERBOSE +int ktr_verbose = KTR_VERBOSE; +TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); +SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); +#endif + +static void +ktr_sysinit(void *dummy) +{ + lwkt_token_init(&ktr_token); +} +SYSINIT(announce, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL); + +static __inline int +ktr_nextindex(void) +{ + lwkt_tokref ilock; + + lwkt_gettoken(&ilock, &ktr_token); + ktr_idx = (ktr_idx + 1) & (KTR_ENTRIES - 1); + lwkt_reltoken(&ilock); + + return(ktr_idx); +} + +static __inline uint64_t +ktr_getts(void) +{ + struct globaldata *gd = curthread->td_gd; + if (cpu_feature & CPUID_TSC) + return(rdtsc()); + return(gd->gd_time_seconds + basetime.tv_sec); +} + +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) +{ + struct ktr_entry *entry; + int cpu, newindex; + + if (panicstr) + return; + if ((ktr_mask & mask) == 0) + return; + cpu = KTR_CPU; + if (((1 << cpu) & ktr_cpumask) == 0) + return; + newindex = ktr_nextindex(); + entry = &ktr_buf[newindex]; + entry->ktr_timestamp = KTR_TIME; + entry->ktr_cpu = cpu; + if (file != NULL) + while (strncmp(file, "../", 3) == 0) + file += 3; + entry->ktr_file = file; + entry->ktr_line = line; +#ifdef KTR_VERBOSE + if (ktr_verbose) { +#ifdef SMP + printf("cpu%d ", cpu); +#endif + if (ktr_verbose > 1) { + printf("%s.%d\t", entry->ktr_file, + entry->ktr_line); + } + printf(format, arg1, arg2, arg3, arg4, arg5, arg6); + printf("\n"); + } +#endif + entry->ktr_desc = format; + entry->ktr_parms[0] = arg1; + entry->ktr_parms[1] = arg2; + entry->ktr_parms[2] = arg3; + entry->ktr_parms[3] = arg4; + entry->ktr_parms[4] = arg5; + entry->ktr_parms[5] = arg6; +} + +#ifdef DDB + +#define db_more_ktr(x) \ + db_more(x); \ + if (nl > NUM_LINES_PER_PAGE) \ + break + +struct tstate { + int cur; + int first; +}; +static struct tstate tstate; +static int db_ktr_verbose; +static int db_mach_vtrace(void); + +#define NUM_LINES_PER_PAGE 18 + +DB_SHOW_COMMAND(ktr, db_ktr_all) +{ + int nl = 0; + + tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); + tstate.first = -1; + if (strcmp(modif, "v") == 0) + db_ktr_verbose = 1; + else + db_ktr_verbose = 0; + if (strcmp(modif, "a") == 0) { + while (cncheckc() != -1) { + db_more_ktr(&nl); + if (db_mach_vtrace() == 0) + break; + } + } else { + while (1) { + db_more_ktr(&nl); + if (db_mach_vtrace() == 0) + break; + } + } +} + +static int +db_mach_vtrace(void) +{ + struct ktr_entry *kp; + + if (tstate.cur == tstate.first) { + db_printf("--- End of trace buffer ---\n"); + return (0); + } + kp = &ktr_buf[tstate.cur]; + + /* Skip over unused entries. */ + if (kp->ktr_desc == NULL) { + db_printf("--- End of trace buffer ---\n"); + return (0); + } + db_printf("%d: ", tstate.cur); +#ifdef SMP + db_printf("cpu%d ", kp->ktr_cpu); +#endif + if (db_ktr_verbose) { + db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp, + kp->ktr_file, kp->ktr_line); + } + db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], + kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], + kp->ktr_parms[5]); + db_printf("\n"); + + if (tstate.first == -1) + tstate.first = tstate.cur; + + if (--tstate.cur < 0) + tstate.cur = KTR_ENTRIES - 1; + + return (1); +} + +#endif /* DDB */ diff --git a/sys/sys/ktr.h b/sys/sys/ktr.h new file mode 100644 index 0000000000..c409746974 --- /dev/null +++ b/sys/sys/ktr.h @@ -0,0 +1,180 @@ +/*- + * Copyright (c) 1996 Berkeley Software Design, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Berkeley Software Design Inc's name may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from BSDI $Id: ktr.h,v 1.10.2.7 2000/03/16 21:44:42 cp Exp $ + * $FreeBSD: /repoman/r/ncvs/src/sys/sys/ktr.h,v 1.21 2003/03/11 20:07:22 jhb Exp $ + * $DragonFly: src/sys/sys/ktr.h,v 1.1 2004/09/20 20:38:17 eirikn Exp $ + */ + +/* + * Wraparound kernel trace buffer support. + */ + +#ifndef _SYS_KTR_H_ +#define _SYS_KTR_H_ + +#include "opt_ktr.h" + +/* + * Trace classes + */ +#define KTR_GEN 0x00000001 /* General (TR) */ +#define KTR_NET 0x00000002 /* Network */ +#define KTR_DEV 0x00000004 /* Device driver */ +#define KTR_LOCK 0x00000008 /* MP locking */ +#define KTR_SMP 0x00000010 /* MP general */ +#define KTR_FS 0x00000020 /* Filesystem */ +#define KTR_PMAP 0x00000040 /* Pmap tracing */ +#define KTR_MALLOC 0x00000080 /* Malloc tracing */ +#define KTR_TRAP 0x00000100 /* Trap processing */ +#define KTR_INTR 0x00000200 /* Interrupt tracing */ +#define KTR_SIG 0x00000400 /* Signal processing */ +#define KTR_CLK 0x00000800 /* hardclock verbose */ +#define KTR_PROC 0x00001000 /* Process scheduling */ +#define KTR_SYSC 0x00002000 /* System call */ +#define KTR_INIT 0x00004000 /* System initialization */ +#define KTR_KGDB 0x00008000 /* Trace kgdb internals */ +#define KTR_IO 0x00010000 /* Upper I/O */ +#define KTR_EVH 0x00020000 /* Eventhandler */ +#define KTR_NFS 0x00040000 /* The obvious */ +#define KTR_VOP 0x00080000 /* The obvious */ +#define KTR_VM 0x00100000 /* The virtual memory system */ +#define KTR_WITNESS 0x00200000 +#define KTR_RUNQ 0x00400000 /* Run queue */ +#define KTR_CONTENTION 0x00800000 /* Lock contention */ +#define KTR_ALL 0x00ffffff + +/* + * Trace classes which can be assigned to particular use at compile time + * These must remain in high 22 as some assembly code counts on it + */ +#define KTR_CT1 0x01000000 +#define KTR_CT2 0x02000000 +#define KTR_CT3 0x04000000 +#define KTR_CT4 0x08000000 +#define KTR_CT5 0x10000000 +#define KTR_CT6 0x20000000 +#define KTR_CT7 0x40000000 +#define KTR_CT8 0x80000000 + +/* Trace classes to compile in */ +#ifndef KTR_COMPILE +#define KTR_COMPILE (KTR_ALL) +#endif + +/* + * Version number for ktr_entry struct. Increment this when you break binary + * compatibility. + */ +#define KTR_VERSION 1 + +#define KTR_PARMS 6 + +#ifndef LOCORE + +struct ktr_entry { + u_int64_t ktr_timestamp; + int ktr_cpu; + int ktr_line; + const char *ktr_file; + const char *ktr_desc; + u_long ktr_parms[KTR_PARMS]; +}; + +extern int ktr_cpumask; +extern int ktr_mask; +extern int ktr_entries; +extern int ktr_verbose; + +extern volatile int ktr_idx; +extern struct ktr_entry ktr_buf[]; + +#endif /* !LOCORE */ + +#ifdef KTR + +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); + +#define CTR6(m, format, p1, p2, p3, p4, p5, p6) do { \ + if (KTR_COMPILE & (m)) \ + ktr_tracepoint((m), __FILE__, __LINE__, format, \ + (u_long)(p1), (u_long)(p2), (u_long)(p3), \ + (u_long)(p4), (u_long)(p5), (u_long)(p6)); \ + } while(0) +#define CTR0(m, format) CTR6(m, format, 0, 0, 0, 0, 0, 0) +#define CTR1(m, format, p1) CTR6(m, format, p1, 0, 0, 0, 0, 0) +#define CTR2(m, format, p1, p2) CTR6(m, format, p1, p2, 0, 0, 0, 0) +#define CTR3(m, format, p1, p2, p3) CTR6(m, format, p1, p2, p3, 0, 0, 0) +#define CTR4(m, format, p1, p2, p3, p4) CTR6(m, format, p1, p2, p3, p4, 0, 0) +#define CTR5(m, format, p1, p2, p3, p4, p5) CTR6(m, format, p1, p2, p3, p4, p5, 0) +#else /* KTR */ +#undef KTR_COMPILE +#define KTR_COMPILE 0 +#define CTR0(m, d) +#define CTR1(m, d, p1) +#define CTR2(m, d, p1, p2) +#define CTR3(m, d, p1, p2, p3) +#define CTR4(m, d, p1, p2, p3, p4) +#define CTR5(m, d, p1, p2, p3, p4, p5) +#define CTR6(m, d, p1, p2, p3, p4, p5, p6) +#endif /* KTR */ + +#define TR0(d) CTR0(KTR_GEN, d) +#define TR1(d, p1) CTR1(KTR_GEN, d, p1) +#define TR2(d, p1, p2) CTR2(KTR_GEN, d, p1, p2) +#define TR3(d, p1, p2, p3) CTR3(KTR_GEN, d, p1, p2, p3) +#define TR4(d, p1, p2, p3, p4) CTR4(KTR_GEN, d, p1, p2, p3, p4) +#define TR5(d, p1, p2, p3, p4, p5) CTR5(KTR_GEN, d, p1, p2, p3, p4, p5) +#define TR6(d, p1, p2, p3, p4, p5, p6) CTR6(KTR_GEN, d, p1, p2, p3, p4, p5, p6) + +/* + * Trace initialization events, similar to CTR with KTR_INIT, but + * completely ifdef'ed out if KTR_INIT isn't in KTR_COMPILE (to + * save string space, the compiler doesn't optimize out strings + * for the conditional ones above). + */ +#if (KTR_COMPILE & KTR_INIT) != 0 +#define ITR0(d) CTR0(KTR_INIT, d) +#define ITR1(d, p1) CTR1(KTR_INIT, d, p1) +#define ITR2(d, p1, p2) CTR2(KTR_INIT, d, p1, p2) +#define ITR3(d, p1, p2, p3) CTR3(KTR_INIT, d, p1, p2, p3) +#define ITR4(d, p1, p2, p3, p4) CTR4(KTR_INIT, d, p1, p2, p3, p4) +#define ITR5(d, p1, p2, p3, p4, p5) CTR5(KTR_INIT, d, p1, p2, p3, p4, p5) +#define ITR6(d, p1, p2, p3, p4, p5, p6) CTR6(KTR_INIT, d, p1, p2, p3, p4, p5, p6) +#else +#define ITR0(d) +#define ITR1(d, p1) +#define ITR2(d, p1, p2) +#define ITR3(d, p1, p2, p3) +#define ITR4(d, p1, p2, p3, p4) +#define ITR5(d, p1, p2, p3, p4, p5) +#define ITR6(d, p1, p2, p3, p4, p5, p6) +#endif + +#endif /* !_SYS_KTR_H_ */ -- 2.28.0