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