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