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.\" 27*c6cb04a4SMark 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 3898491bfaSMark Johnston.Fn SDT_PROBE_DEFINE prov mod func name sname 3998491bfaSMark Johnston.Fn SDT_PROBE_DEFINE0 prov mod func name sname 4098491bfaSMark Johnston.Fn SDT_PROBE_DEFINE1 prov mod func name sname arg0 4198491bfaSMark Johnston.Fn SDT_PROBE_DEFINE2 prov mod func name sname arg0 arg1 4298491bfaSMark Johnston.Fn SDT_PROBE_DEFINE3 prov mod func name sname arg0 arg1 arg2 4398491bfaSMark Johnston.Fn SDT_PROBE_DEFINE4 prov mod func name sname arg0 arg1 arg2 arg3 4498491bfaSMark Johnston.Fn SDT_PROBE_DEFINE5 prov mod func name sname arg0 arg1 arg2 arg3 arg4 4598491bfaSMark Johnston.Fn SDT_PROBE_DEFINE6 prov mod func name sname arg0 arg1 arg2 arg3 arg4 arg5 4698491bfaSMark Johnston.Fn SDT_PROBE_DEFINE7 prov mod func name sname arg0 arg1 arg2 arg3 arg4 arg5 \ 4798491bfaSMark Johnston arg6 48*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE0_XLATE prov mod func name sname 49*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE1_XLATE prov mod func name sname arg0 xarg0 50*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE2_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 51*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE3_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \ 52*c6cb04a4SMark Johnston arg2 xarg2 53*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE4_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \ 54*c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 55*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE5_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \ 56*c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 arg4 xarg4 57*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE6_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \ 58*c6cb04a4SMark Johnston arg2 xarg2 arg3 xarg3 arg4 xarg4 arg5 xarg5 59*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE7_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \ 60*c6cb04a4SMark 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. 13998491bfaSMark JohnstonThe 14098491bfaSMark Johnston.Fn SDT_PROBE_DEFINE* 14198491bfaSMark Johnstonmacros also take an extra 14298491bfaSMark Johnston.Ar sname 14398491bfaSMark Johnstonparameter. 14498491bfaSMark JohnstonThis is used to allow the creation of probes with names containing the 14598491bfaSMark Johnston.Ql - 14698491bfaSMark Johnstoncharacter. 14798491bfaSMark JohnstonSpecifically, the 14898491bfaSMark Johnston.Ar name 14998491bfaSMark Johnstonargument should contain the probe name with all dashes converted to underscores, 15098491bfaSMark Johnstonand the 15198491bfaSMark Johnston.Ar sname 15298491bfaSMark Johnstonargument should be the probe name as it will be referenced by D scripts. 15398491bfaSMark Johnston.Pp 15498491bfaSMark JohnstonThe 15598491bfaSMark Johnston.Fn SDT_PROBE_DEFINE* 15698491bfaSMark Johnstonmacros also allow programmers to declare the types of the arguments that are 15798491bfaSMark Johnstonpassed to probes. 15898491bfaSMark JohnstonThis is optional; if the argument types are omitted (through use of the 15998491bfaSMark Johnston.Fn SDT_PROBE_DEFINE 16098491bfaSMark Johnstonmacro), users wishing to make use of the arguments will have to manually cast 16198491bfaSMark Johnstonthem to the correct types in their D scripts. 16298491bfaSMark JohnstonIt is strongly recommended that probe definitions include a declaration of their 16398491bfaSMark Johnstonargument types. 16498491bfaSMark Johnston.Pp 16598491bfaSMark JohnstonThe 166*c6cb04a4SMark Johnston.Fn SDT_PROBE_DEFINE*_XLATE 167*c6cb04a4SMark Johnstonmacros are used for probes whose argument types are to be dynamically translated 168*c6cb04a4SMark Johnstonto the types specified by the corresponding 169*c6cb04a4SMark Johnston.Ar xarg 170*c6cb04a4SMark Johnstonarguments. 171*c6cb04a4SMark JohnstonThis is mainly useful when porting probe definitions from other operating 172*c6cb04a4SMark Johnstonsystems. 173*c6cb04a4SMark JohnstonAs seen by 174*c6cb04a4SMark Johnston.Xr dtrace 1 , 175*c6cb04a4SMark Johnstonthe arguments of a probe defined using these macros will have types which match 176*c6cb04a4SMark Johnstonthe 177*c6cb04a4SMark Johnston.Ar xarg 178*c6cb04a4SMark Johnstontypes in the probe definition. 179*c6cb04a4SMark JohnstonHowever, the arguments passed in at the trace point will have types matching the 180*c6cb04a4SMark Johnstonnative argument types in the probe definition, and thus the native type is 181*c6cb04a4SMark Johnstondynamically translated to the translated type. 182*c6cb04a4SMark JohnstonSo long as an appropriate translator is defined in 183*c6cb04a4SMark Johnston.Pa /usr/lib/dtrace , 184*c6cb04a4SMark Johnstonscripts making use of the probe need not concern themselves with the underlying 185*c6cb04a4SMark Johnstontype of a given 186*c6cb04a4SMark Johnston.Nm 187*c6cb04a4SMark Johnstonprobe argument. 188*c6cb04a4SMark Johnston.Pp 189*c6cb04a4SMark JohnstonThe 19098491bfaSMark Johnston.Fn SDT_PROBE* 19198491bfaSMark Johnstonmacros are used to create 19298491bfaSMark Johnston.Nm 19398491bfaSMark Johnstontrace points. 19498491bfaSMark JohnstonThey are meant to be added to executable code and can be used to instrument the 19598491bfaSMark Johnstoncode in which they are called. 19698491bfaSMark Johnston.Sh EXAMPLES 19798491bfaSMark JohnstonThe following probe definition will create a DTrace probe called 19898491bfaSMark Johnston.Ql icmp::unreach:pkt-receive , 19998491bfaSMark Johnstonwhich would hypothetically be triggered when the kernel receives an ICMP packet 20098491bfaSMark Johnstonof type Destination Unreachable: 20198491bfaSMark Johnston.Bd -literal -offset indent 20298491bfaSMark JohnstonSDT_PROVIDER_DECLARE(icmp); 20398491bfaSMark Johnston 204*c6cb04a4SMark JohnstonSDT_PROBE_DEFINE1(icmp, , unreach, pkt_receive, pkt-receive, 205*c6cb04a4SMark Johnston "struct icmp *"); 20698491bfaSMark Johnston 20798491bfaSMark Johnston.Ed 208*c6cb04a4SMark JohnstonThis particular probe would take a single argument: a pointer to the struct 209*c6cb04a4SMark Johnstoncontaining the ICMP header for the packet. 21098491bfaSMark JohnstonNote that the module name of this probe is not specified. 21198491bfaSMark Johnston.Pp 21298491bfaSMark JohnstonConsider a DTrace probe which fires when the network stack receives an IP 21398491bfaSMark Johnstonpacket. 21498491bfaSMark JohnstonSuch a probe would be defined by multiple tracepoints: 21598491bfaSMark Johnston.Bd -literal -offset indent 216*c6cb04a4SMark JohnstonSDT_PROBE_DEFINE3(ip, , , receive, receive, "struct ifnet *", 217*c6cb04a4SMark Johnston "struct ip *", "struct ip6_hdr *"); 21898491bfaSMark Johnston 21998491bfaSMark Johnstonint 22098491bfaSMark Johnstonip_input(struct mbuf *m) 22198491bfaSMark Johnston{ 22298491bfaSMark Johnston struct ip *ip; 22398491bfaSMark Johnston ... 22498491bfaSMark Johnston ip = mtod(m, struct ip *); 225*c6cb04a4SMark Johnston SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, ip, NULL); 22698491bfaSMark Johnston ... 22798491bfaSMark Johnston} 22898491bfaSMark Johnston 22998491bfaSMark Johnstonint 23098491bfaSMark Johnstonip6_input(struct mbuf *m) 23198491bfaSMark Johnston{ 23298491bfaSMark Johnston struct ip6_hdr *ip6; 23398491bfaSMark Johnston ... 23498491bfaSMark Johnston ip6 = mtod(m, struct ip6_hdr *); 235*c6cb04a4SMark Johnston SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, NULL, ip6); 23698491bfaSMark Johnston ... 23798491bfaSMark Johnston} 23898491bfaSMark Johnston 23998491bfaSMark Johnston.Ed 24098491bfaSMark JohnstonIn particular, the probe should fire when the kernel receives either an IPv4 24198491bfaSMark Johnstonpacket or an IPv6 packet. 242*c6cb04a4SMark Johnston.Pp 243*c6cb04a4SMark JohnstonConsider the ICMP probe discussed above. 244*c6cb04a4SMark JohnstonWe note that its second argument is of type 245*c6cb04a4SMark Johnston.Ar struct icmp , 246*c6cb04a4SMark Johnstonwhich is a type defined in the FreeBSD kernel to represent the ICMP header of 247*c6cb04a4SMark Johnstonan ICMP packet, defined in RFC 792. 248*c6cb04a4SMark JohnstonLinux has a corresponding type, 249*c6cb04a4SMark Johnston.Ar struct icmphdr , 250*c6cb04a4SMark Johnstonfor the same purpose, but its field names differ from FreeBSD's 251*c6cb04a4SMark Johnston.Ar struct icmp . 252*c6cb04a4SMark JohnstonSimilarly, illumos defines the 253*c6cb04a4SMark Johnston.Ar icmph_t 254*c6cb04a4SMark Johnstontype, again with different field names. 255*c6cb04a4SMark JohnstonEven with the 256*c6cb04a4SMark Johnston.Ql icmp:::pkt-receive 257*c6cb04a4SMark Johnstonprobes defined in all three operating systems, 258*c6cb04a4SMark Johnstonone would still have to write OS-specific scripts to extract a given field out 259*c6cb04a4SMark Johnstonof the ICMP header argument. 260*c6cb04a4SMark JohnstonDynamically-translated types solve this problem: one can define an 261*c6cb04a4SMark JohnstonOS-independent 262*c6cb04a4SMark Johnston.Xr c 7 263*c6cb04a4SMark Johnstonstruct to represent an ICMP header, say 264*c6cb04a4SMark Johnston.Ar struct icmp_hdr_dt , 265*c6cb04a4SMark Johnstonand define translators from each of the three OS-specific types to 266*c6cb04a4SMark Johnston.Ar struct icmp_hdr_dt , 267*c6cb04a4SMark Johnstonall in the 268*c6cb04a4SMark Johnston.Xr dtrace 1 269*c6cb04a4SMark Johnstonlibrary path. 270*c6cb04a4SMark JohnstonThen the FreeBSD probe above can be defined with: 271*c6cb04a4SMark Johnston.Bd -literal -offset indent 272*c6cb04a4SMark JohnstonSDT_PROBE_DEFINE1_XLATE(ip, , , receive, receive, "struct icmp *", 273*c6cb04a4SMark Johnston "struct icmp_hdr_dt *"); 274*c6cb04a4SMark Johnston.Ed 27598491bfaSMark Johnston.Sh SEE ALSO 27698491bfaSMark Johnston.Xr dtrace 1 27798491bfaSMark Johnston.Sh AUTHORS 27898491bfaSMark Johnston.An -nosplit 27998491bfaSMark JohnstonDTrace and the 28098491bfaSMark Johnston.Nm 28198491bfaSMark Johnstonframework were originally ported to FreeBSD from Solaris by 28298491bfaSMark Johnston.An John Birrell Aq jb@FreeBSD.org . 28398491bfaSMark JohnstonThis manual page was written by 28498491bfaSMark Johnston.An Mark Johnston Aq markj@FreeBSD.org . 28598491bfaSMark Johnston.Sh BUGS 28698491bfaSMark JohnstonThe 28798491bfaSMark Johnston.Nm 28898491bfaSMark Johnstonmacros allow the module name of a probe to be specified as part of a probe 28998491bfaSMark Johnstondefinition. 29098491bfaSMark JohnstonHowever, the DTrace framework uses the module name of probes to determine 29198491bfaSMark Johnstonwhich probes should be destroyed when a kernel module is unloaded, so the module 29298491bfaSMark Johnstonname of a probe should match the name of the module in which its defined. 29398491bfaSMark Johnston.Nm 29498491bfaSMark Johnstonwill set the module name properly if it is left unspecified in the probe 29598491bfaSMark Johnstondefinition; see the 29698491bfaSMark Johnston.Sx EXAMPLES 29798491bfaSMark Johnstonsection. 29898491bfaSMark Johnston.Pp 29998491bfaSMark JohnstonOne of the goals of the original 30098491bfaSMark Johnston.Nm 30198491bfaSMark Johnstonimplementation (and by extension, of FreeBSD's port) is that inactive 30298491bfaSMark Johnston.Nm 30398491bfaSMark Johnstonprobes should have no performance impact. 30498491bfaSMark JohnstonThis is unfortunately not the case; 30598491bfaSMark Johnston.Nm 30698491bfaSMark Johnstontrace points will add a small but non-zero amount of latency to the code 30798491bfaSMark Johnstonin which they are defined. 30898491bfaSMark JohnstonA more sophisticated implementation of the probes will help alleviate this 30998491bfaSMark Johnstonproblem. 310