Merge branch 'dev'
[lttng-ust.git] / liblttng-ust / tracepoint.c
CommitLineData
f99be407 1/*
b27f8e75 2 * Copyright (C) 2008-2011 Mathieu Desnoyers
1f8b0dff 3 * Copyright (C) 2009 Pierre-Marc Fournier
f99be407 4 *
34e4b7db
PMF
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
f37142a4
MD
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
f99be407 9 *
34e4b7db 10 * This library is distributed in the hope that it will be useful,
f99be407 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34e4b7db
PMF
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
f99be407 14 *
34e4b7db
PMF
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1f8b0dff
PMF
18 *
19 * Ported to userspace by Pierre-Marc Fournier.
f99be407 20 */
1f8b0dff 21
b0c4126f 22#define _LGPL_SOURCE
474d745f 23#include <errno.h>
b728d87e
MD
24#include <stdint.h>
25#include <stddef.h>
44c72f10 26
b728d87e 27#include <urcu/arch.h>
b7ea1a1c 28#include <urcu-bp.h>
10c56168 29#include <urcu/hlist.h>
edaa1431 30#include <urcu/uatomic.h>
b728d87e 31#include <urcu/compiler.h>
44c72f10
MD
32
33#include <lttng/tracepoint.h>
ff412fb5 34#include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
44c72f10
MD
35
36#include <usterr-signal-safe.h>
35897f8b 37#include <helper.h>
474d745f 38
23c8854a 39#include "tracepoint-internal.h"
b751f722 40#include "ltt-tracer-core.h"
596c4223 41#include "jhash.h"
8f3f8c99 42#include "error.h"
b0c4126f 43
f99be407
PMF
44/* Set to 1 to enable tracepoint debug output */
45static const int tracepoint_debug;
b27f8e75
MD
46static int initialized;
47static void (*new_tracepoint_cb)(struct tracepoint *);
f99be407 48
8792fbae
MD
49/*
50 * tracepoint_mutex nests inside UST mutex.
51 *
52 * Note about interaction with fork/clone: UST does not hold the
53 * tracepoint mutex across fork/clone because it is either:
54 * - nested within UST mutex, in which case holding the UST mutex across
55 * fork/clone suffice,
56 * - taken by a library constructor, which should never race with a
57 * fork/clone if the application is expected to continue running with
58 * the same memory layout (no following exec()).
59 */
60static pthread_mutex_t tracepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
61
efa2c591
MD
62/*
63 * libraries that contain tracepoints (struct tracepoint_lib).
8792fbae 64 * Protected by tracepoint mutex.
efa2c591 65 */
0222e121 66static CDS_LIST_HEAD(libs);
474d745f 67
f99be407 68/*
8792fbae 69 * The tracepoint mutex protects the library tracepoints, the hash table, and
17dfb34b 70 * the library list.
8792fbae 71 * All calls to the tracepoint API must be protected by the tracepoint mutex,
17dfb34b 72 * excepts calls to tracepoint_register_lib and
8792fbae 73 * tracepoint_unregister_lib, which take the tracepoint mutex themselves.
f99be407 74 */
f99be407
PMF
75
76/*
77 * Tracepoint hash table, containing the active tracepoints.
8792fbae 78 * Protected by tracepoint mutex.
f99be407
PMF
79 */
80#define TRACEPOINT_HASH_BITS 6
81#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
10c56168 82static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
f99be407 83
b27f8e75
MD
84static CDS_LIST_HEAD(old_probes);
85static int need_update;
86
f99be407
PMF
87/*
88 * Note about RCU :
89 * It is used to to delay the free of multiple probes array until a quiescent
90 * state is reached.
8792fbae 91 * Tracepoint entries modifications are protected by the tracepoint mutex.
f99be407
PMF
92 */
93struct tracepoint_entry {
10c56168 94 struct cds_hlist_node hlist;
b979b346 95 struct tracepoint_probe *probes;
f99be407 96 int refcount; /* Number of times armed. 0 if disarmed. */
67e5f391 97 const char *signature;
f99be407
PMF
98 char name[0];
99};
100
101struct tp_probes {
102 union {
0222e121 103 struct cds_list_head list;
ade7037b
MD
104 /* Field below only used for call_rcu scheme */
105 /* struct rcu_head head; */
f99be407 106 } u;
b979b346 107 struct tracepoint_probe probes[0];
f99be407
PMF
108};
109
efa2c591 110static void *allocate_probes(int count)
f99be407 111{
b979b346 112 struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe)
909bc43f 113 + sizeof(struct tp_probes));
f99be407
PMF
114 return p == NULL ? NULL : p->probes;
115}
116
efa2c591 117static void release_probes(void *old)
f99be407
PMF
118{
119 if (old) {
b728d87e 120 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 121 struct tp_probes, probes[0]);
474d745f 122 synchronize_rcu();
909bc43f 123 free(tp_probes);
f99be407
PMF
124 }
125}
126
127static void debug_print_probes(struct tracepoint_entry *entry)
128{
129 int i;
130
9dec086e 131 if (!tracepoint_debug || !entry->probes)
f99be407
PMF
132 return;
133
9dec086e
NC
134 for (i = 0; entry->probes[i].func; i++)
135 DBG("Probe %d : %p", i, entry->probes[i].func);
f99be407
PMF
136}
137
138static void *
9dec086e
NC
139tracepoint_entry_add_probe(struct tracepoint_entry *entry,
140 void *probe, void *data)
f99be407
PMF
141{
142 int nr_probes = 0;
b979b346 143 struct tracepoint_probe *old, *new;
f99be407
PMF
144
145 WARN_ON(!probe);
146
147 debug_print_probes(entry);
9dec086e 148 old = entry->probes;
f99be407
PMF
149 if (old) {
150 /* (N -> N+1), (N != 0, 1) probes */
9dec086e
NC
151 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
152 if (old[nr_probes].func == probe &&
153 old[nr_probes].data == data)
f99be407
PMF
154 return ERR_PTR(-EEXIST);
155 }
156 /* + 2 : one for new probe, one for NULL func */
157 new = allocate_probes(nr_probes + 2);
158 if (new == NULL)
159 return ERR_PTR(-ENOMEM);
160 if (old)
b979b346 161 memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe));
9dec086e
NC
162 new[nr_probes].func = probe;
163 new[nr_probes].data = data;
164 new[nr_probes + 1].func = NULL;
f99be407 165 entry->refcount = nr_probes + 1;
9dec086e 166 entry->probes = new;
f99be407
PMF
167 debug_print_probes(entry);
168 return old;
169}
170
171static void *
9dec086e
NC
172tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe,
173 void *data)
f99be407
PMF
174{
175 int nr_probes = 0, nr_del = 0, i;
b979b346 176 struct tracepoint_probe *old, *new;
f99be407 177
9dec086e 178 old = entry->probes;
f99be407
PMF
179
180 if (!old)
181 return ERR_PTR(-ENOENT);
182
183 debug_print_probes(entry);
184 /* (N -> M), (N > 1, M >= 0) probes */
9dec086e 185 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
c66428ac
DG
186 if (!probe ||
187 (old[nr_probes].func == probe &&
9dec086e 188 old[nr_probes].data == data))
f99be407
PMF
189 nr_del++;
190 }
191
192 if (nr_probes - nr_del == 0) {
193 /* N -> 0, (N > 1) */
9dec086e 194 entry->probes = NULL;
f99be407
PMF
195 entry->refcount = 0;
196 debug_print_probes(entry);
197 return old;
198 } else {
199 int j = 0;
200 /* N -> M, (N > 1, M > 0) */
201 /* + 1 for NULL */
202 new = allocate_probes(nr_probes - nr_del + 1);
203 if (new == NULL)
204 return ERR_PTR(-ENOMEM);
9dec086e
NC
205 for (i = 0; old[i].func; i++)
206 if (probe &&
207 (old[i].func != probe || old[i].data != data))
f99be407 208 new[j++] = old[i];
9dec086e 209 new[nr_probes - nr_del].func = NULL;
f99be407 210 entry->refcount = nr_probes - nr_del;
9dec086e 211 entry->probes = new;
f99be407
PMF
212 }
213 debug_print_probes(entry);
214 return old;
215}
216
217/*
218 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
8792fbae 219 * Must be called with tracepoint mutex held.
f99be407
PMF
220 * Returns NULL if not present.
221 */
222static struct tracepoint_entry *get_tracepoint(const char *name)
223{
10c56168
DG
224 struct cds_hlist_head *head;
225 struct cds_hlist_node *node;
f99be407 226 struct tracepoint_entry *e;
ff412fb5
MD
227 size_t name_len = strlen(name);
228 uint32_t hash;
f99be407 229
ff412fb5
MD
230 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
231 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
232 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
233 }
234 hash = jhash(name, name_len, 0);
f99be407 235 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 236 cds_hlist_for_each_entry(e, node, head, hlist) {
ff412fb5 237 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
f99be407
PMF
238 return e;
239 }
240 return NULL;
241}
242
243/*
244 * Add the tracepoint to the tracepoint hash table. Must be called with
8792fbae 245 * tracepoint mutex held.
f99be407 246 */
67e5f391
MD
247static struct tracepoint_entry *add_tracepoint(const char *name,
248 const char *signature)
f99be407 249{
10c56168
DG
250 struct cds_hlist_head *head;
251 struct cds_hlist_node *node;
f99be407 252 struct tracepoint_entry *e;
ff412fb5
MD
253 size_t name_len = strlen(name);
254 uint32_t hash;
f99be407 255
ff412fb5
MD
256 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
257 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
258 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
259 }
260 hash = jhash(name, name_len, 0);
f99be407 261 head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
10c56168 262 cds_hlist_for_each_entry(e, node, head, hlist) {
ff412fb5 263 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
c1f20530 264 DBG("tracepoint %s busy", name);
f99be407
PMF
265 return ERR_PTR(-EEXIST); /* Already there */
266 }
267 }
268 /*
1dba3e6c 269 * Using zmalloc here to allocate a variable length element. Could
f99be407
PMF
270 * cause some memory fragmentation if overused.
271 */
ff412fb5 272 e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1);
f99be407
PMF
273 if (!e)
274 return ERR_PTR(-ENOMEM);
ff412fb5
MD
275 memcpy(&e->name[0], name, name_len + 1);
276 e->name[name_len] = '\0';
9dec086e 277 e->probes = NULL;
f99be407 278 e->refcount = 0;
67e5f391 279 e->signature = signature;
10c56168 280 cds_hlist_add_head(&e->hlist, head);
f99be407
PMF
281 return e;
282}
283
284/*
285 * Remove the tracepoint from the tracepoint hash table. Must be called with
8792fbae 286 * tracepoint mutex held.
f99be407 287 */
efa2c591 288static void remove_tracepoint(struct tracepoint_entry *e)
f99be407 289{
10c56168 290 cds_hlist_del(&e->hlist);
909bc43f 291 free(e);
f99be407
PMF
292}
293
294/*
295 * Sets the probe callback corresponding to one tracepoint.
296 */
297static void set_tracepoint(struct tracepoint_entry **entry,
298 struct tracepoint *elem, int active)
299{
ff412fb5 300 WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0);
67e5f391
MD
301 /*
302 * Check that signatures match before connecting a probe to a
303 * tracepoint. Warn the user if they don't.
304 */
305 if (strcmp(elem->signature, (*entry)->signature) != 0) {
306 static int warned = 0;
307
308 /* Only print once, don't flood console. */
309 if (!warned) {
310 WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application.");
311 WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".",
312 elem->name, elem->signature, (*entry)->signature);
313 warned = 1;
314 }
315 /* Don't accept connecting non-matching signatures. */
316 return;
317 }
f99be407
PMF
318
319 /*
0222e121 320 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
f99be407
PMF
321 * probe callbacks array is consistent before setting a pointer to it.
322 * This array is referenced by __DO_TRACE from
0222e121 323 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
f99be407
PMF
324 * is used.
325 */
9dec086e 326 rcu_assign_pointer(elem->probes, (*entry)->probes);
f36c12ab 327 elem->state = active;
f99be407
PMF
328}
329
330/*
331 * Disable a tracepoint and its probe callback.
332 * Note: only waiting an RCU period after setting elem->call to the empty
333 * function insures that the original callback is not used anymore. This insured
334 * by preempt_disable around the call site.
335 */
336static void disable_tracepoint(struct tracepoint *elem)
337{
f36c12ab 338 elem->state = 0;
9dec086e 339 rcu_assign_pointer(elem->probes, NULL);
f99be407
PMF
340}
341
342/**
343 * tracepoint_update_probe_range - Update a probe range
344 * @begin: beginning of the range
345 * @end: end of the range
346 *
347 * Updates the probe callback corresponding to a range of tracepoints.
348 */
b27f8e75 349static
f218ff28
MD
350void tracepoint_update_probe_range(struct tracepoint * const *begin,
351 struct tracepoint * const *end)
f99be407 352{
f218ff28 353 struct tracepoint * const *iter;
f99be407
PMF
354 struct tracepoint_entry *mark_entry;
355
f99be407 356 for (iter = begin; iter < end; iter++) {
f08ebbe2
MD
357 if (!*iter)
358 continue; /* skip dummy */
f218ff28
MD
359 if (!(*iter)->name) {
360 disable_tracepoint(*iter);
9dec086e
NC
361 continue;
362 }
f218ff28 363 mark_entry = get_tracepoint((*iter)->name);
f99be407 364 if (mark_entry) {
f218ff28 365 set_tracepoint(&mark_entry, *iter,
f99be407
PMF
366 !!mark_entry->refcount);
367 } else {
f218ff28 368 disable_tracepoint(*iter);
f99be407
PMF
369 }
370 }
f99be407
PMF
371}
372
772030fe
PMF
373static void lib_update_tracepoints(void)
374{
375 struct tracepoint_lib *lib;
376
b27f8e75 377 cds_list_for_each_entry(lib, &libs, list) {
772030fe
PMF
378 tracepoint_update_probe_range(lib->tracepoints_start,
379 lib->tracepoints_start + lib->tracepoints_count);
b27f8e75 380 }
772030fe
PMF
381}
382
f99be407
PMF
383/*
384 * Update probes, removing the faulty probes.
385 */
386static void tracepoint_update_probes(void)
387{
b27f8e75 388 /* tracepoints registered from libraries and executable. */
474d745f 389 lib_update_tracepoints();
f99be407
PMF
390}
391
b979b346 392static struct tracepoint_probe *
67e5f391
MD
393tracepoint_add_probe(const char *name, void *probe, void *data,
394 const char *signature)
f99be407
PMF
395{
396 struct tracepoint_entry *entry;
b979b346 397 struct tracepoint_probe *old;
f99be407
PMF
398
399 entry = get_tracepoint(name);
400 if (!entry) {
67e5f391 401 entry = add_tracepoint(name, signature);
f99be407 402 if (IS_ERR(entry))
b979b346 403 return (struct tracepoint_probe *)entry;
f99be407 404 }
9dec086e 405 old = tracepoint_entry_add_probe(entry, probe, data);
f99be407
PMF
406 if (IS_ERR(old) && !entry->refcount)
407 remove_tracepoint(entry);
408 return old;
409}
410
411/**
81614639 412 * __tracepoint_probe_register - Connect a probe to a tracepoint
f99be407
PMF
413 * @name: tracepoint name
414 * @probe: probe handler
415 *
416 * Returns 0 if ok, error value on error.
417 * The probe address must at least be aligned on the architecture pointer size.
8792fbae 418 * Called with the tracepoint mutex held.
f99be407 419 */
67e5f391
MD
420int __tracepoint_probe_register(const char *name, void *probe, void *data,
421 const char *signature)
f99be407
PMF
422{
423 void *old;
8792fbae 424 int ret = 0;
f99be407 425
05780d81
MD
426 DBG("Registering probe to tracepoint %s", name);
427
8792fbae 428 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 429 old = tracepoint_add_probe(name, probe, data, signature);
8792fbae
MD
430 if (IS_ERR(old)) {
431 ret = PTR_ERR(old);
432 goto end;
433 }
f99be407
PMF
434
435 tracepoint_update_probes(); /* may update entry */
436 release_probes(old);
8792fbae
MD
437end:
438 pthread_mutex_unlock(&tracepoint_mutex);
439 return ret;
f99be407 440}
f99be407 441
9dec086e 442static void *tracepoint_remove_probe(const char *name, void *probe, void *data)
f99be407
PMF
443{
444 struct tracepoint_entry *entry;
445 void *old;
446
447 entry = get_tracepoint(name);
448 if (!entry)
449 return ERR_PTR(-ENOENT);
9dec086e 450 old = tracepoint_entry_remove_probe(entry, probe, data);
f99be407
PMF
451 if (IS_ERR(old))
452 return old;
453 if (!entry->refcount)
454 remove_tracepoint(entry);
455 return old;
456}
457
458/**
459 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
460 * @name: tracepoint name
461 * @probe: probe function pointer
9dec086e 462 * @probe: probe data pointer
f99be407 463 */
81614639 464int __tracepoint_probe_unregister(const char *name, void *probe, void *data)
f99be407
PMF
465{
466 void *old;
8792fbae 467 int ret = 0;
f99be407 468
05780d81
MD
469 DBG("Un-registering probe from tracepoint %s", name);
470
8792fbae 471 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 472 old = tracepoint_remove_probe(name, probe, data);
8792fbae
MD
473 if (IS_ERR(old)) {
474 ret = PTR_ERR(old);
475 goto end;
476 }
f99be407
PMF
477 tracepoint_update_probes(); /* may update entry */
478 release_probes(old);
8792fbae
MD
479end:
480 pthread_mutex_unlock(&tracepoint_mutex);
481 return ret;
f99be407 482}
f99be407 483
f99be407
PMF
484static void tracepoint_add_old_probes(void *old)
485{
486 need_update = 1;
487 if (old) {
b728d87e 488 struct tp_probes *tp_probes = caa_container_of(old,
f99be407 489 struct tp_probes, probes[0]);
0222e121 490 cds_list_add(&tp_probes->u.list, &old_probes);
f99be407
PMF
491 }
492}
493
494/**
495 * tracepoint_probe_register_noupdate - register a probe but not connect
496 * @name: tracepoint name
497 * @probe: probe handler
498 *
499 * caller must call tracepoint_probe_update_all()
500 */
9dec086e 501int tracepoint_probe_register_noupdate(const char *name, void *probe,
67e5f391 502 void *data, const char *signature)
f99be407
PMF
503{
504 void *old;
8792fbae 505 int ret = 0;
f99be407 506
8792fbae 507 pthread_mutex_lock(&tracepoint_mutex);
67e5f391 508 old = tracepoint_add_probe(name, probe, data, signature);
f99be407 509 if (IS_ERR(old)) {
8792fbae
MD
510 ret = PTR_ERR(old);
511 goto end;
f99be407
PMF
512 }
513 tracepoint_add_old_probes(old);
8792fbae
MD
514end:
515 pthread_mutex_unlock(&tracepoint_mutex);
516 return ret;
f99be407 517}
f99be407
PMF
518
519/**
520 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
521 * @name: tracepoint name
522 * @probe: probe function pointer
523 *
524 * caller must call tracepoint_probe_update_all()
8792fbae 525 * Called with the tracepoint mutex held.
f99be407 526 */
9dec086e
NC
527int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
528 void *data)
f99be407
PMF
529{
530 void *old;
8792fbae 531 int ret = 0;
f99be407 532
05780d81
MD
533 DBG("Un-registering probe from tracepoint %s", name);
534
8792fbae 535 pthread_mutex_lock(&tracepoint_mutex);
9dec086e 536 old = tracepoint_remove_probe(name, probe, data);
f99be407 537 if (IS_ERR(old)) {
8792fbae
MD
538 ret = PTR_ERR(old);
539 goto end;
f99be407
PMF
540 }
541 tracepoint_add_old_probes(old);
8792fbae
MD
542end:
543 pthread_mutex_unlock(&tracepoint_mutex);
544 return ret;
f99be407 545}
f99be407
PMF
546
547/**
548 * tracepoint_probe_update_all - update tracepoints
549 */
550void tracepoint_probe_update_all(void)
551{
0222e121 552 CDS_LIST_HEAD(release_probes);
f99be407
PMF
553 struct tp_probes *pos, *next;
554
8792fbae 555 pthread_mutex_lock(&tracepoint_mutex);
f99be407 556 if (!need_update) {
8792fbae 557 goto end;
f99be407 558 }
0222e121
MD
559 if (!cds_list_empty(&old_probes))
560 cds_list_replace_init(&old_probes, &release_probes);
f99be407 561 need_update = 0;
f99be407
PMF
562
563 tracepoint_update_probes();
0222e121
MD
564 cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
565 cds_list_del(&pos->u.list);
474d745f 566 synchronize_rcu();
909bc43f 567 free(pos);
f99be407 568 }
8792fbae
MD
569end:
570 pthread_mutex_unlock(&tracepoint_mutex);
f99be407 571}
f99be407 572
474d745f
PMF
573void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
574{
575 new_tracepoint_cb = cb;
576}
f99be407 577
f218ff28 578static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end)
f99be407 579{
f218ff28
MD
580 if (new_tracepoint_cb) {
581 struct tracepoint * const *t;
f08ebbe2 582
b27f8e75 583 for (t = start; t < end; t++) {
f08ebbe2
MD
584 if (*t)
585 new_tracepoint_cb(*t);
474d745f
PMF
586 }
587 }
f99be407 588}
f99be407 589
1622ba22
MD
590static
591void lib_disable_tracepoints(struct tracepoint * const *begin,
592 struct tracepoint * const *end)
593{
594 struct tracepoint * const *iter;
595
596 for (iter = begin; iter < end; iter++) {
597 if (!*iter)
598 continue; /* skip dummy */
599 disable_tracepoint(*iter);
600 }
601
602}
603
b27f8e75
MD
604int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
605 int tracepoints_count)
474d745f 606{
b467f7a7 607 struct tracepoint_lib *pl, *iter;
474d745f 608
edaa1431
MD
609 init_tracepoint();
610
1dba3e6c 611 pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
474d745f
PMF
612
613 pl->tracepoints_start = tracepoints_start;
614 pl->tracepoints_count = tracepoints_count;
615
8792fbae 616 pthread_mutex_lock(&tracepoint_mutex);
b467f7a7
MD
617 /*
618 * We sort the libs by struct lib pointer address.
619 */
620 cds_list_for_each_entry_reverse(iter, &libs, list) {
621 BUG_ON(iter == pl); /* Should never be in the list twice */
622 if (iter < pl) {
623 /* We belong to the location right after iter. */
624 cds_list_add(&pl->list, &iter->list);
625 goto lib_added;
626 }
627 }
628 /* We should be added at the head of the list */
0222e121 629 cds_list_add(&pl->list, &libs);
b467f7a7 630lib_added:
474d745f
PMF
631 new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
632
b27f8e75 633 /* TODO: update just the loaded lib */
474d745f 634 lib_update_tracepoints();
8792fbae 635 pthread_mutex_unlock(&tracepoint_mutex);
474d745f 636
1fcf7ad7
MD
637 DBG("just registered a tracepoints section from %p and having %d tracepoints",
638 tracepoints_start, tracepoints_count);
05780d81
MD
639 if (ust_debug()) {
640 int i;
641
642 for (i = 0; i < tracepoints_count; i++) {
643 DBG("registered tracepoint: %s", tracepoints_start[i]->name);
644 }
645 }
9dec086e 646
474d745f
PMF
647 return 0;
648}
649
f218ff28 650int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
474d745f 651{
24b6668c 652 struct tracepoint_lib *lib;
1622ba22 653 int tracepoints_count;
24b6668c 654
8792fbae 655 pthread_mutex_lock(&tracepoint_mutex);
0222e121 656 cds_list_for_each_entry(lib, &libs, list) {
f218ff28 657 if (lib->tracepoints_start == tracepoints_start) {
24b6668c 658 struct tracepoint_lib *lib2free = lib;
1622ba22 659
0222e121 660 cds_list_del(&lib->list);
1622ba22 661 tracepoints_count = lib->tracepoints_count;
24b6668c 662 free(lib2free);
1622ba22 663 goto found;
24b6668c
PMF
664 }
665 }
1622ba22
MD
666 goto end;
667found:
668 /*
669 * Force tracepoint disarm for all tracepoints of this lib.
670 * This takes care of destructor of library that would leave a
671 * LD_PRELOAD wrapper override function enabled for tracing, but
672 * the session teardown would not be able to reach the
673 * tracepoint anymore to disable it.
674 */
675 lib_disable_tracepoints(tracepoints_start,
676 tracepoints_start + tracepoints_count);
677 DBG("just unregistered a tracepoints section from %p",
678 tracepoints_start);
679end:
8792fbae 680 pthread_mutex_unlock(&tracepoint_mutex);
474d745f
PMF
681 return 0;
682}
b27f8e75 683
edaa1431 684void init_tracepoint(void)
b27f8e75 685{
edaa1431
MD
686 if (uatomic_xchg(&initialized, 1) == 1)
687 return;
5e96a467 688 init_usterr();
b27f8e75
MD
689}
690
edaa1431 691void exit_tracepoint(void)
b27f8e75 692{
17dfb34b 693 initialized = 0;
b27f8e75 694}
40b2b5a4
MD
695
696/*
697 * Create the wrapper symbols.
698 */
699#undef tp_rcu_read_lock_bp
700#undef tp_rcu_read_unlock_bp
701#undef tp_rcu_dereference_bp
702
703void tp_rcu_read_lock_bp(void)
704{
705 rcu_read_lock_bp();
706}
707
708void tp_rcu_read_unlock_bp(void)
709{
710 rcu_read_unlock_bp();
711}
712
713void *tp_rcu_dereference_sym_bp(void *p)
714{
715 return rcu_dereference_bp(p);
716}
This page took 0.065054 seconds and 4 git commands to generate.