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