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