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