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" |
b751f722 | 41 | #include "ltt-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 PMF |
80 | */ |
81 | #define TRACEPOINT_HASH_BITS 6 | |
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 PMF |
145 | |
146 | WARN_ON(!probe); | |
147 | ||
148 | debug_print_probes(entry); | |
9dec086e | 149 | old = entry->probes; |
f99be407 PMF |
150 | if (old) { |
151 | /* (N -> N+1), (N != 0, 1) probes */ | |
9dec086e NC |
152 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) |
153 | if (old[nr_probes].func == probe && | |
154 | old[nr_probes].data == data) | |
f99be407 PMF |
155 | return ERR_PTR(-EEXIST); |
156 | } | |
157 | /* + 2 : one for new probe, one for NULL func */ | |
158 | new = allocate_probes(nr_probes + 2); | |
159 | if (new == NULL) | |
160 | return ERR_PTR(-ENOMEM); | |
161 | if (old) | |
b979b346 | 162 | memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe)); |
9dec086e NC |
163 | new[nr_probes].func = probe; |
164 | new[nr_probes].data = data; | |
165 | new[nr_probes + 1].func = NULL; | |
f99be407 | 166 | entry->refcount = nr_probes + 1; |
9dec086e | 167 | entry->probes = new; |
f99be407 PMF |
168 | debug_print_probes(entry); |
169 | return old; | |
170 | } | |
171 | ||
172 | static void * | |
fbdeb5ec MD |
173 | tracepoint_entry_remove_probe(struct tracepoint_entry *entry, |
174 | void (*probe)(void), void *data) | |
f99be407 PMF |
175 | { |
176 | int nr_probes = 0, nr_del = 0, i; | |
b979b346 | 177 | struct tracepoint_probe *old, *new; |
f99be407 | 178 | |
9dec086e | 179 | old = entry->probes; |
f99be407 PMF |
180 | |
181 | if (!old) | |
182 | return ERR_PTR(-ENOENT); | |
183 | ||
184 | debug_print_probes(entry); | |
185 | /* (N -> M), (N > 1, M >= 0) probes */ | |
9dec086e | 186 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
c66428ac DG |
187 | if (!probe || |
188 | (old[nr_probes].func == probe && | |
9dec086e | 189 | old[nr_probes].data == data)) |
f99be407 PMF |
190 | nr_del++; |
191 | } | |
192 | ||
193 | if (nr_probes - nr_del == 0) { | |
194 | /* N -> 0, (N > 1) */ | |
9dec086e | 195 | entry->probes = NULL; |
f99be407 PMF |
196 | entry->refcount = 0; |
197 | debug_print_probes(entry); | |
198 | return old; | |
199 | } else { | |
200 | int j = 0; | |
201 | /* N -> M, (N > 1, M > 0) */ | |
202 | /* + 1 for NULL */ | |
203 | new = allocate_probes(nr_probes - nr_del + 1); | |
204 | if (new == NULL) | |
205 | return ERR_PTR(-ENOMEM); | |
9dec086e NC |
206 | for (i = 0; old[i].func; i++) |
207 | if (probe && | |
208 | (old[i].func != probe || old[i].data != data)) | |
f99be407 | 209 | new[j++] = old[i]; |
9dec086e | 210 | new[nr_probes - nr_del].func = NULL; |
f99be407 | 211 | entry->refcount = nr_probes - nr_del; |
9dec086e | 212 | entry->probes = new; |
f99be407 PMF |
213 | } |
214 | debug_print_probes(entry); | |
215 | return old; | |
216 | } | |
217 | ||
218 | /* | |
219 | * Get tracepoint if the tracepoint is present in the tracepoint hash table. | |
8792fbae | 220 | * Must be called with tracepoint mutex held. |
f99be407 PMF |
221 | * Returns NULL if not present. |
222 | */ | |
223 | static struct tracepoint_entry *get_tracepoint(const char *name) | |
224 | { | |
10c56168 DG |
225 | struct cds_hlist_head *head; |
226 | struct cds_hlist_node *node; | |
f99be407 | 227 | struct tracepoint_entry *e; |
ff412fb5 MD |
228 | size_t name_len = strlen(name); |
229 | uint32_t hash; | |
f99be407 | 230 | |
ff412fb5 MD |
231 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
232 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
233 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
234 | } | |
235 | hash = jhash(name, name_len, 0); | |
f99be407 | 236 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
10c56168 | 237 | cds_hlist_for_each_entry(e, node, head, hlist) { |
ff412fb5 | 238 | if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) |
f99be407 PMF |
239 | return e; |
240 | } | |
241 | return NULL; | |
242 | } | |
243 | ||
244 | /* | |
245 | * Add the tracepoint to the tracepoint hash table. Must be called with | |
8792fbae | 246 | * tracepoint mutex held. |
f99be407 | 247 | */ |
67e5f391 MD |
248 | static struct tracepoint_entry *add_tracepoint(const char *name, |
249 | const char *signature) | |
f99be407 | 250 | { |
10c56168 DG |
251 | struct cds_hlist_head *head; |
252 | struct cds_hlist_node *node; | |
f99be407 | 253 | struct tracepoint_entry *e; |
ff412fb5 MD |
254 | size_t name_len = strlen(name); |
255 | uint32_t hash; | |
f99be407 | 256 | |
ff412fb5 MD |
257 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
258 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
259 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
260 | } | |
261 | hash = jhash(name, name_len, 0); | |
f99be407 | 262 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
10c56168 | 263 | cds_hlist_for_each_entry(e, node, head, hlist) { |
ff412fb5 | 264 | if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) { |
c1f20530 | 265 | DBG("tracepoint %s busy", name); |
f99be407 PMF |
266 | return ERR_PTR(-EEXIST); /* Already there */ |
267 | } | |
268 | } | |
269 | /* | |
1dba3e6c | 270 | * Using zmalloc here to allocate a variable length element. Could |
f99be407 PMF |
271 | * cause some memory fragmentation if overused. |
272 | */ | |
ff412fb5 | 273 | e = zmalloc(sizeof(struct tracepoint_entry) + name_len + 1); |
f99be407 PMF |
274 | if (!e) |
275 | return ERR_PTR(-ENOMEM); | |
ff412fb5 MD |
276 | memcpy(&e->name[0], name, name_len + 1); |
277 | e->name[name_len] = '\0'; | |
9dec086e | 278 | e->probes = NULL; |
f99be407 | 279 | e->refcount = 0; |
67e5f391 | 280 | e->signature = signature; |
10c56168 | 281 | cds_hlist_add_head(&e->hlist, head); |
f99be407 PMF |
282 | return e; |
283 | } | |
284 | ||
285 | /* | |
286 | * Remove the tracepoint from the tracepoint hash table. Must be called with | |
8792fbae | 287 | * tracepoint mutex held. |
f99be407 | 288 | */ |
efa2c591 | 289 | static void remove_tracepoint(struct tracepoint_entry *e) |
f99be407 | 290 | { |
10c56168 | 291 | cds_hlist_del(&e->hlist); |
909bc43f | 292 | free(e); |
f99be407 PMF |
293 | } |
294 | ||
295 | /* | |
296 | * Sets the probe callback corresponding to one tracepoint. | |
297 | */ | |
298 | static void set_tracepoint(struct tracepoint_entry **entry, | |
299 | struct tracepoint *elem, int active) | |
300 | { | |
ff412fb5 | 301 | WARN_ON(strncmp((*entry)->name, elem->name, LTTNG_UST_SYM_NAME_LEN - 1) != 0); |
67e5f391 MD |
302 | /* |
303 | * Check that signatures match before connecting a probe to a | |
304 | * tracepoint. Warn the user if they don't. | |
305 | */ | |
306 | if (strcmp(elem->signature, (*entry)->signature) != 0) { | |
307 | static int warned = 0; | |
308 | ||
309 | /* Only print once, don't flood console. */ | |
310 | if (!warned) { | |
311 | WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application."); | |
312 | WARN("Tracepoint \"%s\" signatures: call: \"%s\" vs probe: \"%s\".", | |
313 | elem->name, elem->signature, (*entry)->signature); | |
314 | warned = 1; | |
315 | } | |
316 | /* Don't accept connecting non-matching signatures. */ | |
317 | return; | |
318 | } | |
f99be407 PMF |
319 | |
320 | /* | |
0222e121 | 321 | * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new |
f99be407 PMF |
322 | * probe callbacks array is consistent before setting a pointer to it. |
323 | * This array is referenced by __DO_TRACE from | |
0222e121 | 324 | * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends() |
f99be407 PMF |
325 | * is used. |
326 | */ | |
9dec086e | 327 | rcu_assign_pointer(elem->probes, (*entry)->probes); |
f36c12ab | 328 | elem->state = active; |
f99be407 PMF |
329 | } |
330 | ||
331 | /* | |
332 | * Disable a tracepoint and its probe callback. | |
333 | * Note: only waiting an RCU period after setting elem->call to the empty | |
334 | * function insures that the original callback is not used anymore. This insured | |
335 | * by preempt_disable around the call site. | |
336 | */ | |
337 | static void disable_tracepoint(struct tracepoint *elem) | |
338 | { | |
f36c12ab | 339 | elem->state = 0; |
9dec086e | 340 | rcu_assign_pointer(elem->probes, NULL); |
f99be407 PMF |
341 | } |
342 | ||
343 | /** | |
344 | * tracepoint_update_probe_range - Update a probe range | |
345 | * @begin: beginning of the range | |
346 | * @end: end of the range | |
347 | * | |
348 | * Updates the probe callback corresponding to a range of tracepoints. | |
349 | */ | |
b27f8e75 | 350 | static |
f218ff28 MD |
351 | void tracepoint_update_probe_range(struct tracepoint * const *begin, |
352 | struct tracepoint * const *end) | |
f99be407 | 353 | { |
f218ff28 | 354 | struct tracepoint * const *iter; |
f99be407 PMF |
355 | struct tracepoint_entry *mark_entry; |
356 | ||
f99be407 | 357 | for (iter = begin; iter < end; iter++) { |
f08ebbe2 MD |
358 | if (!*iter) |
359 | continue; /* skip dummy */ | |
f218ff28 MD |
360 | if (!(*iter)->name) { |
361 | disable_tracepoint(*iter); | |
9dec086e NC |
362 | continue; |
363 | } | |
f218ff28 | 364 | mark_entry = get_tracepoint((*iter)->name); |
f99be407 | 365 | if (mark_entry) { |
f218ff28 | 366 | set_tracepoint(&mark_entry, *iter, |
f99be407 PMF |
367 | !!mark_entry->refcount); |
368 | } else { | |
f218ff28 | 369 | disable_tracepoint(*iter); |
f99be407 PMF |
370 | } |
371 | } | |
f99be407 PMF |
372 | } |
373 | ||
772030fe PMF |
374 | static void lib_update_tracepoints(void) |
375 | { | |
376 | struct tracepoint_lib *lib; | |
377 | ||
b27f8e75 | 378 | cds_list_for_each_entry(lib, &libs, list) { |
772030fe PMF |
379 | tracepoint_update_probe_range(lib->tracepoints_start, |
380 | lib->tracepoints_start + lib->tracepoints_count); | |
b27f8e75 | 381 | } |
772030fe PMF |
382 | } |
383 | ||
f99be407 PMF |
384 | /* |
385 | * Update probes, removing the faulty probes. | |
386 | */ | |
387 | static void tracepoint_update_probes(void) | |
388 | { | |
b27f8e75 | 389 | /* tracepoints registered from libraries and executable. */ |
474d745f | 390 | lib_update_tracepoints(); |
f99be407 PMF |
391 | } |
392 | ||
b979b346 | 393 | static struct tracepoint_probe * |
fbdeb5ec | 394 | tracepoint_add_probe(const char *name, void (*probe)(void), void *data, |
67e5f391 | 395 | const char *signature) |
f99be407 PMF |
396 | { |
397 | struct tracepoint_entry *entry; | |
b979b346 | 398 | struct tracepoint_probe *old; |
f99be407 PMF |
399 | |
400 | entry = get_tracepoint(name); | |
401 | if (!entry) { | |
67e5f391 | 402 | entry = add_tracepoint(name, signature); |
f99be407 | 403 | if (IS_ERR(entry)) |
b979b346 | 404 | return (struct tracepoint_probe *)entry; |
f99be407 | 405 | } |
9dec086e | 406 | old = tracepoint_entry_add_probe(entry, probe, data); |
f99be407 PMF |
407 | if (IS_ERR(old) && !entry->refcount) |
408 | remove_tracepoint(entry); | |
409 | return old; | |
410 | } | |
411 | ||
412 | /** | |
81614639 | 413 | * __tracepoint_probe_register - Connect a probe to a tracepoint |
f99be407 PMF |
414 | * @name: tracepoint name |
415 | * @probe: probe handler | |
416 | * | |
417 | * Returns 0 if ok, error value on error. | |
418 | * The probe address must at least be aligned on the architecture pointer size. | |
8792fbae | 419 | * Called with the tracepoint mutex held. |
f99be407 | 420 | */ |
fbdeb5ec MD |
421 | int __tracepoint_probe_register(const char *name, void (*probe)(void), |
422 | void *data, const char *signature) | |
f99be407 PMF |
423 | { |
424 | void *old; | |
8792fbae | 425 | int ret = 0; |
f99be407 | 426 | |
05780d81 MD |
427 | DBG("Registering probe to tracepoint %s", name); |
428 | ||
8792fbae | 429 | pthread_mutex_lock(&tracepoint_mutex); |
67e5f391 | 430 | old = tracepoint_add_probe(name, probe, data, signature); |
8792fbae MD |
431 | if (IS_ERR(old)) { |
432 | ret = PTR_ERR(old); | |
433 | goto end; | |
434 | } | |
f99be407 PMF |
435 | |
436 | tracepoint_update_probes(); /* may update entry */ | |
437 | release_probes(old); | |
8792fbae MD |
438 | end: |
439 | pthread_mutex_unlock(&tracepoint_mutex); | |
440 | return ret; | |
f99be407 | 441 | } |
f99be407 | 442 | |
fbdeb5ec MD |
443 | static void *tracepoint_remove_probe(const char *name, void (*probe)(void), |
444 | void *data) | |
f99be407 PMF |
445 | { |
446 | struct tracepoint_entry *entry; | |
447 | void *old; | |
448 | ||
449 | entry = get_tracepoint(name); | |
450 | if (!entry) | |
451 | return ERR_PTR(-ENOENT); | |
9dec086e | 452 | old = tracepoint_entry_remove_probe(entry, probe, data); |
f99be407 PMF |
453 | if (IS_ERR(old)) |
454 | return old; | |
455 | if (!entry->refcount) | |
456 | remove_tracepoint(entry); | |
457 | return old; | |
458 | } | |
459 | ||
460 | /** | |
461 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint | |
462 | * @name: tracepoint name | |
463 | * @probe: probe function pointer | |
9dec086e | 464 | * @probe: probe data pointer |
f99be407 | 465 | */ |
fbdeb5ec MD |
466 | int __tracepoint_probe_unregister(const char *name, void (*probe)(void), |
467 | void *data) | |
f99be407 PMF |
468 | { |
469 | void *old; | |
8792fbae | 470 | int ret = 0; |
f99be407 | 471 | |
05780d81 MD |
472 | DBG("Un-registering probe from tracepoint %s", name); |
473 | ||
8792fbae | 474 | pthread_mutex_lock(&tracepoint_mutex); |
9dec086e | 475 | old = tracepoint_remove_probe(name, probe, data); |
8792fbae MD |
476 | if (IS_ERR(old)) { |
477 | ret = PTR_ERR(old); | |
478 | goto end; | |
479 | } | |
f99be407 PMF |
480 | tracepoint_update_probes(); /* may update entry */ |
481 | release_probes(old); | |
8792fbae MD |
482 | end: |
483 | pthread_mutex_unlock(&tracepoint_mutex); | |
484 | return ret; | |
f99be407 | 485 | } |
f99be407 | 486 | |
f99be407 PMF |
487 | static void tracepoint_add_old_probes(void *old) |
488 | { | |
489 | need_update = 1; | |
490 | if (old) { | |
b728d87e | 491 | struct tp_probes *tp_probes = caa_container_of(old, |
f99be407 | 492 | struct tp_probes, probes[0]); |
0222e121 | 493 | cds_list_add(&tp_probes->u.list, &old_probes); |
f99be407 PMF |
494 | } |
495 | } | |
496 | ||
497 | /** | |
498 | * tracepoint_probe_register_noupdate - register a probe but not connect | |
499 | * @name: tracepoint name | |
500 | * @probe: probe handler | |
501 | * | |
502 | * caller must call tracepoint_probe_update_all() | |
503 | */ | |
fbdeb5ec | 504 | int tracepoint_probe_register_noupdate(const char *name, void (*probe)(void), |
67e5f391 | 505 | void *data, const char *signature) |
f99be407 PMF |
506 | { |
507 | void *old; | |
8792fbae | 508 | int ret = 0; |
f99be407 | 509 | |
8792fbae | 510 | pthread_mutex_lock(&tracepoint_mutex); |
67e5f391 | 511 | old = tracepoint_add_probe(name, probe, data, signature); |
f99be407 | 512 | if (IS_ERR(old)) { |
8792fbae MD |
513 | ret = PTR_ERR(old); |
514 | goto end; | |
f99be407 PMF |
515 | } |
516 | tracepoint_add_old_probes(old); | |
8792fbae MD |
517 | end: |
518 | pthread_mutex_unlock(&tracepoint_mutex); | |
519 | return ret; | |
f99be407 | 520 | } |
f99be407 PMF |
521 | |
522 | /** | |
523 | * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect | |
524 | * @name: tracepoint name | |
525 | * @probe: probe function pointer | |
526 | * | |
527 | * caller must call tracepoint_probe_update_all() | |
8792fbae | 528 | * Called with the tracepoint mutex held. |
f99be407 | 529 | */ |
fbdeb5ec | 530 | int tracepoint_probe_unregister_noupdate(const char *name, void (*probe)(void), |
9dec086e | 531 | void *data) |
f99be407 PMF |
532 | { |
533 | void *old; | |
8792fbae | 534 | int ret = 0; |
f99be407 | 535 | |
05780d81 MD |
536 | DBG("Un-registering probe from tracepoint %s", name); |
537 | ||
8792fbae | 538 | pthread_mutex_lock(&tracepoint_mutex); |
9dec086e | 539 | old = tracepoint_remove_probe(name, probe, data); |
f99be407 | 540 | if (IS_ERR(old)) { |
8792fbae MD |
541 | ret = PTR_ERR(old); |
542 | goto end; | |
f99be407 PMF |
543 | } |
544 | tracepoint_add_old_probes(old); | |
8792fbae MD |
545 | end: |
546 | pthread_mutex_unlock(&tracepoint_mutex); | |
547 | return ret; | |
f99be407 | 548 | } |
f99be407 PMF |
549 | |
550 | /** | |
551 | * tracepoint_probe_update_all - update tracepoints | |
552 | */ | |
553 | void tracepoint_probe_update_all(void) | |
554 | { | |
0222e121 | 555 | CDS_LIST_HEAD(release_probes); |
f99be407 PMF |
556 | struct tp_probes *pos, *next; |
557 | ||
8792fbae | 558 | pthread_mutex_lock(&tracepoint_mutex); |
f99be407 | 559 | if (!need_update) { |
8792fbae | 560 | goto end; |
f99be407 | 561 | } |
0222e121 MD |
562 | if (!cds_list_empty(&old_probes)) |
563 | cds_list_replace_init(&old_probes, &release_probes); | |
f99be407 | 564 | need_update = 0; |
f99be407 PMF |
565 | |
566 | tracepoint_update_probes(); | |
0222e121 MD |
567 | cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) { |
568 | cds_list_del(&pos->u.list); | |
474d745f | 569 | synchronize_rcu(); |
909bc43f | 570 | free(pos); |
f99be407 | 571 | } |
8792fbae MD |
572 | end: |
573 | pthread_mutex_unlock(&tracepoint_mutex); | |
f99be407 | 574 | } |
f99be407 | 575 | |
474d745f PMF |
576 | void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *)) |
577 | { | |
578 | new_tracepoint_cb = cb; | |
579 | } | |
f99be407 | 580 | |
f218ff28 | 581 | static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end) |
f99be407 | 582 | { |
f218ff28 MD |
583 | if (new_tracepoint_cb) { |
584 | struct tracepoint * const *t; | |
f08ebbe2 | 585 | |
b27f8e75 | 586 | for (t = start; t < end; t++) { |
f08ebbe2 MD |
587 | if (*t) |
588 | new_tracepoint_cb(*t); | |
474d745f PMF |
589 | } |
590 | } | |
f99be407 | 591 | } |
f99be407 | 592 | |
1622ba22 MD |
593 | static |
594 | void lib_disable_tracepoints(struct tracepoint * const *begin, | |
595 | struct tracepoint * const *end) | |
596 | { | |
597 | struct tracepoint * const *iter; | |
598 | ||
599 | for (iter = begin; iter < end; iter++) { | |
600 | if (!*iter) | |
601 | continue; /* skip dummy */ | |
602 | disable_tracepoint(*iter); | |
603 | } | |
604 | ||
605 | } | |
606 | ||
b27f8e75 MD |
607 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, |
608 | int tracepoints_count) | |
474d745f | 609 | { |
b467f7a7 | 610 | struct tracepoint_lib *pl, *iter; |
474d745f | 611 | |
edaa1431 MD |
612 | init_tracepoint(); |
613 | ||
1dba3e6c | 614 | pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib)); |
474d745f PMF |
615 | |
616 | pl->tracepoints_start = tracepoints_start; | |
617 | pl->tracepoints_count = tracepoints_count; | |
618 | ||
8792fbae | 619 | pthread_mutex_lock(&tracepoint_mutex); |
b467f7a7 MD |
620 | /* |
621 | * We sort the libs by struct lib pointer address. | |
622 | */ | |
623 | cds_list_for_each_entry_reverse(iter, &libs, list) { | |
624 | BUG_ON(iter == pl); /* Should never be in the list twice */ | |
625 | if (iter < pl) { | |
626 | /* We belong to the location right after iter. */ | |
627 | cds_list_add(&pl->list, &iter->list); | |
628 | goto lib_added; | |
629 | } | |
630 | } | |
631 | /* We should be added at the head of the list */ | |
0222e121 | 632 | cds_list_add(&pl->list, &libs); |
b467f7a7 | 633 | lib_added: |
474d745f PMF |
634 | new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count); |
635 | ||
b27f8e75 | 636 | /* TODO: update just the loaded lib */ |
474d745f | 637 | lib_update_tracepoints(); |
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 | } |