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