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