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.
24 #include <lttng/tracepoint.h>
25 #include <lttng/tracepoint-internal.h>
26 #include <lttng/core.h>
27 #include <lttng/kcompat/kcompat.h>
29 #include <urcu/hlist.h>
30 #include <urcu/uatomic.h>
32 #include <lttng/usterr-signal-safe.h>
33 #include "ltt-tracer-core.h"
35 /* Set to 1 to enable tracepoint debug output */
36 static const int tracepoint_debug
;
37 static int initialized
;
38 static void (*new_tracepoint_cb
)(struct tracepoint
*);
40 /* libraries that contain tracepoints (struct tracepoint_lib) */
41 static CDS_LIST_HEAD(libs
);
44 * The UST lock protects the library tracepoints, the hash table, and
46 * All calls to the tracepoint API must be protected by the UST lock,
47 * excepts calls to tracepoint_register_lib and
48 * tracepoint_unregister_lib, which take the UST lock themselves.
52 * Tracepoint hash table, containing the active tracepoints.
53 * Protected by tracepoints_mutex.
55 #define TRACEPOINT_HASH_BITS 6
56 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
57 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
59 static CDS_LIST_HEAD(old_probes
);
60 static int need_update
;
64 * It is used to to delay the free of multiple probes array until a quiescent
66 * Tracepoint entries modifications are protected by the tracepoints_mutex.
68 struct tracepoint_entry
{
69 struct cds_hlist_node hlist
;
70 struct tracepoint_probe
*probes
;
71 int refcount
; /* Number of times armed. 0 if disarmed. */
77 struct cds_list_head list
;
79 struct tracepoint_probe probes
[0];
82 static inline void *allocate_probes(int count
)
84 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
85 + sizeof(struct tp_probes
));
86 return p
== NULL
? NULL
: p
->probes
;
89 static inline void release_probes(void *old
)
92 struct tp_probes
*tp_probes
= _ust_container_of(old
,
93 struct tp_probes
, probes
[0]);
99 static void debug_print_probes(struct tracepoint_entry
*entry
)
103 if (!tracepoint_debug
|| !entry
->probes
)
106 for (i
= 0; entry
->probes
[i
].func
; i
++)
107 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
111 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
112 void *probe
, void *data
)
115 struct tracepoint_probe
*old
, *new;
119 debug_print_probes(entry
);
122 /* (N -> N+1), (N != 0, 1) probes */
123 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
124 if (old
[nr_probes
].func
== probe
&&
125 old
[nr_probes
].data
== data
)
126 return ERR_PTR(-EEXIST
);
128 /* + 2 : one for new probe, one for NULL func */
129 new = allocate_probes(nr_probes
+ 2);
131 return ERR_PTR(-ENOMEM
);
133 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
134 new[nr_probes
].func
= probe
;
135 new[nr_probes
].data
= data
;
136 new[nr_probes
+ 1].func
= NULL
;
137 entry
->refcount
= nr_probes
+ 1;
139 debug_print_probes(entry
);
144 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
147 int nr_probes
= 0, nr_del
= 0, i
;
148 struct tracepoint_probe
*old
, *new;
153 return ERR_PTR(-ENOENT
);
155 debug_print_probes(entry
);
156 /* (N -> M), (N > 1, M >= 0) probes */
157 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
159 (old
[nr_probes
].func
== probe
&&
160 old
[nr_probes
].data
== data
))
164 if (nr_probes
- nr_del
== 0) {
165 /* N -> 0, (N > 1) */
166 entry
->probes
= NULL
;
168 debug_print_probes(entry
);
172 /* N -> M, (N > 1, M > 0) */
174 new = allocate_probes(nr_probes
- nr_del
+ 1);
176 return ERR_PTR(-ENOMEM
);
177 for (i
= 0; old
[i
].func
; i
++)
179 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
181 new[nr_probes
- nr_del
].func
= NULL
;
182 entry
->refcount
= nr_probes
- nr_del
;
185 debug_print_probes(entry
);
190 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
191 * Must be called with tracepoints_mutex held.
192 * Returns NULL if not present.
194 static struct tracepoint_entry
*get_tracepoint(const char *name
)
196 struct cds_hlist_head
*head
;
197 struct cds_hlist_node
*node
;
198 struct tracepoint_entry
*e
;
199 u32 hash
= jhash(name
, strlen(name
), 0);
201 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
202 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
203 if (!strcmp(name
, e
->name
))
210 * Add the tracepoint to the tracepoint hash table. Must be called with
211 * tracepoints_mutex held.
213 static struct tracepoint_entry
*add_tracepoint(const char *name
)
215 struct cds_hlist_head
*head
;
216 struct cds_hlist_node
*node
;
217 struct tracepoint_entry
*e
;
218 size_t name_len
= strlen(name
) + 1;
219 u32 hash
= jhash(name
, name_len
-1, 0);
221 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
222 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
223 if (!strcmp(name
, e
->name
)) {
224 DBG("tracepoint %s busy", name
);
225 return ERR_PTR(-EEXIST
); /* Already there */
229 * Using zmalloc here to allocate a variable length element. Could
230 * cause some memory fragmentation if overused.
232 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
234 return ERR_PTR(-ENOMEM
);
235 memcpy(&e
->name
[0], name
, name_len
);
238 cds_hlist_add_head(&e
->hlist
, head
);
243 * Remove the tracepoint from the tracepoint hash table. Must be called with
246 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
248 cds_hlist_del(&e
->hlist
);
253 * Sets the probe callback corresponding to one tracepoint.
255 static void set_tracepoint(struct tracepoint_entry
**entry
,
256 struct tracepoint
*elem
, int active
)
258 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
261 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
262 * probe callbacks array is consistent before setting a pointer to it.
263 * This array is referenced by __DO_TRACE from
264 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
267 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
268 elem
->state
= active
;
272 * Disable a tracepoint and its probe callback.
273 * Note: only waiting an RCU period after setting elem->call to the empty
274 * function insures that the original callback is not used anymore. This insured
275 * by preempt_disable around the call site.
277 static void disable_tracepoint(struct tracepoint
*elem
)
280 rcu_assign_pointer(elem
->probes
, NULL
);
284 * tracepoint_update_probe_range - Update a probe range
285 * @begin: beginning of the range
286 * @end: end of the range
288 * Updates the probe callback corresponding to a range of tracepoints.
291 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
292 struct tracepoint
* const *end
)
294 struct tracepoint
* const *iter
;
295 struct tracepoint_entry
*mark_entry
;
297 for (iter
= begin
; iter
< end
; iter
++) {
299 continue; /* skip dummy */
300 if (!(*iter
)->name
) {
301 disable_tracepoint(*iter
);
304 mark_entry
= get_tracepoint((*iter
)->name
);
306 set_tracepoint(&mark_entry
, *iter
,
307 !!mark_entry
->refcount
);
309 disable_tracepoint(*iter
);
314 static void lib_update_tracepoints(void)
316 struct tracepoint_lib
*lib
;
318 cds_list_for_each_entry(lib
, &libs
, list
) {
319 tracepoint_update_probe_range(lib
->tracepoints_start
,
320 lib
->tracepoints_start
+ lib
->tracepoints_count
);
325 * Update probes, removing the faulty probes.
327 static void tracepoint_update_probes(void)
329 /* tracepoints registered from libraries and executable. */
330 lib_update_tracepoints();
333 static struct tracepoint_probe
*
334 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
336 struct tracepoint_entry
*entry
;
337 struct tracepoint_probe
*old
;
339 entry
= get_tracepoint(name
);
341 entry
= add_tracepoint(name
);
343 return (struct tracepoint_probe
*)entry
;
345 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
346 if (IS_ERR(old
) && !entry
->refcount
)
347 remove_tracepoint(entry
);
352 * __tracepoint_probe_register - Connect a probe to a tracepoint
353 * @name: tracepoint name
354 * @probe: probe handler
356 * Returns 0 if ok, error value on error.
357 * The probe address must at least be aligned on the architecture pointer size.
358 * Called with the UST lock held.
360 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
364 old
= tracepoint_add_probe(name
, probe
, data
);
368 tracepoint_update_probes(); /* may update entry */
373 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
375 struct tracepoint_entry
*entry
;
378 entry
= get_tracepoint(name
);
380 return ERR_PTR(-ENOENT
);
381 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
384 if (!entry
->refcount
)
385 remove_tracepoint(entry
);
390 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
391 * @name: tracepoint name
392 * @probe: probe function pointer
393 * @probe: probe data pointer
395 * Called with the UST lock held.
397 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
401 old
= tracepoint_remove_probe(name
, probe
, data
);
405 tracepoint_update_probes(); /* may update entry */
410 static void tracepoint_add_old_probes(void *old
)
414 struct tp_probes
*tp_probes
= _ust_container_of(old
,
415 struct tp_probes
, probes
[0]);
416 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
421 * tracepoint_probe_register_noupdate - register a probe but not connect
422 * @name: tracepoint name
423 * @probe: probe handler
425 * caller must call tracepoint_probe_update_all()
426 * Called with the UST lock held.
428 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
433 old
= tracepoint_add_probe(name
, probe
, data
);
437 tracepoint_add_old_probes(old
);
442 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
443 * @name: tracepoint name
444 * @probe: probe function pointer
446 * caller must call tracepoint_probe_update_all()
447 * Called with the UST lock held.
449 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
454 old
= tracepoint_remove_probe(name
, probe
, data
);
458 tracepoint_add_old_probes(old
);
463 * tracepoint_probe_update_all - update tracepoints
464 * Called with the UST lock held.
466 void tracepoint_probe_update_all(void)
468 CDS_LIST_HEAD(release_probes
);
469 struct tp_probes
*pos
, *next
;
474 if (!cds_list_empty(&old_probes
))
475 cds_list_replace_init(&old_probes
, &release_probes
);
478 tracepoint_update_probes();
479 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
480 cds_list_del(&pos
->u
.list
);
487 * Returns 0 if current not found.
488 * Returns 1 if current found.
490 * Called with tracepoint mutex held
492 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
494 struct tracepoint_lib
*iter_lib
;
497 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
498 if (iter_lib
< iter
->lib
)
500 else if (iter_lib
> iter
->lib
)
501 iter
->tracepoint
= NULL
;
502 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
503 iter_lib
->tracepoints_start
,
504 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
506 iter
->lib
= iter_lib
;
514 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
515 * @tracepoint: current tracepoints (in), next tracepoint (out)
516 * @begin: beginning of the range
517 * @end: end of the range
519 * Returns whether a next tracepoint has been found (1) or not (0).
520 * Will return the first tracepoint in the range if the input tracepoint is
522 * Called with tracepoint mutex held.
524 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
525 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
527 if (!*tracepoint
&& begin
!= end
)
529 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
531 (*tracepoint
)++; /* skip dummy */
539 * Called with tracepoint mutex held.
541 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
545 /* tracepoints in libs. */
546 found
= lib_get_iter_tracepoints(iter
);
548 tracepoint_iter_reset(iter
);
552 * Called with UST lock held.
554 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
556 tracepoint_get_iter(iter
);
560 * Called with UST lock held.
562 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
566 * iter->tracepoint may be invalid because we blindly incremented it.
567 * Make sure it is valid by marshalling on the tracepoints, getting the
568 * tracepoints from following modules if necessary.
570 tracepoint_get_iter(iter
);
574 * Called with UST lock held.
576 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
580 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
582 iter
->tracepoint
= NULL
;
585 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
587 new_tracepoint_cb
= cb
;
590 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
592 if (new_tracepoint_cb
) {
593 struct tracepoint
* const *t
;
595 for (t
= start
; t
< end
; t
++) {
597 new_tracepoint_cb(*t
);
602 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
603 int tracepoints_count
)
605 struct tracepoint_lib
*pl
, *iter
;
609 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
611 pl
->tracepoints_start
= tracepoints_start
;
612 pl
->tracepoints_count
= tracepoints_count
;
616 * We sort the libs by struct lib pointer address.
618 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
619 BUG_ON(iter
== pl
); /* Should never be in the list twice */
621 /* We belong to the location right after iter. */
622 cds_list_add(&pl
->list
, &iter
->list
);
626 /* We should be added at the head of the list */
627 cds_list_add(&pl
->list
, &libs
);
629 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
631 /* TODO: update just the loaded lib */
632 lib_update_tracepoints();
635 DBG("just registered a tracepoints section from %p and having %d tracepoints",
636 tracepoints_start
, tracepoints_count
);
641 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
643 struct tracepoint_lib
*lib
;
646 cds_list_for_each_entry(lib
, &libs
, list
) {
647 if (lib
->tracepoints_start
== tracepoints_start
) {
648 struct tracepoint_lib
*lib2free
= lib
;
649 cds_list_del(&lib
->list
);
659 void init_tracepoint(void)
661 if (uatomic_xchg(&initialized
, 1) == 1)
666 void exit_tracepoint(void)