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 * libraries that contain tracepoints (struct tracepoint_lib).
51 * Protected by UST lock.
53 static CDS_LIST_HEAD(libs
);
56 * The UST lock protects the library tracepoints, the hash table, and
58 * All calls to the tracepoint API must be protected by the UST lock,
59 * excepts calls to tracepoint_register_lib and
60 * tracepoint_unregister_lib, which take the UST lock themselves.
64 * Tracepoint hash table, containing the active tracepoints.
65 * Protected by ust lock.
67 #define TRACEPOINT_HASH_BITS 6
68 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
69 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
71 static CDS_LIST_HEAD(old_probes
);
72 static int need_update
;
76 * It is used to to delay the free of multiple probes array until a quiescent
78 * Tracepoint entries modifications are protected by the ust lock.
80 struct tracepoint_entry
{
81 struct cds_hlist_node hlist
;
82 struct tracepoint_probe
*probes
;
83 int refcount
; /* Number of times armed. 0 if disarmed. */
89 struct cds_list_head list
;
91 struct tracepoint_probe probes
[0];
94 static void *allocate_probes(int count
)
96 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
97 + sizeof(struct tp_probes
));
98 return p
== NULL
? NULL
: p
->probes
;
101 static void release_probes(void *old
)
104 struct tp_probes
*tp_probes
= caa_container_of(old
,
105 struct tp_probes
, probes
[0]);
111 static void debug_print_probes(struct tracepoint_entry
*entry
)
115 if (!tracepoint_debug
|| !entry
->probes
)
118 for (i
= 0; entry
->probes
[i
].func
; i
++)
119 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
123 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
124 void *probe
, void *data
)
127 struct tracepoint_probe
*old
, *new;
131 debug_print_probes(entry
);
134 /* (N -> N+1), (N != 0, 1) probes */
135 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
136 if (old
[nr_probes
].func
== probe
&&
137 old
[nr_probes
].data
== data
)
138 return ERR_PTR(-EEXIST
);
140 /* + 2 : one for new probe, one for NULL func */
141 new = allocate_probes(nr_probes
+ 2);
143 return ERR_PTR(-ENOMEM
);
145 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
146 new[nr_probes
].func
= probe
;
147 new[nr_probes
].data
= data
;
148 new[nr_probes
+ 1].func
= NULL
;
149 entry
->refcount
= nr_probes
+ 1;
151 debug_print_probes(entry
);
156 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
159 int nr_probes
= 0, nr_del
= 0, i
;
160 struct tracepoint_probe
*old
, *new;
165 return ERR_PTR(-ENOENT
);
167 debug_print_probes(entry
);
168 /* (N -> M), (N > 1, M >= 0) probes */
169 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
171 (old
[nr_probes
].func
== probe
&&
172 old
[nr_probes
].data
== data
))
176 if (nr_probes
- nr_del
== 0) {
177 /* N -> 0, (N > 1) */
178 entry
->probes
= NULL
;
180 debug_print_probes(entry
);
184 /* N -> M, (N > 1, M > 0) */
186 new = allocate_probes(nr_probes
- nr_del
+ 1);
188 return ERR_PTR(-ENOMEM
);
189 for (i
= 0; old
[i
].func
; i
++)
191 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
193 new[nr_probes
- nr_del
].func
= NULL
;
194 entry
->refcount
= nr_probes
- nr_del
;
197 debug_print_probes(entry
);
202 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
203 * Must be called with ust lock held.
204 * Returns NULL if not present.
206 static struct tracepoint_entry
*get_tracepoint(const char *name
)
208 struct cds_hlist_head
*head
;
209 struct cds_hlist_node
*node
;
210 struct tracepoint_entry
*e
;
211 size_t name_len
= strlen(name
);
214 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
215 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
216 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
218 hash
= jhash(name
, name_len
, 0);
219 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
220 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
221 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
228 * Add the tracepoint to the tracepoint hash table. Must be called with
231 static struct tracepoint_entry
*add_tracepoint(const char *name
)
233 struct cds_hlist_head
*head
;
234 struct cds_hlist_node
*node
;
235 struct tracepoint_entry
*e
;
236 size_t name_len
= strlen(name
);
239 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
240 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
241 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
243 hash
= jhash(name
, name_len
, 0);
244 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
245 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
246 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
247 DBG("tracepoint %s busy", name
);
248 return ERR_PTR(-EEXIST
); /* Already there */
252 * Using zmalloc here to allocate a variable length element. Could
253 * cause some memory fragmentation if overused.
255 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
257 return ERR_PTR(-ENOMEM
);
258 memcpy(&e
->name
[0], name
, name_len
+ 1);
259 e
->name
[name_len
] = '\0';
262 cds_hlist_add_head(&e
->hlist
, head
);
267 * Remove the tracepoint from the tracepoint hash table. Must be called with
270 static void remove_tracepoint(struct tracepoint_entry
*e
)
272 cds_hlist_del(&e
->hlist
);
277 * Sets the probe callback corresponding to one tracepoint.
279 static void set_tracepoint(struct tracepoint_entry
**entry
,
280 struct tracepoint
*elem
, int active
)
282 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
285 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
286 * probe callbacks array is consistent before setting a pointer to it.
287 * This array is referenced by __DO_TRACE from
288 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
291 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
292 elem
->state
= active
;
296 * Disable a tracepoint and its probe callback.
297 * Note: only waiting an RCU period after setting elem->call to the empty
298 * function insures that the original callback is not used anymore. This insured
299 * by preempt_disable around the call site.
301 static void disable_tracepoint(struct tracepoint
*elem
)
304 rcu_assign_pointer(elem
->probes
, NULL
);
308 * tracepoint_update_probe_range - Update a probe range
309 * @begin: beginning of the range
310 * @end: end of the range
312 * Updates the probe callback corresponding to a range of tracepoints.
315 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
316 struct tracepoint
* const *end
)
318 struct tracepoint
* const *iter
;
319 struct tracepoint_entry
*mark_entry
;
321 for (iter
= begin
; iter
< end
; iter
++) {
323 continue; /* skip dummy */
324 if (!(*iter
)->name
) {
325 disable_tracepoint(*iter
);
328 mark_entry
= get_tracepoint((*iter
)->name
);
330 set_tracepoint(&mark_entry
, *iter
,
331 !!mark_entry
->refcount
);
333 disable_tracepoint(*iter
);
338 static void lib_update_tracepoints(void)
340 struct tracepoint_lib
*lib
;
342 cds_list_for_each_entry(lib
, &libs
, list
) {
343 tracepoint_update_probe_range(lib
->tracepoints_start
,
344 lib
->tracepoints_start
+ lib
->tracepoints_count
);
349 * Update probes, removing the faulty probes.
351 static void tracepoint_update_probes(void)
353 /* tracepoints registered from libraries and executable. */
354 lib_update_tracepoints();
357 static struct tracepoint_probe
*
358 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
360 struct tracepoint_entry
*entry
;
361 struct tracepoint_probe
*old
;
363 entry
= get_tracepoint(name
);
365 entry
= add_tracepoint(name
);
367 return (struct tracepoint_probe
*)entry
;
369 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
370 if (IS_ERR(old
) && !entry
->refcount
)
371 remove_tracepoint(entry
);
376 * __tracepoint_probe_register - Connect a probe to a tracepoint
377 * @name: tracepoint name
378 * @probe: probe handler
380 * Returns 0 if ok, error value on error.
381 * The probe address must at least be aligned on the architecture pointer size.
382 * Called with the UST lock held.
384 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
388 old
= tracepoint_add_probe(name
, probe
, data
);
392 tracepoint_update_probes(); /* may update entry */
397 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
399 struct tracepoint_entry
*entry
;
402 entry
= get_tracepoint(name
);
404 return ERR_PTR(-ENOENT
);
405 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
408 if (!entry
->refcount
)
409 remove_tracepoint(entry
);
414 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
415 * @name: tracepoint name
416 * @probe: probe function pointer
417 * @probe: probe data pointer
419 * Called with the UST lock held.
421 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
425 old
= tracepoint_remove_probe(name
, probe
, data
);
429 tracepoint_update_probes(); /* may update entry */
434 static void tracepoint_add_old_probes(void *old
)
438 struct tp_probes
*tp_probes
= caa_container_of(old
,
439 struct tp_probes
, probes
[0]);
440 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
445 * tracepoint_probe_register_noupdate - register a probe but not connect
446 * @name: tracepoint name
447 * @probe: probe handler
449 * caller must call tracepoint_probe_update_all()
450 * Called with the UST lock held.
452 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
457 old
= tracepoint_add_probe(name
, probe
, data
);
461 tracepoint_add_old_probes(old
);
466 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
467 * @name: tracepoint name
468 * @probe: probe function pointer
470 * caller must call tracepoint_probe_update_all()
471 * Called with the UST lock held.
473 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
478 old
= tracepoint_remove_probe(name
, probe
, data
);
482 tracepoint_add_old_probes(old
);
487 * tracepoint_probe_update_all - update tracepoints
488 * Called with the UST lock held.
490 void tracepoint_probe_update_all(void)
492 CDS_LIST_HEAD(release_probes
);
493 struct tp_probes
*pos
, *next
;
498 if (!cds_list_empty(&old_probes
))
499 cds_list_replace_init(&old_probes
, &release_probes
);
502 tracepoint_update_probes();
503 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
504 cds_list_del(&pos
->u
.list
);
510 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
512 new_tracepoint_cb
= cb
;
515 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
517 if (new_tracepoint_cb
) {
518 struct tracepoint
* const *t
;
520 for (t
= start
; t
< end
; t
++) {
522 new_tracepoint_cb(*t
);
528 void lib_disable_tracepoints(struct tracepoint
* const *begin
,
529 struct tracepoint
* const *end
)
531 struct tracepoint
* const *iter
;
533 for (iter
= begin
; iter
< end
; iter
++) {
535 continue; /* skip dummy */
536 disable_tracepoint(*iter
);
541 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
542 int tracepoints_count
)
544 struct tracepoint_lib
*pl
, *iter
;
548 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
550 pl
->tracepoints_start
= tracepoints_start
;
551 pl
->tracepoints_count
= tracepoints_count
;
555 * We sort the libs by struct lib pointer address.
557 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
558 BUG_ON(iter
== pl
); /* Should never be in the list twice */
560 /* We belong to the location right after iter. */
561 cds_list_add(&pl
->list
, &iter
->list
);
565 /* We should be added at the head of the list */
566 cds_list_add(&pl
->list
, &libs
);
568 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
570 /* TODO: update just the loaded lib */
571 lib_update_tracepoints();
574 DBG("just registered a tracepoints section from %p and having %d tracepoints",
575 tracepoints_start
, tracepoints_count
);
580 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
582 struct tracepoint_lib
*lib
;
583 int tracepoints_count
;
586 cds_list_for_each_entry(lib
, &libs
, list
) {
587 if (lib
->tracepoints_start
== tracepoints_start
) {
588 struct tracepoint_lib
*lib2free
= lib
;
590 cds_list_del(&lib
->list
);
591 tracepoints_count
= lib
->tracepoints_count
;
599 * Force tracepoint disarm for all tracepoints of this lib.
600 * This takes care of destructor of library that would leave a
601 * LD_PRELOAD wrapper override function enabled for tracing, but
602 * the session teardown would not be able to reach the
603 * tracepoint anymore to disable it.
605 lib_disable_tracepoints(tracepoints_start
,
606 tracepoints_start
+ tracepoints_count
);
607 DBG("just unregistered a tracepoints section from %p",
614 void init_tracepoint(void)
616 if (uatomic_xchg(&initialized
, 1) == 1)
621 void exit_tracepoint(void)
627 * Create the wrapper symbols.
629 #undef tp_rcu_read_lock_bp
630 #undef tp_rcu_read_unlock_bp
631 #undef tp_rcu_dereference_bp
633 void tp_rcu_read_lock_bp(void)
638 void tp_rcu_read_unlock_bp(void)
640 rcu_read_unlock_bp();
643 void *tp_rcu_dereference_sym_bp(void *p
)
645 return rcu_dereference_bp(p
);