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