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