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