Remove runtime dependency on liburcu shared objects
[lttng-ust.git] / liblttng-ust / tracepoint.c
1 /*
2 * Copyright (C) 2008-2011 Mathieu Desnoyers
3 * Copyright (C) 2009 Pierre-Marc Fournier
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
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
18 *
19 * Ported to userspace by Pierre-Marc Fournier.
20 */
21
22 #define _LGPL_SOURCE
23 #include <errno.h>
24 #include <stdint.h>
25 #include <stddef.h>
26 #include <stdio.h>
27
28 #include <urcu/arch.h>
29 #include <lttng/urcu/urcu-ust.h>
30 #include <urcu/hlist.h>
31 #include <urcu/uatomic.h>
32 #include <urcu/compiler.h>
33 #include <urcu/system.h>
34
35 #include <lttng/tracepoint.h>
36 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
37
38 #include <usterr-signal-safe.h>
39 #include <helper.h>
40
41 #include "tracepoint-internal.h"
42 #include "lttng-tracer-core.h"
43 #include "jhash.h"
44 #include "error.h"
45
46 /* Test compiler support for weak symbols with hidden visibility. */
47 int __tracepoint_test_symbol1 __attribute__((weak, visibility("hidden")));
48 void *__tracepoint_test_symbol2 __attribute__((weak, visibility("hidden")));
49 struct {
50 char a[24];
51 } __tracepoint_test_symbol3 __attribute__((weak, visibility("hidden")));
52
53 /* Set to 1 to enable tracepoint debug output */
54 static const int tracepoint_debug;
55 static int initialized;
56
57 /*
58 * If tracepoint_destructors_state = 1, tracepoint destructors are
59 * enabled. They are disabled otherwise.
60 */
61 static 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 */
70 int __tracepoints__disable_destructors __attribute__((weak));
71
72 static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
73
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 */
85 static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
86
87 /*
88 * libraries that contain tracepoints (struct tracepoint_lib).
89 * Protected by tracepoint mutex.
90 */
91 static CDS_LIST_HEAD(libs);
92
93 /*
94 * The tracepoint mutex protects the library tracepoints, the hash table, and
95 * the library list.
96 * All calls to the tracepoint API must be protected by the tracepoint mutex,
97 * excepts calls to tracepoint_register_lib and
98 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
99 */
100
101 /*
102 * Tracepoint hash table, containing the active tracepoints.
103 * Protected by tracepoint mutex.
104 */
105 #define TRACEPOINT_HASH_BITS 12
106 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
107 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
108
109 static CDS_LIST_HEAD(old_probes);
110 static int need_update;
111
112 static CDS_LIST_HEAD(release_queue);
113 static int release_queue_need_update;
114
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.
119 * Tracepoint entries modifications are protected by the tracepoint mutex.
120 */
121 struct tracepoint_entry {
122 struct cds_hlist_node hlist;
123 struct lttng_ust_tracepoint_probe *probes;
124 int refcount; /* Number of times armed. 0 if disarmed. */
125 int callsite_refcount; /* how many libs use this tracepoint */
126 char *signature;
127 char *name;
128 };
129
130 struct tp_probes {
131 union {
132 struct cds_list_head list;
133 /* Field below only used for call_rcu scheme */
134 /* struct rcu_head head; */
135 } u;
136 struct lttng_ust_tracepoint_probe probes[0];
137 };
138
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)
145 static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
146
147 struct callsite_entry {
148 struct cds_hlist_node hlist; /* hash table node */
149 struct cds_list_head node; /* lib list of callsites node */
150 struct lttng_ust_tracepoint *tp;
151 bool tp_entry_callsite_ref; /* Has a tp_entry took a ref on this callsite */
152 };
153
154 static int tracepoint_v1_api_used;
155 static void (*lttng_ust_liburcu_bp_synchronize_rcu)(void);
156 static void (*lttng_ust_liburcu_bp_rcu_read_lock)(void);
157 static void (*lttng_ust_liburcu_bp_rcu_read_unlock)(void);
158 void (*lttng_ust_liburcu_bp_before_fork)(void);
159 void (*lttng_ust_liburcu_bp_after_fork_parent)(void);
160 void (*lttng_ust_liburcu_bp_after_fork_child)(void);
161
162 static bool lttng_ust_tracepoint_v1_used(void)
163 {
164 return uatomic_read(&tracepoint_v1_api_used);
165 }
166
167 static 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
220 /* coverity[+alloc] */
221 static void *allocate_probes(int count)
222 {
223 struct tp_probes *p =
224 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
225 + sizeof(struct tp_probes));
226 return p == NULL ? NULL : p->probes;
227 }
228
229 /* coverity[+free : arg-0] */
230 static void release_probes(void *old)
231 {
232 if (old) {
233 struct tp_probes *tp_probes = caa_container_of(old,
234 struct tp_probes, probes[0]);
235 lttng_ust_synchronize_trace();
236 free(tp_probes);
237 }
238 }
239
240 static void debug_print_probes(struct tracepoint_entry *entry)
241 {
242 int i;
243
244 if (!tracepoint_debug || !entry->probes)
245 return;
246
247 for (i = 0; entry->probes[i].func; i++)
248 DBG("Probe %d : %p", i, entry->probes[i].func);
249 }
250
251 static void *
252 tracepoint_entry_add_probe(struct tracepoint_entry *entry,
253 void (*probe)(void), void *data)
254 {
255 int nr_probes = 0;
256 struct lttng_ust_tracepoint_probe *old, *new;
257
258 if (!probe) {
259 WARN_ON(1);
260 return ERR_PTR(-EINVAL);
261 }
262 debug_print_probes(entry);
263 old = entry->probes;
264 if (old) {
265 /* (N -> N+1), (N != 0, 1) probes */
266 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
267 if (old[nr_probes].func == probe &&
268 old[nr_probes].data == data)
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)
276 memcpy(new, old,
277 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
278 new[nr_probes].func = probe;
279 new[nr_probes].data = data;
280 new[nr_probes + 1].func = NULL;
281 entry->refcount = nr_probes + 1;
282 entry->probes = new;
283 debug_print_probes(entry);
284 return old;
285 }
286
287 static void *
288 tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
289 void (*probe)(void), void *data)
290 {
291 int nr_probes = 0, nr_del = 0, i;
292 struct lttng_ust_tracepoint_probe *old, *new;
293
294 old = entry->probes;
295
296 if (!old)
297 return ERR_PTR(-ENOENT);
298
299 debug_print_probes(entry);
300 /* (N -> M), (N > 1, M >= 0) probes */
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 }
307 }
308
309 if (nr_probes - nr_del == 0) {
310 /* N -> 0, (N > 1) */
311 entry->probes = NULL;
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);
322 for (i = 0; old[i].func; i++)
323 if (old[i].func != probe || old[i].data != data)
324 new[j++] = old[i];
325 new[nr_probes - nr_del].func = NULL;
326 entry->refcount = nr_probes - nr_del;
327 entry->probes = new;
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.
335 * Must be called with tracepoint mutex held.
336 * Returns NULL if not present.
337 */
338 static struct tracepoint_entry *get_tracepoint(const char *name)
339 {
340 struct cds_hlist_head *head;
341 struct cds_hlist_node *node;
342 struct tracepoint_entry *e;
343 size_t name_len = strlen(name);
344 uint32_t hash;
345
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);
351 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
352 cds_hlist_for_each_entry(e, node, head, hlist) {
353 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
354 return e;
355 }
356 return NULL;
357 }
358
359 /*
360 * Add the tracepoint to the tracepoint hash table. Must be called with
361 * tracepoint mutex held.
362 */
363 static struct tracepoint_entry *add_tracepoint(const char *name,
364 const char *signature)
365 {
366 struct cds_hlist_head *head;
367 struct cds_hlist_node *node;
368 struct tracepoint_entry *e;
369 size_t name_len = strlen(name);
370 size_t sig_len = strlen(signature);
371 size_t sig_off, name_off;
372 uint32_t hash;
373
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);
379 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
380 cds_hlist_for_each_entry(e, node, head, hlist) {
381 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
382 DBG("tracepoint %s busy", name);
383 return ERR_PTR(-EEXIST); /* Already there */
384 }
385 }
386
387 /*
388 * Using zmalloc here to allocate a variable length elements: name and
389 * signature. Could cause some memory fragmentation if overused.
390 */
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);
395 if (!e)
396 return ERR_PTR(-ENOMEM);
397 e->name = (char *) e + name_off;
398 memcpy(e->name, name, name_len + 1);
399 e->name[name_len] = '\0';
400
401 e->signature = (char *) e + sig_off;
402 memcpy(e->signature, signature, sig_len + 1);
403 e->signature[sig_len] = '\0';
404
405 e->probes = NULL;
406 e->refcount = 0;
407 e->callsite_refcount = 0;
408
409 cds_hlist_add_head(&e->hlist, head);
410 return e;
411 }
412
413 /*
414 * Remove the tracepoint from the tracepoint hash table. Must be called with
415 * tracepoint mutex held.
416 */
417 static void remove_tracepoint(struct tracepoint_entry *e)
418 {
419 cds_hlist_del(&e->hlist);
420 free(e);
421 }
422
423 /*
424 * Sets the probe callback corresponding to one tracepoint.
425 */
426 static void set_tracepoint(struct tracepoint_entry **entry,
427 struct lttng_ust_tracepoint *elem, int active)
428 {
429 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
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 }
447
448 /*
449 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
450 * probe callbacks array is consistent before setting a pointer to it.
451 * This array is referenced by __DO_TRACE from
452 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
453 * is used.
454 */
455 lttng_ust_rcu_assign_pointer(elem->probes, (*entry)->probes);
456 CMM_STORE_SHARED(elem->state, active);
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 */
465 static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
466 {
467 CMM_STORE_SHARED(elem->state, 0);
468 lttng_ust_rcu_assign_pointer(elem->probes, NULL);
469 }
470
471 /*
472 * Add the callsite to the callsite hash table. Must be called with
473 * tracepoint mutex held.
474 */
475 static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
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;
482 struct tracepoint_entry *tp_entry;
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));
491 if (!e) {
492 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
493 return;
494 }
495 cds_hlist_add_head(&e->hlist, head);
496 e->tp = tp;
497 cds_list_add(&e->node, &lib->callsites);
498
499 tp_entry = get_tracepoint(name);
500 if (!tp_entry)
501 return;
502 tp_entry->callsite_refcount++;
503 e->tp_entry_callsite_ref = true;
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 */
510 static void remove_callsite(struct callsite_entry *e)
511 {
512 struct tracepoint_entry *tp_entry;
513
514 tp_entry = get_tracepoint(e->tp->name);
515 if (tp_entry) {
516 if (e->tp_entry_callsite_ref)
517 tp_entry->callsite_refcount--;
518 if (tp_entry->callsite_refcount == 0)
519 disable_tracepoint(e->tp);
520 }
521 cds_hlist_del(&e->hlist);
522 cds_list_del(&e->node);
523 free(e);
524 }
525
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 */
531 static 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) {
548 struct lttng_ust_tracepoint *tp = e->tp;
549
550 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
551 continue;
552 if (tp_entry) {
553 if (!e->tp_entry_callsite_ref) {
554 tp_entry->callsite_refcount++;
555 e->tp_entry_callsite_ref = true;
556 }
557 set_tracepoint(&tp_entry, tp,
558 !!tp_entry->refcount);
559 } else {
560 disable_tracepoint(tp);
561 e->tp_entry_callsite_ref = false;
562 }
563 }
564 }
565
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 */
573 static
574 void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
575 struct lttng_ust_tracepoint * const *end)
576 {
577 struct lttng_ust_tracepoint * const *iter;
578 struct tracepoint_entry *mark_entry;
579
580 for (iter = begin; iter < end; iter++) {
581 if (!*iter)
582 continue; /* skip dummy */
583 if (!(*iter)->name) {
584 disable_tracepoint(*iter);
585 continue;
586 }
587 mark_entry = get_tracepoint((*iter)->name);
588 if (mark_entry) {
589 set_tracepoint(&mark_entry, *iter,
590 !!mark_entry->refcount);
591 } else {
592 disable_tracepoint(*iter);
593 }
594 }
595 }
596
597 static void lib_update_tracepoints(struct tracepoint_lib *lib)
598 {
599 tracepoint_update_probe_range(lib->tracepoints_start,
600 lib->tracepoints_start + lib->tracepoints_count);
601 }
602
603 static void lib_register_callsites(struct tracepoint_lib *lib)
604 {
605 struct lttng_ust_tracepoint * const *begin;
606 struct lttng_ust_tracepoint * const *end;
607 struct lttng_ust_tracepoint * const *iter;
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 }
618 add_callsite(lib, *iter);
619 }
620 }
621
622 static 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
630 /*
631 * Update probes, removing the faulty probes.
632 */
633 static void tracepoint_update_probes(void)
634 {
635 struct tracepoint_lib *lib;
636
637 /* tracepoints registered from libraries and executable. */
638 cds_list_for_each_entry(lib, &libs, list)
639 lib_update_tracepoints(lib);
640 }
641
642 static struct lttng_ust_tracepoint_probe *
643 tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
644 const char *signature)
645 {
646 struct tracepoint_entry *entry;
647 struct lttng_ust_tracepoint_probe *old;
648
649 entry = get_tracepoint(name);
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 {
656 entry = add_tracepoint(name, signature);
657 if (IS_ERR(entry))
658 return (struct lttng_ust_tracepoint_probe *)entry;
659 }
660 old = tracepoint_entry_add_probe(entry, probe, data);
661 if (IS_ERR(old) && !entry->refcount)
662 remove_tracepoint(entry);
663 return old;
664 }
665
666 static 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
676 /**
677 * __tracepoint_probe_register - Connect a probe to a tracepoint
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.
683 * Called with the tracepoint mutex held.
684 */
685 int __tracepoint_probe_register(const char *name, void (*probe)(void),
686 void *data, const char *signature)
687 {
688 void *old;
689 int ret = 0;
690
691 DBG("Registering probe to tracepoint %s", name);
692
693 pthread_mutex_lock(&tracepoint_mutex);
694 old = tracepoint_add_probe(name, probe, data, signature);
695 if (IS_ERR(old)) {
696 ret = PTR_ERR(old);
697 goto end;
698 }
699
700 tracepoint_sync_callsites(name);
701 release_probes(old);
702 end:
703 pthread_mutex_unlock(&tracepoint_mutex);
704 return ret;
705 }
706
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 */
712 int __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);
729 end:
730 pthread_mutex_unlock(&tracepoint_mutex);
731 return ret;
732 }
733
734 static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
735 void *data)
736 {
737 struct tracepoint_entry *entry;
738 void *old;
739
740 entry = get_tracepoint(name);
741 if (!entry)
742 return ERR_PTR(-ENOENT);
743 old = tracepoint_entry_remove_probe(entry, probe, data);
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
755 * @probe: probe data pointer
756 */
757 int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
758 void *data)
759 {
760 void *old;
761 int ret = 0;
762
763 DBG("Un-registering probe from tracepoint %s", name);
764
765 pthread_mutex_lock(&tracepoint_mutex);
766 old = tracepoint_remove_probe(name, probe, data);
767 if (IS_ERR(old)) {
768 ret = PTR_ERR(old);
769 goto end;
770 }
771 tracepoint_sync_callsites(name);
772 release_probes(old);
773 end:
774 pthread_mutex_unlock(&tracepoint_mutex);
775 return ret;
776 }
777
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 */
783 int __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);
799 end:
800 pthread_mutex_unlock(&tracepoint_mutex);
801 return ret;
802 }
803
804 void __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. */
819 lttng_ust_synchronize_trace();
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 }
825 end:
826 pthread_mutex_unlock(&tracepoint_mutex);
827 }
828
829 static void tracepoint_add_old_probes(void *old)
830 {
831 need_update = 1;
832 if (old) {
833 struct tp_probes *tp_probes = caa_container_of(old,
834 struct tp_probes, probes[0]);
835 cds_list_add(&tp_probes->u.list, &old_probes);
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 */
846 int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
847 void *data, const char *signature)
848 {
849 void *old;
850 int ret = 0;
851
852 pthread_mutex_lock(&tracepoint_mutex);
853 old = tracepoint_add_probe(name, probe, data, signature);
854 if (IS_ERR(old)) {
855 ret = PTR_ERR(old);
856 goto end;
857 }
858 tracepoint_add_old_probes(old);
859 end:
860 pthread_mutex_unlock(&tracepoint_mutex);
861 return ret;
862 }
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()
870 * Called with the tracepoint mutex held.
871 */
872 int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
873 void *data)
874 {
875 void *old;
876 int ret = 0;
877
878 DBG("Un-registering probe from tracepoint %s", name);
879
880 pthread_mutex_lock(&tracepoint_mutex);
881 old = tracepoint_remove_probe(name, probe, data);
882 if (IS_ERR(old)) {
883 ret = PTR_ERR(old);
884 goto end;
885 }
886 tracepoint_add_old_probes(old);
887 end:
888 pthread_mutex_unlock(&tracepoint_mutex);
889 return ret;
890 }
891
892 /**
893 * tracepoint_probe_update_all - update tracepoints
894 */
895 void tracepoint_probe_update_all(void)
896 {
897 CDS_LIST_HEAD(release_probes);
898 struct tp_probes *pos, *next;
899
900 pthread_mutex_lock(&tracepoint_mutex);
901 if (!need_update) {
902 goto end;
903 }
904 if (!cds_list_empty(&old_probes))
905 cds_list_replace_init(&old_probes, &release_probes);
906 need_update = 0;
907
908 tracepoint_update_probes();
909 /* Wait for grace period between update_probes and free. */
910 lttng_ust_synchronize_trace();
911 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
912 cds_list_del(&pos->u.list);
913 free(pos);
914 }
915 end:
916 pthread_mutex_unlock(&tracepoint_mutex);
917 }
918
919 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct lttng_ust_tracepoint *))
920 {
921 new_tracepoint_cb = cb;
922 }
923
924 static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
925 struct lttng_ust_tracepoint * const *end)
926 {
927 if (new_tracepoint_cb) {
928 struct lttng_ust_tracepoint * const *t;
929
930 for (t = start; t < end; t++) {
931 if (*t)
932 new_tracepoint_cb(*t);
933 }
934 }
935 }
936
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 */
949 int tracepoint_register_lib2(struct lttng_ust_tracepoint * const *tracepoints_start,
950 int tracepoints_count)
951 {
952 struct tracepoint_lib *pl, *iter;
953
954 init_tracepoint();
955
956 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
957 if (!pl) {
958 PERROR("Unable to register tracepoint lib");
959 return -1;
960 }
961 pl->tracepoints_start = tracepoints_start;
962 pl->tracepoints_count = tracepoints_count;
963 CDS_INIT_LIST_HEAD(&pl->callsites);
964
965 pthread_mutex_lock(&tracepoint_mutex);
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 */
978 cds_list_add(&pl->list, &libs);
979 lib_added:
980 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
981 lib_register_callsites(pl);
982 lib_update_tracepoints(pl);
983 pthread_mutex_unlock(&tracepoint_mutex);
984
985 DBG("just registered a tracepoints section from %p and having %d tracepoints",
986 tracepoints_start, tracepoints_count);
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 }
994
995 return 0;
996 }
997
998 /* Exposed for backward compatibility with old instrumented applications. */
999 int 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
1006 int tracepoint_unregister_lib2(struct lttng_ust_tracepoint * const *tracepoints_start)
1007 {
1008 struct tracepoint_lib *lib;
1009
1010 pthread_mutex_lock(&tracepoint_mutex);
1011 cds_list_for_each_entry(lib, &libs, list) {
1012 if (lib->tracepoints_start != tracepoints_start)
1013 continue;
1014
1015 cds_list_del(&lib->list);
1016 /*
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.
1021 */
1022 lib_unregister_callsites(lib);
1023 DBG("just unregistered a tracepoints section from %p",
1024 lib->tracepoints_start);
1025 free(lib);
1026 break;
1027 }
1028 pthread_mutex_unlock(&tracepoint_mutex);
1029 return 0;
1030 }
1031
1032 /* Exposed for backward compatibility with old instrumented applications. */
1033 int 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
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 */
1045 static void check_weak_hidden(void)
1046 {
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");
1059 }
1060
1061 void init_tracepoint(void)
1062 {
1063 if (uatomic_xchg(&initialized, 1) == 1)
1064 return;
1065 init_usterr();
1066 check_weak_hidden();
1067 }
1068
1069 void exit_tracepoint(void)
1070 {
1071 initialized = 0;
1072 }
1073
1074 /*
1075 * Create the wrapper symbols.
1076 */
1077 #undef tp_rcu_read_lock
1078 #undef tp_rcu_read_unlock
1079 #undef tp_rcu_dereference
1080
1081 void tp_rcu_read_lock(void)
1082 {
1083 lttng_ust_urcu_read_lock();
1084 }
1085
1086 void tp_rcu_read_unlock(void)
1087 {
1088 lttng_ust_urcu_read_unlock();
1089 }
1090
1091 void *tp_rcu_dereference_sym(void *p)
1092 {
1093 return lttng_ust_rcu_dereference(p);
1094 }
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 */
1107 void 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 */
1116 int tp_get_destructors_state(void)
1117 {
1118 return uatomic_read(&tracepoint_destructors_state);
1119 }
1120
1121 void 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 */
1135 void tp_rcu_read_lock_bp(void)
1136 {
1137 lttng_ust_urcu_read_lock();
1138 }
1139
1140 void tp_rcu_read_unlock_bp(void)
1141 {
1142 lttng_ust_urcu_read_unlock();
1143 }
1144
1145 void *tp_rcu_dereference_sym_bp(void *p)
1146 {
1147 return lttng_ust_rcu_dereference(p);
1148 }
This page took 0.06449 seconds and 4 git commands to generate.