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