ust: port tracepoints to userspace and add usage in hello.c
[ust.git] / libmarkers / tracepoint.c
CommitLineData
f99be407
PMF
1/*
2 * Copyright (C) 2008 Mathieu Desnoyers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
474d745f
PMF
18//ust// #include <linux/module.h>
19//ust// #include <linux/mutex.h>
20//ust// #include <linux/types.h>
21//ust// #include <linux/jhash.h>
22//ust// #include <linux/list.h>
23//ust// #include <linux/rcupdate.h>
24//ust// #include <linux/tracepoint.h>
25//ust// #include <linux/err.h>
26//ust// #include <linux/slab.h>
27//ust// #include <linux/immediate.h>
28
29#include <errno.h>
30
31#include "kernelcompat.h"
32#include "tracepoint.h"
33#include "usterr.h"
34#include "list.h"
35
36//extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
37//extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
f99be407
PMF
38
39/* Set to 1 to enable tracepoint debug output */
40static const int tracepoint_debug;
41
474d745f
PMF
42/* libraries that contain tracepoints (struct tracepoint_lib) */
43static LIST_HEAD(libs);
44
f99be407
PMF
45/*
46 * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
47 * builtin and module tracepoints and the hash table.
48 */
49static DEFINE_MUTEX(tracepoints_mutex);
50
51/*
52 * Tracepoint hash table, containing the active tracepoints.
53 * Protected by tracepoints_mutex.
54 */
55#define TRACEPOINT_HASH_BITS 6
56#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
57static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
58
59/*
60 * Note about RCU :
61 * It is used to to delay the free of multiple probes array until a quiescent
62 * state is reached.
63 * Tracepoint entries modifications are protected by the tracepoints_mutex.
64 */
65struct tracepoint_entry {
66 struct hlist_node hlist;
67 void **funcs;
68 int refcount; /* Number of times armed. 0 if disarmed. */
69 char name[0];
70};
71
72struct tp_probes {
73 union {
474d745f 74//ust// struct rcu_head rcu;
f99be407
PMF
75 struct list_head list;
76 } u;
77 void *probes[0];
78};
79
80static inline void *allocate_probes(int count)
81{
82 struct tp_probes *p = kmalloc(count * sizeof(void *)
83 + sizeof(struct tp_probes), GFP_KERNEL);
84 return p == NULL ? NULL : p->probes;
85}
86
474d745f
PMF
87//ust// static void rcu_free_old_probes(struct rcu_head *head)
88//ust// {
89//ust// kfree(container_of(head, struct tp_probes, u.rcu));
90//ust// }
f99be407
PMF
91
92static inline void release_probes(void *old)
93{
94 if (old) {
95 struct tp_probes *tp_probes = container_of(old,
96 struct tp_probes, probes[0]);
474d745f
PMF
97//ust// call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
98 synchronize_rcu();
99 kfree(tp_probes);
f99be407
PMF
100 }
101}
102
103static void debug_print_probes(struct tracepoint_entry *entry)
104{
105 int i;
106
107 if (!tracepoint_debug || !entry->funcs)
108 return;
109
110 for (i = 0; entry->funcs[i]; i++)
111 printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
112}
113
114static void *
115tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
116{
117 int nr_probes = 0;
118 void **old, **new;
119
120 WARN_ON(!probe);
121
122 debug_print_probes(entry);
123 old = entry->funcs;
124 if (old) {
125 /* (N -> N+1), (N != 0, 1) probes */
126 for (nr_probes = 0; old[nr_probes]; nr_probes++)
127 if (old[nr_probes] == probe)
128 return ERR_PTR(-EEXIST);
129 }
130 /* + 2 : one for new probe, one for NULL func */
131 new = allocate_probes(nr_probes + 2);
132 if (new == NULL)
133 return ERR_PTR(-ENOMEM);
134 if (old)
135 memcpy(new, old, nr_probes * sizeof(void *));
136 new[nr_probes] = probe;
137 new[nr_probes + 1] = NULL;
138 entry->refcount = nr_probes + 1;
139 entry->funcs = new;
140 debug_print_probes(entry);
141 return old;
142}
143
144static void *
145tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
146{
147 int nr_probes = 0, nr_del = 0, i;
148 void **old, **new;
149
150 old = entry->funcs;
151
152 if (!old)
153 return ERR_PTR(-ENOENT);
154
155 debug_print_probes(entry);
156 /* (N -> M), (N > 1, M >= 0) probes */
157 for (nr_probes = 0; old[nr_probes]; nr_probes++) {
158 if ((!probe || old[nr_probes] == probe))
159 nr_del++;
160 }
161
162 if (nr_probes - nr_del == 0) {
163 /* N -> 0, (N > 1) */
164 entry->funcs = NULL;
165 entry->refcount = 0;
166 debug_print_probes(entry);
167 return old;
168 } else {
169 int j = 0;
170 /* N -> M, (N > 1, M > 0) */
171 /* + 1 for NULL */
172 new = allocate_probes(nr_probes - nr_del + 1);
173 if (new == NULL)
174 return ERR_PTR(-ENOMEM);
175 for (i = 0; old[i]; i++)
176 if ((probe && old[i] != probe))
177 new[j++] = old[i];
178 new[nr_probes - nr_del] = NULL;
179 entry->refcount = nr_probes - nr_del;
180 entry->funcs = new;
181 }
182 debug_print_probes(entry);
183 return old;
184}
185
186/*
187 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
188 * Must be called with tracepoints_mutex held.
189 * Returns NULL if not present.
190 */
191static struct tracepoint_entry *get_tracepoint(const char *name)
192{
193 struct hlist_head *head;
194 struct hlist_node *node;
195 struct tracepoint_entry *e;
196 u32 hash = jhash(name, strlen(name), 0);
197
198 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
199 hlist_for_each_entry(e, node, head, hlist) {
200 if (!strcmp(name, e->name))
201 return e;
202 }
203 return NULL;
204}
205
206/*
207 * Add the tracepoint to the tracepoint hash table. Must be called with
208 * tracepoints_mutex held.
209 */
210static struct tracepoint_entry *add_tracepoint(const char *name)
211{
212 struct hlist_head *head;
213 struct hlist_node *node;
214 struct tracepoint_entry *e;
215 size_t name_len = strlen(name) + 1;
216 u32 hash = jhash(name, name_len-1, 0);
217
218 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
219 hlist_for_each_entry(e, node, head, hlist) {
220 if (!strcmp(name, e->name)) {
221 printk(KERN_NOTICE
222 "tracepoint %s busy\n", name);
223 return ERR_PTR(-EEXIST); /* Already there */
224 }
225 }
226 /*
227 * Using kmalloc here to allocate a variable length element. Could
228 * cause some memory fragmentation if overused.
229 */
230 e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
231 if (!e)
232 return ERR_PTR(-ENOMEM);
233 memcpy(&e->name[0], name, name_len);
234 e->funcs = NULL;
235 e->refcount = 0;
236 hlist_add_head(&e->hlist, head);
237 return e;
238}
239
240/*
241 * Remove the tracepoint from the tracepoint hash table. Must be called with
242 * mutex_lock held.
243 */
244static inline void remove_tracepoint(struct tracepoint_entry *e)
245{
246 hlist_del(&e->hlist);
247 kfree(e);
248}
249
250/*
251 * Sets the probe callback corresponding to one tracepoint.
252 */
253static void set_tracepoint(struct tracepoint_entry **entry,
254 struct tracepoint *elem, int active)
255{
256 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
257
258 /*
259 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
260 * probe callbacks array is consistent before setting a pointer to it.
261 * This array is referenced by __DO_TRACE from
262 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
263 * is used.
264 */
265 rcu_assign_pointer(elem->funcs, (*entry)->funcs);
266 elem->state__imv = active;
267}
268
269/*
270 * Disable a tracepoint and its probe callback.
271 * Note: only waiting an RCU period after setting elem->call to the empty
272 * function insures that the original callback is not used anymore. This insured
273 * by preempt_disable around the call site.
274 */
275static void disable_tracepoint(struct tracepoint *elem)
276{
277 elem->state__imv = 0;
278 rcu_assign_pointer(elem->funcs, NULL);
279}
280
281/**
282 * tracepoint_update_probe_range - Update a probe range
283 * @begin: beginning of the range
284 * @end: end of the range
285 *
286 * Updates the probe callback corresponding to a range of tracepoints.
287 */
288void tracepoint_update_probe_range(struct tracepoint *begin,
289 struct tracepoint *end)
290{
291 struct tracepoint *iter;
292 struct tracepoint_entry *mark_entry;
293
294 mutex_lock(&tracepoints_mutex);
295 for (iter = begin; iter < end; iter++) {
296 mark_entry = get_tracepoint(iter->name);
297 if (mark_entry) {
298 set_tracepoint(&mark_entry, iter,
299 !!mark_entry->refcount);
300 } else {
301 disable_tracepoint(iter);
302 }
303 }
304 mutex_unlock(&tracepoints_mutex);
305}
306
307/*
308 * Update probes, removing the faulty probes.
309 */
310static void tracepoint_update_probes(void)
311{
312 /* Core kernel tracepoints */
474d745f
PMF
313//ust// tracepoint_update_probe_range(__start___tracepoints,
314//ust// __stop___tracepoints);
f99be407 315 /* tracepoints in modules. */
474d745f 316 lib_update_tracepoints();
f99be407
PMF
317 /* Update immediate values */
318 core_imv_update();
474d745f 319//ust// module_imv_update();
f99be407
PMF
320}
321
322static void *tracepoint_add_probe(const char *name, void *probe)
323{
324 struct tracepoint_entry *entry;
325 void *old;
326
327 entry = get_tracepoint(name);
328 if (!entry) {
329 entry = add_tracepoint(name);
330 if (IS_ERR(entry))
331 return entry;
332 }
333 old = tracepoint_entry_add_probe(entry, probe);
334 if (IS_ERR(old) && !entry->refcount)
335 remove_tracepoint(entry);
336 return old;
337}
338
339/**
340 * tracepoint_probe_register - Connect a probe to a tracepoint
341 * @name: tracepoint name
342 * @probe: probe handler
343 *
344 * Returns 0 if ok, error value on error.
345 * The probe address must at least be aligned on the architecture pointer size.
346 */
347int tracepoint_probe_register(const char *name, void *probe)
348{
349 void *old;
350
351 mutex_lock(&tracepoints_mutex);
352 old = tracepoint_add_probe(name, probe);
353 mutex_unlock(&tracepoints_mutex);
354 if (IS_ERR(old))
355 return PTR_ERR(old);
356
357 tracepoint_update_probes(); /* may update entry */
358 release_probes(old);
359 return 0;
360}
474d745f 361//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
f99be407
PMF
362
363static void *tracepoint_remove_probe(const char *name, void *probe)
364{
365 struct tracepoint_entry *entry;
366 void *old;
367
368 entry = get_tracepoint(name);
369 if (!entry)
370 return ERR_PTR(-ENOENT);
371 old = tracepoint_entry_remove_probe(entry, probe);
372 if (IS_ERR(old))
373 return old;
374 if (!entry->refcount)
375 remove_tracepoint(entry);
376 return old;
377}
378
379/**
380 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
381 * @name: tracepoint name
382 * @probe: probe function pointer
383 *
384 * We do not need to call a synchronize_sched to make sure the probes have
385 * finished running before doing a module unload, because the module unload
386 * itself uses stop_machine(), which insures that every preempt disabled section
387 * have finished.
388 */
389int tracepoint_probe_unregister(const char *name, void *probe)
390{
391 void *old;
392
393 mutex_lock(&tracepoints_mutex);
394 old = tracepoint_remove_probe(name, probe);
395 mutex_unlock(&tracepoints_mutex);
396 if (IS_ERR(old))
397 return PTR_ERR(old);
398
399 tracepoint_update_probes(); /* may update entry */
400 release_probes(old);
401 return 0;
402}
474d745f 403//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
f99be407
PMF
404
405static LIST_HEAD(old_probes);
406static int need_update;
407
408static void tracepoint_add_old_probes(void *old)
409{
410 need_update = 1;
411 if (old) {
412 struct tp_probes *tp_probes = container_of(old,
413 struct tp_probes, probes[0]);
414 list_add(&tp_probes->u.list, &old_probes);
415 }
416}
417
418/**
419 * tracepoint_probe_register_noupdate - register a probe but not connect
420 * @name: tracepoint name
421 * @probe: probe handler
422 *
423 * caller must call tracepoint_probe_update_all()
424 */
425int tracepoint_probe_register_noupdate(const char *name, void *probe)
426{
427 void *old;
428
429 mutex_lock(&tracepoints_mutex);
430 old = tracepoint_add_probe(name, probe);
431 if (IS_ERR(old)) {
432 mutex_unlock(&tracepoints_mutex);
433 return PTR_ERR(old);
434 }
435 tracepoint_add_old_probes(old);
436 mutex_unlock(&tracepoints_mutex);
437 return 0;
438}
474d745f 439//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
f99be407
PMF
440
441/**
442 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
443 * @name: tracepoint name
444 * @probe: probe function pointer
445 *
446 * caller must call tracepoint_probe_update_all()
447 */
448int tracepoint_probe_unregister_noupdate(const char *name, void *probe)
449{
450 void *old;
451
452 mutex_lock(&tracepoints_mutex);
453 old = tracepoint_remove_probe(name, probe);
454 if (IS_ERR(old)) {
455 mutex_unlock(&tracepoints_mutex);
456 return PTR_ERR(old);
457 }
458 tracepoint_add_old_probes(old);
459 mutex_unlock(&tracepoints_mutex);
460 return 0;
461}
474d745f 462//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
f99be407
PMF
463
464/**
465 * tracepoint_probe_update_all - update tracepoints
466 */
467void tracepoint_probe_update_all(void)
468{
469 LIST_HEAD(release_probes);
470 struct tp_probes *pos, *next;
471
472 mutex_lock(&tracepoints_mutex);
473 if (!need_update) {
474 mutex_unlock(&tracepoints_mutex);
475 return;
476 }
477 if (!list_empty(&old_probes))
478 list_replace_init(&old_probes, &release_probes);
479 need_update = 0;
480 mutex_unlock(&tracepoints_mutex);
481
482 tracepoint_update_probes();
483 list_for_each_entry_safe(pos, next, &release_probes, u.list) {
484 list_del(&pos->u.list);
474d745f
PMF
485//ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
486 synchronize_rcu();
487 kfree(pos);
f99be407
PMF
488 }
489}
474d745f 490//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
f99be407
PMF
491
492/**
493 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
494 * @tracepoint: current tracepoints (in), next tracepoint (out)
495 * @begin: beginning of the range
496 * @end: end of the range
497 *
498 * Returns whether a next tracepoint has been found (1) or not (0).
499 * Will return the first tracepoint in the range if the input tracepoint is
500 * NULL.
501 */
502int tracepoint_get_iter_range(struct tracepoint **tracepoint,
503 struct tracepoint *begin, struct tracepoint *end)
504{
505 if (!*tracepoint && begin != end) {
506 *tracepoint = begin;
507 return 1;
508 }
509 if (*tracepoint >= begin && *tracepoint < end)
510 return 1;
511 return 0;
512}
474d745f 513//ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
f99be407
PMF
514
515static void tracepoint_get_iter(struct tracepoint_iter *iter)
516{
517 int found = 0;
518
474d745f
PMF
519//ust// /* Core kernel tracepoints */
520//ust// if (!iter->module) {
521//ust// found = tracepoint_get_iter_range(&iter->tracepoint,
522//ust// __start___tracepoints, __stop___tracepoints);
523//ust// if (found)
524//ust// goto end;
525//ust// }
526 /* tracepoints in libs. */
527 found = lib_get_iter_tracepoints(iter);
f99be407
PMF
528end:
529 if (!found)
530 tracepoint_iter_reset(iter);
531}
532
533void tracepoint_iter_start(struct tracepoint_iter *iter)
534{
535 tracepoint_get_iter(iter);
536}
474d745f 537//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
f99be407
PMF
538
539void tracepoint_iter_next(struct tracepoint_iter *iter)
540{
541 iter->tracepoint++;
542 /*
543 * iter->tracepoint may be invalid because we blindly incremented it.
544 * Make sure it is valid by marshalling on the tracepoints, getting the
545 * tracepoints from following modules if necessary.
546 */
547 tracepoint_get_iter(iter);
548}
474d745f 549//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
f99be407
PMF
550
551void tracepoint_iter_stop(struct tracepoint_iter *iter)
552{
553}
474d745f 554//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
f99be407
PMF
555
556void tracepoint_iter_reset(struct tracepoint_iter *iter)
557{
474d745f 558//ust// iter->module = NULL;
f99be407
PMF
559 iter->tracepoint = NULL;
560}
474d745f
PMF
561//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
562
563//ust// #ifdef CONFIG_MODULES
564
565//ust// int tracepoint_module_notify(struct notifier_block *self,
566//ust// unsigned long val, void *data)
567//ust// {
568//ust// struct module *mod = data;
569//ust//
570//ust// switch (val) {
571//ust// case MODULE_STATE_COMING:
572//ust// tracepoint_update_probe_range(mod->tracepoints,
573//ust// mod->tracepoints + mod->num_tracepoints);
574//ust// break;
575//ust// case MODULE_STATE_GOING:
576//ust// tracepoint_update_probe_range(mod->tracepoints,
577//ust// mod->tracepoints + mod->num_tracepoints);
578//ust// break;
579//ust// }
580//ust// return 0;
581//ust// }
582
583//ust// struct notifier_block tracepoint_module_nb = {
584//ust// .notifier_call = tracepoint_module_notify,
585//ust// .priority = 0,
586//ust// };
587
588//ust// static int init_tracepoints(void)
589//ust// {
590//ust// return register_module_notifier(&tracepoint_module_nb);
591//ust// }
592//ust// __initcall(init_tracepoints);
593
594//ust// #endif /* CONFIG_MODULES */
f99be407 595
474d745f
PMF
596/*
597 * Returns 0 if current not found.
598 * Returns 1 if current found.
599 */
600int lib_get_iter_tracepoints(struct tracepoint_iter *iter)
f99be407 601{
474d745f
PMF
602 struct tracepoint_lib *iter_lib;
603 int found = 0;
f99be407 604
474d745f
PMF
605//ust// mutex_lock(&module_mutex);
606 list_for_each_entry(iter_lib, &libs, list) {
607 if (iter_lib < iter->lib)
608 continue;
609 else if (iter_lib > iter->lib)
610 iter->tracepoint = NULL;
611 found = marker_get_iter_range(&iter->tracepoint,
612 iter_lib->tracepoints_start,
613 iter_lib->tracepoints_start + iter_lib->tracepoints_count);
614 if (found) {
615 iter->lib = iter_lib;
616 break;
617 }
f99be407 618 }
474d745f
PMF
619//ust// mutex_unlock(&module_mutex);
620 return found;
f99be407
PMF
621}
622
474d745f
PMF
623void lib_update_tracepoints(void)
624{
625 struct tracepoint_lib *lib;
626
627//ust// mutex_lock(&module_mutex);
628 list_for_each_entry(lib, &libs, list)
629 tracepoint_update_probe_range(lib->tracepoints_start,
630 lib->tracepoints_start + lib->tracepoints_count);
631//ust// mutex_unlock(&module_mutex);
632}
633
634static void (*new_tracepoint_cb)(struct tracepoint *) = NULL;
635
636void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
637{
638 new_tracepoint_cb = cb;
639}
f99be407 640
474d745f 641static void new_tracepoints(struct tracepoint *start, struct tracepoint *end)
f99be407 642{
474d745f
PMF
643 if(new_tracepoint_cb) {
644 struct tracepoint *t;
645 for(t=start; t < end; t++) {
646 new_tracepoint_cb(t);
647 }
648 }
f99be407 649}
f99be407 650
474d745f
PMF
651int tracepoint_register_lib(struct tracepoint *tracepoints_start, int tracepoints_count)
652{
653 struct tracepoint_lib *pl;
654
655 pl = (struct tracepoint_lib *) malloc(sizeof(struct tracepoint_lib));
656
657 pl->tracepoints_start = tracepoints_start;
658 pl->tracepoints_count = tracepoints_count;
659
660 /* FIXME: maybe protect this with its own mutex? */
661 mutex_lock(&tracepoints_mutex);
662 list_add(&pl->list, &libs);
663 mutex_unlock(&tracepoints_mutex);
664
665 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
666
667 /* FIXME: update just the loaded lib */
668 lib_update_tracepoints();
669
670 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start, tracepoints_count);
671
672 return 0;
673}
674
675int tracepoint_unregister_lib(struct tracepoint *tracepoints_start, int tracepoints_count)
676{
677 /*FIXME: implement; but before implementing, tracepoint_register_lib must
678 have appropriate locking. */
679
680 return 0;
681}
This page took 0.046452 seconds and 4 git commands to generate.