fix gdb integration bug
[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 printk(KERN_DEBUG "Probe %d : %p\n", 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 printk(KERN_NOTICE
229 "tracepoint %s busy\n", name);
230 return ERR_PTR(-EEXIST); /* Already there */
231 }
232 }
233 /*
234 * Using kmalloc here to allocate a variable length element. Could
235 * cause some memory fragmentation if overused.
236 */
237 e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
238 if (!e)
239 return ERR_PTR(-ENOMEM);
240 memcpy(&e->name[0], name, name_len);
241 e->funcs = NULL;
242 e->refcount = 0;
243 hlist_add_head(&e->hlist, head);
244 return e;
245 }
246
247 /*
248 * Remove the tracepoint from the tracepoint hash table. Must be called with
249 * mutex_lock held.
250 */
251 static inline void remove_tracepoint(struct tracepoint_entry *e)
252 {
253 hlist_del(&e->hlist);
254 kfree(e);
255 }
256
257 /*
258 * Sets the probe callback corresponding to one tracepoint.
259 */
260 static void set_tracepoint(struct tracepoint_entry **entry,
261 struct tracepoint *elem, int active)
262 {
263 WARN_ON(strcmp((*entry)->name, elem->name) != 0);
264
265 /*
266 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
267 * probe callbacks array is consistent before setting a pointer to it.
268 * This array is referenced by __DO_TRACE from
269 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
270 * is used.
271 */
272 rcu_assign_pointer(elem->funcs, (*entry)->funcs);
273 elem->state__imv = active;
274 }
275
276 /*
277 * Disable a tracepoint and its probe callback.
278 * Note: only waiting an RCU period after setting elem->call to the empty
279 * function insures that the original callback is not used anymore. This insured
280 * by preempt_disable around the call site.
281 */
282 static void disable_tracepoint(struct tracepoint *elem)
283 {
284 elem->state__imv = 0;
285 rcu_assign_pointer(elem->funcs, NULL);
286 }
287
288 /**
289 * tracepoint_update_probe_range - Update a probe range
290 * @begin: beginning of the range
291 * @end: end of the range
292 *
293 * Updates the probe callback corresponding to a range of tracepoints.
294 */
295 void tracepoint_update_probe_range(struct tracepoint *begin,
296 struct tracepoint *end)
297 {
298 struct tracepoint *iter;
299 struct tracepoint_entry *mark_entry;
300
301 mutex_lock(&tracepoints_mutex);
302 for (iter = begin; iter < end; iter++) {
303 mark_entry = get_tracepoint(iter->name);
304 if (mark_entry) {
305 set_tracepoint(&mark_entry, iter,
306 !!mark_entry->refcount);
307 } else {
308 disable_tracepoint(iter);
309 }
310 }
311 mutex_unlock(&tracepoints_mutex);
312 }
313
314 static void lib_update_tracepoints(void)
315 {
316 struct tracepoint_lib *lib;
317
318 //ust// mutex_lock(&module_mutex);
319 list_for_each_entry(lib, &libs, list)
320 tracepoint_update_probe_range(lib->tracepoints_start,
321 lib->tracepoints_start + lib->tracepoints_count);
322 //ust// mutex_unlock(&module_mutex);
323 }
324
325 /*
326 * Update probes, removing the faulty probes.
327 */
328 static void tracepoint_update_probes(void)
329 {
330 /* Core kernel tracepoints */
331 //ust// tracepoint_update_probe_range(__start___tracepoints,
332 //ust// __stop___tracepoints);
333 /* tracepoints in modules. */
334 lib_update_tracepoints();
335 /* Update immediate values */
336 core_imv_update();
337 //ust// module_imv_update();
338 }
339
340 static void *tracepoint_add_probe(const char *name, void *probe)
341 {
342 struct tracepoint_entry *entry;
343 void *old;
344
345 entry = get_tracepoint(name);
346 if (!entry) {
347 entry = add_tracepoint(name);
348 if (IS_ERR(entry))
349 return entry;
350 }
351 old = tracepoint_entry_add_probe(entry, probe);
352 if (IS_ERR(old) && !entry->refcount)
353 remove_tracepoint(entry);
354 return old;
355 }
356
357 /**
358 * tracepoint_probe_register - Connect a probe to a tracepoint
359 * @name: tracepoint name
360 * @probe: probe handler
361 *
362 * Returns 0 if ok, error value on error.
363 * The probe address must at least be aligned on the architecture pointer size.
364 */
365 int tracepoint_probe_register(const char *name, void *probe)
366 {
367 void *old;
368
369 mutex_lock(&tracepoints_mutex);
370 old = tracepoint_add_probe(name, probe);
371 mutex_unlock(&tracepoints_mutex);
372 if (IS_ERR(old))
373 return PTR_ERR(old);
374
375 tracepoint_update_probes(); /* may update entry */
376 release_probes(old);
377 return 0;
378 }
379 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
380
381 static void *tracepoint_remove_probe(const char *name, void *probe)
382 {
383 struct tracepoint_entry *entry;
384 void *old;
385
386 entry = get_tracepoint(name);
387 if (!entry)
388 return ERR_PTR(-ENOENT);
389 old = tracepoint_entry_remove_probe(entry, probe);
390 if (IS_ERR(old))
391 return old;
392 if (!entry->refcount)
393 remove_tracepoint(entry);
394 return old;
395 }
396
397 /**
398 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
399 * @name: tracepoint name
400 * @probe: probe function pointer
401 *
402 * We do not need to call a synchronize_sched to make sure the probes have
403 * finished running before doing a module unload, because the module unload
404 * itself uses stop_machine(), which insures that every preempt disabled section
405 * have finished.
406 */
407 int tracepoint_probe_unregister(const char *name, void *probe)
408 {
409 void *old;
410
411 mutex_lock(&tracepoints_mutex);
412 old = tracepoint_remove_probe(name, probe);
413 mutex_unlock(&tracepoints_mutex);
414 if (IS_ERR(old))
415 return PTR_ERR(old);
416
417 tracepoint_update_probes(); /* may update entry */
418 release_probes(old);
419 return 0;
420 }
421 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
422
423 static LIST_HEAD(old_probes);
424 static int need_update;
425
426 static void tracepoint_add_old_probes(void *old)
427 {
428 need_update = 1;
429 if (old) {
430 struct tp_probes *tp_probes = container_of(old,
431 struct tp_probes, probes[0]);
432 list_add(&tp_probes->u.list, &old_probes);
433 }
434 }
435
436 /**
437 * tracepoint_probe_register_noupdate - register a probe but not connect
438 * @name: tracepoint name
439 * @probe: probe handler
440 *
441 * caller must call tracepoint_probe_update_all()
442 */
443 int tracepoint_probe_register_noupdate(const char *name, void *probe)
444 {
445 void *old;
446
447 mutex_lock(&tracepoints_mutex);
448 old = tracepoint_add_probe(name, probe);
449 if (IS_ERR(old)) {
450 mutex_unlock(&tracepoints_mutex);
451 return PTR_ERR(old);
452 }
453 tracepoint_add_old_probes(old);
454 mutex_unlock(&tracepoints_mutex);
455 return 0;
456 }
457 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
458
459 /**
460 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
461 * @name: tracepoint name
462 * @probe: probe function pointer
463 *
464 * caller must call tracepoint_probe_update_all()
465 */
466 int tracepoint_probe_unregister_noupdate(const char *name, void *probe)
467 {
468 void *old;
469
470 mutex_lock(&tracepoints_mutex);
471 old = tracepoint_remove_probe(name, probe);
472 if (IS_ERR(old)) {
473 mutex_unlock(&tracepoints_mutex);
474 return PTR_ERR(old);
475 }
476 tracepoint_add_old_probes(old);
477 mutex_unlock(&tracepoints_mutex);
478 return 0;
479 }
480 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
481
482 /**
483 * tracepoint_probe_update_all - update tracepoints
484 */
485 void tracepoint_probe_update_all(void)
486 {
487 LIST_HEAD(release_probes);
488 struct tp_probes *pos, *next;
489
490 mutex_lock(&tracepoints_mutex);
491 if (!need_update) {
492 mutex_unlock(&tracepoints_mutex);
493 return;
494 }
495 if (!list_empty(&old_probes))
496 list_replace_init(&old_probes, &release_probes);
497 need_update = 0;
498 mutex_unlock(&tracepoints_mutex);
499
500 tracepoint_update_probes();
501 list_for_each_entry_safe(pos, next, &release_probes, u.list) {
502 list_del(&pos->u.list);
503 //ust// call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
504 synchronize_rcu();
505 kfree(pos);
506 }
507 }
508 //ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
509
510 /*
511 * Returns 0 if current not found.
512 * Returns 1 if current found.
513 */
514 int lib_get_iter_tracepoints(struct tracepoint_iter *iter)
515 {
516 struct tracepoint_lib *iter_lib;
517 int found = 0;
518
519 //ust// mutex_lock(&module_mutex);
520 list_for_each_entry(iter_lib, &libs, list) {
521 if (iter_lib < iter->lib)
522 continue;
523 else if (iter_lib > iter->lib)
524 iter->tracepoint = NULL;
525 found = tracepoint_get_iter_range(&iter->tracepoint,
526 iter_lib->tracepoints_start,
527 iter_lib->tracepoints_start + iter_lib->tracepoints_count);
528 if (found) {
529 iter->lib = iter_lib;
530 break;
531 }
532 }
533 //ust// mutex_unlock(&module_mutex);
534 return found;
535 }
536
537 /**
538 * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
539 * @tracepoint: current tracepoints (in), next tracepoint (out)
540 * @begin: beginning of the range
541 * @end: end of the range
542 *
543 * Returns whether a next tracepoint has been found (1) or not (0).
544 * Will return the first tracepoint in the range if the input tracepoint is
545 * NULL.
546 */
547 int tracepoint_get_iter_range(struct tracepoint **tracepoint,
548 struct tracepoint *begin, struct tracepoint *end)
549 {
550 if (!*tracepoint && begin != end) {
551 *tracepoint = begin;
552 return 1;
553 }
554 if (*tracepoint >= begin && *tracepoint < end)
555 return 1;
556 return 0;
557 }
558 //ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
559
560 static void tracepoint_get_iter(struct tracepoint_iter *iter)
561 {
562 int found = 0;
563
564 //ust// /* Core kernel tracepoints */
565 //ust// if (!iter->module) {
566 //ust// found = tracepoint_get_iter_range(&iter->tracepoint,
567 //ust// __start___tracepoints, __stop___tracepoints);
568 //ust// if (found)
569 //ust// goto end;
570 //ust// }
571 /* tracepoints in libs. */
572 found = lib_get_iter_tracepoints(iter);
573 //ust// end:
574 if (!found)
575 tracepoint_iter_reset(iter);
576 }
577
578 void tracepoint_iter_start(struct tracepoint_iter *iter)
579 {
580 tracepoint_get_iter(iter);
581 }
582 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
583
584 void tracepoint_iter_next(struct tracepoint_iter *iter)
585 {
586 iter->tracepoint++;
587 /*
588 * iter->tracepoint may be invalid because we blindly incremented it.
589 * Make sure it is valid by marshalling on the tracepoints, getting the
590 * tracepoints from following modules if necessary.
591 */
592 tracepoint_get_iter(iter);
593 }
594 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
595
596 void tracepoint_iter_stop(struct tracepoint_iter *iter)
597 {
598 }
599 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
600
601 void tracepoint_iter_reset(struct tracepoint_iter *iter)
602 {
603 //ust// iter->module = NULL;
604 iter->tracepoint = NULL;
605 }
606 //ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
607
608 //ust// #ifdef CONFIG_MODULES
609
610 //ust// int tracepoint_module_notify(struct notifier_block *self,
611 //ust// unsigned long val, void *data)
612 //ust// {
613 //ust// struct module *mod = data;
614 //ust//
615 //ust// switch (val) {
616 //ust// case MODULE_STATE_COMING:
617 //ust// tracepoint_update_probe_range(mod->tracepoints,
618 //ust// mod->tracepoints + mod->num_tracepoints);
619 //ust// break;
620 //ust// case MODULE_STATE_GOING:
621 //ust// tracepoint_update_probe_range(mod->tracepoints,
622 //ust// mod->tracepoints + mod->num_tracepoints);
623 //ust// break;
624 //ust// }
625 //ust// return 0;
626 //ust// }
627
628 //ust// struct notifier_block tracepoint_module_nb = {
629 //ust// .notifier_call = tracepoint_module_notify,
630 //ust// .priority = 0,
631 //ust// };
632
633 //ust// static int init_tracepoints(void)
634 //ust// {
635 //ust// return register_module_notifier(&tracepoint_module_nb);
636 //ust// }
637 //ust// __initcall(init_tracepoints);
638
639 //ust// #endif /* CONFIG_MODULES */
640
641 static void (*new_tracepoint_cb)(struct tracepoint *) = NULL;
642
643 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
644 {
645 new_tracepoint_cb = cb;
646 }
647
648 static void new_tracepoints(struct tracepoint *start, struct tracepoint *end)
649 {
650 if(new_tracepoint_cb) {
651 struct tracepoint *t;
652 for(t=start; t < end; t++) {
653 new_tracepoint_cb(t);
654 }
655 }
656 }
657
658 int tracepoint_register_lib(struct tracepoint *tracepoints_start, int tracepoints_count)
659 {
660 struct tracepoint_lib *pl;
661
662 pl = (struct tracepoint_lib *) malloc(sizeof(struct tracepoint_lib));
663
664 pl->tracepoints_start = tracepoints_start;
665 pl->tracepoints_count = tracepoints_count;
666
667 /* FIXME: maybe protect this with its own mutex? */
668 mutex_lock(&tracepoints_mutex);
669 list_add(&pl->list, &libs);
670 mutex_unlock(&tracepoints_mutex);
671
672 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
673
674 /* FIXME: update just the loaded lib */
675 lib_update_tracepoints();
676
677 DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start, tracepoints_count);
678
679 return 0;
680 }
681
682 int tracepoint_unregister_lib(struct tracepoint *tracepoints_start, int tracepoints_count)
683 {
684 /*FIXME: implement; but before implementing, tracepoint_register_lib must
685 have appropriate locking. */
686
687 return 0;
688 }
This page took 0.057472 seconds and 4 git commands to generate.