e43c1aeb312967aefaec9e52757dad447fb7034e
[lttng-ust.git] / src / lib / lttng-ust-tracepoint / tracepoint.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2008-2011 Mathieu Desnoyers
5 * Copyright (C) 2009 Pierre-Marc Fournier
6 *
7 * Ported to userspace by Pierre-Marc Fournier.
8 */
9
10 #define _LGPL_SOURCE
11 #include <errno.h>
12 #include <stdint.h>
13 #include <stddef.h>
14 #include <stdio.h>
15
16 #include <urcu/arch.h>
17 #include <lttng/urcu/urcu-ust.h>
18 #include <urcu/hlist.h>
19 #include <urcu/uatomic.h>
20 #include <urcu/compiler.h>
21 #include <urcu/system.h>
22
23 #include <lttng/tracepoint.h>
24 #include <lttng/ust-abi.h> /* for LTTNG_UST_ABI_SYM_NAME_LEN */
25
26 #include "common/logging.h"
27 #include "common/macros.h"
28
29 #include "lib/lttng-ust-tracepoint/tracepoint.h"
30 #include "common/tracepoint.h"
31 #include "common/jhash.h"
32 #include "common/err-ptr.h"
33
34 /* Test compiler support for weak symbols with hidden visibility. */
35 int __tracepoint_test_symbol1 __attribute__((weak, visibility("hidden")));
36 void *__tracepoint_test_symbol2 __attribute__((weak, visibility("hidden")));
37 struct {
38 char a[24];
39 } __tracepoint_test_symbol3 __attribute__((weak, visibility("hidden")));
40
41 /* Set to 1 to enable tracepoint debug output */
42 static const int tracepoint_debug;
43 static int initialized;
44
45 /*
46 * If tracepoint_destructors_state = 1, tracepoint destructors are
47 * enabled. They are disabled otherwise.
48 */
49 static int tracepoint_destructors_state = 1;
50
51 static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
52
53 /*
54 * tracepoint_mutex nests inside UST mutex.
55 *
56 * Note about interaction with fork/clone: UST does not hold the
57 * tracepoint mutex across fork/clone because it is either:
58 * - nested within UST mutex, in which case holding the UST mutex across
59 * fork/clone suffice,
60 * - taken by a library constructor, which should never race with a
61 * fork/clone if the application is expected to continue running with
62 * the same memory layout (no following exec()).
63 */
64 static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
65
66 /*
67 * libraries that contain tracepoints (struct tracepoint_lib).
68 * Protected by tracepoint mutex.
69 */
70 static CDS_LIST_HEAD(libs);
71
72 /*
73 * The tracepoint mutex protects the library tracepoints, the hash table, and
74 * the library list.
75 * All calls to the tracepoint API must be protected by the tracepoint mutex,
76 * excepts calls to tracepoint_register_lib and
77 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
78 */
79
80 /*
81 * Tracepoint hash table, containing the active tracepoints.
82 * Protected by tracepoint mutex.
83 */
84 #define TRACEPOINT_HASH_BITS 12
85 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
86 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
87
88 static CDS_LIST_HEAD(old_probes);
89 static int need_update;
90
91 static CDS_LIST_HEAD(release_queue);
92 static int release_queue_need_update;
93
94 /*
95 * Note about RCU :
96 * It is used to to delay the free of multiple probes array until a quiescent
97 * state is reached.
98 * Tracepoint entries modifications are protected by the tracepoint mutex.
99 */
100 struct tracepoint_entry {
101 struct cds_hlist_node hlist;
102 struct lttng_ust_tracepoint_probe *probes;
103 int refcount; /* Number of times armed. 0 if disarmed. */
104 int callsite_refcount; /* how many libs use this tracepoint */
105 char *signature;
106 char *name;
107 };
108
109 struct tp_probes {
110 union {
111 struct cds_list_head list;
112 /* Field below only used for call_rcu scheme */
113 /* struct rcu_head head; */
114 } u;
115 struct lttng_ust_tracepoint_probe probes[0];
116 };
117
118 /*
119 * Callsite hash table, containing the tracepoint call sites.
120 * Protected by tracepoint mutex.
121 */
122 #define CALLSITE_HASH_BITS 12
123 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
124 static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
125
126 struct callsite_entry {
127 struct cds_hlist_node hlist; /* hash table node */
128 struct cds_list_head node; /* lib list of callsites node */
129 struct lttng_ust_tracepoint *tp;
130 bool tp_entry_callsite_ref; /* Has a tp_entry took a ref on this callsite */
131 };
132
133 lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX == LTTNG_UST_ABI_SYM_NAME_LEN,
134 "Tracepoint name max length mismatch between UST ABI and tracepoint API",
135 Tracepoint_name_max_length_mismatch);
136
137 /* coverity[+alloc] */
138 static void *allocate_probes(int count)
139 {
140 struct tp_probes *p =
141 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
142 + sizeof(struct tp_probes));
143 return p == NULL ? NULL : p->probes;
144 }
145
146 /* coverity[+free : arg-0] */
147 static void release_probes(void *old)
148 {
149 if (old) {
150 struct tp_probes *tp_probes = caa_container_of(old,
151 struct tp_probes, probes[0]);
152 lttng_ust_urcu_synchronize_rcu();
153 free(tp_probes);
154 }
155 }
156
157 static void debug_print_probes(struct tracepoint_entry *entry)
158 {
159 int i;
160
161 if (!tracepoint_debug || !entry->probes)
162 return;
163
164 for (i = 0; entry->probes[i].func; i++)
165 DBG("Probe %d : %p", i, entry->probes[i].func);
166 }
167
168 static void *
169 tracepoint_entry_add_probe(struct tracepoint_entry *entry,
170 void (*probe)(void), void *data)
171 {
172 int nr_probes = 0;
173 struct lttng_ust_tracepoint_probe *old, *new;
174
175 if (!probe) {
176 WARN_ON(1);
177 return ERR_PTR(-EINVAL);
178 }
179 debug_print_probes(entry);
180 old = entry->probes;
181 if (old) {
182 /* (N -> N+1), (N != 0, 1) probes */
183 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
184 if (old[nr_probes].func == probe &&
185 old[nr_probes].data == data)
186 return ERR_PTR(-EEXIST);
187 }
188 /* + 2 : one for new probe, one for NULL func */
189 new = allocate_probes(nr_probes + 2);
190 if (new == NULL)
191 return ERR_PTR(-ENOMEM);
192 if (old)
193 memcpy(new, old,
194 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
195 new[nr_probes].func = probe;
196 new[nr_probes].data = data;
197 new[nr_probes + 1].func = NULL;
198 entry->refcount = nr_probes + 1;
199 entry->probes = new;
200 debug_print_probes(entry);
201 return old;
202 }
203
204 static void *
205 tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
206 void (*probe)(void), void *data)
207 {
208 int nr_probes = 0, nr_del = 0, i;
209 struct lttng_ust_tracepoint_probe *old, *new;
210
211 old = entry->probes;
212
213 if (!old)
214 return ERR_PTR(-ENOENT);
215
216 debug_print_probes(entry);
217 /* (N -> M), (N > 1, M >= 0) probes */
218 if (probe) {
219 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
220 if (old[nr_probes].func == probe &&
221 old[nr_probes].data == data)
222 nr_del++;
223 }
224 }
225
226 if (nr_probes - nr_del == 0) {
227 /* N -> 0, (N > 1) */
228 entry->probes = NULL;
229 entry->refcount = 0;
230 debug_print_probes(entry);
231 return old;
232 } else {
233 int j = 0;
234 /* N -> M, (N > 1, M > 0) */
235 /* + 1 for NULL */
236 new = allocate_probes(nr_probes - nr_del + 1);
237 if (new == NULL)
238 return ERR_PTR(-ENOMEM);
239 for (i = 0; old[i].func; i++)
240 if (old[i].func != probe || old[i].data != data)
241 new[j++] = old[i];
242 new[nr_probes - nr_del].func = NULL;
243 entry->refcount = nr_probes - nr_del;
244 entry->probes = new;
245 }
246 debug_print_probes(entry);
247 return old;
248 }
249
250 /*
251 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
252 * Must be called with tracepoint mutex held.
253 * Returns NULL if not present.
254 */
255 static struct tracepoint_entry *get_tracepoint(const char *name)
256 {
257 struct cds_hlist_head *head;
258 struct cds_hlist_node *node;
259 struct tracepoint_entry *e;
260 size_t name_len = strlen(name);
261 uint32_t hash;
262
263 if (name_len > LTTNG_UST_ABI_SYM_NAME_LEN - 1) {
264 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
265 name_len = LTTNG_UST_ABI_SYM_NAME_LEN - 1;
266 }
267 hash = jhash(name, name_len, 0);
268 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
269 cds_hlist_for_each_entry(e, node, head, hlist) {
270 if (!strncmp(name, e->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1))
271 return e;
272 }
273 return NULL;
274 }
275
276 /*
277 * Add the tracepoint to the tracepoint hash table. Must be called with
278 * tracepoint mutex held.
279 */
280 static struct tracepoint_entry *add_tracepoint(const char *name,
281 const char *signature)
282 {
283 struct cds_hlist_head *head;
284 struct cds_hlist_node *node;
285 struct tracepoint_entry *e;
286 size_t name_len = strlen(name);
287 size_t sig_len = strlen(signature);
288 size_t sig_off, name_off;
289 uint32_t hash;
290
291 if (name_len > LTTNG_UST_ABI_SYM_NAME_LEN - 1) {
292 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
293 name_len = LTTNG_UST_ABI_SYM_NAME_LEN - 1;
294 }
295 hash = jhash(name, name_len, 0);
296 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
297 cds_hlist_for_each_entry(e, node, head, hlist) {
298 if (!strncmp(name, e->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1)) {
299 DBG("tracepoint %s busy", name);
300 return ERR_PTR(-EEXIST); /* Already there */
301 }
302 }
303
304 /*
305 * Using zmalloc here to allocate a variable length elements: name and
306 * signature. Could cause some memory fragmentation if overused.
307 */
308 name_off = sizeof(struct tracepoint_entry);
309 sig_off = name_off + name_len + 1;
310
311 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1 + sig_len + 1);
312 if (!e)
313 return ERR_PTR(-ENOMEM);
314 e->name = (char *) e + name_off;
315 memcpy(e->name, name, name_len + 1);
316 e->name[name_len] = '\0';
317
318 e->signature = (char *) e + sig_off;
319 memcpy(e->signature, signature, sig_len + 1);
320 e->signature[sig_len] = '\0';
321
322 e->probes = NULL;
323 e->refcount = 0;
324 e->callsite_refcount = 0;
325
326 cds_hlist_add_head(&e->hlist, head);
327 return e;
328 }
329
330 /*
331 * Remove the tracepoint from the tracepoint hash table. Must be called with
332 * tracepoint mutex held.
333 */
334 static void remove_tracepoint(struct tracepoint_entry *e)
335 {
336 cds_hlist_del(&e->hlist);
337 free(e);
338 }
339
340 /*
341 * Sets the probe callback corresponding to one tracepoint.
342 */
343 static void set_tracepoint(struct tracepoint_entry **entry,
344 struct lttng_ust_tracepoint *elem, int active)
345 {
346 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1) != 0);
347 /*
348 * Check that signatures match before connecting a probe to a
349 * tracepoint. Warn the user if they don't.
350 */
351 if (strcmp(elem->signature, (*entry)->signature) != 0) {
352 static int warned = 0;
353
354 /* Only print once, don't flood console. */
355 if (!warned) {
356 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
357 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
358 elem->name, elem->signature, (*entry)->signature);
359 warned = 1;
360 }
361 /* Don't accept connecting non-matching signatures. */
362 return;
363 }
364
365 /*
366 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
367 * probe callbacks array is consistent before setting a pointer to it.
368 * This array is referenced by __DO_TRACE from
369 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
370 * is used.
371 */
372 lttng_ust_rcu_assign_pointer(elem->probes, (*entry)->probes);
373 CMM_STORE_SHARED(elem->state, active);
374 }
375
376 /*
377 * Disable a tracepoint and its probe callback.
378 * Note: only waiting an RCU period after setting elem->call to the empty
379 * function insures that the original callback is not used anymore. This insured
380 * by preempt_disable around the call site.
381 */
382 static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
383 {
384 CMM_STORE_SHARED(elem->state, 0);
385 lttng_ust_rcu_assign_pointer(elem->probes, NULL);
386 }
387
388 /*
389 * Add the callsite to the callsite hash table. Must be called with
390 * tracepoint mutex held.
391 */
392 static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
393 {
394 struct cds_hlist_head *head;
395 struct callsite_entry *e;
396 const char *name = tp->name;
397 size_t name_len = strlen(name);
398 uint32_t hash;
399 struct tracepoint_entry *tp_entry;
400
401 if (name_len > LTTNG_UST_ABI_SYM_NAME_LEN - 1) {
402 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
403 name_len = LTTNG_UST_ABI_SYM_NAME_LEN - 1;
404 }
405 hash = jhash(name, name_len, 0);
406 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
407 e = zmalloc(sizeof(struct callsite_entry));
408 if (!e) {
409 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
410 return;
411 }
412 cds_hlist_add_head(&e->hlist, head);
413 e->tp = tp;
414 cds_list_add(&e->node, &lib->callsites);
415
416 tp_entry = get_tracepoint(name);
417 if (!tp_entry)
418 return;
419 tp_entry->callsite_refcount++;
420 e->tp_entry_callsite_ref = true;
421 }
422
423 /*
424 * Remove the callsite from the callsite hash table and from lib
425 * callsite list. Must be called with tracepoint mutex held.
426 */
427 static void remove_callsite(struct callsite_entry *e)
428 {
429 struct tracepoint_entry *tp_entry;
430
431 tp_entry = get_tracepoint(e->tp->name);
432 if (tp_entry) {
433 if (e->tp_entry_callsite_ref)
434 tp_entry->callsite_refcount--;
435 if (tp_entry->callsite_refcount == 0)
436 disable_tracepoint(e->tp);
437 }
438 cds_hlist_del(&e->hlist);
439 cds_list_del(&e->node);
440 free(e);
441 }
442
443 /*
444 * Enable/disable all callsites based on the state of a specific
445 * tracepoint entry.
446 * Must be called with tracepoint mutex held.
447 */
448 static void tracepoint_sync_callsites(const char *name)
449 {
450 struct cds_hlist_head *head;
451 struct cds_hlist_node *node;
452 struct callsite_entry *e;
453 size_t name_len = strlen(name);
454 uint32_t hash;
455 struct tracepoint_entry *tp_entry;
456
457 tp_entry = get_tracepoint(name);
458 if (name_len > LTTNG_UST_ABI_SYM_NAME_LEN - 1) {
459 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
460 name_len = LTTNG_UST_ABI_SYM_NAME_LEN - 1;
461 }
462 hash = jhash(name, name_len, 0);
463 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
464 cds_hlist_for_each_entry(e, node, head, hlist) {
465 struct lttng_ust_tracepoint *tp = e->tp;
466
467 if (strncmp(name, tp->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1))
468 continue;
469 if (tp_entry) {
470 if (!e->tp_entry_callsite_ref) {
471 tp_entry->callsite_refcount++;
472 e->tp_entry_callsite_ref = true;
473 }
474 set_tracepoint(&tp_entry, tp,
475 !!tp_entry->refcount);
476 } else {
477 disable_tracepoint(tp);
478 e->tp_entry_callsite_ref = false;
479 }
480 }
481 }
482
483 /**
484 * tracepoint_update_probe_range - Update a probe range
485 * @begin: beginning of the range
486 * @end: end of the range
487 *
488 * Updates the probe callback corresponding to a range of tracepoints.
489 */
490 static
491 void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
492 struct lttng_ust_tracepoint * const *end)
493 {
494 struct lttng_ust_tracepoint * const *iter;
495 struct tracepoint_entry *mark_entry;
496
497 for (iter = begin; iter < end; iter++) {
498 if (!*iter)
499 continue; /* skip dummy */
500 if (!(*iter)->name) {
501 disable_tracepoint(*iter);
502 continue;
503 }
504 mark_entry = get_tracepoint((*iter)->name);
505 if (mark_entry) {
506 set_tracepoint(&mark_entry, *iter,
507 !!mark_entry->refcount);
508 } else {
509 disable_tracepoint(*iter);
510 }
511 }
512 }
513
514 static void lib_update_tracepoints(struct tracepoint_lib *lib)
515 {
516 tracepoint_update_probe_range(lib->tracepoints_start,
517 lib->tracepoints_start + lib->tracepoints_count);
518 }
519
520 static void lib_register_callsites(struct tracepoint_lib *lib)
521 {
522 struct lttng_ust_tracepoint * const *begin;
523 struct lttng_ust_tracepoint * const *end;
524 struct lttng_ust_tracepoint * const *iter;
525
526 begin = lib->tracepoints_start;
527 end = lib->tracepoints_start + lib->tracepoints_count;
528
529 for (iter = begin; iter < end; iter++) {
530 if (!*iter)
531 continue; /* skip dummy */
532 if (!(*iter)->name) {
533 continue;
534 }
535 add_callsite(lib, *iter);
536 }
537 }
538
539 static void lib_unregister_callsites(struct tracepoint_lib *lib)
540 {
541 struct callsite_entry *callsite, *tmp;
542
543 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
544 remove_callsite(callsite);
545 }
546
547 /*
548 * Update probes, removing the faulty probes.
549 */
550 static void tracepoint_update_probes(void)
551 {
552 struct tracepoint_lib *lib;
553
554 /* tracepoints registered from libraries and executable. */
555 cds_list_for_each_entry(lib, &libs, list)
556 lib_update_tracepoints(lib);
557 }
558
559 static struct lttng_ust_tracepoint_probe *
560 tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
561 const char *signature)
562 {
563 struct tracepoint_entry *entry;
564 struct lttng_ust_tracepoint_probe *old;
565
566 entry = get_tracepoint(name);
567 if (entry) {
568 if (strcmp(entry->signature, signature) != 0) {
569 ERR("Tracepoint and probe signature do not match.");
570 return ERR_PTR(-EINVAL);
571 }
572 } else {
573 entry = add_tracepoint(name, signature);
574 if (IS_ERR(entry))
575 return (struct lttng_ust_tracepoint_probe *)entry;
576 }
577 old = tracepoint_entry_add_probe(entry, probe, data);
578 if (IS_ERR(old) && !entry->refcount)
579 remove_tracepoint(entry);
580 return old;
581 }
582
583 static void tracepoint_release_queue_add_old_probes(void *old)
584 {
585 release_queue_need_update = 1;
586 if (old) {
587 struct tp_probes *tp_probes = caa_container_of(old,
588 struct tp_probes, probes[0]);
589 cds_list_add(&tp_probes->u.list, &release_queue);
590 }
591 }
592
593 /**
594 * __tracepoint_probe_register - Connect a probe to a tracepoint
595 * @name: tracepoint name
596 * @probe: probe handler
597 *
598 * Returns 0 if ok, error value on error.
599 * The probe address must at least be aligned on the architecture pointer size.
600 * Called with the tracepoint mutex held.
601 */
602 int __tracepoint_probe_register(const char *name, void (*probe)(void),
603 void *data, const char *signature)
604 {
605 void *old;
606 int ret = 0;
607
608 DBG("Registering probe to tracepoint %s", name);
609
610 pthread_mutex_lock(&tracepoint_mutex);
611 old = tracepoint_add_probe(name, probe, data, signature);
612 if (IS_ERR(old)) {
613 ret = PTR_ERR(old);
614 goto end;
615 }
616
617 tracepoint_sync_callsites(name);
618 release_probes(old);
619 end:
620 pthread_mutex_unlock(&tracepoint_mutex);
621 return ret;
622 }
623
624 /*
625 * Caller needs to invoke __tracepoint_probe_release_queue() after
626 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
627 * times to ensure it does not leak memory.
628 */
629 int lttng_ust_tp_probe_register_queue_release(const char *name,
630 void (*probe)(void), void *data, const char *signature)
631 {
632 void *old;
633 int ret = 0;
634
635 DBG("Registering probe to tracepoint %s. Queuing release.", name);
636
637 pthread_mutex_lock(&tracepoint_mutex);
638 old = tracepoint_add_probe(name, probe, data, signature);
639 if (IS_ERR(old)) {
640 ret = PTR_ERR(old);
641 goto end;
642 }
643
644 tracepoint_sync_callsites(name);
645 tracepoint_release_queue_add_old_probes(old);
646 end:
647 pthread_mutex_unlock(&tracepoint_mutex);
648 return ret;
649 }
650
651 static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
652 void *data)
653 {
654 struct tracepoint_entry *entry;
655 void *old;
656
657 entry = get_tracepoint(name);
658 if (!entry)
659 return ERR_PTR(-ENOENT);
660 old = tracepoint_entry_remove_probe(entry, probe, data);
661 if (IS_ERR(old))
662 return old;
663 if (!entry->refcount)
664 remove_tracepoint(entry);
665 return old;
666 }
667
668 /**
669 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
670 * @name: tracepoint name
671 * @probe: probe function pointer
672 * @probe: probe data pointer
673 */
674 int __tracepoint_probe_unregister(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_sync_callsites(name);
689 release_probes(old);
690 end:
691 pthread_mutex_unlock(&tracepoint_mutex);
692 return ret;
693 }
694
695 /*
696 * Caller needs to invoke __tracepoint_probe_release_queue() after
697 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
698 * times to ensure it does not leak memory.
699 */
700 int lttng_ust_tp_probe_unregister_queue_release(const char *name,
701 void (*probe)(void), void *data)
702 {
703 void *old;
704 int ret = 0;
705
706 DBG("Un-registering probe from tracepoint %s. Queuing release.", name);
707
708 pthread_mutex_lock(&tracepoint_mutex);
709 old = tracepoint_remove_probe(name, probe, data);
710 if (IS_ERR(old)) {
711 ret = PTR_ERR(old);
712 goto end;
713 }
714 tracepoint_sync_callsites(name);
715 tracepoint_release_queue_add_old_probes(old);
716 end:
717 pthread_mutex_unlock(&tracepoint_mutex);
718 return ret;
719 }
720
721 void lttng_ust_tp_probe_prune_release_queue(void)
722 {
723 CDS_LIST_HEAD(release_probes);
724 struct tp_probes *pos, *next;
725
726 DBG("Release queue of unregistered tracepoint probes.");
727
728 pthread_mutex_lock(&tracepoint_mutex);
729 if (!release_queue_need_update)
730 goto end;
731 if (!cds_list_empty(&release_queue))
732 cds_list_replace_init(&release_queue, &release_probes);
733 release_queue_need_update = 0;
734
735 /* Wait for grace period between all sync_callsites and free. */
736 lttng_ust_urcu_synchronize_rcu();
737
738 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
739 cds_list_del(&pos->u.list);
740 free(pos);
741 }
742 end:
743 pthread_mutex_unlock(&tracepoint_mutex);
744 }
745
746 static void tracepoint_add_old_probes(void *old)
747 {
748 need_update = 1;
749 if (old) {
750 struct tp_probes *tp_probes = caa_container_of(old,
751 struct tp_probes, probes[0]);
752 cds_list_add(&tp_probes->u.list, &old_probes);
753 }
754 }
755
756 /**
757 * tracepoint_probe_register_noupdate - register a probe but not connect
758 * @name: tracepoint name
759 * @probe: probe handler
760 *
761 * caller must call tracepoint_probe_update_all()
762 */
763 int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
764 void *data, const char *signature)
765 {
766 void *old;
767 int ret = 0;
768
769 pthread_mutex_lock(&tracepoint_mutex);
770 old = tracepoint_add_probe(name, probe, data, signature);
771 if (IS_ERR(old)) {
772 ret = PTR_ERR(old);
773 goto end;
774 }
775 tracepoint_add_old_probes(old);
776 end:
777 pthread_mutex_unlock(&tracepoint_mutex);
778 return ret;
779 }
780
781 /**
782 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
783 * @name: tracepoint name
784 * @probe: probe function pointer
785 *
786 * caller must call tracepoint_probe_update_all()
787 * Called with the tracepoint mutex held.
788 */
789 int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
790 void *data)
791 {
792 void *old;
793 int ret = 0;
794
795 DBG("Un-registering probe from tracepoint %s", name);
796
797 pthread_mutex_lock(&tracepoint_mutex);
798 old = tracepoint_remove_probe(name, probe, data);
799 if (IS_ERR(old)) {
800 ret = PTR_ERR(old);
801 goto end;
802 }
803 tracepoint_add_old_probes(old);
804 end:
805 pthread_mutex_unlock(&tracepoint_mutex);
806 return ret;
807 }
808
809 /**
810 * tracepoint_probe_update_all - update tracepoints
811 */
812 void tracepoint_probe_update_all(void)
813 {
814 CDS_LIST_HEAD(release_probes);
815 struct tp_probes *pos, *next;
816
817 pthread_mutex_lock(&tracepoint_mutex);
818 if (!need_update) {
819 goto end;
820 }
821 if (!cds_list_empty(&old_probes))
822 cds_list_replace_init(&old_probes, &release_probes);
823 need_update = 0;
824
825 tracepoint_update_probes();
826 /* Wait for grace period between update_probes and free. */
827 lttng_ust_urcu_synchronize_rcu();
828 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
829 cds_list_del(&pos->u.list);
830 free(pos);
831 }
832 end:
833 pthread_mutex_unlock(&tracepoint_mutex);
834 }
835
836 static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
837 struct lttng_ust_tracepoint * const *end)
838 {
839 if (new_tracepoint_cb) {
840 struct lttng_ust_tracepoint * const *t;
841
842 for (t = start; t < end; t++) {
843 if (*t)
844 new_tracepoint_cb(*t);
845 }
846 }
847 }
848
849 /*
850 * tracepoint_{un,}register_lib is meant to be looked up by instrumented
851 * applications through dlsym(). If found, those can register their
852 * tracepoints, else those tracepoints will not be available for
853 * tracing. The number at the end of those symbols acts as a major
854 * version for tracepoints.
855 *
856 * Older instrumented applications should still work with newer
857 * liblttng-ust, but it is fine that instrumented applications compiled
858 * against recent liblttng-ust headers require a recent liblttng-ust
859 * runtime for those tracepoints to be taken into account.
860 */
861 int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
862 int tracepoints_count);
863 int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
864 int tracepoints_count)
865 {
866 struct tracepoint_lib *pl, *iter;
867
868 lttng_ust_tp_init();
869
870 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
871 if (!pl) {
872 PERROR("Unable to register tracepoint lib");
873 return -1;
874 }
875 pl->tracepoints_start = tracepoints_start;
876 pl->tracepoints_count = tracepoints_count;
877 CDS_INIT_LIST_HEAD(&pl->callsites);
878
879 pthread_mutex_lock(&tracepoint_mutex);
880 /*
881 * We sort the libs by struct lib pointer address.
882 */
883 cds_list_for_each_entry_reverse(iter, &libs, list) {
884 BUG_ON(iter == pl); /* Should never be in the list twice */
885 if (iter < pl) {
886 /* We belong to the location right after iter. */
887 cds_list_add(&pl->list, &iter->list);
888 goto lib_added;
889 }
890 }
891 /* We should be added at the head of the list */
892 cds_list_add(&pl->list, &libs);
893 lib_added:
894 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
895 lib_register_callsites(pl);
896 lib_update_tracepoints(pl);
897 pthread_mutex_unlock(&tracepoint_mutex);
898
899 DBG("just registered a tracepoints section from %p and having %d tracepoints",
900 tracepoints_start, tracepoints_count);
901 if (lttng_ust_logging_debug_enabled()) {
902 int i;
903
904 for (i = 0; i < tracepoints_count; i++) {
905 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
906 }
907 }
908
909 return 0;
910 }
911
912 int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start);
913 int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start)
914 {
915 struct tracepoint_lib *lib;
916
917 pthread_mutex_lock(&tracepoint_mutex);
918 cds_list_for_each_entry(lib, &libs, list) {
919 if (lib->tracepoints_start != tracepoints_start)
920 continue;
921
922 cds_list_del(&lib->list);
923 /*
924 * Unregistering a callsite also decreases the
925 * callsite reference count of the corresponding
926 * tracepoint, and disables the tracepoint if
927 * the reference count drops to zero.
928 */
929 lib_unregister_callsites(lib);
930 DBG("just unregistered a tracepoints section from %p",
931 lib->tracepoints_start);
932 free(lib);
933 break;
934 }
935 pthread_mutex_unlock(&tracepoint_mutex);
936 return 0;
937 }
938
939 /*
940 * Report in debug message whether the compiler correctly supports weak
941 * hidden symbols. This test checks that the address associated with two
942 * weak symbols with hidden visibility is the same when declared within
943 * two compile units part of the same module.
944 */
945 static void check_weak_hidden(void)
946 {
947 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
948 &__tracepoint_test_symbol1 == lttng_ust_tp_check_weak_hidden1() ?
949 "SAME address" :
950 "DIFFERENT addresses");
951 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
952 &__tracepoint_test_symbol2 == lttng_ust_tp_check_weak_hidden2() ?
953 "SAME address" :
954 "DIFFERENT addresses");
955 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
956 &__tracepoint_test_symbol3 == lttng_ust_tp_check_weak_hidden3() ?
957 "SAME address" :
958 "DIFFERENT addresses");
959 }
960
961 void lttng_ust_tp_init(void)
962 {
963 if (uatomic_xchg(&initialized, 1) == 1)
964 return;
965 lttng_ust_logging_init();
966 check_weak_hidden();
967 }
968
969 void lttng_ust_tp_exit(void)
970 {
971 initialized = 0;
972 }
973
974 /*
975 * Create the wrapper symbols.
976 */
977 #undef tp_rcu_read_lock
978 #undef tp_rcu_read_unlock
979 #undef tp_rcu_dereference
980
981 void tp_rcu_read_lock(void);
982 void tp_rcu_read_lock(void)
983 {
984 lttng_ust_urcu_read_lock();
985 }
986
987 void tp_rcu_read_unlock(void);
988 void tp_rcu_read_unlock(void)
989 {
990 lttng_ust_urcu_read_unlock();
991 }
992
993 void *tp_rcu_dereference_sym(void *p);
994 void *tp_rcu_dereference_sym(void *p)
995 {
996 return lttng_ust_rcu_dereference(p);
997 }
998
999 /*
1000 * Programs that have threads that survive after they exit, and therefore call
1001 * library destructors, should disable the tracepoint destructors by calling
1002 * tp_disable_destructors(). This will leak the tracepoint
1003 * instrumentation library shared object, leaving its teardown to the operating
1004 * system process teardown.
1005 *
1006 * To access and/or modify this value, users need to use a combination of
1007 * dlopen(3) and dlsym(3) to get an handle on the
1008 * tp_disable_destructors and tp_get_destructors_state symbols below.
1009 */
1010 void tp_disable_destructors(void);
1011 void tp_disable_destructors(void)
1012 {
1013 uatomic_set(&tracepoint_destructors_state, 0);
1014 }
1015
1016 /*
1017 * Returns 1 if the destructors are enabled and should be executed.
1018 * Returns 0 if the destructors are disabled.
1019 */
1020 int tp_get_destructors_state(void);
1021 int tp_get_destructors_state(void)
1022 {
1023 return uatomic_read(&tracepoint_destructors_state);
1024 }
This page took 0.048181 seconds and 3 git commands to generate.