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. */
102 struct cds_list_head list
;
103 /* Field below only used for call_rcu scheme */
104 /* struct rcu_head head; */
106 struct tracepoint_probe probes
[0];
109 static void *allocate_probes(int count
)
111 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
112 + sizeof(struct tp_probes
));
113 return p
== NULL
? NULL
: p
->probes
;
116 static void release_probes(void *old
)
119 struct tp_probes
*tp_probes
= caa_container_of(old
,
120 struct tp_probes
, probes
[0]);
126 static void debug_print_probes(struct tracepoint_entry
*entry
)
130 if (!tracepoint_debug
|| !entry
->probes
)
133 for (i
= 0; entry
->probes
[i
].func
; i
++)
134 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
138 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
139 void *probe
, void *data
)
142 struct tracepoint_probe
*old
, *new;
146 debug_print_probes(entry
);
149 /* (N -> N+1), (N != 0, 1) probes */
150 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
151 if (old
[nr_probes
].func
== probe
&&
152 old
[nr_probes
].data
== data
)
153 return ERR_PTR(-EEXIST
);
155 /* + 2 : one for new probe, one for NULL func */
156 new = allocate_probes(nr_probes
+ 2);
158 return ERR_PTR(-ENOMEM
);
160 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
161 new[nr_probes
].func
= probe
;
162 new[nr_probes
].data
= data
;
163 new[nr_probes
+ 1].func
= NULL
;
164 entry
->refcount
= nr_probes
+ 1;
166 debug_print_probes(entry
);
171 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
174 int nr_probes
= 0, nr_del
= 0, i
;
175 struct tracepoint_probe
*old
, *new;
180 return ERR_PTR(-ENOENT
);
182 debug_print_probes(entry
);
183 /* (N -> M), (N > 1, M >= 0) probes */
184 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
186 (old
[nr_probes
].func
== probe
&&
187 old
[nr_probes
].data
== data
))
191 if (nr_probes
- nr_del
== 0) {
192 /* N -> 0, (N > 1) */
193 entry
->probes
= NULL
;
195 debug_print_probes(entry
);
199 /* N -> M, (N > 1, M > 0) */
201 new = allocate_probes(nr_probes
- nr_del
+ 1);
203 return ERR_PTR(-ENOMEM
);
204 for (i
= 0; old
[i
].func
; i
++)
206 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
208 new[nr_probes
- nr_del
].func
= NULL
;
209 entry
->refcount
= nr_probes
- nr_del
;
212 debug_print_probes(entry
);
217 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
218 * Must be called with tracepoint mutex held.
219 * Returns NULL if not present.
221 static struct tracepoint_entry
*get_tracepoint(const char *name
)
223 struct cds_hlist_head
*head
;
224 struct cds_hlist_node
*node
;
225 struct tracepoint_entry
*e
;
226 size_t name_len
= strlen(name
);
229 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
230 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
231 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
233 hash
= jhash(name
, name_len
, 0);
234 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
235 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
236 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
243 * Add the tracepoint to the tracepoint hash table. Must be called with
244 * tracepoint mutex held.
246 static struct tracepoint_entry
*add_tracepoint(const char *name
)
248 struct cds_hlist_head
*head
;
249 struct cds_hlist_node
*node
;
250 struct tracepoint_entry
*e
;
251 size_t name_len
= strlen(name
);
254 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
255 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
256 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
258 hash
= jhash(name
, name_len
, 0);
259 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
260 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
261 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
262 DBG("tracepoint %s busy", name
);
263 return ERR_PTR(-EEXIST
); /* Already there */
267 * Using zmalloc here to allocate a variable length element. Could
268 * cause some memory fragmentation if overused.
270 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
272 return ERR_PTR(-ENOMEM
);
273 memcpy(&e
->name
[0], name
, name_len
+ 1);
274 e
->name
[name_len
] = '\0';
277 cds_hlist_add_head(&e
->hlist
, head
);
282 * Remove the tracepoint from the tracepoint hash table. Must be called with
283 * tracepoint mutex held.
285 static void remove_tracepoint(struct tracepoint_entry
*e
)
287 cds_hlist_del(&e
->hlist
);
292 * Sets the probe callback corresponding to one tracepoint.
294 static void set_tracepoint(struct tracepoint_entry
**entry
,
295 struct tracepoint
*elem
, int active
)
297 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
300 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
301 * probe callbacks array is consistent before setting a pointer to it.
302 * This array is referenced by __DO_TRACE from
303 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
306 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
307 elem
->state
= active
;
311 * Disable a tracepoint and its probe callback.
312 * Note: only waiting an RCU period after setting elem->call to the empty
313 * function insures that the original callback is not used anymore. This insured
314 * by preempt_disable around the call site.
316 static void disable_tracepoint(struct tracepoint
*elem
)
319 rcu_assign_pointer(elem
->probes
, NULL
);
323 * tracepoint_update_probe_range - Update a probe range
324 * @begin: beginning of the range
325 * @end: end of the range
327 * Updates the probe callback corresponding to a range of tracepoints.
330 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
331 struct tracepoint
* const *end
)
333 struct tracepoint
* const *iter
;
334 struct tracepoint_entry
*mark_entry
;
336 for (iter
= begin
; iter
< end
; iter
++) {
338 continue; /* skip dummy */
339 if (!(*iter
)->name
) {
340 disable_tracepoint(*iter
);
343 mark_entry
= get_tracepoint((*iter
)->name
);
345 set_tracepoint(&mark_entry
, *iter
,
346 !!mark_entry
->refcount
);
348 disable_tracepoint(*iter
);
353 static void lib_update_tracepoints(void)
355 struct tracepoint_lib
*lib
;
357 cds_list_for_each_entry(lib
, &libs
, list
) {
358 tracepoint_update_probe_range(lib
->tracepoints_start
,
359 lib
->tracepoints_start
+ lib
->tracepoints_count
);
364 * Update probes, removing the faulty probes.
366 static void tracepoint_update_probes(void)
368 /* tracepoints registered from libraries and executable. */
369 lib_update_tracepoints();
372 static struct tracepoint_probe
*
373 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
375 struct tracepoint_entry
*entry
;
376 struct tracepoint_probe
*old
;
378 entry
= get_tracepoint(name
);
380 entry
= add_tracepoint(name
);
382 return (struct tracepoint_probe
*)entry
;
384 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
385 if (IS_ERR(old
) && !entry
->refcount
)
386 remove_tracepoint(entry
);
391 * __tracepoint_probe_register - Connect a probe to a tracepoint
392 * @name: tracepoint name
393 * @probe: probe handler
395 * Returns 0 if ok, error value on error.
396 * The probe address must at least be aligned on the architecture pointer size.
397 * Called with the tracepoint mutex held.
399 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
404 DBG("Registering probe to tracepoint %s", name
);
406 pthread_mutex_lock(&tracepoint_mutex
);
407 old
= tracepoint_add_probe(name
, probe
, data
);
413 tracepoint_update_probes(); /* may update entry */
416 pthread_mutex_unlock(&tracepoint_mutex
);
420 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
422 struct tracepoint_entry
*entry
;
425 entry
= get_tracepoint(name
);
427 return ERR_PTR(-ENOENT
);
428 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
431 if (!entry
->refcount
)
432 remove_tracepoint(entry
);
437 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
438 * @name: tracepoint name
439 * @probe: probe function pointer
440 * @probe: probe data pointer
442 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
447 DBG("Un-registering probe from tracepoint %s", name
);
449 pthread_mutex_lock(&tracepoint_mutex
);
450 old
= tracepoint_remove_probe(name
, probe
, data
);
455 tracepoint_update_probes(); /* may update entry */
458 pthread_mutex_unlock(&tracepoint_mutex
);
462 static void tracepoint_add_old_probes(void *old
)
466 struct tp_probes
*tp_probes
= caa_container_of(old
,
467 struct tp_probes
, probes
[0]);
468 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
473 * tracepoint_probe_register_noupdate - register a probe but not connect
474 * @name: tracepoint name
475 * @probe: probe handler
477 * caller must call tracepoint_probe_update_all()
479 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
485 pthread_mutex_lock(&tracepoint_mutex
);
486 old
= tracepoint_add_probe(name
, probe
, data
);
491 tracepoint_add_old_probes(old
);
493 pthread_mutex_unlock(&tracepoint_mutex
);
498 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
499 * @name: tracepoint name
500 * @probe: probe function pointer
502 * caller must call tracepoint_probe_update_all()
503 * Called with the tracepoint mutex held.
505 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
511 DBG("Un-registering probe from tracepoint %s", name
);
513 pthread_mutex_lock(&tracepoint_mutex
);
514 old
= tracepoint_remove_probe(name
, probe
, data
);
519 tracepoint_add_old_probes(old
);
521 pthread_mutex_unlock(&tracepoint_mutex
);
526 * tracepoint_probe_update_all - update tracepoints
528 void tracepoint_probe_update_all(void)
530 CDS_LIST_HEAD(release_probes
);
531 struct tp_probes
*pos
, *next
;
533 pthread_mutex_lock(&tracepoint_mutex
);
537 if (!cds_list_empty(&old_probes
))
538 cds_list_replace_init(&old_probes
, &release_probes
);
541 tracepoint_update_probes();
542 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
543 cds_list_del(&pos
->u
.list
);
548 pthread_mutex_unlock(&tracepoint_mutex
);
551 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
553 new_tracepoint_cb
= cb
;
556 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
558 if (new_tracepoint_cb
) {
559 struct tracepoint
* const *t
;
561 for (t
= start
; t
< end
; t
++) {
563 new_tracepoint_cb(*t
);
569 void lib_disable_tracepoints(struct tracepoint
* const *begin
,
570 struct tracepoint
* const *end
)
572 struct tracepoint
* const *iter
;
574 for (iter
= begin
; iter
< end
; iter
++) {
576 continue; /* skip dummy */
577 disable_tracepoint(*iter
);
582 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
583 int tracepoints_count
)
585 struct tracepoint_lib
*pl
, *iter
;
589 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
591 pl
->tracepoints_start
= tracepoints_start
;
592 pl
->tracepoints_count
= tracepoints_count
;
594 pthread_mutex_lock(&tracepoint_mutex
);
596 * We sort the libs by struct lib pointer address.
598 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
599 BUG_ON(iter
== pl
); /* Should never be in the list twice */
601 /* We belong to the location right after iter. */
602 cds_list_add(&pl
->list
, &iter
->list
);
606 /* We should be added at the head of the list */
607 cds_list_add(&pl
->list
, &libs
);
609 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
611 /* TODO: update just the loaded lib */
612 lib_update_tracepoints();
613 pthread_mutex_unlock(&tracepoint_mutex
);
615 DBG("just registered a tracepoints section from %p and having %d tracepoints",
616 tracepoints_start
, tracepoints_count
);
620 for (i
= 0; i
< tracepoints_count
; i
++) {
621 DBG("registered tracepoint: %s", tracepoints_start
[i
]->name
);
628 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
630 struct tracepoint_lib
*lib
;
631 int tracepoints_count
;
633 pthread_mutex_lock(&tracepoint_mutex
);
634 cds_list_for_each_entry(lib
, &libs
, list
) {
635 if (lib
->tracepoints_start
== tracepoints_start
) {
636 struct tracepoint_lib
*lib2free
= lib
;
638 cds_list_del(&lib
->list
);
639 tracepoints_count
= lib
->tracepoints_count
;
647 * Force tracepoint disarm for all tracepoints of this lib.
648 * This takes care of destructor of library that would leave a
649 * LD_PRELOAD wrapper override function enabled for tracing, but
650 * the session teardown would not be able to reach the
651 * tracepoint anymore to disable it.
653 lib_disable_tracepoints(tracepoints_start
,
654 tracepoints_start
+ tracepoints_count
);
655 DBG("just unregistered a tracepoints section from %p",
658 pthread_mutex_unlock(&tracepoint_mutex
);
662 void init_tracepoint(void)
664 if (uatomic_xchg(&initialized
, 1) == 1)
669 void exit_tracepoint(void)
675 * Create the wrapper symbols.
677 #undef tp_rcu_read_lock_bp
678 #undef tp_rcu_read_unlock_bp
679 #undef tp_rcu_dereference_bp
681 void tp_rcu_read_lock_bp(void)
686 void tp_rcu_read_unlock_bp(void)
688 rcu_read_unlock_bp();
691 void *tp_rcu_dereference_sym_bp(void *p
)
693 return rcu_dereference_bp(p
);