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