Rename public structure to avoid collisions
[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>
33661f97 26#include <stdio.h>
44c72f10 27
b728d87e 28#include <urcu/arch.h>
b7ea1a1c 29#include <urcu-bp.h>
10c56168 30#include <urcu/hlist.h>
edaa1431 31#include <urcu/uatomic.h>
b728d87e 32#include <urcu/compiler.h>
44c72f10
MD
33
34#include <lttng/tracepoint.h>
ff412fb5 35#include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
44c72f10
MD
36
37#include <usterr-signal-safe.h>
35897f8b 38#include <helper.h>
474d745f 39
23c8854a 40#include "tracepoint-internal.h"
7dd08bec 41#include "lttng-tracer-core.h"
596c4223 42#include "jhash.h"
8f3f8c99 43#include "error.h"
b0c4126f 44
f99be407
PMF
45/* Set to 1 to enable tracepoint debug output */
46static const int tracepoint_debug;
b27f8e75 47static int initialized;
1a206094 48static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *);
f99be407 49
8792fbae
MD
50/*
51 * tracepoint_mutex nests inside UST mutex.
52 *
53 * Note about interaction with fork/clone: UST does not hold the
54 * tracepoint mutex across fork/clone because it is either:
55 * - nested within UST mutex, in which case holding the UST mutex across
56 * fork/clone suffice,
57 * - taken by a library constructor, which should never race with a
58 * fork/clone if the application is expected to continue running with
59 * the same memory layout (no following exec()).
60 */
61static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
62
efa2c591
MD
63/*
64 * libraries that contain tracepoints (struct tracepoint_lib).
8792fbae 65 * Protected by tracepoint mutex.
efa2c591 66 */
0222e121 67static CDS_LIST_HEAD(libs);
474d745f 68
f99be407 69/*
8792fbae 70 * The tracepoint mutex protects the library tracepoints, the hash table, and
17dfb34b 71 * the library list.
8792fbae 72 * All calls to the tracepoint API must be protected by the tracepoint mutex,
17dfb34b 73 * excepts calls to tracepoint_register_lib and
8792fbae 74 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
f99be407 75 */
f99be407
PMF
76
77/*
78 * Tracepoint hash table, containing the active tracepoints.
8792fbae 79 * Protected by tracepoint mutex.
f99be407 80 */
814f7df1 81#define TRACEPOINT_HASH_BITS 12
f99be407 82#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
10c56168 83static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 84
b27f8e75
MD
85static CDS_LIST_HEAD(old_probes);
86static int need_update;
87
f99be407
PMF
88/*
89 * Note about RCU :
90 * It is used to to delay the free of multiple probes array until a quiescent
91 * state is reached.
8792fbae 92 * Tracepoint entries modifications are protected by the tracepoint mutex.
f99be407
PMF
93 */
94struct tracepoint_entry {
10c56168 95 struct cds_hlist_node hlist;
1a206094 96 struct lttng_ust_tracepoint_probe *probes;
f99be407 97 int refcount; /* Number of times armed. 0 if disarmed. */
8a7ad54b 98 int callsite_refcount; /* how many libs use this tracepoint */
67e5f391 99 const char *signature;
f99be407
PMF
100 char name[0];
101};
102
103struct tp_probes {
104 union {
0222e121 105 struct cds_list_head list;
ade7037b
MD
106 /* Field below only used for call_rcu scheme */
107 /* struct rcu_head head; */
f99be407 108 } u;
1a206094 109 struct lttng_ust_tracepoint_probe probes[0];
f99be407
PMF
110};
111
3469bbbe
MD
112/*
113 * Callsite hash table, containing the tracepoint call sites.
114 * Protected by tracepoint mutex.
115 */
116#define CALLSITE_HASH_BITS 12
117#define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS)
118static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE];
119
120struct callsite_entry {
121 struct cds_hlist_node hlist; /* hash table node */
122 struct cds_list_head node; /* lib list of callsites node */
1a206094 123 struct lttng_ust_tracepoint *tp;
3469bbbe
MD
124};
125
5e6df7ea 126/* coverity[+alloc] */
efa2c591 127static void *allocate_probes(int count)
f99be407 128{
1a206094
SM
129 struct tp_probes *p =
130 zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe)
131 + sizeof(struct tp_probes));
f99be407
PMF
132 return p == NULL ? NULL : p->probes;
133}
134
5e6df7ea 135/* coverity[+free : arg-0] */
efa2c591 136static void release_probes(void *old)
f99be407
PMF
137{
138 if (old) {
b728d87e 139 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 140 struct tp_probes, probes[0]);
474d745f 141 synchronize_rcu();
909bc43f 142 free(tp_probes);
f99be407
PMF
143 }
144}
145
146static void debug_print_probes(struct tracepoint_entry *entry)
147{
148 int i;
149
9dec086e 150 if (!tracepoint_debug || !entry->probes)
f99be407
PMF
151 return;
152
9dec086e
NC
153 for (i = 0; entry->probes[i].func; i++)
154 DBG("Probe %d : %p", i, entry->probes[i].func);
f99be407
PMF
155}
156
157static void *
9dec086e 158tracepoint_entry_add_probe(struct tracepoint_entry *entry,
fbdeb5ec 159 void (*probe)(void), void *data)
f99be407
PMF
160{
161 int nr_probes = 0;
1a206094 162 struct lttng_ust_tracepoint_probe *old, *new;
f99be407 163
d7509147
MD
164 if (!probe) {
165 WARN_ON(1);
166 return ERR_PTR(-EINVAL);
167 }
f99be407 168 debug_print_probes(entry);
9dec086e 169 old = entry->probes;
f99be407
PMF
170 if (old) {
171 /* (N -> N+1), (N != 0, 1) probes */
9dec086e
NC
172 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
173 if (old[nr_probes].func == probe &&
174 old[nr_probes].data == data)
f99be407
PMF
175 return ERR_PTR(-EEXIST);
176 }
177 /* + 2 : one for new probe, one for NULL func */
178 new = allocate_probes(nr_probes + 2);
179 if (new == NULL)
180 return ERR_PTR(-ENOMEM);
181 if (old)
1a206094
SM
182 memcpy(new, old,
183 nr_probes * sizeof(struct lttng_ust_tracepoint_probe));
9dec086e
NC
184 new[nr_probes].func = probe;
185 new[nr_probes].data = data;
186 new[nr_probes + 1].func = NULL;
f99be407 187 entry->refcount = nr_probes + 1;
9dec086e 188 entry->probes = new;
f99be407
PMF
189 debug_print_probes(entry);
190 return old;
191}
192
193static void *
fbdeb5ec
MD
194tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
195 void (*probe)(void), void *data)
f99be407
PMF
196{
197 int nr_probes = 0, nr_del = 0, i;
1a206094 198 struct lttng_ust_tracepoint_probe *old, *new;
f99be407 199
9dec086e 200 old = entry->probes;
f99be407
PMF
201
202 if (!old)
203 return ERR_PTR(-ENOENT);
204
205 debug_print_probes(entry);
206 /* (N -> M), (N > 1, M >= 0) probes */
956c6fab
MD
207 if (probe) {
208 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
209 if (old[nr_probes].func == probe &&
210 old[nr_probes].data == data)
211 nr_del++;
212 }
f99be407
PMF
213 }
214
215 if (nr_probes - nr_del == 0) {
216 /* N -> 0, (N > 1) */
9dec086e 217 entry->probes = NULL;
f99be407
PMF
218 entry->refcount = 0;
219 debug_print_probes(entry);
220 return old;
221 } else {
222 int j = 0;
223 /* N -> M, (N > 1, M > 0) */
224 /* + 1 for NULL */
225 new = allocate_probes(nr_probes - nr_del + 1);
226 if (new == NULL)
227 return ERR_PTR(-ENOMEM);
9dec086e 228 for (i = 0; old[i].func; i++)
956c6fab 229 if (old[i].func != probe || old[i].data != data)
f99be407 230 new[j++] = old[i];
9dec086e 231 new[nr_probes - nr_del].func = NULL;
f99be407 232 entry->refcount = nr_probes - nr_del;
9dec086e 233 entry->probes = new;
f99be407
PMF
234 }
235 debug_print_probes(entry);
236 return old;
237}
238
239/*
240 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
8792fbae 241 * Must be called with tracepoint mutex held.
f99be407
PMF
242 * Returns NULL if not present.
243 */
244static struct tracepoint_entry *get_tracepoint(const char *name)
245{
10c56168
DG
246 struct cds_hlist_head *head;
247 struct cds_hlist_node *node;
f99be407 248 struct tracepoint_entry *e;
ff412fb5
MD
249 size_t name_len = strlen(name);
250 uint32_t hash;
f99be407 251
ff412fb5
MD
252 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
253 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
254 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
255 }
256 hash = jhash(name, name_len, 0);
f99be407 257 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 258 cds_hlist_for_each_entry(e, node, head, hlist) {
ff412fb5 259 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
f99be407
PMF
260 return e;
261 }
262 return NULL;
263}
264
265/*
266 * Add the tracepoint to the tracepoint hash table. Must be called with
8792fbae 267 * tracepoint mutex held.
f99be407 268 */
67e5f391
MD
269static struct tracepoint_entry *add_tracepoint(const char *name,
270 const char *signature)
f99be407 271{
10c56168
DG
272 struct cds_hlist_head *head;
273 struct cds_hlist_node *node;
f99be407 274 struct tracepoint_entry *e;
ff412fb5
MD
275 size_t name_len = strlen(name);
276 uint32_t hash;
f99be407 277
ff412fb5
MD
278 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
279 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
280 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
281 }
282 hash = jhash(name, name_len, 0);
f99be407 283 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 284 cds_hlist_for_each_entry(e, node, head, hlist) {
ff412fb5 285 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
c1f20530 286 DBG("tracepoint %s busy", name);
f99be407
PMF
287 return ERR_PTR(-EEXIST); /* Already there */
288 }
289 }
290 /*
1dba3e6c 291 * Using zmalloc here to allocate a variable length element. Could
f99be407
PMF
292 * cause some memory fragmentation if overused.
293 */
ff412fb5 294 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
f99be407
PMF
295 if (!e)
296 return ERR_PTR(-ENOMEM);
ff412fb5
MD
297 memcpy(&e->name[0], name, name_len + 1);
298 e->name[name_len] = '\0';
9dec086e 299 e->probes = NULL;
f99be407 300 e->refcount = 0;
8a7ad54b 301 e->callsite_refcount = 0;
67e5f391 302 e->signature = signature;
10c56168 303 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
304 return e;
305}
306
307/*
308 * Remove the tracepoint from the tracepoint hash table. Must be called with
8792fbae 309 * tracepoint mutex held.
f99be407 310 */
efa2c591 311static void remove_tracepoint(struct tracepoint_entry *e)
f99be407 312{
10c56168 313 cds_hlist_del(&e->hlist);
909bc43f 314 free(e);
f99be407
PMF
315}
316
317/*
318 * Sets the probe callback corresponding to one tracepoint.
319 */
320static void set_tracepoint(struct tracepoint_entry **entry,
1a206094 321 struct lttng_ust_tracepoint *elem, int active)
f99be407 322{
ff412fb5 323 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
67e5f391
MD
324 /*
325 * Check that signatures match before connecting a probe to a
326 * tracepoint. Warn the user if they don't.
327 */
328 if (strcmp(elem->signature, (*entry)->signature) != 0) {
329 static int warned = 0;
330
331 /* Only print once, don't flood console. */
332 if (!warned) {
333 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
334 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
335 elem->name, elem->signature, (*entry)->signature);
336 warned = 1;
337 }
338 /* Don't accept connecting non-matching signatures. */
339 return;
340 }
f99be407
PMF
341
342 /*
0222e121 343 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
344 * probe callbacks array is consistent before setting a pointer to it.
345 * This array is referenced by __DO_TRACE from
0222e121 346 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
347 * is used.
348 */
9dec086e 349 rcu_assign_pointer(elem->probes, (*entry)->probes);
f36c12ab 350 elem->state = active;
f99be407
PMF
351}
352
353/*
354 * Disable a tracepoint and its probe callback.
355 * Note: only waiting an RCU period after setting elem->call to the empty
356 * function insures that the original callback is not used anymore. This insured
357 * by preempt_disable around the call site.
358 */
1a206094 359static void disable_tracepoint(struct lttng_ust_tracepoint *elem)
f99be407 360{
f36c12ab 361 elem->state = 0;
9dec086e 362 rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
363}
364
33f8ed87
MD
365/*
366 * Add the callsite to the callsite hash table. Must be called with
367 * tracepoint mutex held.
368 */
1a206094 369static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp)
33f8ed87
MD
370{
371 struct cds_hlist_head *head;
372 struct callsite_entry *e;
373 const char *name = tp->name;
374 size_t name_len = strlen(name);
375 uint32_t hash;
8a7ad54b 376 struct tracepoint_entry *tp_entry;
33f8ed87
MD
377
378 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
379 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
380 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
381 }
382 hash = jhash(name, name_len, 0);
383 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
384 e = zmalloc(sizeof(struct callsite_entry));
d6297168
MD
385 if (!e) {
386 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
387 return;
388 }
33f8ed87
MD
389 cds_hlist_add_head(&e->hlist, head);
390 e->tp = tp;
391 cds_list_add(&e->node, &lib->callsites);
8a7ad54b
IJ
392
393 tp_entry = get_tracepoint(name);
394 if (!tp_entry)
395 return;
396 tp_entry->callsite_refcount++;
33f8ed87
MD
397}
398
399/*
400 * Remove the callsite from the callsite hash table and from lib
401 * callsite list. Must be called with tracepoint mutex held.
402 */
403static void remove_callsite(struct callsite_entry *e)
404{
8a7ad54b
IJ
405 struct tracepoint_entry *tp_entry;
406
407 tp_entry = get_tracepoint(e->tp->name);
408 if (tp_entry) {
409 tp_entry->callsite_refcount--;
410 if (tp_entry->callsite_refcount == 0)
411 disable_tracepoint(e->tp);
412 }
33f8ed87
MD
413 cds_hlist_del(&e->hlist);
414 cds_list_del(&e->node);
415 free(e);
416}
417
3469bbbe
MD
418/*
419 * Enable/disable all callsites based on the state of a specific
420 * tracepoint entry.
421 * Must be called with tracepoint mutex held.
422 */
423static void tracepoint_sync_callsites(const char *name)
424{
425 struct cds_hlist_head *head;
426 struct cds_hlist_node *node;
427 struct callsite_entry *e;
428 size_t name_len = strlen(name);
429 uint32_t hash;
430 struct tracepoint_entry *tp_entry;
431
432 tp_entry = get_tracepoint(name);
433 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
434 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
435 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
436 }
437 hash = jhash(name, name_len, 0);
438 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
439 cds_hlist_for_each_entry(e, node, head, hlist) {
1a206094 440 struct lttng_ust_tracepoint *tp = e->tp;
3469bbbe
MD
441
442 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
443 continue;
444 if (tp_entry) {
445 set_tracepoint(&tp_entry, tp,
446 !!tp_entry->refcount);
447 } else {
448 disable_tracepoint(tp);
449 }
450 }
451}
452
f99be407
PMF
453/**
454 * tracepoint_update_probe_range - Update a probe range
455 * @begin: beginning of the range
456 * @end: end of the range
457 *
458 * Updates the probe callback corresponding to a range of tracepoints.
459 */
b27f8e75 460static
1a206094
SM
461void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin,
462 struct lttng_ust_tracepoint * const *end)
f99be407 463{
1a206094 464 struct lttng_ust_tracepoint * const *iter;
f99be407
PMF
465 struct tracepoint_entry *mark_entry;
466
f99be407 467 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
468 if (!*iter)
469 continue; /* skip dummy */
f218ff28
MD
470 if (!(*iter)->name) {
471 disable_tracepoint(*iter);
9dec086e
NC
472 continue;
473 }
f218ff28 474 mark_entry = get_tracepoint((*iter)->name);
f99be407 475 if (mark_entry) {
f218ff28 476 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
477 !!mark_entry->refcount);
478 } else {
f218ff28 479 disable_tracepoint(*iter);
f99be407
PMF
480 }
481 }
f99be407
PMF
482}
483
5da10905 484static void lib_update_tracepoints(struct tracepoint_lib *lib)
772030fe 485{
5da10905
MD
486 tracepoint_update_probe_range(lib->tracepoints_start,
487 lib->tracepoints_start + lib->tracepoints_count);
772030fe
PMF
488}
489
3469bbbe
MD
490static void lib_register_callsites(struct tracepoint_lib *lib)
491{
1a206094
SM
492 struct lttng_ust_tracepoint * const *begin;
493 struct lttng_ust_tracepoint * const *end;
494 struct lttng_ust_tracepoint * const *iter;
3469bbbe
MD
495
496 begin = lib->tracepoints_start;
497 end = lib->tracepoints_start + lib->tracepoints_count;
498
499 for (iter = begin; iter < end; iter++) {
500 if (!*iter)
501 continue; /* skip dummy */
502 if (!(*iter)->name) {
503 continue;
504 }
60d87029 505 add_callsite(lib, *iter);
3469bbbe
MD
506 }
507}
508
509static void lib_unregister_callsites(struct tracepoint_lib *lib)
510{
511 struct callsite_entry *callsite, *tmp;
512
513 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
514 remove_callsite(callsite);
515}
516
f99be407
PMF
517/*
518 * Update probes, removing the faulty probes.
519 */
520static void tracepoint_update_probes(void)
521{
5da10905
MD
522 struct tracepoint_lib *lib;
523
b27f8e75 524 /* tracepoints registered from libraries and executable. */
5da10905
MD
525 cds_list_for_each_entry(lib, &libs, list)
526 lib_update_tracepoints(lib);
f99be407
PMF
527}
528
1a206094 529static struct lttng_ust_tracepoint_probe *
fbdeb5ec 530tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
67e5f391 531 const char *signature)
f99be407
PMF
532{
533 struct tracepoint_entry *entry;
1a206094 534 struct lttng_ust_tracepoint_probe *old;
f99be407
PMF
535
536 entry = get_tracepoint(name);
537 if (!entry) {
67e5f391 538 entry = add_tracepoint(name, signature);
f99be407 539 if (IS_ERR(entry))
1a206094 540 return (struct lttng_ust_tracepoint_probe *)entry;
f99be407 541 }
9dec086e 542 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
543 if (IS_ERR(old) && !entry->refcount)
544 remove_tracepoint(entry);
545 return old;
546}
547
548/**
81614639 549 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
550 * @name: tracepoint name
551 * @probe: probe handler
552 *
553 * Returns 0 if ok, error value on error.
554 * The probe address must at least be aligned on the architecture pointer size.
8792fbae 555 * Called with the tracepoint mutex held.
f99be407 556 */
fbdeb5ec
MD
557int __tracepoint_probe_register(const char *name, void (*probe)(void),
558 void *data, const char *signature)
f99be407
PMF
559{
560 void *old;
8792fbae 561 int ret = 0;
f99be407 562
05780d81
MD
563 DBG("Registering probe to tracepoint %s", name);
564
8792fbae 565 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 566 old = tracepoint_add_probe(name, probe, data, signature);
8792fbae
MD
567 if (IS_ERR(old)) {
568 ret = PTR_ERR(old);
569 goto end;
570 }
f99be407 571
3469bbbe 572 tracepoint_sync_callsites(name);
f99be407 573 release_probes(old);
8792fbae
MD
574end:
575 pthread_mutex_unlock(&tracepoint_mutex);
576 return ret;
f99be407 577}
f99be407 578
fbdeb5ec
MD
579static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
580 void *data)
f99be407
PMF
581{
582 struct tracepoint_entry *entry;
583 void *old;
584
585 entry = get_tracepoint(name);
586 if (!entry)
587 return ERR_PTR(-ENOENT);
9dec086e 588 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
589 if (IS_ERR(old))
590 return old;
591 if (!entry->refcount)
592 remove_tracepoint(entry);
593 return old;
594}
595
596/**
597 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
598 * @name: tracepoint name
599 * @probe: probe function pointer
9dec086e 600 * @probe: probe data pointer
f99be407 601 */
fbdeb5ec
MD
602int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
603 void *data)
f99be407
PMF
604{
605 void *old;
8792fbae 606 int ret = 0;
f99be407 607
05780d81
MD
608 DBG("Un-registering probe from tracepoint %s", name);
609
8792fbae 610 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 611 old = tracepoint_remove_probe(name, probe, data);
8792fbae
MD
612 if (IS_ERR(old)) {
613 ret = PTR_ERR(old);
614 goto end;
615 }
3469bbbe 616 tracepoint_sync_callsites(name);
f99be407 617 release_probes(old);
8792fbae
MD
618end:
619 pthread_mutex_unlock(&tracepoint_mutex);
620 return ret;
f99be407 621}
f99be407 622
f99be407
PMF
623static void tracepoint_add_old_probes(void *old)
624{
625 need_update = 1;
626 if (old) {
b728d87e 627 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 628 struct tp_probes, probes[0]);
0222e121 629 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
630 }
631}
632
633/**
634 * tracepoint_probe_register_noupdate - register a probe but not connect
635 * @name: tracepoint name
636 * @probe: probe handler
637 *
638 * caller must call tracepoint_probe_update_all()
639 */
fbdeb5ec 640int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
67e5f391 641 void *data, const char *signature)
f99be407
PMF
642{
643 void *old;
8792fbae 644 int ret = 0;
f99be407 645
8792fbae 646 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 647 old = tracepoint_add_probe(name, probe, data, signature);
f99be407 648 if (IS_ERR(old)) {
8792fbae
MD
649 ret = PTR_ERR(old);
650 goto end;
f99be407
PMF
651 }
652 tracepoint_add_old_probes(old);
8792fbae
MD
653end:
654 pthread_mutex_unlock(&tracepoint_mutex);
655 return ret;
f99be407 656}
f99be407
PMF
657
658/**
659 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
660 * @name: tracepoint name
661 * @probe: probe function pointer
662 *
663 * caller must call tracepoint_probe_update_all()
8792fbae 664 * Called with the tracepoint mutex held.
f99be407 665 */
fbdeb5ec 666int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
9dec086e 667 void *data)
f99be407
PMF
668{
669 void *old;
8792fbae 670 int ret = 0;
f99be407 671
05780d81
MD
672 DBG("Un-registering probe from tracepoint %s", name);
673
8792fbae 674 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 675 old = tracepoint_remove_probe(name, probe, data);
f99be407 676 if (IS_ERR(old)) {
8792fbae
MD
677 ret = PTR_ERR(old);
678 goto end;
f99be407
PMF
679 }
680 tracepoint_add_old_probes(old);
8792fbae
MD
681end:
682 pthread_mutex_unlock(&tracepoint_mutex);
683 return ret;
f99be407 684}
f99be407
PMF
685
686/**
687 * tracepoint_probe_update_all - update tracepoints
688 */
689void tracepoint_probe_update_all(void)
690{
0222e121 691 CDS_LIST_HEAD(release_probes);
f99be407
PMF
692 struct tp_probes *pos, *next;
693
8792fbae 694 pthread_mutex_lock(&tracepoint_mutex);
f99be407 695 if (!need_update) {
8792fbae 696 goto end;
f99be407 697 }
0222e121
MD
698 if (!cds_list_empty(&old_probes))
699 cds_list_replace_init(&old_probes, &release_probes);
f99be407 700 need_update = 0;
f99be407
PMF
701
702 tracepoint_update_probes();
0222e121
MD
703 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
704 cds_list_del(&pos->u.list);
474d745f 705 synchronize_rcu();
909bc43f 706 free(pos);
f99be407 707 }
8792fbae
MD
708end:
709 pthread_mutex_unlock(&tracepoint_mutex);
f99be407 710}
f99be407 711
1a206094 712void tracepoint_set_new_tracepoint_cb(void (*cb)(struct lttng_ust_tracepoint *))
474d745f
PMF
713{
714 new_tracepoint_cb = cb;
715}
f99be407 716
1a206094
SM
717static void new_tracepoints(struct lttng_ust_tracepoint * const *start,
718 struct lttng_ust_tracepoint * const *end)
f99be407 719{
f218ff28 720 if (new_tracepoint_cb) {
1a206094 721 struct lttng_ust_tracepoint * const *t;
f08ebbe2 722
b27f8e75 723 for (t = start; t < end; t++) {
f08ebbe2
MD
724 if (*t)
725 new_tracepoint_cb(*t);
474d745f
PMF
726 }
727 }
f99be407 728}
f99be407 729
1a206094 730int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start,
b27f8e75 731 int tracepoints_count)
474d745f 732{
b467f7a7 733 struct tracepoint_lib *pl, *iter;
474d745f 734
edaa1431
MD
735 init_tracepoint();
736
1dba3e6c 737 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
d6297168
MD
738 if (!pl) {
739 PERROR("Unable to register tracepoint lib");
740 return -1;
741 }
474d745f
PMF
742 pl->tracepoints_start = tracepoints_start;
743 pl->tracepoints_count = tracepoints_count;
3469bbbe 744 CDS_INIT_LIST_HEAD(&pl->callsites);
474d745f 745
8792fbae 746 pthread_mutex_lock(&tracepoint_mutex);
b467f7a7
MD
747 /*
748 * We sort the libs by struct lib pointer address.
749 */
750 cds_list_for_each_entry_reverse(iter, &libs, list) {
751 BUG_ON(iter == pl); /* Should never be in the list twice */
752 if (iter < pl) {
753 /* We belong to the location right after iter. */
754 cds_list_add(&pl->list, &iter->list);
755 goto lib_added;
756 }
757 }
758 /* We should be added at the head of the list */
0222e121 759 cds_list_add(&pl->list, &libs);
b467f7a7 760lib_added:
474d745f 761 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
3469bbbe 762 lib_register_callsites(pl);
5da10905 763 lib_update_tracepoints(pl);
8792fbae 764 pthread_mutex_unlock(&tracepoint_mutex);
474d745f 765
1fcf7ad7
MD
766 DBG("just registered a tracepoints section from %p and having %d tracepoints",
767 tracepoints_start, tracepoints_count);
05780d81
MD
768 if (ust_debug()) {
769 int i;
770
771 for (i = 0; i < tracepoints_count; i++) {
772 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
773 }
774 }
9dec086e 775
474d745f
PMF
776 return 0;
777}
778
1a206094 779int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start)
474d745f 780{
24b6668c
PMF
781 struct tracepoint_lib *lib;
782
8792fbae 783 pthread_mutex_lock(&tracepoint_mutex);
0222e121 784 cds_list_for_each_entry(lib, &libs, list) {
3469bbbe
MD
785 if (lib->tracepoints_start != tracepoints_start)
786 continue;
1622ba22 787
3469bbbe
MD
788 cds_list_del(&lib->list);
789 /*
8a7ad54b
IJ
790 * Unregistering a callsite also decreases the
791 * callsite reference count of the corresponding
792 * tracepoint, and disables the tracepoint if
793 * the reference count drops to zero.
3469bbbe 794 */
3469bbbe
MD
795 lib_unregister_callsites(lib);
796 DBG("just unregistered a tracepoints section from %p",
797 lib->tracepoints_start);
798 free(lib);
799 break;
24b6668c 800 }
8792fbae 801 pthread_mutex_unlock(&tracepoint_mutex);
474d745f
PMF
802 return 0;
803}
b27f8e75 804
edaa1431 805void init_tracepoint(void)
b27f8e75 806{
edaa1431
MD
807 if (uatomic_xchg(&initialized, 1) == 1)
808 return;
5e96a467 809 init_usterr();
b27f8e75
MD
810}
811
edaa1431 812void exit_tracepoint(void)
b27f8e75 813{
17dfb34b 814 initialized = 0;
b27f8e75 815}
40b2b5a4
MD
816
817/*
818 * Create the wrapper symbols.
819 */
820#undef tp_rcu_read_lock_bp
821#undef tp_rcu_read_unlock_bp
822#undef tp_rcu_dereference_bp
823
824void tp_rcu_read_lock_bp(void)
825{
826 rcu_read_lock_bp();
827}
828
829void tp_rcu_read_unlock_bp(void)
830{
831 rcu_read_unlock_bp();
832}
833
834void *tp_rcu_dereference_sym_bp(void *p)
835{
836 return rcu_dereference_bp(p);
837}
This page took 0.074809 seconds and 4 git commands to generate.