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