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