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>
35 #include <usterr-signal-safe.h>
38 #include "tracepoint-internal.h"
39 #include "ltt-tracer-core.h"
43 /* Set to 1 to enable tracepoint debug output */
44 static const int tracepoint_debug
;
45 static int initialized
;
46 static void (*new_tracepoint_cb
)(struct tracepoint
*);
48 /* libraries that contain tracepoints (struct tracepoint_lib) */
49 static CDS_LIST_HEAD(libs
);
52 * The UST lock protects the library tracepoints, the hash table, and
54 * All calls to the tracepoint API must be protected by the UST lock,
55 * excepts calls to tracepoint_register_lib and
56 * tracepoint_unregister_lib, which take the UST lock themselves.
60 * Tracepoint hash table, containing the active tracepoints.
61 * Protected by tracepoints_mutex.
63 #define TRACEPOINT_HASH_BITS 6
64 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
65 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
67 static CDS_LIST_HEAD(old_probes
);
68 static int need_update
;
72 * It is used to to delay the free of multiple probes array until a quiescent
74 * Tracepoint entries modifications are protected by the tracepoints_mutex.
76 struct tracepoint_entry
{
77 struct cds_hlist_node hlist
;
78 struct tracepoint_probe
*probes
;
79 int refcount
; /* Number of times armed. 0 if disarmed. */
85 struct cds_list_head list
;
87 struct tracepoint_probe probes
[0];
90 static inline void *allocate_probes(int count
)
92 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
93 + sizeof(struct tp_probes
));
94 return p
== NULL
? NULL
: p
->probes
;
97 static inline void release_probes(void *old
)
100 struct tp_probes
*tp_probes
= caa_container_of(old
,
101 struct tp_probes
, probes
[0]);
107 static void debug_print_probes(struct tracepoint_entry
*entry
)
111 if (!tracepoint_debug
|| !entry
->probes
)
114 for (i
= 0; entry
->probes
[i
].func
; i
++)
115 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
119 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
120 void *probe
, void *data
)
123 struct tracepoint_probe
*old
, *new;
127 debug_print_probes(entry
);
130 /* (N -> N+1), (N != 0, 1) probes */
131 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
132 if (old
[nr_probes
].func
== probe
&&
133 old
[nr_probes
].data
== data
)
134 return ERR_PTR(-EEXIST
);
136 /* + 2 : one for new probe, one for NULL func */
137 new = allocate_probes(nr_probes
+ 2);
139 return ERR_PTR(-ENOMEM
);
141 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
142 new[nr_probes
].func
= probe
;
143 new[nr_probes
].data
= data
;
144 new[nr_probes
+ 1].func
= NULL
;
145 entry
->refcount
= nr_probes
+ 1;
147 debug_print_probes(entry
);
152 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
155 int nr_probes
= 0, nr_del
= 0, i
;
156 struct tracepoint_probe
*old
, *new;
161 return ERR_PTR(-ENOENT
);
163 debug_print_probes(entry
);
164 /* (N -> M), (N > 1, M >= 0) probes */
165 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
167 (old
[nr_probes
].func
== probe
&&
168 old
[nr_probes
].data
== data
))
172 if (nr_probes
- nr_del
== 0) {
173 /* N -> 0, (N > 1) */
174 entry
->probes
= NULL
;
176 debug_print_probes(entry
);
180 /* N -> M, (N > 1, M > 0) */
182 new = allocate_probes(nr_probes
- nr_del
+ 1);
184 return ERR_PTR(-ENOMEM
);
185 for (i
= 0; old
[i
].func
; i
++)
187 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
189 new[nr_probes
- nr_del
].func
= NULL
;
190 entry
->refcount
= nr_probes
- nr_del
;
193 debug_print_probes(entry
);
198 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
199 * Must be called with tracepoints_mutex held.
200 * Returns NULL if not present.
202 static struct tracepoint_entry
*get_tracepoint(const char *name
)
204 struct cds_hlist_head
*head
;
205 struct cds_hlist_node
*node
;
206 struct tracepoint_entry
*e
;
207 uint32_t hash
= jhash(name
, strlen(name
), 0);
209 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
210 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
211 if (!strcmp(name
, e
->name
))
218 * Add the tracepoint to the tracepoint hash table. Must be called with
219 * tracepoints_mutex held.
221 static struct tracepoint_entry
*add_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
) + 1;
227 uint32_t hash
= jhash(name
, name_len
-1, 0);
229 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
230 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
231 if (!strcmp(name
, e
->name
)) {
232 DBG("tracepoint %s busy", name
);
233 return ERR_PTR(-EEXIST
); /* Already there */
237 * Using zmalloc here to allocate a variable length element. Could
238 * cause some memory fragmentation if overused.
240 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
242 return ERR_PTR(-ENOMEM
);
243 memcpy(&e
->name
[0], name
, name_len
);
246 cds_hlist_add_head(&e
->hlist
, head
);
251 * Remove the tracepoint from the tracepoint hash table. Must be called with
254 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
256 cds_hlist_del(&e
->hlist
);
261 * Sets the probe callback corresponding to one tracepoint.
263 static void set_tracepoint(struct tracepoint_entry
**entry
,
264 struct tracepoint
*elem
, int active
)
266 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
269 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
270 * probe callbacks array is consistent before setting a pointer to it.
271 * This array is referenced by __DO_TRACE from
272 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
275 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
276 elem
->state
= active
;
280 * Disable a tracepoint and its probe callback.
281 * Note: only waiting an RCU period after setting elem->call to the empty
282 * function insures that the original callback is not used anymore. This insured
283 * by preempt_disable around the call site.
285 static void disable_tracepoint(struct tracepoint
*elem
)
288 rcu_assign_pointer(elem
->probes
, NULL
);
292 * tracepoint_update_probe_range - Update a probe range
293 * @begin: beginning of the range
294 * @end: end of the range
296 * Updates the probe callback corresponding to a range of tracepoints.
299 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
300 struct tracepoint
* const *end
)
302 struct tracepoint
* const *iter
;
303 struct tracepoint_entry
*mark_entry
;
305 for (iter
= begin
; iter
< end
; iter
++) {
307 continue; /* skip dummy */
308 if (!(*iter
)->name
) {
309 disable_tracepoint(*iter
);
312 mark_entry
= get_tracepoint((*iter
)->name
);
314 set_tracepoint(&mark_entry
, *iter
,
315 !!mark_entry
->refcount
);
317 disable_tracepoint(*iter
);
322 static void lib_update_tracepoints(void)
324 struct tracepoint_lib
*lib
;
326 cds_list_for_each_entry(lib
, &libs
, list
) {
327 tracepoint_update_probe_range(lib
->tracepoints_start
,
328 lib
->tracepoints_start
+ lib
->tracepoints_count
);
333 * Update probes, removing the faulty probes.
335 static void tracepoint_update_probes(void)
337 /* tracepoints registered from libraries and executable. */
338 lib_update_tracepoints();
341 static struct tracepoint_probe
*
342 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
344 struct tracepoint_entry
*entry
;
345 struct tracepoint_probe
*old
;
347 entry
= get_tracepoint(name
);
349 entry
= add_tracepoint(name
);
351 return (struct tracepoint_probe
*)entry
;
353 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
354 if (IS_ERR(old
) && !entry
->refcount
)
355 remove_tracepoint(entry
);
360 * __tracepoint_probe_register - Connect a probe to a tracepoint
361 * @name: tracepoint name
362 * @probe: probe handler
364 * Returns 0 if ok, error value on error.
365 * The probe address must at least be aligned on the architecture pointer size.
366 * Called with the UST lock held.
368 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
372 old
= tracepoint_add_probe(name
, probe
, data
);
376 tracepoint_update_probes(); /* may update entry */
381 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
383 struct tracepoint_entry
*entry
;
386 entry
= get_tracepoint(name
);
388 return ERR_PTR(-ENOENT
);
389 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
392 if (!entry
->refcount
)
393 remove_tracepoint(entry
);
398 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
399 * @name: tracepoint name
400 * @probe: probe function pointer
401 * @probe: probe data pointer
403 * Called with the UST lock held.
405 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
409 old
= tracepoint_remove_probe(name
, probe
, data
);
413 tracepoint_update_probes(); /* may update entry */
418 static void tracepoint_add_old_probes(void *old
)
422 struct tp_probes
*tp_probes
= caa_container_of(old
,
423 struct tp_probes
, probes
[0]);
424 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
429 * tracepoint_probe_register_noupdate - register a probe but not connect
430 * @name: tracepoint name
431 * @probe: probe handler
433 * caller must call tracepoint_probe_update_all()
434 * Called with the UST lock held.
436 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
441 old
= tracepoint_add_probe(name
, probe
, data
);
445 tracepoint_add_old_probes(old
);
450 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
451 * @name: tracepoint name
452 * @probe: probe function pointer
454 * caller must call tracepoint_probe_update_all()
455 * Called with the UST lock held.
457 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
462 old
= tracepoint_remove_probe(name
, probe
, data
);
466 tracepoint_add_old_probes(old
);
471 * tracepoint_probe_update_all - update tracepoints
472 * Called with the UST lock held.
474 void tracepoint_probe_update_all(void)
476 CDS_LIST_HEAD(release_probes
);
477 struct tp_probes
*pos
, *next
;
482 if (!cds_list_empty(&old_probes
))
483 cds_list_replace_init(&old_probes
, &release_probes
);
486 tracepoint_update_probes();
487 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
488 cds_list_del(&pos
->u
.list
);
495 * Returns 0 if current not found.
496 * Returns 1 if current found.
498 * Called with tracepoint mutex held
500 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
502 struct tracepoint_lib
*iter_lib
;
505 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
506 if (iter_lib
< iter
->lib
)
508 else if (iter_lib
> iter
->lib
)
509 iter
->tracepoint
= NULL
;
510 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
511 iter_lib
->tracepoints_start
,
512 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
514 iter
->lib
= iter_lib
;
522 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
523 * @tracepoint: current tracepoints (in), next tracepoint (out)
524 * @begin: beginning of the range
525 * @end: end of the range
527 * Returns whether a next tracepoint has been found (1) or not (0).
528 * Will return the first tracepoint in the range if the input tracepoint is
530 * Called with tracepoint mutex held.
532 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
533 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
535 if (!*tracepoint
&& begin
!= end
)
537 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
539 (*tracepoint
)++; /* skip dummy */
547 * Called with tracepoint mutex held.
549 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
553 /* tracepoints in libs. */
554 found
= lib_get_iter_tracepoints(iter
);
556 tracepoint_iter_reset(iter
);
560 * Called with UST lock held.
562 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
564 tracepoint_get_iter(iter
);
568 * Called with UST lock held.
570 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
574 * iter->tracepoint may be invalid because we blindly incremented it.
575 * Make sure it is valid by marshalling on the tracepoints, getting the
576 * tracepoints from following modules if necessary.
578 tracepoint_get_iter(iter
);
582 * Called with UST lock held.
584 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
588 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
590 iter
->tracepoint
= NULL
;
593 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
595 new_tracepoint_cb
= cb
;
598 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
600 if (new_tracepoint_cb
) {
601 struct tracepoint
* const *t
;
603 for (t
= start
; t
< end
; t
++) {
605 new_tracepoint_cb(*t
);
610 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
611 int tracepoints_count
)
613 struct tracepoint_lib
*pl
, *iter
;
617 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
619 pl
->tracepoints_start
= tracepoints_start
;
620 pl
->tracepoints_count
= tracepoints_count
;
624 * We sort the libs by struct lib pointer address.
626 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
627 BUG_ON(iter
== pl
); /* Should never be in the list twice */
629 /* We belong to the location right after iter. */
630 cds_list_add(&pl
->list
, &iter
->list
);
634 /* We should be added at the head of the list */
635 cds_list_add(&pl
->list
, &libs
);
637 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
639 /* TODO: update just the loaded lib */
640 lib_update_tracepoints();
643 DBG("just registered a tracepoints section from %p and having %d tracepoints",
644 tracepoints_start
, tracepoints_count
);
649 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
651 struct tracepoint_lib
*lib
;
654 cds_list_for_each_entry(lib
, &libs
, list
) {
655 if (lib
->tracepoints_start
== tracepoints_start
) {
656 struct tracepoint_lib
*lib2free
= lib
;
657 cds_list_del(&lib
->list
);
667 void init_tracepoint(void)
669 if (uatomic_xchg(&initialized
, 1) == 1)
674 void exit_tracepoint(void)