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