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