Add lttng_ust_ prefix before objd_unref
[lttng-ust.git] / include / ust / tracepoint.h
CommitLineData
27b052e3
PMF
1#ifndef _UST_TRACEPOINT_H
2#define _UST_TRACEPOINT_H
f99be407
PMF
3
4/*
e9090899 5 * Copyright (C) 2008-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1f8b0dff 6 * Copyright (C) 2009 Pierre-Marc Fournier
22d72948 7 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
f99be407 8 *
8fc2d8db
PMF
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
f37142a4
MD
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License.
f99be407 13 *
8fc2d8db 14 * This library is distributed in the hope that it will be useful,
1f8b0dff 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8fc2d8db
PMF
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
1f8b0dff 18 *
8fc2d8db
PMF
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f99be407
PMF
22 *
23 * Heavily inspired from the Linux Kernel Markers.
24 *
1f8b0dff 25 * Ported to userspace by Pierre-Marc Fournier.
f99be407
PMF
26 */
27
b7ea1a1c 28#include <urcu-bp.h>
b0c4126f 29#include <urcu/list.h>
f99be407 30
1ea2c2df
MD
31#ifdef __cplusplus
32extern "C" {
33#endif
34
b979b346 35struct tracepoint_probe {
9dec086e
NC
36 void *func;
37 void *data;
38};
39
f99be407
PMF
40struct tracepoint {
41 const char *name; /* Tracepoint name */
f36c12ab 42 char state; /* State. */
b979b346 43 struct tracepoint_probe *probes;
f218ff28 44};
f99be407 45
cc7b66ba
MD
46/*
47 * Tracepoints should be added to the instrumented code using the
48 * "tracepoint()" macro.
49 */
50#define tracepoint(name, args...) __trace_##name(args)
51
f99be407
PMF
52/*
53 * it_func[0] is never NULL because there is at least one element in the array
54 * when the array itself is non NULL.
55 */
56#define __DO_TRACE(tp, proto, args) \
57 do { \
b979b346
MD
58 struct tracepoint_probe *__tp_it_probe_ptr; \
59 void *__tp_it_func; \
60 void *__tp_cb_data; \
f99be407 61 \
9dec086e 62 rcu_read_lock(); \
b979b346
MD
63 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
64 if (__tp_it_probe_ptr) { \
f99be407 65 do { \
b979b346
MD
66 __tp_it_func = __tp_it_probe_ptr->func; \
67 __tp_cb_data = __tp_it_probe_ptr->data; \
1ea2c2df 68 URCU_FORCE_CAST(void(*)(proto), __tp_it_func)(args); \
b979b346 69 } while ((++__tp_it_probe_ptr)->func); \
f99be407 70 } \
9dec086e 71 rcu_read_unlock(); \
f99be407
PMF
72 } while (0)
73
81614639
MD
74#define TP_PARAMS(args...) args
75#define TP_PROTO(args...) args
76#define TP_ARGS(args...) args
77
f36c12ab 78#define __CHECK_TRACE(name, proto, args) \
f99be407 79 do { \
b5a3dfa5 80 if (caa_unlikely(__tracepoint_##name.state)) \
f36c12ab
MD
81 __DO_TRACE(&__tracepoint_##name, \
82 TP_PROTO(proto), TP_ARGS(args)); \
f99be407
PMF
83 } while (0)
84
85/*
86 * Make sure the alignment of the structure in the __tracepoints section will
87 * not add unwanted padding between the beginning of the section and the
88 * structure. Force alignment to the same alignment as the section start.
f99be407 89 */
8d71973c 90#define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
f99be407 91 extern struct tracepoint __tracepoint_##name; \
cc7b66ba 92 static inline void __trace_##name(proto) \
f99be407 93 { \
f36c12ab 94 __CHECK_TRACE(name, TP_PROTO(data_proto), \
9dec086e 95 TP_ARGS(data_args)); \
f99be407 96 } \
9dec086e 97 static inline int \
cc7b66ba 98 __register_trace_##name(void (*probe)(data_proto), void *data) \
f99be407 99 { \
81614639 100 return __tracepoint_probe_register(#name, (void *)probe,\
9dec086e 101 data); \
f99be407 102 } \
9dec086e 103 static inline int \
cc7b66ba 104 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
f99be407 105 { \
81614639 106 return __tracepoint_probe_unregister(#name, (void *)probe, \
9dec086e 107 data); \
f99be407
PMF
108 }
109
332b3a18 110/*
81614639 111 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
377d33ed 112 * (void). "void" is a special value in a function prototype and can
8d71973c 113 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
377d33ed
MD
114 * macro adds a data element at the beginning of the prototype,
115 * we need a way to differentiate "(void *data, proto)" from
116 * "(void *data, void)". The second prototype is invalid.
117 *
8d71973c 118 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
b979b346 119 * and "void *__tp_cb_data" as the callback prototype.
377d33ed 120 *
8d71973c 121 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
b979b346 122 * "void *__tp_cb_data, proto" as the callback prototype.
377d33ed 123 */
81614639
MD
124#define _DECLARE_TRACEPOINT_NOARGS(name) \
125 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
377d33ed 126
81614639
MD
127#define _DECLARE_TRACEPOINT(name, proto, args) \
128 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \
129 TP_PARAMS(void *__tp_cb_data, proto), \
130 TP_PARAMS(__tp_cb_data, args))
9dec086e 131
f99be407 132/*
81614639
MD
133 * __tracepoints_ptrs section is not const (read-only) to let the linker update
134 * the pointer, allowing PIC code.
f99be407 135 */
81614639
MD
136#define _DEFINE_TRACEPOINT(name) \
137 static const char __tpstrtab_##name[] \
138 __attribute__((section("__tracepoints_strings"))) = #name; \
139 struct tracepoint __tracepoint_##name \
140 __attribute__((section("__tracepoints"))) = \
141 { __tpstrtab_##name, 0, NULL }; \
142 static struct tracepoint * __tracepoint_ptr_##name \
143 __attribute__((used, section("__tracepoints_ptrs"))) = \
144 &__tracepoint_##name;
f99be407 145
f99be407 146
81614639
MD
147#define __register_tracepoint(name, probe, data) \
148 __register_trace_##name(probe, data)
149#define __unregister_tracepoint(name, probe, data) \
150 __unregister_trace_##name(probe, data)
f99be407 151
81614639
MD
152/*
153 * Connect a probe to a tracepoint.
154 * Internal API.
155 */
156extern
157int __tracepoint_probe_register(const char *name, void *probe, void *data);
f99be407
PMF
158
159/*
81614639
MD
160 * Disconnect a probe from a tracepoint.
161 * Internal API.
f99be407 162 */
81614639
MD
163extern
164int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
f99be407 165
81614639
MD
166extern
167int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
168 int tracepoints_count);
169extern
170int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
872037bb 171
6cdf29f8
MD
172/*
173 * These weak symbols, the constructor, and destructor take care of
174 * registering only _one_ instance of the tracepoints per shared-ojbect
175 * (or for the whole main program).
6cdf29f8
MD
176 */
177extern struct tracepoint * const __start___tracepoints_ptrs[]
178 __attribute__((weak, visibility("hidden")));
179extern struct tracepoint * const __stop___tracepoints_ptrs[]
180 __attribute__((weak, visibility("hidden")));
181int __tracepoint_registered
182 __attribute__((weak, visibility("hidden")));
183
184static void __attribute__((constructor)) __tracepoints__init(void)
185{
186 if (__tracepoint_registered++)
187 return;
188 tracepoint_register_lib(__start___tracepoints_ptrs,
189 __stop___tracepoints_ptrs -
190 __start___tracepoints_ptrs);
191}
192
193static void __attribute__((destructor)) __tracepoints__destroy(void)
194{
195 if (--__tracepoint_registered)
196 return;
197 tracepoint_unregister_lib(__start___tracepoints_ptrs);
198}
22d72948 199
8d71973c 200#ifndef TRACEPOINT_EVENT
22d72948 201/*
d75775a6
MD
202 * Usage of the TRACEPOINT_EVENT macro:
203 *
204 * In short, an example:
205 *
d5001b43 206 * TRACEPOINT_EVENT(< [com_company_]project_[component_]event >,
d75775a6
MD
207 * TP_PROTO(int arg0, void *arg1, char *string, size_t strlen,
208 * long *arg4, size_t arg4_len),
209 * TP_ARGS(arg0, arg1, string, strlen, arg4, arg4_len),
210 * TP_FIELDS(
211 *
212 * * Integer, printed in base 10 *
213 * ctf_integer(int, field_a, arg0)
214 *
215 * * Integer, printed with 0x base 16 *
216 * ctf_integer_hex(unsigned long, field_d, arg1)
217 *
218 * * Array Sequence, printed as UTF8-encoded array of bytes *
219 * ctf_array_text(char, field_b, string, FIXED_LEN)
220 * ctf_sequence_text(char, field_c, string, size_t, strlen)
221 *
222 * * String, printed as UTF8-encoded string *
223 * ctf_string(field_e, string)
224 *
225 * * Array sequence of signed integer values *
226 * ctf_array(long, field_f, arg4, FIXED_LEN4)
227 * ctf_sequence(long, field_g, arg4, size_t, arg4_len)
228 * )
229 * )
230 *
231 * In more detail:
22d72948 232 *
0a42beb6
MD
233 * We define a tracepoint, its arguments, and its 'fast binary record'
234 * layout.
22d72948 235 *
0a42beb6 236 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name,
22d72948 237 *
abcdecad 238 * The name should be a proper C99 identifier.
0a42beb6
MD
239 * The "name" MUST follow these rules to ensure no namespace clash
240 * occurs:
22d72948 241 *
0a42beb6
MD
242 * For projects (applications and libraries) for which an entity
243 * specific to the project controls the source code and thus its
244 * tracepoints (typically with a scope larger than a single company):
22d72948 245 *
0a42beb6
MD
246 * either:
247 * project_component_event
248 * or:
249 * project_event
250 *
251 * Where "project" is the name of the project,
d5001b43
MD
252 * "component" is the name of the project component (which may
253 * include several levels of sub-components, e.g.
254 * ...component_subcomponent_...) where the tracepoint is located
255 * (optional),
0a42beb6
MD
256 * "event" is the name of the tracepoint event.
257 *
258 * For projects issued from a single company wishing to advertise that
259 * the company controls the source code and thus the tracepoints, the
260 * "com_" prefix should be used:
261 *
262 * either:
263 * com_company_project_component_event
264 * or:
265 * com_company_project_event
266 *
267 * Where "company" is the name of the company,
268 * "project" is the name of the project,
d5001b43
MD
269 * "component" is the name of the project component (which may
270 * include several levels of sub-components, e.g.
271 * ...component_subcomponent_...) where the tracepoint is located
272 * (optional),
0a42beb6
MD
273 * "event" is the name of the tracepoint event.
274 *
275 *
276 * As an example, let's consider a user-space application "someproject"
277 * that would have an internal thread scheduler:
278 *
279 * TRACEPOINT_EVENT(someproject_sched_switch,
22d72948
NC
280 *
281 * *
282 * * A function has a regular function arguments
283 * * prototype, declare it via TP_PROTO():
284 * *
285 *
286 * TP_PROTO(struct rq *rq, struct task_struct *prev,
287 * struct task_struct *next),
288 *
289 * *
290 * * Define the call signature of the 'function'.
291 * * (Design sidenote: we use this instead of a
292 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
293 * *
294 *
295 * TP_ARGS(rq, prev, next),
296 *
297 * *
298 * * Fast binary tracing: define the trace record via
81614639 299 * * TP_FIELDS(). You can think about it like a
22d72948
NC
300 * * regular C structure local variable definition.
301 * *
302 * * This is how the trace record is structured and will
303 * * be saved into the ring buffer. These are the fields
304 * * that will be exposed to readers.
305 * *
0a42beb6 306 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
81614639 307 * * to a standard declaraton:
22d72948 308 * *
81614639 309 * * pid_t prev_pid;
22d72948 310 * *
81614639 311 * * followed by an assignment:
22d72948 312 * *
81614639 313 * * prev_pid = prev->pid;
22d72948 314 * *
0a42beb6 315 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
81614639 316 * * equivalent to:
22d72948 317 * *
81614639 318 * * char prev_comm[TASK_COMM_LEN];
22d72948 319 * *
81614639 320 * * followed by an assignment:
22d72948 321 * *
81614639 322 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
22d72948
NC
323 * *
324 *
81614639 325 * TP_FIELDS(
0a42beb6
MD
326 * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN)
327 * ctf_integer(pid_t, prev_pid, prev->pid)
328 * ctf_integer(int, prev_prio, prev->prio)
329 * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN)
330 * ctf_integer(pid_t, next_pid, next->pid)
331 * ctf_integer(int, next_prio, next->prio)
22d72948 332 * )
0a42beb6 333 * )
abcdecad
MD
334 *
335 * Do _NOT_ add comma (,) nor semicolon (;) after the TRACEPOINT_EVENT
336 * declaration.
337 *
338 * The TRACEPOINT_SYSTEM must be defined when declaring a
339 * TRACEPOINT_EVENT. See ust/tracepoint-event.h for information about
340 * usage of other macros controlling TRACEPOINT_EVENT.
22d72948
NC
341 */
342
81614639
MD
343#define TRACEPOINT_EVENT(name, proto, args, fields) \
344 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 345
81614639
MD
346#define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
347#define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
348 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 349
81614639
MD
350/*
351 * Declaration of tracepoints that take 0 argument.
352 */
353#define TRACEPOINT_EVENT_NOARGS(name, fields) \
354 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 355
81614639
MD
356#define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
357#define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
358 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 359
81614639 360#endif /* #ifndef TRACEPOINT_EVENT */
22d72948 361
abcdecad
MD
362#ifndef TRACEPOINT_LOGLEVEL
363
364/*
365 * Tracepoint Loglevel Declaration Facility
366 *
367 * This is a place-holder the tracepoint loglevel declaration,
368 * overridden by the tracer implementation.
369 *
370 * Typical use of these loglevels:
371 *
372 * 1) Declare the mapping between loglevel names and an integer values
373 * within TRACEPOINT_LOGLEVEL_ENUM, using TP_LOGLEVEL for each tuple.
374 * Do _NOT_ add comma (,) nor semicolon (;) between the
375 * TRACEPOINT_LOGLEVEL_ENUM entries. Do _NOT_ add comma (,) nor
376 * semicolon (;) after the TRACEPOINT_LOGLEVEL_ENUM declaration. The
377 * name should be a proper C99 identifier.
378 *
379 * TRACEPOINT_LOGLEVEL_ENUM(
380 * TP_LOGLEVEL( < loglevel_name >, < value > )
381 * TP_LOGLEVEL( < loglevel_name >, < value > )
382 * ...
383 * )
384 *
385 * e.g.:
386 *
387 * TRACEPOINT_LOGLEVEL_ENUM(
388 * TP_LOGLEVEL(LOG_EMERG, 0)
389 * TP_LOGLEVEL(LOG_ALERT, 1)
390 * TP_LOGLEVEL(LOG_CRIT, 2)
391 * TP_LOGLEVEL(LOG_ERR, 3)
392 * TP_LOGLEVEL(LOG_WARNING, 4)
393 * TP_LOGLEVEL(LOG_NOTICE, 5)
394 * TP_LOGLEVEL(LOG_INFO, 6)
395 * TP_LOGLEVEL(LOG_DEBUG, 7)
396 * )
397 *
e9f37b4c
MD
398 * 2) Then, declare tracepoint loglevels for tracepoints. A
399 * TRACEPOINT_EVENT should be declared prior to the the
400 * TRACEPOINT_LOGLEVEL for a given tracepoint name. The first field
401 * is the name of the tracepoint, the second field is the loglevel
402 * name.
abcdecad
MD
403 *
404 * TRACEPOINT_LOGLEVEL(< [com_company_]project_[component_]event >,
405 * < loglevel_name >)
406 *
407 * The TRACEPOINT_SYSTEM must be defined when declaring a
408 * TRACEPOINT_LOGLEVEL_ENUM and TRACEPOINT_LOGLEVEL. The tracepoint
409 * loglevel enumeration apply to the entire TRACEPOINT_SYSTEM. Only one
410 * tracepoint loglevel enumeration should be declared per tracepoint
411 * system.
412 */
413
414#define TRACEPOINT_LOGLEVEL_ENUM()
415#define TRACEPOINT_LOGLEVEL(name, loglevel)
416
417#endif /* #ifndef TRACEPOINT_LOGLEVEL */
418
1ea2c2df
MD
419#ifdef __cplusplus
420}
421#endif
422
27b052e3 423#endif /* _UST_TRACEPOINT_H */
This page took 0.047737 seconds and 4 git commands to generate.