Commit | Line | Data |
---|---|---|
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 */ |
45 | static const int tracepoint_debug; | |
b27f8e75 MD |
46 | static int initialized; |
47 | static void (*new_tracepoint_cb)(struct tracepoint *); | |
f99be407 | 48 | |
efa2c591 MD |
49 | /* |
50 | * libraries that contain tracepoints (struct tracepoint_lib). | |
51 | * Protected by UST lock. | |
52 | */ | |
0222e121 | 53 | static CDS_LIST_HEAD(libs); |
474d745f | 54 | |
f99be407 | 55 | /* |
17dfb34b MD |
56 | * The UST lock protects the library tracepoints, the hash table, and |
57 | * the library list. | |
58 | * All calls to the tracepoint API must be protected by the UST lock, | |
59 | * excepts calls to tracepoint_register_lib and | |
60 | * tracepoint_unregister_lib, which take the UST lock themselves. | |
f99be407 | 61 | */ |
f99be407 PMF |
62 | |
63 | /* | |
64 | * Tracepoint hash table, containing the active tracepoints. | |
efa2c591 | 65 | * Protected by ust lock. |
f99be407 PMF |
66 | */ |
67 | #define TRACEPOINT_HASH_BITS 6 | |
68 | #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS) | |
10c56168 | 69 | static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE]; |
f99be407 | 70 | |
b27f8e75 MD |
71 | static CDS_LIST_HEAD(old_probes); |
72 | static int need_update; | |
73 | ||
f99be407 PMF |
74 | /* |
75 | * Note about RCU : | |
76 | * It is used to to delay the free of multiple probes array until a quiescent | |
77 | * state is reached. | |
efa2c591 | 78 | * Tracepoint entries modifications are protected by the ust lock. |
f99be407 PMF |
79 | */ |
80 | struct tracepoint_entry { | |
10c56168 | 81 | struct cds_hlist_node hlist; |
b979b346 | 82 | struct tracepoint_probe *probes; |
f99be407 PMF |
83 | int refcount; /* Number of times armed. 0 if disarmed. */ |
84 | char name[0]; | |
85 | }; | |
86 | ||
87 | struct tp_probes { | |
88 | union { | |
0222e121 | 89 | struct cds_list_head list; |
f99be407 | 90 | } u; |
b979b346 | 91 | struct tracepoint_probe probes[0]; |
f99be407 PMF |
92 | }; |
93 | ||
efa2c591 | 94 | static void *allocate_probes(int count) |
f99be407 | 95 | { |
b979b346 | 96 | struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe) |
909bc43f | 97 | + sizeof(struct tp_probes)); |
f99be407 PMF |
98 | return p == NULL ? NULL : p->probes; |
99 | } | |
100 | ||
efa2c591 | 101 | static void release_probes(void *old) |
f99be407 PMF |
102 | { |
103 | if (old) { | |
b728d87e | 104 | struct tp_probes *tp_probes = caa_container_of(old, |
f99be407 | 105 | struct tp_probes, probes[0]); |
474d745f | 106 | synchronize_rcu(); |
909bc43f | 107 | free(tp_probes); |
f99be407 PMF |
108 | } |
109 | } | |
110 | ||
111 | static void debug_print_probes(struct tracepoint_entry *entry) | |
112 | { | |
113 | int i; | |
114 | ||
9dec086e | 115 | if (!tracepoint_debug || !entry->probes) |
f99be407 PMF |
116 | return; |
117 | ||
9dec086e NC |
118 | for (i = 0; entry->probes[i].func; i++) |
119 | DBG("Probe %d : %p", i, entry->probes[i].func); | |
f99be407 PMF |
120 | } |
121 | ||
122 | static void * | |
9dec086e NC |
123 | tracepoint_entry_add_probe(struct tracepoint_entry *entry, |
124 | void *probe, void *data) | |
f99be407 PMF |
125 | { |
126 | int nr_probes = 0; | |
b979b346 | 127 | struct tracepoint_probe *old, *new; |
f99be407 PMF |
128 | |
129 | WARN_ON(!probe); | |
130 | ||
131 | debug_print_probes(entry); | |
9dec086e | 132 | old = entry->probes; |
f99be407 PMF |
133 | if (old) { |
134 | /* (N -> N+1), (N != 0, 1) probes */ | |
9dec086e NC |
135 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) |
136 | if (old[nr_probes].func == probe && | |
137 | old[nr_probes].data == data) | |
f99be407 PMF |
138 | return ERR_PTR(-EEXIST); |
139 | } | |
140 | /* + 2 : one for new probe, one for NULL func */ | |
141 | new = allocate_probes(nr_probes + 2); | |
142 | if (new == NULL) | |
143 | return ERR_PTR(-ENOMEM); | |
144 | if (old) | |
b979b346 | 145 | memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe)); |
9dec086e NC |
146 | new[nr_probes].func = probe; |
147 | new[nr_probes].data = data; | |
148 | new[nr_probes + 1].func = NULL; | |
f99be407 | 149 | entry->refcount = nr_probes + 1; |
9dec086e | 150 | entry->probes = new; |
f99be407 PMF |
151 | debug_print_probes(entry); |
152 | return old; | |
153 | } | |
154 | ||
155 | static void * | |
9dec086e NC |
156 | tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe, |
157 | void *data) | |
f99be407 PMF |
158 | { |
159 | int nr_probes = 0, nr_del = 0, i; | |
b979b346 | 160 | struct tracepoint_probe *old, *new; |
f99be407 | 161 | |
9dec086e | 162 | old = entry->probes; |
f99be407 PMF |
163 | |
164 | if (!old) | |
165 | return ERR_PTR(-ENOENT); | |
166 | ||
167 | debug_print_probes(entry); | |
168 | /* (N -> M), (N > 1, M >= 0) probes */ | |
9dec086e | 169 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
c66428ac DG |
170 | if (!probe || |
171 | (old[nr_probes].func == probe && | |
9dec086e | 172 | old[nr_probes].data == data)) |
f99be407 PMF |
173 | nr_del++; |
174 | } | |
175 | ||
176 | if (nr_probes - nr_del == 0) { | |
177 | /* N -> 0, (N > 1) */ | |
9dec086e | 178 | entry->probes = NULL; |
f99be407 PMF |
179 | entry->refcount = 0; |
180 | debug_print_probes(entry); | |
181 | return old; | |
182 | } else { | |
183 | int j = 0; | |
184 | /* N -> M, (N > 1, M > 0) */ | |
185 | /* + 1 for NULL */ | |
186 | new = allocate_probes(nr_probes - nr_del + 1); | |
187 | if (new == NULL) | |
188 | return ERR_PTR(-ENOMEM); | |
9dec086e NC |
189 | for (i = 0; old[i].func; i++) |
190 | if (probe && | |
191 | (old[i].func != probe || old[i].data != data)) | |
f99be407 | 192 | new[j++] = old[i]; |
9dec086e | 193 | new[nr_probes - nr_del].func = NULL; |
f99be407 | 194 | entry->refcount = nr_probes - nr_del; |
9dec086e | 195 | entry->probes = new; |
f99be407 PMF |
196 | } |
197 | debug_print_probes(entry); | |
198 | return old; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Get tracepoint if the tracepoint is present in the tracepoint hash table. | |
efa2c591 | 203 | * Must be called with ust lock held. |
f99be407 PMF |
204 | * Returns NULL if not present. |
205 | */ | |
206 | static struct tracepoint_entry *get_tracepoint(const char *name) | |
207 | { | |
10c56168 DG |
208 | struct cds_hlist_head *head; |
209 | struct cds_hlist_node *node; | |
f99be407 | 210 | struct tracepoint_entry *e; |
ff412fb5 MD |
211 | size_t name_len = strlen(name); |
212 | uint32_t hash; | |
f99be407 | 213 | |
ff412fb5 MD |
214 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
215 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
216 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
217 | } | |
218 | hash = jhash(name, name_len, 0); | |
f99be407 | 219 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
10c56168 | 220 | cds_hlist_for_each_entry(e, node, head, hlist) { |
ff412fb5 | 221 | if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) |
f99be407 PMF |
222 | return e; |
223 | } | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Add the tracepoint to the tracepoint hash table. Must be called with | |
efa2c591 | 229 | * ust lock held. |
f99be407 PMF |
230 | */ |
231 | static struct tracepoint_entry *add_tracepoint(const char *name) | |
232 | { | |
10c56168 DG |
233 | struct cds_hlist_head *head; |
234 | struct cds_hlist_node *node; | |
f99be407 | 235 | struct tracepoint_entry *e; |
ff412fb5 MD |
236 | size_t name_len = strlen(name); |
237 | uint32_t hash; | |
f99be407 | 238 | |
ff412fb5 MD |
239 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
240 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
241 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
242 | } | |
243 | hash = jhash(name, name_len, 0); | |
f99be407 | 244 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
10c56168 | 245 | cds_hlist_for_each_entry(e, node, head, hlist) { |
ff412fb5 | 246 | if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) { |
c1f20530 | 247 | DBG("tracepoint %s busy", name); |
f99be407 PMF |
248 | return ERR_PTR(-EEXIST); /* Already there */ |
249 | } | |
250 | } | |
251 | /* | |
1dba3e6c | 252 | * Using zmalloc here to allocate a variable length element. Could |
f99be407 PMF |
253 | * cause some memory fragmentation if overused. |
254 | */ | |
ff412fb5 | 255 | e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1); |
f99be407 PMF |
256 | if (!e) |
257 | return ERR_PTR(-ENOMEM); | |
ff412fb5 MD |
258 | memcpy(&e->name[0], name, name_len + 1); |
259 | e->name[name_len] = '\0'; | |
9dec086e | 260 | e->probes = NULL; |
f99be407 | 261 | e->refcount = 0; |
10c56168 | 262 | cds_hlist_add_head(&e->hlist, head); |
f99be407 PMF |
263 | return e; |
264 | } | |
265 | ||
266 | /* | |
267 | * Remove the tracepoint from the tracepoint hash table. Must be called with | |
17dfb34b | 268 | * ust_lock held. |
f99be407 | 269 | */ |
efa2c591 | 270 | static void remove_tracepoint(struct tracepoint_entry *e) |
f99be407 | 271 | { |
10c56168 | 272 | cds_hlist_del(&e->hlist); |
909bc43f | 273 | free(e); |
f99be407 PMF |
274 | } |
275 | ||
276 | /* | |
277 | * Sets the probe callback corresponding to one tracepoint. | |
278 | */ | |
279 | static void set_tracepoint(struct tracepoint_entry **entry, | |
280 | struct tracepoint *elem, int active) | |
281 | { | |
ff412fb5 | 282 | WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0); |
f99be407 PMF |
283 | |
284 | /* | |
0222e121 | 285 | * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new |
f99be407 PMF |
286 | * probe callbacks array is consistent before setting a pointer to it. |
287 | * This array is referenced by __DO_TRACE from | |
0222e121 | 288 | * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends() |
f99be407 PMF |
289 | * is used. |
290 | */ | |
9dec086e | 291 | rcu_assign_pointer(elem->probes, (*entry)->probes); |
f36c12ab | 292 | elem->state = active; |
f99be407 PMF |
293 | } |
294 | ||
295 | /* | |
296 | * Disable a tracepoint and its probe callback. | |
297 | * Note: only waiting an RCU period after setting elem->call to the empty | |
298 | * function insures that the original callback is not used anymore. This insured | |
299 | * by preempt_disable around the call site. | |
300 | */ | |
301 | static void disable_tracepoint(struct tracepoint *elem) | |
302 | { | |
f36c12ab | 303 | elem->state = 0; |
9dec086e | 304 | rcu_assign_pointer(elem->probes, NULL); |
f99be407 PMF |
305 | } |
306 | ||
307 | /** | |
308 | * tracepoint_update_probe_range - Update a probe range | |
309 | * @begin: beginning of the range | |
310 | * @end: end of the range | |
311 | * | |
312 | * Updates the probe callback corresponding to a range of tracepoints. | |
313 | */ | |
b27f8e75 | 314 | static |
f218ff28 MD |
315 | void tracepoint_update_probe_range(struct tracepoint * const *begin, |
316 | struct tracepoint * const *end) | |
f99be407 | 317 | { |
f218ff28 | 318 | struct tracepoint * const *iter; |
f99be407 PMF |
319 | struct tracepoint_entry *mark_entry; |
320 | ||
f99be407 | 321 | for (iter = begin; iter < end; iter++) { |
f08ebbe2 MD |
322 | if (!*iter) |
323 | continue; /* skip dummy */ | |
f218ff28 MD |
324 | if (!(*iter)->name) { |
325 | disable_tracepoint(*iter); | |
9dec086e NC |
326 | continue; |
327 | } | |
f218ff28 | 328 | mark_entry = get_tracepoint((*iter)->name); |
f99be407 | 329 | if (mark_entry) { |
f218ff28 | 330 | set_tracepoint(&mark_entry, *iter, |
f99be407 PMF |
331 | !!mark_entry->refcount); |
332 | } else { | |
f218ff28 | 333 | disable_tracepoint(*iter); |
f99be407 PMF |
334 | } |
335 | } | |
f99be407 PMF |
336 | } |
337 | ||
772030fe PMF |
338 | static void lib_update_tracepoints(void) |
339 | { | |
340 | struct tracepoint_lib *lib; | |
341 | ||
b27f8e75 | 342 | cds_list_for_each_entry(lib, &libs, list) { |
772030fe PMF |
343 | tracepoint_update_probe_range(lib->tracepoints_start, |
344 | lib->tracepoints_start + lib->tracepoints_count); | |
b27f8e75 | 345 | } |
772030fe PMF |
346 | } |
347 | ||
f99be407 PMF |
348 | /* |
349 | * Update probes, removing the faulty probes. | |
350 | */ | |
351 | static void tracepoint_update_probes(void) | |
352 | { | |
b27f8e75 | 353 | /* tracepoints registered from libraries and executable. */ |
474d745f | 354 | lib_update_tracepoints(); |
f99be407 PMF |
355 | } |
356 | ||
b979b346 | 357 | static struct tracepoint_probe * |
9dec086e | 358 | tracepoint_add_probe(const char *name, void *probe, void *data) |
f99be407 PMF |
359 | { |
360 | struct tracepoint_entry *entry; | |
b979b346 | 361 | struct tracepoint_probe *old; |
f99be407 PMF |
362 | |
363 | entry = get_tracepoint(name); | |
364 | if (!entry) { | |
365 | entry = add_tracepoint(name); | |
366 | if (IS_ERR(entry)) | |
b979b346 | 367 | return (struct tracepoint_probe *)entry; |
f99be407 | 368 | } |
9dec086e | 369 | old = tracepoint_entry_add_probe(entry, probe, data); |
f99be407 PMF |
370 | if (IS_ERR(old) && !entry->refcount) |
371 | remove_tracepoint(entry); | |
372 | return old; | |
373 | } | |
374 | ||
375 | /** | |
81614639 | 376 | * __tracepoint_probe_register - Connect a probe to a tracepoint |
f99be407 PMF |
377 | * @name: tracepoint name |
378 | * @probe: probe handler | |
379 | * | |
380 | * Returns 0 if ok, error value on error. | |
381 | * The probe address must at least be aligned on the architecture pointer size. | |
17dfb34b | 382 | * Called with the UST lock held. |
f99be407 | 383 | */ |
81614639 | 384 | int __tracepoint_probe_register(const char *name, void *probe, void *data) |
f99be407 PMF |
385 | { |
386 | void *old; | |
387 | ||
9dec086e | 388 | old = tracepoint_add_probe(name, probe, data); |
f99be407 PMF |
389 | if (IS_ERR(old)) |
390 | return PTR_ERR(old); | |
391 | ||
392 | tracepoint_update_probes(); /* may update entry */ | |
393 | release_probes(old); | |
394 | return 0; | |
395 | } | |
f99be407 | 396 | |
9dec086e | 397 | static void *tracepoint_remove_probe(const char *name, void *probe, void *data) |
f99be407 PMF |
398 | { |
399 | struct tracepoint_entry *entry; | |
400 | void *old; | |
401 | ||
402 | entry = get_tracepoint(name); | |
403 | if (!entry) | |
404 | return ERR_PTR(-ENOENT); | |
9dec086e | 405 | old = tracepoint_entry_remove_probe(entry, probe, data); |
f99be407 PMF |
406 | if (IS_ERR(old)) |
407 | return old; | |
408 | if (!entry->refcount) | |
409 | remove_tracepoint(entry); | |
410 | return old; | |
411 | } | |
412 | ||
413 | /** | |
414 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint | |
415 | * @name: tracepoint name | |
416 | * @probe: probe function pointer | |
9dec086e | 417 | * @probe: probe data pointer |
f99be407 | 418 | * |
17dfb34b | 419 | * Called with the UST lock held. |
f99be407 | 420 | */ |
81614639 | 421 | int __tracepoint_probe_unregister(const char *name, void *probe, void *data) |
f99be407 PMF |
422 | { |
423 | void *old; | |
424 | ||
9dec086e | 425 | old = tracepoint_remove_probe(name, probe, data); |
f99be407 PMF |
426 | if (IS_ERR(old)) |
427 | return PTR_ERR(old); | |
428 | ||
429 | tracepoint_update_probes(); /* may update entry */ | |
430 | release_probes(old); | |
431 | return 0; | |
432 | } | |
f99be407 | 433 | |
f99be407 PMF |
434 | static void tracepoint_add_old_probes(void *old) |
435 | { | |
436 | need_update = 1; | |
437 | if (old) { | |
b728d87e | 438 | struct tp_probes *tp_probes = caa_container_of(old, |
f99be407 | 439 | struct tp_probes, probes[0]); |
0222e121 | 440 | cds_list_add(&tp_probes->u.list, &old_probes); |
f99be407 PMF |
441 | } |
442 | } | |
443 | ||
444 | /** | |
445 | * tracepoint_probe_register_noupdate - register a probe but not connect | |
446 | * @name: tracepoint name | |
447 | * @probe: probe handler | |
448 | * | |
449 | * caller must call tracepoint_probe_update_all() | |
17dfb34b | 450 | * Called with the UST lock held. |
f99be407 | 451 | */ |
9dec086e NC |
452 | int tracepoint_probe_register_noupdate(const char *name, void *probe, |
453 | void *data) | |
f99be407 PMF |
454 | { |
455 | void *old; | |
456 | ||
9dec086e | 457 | old = tracepoint_add_probe(name, probe, data); |
f99be407 | 458 | if (IS_ERR(old)) { |
f99be407 PMF |
459 | return PTR_ERR(old); |
460 | } | |
461 | tracepoint_add_old_probes(old); | |
f99be407 PMF |
462 | return 0; |
463 | } | |
f99be407 PMF |
464 | |
465 | /** | |
466 | * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect | |
467 | * @name: tracepoint name | |
468 | * @probe: probe function pointer | |
469 | * | |
470 | * caller must call tracepoint_probe_update_all() | |
17dfb34b | 471 | * Called with the UST lock held. |
f99be407 | 472 | */ |
9dec086e NC |
473 | int tracepoint_probe_unregister_noupdate(const char *name, void *probe, |
474 | void *data) | |
f99be407 PMF |
475 | { |
476 | void *old; | |
477 | ||
9dec086e | 478 | old = tracepoint_remove_probe(name, probe, data); |
f99be407 | 479 | if (IS_ERR(old)) { |
f99be407 PMF |
480 | return PTR_ERR(old); |
481 | } | |
482 | tracepoint_add_old_probes(old); | |
f99be407 PMF |
483 | return 0; |
484 | } | |
f99be407 PMF |
485 | |
486 | /** | |
487 | * tracepoint_probe_update_all - update tracepoints | |
17dfb34b | 488 | * Called with the UST lock held. |
f99be407 PMF |
489 | */ |
490 | void tracepoint_probe_update_all(void) | |
491 | { | |
0222e121 | 492 | CDS_LIST_HEAD(release_probes); |
f99be407 PMF |
493 | struct tp_probes *pos, *next; |
494 | ||
f99be407 | 495 | if (!need_update) { |
f99be407 PMF |
496 | return; |
497 | } | |
0222e121 MD |
498 | if (!cds_list_empty(&old_probes)) |
499 | cds_list_replace_init(&old_probes, &release_probes); | |
f99be407 | 500 | need_update = 0; |
f99be407 PMF |
501 | |
502 | tracepoint_update_probes(); | |
0222e121 MD |
503 | cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) { |
504 | cds_list_del(&pos->u.list); | |
474d745f | 505 | synchronize_rcu(); |
909bc43f | 506 | free(pos); |
f99be407 PMF |
507 | } |
508 | } | |
f99be407 | 509 | |
474d745f PMF |
510 | void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *)) |
511 | { | |
512 | new_tracepoint_cb = cb; | |
513 | } | |
f99be407 | 514 | |
f218ff28 | 515 | static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end) |
f99be407 | 516 | { |
f218ff28 MD |
517 | if (new_tracepoint_cb) { |
518 | struct tracepoint * const *t; | |
f08ebbe2 | 519 | |
b27f8e75 | 520 | for (t = start; t < end; t++) { |
f08ebbe2 MD |
521 | if (*t) |
522 | new_tracepoint_cb(*t); | |
474d745f PMF |
523 | } |
524 | } | |
f99be407 | 525 | } |
f99be407 | 526 | |
1622ba22 MD |
527 | static |
528 | void lib_disable_tracepoints(struct tracepoint * const *begin, | |
529 | struct tracepoint * const *end) | |
530 | { | |
531 | struct tracepoint * const *iter; | |
532 | ||
533 | for (iter = begin; iter < end; iter++) { | |
534 | if (!*iter) | |
535 | continue; /* skip dummy */ | |
536 | disable_tracepoint(*iter); | |
537 | } | |
538 | ||
539 | } | |
540 | ||
b27f8e75 MD |
541 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, |
542 | int tracepoints_count) | |
474d745f | 543 | { |
b467f7a7 | 544 | struct tracepoint_lib *pl, *iter; |
474d745f | 545 | |
edaa1431 MD |
546 | init_tracepoint(); |
547 | ||
1dba3e6c | 548 | pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib)); |
474d745f PMF |
549 | |
550 | pl->tracepoints_start = tracepoints_start; | |
551 | pl->tracepoints_count = tracepoints_count; | |
552 | ||
17dfb34b | 553 | ust_lock(); |
b467f7a7 MD |
554 | /* |
555 | * We sort the libs by struct lib pointer address. | |
556 | */ | |
557 | cds_list_for_each_entry_reverse(iter, &libs, list) { | |
558 | BUG_ON(iter == pl); /* Should never be in the list twice */ | |
559 | if (iter < pl) { | |
560 | /* We belong to the location right after iter. */ | |
561 | cds_list_add(&pl->list, &iter->list); | |
562 | goto lib_added; | |
563 | } | |
564 | } | |
565 | /* We should be added at the head of the list */ | |
0222e121 | 566 | cds_list_add(&pl->list, &libs); |
b467f7a7 | 567 | lib_added: |
474d745f PMF |
568 | new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count); |
569 | ||
b27f8e75 | 570 | /* TODO: update just the loaded lib */ |
474d745f | 571 | lib_update_tracepoints(); |
17dfb34b | 572 | ust_unlock(); |
474d745f | 573 | |
1fcf7ad7 MD |
574 | DBG("just registered a tracepoints section from %p and having %d tracepoints", |
575 | tracepoints_start, tracepoints_count); | |
9dec086e | 576 | |
474d745f PMF |
577 | return 0; |
578 | } | |
579 | ||
f218ff28 | 580 | int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start) |
474d745f | 581 | { |
24b6668c | 582 | struct tracepoint_lib *lib; |
1622ba22 | 583 | int tracepoints_count; |
24b6668c | 584 | |
17dfb34b | 585 | ust_lock(); |
0222e121 | 586 | cds_list_for_each_entry(lib, &libs, list) { |
f218ff28 | 587 | if (lib->tracepoints_start == tracepoints_start) { |
24b6668c | 588 | struct tracepoint_lib *lib2free = lib; |
1622ba22 | 589 | |
0222e121 | 590 | cds_list_del(&lib->list); |
1622ba22 | 591 | tracepoints_count = lib->tracepoints_count; |
24b6668c | 592 | free(lib2free); |
1622ba22 | 593 | goto found; |
24b6668c PMF |
594 | } |
595 | } | |
1622ba22 MD |
596 | goto end; |
597 | found: | |
598 | /* | |
599 | * Force tracepoint disarm for all tracepoints of this lib. | |
600 | * This takes care of destructor of library that would leave a | |
601 | * LD_PRELOAD wrapper override function enabled for tracing, but | |
602 | * the session teardown would not be able to reach the | |
603 | * tracepoint anymore to disable it. | |
604 | */ | |
605 | lib_disable_tracepoints(tracepoints_start, | |
606 | tracepoints_start + tracepoints_count); | |
607 | DBG("just unregistered a tracepoints section from %p", | |
608 | tracepoints_start); | |
609 | end: | |
17dfb34b | 610 | ust_unlock(); |
474d745f PMF |
611 | return 0; |
612 | } | |
b27f8e75 | 613 | |
edaa1431 | 614 | void init_tracepoint(void) |
b27f8e75 | 615 | { |
edaa1431 MD |
616 | if (uatomic_xchg(&initialized, 1) == 1) |
617 | return; | |
5e96a467 | 618 | init_usterr(); |
b27f8e75 MD |
619 | } |
620 | ||
edaa1431 | 621 | void exit_tracepoint(void) |
b27f8e75 | 622 | { |
17dfb34b | 623 | initialized = 0; |
b27f8e75 | 624 | } |
40b2b5a4 MD |
625 | |
626 | /* | |
627 | * Create the wrapper symbols. | |
628 | */ | |
629 | #undef tp_rcu_read_lock_bp | |
630 | #undef tp_rcu_read_unlock_bp | |
631 | #undef tp_rcu_dereference_bp | |
632 | ||
633 | void tp_rcu_read_lock_bp(void) | |
634 | { | |
635 | rcu_read_lock_bp(); | |
636 | } | |
637 | ||
638 | void tp_rcu_read_unlock_bp(void) | |
639 | { | |
640 | rcu_read_unlock_bp(); | |
641 | } | |
642 | ||
643 | void *tp_rcu_dereference_sym_bp(void *p) | |
644 | { | |
645 | return rcu_dereference_bp(p); | |
646 | } |