Fix: tracepoint out of memory handling
[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. */
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;
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;
8a7ad54b 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
33f8ed87
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;
8a7ad54b 374 struct tracepoint_entry *tp_entry;
33f8ed87
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));
d6297168
MD
383 if (!e) {
384 PERROR("Unable to add callsite for tracepoint \"%s\"", name);
385 return;
386 }
33f8ed87
MD
387 cds_hlist_add_head(&e->hlist, head);
388 e->tp = tp;
389 cds_list_add(&e->node, &lib->callsites);
8a7ad54b
IJ
390
391 tp_entry = get_tracepoint(name);
392 if (!tp_entry)
393 return;
394 tp_entry->callsite_refcount++;
33f8ed87
MD
395}
396
397/*
398 * Remove the callsite from the callsite hash table and from lib
399 * callsite list. Must be called with tracepoint mutex held.
400 */
401static void remove_callsite(struct callsite_entry *e)
402{
8a7ad54b
IJ
403 struct tracepoint_entry *tp_entry;
404
405 tp_entry = get_tracepoint(e->tp->name);
406 if (tp_entry) {
407 tp_entry->callsite_refcount--;
408 if (tp_entry->callsite_refcount == 0)
409 disable_tracepoint(e->tp);
410 }
33f8ed87
MD
411 cds_hlist_del(&e->hlist);
412 cds_list_del(&e->node);
413 free(e);
414}
415
3469bbbe
MD
416/*
417 * Enable/disable all callsites based on the state of a specific
418 * tracepoint entry.
419 * Must be called with tracepoint mutex held.
420 */
421static void tracepoint_sync_callsites(const char *name)
422{
423 struct cds_hlist_head *head;
424 struct cds_hlist_node *node;
425 struct callsite_entry *e;
426 size_t name_len = strlen(name);
427 uint32_t hash;
428 struct tracepoint_entry *tp_entry;
429
430 tp_entry = get_tracepoint(name);
431 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
432 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
433 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
434 }
435 hash = jhash(name, name_len, 0);
436 head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)];
437 cds_hlist_for_each_entry(e, node, head, hlist) {
438 struct tracepoint *tp = e->tp;
439
440 if (strncmp(name, tp->name, LTTNG_UST_SYM_NAME_LEN - 1))
441 continue;
442 if (tp_entry) {
443 set_tracepoint(&tp_entry, tp,
444 !!tp_entry->refcount);
445 } else {
446 disable_tracepoint(tp);
447 }
448 }
449}
450
f99be407
PMF
451/**
452 * tracepoint_update_probe_range - Update a probe range
453 * @begin: beginning of the range
454 * @end: end of the range
455 *
456 * Updates the probe callback corresponding to a range of tracepoints.
457 */
b27f8e75 458static
f218ff28
MD
459void tracepoint_update_probe_range(struct tracepoint * const *begin,
460 struct tracepoint * const *end)
f99be407 461{
f218ff28 462 struct tracepoint * const *iter;
f99be407
PMF
463 struct tracepoint_entry *mark_entry;
464
f99be407 465 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
466 if (!*iter)
467 continue; /* skip dummy */
f218ff28
MD
468 if (!(*iter)->name) {
469 disable_tracepoint(*iter);
9dec086e
NC
470 continue;
471 }
f218ff28 472 mark_entry = get_tracepoint((*iter)->name);
f99be407 473 if (mark_entry) {
f218ff28 474 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
475 !!mark_entry->refcount);
476 } else {
f218ff28 477 disable_tracepoint(*iter);
f99be407
PMF
478 }
479 }
f99be407
PMF
480}
481
5da10905 482static void lib_update_tracepoints(struct tracepoint_lib *lib)
772030fe 483{
5da10905
MD
484 tracepoint_update_probe_range(lib->tracepoints_start,
485 lib->tracepoints_start + lib->tracepoints_count);
772030fe
PMF
486}
487
3469bbbe
MD
488static void lib_register_callsites(struct tracepoint_lib *lib)
489{
490 struct tracepoint * const *begin;
491 struct tracepoint * const *end;
492 struct tracepoint * const *iter;
493
494 begin = lib->tracepoints_start;
495 end = lib->tracepoints_start + lib->tracepoints_count;
496
497 for (iter = begin; iter < end; iter++) {
498 if (!*iter)
499 continue; /* skip dummy */
500 if (!(*iter)->name) {
501 continue;
502 }
60d87029 503 add_callsite(lib, *iter);
3469bbbe
MD
504 }
505}
506
507static void lib_unregister_callsites(struct tracepoint_lib *lib)
508{
509 struct callsite_entry *callsite, *tmp;
510
511 cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node)
512 remove_callsite(callsite);
513}
514
f99be407
PMF
515/*
516 * Update probes, removing the faulty probes.
517 */
518static void tracepoint_update_probes(void)
519{
5da10905
MD
520 struct tracepoint_lib *lib;
521
b27f8e75 522 /* tracepoints registered from libraries and executable. */
5da10905
MD
523 cds_list_for_each_entry(lib, &libs, list)
524 lib_update_tracepoints(lib);
f99be407
PMF
525}
526
b979b346 527static struct tracepoint_probe *
fbdeb5ec 528tracepoint_add_probe(const char *name, void (*probe)(void), void *data,
67e5f391 529 const char *signature)
f99be407
PMF
530{
531 struct tracepoint_entry *entry;
b979b346 532 struct tracepoint_probe *old;
f99be407
PMF
533
534 entry = get_tracepoint(name);
535 if (!entry) {
67e5f391 536 entry = add_tracepoint(name, signature);
f99be407 537 if (IS_ERR(entry))
b979b346 538 return (struct tracepoint_probe *)entry;
f99be407 539 }
9dec086e 540 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
541 if (IS_ERR(old) && !entry->refcount)
542 remove_tracepoint(entry);
543 return old;
544}
545
546/**
81614639 547 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
548 * @name: tracepoint name
549 * @probe: probe handler
550 *
551 * Returns 0 if ok, error value on error.
552 * The probe address must at least be aligned on the architecture pointer size.
8792fbae 553 * Called with the tracepoint mutex held.
f99be407 554 */
fbdeb5ec
MD
555int __tracepoint_probe_register(const char *name, void (*probe)(void),
556 void *data, const char *signature)
f99be407
PMF
557{
558 void *old;
8792fbae 559 int ret = 0;
f99be407 560
05780d81
MD
561 DBG("Registering probe to tracepoint %s", name);
562
8792fbae 563 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 564 old = tracepoint_add_probe(name, probe, data, signature);
8792fbae
MD
565 if (IS_ERR(old)) {
566 ret = PTR_ERR(old);
567 goto end;
568 }
f99be407 569
3469bbbe 570 tracepoint_sync_callsites(name);
f99be407 571 release_probes(old);
8792fbae
MD
572end:
573 pthread_mutex_unlock(&tracepoint_mutex);
574 return ret;
f99be407 575}
f99be407 576
fbdeb5ec
MD
577static void *tracepoint_remove_probe(const char *name, void (*probe)(void),
578 void *data)
f99be407
PMF
579{
580 struct tracepoint_entry *entry;
581 void *old;
582
583 entry = get_tracepoint(name);
584 if (!entry)
585 return ERR_PTR(-ENOENT);
9dec086e 586 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
587 if (IS_ERR(old))
588 return old;
589 if (!entry->refcount)
590 remove_tracepoint(entry);
591 return old;
592}
593
594/**
595 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
596 * @name: tracepoint name
597 * @probe: probe function pointer
9dec086e 598 * @probe: probe data pointer
f99be407 599 */
fbdeb5ec
MD
600int __tracepoint_probe_unregister(const char *name, void (*probe)(void),
601 void *data)
f99be407
PMF
602{
603 void *old;
8792fbae 604 int ret = 0;
f99be407 605
05780d81
MD
606 DBG("Un-registering probe from tracepoint %s", name);
607
8792fbae 608 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 609 old = tracepoint_remove_probe(name, probe, data);
8792fbae
MD
610 if (IS_ERR(old)) {
611 ret = PTR_ERR(old);
612 goto end;
613 }
3469bbbe 614 tracepoint_sync_callsites(name);
f99be407 615 release_probes(old);
8792fbae
MD
616end:
617 pthread_mutex_unlock(&tracepoint_mutex);
618 return ret;
f99be407 619}
f99be407 620
f99be407
PMF
621static void tracepoint_add_old_probes(void *old)
622{
623 need_update = 1;
624 if (old) {
b728d87e 625 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 626 struct tp_probes, probes[0]);
0222e121 627 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
628 }
629}
630
631/**
632 * tracepoint_probe_register_noupdate - register a probe but not connect
633 * @name: tracepoint name
634 * @probe: probe handler
635 *
636 * caller must call tracepoint_probe_update_all()
637 */
fbdeb5ec 638int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void),
67e5f391 639 void *data, const char *signature)
f99be407
PMF
640{
641 void *old;
8792fbae 642 int ret = 0;
f99be407 643
8792fbae 644 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 645 old = tracepoint_add_probe(name, probe, data, signature);
f99be407 646 if (IS_ERR(old)) {
8792fbae
MD
647 ret = PTR_ERR(old);
648 goto end;
f99be407
PMF
649 }
650 tracepoint_add_old_probes(old);
8792fbae
MD
651end:
652 pthread_mutex_unlock(&tracepoint_mutex);
653 return ret;
f99be407 654}
f99be407
PMF
655
656/**
657 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
658 * @name: tracepoint name
659 * @probe: probe function pointer
660 *
661 * caller must call tracepoint_probe_update_all()
8792fbae 662 * Called with the tracepoint mutex held.
f99be407 663 */
fbdeb5ec 664int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void),
9dec086e 665 void *data)
f99be407
PMF
666{
667 void *old;
8792fbae 668 int ret = 0;
f99be407 669
05780d81
MD
670 DBG("Un-registering probe from tracepoint %s", name);
671
8792fbae 672 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 673 old = tracepoint_remove_probe(name, probe, data);
f99be407 674 if (IS_ERR(old)) {
8792fbae
MD
675 ret = PTR_ERR(old);
676 goto end;
f99be407
PMF
677 }
678 tracepoint_add_old_probes(old);
8792fbae
MD
679end:
680 pthread_mutex_unlock(&tracepoint_mutex);
681 return ret;
f99be407 682}
f99be407
PMF
683
684/**
685 * tracepoint_probe_update_all - update tracepoints
686 */
687void tracepoint_probe_update_all(void)
688{
0222e121 689 CDS_LIST_HEAD(release_probes);
f99be407
PMF
690 struct tp_probes *pos, *next;
691
8792fbae 692 pthread_mutex_lock(&tracepoint_mutex);
f99be407 693 if (!need_update) {
8792fbae 694 goto end;
f99be407 695 }
0222e121
MD
696 if (!cds_list_empty(&old_probes))
697 cds_list_replace_init(&old_probes, &release_probes);
f99be407 698 need_update = 0;
f99be407
PMF
699
700 tracepoint_update_probes();
0222e121
MD
701 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
702 cds_list_del(&pos->u.list);
474d745f 703 synchronize_rcu();
909bc43f 704 free(pos);
f99be407 705 }
8792fbae
MD
706end:
707 pthread_mutex_unlock(&tracepoint_mutex);
f99be407 708}
f99be407 709
474d745f
PMF
710void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
711{
712 new_tracepoint_cb = cb;
713}
f99be407 714
f218ff28 715static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
f99be407 716{
f218ff28
MD
717 if (new_tracepoint_cb) {
718 struct tracepoint * const *t;
f08ebbe2 719
b27f8e75 720 for (t = start; t < end; t++) {
f08ebbe2
MD
721 if (*t)
722 new_tracepoint_cb(*t);
474d745f
PMF
723 }
724 }
f99be407 725}
f99be407 726
b27f8e75
MD
727int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
728 int tracepoints_count)
474d745f 729{
b467f7a7 730 struct tracepoint_lib *pl, *iter;
474d745f 731
edaa1431
MD
732 init_tracepoint();
733
1dba3e6c 734 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
d6297168
MD
735 if (!pl) {
736 PERROR("Unable to register tracepoint lib");
737 return -1;
738 }
474d745f
PMF
739 pl->tracepoints_start = tracepoints_start;
740 pl->tracepoints_count = tracepoints_count;
3469bbbe 741 CDS_INIT_LIST_HEAD(&pl->callsites);
474d745f 742
8792fbae 743 pthread_mutex_lock(&tracepoint_mutex);
b467f7a7
MD
744 /*
745 * We sort the libs by struct lib pointer address.
746 */
747 cds_list_for_each_entry_reverse(iter, &libs, list) {
748 BUG_ON(iter == pl); /* Should never be in the list twice */
749 if (iter < pl) {
750 /* We belong to the location right after iter. */
751 cds_list_add(&pl->list, &iter->list);
752 goto lib_added;
753 }
754 }
755 /* We should be added at the head of the list */
0222e121 756 cds_list_add(&pl->list, &libs);
b467f7a7 757lib_added:
474d745f 758 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
3469bbbe 759 lib_register_callsites(pl);
5da10905 760 lib_update_tracepoints(pl);
8792fbae 761 pthread_mutex_unlock(&tracepoint_mutex);
474d745f 762
1fcf7ad7
MD
763 DBG("just registered a tracepoints section from %p and having %d tracepoints",
764 tracepoints_start, tracepoints_count);
05780d81
MD
765 if (ust_debug()) {
766 int i;
767
768 for (i = 0; i < tracepoints_count; i++) {
769 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
770 }
771 }
9dec086e 772
474d745f
PMF
773 return 0;
774}
775
f218ff28 776int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
474d745f 777{
24b6668c
PMF
778 struct tracepoint_lib *lib;
779
8792fbae 780 pthread_mutex_lock(&tracepoint_mutex);
0222e121 781 cds_list_for_each_entry(lib, &libs, list) {
3469bbbe
MD
782 if (lib->tracepoints_start != tracepoints_start)
783 continue;
1622ba22 784
3469bbbe
MD
785 cds_list_del(&lib->list);
786 /*
8a7ad54b
IJ
787 * Unregistering a callsite also decreases the
788 * callsite reference count of the corresponding
789 * tracepoint, and disables the tracepoint if
790 * the reference count drops to zero.
3469bbbe 791 */
3469bbbe
MD
792 lib_unregister_callsites(lib);
793 DBG("just unregistered a tracepoints section from %p",
794 lib->tracepoints_start);
795 free(lib);
796 break;
24b6668c 797 }
8792fbae 798 pthread_mutex_unlock(&tracepoint_mutex);
474d745f
PMF
799 return 0;
800}
b27f8e75 801
edaa1431 802void init_tracepoint(void)
b27f8e75 803{
edaa1431
MD
804 if (uatomic_xchg(&initialized, 1) == 1)
805 return;
5e96a467 806 init_usterr();
b27f8e75
MD
807}
808
edaa1431 809void exit_tracepoint(void)
b27f8e75 810{
17dfb34b 811 initialized = 0;
b27f8e75 812}
40b2b5a4
MD
813
814/*
815 * Create the wrapper symbols.
816 */
817#undef tp_rcu_read_lock_bp
818#undef tp_rcu_read_unlock_bp
819#undef tp_rcu_dereference_bp
820
821void tp_rcu_read_lock_bp(void)
822{
823 rcu_read_lock_bp();
824}
825
826void tp_rcu_read_unlock_bp(void)
827{
828 rcu_read_unlock_bp();
829}
830
831void *tp_rcu_dereference_sym_bp(void *p)
832{
833 return rcu_dereference_bp(p);
834}
This page took 0.073054 seconds and 4 git commands to generate.