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