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