198491bfaSMark Johnston.\" Copyright (c) 2013 Mark Johnston <markj@freebsd.org> 298491bfaSMark Johnston.\" All rights reserved. 398491bfaSMark Johnston.\" 498491bfaSMark Johnston.\" Redistribution and use in source and binary forms, with or without 598491bfaSMark Johnston.\" modification, are permitted provided that the following conditions 698491bfaSMark Johnston.\" are met: 798491bfaSMark Johnston.\" 1. Redistributions of source code must retain the above copyright 898491bfaSMark Johnston.\" notice, this list of conditions and the following disclaimer. 998491bfaSMark Johnston.\" 2. Redistributions in binary form must reproduce the above copyright 1098491bfaSMark Johnston.\" notice, this list of conditions and the following disclaimer in the 1198491bfaSMark Johnston.\" documentation and/or other materials provided with the distribution. 1298491bfaSMark Johnston.\" 1398491bfaSMark Johnston.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1498491bfaSMark Johnston.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1598491bfaSMark Johnston.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1698491bfaSMark Johnston.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1798491bfaSMark Johnston.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1898491bfaSMark Johnston.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1998491bfaSMark Johnston.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2098491bfaSMark Johnston.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2198491bfaSMark Johnston.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2298491bfaSMark Johnston.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2398491bfaSMark Johnston.\" SUCH DAMAGE. 2498491bfaSMark Johnston.\" 2598491bfaSMark Johnston.\" $FreeBSD$ 2698491bfaSMark Johnston.\" 27c6cb04a4SMark Johnston.Dd August 17, 2013 2898491bfaSMark Johnston.Dt SDT 9 2998491bfaSMark Johnston.Os 3098491bfaSMark Johnston.Sh NAME 3198491bfaSMark Johnston.Nm SDT 3298491bfaSMark Johnston.Nd a DTrace framework for adding statically-defined tracing probes 3398491bfaSMark Johnston.Sh SYNOPSIS 3498491bfaSMark Johnston.In sys/sdt.h 3598491bfaSMark Johnston.Fn SDT_PROVIDER_DECLARE prov 3698491bfaSMark Johnston.Fn SDT_PROVIDER_DEFINE prov 3798491bfaSMark Johnston.Fn SDT_PROBE_DECLARE prov mod func name 38*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE prov mod func name 39*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE0 prov mod func name 40*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE1 prov mod func name arg0 41*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE2 prov mod func name arg0 arg1 42*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE3 prov mod func name arg0 arg1 arg2 43*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE4 prov mod func name arg0 arg1 arg2 arg3 44*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE5 prov mod func name arg0 arg1 arg2 arg3 arg4 45*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE6 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5 46*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE7 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5 \ 4798491bfaSMark Johnston arg6 48*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE0_XLATE prov mod func name 49*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE1_XLATE prov mod func name arg0 xarg0 50*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE2_XLATE prov mod func name arg0 xarg0 arg1 xarg1 51*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE3_XLATE prov mod func name arg0 xarg0 arg1 xarg1 \ 52c6cb04a4SMark Johnston arg2 xarg2 53*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE4_XLATE prov mod func name arg0 xarg0 arg1 xarg1 \ 54c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 55*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE5_XLATE prov mod func name arg0 xarg0 arg1 xarg1 \ 56c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 arg4 xarg4 57*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE6_XLATE prov mod func name arg0 xarg0 arg1 xarg1 \ 58c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 arg4 xarg4 arg5 xarg5 59*d9fae5abSAndriy Gapon.Fn SDT_PROBE_DEFINE7_XLATE prov mod func name arg0 xarg0 arg1 xarg1 \ 60c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 arg4 xarg4 arg5 xarg5 arg6 xarg6 6198491bfaSMark Johnston.Fn SDT_PROBE0 prov mod func name 6298491bfaSMark Johnston.Fn SDT_PROBE1 prov mod func name arg0 6398491bfaSMark Johnston.Fn SDT_PROBE2 prov mod func name arg0 arg1 6498491bfaSMark Johnston.Fn SDT_PROBE3 prov mod func name arg0 arg1 arg2 6598491bfaSMark Johnston.Fn SDT_PROBE4 prov mod func name arg0 arg1 arg2 arg3 6698491bfaSMark Johnston.Fn SDT_PROBE5 prov mod func name arg0 arg1 arg2 arg3 arg4 6798491bfaSMark Johnston.Fn SDT_PROBE6 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5 6898491bfaSMark Johnston.Fn SDT_PROBE7 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5 arg6 6998491bfaSMark Johnston.Sh DESCRIPTION 7098491bfaSMark JohnstonThe 7198491bfaSMark Johnston.Nm 7298491bfaSMark Johnstonmacros allow programmers to define static trace points in kernel code. 7398491bfaSMark JohnstonThese trace points are used by the 7498491bfaSMark Johnston.Nm 7598491bfaSMark Johnstonframework to create DTrace probes, allowing the code to be instrumented 7698491bfaSMark Johnstonusing 7798491bfaSMark Johnston.Xr dtrace 1 . 7898491bfaSMark JohnstonBy default, 7998491bfaSMark Johnston.Nm 8098491bfaSMark Johnstontrace points are disabled and have no effect on the surrounding code. 8198491bfaSMark JohnstonWhen a DTrace probe corresponding to a given trace point is enabled, threads 8298491bfaSMark Johnstonthat execute the trace point will call a handler and cause the probe to fire. 8398491bfaSMark JohnstonMoreover, trace points can take arguments, making it possible to pass data 8498491bfaSMark Johnstonto the DTrace framework when an enabled probe fires. 8598491bfaSMark Johnston.Pp 8698491bfaSMark JohnstonMultiple trace points may correspond to a single DTrace probe, allowing 8798491bfaSMark Johnstonprogrammers to create DTrace probes that correspond to logical system events 8898491bfaSMark Johnstonrather than tying probes to specific code execution paths. 8998491bfaSMark JohnstonFor instance, a DTrace probe corresponding to the arrival of an IP packet into 9098491bfaSMark Johnstonthe network stack may be defined using two 9198491bfaSMark Johnston.Nm 9298491bfaSMark Johnstontrace points: one for IPv4 packets and one for IPv6 packets. 9398491bfaSMark Johnston.Pp 9498491bfaSMark JohnstonIn addition to defining DTrace probes, the 9598491bfaSMark Johnston.Nm 9698491bfaSMark Johnstonmacros allow programmers to define new DTrace providers, making it possible to 9798491bfaSMark Johnstonnamespace logically-related probes. 9898491bfaSMark JohnstonAn example is FreeBSD's sctp provider, which contains 9998491bfaSMark Johnston.Nm 10098491bfaSMark Johnstonprobes for FreeBSD's 10198491bfaSMark Johnston.Xr sctp 4 10298491bfaSMark Johnstonimplementation. 10398491bfaSMark Johnston.Pp 10498491bfaSMark JohnstonThe 10598491bfaSMark Johnston.Fn SDT_PROVIDER_DECLARE 10698491bfaSMark Johnstonand 10798491bfaSMark Johnston.Fn SDT_PROVIDER_DEFINE 10898491bfaSMark Johnstonmacros are used respectively to declare and define a DTrace provider named 10998491bfaSMark Johnston.Ar prov 11098491bfaSMark Johnstonwith the 11198491bfaSMark Johnston.Nm 11298491bfaSMark Johnstonframework. 11398491bfaSMark JohnstonA provider need only be defined once; however, the provider must be declared 11498491bfaSMark Johnstonbefore defining any 11598491bfaSMark Johnston.Nm 11698491bfaSMark Johnstonprobes belonging to that provider. 11798491bfaSMark Johnston.Pp 11898491bfaSMark JohnstonSimilarly, the 11998491bfaSMark Johnston.Fn SDT_PROBE_DECLARE 12098491bfaSMark Johnstonand 12198491bfaSMark Johnston.Fn SDT_PROBE_DEFINE* 12298491bfaSMark Johnstonmacros are used to declare and define DTrace probes using the 12398491bfaSMark Johnston.Nm 12498491bfaSMark Johnstonframework. 12598491bfaSMark JohnstonOnce a probe has been defined, trace points for that probe may be added to 12698491bfaSMark Johnstonkernel code. 12798491bfaSMark JohnstonDTrace probe identifiers consist of a provider, module, function and name, all 12898491bfaSMark Johnstonof which may be specified in the 12998491bfaSMark Johnston.Nm 13098491bfaSMark Johnstonprobe definition. 13198491bfaSMark JohnstonNote that probes should not specify a module name: the module name of a probe is 13298491bfaSMark Johnstonused to determine whether or not it should be destroyed when a kernel module is 13398491bfaSMark Johnstonunloaded. 13498491bfaSMark JohnstonSee the 13598491bfaSMark Johnston.Sx BUGS 13698491bfaSMark Johnstonsection. 13798491bfaSMark JohnstonNote in particular that probes must not be defined across multiple kernel 13898491bfaSMark Johnstonmodules. 139*d9fae5abSAndriy Gapon.Pp 140*d9fae5abSAndriy GaponIf 14198491bfaSMark Johnston.Ql - 142*d9fae5abSAndriy Gaponcharacter (dash) is wanted in a probe name, 143*d9fae5abSAndriy Gaponthen it should be represented as 144*d9fae5abSAndriy Gapon.Ql __ 145*d9fae5abSAndriy Gapon(double underscore) in the probe 14698491bfaSMark Johnston.Ar name 147*d9fae5abSAndriy Gaponparameter passed to various 148*d9fae5abSAndriy Gapon.Fn SDT_* 149*d9fae5abSAndriy Gaponmacros, 150*d9fae5abSAndriy Gaponbecause of technical reasons 151*d9fae5abSAndriy Gapon(a dash is not valid in C identifiers). 15298491bfaSMark Johnston.Pp 15398491bfaSMark JohnstonThe 15498491bfaSMark Johnston.Fn SDT_PROBE_DEFINE* 15598491bfaSMark Johnstonmacros also allow programmers to declare the types of the arguments that are 15698491bfaSMark Johnstonpassed to probes. 15798491bfaSMark JohnstonThis is optional; if the argument types are omitted (through use of the 15898491bfaSMark Johnston.Fn SDT_PROBE_DEFINE 15998491bfaSMark Johnstonmacro), users wishing to make use of the arguments will have to manually cast 16098491bfaSMark Johnstonthem to the correct types in their D scripts. 16198491bfaSMark JohnstonIt is strongly recommended that probe definitions include a declaration of their 16298491bfaSMark Johnstonargument types. 16398491bfaSMark Johnston.Pp 16498491bfaSMark JohnstonThe 165c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE*_XLATE 166c6cb04a4SMark Johnstonmacros are used for probes whose argument types are to be dynamically translated 167c6cb04a4SMark Johnstonto the types specified by the corresponding 168c6cb04a4SMark Johnston.Ar xarg 169c6cb04a4SMark Johnstonarguments. 170c6cb04a4SMark JohnstonThis is mainly useful when porting probe definitions from other operating 171c6cb04a4SMark Johnstonsystems. 172c6cb04a4SMark JohnstonAs seen by 173c6cb04a4SMark Johnston.Xr dtrace 1 , 174c6cb04a4SMark Johnstonthe arguments of a probe defined using these macros will have types which match 175c6cb04a4SMark Johnstonthe 176c6cb04a4SMark Johnston.Ar xarg 177c6cb04a4SMark Johnstontypes in the probe definition. 178c6cb04a4SMark JohnstonHowever, the arguments passed in at the trace point will have types matching the 179c6cb04a4SMark Johnstonnative argument types in the probe definition, and thus the native type is 180c6cb04a4SMark Johnstondynamically translated to the translated type. 181c6cb04a4SMark JohnstonSo long as an appropriate translator is defined in 182c6cb04a4SMark Johnston.Pa /usr/lib/dtrace , 183c6cb04a4SMark Johnstonscripts making use of the probe need not concern themselves with the underlying 184c6cb04a4SMark Johnstontype of a given 185c6cb04a4SMark Johnston.Nm 186c6cb04a4SMark Johnstonprobe argument. 187c6cb04a4SMark Johnston.Pp 188c6cb04a4SMark JohnstonThe 18998491bfaSMark Johnston.Fn SDT_PROBE* 19098491bfaSMark Johnstonmacros are used to create 19198491bfaSMark Johnston.Nm 19298491bfaSMark Johnstontrace points. 19398491bfaSMark JohnstonThey are meant to be added to executable code and can be used to instrument the 19498491bfaSMark Johnstoncode in which they are called. 19598491bfaSMark Johnston.Sh EXAMPLES 19698491bfaSMark JohnstonThe following probe definition will create a DTrace probe called 19798491bfaSMark Johnston.Ql icmp::unreach:pkt-receive , 19898491bfaSMark Johnstonwhich would hypothetically be triggered when the kernel receives an ICMP packet 19998491bfaSMark Johnstonof type Destination Unreachable: 20098491bfaSMark Johnston.Bd -literal -offset indent 20198491bfaSMark JohnstonSDT_PROVIDER_DECLARE(icmp); 20298491bfaSMark Johnston 203c6cb04a4SMark JohnstonSDT_PROBE_DEFINE1(icmp, , unreach, pkt_receive, pkt-receive, 204c6cb04a4SMark Johnston "struct icmp *"); 20598491bfaSMark Johnston 20698491bfaSMark Johnston.Ed 207c6cb04a4SMark JohnstonThis particular probe would take a single argument: a pointer to the struct 208c6cb04a4SMark Johnstoncontaining the ICMP header for the packet. 20998491bfaSMark JohnstonNote that the module name of this probe is not specified. 21098491bfaSMark Johnston.Pp 21198491bfaSMark JohnstonConsider a DTrace probe which fires when the network stack receives an IP 21298491bfaSMark Johnstonpacket. 21398491bfaSMark JohnstonSuch a probe would be defined by multiple tracepoints: 21498491bfaSMark Johnston.Bd -literal -offset indent 215c6cb04a4SMark JohnstonSDT_PROBE_DEFINE3(ip, , , receive, receive, "struct ifnet *", 216c6cb04a4SMark Johnston "struct ip *", "struct ip6_hdr *"); 21798491bfaSMark Johnston 21898491bfaSMark Johnstonint 21998491bfaSMark Johnstonip_input(struct mbuf *m) 22098491bfaSMark Johnston{ 22198491bfaSMark Johnston struct ip *ip; 22298491bfaSMark Johnston ... 22398491bfaSMark Johnston ip = mtod(m, struct ip *); 224c6cb04a4SMark Johnston SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, ip, NULL); 22598491bfaSMark Johnston ... 22698491bfaSMark Johnston} 22798491bfaSMark Johnston 22898491bfaSMark Johnstonint 22998491bfaSMark Johnstonip6_input(struct mbuf *m) 23098491bfaSMark Johnston{ 23198491bfaSMark Johnston struct ip6_hdr *ip6; 23298491bfaSMark Johnston ... 23398491bfaSMark Johnston ip6 = mtod(m, struct ip6_hdr *); 234c6cb04a4SMark Johnston SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, NULL, ip6); 23598491bfaSMark Johnston ... 23698491bfaSMark Johnston} 23798491bfaSMark Johnston 23898491bfaSMark Johnston.Ed 23998491bfaSMark JohnstonIn particular, the probe should fire when the kernel receives either an IPv4 24098491bfaSMark Johnstonpacket or an IPv6 packet. 241c6cb04a4SMark Johnston.Pp 242c6cb04a4SMark JohnstonConsider the ICMP probe discussed above. 243c6cb04a4SMark JohnstonWe note that its second argument is of type 244c6cb04a4SMark Johnston.Ar struct icmp , 245c6cb04a4SMark Johnstonwhich is a type defined in the FreeBSD kernel to represent the ICMP header of 246c6cb04a4SMark Johnstonan ICMP packet, defined in RFC 792. 247c6cb04a4SMark JohnstonLinux has a corresponding type, 248c6cb04a4SMark Johnston.Ar struct icmphdr , 249c6cb04a4SMark Johnstonfor the same purpose, but its field names differ from FreeBSD's 250c6cb04a4SMark Johnston.Ar struct icmp . 251c6cb04a4SMark JohnstonSimilarly, illumos defines the 252c6cb04a4SMark Johnston.Ar icmph_t 253c6cb04a4SMark Johnstontype, again with different field names. 254c6cb04a4SMark JohnstonEven with the 255c6cb04a4SMark Johnston.Ql icmp:::pkt-receive 256c6cb04a4SMark Johnstonprobes defined in all three operating systems, 257c6cb04a4SMark Johnstonone would still have to write OS-specific scripts to extract a given field out 258c6cb04a4SMark Johnstonof the ICMP header argument. 259c6cb04a4SMark JohnstonDynamically-translated types solve this problem: one can define an 260c6cb04a4SMark JohnstonOS-independent 261c6cb04a4SMark Johnston.Xr c 7 262c6cb04a4SMark Johnstonstruct to represent an ICMP header, say 263c6cb04a4SMark Johnston.Ar struct icmp_hdr_dt , 264c6cb04a4SMark Johnstonand define translators from each of the three OS-specific types to 265c6cb04a4SMark Johnston.Ar struct icmp_hdr_dt , 266c6cb04a4SMark Johnstonall in the 267c6cb04a4SMark Johnston.Xr dtrace 1 268c6cb04a4SMark Johnstonlibrary path. 269c6cb04a4SMark JohnstonThen the FreeBSD probe above can be defined with: 270c6cb04a4SMark Johnston.Bd -literal -offset indent 271c6cb04a4SMark JohnstonSDT_PROBE_DEFINE1_XLATE(ip, , , receive, receive, "struct icmp *", 272c6cb04a4SMark Johnston "struct icmp_hdr_dt *"); 273c6cb04a4SMark Johnston.Ed 27498491bfaSMark Johnston.Sh SEE ALSO 27598491bfaSMark Johnston.Xr dtrace 1 27698491bfaSMark Johnston.Sh AUTHORS 27798491bfaSMark Johnston.An -nosplit 27898491bfaSMark JohnstonDTrace and the 27998491bfaSMark Johnston.Nm 28098491bfaSMark Johnstonframework were originally ported to FreeBSD from Solaris by 28198491bfaSMark Johnston.An John Birrell Aq jb@FreeBSD.org . 28298491bfaSMark JohnstonThis manual page was written by 28398491bfaSMark Johnston.An Mark Johnston Aq markj@FreeBSD.org . 28498491bfaSMark Johnston.Sh BUGS 28598491bfaSMark JohnstonThe 28698491bfaSMark Johnston.Nm 28798491bfaSMark Johnstonmacros allow the module name of a probe to be specified as part of a probe 28898491bfaSMark Johnstondefinition. 28998491bfaSMark JohnstonHowever, the DTrace framework uses the module name of probes to determine 29098491bfaSMark Johnstonwhich probes should be destroyed when a kernel module is unloaded, so the module 29198491bfaSMark Johnstonname of a probe should match the name of the module in which its defined. 29298491bfaSMark Johnston.Nm 29398491bfaSMark Johnstonwill set the module name properly if it is left unspecified in the probe 29498491bfaSMark Johnstondefinition; see the 29598491bfaSMark Johnston.Sx EXAMPLES 29698491bfaSMark Johnstonsection. 29798491bfaSMark Johnston.Pp 29898491bfaSMark JohnstonOne of the goals of the original 29998491bfaSMark Johnston.Nm 30098491bfaSMark Johnstonimplementation (and by extension, of FreeBSD's port) is that inactive 30198491bfaSMark Johnston.Nm 30298491bfaSMark Johnstonprobes should have no performance impact. 30398491bfaSMark JohnstonThis is unfortunately not the case; 30498491bfaSMark Johnston.Nm 30598491bfaSMark Johnstontrace points will add a small but non-zero amount of latency to the code 30698491bfaSMark Johnstonin which they are defined. 30798491bfaSMark JohnstonA more sophisticated implementation of the probes will help alleviate this 30898491bfaSMark Johnstonproblem. 309