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