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>
33 #include <urcu/system.h>
35 #include <lttng/tracepoint.h>
36 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
38 #include <usterr-signal-safe.h>
41 #include "tracepoint-internal.h"
42 #include "lttng-tracer-core.h"
46 /* Set to 1 to enable tracepoint debug output */
47 static const int tracepoint_debug
;
48 static int initialized
;
49 static void (*new_tracepoint_cb
)(struct tracepoint
*);
52 * tracepoint_mutex nests inside UST mutex.
54 * Note about interaction with fork/clone: UST does not hold the
55 * tracepoint mutex across fork/clone because it is either:
56 * - nested within UST mutex, in which case holding the UST mutex across
58 * - taken by a library constructor, which should never race with a
59 * fork/clone if the application is expected to continue running with
60 * the same memory layout (no following exec()).
62 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
65 * libraries that contain tracepoints (struct tracepoint_lib).
66 * Protected by tracepoint mutex.
68 static CDS_LIST_HEAD(libs
);
71 * The tracepoint mutex protects the library tracepoints, the hash table, and
73 * All calls to the tracepoint API must be protected by the tracepoint mutex,
74 * excepts calls to tracepoint_register_lib and
75 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
79 * Tracepoint hash table, containing the active tracepoints.
80 * Protected by tracepoint mutex.
82 #define TRACEPOINT_HASH_BITS 12
83 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
84 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
86 static CDS_LIST_HEAD(old_probes
);
87 static int need_update
;
91 * It is used to to delay the free of multiple probes array until a quiescent
93 * Tracepoint entries modifications are protected by the tracepoint mutex.
95 struct tracepoint_entry
{
96 struct cds_hlist_node hlist
;
97 struct tracepoint_probe
*probes
;
98 int refcount
; /* Number of times armed. 0 if disarmed. */
99 int callsite_refcount
; /* how many libs use this tracepoint */
100 const char *signature
;
106 struct cds_list_head list
;
107 /* Field below only used for call_rcu scheme */
108 /* struct rcu_head head; */
110 struct tracepoint_probe probes
[0];
114 * Callsite hash table, containing the tracepoint call sites.
115 * Protected by tracepoint mutex.
117 #define CALLSITE_HASH_BITS 12
118 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
119 static struct cds_hlist_head callsite_table
[CALLSITE_TABLE_SIZE
];
121 struct callsite_entry
{
122 struct cds_hlist_node hlist
; /* hash table node */
123 struct cds_list_head node
; /* lib list of callsites node */
124 struct tracepoint
*tp
;
127 /* coverity[+alloc] */
128 static void *allocate_probes(int count
)
130 struct tp_probes
*p
= zmalloc(count
* sizeof(struct 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 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
);
182 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
183 new[nr_probes
].func
= probe
;
184 new[nr_probes
].data
= data
;
185 new[nr_probes
+ 1].func
= NULL
;
186 entry
->refcount
= nr_probes
+ 1;
188 debug_print_probes(entry
);
193 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
194 void (*probe
)(void), void *data
)
196 int nr_probes
= 0, nr_del
= 0, i
;
197 struct tracepoint_probe
*old
, *new;
202 return ERR_PTR(-ENOENT
);
204 debug_print_probes(entry
);
205 /* (N -> M), (N > 1, M >= 0) probes */
207 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
208 if (old
[nr_probes
].func
== probe
&&
209 old
[nr_probes
].data
== data
)
214 if (nr_probes
- nr_del
== 0) {
215 /* N -> 0, (N > 1) */
216 entry
->probes
= NULL
;
218 debug_print_probes(entry
);
222 /* N -> M, (N > 1, M > 0) */
224 new = allocate_probes(nr_probes
- nr_del
+ 1);
226 return ERR_PTR(-ENOMEM
);
227 for (i
= 0; old
[i
].func
; i
++)
228 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
230 new[nr_probes
- nr_del
].func
= NULL
;
231 entry
->refcount
= nr_probes
- nr_del
;
234 debug_print_probes(entry
);
239 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
240 * Must be called with tracepoint mutex held.
241 * Returns NULL if not present.
243 static struct tracepoint_entry
*get_tracepoint(const char *name
)
245 struct cds_hlist_head
*head
;
246 struct cds_hlist_node
*node
;
247 struct tracepoint_entry
*e
;
248 size_t name_len
= strlen(name
);
251 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
252 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
253 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
255 hash
= jhash(name
, name_len
, 0);
256 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
257 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
258 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
265 * Add the tracepoint to the tracepoint hash table. Must be called with
266 * tracepoint mutex held.
268 static struct tracepoint_entry
*add_tracepoint(const char *name
,
269 const char *signature
)
271 struct cds_hlist_head
*head
;
272 struct cds_hlist_node
*node
;
273 struct tracepoint_entry
*e
;
274 size_t name_len
= strlen(name
);
277 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
278 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
279 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
281 hash
= jhash(name
, name_len
, 0);
282 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
283 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
284 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
285 DBG("tracepoint %s busy", name
);
286 return ERR_PTR(-EEXIST
); /* Already there */
290 * Using zmalloc here to allocate a variable length element. Could
291 * cause some memory fragmentation if overused.
293 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
295 return ERR_PTR(-ENOMEM
);
296 memcpy(&e
->name
[0], name
, name_len
+ 1);
297 e
->name
[name_len
] = '\0';
300 e
->callsite_refcount
= 0;
301 e
->signature
= signature
;
302 cds_hlist_add_head(&e
->hlist
, head
);
307 * Remove the tracepoint from the tracepoint hash table. Must be called with
308 * tracepoint mutex held.
310 static void remove_tracepoint(struct tracepoint_entry
*e
)
312 cds_hlist_del(&e
->hlist
);
317 * Sets the probe callback corresponding to one tracepoint.
319 static void set_tracepoint(struct tracepoint_entry
**entry
,
320 struct tracepoint
*elem
, int active
)
322 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
324 * Check that signatures match before connecting a probe to a
325 * tracepoint. Warn the user if they don't.
327 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
328 static int warned
= 0;
330 /* Only print once, don't flood console. */
332 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
333 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
334 elem
->name
, elem
->signature
, (*entry
)->signature
);
337 /* Don't accept connecting non-matching signatures. */
342 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
343 * probe callbacks array is consistent before setting a pointer to it.
344 * This array is referenced by __DO_TRACE from
345 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
348 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
349 CMM_STORE_SHARED(elem
->state
, active
);
353 * Disable a tracepoint and its probe callback.
354 * Note: only waiting an RCU period after setting elem->call to the empty
355 * function insures that the original callback is not used anymore. This insured
356 * by preempt_disable around the call site.
358 static void disable_tracepoint(struct tracepoint
*elem
)
360 CMM_STORE_SHARED(elem
->state
, 0);
361 rcu_assign_pointer(elem
->probes
, NULL
);
365 * Add the callsite to the callsite hash table. Must be called with
366 * tracepoint mutex held.
368 static void add_callsite(struct tracepoint_lib
* lib
, struct tracepoint
*tp
)
370 struct cds_hlist_head
*head
;
371 struct callsite_entry
*e
;
372 const char *name
= tp
->name
;
373 size_t name_len
= strlen(name
);
375 struct tracepoint_entry
*tp_entry
;
377 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
378 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
379 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
381 hash
= jhash(name
, name_len
, 0);
382 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
383 e
= zmalloc(sizeof(struct callsite_entry
));
385 PERROR("Unable to add callsite for tracepoint \"%s\"", name
);
388 cds_hlist_add_head(&e
->hlist
, head
);
390 cds_list_add(&e
->node
, &lib
->callsites
);
392 tp_entry
= get_tracepoint(name
);
395 tp_entry
->callsite_refcount
++;
399 * Remove the callsite from the callsite hash table and from lib
400 * callsite list. Must be called with tracepoint mutex held.
402 static void remove_callsite(struct callsite_entry
*e
)
404 struct tracepoint_entry
*tp_entry
;
406 tp_entry
= get_tracepoint(e
->tp
->name
);
408 tp_entry
->callsite_refcount
--;
409 if (tp_entry
->callsite_refcount
== 0)
410 disable_tracepoint(e
->tp
);
412 cds_hlist_del(&e
->hlist
);
413 cds_list_del(&e
->node
);
418 * Enable/disable all callsites based on the state of a specific
420 * Must be called with tracepoint mutex held.
422 static void tracepoint_sync_callsites(const char *name
)
424 struct cds_hlist_head
*head
;
425 struct cds_hlist_node
*node
;
426 struct callsite_entry
*e
;
427 size_t name_len
= strlen(name
);
429 struct tracepoint_entry
*tp_entry
;
431 tp_entry
= get_tracepoint(name
);
432 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
433 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
434 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
436 hash
= jhash(name
, name_len
, 0);
437 head
= &callsite_table
[hash
& (CALLSITE_TABLE_SIZE
- 1)];
438 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
439 struct tracepoint
*tp
= e
->tp
;
441 if (strncmp(name
, tp
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
444 set_tracepoint(&tp_entry
, tp
,
445 !!tp_entry
->refcount
);
447 disable_tracepoint(tp
);
453 * tracepoint_update_probe_range - Update a probe range
454 * @begin: beginning of the range
455 * @end: end of the range
457 * Updates the probe callback corresponding to a range of tracepoints.
460 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
461 struct tracepoint
* const *end
)
463 struct tracepoint
* const *iter
;
464 struct tracepoint_entry
*mark_entry
;
466 for (iter
= begin
; iter
< end
; iter
++) {
468 continue; /* skip dummy */
469 if (!(*iter
)->name
) {
470 disable_tracepoint(*iter
);
473 mark_entry
= get_tracepoint((*iter
)->name
);
475 set_tracepoint(&mark_entry
, *iter
,
476 !!mark_entry
->refcount
);
478 disable_tracepoint(*iter
);
483 static void lib_update_tracepoints(struct tracepoint_lib
*lib
)
485 tracepoint_update_probe_range(lib
->tracepoints_start
,
486 lib
->tracepoints_start
+ lib
->tracepoints_count
);
489 static void lib_register_callsites(struct tracepoint_lib
*lib
)
491 struct tracepoint
* const *begin
;
492 struct tracepoint
* const *end
;
493 struct tracepoint
* const *iter
;
495 begin
= lib
->tracepoints_start
;
496 end
= lib
->tracepoints_start
+ lib
->tracepoints_count
;
498 for (iter
= begin
; iter
< end
; iter
++) {
500 continue; /* skip dummy */
501 if (!(*iter
)->name
) {
504 add_callsite(lib
, *iter
);
508 static void lib_unregister_callsites(struct tracepoint_lib
*lib
)
510 struct callsite_entry
*callsite
, *tmp
;
512 cds_list_for_each_entry_safe(callsite
, tmp
, &lib
->callsites
, node
)
513 remove_callsite(callsite
);
517 * Update probes, removing the faulty probes.
519 static void tracepoint_update_probes(void)
521 struct tracepoint_lib
*lib
;
523 /* tracepoints registered from libraries and executable. */
524 cds_list_for_each_entry(lib
, &libs
, list
)
525 lib_update_tracepoints(lib
);
528 static struct tracepoint_probe
*
529 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
530 const char *signature
)
532 struct tracepoint_entry
*entry
;
533 struct tracepoint_probe
*old
;
535 entry
= get_tracepoint(name
);
537 entry
= add_tracepoint(name
, signature
);
539 return (struct tracepoint_probe
*)entry
;
541 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
542 if (IS_ERR(old
) && !entry
->refcount
)
543 remove_tracepoint(entry
);
548 * __tracepoint_probe_register - Connect a probe to a tracepoint
549 * @name: tracepoint name
550 * @probe: probe handler
552 * Returns 0 if ok, error value on error.
553 * The probe address must at least be aligned on the architecture pointer size.
554 * Called with the tracepoint mutex held.
556 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
557 void *data
, const char *signature
)
562 DBG("Registering probe to tracepoint %s", name
);
564 pthread_mutex_lock(&tracepoint_mutex
);
565 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
571 tracepoint_sync_callsites(name
);
574 pthread_mutex_unlock(&tracepoint_mutex
);
578 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
581 struct tracepoint_entry
*entry
;
584 entry
= get_tracepoint(name
);
586 return ERR_PTR(-ENOENT
);
587 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
590 if (!entry
->refcount
)
591 remove_tracepoint(entry
);
596 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
597 * @name: tracepoint name
598 * @probe: probe function pointer
599 * @probe: probe data pointer
601 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
607 DBG("Un-registering probe from tracepoint %s", name
);
609 pthread_mutex_lock(&tracepoint_mutex
);
610 old
= tracepoint_remove_probe(name
, probe
, data
);
615 tracepoint_sync_callsites(name
);
618 pthread_mutex_unlock(&tracepoint_mutex
);
622 static void tracepoint_add_old_probes(void *old
)
626 struct tp_probes
*tp_probes
= caa_container_of(old
,
627 struct tp_probes
, probes
[0]);
628 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
633 * tracepoint_probe_register_noupdate - register a probe but not connect
634 * @name: tracepoint name
635 * @probe: probe handler
637 * caller must call tracepoint_probe_update_all()
639 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
640 void *data
, const char *signature
)
645 pthread_mutex_lock(&tracepoint_mutex
);
646 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
651 tracepoint_add_old_probes(old
);
653 pthread_mutex_unlock(&tracepoint_mutex
);
658 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
659 * @name: tracepoint name
660 * @probe: probe function pointer
662 * caller must call tracepoint_probe_update_all()
663 * Called with the tracepoint mutex held.
665 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
671 DBG("Un-registering probe from tracepoint %s", name
);
673 pthread_mutex_lock(&tracepoint_mutex
);
674 old
= tracepoint_remove_probe(name
, probe
, data
);
679 tracepoint_add_old_probes(old
);
681 pthread_mutex_unlock(&tracepoint_mutex
);
686 * tracepoint_probe_update_all - update tracepoints
688 void tracepoint_probe_update_all(void)
690 CDS_LIST_HEAD(release_probes
);
691 struct tp_probes
*pos
, *next
;
693 pthread_mutex_lock(&tracepoint_mutex
);
697 if (!cds_list_empty(&old_probes
))
698 cds_list_replace_init(&old_probes
, &release_probes
);
701 tracepoint_update_probes();
702 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
703 cds_list_del(&pos
->u
.list
);
708 pthread_mutex_unlock(&tracepoint_mutex
);
711 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
713 new_tracepoint_cb
= cb
;
716 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
718 if (new_tracepoint_cb
) {
719 struct tracepoint
* const *t
;
721 for (t
= start
; t
< end
; t
++) {
723 new_tracepoint_cb(*t
);
728 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
729 int tracepoints_count
)
731 struct tracepoint_lib
*pl
, *iter
;
735 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
737 PERROR("Unable to register tracepoint lib");
740 pl
->tracepoints_start
= tracepoints_start
;
741 pl
->tracepoints_count
= tracepoints_count
;
742 CDS_INIT_LIST_HEAD(&pl
->callsites
);
744 pthread_mutex_lock(&tracepoint_mutex
);
746 * We sort the libs by struct lib pointer address.
748 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
749 BUG_ON(iter
== pl
); /* Should never be in the list twice */
751 /* We belong to the location right after iter. */
752 cds_list_add(&pl
->list
, &iter
->list
);
756 /* We should be added at the head of the list */
757 cds_list_add(&pl
->list
, &libs
);
759 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
760 lib_register_callsites(pl
);
761 lib_update_tracepoints(pl
);
762 pthread_mutex_unlock(&tracepoint_mutex
);
764 DBG("just registered a tracepoints section from %p and having %d tracepoints",
765 tracepoints_start
, tracepoints_count
);
769 for (i
= 0; i
< tracepoints_count
; i
++) {
770 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
777 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
779 struct tracepoint_lib
*lib
;
781 pthread_mutex_lock(&tracepoint_mutex
);
782 cds_list_for_each_entry(lib
, &libs
, list
) {
783 if (lib
->tracepoints_start
!= tracepoints_start
)
786 cds_list_del(&lib
->list
);
788 * Unregistering a callsite also decreases the
789 * callsite reference count of the corresponding
790 * tracepoint, and disables the tracepoint if
791 * the reference count drops to zero.
793 lib_unregister_callsites(lib
);
794 DBG("just unregistered a tracepoints section from %p",
795 lib
->tracepoints_start
);
799 pthread_mutex_unlock(&tracepoint_mutex
);
803 void init_tracepoint(void)
805 if (uatomic_xchg(&initialized
, 1) == 1)
810 void exit_tracepoint(void)
816 * Create the wrapper symbols.
818 #undef tp_rcu_read_lock_bp
819 #undef tp_rcu_read_unlock_bp
820 #undef tp_rcu_dereference_bp
822 void tp_rcu_read_lock_bp(void)
827 void tp_rcu_read_unlock_bp(void)
829 rcu_read_unlock_bp();
832 void *tp_rcu_dereference_sym_bp(void *p
)
834 return rcu_dereference_bp(p
);