Use iterator on tracepoint probes instead of tracepoints per se
[lttng-ust.git] / liblttng-ust / tracepoint.c
... / ...
CommitLineData
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 */
44static const int tracepoint_debug;
45static int initialized;
46static void (*new_tracepoint_cb)(struct tracepoint *);
47
48/* libraries that contain tracepoints (struct tracepoint_lib) */
49static 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)
65static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
66
67static CDS_LIST_HEAD(old_probes);
68static 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 */
76struct 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
83struct tp_probes {
84 union {
85 struct cds_list_head list;
86 } u;
87 struct tracepoint_probe probes[0];
88};
89
90static 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
97static 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
107static 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
118static void *
119tracepoint_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
151static void *
152tracepoint_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 */
202static 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 */
221static 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 */
254static 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 */
263static 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 */
285static 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 */
298static
299void 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
322static 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 */
335static void tracepoint_update_probes(void)
336{
337 /* tracepoints registered from libraries and executable. */
338 lib_update_tracepoints();
339}
340
341static struct tracepoint_probe *
342tracepoint_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 */
368int __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
381static 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 */
405int __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
418static 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 */
436int 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 */
457int 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 */
474void 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
494void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
495{
496 new_tracepoint_cb = cb;
497}
498
499static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
500{
501 if (new_tracepoint_cb) {
502 struct tracepoint * const *t;
503
504 for (t = start; t < end; t++) {
505 if (*t)
506 new_tracepoint_cb(*t);
507 }
508 }
509}
510
511int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
512 int tracepoints_count)
513{
514 struct tracepoint_lib *pl, *iter;
515
516 init_tracepoint();
517
518 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
519
520 pl->tracepoints_start = tracepoints_start;
521 pl->tracepoints_count = tracepoints_count;
522
523 ust_lock();
524 /*
525 * We sort the libs by struct lib pointer address.
526 */
527 cds_list_for_each_entry_reverse(iter, &libs, list) {
528 BUG_ON(iter == pl); /* Should never be in the list twice */
529 if (iter < pl) {
530 /* We belong to the location right after iter. */
531 cds_list_add(&pl->list, &iter->list);
532 goto lib_added;
533 }
534 }
535 /* We should be added at the head of the list */
536 cds_list_add(&pl->list, &libs);
537lib_added:
538 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
539
540 /* TODO: update just the loaded lib */
541 lib_update_tracepoints();
542 ust_unlock();
543
544 DBG("just registered a tracepoints section from %p and having %d tracepoints",
545 tracepoints_start, tracepoints_count);
546
547 return 0;
548}
549
550int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
551{
552 struct tracepoint_lib *lib;
553
554 ust_lock();
555 cds_list_for_each_entry(lib, &libs, list) {
556 if (lib->tracepoints_start == tracepoints_start) {
557 struct tracepoint_lib *lib2free = lib;
558 cds_list_del(&lib->list);
559 free(lib2free);
560 break;
561 }
562 }
563 ust_unlock();
564
565 return 0;
566}
567
568void init_tracepoint(void)
569{
570 if (uatomic_xchg(&initialized, 1) == 1)
571 return;
572 init_usterr();
573}
574
575void exit_tracepoint(void)
576{
577 initialized = 0;
578}
This page took 0.02482 seconds and 4 git commands to generate.