| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2008-2011 Mathieu Desnoyers |
| 5 | * Copyright (C) 2009 Pierre-Marc Fournier |
| 6 | * |
| 7 | * Ported to userspace by Pierre-Marc Fournier. |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <errno.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stddef.h> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include <urcu/arch.h> |
| 17 | #include <lttng/urcu/urcu-ust.h> |
| 18 | #include <urcu/hlist.h> |
| 19 | #include <urcu/uatomic.h> |
| 20 | #include <urcu/compiler.h> |
| 21 | #include <urcu/system.h> |
| 22 | |
| 23 | #include <lttng/tracepoint.h> |
| 24 | #include <lttng/ust-abi.h> /* for LTTNG_UST_ABI_SYM_NAME_LEN */ |
| 25 | #include <lttng/ust-common.h> |
| 26 | |
| 27 | #include "common/logging.h" |
| 28 | #include "common/macros.h" |
| 29 | |
| 30 | #include "lib/lttng-ust-tracepoint/tracepoint.h" |
| 31 | #include "common/tracepoint.h" |
| 32 | #include "common/jhash.h" |
| 33 | #include "common/err-ptr.h" |
| 34 | |
| 35 | /* Test compiler support for weak symbols with hidden visibility. */ |
| 36 | int lttng_ust_tracepoint_test_symbol1 __attribute__((weak, visibility("hidden"))); |
| 37 | void *lttng_ust_tracepoint_test_symbol2 __attribute__((weak, visibility("hidden"))); |
| 38 | struct { |
| 39 | char a[24]; |
| 40 | } lttng_ust_tracepoint_test_symbol3 __attribute__((weak, visibility("hidden"))); |
| 41 | |
| 42 | /* Set to 1 to enable tracepoint debug output */ |
| 43 | static const int tracepoint_debug; |
| 44 | static int initialized; |
| 45 | |
| 46 | /* |
| 47 | * If tracepoint_destructors_state = 1, tracepoint destructors are |
| 48 | * enabled. They are disabled otherwise. |
| 49 | */ |
| 50 | static int tracepoint_destructors_state = 1; |
| 51 | |
| 52 | static void (*new_tracepoint_cb)(struct lttng_ust_tracepoint *); |
| 53 | |
| 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 | |
| 67 | /* |
| 68 | * libraries that contain tracepoints (struct tracepoint_lib). |
| 69 | * Protected by tracepoint mutex. |
| 70 | */ |
| 71 | static CDS_LIST_HEAD(libs); |
| 72 | |
| 73 | /* |
| 74 | * The tracepoint mutex protects the library tracepoints, the hash table, and |
| 75 | * the library list. |
| 76 | * All calls to the tracepoint API must be protected by the tracepoint mutex, |
| 77 | * excepts calls to tracepoint_register_lib and |
| 78 | * tracepoint_unregister_lib, which take the tracepoint mutex themselves. |
| 79 | */ |
| 80 | |
| 81 | /* |
| 82 | * Tracepoint hash table, containing the active tracepoints. |
| 83 | * Protected by tracepoint mutex. |
| 84 | */ |
| 85 | #define TRACEPOINT_HASH_BITS 12 |
| 86 | #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS) |
| 87 | static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE]; |
| 88 | |
| 89 | static CDS_LIST_HEAD(old_probes); |
| 90 | static int need_update; |
| 91 | |
| 92 | static CDS_LIST_HEAD(release_queue); |
| 93 | static int release_queue_need_update; |
| 94 | |
| 95 | /* |
| 96 | * Note about RCU : |
| 97 | * It is used to to delay the free of multiple probes array until a quiescent |
| 98 | * state is reached. |
| 99 | * Tracepoint entries modifications are protected by the tracepoint mutex. |
| 100 | */ |
| 101 | struct tracepoint_entry { |
| 102 | struct cds_hlist_node hlist; |
| 103 | struct lttng_ust_tracepoint_probe *probes; |
| 104 | int refcount; /* Number of times armed. 0 if disarmed. */ |
| 105 | int callsite_refcount; /* how many libs use this tracepoint */ |
| 106 | char *signature; |
| 107 | char *provider_name; |
| 108 | char *event_name; |
| 109 | }; |
| 110 | |
| 111 | struct tp_probes { |
| 112 | union { |
| 113 | struct cds_list_head list; |
| 114 | /* Field below only used for call_rcu scheme */ |
| 115 | /* struct rcu_head head; */ |
| 116 | } u; |
| 117 | struct lttng_ust_tracepoint_probe probes[0]; |
| 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * Callsite hash table, containing the tracepoint call sites. |
| 122 | * Protected by tracepoint mutex. |
| 123 | */ |
| 124 | #define CALLSITE_HASH_BITS 12 |
| 125 | #define CALLSITE_TABLE_SIZE (1 << CALLSITE_HASH_BITS) |
| 126 | static struct cds_hlist_head callsite_table[CALLSITE_TABLE_SIZE]; |
| 127 | |
| 128 | struct callsite_entry { |
| 129 | struct cds_hlist_node hlist; /* hash table node */ |
| 130 | struct cds_list_head node; /* lib list of callsites node */ |
| 131 | struct lttng_ust_tracepoint *tp; |
| 132 | bool tp_entry_callsite_ref; /* Has a tp_entry took a ref on this callsite */ |
| 133 | }; |
| 134 | |
| 135 | lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX == LTTNG_UST_ABI_SYM_NAME_LEN, |
| 136 | "Tracepoint name max length mismatch between UST ABI and tracepoint API", |
| 137 | Tracepoint_name_max_length_mismatch); |
| 138 | |
| 139 | static |
| 140 | bool lttng_ust_tp_validate_event_name(const struct lttng_ust_tracepoint *tp) |
| 141 | { |
| 142 | if (strlen(tp->provider_name) + 1 + |
| 143 | strlen(tp->event_name) >= LTTNG_UST_TRACEPOINT_NAME_LEN_MAX) |
| 144 | return false; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | /* coverity[+alloc] */ |
| 149 | static void *allocate_probes(int count) |
| 150 | { |
| 151 | struct tp_probes *p = |
| 152 | zmalloc(count * sizeof(struct lttng_ust_tracepoint_probe) |
| 153 | + sizeof(struct tp_probes)); |
| 154 | return p == NULL ? NULL : p->probes; |
| 155 | } |
| 156 | |
| 157 | /* coverity[+free : arg-0] */ |
| 158 | static void release_probes(void *old) |
| 159 | { |
| 160 | if (old) { |
| 161 | struct tp_probes *tp_probes = caa_container_of(old, |
| 162 | struct tp_probes, probes[0]); |
| 163 | lttng_ust_urcu_synchronize_rcu(); |
| 164 | free(tp_probes); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void debug_print_probes(struct tracepoint_entry *entry) |
| 169 | { |
| 170 | int i; |
| 171 | |
| 172 | if (!tracepoint_debug || !entry->probes) |
| 173 | return; |
| 174 | |
| 175 | for (i = 0; entry->probes[i].func; i++) |
| 176 | DBG("Probe %d : %p", i, entry->probes[i].func); |
| 177 | } |
| 178 | |
| 179 | static void * |
| 180 | tracepoint_entry_add_probe(struct tracepoint_entry *entry, |
| 181 | void (*probe)(void), void *data) |
| 182 | { |
| 183 | int nr_probes = 0; |
| 184 | struct lttng_ust_tracepoint_probe *old, *new; |
| 185 | |
| 186 | if (!probe) { |
| 187 | WARN_ON(1); |
| 188 | return ERR_PTR(-EINVAL); |
| 189 | } |
| 190 | debug_print_probes(entry); |
| 191 | old = entry->probes; |
| 192 | if (old) { |
| 193 | /* (N -> N+1), (N != 0, 1) probes */ |
| 194 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) |
| 195 | if (old[nr_probes].func == probe && |
| 196 | old[nr_probes].data == data) |
| 197 | return ERR_PTR(-EEXIST); |
| 198 | } |
| 199 | /* + 2 : one for new probe, one for NULL func */ |
| 200 | new = allocate_probes(nr_probes + 2); |
| 201 | if (new == NULL) |
| 202 | return ERR_PTR(-ENOMEM); |
| 203 | if (old) |
| 204 | memcpy(new, old, |
| 205 | nr_probes * sizeof(struct lttng_ust_tracepoint_probe)); |
| 206 | new[nr_probes].func = probe; |
| 207 | new[nr_probes].data = data; |
| 208 | new[nr_probes + 1].func = NULL; |
| 209 | entry->refcount = nr_probes + 1; |
| 210 | entry->probes = new; |
| 211 | debug_print_probes(entry); |
| 212 | return old; |
| 213 | } |
| 214 | |
| 215 | static void * |
| 216 | tracepoint_entry_remove_probe(struct tracepoint_entry *entry, |
| 217 | void (*probe)(void), void *data) |
| 218 | { |
| 219 | int nr_probes = 0, nr_del = 0, i; |
| 220 | struct lttng_ust_tracepoint_probe *old, *new; |
| 221 | |
| 222 | old = entry->probes; |
| 223 | |
| 224 | if (!old) |
| 225 | return ERR_PTR(-ENOENT); |
| 226 | |
| 227 | debug_print_probes(entry); |
| 228 | /* (N -> M), (N > 1, M >= 0) probes */ |
| 229 | if (probe) { |
| 230 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
| 231 | if (old[nr_probes].func == probe && |
| 232 | old[nr_probes].data == data) |
| 233 | nr_del++; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (nr_probes - nr_del == 0) { |
| 238 | /* N -> 0, (N > 1) */ |
| 239 | entry->probes = NULL; |
| 240 | entry->refcount = 0; |
| 241 | debug_print_probes(entry); |
| 242 | return old; |
| 243 | } else { |
| 244 | int j = 0; |
| 245 | /* N -> M, (N > 1, M > 0) */ |
| 246 | /* + 1 for NULL */ |
| 247 | new = allocate_probes(nr_probes - nr_del + 1); |
| 248 | if (new == NULL) |
| 249 | return ERR_PTR(-ENOMEM); |
| 250 | for (i = 0; old[i].func; i++) |
| 251 | if (old[i].func != probe || old[i].data != data) |
| 252 | new[j++] = old[i]; |
| 253 | new[nr_probes - nr_del].func = NULL; |
| 254 | entry->refcount = nr_probes - nr_del; |
| 255 | entry->probes = new; |
| 256 | } |
| 257 | debug_print_probes(entry); |
| 258 | return old; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Get tracepoint if the tracepoint is present in the tracepoint hash table. |
| 263 | * Must be called with tracepoint mutex held. |
| 264 | * Returns NULL if not present. |
| 265 | */ |
| 266 | static struct tracepoint_entry *get_tracepoint(const char *provider_name, const char *event_name) |
| 267 | { |
| 268 | struct cds_hlist_head *head; |
| 269 | struct cds_hlist_node *node; |
| 270 | struct tracepoint_entry *e; |
| 271 | uint32_t hash; |
| 272 | |
| 273 | hash = jhash(provider_name, strlen(provider_name), 0) ^ |
| 274 | jhash(event_name, strlen(event_name), 0); |
| 275 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
| 276 | cds_hlist_for_each_entry(e, node, head, hlist) { |
| 277 | if (!strcmp(event_name, e->event_name) && !strcmp(provider_name, e->provider_name)) |
| 278 | return e; |
| 279 | } |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Add the tracepoint to the tracepoint hash table. Must be called with |
| 285 | * tracepoint mutex held. |
| 286 | */ |
| 287 | static struct tracepoint_entry *add_tracepoint(const char *provider_name, const char *event_name, |
| 288 | const char *signature) |
| 289 | { |
| 290 | struct cds_hlist_head *head; |
| 291 | struct cds_hlist_node *node; |
| 292 | struct tracepoint_entry *e; |
| 293 | size_t sig_len = strlen(signature); |
| 294 | size_t sig_off, provider_name_off, event_name_off; |
| 295 | size_t provider_name_len = strlen(provider_name); |
| 296 | size_t event_name_len = strlen(event_name); |
| 297 | uint32_t hash; |
| 298 | |
| 299 | hash = jhash(provider_name, provider_name_len, 0) ^ |
| 300 | jhash(event_name, event_name_len, 0); |
| 301 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
| 302 | cds_hlist_for_each_entry(e, node, head, hlist) { |
| 303 | if (!strcmp(event_name, e->event_name) && !strcmp(provider_name, e->provider_name)) { |
| 304 | DBG("tracepoint \"%s:%s\" busy", provider_name, event_name); |
| 305 | return ERR_PTR(-EEXIST); /* Already there */ |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Using zmalloc here to allocate a variable length elements: name and |
| 311 | * signature. Could cause some memory fragmentation if overused. |
| 312 | */ |
| 313 | provider_name_off = sizeof(struct tracepoint_entry); |
| 314 | event_name_off = provider_name_off + provider_name_len + 1; |
| 315 | sig_off = event_name_off + event_name_len + 1; |
| 316 | |
| 317 | e = zmalloc(sizeof(struct tracepoint_entry) + provider_name_len + 1 + |
| 318 | event_name_len + 1 + sig_len + 1); |
| 319 | if (!e) |
| 320 | return ERR_PTR(-ENOMEM); |
| 321 | e->provider_name = (char *) e + provider_name_off; |
| 322 | memcpy(e->provider_name, provider_name, provider_name_len + 1); |
| 323 | e->provider_name[provider_name_len] = '\0'; |
| 324 | |
| 325 | e->event_name = (char *) e + event_name_off; |
| 326 | memcpy(e->event_name, event_name, event_name_len + 1); |
| 327 | e->event_name[event_name_len] = '\0'; |
| 328 | |
| 329 | e->signature = (char *) e + sig_off; |
| 330 | memcpy(e->signature, signature, sig_len + 1); |
| 331 | e->signature[sig_len] = '\0'; |
| 332 | |
| 333 | e->probes = NULL; |
| 334 | e->refcount = 0; |
| 335 | e->callsite_refcount = 0; |
| 336 | |
| 337 | cds_hlist_add_head(&e->hlist, head); |
| 338 | return e; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Remove the tracepoint from the tracepoint hash table. Must be called with |
| 343 | * tracepoint mutex held. |
| 344 | */ |
| 345 | static void remove_tracepoint(struct tracepoint_entry *e) |
| 346 | { |
| 347 | cds_hlist_del(&e->hlist); |
| 348 | free(e); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Sets the probe callback corresponding to one tracepoint. |
| 353 | */ |
| 354 | static void set_tracepoint(struct tracepoint_entry **entry, |
| 355 | struct lttng_ust_tracepoint *elem, int active) |
| 356 | { |
| 357 | WARN_ON(strcmp((*entry)->provider_name, elem->provider_name) != 0); |
| 358 | WARN_ON(strcmp((*entry)->event_name, elem->event_name) != 0); |
| 359 | /* |
| 360 | * Check that signatures match before connecting a probe to a |
| 361 | * tracepoint. Warn the user if they don't. |
| 362 | */ |
| 363 | if (strcmp(elem->signature, (*entry)->signature) != 0) { |
| 364 | static int warned = 0; |
| 365 | |
| 366 | /* Only print once, don't flood console. */ |
| 367 | if (!warned) { |
| 368 | WARN("Tracepoint signature mismatch, not enabling one or more tracepoints. Ensure that the tracepoint probes prototypes match the application."); |
| 369 | WARN("Tracepoint \"%s:%s\" signatures: call: \"%s\" vs probe: \"%s\".", |
| 370 | elem->provider_name, elem->event_name, elem->signature, (*entry)->signature); |
| 371 | warned = 1; |
| 372 | } |
| 373 | /* Don't accept connecting non-matching signatures. */ |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new |
| 379 | * probe callbacks array is consistent before setting a pointer to it. |
| 380 | * This array is referenced by __DO_TRACE from |
| 381 | * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends() |
| 382 | * is used. |
| 383 | */ |
| 384 | lttng_ust_rcu_assign_pointer(elem->probes, (*entry)->probes); |
| 385 | CMM_STORE_SHARED(elem->state, active); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Disable a tracepoint and its probe callback. |
| 390 | * Note: only waiting an RCU period after setting elem->call to the empty |
| 391 | * function insures that the original callback is not used anymore. This insured |
| 392 | * by preempt_disable around the call site. |
| 393 | */ |
| 394 | static void disable_tracepoint(struct lttng_ust_tracepoint *elem) |
| 395 | { |
| 396 | CMM_STORE_SHARED(elem->state, 0); |
| 397 | lttng_ust_rcu_assign_pointer(elem->probes, NULL); |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Add the callsite to the callsite hash table. Must be called with |
| 402 | * tracepoint mutex held. |
| 403 | */ |
| 404 | static void add_callsite(struct tracepoint_lib * lib, struct lttng_ust_tracepoint *tp) |
| 405 | { |
| 406 | struct cds_hlist_head *head; |
| 407 | struct callsite_entry *e; |
| 408 | uint32_t hash; |
| 409 | struct tracepoint_entry *tp_entry; |
| 410 | |
| 411 | if (!lttng_ust_tp_validate_event_name(tp)) { |
| 412 | WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars", |
| 413 | tp->provider_name, tp->event_name, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1); |
| 414 | return; |
| 415 | } |
| 416 | hash = jhash(tp->provider_name, strlen(tp->provider_name), 0) ^ |
| 417 | jhash(tp->event_name, strlen(tp->event_name), 0); |
| 418 | head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)]; |
| 419 | e = zmalloc(sizeof(struct callsite_entry)); |
| 420 | if (!e) { |
| 421 | PERROR("Unable to add callsite for tracepoint \"%s:%s\"", tp->provider_name, tp->event_name); |
| 422 | return; |
| 423 | } |
| 424 | cds_hlist_add_head(&e->hlist, head); |
| 425 | e->tp = tp; |
| 426 | cds_list_add(&e->node, &lib->callsites); |
| 427 | |
| 428 | tp_entry = get_tracepoint(tp->provider_name, tp->event_name); |
| 429 | if (!tp_entry) |
| 430 | return; |
| 431 | tp_entry->callsite_refcount++; |
| 432 | e->tp_entry_callsite_ref = true; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Remove the callsite from the callsite hash table and from lib |
| 437 | * callsite list. Must be called with tracepoint mutex held. |
| 438 | */ |
| 439 | static void remove_callsite(struct callsite_entry *e) |
| 440 | { |
| 441 | struct tracepoint_entry *tp_entry; |
| 442 | |
| 443 | tp_entry = get_tracepoint(e->tp->provider_name, e->tp->event_name); |
| 444 | if (tp_entry) { |
| 445 | if (e->tp_entry_callsite_ref) |
| 446 | tp_entry->callsite_refcount--; |
| 447 | if (tp_entry->callsite_refcount == 0) |
| 448 | disable_tracepoint(e->tp); |
| 449 | } |
| 450 | cds_hlist_del(&e->hlist); |
| 451 | cds_list_del(&e->node); |
| 452 | free(e); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Enable/disable all callsites based on the state of a specific |
| 457 | * tracepoint entry. |
| 458 | * Must be called with tracepoint mutex held. |
| 459 | */ |
| 460 | static void tracepoint_sync_callsites(const char *provider_name, const char *event_name) |
| 461 | { |
| 462 | struct tracepoint_entry *tp_entry; |
| 463 | struct cds_hlist_head *head; |
| 464 | struct cds_hlist_node *node; |
| 465 | struct callsite_entry *e; |
| 466 | uint32_t hash; |
| 467 | |
| 468 | tp_entry = get_tracepoint(provider_name, event_name); |
| 469 | hash = jhash(provider_name, strlen(provider_name), 0) ^ |
| 470 | jhash(event_name, strlen(event_name), 0); |
| 471 | head = &callsite_table[hash & (CALLSITE_TABLE_SIZE - 1)]; |
| 472 | cds_hlist_for_each_entry(e, node, head, hlist) { |
| 473 | struct lttng_ust_tracepoint *tp = e->tp; |
| 474 | |
| 475 | if (strcmp(event_name, tp->event_name)) |
| 476 | continue; |
| 477 | if (strcmp(provider_name, tp->provider_name)) |
| 478 | continue; |
| 479 | if (tp_entry) { |
| 480 | if (!e->tp_entry_callsite_ref) { |
| 481 | tp_entry->callsite_refcount++; |
| 482 | e->tp_entry_callsite_ref = true; |
| 483 | } |
| 484 | set_tracepoint(&tp_entry, tp, |
| 485 | !!tp_entry->refcount); |
| 486 | } else { |
| 487 | disable_tracepoint(tp); |
| 488 | e->tp_entry_callsite_ref = false; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * tracepoint_update_probe_range - Update a probe range |
| 495 | * @begin: beginning of the range |
| 496 | * @end: end of the range |
| 497 | * |
| 498 | * Updates the probe callback corresponding to a range of tracepoints. |
| 499 | */ |
| 500 | static |
| 501 | void tracepoint_update_probe_range(struct lttng_ust_tracepoint * const *begin, |
| 502 | struct lttng_ust_tracepoint * const *end) |
| 503 | { |
| 504 | struct lttng_ust_tracepoint * const *iter; |
| 505 | struct tracepoint_entry *mark_entry; |
| 506 | |
| 507 | for (iter = begin; iter < end; iter++) { |
| 508 | if (!*iter) |
| 509 | continue; /* skip dummy */ |
| 510 | if (!(*iter)->provider_name || !(*iter)->event_name) { |
| 511 | disable_tracepoint(*iter); |
| 512 | continue; |
| 513 | } |
| 514 | if (!lttng_ust_tp_validate_event_name(*iter)) { |
| 515 | WARN("Rejecting tracepoint name \"%s:%s\" which exceeds size limits of %u chars", |
| 516 | (*iter)->provider_name, (*iter)->event_name, LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1); |
| 517 | disable_tracepoint(*iter); |
| 518 | continue; |
| 519 | } |
| 520 | mark_entry = get_tracepoint((*iter)->provider_name, (*iter)->event_name); |
| 521 | if (mark_entry) { |
| 522 | set_tracepoint(&mark_entry, *iter, |
| 523 | !!mark_entry->refcount); |
| 524 | } else { |
| 525 | disable_tracepoint(*iter); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static void lib_update_tracepoints(struct tracepoint_lib *lib) |
| 531 | { |
| 532 | tracepoint_update_probe_range(lib->tracepoints_start, |
| 533 | lib->tracepoints_start + lib->tracepoints_count); |
| 534 | } |
| 535 | |
| 536 | static void lib_register_callsites(struct tracepoint_lib *lib) |
| 537 | { |
| 538 | struct lttng_ust_tracepoint * const *begin; |
| 539 | struct lttng_ust_tracepoint * const *end; |
| 540 | struct lttng_ust_tracepoint * const *iter; |
| 541 | |
| 542 | begin = lib->tracepoints_start; |
| 543 | end = lib->tracepoints_start + lib->tracepoints_count; |
| 544 | |
| 545 | for (iter = begin; iter < end; iter++) { |
| 546 | if (!*iter) |
| 547 | continue; /* skip dummy */ |
| 548 | if (!(*iter)->provider_name || !(*iter)->event_name) { |
| 549 | continue; |
| 550 | } |
| 551 | add_callsite(lib, *iter); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static void lib_unregister_callsites(struct tracepoint_lib *lib) |
| 556 | { |
| 557 | struct callsite_entry *callsite, *tmp; |
| 558 | |
| 559 | cds_list_for_each_entry_safe(callsite, tmp, &lib->callsites, node) |
| 560 | remove_callsite(callsite); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Update probes, removing the faulty probes. |
| 565 | */ |
| 566 | static void tracepoint_update_probes(void) |
| 567 | { |
| 568 | struct tracepoint_lib *lib; |
| 569 | |
| 570 | /* tracepoints registered from libraries and executable. */ |
| 571 | cds_list_for_each_entry(lib, &libs, list) |
| 572 | lib_update_tracepoints(lib); |
| 573 | } |
| 574 | |
| 575 | static struct lttng_ust_tracepoint_probe * |
| 576 | tracepoint_add_probe(const char *provider_name, const char *event_name, |
| 577 | void (*probe)(void), void *data, const char *signature) |
| 578 | { |
| 579 | struct tracepoint_entry *entry; |
| 580 | struct lttng_ust_tracepoint_probe *old; |
| 581 | |
| 582 | entry = get_tracepoint(provider_name, event_name); |
| 583 | if (entry) { |
| 584 | if (strcmp(entry->signature, signature) != 0) { |
| 585 | ERR("Tracepoint \"%s:%s\": tracepoint and probe signature do not match: \"%s\" vs \"%s\".", |
| 586 | provider_name, event_name, entry->signature, signature); |
| 587 | return ERR_PTR(-EINVAL); |
| 588 | } |
| 589 | } else { |
| 590 | entry = add_tracepoint(provider_name, event_name, signature); |
| 591 | if (IS_ERR(entry)) |
| 592 | return (struct lttng_ust_tracepoint_probe *)entry; |
| 593 | } |
| 594 | old = tracepoint_entry_add_probe(entry, probe, data); |
| 595 | if (IS_ERR(old) && !entry->refcount) |
| 596 | remove_tracepoint(entry); |
| 597 | return old; |
| 598 | } |
| 599 | |
| 600 | static void tracepoint_release_queue_add_old_probes(void *old) |
| 601 | { |
| 602 | release_queue_need_update = 1; |
| 603 | if (old) { |
| 604 | struct tp_probes *tp_probes = caa_container_of(old, |
| 605 | struct tp_probes, probes[0]); |
| 606 | cds_list_add(&tp_probes->u.list, &release_queue); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * lttng_ust_tracepoint_probe_register - Connect a probe to a tracepoint |
| 612 | * @name: tracepoint provider name |
| 613 | * @name: tracepoint event name |
| 614 | * @probe: probe handler |
| 615 | * |
| 616 | * Returns 0 if ok, error value on error. |
| 617 | * The probe address must at least be aligned on the architecture pointer size. |
| 618 | * Called with the tracepoint mutex held. |
| 619 | */ |
| 620 | int lttng_ust_tracepoint_probe_register(const char *provider_name, const char *event_name, |
| 621 | void (*probe)(void), void *data, const char *signature) |
| 622 | { |
| 623 | void *old; |
| 624 | int ret = 0; |
| 625 | |
| 626 | DBG("Registering probe to tracepoint \"%s:%s\"", provider_name, event_name); |
| 627 | |
| 628 | pthread_mutex_lock(&tracepoint_mutex); |
| 629 | old = tracepoint_add_probe(provider_name, event_name, probe, data, signature); |
| 630 | if (IS_ERR(old)) { |
| 631 | ret = PTR_ERR(old); |
| 632 | goto end; |
| 633 | } |
| 634 | |
| 635 | tracepoint_sync_callsites(provider_name, event_name); |
| 636 | release_probes(old); |
| 637 | end: |
| 638 | pthread_mutex_unlock(&tracepoint_mutex); |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after |
| 644 | * calling lttng_ust_tp_probe_register_queue_release() one or multiple |
| 645 | * times to ensure it does not leak memory. |
| 646 | */ |
| 647 | int lttng_ust_tp_probe_register_queue_release(const char *provider_name, const char *event_name, |
| 648 | void (*probe)(void), void *data, const char *signature) |
| 649 | { |
| 650 | void *old; |
| 651 | int ret = 0; |
| 652 | |
| 653 | DBG("Registering probe to tracepoint \"%s:%s\". Queuing release.", provider_name, event_name); |
| 654 | |
| 655 | pthread_mutex_lock(&tracepoint_mutex); |
| 656 | old = tracepoint_add_probe(provider_name, event_name, probe, data, signature); |
| 657 | if (IS_ERR(old)) { |
| 658 | ret = PTR_ERR(old); |
| 659 | goto end; |
| 660 | } |
| 661 | |
| 662 | tracepoint_sync_callsites(provider_name, event_name); |
| 663 | tracepoint_release_queue_add_old_probes(old); |
| 664 | end: |
| 665 | pthread_mutex_unlock(&tracepoint_mutex); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | static void *tracepoint_remove_probe(const char *provider_name, const char *event_name, |
| 670 | void (*probe)(void), void *data) |
| 671 | { |
| 672 | struct tracepoint_entry *entry; |
| 673 | void *old; |
| 674 | |
| 675 | entry = get_tracepoint(provider_name, event_name); |
| 676 | if (!entry) |
| 677 | return ERR_PTR(-ENOENT); |
| 678 | old = tracepoint_entry_remove_probe(entry, probe, data); |
| 679 | if (IS_ERR(old)) |
| 680 | return old; |
| 681 | if (!entry->refcount) |
| 682 | remove_tracepoint(entry); |
| 683 | return old; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint |
| 688 | * @provider_name: tracepoint provider name |
| 689 | * @event_name: tracepoint event name |
| 690 | * @probe: probe function pointer |
| 691 | * @probe: probe data pointer |
| 692 | */ |
| 693 | int lttng_ust_tracepoint_probe_unregister(const char *provider_name, const char *event_name, |
| 694 | void (*probe)(void), void *data) |
| 695 | { |
| 696 | void *old; |
| 697 | int ret = 0; |
| 698 | |
| 699 | DBG("Un-registering probe \"%s:%s\" from tracepoint %p", provider_name, event_name, probe); |
| 700 | |
| 701 | pthread_mutex_lock(&tracepoint_mutex); |
| 702 | old = tracepoint_remove_probe(provider_name, event_name, probe, data); |
| 703 | if (IS_ERR(old)) { |
| 704 | ret = PTR_ERR(old); |
| 705 | goto end; |
| 706 | } |
| 707 | tracepoint_sync_callsites(provider_name, event_name); |
| 708 | release_probes(old); |
| 709 | end: |
| 710 | pthread_mutex_unlock(&tracepoint_mutex); |
| 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * Caller needs to invoke lttng_ust_tracepoint_probe_release_queue() after |
| 716 | * calling lttng_ust_tp_probe_unregister_queue_release() one or multiple |
| 717 | * times to ensure it does not leak memory. |
| 718 | */ |
| 719 | int lttng_ust_tp_probe_unregister_queue_release(const char *provider_name, const char *event_name, |
| 720 | void (*probe)(void), void *data) |
| 721 | { |
| 722 | void *old; |
| 723 | int ret = 0; |
| 724 | |
| 725 | DBG("Un-registering probe from tracepoint \"%s:%s\". Queuing release.", provider_name, event_name); |
| 726 | |
| 727 | pthread_mutex_lock(&tracepoint_mutex); |
| 728 | old = tracepoint_remove_probe(provider_name, event_name, probe, data); |
| 729 | if (IS_ERR(old)) { |
| 730 | ret = PTR_ERR(old); |
| 731 | goto end; |
| 732 | } |
| 733 | tracepoint_sync_callsites(provider_name, event_name); |
| 734 | tracepoint_release_queue_add_old_probes(old); |
| 735 | end: |
| 736 | pthread_mutex_unlock(&tracepoint_mutex); |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | void lttng_ust_tp_probe_prune_release_queue(void) |
| 741 | { |
| 742 | CDS_LIST_HEAD(release_probes); |
| 743 | struct tp_probes *pos, *next; |
| 744 | |
| 745 | DBG("Release queue of unregistered tracepoint probes."); |
| 746 | |
| 747 | pthread_mutex_lock(&tracepoint_mutex); |
| 748 | if (!release_queue_need_update) |
| 749 | goto end; |
| 750 | if (!cds_list_empty(&release_queue)) |
| 751 | cds_list_replace_init(&release_queue, &release_probes); |
| 752 | release_queue_need_update = 0; |
| 753 | |
| 754 | /* Wait for grace period between all sync_callsites and free. */ |
| 755 | lttng_ust_urcu_synchronize_rcu(); |
| 756 | |
| 757 | cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) { |
| 758 | cds_list_del(&pos->u.list); |
| 759 | free(pos); |
| 760 | } |
| 761 | end: |
| 762 | pthread_mutex_unlock(&tracepoint_mutex); |
| 763 | } |
| 764 | |
| 765 | static void tracepoint_add_old_probes(void *old) |
| 766 | { |
| 767 | need_update = 1; |
| 768 | if (old) { |
| 769 | struct tp_probes *tp_probes = caa_container_of(old, |
| 770 | struct tp_probes, probes[0]); |
| 771 | cds_list_add(&tp_probes->u.list, &old_probes); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * tracepoint_probe_register_noupdate - register a probe but not connect |
| 777 | * @name: tracepoint name |
| 778 | * @probe: probe handler |
| 779 | * |
| 780 | * caller must call tracepoint_probe_update_all() |
| 781 | */ |
| 782 | int tracepoint_probe_register_noupdate(const char *provider_name, const char *event_name, |
| 783 | void (*probe)(void), void *data, const char *signature) |
| 784 | { |
| 785 | void *old; |
| 786 | int ret = 0; |
| 787 | |
| 788 | pthread_mutex_lock(&tracepoint_mutex); |
| 789 | old = tracepoint_add_probe(provider_name, event_name, probe, data, signature); |
| 790 | if (IS_ERR(old)) { |
| 791 | ret = PTR_ERR(old); |
| 792 | goto end; |
| 793 | } |
| 794 | tracepoint_add_old_probes(old); |
| 795 | end: |
| 796 | pthread_mutex_unlock(&tracepoint_mutex); |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect |
| 802 | * @name: tracepoint name |
| 803 | * @probe: probe function pointer |
| 804 | * |
| 805 | * caller must call tracepoint_probe_update_all() |
| 806 | * Called with the tracepoint mutex held. |
| 807 | */ |
| 808 | int tracepoint_probe_unregister_noupdate(const char *provider_name, const char *event_name, |
| 809 | void (*probe)(void), void *data) |
| 810 | { |
| 811 | void *old; |
| 812 | int ret = 0; |
| 813 | |
| 814 | DBG("Un-registering probe from tracepoint \"%s:%s\"", provider_name, event_name); |
| 815 | |
| 816 | pthread_mutex_lock(&tracepoint_mutex); |
| 817 | old = tracepoint_remove_probe(provider_name, event_name, probe, data); |
| 818 | if (IS_ERR(old)) { |
| 819 | ret = PTR_ERR(old); |
| 820 | goto end; |
| 821 | } |
| 822 | tracepoint_add_old_probes(old); |
| 823 | end: |
| 824 | pthread_mutex_unlock(&tracepoint_mutex); |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * tracepoint_probe_update_all - update tracepoints |
| 830 | */ |
| 831 | void tracepoint_probe_update_all(void) |
| 832 | { |
| 833 | CDS_LIST_HEAD(release_probes); |
| 834 | struct tp_probes *pos, *next; |
| 835 | |
| 836 | pthread_mutex_lock(&tracepoint_mutex); |
| 837 | if (!need_update) { |
| 838 | goto end; |
| 839 | } |
| 840 | if (!cds_list_empty(&old_probes)) |
| 841 | cds_list_replace_init(&old_probes, &release_probes); |
| 842 | need_update = 0; |
| 843 | |
| 844 | tracepoint_update_probes(); |
| 845 | /* Wait for grace period between update_probes and free. */ |
| 846 | lttng_ust_urcu_synchronize_rcu(); |
| 847 | cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) { |
| 848 | cds_list_del(&pos->u.list); |
| 849 | free(pos); |
| 850 | } |
| 851 | end: |
| 852 | pthread_mutex_unlock(&tracepoint_mutex); |
| 853 | } |
| 854 | |
| 855 | static void new_tracepoints(struct lttng_ust_tracepoint * const *start, |
| 856 | struct lttng_ust_tracepoint * const *end) |
| 857 | { |
| 858 | if (new_tracepoint_cb) { |
| 859 | struct lttng_ust_tracepoint * const *t; |
| 860 | |
| 861 | for (t = start; t < end; t++) { |
| 862 | if (*t) |
| 863 | new_tracepoint_cb(*t); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * tracepoint_{un,}register_lib is meant to be looked up by instrumented |
| 870 | * applications through dlsym(). If found, those can register their |
| 871 | * tracepoints, else those tracepoints will not be available for |
| 872 | * tracing. The number at the end of those symbols acts as a major |
| 873 | * version for tracepoints. |
| 874 | * |
| 875 | * Older instrumented applications should still work with newer |
| 876 | * liblttng-ust, but it is fine that instrumented applications compiled |
| 877 | * against recent liblttng-ust headers require a recent liblttng-ust |
| 878 | * runtime for those tracepoints to be taken into account. |
| 879 | */ |
| 880 | int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start, |
| 881 | int tracepoints_count); |
| 882 | int tracepoint_register_lib(struct lttng_ust_tracepoint * const *tracepoints_start, |
| 883 | int tracepoints_count) |
| 884 | { |
| 885 | struct tracepoint_lib *pl, *iter; |
| 886 | |
| 887 | lttng_ust_tp_init(); |
| 888 | |
| 889 | pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib)); |
| 890 | if (!pl) { |
| 891 | PERROR("Unable to register tracepoint lib"); |
| 892 | return -1; |
| 893 | } |
| 894 | pl->tracepoints_start = tracepoints_start; |
| 895 | pl->tracepoints_count = tracepoints_count; |
| 896 | CDS_INIT_LIST_HEAD(&pl->callsites); |
| 897 | |
| 898 | pthread_mutex_lock(&tracepoint_mutex); |
| 899 | /* |
| 900 | * We sort the libs by struct lib pointer address. |
| 901 | */ |
| 902 | cds_list_for_each_entry_reverse(iter, &libs, list) { |
| 903 | BUG_ON(iter == pl); /* Should never be in the list twice */ |
| 904 | if (iter < pl) { |
| 905 | /* We belong to the location right after iter. */ |
| 906 | cds_list_add(&pl->list, &iter->list); |
| 907 | goto lib_added; |
| 908 | } |
| 909 | } |
| 910 | /* We should be added at the head of the list */ |
| 911 | cds_list_add(&pl->list, &libs); |
| 912 | lib_added: |
| 913 | new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count); |
| 914 | lib_register_callsites(pl); |
| 915 | lib_update_tracepoints(pl); |
| 916 | pthread_mutex_unlock(&tracepoint_mutex); |
| 917 | |
| 918 | DBG("just registered a tracepoints section from %p and having %d tracepoints", |
| 919 | tracepoints_start, tracepoints_count); |
| 920 | if (lttng_ust_logging_debug_enabled()) { |
| 921 | int i; |
| 922 | |
| 923 | for (i = 0; i < tracepoints_count; i++) { |
| 924 | if (!lttng_ust_tp_validate_event_name(tracepoints_start[i])) |
| 925 | continue; |
| 926 | DBG("registered tracepoint: \"%s:%s\"", |
| 927 | tracepoints_start[i]->provider_name, |
| 928 | tracepoints_start[i]->event_name); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start); |
| 936 | int tracepoint_unregister_lib(struct lttng_ust_tracepoint * const *tracepoints_start) |
| 937 | { |
| 938 | struct tracepoint_lib *lib; |
| 939 | |
| 940 | pthread_mutex_lock(&tracepoint_mutex); |
| 941 | cds_list_for_each_entry(lib, &libs, list) { |
| 942 | if (lib->tracepoints_start != tracepoints_start) |
| 943 | continue; |
| 944 | |
| 945 | cds_list_del(&lib->list); |
| 946 | /* |
| 947 | * Unregistering a callsite also decreases the |
| 948 | * callsite reference count of the corresponding |
| 949 | * tracepoint, and disables the tracepoint if |
| 950 | * the reference count drops to zero. |
| 951 | */ |
| 952 | lib_unregister_callsites(lib); |
| 953 | DBG("just unregistered a tracepoints section from %p", |
| 954 | lib->tracepoints_start); |
| 955 | free(lib); |
| 956 | break; |
| 957 | } |
| 958 | pthread_mutex_unlock(&tracepoint_mutex); |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * Report in debug message whether the compiler correctly supports weak |
| 964 | * hidden symbols. This test checks that the address associated with two |
| 965 | * weak symbols with hidden visibility is the same when declared within |
| 966 | * two compile units part of the same module. |
| 967 | */ |
| 968 | static void check_weak_hidden(void) |
| 969 | { |
| 970 | DBG("Your compiler treats weak symbols with hidden visibility for integer objects as %s between compile units part of the same module.", |
| 971 | <tng_ust_tracepoint_test_symbol1 == lttng_ust_tp_check_weak_hidden1() ? |
| 972 | "SAME address" : |
| 973 | "DIFFERENT addresses"); |
| 974 | DBG("Your compiler treats weak symbols with hidden visibility for pointer objects as %s between compile units part of the same module.", |
| 975 | <tng_ust_tracepoint_test_symbol2 == lttng_ust_tp_check_weak_hidden2() ? |
| 976 | "SAME address" : |
| 977 | "DIFFERENT addresses"); |
| 978 | DBG("Your compiler treats weak symbols with hidden visibility for 24-byte structure objects as %s between compile units part of the same module.", |
| 979 | <tng_ust_tracepoint_test_symbol3 == lttng_ust_tp_check_weak_hidden3() ? |
| 980 | "SAME address" : |
| 981 | "DIFFERENT addresses"); |
| 982 | } |
| 983 | |
| 984 | void lttng_ust_tp_init(void) |
| 985 | { |
| 986 | if (uatomic_xchg(&initialized, 1) == 1) |
| 987 | return; |
| 988 | lttng_ust_logging_init(); |
| 989 | lttng_ust_common_ctor(); |
| 990 | check_weak_hidden(); |
| 991 | } |
| 992 | |
| 993 | void lttng_ust_tp_exit(void) |
| 994 | { |
| 995 | initialized = 0; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Create the wrapper symbols. |
| 1000 | */ |
| 1001 | #undef tp_rcu_read_lock |
| 1002 | #undef tp_rcu_read_unlock |
| 1003 | #undef tp_rcu_dereference |
| 1004 | |
| 1005 | void tp_rcu_read_lock(void); |
| 1006 | void tp_rcu_read_lock(void) |
| 1007 | { |
| 1008 | lttng_ust_urcu_read_lock(); |
| 1009 | } |
| 1010 | |
| 1011 | void tp_rcu_read_unlock(void); |
| 1012 | void tp_rcu_read_unlock(void) |
| 1013 | { |
| 1014 | lttng_ust_urcu_read_unlock(); |
| 1015 | } |
| 1016 | |
| 1017 | void *tp_rcu_dereference_sym(void *p); |
| 1018 | void *tp_rcu_dereference_sym(void *p) |
| 1019 | { |
| 1020 | return lttng_ust_rcu_dereference(p); |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * Programs that have threads that survive after they exit, and therefore call |
| 1025 | * library destructors, should disable the tracepoint destructors by calling |
| 1026 | * tp_disable_destructors(). This will leak the tracepoint |
| 1027 | * instrumentation library shared object, leaving its teardown to the operating |
| 1028 | * system process teardown. |
| 1029 | * |
| 1030 | * To access and/or modify this value, users need to use a combination of |
| 1031 | * dlopen(3) and dlsym(3) to get an handle on the |
| 1032 | * tp_disable_destructors and tp_get_destructors_state symbols below. |
| 1033 | */ |
| 1034 | void tp_disable_destructors(void); |
| 1035 | void tp_disable_destructors(void) |
| 1036 | { |
| 1037 | uatomic_set(&tracepoint_destructors_state, 0); |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Returns 1 if the destructors are enabled and should be executed. |
| 1042 | * Returns 0 if the destructors are disabled. |
| 1043 | */ |
| 1044 | int tp_get_destructors_state(void); |
| 1045 | int tp_get_destructors_state(void) |
| 1046 | { |
| 1047 | return uatomic_read(&tracepoint_destructors_state); |
| 1048 | } |