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/core.h>
28 #include <urcu/arch.h>
30 #include <urcu/hlist.h>
31 #include <urcu/uatomic.h>
32 #include <urcu/compiler.h>
34 #include <lttng/usterr-signal-safe.h>
35 #include "tracepoint-internal.h"
36 #include "ltt-tracer-core.h"
39 /* Set to 1 to enable tracepoint debug output */
40 static const int tracepoint_debug
;
41 static int initialized
;
42 static void (*new_tracepoint_cb
)(struct tracepoint
*);
44 /* libraries that contain tracepoints (struct tracepoint_lib) */
45 static CDS_LIST_HEAD(libs
);
48 * The UST lock protects the library tracepoints, the hash table, and
50 * All calls to the tracepoint API must be protected by the UST lock,
51 * excepts calls to tracepoint_register_lib and
52 * tracepoint_unregister_lib, which take the UST lock themselves.
56 * Tracepoint hash table, containing the active tracepoints.
57 * Protected by tracepoints_mutex.
59 #define TRACEPOINT_HASH_BITS 6
60 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
61 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
63 static CDS_LIST_HEAD(old_probes
);
64 static int need_update
;
68 * It is used to to delay the free of multiple probes array until a quiescent
70 * Tracepoint entries modifications are protected by the tracepoints_mutex.
72 struct tracepoint_entry
{
73 struct cds_hlist_node hlist
;
74 struct tracepoint_probe
*probes
;
75 int refcount
; /* Number of times armed. 0 if disarmed. */
81 struct cds_list_head list
;
83 struct tracepoint_probe probes
[0];
86 static inline void *allocate_probes(int count
)
88 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
89 + sizeof(struct tp_probes
));
90 return p
== NULL
? NULL
: p
->probes
;
93 static inline void release_probes(void *old
)
96 struct tp_probes
*tp_probes
= caa_container_of(old
,
97 struct tp_probes
, probes
[0]);
103 static void debug_print_probes(struct tracepoint_entry
*entry
)
107 if (!tracepoint_debug
|| !entry
->probes
)
110 for (i
= 0; entry
->probes
[i
].func
; i
++)
111 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
115 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
116 void *probe
, void *data
)
119 struct tracepoint_probe
*old
, *new;
123 debug_print_probes(entry
);
126 /* (N -> N+1), (N != 0, 1) probes */
127 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
128 if (old
[nr_probes
].func
== probe
&&
129 old
[nr_probes
].data
== data
)
130 return ERR_PTR(-EEXIST
);
132 /* + 2 : one for new probe, one for NULL func */
133 new = allocate_probes(nr_probes
+ 2);
135 return ERR_PTR(-ENOMEM
);
137 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
138 new[nr_probes
].func
= probe
;
139 new[nr_probes
].data
= data
;
140 new[nr_probes
+ 1].func
= NULL
;
141 entry
->refcount
= nr_probes
+ 1;
143 debug_print_probes(entry
);
148 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
151 int nr_probes
= 0, nr_del
= 0, i
;
152 struct tracepoint_probe
*old
, *new;
157 return ERR_PTR(-ENOENT
);
159 debug_print_probes(entry
);
160 /* (N -> M), (N > 1, M >= 0) probes */
161 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
163 (old
[nr_probes
].func
== probe
&&
164 old
[nr_probes
].data
== data
))
168 if (nr_probes
- nr_del
== 0) {
169 /* N -> 0, (N > 1) */
170 entry
->probes
= NULL
;
172 debug_print_probes(entry
);
176 /* N -> M, (N > 1, M > 0) */
178 new = allocate_probes(nr_probes
- nr_del
+ 1);
180 return ERR_PTR(-ENOMEM
);
181 for (i
= 0; old
[i
].func
; i
++)
183 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
185 new[nr_probes
- nr_del
].func
= NULL
;
186 entry
->refcount
= nr_probes
- nr_del
;
189 debug_print_probes(entry
);
194 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
195 * Must be called with tracepoints_mutex held.
196 * Returns NULL if not present.
198 static struct tracepoint_entry
*get_tracepoint(const char *name
)
200 struct cds_hlist_head
*head
;
201 struct cds_hlist_node
*node
;
202 struct tracepoint_entry
*e
;
203 uint32_t hash
= jhash(name
, strlen(name
), 0);
205 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
206 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
207 if (!strcmp(name
, e
->name
))
214 * Add the tracepoint to the tracepoint hash table. Must be called with
215 * tracepoints_mutex held.
217 static struct tracepoint_entry
*add_tracepoint(const char *name
)
219 struct cds_hlist_head
*head
;
220 struct cds_hlist_node
*node
;
221 struct tracepoint_entry
*e
;
222 size_t name_len
= strlen(name
) + 1;
223 uint32_t hash
= jhash(name
, name_len
-1, 0);
225 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
226 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
227 if (!strcmp(name
, e
->name
)) {
228 DBG("tracepoint %s busy", name
);
229 return ERR_PTR(-EEXIST
); /* Already there */
233 * Using zmalloc here to allocate a variable length element. Could
234 * cause some memory fragmentation if overused.
236 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
238 return ERR_PTR(-ENOMEM
);
239 memcpy(&e
->name
[0], name
, name_len
);
242 cds_hlist_add_head(&e
->hlist
, head
);
247 * Remove the tracepoint from the tracepoint hash table. Must be called with
250 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
252 cds_hlist_del(&e
->hlist
);
257 * Sets the probe callback corresponding to one tracepoint.
259 static void set_tracepoint(struct tracepoint_entry
**entry
,
260 struct tracepoint
*elem
, int active
)
262 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
265 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
266 * probe callbacks array is consistent before setting a pointer to it.
267 * This array is referenced by __DO_TRACE from
268 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
271 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
272 elem
->state
= active
;
276 * Disable a tracepoint and its probe callback.
277 * Note: only waiting an RCU period after setting elem->call to the empty
278 * function insures that the original callback is not used anymore. This insured
279 * by preempt_disable around the call site.
281 static void disable_tracepoint(struct tracepoint
*elem
)
284 rcu_assign_pointer(elem
->probes
, NULL
);
288 * tracepoint_update_probe_range - Update a probe range
289 * @begin: beginning of the range
290 * @end: end of the range
292 * Updates the probe callback corresponding to a range of tracepoints.
295 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
296 struct tracepoint
* const *end
)
298 struct tracepoint
* const *iter
;
299 struct tracepoint_entry
*mark_entry
;
301 for (iter
= begin
; iter
< end
; iter
++) {
303 continue; /* skip dummy */
304 if (!(*iter
)->name
) {
305 disable_tracepoint(*iter
);
308 mark_entry
= get_tracepoint((*iter
)->name
);
310 set_tracepoint(&mark_entry
, *iter
,
311 !!mark_entry
->refcount
);
313 disable_tracepoint(*iter
);
318 static void lib_update_tracepoints(void)
320 struct tracepoint_lib
*lib
;
322 cds_list_for_each_entry(lib
, &libs
, list
) {
323 tracepoint_update_probe_range(lib
->tracepoints_start
,
324 lib
->tracepoints_start
+ lib
->tracepoints_count
);
329 * Update probes, removing the faulty probes.
331 static void tracepoint_update_probes(void)
333 /* tracepoints registered from libraries and executable. */
334 lib_update_tracepoints();
337 static struct tracepoint_probe
*
338 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
340 struct tracepoint_entry
*entry
;
341 struct tracepoint_probe
*old
;
343 entry
= get_tracepoint(name
);
345 entry
= add_tracepoint(name
);
347 return (struct tracepoint_probe
*)entry
;
349 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
350 if (IS_ERR(old
) && !entry
->refcount
)
351 remove_tracepoint(entry
);
356 * __tracepoint_probe_register - Connect a probe to a tracepoint
357 * @name: tracepoint name
358 * @probe: probe handler
360 * Returns 0 if ok, error value on error.
361 * The probe address must at least be aligned on the architecture pointer size.
362 * Called with the UST lock held.
364 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
368 old
= tracepoint_add_probe(name
, probe
, data
);
372 tracepoint_update_probes(); /* may update entry */
377 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
379 struct tracepoint_entry
*entry
;
382 entry
= get_tracepoint(name
);
384 return ERR_PTR(-ENOENT
);
385 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
388 if (!entry
->refcount
)
389 remove_tracepoint(entry
);
394 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
395 * @name: tracepoint name
396 * @probe: probe function pointer
397 * @probe: probe data pointer
399 * Called with the UST lock held.
401 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
405 old
= tracepoint_remove_probe(name
, probe
, data
);
409 tracepoint_update_probes(); /* may update entry */
414 static void tracepoint_add_old_probes(void *old
)
418 struct tp_probes
*tp_probes
= caa_container_of(old
,
419 struct tp_probes
, probes
[0]);
420 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
425 * tracepoint_probe_register_noupdate - register a probe but not connect
426 * @name: tracepoint name
427 * @probe: probe handler
429 * caller must call tracepoint_probe_update_all()
430 * Called with the UST lock held.
432 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
437 old
= tracepoint_add_probe(name
, probe
, data
);
441 tracepoint_add_old_probes(old
);
446 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
447 * @name: tracepoint name
448 * @probe: probe function pointer
450 * caller must call tracepoint_probe_update_all()
451 * Called with the UST lock held.
453 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
458 old
= tracepoint_remove_probe(name
, probe
, data
);
462 tracepoint_add_old_probes(old
);
467 * tracepoint_probe_update_all - update tracepoints
468 * Called with the UST lock held.
470 void tracepoint_probe_update_all(void)
472 CDS_LIST_HEAD(release_probes
);
473 struct tp_probes
*pos
, *next
;
478 if (!cds_list_empty(&old_probes
))
479 cds_list_replace_init(&old_probes
, &release_probes
);
482 tracepoint_update_probes();
483 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
484 cds_list_del(&pos
->u
.list
);
491 * Returns 0 if current not found.
492 * Returns 1 if current found.
494 * Called with tracepoint mutex held
496 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
498 struct tracepoint_lib
*iter_lib
;
501 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
502 if (iter_lib
< iter
->lib
)
504 else if (iter_lib
> iter
->lib
)
505 iter
->tracepoint
= NULL
;
506 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
507 iter_lib
->tracepoints_start
,
508 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
510 iter
->lib
= iter_lib
;
518 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
519 * @tracepoint: current tracepoints (in), next tracepoint (out)
520 * @begin: beginning of the range
521 * @end: end of the range
523 * Returns whether a next tracepoint has been found (1) or not (0).
524 * Will return the first tracepoint in the range if the input tracepoint is
526 * Called with tracepoint mutex held.
528 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
529 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
531 if (!*tracepoint
&& begin
!= end
)
533 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
535 (*tracepoint
)++; /* skip dummy */
543 * Called with tracepoint mutex held.
545 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
549 /* tracepoints in libs. */
550 found
= lib_get_iter_tracepoints(iter
);
552 tracepoint_iter_reset(iter
);
556 * Called with UST lock held.
558 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
560 tracepoint_get_iter(iter
);
564 * Called with UST lock held.
566 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
570 * iter->tracepoint may be invalid because we blindly incremented it.
571 * Make sure it is valid by marshalling on the tracepoints, getting the
572 * tracepoints from following modules if necessary.
574 tracepoint_get_iter(iter
);
578 * Called with UST lock held.
580 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
584 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
586 iter
->tracepoint
= NULL
;
589 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
591 new_tracepoint_cb
= cb
;
594 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
596 if (new_tracepoint_cb
) {
597 struct tracepoint
* const *t
;
599 for (t
= start
; t
< end
; t
++) {
601 new_tracepoint_cb(*t
);
606 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
607 int tracepoints_count
)
609 struct tracepoint_lib
*pl
, *iter
;
613 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
615 pl
->tracepoints_start
= tracepoints_start
;
616 pl
->tracepoints_count
= tracepoints_count
;
620 * We sort the libs by struct lib pointer address.
622 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
623 BUG_ON(iter
== pl
); /* Should never be in the list twice */
625 /* We belong to the location right after iter. */
626 cds_list_add(&pl
->list
, &iter
->list
);
630 /* We should be added at the head of the list */
631 cds_list_add(&pl
->list
, &libs
);
633 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
635 /* TODO: update just the loaded lib */
636 lib_update_tracepoints();
639 DBG("just registered a tracepoints section from %p and having %d tracepoints",
640 tracepoints_start
, tracepoints_count
);
645 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
647 struct tracepoint_lib
*lib
;
650 cds_list_for_each_entry(lib
, &libs
, list
) {
651 if (lib
->tracepoints_start
== tracepoints_start
) {
652 struct tracepoint_lib
*lib2free
= lib
;
653 cds_list_del(&lib
->list
);
663 void init_tracepoint(void)
665 if (uatomic_xchg(&initialized
, 1) == 1)
670 void exit_tracepoint(void)