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