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 /* Set to 1 to enable tracepoint debug output */
35 static const int tracepoint_debug
;
36 static int initialized
;
37 static void (*new_tracepoint_cb
)(struct tracepoint
*);
39 /* libraries that contain tracepoints (struct tracepoint_lib) */
40 static CDS_LIST_HEAD(libs
);
43 * Allow nested mutex for mutex listing and nested enable.
45 static __thread
int nested_mutex
;
48 * Tracepoints mutex protects the library tracepoints, the hash table,
49 * and the library list.
51 static pthread_mutex_t tracepoints_mutex
;
54 void lock_tracepoints(void)
56 if (!(nested_mutex
++))
57 pthread_mutex_lock(&tracepoints_mutex
);
61 void unlock_tracepoints(void)
63 if (!(--nested_mutex
))
64 pthread_mutex_unlock(&tracepoints_mutex
);
68 * Tracepoint hash table, containing the active tracepoints.
69 * Protected by tracepoints_mutex.
71 #define TRACEPOINT_HASH_BITS 6
72 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
73 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
75 static CDS_LIST_HEAD(old_probes
);
76 static int need_update
;
80 * It is used to to delay the free of multiple probes array until a quiescent
82 * Tracepoint entries modifications are protected by the tracepoints_mutex.
84 struct tracepoint_entry
{
85 struct cds_hlist_node hlist
;
86 struct tracepoint_probe
*probes
;
87 int refcount
; /* Number of times armed. 0 if disarmed. */
93 struct cds_list_head list
;
95 struct tracepoint_probe probes
[0];
98 static inline void *allocate_probes(int count
)
100 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
101 + sizeof(struct tp_probes
));
102 return p
== NULL
? NULL
: p
->probes
;
105 static inline void release_probes(void *old
)
108 struct tp_probes
*tp_probes
= _ust_container_of(old
,
109 struct tp_probes
, probes
[0]);
115 static void debug_print_probes(struct tracepoint_entry
*entry
)
119 if (!tracepoint_debug
|| !entry
->probes
)
122 for (i
= 0; entry
->probes
[i
].func
; i
++)
123 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
127 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
128 void *probe
, void *data
)
131 struct tracepoint_probe
*old
, *new;
135 debug_print_probes(entry
);
138 /* (N -> N+1), (N != 0, 1) probes */
139 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
140 if (old
[nr_probes
].func
== probe
&&
141 old
[nr_probes
].data
== data
)
142 return ERR_PTR(-EEXIST
);
144 /* + 2 : one for new probe, one for NULL func */
145 new = allocate_probes(nr_probes
+ 2);
147 return ERR_PTR(-ENOMEM
);
149 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
150 new[nr_probes
].func
= probe
;
151 new[nr_probes
].data
= data
;
152 new[nr_probes
+ 1].func
= NULL
;
153 entry
->refcount
= nr_probes
+ 1;
155 debug_print_probes(entry
);
160 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
163 int nr_probes
= 0, nr_del
= 0, i
;
164 struct tracepoint_probe
*old
, *new;
169 return ERR_PTR(-ENOENT
);
171 debug_print_probes(entry
);
172 /* (N -> M), (N > 1, M >= 0) probes */
173 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
175 (old
[nr_probes
].func
== probe
&&
176 old
[nr_probes
].data
== data
))
180 if (nr_probes
- nr_del
== 0) {
181 /* N -> 0, (N > 1) */
182 entry
->probes
= NULL
;
184 debug_print_probes(entry
);
188 /* N -> M, (N > 1, M > 0) */
190 new = allocate_probes(nr_probes
- nr_del
+ 1);
192 return ERR_PTR(-ENOMEM
);
193 for (i
= 0; old
[i
].func
; i
++)
195 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
197 new[nr_probes
- nr_del
].func
= NULL
;
198 entry
->refcount
= nr_probes
- nr_del
;
201 debug_print_probes(entry
);
206 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
207 * Must be called with tracepoints_mutex held.
208 * Returns NULL if not present.
210 static struct tracepoint_entry
*get_tracepoint(const char *name
)
212 struct cds_hlist_head
*head
;
213 struct cds_hlist_node
*node
;
214 struct tracepoint_entry
*e
;
215 u32 hash
= jhash(name
, strlen(name
), 0);
217 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
218 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
219 if (!strcmp(name
, e
->name
))
226 * Add the tracepoint to the tracepoint hash table. Must be called with
227 * tracepoints_mutex held.
229 static struct tracepoint_entry
*add_tracepoint(const char *name
)
231 struct cds_hlist_head
*head
;
232 struct cds_hlist_node
*node
;
233 struct tracepoint_entry
*e
;
234 size_t name_len
= strlen(name
) + 1;
235 u32 hash
= jhash(name
, name_len
-1, 0);
237 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
238 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
239 if (!strcmp(name
, e
->name
)) {
240 DBG("tracepoint %s busy", name
);
241 return ERR_PTR(-EEXIST
); /* Already there */
245 * Using zmalloc here to allocate a variable length element. Could
246 * cause some memory fragmentation if overused.
248 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
250 return ERR_PTR(-ENOMEM
);
251 memcpy(&e
->name
[0], name
, name_len
);
254 cds_hlist_add_head(&e
->hlist
, head
);
259 * Remove the tracepoint from the tracepoint hash table. Must be called with
262 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
264 cds_hlist_del(&e
->hlist
);
269 * Sets the probe callback corresponding to one tracepoint.
271 static void set_tracepoint(struct tracepoint_entry
**entry
,
272 struct tracepoint
*elem
, int active
)
274 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
277 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
278 * probe callbacks array is consistent before setting a pointer to it.
279 * This array is referenced by __DO_TRACE from
280 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
283 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
284 elem
->state
= active
;
288 * Disable a tracepoint and its probe callback.
289 * Note: only waiting an RCU period after setting elem->call to the empty
290 * function insures that the original callback is not used anymore. This insured
291 * by preempt_disable around the call site.
293 static void disable_tracepoint(struct tracepoint
*elem
)
296 rcu_assign_pointer(elem
->probes
, NULL
);
300 * tracepoint_update_probe_range - Update a probe range
301 * @begin: beginning of the range
302 * @end: end of the range
304 * Updates the probe callback corresponding to a range of tracepoints.
307 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
308 struct tracepoint
* const *end
)
310 struct tracepoint
* const *iter
;
311 struct tracepoint_entry
*mark_entry
;
313 for (iter
= begin
; iter
< end
; iter
++) {
315 continue; /* skip dummy */
316 if (!(*iter
)->name
) {
317 disable_tracepoint(*iter
);
320 mark_entry
= get_tracepoint((*iter
)->name
);
322 set_tracepoint(&mark_entry
, *iter
,
323 !!mark_entry
->refcount
);
325 disable_tracepoint(*iter
);
330 static void lib_update_tracepoints(void)
332 struct tracepoint_lib
*lib
;
335 cds_list_for_each_entry(lib
, &libs
, list
) {
336 tracepoint_update_probe_range(lib
->tracepoints_start
,
337 lib
->tracepoints_start
+ lib
->tracepoints_count
);
339 unlock_tracepoints();
343 * Update probes, removing the faulty probes.
345 static void tracepoint_update_probes(void)
347 /* tracepoints registered from libraries and executable. */
348 lib_update_tracepoints();
351 static struct tracepoint_probe
*
352 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
354 struct tracepoint_entry
*entry
;
355 struct tracepoint_probe
*old
;
357 entry
= get_tracepoint(name
);
359 entry
= add_tracepoint(name
);
361 return (struct tracepoint_probe
*)entry
;
363 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
364 if (IS_ERR(old
) && !entry
->refcount
)
365 remove_tracepoint(entry
);
370 * __tracepoint_probe_register - Connect a probe to a tracepoint
371 * @name: tracepoint name
372 * @probe: probe handler
374 * Returns 0 if ok, error value on error.
375 * The probe address must at least be aligned on the architecture pointer size.
377 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
382 old
= tracepoint_add_probe(name
, probe
, data
);
383 unlock_tracepoints();
387 tracepoint_update_probes(); /* may update entry */
392 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
394 struct tracepoint_entry
*entry
;
397 entry
= get_tracepoint(name
);
399 return ERR_PTR(-ENOENT
);
400 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
403 if (!entry
->refcount
)
404 remove_tracepoint(entry
);
409 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
410 * @name: tracepoint name
411 * @probe: probe function pointer
412 * @probe: probe data pointer
414 * We do not need to call a synchronize_sched to make sure the probes have
415 * finished running before doing a module unload, because the module unload
416 * itself uses stop_machine(), which insures that every preempt disabled section
419 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
424 old
= tracepoint_remove_probe(name
, probe
, data
);
425 unlock_tracepoints();
429 tracepoint_update_probes(); /* may update entry */
434 static void tracepoint_add_old_probes(void *old
)
438 struct tp_probes
*tp_probes
= _ust_container_of(old
,
439 struct tp_probes
, probes
[0]);
440 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
445 * tracepoint_probe_register_noupdate - register a probe but not connect
446 * @name: tracepoint name
447 * @probe: probe handler
449 * caller must call tracepoint_probe_update_all()
451 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
457 old
= tracepoint_add_probe(name
, probe
, data
);
459 unlock_tracepoints();
462 tracepoint_add_old_probes(old
);
463 unlock_tracepoints();
468 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
469 * @name: tracepoint name
470 * @probe: probe function pointer
472 * caller must call tracepoint_probe_update_all()
474 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
480 old
= tracepoint_remove_probe(name
, probe
, data
);
482 unlock_tracepoints();
485 tracepoint_add_old_probes(old
);
486 unlock_tracepoints();
491 * tracepoint_probe_update_all - update tracepoints
493 void tracepoint_probe_update_all(void)
495 CDS_LIST_HEAD(release_probes
);
496 struct tp_probes
*pos
, *next
;
500 unlock_tracepoints();
503 if (!cds_list_empty(&old_probes
))
504 cds_list_replace_init(&old_probes
, &release_probes
);
506 unlock_tracepoints();
508 tracepoint_update_probes();
509 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
510 cds_list_del(&pos
->u
.list
);
517 * Returns 0 if current not found.
518 * Returns 1 if current found.
520 * Called with tracepoint mutex held
522 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
524 struct tracepoint_lib
*iter_lib
;
527 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
528 if (iter_lib
< iter
->lib
)
530 else if (iter_lib
> iter
->lib
)
531 iter
->tracepoint
= NULL
;
532 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
533 iter_lib
->tracepoints_start
,
534 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
536 iter
->lib
= iter_lib
;
544 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
545 * @tracepoint: current tracepoints (in), next tracepoint (out)
546 * @begin: beginning of the range
547 * @end: end of the range
549 * Returns whether a next tracepoint has been found (1) or not (0).
550 * Will return the first tracepoint in the range if the input tracepoint is
552 * Called with tracepoint mutex held.
554 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
555 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
557 if (!*tracepoint
&& begin
!= end
)
559 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
561 (*tracepoint
)++; /* skip dummy */
569 * Called with tracepoint mutex held.
571 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
575 /* tracepoints in libs. */
576 found
= lib_get_iter_tracepoints(iter
);
578 tracepoint_iter_reset(iter
);
581 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
584 tracepoint_get_iter(iter
);
588 * Called with tracepoint mutex held.
590 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
594 * iter->tracepoint may be invalid because we blindly incremented it.
595 * Make sure it is valid by marshalling on the tracepoints, getting the
596 * tracepoints from following modules if necessary.
598 tracepoint_get_iter(iter
);
601 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
603 unlock_tracepoints();
606 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
608 iter
->tracepoint
= NULL
;
611 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
613 new_tracepoint_cb
= cb
;
616 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
618 if (new_tracepoint_cb
) {
619 struct tracepoint
* const *t
;
621 for (t
= start
; t
< end
; t
++) {
623 new_tracepoint_cb(*t
);
628 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
629 int tracepoints_count
)
631 struct tracepoint_lib
*pl
, *iter
;
635 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
637 pl
->tracepoints_start
= tracepoints_start
;
638 pl
->tracepoints_count
= tracepoints_count
;
642 * We sort the libs by struct lib pointer address.
644 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
645 BUG_ON(iter
== pl
); /* Should never be in the list twice */
647 /* We belong to the location right after iter. */
648 cds_list_add(&pl
->list
, &iter
->list
);
652 /* We should be added at the head of the list */
653 cds_list_add(&pl
->list
, &libs
);
655 unlock_tracepoints();
657 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
659 /* TODO: update just the loaded lib */
660 lib_update_tracepoints();
662 DBG("just registered a tracepoints section from %p and having %d tracepoints",
663 tracepoints_start
, tracepoints_count
);
668 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
670 struct tracepoint_lib
*lib
;
673 cds_list_for_each_entry(lib
, &libs
, list
) {
674 if (lib
->tracepoints_start
== tracepoints_start
) {
675 struct tracepoint_lib
*lib2free
= lib
;
676 cds_list_del(&lib
->list
);
681 unlock_tracepoints();
686 void init_tracepoint(void)
688 if (uatomic_xchg(&initialized
, 1) == 1)
693 void exit_tracepoint(void)