]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - share/man/man9/SDT.9
MFC r324538
[FreeBSD/stable/9.git] / share / man / man9 / SDT.9
1 .\" Copyright (c) 2013 Mark Johnston <markj@freebsd.org>
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD$
26 .\"
27 .Dd August 17, 2013
28 .Dt SDT 9
29 .Os
30 .Sh NAME
31 .Nm SDT
32 .Nd a DTrace framework for adding statically-defined tracing probes
33 .Sh SYNOPSIS
34 .In sys/sdt.h
35 .Fn SDT_PROVIDER_DECLARE prov
36 .Fn SDT_PROVIDER_DEFINE prov
37 .Fn SDT_PROBE_DECLARE prov mod func name
38 .Fn SDT_PROBE_DEFINE prov mod func name sname
39 .Fn SDT_PROBE_DEFINE0 prov mod func name sname
40 .Fn SDT_PROBE_DEFINE1 prov mod func name sname arg0
41 .Fn SDT_PROBE_DEFINE2 prov mod func name sname arg0 arg1
42 .Fn SDT_PROBE_DEFINE3 prov mod func name sname arg0 arg1 arg2
43 .Fn SDT_PROBE_DEFINE4 prov mod func name sname arg0 arg1 arg2 arg3
44 .Fn SDT_PROBE_DEFINE5 prov mod func name sname arg0 arg1 arg2 arg3 arg4
45 .Fn SDT_PROBE_DEFINE6 prov mod func name sname arg0 arg1 arg2 arg3 arg4 arg5
46 .Fn SDT_PROBE_DEFINE7 prov mod func name sname arg0 arg1 arg2 arg3 arg4 arg5   \
47     arg6
48 .Fn SDT_PROBE_DEFINE0_XLATE prov mod func name sname
49 .Fn SDT_PROBE_DEFINE1_XLATE prov mod func name sname arg0 xarg0
50 .Fn SDT_PROBE_DEFINE2_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1
51 .Fn SDT_PROBE_DEFINE3_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \
52     arg2 xarg2
53 .Fn SDT_PROBE_DEFINE4_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \
54     arg2 xarg2 arg3 xarg3
55 .Fn SDT_PROBE_DEFINE5_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \
56     arg2 xarg2 arg3 xarg3 arg4 xarg4
57 .Fn SDT_PROBE_DEFINE6_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \
58     arg2 xarg2 arg3 xarg3 arg4 xarg4 arg5 xarg5
59 .Fn SDT_PROBE_DEFINE7_XLATE prov mod func name sname arg0 xarg0 arg1 xarg1 \
60     arg2 xarg2 arg3 xarg3 arg4 xarg4 arg5 xarg5 arg6 xarg6
61 .Fn SDT_PROBE0 prov mod func name
62 .Fn SDT_PROBE1 prov mod func name arg0
63 .Fn SDT_PROBE2 prov mod func name arg0 arg1
64 .Fn SDT_PROBE3 prov mod func name arg0 arg1 arg2
65 .Fn SDT_PROBE4 prov mod func name arg0 arg1 arg2 arg3
66 .Fn SDT_PROBE5 prov mod func name arg0 arg1 arg2 arg3 arg4
67 .Fn SDT_PROBE6 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5
68 .Fn SDT_PROBE7 prov mod func name arg0 arg1 arg2 arg3 arg4 arg5 arg6
69 .Sh DESCRIPTION
70 .Pp
71 The
72 .Nm
73 macros allow programmers to define static trace points in kernel code.
74 These trace points are used by the
75 .Nm
76 framework to create DTrace probes, allowing the code to be instrumented
77 using
78 .Xr dtrace 1 .
79 By default,
80 .Nm
81 trace points are disabled and have no effect on the surrounding code.
82 When a DTrace probe corresponding to a given trace point is enabled, threads
83 that execute the trace point will call a handler and cause the probe to fire.
84 Moreover, trace points can take arguments, making it possible to pass data
85 to the DTrace framework when an enabled probe fires.
86 .Pp
87 Multiple trace points may correspond to a single DTrace probe, allowing
88 programmers to create DTrace probes that correspond to logical system events
89 rather than tying probes to specific code execution paths.
90 For instance, a DTrace probe corresponding to the arrival of an IP packet into
91 the network stack may be defined using two
92 .Nm
93 trace points: one for IPv4 packets and one for IPv6 packets.
94 .Pp
95 In addition to defining DTrace probes, the
96 .Nm
97 macros allow programmers to define new DTrace providers, making it possible to
98 namespace logically-related probes.
99 An example is FreeBSD's sctp provider, which contains
100 .Nm
101 probes for FreeBSD's
102 .Xr sctp 4
103 implementation.
104 .Pp
105 The
106 .Fn SDT_PROVIDER_DECLARE
107 and
108 .Fn SDT_PROVIDER_DEFINE
109 macros are used respectively to declare and define a DTrace provider named
110 .Ar prov
111 with the
112 .Nm
113 framework.
114 A provider need only be defined once; however, the provider must be declared
115 before defining any
116 .Nm
117 probes belonging to that provider.
118 .Pp
119 Similarly, the
120 .Fn SDT_PROBE_DECLARE
121 and
122 .Fn SDT_PROBE_DEFINE*
123 macros are used to declare and define DTrace probes using the
124 .Nm
125 framework.
126 Once a probe has been defined, trace points for that probe may be added to
127 kernel code.
128 DTrace probe identifiers consist of a provider, module, function and name, all
129 of which may be specified in the
130 .Nm
131 probe definition.
132 Note that probes should not specify a module name: the module name of a probe is
133 used to determine whether or not it should be destroyed when a kernel module is
134 unloaded.
135 See the
136 .Sx BUGS
137 section.
138 Note in particular that probes must not be defined across multiple kernel
139 modules.
140 The
141 .Fn SDT_PROBE_DEFINE*
142 macros also take an extra
143 .Ar sname
144 parameter.
145 This is used to allow the creation of probes with names containing the
146 .Ql -
147 character.
148 Specifically, the
149 .Ar name
150 argument should contain the probe name with all dashes converted to underscores,
151 and the
152 .Ar sname
153 argument should be the probe name as it will be referenced by D scripts.
154 .Pp
155 The
156 .Fn SDT_PROBE_DEFINE*
157 macros also allow programmers to declare the types of the arguments that are
158 passed to probes.
159 This is optional; if the argument types are omitted (through use of the
160 .Fn SDT_PROBE_DEFINE
161 macro), users wishing to make use of the arguments will have to manually cast
162 them to the correct types in their D scripts.
163 It is strongly recommended that probe definitions include a declaration of their
164 argument types.
165 .Pp
166 The
167 .Fn SDT_PROBE_DEFINE*_XLATE
168 macros are used for probes whose argument types are to be dynamically translated
169 to the types specified by the corresponding
170 .Ar xarg
171 arguments.
172 This is mainly useful when porting probe definitions from other operating
173 systems.
174 As seen by
175 .Xr dtrace 1 ,
176 the arguments of a probe defined using these macros will have types which match
177 the
178 .Ar xarg
179 types in the probe definition.
180 However, the arguments passed in at the trace point will have types matching the
181 native argument types in the probe definition, and thus the native type is
182 dynamically translated to the translated type.
183 So long as an appropriate translator is defined in
184 .Pa /usr/lib/dtrace ,
185 scripts making use of the probe need not concern themselves with the underlying
186 type of a given
187 .Nm
188 probe argument.
189 .Pp
190 The
191 .Fn SDT_PROBE*
192 macros are used to create
193 .Nm
194 trace points.
195 They are meant to be added to executable code and can be used to instrument the
196 code in which they are called.
197 .Sh EXAMPLES
198 .Pp
199 The following probe definition will create a DTrace probe called
200 .Ql icmp::unreach:pkt-receive ,
201 which would hypothetically be triggered when the kernel receives an ICMP packet
202 of type Destination Unreachable:
203 .Bd -literal -offset indent
204 SDT_PROVIDER_DECLARE(icmp);
205
206 SDT_PROBE_DEFINE1(icmp, , unreach, pkt__receive,
207     "struct icmp *");
208
209 .Ed
210 This particular probe would take a single argument: a pointer to the struct
211 containing the ICMP header for the packet.
212 Note that the module name of this probe is not specified.
213 .Pp
214 Consider a DTrace probe which fires when the network stack receives an IP
215 packet.
216 Such a probe would be defined by multiple tracepoints:
217 .Bd -literal -offset indent
218 SDT_PROBE_DEFINE3(ip, , , receive, "struct ifnet *",
219     "struct ip *", "struct ip6_hdr *");
220
221 int
222 ip_input(struct mbuf *m)
223 {
224         struct ip *ip;
225         ...
226         ip = mtod(m, struct ip *);
227         SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, ip, NULL);
228         ...
229 }
230
231 int
232 ip6_input(struct mbuf *m)
233 {
234         struct ip6_hdr *ip6;
235         ...
236         ip6 = mtod(m, struct ip6_hdr *);
237         SDT_PROBE3(ip, , , receive, m->m_pkthdr.rcvif, NULL, ip6);
238         ...
239 }
240
241 .Ed
242 In particular, the probe should fire when the kernel receives either an IPv4
243 packet or an IPv6 packet.
244 .Pp
245 Consider the ICMP probe discussed above.
246 We note that its second argument is of type
247 .Ar struct icmp ,
248 which is a type defined in the FreeBSD kernel to represent the ICMP header of
249 an ICMP packet, defined in RFC 792.
250 Linux has a corresponding type,
251 .Ar struct icmphdr ,
252 for the same purpose, but its field names differ from FreeBSD's
253 .Ar struct icmp .
254 Similarly, illumos defines the
255 .Ar icmph_t
256 type, again with different field names.
257 Even with the
258 .Ql icmp:::pkt-receive
259 probes defined in all three operating systems,
260 one would still have to write OS-specific scripts to extract a given field out
261 of the ICMP header argument.
262 Dynamically-translated types solve this problem: one can define an
263 OS-independent
264 .Xr c 7
265 struct to represent an ICMP header, say
266 .Ar struct icmp_hdr_dt ,
267 and define translators from each of the three OS-specific types to
268 .Ar struct icmp_hdr_dt ,
269 all in the
270 .Xr dtrace 1
271 library path.
272 Then the FreeBSD probe above can be defined with:
273 .Bd -literal -offset indent
274 SDT_PROBE_DEFINE1_XLATE(ip, , , receive, "struct icmp *",
275     "struct icmp_hdr_dt *");
276 .Ed
277 .Sh SEE ALSO
278 .Xr dtrace 1
279 .Sh AUTHORS
280 .An -nosplit
281 DTrace and the
282 .Nm
283 framework were originally ported to FreeBSD from Solaris by
284 .An John Birrell Aq jb@FreeBSD.org .
285 This manual page was written by
286 .An Mark Johnston Aq markj@FreeBSD.org .
287 .Sh BUGS
288 .Pp
289 The
290 .Nm
291 macros allow the module name of a probe to be specified as part of a probe
292 definition.
293 However, the DTrace framework uses the module name of probes to determine
294 which probes should be destroyed when a kernel module is unloaded, so the module
295 name of a probe should match the name of the module in which its defined.
296 .Nm
297 will set the module name properly if it is left unspecified in the probe
298 definition; see the
299 .Sx EXAMPLES
300 section.
301 .Pp
302 One of the goals of the original
303 .Nm
304 implementation (and by extension, of FreeBSD's port) is that inactive
305 .Nm
306 probes should have no performance impact.
307 This is unfortunately not the case;
308 .Nm
309 trace points will add a small but non-zero amount of latency to the code
310 in which they are defined.
311 A more sophisticated implementation of the probes will help alleviate this
312 problem.