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