14b8231f879fca9d3aa8e3bde5a6a661cbb6b4c2
[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 <urcu-bp.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 static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
57
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 */
69 static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
70
71 /*
72 * libraries that contain tracepoints (struct tracepoint_lib).
73 * Protected by tracepoint mutex.
74 */
75 static CDS_LIST_HEAD(libs);
76
77 /*
78 * The tracepoint mutex protects the library tracepoints, the hash table, and
79 * the library list.
80 * All calls to the tracepoint API must be protected by the tracepoint mutex,
81 * excepts calls to tracepoint_register_lib and
82 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
83 */
84
85 /*
86 * Tracepoint hash table, containing the active tracepoints.
87 * Protected by tracepoint mutex.
88 */
89 #define TRACEPOINT_HASH_BITS 12
90 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
91 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
92
93 static CDS_LIST_HEAD(old_probes);
94 static int need_update;
95
96 static CDS_LIST_HEAD(release_queue);
97 static int release_queue_need_update;
98
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.
103 * Tracepoint entries modifications are protected by the tracepoint mutex.
104 */
105 struct tracepoint_entry {
106 struct cds_hlist_node hlist;
107 struct lttng_ust_tracepoint_probe *probes;
108 int refcount; /* Number of times armed. 0 if disarmed. */
109 int callsite_refcount; /* how many libs use this tracepoint */
110 const char *signature;
111 char name[0];
112 };
113
114 struct tp_probes {
115 union {
116 struct cds_list_head list;
117 /* Field below only used for call_rcu scheme */
118 /* struct rcu_head head; */
119 } u;
120 struct lttng_ust_tracepoint_probe probes[0];
121 };
122
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)
129 static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
130
131 struct callsite_entry {
132 struct cds_hlist_node hlist; /* hash table node */
133 struct cds_list_head node; /* lib list of callsites node */
134 struct lttng_ust_tracepoint *tp;
135 };
136
137 /* coverity[+alloc] */
138 static void *allocate_probes(int count)
139 {
140 struct tp_probes *p =
141 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
142 + sizeof(struct tp_probes));
143 return p == NULL ? NULL : p->probes;
144 }
145
146 /* coverity[+free : arg-0] */
147 static void release_probes(void *old)
148 {
149 if (old) {
150 struct tp_probes *tp_probes = caa_container_of(old,
151 struct tp_probes, probes[0]);
152 synchronize_rcu();
153 free(tp_probes);
154 }
155 }
156
157 static void debug_print_probes(struct tracepoint_entry *entry)
158 {
159 int i;
160
161 if (!tracepoint_debug || !entry->probes)
162 return;
163
164 for (i = 0; entry->probes[i].func; i++)
165 DBG("Probe %d : %p", i, entry->probes[i].func);
166 }
167
168 static void *
169 tracepoint_entry_add_probe(struct tracepoint_entry *entry,
170 void (*probe)(void), void *data)
171 {
172 int nr_probes = 0;
173 struct lttng_ust_tracepoint_probe *old, *new;
174
175 if (!probe) {
176 WARN_ON(1);
177 return ERR_PTR(-EINVAL);
178 }
179 debug_print_probes(entry);
180 old = entry->probes;
181 if (old) {
182 /* (N -> N+1), (N != 0, 1) probes */
183 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
184 if (old[nr_probes].func == probe &&
185 old[nr_probes].data == data)
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)
193 memcpy(new, old,
194 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
195 new[nr_probes].func = probe;
196 new[nr_probes].data = data;
197 new[nr_probes + 1].func = NULL;
198 entry->refcount = nr_probes + 1;
199 entry->probes = new;
200 debug_print_probes(entry);
201 return old;
202 }
203
204 static void *
205 tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
206 void (*probe)(void), void *data)
207 {
208 int nr_probes = 0, nr_del = 0, i;
209 struct lttng_ust_tracepoint_probe *old, *new;
210
211 old = entry->probes;
212
213 if (!old)
214 return ERR_PTR(-ENOENT);
215
216 debug_print_probes(entry);
217 /* (N -> M), (N > 1, M >= 0) probes */
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 }
224 }
225
226 if (nr_probes - nr_del == 0) {
227 /* N -> 0, (N > 1) */
228 entry->probes = NULL;
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);
239 for (i = 0; old[i].func; i++)
240 if (old[i].func != probe || old[i].data != data)
241 new[j++] = old[i];
242 new[nr_probes - nr_del].func = NULL;
243 entry->refcount = nr_probes - nr_del;
244 entry->probes = new;
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.
252 * Must be called with tracepoint mutex held.
253 * Returns NULL if not present.
254 */
255 static struct tracepoint_entry *get_tracepoint(const char *name)
256 {
257 struct cds_hlist_head *head;
258 struct cds_hlist_node *node;
259 struct tracepoint_entry *e;
260 size_t name_len = strlen(name);
261 uint32_t hash;
262
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);
268 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
269 cds_hlist_for_each_entry(e, node, head, hlist) {
270 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
271 return e;
272 }
273 return NULL;
274 }
275
276 /*
277 * Add the tracepoint to the tracepoint hash table. Must be called with
278 * tracepoint mutex held.
279 */
280 static struct tracepoint_entry *add_tracepoint(const char *name,
281 const char *signature)
282 {
283 struct cds_hlist_head *head;
284 struct cds_hlist_node *node;
285 struct tracepoint_entry *e;
286 size_t name_len = strlen(name);
287 uint32_t hash;
288
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);
294 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
295 cds_hlist_for_each_entry(e, node, head, hlist) {
296 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
297 DBG("tracepoint %s busy", name);
298 return ERR_PTR(-EEXIST); /* Already there */
299 }
300 }
301 /*
302 * Using zmalloc here to allocate a variable length element. Could
303 * cause some memory fragmentation if overused.
304 */
305 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
306 if (!e)
307 return ERR_PTR(-ENOMEM);
308 memcpy(&e->name[0], name, name_len + 1);
309 e->name[name_len] = '\0';
310 e->probes = NULL;
311 e->refcount = 0;
312 e->callsite_refcount = 0;
313 e->signature = signature;
314 cds_hlist_add_head(&e->hlist, head);
315 return e;
316 }
317
318 /*
319 * Remove the tracepoint from the tracepoint hash table. Must be called with
320 * tracepoint mutex held.
321 */
322 static void remove_tracepoint(struct tracepoint_entry *e)
323 {
324 cds_hlist_del(&e->hlist);
325 free(e);
326 }
327
328 /*
329 * Sets the probe callback corresponding to one tracepoint.
330 */
331 static void set_tracepoint(struct tracepoint_entry **entry,
332 struct lttng_ust_tracepoint *elem, int active)
333 {
334 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
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 }
352
353 /*
354 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
355 * probe callbacks array is consistent before setting a pointer to it.
356 * This array is referenced by __DO_TRACE from
357 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
358 * is used.
359 */
360 rcu_assign_pointer(elem->probes, (*entry)->probes);
361 CMM_STORE_SHARED(elem->state, active);
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 */
370 static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
371 {
372 CMM_STORE_SHARED(elem->state, 0);
373 rcu_assign_pointer(elem->probes, NULL);
374 }
375
376 /*
377 * Add the callsite to the callsite hash table. Must be called with
378 * tracepoint mutex held.
379 */
380 static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
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;
387 struct tracepoint_entry *tp_entry;
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));
396 if (!e) {
397 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
398 return;
399 }
400 cds_hlist_add_head(&e->hlist, head);
401 e->tp = tp;
402 cds_list_add(&e->node, &lib->callsites);
403
404 tp_entry = get_tracepoint(name);
405 if (!tp_entry)
406 return;
407 tp_entry->callsite_refcount++;
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 */
414 static void remove_callsite(struct callsite_entry *e)
415 {
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 }
424 cds_hlist_del(&e->hlist);
425 cds_list_del(&e->node);
426 free(e);
427 }
428
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 */
434 static 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) {
451 struct lttng_ust_tracepoint *tp = e->tp;
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
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 */
471 static
472 void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
473 struct lttng_ust_tracepoint * const *end)
474 {
475 struct lttng_ust_tracepoint * const *iter;
476 struct tracepoint_entry *mark_entry;
477
478 for (iter = begin; iter < end; iter++) {
479 if (!*iter)
480 continue; /* skip dummy */
481 if (!(*iter)->name) {
482 disable_tracepoint(*iter);
483 continue;
484 }
485 mark_entry = get_tracepoint((*iter)->name);
486 if (mark_entry) {
487 set_tracepoint(&mark_entry, *iter,
488 !!mark_entry->refcount);
489 } else {
490 disable_tracepoint(*iter);
491 }
492 }
493 }
494
495 static void lib_update_tracepoints(struct tracepoint_lib *lib)
496 {
497 tracepoint_update_probe_range(lib->tracepoints_start,
498 lib->tracepoints_start + lib->tracepoints_count);
499 }
500
501 static void lib_register_callsites(struct tracepoint_lib *lib)
502 {
503 struct lttng_ust_tracepoint * const *begin;
504 struct lttng_ust_tracepoint * const *end;
505 struct lttng_ust_tracepoint * const *iter;
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 }
516 add_callsite(lib, *iter);
517 }
518 }
519
520 static 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
528 /*
529 * Update probes, removing the faulty probes.
530 */
531 static void tracepoint_update_probes(void)
532 {
533 struct tracepoint_lib *lib;
534
535 /* tracepoints registered from libraries and executable. */
536 cds_list_for_each_entry(lib, &libs, list)
537 lib_update_tracepoints(lib);
538 }
539
540 static struct lttng_ust_tracepoint_probe *
541 tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
542 const char *signature)
543 {
544 struct tracepoint_entry *entry;
545 struct lttng_ust_tracepoint_probe *old;
546
547 entry = get_tracepoint(name);
548 if (!entry) {
549 entry = add_tracepoint(name, signature);
550 if (IS_ERR(entry))
551 return (struct lttng_ust_tracepoint_probe *)entry;
552 }
553 old = tracepoint_entry_add_probe(entry, probe, data);
554 if (IS_ERR(old) && !entry->refcount)
555 remove_tracepoint(entry);
556 return old;
557 }
558
559 static 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
569 /**
570 * __tracepoint_probe_register - Connect a probe to a tracepoint
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.
576 * Called with the tracepoint mutex held.
577 */
578 int __tracepoint_probe_register(const char *name, void (*probe)(void),
579 void *data, const char *signature)
580 {
581 void *old;
582 int ret = 0;
583
584 DBG("Registering probe to tracepoint %s", name);
585
586 pthread_mutex_lock(&tracepoint_mutex);
587 old = tracepoint_add_probe(name, probe, data, signature);
588 if (IS_ERR(old)) {
589 ret = PTR_ERR(old);
590 goto end;
591 }
592
593 tracepoint_sync_callsites(name);
594 release_probes(old);
595 end:
596 pthread_mutex_unlock(&tracepoint_mutex);
597 return ret;
598 }
599
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 */
605 int __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);
622 end:
623 pthread_mutex_unlock(&tracepoint_mutex);
624 return ret;
625 }
626
627 static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
628 void *data)
629 {
630 struct tracepoint_entry *entry;
631 void *old;
632
633 entry = get_tracepoint(name);
634 if (!entry)
635 return ERR_PTR(-ENOENT);
636 old = tracepoint_entry_remove_probe(entry, probe, data);
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
648 * @probe: probe data pointer
649 */
650 int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
651 void *data)
652 {
653 void *old;
654 int ret = 0;
655
656 DBG("Un-registering probe from tracepoint %s", name);
657
658 pthread_mutex_lock(&tracepoint_mutex);
659 old = tracepoint_remove_probe(name, probe, data);
660 if (IS_ERR(old)) {
661 ret = PTR_ERR(old);
662 goto end;
663 }
664 tracepoint_sync_callsites(name);
665 release_probes(old);
666 end:
667 pthread_mutex_unlock(&tracepoint_mutex);
668 return ret;
669 }
670
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 */
676 int __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);
692 end:
693 pthread_mutex_unlock(&tracepoint_mutex);
694 return ret;
695 }
696
697 void __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 }
718 end:
719 pthread_mutex_unlock(&tracepoint_mutex);
720 }
721
722 static void tracepoint_add_old_probes(void *old)
723 {
724 need_update = 1;
725 if (old) {
726 struct tp_probes *tp_probes = caa_container_of(old,
727 struct tp_probes, probes[0]);
728 cds_list_add(&tp_probes->u.list, &old_probes);
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 */
739 int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
740 void *data, const char *signature)
741 {
742 void *old;
743 int ret = 0;
744
745 pthread_mutex_lock(&tracepoint_mutex);
746 old = tracepoint_add_probe(name, probe, data, signature);
747 if (IS_ERR(old)) {
748 ret = PTR_ERR(old);
749 goto end;
750 }
751 tracepoint_add_old_probes(old);
752 end:
753 pthread_mutex_unlock(&tracepoint_mutex);
754 return ret;
755 }
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()
763 * Called with the tracepoint mutex held.
764 */
765 int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
766 void *data)
767 {
768 void *old;
769 int ret = 0;
770
771 DBG("Un-registering probe from tracepoint %s", name);
772
773 pthread_mutex_lock(&tracepoint_mutex);
774 old = tracepoint_remove_probe(name, probe, data);
775 if (IS_ERR(old)) {
776 ret = PTR_ERR(old);
777 goto end;
778 }
779 tracepoint_add_old_probes(old);
780 end:
781 pthread_mutex_unlock(&tracepoint_mutex);
782 return ret;
783 }
784
785 /**
786 * tracepoint_probe_update_all - update tracepoints
787 */
788 void tracepoint_probe_update_all(void)
789 {
790 CDS_LIST_HEAD(release_probes);
791 struct tp_probes *pos, *next;
792
793 pthread_mutex_lock(&tracepoint_mutex);
794 if (!need_update) {
795 goto end;
796 }
797 if (!cds_list_empty(&old_probes))
798 cds_list_replace_init(&old_probes, &release_probes);
799 need_update = 0;
800
801 tracepoint_update_probes();
802 /* Wait for grace period between update_probes and free. */
803 synchronize_rcu();
804 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
805 cds_list_del(&pos->u.list);
806 free(pos);
807 }
808 end:
809 pthread_mutex_unlock(&tracepoint_mutex);
810 }
811
812 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct lttng_ust_tracepoint *))
813 {
814 new_tracepoint_cb = cb;
815 }
816
817 static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
818 struct lttng_ust_tracepoint * const *end)
819 {
820 if (new_tracepoint_cb) {
821 struct lttng_ust_tracepoint * const *t;
822
823 for (t = start; t < end; t++) {
824 if (*t)
825 new_tracepoint_cb(*t);
826 }
827 }
828 }
829
830 int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
831 int tracepoints_count)
832 {
833 struct tracepoint_lib *pl, *iter;
834
835 init_tracepoint();
836
837 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
838 if (!pl) {
839 PERROR("Unable to register tracepoint lib");
840 return -1;
841 }
842 pl->tracepoints_start = tracepoints_start;
843 pl->tracepoints_count = tracepoints_count;
844 CDS_INIT_LIST_HEAD(&pl->callsites);
845
846 pthread_mutex_lock(&tracepoint_mutex);
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 */
859 cds_list_add(&pl->list, &libs);
860 lib_added:
861 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
862 lib_register_callsites(pl);
863 lib_update_tracepoints(pl);
864 pthread_mutex_unlock(&tracepoint_mutex);
865
866 DBG("just registered a tracepoints section from %p and having %d tracepoints",
867 tracepoints_start, tracepoints_count);
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 }
875
876 return 0;
877 }
878
879 int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start)
880 {
881 struct tracepoint_lib *lib;
882
883 pthread_mutex_lock(&tracepoint_mutex);
884 cds_list_for_each_entry(lib, &libs, list) {
885 if (lib->tracepoints_start != tracepoints_start)
886 continue;
887
888 cds_list_del(&lib->list);
889 /*
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.
894 */
895 lib_unregister_callsites(lib);
896 DBG("just unregistered a tracepoints section from %p",
897 lib->tracepoints_start);
898 free(lib);
899 break;
900 }
901 pthread_mutex_unlock(&tracepoint_mutex);
902 return 0;
903 }
904
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 */
911 static void check_weak_hidden(void)
912 {
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");
925 }
926
927 void init_tracepoint(void)
928 {
929 if (uatomic_xchg(&initialized, 1) == 1)
930 return;
931 init_usterr();
932 check_weak_hidden();
933 }
934
935 void exit_tracepoint(void)
936 {
937 initialized = 0;
938 }
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
947 void tp_rcu_read_lock_bp(void)
948 {
949 rcu_read_lock_bp();
950 }
951
952 void tp_rcu_read_unlock_bp(void)
953 {
954 rcu_read_unlock_bp();
955 }
956
957 void *tp_rcu_dereference_sym_bp(void *p)
958 {
959 return rcu_dereference_bp(p);
960 }
This page took 0.047445 seconds and 3 git commands to generate.