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