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