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 static void *allocate_probes(int count
)
127 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
128 + sizeof(struct tp_probes
));
129 return p
== NULL
? NULL
: p
->probes
;
132 static void release_probes(void *old
)
135 struct tp_probes
*tp_probes
= caa_container_of(old
,
136 struct tp_probes
, probes
[0]);
142 static void debug_print_probes(struct tracepoint_entry
*entry
)
146 if (!tracepoint_debug
|| !entry
->probes
)
149 for (i
= 0; entry
->probes
[i
].func
; i
++)
150 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
154 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
155 void (*probe
)(void), void *data
)
158 struct tracepoint_probe
*old
, *new;
162 return ERR_PTR(-EINVAL
);
164 debug_print_probes(entry
);
167 /* (N -> N+1), (N != 0, 1) probes */
168 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
169 if (old
[nr_probes
].func
== probe
&&
170 old
[nr_probes
].data
== data
)
171 return ERR_PTR(-EEXIST
);
173 /* + 2 : one for new probe, one for NULL func */
174 new = allocate_probes(nr_probes
+ 2);
176 return ERR_PTR(-ENOMEM
);
178 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
179 new[nr_probes
].func
= probe
;
180 new[nr_probes
].data
= data
;
181 new[nr_probes
+ 1].func
= NULL
;
182 entry
->refcount
= nr_probes
+ 1;
184 debug_print_probes(entry
);
189 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
190 void (*probe
)(void), void *data
)
192 int nr_probes
= 0, nr_del
= 0, i
;
193 struct tracepoint_probe
*old
, *new;
198 return ERR_PTR(-ENOENT
);
200 debug_print_probes(entry
);
201 /* (N -> M), (N > 1, M >= 0) probes */
203 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
204 if (old
[nr_probes
].func
== probe
&&
205 old
[nr_probes
].data
== data
)
210 if (nr_probes
- nr_del
== 0) {
211 /* N -> 0, (N > 1) */
212 entry
->probes
= NULL
;
214 debug_print_probes(entry
);
218 /* N -> M, (N > 1, M > 0) */
220 new = allocate_probes(nr_probes
- nr_del
+ 1);
222 return ERR_PTR(-ENOMEM
);
223 for (i
= 0; old
[i
].func
; i
++)
224 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
226 new[nr_probes
- nr_del
].func
= NULL
;
227 entry
->refcount
= nr_probes
- nr_del
;
230 debug_print_probes(entry
);
235 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
236 * Must be called with tracepoint mutex held.
237 * Returns NULL if not present.
239 static struct tracepoint_entry
*get_tracepoint(const char *name
)
241 struct cds_hlist_head
*head
;
242 struct cds_hlist_node
*node
;
243 struct tracepoint_entry
*e
;
244 size_t name_len
= strlen(name
);
247 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
248 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
249 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
251 hash
= jhash(name
, name_len
, 0);
252 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
253 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
254 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
261 * Add the tracepoint to the tracepoint hash table. Must be called with
262 * tracepoint mutex held.
264 static struct tracepoint_entry
*add_tracepoint(const char *name
,
265 const char *signature
)
267 struct cds_hlist_head
*head
;
268 struct cds_hlist_node
*node
;
269 struct tracepoint_entry
*e
;
270 size_t name_len
= strlen(name
);
273 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
274 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
275 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
277 hash
= jhash(name
, name_len
, 0);
278 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
279 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
280 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
281 DBG("tracepoint %s busy", name
);
282 return ERR_PTR(-EEXIST
); /* Already there */
286 * Using zmalloc here to allocate a variable length element. Could
287 * cause some memory fragmentation if overused.
289 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
291 return ERR_PTR(-ENOMEM
);
292 memcpy(&e
->name
[0], name
, name_len
+ 1);
293 e
->name
[name_len
] = '\0';
296 e
->signature
= signature
;
297 cds_hlist_add_head(&e
->hlist
, head
);
302 * Remove the tracepoint from the tracepoint hash table. Must be called with
303 * tracepoint mutex held.
305 static void remove_tracepoint(struct tracepoint_entry
*e
)
307 cds_hlist_del(&e
->hlist
);
312 * Add the callsite to the callsite hash table. Must be called with
313 * tracepoint mutex held.
315 static void add_callsite(struct tracepoint
*tp
)
317 struct cds_hlist_head
*head
;
318 struct callsite_entry
*e
;
319 const char *name
= tp
->name
;
320 size_t name_len
= strlen(name
);
323 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
324 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
325 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
327 hash
= jhash(name
, name_len
, 0);
328 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
329 e
= zmalloc(sizeof(struct callsite_entry
));
331 cds_hlist_add_head(&e
->hlist
, head
);
336 * Remove the callsite from the callsite hash table and from lib
337 * callsite list. Must be called with tracepoint mutex held.
339 static void remove_callsite(struct callsite_entry
*e
)
341 cds_hlist_del(&e
->hlist
);
342 cds_list_del(&e
->node
);
347 * Sets the probe callback corresponding to one tracepoint.
349 static void set_tracepoint(struct tracepoint_entry
**entry
,
350 struct tracepoint
*elem
, int active
)
352 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
354 * Check that signatures match before connecting a probe to a
355 * tracepoint. Warn the user if they don't.
357 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
358 static int warned
= 0;
360 /* Only print once, don't flood console. */
362 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
363 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
364 elem
->name
, elem
->signature
, (*entry
)->signature
);
367 /* Don't accept connecting non-matching signatures. */
372 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
373 * probe callbacks array is consistent before setting a pointer to it.
374 * This array is referenced by __DO_TRACE from
375 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
378 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
379 elem
->state
= active
;
383 * Disable a tracepoint and its probe callback.
384 * Note: only waiting an RCU period after setting elem->call to the empty
385 * function insures that the original callback is not used anymore. This insured
386 * by preempt_disable around the call site.
388 static void disable_tracepoint(struct tracepoint
*elem
)
391 rcu_assign_pointer(elem
->probes
, NULL
);
395 * Enable/disable all callsites based on the state of a specific
397 * Must be called with tracepoint mutex held.
399 static void tracepoint_sync_callsites(const char *name
)
401 struct cds_hlist_head
*head
;
402 struct cds_hlist_node
*node
;
403 struct callsite_entry
*e
;
404 size_t name_len
= strlen(name
);
406 struct tracepoint_entry
*tp_entry
;
408 tp_entry
= get_tracepoint(name
);
409 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
410 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
411 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
413 hash
= jhash(name
, name_len
, 0);
414 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
415 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
416 struct tracepoint
*tp
= e
->tp
;
418 if (strncmp(name
, tp
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
421 set_tracepoint(&tp_entry
, tp
,
422 !!tp_entry
->refcount
);
424 disable_tracepoint(tp
);
430 * tracepoint_update_probe_range - Update a probe range
431 * @begin: beginning of the range
432 * @end: end of the range
434 * Updates the probe callback corresponding to a range of tracepoints.
437 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
438 struct tracepoint
* const *end
)
440 struct tracepoint
* const *iter
;
441 struct tracepoint_entry
*mark_entry
;
443 for (iter
= begin
; iter
< end
; iter
++) {
445 continue; /* skip dummy */
446 if (!(*iter
)->name
) {
447 disable_tracepoint(*iter
);
450 mark_entry
= get_tracepoint((*iter
)->name
);
452 set_tracepoint(&mark_entry
, *iter
,
453 !!mark_entry
->refcount
);
455 disable_tracepoint(*iter
);
460 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
462 tracepoint_update_probe_range(lib
->tracepoints_start
,
463 lib
->tracepoints_start
+ lib
->tracepoints_count
);
466 static void lib_register_callsites(struct tracepoint_lib
*lib
)
468 struct tracepoint
* const *begin
;
469 struct tracepoint
* const *end
;
470 struct tracepoint
* const *iter
;
472 begin
= lib
->tracepoints_start
;
473 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
475 for (iter
= begin
; iter
< end
; iter
++) {
477 continue; /* skip dummy */
478 if (!(*iter
)->name
) {
485 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
487 struct callsite_entry
*callsite
, *tmp
;
489 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
490 remove_callsite(callsite
);
494 * Update probes, removing the faulty probes.
496 static void tracepoint_update_probes(void)
498 struct tracepoint_lib
*lib
;
500 /* tracepoints registered from libraries and executable. */
501 cds_list_for_each_entry(lib
, &libs
, list
)
502 lib_update_tracepoints(lib
);
505 static struct tracepoint_probe
*
506 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
507 const char *signature
)
509 struct tracepoint_entry
*entry
;
510 struct tracepoint_probe
*old
;
512 entry
= get_tracepoint(name
);
514 entry
= add_tracepoint(name
, signature
);
516 return (struct tracepoint_probe
*)entry
;
518 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
519 if (IS_ERR(old
) && !entry
->refcount
)
520 remove_tracepoint(entry
);
525 * __tracepoint_probe_register - Connect a probe to a tracepoint
526 * @name: tracepoint name
527 * @probe: probe handler
529 * Returns 0 if ok, error value on error.
530 * The probe address must at least be aligned on the architecture pointer size.
531 * Called with the tracepoint mutex held.
533 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
534 void *data
, const char *signature
)
539 DBG("Registering probe to tracepoint %s", name
);
541 pthread_mutex_lock(&tracepoint_mutex
);
542 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
548 tracepoint_sync_callsites(name
);
551 pthread_mutex_unlock(&tracepoint_mutex
);
555 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
558 struct tracepoint_entry
*entry
;
561 entry
= get_tracepoint(name
);
563 return ERR_PTR(-ENOENT
);
564 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
567 if (!entry
->refcount
)
568 remove_tracepoint(entry
);
573 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
574 * @name: tracepoint name
575 * @probe: probe function pointer
576 * @probe: probe data pointer
578 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
584 DBG("Un-registering probe from tracepoint %s", name
);
586 pthread_mutex_lock(&tracepoint_mutex
);
587 old
= tracepoint_remove_probe(name
, probe
, data
);
592 tracepoint_sync_callsites(name
);
595 pthread_mutex_unlock(&tracepoint_mutex
);
599 static void tracepoint_add_old_probes(void *old
)
603 struct tp_probes
*tp_probes
= caa_container_of(old
,
604 struct tp_probes
, probes
[0]);
605 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
610 * tracepoint_probe_register_noupdate - register a probe but not connect
611 * @name: tracepoint name
612 * @probe: probe handler
614 * caller must call tracepoint_probe_update_all()
616 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
617 void *data
, const char *signature
)
622 pthread_mutex_lock(&tracepoint_mutex
);
623 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
628 tracepoint_add_old_probes(old
);
630 pthread_mutex_unlock(&tracepoint_mutex
);
635 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
636 * @name: tracepoint name
637 * @probe: probe function pointer
639 * caller must call tracepoint_probe_update_all()
640 * Called with the tracepoint mutex held.
642 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
648 DBG("Un-registering probe from tracepoint %s", name
);
650 pthread_mutex_lock(&tracepoint_mutex
);
651 old
= tracepoint_remove_probe(name
, probe
, data
);
656 tracepoint_add_old_probes(old
);
658 pthread_mutex_unlock(&tracepoint_mutex
);
663 * tracepoint_probe_update_all - update tracepoints
665 void tracepoint_probe_update_all(void)
667 CDS_LIST_HEAD(release_probes
);
668 struct tp_probes
*pos
, *next
;
670 pthread_mutex_lock(&tracepoint_mutex
);
674 if (!cds_list_empty(&old_probes
))
675 cds_list_replace_init(&old_probes
, &release_probes
);
678 tracepoint_update_probes();
679 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
680 cds_list_del(&pos
->u
.list
);
685 pthread_mutex_unlock(&tracepoint_mutex
);
688 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
690 new_tracepoint_cb
= cb
;
693 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
695 if (new_tracepoint_cb
) {
696 struct tracepoint
* const *t
;
698 for (t
= start
; t
< end
; t
++) {
700 new_tracepoint_cb(*t
);
706 void lib_disable_tracepoints(struct tracepoint_lib
*lib
)
708 struct tracepoint
* const *begin
;
709 struct tracepoint
* const *end
;
710 struct tracepoint
* const *iter
;
712 begin
= lib
->tracepoints_start
;
713 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
715 for (iter
= begin
; iter
< end
; iter
++) {
717 continue; /* skip dummy */
718 disable_tracepoint(*iter
);
723 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
724 int tracepoints_count
)
726 struct tracepoint_lib
*pl
, *iter
;
730 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
732 pl
->tracepoints_start
= tracepoints_start
;
733 pl
->tracepoints_count
= tracepoints_count
;
734 CDS_INIT_LIST_HEAD(&pl
->callsites
);
736 pthread_mutex_lock(&tracepoint_mutex
);
738 * We sort the libs by struct lib pointer address.
740 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
741 BUG_ON(iter
== pl
); /* Should never be in the list twice */
743 /* We belong to the location right after iter. */
744 cds_list_add(&pl
->list
, &iter
->list
);
748 /* We should be added at the head of the list */
749 cds_list_add(&pl
->list
, &libs
);
751 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
752 lib_register_callsites(pl
);
753 lib_update_tracepoints(pl
);
754 pthread_mutex_unlock(&tracepoint_mutex
);
756 DBG("just registered a tracepoints section from %p and having %d tracepoints",
757 tracepoints_start
, tracepoints_count
);
761 for (i
= 0; i
< tracepoints_count
; i
++) {
762 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
769 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
771 struct tracepoint_lib
*lib
;
773 pthread_mutex_lock(&tracepoint_mutex
);
774 cds_list_for_each_entry(lib
, &libs
, list
) {
775 if (lib
->tracepoints_start
!= tracepoints_start
)
778 cds_list_del(&lib
->list
);
780 * Force tracepoint disarm for all tracepoints of this lib.
781 * This takes care of destructor of library that would leave a
782 * LD_PRELOAD wrapper override function enabled for tracing, but
783 * the session teardown would not be able to reach the
784 * tracepoint anymore to disable it.
786 lib_disable_tracepoints(lib
);
787 lib_unregister_callsites(lib
);
788 DBG("just unregistered a tracepoints section from %p",
789 lib
->tracepoints_start
);
793 pthread_mutex_unlock(&tracepoint_mutex
);
797 void init_tracepoint(void)
799 if (uatomic_xchg(&initialized
, 1) == 1)
804 void exit_tracepoint(void)
810 * Create the wrapper symbols.
812 #undef tp_rcu_read_lock_bp
813 #undef tp_rcu_read_unlock_bp
814 #undef tp_rcu_dereference_bp
816 void tp_rcu_read_lock_bp(void)
821 void tp_rcu_read_unlock_bp(void)
823 rcu_read_unlock_bp();
826 void *tp_rcu_dereference_sym_bp(void *p
)
828 return rcu_dereference_bp(p
);