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