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