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