1a1382e7fSJohn Baldwin.\" Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org> 2a1382e7fSJohn Baldwin.\" 3a1382e7fSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 4a1382e7fSJohn Baldwin.\" modification, are permitted provided that the following conditions 5a1382e7fSJohn Baldwin.\" are met: 6a1382e7fSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright 7a1382e7fSJohn Baldwin.\" notice, this list of conditions and the following disclaimer. 8a1382e7fSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 9a1382e7fSJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 10a1382e7fSJohn Baldwin.\" documentation and/or other materials provided with the distribution. 11a1382e7fSJohn Baldwin.\" 12a1382e7fSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13a1382e7fSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14a1382e7fSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15a1382e7fSJohn Baldwin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 16a1382e7fSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17a1382e7fSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18a1382e7fSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19a1382e7fSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20a1382e7fSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21a1382e7fSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22a1382e7fSJohn Baldwin.\" SUCH DAMAGE. 23a1382e7fSJohn Baldwin.\" 24a1382e7fSJohn Baldwin.\" $FreeBSD$ 25a1382e7fSJohn Baldwin.\" 26*4eb43c2cSJohn Baldwin.Dd April 12, 2022 27a1382e7fSJohn Baldwin.Dt KTR 9 28a1382e7fSJohn Baldwin.Os 29a1382e7fSJohn Baldwin.Sh NAME 3083c93009SRuslan Ermilov.Nm CTR0 , CTR1 , CTR2 , CTR3 , CTR4 , CTR5 31a1382e7fSJohn Baldwin.Nd kernel tracing facility 32a1382e7fSJohn Baldwin.Sh SYNOPSIS 33f16b3c0dSChad David.In sys/param.h 3483c93009SRuslan Ermilov.In sys/ktr.h 35a1382e7fSJohn Baldwin.Vt "extern int ktr_cpumask" ; 36a1382e7fSJohn Baldwin.Vt "extern int ktr_entries" ; 37a1382e7fSJohn Baldwin.Vt "extern int ktr_extend" ; 38a1382e7fSJohn Baldwin.Vt "extern int ktr_mask" ; 39a1382e7fSJohn Baldwin.Vt "extern int ktr_verbose" ; 40a1382e7fSJohn Baldwin.Vt "extern struct ktr_entry ktr_buf[]" ; 41a1382e7fSJohn Baldwin.Ft void 42*4eb43c2cSJohn Baldwin.Fn CTR "u_int mask" "char *format" "..." 43*4eb43c2cSJohn Baldwin.Ft void 44a1382e7fSJohn Baldwin.Fn CTR0 "u_int mask" "char *format" 45a1382e7fSJohn Baldwin.Ft void 46a1382e7fSJohn Baldwin.Fn CTR1 "u_int mask" "char *format" "arg1" 47a1382e7fSJohn Baldwin.Ft void 48a1382e7fSJohn Baldwin.Fn CTR2 "u_int mask" "char *format" "arg1" "arg2" 49a1382e7fSJohn Baldwin.Ft void 50a1382e7fSJohn Baldwin.Fn CTR3 "u_int mask" "char *format" "arg1" "arg2" "arg3" 51a1382e7fSJohn Baldwin.Ft void 52a1382e7fSJohn Baldwin.Fn CTR4 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" 53a1382e7fSJohn Baldwin.Ft void 54a1382e7fSJohn Baldwin.Fn CTR5 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" 557b53693fSPawel Jakub Dawidek.Ft void 567b53693fSPawel Jakub Dawidek.Fn CTR6 "u_int mask" "char *format" "arg1" "arg2" "arg3" "arg4" "arg5" "arg6" 57a1382e7fSJohn Baldwin.Sh DESCRIPTION 5883c93009SRuslan ErmilovKTR provides a circular buffer of events that can be logged in a 5983c93009SRuslan Ermilov.Xr printf 9 6083c93009SRuslan Ermilovstyle 61a1382e7fSJohn Baldwinfashion. 62830d03bcSHiten PandyaThese events can then be dumped with 63830d03bcSHiten Pandya.Xr ddb 4 , 64830d03bcSHiten Pandya.Xr gdb 1 65a1382e7fSJohn Baldwinor 66830d03bcSHiten Pandya.Xr ktrdump 8 . 67a1382e7fSJohn Baldwin.Pp 68a1382e7fSJohn BaldwinEvents are created and logged in the kernel via the 69*4eb43c2cSJohn Baldwin.Dv CTR 70*4eb43c2cSJohn Baldwinand 71d6071d2fSRuslan Ermilov.Dv CTR Ns Ar x 72a1382e7fSJohn Baldwinmacros. 73a1382e7fSJohn BaldwinThe first parameter is a mask of event types 74a1382e7fSJohn Baldwin.Pq Dv KTR_* 75a1382e7fSJohn Baldwindefined in 7655baee8cSBryan Drewery.In sys/ktr_class.h . 77a1382e7fSJohn BaldwinThe event will be logged only if any of the event types specified in 78a1382e7fSJohn Baldwin.Fa mask 79a1382e7fSJohn Baldwinare enabled in the global event mask stored in 80a1382e7fSJohn Baldwin.Va ktr_mask . 81a1382e7fSJohn BaldwinThe 82a1382e7fSJohn Baldwin.Fa format 83a1382e7fSJohn Baldwinargument is a 84a1382e7fSJohn Baldwin.Xr printf 9 85a1382e7fSJohn Baldwinstyle format string used to build the text of the event log message. 86a1382e7fSJohn BaldwinFollowing the 87a1382e7fSJohn Baldwin.Fa format 88*4eb43c2cSJohn Baldwinstring are zero to six arguments referenced by 89a1382e7fSJohn Baldwin.Fa format . 900886d6e4SKonstantin BelousovEach event is logged with a file name and source line number of the 910886d6e4SKonstantin Belousovoriginating CTR call, and a timestamp in addition to the log message. 920886d6e4SKonstantin Belousov.Pp 930886d6e4SKonstantin BelousovThe event is stored in the circular buffer with supplied arguments as is, 940886d6e4SKonstantin Belousovand formatting is done at the dump time. 950886d6e4SKonstantin BelousovDo not use pointers to the objects with limited lifetime, for instance, 960886d6e4SKonstantin Belousovstrings, because the pointer may become invalid when buffer is printed. 970886d6e4SKonstantin Belousov.Pp 98*4eb43c2cSJohn BaldwinThe 99*4eb43c2cSJohn Baldwin.Dv CTR Ns Ar x 100*4eb43c2cSJohn Baldwinmacros differ only in the number of arguments each 101a1382e7fSJohn Baldwinone takes, as indicated by its name. 102a1382e7fSJohn Baldwin.Pp 103a1382e7fSJohn BaldwinThe 104a1382e7fSJohn Baldwin.Va ktr_entries 105a1382e7fSJohn Baldwinvariable contains the number of entries in the 106a1382e7fSJohn Baldwin.Va ktr_buf 107a1382e7fSJohn Baldwinarray. 108a1382e7fSJohn BaldwinThese variables are mostly useful for post-mortem crash dump tools to locate 109a1382e7fSJohn Baldwinthe base of the circular trace buffer and its length. 110a1382e7fSJohn Baldwin.Pp 111a1382e7fSJohn BaldwinThe 112a1382e7fSJohn Baldwin.Va ktr_mask 113a1382e7fSJohn Baldwinvariable contains the run time mask of events to log. 114a1382e7fSJohn Baldwin.Pp 115a1382e7fSJohn BaldwinThe CPU event mask is stored in the 116a1382e7fSJohn Baldwin.Va ktr_cpumask 117a1382e7fSJohn Baldwinvariable. 118a1382e7fSJohn Baldwin.Pp 119a1382e7fSJohn BaldwinThe 120a1382e7fSJohn Baldwin.Va ktr_verbose 121a1382e7fSJohn Baldwinvariable stores the verbose flag that controls whether events are logged to 122a1382e7fSJohn Baldwinthe console in addition to the event buffer. 123a1382e7fSJohn Baldwin.Sh EXAMPLES 124a1382e7fSJohn BaldwinThis example demonstrates the use of tracepoints at the 125a1382e7fSJohn Baldwin.Dv KTR_PROC 126a1382e7fSJohn Baldwinlogging level. 1274f068961SRuslan Ermilov.Bd -literal 128a1382e7fSJohn Baldwinvoid 129a1382e7fSJohn Baldwinmi_switch() 130a1382e7fSJohn Baldwin{ 131a1382e7fSJohn Baldwin ... 132a1382e7fSJohn Baldwin /* 133a1382e7fSJohn Baldwin * Pick a new current process and record its start time. 134a1382e7fSJohn Baldwin */ 135a1382e7fSJohn Baldwin ... 1360886d6e4SKonstantin Belousov CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d)", p, p->p_pid); 137a1382e7fSJohn Baldwin ... 138a1382e7fSJohn Baldwin cpu_switch(); 139a1382e7fSJohn Baldwin ... 1400886d6e4SKonstantin Belousov CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d)", p, p->p_pid); 141a1382e7fSJohn Baldwin ... 142a1382e7fSJohn Baldwin} 143a1382e7fSJohn Baldwin.Ed 144a1382e7fSJohn Baldwin.Sh SEE ALSO 145d627754aSJeff Roberson.Xr ktr 4 , 146d627754aSJeff Roberson.Xr ktrdump 8 147a1382e7fSJohn Baldwin.Sh HISTORY 148a1382e7fSJohn BaldwinThe KTR kernel tracing facility first appeared in 149a1382e7fSJohn Baldwin.Bsx 3.0 150a1382e7fSJohn Baldwinand was imported into 151a1382e7fSJohn Baldwin.Fx 5.0 . 152*4eb43c2cSJohn Baldwin.Pp 153*4eb43c2cSJohn BaldwinThe 154*4eb43c2cSJohn Baldwin.Fn CTR 155*4eb43c2cSJohn Baldwinmacro accepting a variable number of arguments first appeared in 156*4eb43c2cSJohn Baldwin.Fx 14.0 . 157a1382e7fSJohn Baldwin.Sh BUGS 158a1382e7fSJohn BaldwinCurrently there is one global buffer shared among all CPUs. 159a1382e7fSJohn BaldwinIt might be profitable at some point in time to use per-CPU buffers instead 160a1382e7fSJohn Baldwinso that if one CPU halts or starts spinning, then the log messages it 161a1382e7fSJohn Baldwinemitted just prior to halting or spinning will not be drowned out by events 162a1382e7fSJohn Baldwinfrom the other CPUs. 163a1a72ac7SPawel Jakub Dawidek.Pp 164a1a72ac7SPawel Jakub DawidekThe arguments given in 165a1a72ac7SPawel Jakub Dawidek.Fn CTRx 166a1a72ac7SPawel Jakub Dawidekmacros are stored as 167a1a72ac7SPawel Jakub Dawidek.Vt u_long , 168627a09e3SDaniel Gerzoso do not pass arguments larger than size of an 169a1a72ac7SPawel Jakub Dawidek.Vt u_long 170a1a72ac7SPawel Jakub Dawidektype. 171a1a72ac7SPawel Jakub DawidekFor example passing 64bit arguments on 32bit architectures will give incorrect 172a1a72ac7SPawel Jakub Dawidekresults. 173