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 "common/logging.h"
27 #include "common/macros.h"
29 #include "lib/lttng-ust-tracepoint/tracepoint.h"
30 #include "common/tracepoint.h"
31 #include "common/jhash.h"
32 #include "common/err-ptr.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 lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
== LTTNG_UST_ABI_SYM_NAME_LEN
,
134 "Tracepoint name max length mismatch between UST ABI and tracepoint API",
135 Tracepoint_name_max_length_mismatch
);
137 /* coverity[+alloc] */
138 static void *allocate_probes(int count
)
140 struct tp_probes
*p
=
141 zmalloc(count
* sizeof(struct lttng_ust_tracepoint_probe
)
142 + sizeof(struct tp_probes
));
143 return p
== NULL
? NULL
: p
->probes
;
146 /* coverity[+free : arg-0] */
147 static void release_probes(void *old
)
150 struct tp_probes
*tp_probes
= caa_container_of(old
,
151 struct tp_probes
, probes
[0]);
152 lttng_ust_urcu_synchronize_rcu();
157 static void debug_print_probes(struct tracepoint_entry
*entry
)
161 if (!tracepoint_debug
|| !entry
->probes
)
164 for (i
= 0; entry
->probes
[i
].func
; i
++)
165 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
169 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
170 void (*probe
)(void), void *data
)
173 struct lttng_ust_tracepoint_probe
*old
, *new;
177 return ERR_PTR(-EINVAL
);
179 debug_print_probes(entry
);
182 /* (N -> N+1), (N != 0, 1) probes */
183 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
184 if (old
[nr_probes
].func
== probe
&&
185 old
[nr_probes
].data
== data
)
186 return ERR_PTR(-EEXIST
);
188 /* + 2 : one for new probe, one for NULL func */
189 new = allocate_probes(nr_probes
+ 2);
191 return ERR_PTR(-ENOMEM
);
194 nr_probes
* sizeof(struct lttng_ust_tracepoint_probe
));
195 new[nr_probes
].func
= probe
;
196 new[nr_probes
].data
= data
;
197 new[nr_probes
+ 1].func
= NULL
;
198 entry
->refcount
= nr_probes
+ 1;
200 debug_print_probes(entry
);
205 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
206 void (*probe
)(void), void *data
)
208 int nr_probes
= 0, nr_del
= 0, i
;
209 struct lttng_ust_tracepoint_probe
*old
, *new;
214 return ERR_PTR(-ENOENT
);
216 debug_print_probes(entry
);
217 /* (N -> M), (N > 1, M >= 0) probes */
219 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
220 if (old
[nr_probes
].func
== probe
&&
221 old
[nr_probes
].data
== data
)
226 if (nr_probes
- nr_del
== 0) {
227 /* N -> 0, (N > 1) */
228 entry
->probes
= NULL
;
230 debug_print_probes(entry
);
234 /* N -> M, (N > 1, M > 0) */
236 new = allocate_probes(nr_probes
- nr_del
+ 1);
238 return ERR_PTR(-ENOMEM
);
239 for (i
= 0; old
[i
].func
; i
++)
240 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
242 new[nr_probes
- nr_del
].func
= NULL
;
243 entry
->refcount
= nr_probes
- nr_del
;
246 debug_print_probes(entry
);
251 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
252 * Must be called with tracepoint mutex held.
253 * Returns NULL if not present.
255 static struct tracepoint_entry
*get_tracepoint(const char *name
)
257 struct cds_hlist_head
*head
;
258 struct cds_hlist_node
*node
;
259 struct tracepoint_entry
*e
;
260 size_t name_len
= strlen(name
);
263 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
264 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
265 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
267 hash
= jhash(name
, name_len
, 0);
268 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
269 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
270 if (!strncmp(name
, e
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1))
277 * Add the tracepoint to the tracepoint hash table. Must be called with
278 * tracepoint mutex held.
280 static struct tracepoint_entry
*add_tracepoint(const char *name
,
281 const char *signature
)
283 struct cds_hlist_head
*head
;
284 struct cds_hlist_node
*node
;
285 struct tracepoint_entry
*e
;
286 size_t name_len
= strlen(name
);
287 size_t sig_len
= strlen(signature
);
288 size_t sig_off
, name_off
;
291 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
292 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
293 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
295 hash
= jhash(name
, name_len
, 0);
296 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
297 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
298 if (!strncmp(name
, e
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1)) {
299 DBG("tracepoint %s busy", name
);
300 return ERR_PTR(-EEXIST
); /* Already there */
305 * Using zmalloc here to allocate a variable length elements: name and
306 * signature. Could cause some memory fragmentation if overused.
308 name_off
= sizeof(struct tracepoint_entry
);
309 sig_off
= name_off
+ name_len
+ 1;
311 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1 + sig_len
+ 1);
313 return ERR_PTR(-ENOMEM
);
314 e
->name
= (char *) e
+ name_off
;
315 memcpy(e
->name
, name
, name_len
+ 1);
316 e
->name
[name_len
] = '\0';
318 e
->signature
= (char *) e
+ sig_off
;
319 memcpy(e
->signature
, signature
, sig_len
+ 1);
320 e
->signature
[sig_len
] = '\0';
324 e
->callsite_refcount
= 0;
326 cds_hlist_add_head(&e
->hlist
, head
);
331 * Remove the tracepoint from the tracepoint hash table. Must be called with
332 * tracepoint mutex held.
334 static void remove_tracepoint(struct tracepoint_entry
*e
)
336 cds_hlist_del(&e
->hlist
);
341 * Sets the probe callback corresponding to one tracepoint.
343 static void set_tracepoint(struct tracepoint_entry
**entry
,
344 struct lttng_ust_tracepoint
*elem
, int active
)
346 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1) != 0);
348 * Check that signatures match before connecting a probe to a
349 * tracepoint. Warn the user if they don't.
351 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
352 static int warned
= 0;
354 /* Only print once, don't flood console. */
356 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
357 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
358 elem
->name
, elem
->signature
, (*entry
)->signature
);
361 /* Don't accept connecting non-matching signatures. */
366 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
367 * probe callbacks array is consistent before setting a pointer to it.
368 * This array is referenced by __DO_TRACE from
369 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
372 lttng_ust_rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
373 CMM_STORE_SHARED(elem
->state
, active
);
377 * Disable a tracepoint and its probe callback.
378 * Note: only waiting an RCU period after setting elem->call to the empty
379 * function insures that the original callback is not used anymore. This insured
380 * by preempt_disable around the call site.
382 static void disable_tracepoint(struct lttng_ust_tracepoint
*elem
)
384 CMM_STORE_SHARED(elem
->state
, 0);
385 lttng_ust_rcu_assign_pointer(elem
->probes
, NULL
);
389 * Add the callsite to the callsite hash table. Must be called with
390 * tracepoint mutex held.
392 static void add_callsite(struct tracepoint_lib
* lib
, struct lttng_ust_tracepoint
*tp
)
394 struct cds_hlist_head
*head
;
395 struct callsite_entry
*e
;
396 const char *name
= tp
->name
;
397 size_t name_len
= strlen(name
);
399 struct tracepoint_entry
*tp_entry
;
401 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
402 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
403 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
405 hash
= jhash(name
, name_len
, 0);
406 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
407 e
= zmalloc(sizeof(struct callsite_entry
));
409 PERROR("Unable to add callsite for tracepoint \"%s\"", name
);
412 cds_hlist_add_head(&e
->hlist
, head
);
414 cds_list_add(&e
->node
, &lib
->callsites
);
416 tp_entry
= get_tracepoint(name
);
419 tp_entry
->callsite_refcount
++;
420 e
->tp_entry_callsite_ref
= true;
424 * Remove the callsite from the callsite hash table and from lib
425 * callsite list. Must be called with tracepoint mutex held.
427 static void remove_callsite(struct callsite_entry
*e
)
429 struct tracepoint_entry
*tp_entry
;
431 tp_entry
= get_tracepoint(e
->tp
->name
);
433 if (e
->tp_entry_callsite_ref
)
434 tp_entry
->callsite_refcount
--;
435 if (tp_entry
->callsite_refcount
== 0)
436 disable_tracepoint(e
->tp
);
438 cds_hlist_del(&e
->hlist
);
439 cds_list_del(&e
->node
);
444 * Enable/disable all callsites based on the state of a specific
446 * Must be called with tracepoint mutex held.
448 static void tracepoint_sync_callsites(const char *name
)
450 struct cds_hlist_head
*head
;
451 struct cds_hlist_node
*node
;
452 struct callsite_entry
*e
;
453 size_t name_len
= strlen(name
);
455 struct tracepoint_entry
*tp_entry
;
457 tp_entry
= get_tracepoint(name
);
458 if (name_len
> LTTNG_UST_ABI_SYM_NAME_LEN
- 1) {
459 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
460 name_len
= LTTNG_UST_ABI_SYM_NAME_LEN
- 1;
462 hash
= jhash(name
, name_len
, 0);
463 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
464 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
465 struct lttng_ust_tracepoint
*tp
= e
->tp
;
467 if (strncmp(name
, tp
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1))
470 if (!e
->tp_entry_callsite_ref
) {
471 tp_entry
->callsite_refcount
++;
472 e
->tp_entry_callsite_ref
= true;
474 set_tracepoint(&tp_entry
, tp
,
475 !!tp_entry
->refcount
);
477 disable_tracepoint(tp
);
478 e
->tp_entry_callsite_ref
= false;
484 * tracepoint_update_probe_range - Update a probe range
485 * @begin: beginning of the range
486 * @end: end of the range
488 * Updates the probe callback corresponding to a range of tracepoints.
491 void tracepoint_update_probe_range(struct lttng_ust_tracepoint
* const *begin
,
492 struct lttng_ust_tracepoint
* const *end
)
494 struct lttng_ust_tracepoint
* const *iter
;
495 struct tracepoint_entry
*mark_entry
;
497 for (iter
= begin
; iter
< end
; iter
++) {
499 continue; /* skip dummy */
500 if (!(*iter
)->name
) {
501 disable_tracepoint(*iter
);
504 mark_entry
= get_tracepoint((*iter
)->name
);
506 set_tracepoint(&mark_entry
, *iter
,
507 !!mark_entry
->refcount
);
509 disable_tracepoint(*iter
);
514 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
516 tracepoint_update_probe_range(lib
->tracepoints_start
,
517 lib
->tracepoints_start
+ lib
->tracepoints_count
);
520 static void lib_register_callsites(struct tracepoint_lib
*lib
)
522 struct lttng_ust_tracepoint
* const *begin
;
523 struct lttng_ust_tracepoint
* const *end
;
524 struct lttng_ust_tracepoint
* const *iter
;
526 begin
= lib
->tracepoints_start
;
527 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
529 for (iter
= begin
; iter
< end
; iter
++) {
531 continue; /* skip dummy */
532 if (!(*iter
)->name
) {
535 add_callsite(lib
, *iter
);
539 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
541 struct callsite_entry
*callsite
, *tmp
;
543 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
544 remove_callsite(callsite
);
548 * Update probes, removing the faulty probes.
550 static void tracepoint_update_probes(void)
552 struct tracepoint_lib
*lib
;
554 /* tracepoints registered from libraries and executable. */
555 cds_list_for_each_entry(lib
, &libs
, list
)
556 lib_update_tracepoints(lib
);
559 static struct lttng_ust_tracepoint_probe
*
560 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
561 const char *signature
)
563 struct tracepoint_entry
*entry
;
564 struct lttng_ust_tracepoint_probe
*old
;
566 entry
= get_tracepoint(name
);
568 if (strcmp(entry
->signature
, signature
) != 0) {
569 ERR("Tracepoint and probe signature do not match.");
570 return ERR_PTR(-EINVAL
);
573 entry
= add_tracepoint(name
, signature
);
575 return (struct lttng_ust_tracepoint_probe
*)entry
;
577 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
578 if (IS_ERR(old
) && !entry
->refcount
)
579 remove_tracepoint(entry
);
583 static void tracepoint_release_queue_add_old_probes(void *old
)
585 release_queue_need_update
= 1;
587 struct tp_probes
*tp_probes
= caa_container_of(old
,
588 struct tp_probes
, probes
[0]);
589 cds_list_add(&tp_probes
->u
.list
, &release_queue
);
594 * __tracepoint_probe_register - Connect a probe to a tracepoint
595 * @name: tracepoint name
596 * @probe: probe handler
598 * Returns 0 if ok, error value on error.
599 * The probe address must at least be aligned on the architecture pointer size.
600 * Called with the tracepoint mutex held.
602 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
603 void *data
, const char *signature
)
608 DBG("Registering probe to tracepoint %s", name
);
610 pthread_mutex_lock(&tracepoint_mutex
);
611 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
617 tracepoint_sync_callsites(name
);
620 pthread_mutex_unlock(&tracepoint_mutex
);
625 * Caller needs to invoke __tracepoint_probe_release_queue() after
626 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
627 * times to ensure it does not leak memory.
629 int lttng_ust_tp_probe_register_queue_release(const char *name
,
630 void (*probe
)(void), void *data
, const char *signature
)
635 DBG("Registering probe to tracepoint %s. Queuing release.", name
);
637 pthread_mutex_lock(&tracepoint_mutex
);
638 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
644 tracepoint_sync_callsites(name
);
645 tracepoint_release_queue_add_old_probes(old
);
647 pthread_mutex_unlock(&tracepoint_mutex
);
651 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
654 struct tracepoint_entry
*entry
;
657 entry
= get_tracepoint(name
);
659 return ERR_PTR(-ENOENT
);
660 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
663 if (!entry
->refcount
)
664 remove_tracepoint(entry
);
669 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
670 * @name: tracepoint name
671 * @probe: probe function pointer
672 * @probe: probe data pointer
674 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
680 DBG("Un-registering probe from tracepoint %s", name
);
682 pthread_mutex_lock(&tracepoint_mutex
);
683 old
= tracepoint_remove_probe(name
, probe
, data
);
688 tracepoint_sync_callsites(name
);
691 pthread_mutex_unlock(&tracepoint_mutex
);
696 * Caller needs to invoke __tracepoint_probe_release_queue() after
697 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
698 * times to ensure it does not leak memory.
700 int lttng_ust_tp_probe_unregister_queue_release(const char *name
,
701 void (*probe
)(void), void *data
)
706 DBG("Un-registering probe from tracepoint %s. Queuing release.", name
);
708 pthread_mutex_lock(&tracepoint_mutex
);
709 old
= tracepoint_remove_probe(name
, probe
, data
);
714 tracepoint_sync_callsites(name
);
715 tracepoint_release_queue_add_old_probes(old
);
717 pthread_mutex_unlock(&tracepoint_mutex
);
721 void lttng_ust_tp_probe_prune_release_queue(void)
723 CDS_LIST_HEAD(release_probes
);
724 struct tp_probes
*pos
, *next
;
726 DBG("Release queue of unregistered tracepoint probes.");
728 pthread_mutex_lock(&tracepoint_mutex
);
729 if (!release_queue_need_update
)
731 if (!cds_list_empty(&release_queue
))
732 cds_list_replace_init(&release_queue
, &release_probes
);
733 release_queue_need_update
= 0;
735 /* Wait for grace period between all sync_callsites and free. */
736 lttng_ust_urcu_synchronize_rcu();
738 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
739 cds_list_del(&pos
->u
.list
);
743 pthread_mutex_unlock(&tracepoint_mutex
);
746 static void tracepoint_add_old_probes(void *old
)
750 struct tp_probes
*tp_probes
= caa_container_of(old
,
751 struct tp_probes
, probes
[0]);
752 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
757 * tracepoint_probe_register_noupdate - register a probe but not connect
758 * @name: tracepoint name
759 * @probe: probe handler
761 * caller must call tracepoint_probe_update_all()
763 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
764 void *data
, const char *signature
)
769 pthread_mutex_lock(&tracepoint_mutex
);
770 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
775 tracepoint_add_old_probes(old
);
777 pthread_mutex_unlock(&tracepoint_mutex
);
782 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
783 * @name: tracepoint name
784 * @probe: probe function pointer
786 * caller must call tracepoint_probe_update_all()
787 * Called with the tracepoint mutex held.
789 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
795 DBG("Un-registering probe from tracepoint %s", name
);
797 pthread_mutex_lock(&tracepoint_mutex
);
798 old
= tracepoint_remove_probe(name
, probe
, data
);
803 tracepoint_add_old_probes(old
);
805 pthread_mutex_unlock(&tracepoint_mutex
);
810 * tracepoint_probe_update_all - update tracepoints
812 void tracepoint_probe_update_all(void)
814 CDS_LIST_HEAD(release_probes
);
815 struct tp_probes
*pos
, *next
;
817 pthread_mutex_lock(&tracepoint_mutex
);
821 if (!cds_list_empty(&old_probes
))
822 cds_list_replace_init(&old_probes
, &release_probes
);
825 tracepoint_update_probes();
826 /* Wait for grace period between update_probes and free. */
827 lttng_ust_urcu_synchronize_rcu();
828 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
829 cds_list_del(&pos
->u
.list
);
833 pthread_mutex_unlock(&tracepoint_mutex
);
836 static void new_tracepoints(struct lttng_ust_tracepoint
* const *start
,
837 struct lttng_ust_tracepoint
* const *end
)
839 if (new_tracepoint_cb
) {
840 struct lttng_ust_tracepoint
* const *t
;
842 for (t
= start
; t
< end
; t
++) {
844 new_tracepoint_cb(*t
);
850 * tracepoint_{un,}register_lib is meant to be looked up by instrumented
851 * applications through dlsym(). If found, those can register their
852 * tracepoints, else those tracepoints will not be available for
853 * tracing. The number at the end of those symbols acts as a major
854 * version for tracepoints.
856 * Older instrumented applications should still work with newer
857 * liblttng-ust, but it is fine that instrumented applications compiled
858 * against recent liblttng-ust headers require a recent liblttng-ust
859 * runtime for those tracepoints to be taken into account.
861 int tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
862 int tracepoints_count
);
863 int tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
864 int tracepoints_count
)
866 struct tracepoint_lib
*pl
, *iter
;
870 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
872 PERROR("Unable to register tracepoint lib");
875 pl
->tracepoints_start
= tracepoints_start
;
876 pl
->tracepoints_count
= tracepoints_count
;
877 CDS_INIT_LIST_HEAD(&pl
->callsites
);
879 pthread_mutex_lock(&tracepoint_mutex
);
881 * We sort the libs by struct lib pointer address.
883 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
884 BUG_ON(iter
== pl
); /* Should never be in the list twice */
886 /* We belong to the location right after iter. */
887 cds_list_add(&pl
->list
, &iter
->list
);
891 /* We should be added at the head of the list */
892 cds_list_add(&pl
->list
, &libs
);
894 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
895 lib_register_callsites(pl
);
896 lib_update_tracepoints(pl
);
897 pthread_mutex_unlock(&tracepoint_mutex
);
899 DBG("just registered a tracepoints section from %p and having %d tracepoints",
900 tracepoints_start
, tracepoints_count
);
901 if (lttng_ust_logging_debug_enabled()) {
904 for (i
= 0; i
< tracepoints_count
; i
++) {
905 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
912 int tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
);
913 int tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
)
915 struct tracepoint_lib
*lib
;
917 pthread_mutex_lock(&tracepoint_mutex
);
918 cds_list_for_each_entry(lib
, &libs
, list
) {
919 if (lib
->tracepoints_start
!= tracepoints_start
)
922 cds_list_del(&lib
->list
);
924 * Unregistering a callsite also decreases the
925 * callsite reference count of the corresponding
926 * tracepoint, and disables the tracepoint if
927 * the reference count drops to zero.
929 lib_unregister_callsites(lib
);
930 DBG("just unregistered a tracepoints section from %p",
931 lib
->tracepoints_start
);
935 pthread_mutex_unlock(&tracepoint_mutex
);
940 * Report in debug message whether the compiler correctly supports weak
941 * hidden symbols. This test checks that the address associated with two
942 * weak symbols with hidden visibility is the same when declared within
943 * two compile units part of the same module.
945 static void check_weak_hidden(void)
947 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
948 &__tracepoint_test_symbol1
== lttng_ust_tp_check_weak_hidden1() ?
950 "DIFFERENT addresses");
951 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
952 &__tracepoint_test_symbol2
== lttng_ust_tp_check_weak_hidden2() ?
954 "DIFFERENT addresses");
955 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
956 &__tracepoint_test_symbol3
== lttng_ust_tp_check_weak_hidden3() ?
958 "DIFFERENT addresses");
961 void lttng_ust_tp_init(void)
963 if (uatomic_xchg(&initialized
, 1) == 1)
965 lttng_ust_logging_init();
969 void lttng_ust_tp_exit(void)
975 * Create the wrapper symbols.
977 #undef tp_rcu_read_lock
978 #undef tp_rcu_read_unlock
979 #undef tp_rcu_dereference
981 void tp_rcu_read_lock(void);
982 void tp_rcu_read_lock(void)
984 lttng_ust_urcu_read_lock();
987 void tp_rcu_read_unlock(void);
988 void tp_rcu_read_unlock(void)
990 lttng_ust_urcu_read_unlock();
993 void *tp_rcu_dereference_sym(void *p
);
994 void *tp_rcu_dereference_sym(void *p
)
996 return lttng_ust_rcu_dereference(p
);
1000 * Programs that have threads that survive after they exit, and therefore call
1001 * library destructors, should disable the tracepoint destructors by calling
1002 * tp_disable_destructors(). This will leak the tracepoint
1003 * instrumentation library shared object, leaving its teardown to the operating
1004 * system process teardown.
1006 * To access and/or modify this value, users need to use a combination of
1007 * dlopen(3) and dlsym(3) to get an handle on the
1008 * tp_disable_destructors and tp_get_destructors_state symbols below.
1010 void tp_disable_destructors(void);
1011 void tp_disable_destructors(void)
1013 uatomic_set(&tracepoint_destructors_state
, 0);
1017 * Returns 1 if the destructors are enabled and should be executed.
1018 * Returns 0 if the destructors are disabled.
1020 int tp_get_destructors_state(void);
1021 int tp_get_destructors_state(void)
1023 return uatomic_read(&tracepoint_destructors_state
);