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>
29 #include <urcu/arch.h>
31 #include <urcu/hlist.h>
32 #include <urcu/uatomic.h>
33 #include <urcu/compiler.h>
35 #include <lttng/usterr-signal-safe.h>
36 #include "ltt-tracer-core.h"
38 /* Set to 1 to enable tracepoint debug output */
39 static const int tracepoint_debug
;
40 static int initialized
;
41 static void (*new_tracepoint_cb
)(struct tracepoint
*);
43 /* libraries that contain tracepoints (struct tracepoint_lib) */
44 static CDS_LIST_HEAD(libs
);
47 * The UST lock protects the library tracepoints, the hash table, and
49 * All calls to the tracepoint API must be protected by the UST lock,
50 * excepts calls to tracepoint_register_lib and
51 * tracepoint_unregister_lib, which take the UST lock themselves.
55 * Tracepoint hash table, containing the active tracepoints.
56 * Protected by tracepoints_mutex.
58 #define TRACEPOINT_HASH_BITS 6
59 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
60 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
62 static CDS_LIST_HEAD(old_probes
);
63 static int need_update
;
67 * It is used to to delay the free of multiple probes array until a quiescent
69 * Tracepoint entries modifications are protected by the tracepoints_mutex.
71 struct tracepoint_entry
{
72 struct cds_hlist_node hlist
;
73 struct tracepoint_probe
*probes
;
74 int refcount
; /* Number of times armed. 0 if disarmed. */
80 struct cds_list_head list
;
82 struct tracepoint_probe probes
[0];
85 static inline void *allocate_probes(int count
)
87 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
88 + sizeof(struct tp_probes
));
89 return p
== NULL
? NULL
: p
->probes
;
92 static inline void release_probes(void *old
)
95 struct tp_probes
*tp_probes
= caa_container_of(old
,
96 struct tp_probes
, probes
[0]);
102 static void debug_print_probes(struct tracepoint_entry
*entry
)
106 if (!tracepoint_debug
|| !entry
->probes
)
109 for (i
= 0; entry
->probes
[i
].func
; i
++)
110 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
114 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
115 void *probe
, void *data
)
118 struct tracepoint_probe
*old
, *new;
122 debug_print_probes(entry
);
125 /* (N -> N+1), (N != 0, 1) probes */
126 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
127 if (old
[nr_probes
].func
== probe
&&
128 old
[nr_probes
].data
== data
)
129 return ERR_PTR(-EEXIST
);
131 /* + 2 : one for new probe, one for NULL func */
132 new = allocate_probes(nr_probes
+ 2);
134 return ERR_PTR(-ENOMEM
);
136 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
137 new[nr_probes
].func
= probe
;
138 new[nr_probes
].data
= data
;
139 new[nr_probes
+ 1].func
= NULL
;
140 entry
->refcount
= nr_probes
+ 1;
142 debug_print_probes(entry
);
147 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
150 int nr_probes
= 0, nr_del
= 0, i
;
151 struct tracepoint_probe
*old
, *new;
156 return ERR_PTR(-ENOENT
);
158 debug_print_probes(entry
);
159 /* (N -> M), (N > 1, M >= 0) probes */
160 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
162 (old
[nr_probes
].func
== probe
&&
163 old
[nr_probes
].data
== data
))
167 if (nr_probes
- nr_del
== 0) {
168 /* N -> 0, (N > 1) */
169 entry
->probes
= NULL
;
171 debug_print_probes(entry
);
175 /* N -> M, (N > 1, M > 0) */
177 new = allocate_probes(nr_probes
- nr_del
+ 1);
179 return ERR_PTR(-ENOMEM
);
180 for (i
= 0; old
[i
].func
; i
++)
182 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
184 new[nr_probes
- nr_del
].func
= NULL
;
185 entry
->refcount
= nr_probes
- nr_del
;
188 debug_print_probes(entry
);
193 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
194 * Must be called with tracepoints_mutex held.
195 * Returns NULL if not present.
197 static struct tracepoint_entry
*get_tracepoint(const char *name
)
199 struct cds_hlist_head
*head
;
200 struct cds_hlist_node
*node
;
201 struct tracepoint_entry
*e
;
202 u32 hash
= jhash(name
, strlen(name
), 0);
204 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
205 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
206 if (!strcmp(name
, e
->name
))
213 * Add the tracepoint to the tracepoint hash table. Must be called with
214 * tracepoints_mutex held.
216 static struct tracepoint_entry
*add_tracepoint(const char *name
)
218 struct cds_hlist_head
*head
;
219 struct cds_hlist_node
*node
;
220 struct tracepoint_entry
*e
;
221 size_t name_len
= strlen(name
) + 1;
222 u32 hash
= jhash(name
, name_len
-1, 0);
224 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
225 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
226 if (!strcmp(name
, e
->name
)) {
227 DBG("tracepoint %s busy", name
);
228 return ERR_PTR(-EEXIST
); /* Already there */
232 * Using zmalloc here to allocate a variable length element. Could
233 * cause some memory fragmentation if overused.
235 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
237 return ERR_PTR(-ENOMEM
);
238 memcpy(&e
->name
[0], name
, name_len
);
241 cds_hlist_add_head(&e
->hlist
, head
);
246 * Remove the tracepoint from the tracepoint hash table. Must be called with
249 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
251 cds_hlist_del(&e
->hlist
);
256 * Sets the probe callback corresponding to one tracepoint.
258 static void set_tracepoint(struct tracepoint_entry
**entry
,
259 struct tracepoint
*elem
, int active
)
261 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
264 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
265 * probe callbacks array is consistent before setting a pointer to it.
266 * This array is referenced by __DO_TRACE from
267 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
270 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
271 elem
->state
= active
;
275 * Disable a tracepoint and its probe callback.
276 * Note: only waiting an RCU period after setting elem->call to the empty
277 * function insures that the original callback is not used anymore. This insured
278 * by preempt_disable around the call site.
280 static void disable_tracepoint(struct tracepoint
*elem
)
283 rcu_assign_pointer(elem
->probes
, NULL
);
287 * tracepoint_update_probe_range - Update a probe range
288 * @begin: beginning of the range
289 * @end: end of the range
291 * Updates the probe callback corresponding to a range of tracepoints.
294 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
295 struct tracepoint
* const *end
)
297 struct tracepoint
* const *iter
;
298 struct tracepoint_entry
*mark_entry
;
300 for (iter
= begin
; iter
< end
; iter
++) {
302 continue; /* skip dummy */
303 if (!(*iter
)->name
) {
304 disable_tracepoint(*iter
);
307 mark_entry
= get_tracepoint((*iter
)->name
);
309 set_tracepoint(&mark_entry
, *iter
,
310 !!mark_entry
->refcount
);
312 disable_tracepoint(*iter
);
317 static void lib_update_tracepoints(void)
319 struct tracepoint_lib
*lib
;
321 cds_list_for_each_entry(lib
, &libs
, list
) {
322 tracepoint_update_probe_range(lib
->tracepoints_start
,
323 lib
->tracepoints_start
+ lib
->tracepoints_count
);
328 * Update probes, removing the faulty probes.
330 static void tracepoint_update_probes(void)
332 /* tracepoints registered from libraries and executable. */
333 lib_update_tracepoints();
336 static struct tracepoint_probe
*
337 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
339 struct tracepoint_entry
*entry
;
340 struct tracepoint_probe
*old
;
342 entry
= get_tracepoint(name
);
344 entry
= add_tracepoint(name
);
346 return (struct tracepoint_probe
*)entry
;
348 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
349 if (IS_ERR(old
) && !entry
->refcount
)
350 remove_tracepoint(entry
);
355 * __tracepoint_probe_register - Connect a probe to a tracepoint
356 * @name: tracepoint name
357 * @probe: probe handler
359 * Returns 0 if ok, error value on error.
360 * The probe address must at least be aligned on the architecture pointer size.
361 * Called with the UST lock held.
363 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
367 old
= tracepoint_add_probe(name
, probe
, data
);
371 tracepoint_update_probes(); /* may update entry */
376 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
378 struct tracepoint_entry
*entry
;
381 entry
= get_tracepoint(name
);
383 return ERR_PTR(-ENOENT
);
384 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
387 if (!entry
->refcount
)
388 remove_tracepoint(entry
);
393 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
394 * @name: tracepoint name
395 * @probe: probe function pointer
396 * @probe: probe data pointer
398 * Called with the UST lock held.
400 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
404 old
= tracepoint_remove_probe(name
, probe
, data
);
408 tracepoint_update_probes(); /* may update entry */
413 static void tracepoint_add_old_probes(void *old
)
417 struct tp_probes
*tp_probes
= caa_container_of(old
,
418 struct tp_probes
, probes
[0]);
419 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
424 * tracepoint_probe_register_noupdate - register a probe but not connect
425 * @name: tracepoint name
426 * @probe: probe handler
428 * caller must call tracepoint_probe_update_all()
429 * Called with the UST lock held.
431 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
436 old
= tracepoint_add_probe(name
, probe
, data
);
440 tracepoint_add_old_probes(old
);
445 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
446 * @name: tracepoint name
447 * @probe: probe function pointer
449 * caller must call tracepoint_probe_update_all()
450 * Called with the UST lock held.
452 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
457 old
= tracepoint_remove_probe(name
, probe
, data
);
461 tracepoint_add_old_probes(old
);
466 * tracepoint_probe_update_all - update tracepoints
467 * Called with the UST lock held.
469 void tracepoint_probe_update_all(void)
471 CDS_LIST_HEAD(release_probes
);
472 struct tp_probes
*pos
, *next
;
477 if (!cds_list_empty(&old_probes
))
478 cds_list_replace_init(&old_probes
, &release_probes
);
481 tracepoint_update_probes();
482 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
483 cds_list_del(&pos
->u
.list
);
490 * Returns 0 if current not found.
491 * Returns 1 if current found.
493 * Called with tracepoint mutex held
495 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
497 struct tracepoint_lib
*iter_lib
;
500 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
501 if (iter_lib
< iter
->lib
)
503 else if (iter_lib
> iter
->lib
)
504 iter
->tracepoint
= NULL
;
505 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
506 iter_lib
->tracepoints_start
,
507 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
509 iter
->lib
= iter_lib
;
517 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
518 * @tracepoint: current tracepoints (in), next tracepoint (out)
519 * @begin: beginning of the range
520 * @end: end of the range
522 * Returns whether a next tracepoint has been found (1) or not (0).
523 * Will return the first tracepoint in the range if the input tracepoint is
525 * Called with tracepoint mutex held.
527 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
528 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
530 if (!*tracepoint
&& begin
!= end
)
532 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
534 (*tracepoint
)++; /* skip dummy */
542 * Called with tracepoint mutex held.
544 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
548 /* tracepoints in libs. */
549 found
= lib_get_iter_tracepoints(iter
);
551 tracepoint_iter_reset(iter
);
555 * Called with UST lock held.
557 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
559 tracepoint_get_iter(iter
);
563 * Called with UST lock held.
565 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
569 * iter->tracepoint may be invalid because we blindly incremented it.
570 * Make sure it is valid by marshalling on the tracepoints, getting the
571 * tracepoints from following modules if necessary.
573 tracepoint_get_iter(iter
);
577 * Called with UST lock held.
579 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
583 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
585 iter
->tracepoint
= NULL
;
588 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
590 new_tracepoint_cb
= cb
;
593 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
595 if (new_tracepoint_cb
) {
596 struct tracepoint
* const *t
;
598 for (t
= start
; t
< end
; t
++) {
600 new_tracepoint_cb(*t
);
605 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
606 int tracepoints_count
)
608 struct tracepoint_lib
*pl
, *iter
;
612 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
614 pl
->tracepoints_start
= tracepoints_start
;
615 pl
->tracepoints_count
= tracepoints_count
;
619 * We sort the libs by struct lib pointer address.
621 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
622 BUG_ON(iter
== pl
); /* Should never be in the list twice */
624 /* We belong to the location right after iter. */
625 cds_list_add(&pl
->list
, &iter
->list
);
629 /* We should be added at the head of the list */
630 cds_list_add(&pl
->list
, &libs
);
632 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
634 /* TODO: update just the loaded lib */
635 lib_update_tracepoints();
638 DBG("just registered a tracepoints section from %p and having %d tracepoints",
639 tracepoints_start
, tracepoints_count
);
644 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
646 struct tracepoint_lib
*lib
;
649 cds_list_for_each_entry(lib
, &libs
, list
) {
650 if (lib
->tracepoints_start
== tracepoints_start
) {
651 struct tracepoint_lib
*lib2free
= lib
;
652 cds_list_del(&lib
->list
);
662 void init_tracepoint(void)
664 if (uatomic_xchg(&initialized
, 1) == 1)
669 void exit_tracepoint(void)