diff --git a/share/man/man9/ktr.9 b/share/man/man9/ktr.9 index c7f4138b855c..cfe9cacf8611 100644 --- a/share/man/man9/ktr.9 +++ b/share/man/man9/ktr.9 @@ -1,161 +1,172 @@ .\" Copyright (c) 2001 John H. Baldwin .\" .\" 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$ .\" -.Dd March 26, 2021 +.Dd April 12, 2022 .Dt KTR 9 .Os .Sh NAME .Nm CTR0 , CTR1 , CTR2 , CTR3 , CTR4 , CTR5 .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 CTR "u_int mask" "char *format" "..." +.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 with .Xr ddb 4 , .Xr gdb 1 or .Xr ktrdump 8 . .Pp Events are created and logged in the kernel via the +.Dv CTR +and .Dv CTR Ns Ar x macros. The first parameter is a mask of event types .Pq Dv KTR_* defined in .In sys/ktr_class.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 +string are zero to six arguments referenced by .Fa format . Each event is logged with a file name and source line number of the originating CTR call, and a timestamp in addition to the log message. .Pp The event is stored in the circular buffer with supplied arguments as is, and formatting is done at the dump time. Do not use pointers to the objects with limited lifetime, for instance, strings, because the pointer may become invalid when buffer is printed. .Pp -Note that the different macros differ only in the number of arguments each +The +.Dv CTR Ns Ar x +macros differ only in the number of arguments each one takes, as indicated by its name. .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 void mi_switch() { ... /* * Pick a new current process and record its start time. */ ... CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d)", p, p->p_pid); ... cpu_switch(); ... CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d)", p, p->p_pid); ... } .Ed .Sh SEE ALSO .Xr ktr 4 , .Xr ktrdump 8 .Sh HISTORY The KTR kernel tracing facility first appeared in .Bsx 3.0 and was imported into .Fx 5.0 . +.Pp +The +.Fn CTR +macro accepting a variable number of arguments first appeared in +.Fx 14.0 . .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. .Pp The arguments given in .Fn CTRx macros are stored as .Vt u_long , so do not pass arguments larger than size of an .Vt u_long type. For example passing 64bit arguments on 32bit architectures will give incorrect results. diff --git a/sys/sys/ktr.h b/sys/sys/ktr.h index dc06be9fa9cb..396ad21cffc7 100644 --- a/sys/sys/ktr.h +++ b/sys/sys/ktr.h @@ -1,269 +1,276 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * 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$ */ /* * Wraparound kernel trace buffer support. */ #ifndef _SYS_KTR_H_ #define _SYS_KTR_H_ #include /* * Version number for ktr_entry struct. Increment this when you break binary * compatibility. */ #define KTR_VERSION 2 #define KTR_PARMS 6 #ifndef LOCORE #include #include struct ktr_entry { u_int64_t ktr_timestamp; int ktr_cpu; int ktr_line; const char *ktr_file; const char *ktr_desc; struct thread *ktr_thread; u_long ktr_parms[KTR_PARMS]; }; extern cpuset_t ktr_cpumask; extern uint64_t ktr_mask; extern int ktr_entries; extern int ktr_verbose; extern volatile int ktr_idx; extern struct ktr_entry *ktr_buf; #ifdef KTR void ktr_tracepoint(uint64_t 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 */ #define CTR0(m, d) (void)0 #define CTR1(m, d, p1) (void)0 #define CTR2(m, d, p1, p2) (void)0 #define CTR3(m, d, p1, p2, p3) (void)0 #define CTR4(m, d, p1, p2, p3, p4) (void)0 #define CTR5(m, d, p1, p2, p3, p4, p5) (void)0 #define CTR6(m, d, p1, p2, p3, p4, p5, p6) (void)0 #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) +#define _KTR_MACRO(m, format, _1, _2, _3, _4, _5, _6, NAME, ...) \ + NAME +#define CTR(...) \ + _KTR_MACRO(__VA_ARGS__, CTR6, CTR5, CTR4, CTR3, CTR2, CTR1, \ + CTR0)(__VA_ARGS__) +#define TR(...) CTR(KTR_GEN, __VA_ARGS__) + /* * The event macros implement KTR graphic plotting facilities provided * by src/tools/sched/schedgraph.py. Three generic types of events are * supported: states, counters, and points. * * m is the ktr class for ktr_mask. * ident is the string identifier that owns the event (ie: "thread 10001") * etype is the type of event to plot (state, counter, point) * edat is the event specific data (state name, counter value, point name) * up to four attributes may be supplied as a name, value pair of arguments. * * etype and attribute names must be string constants. This minimizes the * number of ktr slots required by construction the final format strings * at compile time. Both must also include a colon and format specifier * (ie. "prio:%d", prio). It is recommended that string arguments be * contained within escaped quotes if they may contain ',' or ':' characters. * * The special attribute (KTR_ATTR_LINKED, ident) creates a reference to another * id on the graph for easy traversal of related graph elements. */ #define KTR_ATTR_LINKED "linkedto:\"%s\"" #define KTR_EFMT(egroup, ident, etype) \ "KTRGRAPH group:\"" egroup "\", id:\"%s\", " etype ", attributes: " #define KTR_EVENT0(m, egroup, ident, etype, edat) \ CTR2(m, KTR_EFMT(egroup, ident, etype) "none", ident, edat) #define KTR_EVENT1(m, egroup, ident, etype, edat, a0, v0) \ CTR3(m, KTR_EFMT(egroup, ident, etype) a0, ident, edat, (v0)) #define KTR_EVENT2(m, egroup, ident, etype, edat, a0, v0, a1, v1) \ CTR4(m, KTR_EFMT(egroup, ident, etype) a0 ", " a1, \ ident, edat, (v0), (v1)) #define KTR_EVENT3(m, egroup, ident, etype, edat, a0, v0, a1, v1, a2, v2)\ CTR5(m,KTR_EFMT(egroup, ident, etype) a0 ", " a1 ", " a2, \ ident, edat, (v0), (v1), (v2)) #define KTR_EVENT4(m, egroup, ident, etype, edat, \ a0, v0, a1, v1, a2, v2, a3, v3) \ CTR6(m,KTR_EFMT(egroup, ident, etype) a0 ", " a1 ", " a2 ", " a3,\ ident, edat, (v0), (v1), (v2), (v3)) /* * State functions graph state changes on an ident. */ #define KTR_STATE0(m, egroup, ident, state) \ KTR_EVENT0(m, egroup, ident, "state:\"%s\"", state) #define KTR_STATE1(m, egroup, ident, state, a0, v0) \ KTR_EVENT1(m, egroup, ident, "state:\"%s\"", state, a0, (v0)) #define KTR_STATE2(m, egroup, ident, state, a0, v0, a1, v1) \ KTR_EVENT2(m, egroup, ident, "state:\"%s\"", state, a0, (v0), a1, (v1)) #define KTR_STATE3(m, egroup, ident, state, a0, v0, a1, v1, a2, v2) \ KTR_EVENT3(m, egroup, ident, "state:\"%s\"", \ state, a0, (v0), a1, (v1), a2, (v2)) #define KTR_STATE4(m, egroup, ident, state, a0, v0, a1, v1, a2, v2, a3, v3)\ KTR_EVENT4(m, egroup, ident, "state:\"%s\"", \ state, a0, (v0), a1, (v1), a2, (v2), a3, (v3)) /* * Counter functions graph counter values. The counter id * must not be intermixed with a state id. */ #define KTR_COUNTER0(m, egroup, ident, counter) \ KTR_EVENT0(m, egroup, ident, "counter:%d", counter) #define KTR_COUNTER1(m, egroup, ident, edat, a0, v0) \ KTR_EVENT1(m, egroup, ident, "counter:%d", counter, a0, (v0)) #define KTR_COUNTER2(m, egroup, ident, counter, a0, v0, a1, v1) \ KTR_EVENT2(m, egroup, ident, "counter:%d", counter, a0, (v0), a1, (v1)) #define KTR_COUNTER3(m, egroup, ident, counter, a0, v0, a1, v1, a2, v2) \ KTR_EVENT3(m, egroup, ident, "counter:%d", \ counter, a0, (v0), a1, (v1), a2, (v2)) #define KTR_COUNTER4(m, egroup, ident, counter, a0, v0, a1, v1, a2, v2, a3, v3)\ KTR_EVENT4(m, egroup, ident, "counter:%d", \ counter, a0, (v0), a1, (v1), a2, (v2), a3, (v3)) /* * Point functions plot points of interest on counter or state graphs. */ #define KTR_POINT0(m, egroup, ident, point) \ KTR_EVENT0(m, egroup, ident, "point:\"%s\"", point) #define KTR_POINT1(m, egroup, ident, point, a0, v0) \ KTR_EVENT1(m, egroup, ident, "point:\"%s\"", point, a0, (v0)) #define KTR_POINT2(m, egroup, ident, point, a0, v0, a1, v1) \ KTR_EVENT2(m, egroup, ident, "point:\"%s\"", point, a0, (v0), a1, (v1)) #define KTR_POINT3(m, egroup, ident, point, a0, v0, a1, v1, a2, v2) \ KTR_EVENT3(m, egroup, ident, "point:\"%s\"", point, \ a0, (v0), a1, (v1), a2, (v2)) #define KTR_POINT4(m, egroup, ident, point, a0, v0, a1, v1, a2, v2, a3, v3)\ KTR_EVENT4(m, egroup, ident, "point:\"%s\"", \ point, a0, (v0), a1, (v1), a2, (v2), a3, (v3)) /* * Start functions denote the start of a region of code or operation * and should be paired with stop functions for timing of nested * sequences. * * Specifying extra attributes with the name "key" will result in * multi-part keys. For example a block device and offset pair * might be used to describe a buf undergoing I/O. */ #define KTR_START0(m, egroup, ident, key) \ KTR_EVENT0(m, egroup, ident, "start:0x%jX", (uintmax_t)key) #define KTR_START1(m, egroup, ident, key, a0, v0) \ KTR_EVENT1(m, egroup, ident, "start:0x%jX", (uintmax_t)key, a0, (v0)) #define KTR_START2(m, egroup, ident, key, a0, v0, a1, v1) \ KTR_EVENT2(m, egroup, ident, "start:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1)) #define KTR_START3(m, egroup, ident, key, a0, v0, a1, v1, a2, v2)\ KTR_EVENT3(m, egroup, ident, "start:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1), a2, (v2)) #define KTR_START4(m, egroup, ident, key, \ a0, v0, a1, v1, a2, v2, a3, v3) \ KTR_EVENT4(m, egroup, ident, "start:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1), a2, (v2), a3, (v3)) /* * Stop functions denote the end of a region of code or operation * and should be paired with start functions for timing of nested * sequences. */ #define KTR_STOP0(m, egroup, ident, key) \ KTR_EVENT0(m, egroup, ident, "stop:0x%jX", (uintmax_t)key) #define KTR_STOP1(m, egroup, ident, key, a0, v0) \ KTR_EVENT1(m, egroup, ident, "stop:0x%jX", (uintmax_t)key, a0, (v0)) #define KTR_STOP2(m, egroup, ident, key, a0, v0, a1, v1) \ KTR_EVENT2(m, egroup, ident, "stop:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1)) #define KTR_STOP3(m, egroup, ident, key, a0, v0, a1, v1, a2, v2)\ KTR_EVENT3(m, egroup, ident, "stop:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1), a2, (v2)) #define KTR_STOP4(m, egroup, ident, \ key, a0, v0, a1, v1, a2, v2, a3, v3) \ KTR_EVENT4(m, egroup, ident, "stop:0x%jX", (uintmax_t)key, \ a0, (v0), a1, (v1), a2, (v2), a3, (v3)) /* * 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 /* !LOCORE */ #endif /* !_SYS_KTR_H_ */