| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright 2010-2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Holds LTTng probes registry. |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <string.h> |
| 11 | #include <errno.h> |
| 12 | #include <urcu/list.h> |
| 13 | #include <urcu/hlist.h> |
| 14 | #include <lttng/ust-events.h> |
| 15 | #include <lttng/tracepoint.h> |
| 16 | #include "common/tracepoint.h" |
| 17 | #include <assert.h> |
| 18 | #include "common/macros.h" |
| 19 | #include <ctype.h> |
| 20 | |
| 21 | #include "lttng-tracer-core.h" |
| 22 | #include "common/jhash.h" |
| 23 | #include "lib/lttng-ust/events.h" |
| 24 | |
| 25 | /* |
| 26 | * probe list is protected by ust_lock()/ust_unlock(). |
| 27 | */ |
| 28 | static CDS_LIST_HEAD(_probe_list); |
| 29 | |
| 30 | /* |
| 31 | * List of probes registered by not yet processed. |
| 32 | */ |
| 33 | static CDS_LIST_HEAD(lazy_probe_init); |
| 34 | |
| 35 | /* |
| 36 | * lazy_nesting counter ensures we don't trigger lazy probe registration |
| 37 | * fixup while we are performing the fixup. It is protected by the ust |
| 38 | * mutex. |
| 39 | */ |
| 40 | static int lazy_nesting; |
| 41 | |
| 42 | static |
| 43 | int check_provider_version(const struct lttng_ust_probe_desc *desc) |
| 44 | { |
| 45 | /* |
| 46 | * Check tracepoint provider version compatibility. |
| 47 | */ |
| 48 | if (desc->major <= LTTNG_UST_PROVIDER_MAJOR && |
| 49 | desc->major >= LTTNG_UST_PROVIDER_MAJOR_OLDEST_COMPATIBLE) { |
| 50 | DBG("Provider \"%s\" accepted, version %u.%u is compatible " |
| 51 | "with LTTng UST provider version %u.%u.", |
| 52 | desc->provider_name, desc->major, desc->minor, |
| 53 | LTTNG_UST_PROVIDER_MAJOR, |
| 54 | LTTNG_UST_PROVIDER_MINOR); |
| 55 | if (desc->major < LTTNG_UST_PROVIDER_MAJOR) { |
| 56 | DBG("However, some LTTng UST features might not be " |
| 57 | "available for this provider unless it is " |
| 58 | "recompiled against a more recent LTTng UST."); |
| 59 | } |
| 60 | return 1; /* accept */ |
| 61 | } else { |
| 62 | ERR("Provider \"%s\" rejected, version %u.%u is incompatible " |
| 63 | "with LTTng UST provider version %u.%u. Please upgrade " |
| 64 | "LTTng UST.", |
| 65 | desc->provider_name, desc->major, desc->minor, |
| 66 | LTTNG_UST_PROVIDER_MAJOR, |
| 67 | LTTNG_UST_PROVIDER_MINOR); |
| 68 | return 0; /* reject */ |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | bool check_type_provider(const struct lttng_ust_type_common *type); |
| 74 | |
| 75 | static |
| 76 | bool check_type_provider(const struct lttng_ust_type_common *type) |
| 77 | { |
| 78 | switch (type->type) { |
| 79 | case lttng_ust_type_integer: |
| 80 | return true; |
| 81 | case lttng_ust_type_string: |
| 82 | return true; |
| 83 | case lttng_ust_type_float: |
| 84 | return true; |
| 85 | case lttng_ust_type_dynamic: |
| 86 | return true; |
| 87 | case lttng_ust_type_enum: |
| 88 | { |
| 89 | const struct lttng_ust_type_enum *enum_type = caa_container_of(type, const struct lttng_ust_type_enum, parent); |
| 90 | |
| 91 | return check_provider_version(enum_type->desc->probe_desc); |
| 92 | } |
| 93 | case lttng_ust_type_array: |
| 94 | { |
| 95 | const struct lttng_ust_type_array *array_type = caa_container_of(type, const struct lttng_ust_type_array, parent); |
| 96 | |
| 97 | return check_type_provider(array_type->elem_type); |
| 98 | } |
| 99 | case lttng_ust_type_sequence: |
| 100 | { |
| 101 | const struct lttng_ust_type_sequence *sequence_type = caa_container_of(type, const struct lttng_ust_type_sequence, parent); |
| 102 | |
| 103 | return check_type_provider(sequence_type->elem_type); |
| 104 | } |
| 105 | case lttng_ust_type_struct: |
| 106 | { |
| 107 | const struct lttng_ust_type_struct *struct_type = caa_container_of(type, const struct lttng_ust_type_struct, parent); |
| 108 | size_t i; |
| 109 | |
| 110 | for (i = 0; i < struct_type->nr_fields; i++) { |
| 111 | if (!check_type_provider(struct_type->fields[i]->type)) |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | default: |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Validate that each event within the probe provider refers to the |
| 123 | * right probe, that the resulting name is not too long, and that the |
| 124 | * event class belongs to a provider with compatible version. |
| 125 | */ |
| 126 | static |
| 127 | bool check_event_provider(const struct lttng_ust_probe_desc *probe_desc) |
| 128 | { |
| 129 | int i, j; |
| 130 | |
| 131 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 132 | const struct lttng_ust_event_desc *event_desc = probe_desc->event_desc[i]; |
| 133 | const struct lttng_ust_tracepoint_class *tp_class = event_desc->tp_class; |
| 134 | |
| 135 | if (event_desc->probe_desc != probe_desc) { |
| 136 | ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.", |
| 137 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); |
| 138 | return false; /* provider mismatch */ |
| 139 | } |
| 140 | if (!lttng_ust_validate_event_name(event_desc)) { |
| 141 | ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.", |
| 142 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); |
| 143 | return false; /* provider mismatch */ |
| 144 | } |
| 145 | if (!check_provider_version(tp_class->probe_desc)) { |
| 146 | ERR("Error registering probe provider '%s'. Event '%s:%s' refers to an event class in a provider with incompatible version.", |
| 147 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); |
| 148 | return false; |
| 149 | } |
| 150 | for (j = 0; j < tp_class->nr_fields; j++) { |
| 151 | const struct lttng_ust_event_field *field = tp_class->fields[j]; |
| 152 | |
| 153 | if (!check_type_provider(field->type)) { |
| 154 | ERR("Error registering probe provider '%s'. Event '%s:%s' contains a field which refers to an provider with incompatible version.", |
| 155 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Called under ust lock. |
| 165 | */ |
| 166 | static |
| 167 | void lttng_lazy_probe_register(struct lttng_ust_registered_probe *reg_probe) |
| 168 | { |
| 169 | struct lttng_ust_registered_probe *iter; |
| 170 | struct cds_list_head *probe_list; |
| 171 | |
| 172 | /* |
| 173 | * The provider ensures there are no duplicate event names. |
| 174 | * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a |
| 175 | * compile-time error due to duplicated symbol names. |
| 176 | */ |
| 177 | |
| 178 | /* |
| 179 | * We sort the providers by struct lttng_ust_probe_desc pointer |
| 180 | * address. |
| 181 | */ |
| 182 | probe_list = &_probe_list; |
| 183 | cds_list_for_each_entry_reverse(iter, probe_list, head) { |
| 184 | BUG_ON(iter == reg_probe); /* Should never be in the list twice */ |
| 185 | if (iter < reg_probe) { |
| 186 | /* We belong to the location right after iter. */ |
| 187 | cds_list_add(®_probe->head, &iter->head); |
| 188 | goto probe_added; |
| 189 | } |
| 190 | } |
| 191 | /* We should be added at the head of the list */ |
| 192 | cds_list_add(®_probe->head, probe_list); |
| 193 | probe_added: |
| 194 | DBG("just registered probe %s containing %u events", |
| 195 | reg_probe->desc->provider_name, reg_probe->desc->nr_events); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Called under ust lock. |
| 200 | */ |
| 201 | static |
| 202 | void fixup_lazy_probes(void) |
| 203 | { |
| 204 | struct lttng_ust_registered_probe *iter, *tmp; |
| 205 | int ret; |
| 206 | |
| 207 | lazy_nesting++; |
| 208 | cds_list_for_each_entry_safe(iter, tmp, |
| 209 | &lazy_probe_init, lazy_init_head) { |
| 210 | lttng_lazy_probe_register(iter); |
| 211 | iter->lazy = 0; |
| 212 | cds_list_del(&iter->lazy_init_head); |
| 213 | } |
| 214 | ret = lttng_fix_pending_events(); |
| 215 | assert(!ret); |
| 216 | lazy_nesting--; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Called under ust lock. |
| 221 | */ |
| 222 | struct cds_list_head *lttng_get_probe_list_head(void) |
| 223 | { |
| 224 | if (!lazy_nesting && !cds_list_empty(&lazy_probe_init)) |
| 225 | fixup_lazy_probes(); |
| 226 | return &_probe_list; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | struct lttng_ust_registered_probe *lttng_ust_probe_register(const struct lttng_ust_probe_desc *desc) |
| 231 | { |
| 232 | struct lttng_ust_registered_probe *reg_probe = NULL; |
| 233 | |
| 234 | lttng_ust_alloc_tls(); |
| 235 | |
| 236 | /* |
| 237 | * If version mismatch, don't register, but don't trigger assert |
| 238 | * on caller. The version check just prints an error. |
| 239 | */ |
| 240 | if (!check_provider_version(desc)) |
| 241 | return NULL; |
| 242 | if (!check_event_provider(desc)) |
| 243 | return NULL; |
| 244 | |
| 245 | ust_lock_nocheck(); |
| 246 | |
| 247 | reg_probe = zmalloc(sizeof(struct lttng_ust_registered_probe)); |
| 248 | if (!reg_probe) |
| 249 | goto end; |
| 250 | reg_probe->desc = desc; |
| 251 | cds_list_add(®_probe->lazy_init_head, &lazy_probe_init); |
| 252 | reg_probe->lazy = 1; |
| 253 | |
| 254 | DBG("adding probe %s containing %u events to lazy registration list", |
| 255 | desc->provider_name, desc->nr_events); |
| 256 | /* |
| 257 | * If there is at least one active session, we need to register |
| 258 | * the probe immediately, since we cannot delay event |
| 259 | * registration because they are needed ASAP. |
| 260 | */ |
| 261 | if (lttng_session_active()) |
| 262 | fixup_lazy_probes(); |
| 263 | |
| 264 | lttng_fix_pending_event_notifiers(); |
| 265 | end: |
| 266 | ust_unlock(); |
| 267 | return reg_probe; |
| 268 | } |
| 269 | |
| 270 | void lttng_ust_probe_unregister(struct lttng_ust_registered_probe *reg_probe) |
| 271 | { |
| 272 | lttng_ust_alloc_tls(); |
| 273 | |
| 274 | if (!reg_probe) |
| 275 | return; |
| 276 | if (!check_provider_version(reg_probe->desc)) |
| 277 | return; |
| 278 | |
| 279 | ust_lock_nocheck(); |
| 280 | if (!reg_probe->lazy) |
| 281 | cds_list_del(®_probe->head); |
| 282 | else |
| 283 | cds_list_del(®_probe->lazy_init_head); |
| 284 | |
| 285 | lttng_probe_provider_unregister_events(reg_probe->desc); |
| 286 | DBG("just unregistered probes of provider %s", reg_probe->desc->provider_name); |
| 287 | ust_unlock(); |
| 288 | free(reg_probe); |
| 289 | } |
| 290 | |
| 291 | void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) |
| 292 | { |
| 293 | struct tp_list_entry *list_entry, *tmp; |
| 294 | |
| 295 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { |
| 296 | cds_list_del(&list_entry->head); |
| 297 | free(list_entry); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * called with UST lock held. |
| 303 | */ |
| 304 | int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list) |
| 305 | { |
| 306 | struct lttng_ust_registered_probe *reg_probe; |
| 307 | struct cds_list_head *probe_list; |
| 308 | int i; |
| 309 | |
| 310 | probe_list = lttng_get_probe_list_head(); |
| 311 | CDS_INIT_LIST_HEAD(&list->head); |
| 312 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
| 313 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; |
| 314 | |
| 315 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 316 | const struct lttng_ust_event_desc *event_desc = |
| 317 | probe_desc->event_desc[i]; |
| 318 | struct tp_list_entry *list_entry; |
| 319 | |
| 320 | /* Skip event if name is too long. */ |
| 321 | if (!lttng_ust_validate_event_name(event_desc)) |
| 322 | continue; |
| 323 | list_entry = zmalloc(sizeof(*list_entry)); |
| 324 | if (!list_entry) |
| 325 | goto err_nomem; |
| 326 | cds_list_add(&list_entry->head, &list->head); |
| 327 | lttng_ust_format_event_name(event_desc, list_entry->tp.name); |
| 328 | if (!event_desc->loglevel) { |
| 329 | list_entry->tp.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT; |
| 330 | } else { |
| 331 | list_entry->tp.loglevel = *(*event_desc->loglevel); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | if (cds_list_empty(&list->head)) |
| 336 | list->iter = NULL; |
| 337 | else |
| 338 | list->iter = |
| 339 | cds_list_first_entry(&list->head, struct tp_list_entry, head); |
| 340 | return 0; |
| 341 | |
| 342 | err_nomem: |
| 343 | lttng_probes_prune_event_list(list); |
| 344 | return -ENOMEM; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Return current iteration position, advance internal iterator to next. |
| 349 | * Return NULL if end of list. |
| 350 | */ |
| 351 | struct lttng_ust_abi_tracepoint_iter * |
| 352 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) |
| 353 | { |
| 354 | struct tp_list_entry *entry; |
| 355 | |
| 356 | if (!list->iter) |
| 357 | return NULL; |
| 358 | entry = list->iter; |
| 359 | if (entry->head.next == &list->head) |
| 360 | list->iter = NULL; |
| 361 | else |
| 362 | list->iter = cds_list_entry(entry->head.next, |
| 363 | struct tp_list_entry, head); |
| 364 | return &entry->tp; |
| 365 | } |
| 366 | |
| 367 | void lttng_probes_prune_field_list(struct lttng_ust_field_list *list) |
| 368 | { |
| 369 | struct tp_field_list_entry *list_entry, *tmp; |
| 370 | |
| 371 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { |
| 372 | cds_list_del(&list_entry->head); |
| 373 | free(list_entry); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * called with UST lock held. |
| 379 | */ |
| 380 | int lttng_probes_get_field_list(struct lttng_ust_field_list *list) |
| 381 | { |
| 382 | struct lttng_ust_registered_probe *reg_probe; |
| 383 | struct cds_list_head *probe_list; |
| 384 | int i; |
| 385 | |
| 386 | probe_list = lttng_get_probe_list_head(); |
| 387 | CDS_INIT_LIST_HEAD(&list->head); |
| 388 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
| 389 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; |
| 390 | |
| 391 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 392 | const struct lttng_ust_event_desc *event_desc = |
| 393 | probe_desc->event_desc[i]; |
| 394 | int j; |
| 395 | |
| 396 | if (event_desc->tp_class->nr_fields == 0) { |
| 397 | /* Events without fields. */ |
| 398 | struct tp_field_list_entry *list_entry; |
| 399 | |
| 400 | /* Skip event if name is too long. */ |
| 401 | if (!lttng_ust_validate_event_name(event_desc)) |
| 402 | continue; |
| 403 | list_entry = zmalloc(sizeof(*list_entry)); |
| 404 | if (!list_entry) |
| 405 | goto err_nomem; |
| 406 | cds_list_add(&list_entry->head, &list->head); |
| 407 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
| 408 | list_entry->field.field_name[0] = '\0'; |
| 409 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
| 410 | if (!event_desc->loglevel) { |
| 411 | list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT; |
| 412 | } else { |
| 413 | list_entry->field.loglevel = *(*event_desc->loglevel); |
| 414 | } |
| 415 | list_entry->field.nowrite = 1; |
| 416 | } |
| 417 | |
| 418 | for (j = 0; j < event_desc->tp_class->nr_fields; j++) { |
| 419 | const struct lttng_ust_event_field *event_field = |
| 420 | event_desc->tp_class->fields[j]; |
| 421 | struct tp_field_list_entry *list_entry; |
| 422 | |
| 423 | /* Skip event if name is too long. */ |
| 424 | if (!lttng_ust_validate_event_name(event_desc)) |
| 425 | continue; |
| 426 | list_entry = zmalloc(sizeof(*list_entry)); |
| 427 | if (!list_entry) |
| 428 | goto err_nomem; |
| 429 | cds_list_add(&list_entry->head, &list->head); |
| 430 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
| 431 | strncpy(list_entry->field.field_name, |
| 432 | event_field->name, |
| 433 | LTTNG_UST_ABI_SYM_NAME_LEN); |
| 434 | list_entry->field.field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 435 | switch (event_field->type->type) { |
| 436 | case lttng_ust_type_integer: |
| 437 | list_entry->field.type = LTTNG_UST_ABI_FIELD_INTEGER; |
| 438 | break; |
| 439 | case lttng_ust_type_string: |
| 440 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
| 441 | break; |
| 442 | case lttng_ust_type_array: |
| 443 | if (lttng_ust_get_type_array(event_field->type)->encoding == lttng_ust_string_encoding_none) |
| 444 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
| 445 | else |
| 446 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
| 447 | break; |
| 448 | case lttng_ust_type_sequence: |
| 449 | if (lttng_ust_get_type_sequence(event_field->type)->encoding == lttng_ust_string_encoding_none) |
| 450 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
| 451 | else |
| 452 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
| 453 | break; |
| 454 | case lttng_ust_type_float: |
| 455 | list_entry->field.type = LTTNG_UST_ABI_FIELD_FLOAT; |
| 456 | break; |
| 457 | case lttng_ust_type_enum: |
| 458 | list_entry->field.type = LTTNG_UST_ABI_FIELD_ENUM; |
| 459 | break; |
| 460 | default: |
| 461 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
| 462 | } |
| 463 | if (!event_desc->loglevel) { |
| 464 | list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT; |
| 465 | } else { |
| 466 | list_entry->field.loglevel = *(*event_desc->loglevel); |
| 467 | } |
| 468 | list_entry->field.nowrite = event_field->nowrite; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | if (cds_list_empty(&list->head)) |
| 473 | list->iter = NULL; |
| 474 | else |
| 475 | list->iter = |
| 476 | cds_list_first_entry(&list->head, |
| 477 | struct tp_field_list_entry, head); |
| 478 | return 0; |
| 479 | |
| 480 | err_nomem: |
| 481 | lttng_probes_prune_field_list(list); |
| 482 | return -ENOMEM; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Return current iteration position, advance internal iterator to next. |
| 487 | * Return NULL if end of list. |
| 488 | */ |
| 489 | struct lttng_ust_abi_field_iter * |
| 490 | lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list) |
| 491 | { |
| 492 | struct tp_field_list_entry *entry; |
| 493 | |
| 494 | if (!list->iter) |
| 495 | return NULL; |
| 496 | entry = list->iter; |
| 497 | if (entry->head.next == &list->head) |
| 498 | list->iter = NULL; |
| 499 | else |
| 500 | list->iter = cds_list_entry(entry->head.next, |
| 501 | struct tp_field_list_entry, head); |
| 502 | return &entry->field; |
| 503 | } |