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 */
25 #include <lttng/ust-common.h>
27 #include "common/logging.h"
28 #include "common/macros.h"
30 #include "lib/lttng-ust-tracepoint/tracepoint.h"
31 #include "common/tracepoint.h"
32 #include "common/jhash.h"
33 #include "common/err-ptr.h"
35 /* Test compiler support for weak symbols with hidden visibility. */
36 int lttng_ust_tracepoint_test_symbol1
__attribute__((weak
, visibility("hidden")));
37 void *lttng_ust_tracepoint_test_symbol2
__attribute__((weak
, visibility("hidden")));
40 } lttng_ust_tracepoint_test_symbol3
__attribute__((weak
, visibility("hidden")));
42 /* Set to 1 to enable tracepoint debug output */
43 static const int tracepoint_debug
;
44 static int initialized
;
47 * If tracepoint_destructors_state = 1, tracepoint destructors are
48 * enabled. They are disabled otherwise.
50 static int tracepoint_destructors_state
= 1;
52 static void (*new_tracepoint_cb
)(struct lttng_ust_tracepoint
*);
55 * tracepoint_mutex nests inside UST mutex.
57 * Note about interaction with fork/clone: UST does not hold the
58 * tracepoint mutex across fork/clone because it is either:
59 * - nested within UST mutex, in which case holding the UST mutex across
61 * - taken by a library constructor, which should never race with a
62 * fork/clone if the application is expected to continue running with
63 * the same memory layout (no following exec()).
65 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
68 * libraries that contain tracepoints (struct tracepoint_lib).
69 * Protected by tracepoint mutex.
71 static CDS_LIST_HEAD(libs
);
74 * The tracepoint mutex protects the library tracepoints, the hash table, and
76 * All calls to the tracepoint API must be protected by the tracepoint mutex,
77 * excepts calls to lttng_ust_tracepoint_register_lib and
78 * lttng_ust_tracepoint_unregister_lib, which take the tracepoint mutex themselves.
82 * Tracepoint hash table, containing the active tracepoints.
83 * Protected by tracepoint mutex.
85 #define TRACEPOINT_HASH_BITS 12
86 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
87 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
89 static CDS_LIST_HEAD(old_probes
);
90 static int need_update
;
92 static CDS_LIST_HEAD(release_queue
);
93 static int release_queue_need_update
;
97 * It is used to to delay the free of multiple probes array until a quiescent
99 * Tracepoint entries modifications are protected by the tracepoint mutex.
101 struct tracepoint_entry
{
102 struct cds_hlist_node hlist
;
103 struct lttng_ust_tracepoint_probe
*probes
;
104 int refcount
; /* Number of times armed. 0 if disarmed. */
105 int callsite_refcount
; /* how many libs use this tracepoint */
113 struct cds_list_head list
;
114 /* Field below only used for call_rcu scheme */
115 /* struct rcu_head head; */
117 struct lttng_ust_tracepoint_probe probes
[0];
121 * Callsite hash table, containing the tracepoint call sites.
122 * Protected by tracepoint mutex.
124 #define CALLSITE_HASH_BITS 12
125 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
126 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
128 struct callsite_entry
{
129 struct cds_hlist_node hlist
; /* hash table node */
130 struct cds_list_head node
; /* lib list of callsites node */
131 struct lttng_ust_tracepoint
*tp
;
132 bool tp_entry_callsite_ref
; /* Has a tp_entry took a ref on this callsite */
135 lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
== LTTNG_UST_ABI_SYM_NAME_LEN
,
136 "Tracepoint name max length mismatch between UST ABI and tracepoint API",
137 Tracepoint_name_max_length_mismatch
);
140 bool lttng_ust_tp_validate_event_name(const struct lttng_ust_tracepoint
*tp
)
142 if (strlen(tp
->provider_name
) + 1 +
143 strlen(tp
->event_name
) >= LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
)
148 /* coverity[+alloc] */
149 static void *allocate_probes(int count
)
151 struct tp_probes
*p
=
152 zmalloc(count
* sizeof(struct lttng_ust_tracepoint_probe
)
153 + sizeof(struct tp_probes
));
154 return p
== NULL
? NULL
: p
->probes
;
157 /* coverity[+free : arg-0] */
158 static void release_probes(void *old
)
161 struct tp_probes
*tp_probes
= caa_container_of(old
,
162 struct tp_probes
, probes
[0]);
163 lttng_ust_urcu_synchronize_rcu();
168 static void debug_print_probes(struct tracepoint_entry
*entry
)
172 if (!tracepoint_debug
|| !entry
->probes
)
175 for (i
= 0; entry
->probes
[i
].func
; i
++)
176 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
180 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
181 void (*probe
)(void), void *data
)
184 struct lttng_ust_tracepoint_probe
*old
, *new;
188 return ERR_PTR(-EINVAL
);
190 debug_print_probes(entry
);
193 /* (N -> N+1), (N != 0, 1) probes */
194 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
195 if (old
[nr_probes
].func
== probe
&&
196 old
[nr_probes
].data
== data
)
197 return ERR_PTR(-EEXIST
);
199 /* + 2 : one for new probe, one for NULL func */
200 new = allocate_probes(nr_probes
+ 2);
202 return ERR_PTR(-ENOMEM
);
205 nr_probes
* sizeof(struct lttng_ust_tracepoint_probe
));
206 new[nr_probes
].func
= probe
;
207 new[nr_probes
].data
= data
;
208 new[nr_probes
+ 1].func
= NULL
;
209 entry
->refcount
= nr_probes
+ 1;
211 debug_print_probes(entry
);
216 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
217 void (*probe
)(void), void *data
)
219 int nr_probes
= 0, nr_del
= 0, i
;
220 struct lttng_ust_tracepoint_probe
*old
, *new;
225 return ERR_PTR(-ENOENT
);
227 debug_print_probes(entry
);
228 /* (N -> M), (N > 1, M >= 0) probes */
230 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
231 if (old
[nr_probes
].func
== probe
&&
232 old
[nr_probes
].data
== data
)
237 if (nr_probes
- nr_del
== 0) {
238 /* N -> 0, (N > 1) */
239 entry
->probes
= NULL
;
241 debug_print_probes(entry
);
245 /* N -> M, (N > 1, M > 0) */
247 new = allocate_probes(nr_probes
- nr_del
+ 1);
249 return ERR_PTR(-ENOMEM
);
250 for (i
= 0; old
[i
].func
; i
++)
251 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
253 new[nr_probes
- nr_del
].func
= NULL
;
254 entry
->refcount
= nr_probes
- nr_del
;
257 debug_print_probes(entry
);
262 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
263 * Must be called with tracepoint mutex held.
264 * Returns NULL if not present.
266 static struct tracepoint_entry
*get_tracepoint(const char *provider_name
, const char *event_name
)
268 struct cds_hlist_head
*head
;
269 struct cds_hlist_node
*node
;
270 struct tracepoint_entry
*e
;
273 hash
= jhash(provider_name
, strlen(provider_name
), 0) ^
274 jhash(event_name
, strlen(event_name
), 0);
275 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
276 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
277 if (!strcmp(event_name
, e
->event_name
) && !strcmp(provider_name
, e
->provider_name
))
284 * Add the tracepoint to the tracepoint hash table. Must be called with
285 * tracepoint mutex held.
287 static struct tracepoint_entry
*add_tracepoint(const char *provider_name
, const char *event_name
,
288 const char *signature
)
290 struct cds_hlist_head
*head
;
291 struct cds_hlist_node
*node
;
292 struct tracepoint_entry
*e
;
293 size_t sig_len
= strlen(signature
);
294 size_t sig_off
, provider_name_off
, event_name_off
;
295 size_t provider_name_len
= strlen(provider_name
);
296 size_t event_name_len
= strlen(event_name
);
299 hash
= jhash(provider_name
, provider_name_len
, 0) ^
300 jhash(event_name
, event_name_len
, 0);
301 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
302 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
303 if (!strcmp(event_name
, e
->event_name
) && !strcmp(provider_name
, e
->provider_name
)) {
304 DBG("tracepoint \"%s:%s\" busy", provider_name
, event_name
);
305 return ERR_PTR(-EEXIST
); /* Already there */
310 * Using zmalloc here to allocate a variable length elements: name and
311 * signature. Could cause some memory fragmentation if overused.
313 provider_name_off
= sizeof(struct tracepoint_entry
);
314 event_name_off
= provider_name_off
+ provider_name_len
+ 1;
315 sig_off
= event_name_off
+ event_name_len
+ 1;
317 e
= zmalloc(sizeof(struct tracepoint_entry
) + provider_name_len
+ 1 +
318 event_name_len
+ 1 + sig_len
+ 1);
320 return ERR_PTR(-ENOMEM
);
321 e
->provider_name
= (char *) e
+ provider_name_off
;
322 memcpy(e
->provider_name
, provider_name
, provider_name_len
+ 1);
323 e
->provider_name
[provider_name_len
] = '\0';
325 e
->event_name
= (char *) e
+ event_name_off
;
326 memcpy(e
->event_name
, event_name
, event_name_len
+ 1);
327 e
->event_name
[event_name_len
] = '\0';
329 e
->signature
= (char *) e
+ sig_off
;
330 memcpy(e
->signature
, signature
, sig_len
+ 1);
331 e
->signature
[sig_len
] = '\0';
335 e
->callsite_refcount
= 0;
337 cds_hlist_add_head(&e
->hlist
, head
);
342 * Remove the tracepoint from the tracepoint hash table. Must be called with
343 * tracepoint mutex held.
345 static void remove_tracepoint(struct tracepoint_entry
*e
)
347 cds_hlist_del(&e
->hlist
);
352 * Sets the probe callback corresponding to one tracepoint.
354 static void set_tracepoint(struct tracepoint_entry
**entry
,
355 struct lttng_ust_tracepoint
*elem
, int active
)
357 WARN_ON(strcmp((*entry
)->provider_name
, elem
->provider_name
) != 0);
358 WARN_ON(strcmp((*entry
)->event_name
, elem
->event_name
) != 0);
360 * Check that signatures match before connecting a probe to a
361 * tracepoint. Warn the user if they don't.
363 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
364 static int warned
= 0;
366 /* Only print once, don't flood console. */
368 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
369 WARN("Tracepoint \"%s:%s\" signatures: call: \"%s\" vs probe: \"%s\".",
370 elem
->provider_name
, elem
->event_name
, elem
->signature
, (*entry
)->signature
);
373 /* Don't accept connecting non-matching signatures. */
378 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
379 * probe callbacks array is consistent before setting a pointer to it.
380 * This array is referenced by __DO_TRACE from
381 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
384 lttng_ust_rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
385 CMM_STORE_SHARED(elem
->state
, active
);
389 * Disable a tracepoint and its probe callback.
390 * Note: only waiting an RCU period after setting elem->call to the empty
391 * function insures that the original callback is not used anymore. This insured
392 * by preempt_disable around the call site.
394 static void disable_tracepoint(struct lttng_ust_tracepoint
*elem
)
396 CMM_STORE_SHARED(elem
->state
, 0);
397 lttng_ust_rcu_assign_pointer(elem
->probes
, NULL
);
401 * Add the callsite to the callsite hash table. Must be called with
402 * tracepoint mutex held.
404 static void add_callsite(struct tracepoint_lib
* lib
, struct lttng_ust_tracepoint
*tp
)
406 struct cds_hlist_head
*head
;
407 struct callsite_entry
*e
;
409 struct tracepoint_entry
*tp_entry
;
411 if (!lttng_ust_tp_validate_event_name(tp
)) {
412 WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars",
413 tp
->provider_name
, tp
->event_name
, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
- 1);
416 hash
= jhash(tp
->provider_name
, strlen(tp
->provider_name
), 0) ^
417 jhash(tp
->event_name
, strlen(tp
->event_name
), 0);
418 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
419 e
= zmalloc(sizeof(struct callsite_entry
));
421 PERROR("Unable to add callsite for tracepoint \"%s:%s\"", tp
->provider_name
, tp
->event_name
);
424 cds_hlist_add_head(&e
->hlist
, head
);
426 cds_list_add(&e
->node
, &lib
->callsites
);
428 tp_entry
= get_tracepoint(tp
->provider_name
, tp
->event_name
);
431 tp_entry
->callsite_refcount
++;
432 e
->tp_entry_callsite_ref
= true;
436 * Remove the callsite from the callsite hash table and from lib
437 * callsite list. Must be called with tracepoint mutex held.
439 static void remove_callsite(struct callsite_entry
*e
)
441 struct tracepoint_entry
*tp_entry
;
443 tp_entry
= get_tracepoint(e
->tp
->provider_name
, e
->tp
->event_name
);
445 if (e
->tp_entry_callsite_ref
)
446 tp_entry
->callsite_refcount
--;
447 if (tp_entry
->callsite_refcount
== 0)
448 disable_tracepoint(e
->tp
);
450 cds_hlist_del(&e
->hlist
);
451 cds_list_del(&e
->node
);
456 * Enable/disable all callsites based on the state of a specific
458 * Must be called with tracepoint mutex held.
460 static void tracepoint_sync_callsites(const char *provider_name
, const char *event_name
)
462 struct tracepoint_entry
*tp_entry
;
463 struct cds_hlist_head
*head
;
464 struct cds_hlist_node
*node
;
465 struct callsite_entry
*e
;
468 tp_entry
= get_tracepoint(provider_name
, event_name
);
469 hash
= jhash(provider_name
, strlen(provider_name
), 0) ^
470 jhash(event_name
, strlen(event_name
), 0);
471 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
472 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
473 struct lttng_ust_tracepoint
*tp
= e
->tp
;
475 if (strcmp(event_name
, tp
->event_name
))
477 if (strcmp(provider_name
, tp
->provider_name
))
480 if (!e
->tp_entry_callsite_ref
) {
481 tp_entry
->callsite_refcount
++;
482 e
->tp_entry_callsite_ref
= true;
484 set_tracepoint(&tp_entry
, tp
,
485 !!tp_entry
->refcount
);
487 disable_tracepoint(tp
);
488 e
->tp_entry_callsite_ref
= false;
494 * tracepoint_update_probe_range - Update a probe range
495 * @begin: beginning of the range
496 * @end: end of the range
498 * Updates the probe callback corresponding to a range of tracepoints.
501 void tracepoint_update_probe_range(struct lttng_ust_tracepoint
* const *begin
,
502 struct lttng_ust_tracepoint
* const *end
)
504 struct lttng_ust_tracepoint
* const *iter
;
505 struct tracepoint_entry
*mark_entry
;
507 for (iter
= begin
; iter
< end
; iter
++) {
509 continue; /* skip dummy */
510 if (!(*iter
)->provider_name
|| !(*iter
)->event_name
) {
511 disable_tracepoint(*iter
);
514 if (!lttng_ust_tp_validate_event_name(*iter
)) {
515 WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars",
516 (*iter
)->provider_name
, (*iter
)->event_name
, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX
- 1);
517 disable_tracepoint(*iter
);
520 mark_entry
= get_tracepoint((*iter
)->provider_name
, (*iter
)->event_name
);
522 set_tracepoint(&mark_entry
, *iter
,
523 !!mark_entry
->refcount
);
525 disable_tracepoint(*iter
);
530 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
532 tracepoint_update_probe_range(lib
->tracepoints_start
,
533 lib
->tracepoints_start
+ lib
->tracepoints_count
);
536 static void lib_register_callsites(struct tracepoint_lib
*lib
)
538 struct lttng_ust_tracepoint
* const *begin
;
539 struct lttng_ust_tracepoint
* const *end
;
540 struct lttng_ust_tracepoint
* const *iter
;
542 begin
= lib
->tracepoints_start
;
543 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
545 for (iter
= begin
; iter
< end
; iter
++) {
547 continue; /* skip dummy */
548 if (!(*iter
)->provider_name
|| !(*iter
)->event_name
) {
551 add_callsite(lib
, *iter
);
555 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
557 struct callsite_entry
*callsite
, *tmp
;
559 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
560 remove_callsite(callsite
);
564 * Update probes, removing the faulty probes.
566 static void tracepoint_update_probes(void)
568 struct tracepoint_lib
*lib
;
570 /* tracepoints registered from libraries and executable. */
571 cds_list_for_each_entry(lib
, &libs
, list
)
572 lib_update_tracepoints(lib
);
575 static struct lttng_ust_tracepoint_probe
*
576 tracepoint_add_probe(const char *provider_name
, const char *event_name
,
577 void (*probe
)(void), void *data
, const char *signature
)
579 struct tracepoint_entry
*entry
;
580 struct lttng_ust_tracepoint_probe
*old
;
582 entry
= get_tracepoint(provider_name
, event_name
);
584 if (strcmp(entry
->signature
, signature
) != 0) {
585 ERR("Tracepoint \"%s:%s\": tracepoint and probe signature do not match: \"%s\" vs \"%s\".",
586 provider_name
, event_name
, entry
->signature
, signature
);
587 return ERR_PTR(-EINVAL
);
590 entry
= add_tracepoint(provider_name
, event_name
, signature
);
592 return (struct lttng_ust_tracepoint_probe
*)entry
;
594 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
595 if (IS_ERR(old
) && !entry
->refcount
)
596 remove_tracepoint(entry
);
600 static void tracepoint_release_queue_add_old_probes(void *old
)
602 release_queue_need_update
= 1;
604 struct tp_probes
*tp_probes
= caa_container_of(old
,
605 struct tp_probes
, probes
[0]);
606 cds_list_add(&tp_probes
->u
.list
, &release_queue
);
611 * lttng_ust_tracepoint_provider_register - Connect a probe to a tracepoint
612 * @name: tracepoint provider name
613 * @name: tracepoint event name
614 * @probe: probe handler
616 * Returns 0 if ok, error value on error.
617 * The probe address must at least be aligned on the architecture pointer size.
618 * Called with the tracepoint mutex held.
620 int lttng_ust_tracepoint_provider_register(const char *provider_name
, const char *event_name
,
621 void (*probe
)(void), void *data
, const char *signature
)
626 DBG("Registering probe to tracepoint \"%s:%s\"", provider_name
, event_name
);
628 pthread_mutex_lock(&tracepoint_mutex
);
629 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
635 tracepoint_sync_callsites(provider_name
, event_name
);
638 pthread_mutex_unlock(&tracepoint_mutex
);
643 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
644 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
645 * times to ensure it does not leak memory.
647 int lttng_ust_tp_probe_register_queue_release(const char *provider_name
, const char *event_name
,
648 void (*probe
)(void), void *data
, const char *signature
)
653 DBG("Registering probe to tracepoint \"%s:%s\". Queuing release.", provider_name
, event_name
);
655 pthread_mutex_lock(&tracepoint_mutex
);
656 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
662 tracepoint_sync_callsites(provider_name
, event_name
);
663 tracepoint_release_queue_add_old_probes(old
);
665 pthread_mutex_unlock(&tracepoint_mutex
);
669 static void *tracepoint_remove_probe(const char *provider_name
, const char *event_name
,
670 void (*probe
)(void), void *data
)
672 struct tracepoint_entry
*entry
;
675 entry
= get_tracepoint(provider_name
, event_name
);
677 return ERR_PTR(-ENOENT
);
678 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
681 if (!entry
->refcount
)
682 remove_tracepoint(entry
);
687 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
688 * @provider_name: tracepoint provider name
689 * @event_name: tracepoint event name
690 * @probe: probe function pointer
691 * @probe: probe data pointer
693 int lttng_ust_tracepoint_provider_unregister(const char *provider_name
, const char *event_name
,
694 void (*probe
)(void), void *data
)
699 DBG("Un-registering probe \"%s:%s\" from tracepoint %p", provider_name
, event_name
, probe
);
701 pthread_mutex_lock(&tracepoint_mutex
);
702 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
707 tracepoint_sync_callsites(provider_name
, event_name
);
710 pthread_mutex_unlock(&tracepoint_mutex
);
715 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
716 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
717 * times to ensure it does not leak memory.
719 int lttng_ust_tp_probe_unregister_queue_release(const char *provider_name
, const char *event_name
,
720 void (*probe
)(void), void *data
)
725 DBG("Un-registering probe from tracepoint \"%s:%s\". Queuing release.", provider_name
, event_name
);
727 pthread_mutex_lock(&tracepoint_mutex
);
728 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
733 tracepoint_sync_callsites(provider_name
, event_name
);
734 tracepoint_release_queue_add_old_probes(old
);
736 pthread_mutex_unlock(&tracepoint_mutex
);
740 void lttng_ust_tp_probe_prune_release_queue(void)
742 CDS_LIST_HEAD(release_probes
);
743 struct tp_probes
*pos
, *next
;
745 DBG("Release queue of unregistered tracepoint probes.");
747 pthread_mutex_lock(&tracepoint_mutex
);
748 if (!release_queue_need_update
)
750 if (!cds_list_empty(&release_queue
))
751 cds_list_replace_init(&release_queue
, &release_probes
);
752 release_queue_need_update
= 0;
754 /* Wait for grace period between all sync_callsites and free. */
755 lttng_ust_urcu_synchronize_rcu();
757 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
758 cds_list_del(&pos
->u
.list
);
762 pthread_mutex_unlock(&tracepoint_mutex
);
765 static void tracepoint_add_old_probes(void *old
)
769 struct tp_probes
*tp_probes
= caa_container_of(old
,
770 struct tp_probes
, probes
[0]);
771 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
776 * tracepoint_probe_register_noupdate - register a probe but not connect
777 * @name: tracepoint name
778 * @probe: probe handler
780 * caller must call tracepoint_probe_update_all()
782 int tracepoint_probe_register_noupdate(const char *provider_name
, const char *event_name
,
783 void (*probe
)(void), void *data
, const char *signature
)
788 pthread_mutex_lock(&tracepoint_mutex
);
789 old
= tracepoint_add_probe(provider_name
, event_name
, probe
, data
, signature
);
794 tracepoint_add_old_probes(old
);
796 pthread_mutex_unlock(&tracepoint_mutex
);
801 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
802 * @name: tracepoint name
803 * @probe: probe function pointer
805 * caller must call tracepoint_probe_update_all()
806 * Called with the tracepoint mutex held.
808 int tracepoint_probe_unregister_noupdate(const char *provider_name
, const char *event_name
,
809 void (*probe
)(void), void *data
)
814 DBG("Un-registering probe from tracepoint \"%s:%s\"", provider_name
, event_name
);
816 pthread_mutex_lock(&tracepoint_mutex
);
817 old
= tracepoint_remove_probe(provider_name
, event_name
, probe
, data
);
822 tracepoint_add_old_probes(old
);
824 pthread_mutex_unlock(&tracepoint_mutex
);
829 * tracepoint_probe_update_all - update tracepoints
831 void tracepoint_probe_update_all(void)
833 CDS_LIST_HEAD(release_probes
);
834 struct tp_probes
*pos
, *next
;
836 pthread_mutex_lock(&tracepoint_mutex
);
840 if (!cds_list_empty(&old_probes
))
841 cds_list_replace_init(&old_probes
, &release_probes
);
844 tracepoint_update_probes();
845 /* Wait for grace period between update_probes and free. */
846 lttng_ust_urcu_synchronize_rcu();
847 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
848 cds_list_del(&pos
->u
.list
);
852 pthread_mutex_unlock(&tracepoint_mutex
);
855 static void new_tracepoints(struct lttng_ust_tracepoint
* const *start
,
856 struct lttng_ust_tracepoint
* const *end
)
858 if (new_tracepoint_cb
) {
859 struct lttng_ust_tracepoint
* const *t
;
861 for (t
= start
; t
< end
; t
++) {
863 new_tracepoint_cb(*t
);
869 * tracepoint_{un,}register_lib is meant to be looked up by instrumented
870 * applications through dlsym(). If found, those can register their
871 * tracepoints, else those tracepoints will not be available for
872 * tracing. The number at the end of those symbols acts as a major
873 * version for tracepoints.
875 * Older instrumented applications should still work with newer
876 * liblttng-ust, but it is fine that instrumented applications compiled
877 * against recent liblttng-ust headers require a recent liblttng-ust
878 * runtime for those tracepoints to be taken into account.
880 int lttng_ust_tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
881 int tracepoints_count
);
882 int lttng_ust_tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
883 int tracepoints_count
)
885 struct tracepoint_lib
*pl
, *iter
;
889 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
891 PERROR("Unable to register tracepoint lib");
894 pl
->tracepoints_start
= tracepoints_start
;
895 pl
->tracepoints_count
= tracepoints_count
;
896 CDS_INIT_LIST_HEAD(&pl
->callsites
);
898 pthread_mutex_lock(&tracepoint_mutex
);
900 * We sort the libs by struct lib pointer address.
902 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
903 BUG_ON(iter
== pl
); /* Should never be in the list twice */
905 /* We belong to the location right after iter. */
906 cds_list_add(&pl
->list
, &iter
->list
);
910 /* We should be added at the head of the list */
911 cds_list_add(&pl
->list
, &libs
);
913 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
914 lib_register_callsites(pl
);
915 lib_update_tracepoints(pl
);
916 pthread_mutex_unlock(&tracepoint_mutex
);
918 DBG("just registered a tracepoints section from %p and having %d tracepoints",
919 tracepoints_start
, tracepoints_count
);
920 if (lttng_ust_logging_debug_enabled()) {
923 for (i
= 0; i
< tracepoints_count
; i
++) {
924 if (!lttng_ust_tp_validate_event_name(tracepoints_start
[i
]))
926 DBG("registered tracepoint: \"%s:%s\"",
927 tracepoints_start
[i
]->provider_name
,
928 tracepoints_start
[i
]->event_name
);
935 int lttng_ust_tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
);
936 int lttng_ust_tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
)
938 struct tracepoint_lib
*lib
;
940 pthread_mutex_lock(&tracepoint_mutex
);
941 cds_list_for_each_entry(lib
, &libs
, list
) {
942 if (lib
->tracepoints_start
!= tracepoints_start
)
945 cds_list_del(&lib
->list
);
947 * Unregistering a callsite also decreases the
948 * callsite reference count of the corresponding
949 * tracepoint, and disables the tracepoint if
950 * the reference count drops to zero.
952 lib_unregister_callsites(lib
);
953 DBG("just unregistered a tracepoints section from %p",
954 lib
->tracepoints_start
);
958 pthread_mutex_unlock(&tracepoint_mutex
);
963 * Report in debug message whether the compiler correctly supports weak
964 * hidden symbols. This test checks that the address associated with two
965 * weak symbols with hidden visibility is the same when declared within
966 * two compile units part of the same module.
968 static void check_weak_hidden(void)
970 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
971 <tng_ust_tracepoint_test_symbol1
== lttng_ust_tp_check_weak_hidden1() ?
973 "DIFFERENT addresses");
974 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
975 <tng_ust_tracepoint_test_symbol2
== lttng_ust_tp_check_weak_hidden2() ?
977 "DIFFERENT addresses");
978 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
979 <tng_ust_tracepoint_test_symbol3
== lttng_ust_tp_check_weak_hidden3() ?
981 "DIFFERENT addresses");
984 void lttng_ust_tp_init(void)
986 if (uatomic_xchg(&initialized
, 1) == 1)
988 lttng_ust_logging_init();
989 lttng_ust_common_ctor();
993 void lttng_ust_tp_exit(void)
999 * Create the wrapper symbols.
1001 #undef lttng_ust_tp_rcu_read_lock
1002 #undef lttng_ust_tp_rcu_read_unlock
1003 #undef lttng_ust_tp_rcu_dereference
1005 void lttng_ust_tp_rcu_read_lock(void);
1006 void lttng_ust_tp_rcu_read_lock(void)
1008 lttng_ust_urcu_read_lock();
1011 void lttng_ust_tp_rcu_read_unlock(void);
1012 void lttng_ust_tp_rcu_read_unlock(void)
1014 lttng_ust_urcu_read_unlock();
1017 void *lttng_ust_tp_rcu_dereference_sym(void *p
);
1018 void *lttng_ust_tp_rcu_dereference_sym(void *p
)
1020 return lttng_ust_rcu_dereference(p
);
1024 * Programs that have threads that survive after they exit, and therefore call
1025 * library destructors, should disable the tracepoint destructors by calling
1026 * lttng_ust_tp_disable_destructors(). This will leak the tracepoint
1027 * instrumentation library shared object, leaving its teardown to the operating
1028 * system process teardown.
1030 * To access and/or modify this value, users need to use a combination of
1031 * dlopen(3) and dlsym(3) to get an handle on the
1032 * lttng_ust_tp_disable_destructors and lttng_ust_tp_get_destructors_state symbols below.
1034 void lttng_ust_tp_disable_destructors(void);
1035 void lttng_ust_tp_disable_destructors(void)
1037 uatomic_set(&tracepoint_destructors_state
, 0);
1041 * Returns 1 if the destructors are enabled and should be executed.
1042 * Returns 0 if the destructors are disabled.
1044 int lttng_ust_tp_get_destructors_state(void);
1045 int lttng_ust_tp_get_destructors_state(void)
1047 return uatomic_read(&tracepoint_destructors_state
);