Detect unsupported use of .so.0 and .so.1 libraries within same process
[lttng-ust.git] / src / lib / lttng-ust-tracepoint / tracepoint.c
CommitLineData
f99be407 1/*
c0c0989a
MJ
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
b27f8e75 4 * Copyright (C) 2008-2011 Mathieu Desnoyers
1f8b0dff 5 * Copyright (C) 2009 Pierre-Marc Fournier
f99be407 6 *
1f8b0dff 7 * Ported to userspace by Pierre-Marc Fournier.
f99be407 8 */
1f8b0dff 9
b0c4126f 10#define _LGPL_SOURCE
474d745f 11#include <errno.h>
b728d87e
MD
12#include <stdint.h>
13#include <stddef.h>
33661f97 14#include <stdio.h>
6f78600e 15#include <dlfcn.h>
44c72f10 16
b728d87e 17#include <urcu/arch.h>
10544ee8 18#include <lttng/urcu/urcu-ust.h>
10c56168 19#include <urcu/hlist.h>
edaa1431 20#include <urcu/uatomic.h>
b728d87e 21#include <urcu/compiler.h>
c082d14b 22#include <urcu/system.h>
44c72f10
MD
23
24#include <lttng/tracepoint.h>
fd17d7ce 25#include <lttng/ust-abi.h> /* for LTTNG_UST_ABI_SYM_NAME_LEN */
fca97dfd 26#include <lttng/ust-common.h>
44c72f10 27
9d315d6d
MJ
28#include "common/logging.h"
29#include "common/macros.h"
474d745f 30
8f51c684
MJ
31#include "lib/lttng-ust-tracepoint/tracepoint.h"
32#include "common/tracepoint.h"
e58e5ad5 33#include "common/jhash.h"
9c4fe08d 34#include "common/err-ptr.h"
b0c4126f 35
5517d34d 36/* Test compiler support for weak symbols with hidden visibility. */
0274f7f2
MJ
37int lttng_ust_tracepoint_test_symbol1 __attribute__((weak, visibility("hidden")));
38void *lttng_ust_tracepoint_test_symbol2 __attribute__((weak, visibility("hidden")));
b0e63efd
MD
39struct {
40 char a[24];
0274f7f2 41} lttng_ust_tracepoint_test_symbol3 __attribute__((weak, visibility("hidden")));
5517d34d 42
f99be407
PMF
43/* Set to 1 to enable tracepoint debug output */
44static const int tracepoint_debug;
b27f8e75 45static int initialized;
0fd0de10
FD
46
47/*
48 * If tracepoint_destructors_state = 1, tracepoint destructors are
49 * enabled. They are disabled otherwise.
50 */
51static int tracepoint_destructors_state = 1;
52
1a206094 53static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
f99be407 54
8792fbae
MD
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 */
66static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
67
efa2c591
MD
68/*
69 * libraries that contain tracepoints (struct tracepoint_lib).
8792fbae 70 * Protected by tracepoint mutex.
efa2c591 71 */
0222e121 72static CDS_LIST_HEAD(libs);
474d745f 73
f99be407 74/*
8792fbae 75 * The tracepoint mutex protects the library tracepoints, the hash table, and
17dfb34b 76 * the library list.
8792fbae 77 * All calls to the tracepoint API must be protected by the tracepoint mutex,
14227bde
MJ
78 * excepts calls to lttng_ust_tracepoint_module_register and
79 * lttng_ust_tracepoint_module_unregister, which take the tracepoint mutex themselves.
f99be407 80 */
f99be407
PMF
81
82/*
83 * Tracepoint hash table, containing the active tracepoints.
8792fbae 84 * Protected by tracepoint mutex.
f99be407 85 */
814f7df1 86#define TRACEPOINT_HASH_BITS 12
f99be407 87#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
10c56168 88static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 89
b27f8e75
MD
90static CDS_LIST_HEAD(old_probes);
91static int need_update;
92
baa1e0bc
MD
93static CDS_LIST_HEAD(release_queue);
94static int release_queue_need_update;
95
f99be407
PMF
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.
8792fbae 100 * Tracepoint entries modifications are protected by the tracepoint mutex.
f99be407
PMF
101 */
102struct tracepoint_entry {
10c56168 103 struct cds_hlist_node hlist;
1a206094 104 struct lttng_ust_tracepoint_probe *probes;
f99be407 105 int refcount; /* Number of times armed. 0 if disarmed. */
8a7ad54b 106 int callsite_refcount; /* how many libs use this tracepoint */
2c05c691 107 char *signature;
34f7f142
MD
108 char *provider_name;
109 char *event_name;
f99be407
PMF
110};
111
112struct tp_probes {
113 union {
0222e121 114 struct cds_list_head list;
ade7037b
MD
115 /* Field below only used for call_rcu scheme */
116 /* struct rcu_head head; */
f99be407 117 } u;
1a206094 118 struct lttng_ust_tracepoint_probe probes[0];
f99be407
PMF
119};
120
3469bbbe
MD
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)
127static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
128
129struct callsite_entry {
130 struct cds_hlist_node hlist; /* hash table node */
131 struct cds_list_head node; /* lib list of callsites node */
1a206094 132 struct lttng_ust_tracepoint *tp;
2c05c691 133 bool tp_entry_callsite_ref; /* Has a tp_entry took a ref on this callsite */
3469bbbe
MD
134};
135
81b16412
MD
136lttng_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
34f7f142
MD
140static
141bool 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
5e6df7ea 149/* coverity[+alloc] */
efa2c591 150static void *allocate_probes(int count)
f99be407 151{
1a206094
SM
152 struct tp_probes *p =
153 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
154 + sizeof(struct tp_probes));
f99be407
PMF
155 return p == NULL ? NULL : p->probes;
156}
157
5e6df7ea 158/* coverity[+free : arg-0] */
efa2c591 159static void release_probes(void *old)
f99be407
PMF
160{
161 if (old) {
b728d87e 162 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 163 struct tp_probes, probes[0]);
b653ddc1 164 lttng_ust_urcu_synchronize_rcu();
909bc43f 165 free(tp_probes);
f99be407
PMF
166 }
167}
168
169static void debug_print_probes(struct tracepoint_entry *entry)
170{
171 int i;
172
9dec086e 173 if (!tracepoint_debug || !entry->probes)
f99be407
PMF
174 return;
175
9dec086e
NC
176 for (i = 0; entry->probes[i].func; i++)
177 DBG("Probe %d : %p", i, entry->probes[i].func);
f99be407
PMF
178}
179
180static void *
9dec086e 181tracepoint_entry_add_probe(struct tracepoint_entry *entry,
fbdeb5ec 182 void (*probe)(void), void *data)
f99be407
PMF
183{
184 int nr_probes = 0;
1a206094 185 struct lttng_ust_tracepoint_probe *old, *new;
f99be407 186
d7509147
MD
187 if (!probe) {
188 WARN_ON(1);
189 return ERR_PTR(-EINVAL);
190 }
f99be407 191 debug_print_probes(entry);
9dec086e 192 old = entry->probes;
f99be407
PMF
193 if (old) {
194 /* (N -> N+1), (N != 0, 1) probes */
9dec086e
NC
195 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
196 if (old[nr_probes].func == probe &&
197 old[nr_probes].data == data)
f99be407
PMF
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)
1a206094
SM
205 memcpy(new, old,
206 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
9dec086e
NC
207 new[nr_probes].func = probe;
208 new[nr_probes].data = data;
209 new[nr_probes + 1].func = NULL;
f99be407 210 entry->refcount = nr_probes + 1;
9dec086e 211 entry->probes = new;
f99be407
PMF
212 debug_print_probes(entry);
213 return old;
214}
215
216static void *
fbdeb5ec
MD
217tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
218 void (*probe)(void), void *data)
f99be407
PMF
219{
220 int nr_probes = 0, nr_del = 0, i;
1a206094 221 struct lttng_ust_tracepoint_probe *old, *new;
f99be407 222
9dec086e 223 old = entry->probes;
f99be407
PMF
224
225 if (!old)
226 return ERR_PTR(-ENOENT);
227
228 debug_print_probes(entry);
229 /* (N -> M), (N > 1, M >= 0) probes */
956c6fab
MD
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 }
f99be407
PMF
236 }
237
238 if (nr_probes - nr_del == 0) {
239 /* N -> 0, (N > 1) */
9dec086e 240 entry->probes = NULL;
f99be407
PMF
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);
9dec086e 251 for (i = 0; old[i].func; i++)
956c6fab 252 if (old[i].func != probe || old[i].data != data)
f99be407 253 new[j++] = old[i];
9dec086e 254 new[nr_probes - nr_del].func = NULL;
f99be407 255 entry->refcount = nr_probes - nr_del;
9dec086e 256 entry->probes = new;
f99be407
PMF
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.
8792fbae 264 * Must be called with tracepoint mutex held.
f99be407
PMF
265 * Returns NULL if not present.
266 */
34f7f142 267static struct tracepoint_entry *get_tracepoint(const char *provider_name, const char *event_name)
f99be407 268{
10c56168
DG
269 struct cds_hlist_head *head;
270 struct cds_hlist_node *node;
f99be407 271 struct tracepoint_entry *e;
ff412fb5 272 uint32_t hash;
f99be407 273
34f7f142
MD
274 hash = jhash(provider_name, strlen(provider_name), 0) ^
275 jhash(event_name, strlen(event_name), 0);
f99be407 276 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 277 cds_hlist_for_each_entry(e, node, head, hlist) {
34f7f142 278 if (!strcmp(event_name, e->event_name) && !strcmp(provider_name, e->provider_name))
f99be407
PMF
279 return e;
280 }
281 return NULL;
282}
283
284/*
285 * Add the tracepoint to the tracepoint hash table. Must be called with
8792fbae 286 * tracepoint mutex held.
f99be407 287 */
34f7f142 288static struct tracepoint_entry *add_tracepoint(const char *provider_name, const char *event_name,
67e5f391 289 const char *signature)
f99be407 290{
10c56168
DG
291 struct cds_hlist_head *head;
292 struct cds_hlist_node *node;
f99be407 293 struct tracepoint_entry *e;
2c05c691 294 size_t sig_len = strlen(signature);
34f7f142
MD
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);
ff412fb5 298 uint32_t hash;
f99be407 299
34f7f142
MD
300 hash = jhash(provider_name, provider_name_len, 0) ^
301 jhash(event_name, event_name_len, 0);
f99be407 302 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 303 cds_hlist_for_each_entry(e, node, head, hlist) {
34f7f142
MD
304 if (!strcmp(event_name, e->event_name) && !strcmp(provider_name, e->provider_name)) {
305 DBG("tracepoint \"%s:%s\" busy", provider_name, event_name);
f99be407
PMF
306 return ERR_PTR(-EEXIST); /* Already there */
307 }
308 }
2c05c691 309
f99be407 310 /*
2c05c691
FD
311 * Using zmalloc here to allocate a variable length elements: name and
312 * signature. Could cause some memory fragmentation if overused.
f99be407 313 */
34f7f142
MD
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;
2c05c691 317
34f7f142
MD
318 e = zmalloc(sizeof(struct tracepoint_entry) + provider_name_len + 1 +
319 event_name_len + 1 + sig_len + 1);
f99be407
PMF
320 if (!e)
321 return ERR_PTR(-ENOMEM);
34f7f142
MD
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';
2c05c691
FD
329
330 e->signature = (char *) e + sig_off;
331 memcpy(e->signature, signature, sig_len + 1);
332 e->signature[sig_len] = '\0';
333
9dec086e 334 e->probes = NULL;
f99be407 335 e->refcount = 0;
8a7ad54b 336 e->callsite_refcount = 0;
2c05c691 337
10c56168 338 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
339 return e;
340}
341
342/*
343 * Remove the tracepoint from the tracepoint hash table. Must be called with
8792fbae 344 * tracepoint mutex held.
f99be407 345 */
efa2c591 346static void remove_tracepoint(struct tracepoint_entry *e)
f99be407 347{
10c56168 348 cds_hlist_del(&e->hlist);
909bc43f 349 free(e);
f99be407
PMF
350}
351
352/*
353 * Sets the probe callback corresponding to one tracepoint.
354 */
355static void set_tracepoint(struct tracepoint_entry **entry,
1a206094 356 struct lttng_ust_tracepoint *elem, int active)
f99be407 357{
34f7f142
MD
358 WARN_ON(strcmp((*entry)->provider_name, elem->provider_name) != 0);
359 WARN_ON(strcmp((*entry)->event_name, elem->event_name) != 0);
67e5f391
MD
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.");
34f7f142
MD
370 WARN("Tracepoint \"%s:%s\" signatures: call: \"%s\" vs probe: \"%s\".",
371 elem->provider_name, elem->event_name, elem->signature, (*entry)->signature);
67e5f391
MD
372 warned = 1;
373 }
374 /* Don't accept connecting non-matching signatures. */
375 return;
376 }
f99be407
PMF
377
378 /*
0222e121 379 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
380 * probe callbacks array is consistent before setting a pointer to it.
381 * This array is referenced by __DO_TRACE from
0222e121 382 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
383 * is used.
384 */
10544ee8 385 lttng_ust_rcu_assign_pointer(elem->probes, (*entry)->probes);
c082d14b 386 CMM_STORE_SHARED(elem->state, active);
f99be407
PMF
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 */
1a206094 395static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
f99be407 396{
c082d14b 397 CMM_STORE_SHARED(elem->state, 0);
10544ee8 398 lttng_ust_rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
399}
400
33f8ed87
MD
401/*
402 * Add the callsite to the callsite hash table. Must be called with
403 * tracepoint mutex held.
404 */
1a206094 405static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
33f8ed87
MD
406{
407 struct cds_hlist_head *head;
408 struct callsite_entry *e;
33f8ed87 409 uint32_t hash;
8a7ad54b 410 struct tracepoint_entry *tp_entry;
33f8ed87 411
34f7f142
MD
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;
33f8ed87 416 }
34f7f142
MD
417 hash = jhash(tp->provider_name, strlen(tp->provider_name), 0) ^
418 jhash(tp->event_name, strlen(tp->event_name), 0);
33f8ed87
MD
419 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
420 e = zmalloc(sizeof(struct callsite_entry));
d6297168 421 if (!e) {
34f7f142 422 PERROR("Unable to add callsite for tracepoint \"%s:%s\"", tp->provider_name, tp->event_name);
d6297168
MD
423 return;
424 }
33f8ed87
MD
425 cds_hlist_add_head(&e->hlist, head);
426 e->tp = tp;
427 cds_list_add(&e->node, &lib->callsites);
8a7ad54b 428
34f7f142 429 tp_entry = get_tracepoint(tp->provider_name, tp->event_name);
8a7ad54b
IJ
430 if (!tp_entry)
431 return;
432 tp_entry->callsite_refcount++;
2c05c691 433 e->tp_entry_callsite_ref = true;
33f8ed87
MD
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 */
440static void remove_callsite(struct callsite_entry *e)
441{
8a7ad54b
IJ
442 struct tracepoint_entry *tp_entry;
443
34f7f142 444 tp_entry = get_tracepoint(e->tp->provider_name, e->tp->event_name);
8a7ad54b 445 if (tp_entry) {
2c05c691
FD
446 if (e->tp_entry_callsite_ref)
447 tp_entry->callsite_refcount--;
8a7ad54b
IJ
448 if (tp_entry->callsite_refcount == 0)
449 disable_tracepoint(e->tp);
450 }
33f8ed87
MD
451 cds_hlist_del(&e->hlist);
452 cds_list_del(&e->node);
453 free(e);
454}
455
3469bbbe
MD
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 */
34f7f142 461static void tracepoint_sync_callsites(const char *provider_name, const char *event_name)
3469bbbe 462{
34f7f142 463 struct tracepoint_entry *tp_entry;
3469bbbe
MD
464 struct cds_hlist_head *head;
465 struct cds_hlist_node *node;
466 struct callsite_entry *e;
3469bbbe 467 uint32_t hash;
3469bbbe 468
34f7f142
MD
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);
3469bbbe
MD
472 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
473 cds_hlist_for_each_entry(e, node, head, hlist) {
1a206094 474 struct lttng_ust_tracepoint *tp = e->tp;
3469bbbe 475
34f7f142
MD
476 if (strcmp(event_name, tp->event_name))
477 continue;
478 if (strcmp(provider_name, tp->provider_name))
3469bbbe
MD
479 continue;
480 if (tp_entry) {
2c05c691
FD
481 if (!e->tp_entry_callsite_ref) {
482 tp_entry->callsite_refcount++;
483 e->tp_entry_callsite_ref = true;
484 }
3469bbbe
MD
485 set_tracepoint(&tp_entry, tp,
486 !!tp_entry->refcount);
487 } else {
488 disable_tracepoint(tp);
2c05c691 489 e->tp_entry_callsite_ref = false;
3469bbbe
MD
490 }
491 }
492}
493
f99be407
PMF
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 */
b27f8e75 501static
1a206094
SM
502void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
503 struct lttng_ust_tracepoint * const *end)
f99be407 504{
1a206094 505 struct lttng_ust_tracepoint * const *iter;
f99be407
PMF
506 struct tracepoint_entry *mark_entry;
507
f99be407 508 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
509 if (!*iter)
510 continue; /* skip dummy */
34f7f142
MD
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);
f218ff28 518 disable_tracepoint(*iter);
9dec086e
NC
519 continue;
520 }
34f7f142 521 mark_entry = get_tracepoint((*iter)->provider_name, (*iter)->event_name);
f99be407 522 if (mark_entry) {
f218ff28 523 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
524 !!mark_entry->refcount);
525 } else {
f218ff28 526 disable_tracepoint(*iter);
f99be407
PMF
527 }
528 }
f99be407
PMF
529}
530
5da10905 531static void lib_update_tracepoints(struct tracepoint_lib *lib)
772030fe 532{
5da10905
MD
533 tracepoint_update_probe_range(lib->tracepoints_start,
534 lib->tracepoints_start + lib->tracepoints_count);
772030fe
PMF
535}
536
3469bbbe
MD
537static void lib_register_callsites(struct tracepoint_lib *lib)
538{
1a206094
SM
539 struct lttng_ust_tracepoint * const *begin;
540 struct lttng_ust_tracepoint * const *end;
541 struct lttng_ust_tracepoint * const *iter;
3469bbbe
MD
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 */
34f7f142 549 if (!(*iter)->provider_name || !(*iter)->event_name) {
3469bbbe
MD
550 continue;
551 }
60d87029 552 add_callsite(lib, *iter);
3469bbbe
MD
553 }
554}
555
556static 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
f99be407
PMF
564/*
565 * Update probes, removing the faulty probes.
566 */
567static void tracepoint_update_probes(void)
568{
5da10905
MD
569 struct tracepoint_lib *lib;
570
b27f8e75 571 /* tracepoints registered from libraries and executable. */
5da10905
MD
572 cds_list_for_each_entry(lib, &libs, list)
573 lib_update_tracepoints(lib);
f99be407
PMF
574}
575
1a206094 576static struct lttng_ust_tracepoint_probe *
34f7f142
MD
577tracepoint_add_probe(const char *provider_name, const char *event_name,
578 void (*probe)(void), void *data, const char *signature)
f99be407
PMF
579{
580 struct tracepoint_entry *entry;
1a206094 581 struct lttng_ust_tracepoint_probe *old;
f99be407 582
34f7f142 583 entry = get_tracepoint(provider_name, event_name);
2c05c691
FD
584 if (entry) {
585 if (strcmp(entry->signature, signature) != 0) {
34f7f142
MD
586 ERR("Tracepoint \"%s:%s\": tracepoint and probe signature do not match: \"%s\" vs \"%s\".",
587 provider_name, event_name, entry->signature, signature);
2c05c691
FD
588 return ERR_PTR(-EINVAL);
589 }
590 } else {
34f7f142 591 entry = add_tracepoint(provider_name, event_name, signature);
f99be407 592 if (IS_ERR(entry))
1a206094 593 return (struct lttng_ust_tracepoint_probe *)entry;
f99be407 594 }
9dec086e 595 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
596 if (IS_ERR(old) && !entry->refcount)
597 remove_tracepoint(entry);
598 return old;
599}
600
baa1e0bc
MD
601static 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
6f78600e
MD
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
617static
618void 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 */
634int tracepoint_register_lib(void *arg0 __attribute__((unused)), int arg1 __attribute__((unused)));
635int 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
f99be407 644/**
9b34a2b2 645 * lttng_ust_tracepoint_provider_register - Connect a probe to a tracepoint
34f7f142
MD
646 * @name: tracepoint provider name
647 * @name: tracepoint event name
f99be407
PMF
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.
8792fbae 652 * Called with the tracepoint mutex held.
f99be407 653 */
9b34a2b2 654int lttng_ust_tracepoint_provider_register(const char *provider_name, const char *event_name,
34f7f142 655 void (*probe)(void), void *data, const char *signature)
f99be407
PMF
656{
657 void *old;
8792fbae 658 int ret = 0;
f99be407 659
6f78600e
MD
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
34f7f142 668 DBG("Registering probe to tracepoint \"%s:%s\"", provider_name, event_name);
05780d81 669
8792fbae 670 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 671 old = tracepoint_add_probe(provider_name, event_name, probe, data, signature);
8792fbae
MD
672 if (IS_ERR(old)) {
673 ret = PTR_ERR(old);
674 goto end;
675 }
f99be407 676
34f7f142 677 tracepoint_sync_callsites(provider_name, event_name);
f99be407 678 release_probes(old);
8792fbae
MD
679end:
680 pthread_mutex_unlock(&tracepoint_mutex);
681 return ret;
f99be407 682}
f99be407 683
baa1e0bc 684/*
0274f7f2 685 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
d10a2205 686 * calling lttng_ust_tp_probe_register_queue_release() one or multiple
baa1e0bc
MD
687 * times to ensure it does not leak memory.
688 */
34f7f142 689int lttng_ust_tp_probe_register_queue_release(const char *provider_name, const char *event_name,
baa1e0bc
MD
690 void (*probe)(void), void *data, const char *signature)
691{
692 void *old;
693 int ret = 0;
694
34f7f142 695 DBG("Registering probe to tracepoint \"%s:%s\". Queuing release.", provider_name, event_name);
baa1e0bc
MD
696
697 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 698 old = tracepoint_add_probe(provider_name, event_name, probe, data, signature);
baa1e0bc
MD
699 if (IS_ERR(old)) {
700 ret = PTR_ERR(old);
701 goto end;
702 }
703
34f7f142 704 tracepoint_sync_callsites(provider_name, event_name);
baa1e0bc
MD
705 tracepoint_release_queue_add_old_probes(old);
706end:
707 pthread_mutex_unlock(&tracepoint_mutex);
708 return ret;
709}
710
34f7f142
MD
711static void *tracepoint_remove_probe(const char *provider_name, const char *event_name,
712 void (*probe)(void), void *data)
f99be407
PMF
713{
714 struct tracepoint_entry *entry;
715 void *old;
716
34f7f142 717 entry = get_tracepoint(provider_name, event_name);
f99be407
PMF
718 if (!entry)
719 return ERR_PTR(-ENOENT);
9dec086e 720 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
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
34f7f142
MD
730 * @provider_name: tracepoint provider name
731 * @event_name: tracepoint event name
f99be407 732 * @probe: probe function pointer
9dec086e 733 * @probe: probe data pointer
f99be407 734 */
9b34a2b2 735int lttng_ust_tracepoint_provider_unregister(const char *provider_name, const char *event_name,
34f7f142 736 void (*probe)(void), void *data)
f99be407
PMF
737{
738 void *old;
8792fbae 739 int ret = 0;
f99be407 740
34f7f142 741 DBG("Un-registering probe \"%s:%s\" from tracepoint %p", provider_name, event_name, probe);
05780d81 742
8792fbae 743 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 744 old = tracepoint_remove_probe(provider_name, event_name, probe, data);
8792fbae
MD
745 if (IS_ERR(old)) {
746 ret = PTR_ERR(old);
747 goto end;
748 }
34f7f142 749 tracepoint_sync_callsites(provider_name, event_name);
f99be407 750 release_probes(old);
8792fbae
MD
751end:
752 pthread_mutex_unlock(&tracepoint_mutex);
753 return ret;
f99be407 754}
f99be407 755
baa1e0bc 756/*
0274f7f2 757 * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after
d10a2205 758 * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple
baa1e0bc
MD
759 * times to ensure it does not leak memory.
760 */
34f7f142 761int lttng_ust_tp_probe_unregister_queue_release(const char *provider_name, const char *event_name,
baa1e0bc
MD
762 void (*probe)(void), void *data)
763{
764 void *old;
765 int ret = 0;
766
34f7f142 767 DBG("Un-registering probe from tracepoint \"%s:%s\". Queuing release.", provider_name, event_name);
baa1e0bc
MD
768
769 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 770 old = tracepoint_remove_probe(provider_name, event_name, probe, data);
baa1e0bc
MD
771 if (IS_ERR(old)) {
772 ret = PTR_ERR(old);
773 goto end;
774 }
34f7f142 775 tracepoint_sync_callsites(provider_name, event_name);
baa1e0bc
MD
776 tracepoint_release_queue_add_old_probes(old);
777end:
778 pthread_mutex_unlock(&tracepoint_mutex);
779 return ret;
780}
781
d10a2205 782void lttng_ust_tp_probe_prune_release_queue(void)
baa1e0bc
MD
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. */
b653ddc1 797 lttng_ust_urcu_synchronize_rcu();
baa1e0bc
MD
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 }
803end:
804 pthread_mutex_unlock(&tracepoint_mutex);
805}
806
f99be407
PMF
807static void tracepoint_add_old_probes(void *old)
808{
809 need_update = 1;
810 if (old) {
b728d87e 811 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 812 struct tp_probes, probes[0]);
0222e121 813 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
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 */
34f7f142
MD
824int tracepoint_probe_register_noupdate(const char *provider_name, const char *event_name,
825 void (*probe)(void), void *data, const char *signature)
f99be407
PMF
826{
827 void *old;
8792fbae 828 int ret = 0;
f99be407 829
8792fbae 830 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 831 old = tracepoint_add_probe(provider_name, event_name, probe, data, signature);
f99be407 832 if (IS_ERR(old)) {
8792fbae
MD
833 ret = PTR_ERR(old);
834 goto end;
f99be407
PMF
835 }
836 tracepoint_add_old_probes(old);
8792fbae
MD
837end:
838 pthread_mutex_unlock(&tracepoint_mutex);
839 return ret;
f99be407 840}
f99be407
PMF
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()
8792fbae 848 * Called with the tracepoint mutex held.
f99be407 849 */
34f7f142
MD
850int tracepoint_probe_unregister_noupdate(const char *provider_name, const char *event_name,
851 void (*probe)(void), void *data)
f99be407
PMF
852{
853 void *old;
8792fbae 854 int ret = 0;
f99be407 855
34f7f142 856 DBG("Un-registering probe from tracepoint \"%s:%s\"", provider_name, event_name);
05780d81 857
8792fbae 858 pthread_mutex_lock(&tracepoint_mutex);
34f7f142 859 old = tracepoint_remove_probe(provider_name, event_name, probe, data);
f99be407 860 if (IS_ERR(old)) {
8792fbae
MD
861 ret = PTR_ERR(old);
862 goto end;
f99be407
PMF
863 }
864 tracepoint_add_old_probes(old);
8792fbae
MD
865end:
866 pthread_mutex_unlock(&tracepoint_mutex);
867 return ret;
f99be407 868}
f99be407
PMF
869
870/**
871 * tracepoint_probe_update_all - update tracepoints
872 */
873void tracepoint_probe_update_all(void)
874{
0222e121 875 CDS_LIST_HEAD(release_probes);
f99be407
PMF
876 struct tp_probes *pos, *next;
877
8792fbae 878 pthread_mutex_lock(&tracepoint_mutex);
f99be407 879 if (!need_update) {
8792fbae 880 goto end;
f99be407 881 }
0222e121
MD
882 if (!cds_list_empty(&old_probes))
883 cds_list_replace_init(&old_probes, &release_probes);
f99be407 884 need_update = 0;
f99be407
PMF
885
886 tracepoint_update_probes();
baa1e0bc 887 /* Wait for grace period between update_probes and free. */
b653ddc1 888 lttng_ust_urcu_synchronize_rcu();
0222e121
MD
889 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
890 cds_list_del(&pos->u.list);
909bc43f 891 free(pos);
f99be407 892 }
8792fbae
MD
893end:
894 pthread_mutex_unlock(&tracepoint_mutex);
f99be407 895}
f99be407 896
1a206094
SM
897static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
898 struct lttng_ust_tracepoint * const *end)
f99be407 899{
f218ff28 900 if (new_tracepoint_cb) {
1a206094 901 struct lttng_ust_tracepoint * const *t;
f08ebbe2 902
b27f8e75 903 for (t = start; t < end; t++) {
f08ebbe2
MD
904 if (*t)
905 new_tracepoint_cb(*t);
474d745f
PMF
906 }
907 }
f99be407 908}
f99be407 909
10544ee8
MD
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 */
14227bde 922int lttng_ust_tracepoint_module_register(struct lttng_ust_tracepoint * const *tracepoints_start,
4b4a1337 923 int tracepoints_count);
14227bde 924int lttng_ust_tracepoint_module_register(struct lttng_ust_tracepoint * const *tracepoints_start,
10544ee8 925 int tracepoints_count)
474d745f 926{
b467f7a7 927 struct tracepoint_lib *pl, *iter;
474d745f 928
25afb7ac 929 lttng_ust_tp_init();
edaa1431 930
1dba3e6c 931 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
d6297168
MD
932 if (!pl) {
933 PERROR("Unable to register tracepoint lib");
934 return -1;
935 }
474d745f
PMF
936 pl->tracepoints_start = tracepoints_start;
937 pl->tracepoints_count = tracepoints_count;
3469bbbe 938 CDS_INIT_LIST_HEAD(&pl->callsites);
474d745f 939
8792fbae 940 pthread_mutex_lock(&tracepoint_mutex);
b467f7a7
MD
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 */
0222e121 953 cds_list_add(&pl->list, &libs);
b467f7a7 954lib_added:
474d745f 955 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
3469bbbe 956 lib_register_callsites(pl);
5da10905 957 lib_update_tracepoints(pl);
8792fbae 958 pthread_mutex_unlock(&tracepoint_mutex);
474d745f 959
1fcf7ad7
MD
960 DBG("just registered a tracepoints section from %p and having %d tracepoints",
961 tracepoints_start, tracepoints_count);
c6e6343c 962 if (lttng_ust_logging_debug_enabled()) {
05780d81
MD
963 int i;
964
965 for (i = 0; i < tracepoints_count; i++) {
34f7f142
MD
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);
05780d81
MD
971 }
972 }
9dec086e 973
474d745f
PMF
974 return 0;
975}
976
14227bde
MJ
977int lttng_ust_tracepoint_module_unregister(struct lttng_ust_tracepoint * const *tracepoints_start);
978int lttng_ust_tracepoint_module_unregister(struct lttng_ust_tracepoint * const *tracepoints_start)
474d745f 979{
24b6668c
PMF
980 struct tracepoint_lib *lib;
981
8792fbae 982 pthread_mutex_lock(&tracepoint_mutex);
0222e121 983 cds_list_for_each_entry(lib, &libs, list) {
3469bbbe
MD
984 if (lib->tracepoints_start != tracepoints_start)
985 continue;
1622ba22 986
3469bbbe
MD
987 cds_list_del(&lib->list);
988 /*
8a7ad54b
IJ
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.
3469bbbe 993 */
3469bbbe
MD
994 lib_unregister_callsites(lib);
995 DBG("just unregistered a tracepoints section from %p",
996 lib->tracepoints_start);
997 free(lib);
998 break;
24b6668c 999 }
8792fbae 1000 pthread_mutex_unlock(&tracepoint_mutex);
474d745f
PMF
1001 return 0;
1002}
b27f8e75 1003
5517d34d
MD
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 */
1010static void check_weak_hidden(void)
1011{
b0e63efd 1012 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
0274f7f2 1013 &lttng_ust_tracepoint_test_symbol1 == lttng_ust_tp_check_weak_hidden1() ?
b0e63efd
MD
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.",
0274f7f2 1017 &lttng_ust_tracepoint_test_symbol2 == lttng_ust_tp_check_weak_hidden2() ?
b0e63efd
MD
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.",
0274f7f2 1021 &lttng_ust_tracepoint_test_symbol3 == lttng_ust_tp_check_weak_hidden3() ?
b0e63efd
MD
1022 "SAME address" :
1023 "DIFFERENT addresses");
5517d34d
MD
1024}
1025
25afb7ac 1026void lttng_ust_tp_init(void)
b27f8e75 1027{
edaa1431
MD
1028 if (uatomic_xchg(&initialized, 1) == 1)
1029 return;
c6e6343c 1030 lttng_ust_logging_init();
fca97dfd 1031 lttng_ust_common_ctor();
5517d34d 1032 check_weak_hidden();
b27f8e75
MD
1033}
1034
25afb7ac 1035void lttng_ust_tp_exit(void)
b27f8e75 1036{
17dfb34b 1037 initialized = 0;
b27f8e75 1038}
40b2b5a4
MD
1039
1040/*
1041 * Create the wrapper symbols.
1042 */
251c9f75
MJ
1043#undef lttng_ust_tp_rcu_read_lock
1044#undef lttng_ust_tp_rcu_read_unlock
1045#undef lttng_ust_tp_rcu_dereference
40b2b5a4 1046
251c9f75
MJ
1047void lttng_ust_tp_rcu_read_lock(void);
1048void lttng_ust_tp_rcu_read_lock(void)
40b2b5a4 1049{
10544ee8 1050 lttng_ust_urcu_read_lock();
40b2b5a4
MD
1051}
1052
251c9f75
MJ
1053void lttng_ust_tp_rcu_read_unlock(void);
1054void lttng_ust_tp_rcu_read_unlock(void)
40b2b5a4 1055{
10544ee8 1056 lttng_ust_urcu_read_unlock();
40b2b5a4
MD
1057}
1058
251c9f75
MJ
1059void *lttng_ust_tp_rcu_dereference_sym(void *p);
1060void *lttng_ust_tp_rcu_dereference_sym(void *p)
40b2b5a4 1061{
10544ee8 1062 return lttng_ust_rcu_dereference(p);
40b2b5a4 1063}
0fd0de10
FD
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
096c06e5 1068 * lttng_ust_tp_disable_destructors(). This will leak the tracepoint
0fd0de10
FD
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
096c06e5 1074 * lttng_ust_tp_disable_destructors and lttng_ust_tp_get_destructors_state symbols below.
0fd0de10 1075 */
096c06e5
MJ
1076void lttng_ust_tp_disable_destructors(void);
1077void lttng_ust_tp_disable_destructors(void)
0fd0de10
FD
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 */
096c06e5
MJ
1086int lttng_ust_tp_get_destructors_state(void);
1087int lttng_ust_tp_get_destructors_state(void)
0fd0de10
FD
1088{
1089 return uatomic_read(&tracepoint_destructors_state);
1090}
This page took 0.099045 seconds and 4 git commands to generate.