2 * Copyright (C) 2008 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; either
8 * version 2.1 of the License, or (at your option) any later version.
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.
23 #include <ust/tracepoint.h>
25 #include <ust/kcompat/kcompat.h>
31 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
32 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
34 /* Set to 1 to enable tracepoint debug output */
35 static const int tracepoint_debug
;
37 /* libraries that contain tracepoints (struct tracepoint_lib) */
38 static LIST_HEAD(libs
);
41 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
42 * builtin and module tracepoints and the hash table.
44 static DEFINE_MUTEX(tracepoints_mutex
);
47 * Tracepoint hash table, containing the active tracepoints.
48 * Protected by tracepoints_mutex.
50 #define TRACEPOINT_HASH_BITS 6
51 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
52 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
56 * It is used to to delay the free of multiple probes array until a quiescent
58 * Tracepoint entries modifications are protected by the tracepoints_mutex.
60 struct tracepoint_entry
{
61 struct hlist_node hlist
;
63 int refcount
; /* Number of times armed. 0 if disarmed. */
69 //ust// struct rcu_head rcu;
70 struct list_head list
;
75 static inline void *allocate_probes(int count
)
77 struct tp_probes
*p
= malloc(count
* sizeof(void *)
78 + sizeof(struct tp_probes
));
79 return p
== NULL
? NULL
: p
->probes
;
82 //ust// static void rcu_free_old_probes(struct rcu_head *head)
84 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
87 static inline void release_probes(void *old
)
90 struct tp_probes
*tp_probes
= container_of(old
,
91 struct tp_probes
, probes
[0]);
92 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
98 static void debug_print_probes(struct tracepoint_entry
*entry
)
102 if (!tracepoint_debug
|| !entry
->funcs
)
105 for (i
= 0; entry
->funcs
[i
]; i
++)
106 DBG("Probe %d : %p", i
, entry
->funcs
[i
]);
110 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
117 debug_print_probes(entry
);
120 /* (N -> N+1), (N != 0, 1) probes */
121 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
122 if (old
[nr_probes
] == probe
)
123 return ERR_PTR(-EEXIST
);
125 /* + 2 : one for new probe, one for NULL func */
126 new = allocate_probes(nr_probes
+ 2);
128 return ERR_PTR(-ENOMEM
);
130 memcpy(new, old
, nr_probes
* sizeof(void *));
131 new[nr_probes
] = probe
;
132 new[nr_probes
+ 1] = NULL
;
133 entry
->refcount
= nr_probes
+ 1;
135 debug_print_probes(entry
);
140 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
142 int nr_probes
= 0, nr_del
= 0, i
;
148 return ERR_PTR(-ENOENT
);
150 debug_print_probes(entry
);
151 /* (N -> M), (N > 1, M >= 0) probes */
152 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
153 if ((!probe
|| old
[nr_probes
] == probe
))
157 if (nr_probes
- nr_del
== 0) {
158 /* N -> 0, (N > 1) */
161 debug_print_probes(entry
);
165 /* N -> M, (N > 1, M > 0) */
167 new = allocate_probes(nr_probes
- nr_del
+ 1);
169 return ERR_PTR(-ENOMEM
);
170 for (i
= 0; old
[i
]; i
++)
171 if ((probe
&& old
[i
] != probe
))
173 new[nr_probes
- nr_del
] = NULL
;
174 entry
->refcount
= nr_probes
- nr_del
;
177 debug_print_probes(entry
);
182 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
183 * Must be called with tracepoints_mutex held.
184 * Returns NULL if not present.
186 static struct tracepoint_entry
*get_tracepoint(const char *name
)
188 struct hlist_head
*head
;
189 struct hlist_node
*node
;
190 struct tracepoint_entry
*e
;
191 u32 hash
= jhash(name
, strlen(name
), 0);
193 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
194 hlist_for_each_entry(e
, node
, head
, hlist
) {
195 if (!strcmp(name
, e
->name
))
202 * Add the tracepoint to the tracepoint hash table. Must be called with
203 * tracepoints_mutex held.
205 static struct tracepoint_entry
*add_tracepoint(const char *name
)
207 struct hlist_head
*head
;
208 struct hlist_node
*node
;
209 struct tracepoint_entry
*e
;
210 size_t name_len
= strlen(name
) + 1;
211 u32 hash
= jhash(name
, name_len
-1, 0);
213 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
214 hlist_for_each_entry(e
, node
, head
, hlist
) {
215 if (!strcmp(name
, e
->name
)) {
216 DBG("tracepoint %s busy", name
);
217 return ERR_PTR(-EEXIST
); /* Already there */
221 * Using kmalloc here to allocate a variable length element. Could
222 * cause some memory fragmentation if overused.
224 e
= malloc(sizeof(struct tracepoint_entry
) + name_len
);
226 return ERR_PTR(-ENOMEM
);
227 memcpy(&e
->name
[0], name
, name_len
);
230 hlist_add_head(&e
->hlist
, head
);
235 * Remove the tracepoint from the tracepoint hash table. Must be called with
238 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
240 hlist_del(&e
->hlist
);
245 * Sets the probe callback corresponding to one tracepoint.
247 static void set_tracepoint(struct tracepoint_entry
**entry
,
248 struct tracepoint
*elem
, int active
)
250 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
253 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
254 * probe callbacks array is consistent before setting a pointer to it.
255 * This array is referenced by __DO_TRACE from
256 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
259 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
260 elem
->state__imv
= active
;
264 * Disable a tracepoint and its probe callback.
265 * Note: only waiting an RCU period after setting elem->call to the empty
266 * function insures that the original callback is not used anymore. This insured
267 * by preempt_disable around the call site.
269 static void disable_tracepoint(struct tracepoint
*elem
)
271 elem
->state__imv
= 0;
272 rcu_assign_pointer(elem
->funcs
, NULL
);
276 * tracepoint_update_probe_range - Update a probe range
277 * @begin: beginning of the range
278 * @end: end of the range
280 * Updates the probe callback corresponding to a range of tracepoints.
282 void tracepoint_update_probe_range(struct tracepoint
*begin
,
283 struct tracepoint
*end
)
285 struct tracepoint
*iter
;
286 struct tracepoint_entry
*mark_entry
;
288 pthread_mutex_lock(&tracepoints_mutex
);
289 for (iter
= begin
; iter
< end
; iter
++) {
290 mark_entry
= get_tracepoint(iter
->name
);
292 set_tracepoint(&mark_entry
, iter
,
293 !!mark_entry
->refcount
);
295 disable_tracepoint(iter
);
298 pthread_mutex_unlock(&tracepoints_mutex
);
301 static void lib_update_tracepoints(void)
303 struct tracepoint_lib
*lib
;
305 //ust// pthread_mutex_lock(&module_mutex);
306 list_for_each_entry(lib
, &libs
, list
)
307 tracepoint_update_probe_range(lib
->tracepoints_start
,
308 lib
->tracepoints_start
+ lib
->tracepoints_count
);
309 //ust// pthread_mutex_unlock(&module_mutex);
313 * Update probes, removing the faulty probes.
315 static void tracepoint_update_probes(void)
317 /* Core kernel tracepoints */
318 //ust// tracepoint_update_probe_range(__start___tracepoints,
319 //ust// __stop___tracepoints);
320 /* tracepoints in modules. */
321 lib_update_tracepoints();
322 /* Update immediate values */
324 //ust// module_imv_update();
327 static void *tracepoint_add_probe(const char *name
, void *probe
)
329 struct tracepoint_entry
*entry
;
332 entry
= get_tracepoint(name
);
334 entry
= add_tracepoint(name
);
338 old
= tracepoint_entry_add_probe(entry
, probe
);
339 if (IS_ERR(old
) && !entry
->refcount
)
340 remove_tracepoint(entry
);
345 * tracepoint_probe_register - Connect a probe to a tracepoint
346 * @name: tracepoint name
347 * @probe: probe handler
349 * Returns 0 if ok, error value on error.
350 * The probe address must at least be aligned on the architecture pointer size.
352 int tracepoint_probe_register(const char *name
, void *probe
)
356 pthread_mutex_lock(&tracepoints_mutex
);
357 old
= tracepoint_add_probe(name
, probe
);
358 pthread_mutex_unlock(&tracepoints_mutex
);
362 tracepoint_update_probes(); /* may update entry */
366 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
368 static void *tracepoint_remove_probe(const char *name
, void *probe
)
370 struct tracepoint_entry
*entry
;
373 entry
= get_tracepoint(name
);
375 return ERR_PTR(-ENOENT
);
376 old
= tracepoint_entry_remove_probe(entry
, probe
);
379 if (!entry
->refcount
)
380 remove_tracepoint(entry
);
385 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
386 * @name: tracepoint name
387 * @probe: probe function pointer
389 * We do not need to call a synchronize_sched to make sure the probes have
390 * finished running before doing a module unload, because the module unload
391 * itself uses stop_machine(), which insures that every preempt disabled section
394 int tracepoint_probe_unregister(const char *name
, void *probe
)
398 pthread_mutex_lock(&tracepoints_mutex
);
399 old
= tracepoint_remove_probe(name
, probe
);
400 pthread_mutex_unlock(&tracepoints_mutex
);
404 tracepoint_update_probes(); /* may update entry */
408 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
410 static LIST_HEAD(old_probes
);
411 static int need_update
;
413 static void tracepoint_add_old_probes(void *old
)
417 struct tp_probes
*tp_probes
= container_of(old
,
418 struct tp_probes
, probes
[0]);
419 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()
430 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
434 pthread_mutex_lock(&tracepoints_mutex
);
435 old
= tracepoint_add_probe(name
, probe
);
437 pthread_mutex_unlock(&tracepoints_mutex
);
440 tracepoint_add_old_probes(old
);
441 pthread_mutex_unlock(&tracepoints_mutex
);
444 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
447 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
448 * @name: tracepoint name
449 * @probe: probe function pointer
451 * caller must call tracepoint_probe_update_all()
453 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
457 pthread_mutex_lock(&tracepoints_mutex
);
458 old
= tracepoint_remove_probe(name
, probe
);
460 pthread_mutex_unlock(&tracepoints_mutex
);
463 tracepoint_add_old_probes(old
);
464 pthread_mutex_unlock(&tracepoints_mutex
);
467 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
470 * tracepoint_probe_update_all - update tracepoints
472 void tracepoint_probe_update_all(void)
474 LIST_HEAD(release_probes
);
475 struct tp_probes
*pos
, *next
;
477 pthread_mutex_lock(&tracepoints_mutex
);
479 pthread_mutex_unlock(&tracepoints_mutex
);
482 if (!list_empty(&old_probes
))
483 list_replace_init(&old_probes
, &release_probes
);
485 pthread_mutex_unlock(&tracepoints_mutex
);
487 tracepoint_update_probes();
488 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
489 list_del(&pos
->u
.list
);
490 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
495 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
498 * Returns 0 if current not found.
499 * Returns 1 if current found.
501 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
503 struct tracepoint_lib
*iter_lib
;
506 //ust// pthread_mutex_lock(&module_mutex);
507 list_for_each_entry(iter_lib
, &libs
, list
) {
508 if (iter_lib
< iter
->lib
)
510 else if (iter_lib
> iter
->lib
)
511 iter
->tracepoint
= NULL
;
512 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
513 iter_lib
->tracepoints_start
,
514 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
516 iter
->lib
= iter_lib
;
520 //ust// pthread_mutex_unlock(&module_mutex);
525 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
526 * @tracepoint: current tracepoints (in), next tracepoint (out)
527 * @begin: beginning of the range
528 * @end: end of the range
530 * Returns whether a next tracepoint has been found (1) or not (0).
531 * Will return the first tracepoint in the range if the input tracepoint is
534 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
535 struct tracepoint
*begin
, struct tracepoint
*end
)
537 if (!*tracepoint
&& begin
!= end
) {
541 if (*tracepoint
>= begin
&& *tracepoint
< end
)
545 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
547 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
551 //ust// /* Core kernel tracepoints */
552 //ust// if (!iter->module) {
553 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
554 //ust// __start___tracepoints, __stop___tracepoints);
558 /* tracepoints in libs. */
559 found
= lib_get_iter_tracepoints(iter
);
562 tracepoint_iter_reset(iter
);
565 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
567 tracepoint_get_iter(iter
);
569 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
571 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
575 * iter->tracepoint may be invalid because we blindly incremented it.
576 * Make sure it is valid by marshalling on the tracepoints, getting the
577 * tracepoints from following modules if necessary.
579 tracepoint_get_iter(iter
);
581 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
583 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
586 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
588 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
590 //ust// iter->module = NULL;
591 iter
->tracepoint
= NULL
;
593 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
595 //ust// #ifdef CONFIG_MODULES
597 //ust// int tracepoint_module_notify(struct notifier_block *self,
598 //ust// unsigned long val, void *data)
600 //ust// struct module *mod = data;
602 //ust// switch (val) {
603 //ust// case MODULE_STATE_COMING:
604 //ust// tracepoint_update_probe_range(mod->tracepoints,
605 //ust// mod->tracepoints + mod->num_tracepoints);
607 //ust// case MODULE_STATE_GOING:
608 //ust// tracepoint_update_probe_range(mod->tracepoints,
609 //ust// mod->tracepoints + mod->num_tracepoints);
615 //ust// struct notifier_block tracepoint_module_nb = {
616 //ust// .notifier_call = tracepoint_module_notify,
617 //ust// .priority = 0,
620 //ust// static int init_tracepoints(void)
622 //ust// return register_module_notifier(&tracepoint_module_nb);
624 //ust// __initcall(init_tracepoints);
626 //ust// #endif /* CONFIG_MODULES */
628 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
630 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
632 new_tracepoint_cb
= cb
;
635 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
637 if(new_tracepoint_cb
) {
638 struct tracepoint
*t
;
639 for(t
=start
; t
< end
; t
++) {
640 new_tracepoint_cb(t
);
645 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
647 struct tracepoint_lib
*pl
;
649 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
651 pl
->tracepoints_start
= tracepoints_start
;
652 pl
->tracepoints_count
= tracepoints_count
;
654 /* FIXME: maybe protect this with its own mutex? */
655 pthread_mutex_lock(&tracepoints_mutex
);
656 list_add(&pl
->list
, &libs
);
657 pthread_mutex_unlock(&tracepoints_mutex
);
659 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
661 /* FIXME: update just the loaded lib */
662 lib_update_tracepoints();
664 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
669 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
)
671 struct tracepoint_lib
*lib
;
673 pthread_mutex_lock(&tracepoints_mutex
);
675 list_for_each_entry(lib
, &libs
, list
) {
676 if(lib
->tracepoints_start
== tracepoints_start
) {
677 struct tracepoint_lib
*lib2free
= lib
;
678 list_del(&lib
->list
);
684 pthread_mutex_unlock(&tracepoints_mutex
);