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.
27 #include <urcu/arch.h>
29 #include <urcu/hlist.h>
30 #include <urcu/uatomic.h>
31 #include <urcu/compiler.h>
33 #include <lttng/tracepoint.h>
34 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
36 #include <usterr-signal-safe.h>
39 #include "tracepoint-internal.h"
40 #include "ltt-tracer-core.h"
44 /* Set to 1 to enable tracepoint debug output */
45 static const int tracepoint_debug
;
46 static int initialized
;
47 static void (*new_tracepoint_cb
)(struct tracepoint
*);
50 * tracepoint_mutex nests inside UST mutex.
52 * Note about interaction with fork/clone: UST does not hold the
53 * tracepoint mutex across fork/clone because it is either:
54 * - nested within UST mutex, in which case holding the UST mutex across
56 * - taken by a library constructor, which should never race with a
57 * fork/clone if the application is expected to continue running with
58 * the same memory layout (no following exec()).
60 static pthread_mutex_t tracepoint_mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 * libraries that contain tracepoints (struct tracepoint_lib).
64 * Protected by tracepoint mutex.
66 static CDS_LIST_HEAD(libs
);
69 * The tracepoint mutex protects the library tracepoints, the hash table, and
71 * All calls to the tracepoint API must be protected by the tracepoint mutex,
72 * excepts calls to tracepoint_register_lib and
73 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
77 * Tracepoint hash table, containing the active tracepoints.
78 * Protected by tracepoint mutex.
80 #define TRACEPOINT_HASH_BITS 6
81 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
82 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
84 static CDS_LIST_HEAD(old_probes
);
85 static int need_update
;
89 * It is used to to delay the free of multiple probes array until a quiescent
91 * Tracepoint entries modifications are protected by the tracepoint mutex.
93 struct tracepoint_entry
{
94 struct cds_hlist_node hlist
;
95 struct tracepoint_probe
*probes
;
96 int refcount
; /* Number of times armed. 0 if disarmed. */
97 const char *signature
;
103 struct cds_list_head list
;
104 /* Field below only used for call_rcu scheme */
105 /* struct rcu_head head; */
107 struct tracepoint_probe probes
[0];
110 static void *allocate_probes(int count
)
112 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
113 + sizeof(struct tp_probes
));
114 return p
== NULL
? NULL
: p
->probes
;
117 static void release_probes(void *old
)
120 struct tp_probes
*tp_probes
= caa_container_of(old
,
121 struct tp_probes
, probes
[0]);
127 static void debug_print_probes(struct tracepoint_entry
*entry
)
131 if (!tracepoint_debug
|| !entry
->probes
)
134 for (i
= 0; entry
->probes
[i
].func
; i
++)
135 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
139 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
140 void *probe
, void *data
)
143 struct tracepoint_probe
*old
, *new;
147 debug_print_probes(entry
);
150 /* (N -> N+1), (N != 0, 1) probes */
151 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
152 if (old
[nr_probes
].func
== probe
&&
153 old
[nr_probes
].data
== data
)
154 return ERR_PTR(-EEXIST
);
156 /* + 2 : one for new probe, one for NULL func */
157 new = allocate_probes(nr_probes
+ 2);
159 return ERR_PTR(-ENOMEM
);
161 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
162 new[nr_probes
].func
= probe
;
163 new[nr_probes
].data
= data
;
164 new[nr_probes
+ 1].func
= NULL
;
165 entry
->refcount
= nr_probes
+ 1;
167 debug_print_probes(entry
);
172 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
175 int nr_probes
= 0, nr_del
= 0, i
;
176 struct tracepoint_probe
*old
, *new;
181 return ERR_PTR(-ENOENT
);
183 debug_print_probes(entry
);
184 /* (N -> M), (N > 1, M >= 0) probes */
185 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
187 (old
[nr_probes
].func
== probe
&&
188 old
[nr_probes
].data
== data
))
192 if (nr_probes
- nr_del
== 0) {
193 /* N -> 0, (N > 1) */
194 entry
->probes
= NULL
;
196 debug_print_probes(entry
);
200 /* N -> M, (N > 1, M > 0) */
202 new = allocate_probes(nr_probes
- nr_del
+ 1);
204 return ERR_PTR(-ENOMEM
);
205 for (i
= 0; old
[i
].func
; i
++)
207 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
209 new[nr_probes
- nr_del
].func
= NULL
;
210 entry
->refcount
= nr_probes
- nr_del
;
213 debug_print_probes(entry
);
218 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
219 * Must be called with tracepoint mutex held.
220 * Returns NULL if not present.
222 static struct tracepoint_entry
*get_tracepoint(const char *name
)
224 struct cds_hlist_head
*head
;
225 struct cds_hlist_node
*node
;
226 struct tracepoint_entry
*e
;
227 size_t name_len
= strlen(name
);
230 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
231 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
232 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
234 hash
= jhash(name
, name_len
, 0);
235 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
236 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
237 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
244 * Add the tracepoint to the tracepoint hash table. Must be called with
245 * tracepoint mutex held.
247 static struct tracepoint_entry
*add_tracepoint(const char *name
,
248 const char *signature
)
250 struct cds_hlist_head
*head
;
251 struct cds_hlist_node
*node
;
252 struct tracepoint_entry
*e
;
253 size_t name_len
= strlen(name
);
256 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
257 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
258 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
260 hash
= jhash(name
, name_len
, 0);
261 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
262 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
263 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
264 DBG("tracepoint %s busy", name
);
265 return ERR_PTR(-EEXIST
); /* Already there */
269 * Using zmalloc here to allocate a variable length element. Could
270 * cause some memory fragmentation if overused.
272 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
274 return ERR_PTR(-ENOMEM
);
275 memcpy(&e
->name
[0], name
, name_len
+ 1);
276 e
->name
[name_len
] = '\0';
279 e
->signature
= signature
;
280 cds_hlist_add_head(&e
->hlist
, head
);
285 * Remove the tracepoint from the tracepoint hash table. Must be called with
286 * tracepoint mutex held.
288 static void remove_tracepoint(struct tracepoint_entry
*e
)
290 cds_hlist_del(&e
->hlist
);
295 * Sets the probe callback corresponding to one tracepoint.
297 static void set_tracepoint(struct tracepoint_entry
**entry
,
298 struct tracepoint
*elem
, int active
)
300 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
302 * Check that signatures match before connecting a probe to a
303 * tracepoint. Warn the user if they don't.
305 if (strcmp(elem
->signature
, (*entry
)->signature
) != 0) {
306 static int warned
= 0;
308 /* Only print once, don't flood console. */
310 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
311 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
312 elem
->name
, elem
->signature
, (*entry
)->signature
);
315 /* Don't accept connecting non-matching signatures. */
320 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
321 * probe callbacks array is consistent before setting a pointer to it.
322 * This array is referenced by __DO_TRACE from
323 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
326 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
327 elem
->state
= active
;
331 * Disable a tracepoint and its probe callback.
332 * Note: only waiting an RCU period after setting elem->call to the empty
333 * function insures that the original callback is not used anymore. This insured
334 * by preempt_disable around the call site.
336 static void disable_tracepoint(struct tracepoint
*elem
)
339 rcu_assign_pointer(elem
->probes
, NULL
);
343 * tracepoint_update_probe_range - Update a probe range
344 * @begin: beginning of the range
345 * @end: end of the range
347 * Updates the probe callback corresponding to a range of tracepoints.
350 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
351 struct tracepoint
* const *end
)
353 struct tracepoint
* const *iter
;
354 struct tracepoint_entry
*mark_entry
;
356 for (iter
= begin
; iter
< end
; iter
++) {
358 continue; /* skip dummy */
359 if (!(*iter
)->name
) {
360 disable_tracepoint(*iter
);
363 mark_entry
= get_tracepoint((*iter
)->name
);
365 set_tracepoint(&mark_entry
, *iter
,
366 !!mark_entry
->refcount
);
368 disable_tracepoint(*iter
);
373 static void lib_update_tracepoints(void)
375 struct tracepoint_lib
*lib
;
377 cds_list_for_each_entry(lib
, &libs
, list
) {
378 tracepoint_update_probe_range(lib
->tracepoints_start
,
379 lib
->tracepoints_start
+ lib
->tracepoints_count
);
384 * Update probes, removing the faulty probes.
386 static void tracepoint_update_probes(void)
388 /* tracepoints registered from libraries and executable. */
389 lib_update_tracepoints();
392 static struct tracepoint_probe
*
393 tracepoint_add_probe(const char *name
, void *probe
, void *data
,
394 const char *signature
)
396 struct tracepoint_entry
*entry
;
397 struct tracepoint_probe
*old
;
399 entry
= get_tracepoint(name
);
401 entry
= add_tracepoint(name
, signature
);
403 return (struct tracepoint_probe
*)entry
;
405 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
406 if (IS_ERR(old
) && !entry
->refcount
)
407 remove_tracepoint(entry
);
412 * __tracepoint_probe_register - Connect a probe to a tracepoint
413 * @name: tracepoint name
414 * @probe: probe handler
416 * Returns 0 if ok, error value on error.
417 * The probe address must at least be aligned on the architecture pointer size.
418 * Called with the tracepoint mutex held.
420 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
,
421 const char *signature
)
426 DBG("Registering probe to tracepoint %s", name
);
428 pthread_mutex_lock(&tracepoint_mutex
);
429 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
435 tracepoint_update_probes(); /* may update entry */
438 pthread_mutex_unlock(&tracepoint_mutex
);
442 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
444 struct tracepoint_entry
*entry
;
447 entry
= get_tracepoint(name
);
449 return ERR_PTR(-ENOENT
);
450 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
453 if (!entry
->refcount
)
454 remove_tracepoint(entry
);
459 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
460 * @name: tracepoint name
461 * @probe: probe function pointer
462 * @probe: probe data pointer
464 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
469 DBG("Un-registering probe from tracepoint %s", name
);
471 pthread_mutex_lock(&tracepoint_mutex
);
472 old
= tracepoint_remove_probe(name
, probe
, data
);
477 tracepoint_update_probes(); /* may update entry */
480 pthread_mutex_unlock(&tracepoint_mutex
);
484 static void tracepoint_add_old_probes(void *old
)
488 struct tp_probes
*tp_probes
= caa_container_of(old
,
489 struct tp_probes
, probes
[0]);
490 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
495 * tracepoint_probe_register_noupdate - register a probe but not connect
496 * @name: tracepoint name
497 * @probe: probe handler
499 * caller must call tracepoint_probe_update_all()
501 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
502 void *data
, const char *signature
)
507 pthread_mutex_lock(&tracepoint_mutex
);
508 old
= tracepoint_add_probe(name
, probe
, data
, signature
);
513 tracepoint_add_old_probes(old
);
515 pthread_mutex_unlock(&tracepoint_mutex
);
520 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
521 * @name: tracepoint name
522 * @probe: probe function pointer
524 * caller must call tracepoint_probe_update_all()
525 * Called with the tracepoint mutex held.
527 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
533 DBG("Un-registering probe from tracepoint %s", name
);
535 pthread_mutex_lock(&tracepoint_mutex
);
536 old
= tracepoint_remove_probe(name
, probe
, data
);
541 tracepoint_add_old_probes(old
);
543 pthread_mutex_unlock(&tracepoint_mutex
);
548 * tracepoint_probe_update_all - update tracepoints
550 void tracepoint_probe_update_all(void)
552 CDS_LIST_HEAD(release_probes
);
553 struct tp_probes
*pos
, *next
;
555 pthread_mutex_lock(&tracepoint_mutex
);
559 if (!cds_list_empty(&old_probes
))
560 cds_list_replace_init(&old_probes
, &release_probes
);
563 tracepoint_update_probes();
564 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
565 cds_list_del(&pos
->u
.list
);
570 pthread_mutex_unlock(&tracepoint_mutex
);
573 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
575 new_tracepoint_cb
= cb
;
578 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
580 if (new_tracepoint_cb
) {
581 struct tracepoint
* const *t
;
583 for (t
= start
; t
< end
; t
++) {
585 new_tracepoint_cb(*t
);
591 void lib_disable_tracepoints(struct tracepoint
* const *begin
,
592 struct tracepoint
* const *end
)
594 struct tracepoint
* const *iter
;
596 for (iter
= begin
; iter
< end
; iter
++) {
598 continue; /* skip dummy */
599 disable_tracepoint(*iter
);
604 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
605 int tracepoints_count
)
607 struct tracepoint_lib
*pl
, *iter
;
611 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
613 pl
->tracepoints_start
= tracepoints_start
;
614 pl
->tracepoints_count
= tracepoints_count
;
616 pthread_mutex_lock(&tracepoint_mutex
);
618 * We sort the libs by struct lib pointer address.
620 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
621 BUG_ON(iter
== pl
); /* Should never be in the list twice */
623 /* We belong to the location right after iter. */
624 cds_list_add(&pl
->list
, &iter
->list
);
628 /* We should be added at the head of the list */
629 cds_list_add(&pl
->list
, &libs
);
631 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
633 /* TODO: update just the loaded lib */
634 lib_update_tracepoints();
635 pthread_mutex_unlock(&tracepoint_mutex
);
637 DBG("just registered a tracepoints section from %p and having %d tracepoints",
638 tracepoints_start
, tracepoints_count
);
642 for (i
= 0; i
< tracepoints_count
; i
++) {
643 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
650 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
652 struct tracepoint_lib
*lib
;
653 int tracepoints_count
;
655 pthread_mutex_lock(&tracepoint_mutex
);
656 cds_list_for_each_entry(lib
, &libs
, list
) {
657 if (lib
->tracepoints_start
== tracepoints_start
) {
658 struct tracepoint_lib
*lib2free
= lib
;
660 cds_list_del(&lib
->list
);
661 tracepoints_count
= lib
->tracepoints_count
;
669 * Force tracepoint disarm for all tracepoints of this lib.
670 * This takes care of destructor of library that would leave a
671 * LD_PRELOAD wrapper override function enabled for tracing, but
672 * the session teardown would not be able to reach the
673 * tracepoint anymore to disable it.
675 lib_disable_tracepoints(tracepoints_start
,
676 tracepoints_start
+ tracepoints_count
);
677 DBG("just unregistered a tracepoints section from %p",
680 pthread_mutex_unlock(&tracepoint_mutex
);
684 void init_tracepoint(void)
686 if (uatomic_xchg(&initialized
, 1) == 1)
691 void exit_tracepoint(void)
697 * Create the wrapper symbols.
699 #undef tp_rcu_read_lock_bp
700 #undef tp_rcu_read_unlock_bp
701 #undef tp_rcu_dereference_bp
703 void tp_rcu_read_lock_bp(void)
708 void tp_rcu_read_unlock_bp(void)
710 rcu_read_unlock_bp();
713 void *tp_rcu_dereference_sym_bp(void *p
)
715 return rcu_dereference_bp(p
);