Increase tracepoint provider:name len limit to 255, add compile-time warning
[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
474d745f 48/* libraries that contain tracepoints (struct tracepoint_lib) */
0222e121 49static CDS_LIST_HEAD(libs);
474d745f 50
f99be407 51/*
17dfb34b
MD
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.
f99be407 57 */
f99be407
PMF
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)
10c56168 65static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 66
b27f8e75
MD
67static CDS_LIST_HEAD(old_probes);
68static int need_update;
69
f99be407
PMF
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 {
10c56168 77 struct cds_hlist_node hlist;
b979b346 78 struct tracepoint_probe *probes;
f99be407
PMF
79 int refcount; /* Number of times armed. 0 if disarmed. */
80 char name[0];
81};
82
83struct tp_probes {
84 union {
0222e121 85 struct cds_list_head list;
f99be407 86 } u;
b979b346 87 struct tracepoint_probe probes[0];
f99be407
PMF
88};
89
90static inline void *allocate_probes(int count)
91{
b979b346 92 struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe)
909bc43f 93 + sizeof(struct tp_probes));
f99be407
PMF
94 return p == NULL ? NULL : p->probes;
95}
96
f99be407
PMF
97static inline void release_probes(void *old)
98{
99 if (old) {
b728d87e 100 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 101 struct tp_probes, probes[0]);
474d745f 102 synchronize_rcu();
909bc43f 103 free(tp_probes);
f99be407
PMF
104 }
105}
106
107static void debug_print_probes(struct tracepoint_entry *entry)
108{
109 int i;
110
9dec086e 111 if (!tracepoint_debug || !entry->probes)
f99be407
PMF
112 return;
113
9dec086e
NC
114 for (i = 0; entry->probes[i].func; i++)
115 DBG("Probe %d : %p", i, entry->probes[i].func);
f99be407
PMF
116}
117
118static void *
9dec086e
NC
119tracepoint_entry_add_probe(struct tracepoint_entry *entry,
120 void *probe, void *data)
f99be407
PMF
121{
122 int nr_probes = 0;
b979b346 123 struct tracepoint_probe *old, *new;
f99be407
PMF
124
125 WARN_ON(!probe);
126
127 debug_print_probes(entry);
9dec086e 128 old = entry->probes;
f99be407
PMF
129 if (old) {
130 /* (N -> N+1), (N != 0, 1) probes */
9dec086e
NC
131 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
132 if (old[nr_probes].func == probe &&
133 old[nr_probes].data == data)
f99be407
PMF
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)
b979b346 141 memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe));
9dec086e
NC
142 new[nr_probes].func = probe;
143 new[nr_probes].data = data;
144 new[nr_probes + 1].func = NULL;
f99be407 145 entry->refcount = nr_probes + 1;
9dec086e 146 entry->probes = new;
f99be407
PMF
147 debug_print_probes(entry);
148 return old;
149}
150
151static void *
9dec086e
NC
152tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe,
153 void *data)
f99be407
PMF
154{
155 int nr_probes = 0, nr_del = 0, i;
b979b346 156 struct tracepoint_probe *old, *new;
f99be407 157
9dec086e 158 old = entry->probes;
f99be407
PMF
159
160 if (!old)
161 return ERR_PTR(-ENOENT);
162
163 debug_print_probes(entry);
164 /* (N -> M), (N > 1, M >= 0) probes */
9dec086e 165 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
c66428ac
DG
166 if (!probe ||
167 (old[nr_probes].func == probe &&
9dec086e 168 old[nr_probes].data == data))
f99be407
PMF
169 nr_del++;
170 }
171
172 if (nr_probes - nr_del == 0) {
173 /* N -> 0, (N > 1) */
9dec086e 174 entry->probes = NULL;
f99be407
PMF
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);
9dec086e
NC
185 for (i = 0; old[i].func; i++)
186 if (probe &&
187 (old[i].func != probe || old[i].data != data))
f99be407 188 new[j++] = old[i];
9dec086e 189 new[nr_probes - nr_del].func = NULL;
f99be407 190 entry->refcount = nr_probes - nr_del;
9dec086e 191 entry->probes = new;
f99be407
PMF
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{
10c56168
DG
204 struct cds_hlist_head *head;
205 struct cds_hlist_node *node;
f99be407 206 struct tracepoint_entry *e;
23c8854a 207 uint32_t hash = jhash(name, strlen(name), 0);
f99be407
PMF
208
209 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 210 cds_hlist_for_each_entry(e, node, head, hlist) {
f99be407
PMF
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{
10c56168
DG
223 struct cds_hlist_head *head;
224 struct cds_hlist_node *node;
f99be407
PMF
225 struct tracepoint_entry *e;
226 size_t name_len = strlen(name) + 1;
23c8854a 227 uint32_t hash = jhash(name, name_len-1, 0);
f99be407
PMF
228
229 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 230 cds_hlist_for_each_entry(e, node, head, hlist) {
f99be407 231 if (!strcmp(name, e->name)) {
c1f20530 232 DBG("tracepoint %s busy", name);
f99be407
PMF
233 return ERR_PTR(-EEXIST); /* Already there */
234 }
235 }
236 /*
1dba3e6c 237 * Using zmalloc here to allocate a variable length element. Could
f99be407
PMF
238 * cause some memory fragmentation if overused.
239 */
1dba3e6c 240 e = zmalloc(sizeof(struct tracepoint_entry) + name_len);
f99be407
PMF
241 if (!e)
242 return ERR_PTR(-ENOMEM);
243 memcpy(&e->name[0], name, name_len);
9dec086e 244 e->probes = NULL;
f99be407 245 e->refcount = 0;
10c56168 246 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
247 return e;
248}
249
250/*
251 * Remove the tracepoint from the tracepoint hash table. Must be called with
17dfb34b 252 * ust_lock held.
f99be407
PMF
253 */
254static inline void remove_tracepoint(struct tracepoint_entry *e)
255{
10c56168 256 cds_hlist_del(&e->hlist);
909bc43f 257 free(e);
f99be407
PMF
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 /*
0222e121 269 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
270 * probe callbacks array is consistent before setting a pointer to it.
271 * This array is referenced by __DO_TRACE from
0222e121 272 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
273 * is used.
274 */
9dec086e 275 rcu_assign_pointer(elem->probes, (*entry)->probes);
f36c12ab 276 elem->state = active;
f99be407
PMF
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{
f36c12ab 287 elem->state = 0;
9dec086e 288 rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
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 */
b27f8e75 298static
f218ff28
MD
299void tracepoint_update_probe_range(struct tracepoint * const *begin,
300 struct tracepoint * const *end)
f99be407 301{
f218ff28 302 struct tracepoint * const *iter;
f99be407
PMF
303 struct tracepoint_entry *mark_entry;
304
f99be407 305 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
306 if (!*iter)
307 continue; /* skip dummy */
f218ff28
MD
308 if (!(*iter)->name) {
309 disable_tracepoint(*iter);
9dec086e
NC
310 continue;
311 }
f218ff28 312 mark_entry = get_tracepoint((*iter)->name);
f99be407 313 if (mark_entry) {
f218ff28 314 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
315 !!mark_entry->refcount);
316 } else {
f218ff28 317 disable_tracepoint(*iter);
f99be407
PMF
318 }
319 }
f99be407
PMF
320}
321
772030fe
PMF
322static void lib_update_tracepoints(void)
323{
324 struct tracepoint_lib *lib;
325
b27f8e75 326 cds_list_for_each_entry(lib, &libs, list) {
772030fe
PMF
327 tracepoint_update_probe_range(lib->tracepoints_start,
328 lib->tracepoints_start + lib->tracepoints_count);
b27f8e75 329 }
772030fe
PMF
330}
331
f99be407
PMF
332/*
333 * Update probes, removing the faulty probes.
334 */
335static void tracepoint_update_probes(void)
336{
b27f8e75 337 /* tracepoints registered from libraries and executable. */
474d745f 338 lib_update_tracepoints();
f99be407
PMF
339}
340
b979b346 341static struct tracepoint_probe *
9dec086e 342tracepoint_add_probe(const char *name, void *probe, void *data)
f99be407
PMF
343{
344 struct tracepoint_entry *entry;
b979b346 345 struct tracepoint_probe *old;
f99be407
PMF
346
347 entry = get_tracepoint(name);
348 if (!entry) {
349 entry = add_tracepoint(name);
350 if (IS_ERR(entry))
b979b346 351 return (struct tracepoint_probe *)entry;
f99be407 352 }
9dec086e 353 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
354 if (IS_ERR(old) && !entry->refcount)
355 remove_tracepoint(entry);
356 return old;
357}
358
359/**
81614639 360 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
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.
17dfb34b 366 * Called with the UST lock held.
f99be407 367 */
81614639 368int __tracepoint_probe_register(const char *name, void *probe, void *data)
f99be407
PMF
369{
370 void *old;
371
9dec086e 372 old = tracepoint_add_probe(name, probe, data);
f99be407
PMF
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}
f99be407 380
9dec086e 381static void *tracepoint_remove_probe(const char *name, void *probe, void *data)
f99be407
PMF
382{
383 struct tracepoint_entry *entry;
384 void *old;
385
386 entry = get_tracepoint(name);
387 if (!entry)
388 return ERR_PTR(-ENOENT);
9dec086e 389 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
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
9dec086e 401 * @probe: probe data pointer
f99be407 402 *
17dfb34b 403 * Called with the UST lock held.
f99be407 404 */
81614639 405int __tracepoint_probe_unregister(const char *name, void *probe, void *data)
f99be407
PMF
406{
407 void *old;
408
9dec086e 409 old = tracepoint_remove_probe(name, probe, data);
f99be407
PMF
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}
f99be407 417
f99be407
PMF
418static void tracepoint_add_old_probes(void *old)
419{
420 need_update = 1;
421 if (old) {
b728d87e 422 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 423 struct tp_probes, probes[0]);
0222e121 424 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
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()
17dfb34b 434 * Called with the UST lock held.
f99be407 435 */
9dec086e
NC
436int tracepoint_probe_register_noupdate(const char *name, void *probe,
437 void *data)
f99be407
PMF
438{
439 void *old;
440
9dec086e 441 old = tracepoint_add_probe(name, probe, data);
f99be407 442 if (IS_ERR(old)) {
f99be407
PMF
443 return PTR_ERR(old);
444 }
445 tracepoint_add_old_probes(old);
f99be407
PMF
446 return 0;
447}
f99be407
PMF
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()
17dfb34b 455 * Called with the UST lock held.
f99be407 456 */
9dec086e
NC
457int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
458 void *data)
f99be407
PMF
459{
460 void *old;
461
9dec086e 462 old = tracepoint_remove_probe(name, probe, data);
f99be407 463 if (IS_ERR(old)) {
f99be407
PMF
464 return PTR_ERR(old);
465 }
466 tracepoint_add_old_probes(old);
f99be407
PMF
467 return 0;
468}
f99be407
PMF
469
470/**
471 * tracepoint_probe_update_all - update tracepoints
17dfb34b 472 * Called with the UST lock held.
f99be407
PMF
473 */
474void tracepoint_probe_update_all(void)
475{
0222e121 476 CDS_LIST_HEAD(release_probes);
f99be407
PMF
477 struct tp_probes *pos, *next;
478
f99be407 479 if (!need_update) {
f99be407
PMF
480 return;
481 }
0222e121
MD
482 if (!cds_list_empty(&old_probes))
483 cds_list_replace_init(&old_probes, &release_probes);
f99be407 484 need_update = 0;
f99be407
PMF
485
486 tracepoint_update_probes();
0222e121
MD
487 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
488 cds_list_del(&pos->u.list);
474d745f 489 synchronize_rcu();
909bc43f 490 free(pos);
f99be407
PMF
491 }
492}
f99be407 493
474d745f
PMF
494void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
495{
496 new_tracepoint_cb = cb;
497}
f99be407 498
f218ff28 499static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
f99be407 500{
f218ff28
MD
501 if (new_tracepoint_cb) {
502 struct tracepoint * const *t;
f08ebbe2 503
b27f8e75 504 for (t = start; t < end; t++) {
f08ebbe2
MD
505 if (*t)
506 new_tracepoint_cb(*t);
474d745f
PMF
507 }
508 }
f99be407 509}
f99be407 510
b27f8e75
MD
511int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
512 int tracepoints_count)
474d745f 513{
b467f7a7 514 struct tracepoint_lib *pl, *iter;
474d745f 515
edaa1431
MD
516 init_tracepoint();
517
1dba3e6c 518 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
474d745f
PMF
519
520 pl->tracepoints_start = tracepoints_start;
521 pl->tracepoints_count = tracepoints_count;
522
17dfb34b 523 ust_lock();
b467f7a7
MD
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 */
0222e121 536 cds_list_add(&pl->list, &libs);
b467f7a7 537lib_added:
474d745f
PMF
538 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
539
b27f8e75 540 /* TODO: update just the loaded lib */
474d745f 541 lib_update_tracepoints();
17dfb34b 542 ust_unlock();
474d745f 543
1fcf7ad7
MD
544 DBG("just registered a tracepoints section from %p and having %d tracepoints",
545 tracepoints_start, tracepoints_count);
9dec086e 546
474d745f
PMF
547 return 0;
548}
549
f218ff28 550int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
474d745f 551{
24b6668c
PMF
552 struct tracepoint_lib *lib;
553
17dfb34b 554 ust_lock();
0222e121 555 cds_list_for_each_entry(lib, &libs, list) {
f218ff28 556 if (lib->tracepoints_start == tracepoints_start) {
24b6668c 557 struct tracepoint_lib *lib2free = lib;
0222e121 558 cds_list_del(&lib->list);
24b6668c
PMF
559 free(lib2free);
560 break;
561 }
562 }
17dfb34b 563 ust_unlock();
474d745f
PMF
564
565 return 0;
566}
b27f8e75 567
edaa1431 568void init_tracepoint(void)
b27f8e75 569{
edaa1431
MD
570 if (uatomic_xchg(&initialized, 1) == 1)
571 return;
5e96a467 572 init_usterr();
b27f8e75
MD
573}
574
edaa1431 575void exit_tracepoint(void)
b27f8e75 576{
17dfb34b 577 initialized = 0;
b27f8e75 578}
This page took 0.068617 seconds and 4 git commands to generate.