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