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 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 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 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 tracepoint
*tp
;
126 /* coverity[+alloc] */
127 static void *allocate_probes(int count
)
129 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
130 + sizeof(struct tp_probes
));
131 return p
== NULL
? NULL
: p
->probes
;
134 /* coverity[+free : arg-0] */
135 static void release_probes(void *old
)
138 struct tp_probes
*tp_probes
= caa_container_of(old
,
139 struct tp_probes
, probes
[0]);
145 static void debug_print_probes(struct tracepoint_entry
*entry
)
149 if (!tracepoint_debug
|| !entry
->probes
)
152 for (i
= 0; entry
->probes
[i
].func
; i
++)
153 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
157 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
158 void (*probe
)(void), void *data
)
161 struct tracepoint_probe
*old
, *new;
165 return ERR_PTR(-EINVAL
);
167 debug_print_probes(entry
);
170 /* (N -> N+1), (N != 0, 1) probes */
171 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
172 if (old
[nr_probes
].func
== probe
&&
173 old
[nr_probes
].data
== data
)
174 return ERR_PTR(-EEXIST
);
176 /* + 2 : one for new probe, one for NULL func */
177 new = allocate_probes(nr_probes
+ 2);
179 return ERR_PTR(-ENOMEM
);
181 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
182 new[nr_probes
].func
= probe
;
183 new[nr_probes
].data
= data
;
184 new[nr_probes
+ 1].func
= NULL
;
185 entry
->refcount
= nr_probes
+ 1;
187 debug_print_probes(entry
);
192 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
193 void (*probe
)(void), void *data
)
195 int nr_probes
= 0, nr_del
= 0, i
;
196 struct tracepoint_probe
*old
, *new;
201 return ERR_PTR(-ENOENT
);
203 debug_print_probes(entry
);
204 /* (N -> M), (N > 1, M >= 0) probes */
206 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
207 if (old
[nr_probes
].func
== probe
&&
208 old
[nr_probes
].data
== data
)
213 if (nr_probes
- nr_del
== 0) {
214 /* N -> 0, (N > 1) */
215 entry
->probes
= NULL
;
217 debug_print_probes(entry
);
221 /* N -> M, (N > 1, M > 0) */
223 new = allocate_probes(nr_probes
- nr_del
+ 1);
225 return ERR_PTR(-ENOMEM
);
226 for (i
= 0; old
[i
].func
; i
++)
227 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
229 new[nr_probes
- nr_del
].func
= NULL
;
230 entry
->refcount
= nr_probes
- nr_del
;
233 debug_print_probes(entry
);
238 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
239 * Must be called with tracepoint mutex held.
240 * Returns NULL if not present.
242 static struct tracepoint_entry
*get_tracepoint(const char *name
)
244 struct cds_hlist_head
*head
;
245 struct cds_hlist_node
*node
;
246 struct tracepoint_entry
*e
;
247 size_t name_len
= strlen(name
);
250 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
251 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
252 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
254 hash
= jhash(name
, name_len
, 0);
255 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
256 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
257 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
264 * Add the tracepoint to the tracepoint hash table. Must be called with
265 * tracepoint mutex held.
267 static struct tracepoint_entry
*add_tracepoint(const char *name
,
268 const char *signature
)
270 struct cds_hlist_head
*head
;
271 struct cds_hlist_node
*node
;
272 struct tracepoint_entry
*e
;
273 size_t name_len
= strlen(name
);
276 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
277 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
278 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
280 hash
= jhash(name
, name_len
, 0);
281 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
282 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
283 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
284 DBG("tracepoint %s busy", name
);
285 return ERR_PTR(-EEXIST
); /* Already there */
289 * Using zmalloc here to allocate a variable length element. Could
290 * cause some memory fragmentation if overused.
292 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
294 return ERR_PTR(-ENOMEM
);
295 memcpy(&e
->name
[0], name
, name_len
+ 1);
296 e
->name
[name_len
] = '\0';
299 e
->callsite_refcount
= 0;
300 e
->signature
= signature
;
301 cds_hlist_add_head(&e
->hlist
, head
);
306 * Remove the tracepoint from the tracepoint hash table. Must be called with
307 * tracepoint mutex held.
309 static void remove_tracepoint(struct tracepoint_entry
*e
)
311 cds_hlist_del(&e
->hlist
);
316 * Sets the probe callback corresponding to one tracepoint.
318 static void set_tracepoint(struct tracepoint_entry
**entry
,
319 struct tracepoint
*elem
, int active
)
321 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
323 * Check that signatures match before connecting a probe to a
324 * tracepoint. Warn the user if they don't.
326 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
327 static int warned
= 0;
329 /* Only print once, don't flood console. */
331 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
332 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
333 elem
->name
, elem
->signature
, (*entry
)->signature
);
336 /* Don't accept connecting non-matching signatures. */
341 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
342 * probe callbacks array is consistent before setting a pointer to it.
343 * This array is referenced by __DO_TRACE from
344 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
347 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
348 elem
->state
= active
;
352 * Disable a tracepoint and its probe callback.
353 * Note: only waiting an RCU period after setting elem->call to the empty
354 * function insures that the original callback is not used anymore. This insured
355 * by preempt_disable around the call site.
357 static void disable_tracepoint(struct tracepoint
*elem
)
360 rcu_assign_pointer(elem
->probes
, NULL
);
364 * Add the callsite to the callsite hash table. Must be called with
365 * tracepoint mutex held.
367 static void add_callsite(struct tracepoint_lib
* lib
, struct tracepoint
*tp
)
369 struct cds_hlist_head
*head
;
370 struct callsite_entry
*e
;
371 const char *name
= tp
->name
;
372 size_t name_len
= strlen(name
);
374 struct tracepoint_entry
*tp_entry
;
376 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
377 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
378 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
380 hash
= jhash(name
, name_len
, 0);
381 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
382 e
= zmalloc(sizeof(struct callsite_entry
));
384 cds_hlist_add_head(&e
->hlist
, head
);
386 cds_list_add(&e
->node
, &lib
->callsites
);
388 tp_entry
= get_tracepoint(name
);
391 tp_entry
->callsite_refcount
++;
395 * Remove the callsite from the callsite hash table and from lib
396 * callsite list. Must be called with tracepoint mutex held.
398 static void remove_callsite(struct callsite_entry
*e
)
400 struct tracepoint_entry
*tp_entry
;
402 tp_entry
= get_tracepoint(e
->tp
->name
);
404 tp_entry
->callsite_refcount
--;
405 if (tp_entry
->callsite_refcount
== 0)
406 disable_tracepoint(e
->tp
);
408 cds_hlist_del(&e
->hlist
);
409 cds_list_del(&e
->node
);
414 * Enable/disable all callsites based on the state of a specific
416 * Must be called with tracepoint mutex held.
418 static void tracepoint_sync_callsites(const char *name
)
420 struct cds_hlist_head
*head
;
421 struct cds_hlist_node
*node
;
422 struct callsite_entry
*e
;
423 size_t name_len
= strlen(name
);
425 struct tracepoint_entry
*tp_entry
;
427 tp_entry
= get_tracepoint(name
);
428 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
429 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
430 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
432 hash
= jhash(name
, name_len
, 0);
433 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
434 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
435 struct tracepoint
*tp
= e
->tp
;
437 if (strncmp(name
, tp
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
440 set_tracepoint(&tp_entry
, tp
,
441 !!tp_entry
->refcount
);
443 disable_tracepoint(tp
);
449 * tracepoint_update_probe_range - Update a probe range
450 * @begin: beginning of the range
451 * @end: end of the range
453 * Updates the probe callback corresponding to a range of tracepoints.
456 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
457 struct tracepoint
* const *end
)
459 struct tracepoint
* const *iter
;
460 struct tracepoint_entry
*mark_entry
;
462 for (iter
= begin
; iter
< end
; iter
++) {
464 continue; /* skip dummy */
465 if (!(*iter
)->name
) {
466 disable_tracepoint(*iter
);
469 mark_entry
= get_tracepoint((*iter
)->name
);
471 set_tracepoint(&mark_entry
, *iter
,
472 !!mark_entry
->refcount
);
474 disable_tracepoint(*iter
);
479 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
481 tracepoint_update_probe_range(lib
->tracepoints_start
,
482 lib
->tracepoints_start
+ lib
->tracepoints_count
);
485 static void lib_register_callsites(struct tracepoint_lib
*lib
)
487 struct tracepoint
* const *begin
;
488 struct tracepoint
* const *end
;
489 struct tracepoint
* const *iter
;
491 begin
= lib
->tracepoints_start
;
492 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
494 for (iter
= begin
; iter
< end
; iter
++) {
496 continue; /* skip dummy */
497 if (!(*iter
)->name
) {
500 add_callsite(lib
, *iter
);
504 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
506 struct callsite_entry
*callsite
, *tmp
;
508 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
509 remove_callsite(callsite
);
513 * Update probes, removing the faulty probes.
515 static void tracepoint_update_probes(void)
517 struct tracepoint_lib
*lib
;
519 /* tracepoints registered from libraries and executable. */
520 cds_list_for_each_entry(lib
, &libs
, list
)
521 lib_update_tracepoints(lib
);
524 static struct tracepoint_probe
*
525 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
526 const char *signature
)
528 struct tracepoint_entry
*entry
;
529 struct tracepoint_probe
*old
;
531 entry
= get_tracepoint(name
);
533 entry
= add_tracepoint(name
, signature
);
535 return (struct tracepoint_probe
*)entry
;
537 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
538 if (IS_ERR(old
) && !entry
->refcount
)
539 remove_tracepoint(entry
);
544 * __tracepoint_probe_register - Connect a probe to a tracepoint
545 * @name: tracepoint name
546 * @probe: probe handler
548 * Returns 0 if ok, error value on error.
549 * The probe address must at least be aligned on the architecture pointer size.
550 * Called with the tracepoint mutex held.
552 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
553 void *data
, const char *signature
)
558 DBG("Registering probe to tracepoint %s", name
);
560 pthread_mutex_lock(&tracepoint_mutex
);
561 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
567 tracepoint_sync_callsites(name
);
570 pthread_mutex_unlock(&tracepoint_mutex
);
574 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
577 struct tracepoint_entry
*entry
;
580 entry
= get_tracepoint(name
);
582 return ERR_PTR(-ENOENT
);
583 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
586 if (!entry
->refcount
)
587 remove_tracepoint(entry
);
592 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
593 * @name: tracepoint name
594 * @probe: probe function pointer
595 * @probe: probe data pointer
597 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
603 DBG("Un-registering probe from tracepoint %s", name
);
605 pthread_mutex_lock(&tracepoint_mutex
);
606 old
= tracepoint_remove_probe(name
, probe
, data
);
611 tracepoint_sync_callsites(name
);
614 pthread_mutex_unlock(&tracepoint_mutex
);
618 static void tracepoint_add_old_probes(void *old
)
622 struct tp_probes
*tp_probes
= caa_container_of(old
,
623 struct tp_probes
, probes
[0]);
624 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
629 * tracepoint_probe_register_noupdate - register a probe but not connect
630 * @name: tracepoint name
631 * @probe: probe handler
633 * caller must call tracepoint_probe_update_all()
635 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
636 void *data
, const char *signature
)
641 pthread_mutex_lock(&tracepoint_mutex
);
642 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
647 tracepoint_add_old_probes(old
);
649 pthread_mutex_unlock(&tracepoint_mutex
);
654 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
655 * @name: tracepoint name
656 * @probe: probe function pointer
658 * caller must call tracepoint_probe_update_all()
659 * Called with the tracepoint mutex held.
661 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
667 DBG("Un-registering probe from tracepoint %s", name
);
669 pthread_mutex_lock(&tracepoint_mutex
);
670 old
= tracepoint_remove_probe(name
, probe
, data
);
675 tracepoint_add_old_probes(old
);
677 pthread_mutex_unlock(&tracepoint_mutex
);
682 * tracepoint_probe_update_all - update tracepoints
684 void tracepoint_probe_update_all(void)
686 CDS_LIST_HEAD(release_probes
);
687 struct tp_probes
*pos
, *next
;
689 pthread_mutex_lock(&tracepoint_mutex
);
693 if (!cds_list_empty(&old_probes
))
694 cds_list_replace_init(&old_probes
, &release_probes
);
697 tracepoint_update_probes();
698 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
699 cds_list_del(&pos
->u
.list
);
704 pthread_mutex_unlock(&tracepoint_mutex
);
707 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
709 new_tracepoint_cb
= cb
;
712 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
714 if (new_tracepoint_cb
) {
715 struct tracepoint
* const *t
;
717 for (t
= start
; t
< end
; t
++) {
719 new_tracepoint_cb(*t
);
724 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
725 int tracepoints_count
)
727 struct tracepoint_lib
*pl
, *iter
;
731 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
733 pl
->tracepoints_start
= tracepoints_start
;
734 pl
->tracepoints_count
= tracepoints_count
;
735 CDS_INIT_LIST_HEAD(&pl
->callsites
);
737 pthread_mutex_lock(&tracepoint_mutex
);
739 * We sort the libs by struct lib pointer address.
741 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
742 BUG_ON(iter
== pl
); /* Should never be in the list twice */
744 /* We belong to the location right after iter. */
745 cds_list_add(&pl
->list
, &iter
->list
);
749 /* We should be added at the head of the list */
750 cds_list_add(&pl
->list
, &libs
);
752 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
753 lib_register_callsites(pl
);
754 lib_update_tracepoints(pl
);
755 pthread_mutex_unlock(&tracepoint_mutex
);
757 DBG("just registered a tracepoints section from %p and having %d tracepoints",
758 tracepoints_start
, tracepoints_count
);
762 for (i
= 0; i
< tracepoints_count
; i
++) {
763 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
770 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
772 struct tracepoint_lib
*lib
;
774 pthread_mutex_lock(&tracepoint_mutex
);
775 cds_list_for_each_entry(lib
, &libs
, list
) {
776 if (lib
->tracepoints_start
!= tracepoints_start
)
779 cds_list_del(&lib
->list
);
781 * Unregistering a callsite also decreases the
782 * callsite reference count of the corresponding
783 * tracepoint, and disables the tracepoint if
784 * the reference count drops to zero.
786 lib_unregister_callsites(lib
);
787 DBG("just unregistered a tracepoints section from %p",
788 lib
->tracepoints_start
);
792 pthread_mutex_unlock(&tracepoint_mutex
);
796 void init_tracepoint(void)
798 if (uatomic_xchg(&initialized
, 1) == 1)
803 void exit_tracepoint(void)
809 * Create the wrapper symbols.
811 #undef tp_rcu_read_lock_bp
812 #undef tp_rcu_read_unlock_bp
813 #undef tp_rcu_dereference_bp
815 void tp_rcu_read_lock_bp(void)
820 void tp_rcu_read_unlock_bp(void)
822 rcu_read_unlock_bp();
825 void *tp_rcu_dereference_sym_bp(void *p
)
827 return rcu_dereference_bp(p
);