Add sock flush buffer ctl API
[lttng-ust.git] / liblttng-ust / tracepoint.c
CommitLineData
f99be407 1/*
b27f8e75 2 * Copyright (C) 2008-2011 Mathieu Desnoyers
1f8b0dff 3 * Copyright (C) 2009 Pierre-Marc Fournier
f99be407 4 *
34e4b7db
PMF
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
f37142a4
MD
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
f99be407 9 *
34e4b7db 10 * This library is distributed in the hope that it will be useful,
f99be407 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34e4b7db
PMF
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
f99be407 14 *
34e4b7db
PMF
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
1f8b0dff
PMF
18 *
19 * Ported to userspace by Pierre-Marc Fournier.
f99be407 20 */
1f8b0dff 21
b0c4126f 22#define _LGPL_SOURCE
474d745f 23#include <errno.h>
b728d87e
MD
24#include <stdint.h>
25#include <stddef.h>
44c72f10 26
b728d87e 27#include <urcu/arch.h>
b7ea1a1c 28#include <urcu-bp.h>
10c56168 29#include <urcu/hlist.h>
edaa1431 30#include <urcu/uatomic.h>
b728d87e 31#include <urcu/compiler.h>
44c72f10
MD
32
33#include <lttng/tracepoint.h>
34
35#include <usterr-signal-safe.h>
35897f8b 36#include <helper.h>
474d745f 37
23c8854a 38#include "tracepoint-internal.h"
b751f722 39#include "ltt-tracer-core.h"
596c4223 40#include "jhash.h"
8f3f8c99 41#include "error.h"
b0c4126f 42
f99be407
PMF
43/* Set to 1 to enable tracepoint debug output */
44static const int tracepoint_debug;
b27f8e75
MD
45static int initialized;
46static void (*new_tracepoint_cb)(struct tracepoint *);
f99be407 47
efa2c591
MD
48/*
49 * libraries that contain tracepoints (struct tracepoint_lib).
50 * Protected by UST lock.
51 */
0222e121 52static CDS_LIST_HEAD(libs);
474d745f 53
f99be407 54/*
17dfb34b
MD
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.
f99be407 60 */
f99be407
PMF
61
62/*
63 * Tracepoint hash table, containing the active tracepoints.
efa2c591 64 * Protected by ust lock.
f99be407
PMF
65 */
66#define TRACEPOINT_HASH_BITS 6
67#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
10c56168 68static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 69
b27f8e75
MD
70static CDS_LIST_HEAD(old_probes);
71static int need_update;
72
f99be407
PMF
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.
efa2c591 77 * Tracepoint entries modifications are protected by the ust lock.
f99be407
PMF
78 */
79struct tracepoint_entry {
10c56168 80 struct cds_hlist_node hlist;
b979b346 81 struct tracepoint_probe *probes;
f99be407
PMF
82 int refcount; /* Number of times armed. 0 if disarmed. */
83 char name[0];
84};
85
86struct tp_probes {
87 union {
0222e121 88 struct cds_list_head list;
f99be407 89 } u;
b979b346 90 struct tracepoint_probe probes[0];
f99be407
PMF
91};
92
efa2c591 93static void *allocate_probes(int count)
f99be407 94{
b979b346 95 struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe)
909bc43f 96 + sizeof(struct tp_probes));
f99be407
PMF
97 return p == NULL ? NULL : p->probes;
98}
99
efa2c591 100static void release_probes(void *old)
f99be407
PMF
101{
102 if (old) {
b728d87e 103 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 104 struct tp_probes, probes[0]);
474d745f 105 synchronize_rcu();
909bc43f 106 free(tp_probes);
f99be407
PMF
107 }
108}
109
110static void debug_print_probes(struct tracepoint_entry *entry)
111{
112 int i;
113
9dec086e 114 if (!tracepoint_debug || !entry->probes)
f99be407
PMF
115 return;
116
9dec086e
NC
117 for (i = 0; entry->probes[i].func; i++)
118 DBG("Probe %d : %p", i, entry->probes[i].func);
f99be407
PMF
119}
120
121static void *
9dec086e
NC
122tracepoint_entry_add_probe(struct tracepoint_entry *entry,
123 void *probe, void *data)
f99be407
PMF
124{
125 int nr_probes = 0;
b979b346 126 struct tracepoint_probe *old, *new;
f99be407
PMF
127
128 WARN_ON(!probe);
129
130 debug_print_probes(entry);
9dec086e 131 old = entry->probes;
f99be407
PMF
132 if (old) {
133 /* (N -> N+1), (N != 0, 1) probes */
9dec086e
NC
134 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
135 if (old[nr_probes].func == probe &&
136 old[nr_probes].data == data)
f99be407
PMF
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)
b979b346 144 memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe));
9dec086e
NC
145 new[nr_probes].func = probe;
146 new[nr_probes].data = data;
147 new[nr_probes + 1].func = NULL;
f99be407 148 entry->refcount = nr_probes + 1;
9dec086e 149 entry->probes = new;
f99be407
PMF
150 debug_print_probes(entry);
151 return old;
152}
153
154static void *
9dec086e
NC
155tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe,
156 void *data)
f99be407
PMF
157{
158 int nr_probes = 0, nr_del = 0, i;
b979b346 159 struct tracepoint_probe *old, *new;
f99be407 160
9dec086e 161 old = entry->probes;
f99be407
PMF
162
163 if (!old)
164 return ERR_PTR(-ENOENT);
165
166 debug_print_probes(entry);
167 /* (N -> M), (N > 1, M >= 0) probes */
9dec086e 168 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
c66428ac
DG
169 if (!probe ||
170 (old[nr_probes].func == probe &&
9dec086e 171 old[nr_probes].data == data))
f99be407
PMF
172 nr_del++;
173 }
174
175 if (nr_probes - nr_del == 0) {
176 /* N -> 0, (N > 1) */
9dec086e 177 entry->probes = NULL;
f99be407
PMF
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);
9dec086e
NC
188 for (i = 0; old[i].func; i++)
189 if (probe &&
190 (old[i].func != probe || old[i].data != data))
f99be407 191 new[j++] = old[i];
9dec086e 192 new[nr_probes - nr_del].func = NULL;
f99be407 193 entry->refcount = nr_probes - nr_del;
9dec086e 194 entry->probes = new;
f99be407
PMF
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.
efa2c591 202 * Must be called with ust lock held.
f99be407
PMF
203 * Returns NULL if not present.
204 */
205static struct tracepoint_entry *get_tracepoint(const char *name)
206{
10c56168
DG
207 struct cds_hlist_head *head;
208 struct cds_hlist_node *node;
f99be407 209 struct tracepoint_entry *e;
23c8854a 210 uint32_t hash = jhash(name, strlen(name), 0);
f99be407
PMF
211
212 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 213 cds_hlist_for_each_entry(e, node, head, hlist) {
f99be407
PMF
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
efa2c591 222 * ust lock held.
f99be407
PMF
223 */
224static struct tracepoint_entry *add_tracepoint(const char *name)
225{
10c56168
DG
226 struct cds_hlist_head *head;
227 struct cds_hlist_node *node;
f99be407
PMF
228 struct tracepoint_entry *e;
229 size_t name_len = strlen(name) + 1;
23c8854a 230 uint32_t hash = jhash(name, name_len-1, 0);
f99be407
PMF
231
232 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 233 cds_hlist_for_each_entry(e, node, head, hlist) {
f99be407 234 if (!strcmp(name, e->name)) {
c1f20530 235 DBG("tracepoint %s busy", name);
f99be407
PMF
236 return ERR_PTR(-EEXIST); /* Already there */
237 }
238 }
239 /*
1dba3e6c 240 * Using zmalloc here to allocate a variable length element. Could
f99be407
PMF
241 * cause some memory fragmentation if overused.
242 */
1dba3e6c 243 e = zmalloc(sizeof(struct tracepoint_entry) + name_len);
f99be407
PMF
244 if (!e)
245 return ERR_PTR(-ENOMEM);
246 memcpy(&e->name[0], name, name_len);
9dec086e 247 e->probes = NULL;
f99be407 248 e->refcount = 0;
10c56168 249 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
250 return e;
251}
252
253/*
254 * Remove the tracepoint from the tracepoint hash table. Must be called with
17dfb34b 255 * ust_lock held.
f99be407 256 */
efa2c591 257static void remove_tracepoint(struct tracepoint_entry *e)
f99be407 258{
10c56168 259 cds_hlist_del(&e->hlist);
909bc43f 260 free(e);
f99be407
PMF
261}
262
263/*
264 * Sets the probe callback corresponding to one tracepoint.
265 */
266static 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 /*
0222e121 272 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
273 * probe callbacks array is consistent before setting a pointer to it.
274 * This array is referenced by __DO_TRACE from
0222e121 275 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
276 * is used.
277 */
9dec086e 278 rcu_assign_pointer(elem->probes, (*entry)->probes);
f36c12ab 279 elem->state = active;
f99be407
PMF
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 */
288static void disable_tracepoint(struct tracepoint *elem)
289{
f36c12ab 290 elem->state = 0;
9dec086e 291 rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
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 */
b27f8e75 301static
f218ff28
MD
302void tracepoint_update_probe_range(struct tracepoint * const *begin,
303 struct tracepoint * const *end)
f99be407 304{
f218ff28 305 struct tracepoint * const *iter;
f99be407
PMF
306 struct tracepoint_entry *mark_entry;
307
f99be407 308 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
309 if (!*iter)
310 continue; /* skip dummy */
f218ff28
MD
311 if (!(*iter)->name) {
312 disable_tracepoint(*iter);
9dec086e
NC
313 continue;
314 }
f218ff28 315 mark_entry = get_tracepoint((*iter)->name);
f99be407 316 if (mark_entry) {
f218ff28 317 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
318 !!mark_entry->refcount);
319 } else {
f218ff28 320 disable_tracepoint(*iter);
f99be407
PMF
321 }
322 }
f99be407
PMF
323}
324
772030fe
PMF
325static void lib_update_tracepoints(void)
326{
327 struct tracepoint_lib *lib;
328
b27f8e75 329 cds_list_for_each_entry(lib, &libs, list) {
772030fe
PMF
330 tracepoint_update_probe_range(lib->tracepoints_start,
331 lib->tracepoints_start + lib->tracepoints_count);
b27f8e75 332 }
772030fe
PMF
333}
334
f99be407
PMF
335/*
336 * Update probes, removing the faulty probes.
337 */
338static void tracepoint_update_probes(void)
339{
b27f8e75 340 /* tracepoints registered from libraries and executable. */
474d745f 341 lib_update_tracepoints();
f99be407
PMF
342}
343
b979b346 344static struct tracepoint_probe *
9dec086e 345tracepoint_add_probe(const char *name, void *probe, void *data)
f99be407
PMF
346{
347 struct tracepoint_entry *entry;
b979b346 348 struct tracepoint_probe *old;
f99be407
PMF
349
350 entry = get_tracepoint(name);
351 if (!entry) {
352 entry = add_tracepoint(name);
353 if (IS_ERR(entry))
b979b346 354 return (struct tracepoint_probe *)entry;
f99be407 355 }
9dec086e 356 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
357 if (IS_ERR(old) && !entry->refcount)
358 remove_tracepoint(entry);
359 return old;
360}
361
362/**
81614639 363 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
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.
17dfb34b 369 * Called with the UST lock held.
f99be407 370 */
81614639 371int __tracepoint_probe_register(const char *name, void *probe, void *data)
f99be407
PMF
372{
373 void *old;
374
9dec086e 375 old = tracepoint_add_probe(name, probe, data);
f99be407
PMF
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}
f99be407 383
9dec086e 384static void *tracepoint_remove_probe(const char *name, void *probe, void *data)
f99be407
PMF
385{
386 struct tracepoint_entry *entry;
387 void *old;
388
389 entry = get_tracepoint(name);
390 if (!entry)
391 return ERR_PTR(-ENOENT);
9dec086e 392 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
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
9dec086e 404 * @probe: probe data pointer
f99be407 405 *
17dfb34b 406 * Called with the UST lock held.
f99be407 407 */
81614639 408int __tracepoint_probe_unregister(const char *name, void *probe, void *data)
f99be407
PMF
409{
410 void *old;
411
9dec086e 412 old = tracepoint_remove_probe(name, probe, data);
f99be407
PMF
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}
f99be407 420
f99be407
PMF
421static void tracepoint_add_old_probes(void *old)
422{
423 need_update = 1;
424 if (old) {
b728d87e 425 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 426 struct tp_probes, probes[0]);
0222e121 427 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
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()
17dfb34b 437 * Called with the UST lock held.
f99be407 438 */
9dec086e
NC
439int tracepoint_probe_register_noupdate(const char *name, void *probe,
440 void *data)
f99be407
PMF
441{
442 void *old;
443
9dec086e 444 old = tracepoint_add_probe(name, probe, data);
f99be407 445 if (IS_ERR(old)) {
f99be407
PMF
446 return PTR_ERR(old);
447 }
448 tracepoint_add_old_probes(old);
f99be407
PMF
449 return 0;
450}
f99be407
PMF
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()
17dfb34b 458 * Called with the UST lock held.
f99be407 459 */
9dec086e
NC
460int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
461 void *data)
f99be407
PMF
462{
463 void *old;
464
9dec086e 465 old = tracepoint_remove_probe(name, probe, data);
f99be407 466 if (IS_ERR(old)) {
f99be407
PMF
467 return PTR_ERR(old);
468 }
469 tracepoint_add_old_probes(old);
f99be407
PMF
470 return 0;
471}
f99be407
PMF
472
473/**
474 * tracepoint_probe_update_all - update tracepoints
17dfb34b 475 * Called with the UST lock held.
f99be407
PMF
476 */
477void tracepoint_probe_update_all(void)
478{
0222e121 479 CDS_LIST_HEAD(release_probes);
f99be407
PMF
480 struct tp_probes *pos, *next;
481
f99be407 482 if (!need_update) {
f99be407
PMF
483 return;
484 }
0222e121
MD
485 if (!cds_list_empty(&old_probes))
486 cds_list_replace_init(&old_probes, &release_probes);
f99be407 487 need_update = 0;
f99be407
PMF
488
489 tracepoint_update_probes();
0222e121
MD
490 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
491 cds_list_del(&pos->u.list);
474d745f 492 synchronize_rcu();
909bc43f 493 free(pos);
f99be407
PMF
494 }
495}
f99be407 496
474d745f
PMF
497void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
498{
499 new_tracepoint_cb = cb;
500}
f99be407 501
f218ff28 502static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
f99be407 503{
f218ff28
MD
504 if (new_tracepoint_cb) {
505 struct tracepoint * const *t;
f08ebbe2 506
b27f8e75 507 for (t = start; t < end; t++) {
f08ebbe2
MD
508 if (*t)
509 new_tracepoint_cb(*t);
474d745f
PMF
510 }
511 }
f99be407 512}
f99be407 513
b27f8e75
MD
514int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
515 int tracepoints_count)
474d745f 516{
b467f7a7 517 struct tracepoint_lib *pl, *iter;
474d745f 518
edaa1431
MD
519 init_tracepoint();
520
1dba3e6c 521 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
474d745f
PMF
522
523 pl->tracepoints_start = tracepoints_start;
524 pl->tracepoints_count = tracepoints_count;
525
17dfb34b 526 ust_lock();
b467f7a7
MD
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 */
0222e121 539 cds_list_add(&pl->list, &libs);
b467f7a7 540lib_added:
474d745f
PMF
541 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
542
b27f8e75 543 /* TODO: update just the loaded lib */
474d745f 544 lib_update_tracepoints();
17dfb34b 545 ust_unlock();
474d745f 546
1fcf7ad7
MD
547 DBG("just registered a tracepoints section from %p and having %d tracepoints",
548 tracepoints_start, tracepoints_count);
9dec086e 549
474d745f
PMF
550 return 0;
551}
552
f218ff28 553int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
474d745f 554{
24b6668c
PMF
555 struct tracepoint_lib *lib;
556
17dfb34b 557 ust_lock();
0222e121 558 cds_list_for_each_entry(lib, &libs, list) {
f218ff28 559 if (lib->tracepoints_start == tracepoints_start) {
24b6668c 560 struct tracepoint_lib *lib2free = lib;
0222e121 561 cds_list_del(&lib->list);
24b6668c
PMF
562 free(lib2free);
563 break;
564 }
565 }
17dfb34b 566 ust_unlock();
474d745f
PMF
567
568 return 0;
569}
b27f8e75 570
edaa1431 571void init_tracepoint(void)
b27f8e75 572{
edaa1431
MD
573 if (uatomic_xchg(&initialized, 1) == 1)
574 return;
5e96a467 575 init_usterr();
b27f8e75
MD
576}
577
edaa1431 578void exit_tracepoint(void)
b27f8e75 579{
17dfb34b 580 initialized = 0;
b27f8e75 581}
40b2b5a4
MD
582
583/*
584 * Create the wrapper symbols.
585 */
586#undef tp_rcu_read_lock_bp
587#undef tp_rcu_read_unlock_bp
588#undef tp_rcu_dereference_bp
589
590void tp_rcu_read_lock_bp(void)
591{
592 rcu_read_lock_bp();
593}
594
595void tp_rcu_read_unlock_bp(void)
596{
597 rcu_read_unlock_bp();
598}
599
600void *tp_rcu_dereference_sym_bp(void *p)
601{
602 return rcu_dereference_bp(p);
603}
This page took 0.05986 seconds and 4 git commands to generate.