2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2008-2011 Mathieu Desnoyers
5 * Copyright (C) 2009 Pierre-Marc Fournier
7 * Ported to userspace by Pierre-Marc Fournier.
17 #include <urcu/arch.h>
18 #include <lttng/urcu/urcu-ust.h>
19 #include <urcu/hlist.h>
20 #include <urcu/uatomic.h>
21 #include <urcu/compiler.h>
22 #include <urcu/system.h>
24 #include <lttng/tracepoint.h>
25 #include <lttng/ust-abi.h> /* for LTTNG_UST_ABI_SYM_NAME_LEN */
26 #include <lttng/ust-common.h>
28 #include "common/logging.h"
29 #include "common/macros.h"
31 #include "lib/lttng-ust-tracepoint/tracepoint.h"
32 #include "common/tracepoint.h"
33 #include "common/jhash.h"
34 #include "common/err-ptr.h"
36 /* Test compiler support for weak symbols with hidden visibility. */
37 int lttng_ust_tracepoint_test_symbol1
__attribute__((weak
, visibility("hidden")));
38 void *lttng_ust_tracepoint_test_symbol2
__attribute__((weak
, visibility("hidden")));
41 } lttng_ust_tracepoint_test_symbol3
__attribute__((weak
, visibility("hidden")));
43 /* Set to 1 to enable tracepoint debug output */
44 static const int tracepoint_debug
;
45 static int initialized
;
48 * If tracepoint_destructors_state = 1, tracepoint destructors are
49 * enabled. They are disabled otherwise.
51 static int tracepoint_destructors_state
= 1;
53 static void (*new_tracepoint_cb
)(struct lttng_ust_tracepoint
*);
56 * tracepoint_mutex nests inside UST mutex.
58 * Note about interaction with fork/clone: UST does not hold the
59 * tracepoint mutex across fork/clone because it is either:
60 * - nested within UST mutex, in which case holding the UST mutex across
62 * - taken by a library constructor, which should never race with a
63 * fork/clone if the application is expected to continue running with
64 * the same memory layout (no following exec()).
66 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
69 * libraries that contain tracepoints (struct tracepoint_lib).
70 * Protected by tracepoint mutex.
72 static CDS_LIST_HEAD(libs
);
75 * The tracepoint mutex protects the library tracepoints, the hash table, and
77 * All calls to the tracepoint API must be protected by the tracepoint mutex,
78 * excepts calls to lttng_ust_tracepoint_module_register and
79 * lttng_ust_tracepoint_module_unregister, which take the tracepoint mutex themselves.
83 * Tracepoint hash table, containing the active tracepoints.
84 * Protected by tracepoint mutex.
86 #define TRACEPOINT_HASH_BITS 12
87 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
88 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
90 static CDS_LIST_HEAD(old_probes
);
91 static int need_update
;
93 static CDS_LIST_HEAD(release_queue
);
94 static int release_queue_need_update
;
98 * It is used to to delay the free of multiple probes array until a quiescent
100 * Tracepoint entries modifications are protected by the tracepoint mutex.
102 struct tracepoint_entry
{
103 struct cds_hlist_node hlist
;
104 struct lttng_ust_tracepoint_probe
*probes
;
105 int refcount
; /* Number of times armed. 0 if disarmed. */
106 int callsite_refcount
; /* how many libs use this tracepoint */
114 struct cds_list_head list
;
115 /* Field below only used for call_rcu scheme */
116 /* struct rcu_head head; */
118 struct lttng_ust_tracepoint_probe probes
[0];
122 * Callsite hash table, containing the tracepoint call sites.
123 * Protected by tracepoint mutex.
125 #define CALLSITE_HASH_BITS 12
126 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
127 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
129 struct callsite_entry
{
130 struct cds_hlist_node hlist
; /* hash table node */
131 struct cds_list_head node
; /* lib list of callsites node */
132 struct lttng_ust_tracepoint
*tp
;
133 bool tp_entry_callsite_ref
; /* Has a tp_entry took a ref on this callsite */
136 lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
== LTTNG_UST_ABI_SYM_NAME_LEN
,
137 "Tracepoint name max length mismatch between UST ABI and tracepoint API",
138 Tracepoint_name_max_length_mismatch
);
141 bool lttng_ust_tp_validate_event_name(const struct lttng_ust_tracepoint
*tp
)
143 if (strlen(tp
->provider_name
) + 1 +
144 strlen(tp
->event_name
) >= LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
)
149 /* coverity[+alloc] */
150 static void *allocate_probes(int count
)
152 struct tp_probes
*p
=
153 zmalloc(count
* sizeof(struct lttng_ust_tracepoint_probe
)
154 + sizeof(struct tp_probes
));
155 return p
== NULL
? NULL
: p
->probes
;
158 /* coverity[+free : arg-0] */
159 static void release_probes(void *old
)
162 struct tp_probes
*tp_probes
= caa_container_of(old
,
163 struct tp_probes
, probes
[0]);
164 lttng_ust_urcu_synchronize_rcu();
169 static void debug_print_probes(struct tracepoint_entry
*entry
)
173 if (!tracepoint_debug
|| !entry
->probes
)
176 for (i
= 0; entry
->probes
[i
].func
; i
++)
177 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
181 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
182 void (*probe
)(void), void *data
)
185 struct lttng_ust_tracepoint_probe
*old
, *new;
189 return ERR_PTR(-EINVAL
);
191 debug_print_probes(entry
);
194 /* (N -> N+1), (N != 0, 1) probes */
195 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
196 if (old
[nr_probes
].func
== probe
&&
197 old
[nr_probes
].data
== data
)
198 return ERR_PTR(-EEXIST
);
200 /* + 2 : one for new probe, one for NULL func */
201 new = allocate_probes(nr_probes
+ 2);
203 return ERR_PTR(-ENOMEM
);
206 nr_probes
* sizeof(struct lttng_ust_tracepoint_probe
));
207 new[nr_probes
].func
= probe
;
208 new[nr_probes
].data
= data
;
209 new[nr_probes
+ 1].func
= NULL
;
210 entry
->refcount
= nr_probes
+ 1;
212 debug_print_probes(entry
);
217 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
218 void (*probe
)(void), void *data
)
220 int nr_probes
= 0, nr_del
= 0, i
;
221 struct lttng_ust_tracepoint_probe
*old
, *new;
226 return ERR_PTR(-ENOENT
);
228 debug_print_probes(entry
);
229 /* (N -> M), (N > 1, M >= 0) probes */
231 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
232 if (old
[nr_probes
].func
== probe
&&
233 old
[nr_probes
].data
== data
)
238 if (nr_probes
- nr_del
== 0) {
239 /* N -> 0, (N > 1) */
240 entry
->probes
= NULL
;
242 debug_print_probes(entry
);
246 /* N -> M, (N > 1, M > 0) */
248 new = allocate_probes(nr_probes
- nr_del
+ 1);
250 return ERR_PTR(-ENOMEM
);
251 for (i
= 0; old
[i
].func
; i
++)
252 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
254 new[nr_probes
- nr_del
].func
= NULL
;
255 entry
->refcount
= nr_probes
- nr_del
;
258 debug_print_probes(entry
);
263 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
264 * Must be called with tracepoint mutex held.
265 * Returns NULL if not present.
267 static struct tracepoint_entry
*get_tracepoint(const char *provider_name
, const char *event_name
)
269 struct cds_hlist_head
*head
;
270 struct cds_hlist_node
*node
;
271 struct tracepoint_entry
*e
;
274 hash
= jhash(provider_name
, strlen(provider_name
), 0) ^
275 jhash(event_name
, strlen(event_name
), 0);
276 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
277 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
278 if (!strcmp(event_name
, e
->event_name
) && !strcmp(provider_name
, e
->provider_name
))
285 * Add the tracepoint to the tracepoint hash table. Must be called with
286 * tracepoint mutex held.
288 static struct tracepoint_entry
*add_tracepoint(const char *provider_name
, const char *event_name
,
289 const char *signature
)
291 struct cds_hlist_head
*head
;
292 struct cds_hlist_node
*node
;
293 struct tracepoint_entry
*e
;
294 size_t sig_len
= strlen(signature
);
295 size_t sig_off
, provider_name_off
, event_name_off
;
296 size_t provider_name_len
= strlen(provider_name
);
297 size_t event_name_len
= strlen(event_name
);
300 hash
= jhash(provider_name
, provider_name_len
, 0) ^
301 jhash(event_name
, event_name_len
, 0);
302 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
303 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
304 if (!strcmp(event_name
, e
->event_name
) && !strcmp(provider_name
, e
->provider_name
)) {
305 DBG("tracepoint \"%s:%s\" busy", provider_name
, event_name
);
306 return ERR_PTR(-EEXIST
); /* Already there */
311 * Using zmalloc here to allocate a variable length elements: name and
312 * signature. Could cause some memory fragmentation if overused.
314 provider_name_off
= sizeof(struct tracepoint_entry
);
315 event_name_off
= provider_name_off
+ provider_name_len
+ 1;
316 sig_off
= event_name_off
+ event_name_len
+ 1;
318 e
= zmalloc(sizeof(struct tracepoint_entry
) + provider_name_len
+ 1 +
319 event_name_len
+ 1 + sig_len
+ 1);
321 return ERR_PTR(-ENOMEM
);
322 e
->provider_name
= (char *) e
+ provider_name_off
;
323 memcpy(e
->provider_name
, provider_name
, provider_name_len
+ 1);
324 e
->provider_name
[provider_name_len
] = '\0';
326 e
->event_name
= (char *) e
+ event_name_off
;
327 memcpy(e
->event_name
, event_name
, event_name_len
+ 1);
328 e
->event_name
[event_name_len
] = '\0';
330 e
->signature
= (char *) e
+ sig_off
;
331 memcpy(e
->signature
, signature
, sig_len
+ 1);
332 e
->signature
[sig_len
] = '\0';
336 e
->callsite_refcount
= 0;
338 cds_hlist_add_head(&e
->hlist
, head
);
343 * Remove the tracepoint from the tracepoint hash table. Must be called with
344 * tracepoint mutex held.
346 static void remove_tracepoint(struct tracepoint_entry
*e
)
348 cds_hlist_del(&e
->hlist
);
353 * Sets the probe callback corresponding to one tracepoint.
355 static void set_tracepoint(struct tracepoint_entry
**entry
,
356 struct lttng_ust_tracepoint
*elem
, int active
)
358 WARN_ON(strcmp((*entry
)->provider_name
, elem
->provider_name
) != 0);
359 WARN_ON(strcmp((*entry
)->event_name
, elem
->event_name
) != 0);
361 * Check that signatures match before connecting a probe to a
362 * tracepoint. Warn the user if they don't.
364 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
365 static int warned
= 0;
367 /* Only print once, don't flood console. */
369 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
370 WARN("Tracepoint \"%s:%s\" signatures: call: \"%s\" vs probe: \"%s\".",
371 elem
->provider_name
, elem
->event_name
, elem
->signature
, (*entry
)->signature
);
374 /* Don't accept connecting non-matching signatures. */
379 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
380 * probe callbacks array is consistent before setting a pointer to it.
381 * This array is referenced by __DO_TRACE from
382 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
385 lttng_ust_rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
386 CMM_STORE_SHARED(elem
->state
, active
);
390 * Disable a tracepoint and its probe callback.
391 * Note: only waiting an RCU period after setting elem->call to the empty
392 * function insures that the original callback is not used anymore. This insured
393 * by preempt_disable around the call site.
395 static void disable_tracepoint(struct lttng_ust_tracepoint
*elem
)
397 CMM_STORE_SHARED(elem
->state
, 0);
398 lttng_ust_rcu_assign_pointer(elem
->probes
, NULL
);
402 * Add the callsite to the callsite hash table. Must be called with
403 * tracepoint mutex held.
405 static void add_callsite(struct tracepoint_lib
* lib
, struct lttng_ust_tracepoint
*tp
)
407 struct cds_hlist_head
*head
;
408 struct callsite_entry
*e
;
410 struct tracepoint_entry
*tp_entry
;
412 if (!lttng_ust_tp_validate_event_name(tp
)) {
413 WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars",
414 tp
->provider_name
, tp
->event_name
, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
- 1);
417 hash
= jhash(tp
->provider_name
, strlen(tp
->provider_name
), 0) ^
418 jhash(tp
->event_name
, strlen(tp
->event_name
), 0);
419 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
420 e
= zmalloc(sizeof(struct callsite_entry
));
422 PERROR("Unable to add callsite for tracepoint \"%s:%s\"", tp
->provider_name
, tp
->event_name
);
425 cds_hlist_add_head(&e
->hlist
, head
);
427 cds_list_add(&e
->node
, &lib
->callsites
);
429 tp_entry
= get_tracepoint(tp
->provider_name
, tp
->event_name
);
432 tp_entry
->callsite_refcount
++;
433 e
->tp_entry_callsite_ref
= true;
437 * Remove the callsite from the callsite hash table and from lib
438 * callsite list. Must be called with tracepoint mutex held.
440 static void remove_callsite(struct callsite_entry
*e
)
442 struct tracepoint_entry
*tp_entry
;
444 tp_entry
= get_tracepoint(e
->tp
->provider_name
, e
->tp
->event_name
);
446 if (e
->tp_entry_callsite_ref
)
447 tp_entry
->callsite_refcount
--;
448 if (tp_entry
->callsite_refcount
== 0)
449 disable_tracepoint(e
->tp
);
451 cds_hlist_del(&e
->hlist
);
452 cds_list_del(&e
->node
);
457 * Enable/disable all callsites based on the state of a specific
459 * Must be called with tracepoint mutex held.
461 static void tracepoint_sync_callsites(const char *provider_name
, const char *event_name
)
463 struct tracepoint_entry
*tp_entry
;
464 struct cds_hlist_head
*head
;
465 struct cds_hlist_node
*node
;
466 struct callsite_entry
*e
;
469 tp_entry
= get_tracepoint(provider_name
, event_name
);
470 hash
= jhash(provider_name
, strlen(provider_name
), 0) ^
471 jhash(event_name
, strlen(event_name
), 0);
472 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
473 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
474 struct lttng_ust_tracepoint
*tp
= e
->tp
;
476 if (strcmp(event_name
, tp
->event_name
))
478 if (strcmp(provider_name
, tp
->provider_name
))
481 if (!e
->tp_entry_callsite_ref
) {
482 tp_entry
->callsite_refcount
++;
483 e
->tp_entry_callsite_ref
= true;
485 set_tracepoint(&tp_entry
, tp
,
486 !!tp_entry
->refcount
);
488 disable_tracepoint(tp
);
489 e
->tp_entry_callsite_ref
= false;
495 * tracepoint_update_probe_range - Update a probe range
496 * @begin: beginning of the range
497 * @end: end of the range
499 * Updates the probe callback corresponding to a range of tracepoints.
502 void tracepoint_update_probe_range(struct lttng_ust_tracepoint
* const *begin
,
503 struct lttng_ust_tracepoint
* const *end
)
505 struct lttng_ust_tracepoint
* const *iter
;
506 struct tracepoint_entry
*mark_entry
;
508 for (iter
= begin
; iter
< end
; iter
++) {
510 continue; /* skip dummy */
511 if (!(*iter
)->provider_name
|| !(*iter
)->event_name
) {
512 disable_tracepoint(*iter
);
515 if (!lttng_ust_tp_validate_event_name(*iter
)) {
516 WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars",
517 (*iter
)->provider_name
, (*iter
)->event_name
, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
- 1);
518 disable_tracepoint(*iter
);
521 mark_entry
= get_tracepoint((*iter
)->provider_name
, (*iter
)->event_name
);
523 set_tracepoint(&mark_entry
, *iter
,
524 !!mark_entry
->refcount
);
526 disable_tracepoint(*iter
);
531 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
533 tracepoint_update_probe_range(lib
->tracepoints_start
,
534 lib
->tracepoints_start
+ lib
->tracepoints_count
);
537 static void lib_register_callsites(struct tracepoint_lib
*lib
)
539 struct lttng_ust_tracepoint
* const *begin
;
540 struct lttng_ust_tracepoint
* const *end
;
541 struct lttng_ust_tracepoint
* const *iter
;
543 begin
= lib
->tracepoints_start
;
544 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
546 for (iter
= begin
; iter
< end
; iter
++) {
548 continue; /* skip dummy */
549 if (!(*iter
)->provider_name
|| !(*iter
)->event_name
) {
552 add_callsite(lib
, *iter
);
556 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
558 struct callsite_entry
*callsite
, *tmp
;
560 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
561 remove_callsite(callsite
);
565 * Update probes, removing the faulty probes.
567 static void tracepoint_update_probes(void)
569 struct tracepoint_lib
*lib
;
571 /* tracepoints registered from libraries and executable. */
572 cds_list_for_each_entry(lib
, &libs
, list
)
573 lib_update_tracepoints(lib
);
576 static struct lttng_ust_tracepoint_probe
*
577 tracepoint_add_probe(const char *provider_name
, const char *event_name
,
578 void (*probe
)(void), void *data
, const char *signature
)
580 struct tracepoint_entry
*entry
;
581 struct lttng_ust_tracepoint_probe
*old
;
583 entry
= get_tracepoint(provider_name
, event_name
);
585 if (strcmp(entry
->signature
, signature
) != 0) {
586 ERR("Tracepoint \"%s:%s\": tracepoint and probe signature do not match: \"%s\" vs \"%s\".",
587 provider_name
, event_name
, entry
->signature
, signature
);
588 return ERR_PTR(-EINVAL
);
591 entry
= add_tracepoint(provider_name
, event_name
, signature
);
593 return (struct lttng_ust_tracepoint_probe
*)entry
;
595 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
596 if (IS_ERR(old
) && !entry
->refcount
)
597 remove_tracepoint(entry
);
601 static void tracepoint_release_queue_add_old_probes(void *old
)
603 release_queue_need_update
= 1;
605 struct tp_probes
*tp_probes
= caa_container_of(old
,
606 struct tp_probes
, probes
[0]);
607 cds_list_add(&tp_probes
->u
.list
, &release_queue
);
612 * Use a symbol of the previous ABI to detect if liblttng-ust-tracepoint.so.0
613 * is loaded in the current process.
615 #define LTTNG_UST_TRACEPOINT_SONAME_0_SYM "tracepoint_unregister_lib"
618 void lttng_ust_tracepoint_check_soname_0(void)
620 if (!dlsym(RTLD_DEFAULT
, LTTNG_UST_TRACEPOINT_SONAME_0_SYM
))
623 CRIT("Incompatible library ABIs detected within the same process. "
624 "The process is likely linked against different major soname of LTTng-UST which is unsupported. "
625 "The detection was triggered by lookup of ABI 0 symbol \"%s\" in the Global Symbol Table\n",
626 LTTNG_UST_TRACEPOINT_SONAME_0_SYM
);
630 * Expose a canary symbol of the previous ABI to ensure we catch uses of a
631 * liblttng-ust-tracepoint.so.0 dlopen'd after .so.1 has been loaded. Use a
632 * different symbol than the detection code to ensure we don't detect ourself.
634 int tracepoint_register_lib(void *arg0
__attribute__((unused
)), int arg1
__attribute__((unused
)));
635 int tracepoint_register_lib(void *arg0
__attribute__((unused
)), int arg1
__attribute__((unused
)))
637 CRIT("Incompatible library ABIs detected within the same process. "
638 "The process is likely linked against different major soname of LTTng-UST which is unsupported. "
639 "The detection was triggered by canary symbol \"%s\"\n", __func__
);
645 * lttng_ust_tracepoint_provider_register - Connect a probe to a tracepoint
646 * @name: tracepoint provider name
647 * @name: tracepoint event name
648 * @probe: probe handler
650 * Returns 0 if ok, error value on error.
651 * The probe address must at least be aligned on the architecture pointer size.
652 * Called with the tracepoint mutex held.
654 int lttng_ust_tracepoint_provider_register(const char *provider_name
, const char *event_name
,
655 void (*probe
)(void), void *data
, const char *signature
)
661 * Check if we find a symbol of the previous ABI in the current process
662 * as different ABIs of liblttng-ust can't co-exist in a process. If we
663 * do so, emit a critical log message which will also abort if the
664 * LTTNG_UST_ABORT_ON_CRITICAL environment variable is set.
666 lttng_ust_tracepoint_check_soname_0();
668 DBG("Registering probe to tracepoint \"%s:%s\"", provider_name
, event_name
);
670 pthread_mutex_lock(&tracepoint_mutex
);
671 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
677 tracepoint_sync_callsites(provider_name
, event_name
);
680 pthread_mutex_unlock(&tracepoint_mutex
);
685 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
686 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
687 * times to ensure it does not leak memory.
689 int lttng_ust_tp_probe_register_queue_release(const char *provider_name
, const char *event_name
,
690 void (*probe
)(void), void *data
, const char *signature
)
695 DBG("Registering probe to tracepoint \"%s:%s\". Queuing release.", provider_name
, event_name
);
697 pthread_mutex_lock(&tracepoint_mutex
);
698 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
704 tracepoint_sync_callsites(provider_name
, event_name
);
705 tracepoint_release_queue_add_old_probes(old
);
707 pthread_mutex_unlock(&tracepoint_mutex
);
711 static void *tracepoint_remove_probe(const char *provider_name
, const char *event_name
,
712 void (*probe
)(void), void *data
)
714 struct tracepoint_entry
*entry
;
717 entry
= get_tracepoint(provider_name
, event_name
);
719 return ERR_PTR(-ENOENT
);
720 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
723 if (!entry
->refcount
)
724 remove_tracepoint(entry
);
729 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
730 * @provider_name: tracepoint provider name
731 * @event_name: tracepoint event name
732 * @probe: probe function pointer
733 * @probe: probe data pointer
735 int lttng_ust_tracepoint_provider_unregister(const char *provider_name
, const char *event_name
,
736 void (*probe
)(void), void *data
)
741 DBG("Un-registering probe \"%s:%s\" from tracepoint %p", provider_name
, event_name
, probe
);
743 pthread_mutex_lock(&tracepoint_mutex
);
744 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
749 tracepoint_sync_callsites(provider_name
, event_name
);
752 pthread_mutex_unlock(&tracepoint_mutex
);
757 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
758 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
759 * times to ensure it does not leak memory.
761 int lttng_ust_tp_probe_unregister_queue_release(const char *provider_name
, const char *event_name
,
762 void (*probe
)(void), void *data
)
767 DBG("Un-registering probe from tracepoint \"%s:%s\". Queuing release.", provider_name
, event_name
);
769 pthread_mutex_lock(&tracepoint_mutex
);
770 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
775 tracepoint_sync_callsites(provider_name
, event_name
);
776 tracepoint_release_queue_add_old_probes(old
);
778 pthread_mutex_unlock(&tracepoint_mutex
);
782 void lttng_ust_tp_probe_prune_release_queue(void)
784 CDS_LIST_HEAD(release_probes
);
785 struct tp_probes
*pos
, *next
;
787 DBG("Release queue of unregistered tracepoint probes.");
789 pthread_mutex_lock(&tracepoint_mutex
);
790 if (!release_queue_need_update
)
792 if (!cds_list_empty(&release_queue
))
793 cds_list_replace_init(&release_queue
, &release_probes
);
794 release_queue_need_update
= 0;
796 /* Wait for grace period between all sync_callsites and free. */
797 lttng_ust_urcu_synchronize_rcu();
799 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
800 cds_list_del(&pos
->u
.list
);
804 pthread_mutex_unlock(&tracepoint_mutex
);
807 static void tracepoint_add_old_probes(void *old
)
811 struct tp_probes
*tp_probes
= caa_container_of(old
,
812 struct tp_probes
, probes
[0]);
813 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
818 * tracepoint_probe_register_noupdate - register a probe but not connect
819 * @name: tracepoint name
820 * @probe: probe handler
822 * caller must call tracepoint_probe_update_all()
824 int tracepoint_probe_register_noupdate(const char *provider_name
, const char *event_name
,
825 void (*probe
)(void), void *data
, const char *signature
)
830 pthread_mutex_lock(&tracepoint_mutex
);
831 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
836 tracepoint_add_old_probes(old
);
838 pthread_mutex_unlock(&tracepoint_mutex
);
843 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
844 * @name: tracepoint name
845 * @probe: probe function pointer
847 * caller must call tracepoint_probe_update_all()
848 * Called with the tracepoint mutex held.
850 int tracepoint_probe_unregister_noupdate(const char *provider_name
, const char *event_name
,
851 void (*probe
)(void), void *data
)
856 DBG("Un-registering probe from tracepoint \"%s:%s\"", provider_name
, event_name
);
858 pthread_mutex_lock(&tracepoint_mutex
);
859 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
864 tracepoint_add_old_probes(old
);
866 pthread_mutex_unlock(&tracepoint_mutex
);
871 * tracepoint_probe_update_all - update tracepoints
873 void tracepoint_probe_update_all(void)
875 CDS_LIST_HEAD(release_probes
);
876 struct tp_probes
*pos
, *next
;
878 pthread_mutex_lock(&tracepoint_mutex
);
882 if (!cds_list_empty(&old_probes
))
883 cds_list_replace_init(&old_probes
, &release_probes
);
886 tracepoint_update_probes();
887 /* Wait for grace period between update_probes and free. */
888 lttng_ust_urcu_synchronize_rcu();
889 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
890 cds_list_del(&pos
->u
.list
);
894 pthread_mutex_unlock(&tracepoint_mutex
);
897 static void new_tracepoints(struct lttng_ust_tracepoint
* const *start
,
898 struct lttng_ust_tracepoint
* const *end
)
900 if (new_tracepoint_cb
) {
901 struct lttng_ust_tracepoint
* const *t
;
903 for (t
= start
; t
< end
; t
++) {
905 new_tracepoint_cb(*t
);
911 * tracepoint_{un,}register_lib is meant to be looked up by instrumented
912 * applications through dlsym(). If found, those can register their
913 * tracepoints, else those tracepoints will not be available for
914 * tracing. The number at the end of those symbols acts as a major
915 * version for tracepoints.
917 * Older instrumented applications should still work with newer
918 * liblttng-ust, but it is fine that instrumented applications compiled
919 * against recent liblttng-ust headers require a recent liblttng-ust
920 * runtime for those tracepoints to be taken into account.
922 int lttng_ust_tracepoint_module_register(struct lttng_ust_tracepoint
* const *tracepoints_start
,
923 int tracepoints_count
);
924 int lttng_ust_tracepoint_module_register(struct lttng_ust_tracepoint
* const *tracepoints_start
,
925 int tracepoints_count
)
927 struct tracepoint_lib
*pl
, *iter
;
931 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
933 PERROR("Unable to register tracepoint lib");
936 pl
->tracepoints_start
= tracepoints_start
;
937 pl
->tracepoints_count
= tracepoints_count
;
938 CDS_INIT_LIST_HEAD(&pl
->callsites
);
940 pthread_mutex_lock(&tracepoint_mutex
);
942 * We sort the libs by struct lib pointer address.
944 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
945 BUG_ON(iter
== pl
); /* Should never be in the list twice */
947 /* We belong to the location right after iter. */
948 cds_list_add(&pl
->list
, &iter
->list
);
952 /* We should be added at the head of the list */
953 cds_list_add(&pl
->list
, &libs
);
955 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
956 lib_register_callsites(pl
);
957 lib_update_tracepoints(pl
);
958 pthread_mutex_unlock(&tracepoint_mutex
);
960 DBG("just registered a tracepoints section from %p and having %d tracepoints",
961 tracepoints_start
, tracepoints_count
);
962 if (lttng_ust_logging_debug_enabled()) {
965 for (i
= 0; i
< tracepoints_count
; i
++) {
966 if (!lttng_ust_tp_validate_event_name(tracepoints_start
[i
]))
968 DBG("registered tracepoint: \"%s:%s\"",
969 tracepoints_start
[i
]->provider_name
,
970 tracepoints_start
[i
]->event_name
);
977 int lttng_ust_tracepoint_module_unregister(struct lttng_ust_tracepoint
* const *tracepoints_start
);
978 int lttng_ust_tracepoint_module_unregister(struct lttng_ust_tracepoint
* const *tracepoints_start
)
980 struct tracepoint_lib
*lib
;
982 pthread_mutex_lock(&tracepoint_mutex
);
983 cds_list_for_each_entry(lib
, &libs
, list
) {
984 if (lib
->tracepoints_start
!= tracepoints_start
)
987 cds_list_del(&lib
->list
);
989 * Unregistering a callsite also decreases the
990 * callsite reference count of the corresponding
991 * tracepoint, and disables the tracepoint if
992 * the reference count drops to zero.
994 lib_unregister_callsites(lib
);
995 DBG("just unregistered a tracepoints section from %p",
996 lib
->tracepoints_start
);
1000 pthread_mutex_unlock(&tracepoint_mutex
);
1005 * Report in debug message whether the compiler correctly supports weak
1006 * hidden symbols. This test checks that the address associated with two
1007 * weak symbols with hidden visibility is the same when declared within
1008 * two compile units part of the same module.
1010 static void check_weak_hidden(void)
1012 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
1013 <tng_ust_tracepoint_test_symbol1
== lttng_ust_tp_check_weak_hidden1() ?
1015 "DIFFERENT addresses");
1016 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
1017 <tng_ust_tracepoint_test_symbol2
== lttng_ust_tp_check_weak_hidden2() ?
1019 "DIFFERENT addresses");
1020 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
1021 <tng_ust_tracepoint_test_symbol3
== lttng_ust_tp_check_weak_hidden3() ?
1023 "DIFFERENT addresses");
1026 void lttng_ust_tp_init(void)
1028 if (uatomic_xchg(&initialized
, 1) == 1)
1030 lttng_ust_logging_init();
1031 lttng_ust_common_ctor();
1032 check_weak_hidden();
1035 void lttng_ust_tp_exit(void)
1041 * Create the wrapper symbols.
1043 #undef lttng_ust_tp_rcu_read_lock
1044 #undef lttng_ust_tp_rcu_read_unlock
1045 #undef lttng_ust_tp_rcu_dereference
1047 void lttng_ust_tp_rcu_read_lock(void);
1048 void lttng_ust_tp_rcu_read_lock(void)
1050 lttng_ust_urcu_read_lock();
1053 void lttng_ust_tp_rcu_read_unlock(void);
1054 void lttng_ust_tp_rcu_read_unlock(void)
1056 lttng_ust_urcu_read_unlock();
1059 void *lttng_ust_tp_rcu_dereference_sym(void *p
);
1060 void *lttng_ust_tp_rcu_dereference_sym(void *p
)
1062 return lttng_ust_rcu_dereference(p
);
1066 * Programs that have threads that survive after they exit, and therefore call
1067 * library destructors, should disable the tracepoint destructors by calling
1068 * lttng_ust_tp_disable_destructors(). This will leak the tracepoint
1069 * instrumentation library shared object, leaving its teardown to the operating
1070 * system process teardown.
1072 * To access and/or modify this value, users need to use a combination of
1073 * dlopen(3) and dlsym(3) to get an handle on the
1074 * lttng_ust_tp_disable_destructors and lttng_ust_tp_get_destructors_state symbols below.
1076 void lttng_ust_tp_disable_destructors(void);
1077 void lttng_ust_tp_disable_destructors(void)
1079 uatomic_set(&tracepoint_destructors_state
, 0);
1083 * Returns 1 if the destructors are enabled and should be executed.
1084 * Returns 0 if the destructors are disabled.
1086 int lttng_ust_tp_get_destructors_state(void);
1087 int lttng_ust_tp_get_destructors_state(void)
1089 return uatomic_read(&tracepoint_destructors_state
);