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.
22 //ust// #include <linux/module.h>
23 //ust// #include <linux/mutex.h>
24 //ust// #include <linux/types.h>
25 //ust// #include <linux/jhash.h>
26 //ust// #include <linux/list.h>
27 //ust// #include <linux/rcupdate.h>
28 //ust// #include <linux/tracepoint.h>
29 //ust// #include <linux/err.h>
30 //ust// #include <linux/slab.h>
31 //ust// #include <linux/immediate.h>
35 #include <ust/kernelcompat.h>
36 #include <ust/tracepoint.h>
43 //extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
44 //extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
46 /* Set to 1 to enable tracepoint debug output */
47 static const int tracepoint_debug
;
49 /* libraries that contain tracepoints (struct tracepoint_lib) */
50 static LIST_HEAD(libs
);
53 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
54 * builtin and module tracepoints and the hash table.
56 static DEFINE_MUTEX(tracepoints_mutex
);
59 * Tracepoint hash table, containing the active tracepoints.
60 * Protected by tracepoints_mutex.
62 #define TRACEPOINT_HASH_BITS 6
63 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
64 static struct hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
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 hlist_node hlist
;
75 int refcount
; /* Number of times armed. 0 if disarmed. */
81 //ust// struct rcu_head rcu;
82 struct list_head list
;
87 static inline void *allocate_probes(int count
)
89 struct tp_probes
*p
= malloc(count
* sizeof(void *)
90 + sizeof(struct tp_probes
));
91 return p
== NULL
? NULL
: p
->probes
;
94 //ust// static void rcu_free_old_probes(struct rcu_head *head)
96 //ust// kfree(container_of(head, struct tp_probes, u.rcu));
99 static inline void release_probes(void *old
)
102 struct tp_probes
*tp_probes
= container_of(old
,
103 struct tp_probes
, probes
[0]);
104 //ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
110 static void debug_print_probes(struct tracepoint_entry
*entry
)
114 if (!tracepoint_debug
|| !entry
->funcs
)
117 for (i
= 0; entry
->funcs
[i
]; i
++)
118 DBG("Probe %d : %p", i
, entry
->funcs
[i
]);
122 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
, void *probe
)
129 debug_print_probes(entry
);
132 /* (N -> N+1), (N != 0, 1) probes */
133 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++)
134 if (old
[nr_probes
] == probe
)
135 return ERR_PTR(-EEXIST
);
137 /* + 2 : one for new probe, one for NULL func */
138 new = allocate_probes(nr_probes
+ 2);
140 return ERR_PTR(-ENOMEM
);
142 memcpy(new, old
, nr_probes
* sizeof(void *));
143 new[nr_probes
] = probe
;
144 new[nr_probes
+ 1] = NULL
;
145 entry
->refcount
= nr_probes
+ 1;
147 debug_print_probes(entry
);
152 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
)
154 int nr_probes
= 0, nr_del
= 0, i
;
160 return ERR_PTR(-ENOENT
);
162 debug_print_probes(entry
);
163 /* (N -> M), (N > 1, M >= 0) probes */
164 for (nr_probes
= 0; old
[nr_probes
]; nr_probes
++) {
165 if ((!probe
|| old
[nr_probes
] == probe
))
169 if (nr_probes
- nr_del
== 0) {
170 /* N -> 0, (N > 1) */
173 debug_print_probes(entry
);
177 /* N -> M, (N > 1, M > 0) */
179 new = allocate_probes(nr_probes
- nr_del
+ 1);
181 return ERR_PTR(-ENOMEM
);
182 for (i
= 0; old
[i
]; i
++)
183 if ((probe
&& old
[i
] != probe
))
185 new[nr_probes
- nr_del
] = 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 hlist_head
*head
;
201 struct hlist_node
*node
;
202 struct tracepoint_entry
*e
;
203 u32 hash
= jhash(name
, strlen(name
), 0);
205 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
206 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 hlist_head
*head
;
220 struct hlist_node
*node
;
221 struct tracepoint_entry
*e
;
222 size_t name_len
= strlen(name
) + 1;
223 u32 hash
= jhash(name
, name_len
-1, 0);
225 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
226 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 kmalloc here to allocate a variable length element. Could
234 * cause some memory fragmentation if overused.
236 e
= malloc(sizeof(struct tracepoint_entry
) + name_len
);
238 return ERR_PTR(-ENOMEM
);
239 memcpy(&e
->name
[0], name
, name_len
);
242 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 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 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 smp_read_barrier_depends()
271 rcu_assign_pointer(elem
->funcs
, (*entry
)->funcs
);
272 elem
->state__imv
= 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
)
283 elem
->state__imv
= 0;
284 rcu_assign_pointer(elem
->funcs
, 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.
294 void tracepoint_update_probe_range(struct tracepoint
*begin
,
295 struct tracepoint
*end
)
297 struct tracepoint
*iter
;
298 struct tracepoint_entry
*mark_entry
;
300 mutex_lock(&tracepoints_mutex
);
301 for (iter
= begin
; iter
< end
; iter
++) {
302 mark_entry
= get_tracepoint(iter
->name
);
304 set_tracepoint(&mark_entry
, iter
,
305 !!mark_entry
->refcount
);
307 disable_tracepoint(iter
);
310 mutex_unlock(&tracepoints_mutex
);
313 static void lib_update_tracepoints(void)
315 struct tracepoint_lib
*lib
;
317 //ust// mutex_lock(&module_mutex);
318 list_for_each_entry(lib
, &libs
, list
)
319 tracepoint_update_probe_range(lib
->tracepoints_start
,
320 lib
->tracepoints_start
+ lib
->tracepoints_count
);
321 //ust// mutex_unlock(&module_mutex);
325 * Update probes, removing the faulty probes.
327 static void tracepoint_update_probes(void)
329 /* Core kernel tracepoints */
330 //ust// tracepoint_update_probe_range(__start___tracepoints,
331 //ust// __stop___tracepoints);
332 /* tracepoints in modules. */
333 lib_update_tracepoints();
334 /* Update immediate values */
336 //ust// module_imv_update();
339 static void *tracepoint_add_probe(const char *name
, void *probe
)
341 struct tracepoint_entry
*entry
;
344 entry
= get_tracepoint(name
);
346 entry
= add_tracepoint(name
);
350 old
= tracepoint_entry_add_probe(entry
, probe
);
351 if (IS_ERR(old
) && !entry
->refcount
)
352 remove_tracepoint(entry
);
357 * tracepoint_probe_register - Connect a probe to a tracepoint
358 * @name: tracepoint name
359 * @probe: probe handler
361 * Returns 0 if ok, error value on error.
362 * The probe address must at least be aligned on the architecture pointer size.
364 int tracepoint_probe_register(const char *name
, void *probe
)
368 mutex_lock(&tracepoints_mutex
);
369 old
= tracepoint_add_probe(name
, probe
);
370 mutex_unlock(&tracepoints_mutex
);
374 tracepoint_update_probes(); /* may update entry */
378 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
380 static void *tracepoint_remove_probe(const char *name
, void *probe
)
382 struct tracepoint_entry
*entry
;
385 entry
= get_tracepoint(name
);
387 return ERR_PTR(-ENOENT
);
388 old
= tracepoint_entry_remove_probe(entry
, probe
);
391 if (!entry
->refcount
)
392 remove_tracepoint(entry
);
397 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
398 * @name: tracepoint name
399 * @probe: probe function pointer
401 * We do not need to call a synchronize_sched to make sure the probes have
402 * finished running before doing a module unload, because the module unload
403 * itself uses stop_machine(), which insures that every preempt disabled section
406 int tracepoint_probe_unregister(const char *name
, void *probe
)
410 mutex_lock(&tracepoints_mutex
);
411 old
= tracepoint_remove_probe(name
, probe
);
412 mutex_unlock(&tracepoints_mutex
);
416 tracepoint_update_probes(); /* may update entry */
420 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
422 static LIST_HEAD(old_probes
);
423 static int need_update
;
425 static void tracepoint_add_old_probes(void *old
)
429 struct tp_probes
*tp_probes
= container_of(old
,
430 struct tp_probes
, probes
[0]);
431 list_add(&tp_probes
->u
.list
, &old_probes
);
436 * tracepoint_probe_register_noupdate - register a probe but not connect
437 * @name: tracepoint name
438 * @probe: probe handler
440 * caller must call tracepoint_probe_update_all()
442 int tracepoint_probe_register_noupdate(const char *name
, void *probe
)
446 mutex_lock(&tracepoints_mutex
);
447 old
= tracepoint_add_probe(name
, probe
);
449 mutex_unlock(&tracepoints_mutex
);
452 tracepoint_add_old_probes(old
);
453 mutex_unlock(&tracepoints_mutex
);
456 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
459 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
460 * @name: tracepoint name
461 * @probe: probe function pointer
463 * caller must call tracepoint_probe_update_all()
465 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
)
469 mutex_lock(&tracepoints_mutex
);
470 old
= tracepoint_remove_probe(name
, probe
);
472 mutex_unlock(&tracepoints_mutex
);
475 tracepoint_add_old_probes(old
);
476 mutex_unlock(&tracepoints_mutex
);
479 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
482 * tracepoint_probe_update_all - update tracepoints
484 void tracepoint_probe_update_all(void)
486 LIST_HEAD(release_probes
);
487 struct tp_probes
*pos
, *next
;
489 mutex_lock(&tracepoints_mutex
);
491 mutex_unlock(&tracepoints_mutex
);
494 if (!list_empty(&old_probes
))
495 list_replace_init(&old_probes
, &release_probes
);
497 mutex_unlock(&tracepoints_mutex
);
499 tracepoint_update_probes();
500 list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
501 list_del(&pos
->u
.list
);
502 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
507 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
510 * Returns 0 if current not found.
511 * Returns 1 if current found.
513 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
515 struct tracepoint_lib
*iter_lib
;
518 //ust// mutex_lock(&module_mutex);
519 list_for_each_entry(iter_lib
, &libs
, list
) {
520 if (iter_lib
< iter
->lib
)
522 else if (iter_lib
> iter
->lib
)
523 iter
->tracepoint
= NULL
;
524 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
525 iter_lib
->tracepoints_start
,
526 iter_lib
->tracepoints_start
+ iter_lib
->tracepoints_count
);
528 iter
->lib
= iter_lib
;
532 //ust// mutex_unlock(&module_mutex);
537 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
538 * @tracepoint: current tracepoints (in), next tracepoint (out)
539 * @begin: beginning of the range
540 * @end: end of the range
542 * Returns whether a next tracepoint has been found (1) or not (0).
543 * Will return the first tracepoint in the range if the input tracepoint is
546 int tracepoint_get_iter_range(struct tracepoint
**tracepoint
,
547 struct tracepoint
*begin
, struct tracepoint
*end
)
549 if (!*tracepoint
&& begin
!= end
) {
553 if (*tracepoint
>= begin
&& *tracepoint
< end
)
557 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
559 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
563 //ust// /* Core kernel tracepoints */
564 //ust// if (!iter->module) {
565 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
566 //ust// __start___tracepoints, __stop___tracepoints);
570 /* tracepoints in libs. */
571 found
= lib_get_iter_tracepoints(iter
);
574 tracepoint_iter_reset(iter
);
577 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
579 tracepoint_get_iter(iter
);
581 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
583 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
587 * iter->tracepoint may be invalid because we blindly incremented it.
588 * Make sure it is valid by marshalling on the tracepoints, getting the
589 * tracepoints from following modules if necessary.
591 tracepoint_get_iter(iter
);
593 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
595 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
598 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
600 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
602 //ust// iter->module = NULL;
603 iter
->tracepoint
= NULL
;
605 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
607 //ust// #ifdef CONFIG_MODULES
609 //ust// int tracepoint_module_notify(struct notifier_block *self,
610 //ust// unsigned long val, void *data)
612 //ust// struct module *mod = data;
614 //ust// switch (val) {
615 //ust// case MODULE_STATE_COMING:
616 //ust// tracepoint_update_probe_range(mod->tracepoints,
617 //ust// mod->tracepoints + mod->num_tracepoints);
619 //ust// case MODULE_STATE_GOING:
620 //ust// tracepoint_update_probe_range(mod->tracepoints,
621 //ust// mod->tracepoints + mod->num_tracepoints);
627 //ust// struct notifier_block tracepoint_module_nb = {
628 //ust// .notifier_call = tracepoint_module_notify,
629 //ust// .priority = 0,
632 //ust// static int init_tracepoints(void)
634 //ust// return register_module_notifier(&tracepoint_module_nb);
636 //ust// __initcall(init_tracepoints);
638 //ust// #endif /* CONFIG_MODULES */
640 static void (*new_tracepoint_cb
)(struct tracepoint
*) = NULL
;
642 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
644 new_tracepoint_cb
= cb
;
647 static void new_tracepoints(struct tracepoint
*start
, struct tracepoint
*end
)
649 if(new_tracepoint_cb
) {
650 struct tracepoint
*t
;
651 for(t
=start
; t
< end
; t
++) {
652 new_tracepoint_cb(t
);
657 int tracepoint_register_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
659 struct tracepoint_lib
*pl
;
661 pl
= (struct tracepoint_lib
*) malloc(sizeof(struct tracepoint_lib
));
663 pl
->tracepoints_start
= tracepoints_start
;
664 pl
->tracepoints_count
= tracepoints_count
;
666 /* FIXME: maybe protect this with its own mutex? */
667 mutex_lock(&tracepoints_mutex
);
668 list_add(&pl
->list
, &libs
);
669 mutex_unlock(&tracepoints_mutex
);
671 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
673 /* FIXME: update just the loaded lib */
674 lib_update_tracepoints();
676 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start
, tracepoints_count
);
681 int tracepoint_unregister_lib(struct tracepoint
*tracepoints_start
, int tracepoints_count
)
683 /*FIXME: implement; but before implementing, tracepoint_register_lib must
684 have appropriate locking. */