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 <ust/tracepoint.h>
25 #include <ust/tracepoint-internal.h>
27 #include <ust/kcompat/kcompat.h>
29 #include <urcu/hlist.h>
30 #include <urcu/uatomic.h>
32 #include <ust/usterr-signal-safe.h>
34 extern struct tracepoint
* const __start___tracepoints_ptrs
[]
35 __attribute__((visibility("hidden")));
36 extern struct tracepoint
* const __stop___tracepoints_ptrs
[]
37 __attribute__((visibility("hidden")));
39 static struct tracepoint
* __tracepoint_ptr_dummy
40 __attribute__((used
, section("__tracepoints_ptrs")));
42 /* Set to 1 to enable tracepoint debug output */
43 static const int tracepoint_debug
;
44 static int initialized
;
45 static void (*new_tracepoint_cb
)(struct tracepoint
*);
47 /* libraries that contain tracepoints (struct tracepoint_lib) */
48 static CDS_LIST_HEAD(libs
);
51 * Allow nested mutex for mutex listing and nested enable.
53 static __thread
int nested_mutex
;
56 * Tracepoints mutex protects the library tracepoints, the hash table,
57 * and the library list.
59 static pthread_mutex_t tracepoints_mutex
;
62 void lock_tracepoints(void)
64 if (!(nested_mutex
++))
65 pthread_mutex_lock(&tracepoints_mutex
);
69 void unlock_tracepoints(void)
71 if (!(--nested_mutex
))
72 pthread_mutex_unlock(&tracepoints_mutex
);
76 * Tracepoint hash table, containing the active tracepoints.
77 * Protected by tracepoints_mutex.
79 #define TRACEPOINT_HASH_BITS 6
80 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
81 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
83 static CDS_LIST_HEAD(old_probes
);
84 static int need_update
;
88 * It is used to to delay the free of multiple probes array until a quiescent
90 * Tracepoint entries modifications are protected by the tracepoints_mutex.
92 struct tracepoint_entry
{
93 struct cds_hlist_node hlist
;
94 struct tracepoint_probe
*probes
;
95 int refcount
; /* Number of times armed. 0 if disarmed. */
101 struct cds_list_head list
;
103 struct tracepoint_probe probes
[0];
106 static inline void *allocate_probes(int count
)
108 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
109 + sizeof(struct tp_probes
));
110 return p
== NULL
? NULL
: p
->probes
;
113 static inline void release_probes(void *old
)
116 struct tp_probes
*tp_probes
= _ust_container_of(old
,
117 struct tp_probes
, probes
[0]);
123 static void debug_print_probes(struct tracepoint_entry
*entry
)
127 if (!tracepoint_debug
|| !entry
->probes
)
130 for (i
= 0; entry
->probes
[i
].func
; i
++)
131 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
135 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
136 void *probe
, void *data
)
139 struct tracepoint_probe
*old
, *new;
143 debug_print_probes(entry
);
146 /* (N -> N+1), (N != 0, 1) probes */
147 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
148 if (old
[nr_probes
].func
== probe
&&
149 old
[nr_probes
].data
== data
)
150 return ERR_PTR(-EEXIST
);
152 /* + 2 : one for new probe, one for NULL func */
153 new = allocate_probes(nr_probes
+ 2);
155 return ERR_PTR(-ENOMEM
);
157 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
158 new[nr_probes
].func
= probe
;
159 new[nr_probes
].data
= data
;
160 new[nr_probes
+ 1].func
= NULL
;
161 entry
->refcount
= nr_probes
+ 1;
163 debug_print_probes(entry
);
168 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
171 int nr_probes
= 0, nr_del
= 0, i
;
172 struct tracepoint_probe
*old
, *new;
177 return ERR_PTR(-ENOENT
);
179 debug_print_probes(entry
);
180 /* (N -> M), (N > 1, M >= 0) probes */
181 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
183 (old
[nr_probes
].func
== probe
&&
184 old
[nr_probes
].data
== data
))
188 if (nr_probes
- nr_del
== 0) {
189 /* N -> 0, (N > 1) */
190 entry
->probes
= NULL
;
192 debug_print_probes(entry
);
196 /* N -> M, (N > 1, M > 0) */
198 new = allocate_probes(nr_probes
- nr_del
+ 1);
200 return ERR_PTR(-ENOMEM
);
201 for (i
= 0; old
[i
].func
; i
++)
203 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
205 new[nr_probes
- nr_del
].func
= NULL
;
206 entry
->refcount
= nr_probes
- nr_del
;
209 debug_print_probes(entry
);
214 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
215 * Must be called with tracepoints_mutex held.
216 * Returns NULL if not present.
218 static struct tracepoint_entry
*get_tracepoint(const char *name
)
220 struct cds_hlist_head
*head
;
221 struct cds_hlist_node
*node
;
222 struct tracepoint_entry
*e
;
223 u32 hash
= jhash(name
, strlen(name
), 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
))
234 * Add the tracepoint to the tracepoint hash table. Must be called with
235 * tracepoints_mutex held.
237 static struct tracepoint_entry
*add_tracepoint(const char *name
)
239 struct cds_hlist_head
*head
;
240 struct cds_hlist_node
*node
;
241 struct tracepoint_entry
*e
;
242 size_t name_len
= strlen(name
) + 1;
243 u32 hash
= jhash(name
, name_len
-1, 0);
245 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
246 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
247 if (!strcmp(name
, e
->name
)) {
248 DBG("tracepoint %s busy", name
);
249 return ERR_PTR(-EEXIST
); /* Already there */
253 * Using zmalloc here to allocate a variable length element. Could
254 * cause some memory fragmentation if overused.
256 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
258 return ERR_PTR(-ENOMEM
);
259 memcpy(&e
->name
[0], name
, name_len
);
262 cds_hlist_add_head(&e
->hlist
, head
);
267 * Remove the tracepoint from the tracepoint hash table. Must be called with
270 static inline 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(strcmp((*entry
)->name
, elem
->name
) != 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
;
343 cds_list_for_each_entry(lib
, &libs
, list
) {
344 tracepoint_update_probe_range(lib
->tracepoints_start
,
345 lib
->tracepoints_start
+ lib
->tracepoints_count
);
347 unlock_tracepoints();
351 * Update probes, removing the faulty probes.
353 static void tracepoint_update_probes(void)
355 /* tracepoints registered from libraries and executable. */
356 lib_update_tracepoints();
359 static struct tracepoint_probe
*
360 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
362 struct tracepoint_entry
*entry
;
363 struct tracepoint_probe
*old
;
365 entry
= get_tracepoint(name
);
367 entry
= add_tracepoint(name
);
369 return (struct tracepoint_probe
*)entry
;
371 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
372 if (IS_ERR(old
) && !entry
->refcount
)
373 remove_tracepoint(entry
);
378 * __tracepoint_probe_register - Connect a probe to a tracepoint
379 * @name: tracepoint name
380 * @probe: probe handler
382 * Returns 0 if ok, error value on error.
383 * The probe address must at least be aligned on the architecture pointer size.
385 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
390 old
= tracepoint_add_probe(name
, probe
, data
);
391 unlock_tracepoints();
395 tracepoint_update_probes(); /* may update entry */
400 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
402 struct tracepoint_entry
*entry
;
405 entry
= get_tracepoint(name
);
407 return ERR_PTR(-ENOENT
);
408 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
411 if (!entry
->refcount
)
412 remove_tracepoint(entry
);
417 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
418 * @name: tracepoint name
419 * @probe: probe function pointer
420 * @probe: probe data pointer
422 * We do not need to call a synchronize_sched to make sure the probes have
423 * finished running before doing a module unload, because the module unload
424 * itself uses stop_machine(), which insures that every preempt disabled section
427 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
432 old
= tracepoint_remove_probe(name
, probe
, data
);
433 unlock_tracepoints();
437 tracepoint_update_probes(); /* may update entry */
442 static void tracepoint_add_old_probes(void *old
)
446 struct tp_probes
*tp_probes
= _ust_container_of(old
,
447 struct tp_probes
, probes
[0]);
448 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
453 * tracepoint_probe_register_noupdate - register a probe but not connect
454 * @name: tracepoint name
455 * @probe: probe handler
457 * caller must call tracepoint_probe_update_all()
459 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
465 old
= tracepoint_add_probe(name
, probe
, data
);
467 unlock_tracepoints();
470 tracepoint_add_old_probes(old
);
471 unlock_tracepoints();
476 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
477 * @name: tracepoint name
478 * @probe: probe function pointer
480 * caller must call tracepoint_probe_update_all()
482 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
488 old
= tracepoint_remove_probe(name
, probe
, data
);
490 unlock_tracepoints();
493 tracepoint_add_old_probes(old
);
494 unlock_tracepoints();
499 * tracepoint_probe_update_all - update tracepoints
501 void tracepoint_probe_update_all(void)
503 CDS_LIST_HEAD(release_probes
);
504 struct tp_probes
*pos
, *next
;
508 unlock_tracepoints();
511 if (!cds_list_empty(&old_probes
))
512 cds_list_replace_init(&old_probes
, &release_probes
);
514 unlock_tracepoints();
516 tracepoint_update_probes();
517 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
518 cds_list_del(&pos
->u
.list
);
525 * Returns 0 if current not found.
526 * Returns 1 if current found.
528 * Called with tracepoint mutex held
530 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
532 struct tracepoint_lib
*iter_lib
;
535 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
536 if (iter_lib
< iter
->lib
)
538 else if (iter_lib
> iter
->lib
)
539 iter
->tracepoint
= NULL
;
540 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
541 iter_lib
->tracepoints_start
,
542 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
544 iter
->lib
= iter_lib
;
552 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
553 * @tracepoint: current tracepoints (in), next tracepoint (out)
554 * @begin: beginning of the range
555 * @end: end of the range
557 * Returns whether a next tracepoint has been found (1) or not (0).
558 * Will return the first tracepoint in the range if the input tracepoint is
560 * Called with tracepoint mutex held.
562 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
563 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
565 if (!*tracepoint
&& begin
!= end
)
567 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
569 (*tracepoint
)++; /* skip dummy */
577 * Called with tracepoint mutex held.
579 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
583 /* tracepoints in libs. */
584 found
= lib_get_iter_tracepoints(iter
);
586 tracepoint_iter_reset(iter
);
589 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
592 tracepoint_get_iter(iter
);
596 * Called with tracepoint mutex held.
598 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
602 * iter->tracepoint may be invalid because we blindly incremented it.
603 * Make sure it is valid by marshalling on the tracepoints, getting the
604 * tracepoints from following modules if necessary.
606 tracepoint_get_iter(iter
);
609 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
611 unlock_tracepoints();
614 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
616 iter
->tracepoint
= NULL
;
619 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
621 new_tracepoint_cb
= cb
;
624 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
626 if (new_tracepoint_cb
) {
627 struct tracepoint
* const *t
;
629 for (t
= start
; t
< end
; t
++) {
631 new_tracepoint_cb(*t
);
636 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
637 int tracepoints_count
)
639 struct tracepoint_lib
*pl
, *iter
;
643 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
645 pl
->tracepoints_start
= tracepoints_start
;
646 pl
->tracepoints_count
= tracepoints_count
;
650 * We sort the libs by struct lib pointer address.
652 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
653 BUG_ON(iter
== pl
); /* Should never be in the list twice */
655 /* We belong to the location right after iter. */
656 cds_list_add(&pl
->list
, &iter
->list
);
660 /* We should be added at the head of the list */
661 cds_list_add(&pl
->list
, &libs
);
663 unlock_tracepoints();
665 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
667 /* TODO: update just the loaded lib */
668 lib_update_tracepoints();
670 /* tracepoints_count - 1: skip dummy */
671 DBG("just registered a tracepoints section from %p and having %d tracepoints (minus dummy tracepoints)", tracepoints_start
, tracepoints_count
);
676 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
678 struct tracepoint_lib
*lib
;
681 cds_list_for_each_entry(lib
, &libs
, list
) {
682 if (lib
->tracepoints_start
== tracepoints_start
) {
683 struct tracepoint_lib
*lib2free
= lib
;
684 cds_list_del(&lib
->list
);
689 unlock_tracepoints();
694 void init_tracepoint(void)
696 if (uatomic_xchg(&initialized
, 1) == 1)
699 tracepoint_register_lib(__start___tracepoints_ptrs
,
700 __stop___tracepoints_ptrs
701 - __start___tracepoints_ptrs
);
704 void exit_tracepoint(void)
706 tracepoint_unregister_lib(__start___tracepoints_ptrs
);