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