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 const char *signature
;
104 struct cds_list_head list
;
105 /* Field below only used for call_rcu scheme */
106 /* struct rcu_head head; */
108 struct tracepoint_probe probes
[0];
112 * Callsite hash table, containing the tracepoint call sites.
113 * Protected by tracepoint mutex.
115 #define CALLSITE_HASH_BITS 12
116 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
117 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
119 struct callsite_entry
{
120 struct cds_hlist_node hlist
; /* hash table node */
121 struct cds_list_head node
; /* lib list of callsites node */
122 struct tracepoint
*tp
;
125 /* coverity[+alloc] */
126 static void *allocate_probes(int count
)
128 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
129 + sizeof(struct tp_probes
));
130 return p
== NULL
? NULL
: p
->probes
;
133 /* coverity[+free : arg-0] */
134 static void release_probes(void *old
)
137 struct tp_probes
*tp_probes
= caa_container_of(old
,
138 struct tp_probes
, probes
[0]);
144 static void debug_print_probes(struct tracepoint_entry
*entry
)
148 if (!tracepoint_debug
|| !entry
->probes
)
151 for (i
= 0; entry
->probes
[i
].func
; i
++)
152 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
156 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
157 void (*probe
)(void), void *data
)
160 struct tracepoint_probe
*old
, *new;
164 return ERR_PTR(-EINVAL
);
166 debug_print_probes(entry
);
169 /* (N -> N+1), (N != 0, 1) probes */
170 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
171 if (old
[nr_probes
].func
== probe
&&
172 old
[nr_probes
].data
== data
)
173 return ERR_PTR(-EEXIST
);
175 /* + 2 : one for new probe, one for NULL func */
176 new = allocate_probes(nr_probes
+ 2);
178 return ERR_PTR(-ENOMEM
);
180 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
181 new[nr_probes
].func
= probe
;
182 new[nr_probes
].data
= data
;
183 new[nr_probes
+ 1].func
= NULL
;
184 entry
->refcount
= nr_probes
+ 1;
186 debug_print_probes(entry
);
191 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
192 void (*probe
)(void), void *data
)
194 int nr_probes
= 0, nr_del
= 0, i
;
195 struct tracepoint_probe
*old
, *new;
200 return ERR_PTR(-ENOENT
);
202 debug_print_probes(entry
);
203 /* (N -> M), (N > 1, M >= 0) probes */
205 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
206 if (old
[nr_probes
].func
== probe
&&
207 old
[nr_probes
].data
== data
)
212 if (nr_probes
- nr_del
== 0) {
213 /* N -> 0, (N > 1) */
214 entry
->probes
= NULL
;
216 debug_print_probes(entry
);
220 /* N -> M, (N > 1, M > 0) */
222 new = allocate_probes(nr_probes
- nr_del
+ 1);
224 return ERR_PTR(-ENOMEM
);
225 for (i
= 0; old
[i
].func
; i
++)
226 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
228 new[nr_probes
- nr_del
].func
= NULL
;
229 entry
->refcount
= nr_probes
- nr_del
;
232 debug_print_probes(entry
);
237 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
238 * Must be called with tracepoint mutex held.
239 * Returns NULL if not present.
241 static struct tracepoint_entry
*get_tracepoint(const char *name
)
243 struct cds_hlist_head
*head
;
244 struct cds_hlist_node
*node
;
245 struct tracepoint_entry
*e
;
246 size_t name_len
= strlen(name
);
249 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
250 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
251 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
253 hash
= jhash(name
, name_len
, 0);
254 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
255 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
256 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
263 * Add the tracepoint to the tracepoint hash table. Must be called with
264 * tracepoint mutex held.
266 static struct tracepoint_entry
*add_tracepoint(const char *name
,
267 const char *signature
)
269 struct cds_hlist_head
*head
;
270 struct cds_hlist_node
*node
;
271 struct tracepoint_entry
*e
;
272 size_t name_len
= strlen(name
);
275 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
276 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
277 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
279 hash
= jhash(name
, name_len
, 0);
280 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
281 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
282 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
283 DBG("tracepoint %s busy", name
);
284 return ERR_PTR(-EEXIST
); /* Already there */
288 * Using zmalloc here to allocate a variable length element. Could
289 * cause some memory fragmentation if overused.
291 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
293 return ERR_PTR(-ENOMEM
);
294 memcpy(&e
->name
[0], name
, name_len
+ 1);
295 e
->name
[name_len
] = '\0';
298 e
->signature
= signature
;
299 cds_hlist_add_head(&e
->hlist
, head
);
304 * Remove the tracepoint from the tracepoint hash table. Must be called with
305 * tracepoint mutex held.
307 static void remove_tracepoint(struct tracepoint_entry
*e
)
309 cds_hlist_del(&e
->hlist
);
314 * Sets the probe callback corresponding to one tracepoint.
316 static void set_tracepoint(struct tracepoint_entry
**entry
,
317 struct tracepoint
*elem
, int active
)
319 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
321 * Check that signatures match before connecting a probe to a
322 * tracepoint. Warn the user if they don't.
324 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
325 static int warned
= 0;
327 /* Only print once, don't flood console. */
329 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
330 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
331 elem
->name
, elem
->signature
, (*entry
)->signature
);
334 /* Don't accept connecting non-matching signatures. */
339 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
340 * probe callbacks array is consistent before setting a pointer to it.
341 * This array is referenced by __DO_TRACE from
342 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
345 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
346 elem
->state
= active
;
350 * Disable a tracepoint and its probe callback.
351 * Note: only waiting an RCU period after setting elem->call to the empty
352 * function insures that the original callback is not used anymore. This insured
353 * by preempt_disable around the call site.
355 static void disable_tracepoint(struct tracepoint
*elem
)
358 rcu_assign_pointer(elem
->probes
, NULL
);
362 * Add the callsite to the callsite hash table. Must be called with
363 * tracepoint mutex held.
365 static void add_callsite(struct tracepoint_lib
* lib
, struct tracepoint
*tp
)
367 struct cds_hlist_head
*head
;
368 struct callsite_entry
*e
;
369 const char *name
= tp
->name
;
370 size_t name_len
= strlen(name
);
373 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
374 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
375 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
377 hash
= jhash(name
, name_len
, 0);
378 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
379 e
= zmalloc(sizeof(struct callsite_entry
));
381 cds_hlist_add_head(&e
->hlist
, head
);
383 cds_list_add(&e
->node
, &lib
->callsites
);
387 * Remove the callsite from the callsite hash table and from lib
388 * callsite list. Must be called with tracepoint mutex held.
390 static void remove_callsite(struct callsite_entry
*e
)
392 cds_hlist_del(&e
->hlist
);
393 cds_list_del(&e
->node
);
398 * Enable/disable all callsites based on the state of a specific
400 * Must be called with tracepoint mutex held.
402 static void tracepoint_sync_callsites(const char *name
)
404 struct cds_hlist_head
*head
;
405 struct cds_hlist_node
*node
;
406 struct callsite_entry
*e
;
407 size_t name_len
= strlen(name
);
409 struct tracepoint_entry
*tp_entry
;
411 tp_entry
= get_tracepoint(name
);
412 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
413 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
414 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
416 hash
= jhash(name
, name_len
, 0);
417 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
418 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
419 struct tracepoint
*tp
= e
->tp
;
421 if (strncmp(name
, tp
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
424 set_tracepoint(&tp_entry
, tp
,
425 !!tp_entry
->refcount
);
427 disable_tracepoint(tp
);
433 * tracepoint_update_probe_range - Update a probe range
434 * @begin: beginning of the range
435 * @end: end of the range
437 * Updates the probe callback corresponding to a range of tracepoints.
440 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
441 struct tracepoint
* const *end
)
443 struct tracepoint
* const *iter
;
444 struct tracepoint_entry
*mark_entry
;
446 for (iter
= begin
; iter
< end
; iter
++) {
448 continue; /* skip dummy */
449 if (!(*iter
)->name
) {
450 disable_tracepoint(*iter
);
453 mark_entry
= get_tracepoint((*iter
)->name
);
455 set_tracepoint(&mark_entry
, *iter
,
456 !!mark_entry
->refcount
);
458 disable_tracepoint(*iter
);
463 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
465 tracepoint_update_probe_range(lib
->tracepoints_start
,
466 lib
->tracepoints_start
+ lib
->tracepoints_count
);
469 static void lib_register_callsites(struct tracepoint_lib
*lib
)
471 struct tracepoint
* const *begin
;
472 struct tracepoint
* const *end
;
473 struct tracepoint
* const *iter
;
475 begin
= lib
->tracepoints_start
;
476 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
478 for (iter
= begin
; iter
< end
; iter
++) {
480 continue; /* skip dummy */
481 if (!(*iter
)->name
) {
484 add_callsite(lib
, *iter
);
488 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
490 struct callsite_entry
*callsite
, *tmp
;
492 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
493 remove_callsite(callsite
);
497 * Update probes, removing the faulty probes.
499 static void tracepoint_update_probes(void)
501 struct tracepoint_lib
*lib
;
503 /* tracepoints registered from libraries and executable. */
504 cds_list_for_each_entry(lib
, &libs
, list
)
505 lib_update_tracepoints(lib
);
508 static struct tracepoint_probe
*
509 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
510 const char *signature
)
512 struct tracepoint_entry
*entry
;
513 struct tracepoint_probe
*old
;
515 entry
= get_tracepoint(name
);
517 entry
= add_tracepoint(name
, signature
);
519 return (struct tracepoint_probe
*)entry
;
521 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
522 if (IS_ERR(old
) && !entry
->refcount
)
523 remove_tracepoint(entry
);
528 * __tracepoint_probe_register - Connect a probe to a tracepoint
529 * @name: tracepoint name
530 * @probe: probe handler
532 * Returns 0 if ok, error value on error.
533 * The probe address must at least be aligned on the architecture pointer size.
534 * Called with the tracepoint mutex held.
536 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
537 void *data
, const char *signature
)
542 DBG("Registering probe to tracepoint %s", name
);
544 pthread_mutex_lock(&tracepoint_mutex
);
545 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
551 tracepoint_sync_callsites(name
);
554 pthread_mutex_unlock(&tracepoint_mutex
);
558 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
561 struct tracepoint_entry
*entry
;
564 entry
= get_tracepoint(name
);
566 return ERR_PTR(-ENOENT
);
567 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
570 if (!entry
->refcount
)
571 remove_tracepoint(entry
);
576 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
577 * @name: tracepoint name
578 * @probe: probe function pointer
579 * @probe: probe data pointer
581 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
587 DBG("Un-registering probe from tracepoint %s", name
);
589 pthread_mutex_lock(&tracepoint_mutex
);
590 old
= tracepoint_remove_probe(name
, probe
, data
);
595 tracepoint_sync_callsites(name
);
598 pthread_mutex_unlock(&tracepoint_mutex
);
602 static void tracepoint_add_old_probes(void *old
)
606 struct tp_probes
*tp_probes
= caa_container_of(old
,
607 struct tp_probes
, probes
[0]);
608 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
613 * tracepoint_probe_register_noupdate - register a probe but not connect
614 * @name: tracepoint name
615 * @probe: probe handler
617 * caller must call tracepoint_probe_update_all()
619 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
620 void *data
, const char *signature
)
625 pthread_mutex_lock(&tracepoint_mutex
);
626 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
631 tracepoint_add_old_probes(old
);
633 pthread_mutex_unlock(&tracepoint_mutex
);
638 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
639 * @name: tracepoint name
640 * @probe: probe function pointer
642 * caller must call tracepoint_probe_update_all()
643 * Called with the tracepoint mutex held.
645 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
651 DBG("Un-registering probe from tracepoint %s", name
);
653 pthread_mutex_lock(&tracepoint_mutex
);
654 old
= tracepoint_remove_probe(name
, probe
, data
);
659 tracepoint_add_old_probes(old
);
661 pthread_mutex_unlock(&tracepoint_mutex
);
666 * tracepoint_probe_update_all - update tracepoints
668 void tracepoint_probe_update_all(void)
670 CDS_LIST_HEAD(release_probes
);
671 struct tp_probes
*pos
, *next
;
673 pthread_mutex_lock(&tracepoint_mutex
);
677 if (!cds_list_empty(&old_probes
))
678 cds_list_replace_init(&old_probes
, &release_probes
);
681 tracepoint_update_probes();
682 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
683 cds_list_del(&pos
->u
.list
);
688 pthread_mutex_unlock(&tracepoint_mutex
);
691 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
693 new_tracepoint_cb
= cb
;
696 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
698 if (new_tracepoint_cb
) {
699 struct tracepoint
* const *t
;
701 for (t
= start
; t
< end
; t
++) {
703 new_tracepoint_cb(*t
);
709 void lib_disable_tracepoints(struct tracepoint_lib
*lib
)
711 struct tracepoint
* const *begin
;
712 struct tracepoint
* const *end
;
713 struct tracepoint
* const *iter
;
715 begin
= lib
->tracepoints_start
;
716 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
718 for (iter
= begin
; iter
< end
; iter
++) {
720 continue; /* skip dummy */
721 disable_tracepoint(*iter
);
726 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
727 int tracepoints_count
)
729 struct tracepoint_lib
*pl
, *iter
;
733 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
735 pl
->tracepoints_start
= tracepoints_start
;
736 pl
->tracepoints_count
= tracepoints_count
;
737 CDS_INIT_LIST_HEAD(&pl
->callsites
);
739 pthread_mutex_lock(&tracepoint_mutex
);
741 * We sort the libs by struct lib pointer address.
743 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
744 BUG_ON(iter
== pl
); /* Should never be in the list twice */
746 /* We belong to the location right after iter. */
747 cds_list_add(&pl
->list
, &iter
->list
);
751 /* We should be added at the head of the list */
752 cds_list_add(&pl
->list
, &libs
);
754 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
755 lib_register_callsites(pl
);
756 lib_update_tracepoints(pl
);
757 pthread_mutex_unlock(&tracepoint_mutex
);
759 DBG("just registered a tracepoints section from %p and having %d tracepoints",
760 tracepoints_start
, tracepoints_count
);
764 for (i
= 0; i
< tracepoints_count
; i
++) {
765 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
772 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
774 struct tracepoint_lib
*lib
;
776 pthread_mutex_lock(&tracepoint_mutex
);
777 cds_list_for_each_entry(lib
, &libs
, list
) {
778 if (lib
->tracepoints_start
!= tracepoints_start
)
781 cds_list_del(&lib
->list
);
783 * Force tracepoint disarm for all tracepoints of this lib.
784 * This takes care of destructor of library that would leave a
785 * LD_PRELOAD wrapper override function enabled for tracing, but
786 * the session teardown would not be able to reach the
787 * tracepoint anymore to disable it.
789 lib_disable_tracepoints(lib
);
790 lib_unregister_callsites(lib
);
791 DBG("just unregistered a tracepoints section from %p",
792 lib
->tracepoints_start
);
796 pthread_mutex_unlock(&tracepoint_mutex
);
800 void init_tracepoint(void)
802 if (uatomic_xchg(&initialized
, 1) == 1)
807 void exit_tracepoint(void)
813 * Create the wrapper symbols.
815 #undef tp_rcu_read_lock_bp
816 #undef tp_rcu_read_unlock_bp
817 #undef tp_rcu_dereference_bp
819 void tp_rcu_read_lock_bp(void)
824 void tp_rcu_read_unlock_bp(void)
826 rcu_read_unlock_bp();
829 void *tp_rcu_dereference_sym_bp(void *p
)
831 return rcu_dereference_bp(p
);