2 * Copyright (C) 2008-2011 Mathieu Desnoyers
3 * Copyright (C) 2009 Pierre-Marc Fournier
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Ported to userspace by Pierre-Marc Fournier.
28 #include <urcu/arch.h>
30 #include <urcu/hlist.h>
31 #include <urcu/uatomic.h>
32 #include <urcu/compiler.h>
34 #include <lttng/tracepoint.h>
35 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
37 #include <usterr-signal-safe.h>
40 #include "tracepoint-internal.h"
41 #include "lttng-tracer-core.h"
45 /* Set to 1 to enable tracepoint debug output */
46 static const int tracepoint_debug
;
47 static int initialized
;
48 static void (*new_tracepoint_cb
)(struct lttng_ust_tracepoint
*);
51 * tracepoint_mutex nests inside UST mutex.
53 * Note about interaction with fork/clone: UST does not hold the
54 * tracepoint mutex across fork/clone because it is either:
55 * - nested within UST mutex, in which case holding the UST mutex across
57 * - taken by a library constructor, which should never race with a
58 * fork/clone if the application is expected to continue running with
59 * the same memory layout (no following exec()).
61 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
64 * libraries that contain tracepoints (struct tracepoint_lib).
65 * Protected by tracepoint mutex.
67 static CDS_LIST_HEAD(libs
);
70 * The tracepoint mutex protects the library tracepoints, the hash table, and
72 * All calls to the tracepoint API must be protected by the tracepoint mutex,
73 * excepts calls to tracepoint_register_lib and
74 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
78 * Tracepoint hash table, containing the active tracepoints.
79 * Protected by tracepoint mutex.
81 #define TRACEPOINT_HASH_BITS 12
82 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
83 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
85 static CDS_LIST_HEAD(old_probes
);
86 static int need_update
;
90 * It is used to to delay the free of multiple probes array until a quiescent
92 * Tracepoint entries modifications are protected by the tracepoint mutex.
94 struct tracepoint_entry
{
95 struct cds_hlist_node hlist
;
96 struct lttng_ust_tracepoint_probe
*probes
;
97 int refcount
; /* Number of times armed. 0 if disarmed. */
98 int callsite_refcount
; /* how many libs use this tracepoint */
99 const char *signature
;
105 struct cds_list_head list
;
106 /* Field below only used for call_rcu scheme */
107 /* struct rcu_head head; */
109 struct lttng_ust_tracepoint_probe probes
[0];
113 * Callsite hash table, containing the tracepoint call sites.
114 * Protected by tracepoint mutex.
116 #define CALLSITE_HASH_BITS 12
117 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
118 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
120 struct callsite_entry
{
121 struct cds_hlist_node hlist
; /* hash table node */
122 struct cds_list_head node
; /* lib list of callsites node */
123 struct lttng_ust_tracepoint
*tp
;
126 /* coverity[+alloc] */
127 static void *allocate_probes(int count
)
129 struct tp_probes
*p
=
130 zmalloc(count
* sizeof(struct lttng_ust_tracepoint_probe
)
131 + sizeof(struct tp_probes
));
132 return p
== NULL
? NULL
: p
->probes
;
135 /* coverity[+free : arg-0] */
136 static void release_probes(void *old
)
139 struct tp_probes
*tp_probes
= caa_container_of(old
,
140 struct tp_probes
, probes
[0]);
146 static void debug_print_probes(struct tracepoint_entry
*entry
)
150 if (!tracepoint_debug
|| !entry
->probes
)
153 for (i
= 0; entry
->probes
[i
].func
; i
++)
154 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
158 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
159 void (*probe
)(void), void *data
)
162 struct lttng_ust_tracepoint_probe
*old
, *new;
166 return ERR_PTR(-EINVAL
);
168 debug_print_probes(entry
);
171 /* (N -> N+1), (N != 0, 1) probes */
172 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
173 if (old
[nr_probes
].func
== probe
&&
174 old
[nr_probes
].data
== data
)
175 return ERR_PTR(-EEXIST
);
177 /* + 2 : one for new probe, one for NULL func */
178 new = allocate_probes(nr_probes
+ 2);
180 return ERR_PTR(-ENOMEM
);
183 nr_probes
* sizeof(struct lttng_ust_tracepoint_probe
));
184 new[nr_probes
].func
= probe
;
185 new[nr_probes
].data
= data
;
186 new[nr_probes
+ 1].func
= NULL
;
187 entry
->refcount
= nr_probes
+ 1;
189 debug_print_probes(entry
);
194 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
195 void (*probe
)(void), void *data
)
197 int nr_probes
= 0, nr_del
= 0, i
;
198 struct lttng_ust_tracepoint_probe
*old
, *new;
203 return ERR_PTR(-ENOENT
);
205 debug_print_probes(entry
);
206 /* (N -> M), (N > 1, M >= 0) probes */
208 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
209 if (old
[nr_probes
].func
== probe
&&
210 old
[nr_probes
].data
== data
)
215 if (nr_probes
- nr_del
== 0) {
216 /* N -> 0, (N > 1) */
217 entry
->probes
= NULL
;
219 debug_print_probes(entry
);
223 /* N -> M, (N > 1, M > 0) */
225 new = allocate_probes(nr_probes
- nr_del
+ 1);
227 return ERR_PTR(-ENOMEM
);
228 for (i
= 0; old
[i
].func
; i
++)
229 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
231 new[nr_probes
- nr_del
].func
= NULL
;
232 entry
->refcount
= nr_probes
- nr_del
;
235 debug_print_probes(entry
);
240 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
241 * Must be called with tracepoint mutex held.
242 * Returns NULL if not present.
244 static struct tracepoint_entry
*get_tracepoint(const char *name
)
246 struct cds_hlist_head
*head
;
247 struct cds_hlist_node
*node
;
248 struct tracepoint_entry
*e
;
249 size_t name_len
= strlen(name
);
252 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
253 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
254 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
256 hash
= jhash(name
, name_len
, 0);
257 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
258 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
259 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
266 * Add the tracepoint to the tracepoint hash table. Must be called with
267 * tracepoint mutex held.
269 static struct tracepoint_entry
*add_tracepoint(const char *name
,
270 const char *signature
)
272 struct cds_hlist_head
*head
;
273 struct cds_hlist_node
*node
;
274 struct tracepoint_entry
*e
;
275 size_t name_len
= strlen(name
);
278 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
279 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
280 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
282 hash
= jhash(name
, name_len
, 0);
283 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
284 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
285 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
286 DBG("tracepoint %s busy", name
);
287 return ERR_PTR(-EEXIST
); /* Already there */
291 * Using zmalloc here to allocate a variable length element. Could
292 * cause some memory fragmentation if overused.
294 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
296 return ERR_PTR(-ENOMEM
);
297 memcpy(&e
->name
[0], name
, name_len
+ 1);
298 e
->name
[name_len
] = '\0';
301 e
->callsite_refcount
= 0;
302 e
->signature
= signature
;
303 cds_hlist_add_head(&e
->hlist
, head
);
308 * Remove the tracepoint from the tracepoint hash table. Must be called with
309 * tracepoint mutex held.
311 static void remove_tracepoint(struct tracepoint_entry
*e
)
313 cds_hlist_del(&e
->hlist
);
318 * Sets the probe callback corresponding to one tracepoint.
320 static void set_tracepoint(struct tracepoint_entry
**entry
,
321 struct lttng_ust_tracepoint
*elem
, int active
)
323 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
325 * Check that signatures match before connecting a probe to a
326 * tracepoint. Warn the user if they don't.
328 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
329 static int warned
= 0;
331 /* Only print once, don't flood console. */
333 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
334 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
335 elem
->name
, elem
->signature
, (*entry
)->signature
);
338 /* Don't accept connecting non-matching signatures. */
343 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
344 * probe callbacks array is consistent before setting a pointer to it.
345 * This array is referenced by __DO_TRACE from
346 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
349 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
350 elem
->state
= active
;
354 * Disable a tracepoint and its probe callback.
355 * Note: only waiting an RCU period after setting elem->call to the empty
356 * function insures that the original callback is not used anymore. This insured
357 * by preempt_disable around the call site.
359 static void disable_tracepoint(struct lttng_ust_tracepoint
*elem
)
362 rcu_assign_pointer(elem
->probes
, NULL
);
366 * Add the callsite to the callsite hash table. Must be called with
367 * tracepoint mutex held.
369 static void add_callsite(struct tracepoint_lib
* lib
, struct lttng_ust_tracepoint
*tp
)
371 struct cds_hlist_head
*head
;
372 struct callsite_entry
*e
;
373 const char *name
= tp
->name
;
374 size_t name_len
= strlen(name
);
376 struct tracepoint_entry
*tp_entry
;
378 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
379 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
380 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
382 hash
= jhash(name
, name_len
, 0);
383 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
384 e
= zmalloc(sizeof(struct callsite_entry
));
386 PERROR("Unable to add callsite for tracepoint \"%s\"", name
);
389 cds_hlist_add_head(&e
->hlist
, head
);
391 cds_list_add(&e
->node
, &lib
->callsites
);
393 tp_entry
= get_tracepoint(name
);
396 tp_entry
->callsite_refcount
++;
400 * Remove the callsite from the callsite hash table and from lib
401 * callsite list. Must be called with tracepoint mutex held.
403 static void remove_callsite(struct callsite_entry
*e
)
405 struct tracepoint_entry
*tp_entry
;
407 tp_entry
= get_tracepoint(e
->tp
->name
);
409 tp_entry
->callsite_refcount
--;
410 if (tp_entry
->callsite_refcount
== 0)
411 disable_tracepoint(e
->tp
);
413 cds_hlist_del(&e
->hlist
);
414 cds_list_del(&e
->node
);
419 * Enable/disable all callsites based on the state of a specific
421 * Must be called with tracepoint mutex held.
423 static void tracepoint_sync_callsites(const char *name
)
425 struct cds_hlist_head
*head
;
426 struct cds_hlist_node
*node
;
427 struct callsite_entry
*e
;
428 size_t name_len
= strlen(name
);
430 struct tracepoint_entry
*tp_entry
;
432 tp_entry
= get_tracepoint(name
);
433 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
434 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
435 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
437 hash
= jhash(name
, name_len
, 0);
438 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
439 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
440 struct lttng_ust_tracepoint
*tp
= e
->tp
;
442 if (strncmp(name
, tp
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
445 set_tracepoint(&tp_entry
, tp
,
446 !!tp_entry
->refcount
);
448 disable_tracepoint(tp
);
454 * tracepoint_update_probe_range - Update a probe range
455 * @begin: beginning of the range
456 * @end: end of the range
458 * Updates the probe callback corresponding to a range of tracepoints.
461 void tracepoint_update_probe_range(struct lttng_ust_tracepoint
* const *begin
,
462 struct lttng_ust_tracepoint
* const *end
)
464 struct lttng_ust_tracepoint
* const *iter
;
465 struct tracepoint_entry
*mark_entry
;
467 for (iter
= begin
; iter
< end
; iter
++) {
469 continue; /* skip dummy */
470 if (!(*iter
)->name
) {
471 disable_tracepoint(*iter
);
474 mark_entry
= get_tracepoint((*iter
)->name
);
476 set_tracepoint(&mark_entry
, *iter
,
477 !!mark_entry
->refcount
);
479 disable_tracepoint(*iter
);
484 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
486 tracepoint_update_probe_range(lib
->tracepoints_start
,
487 lib
->tracepoints_start
+ lib
->tracepoints_count
);
490 static void lib_register_callsites(struct tracepoint_lib
*lib
)
492 struct lttng_ust_tracepoint
* const *begin
;
493 struct lttng_ust_tracepoint
* const *end
;
494 struct lttng_ust_tracepoint
* const *iter
;
496 begin
= lib
->tracepoints_start
;
497 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
499 for (iter
= begin
; iter
< end
; iter
++) {
501 continue; /* skip dummy */
502 if (!(*iter
)->name
) {
505 add_callsite(lib
, *iter
);
509 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
511 struct callsite_entry
*callsite
, *tmp
;
513 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
514 remove_callsite(callsite
);
518 * Update probes, removing the faulty probes.
520 static void tracepoint_update_probes(void)
522 struct tracepoint_lib
*lib
;
524 /* tracepoints registered from libraries and executable. */
525 cds_list_for_each_entry(lib
, &libs
, list
)
526 lib_update_tracepoints(lib
);
529 static struct lttng_ust_tracepoint_probe
*
530 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
531 const char *signature
)
533 struct tracepoint_entry
*entry
;
534 struct lttng_ust_tracepoint_probe
*old
;
536 entry
= get_tracepoint(name
);
538 entry
= add_tracepoint(name
, signature
);
540 return (struct lttng_ust_tracepoint_probe
*)entry
;
542 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
543 if (IS_ERR(old
) && !entry
->refcount
)
544 remove_tracepoint(entry
);
549 * __tracepoint_probe_register - Connect a probe to a tracepoint
550 * @name: tracepoint name
551 * @probe: probe handler
553 * Returns 0 if ok, error value on error.
554 * The probe address must at least be aligned on the architecture pointer size.
555 * Called with the tracepoint mutex held.
557 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
558 void *data
, const char *signature
)
563 DBG("Registering probe to tracepoint %s", name
);
565 pthread_mutex_lock(&tracepoint_mutex
);
566 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
572 tracepoint_sync_callsites(name
);
575 pthread_mutex_unlock(&tracepoint_mutex
);
579 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
582 struct tracepoint_entry
*entry
;
585 entry
= get_tracepoint(name
);
587 return ERR_PTR(-ENOENT
);
588 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
591 if (!entry
->refcount
)
592 remove_tracepoint(entry
);
597 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
598 * @name: tracepoint name
599 * @probe: probe function pointer
600 * @probe: probe data pointer
602 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
608 DBG("Un-registering probe from tracepoint %s", name
);
610 pthread_mutex_lock(&tracepoint_mutex
);
611 old
= tracepoint_remove_probe(name
, probe
, data
);
616 tracepoint_sync_callsites(name
);
619 pthread_mutex_unlock(&tracepoint_mutex
);
623 static void tracepoint_add_old_probes(void *old
)
627 struct tp_probes
*tp_probes
= caa_container_of(old
,
628 struct tp_probes
, probes
[0]);
629 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
634 * tracepoint_probe_register_noupdate - register a probe but not connect
635 * @name: tracepoint name
636 * @probe: probe handler
638 * caller must call tracepoint_probe_update_all()
640 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
641 void *data
, const char *signature
)
646 pthread_mutex_lock(&tracepoint_mutex
);
647 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
652 tracepoint_add_old_probes(old
);
654 pthread_mutex_unlock(&tracepoint_mutex
);
659 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
660 * @name: tracepoint name
661 * @probe: probe function pointer
663 * caller must call tracepoint_probe_update_all()
664 * Called with the tracepoint mutex held.
666 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
672 DBG("Un-registering probe from tracepoint %s", name
);
674 pthread_mutex_lock(&tracepoint_mutex
);
675 old
= tracepoint_remove_probe(name
, probe
, data
);
680 tracepoint_add_old_probes(old
);
682 pthread_mutex_unlock(&tracepoint_mutex
);
687 * tracepoint_probe_update_all - update tracepoints
689 void tracepoint_probe_update_all(void)
691 CDS_LIST_HEAD(release_probes
);
692 struct tp_probes
*pos
, *next
;
694 pthread_mutex_lock(&tracepoint_mutex
);
698 if (!cds_list_empty(&old_probes
))
699 cds_list_replace_init(&old_probes
, &release_probes
);
702 tracepoint_update_probes();
703 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
704 cds_list_del(&pos
->u
.list
);
709 pthread_mutex_unlock(&tracepoint_mutex
);
712 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct lttng_ust_tracepoint
*))
714 new_tracepoint_cb
= cb
;
717 static void new_tracepoints(struct lttng_ust_tracepoint
* const *start
,
718 struct lttng_ust_tracepoint
* const *end
)
720 if (new_tracepoint_cb
) {
721 struct lttng_ust_tracepoint
* const *t
;
723 for (t
= start
; t
< end
; t
++) {
725 new_tracepoint_cb(*t
);
730 int tracepoint_register_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
,
731 int tracepoints_count
)
733 struct tracepoint_lib
*pl
, *iter
;
737 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
739 PERROR("Unable to register tracepoint lib");
742 pl
->tracepoints_start
= tracepoints_start
;
743 pl
->tracepoints_count
= tracepoints_count
;
744 CDS_INIT_LIST_HEAD(&pl
->callsites
);
746 pthread_mutex_lock(&tracepoint_mutex
);
748 * We sort the libs by struct lib pointer address.
750 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
751 BUG_ON(iter
== pl
); /* Should never be in the list twice */
753 /* We belong to the location right after iter. */
754 cds_list_add(&pl
->list
, &iter
->list
);
758 /* We should be added at the head of the list */
759 cds_list_add(&pl
->list
, &libs
);
761 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
762 lib_register_callsites(pl
);
763 lib_update_tracepoints(pl
);
764 pthread_mutex_unlock(&tracepoint_mutex
);
766 DBG("just registered a tracepoints section from %p and having %d tracepoints",
767 tracepoints_start
, tracepoints_count
);
771 for (i
= 0; i
< tracepoints_count
; i
++) {
772 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
779 int tracepoint_unregister_lib(struct lttng_ust_tracepoint
* const *tracepoints_start
)
781 struct tracepoint_lib
*lib
;
783 pthread_mutex_lock(&tracepoint_mutex
);
784 cds_list_for_each_entry(lib
, &libs
, list
) {
785 if (lib
->tracepoints_start
!= tracepoints_start
)
788 cds_list_del(&lib
->list
);
790 * Unregistering a callsite also decreases the
791 * callsite reference count of the corresponding
792 * tracepoint, and disables the tracepoint if
793 * the reference count drops to zero.
795 lib_unregister_callsites(lib
);
796 DBG("just unregistered a tracepoints section from %p",
797 lib
->tracepoints_start
);
801 pthread_mutex_unlock(&tracepoint_mutex
);
805 void init_tracepoint(void)
807 if (uatomic_xchg(&initialized
, 1) == 1)
812 void exit_tracepoint(void)
818 * Create the wrapper symbols.
820 #undef tp_rcu_read_lock_bp
821 #undef tp_rcu_read_unlock_bp
822 #undef tp_rcu_dereference_bp
824 void tp_rcu_read_lock_bp(void)
829 void tp_rcu_read_unlock_bp(void)
831 rcu_read_unlock_bp();
834 void *tp_rcu_dereference_sym_bp(void *p
)
836 return rcu_dereference_bp(p
);