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