Fix: many-events registration/unregistration speed
[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>
b7ea1a1c 29#include <urcu-bp.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;
1a206094 56static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
f99be407 57
8792fbae
MD
58/*
59 * tracepoint_mutex nests inside UST mutex.
60 *
61 * Note about interaction with fork/clone: UST does not hold the
62 * tracepoint mutex across fork/clone because it is either:
63 * - nested within UST mutex, in which case holding the UST mutex across
64 * fork/clone suffice,
65 * - taken by a library constructor, which should never race with a
66 * fork/clone if the application is expected to continue running with
67 * the same memory layout (no following exec()).
68 */
69static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
70
efa2c591
MD
71/*
72 * libraries that contain tracepoints (struct tracepoint_lib).
8792fbae 73 * Protected by tracepoint mutex.
efa2c591 74 */
0222e121 75static CDS_LIST_HEAD(libs);
474d745f 76
f99be407 77/*
8792fbae 78 * The tracepoint mutex protects the library tracepoints, the hash table, and
17dfb34b 79 * the library list.
8792fbae 80 * All calls to the tracepoint API must be protected by the tracepoint mutex,
17dfb34b 81 * excepts calls to tracepoint_register_lib and
8792fbae 82 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
f99be407 83 */
f99be407
PMF
84
85/*
86 * Tracepoint hash table, containing the active tracepoints.
8792fbae 87 * Protected by tracepoint mutex.
f99be407 88 */
814f7df1 89#define TRACEPOINT_HASH_BITS 12
f99be407 90#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
10c56168 91static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 92
b27f8e75
MD
93static CDS_LIST_HEAD(old_probes);
94static int need_update;
95
baa1e0bc
MD
96static CDS_LIST_HEAD(release_queue);
97static int release_queue_need_update;
98
f99be407
PMF
99/*
100 * Note about RCU :
101 * It is used to to delay the free of multiple probes array until a quiescent
102 * state is reached.
8792fbae 103 * Tracepoint entries modifications are protected by the tracepoint mutex.
f99be407
PMF
104 */
105struct tracepoint_entry {
10c56168 106 struct cds_hlist_node hlist;
1a206094 107 struct lttng_ust_tracepoint_probe *probes;
f99be407 108 int refcount; /* Number of times armed. 0 if disarmed. */
8a7ad54b 109 int callsite_refcount; /* how many libs use this tracepoint */
67e5f391 110 const char *signature;
f99be407
PMF
111 char name[0];
112};
113
114struct tp_probes {
115 union {
0222e121 116 struct cds_list_head list;
ade7037b
MD
117 /* Field below only used for call_rcu scheme */
118 /* struct rcu_head head; */
f99be407 119 } u;
1a206094 120 struct lttng_ust_tracepoint_probe probes[0];
f99be407
PMF
121};
122
3469bbbe
MD
123/*
124 * Callsite hash table, containing the tracepoint call sites.
125 * Protected by tracepoint mutex.
126 */
127#define CALLSITE_HASH_BITS 12
128#define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
129static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
130
131struct callsite_entry {
132 struct cds_hlist_node hlist; /* hash table node */
133 struct cds_list_head node; /* lib list of callsites node */
1a206094 134 struct lttng_ust_tracepoint *tp;
3469bbbe
MD
135};
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]);
474d745f 152 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
ff412fb5
MD
263 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
264 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
265 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
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) {
ff412fb5 270 if (!strncmp(name, e->name, LTTNG_UST_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
MD
286 size_t name_len = strlen(name);
287 uint32_t hash;
f99be407 288
ff412fb5
MD
289 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
290 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
291 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
292 }
293 hash = jhash(name, name_len, 0);
f99be407 294 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 295 cds_hlist_for_each_entry(e, node, head, hlist) {
ff412fb5 296 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
c1f20530 297 DBG("tracepoint %s busy", name);
f99be407
PMF
298 return ERR_PTR(-EEXIST); /* Already there */
299 }
300 }
301 /*
1dba3e6c 302 * Using zmalloc here to allocate a variable length element. Could
f99be407
PMF
303 * cause some memory fragmentation if overused.
304 */
ff412fb5 305 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
f99be407
PMF
306 if (!e)
307 return ERR_PTR(-ENOMEM);
ff412fb5
MD
308 memcpy(&e->name[0], name, name_len + 1);
309 e->name[name_len] = '\0';
9dec086e 310 e->probes = NULL;
f99be407 311 e->refcount = 0;
8a7ad54b 312 e->callsite_refcount = 0;
67e5f391 313 e->signature = signature;
10c56168 314 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
315 return e;
316}
317
318/*
319 * Remove the tracepoint from the tracepoint hash table. Must be called with
8792fbae 320 * tracepoint mutex held.
f99be407 321 */
efa2c591 322static void remove_tracepoint(struct tracepoint_entry *e)
f99be407 323{
10c56168 324 cds_hlist_del(&e->hlist);
909bc43f 325 free(e);
f99be407
PMF
326}
327
328/*
329 * Sets the probe callback corresponding to one tracepoint.
330 */
331static void set_tracepoint(struct tracepoint_entry **entry,
1a206094 332 struct lttng_ust_tracepoint *elem, int active)
f99be407 333{
ff412fb5 334 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
67e5f391
MD
335 /*
336 * Check that signatures match before connecting a probe to a
337 * tracepoint. Warn the user if they don't.
338 */
339 if (strcmp(elem->signature, (*entry)->signature) != 0) {
340 static int warned = 0;
341
342 /* Only print once, don't flood console. */
343 if (!warned) {
344 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
345 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
346 elem->name, elem->signature, (*entry)->signature);
347 warned = 1;
348 }
349 /* Don't accept connecting non-matching signatures. */
350 return;
351 }
f99be407
PMF
352
353 /*
0222e121 354 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
355 * probe callbacks array is consistent before setting a pointer to it.
356 * This array is referenced by __DO_TRACE from
0222e121 357 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
358 * is used.
359 */
9dec086e 360 rcu_assign_pointer(elem->probes, (*entry)->probes);
c082d14b 361 CMM_STORE_SHARED(elem->state, active);
f99be407
PMF
362}
363
364/*
365 * Disable a tracepoint and its probe callback.
366 * Note: only waiting an RCU period after setting elem->call to the empty
367 * function insures that the original callback is not used anymore. This insured
368 * by preempt_disable around the call site.
369 */
1a206094 370static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
f99be407 371{
c082d14b 372 CMM_STORE_SHARED(elem->state, 0);
9dec086e 373 rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
374}
375
33f8ed87
MD
376/*
377 * Add the callsite to the callsite hash table. Must be called with
378 * tracepoint mutex held.
379 */
1a206094 380static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
33f8ed87
MD
381{
382 struct cds_hlist_head *head;
383 struct callsite_entry *e;
384 const char *name = tp->name;
385 size_t name_len = strlen(name);
386 uint32_t hash;
8a7ad54b 387 struct tracepoint_entry *tp_entry;
33f8ed87
MD
388
389 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
390 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
391 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
392 }
393 hash = jhash(name, name_len, 0);
394 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
395 e = zmalloc(sizeof(struct callsite_entry));
d6297168
MD
396 if (!e) {
397 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
398 return;
399 }
33f8ed87
MD
400 cds_hlist_add_head(&e->hlist, head);
401 e->tp = tp;
402 cds_list_add(&e->node, &lib->callsites);
8a7ad54b
IJ
403
404 tp_entry = get_tracepoint(name);
405 if (!tp_entry)
406 return;
407 tp_entry->callsite_refcount++;
33f8ed87
MD
408}
409
410/*
411 * Remove the callsite from the callsite hash table and from lib
412 * callsite list. Must be called with tracepoint mutex held.
413 */
414static void remove_callsite(struct callsite_entry *e)
415{
8a7ad54b
IJ
416 struct tracepoint_entry *tp_entry;
417
418 tp_entry = get_tracepoint(e->tp->name);
419 if (tp_entry) {
420 tp_entry->callsite_refcount--;
421 if (tp_entry->callsite_refcount == 0)
422 disable_tracepoint(e->tp);
423 }
33f8ed87
MD
424 cds_hlist_del(&e->hlist);
425 cds_list_del(&e->node);
426 free(e);
427}
428
3469bbbe
MD
429/*
430 * Enable/disable all callsites based on the state of a specific
431 * tracepoint entry.
432 * Must be called with tracepoint mutex held.
433 */
434static void tracepoint_sync_callsites(const char *name)
435{
436 struct cds_hlist_head *head;
437 struct cds_hlist_node *node;
438 struct callsite_entry *e;
439 size_t name_len = strlen(name);
440 uint32_t hash;
441 struct tracepoint_entry *tp_entry;
442
443 tp_entry = get_tracepoint(name);
444 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
445 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
446 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
447 }
448 hash = jhash(name, name_len, 0);
449 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
450 cds_hlist_for_each_entry(e, node, head, hlist) {
1a206094 451 struct lttng_ust_tracepoint *tp = e->tp;
3469bbbe
MD
452
453 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
454 continue;
455 if (tp_entry) {
456 set_tracepoint(&tp_entry, tp,
457 !!tp_entry->refcount);
458 } else {
459 disable_tracepoint(tp);
460 }
461 }
462}
463
f99be407
PMF
464/**
465 * tracepoint_update_probe_range - Update a probe range
466 * @begin: beginning of the range
467 * @end: end of the range
468 *
469 * Updates the probe callback corresponding to a range of tracepoints.
470 */
b27f8e75 471static
1a206094
SM
472void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
473 struct lttng_ust_tracepoint * const *end)
f99be407 474{
1a206094 475 struct lttng_ust_tracepoint * const *iter;
f99be407
PMF
476 struct tracepoint_entry *mark_entry;
477
f99be407 478 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
479 if (!*iter)
480 continue; /* skip dummy */
f218ff28
MD
481 if (!(*iter)->name) {
482 disable_tracepoint(*iter);
9dec086e
NC
483 continue;
484 }
f218ff28 485 mark_entry = get_tracepoint((*iter)->name);
f99be407 486 if (mark_entry) {
f218ff28 487 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
488 !!mark_entry->refcount);
489 } else {
f218ff28 490 disable_tracepoint(*iter);
f99be407
PMF
491 }
492 }
f99be407
PMF
493}
494
5da10905 495static void lib_update_tracepoints(struct tracepoint_lib *lib)
772030fe 496{
5da10905
MD
497 tracepoint_update_probe_range(lib->tracepoints_start,
498 lib->tracepoints_start + lib->tracepoints_count);
772030fe
PMF
499}
500
3469bbbe
MD
501static void lib_register_callsites(struct tracepoint_lib *lib)
502{
1a206094
SM
503 struct lttng_ust_tracepoint * const *begin;
504 struct lttng_ust_tracepoint * const *end;
505 struct lttng_ust_tracepoint * const *iter;
3469bbbe
MD
506
507 begin = lib->tracepoints_start;
508 end = lib->tracepoints_start + lib->tracepoints_count;
509
510 for (iter = begin; iter < end; iter++) {
511 if (!*iter)
512 continue; /* skip dummy */
513 if (!(*iter)->name) {
514 continue;
515 }
60d87029 516 add_callsite(lib, *iter);
3469bbbe
MD
517 }
518}
519
520static void lib_unregister_callsites(struct tracepoint_lib *lib)
521{
522 struct callsite_entry *callsite, *tmp;
523
524 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
525 remove_callsite(callsite);
526}
527
f99be407
PMF
528/*
529 * Update probes, removing the faulty probes.
530 */
531static void tracepoint_update_probes(void)
532{
5da10905
MD
533 struct tracepoint_lib *lib;
534
b27f8e75 535 /* tracepoints registered from libraries and executable. */
5da10905
MD
536 cds_list_for_each_entry(lib, &libs, list)
537 lib_update_tracepoints(lib);
f99be407
PMF
538}
539
1a206094 540static struct lttng_ust_tracepoint_probe *
fbdeb5ec 541tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
67e5f391 542 const char *signature)
f99be407
PMF
543{
544 struct tracepoint_entry *entry;
1a206094 545 struct lttng_ust_tracepoint_probe *old;
f99be407
PMF
546
547 entry = get_tracepoint(name);
548 if (!entry) {
67e5f391 549 entry = add_tracepoint(name, signature);
f99be407 550 if (IS_ERR(entry))
1a206094 551 return (struct lttng_ust_tracepoint_probe *)entry;
f99be407 552 }
9dec086e 553 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
554 if (IS_ERR(old) && !entry->refcount)
555 remove_tracepoint(entry);
556 return old;
557}
558
baa1e0bc
MD
559static void tracepoint_release_queue_add_old_probes(void *old)
560{
561 release_queue_need_update = 1;
562 if (old) {
563 struct tp_probes *tp_probes = caa_container_of(old,
564 struct tp_probes, probes[0]);
565 cds_list_add(&tp_probes->u.list, &release_queue);
566 }
567}
568
f99be407 569/**
81614639 570 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
571 * @name: tracepoint name
572 * @probe: probe handler
573 *
574 * Returns 0 if ok, error value on error.
575 * The probe address must at least be aligned on the architecture pointer size.
8792fbae 576 * Called with the tracepoint mutex held.
f99be407 577 */
fbdeb5ec
MD
578int __tracepoint_probe_register(const char *name, void (*probe)(void),
579 void *data, const char *signature)
f99be407
PMF
580{
581 void *old;
8792fbae 582 int ret = 0;
f99be407 583
05780d81
MD
584 DBG("Registering probe to tracepoint %s", name);
585
8792fbae 586 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 587 old = tracepoint_add_probe(name, probe, data, signature);
8792fbae
MD
588 if (IS_ERR(old)) {
589 ret = PTR_ERR(old);
590 goto end;
591 }
f99be407 592
3469bbbe 593 tracepoint_sync_callsites(name);
f99be407 594 release_probes(old);
8792fbae
MD
595end:
596 pthread_mutex_unlock(&tracepoint_mutex);
597 return ret;
f99be407 598}
f99be407 599
baa1e0bc
MD
600/*
601 * Caller needs to invoke __tracepoint_probe_release_queue() after
602 * calling __tracepoint_probe_register_queue_release() one or multiple
603 * times to ensure it does not leak memory.
604 */
605int __tracepoint_probe_register_queue_release(const char *name,
606 void (*probe)(void), void *data, const char *signature)
607{
608 void *old;
609 int ret = 0;
610
611 DBG("Registering probe to tracepoint %s. Queuing release.", name);
612
613 pthread_mutex_lock(&tracepoint_mutex);
614 old = tracepoint_add_probe(name, probe, data, signature);
615 if (IS_ERR(old)) {
616 ret = PTR_ERR(old);
617 goto end;
618 }
619
620 tracepoint_sync_callsites(name);
621 tracepoint_release_queue_add_old_probes(old);
622end:
623 pthread_mutex_unlock(&tracepoint_mutex);
624 return ret;
625}
626
fbdeb5ec
MD
627static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
628 void *data)
f99be407
PMF
629{
630 struct tracepoint_entry *entry;
631 void *old;
632
633 entry = get_tracepoint(name);
634 if (!entry)
635 return ERR_PTR(-ENOENT);
9dec086e 636 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
637 if (IS_ERR(old))
638 return old;
639 if (!entry->refcount)
640 remove_tracepoint(entry);
641 return old;
642}
643
644/**
645 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
646 * @name: tracepoint name
647 * @probe: probe function pointer
9dec086e 648 * @probe: probe data pointer
f99be407 649 */
fbdeb5ec
MD
650int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
651 void *data)
f99be407
PMF
652{
653 void *old;
8792fbae 654 int ret = 0;
f99be407 655
05780d81
MD
656 DBG("Un-registering probe from tracepoint %s", name);
657
8792fbae 658 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 659 old = tracepoint_remove_probe(name, probe, data);
8792fbae
MD
660 if (IS_ERR(old)) {
661 ret = PTR_ERR(old);
662 goto end;
663 }
3469bbbe 664 tracepoint_sync_callsites(name);
f99be407 665 release_probes(old);
8792fbae
MD
666end:
667 pthread_mutex_unlock(&tracepoint_mutex);
668 return ret;
f99be407 669}
f99be407 670
baa1e0bc
MD
671/*
672 * Caller needs to invoke __tracepoint_probe_release_queue() after
673 * calling __tracepoint_probe_unregister_queue_release() one or multiple
674 * times to ensure it does not leak memory.
675 */
676int __tracepoint_probe_unregister_queue_release(const char *name,
677 void (*probe)(void), void *data)
678{
679 void *old;
680 int ret = 0;
681
682 DBG("Un-registering probe from tracepoint %s. Queuing release.", name);
683
684 pthread_mutex_lock(&tracepoint_mutex);
685 old = tracepoint_remove_probe(name, probe, data);
686 if (IS_ERR(old)) {
687 ret = PTR_ERR(old);
688 goto end;
689 }
690 tracepoint_sync_callsites(name);
691 tracepoint_release_queue_add_old_probes(old);
692end:
693 pthread_mutex_unlock(&tracepoint_mutex);
694 return ret;
695}
696
697void __tracepoint_probe_prune_release_queue(void)
698{
699 CDS_LIST_HEAD(release_probes);
700 struct tp_probes *pos, *next;
701
702 DBG("Release queue of unregistered tracepoint probes.");
703
704 pthread_mutex_lock(&tracepoint_mutex);
705 if (!release_queue_need_update)
706 goto end;
707 if (!cds_list_empty(&release_queue))
708 cds_list_replace_init(&release_queue, &release_probes);
709 release_queue_need_update = 0;
710
711 /* Wait for grace period between all sync_callsites and free. */
712 synchronize_rcu();
713
714 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
715 cds_list_del(&pos->u.list);
716 free(pos);
717 }
718end:
719 pthread_mutex_unlock(&tracepoint_mutex);
720}
721
f99be407
PMF
722static void tracepoint_add_old_probes(void *old)
723{
724 need_update = 1;
725 if (old) {
b728d87e 726 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 727 struct tp_probes, probes[0]);
0222e121 728 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
729 }
730}
731
732/**
733 * tracepoint_probe_register_noupdate - register a probe but not connect
734 * @name: tracepoint name
735 * @probe: probe handler
736 *
737 * caller must call tracepoint_probe_update_all()
738 */
fbdeb5ec 739int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
67e5f391 740 void *data, const char *signature)
f99be407
PMF
741{
742 void *old;
8792fbae 743 int ret = 0;
f99be407 744
8792fbae 745 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 746 old = tracepoint_add_probe(name, probe, data, signature);
f99be407 747 if (IS_ERR(old)) {
8792fbae
MD
748 ret = PTR_ERR(old);
749 goto end;
f99be407
PMF
750 }
751 tracepoint_add_old_probes(old);
8792fbae
MD
752end:
753 pthread_mutex_unlock(&tracepoint_mutex);
754 return ret;
f99be407 755}
f99be407
PMF
756
757/**
758 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
759 * @name: tracepoint name
760 * @probe: probe function pointer
761 *
762 * caller must call tracepoint_probe_update_all()
8792fbae 763 * Called with the tracepoint mutex held.
f99be407 764 */
fbdeb5ec 765int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
9dec086e 766 void *data)
f99be407
PMF
767{
768 void *old;
8792fbae 769 int ret = 0;
f99be407 770
05780d81
MD
771 DBG("Un-registering probe from tracepoint %s", name);
772
8792fbae 773 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 774 old = tracepoint_remove_probe(name, probe, data);
f99be407 775 if (IS_ERR(old)) {
8792fbae
MD
776 ret = PTR_ERR(old);
777 goto end;
f99be407
PMF
778 }
779 tracepoint_add_old_probes(old);
8792fbae
MD
780end:
781 pthread_mutex_unlock(&tracepoint_mutex);
782 return ret;
f99be407 783}
f99be407
PMF
784
785/**
786 * tracepoint_probe_update_all - update tracepoints
787 */
788void tracepoint_probe_update_all(void)
789{
0222e121 790 CDS_LIST_HEAD(release_probes);
f99be407
PMF
791 struct tp_probes *pos, *next;
792
8792fbae 793 pthread_mutex_lock(&tracepoint_mutex);
f99be407 794 if (!need_update) {
8792fbae 795 goto end;
f99be407 796 }
0222e121
MD
797 if (!cds_list_empty(&old_probes))
798 cds_list_replace_init(&old_probes, &release_probes);
f99be407 799 need_update = 0;
f99be407
PMF
800
801 tracepoint_update_probes();
baa1e0bc
MD
802 /* Wait for grace period between update_probes and free. */
803 synchronize_rcu();
0222e121
MD
804 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
805 cds_list_del(&pos->u.list);
909bc43f 806 free(pos);
f99be407 807 }
8792fbae
MD
808end:
809 pthread_mutex_unlock(&tracepoint_mutex);
f99be407 810}
f99be407 811
1a206094 812void tracepoint_set_new_tracepoint_cb(void (*cb)(struct lttng_ust_tracepoint *))
474d745f
PMF
813{
814 new_tracepoint_cb = cb;
815}
f99be407 816
1a206094
SM
817static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
818 struct lttng_ust_tracepoint * const *end)
f99be407 819{
f218ff28 820 if (new_tracepoint_cb) {
1a206094 821 struct lttng_ust_tracepoint * const *t;
f08ebbe2 822
b27f8e75 823 for (t = start; t < end; t++) {
f08ebbe2
MD
824 if (*t)
825 new_tracepoint_cb(*t);
474d745f
PMF
826 }
827 }
f99be407 828}
f99be407 829
1a206094 830int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
b27f8e75 831 int tracepoints_count)
474d745f 832{
b467f7a7 833 struct tracepoint_lib *pl, *iter;
474d745f 834
edaa1431
MD
835 init_tracepoint();
836
1dba3e6c 837 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
d6297168
MD
838 if (!pl) {
839 PERROR("Unable to register tracepoint lib");
840 return -1;
841 }
474d745f
PMF
842 pl->tracepoints_start = tracepoints_start;
843 pl->tracepoints_count = tracepoints_count;
3469bbbe 844 CDS_INIT_LIST_HEAD(&pl->callsites);
474d745f 845
8792fbae 846 pthread_mutex_lock(&tracepoint_mutex);
b467f7a7
MD
847 /*
848 * We sort the libs by struct lib pointer address.
849 */
850 cds_list_for_each_entry_reverse(iter, &libs, list) {
851 BUG_ON(iter == pl); /* Should never be in the list twice */
852 if (iter < pl) {
853 /* We belong to the location right after iter. */
854 cds_list_add(&pl->list, &iter->list);
855 goto lib_added;
856 }
857 }
858 /* We should be added at the head of the list */
0222e121 859 cds_list_add(&pl->list, &libs);
b467f7a7 860lib_added:
474d745f 861 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
3469bbbe 862 lib_register_callsites(pl);
5da10905 863 lib_update_tracepoints(pl);
8792fbae 864 pthread_mutex_unlock(&tracepoint_mutex);
474d745f 865
1fcf7ad7
MD
866 DBG("just registered a tracepoints section from %p and having %d tracepoints",
867 tracepoints_start, tracepoints_count);
05780d81
MD
868 if (ust_debug()) {
869 int i;
870
871 for (i = 0; i < tracepoints_count; i++) {
872 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
873 }
874 }
9dec086e 875
474d745f
PMF
876 return 0;
877}
878
1a206094 879int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start)
474d745f 880{
24b6668c
PMF
881 struct tracepoint_lib *lib;
882
8792fbae 883 pthread_mutex_lock(&tracepoint_mutex);
0222e121 884 cds_list_for_each_entry(lib, &libs, list) {
3469bbbe
MD
885 if (lib->tracepoints_start != tracepoints_start)
886 continue;
1622ba22 887
3469bbbe
MD
888 cds_list_del(&lib->list);
889 /*
8a7ad54b
IJ
890 * Unregistering a callsite also decreases the
891 * callsite reference count of the corresponding
892 * tracepoint, and disables the tracepoint if
893 * the reference count drops to zero.
3469bbbe 894 */
3469bbbe
MD
895 lib_unregister_callsites(lib);
896 DBG("just unregistered a tracepoints section from %p",
897 lib->tracepoints_start);
898 free(lib);
899 break;
24b6668c 900 }
8792fbae 901 pthread_mutex_unlock(&tracepoint_mutex);
474d745f
PMF
902 return 0;
903}
b27f8e75 904
5517d34d
MD
905/*
906 * Report in debug message whether the compiler correctly supports weak
907 * hidden symbols. This test checks that the address associated with two
908 * weak symbols with hidden visibility is the same when declared within
909 * two compile units part of the same module.
910 */
911static void check_weak_hidden(void)
912{
b0e63efd
MD
913 DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.",
914 &__tracepoint_test_symbol1 == lttng_ust_tp_check_weak_hidden1() ?
915 "SAME address" :
916 "DIFFERENT addresses");
917 DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.",
918 &__tracepoint_test_symbol2 == lttng_ust_tp_check_weak_hidden2() ?
919 "SAME address" :
920 "DIFFERENT addresses");
921 DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.",
922 &__tracepoint_test_symbol3 == lttng_ust_tp_check_weak_hidden3() ?
923 "SAME address" :
924 "DIFFERENT addresses");
5517d34d
MD
925}
926
edaa1431 927void init_tracepoint(void)
b27f8e75 928{
edaa1431
MD
929 if (uatomic_xchg(&initialized, 1) == 1)
930 return;
5e96a467 931 init_usterr();
5517d34d 932 check_weak_hidden();
b27f8e75
MD
933}
934
edaa1431 935void exit_tracepoint(void)
b27f8e75 936{
17dfb34b 937 initialized = 0;
b27f8e75 938}
40b2b5a4
MD
939
940/*
941 * Create the wrapper symbols.
942 */
943#undef tp_rcu_read_lock_bp
944#undef tp_rcu_read_unlock_bp
945#undef tp_rcu_dereference_bp
946
947void tp_rcu_read_lock_bp(void)
948{
949 rcu_read_lock_bp();
950}
951
952void tp_rcu_read_unlock_bp(void)
953{
954 rcu_read_unlock_bp();
955}
956
957void *tp_rcu_dereference_sym_bp(void *p)
958{
959 return rcu_dereference_bp(p);
960}
This page took 0.094207 seconds and 5 git commands to generate.