Fix: perform volatile load of tracepoint state
[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 /* Set to 1 to enable tracepoint debug output */
47 static const int tracepoint_debug;
48 static int initialized;
49 static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
50
51 /*
52 * tracepoint_mutex nests inside UST mutex.
53 *
54 * Note about interaction with fork/clone: UST does not hold the
55 * tracepoint mutex across fork/clone because it is either:
56 * - nested within UST mutex, in which case holding the UST mutex across
57 * fork/clone suffice,
58 * - taken by a library constructor, which should never race with a
59 * fork/clone if the application is expected to continue running with
60 * the same memory layout (no following exec()).
61 */
62 static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
63
64 /*
65 * libraries that contain tracepoints (struct tracepoint_lib).
66 * Protected by tracepoint mutex.
67 */
68 static CDS_LIST_HEAD(libs);
69
70 /*
71 * The tracepoint mutex protects the library tracepoints, the hash table, and
72 * the library list.
73 * All calls to the tracepoint API must be protected by the tracepoint mutex,
74 * excepts calls to tracepoint_register_lib and
75 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
76 */
77
78 /*
79 * Tracepoint hash table, containing the active tracepoints.
80 * Protected by tracepoint mutex.
81 */
82 #define TRACEPOINT_HASH_BITS 12
83 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
84 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
85
86 static CDS_LIST_HEAD(old_probes);
87 static int need_update;
88
89 /*
90 * Note about RCU :
91 * It is used to to delay the free of multiple probes array until a quiescent
92 * state is reached.
93 * Tracepoint entries modifications are protected by the tracepoint mutex.
94 */
95 struct tracepoint_entry {
96 struct cds_hlist_node hlist;
97 struct lttng_ust_tracepoint_probe *probes;
98 int refcount; /* Number of times armed. 0 if disarmed. */
99 int callsite_refcount; /* how many libs use this tracepoint */
100 const char *signature;
101 char name[0];
102 };
103
104 struct tp_probes {
105 union {
106 struct cds_list_head list;
107 /* Field below only used for call_rcu scheme */
108 /* struct rcu_head head; */
109 } u;
110 struct lttng_ust_tracepoint_probe probes[0];
111 };
112
113 /*
114 * Callsite hash table, containing the tracepoint call sites.
115 * Protected by tracepoint mutex.
116 */
117 #define CALLSITE_HASH_BITS 12
118 #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
119 static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
120
121 struct callsite_entry {
122 struct cds_hlist_node hlist; /* hash table node */
123 struct cds_list_head node; /* lib list of callsites node */
124 struct lttng_ust_tracepoint *tp;
125 };
126
127 /* coverity[+alloc] */
128 static void *allocate_probes(int count)
129 {
130 struct tp_probes *p =
131 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
132 + sizeof(struct tp_probes));
133 return p == NULL ? NULL : p->probes;
134 }
135
136 /* coverity[+free : arg-0] */
137 static void release_probes(void *old)
138 {
139 if (old) {
140 struct tp_probes *tp_probes = caa_container_of(old,
141 struct tp_probes, probes[0]);
142 synchronize_rcu();
143 free(tp_probes);
144 }
145 }
146
147 static void debug_print_probes(struct tracepoint_entry *entry)
148 {
149 int i;
150
151 if (!tracepoint_debug || !entry->probes)
152 return;
153
154 for (i = 0; entry->probes[i].func; i++)
155 DBG("Probe %d : %p", i, entry->probes[i].func);
156 }
157
158 static void *
159 tracepoint_entry_add_probe(struct tracepoint_entry *entry,
160 void (*probe)(void), void *data)
161 {
162 int nr_probes = 0;
163 struct lttng_ust_tracepoint_probe *old, *new;
164
165 if (!probe) {
166 WARN_ON(1);
167 return ERR_PTR(-EINVAL);
168 }
169 debug_print_probes(entry);
170 old = entry->probes;
171 if (old) {
172 /* (N -> N+1), (N != 0, 1) probes */
173 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
174 if (old[nr_probes].func == probe &&
175 old[nr_probes].data == data)
176 return ERR_PTR(-EEXIST);
177 }
178 /* + 2 : one for new probe, one for NULL func */
179 new = allocate_probes(nr_probes + 2);
180 if (new == NULL)
181 return ERR_PTR(-ENOMEM);
182 if (old)
183 memcpy(new, old,
184 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
185 new[nr_probes].func = probe;
186 new[nr_probes].data = data;
187 new[nr_probes + 1].func = NULL;
188 entry->refcount = nr_probes + 1;
189 entry->probes = new;
190 debug_print_probes(entry);
191 return old;
192 }
193
194 static void *
195 tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
196 void (*probe)(void), void *data)
197 {
198 int nr_probes = 0, nr_del = 0, i;
199 struct lttng_ust_tracepoint_probe *old, *new;
200
201 old = entry->probes;
202
203 if (!old)
204 return ERR_PTR(-ENOENT);
205
206 debug_print_probes(entry);
207 /* (N -> M), (N > 1, M >= 0) probes */
208 if (probe) {
209 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
210 if (old[nr_probes].func == probe &&
211 old[nr_probes].data == data)
212 nr_del++;
213 }
214 }
215
216 if (nr_probes - nr_del == 0) {
217 /* N -> 0, (N > 1) */
218 entry->probes = NULL;
219 entry->refcount = 0;
220 debug_print_probes(entry);
221 return old;
222 } else {
223 int j = 0;
224 /* N -> M, (N > 1, M > 0) */
225 /* + 1 for NULL */
226 new = allocate_probes(nr_probes - nr_del + 1);
227 if (new == NULL)
228 return ERR_PTR(-ENOMEM);
229 for (i = 0; old[i].func; i++)
230 if (old[i].func != probe || old[i].data != data)
231 new[j++] = old[i];
232 new[nr_probes - nr_del].func = NULL;
233 entry->refcount = nr_probes - nr_del;
234 entry->probes = new;
235 }
236 debug_print_probes(entry);
237 return old;
238 }
239
240 /*
241 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
242 * Must be called with tracepoint mutex held.
243 * Returns NULL if not present.
244 */
245 static struct tracepoint_entry *get_tracepoint(const char *name)
246 {
247 struct cds_hlist_head *head;
248 struct cds_hlist_node *node;
249 struct tracepoint_entry *e;
250 size_t name_len = strlen(name);
251 uint32_t hash;
252
253 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
254 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
255 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
256 }
257 hash = jhash(name, name_len, 0);
258 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
259 cds_hlist_for_each_entry(e, node, head, hlist) {
260 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
261 return e;
262 }
263 return NULL;
264 }
265
266 /*
267 * Add the tracepoint to the tracepoint hash table. Must be called with
268 * tracepoint mutex held.
269 */
270 static struct tracepoint_entry *add_tracepoint(const char *name,
271 const char *signature)
272 {
273 struct cds_hlist_head *head;
274 struct cds_hlist_node *node;
275 struct tracepoint_entry *e;
276 size_t name_len = strlen(name);
277 uint32_t hash;
278
279 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
280 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
281 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
282 }
283 hash = jhash(name, name_len, 0);
284 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
285 cds_hlist_for_each_entry(e, node, head, hlist) {
286 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
287 DBG("tracepoint %s busy", name);
288 return ERR_PTR(-EEXIST); /* Already there */
289 }
290 }
291 /*
292 * Using zmalloc here to allocate a variable length element. Could
293 * cause some memory fragmentation if overused.
294 */
295 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
296 if (!e)
297 return ERR_PTR(-ENOMEM);
298 memcpy(&e->name[0], name, name_len + 1);
299 e->name[name_len] = '\0';
300 e->probes = NULL;
301 e->refcount = 0;
302 e->callsite_refcount = 0;
303 e->signature = signature;
304 cds_hlist_add_head(&e->hlist, head);
305 return e;
306 }
307
308 /*
309 * Remove the tracepoint from the tracepoint hash table. Must be called with
310 * tracepoint mutex held.
311 */
312 static void remove_tracepoint(struct tracepoint_entry *e)
313 {
314 cds_hlist_del(&e->hlist);
315 free(e);
316 }
317
318 /*
319 * Sets the probe callback corresponding to one tracepoint.
320 */
321 static void set_tracepoint(struct tracepoint_entry **entry,
322 struct lttng_ust_tracepoint *elem, int active)
323 {
324 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
325 /*
326 * Check that signatures match before connecting a probe to a
327 * tracepoint. Warn the user if they don't.
328 */
329 if (strcmp(elem->signature, (*entry)->signature) != 0) {
330 static int warned = 0;
331
332 /* Only print once, don't flood console. */
333 if (!warned) {
334 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
335 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
336 elem->name, elem->signature, (*entry)->signature);
337 warned = 1;
338 }
339 /* Don't accept connecting non-matching signatures. */
340 return;
341 }
342
343 /*
344 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
345 * probe callbacks array is consistent before setting a pointer to it.
346 * This array is referenced by __DO_TRACE from
347 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
348 * is used.
349 */
350 rcu_assign_pointer(elem->probes, (*entry)->probes);
351 CMM_STORE_SHARED(elem->state, active);
352 }
353
354 /*
355 * Disable a tracepoint and its probe callback.
356 * Note: only waiting an RCU period after setting elem->call to the empty
357 * function insures that the original callback is not used anymore. This insured
358 * by preempt_disable around the call site.
359 */
360 static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
361 {
362 CMM_STORE_SHARED(elem->state, 0);
363 rcu_assign_pointer(elem->probes, NULL);
364 }
365
366 /*
367 * Add the callsite to the callsite hash table. Must be called with
368 * tracepoint mutex held.
369 */
370 static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
371 {
372 struct cds_hlist_head *head;
373 struct callsite_entry *e;
374 const char *name = tp->name;
375 size_t name_len = strlen(name);
376 uint32_t hash;
377 struct tracepoint_entry *tp_entry;
378
379 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
380 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
381 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
382 }
383 hash = jhash(name, name_len, 0);
384 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
385 e = zmalloc(sizeof(struct callsite_entry));
386 if (!e) {
387 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
388 return;
389 }
390 cds_hlist_add_head(&e->hlist, head);
391 e->tp = tp;
392 cds_list_add(&e->node, &lib->callsites);
393
394 tp_entry = get_tracepoint(name);
395 if (!tp_entry)
396 return;
397 tp_entry->callsite_refcount++;
398 }
399
400 /*
401 * Remove the callsite from the callsite hash table and from lib
402 * callsite list. Must be called with tracepoint mutex held.
403 */
404 static void remove_callsite(struct callsite_entry *e)
405 {
406 struct tracepoint_entry *tp_entry;
407
408 tp_entry = get_tracepoint(e->tp->name);
409 if (tp_entry) {
410 tp_entry->callsite_refcount--;
411 if (tp_entry->callsite_refcount == 0)
412 disable_tracepoint(e->tp);
413 }
414 cds_hlist_del(&e->hlist);
415 cds_list_del(&e->node);
416 free(e);
417 }
418
419 /*
420 * Enable/disable all callsites based on the state of a specific
421 * tracepoint entry.
422 * Must be called with tracepoint mutex held.
423 */
424 static void tracepoint_sync_callsites(const char *name)
425 {
426 struct cds_hlist_head *head;
427 struct cds_hlist_node *node;
428 struct callsite_entry *e;
429 size_t name_len = strlen(name);
430 uint32_t hash;
431 struct tracepoint_entry *tp_entry;
432
433 tp_entry = get_tracepoint(name);
434 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
435 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
436 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
437 }
438 hash = jhash(name, name_len, 0);
439 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
440 cds_hlist_for_each_entry(e, node, head, hlist) {
441 struct lttng_ust_tracepoint *tp = e->tp;
442
443 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
444 continue;
445 if (tp_entry) {
446 set_tracepoint(&tp_entry, tp,
447 !!tp_entry->refcount);
448 } else {
449 disable_tracepoint(tp);
450 }
451 }
452 }
453
454 /**
455 * tracepoint_update_probe_range - Update a probe range
456 * @begin: beginning of the range
457 * @end: end of the range
458 *
459 * Updates the probe callback corresponding to a range of tracepoints.
460 */
461 static
462 void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
463 struct lttng_ust_tracepoint * const *end)
464 {
465 struct lttng_ust_tracepoint * const *iter;
466 struct tracepoint_entry *mark_entry;
467
468 for (iter = begin; iter < end; iter++) {
469 if (!*iter)
470 continue; /* skip dummy */
471 if (!(*iter)->name) {
472 disable_tracepoint(*iter);
473 continue;
474 }
475 mark_entry = get_tracepoint((*iter)->name);
476 if (mark_entry) {
477 set_tracepoint(&mark_entry, *iter,
478 !!mark_entry->refcount);
479 } else {
480 disable_tracepoint(*iter);
481 }
482 }
483 }
484
485 static void lib_update_tracepoints(struct tracepoint_lib *lib)
486 {
487 tracepoint_update_probe_range(lib->tracepoints_start,
488 lib->tracepoints_start + lib->tracepoints_count);
489 }
490
491 static void lib_register_callsites(struct tracepoint_lib *lib)
492 {
493 struct lttng_ust_tracepoint * const *begin;
494 struct lttng_ust_tracepoint * const *end;
495 struct lttng_ust_tracepoint * const *iter;
496
497 begin = lib->tracepoints_start;
498 end = lib->tracepoints_start + lib->tracepoints_count;
499
500 for (iter = begin; iter < end; iter++) {
501 if (!*iter)
502 continue; /* skip dummy */
503 if (!(*iter)->name) {
504 continue;
505 }
506 add_callsite(lib, *iter);
507 }
508 }
509
510 static void lib_unregister_callsites(struct tracepoint_lib *lib)
511 {
512 struct callsite_entry *callsite, *tmp;
513
514 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
515 remove_callsite(callsite);
516 }
517
518 /*
519 * Update probes, removing the faulty probes.
520 */
521 static void tracepoint_update_probes(void)
522 {
523 struct tracepoint_lib *lib;
524
525 /* tracepoints registered from libraries and executable. */
526 cds_list_for_each_entry(lib, &libs, list)
527 lib_update_tracepoints(lib);
528 }
529
530 static struct lttng_ust_tracepoint_probe *
531 tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
532 const char *signature)
533 {
534 struct tracepoint_entry *entry;
535 struct lttng_ust_tracepoint_probe *old;
536
537 entry = get_tracepoint(name);
538 if (!entry) {
539 entry = add_tracepoint(name, signature);
540 if (IS_ERR(entry))
541 return (struct lttng_ust_tracepoint_probe *)entry;
542 }
543 old = tracepoint_entry_add_probe(entry, probe, data);
544 if (IS_ERR(old) && !entry->refcount)
545 remove_tracepoint(entry);
546 return old;
547 }
548
549 /**
550 * __tracepoint_probe_register - Connect a probe to a tracepoint
551 * @name: tracepoint name
552 * @probe: probe handler
553 *
554 * Returns 0 if ok, error value on error.
555 * The probe address must at least be aligned on the architecture pointer size.
556 * Called with the tracepoint mutex held.
557 */
558 int __tracepoint_probe_register(const char *name, void (*probe)(void),
559 void *data, const char *signature)
560 {
561 void *old;
562 int ret = 0;
563
564 DBG("Registering probe to tracepoint %s", name);
565
566 pthread_mutex_lock(&tracepoint_mutex);
567 old = tracepoint_add_probe(name, probe, data, signature);
568 if (IS_ERR(old)) {
569 ret = PTR_ERR(old);
570 goto end;
571 }
572
573 tracepoint_sync_callsites(name);
574 release_probes(old);
575 end:
576 pthread_mutex_unlock(&tracepoint_mutex);
577 return ret;
578 }
579
580 static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
581 void *data)
582 {
583 struct tracepoint_entry *entry;
584 void *old;
585
586 entry = get_tracepoint(name);
587 if (!entry)
588 return ERR_PTR(-ENOENT);
589 old = tracepoint_entry_remove_probe(entry, probe, data);
590 if (IS_ERR(old))
591 return old;
592 if (!entry->refcount)
593 remove_tracepoint(entry);
594 return old;
595 }
596
597 /**
598 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
599 * @name: tracepoint name
600 * @probe: probe function pointer
601 * @probe: probe data pointer
602 */
603 int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
604 void *data)
605 {
606 void *old;
607 int ret = 0;
608
609 DBG("Un-registering probe from tracepoint %s", name);
610
611 pthread_mutex_lock(&tracepoint_mutex);
612 old = tracepoint_remove_probe(name, probe, data);
613 if (IS_ERR(old)) {
614 ret = PTR_ERR(old);
615 goto end;
616 }
617 tracepoint_sync_callsites(name);
618 release_probes(old);
619 end:
620 pthread_mutex_unlock(&tracepoint_mutex);
621 return ret;
622 }
623
624 static void tracepoint_add_old_probes(void *old)
625 {
626 need_update = 1;
627 if (old) {
628 struct tp_probes *tp_probes = caa_container_of(old,
629 struct tp_probes, probes[0]);
630 cds_list_add(&tp_probes->u.list, &old_probes);
631 }
632 }
633
634 /**
635 * tracepoint_probe_register_noupdate - register a probe but not connect
636 * @name: tracepoint name
637 * @probe: probe handler
638 *
639 * caller must call tracepoint_probe_update_all()
640 */
641 int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
642 void *data, const char *signature)
643 {
644 void *old;
645 int ret = 0;
646
647 pthread_mutex_lock(&tracepoint_mutex);
648 old = tracepoint_add_probe(name, probe, data, signature);
649 if (IS_ERR(old)) {
650 ret = PTR_ERR(old);
651 goto end;
652 }
653 tracepoint_add_old_probes(old);
654 end:
655 pthread_mutex_unlock(&tracepoint_mutex);
656 return ret;
657 }
658
659 /**
660 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
661 * @name: tracepoint name
662 * @probe: probe function pointer
663 *
664 * caller must call tracepoint_probe_update_all()
665 * Called with the tracepoint mutex held.
666 */
667 int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
668 void *data)
669 {
670 void *old;
671 int ret = 0;
672
673 DBG("Un-registering probe from tracepoint %s", name);
674
675 pthread_mutex_lock(&tracepoint_mutex);
676 old = tracepoint_remove_probe(name, probe, data);
677 if (IS_ERR(old)) {
678 ret = PTR_ERR(old);
679 goto end;
680 }
681 tracepoint_add_old_probes(old);
682 end:
683 pthread_mutex_unlock(&tracepoint_mutex);
684 return ret;
685 }
686
687 /**
688 * tracepoint_probe_update_all - update tracepoints
689 */
690 void tracepoint_probe_update_all(void)
691 {
692 CDS_LIST_HEAD(release_probes);
693 struct tp_probes *pos, *next;
694
695 pthread_mutex_lock(&tracepoint_mutex);
696 if (!need_update) {
697 goto end;
698 }
699 if (!cds_list_empty(&old_probes))
700 cds_list_replace_init(&old_probes, &release_probes);
701 need_update = 0;
702
703 tracepoint_update_probes();
704 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
705 cds_list_del(&pos->u.list);
706 synchronize_rcu();
707 free(pos);
708 }
709 end:
710 pthread_mutex_unlock(&tracepoint_mutex);
711 }
712
713 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct lttng_ust_tracepoint *))
714 {
715 new_tracepoint_cb = cb;
716 }
717
718 static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
719 struct lttng_ust_tracepoint * const *end)
720 {
721 if (new_tracepoint_cb) {
722 struct lttng_ust_tracepoint * const *t;
723
724 for (t = start; t < end; t++) {
725 if (*t)
726 new_tracepoint_cb(*t);
727 }
728 }
729 }
730
731 int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
732 int tracepoints_count)
733 {
734 struct tracepoint_lib *pl, *iter;
735
736 init_tracepoint();
737
738 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
739 if (!pl) {
740 PERROR("Unable to register tracepoint lib");
741 return -1;
742 }
743 pl->tracepoints_start = tracepoints_start;
744 pl->tracepoints_count = tracepoints_count;
745 CDS_INIT_LIST_HEAD(&pl->callsites);
746
747 pthread_mutex_lock(&tracepoint_mutex);
748 /*
749 * We sort the libs by struct lib pointer address.
750 */
751 cds_list_for_each_entry_reverse(iter, &libs, list) {
752 BUG_ON(iter == pl); /* Should never be in the list twice */
753 if (iter < pl) {
754 /* We belong to the location right after iter. */
755 cds_list_add(&pl->list, &iter->list);
756 goto lib_added;
757 }
758 }
759 /* We should be added at the head of the list */
760 cds_list_add(&pl->list, &libs);
761 lib_added:
762 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
763 lib_register_callsites(pl);
764 lib_update_tracepoints(pl);
765 pthread_mutex_unlock(&tracepoint_mutex);
766
767 DBG("just registered a tracepoints section from %p and having %d tracepoints",
768 tracepoints_start, tracepoints_count);
769 if (ust_debug()) {
770 int i;
771
772 for (i = 0; i < tracepoints_count; i++) {
773 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
774 }
775 }
776
777 return 0;
778 }
779
780 int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start)
781 {
782 struct tracepoint_lib *lib;
783
784 pthread_mutex_lock(&tracepoint_mutex);
785 cds_list_for_each_entry(lib, &libs, list) {
786 if (lib->tracepoints_start != tracepoints_start)
787 continue;
788
789 cds_list_del(&lib->list);
790 /*
791 * Unregistering a callsite also decreases the
792 * callsite reference count of the corresponding
793 * tracepoint, and disables the tracepoint if
794 * the reference count drops to zero.
795 */
796 lib_unregister_callsites(lib);
797 DBG("just unregistered a tracepoints section from %p",
798 lib->tracepoints_start);
799 free(lib);
800 break;
801 }
802 pthread_mutex_unlock(&tracepoint_mutex);
803 return 0;
804 }
805
806 void init_tracepoint(void)
807 {
808 if (uatomic_xchg(&initialized, 1) == 1)
809 return;
810 init_usterr();
811 }
812
813 void exit_tracepoint(void)
814 {
815 initialized = 0;
816 }
817
818 /*
819 * Create the wrapper symbols.
820 */
821 #undef tp_rcu_read_lock_bp
822 #undef tp_rcu_read_unlock_bp
823 #undef tp_rcu_dereference_bp
824
825 void tp_rcu_read_lock_bp(void)
826 {
827 rcu_read_lock_bp();
828 }
829
830 void tp_rcu_read_unlock_bp(void)
831 {
832 rcu_read_unlock_bp();
833 }
834
835 void *tp_rcu_dereference_sym_bp(void *p)
836 {
837 return rcu_dereference_bp(p);
838 }
This page took 0.04623 seconds and 4 git commands to generate.