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