Add usage reference count for tracepoints
[lttng-ust.git] / liblttng-ust / 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 <stdint.h>
25 #include <stddef.h>
26 #include <stdio.h>
27
28 #include <urcu/arch.h>
29 #include <urcu-bp.h>
30 #include <urcu/hlist.h>
31 #include <urcu/uatomic.h>
32 #include <urcu/compiler.h>
33
34 #include <lttng/tracepoint.h>
35 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
36
37 #include <usterr-signal-safe.h>
38 #include <helper.h>
39
40 #include "tracepoint-internal.h"
41 #include "lttng-tracer-core.h"
42 #include "jhash.h"
43 #include "error.h"
44
45 /* Set to 1 to enable tracepoint debug output */
46 static const int tracepoint_debug;
47 static int initialized;
48 static void (*new_tracepoint_cb)(struct tracepoint *);
49
50 /*
51 * tracepoint_mutex nests inside UST mutex.
52 *
53 * Note about interaction with fork/clone: UST does not hold the
54 * tracepoint mutex across fork/clone because it is either:
55 * - nested within UST mutex, in which case holding the UST mutex across
56 * fork/clone suffice,
57 * - taken by a library constructor, which should never race with a
58 * fork/clone if the application is expected to continue running with
59 * the same memory layout (no following exec()).
60 */
61 static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
62
63 /*
64 * libraries that contain tracepoints (struct tracepoint_lib).
65 * Protected by tracepoint mutex.
66 */
67 static CDS_LIST_HEAD(libs);
68
69 /*
70 * The tracepoint mutex protects the library tracepoints, the hash table, and
71 * the library list.
72 * All calls to the tracepoint API must be protected by the tracepoint mutex,
73 * excepts calls to tracepoint_register_lib and
74 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
75 */
76
77 /*
78 * Tracepoint hash table, containing the active tracepoints.
79 * Protected by tracepoint mutex.
80 */
81 #define TRACEPOINT_HASH_BITS 12
82 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
83 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
84
85 static CDS_LIST_HEAD(old_probes);
86 static int need_update;
87
88 /*
89 * Note about RCU :
90 * It is used to to delay the free of multiple probes array until a quiescent
91 * state is reached.
92 * Tracepoint entries modifications are protected by the tracepoint mutex.
93 */
94 struct tracepoint_entry {
95 struct cds_hlist_node hlist;
96 struct tracepoint_probe *probes;
97 int refcount; /* Number of times armed. 0 if disarmed. */
98 int callsite_refcount; /* how many libs use this tracepoint */
99 const char *signature;
100 char name[0];
101 };
102
103 struct tp_probes {
104 union {
105 struct cds_list_head list;
106 /* Field below only used for call_rcu scheme */
107 /* struct rcu_head head; */
108 } u;
109 struct tracepoint_probe probes[0];
110 };
111
112 /*
113 * Callsite hash table, containing the tracepoint call sites.
114 * Protected by tracepoint mutex.
115 */
116 #define CALLSITE_HASH_BITS 12
117 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
118 static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
119
120 struct callsite_entry {
121 struct cds_hlist_node hlist; /* hash table node */
122 struct cds_list_head node; /* lib list of callsites node */
123 struct tracepoint *tp;
124 };
125
126 /* coverity[+alloc] */
127 static void *allocate_probes(int count)
128 {
129 struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe)
130 + sizeof(struct tp_probes));
131 return p == NULL ? NULL : p->probes;
132 }
133
134 /* coverity[+free : arg-0] */
135 static void release_probes(void *old)
136 {
137 if (old) {
138 struct tp_probes *tp_probes = caa_container_of(old,
139 struct tp_probes, probes[0]);
140 synchronize_rcu();
141 free(tp_probes);
142 }
143 }
144
145 static void debug_print_probes(struct tracepoint_entry *entry)
146 {
147 int i;
148
149 if (!tracepoint_debug || !entry->probes)
150 return;
151
152 for (i = 0; entry->probes[i].func; i++)
153 DBG("Probe %d : %p", i, entry->probes[i].func);
154 }
155
156 static void *
157 tracepoint_entry_add_probe(struct tracepoint_entry *entry,
158 void (*probe)(void), void *data)
159 {
160 int nr_probes = 0;
161 struct tracepoint_probe *old, *new;
162
163 if (!probe) {
164 WARN_ON(1);
165 return ERR_PTR(-EINVAL);
166 }
167 debug_print_probes(entry);
168 old = entry->probes;
169 if (old) {
170 /* (N -> N+1), (N != 0, 1) probes */
171 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
172 if (old[nr_probes].func == probe &&
173 old[nr_probes].data == data)
174 return ERR_PTR(-EEXIST);
175 }
176 /* + 2 : one for new probe, one for NULL func */
177 new = allocate_probes(nr_probes + 2);
178 if (new == NULL)
179 return ERR_PTR(-ENOMEM);
180 if (old)
181 memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe));
182 new[nr_probes].func = probe;
183 new[nr_probes].data = data;
184 new[nr_probes + 1].func = NULL;
185 entry->refcount = nr_probes + 1;
186 entry->probes = new;
187 debug_print_probes(entry);
188 return old;
189 }
190
191 static void *
192 tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
193 void (*probe)(void), void *data)
194 {
195 int nr_probes = 0, nr_del = 0, i;
196 struct tracepoint_probe *old, *new;
197
198 old = entry->probes;
199
200 if (!old)
201 return ERR_PTR(-ENOENT);
202
203 debug_print_probes(entry);
204 /* (N -> M), (N > 1, M >= 0) probes */
205 if (probe) {
206 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
207 if (old[nr_probes].func == probe &&
208 old[nr_probes].data == data)
209 nr_del++;
210 }
211 }
212
213 if (nr_probes - nr_del == 0) {
214 /* N -> 0, (N > 1) */
215 entry->probes = NULL;
216 entry->refcount = 0;
217 debug_print_probes(entry);
218 return old;
219 } else {
220 int j = 0;
221 /* N -> M, (N > 1, M > 0) */
222 /* + 1 for NULL */
223 new = allocate_probes(nr_probes - nr_del + 1);
224 if (new == NULL)
225 return ERR_PTR(-ENOMEM);
226 for (i = 0; old[i].func; i++)
227 if (old[i].func != probe || old[i].data != data)
228 new[j++] = old[i];
229 new[nr_probes - nr_del].func = NULL;
230 entry->refcount = nr_probes - nr_del;
231 entry->probes = new;
232 }
233 debug_print_probes(entry);
234 return old;
235 }
236
237 /*
238 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
239 * Must be called with tracepoint mutex held.
240 * Returns NULL if not present.
241 */
242 static struct tracepoint_entry *get_tracepoint(const char *name)
243 {
244 struct cds_hlist_head *head;
245 struct cds_hlist_node *node;
246 struct tracepoint_entry *e;
247 size_t name_len = strlen(name);
248 uint32_t hash;
249
250 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
251 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
252 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
253 }
254 hash = jhash(name, name_len, 0);
255 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
256 cds_hlist_for_each_entry(e, node, head, hlist) {
257 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
258 return e;
259 }
260 return NULL;
261 }
262
263 /*
264 * Add the tracepoint to the tracepoint hash table. Must be called with
265 * tracepoint mutex held.
266 */
267 static struct tracepoint_entry *add_tracepoint(const char *name,
268 const char *signature)
269 {
270 struct cds_hlist_head *head;
271 struct cds_hlist_node *node;
272 struct tracepoint_entry *e;
273 size_t name_len = strlen(name);
274 uint32_t hash;
275
276 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
277 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
278 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
279 }
280 hash = jhash(name, name_len, 0);
281 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
282 cds_hlist_for_each_entry(e, node, head, hlist) {
283 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
284 DBG("tracepoint %s busy", name);
285 return ERR_PTR(-EEXIST); /* Already there */
286 }
287 }
288 /*
289 * Using zmalloc here to allocate a variable length element. Could
290 * cause some memory fragmentation if overused.
291 */
292 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
293 if (!e)
294 return ERR_PTR(-ENOMEM);
295 memcpy(&e->name[0], name, name_len + 1);
296 e->name[name_len] = '\0';
297 e->probes = NULL;
298 e->refcount = 0;
299 e->callsite_refcount = 0;
300 e->signature = signature;
301 cds_hlist_add_head(&e->hlist, head);
302 return e;
303 }
304
305 /*
306 * Remove the tracepoint from the tracepoint hash table. Must be called with
307 * tracepoint mutex held.
308 */
309 static void remove_tracepoint(struct tracepoint_entry *e)
310 {
311 cds_hlist_del(&e->hlist);
312 free(e);
313 }
314
315 /*
316 * Sets the probe callback corresponding to one tracepoint.
317 */
318 static void set_tracepoint(struct tracepoint_entry **entry,
319 struct tracepoint *elem, int active)
320 {
321 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
322 /*
323 * Check that signatures match before connecting a probe to a
324 * tracepoint. Warn the user if they don't.
325 */
326 if (strcmp(elem->signature, (*entry)->signature) != 0) {
327 static int warned = 0;
328
329 /* Only print once, don't flood console. */
330 if (!warned) {
331 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
332 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
333 elem->name, elem->signature, (*entry)->signature);
334 warned = 1;
335 }
336 /* Don't accept connecting non-matching signatures. */
337 return;
338 }
339
340 /*
341 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
342 * probe callbacks array is consistent before setting a pointer to it.
343 * This array is referenced by __DO_TRACE from
344 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
345 * is used.
346 */
347 rcu_assign_pointer(elem->probes, (*entry)->probes);
348 elem->state = active;
349 }
350
351 /*
352 * Disable a tracepoint and its probe callback.
353 * Note: only waiting an RCU period after setting elem->call to the empty
354 * function insures that the original callback is not used anymore. This insured
355 * by preempt_disable around the call site.
356 */
357 static void disable_tracepoint(struct tracepoint *elem)
358 {
359 elem->state = 0;
360 rcu_assign_pointer(elem->probes, NULL);
361 }
362
363 /*
364 * Add the callsite to the callsite hash table. Must be called with
365 * tracepoint mutex held.
366 */
367 static void add_callsite(struct tracepoint_lib * lib, struct tracepoint *tp)
368 {
369 struct cds_hlist_head *head;
370 struct callsite_entry *e;
371 const char *name = tp->name;
372 size_t name_len = strlen(name);
373 uint32_t hash;
374 struct tracepoint_entry *tp_entry;
375
376 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
377 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
378 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
379 }
380 hash = jhash(name, name_len, 0);
381 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
382 e = zmalloc(sizeof(struct callsite_entry));
383 assert(e);
384 cds_hlist_add_head(&e->hlist, head);
385 e->tp = tp;
386 cds_list_add(&e->node, &lib->callsites);
387
388 tp_entry = get_tracepoint(name);
389 if (!tp_entry)
390 return;
391 tp_entry->callsite_refcount++;
392 }
393
394 /*
395 * Remove the callsite from the callsite hash table and from lib
396 * callsite list. Must be called with tracepoint mutex held.
397 */
398 static void remove_callsite(struct callsite_entry *e)
399 {
400 struct tracepoint_entry *tp_entry;
401
402 tp_entry = get_tracepoint(e->tp->name);
403 if (tp_entry) {
404 tp_entry->callsite_refcount--;
405 if (tp_entry->callsite_refcount == 0)
406 disable_tracepoint(e->tp);
407 }
408 cds_hlist_del(&e->hlist);
409 cds_list_del(&e->node);
410 free(e);
411 }
412
413 /*
414 * Enable/disable all callsites based on the state of a specific
415 * tracepoint entry.
416 * Must be called with tracepoint mutex held.
417 */
418 static void tracepoint_sync_callsites(const char *name)
419 {
420 struct cds_hlist_head *head;
421 struct cds_hlist_node *node;
422 struct callsite_entry *e;
423 size_t name_len = strlen(name);
424 uint32_t hash;
425 struct tracepoint_entry *tp_entry;
426
427 tp_entry = get_tracepoint(name);
428 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
429 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
430 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
431 }
432 hash = jhash(name, name_len, 0);
433 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
434 cds_hlist_for_each_entry(e, node, head, hlist) {
435 struct tracepoint *tp = e->tp;
436
437 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
438 continue;
439 if (tp_entry) {
440 set_tracepoint(&tp_entry, tp,
441 !!tp_entry->refcount);
442 } else {
443 disable_tracepoint(tp);
444 }
445 }
446 }
447
448 /**
449 * tracepoint_update_probe_range - Update a probe range
450 * @begin: beginning of the range
451 * @end: end of the range
452 *
453 * Updates the probe callback corresponding to a range of tracepoints.
454 */
455 static
456 void tracepoint_update_probe_range(struct tracepoint * const *begin,
457 struct tracepoint * const *end)
458 {
459 struct tracepoint * const *iter;
460 struct tracepoint_entry *mark_entry;
461
462 for (iter = begin; iter < end; iter++) {
463 if (!*iter)
464 continue; /* skip dummy */
465 if (!(*iter)->name) {
466 disable_tracepoint(*iter);
467 continue;
468 }
469 mark_entry = get_tracepoint((*iter)->name);
470 if (mark_entry) {
471 set_tracepoint(&mark_entry, *iter,
472 !!mark_entry->refcount);
473 } else {
474 disable_tracepoint(*iter);
475 }
476 }
477 }
478
479 static void lib_update_tracepoints(struct tracepoint_lib *lib)
480 {
481 tracepoint_update_probe_range(lib->tracepoints_start,
482 lib->tracepoints_start + lib->tracepoints_count);
483 }
484
485 static void lib_register_callsites(struct tracepoint_lib *lib)
486 {
487 struct tracepoint * const *begin;
488 struct tracepoint * const *end;
489 struct tracepoint * const *iter;
490
491 begin = lib->tracepoints_start;
492 end = lib->tracepoints_start + lib->tracepoints_count;
493
494 for (iter = begin; iter < end; iter++) {
495 if (!*iter)
496 continue; /* skip dummy */
497 if (!(*iter)->name) {
498 continue;
499 }
500 add_callsite(lib, *iter);
501 }
502 }
503
504 static void lib_unregister_callsites(struct tracepoint_lib *lib)
505 {
506 struct callsite_entry *callsite, *tmp;
507
508 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
509 remove_callsite(callsite);
510 }
511
512 /*
513 * Update probes, removing the faulty probes.
514 */
515 static void tracepoint_update_probes(void)
516 {
517 struct tracepoint_lib *lib;
518
519 /* tracepoints registered from libraries and executable. */
520 cds_list_for_each_entry(lib, &libs, list)
521 lib_update_tracepoints(lib);
522 }
523
524 static struct tracepoint_probe *
525 tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
526 const char *signature)
527 {
528 struct tracepoint_entry *entry;
529 struct tracepoint_probe *old;
530
531 entry = get_tracepoint(name);
532 if (!entry) {
533 entry = add_tracepoint(name, signature);
534 if (IS_ERR(entry))
535 return (struct tracepoint_probe *)entry;
536 }
537 old = tracepoint_entry_add_probe(entry, probe, data);
538 if (IS_ERR(old) && !entry->refcount)
539 remove_tracepoint(entry);
540 return old;
541 }
542
543 /**
544 * __tracepoint_probe_register - Connect a probe to a tracepoint
545 * @name: tracepoint name
546 * @probe: probe handler
547 *
548 * Returns 0 if ok, error value on error.
549 * The probe address must at least be aligned on the architecture pointer size.
550 * Called with the tracepoint mutex held.
551 */
552 int __tracepoint_probe_register(const char *name, void (*probe)(void),
553 void *data, const char *signature)
554 {
555 void *old;
556 int ret = 0;
557
558 DBG("Registering probe to tracepoint %s", name);
559
560 pthread_mutex_lock(&tracepoint_mutex);
561 old = tracepoint_add_probe(name, probe, data, signature);
562 if (IS_ERR(old)) {
563 ret = PTR_ERR(old);
564 goto end;
565 }
566
567 tracepoint_sync_callsites(name);
568 release_probes(old);
569 end:
570 pthread_mutex_unlock(&tracepoint_mutex);
571 return ret;
572 }
573
574 static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
575 void *data)
576 {
577 struct tracepoint_entry *entry;
578 void *old;
579
580 entry = get_tracepoint(name);
581 if (!entry)
582 return ERR_PTR(-ENOENT);
583 old = tracepoint_entry_remove_probe(entry, probe, data);
584 if (IS_ERR(old))
585 return old;
586 if (!entry->refcount)
587 remove_tracepoint(entry);
588 return old;
589 }
590
591 /**
592 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
593 * @name: tracepoint name
594 * @probe: probe function pointer
595 * @probe: probe data pointer
596 */
597 int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
598 void *data)
599 {
600 void *old;
601 int ret = 0;
602
603 DBG("Un-registering probe from tracepoint %s", name);
604
605 pthread_mutex_lock(&tracepoint_mutex);
606 old = tracepoint_remove_probe(name, probe, data);
607 if (IS_ERR(old)) {
608 ret = PTR_ERR(old);
609 goto end;
610 }
611 tracepoint_sync_callsites(name);
612 release_probes(old);
613 end:
614 pthread_mutex_unlock(&tracepoint_mutex);
615 return ret;
616 }
617
618 static void tracepoint_add_old_probes(void *old)
619 {
620 need_update = 1;
621 if (old) {
622 struct tp_probes *tp_probes = caa_container_of(old,
623 struct tp_probes, probes[0]);
624 cds_list_add(&tp_probes->u.list, &old_probes);
625 }
626 }
627
628 /**
629 * tracepoint_probe_register_noupdate - register a probe but not connect
630 * @name: tracepoint name
631 * @probe: probe handler
632 *
633 * caller must call tracepoint_probe_update_all()
634 */
635 int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
636 void *data, const char *signature)
637 {
638 void *old;
639 int ret = 0;
640
641 pthread_mutex_lock(&tracepoint_mutex);
642 old = tracepoint_add_probe(name, probe, data, signature);
643 if (IS_ERR(old)) {
644 ret = PTR_ERR(old);
645 goto end;
646 }
647 tracepoint_add_old_probes(old);
648 end:
649 pthread_mutex_unlock(&tracepoint_mutex);
650 return ret;
651 }
652
653 /**
654 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
655 * @name: tracepoint name
656 * @probe: probe function pointer
657 *
658 * caller must call tracepoint_probe_update_all()
659 * Called with the tracepoint mutex held.
660 */
661 int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
662 void *data)
663 {
664 void *old;
665 int ret = 0;
666
667 DBG("Un-registering probe from tracepoint %s", name);
668
669 pthread_mutex_lock(&tracepoint_mutex);
670 old = tracepoint_remove_probe(name, probe, data);
671 if (IS_ERR(old)) {
672 ret = PTR_ERR(old);
673 goto end;
674 }
675 tracepoint_add_old_probes(old);
676 end:
677 pthread_mutex_unlock(&tracepoint_mutex);
678 return ret;
679 }
680
681 /**
682 * tracepoint_probe_update_all - update tracepoints
683 */
684 void tracepoint_probe_update_all(void)
685 {
686 CDS_LIST_HEAD(release_probes);
687 struct tp_probes *pos, *next;
688
689 pthread_mutex_lock(&tracepoint_mutex);
690 if (!need_update) {
691 goto end;
692 }
693 if (!cds_list_empty(&old_probes))
694 cds_list_replace_init(&old_probes, &release_probes);
695 need_update = 0;
696
697 tracepoint_update_probes();
698 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
699 cds_list_del(&pos->u.list);
700 synchronize_rcu();
701 free(pos);
702 }
703 end:
704 pthread_mutex_unlock(&tracepoint_mutex);
705 }
706
707 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
708 {
709 new_tracepoint_cb = cb;
710 }
711
712 static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
713 {
714 if (new_tracepoint_cb) {
715 struct tracepoint * const *t;
716
717 for (t = start; t < end; t++) {
718 if (*t)
719 new_tracepoint_cb(*t);
720 }
721 }
722 }
723
724 int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
725 int tracepoints_count)
726 {
727 struct tracepoint_lib *pl, *iter;
728
729 init_tracepoint();
730
731 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
732
733 pl->tracepoints_start = tracepoints_start;
734 pl->tracepoints_count = tracepoints_count;
735 CDS_INIT_LIST_HEAD(&pl->callsites);
736
737 pthread_mutex_lock(&tracepoint_mutex);
738 /*
739 * We sort the libs by struct lib pointer address.
740 */
741 cds_list_for_each_entry_reverse(iter, &libs, list) {
742 BUG_ON(iter == pl); /* Should never be in the list twice */
743 if (iter < pl) {
744 /* We belong to the location right after iter. */
745 cds_list_add(&pl->list, &iter->list);
746 goto lib_added;
747 }
748 }
749 /* We should be added at the head of the list */
750 cds_list_add(&pl->list, &libs);
751 lib_added:
752 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
753 lib_register_callsites(pl);
754 lib_update_tracepoints(pl);
755 pthread_mutex_unlock(&tracepoint_mutex);
756
757 DBG("just registered a tracepoints section from %p and having %d tracepoints",
758 tracepoints_start, tracepoints_count);
759 if (ust_debug()) {
760 int i;
761
762 for (i = 0; i < tracepoints_count; i++) {
763 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
764 }
765 }
766
767 return 0;
768 }
769
770 int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
771 {
772 struct tracepoint_lib *lib;
773
774 pthread_mutex_lock(&tracepoint_mutex);
775 cds_list_for_each_entry(lib, &libs, list) {
776 if (lib->tracepoints_start != tracepoints_start)
777 continue;
778
779 cds_list_del(&lib->list);
780 /*
781 * Unregistering a callsite also decreases the
782 * callsite reference count of the corresponding
783 * tracepoint, and disables the tracepoint if
784 * the reference count drops to zero.
785 */
786 lib_unregister_callsites(lib);
787 DBG("just unregistered a tracepoints section from %p",
788 lib->tracepoints_start);
789 free(lib);
790 break;
791 }
792 pthread_mutex_unlock(&tracepoint_mutex);
793 return 0;
794 }
795
796 void init_tracepoint(void)
797 {
798 if (uatomic_xchg(&initialized, 1) == 1)
799 return;
800 init_usterr();
801 }
802
803 void exit_tracepoint(void)
804 {
805 initialized = 0;
806 }
807
808 /*
809 * Create the wrapper symbols.
810 */
811 #undef tp_rcu_read_lock_bp
812 #undef tp_rcu_read_unlock_bp
813 #undef tp_rcu_dereference_bp
814
815 void tp_rcu_read_lock_bp(void)
816 {
817 rcu_read_lock_bp();
818 }
819
820 void tp_rcu_read_unlock_bp(void)
821 {
822 rcu_read_unlock_bp();
823 }
824
825 void *tp_rcu_dereference_sym_bp(void *p)
826 {
827 return rcu_dereference_bp(p);
828 }
This page took 0.046042 seconds and 4 git commands to generate.