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.
16 #include <urcu/arch.h>
17 #include <lttng/urcu/urcu-ust.h>
18 #include <urcu/hlist.h>
19 #include <urcu/uatomic.h>
20 #include <urcu/compiler.h>
21 #include <urcu/system.h>
23 #include <lttng/tracepoint.h>
24 #include <lttng/ust-abi.h> /* for LTTNG_UST_ABI_SYM_NAME_LEN */
26 #include <usterr-signal-safe.h>
27 #include <ust-helper.h>
29 #include "tracepoint-internal.h"
30 #include "lttng-tracer-core.h"
34 /* Test compiler support for weak symbols with hidden visibility. */
35 int __tracepoint_test_symbol1
__attribute__((weak
, visibility("hidden")));
36 void *__tracepoint_test_symbol2
__attribute__((weak
, visibility("hidden")));
39 } __tracepoint_test_symbol3
__attribute__((weak
, visibility("hidden")));
41 /* Set to 1 to enable tracepoint debug output */
42 static const int tracepoint_debug
;
43 static int initialized
;
46 * If tracepoint_destructors_state = 1, tracepoint destructors are
47 * enabled. They are disabled otherwise.
49 static int tracepoint_destructors_state
= 1;
51 static void (*new_tracepoint_cb
)(struct lttng_ust_tracepoint
*);
54 * tracepoint_mutex nests inside UST mutex.
56 * Note about interaction with fork/clone: UST does not hold the
57 * tracepoint mutex across fork/clone because it is either:
58 * - nested within UST mutex, in which case holding the UST mutex across
60 * - taken by a library constructor, which should never race with a
61 * fork/clone if the application is expected to continue running with
62 * the same memory layout (no following exec()).
64 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
67 * libraries that contain tracepoints (struct tracepoint_lib).
68 * Protected by tracepoint mutex.
70 static CDS_LIST_HEAD(libs
);
73 * The tracepoint mutex protects the library tracepoints, the hash table, and
75 * All calls to the tracepoint API must be protected by the tracepoint mutex,
76 * excepts calls to tracepoint_register_lib and
77 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
81 * Tracepoint hash table, containing the active tracepoints.
82 * Protected by tracepoint mutex.
84 #define TRACEPOINT_HASH_BITS 12
85 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
86 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
88 static CDS_LIST_HEAD(old_probes
);
89 static int need_update
;
91 static CDS_LIST_HEAD(release_queue
);
92 static int release_queue_need_update
;
96 * It is used to to delay the free of multiple probes array until a quiescent
98 * Tracepoint entries modifications are protected by the tracepoint mutex.
100 struct tracepoint_entry
{
101 struct cds_hlist_node hlist
;
102 struct lttng_ust_tracepoint_probe
*probes
;
103 int refcount
; /* Number of times armed. 0 if disarmed. */
104 int callsite_refcount
; /* how many libs use this tracepoint */
111 struct cds_list_head list
;
112 /* Field below only used for call_rcu scheme */
113 /* struct rcu_head head; */
115 struct lttng_ust_tracepoint_probe probes
[0];
119 * Callsite hash table, containing the tracepoint call sites.
120 * Protected by tracepoint mutex.
122 #define CALLSITE_HASH_BITS 12
123 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
124 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
126 struct callsite_entry
{
127 struct cds_hlist_node hlist
; /* hash table node */
128 struct cds_list_head node
; /* lib list of callsites node */
129 struct lttng_ust_tracepoint
*tp
;
130 bool tp_entry_callsite_ref
; /* Has a tp_entry took a ref on this callsite */
133 /* coverity[+alloc] */
134 static void *allocate_probes(int count
)
136 struct tp_probes
*p
=
137 zmalloc(count
* sizeof(struct lttng_ust_tracepoint_probe
)
138 + sizeof(struct tp_probes
));
139 return p
== NULL
? NULL
: p
->probes
;
142 /* coverity[+free : arg-0] */
143 static void release_probes(void *old
)
146 struct tp_probes
*tp_probes
= caa_container_of(old
,
147 struct tp_probes
, probes
[0]);
148 lttng_ust_urcu_synchronize_rcu();
153 static void debug_print_probes(struct tracepoint_entry
*entry
)
157 if (!tracepoint_debug
|| !entry
->probes
)
160 for (i
= 0; entry
->probes
[i
].func
; i
++)
161 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
165 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
166 void (*probe
)(void), void *data
)
169 struct lttng_ust_tracepoint_probe
*old
, *new;
173 return ERR_PTR(-EINVAL
);
175 debug_print_probes(entry
);
178 /* (N -> N+1), (N != 0, 1) probes */
179 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
180 if (old
[nr_probes
].func
== probe
&&
181 old
[nr_probes
].data
== data
)
182 return ERR_PTR(-EEXIST
);
184 /* + 2 : one for new probe, one for NULL func */
185 new = allocate_probes(nr_probes
+ 2);
187 return ERR_PTR(-ENOMEM
);
190 nr_probes
* sizeof(struct lttng_ust_tracepoint_probe
));
191 new[nr_probes
].func
= probe
;
192 new[nr_probes
].data
= data
;
193 new[nr_probes
+ 1].func
= NULL
;
194 entry
->refcount
= nr_probes
+ 1;
196 debug_print_probes(entry
);
201 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
202 void (*probe
)(void), void *data
)
204 int nr_probes
= 0, nr_del
= 0, i
;
205 struct lttng_ust_tracepoint_probe
*old
, *new;
210 return ERR_PTR(-ENOENT
);
212 debug_print_probes(entry
);
213 /* (N -> M), (N > 1, M >= 0) probes */
215 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
216 if (old
[nr_probes
].func
== probe
&&
217 old
[nr_probes
].data
== data
)
222 if (nr_probes
- nr_del
== 0) {
223 /* N -> 0, (N > 1) */
224 entry
->probes
= NULL
;
226 debug_print_probes(entry
);
230 /* N -> M, (N > 1, M > 0) */
232 new = allocate_probes(nr_probes
- nr_del
+ 1);
234 return ERR_PTR(-ENOMEM
);
235 for (i
= 0; old
[i
].func
; i
++)
236 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
238 new[nr_probes
- nr_del
].func
= NULL
;
239 entry
->refcount
= nr_probes
- nr_del
;
242 debug_print_probes(entry
);
247 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
248 * Must be called with tracepoint mutex held.
249 * Returns NULL if not present.
251 static struct tracepoint_entry
*get_tracepoint(const char *name
)
253 struct cds_hlist_head
*head
;
254 struct cds_hlist_node
*node
;
255 struct tracepoint_entry
*e
;
256 size_t name_len
= strlen(name
);
259 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
260 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
261 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
263 hash
= jhash(name
, name_len
, 0);
264 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
265 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
266 if (!strncmp(name
, e
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1))
273 * Add the tracepoint to the tracepoint hash table. Must be called with
274 * tracepoint mutex held.
276 static struct tracepoint_entry
*add_tracepoint(const char *name
,
277 const char *signature
)
279 struct cds_hlist_head
*head
;
280 struct cds_hlist_node
*node
;
281 struct tracepoint_entry
*e
;
282 size_t name_len
= strlen(name
);
283 size_t sig_len
= strlen(signature
);
284 size_t sig_off
, name_off
;
287 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
288 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
289 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
291 hash
= jhash(name
, name_len
, 0);
292 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
293 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
294 if (!strncmp(name
, e
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1)) {
295 DBG("tracepoint %s busy", name
);
296 return ERR_PTR(-EEXIST
); /* Already there */
301 * Using zmalloc here to allocate a variable length elements: name and
302 * signature. Could cause some memory fragmentation if overused.
304 name_off
= sizeof(struct tracepoint_entry
);
305 sig_off
= name_off
+ name_len
+ 1;
307 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1 + sig_len
+ 1);
309 return ERR_PTR(-ENOMEM
);
310 e
->name
= (char *) e
+ name_off
;
311 memcpy(e
->name
, name
, name_len
+ 1);
312 e
->name
[name_len
] = '\0';
314 e
->signature
= (char *) e
+ sig_off
;
315 memcpy(e
->signature
, signature
, sig_len
+ 1);
316 e
->signature
[sig_len
] = '\0';
320 e
->callsite_refcount
= 0;
322 cds_hlist_add_head(&e
->hlist
, head
);
327 * Remove the tracepoint from the tracepoint hash table. Must be called with
328 * tracepoint mutex held.
330 static void remove_tracepoint(struct tracepoint_entry
*e
)
332 cds_hlist_del(&e
->hlist
);
337 * Sets the probe callback corresponding to one tracepoint.
339 static void set_tracepoint(struct tracepoint_entry
**entry
,
340 struct lttng_ust_tracepoint
*elem
, int active
)
342 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1) != 0);
344 * Check that signatures match before connecting a probe to a
345 * tracepoint. Warn the user if they don't.
347 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
348 static int warned
= 0;
350 /* Only print once, don't flood console. */
352 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
353 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
354 elem
->name
, elem
->signature
, (*entry
)->signature
);
357 /* Don't accept connecting non-matching signatures. */
362 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
363 * probe callbacks array is consistent before setting a pointer to it.
364 * This array is referenced by __DO_TRACE from
365 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
368 lttng_ust_rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
369 CMM_STORE_SHARED(elem
->state
, active
);
373 * Disable a tracepoint and its probe callback.
374 * Note: only waiting an RCU period after setting elem->call to the empty
375 * function insures that the original callback is not used anymore. This insured
376 * by preempt_disable around the call site.
378 static void disable_tracepoint(struct lttng_ust_tracepoint
*elem
)
380 CMM_STORE_SHARED(elem
->state
, 0);
381 lttng_ust_rcu_assign_pointer(elem
->probes
, NULL
);
385 * Add the callsite to the callsite hash table. Must be called with
386 * tracepoint mutex held.
388 static void add_callsite(struct tracepoint_lib
* lib
, struct lttng_ust_tracepoint
*tp
)
390 struct cds_hlist_head
*head
;
391 struct callsite_entry
*e
;
392 const char *name
= tp
->name
;
393 size_t name_len
= strlen(name
);
395 struct tracepoint_entry
*tp_entry
;
397 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
398 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
399 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
401 hash
= jhash(name
, name_len
, 0);
402 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
403 e
= zmalloc(sizeof(struct callsite_entry
));
405 PERROR("Unable to add callsite for tracepoint \"%s\"", name
);
408 cds_hlist_add_head(&e
->hlist
, head
);
410 cds_list_add(&e
->node
, &lib
->callsites
);
412 tp_entry
= get_tracepoint(name
);
415 tp_entry
->callsite_refcount
++;
416 e
->tp_entry_callsite_ref
= true;
420 * Remove the callsite from the callsite hash table and from lib
421 * callsite list. Must be called with tracepoint mutex held.
423 static void remove_callsite(struct callsite_entry
*e
)
425 struct tracepoint_entry
*tp_entry
;
427 tp_entry
= get_tracepoint(e
->tp
->name
);
429 if (e
->tp_entry_callsite_ref
)
430 tp_entry
->callsite_refcount
--;
431 if (tp_entry
->callsite_refcount
== 0)
432 disable_tracepoint(e
->tp
);
434 cds_hlist_del(&e
->hlist
);
435 cds_list_del(&e
->node
);
440 * Enable/disable all callsites based on the state of a specific
442 * Must be called with tracepoint mutex held.
444 static void tracepoint_sync_callsites(const char *name
)
446 struct cds_hlist_head
*head
;
447 struct cds_hlist_node
*node
;
448 struct callsite_entry
*e
;
449 size_t name_len
= strlen(name
);
451 struct tracepoint_entry
*tp_entry
;
453 tp_entry
= get_tracepoint(name
);
454 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
455 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
456 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
458 hash
= jhash(name
, name_len
, 0);
459 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
460 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
461 struct lttng_ust_tracepoint
*tp
= e
->tp
;
463 if (strncmp(name
, tp
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1))
466 if (!e
->tp_entry_callsite_ref
) {
467 tp_entry
->callsite_refcount
++;
468 e
->tp_entry_callsite_ref
= true;
470 set_tracepoint(&tp_entry
, tp
,
471 !!tp_entry
->refcount
);
473 disable_tracepoint(tp
);
474 e
->tp_entry_callsite_ref
= false;
480 * tracepoint_update_probe_range - Update a probe range
481 * @begin: beginning of the range
482 * @end: end of the range
484 * Updates the probe callback corresponding to a range of tracepoints.
487 void tracepoint_update_probe_range(struct lttng_ust_tracepoint
* const *begin
,
488 struct lttng_ust_tracepoint
* const *end
)
490 struct lttng_ust_tracepoint
* const *iter
;
491 struct tracepoint_entry
*mark_entry
;
493 for (iter
= begin
; iter
< end
; iter
++) {
495 continue; /* skip dummy */
496 if (!(*iter
)->name
) {
497 disable_tracepoint(*iter
);
500 mark_entry
= get_tracepoint((*iter
)->name
);
502 set_tracepoint(&mark_entry
, *iter
,
503 !!mark_entry
->refcount
);
505 disable_tracepoint(*iter
);
510 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
512 tracepoint_update_probe_range(lib
->tracepoints_start
,
513 lib
->tracepoints_start
+ lib
->tracepoints_count
);
516 static void lib_register_callsites(struct tracepoint_lib
*lib
)
518 struct lttng_ust_tracepoint
* const *begin
;
519 struct lttng_ust_tracepoint
* const *end
;
520 struct lttng_ust_tracepoint
* const *iter
;
522 begin
= lib
->tracepoints_start
;
523 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
525 for (iter
= begin
; iter
< end
; iter
++) {
527 continue; /* skip dummy */
528 if (!(*iter
)->name
) {
531 add_callsite(lib
, *iter
);
535 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
537 struct callsite_entry
*callsite
, *tmp
;
539 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
540 remove_callsite(callsite
);
544 * Update probes, removing the faulty probes.
546 static void tracepoint_update_probes(void)
548 struct tracepoint_lib
*lib
;
550 /* tracepoints registered from libraries and executable. */
551 cds_list_for_each_entry(lib
, &libs
, list
)
552 lib_update_tracepoints(lib
);
555 static struct lttng_ust_tracepoint_probe
*
556 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
557 const char *signature
)
559 struct tracepoint_entry
*entry
;
560 struct lttng_ust_tracepoint_probe
*old
;
562 entry
= get_tracepoint(name
);
564 if (strcmp(entry
->signature
, signature
) != 0) {
565 ERR("Tracepoint and probe signature do not match.");
566 return ERR_PTR(-EINVAL
);
569 entry
= add_tracepoint(name
, signature
);
571 return (struct lttng_ust_tracepoint_probe
*)entry
;
573 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
574 if (IS_ERR(old
) && !entry
->refcount
)
575 remove_tracepoint(entry
);
579 static void tracepoint_release_queue_add_old_probes(void *old
)
581 release_queue_need_update
= 1;
583 struct tp_probes
*tp_probes
= caa_container_of(old
,
584 struct tp_probes
, probes
[0]);
585 cds_list_add(&tp_probes
->u
.list
, &release_queue
);
590 * __tracepoint_probe_register - Connect a probe to a tracepoint
591 * @name: tracepoint name
592 * @probe: probe handler
594 * Returns 0 if ok, error value on error.
595 * The probe address must at least be aligned on the architecture pointer size.
596 * Called with the tracepoint mutex held.
598 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
599 void *data
, const char *signature
)
604 DBG("Registering probe to tracepoint %s", name
);
606 pthread_mutex_lock(&tracepoint_mutex
);
607 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
613 tracepoint_sync_callsites(name
);
616 pthread_mutex_unlock(&tracepoint_mutex
);
621 * Caller needs to invoke __tracepoint_probe_release_queue() after
622 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
623 * times to ensure it does not leak memory.
625 int lttng_ust_tp_probe_register_queue_release(const char *name
,
626 void (*probe
)(void), void *data
, const char *signature
)
631 DBG("Registering probe to tracepoint %s. Queuing release.", name
);
633 pthread_mutex_lock(&tracepoint_mutex
);
634 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
640 tracepoint_sync_callsites(name
);
641 tracepoint_release_queue_add_old_probes(old
);
643 pthread_mutex_unlock(&tracepoint_mutex
);
647 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
650 struct tracepoint_entry
*entry
;
653 entry
= get_tracepoint(name
);
655 return ERR_PTR(-ENOENT
);
656 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
659 if (!entry
->refcount
)
660 remove_tracepoint(entry
);
665 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
666 * @name: tracepoint name
667 * @probe: probe function pointer
668 * @probe: probe data pointer
670 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
676 DBG("Un-registering probe from tracepoint %s", name
);
678 pthread_mutex_lock(&tracepoint_mutex
);
679 old
= tracepoint_remove_probe(name
, probe
, data
);
684 tracepoint_sync_callsites(name
);
687 pthread_mutex_unlock(&tracepoint_mutex
);
692 * Caller needs to invoke __tracepoint_probe_release_queue() after
693 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
694 * times to ensure it does not leak memory.
696 int lttng_ust_tp_probe_unregister_queue_release(const char *name
,
697 void (*probe
)(void), void *data
)
702 DBG("Un-registering probe from tracepoint %s. Queuing release.", name
);
704 pthread_mutex_lock(&tracepoint_mutex
);
705 old
= tracepoint_remove_probe(name
, probe
, data
);
710 tracepoint_sync_callsites(name
);
711 tracepoint_release_queue_add_old_probes(old
);
713 pthread_mutex_unlock(&tracepoint_mutex
);
717 void lttng_ust_tp_probe_prune_release_queue(void)
719 CDS_LIST_HEAD(release_probes
);
720 struct tp_probes
*pos
, *next
;
722 DBG("Release queue of unregistered tracepoint probes.");
724 pthread_mutex_lock(&tracepoint_mutex
);
725 if (!release_queue_need_update
)
727 if (!cds_list_empty(&release_queue
))
728 cds_list_replace_init(&release_queue
, &release_probes
);
729 release_queue_need_update
= 0;
731 /* Wait for grace period between all sync_callsites and free. */
732 lttng_ust_urcu_synchronize_rcu();
734 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
735 cds_list_del(&pos
->u
.list
);
739 pthread_mutex_unlock(&tracepoint_mutex
);
742 static void tracepoint_add_old_probes(void *old
)
746 struct tp_probes
*tp_probes
= caa_container_of(old
,
747 struct tp_probes
, probes
[0]);
748 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
753 * tracepoint_probe_register_noupdate - register a probe but not connect
754 * @name: tracepoint name
755 * @probe: probe handler
757 * caller must call tracepoint_probe_update_all()
759 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
760 void *data
, const char *signature
)
765 pthread_mutex_lock(&tracepoint_mutex
);
766 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
771 tracepoint_add_old_probes(old
);
773 pthread_mutex_unlock(&tracepoint_mutex
);
778 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
779 * @name: tracepoint name
780 * @probe: probe function pointer
782 * caller must call tracepoint_probe_update_all()
783 * Called with the tracepoint mutex held.
785 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
791 DBG("Un-registering probe from tracepoint %s", name
);
793 pthread_mutex_lock(&tracepoint_mutex
);
794 old
= tracepoint_remove_probe(name
, probe
, data
);
799 tracepoint_add_old_probes(old
);
801 pthread_mutex_unlock(&tracepoint_mutex
);
806 * tracepoint_probe_update_all - update tracepoints
808 void tracepoint_probe_update_all(void)
810 CDS_LIST_HEAD(release_probes
);
811 struct tp_probes
*pos
, *next
;
813 pthread_mutex_lock(&tracepoint_mutex
);
817 if (!cds_list_empty(&old_probes
))
818 cds_list_replace_init(&old_probes
, &release_probes
);
821 tracepoint_update_probes();
822 /* Wait for grace period between update_probes and free. */
823 lttng_ust_urcu_synchronize_rcu();
824 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
825 cds_list_del(&pos
->u
.list
);
829 pthread_mutex_unlock(&tracepoint_mutex
);
832 static void new_tracepoints(struct lttng_ust_tracepoint
* const *start
,
833 struct lttng_ust_tracepoint
* const *end
)
835 if (new_tracepoint_cb
) {
836 struct lttng_ust_tracepoint
* const *t
;
838 for (t
= start
; t
< end
; t
++) {
840 new_tracepoint_cb(*t
);
846 * tracepoint_{un,}register_lib is meant to be looked up by instrumented
847 * applications through dlsym(). If found, those can register their
848 * tracepoints, else those tracepoints will not be available for
849 * tracing. The number at the end of those symbols acts as a major
850 * version for tracepoints.
852 * Older instrumented applications should still work with newer
853 * liblttng-ust, but it is fine that instrumented applications compiled
854 * against recent liblttng-ust headers require a recent liblttng-ust
855 * runtime for those tracepoints to be taken into account.
857 int tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
858 int tracepoints_count
);
859 int tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
860 int tracepoints_count
)
862 struct tracepoint_lib
*pl
, *iter
;
866 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
868 PERROR("Unable to register tracepoint lib");
871 pl
->tracepoints_start
= tracepoints_start
;
872 pl
->tracepoints_count
= tracepoints_count
;
873 CDS_INIT_LIST_HEAD(&pl
->callsites
);
875 pthread_mutex_lock(&tracepoint_mutex
);
877 * We sort the libs by struct lib pointer address.
879 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
880 BUG_ON(iter
== pl
); /* Should never be in the list twice */
882 /* We belong to the location right after iter. */
883 cds_list_add(&pl
->list
, &iter
->list
);
887 /* We should be added at the head of the list */
888 cds_list_add(&pl
->list
, &libs
);
890 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
891 lib_register_callsites(pl
);
892 lib_update_tracepoints(pl
);
893 pthread_mutex_unlock(&tracepoint_mutex
);
895 DBG("just registered a tracepoints section from %p and having %d tracepoints",
896 tracepoints_start
, tracepoints_count
);
897 if (ust_err_debug_enabled()) {
900 for (i
= 0; i
< tracepoints_count
; i
++) {
901 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
908 int tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
);
909 int tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
)
911 struct tracepoint_lib
*lib
;
913 pthread_mutex_lock(&tracepoint_mutex
);
914 cds_list_for_each_entry(lib
, &libs
, list
) {
915 if (lib
->tracepoints_start
!= tracepoints_start
)
918 cds_list_del(&lib
->list
);
920 * Unregistering a callsite also decreases the
921 * callsite reference count of the corresponding
922 * tracepoint, and disables the tracepoint if
923 * the reference count drops to zero.
925 lib_unregister_callsites(lib
);
926 DBG("just unregistered a tracepoints section from %p",
927 lib
->tracepoints_start
);
931 pthread_mutex_unlock(&tracepoint_mutex
);
936 * Report in debug message whether the compiler correctly supports weak
937 * hidden symbols. This test checks that the address associated with two
938 * weak symbols with hidden visibility is the same when declared within
939 * two compile units part of the same module.
941 static void check_weak_hidden(void)
943 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
944 &__tracepoint_test_symbol1
== lttng_ust_tp_check_weak_hidden1() ?
946 "DIFFERENT addresses");
947 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
948 &__tracepoint_test_symbol2
== lttng_ust_tp_check_weak_hidden2() ?
950 "DIFFERENT addresses");
951 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
952 &__tracepoint_test_symbol3
== lttng_ust_tp_check_weak_hidden3() ?
954 "DIFFERENT addresses");
957 void lttng_ust_tp_init(void)
959 if (uatomic_xchg(&initialized
, 1) == 1)
965 void lttng_ust_tp_exit(void)
971 * Create the wrapper symbols.
973 #undef tp_rcu_read_lock
974 #undef tp_rcu_read_unlock
975 #undef tp_rcu_dereference
977 void tp_rcu_read_lock(void);
978 void tp_rcu_read_lock(void)
980 lttng_ust_urcu_read_lock();
983 void tp_rcu_read_unlock(void);
984 void tp_rcu_read_unlock(void)
986 lttng_ust_urcu_read_unlock();
989 void *tp_rcu_dereference_sym(void *p
);
990 void *tp_rcu_dereference_sym(void *p
)
992 return lttng_ust_rcu_dereference(p
);
996 * Programs that have threads that survive after they exit, and therefore call
997 * library destructors, should disable the tracepoint destructors by calling
998 * tp_disable_destructors(). This will leak the tracepoint
999 * instrumentation library shared object, leaving its teardown to the operating
1000 * system process teardown.
1002 * To access and/or modify this value, users need to use a combination of
1003 * dlopen(3) and dlsym(3) to get an handle on the
1004 * tp_disable_destructors and tp_get_destructors_state symbols below.
1006 void tp_disable_destructors(void);
1007 void tp_disable_destructors(void)
1009 uatomic_set(&tracepoint_destructors_state
, 0);
1013 * Returns 1 if the destructors are enabled and should be executed.
1014 * Returns 0 if the destructors are disabled.
1016 int tp_get_destructors_state(void);
1017 int tp_get_destructors_state(void)
1019 return uatomic_read(&tracepoint_destructors_state
);