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