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