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