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