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>
31 #include <ust/usterr-signal-safe.h>
33 extern struct tracepoint
* const __start___tracepoints_ptrs
[]
34 __attribute__((visibility("hidden")));
35 extern struct tracepoint
* const __stop___tracepoints_ptrs
[]
36 __attribute__((visibility("hidden")));
38 static struct tracepoint
* __tracepoint_ptr_dummy
39 __attribute__((used
, section("__tracepoints_ptrs")));
41 /* Set to 1 to enable tracepoint debug output */
42 static const int tracepoint_debug
;
43 static int initialized
;
44 static void (*new_tracepoint_cb
)(struct tracepoint
*);
46 /* libraries that contain tracepoints (struct tracepoint_lib) */
47 static CDS_LIST_HEAD(libs
);
50 * Allow nested mutex for mutex listing and nested enable.
52 static __thread
int nested_mutex
;
55 * Tracepoints mutex protects the library tracepoints, the hash table,
56 * and the library list.
58 static pthread_mutex_t tracepoints_mutex
;
61 void lock_tracepoints(void)
63 if (!(nested_mutex
++))
64 pthread_mutex_lock(&tracepoints_mutex
);
68 void unlock_tracepoints(void)
70 if (!(--nested_mutex
))
71 pthread_mutex_unlock(&tracepoints_mutex
);
75 * Tracepoint hash table, containing the active tracepoints.
76 * Protected by tracepoints_mutex.
78 #define TRACEPOINT_HASH_BITS 6
79 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
80 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
82 static CDS_LIST_HEAD(old_probes
);
83 static int need_update
;
87 * It is used to to delay the free of multiple probes array until a quiescent
89 * Tracepoint entries modifications are protected by the tracepoints_mutex.
91 struct tracepoint_entry
{
92 struct cds_hlist_node hlist
;
93 struct tracepoint_probe
*probes
;
94 int refcount
; /* Number of times armed. 0 if disarmed. */
100 struct cds_list_head list
;
102 struct tracepoint_probe probes
[0];
105 static inline void *allocate_probes(int count
)
107 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
108 + sizeof(struct tp_probes
));
109 return p
== NULL
? NULL
: p
->probes
;
112 static inline void release_probes(void *old
)
115 struct tp_probes
*tp_probes
= _ust_container_of(old
,
116 struct tp_probes
, probes
[0]);
122 static void debug_print_probes(struct tracepoint_entry
*entry
)
126 if (!tracepoint_debug
|| !entry
->probes
)
129 for (i
= 0; entry
->probes
[i
].func
; i
++)
130 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
134 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
135 void *probe
, void *data
)
138 struct tracepoint_probe
*old
, *new;
142 debug_print_probes(entry
);
145 /* (N -> N+1), (N != 0, 1) probes */
146 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
147 if (old
[nr_probes
].func
== probe
&&
148 old
[nr_probes
].data
== data
)
149 return ERR_PTR(-EEXIST
);
151 /* + 2 : one for new probe, one for NULL func */
152 new = allocate_probes(nr_probes
+ 2);
154 return ERR_PTR(-ENOMEM
);
156 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
157 new[nr_probes
].func
= probe
;
158 new[nr_probes
].data
= data
;
159 new[nr_probes
+ 1].func
= NULL
;
160 entry
->refcount
= nr_probes
+ 1;
162 debug_print_probes(entry
);
167 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
170 int nr_probes
= 0, nr_del
= 0, i
;
171 struct tracepoint_probe
*old
, *new;
176 return ERR_PTR(-ENOENT
);
178 debug_print_probes(entry
);
179 /* (N -> M), (N > 1, M >= 0) probes */
180 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
182 (old
[nr_probes
].func
== probe
&&
183 old
[nr_probes
].data
== data
))
187 if (nr_probes
- nr_del
== 0) {
188 /* N -> 0, (N > 1) */
189 entry
->probes
= NULL
;
191 debug_print_probes(entry
);
195 /* N -> M, (N > 1, M > 0) */
197 new = allocate_probes(nr_probes
- nr_del
+ 1);
199 return ERR_PTR(-ENOMEM
);
200 for (i
= 0; old
[i
].func
; i
++)
202 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
204 new[nr_probes
- nr_del
].func
= NULL
;
205 entry
->refcount
= nr_probes
- nr_del
;
208 debug_print_probes(entry
);
213 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
214 * Must be called with tracepoints_mutex held.
215 * Returns NULL if not present.
217 static struct tracepoint_entry
*get_tracepoint(const char *name
)
219 struct cds_hlist_head
*head
;
220 struct cds_hlist_node
*node
;
221 struct tracepoint_entry
*e
;
222 u32 hash
= jhash(name
, strlen(name
), 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
))
233 * Add the tracepoint to the tracepoint hash table. Must be called with
234 * tracepoints_mutex held.
236 static struct tracepoint_entry
*add_tracepoint(const char *name
)
238 struct cds_hlist_head
*head
;
239 struct cds_hlist_node
*node
;
240 struct tracepoint_entry
*e
;
241 size_t name_len
= strlen(name
) + 1;
242 u32 hash
= jhash(name
, name_len
-1, 0);
244 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
245 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
246 if (!strcmp(name
, e
->name
)) {
247 DBG("tracepoint %s busy", name
);
248 return ERR_PTR(-EEXIST
); /* Already there */
252 * Using zmalloc here to allocate a variable length element. Could
253 * cause some memory fragmentation if overused.
255 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
257 return ERR_PTR(-ENOMEM
);
258 memcpy(&e
->name
[0], name
, name_len
);
261 cds_hlist_add_head(&e
->hlist
, head
);
266 * Remove the tracepoint from the tracepoint hash table. Must be called with
269 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
271 cds_hlist_del(&e
->hlist
);
276 * Sets the probe callback corresponding to one tracepoint.
278 static void set_tracepoint(struct tracepoint_entry
**entry
,
279 struct tracepoint
*elem
, int active
)
281 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
284 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
285 * probe callbacks array is consistent before setting a pointer to it.
286 * This array is referenced by __DO_TRACE from
287 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
290 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
291 elem
->state
= active
;
295 * Disable a tracepoint and its probe callback.
296 * Note: only waiting an RCU period after setting elem->call to the empty
297 * function insures that the original callback is not used anymore. This insured
298 * by preempt_disable around the call site.
300 static void disable_tracepoint(struct tracepoint
*elem
)
303 rcu_assign_pointer(elem
->probes
, NULL
);
307 * tracepoint_update_probe_range - Update a probe range
308 * @begin: beginning of the range
309 * @end: end of the range
311 * Updates the probe callback corresponding to a range of tracepoints.
314 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
315 struct tracepoint
* const *end
)
317 struct tracepoint
* const *iter
;
318 struct tracepoint_entry
*mark_entry
;
320 for (iter
= begin
; iter
< end
; iter
++) {
322 continue; /* skip dummy */
323 if (!(*iter
)->name
) {
324 disable_tracepoint(*iter
);
327 mark_entry
= get_tracepoint((*iter
)->name
);
329 set_tracepoint(&mark_entry
, *iter
,
330 !!mark_entry
->refcount
);
332 disable_tracepoint(*iter
);
337 static void lib_update_tracepoints(void)
339 struct tracepoint_lib
*lib
;
342 cds_list_for_each_entry(lib
, &libs
, list
) {
343 tracepoint_update_probe_range(lib
->tracepoints_start
,
344 lib
->tracepoints_start
+ lib
->tracepoints_count
);
346 unlock_tracepoints();
350 * Update probes, removing the faulty probes.
352 static void tracepoint_update_probes(void)
354 /* tracepoints registered from libraries and executable. */
355 lib_update_tracepoints();
358 static struct tracepoint_probe
*
359 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
361 struct tracepoint_entry
*entry
;
362 struct tracepoint_probe
*old
;
364 entry
= get_tracepoint(name
);
366 entry
= add_tracepoint(name
);
368 return (struct tracepoint_probe
*)entry
;
370 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
371 if (IS_ERR(old
) && !entry
->refcount
)
372 remove_tracepoint(entry
);
377 * __tracepoint_probe_register - Connect a probe to a tracepoint
378 * @name: tracepoint name
379 * @probe: probe handler
381 * Returns 0 if ok, error value on error.
382 * The probe address must at least be aligned on the architecture pointer size.
384 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
389 old
= tracepoint_add_probe(name
, probe
, data
);
390 unlock_tracepoints();
394 tracepoint_update_probes(); /* may update entry */
399 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
401 struct tracepoint_entry
*entry
;
404 entry
= get_tracepoint(name
);
406 return ERR_PTR(-ENOENT
);
407 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
410 if (!entry
->refcount
)
411 remove_tracepoint(entry
);
416 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
417 * @name: tracepoint name
418 * @probe: probe function pointer
419 * @probe: probe data pointer
421 * We do not need to call a synchronize_sched to make sure the probes have
422 * finished running before doing a module unload, because the module unload
423 * itself uses stop_machine(), which insures that every preempt disabled section
426 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
431 old
= tracepoint_remove_probe(name
, probe
, data
);
432 unlock_tracepoints();
436 tracepoint_update_probes(); /* may update entry */
441 static void tracepoint_add_old_probes(void *old
)
445 struct tp_probes
*tp_probes
= _ust_container_of(old
,
446 struct tp_probes
, probes
[0]);
447 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
452 * tracepoint_probe_register_noupdate - register a probe but not connect
453 * @name: tracepoint name
454 * @probe: probe handler
456 * caller must call tracepoint_probe_update_all()
458 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
464 old
= tracepoint_add_probe(name
, probe
, data
);
466 unlock_tracepoints();
469 tracepoint_add_old_probes(old
);
470 unlock_tracepoints();
475 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
476 * @name: tracepoint name
477 * @probe: probe function pointer
479 * caller must call tracepoint_probe_update_all()
481 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
487 old
= tracepoint_remove_probe(name
, probe
, data
);
489 unlock_tracepoints();
492 tracepoint_add_old_probes(old
);
493 unlock_tracepoints();
498 * tracepoint_probe_update_all - update tracepoints
500 void tracepoint_probe_update_all(void)
502 CDS_LIST_HEAD(release_probes
);
503 struct tp_probes
*pos
, *next
;
507 unlock_tracepoints();
510 if (!cds_list_empty(&old_probes
))
511 cds_list_replace_init(&old_probes
, &release_probes
);
513 unlock_tracepoints();
515 tracepoint_update_probes();
516 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
517 cds_list_del(&pos
->u
.list
);
524 * Returns 0 if current not found.
525 * Returns 1 if current found.
527 * Called with tracepoint mutex held
529 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
531 struct tracepoint_lib
*iter_lib
;
534 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
535 if (iter_lib
< iter
->lib
)
537 else if (iter_lib
> iter
->lib
)
538 iter
->tracepoint
= NULL
;
539 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
540 iter_lib
->tracepoints_start
,
541 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
543 iter
->lib
= iter_lib
;
551 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
552 * @tracepoint: current tracepoints (in), next tracepoint (out)
553 * @begin: beginning of the range
554 * @end: end of the range
556 * Returns whether a next tracepoint has been found (1) or not (0).
557 * Will return the first tracepoint in the range if the input tracepoint is
559 * Called with tracepoint mutex held.
561 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
562 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
564 if (!*tracepoint
&& begin
!= end
)
566 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
568 (*tracepoint
)++; /* skip dummy */
576 * Called with tracepoint mutex held.
578 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
582 /* tracepoints in libs. */
583 found
= lib_get_iter_tracepoints(iter
);
585 tracepoint_iter_reset(iter
);
588 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
591 tracepoint_get_iter(iter
);
595 * Called with tracepoint mutex held.
597 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
601 * iter->tracepoint may be invalid because we blindly incremented it.
602 * Make sure it is valid by marshalling on the tracepoints, getting the
603 * tracepoints from following modules if necessary.
605 tracepoint_get_iter(iter
);
608 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
610 unlock_tracepoints();
613 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
615 iter
->tracepoint
= NULL
;
618 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
620 new_tracepoint_cb
= cb
;
623 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
625 if (new_tracepoint_cb
) {
626 struct tracepoint
* const *t
;
628 for (t
= start
; t
< end
; t
++) {
630 new_tracepoint_cb(*t
);
635 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
636 int tracepoints_count
)
638 struct tracepoint_lib
*pl
, *iter
;
640 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
642 pl
->tracepoints_start
= tracepoints_start
;
643 pl
->tracepoints_count
= tracepoints_count
;
647 * We sort the libs by struct lib pointer address.
649 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
650 BUG_ON(iter
== pl
); /* Should never be in the list twice */
652 /* We belong to the location right after iter. */
653 cds_list_add(&pl
->list
, &iter
->list
);
657 /* We should be added at the head of the list */
658 cds_list_add(&pl
->list
, &libs
);
660 unlock_tracepoints();
662 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
664 /* TODO: update just the loaded lib */
665 lib_update_tracepoints();
667 /* tracepoints_count - 1: skip dummy */
668 DBG("just registered a tracepoints section from %p and having %d tracepoints (minus dummy tracepoints)", tracepoints_start
, tracepoints_count
);
673 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
675 struct tracepoint_lib
*lib
;
678 cds_list_for_each_entry(lib
, &libs
, list
) {
679 if (lib
->tracepoints_start
== tracepoints_start
) {
680 struct tracepoint_lib
*lib2free
= lib
;
681 cds_list_del(&lib
->list
);
686 unlock_tracepoints();
691 void __attribute__((constructor
)) init_tracepoint(void)
695 tracepoint_register_lib(__start___tracepoints_ptrs
,
696 __stop___tracepoints_ptrs
697 - __start___tracepoints_ptrs
);
702 void __attribute__((destructor
)) destroy_tracepoint(void)
704 tracepoint_unregister_lib(__start___tracepoints_ptrs
);