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];
111 static void *allocate_probes(int count
)
113 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
114 + sizeof(struct tp_probes
));
115 return p
== NULL
? NULL
: p
->probes
;
118 static void release_probes(void *old
)
121 struct tp_probes
*tp_probes
= caa_container_of(old
,
122 struct tp_probes
, probes
[0]);
128 static void debug_print_probes(struct tracepoint_entry
*entry
)
132 if (!tracepoint_debug
|| !entry
->probes
)
135 for (i
= 0; entry
->probes
[i
].func
; i
++)
136 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
140 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
141 void (*probe
)(void), void *data
)
144 struct tracepoint_probe
*old
, *new;
148 return ERR_PTR(-EINVAL
);
150 debug_print_probes(entry
);
153 /* (N -> N+1), (N != 0, 1) probes */
154 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
155 if (old
[nr_probes
].func
== probe
&&
156 old
[nr_probes
].data
== data
)
157 return ERR_PTR(-EEXIST
);
159 /* + 2 : one for new probe, one for NULL func */
160 new = allocate_probes(nr_probes
+ 2);
162 return ERR_PTR(-ENOMEM
);
164 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
165 new[nr_probes
].func
= probe
;
166 new[nr_probes
].data
= data
;
167 new[nr_probes
+ 1].func
= NULL
;
168 entry
->refcount
= nr_probes
+ 1;
170 debug_print_probes(entry
);
175 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
,
176 void (*probe
)(void), void *data
)
178 int nr_probes
= 0, nr_del
= 0, i
;
179 struct tracepoint_probe
*old
, *new;
184 return ERR_PTR(-ENOENT
);
186 debug_print_probes(entry
);
187 /* (N -> M), (N > 1, M >= 0) probes */
189 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
190 if (old
[nr_probes
].func
== probe
&&
191 old
[nr_probes
].data
== data
)
198 if (nr_probes
- nr_del
== 0) {
199 /* N -> 0, (N > 1) */
200 entry
->probes
= NULL
;
202 debug_print_probes(entry
);
206 /* N -> M, (N > 1, M > 0) */
208 new = allocate_probes(nr_probes
- nr_del
+ 1);
210 return ERR_PTR(-ENOMEM
);
211 for (i
= 0; old
[i
].func
; i
++)
212 if (old
[i
].func
!= probe
|| old
[i
].data
!= data
)
214 new[nr_probes
- nr_del
].func
= NULL
;
215 entry
->refcount
= nr_probes
- nr_del
;
218 debug_print_probes(entry
);
223 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
224 * Must be called with tracepoint mutex held.
225 * Returns NULL if not present.
227 static struct tracepoint_entry
*get_tracepoint(const char *name
)
229 struct cds_hlist_head
*head
;
230 struct cds_hlist_node
*node
;
231 struct tracepoint_entry
*e
;
232 size_t name_len
= strlen(name
);
235 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
236 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
237 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
239 hash
= jhash(name
, name_len
, 0);
240 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
241 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
242 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
249 * Add the tracepoint to the tracepoint hash table. Must be called with
250 * tracepoint mutex held.
252 static struct tracepoint_entry
*add_tracepoint(const char *name
,
253 const char *signature
)
255 struct cds_hlist_head
*head
;
256 struct cds_hlist_node
*node
;
257 struct tracepoint_entry
*e
;
258 size_t name_len
= strlen(name
);
261 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
262 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
263 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
265 hash
= jhash(name
, name_len
, 0);
266 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
267 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
268 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
269 DBG("tracepoint %s busy", name
);
270 return ERR_PTR(-EEXIST
); /* Already there */
274 * Using zmalloc here to allocate a variable length element. Could
275 * cause some memory fragmentation if overused.
277 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
279 return ERR_PTR(-ENOMEM
);
280 memcpy(&e
->name
[0], name
, name_len
+ 1);
281 e
->name
[name_len
] = '\0';
284 e
->signature
= signature
;
285 cds_hlist_add_head(&e
->hlist
, head
);
290 * Remove the tracepoint from the tracepoint hash table. Must be called with
291 * tracepoint mutex held.
293 static void remove_tracepoint(struct tracepoint_entry
*e
)
295 cds_hlist_del(&e
->hlist
);
300 * Sets the probe callback corresponding to one tracepoint.
302 static void set_tracepoint(struct tracepoint_entry
**entry
,
303 struct tracepoint
*elem
, int active
)
305 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
307 * Check that signatures match before connecting a probe to a
308 * tracepoint. Warn the user if they don't.
310 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
311 static int warned
= 0;
313 /* Only print once, don't flood console. */
315 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
316 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
317 elem
->name
, elem
->signature
, (*entry
)->signature
);
320 /* Don't accept connecting non-matching signatures. */
325 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
326 * probe callbacks array is consistent before setting a pointer to it.
327 * This array is referenced by __DO_TRACE from
328 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
331 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
332 elem
->state
= active
;
336 * Disable a tracepoint and its probe callback.
337 * Note: only waiting an RCU period after setting elem->call to the empty
338 * function insures that the original callback is not used anymore. This insured
339 * by preempt_disable around the call site.
341 static void disable_tracepoint(struct tracepoint
*elem
)
344 rcu_assign_pointer(elem
->probes
, NULL
);
348 * tracepoint_update_probe_range - Update a probe range
349 * @begin: beginning of the range
350 * @end: end of the range
352 * Updates the probe callback corresponding to a range of tracepoints.
355 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
356 struct tracepoint
* const *end
)
358 struct tracepoint
* const *iter
;
359 struct tracepoint_entry
*mark_entry
;
361 for (iter
= begin
; iter
< end
; iter
++) {
363 continue; /* skip dummy */
364 if (!(*iter
)->name
) {
365 disable_tracepoint(*iter
);
368 mark_entry
= get_tracepoint((*iter
)->name
);
370 set_tracepoint(&mark_entry
, *iter
,
371 !!mark_entry
->refcount
);
373 disable_tracepoint(*iter
);
378 static void lib_update_tracepoints(void)
380 struct tracepoint_lib
*lib
;
382 cds_list_for_each_entry(lib
, &libs
, list
) {
383 tracepoint_update_probe_range(lib
->tracepoints_start
,
384 lib
->tracepoints_start
+ lib
->tracepoints_count
);
389 * Update probes, removing the faulty probes.
391 static void tracepoint_update_probes(void)
393 /* tracepoints registered from libraries and executable. */
394 lib_update_tracepoints();
397 static struct tracepoint_probe
*
398 tracepoint_add_probe(const char *name
, void (*probe
)(void), void *data
,
399 const char *signature
)
401 struct tracepoint_entry
*entry
;
402 struct tracepoint_probe
*old
;
404 entry
= get_tracepoint(name
);
406 entry
= add_tracepoint(name
, signature
);
408 return (struct tracepoint_probe
*)entry
;
410 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
411 if (IS_ERR(old
) && !entry
->refcount
)
412 remove_tracepoint(entry
);
417 * __tracepoint_probe_register - Connect a probe to a tracepoint
418 * @name: tracepoint name
419 * @probe: probe handler
421 * Returns 0 if ok, error value on error.
422 * The probe address must at least be aligned on the architecture pointer size.
423 * Called with the tracepoint mutex held.
425 int __tracepoint_probe_register(const char *name
, void (*probe
)(void),
426 void *data
, const char *signature
)
431 DBG("Registering probe to tracepoint %s", name
);
433 pthread_mutex_lock(&tracepoint_mutex
);
434 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
440 tracepoint_update_probes(); /* may update entry */
443 pthread_mutex_unlock(&tracepoint_mutex
);
447 static void *tracepoint_remove_probe(const char *name
, void (*probe
)(void),
450 struct tracepoint_entry
*entry
;
453 entry
= get_tracepoint(name
);
455 return ERR_PTR(-ENOENT
);
456 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
459 if (!entry
->refcount
)
460 remove_tracepoint(entry
);
465 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
466 * @name: tracepoint name
467 * @probe: probe function pointer
468 * @probe: probe data pointer
470 int __tracepoint_probe_unregister(const char *name
, void (*probe
)(void),
476 DBG("Un-registering probe from tracepoint %s", name
);
478 pthread_mutex_lock(&tracepoint_mutex
);
479 old
= tracepoint_remove_probe(name
, probe
, data
);
484 tracepoint_update_probes(); /* may update entry */
487 pthread_mutex_unlock(&tracepoint_mutex
);
491 static void tracepoint_add_old_probes(void *old
)
495 struct tp_probes
*tp_probes
= caa_container_of(old
,
496 struct tp_probes
, probes
[0]);
497 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
502 * tracepoint_probe_register_noupdate - register a probe but not connect
503 * @name: tracepoint name
504 * @probe: probe handler
506 * caller must call tracepoint_probe_update_all()
508 int tracepoint_probe_register_noupdate(const char *name
, void (*probe
)(void),
509 void *data
, const char *signature
)
514 pthread_mutex_lock(&tracepoint_mutex
);
515 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
520 tracepoint_add_old_probes(old
);
522 pthread_mutex_unlock(&tracepoint_mutex
);
527 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
528 * @name: tracepoint name
529 * @probe: probe function pointer
531 * caller must call tracepoint_probe_update_all()
532 * Called with the tracepoint mutex held.
534 int tracepoint_probe_unregister_noupdate(const char *name
, void (*probe
)(void),
540 DBG("Un-registering probe from tracepoint %s", name
);
542 pthread_mutex_lock(&tracepoint_mutex
);
543 old
= tracepoint_remove_probe(name
, probe
, data
);
548 tracepoint_add_old_probes(old
);
550 pthread_mutex_unlock(&tracepoint_mutex
);
555 * tracepoint_probe_update_all - update tracepoints
557 void tracepoint_probe_update_all(void)
559 CDS_LIST_HEAD(release_probes
);
560 struct tp_probes
*pos
, *next
;
562 pthread_mutex_lock(&tracepoint_mutex
);
566 if (!cds_list_empty(&old_probes
))
567 cds_list_replace_init(&old_probes
, &release_probes
);
570 tracepoint_update_probes();
571 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
572 cds_list_del(&pos
->u
.list
);
577 pthread_mutex_unlock(&tracepoint_mutex
);
580 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
582 new_tracepoint_cb
= cb
;
585 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
587 if (new_tracepoint_cb
) {
588 struct tracepoint
* const *t
;
590 for (t
= start
; t
< end
; t
++) {
592 new_tracepoint_cb(*t
);
598 void lib_disable_tracepoints(struct tracepoint
* const *begin
,
599 struct tracepoint
* const *end
)
601 struct tracepoint
* const *iter
;
603 for (iter
= begin
; iter
< end
; iter
++) {
605 continue; /* skip dummy */
606 disable_tracepoint(*iter
);
611 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
612 int tracepoints_count
)
614 struct tracepoint_lib
*pl
, *iter
;
618 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
620 pl
->tracepoints_start
= tracepoints_start
;
621 pl
->tracepoints_count
= tracepoints_count
;
623 pthread_mutex_lock(&tracepoint_mutex
);
625 * We sort the libs by struct lib pointer address.
627 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
628 BUG_ON(iter
== pl
); /* Should never be in the list twice */
630 /* We belong to the location right after iter. */
631 cds_list_add(&pl
->list
, &iter
->list
);
635 /* We should be added at the head of the list */
636 cds_list_add(&pl
->list
, &libs
);
638 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
640 /* TODO: update just the loaded lib */
641 lib_update_tracepoints();
642 pthread_mutex_unlock(&tracepoint_mutex
);
644 DBG("just registered a tracepoints section from %p and having %d tracepoints",
645 tracepoints_start
, tracepoints_count
);
649 for (i
= 0; i
< tracepoints_count
; i
++) {
650 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
657 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
659 struct tracepoint_lib
*lib
;
660 int tracepoints_count
;
662 pthread_mutex_lock(&tracepoint_mutex
);
663 cds_list_for_each_entry(lib
, &libs
, list
) {
664 if (lib
->tracepoints_start
== tracepoints_start
) {
665 struct tracepoint_lib
*lib2free
= lib
;
667 cds_list_del(&lib
->list
);
668 tracepoints_count
= lib
->tracepoints_count
;
676 * Force tracepoint disarm for all tracepoints of this lib.
677 * This takes care of destructor of library that would leave a
678 * LD_PRELOAD wrapper override function enabled for tracing, but
679 * the session teardown would not be able to reach the
680 * tracepoint anymore to disable it.
682 lib_disable_tracepoints(tracepoints_start
,
683 tracepoints_start
+ tracepoints_count
);
684 DBG("just unregistered a tracepoints section from %p",
687 pthread_mutex_unlock(&tracepoint_mutex
);
691 void init_tracepoint(void)
693 if (uatomic_xchg(&initialized
, 1) == 1)
698 void exit_tracepoint(void)
704 * Create the wrapper symbols.
706 #undef tp_rcu_read_lock_bp
707 #undef tp_rcu_read_unlock_bp
708 #undef tp_rcu_dereference_bp
710 void tp_rcu_read_lock_bp(void)
715 void tp_rcu_read_unlock_bp(void)
717 rcu_read_unlock_bp();
720 void *tp_rcu_dereference_sym_bp(void *p
)
722 return rcu_dereference_bp(p
);