| 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 "tracepoint-internal.h" |
| 17 | #include <assert.h> |
| 18 | #include <ust-helper.h> |
| 19 | #include <ctype.h> |
| 20 | |
| 21 | #include "lttng-tracer-core.h" |
| 22 | #include "jhash.h" |
| 23 | #include "error.h" |
| 24 | #include "ust-events-internal.h" |
| 25 | |
| 26 | /* |
| 27 | * probe list is protected by ust_lock()/ust_unlock(). |
| 28 | */ |
| 29 | static CDS_LIST_HEAD(_probe_list); |
| 30 | |
| 31 | /* |
| 32 | * List of probes registered by not yet processed. |
| 33 | */ |
| 34 | static CDS_LIST_HEAD(lazy_probe_init); |
| 35 | |
| 36 | /* |
| 37 | * lazy_nesting counter ensures we don't trigger lazy probe registration |
| 38 | * fixup while we are performing the fixup. It is protected by the ust |
| 39 | * mutex. |
| 40 | */ |
| 41 | static int lazy_nesting; |
| 42 | |
| 43 | /* |
| 44 | * Called under ust lock. |
| 45 | */ |
| 46 | static |
| 47 | int check_event_provider(struct lttng_probe_desc *desc) |
| 48 | { |
| 49 | int i; |
| 50 | size_t provider_name_len; |
| 51 | |
| 52 | provider_name_len = strnlen(desc->provider, |
| 53 | LTTNG_UST_SYM_NAME_LEN - 1); |
| 54 | for (i = 0; i < desc->nr_events; i++) { |
| 55 | if (strncmp(desc->event_desc[i]->name, |
| 56 | desc->provider, |
| 57 | provider_name_len)) |
| 58 | return 0; /* provider mismatch */ |
| 59 | } |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Called under ust lock. |
| 65 | */ |
| 66 | static |
| 67 | void lttng_lazy_probe_register(struct lttng_probe_desc *desc) |
| 68 | { |
| 69 | struct lttng_probe_desc *iter; |
| 70 | struct cds_list_head *probe_list; |
| 71 | |
| 72 | /* |
| 73 | * Each provider enforce that every event name begins with the |
| 74 | * provider name. Check this in an assertion for extra |
| 75 | * carefulness. This ensures we cannot have duplicate event |
| 76 | * names across providers. |
| 77 | */ |
| 78 | assert(check_event_provider(desc)); |
| 79 | |
| 80 | /* |
| 81 | * The provider ensures there are no duplicate event names. |
| 82 | * Duplicated TRACEPOINT_EVENT event names would generate a |
| 83 | * compile-time error due to duplicated symbol names. |
| 84 | */ |
| 85 | |
| 86 | /* |
| 87 | * We sort the providers by struct lttng_probe_desc pointer |
| 88 | * address. |
| 89 | */ |
| 90 | probe_list = &_probe_list; |
| 91 | cds_list_for_each_entry_reverse(iter, probe_list, head) { |
| 92 | BUG_ON(iter == desc); /* Should never be in the list twice */ |
| 93 | if (iter < desc) { |
| 94 | /* We belong to the location right after iter. */ |
| 95 | cds_list_add(&desc->head, &iter->head); |
| 96 | goto desc_added; |
| 97 | } |
| 98 | } |
| 99 | /* We should be added at the head of the list */ |
| 100 | cds_list_add(&desc->head, probe_list); |
| 101 | desc_added: |
| 102 | DBG("just registered probe %s containing %u events", |
| 103 | desc->provider, desc->nr_events); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Called under ust lock. |
| 108 | */ |
| 109 | static |
| 110 | void fixup_lazy_probes(void) |
| 111 | { |
| 112 | struct lttng_probe_desc *iter, *tmp; |
| 113 | int ret; |
| 114 | |
| 115 | lazy_nesting++; |
| 116 | cds_list_for_each_entry_safe(iter, tmp, |
| 117 | &lazy_probe_init, lazy_init_head) { |
| 118 | lttng_lazy_probe_register(iter); |
| 119 | iter->lazy = 0; |
| 120 | cds_list_del(&iter->lazy_init_head); |
| 121 | } |
| 122 | ret = lttng_fix_pending_events(); |
| 123 | assert(!ret); |
| 124 | lazy_nesting--; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Called under ust lock. |
| 129 | */ |
| 130 | struct cds_list_head *lttng_get_probe_list_head(void) |
| 131 | { |
| 132 | if (!lazy_nesting && !cds_list_empty(&lazy_probe_init)) |
| 133 | fixup_lazy_probes(); |
| 134 | return &_probe_list; |
| 135 | } |
| 136 | |
| 137 | static |
| 138 | int check_provider_version(struct lttng_probe_desc *desc) |
| 139 | { |
| 140 | /* |
| 141 | * Check tracepoint provider version compatibility. |
| 142 | */ |
| 143 | if (desc->major <= LTTNG_UST_PROVIDER_MAJOR) { |
| 144 | DBG("Provider \"%s\" accepted, version %u.%u is compatible " |
| 145 | "with LTTng UST provider version %u.%u.", |
| 146 | desc->provider, desc->major, desc->minor, |
| 147 | LTTNG_UST_PROVIDER_MAJOR, |
| 148 | LTTNG_UST_PROVIDER_MINOR); |
| 149 | if (desc->major < LTTNG_UST_PROVIDER_MAJOR) { |
| 150 | DBG("However, some LTTng UST features might not be " |
| 151 | "available for this provider unless it is " |
| 152 | "recompiled against a more recent LTTng UST."); |
| 153 | } |
| 154 | return 1; /* accept */ |
| 155 | } else { |
| 156 | ERR("Provider \"%s\" rejected, version %u.%u is incompatible " |
| 157 | "with LTTng UST provider version %u.%u. Please upgrade " |
| 158 | "LTTng UST.", |
| 159 | desc->provider, desc->major, desc->minor, |
| 160 | LTTNG_UST_PROVIDER_MAJOR, |
| 161 | LTTNG_UST_PROVIDER_MINOR); |
| 162 | return 0; /* reject */ |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | int lttng_probe_register(struct lttng_probe_desc *desc) |
| 168 | { |
| 169 | int ret = 0; |
| 170 | |
| 171 | lttng_ust_fixup_tls(); |
| 172 | |
| 173 | /* |
| 174 | * If version mismatch, don't register, but don't trigger assert |
| 175 | * on caller. The version check just prints an error. |
| 176 | */ |
| 177 | if (!check_provider_version(desc)) |
| 178 | return 0; |
| 179 | |
| 180 | ust_lock_nocheck(); |
| 181 | |
| 182 | cds_list_add(&desc->lazy_init_head, &lazy_probe_init); |
| 183 | desc->lazy = 1; |
| 184 | DBG("adding probe %s containing %u events to lazy registration list", |
| 185 | desc->provider, desc->nr_events); |
| 186 | /* |
| 187 | * If there is at least one active session, we need to register |
| 188 | * the probe immediately, since we cannot delay event |
| 189 | * registration because they are needed ASAP. |
| 190 | */ |
| 191 | if (lttng_session_active()) |
| 192 | fixup_lazy_probes(); |
| 193 | |
| 194 | lttng_fix_pending_event_notifiers(); |
| 195 | |
| 196 | ust_unlock(); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | void lttng_probe_unregister(struct lttng_probe_desc *desc) |
| 201 | { |
| 202 | lttng_ust_fixup_tls(); |
| 203 | |
| 204 | if (!check_provider_version(desc)) |
| 205 | return; |
| 206 | |
| 207 | ust_lock_nocheck(); |
| 208 | if (!desc->lazy) |
| 209 | cds_list_del(&desc->head); |
| 210 | else |
| 211 | cds_list_del(&desc->lazy_init_head); |
| 212 | |
| 213 | lttng_probe_provider_unregister_events(desc); |
| 214 | DBG("just unregistered probes of provider %s", desc->provider); |
| 215 | |
| 216 | ust_unlock(); |
| 217 | } |
| 218 | |
| 219 | void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) |
| 220 | { |
| 221 | struct tp_list_entry *list_entry, *tmp; |
| 222 | |
| 223 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { |
| 224 | cds_list_del(&list_entry->head); |
| 225 | free(list_entry); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * called with UST lock held. |
| 231 | */ |
| 232 | int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list) |
| 233 | { |
| 234 | struct lttng_probe_desc *probe_desc; |
| 235 | int i; |
| 236 | struct cds_list_head *probe_list; |
| 237 | |
| 238 | probe_list = lttng_get_probe_list_head(); |
| 239 | CDS_INIT_LIST_HEAD(&list->head); |
| 240 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
| 241 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 242 | struct tp_list_entry *list_entry; |
| 243 | |
| 244 | list_entry = zmalloc(sizeof(*list_entry)); |
| 245 | if (!list_entry) |
| 246 | goto err_nomem; |
| 247 | cds_list_add(&list_entry->head, &list->head); |
| 248 | strncpy(list_entry->tp.name, |
| 249 | probe_desc->event_desc[i]->name, |
| 250 | LTTNG_UST_SYM_NAME_LEN); |
| 251 | list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 252 | if (!probe_desc->event_desc[i]->loglevel) { |
| 253 | list_entry->tp.loglevel = TRACE_DEFAULT; |
| 254 | } else { |
| 255 | list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | if (cds_list_empty(&list->head)) |
| 260 | list->iter = NULL; |
| 261 | else |
| 262 | list->iter = |
| 263 | cds_list_first_entry(&list->head, struct tp_list_entry, head); |
| 264 | return 0; |
| 265 | |
| 266 | err_nomem: |
| 267 | lttng_probes_prune_event_list(list); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Return current iteration position, advance internal iterator to next. |
| 273 | * Return NULL if end of list. |
| 274 | */ |
| 275 | struct lttng_ust_tracepoint_iter * |
| 276 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) |
| 277 | { |
| 278 | struct tp_list_entry *entry; |
| 279 | |
| 280 | if (!list->iter) |
| 281 | return NULL; |
| 282 | entry = list->iter; |
| 283 | if (entry->head.next == &list->head) |
| 284 | list->iter = NULL; |
| 285 | else |
| 286 | list->iter = cds_list_entry(entry->head.next, |
| 287 | struct tp_list_entry, head); |
| 288 | return &entry->tp; |
| 289 | } |
| 290 | |
| 291 | void lttng_probes_prune_field_list(struct lttng_ust_field_list *list) |
| 292 | { |
| 293 | struct tp_field_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_field_list(struct lttng_ust_field_list *list) |
| 305 | { |
| 306 | struct lttng_probe_desc *probe_desc; |
| 307 | int i; |
| 308 | struct cds_list_head *probe_list; |
| 309 | |
| 310 | probe_list = lttng_get_probe_list_head(); |
| 311 | CDS_INIT_LIST_HEAD(&list->head); |
| 312 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
| 313 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 314 | const struct lttng_event_desc *event_desc = |
| 315 | probe_desc->event_desc[i]; |
| 316 | int j; |
| 317 | |
| 318 | if (event_desc->nr_fields == 0) { |
| 319 | /* Events without fields. */ |
| 320 | struct tp_field_list_entry *list_entry; |
| 321 | |
| 322 | list_entry = zmalloc(sizeof(*list_entry)); |
| 323 | if (!list_entry) |
| 324 | goto err_nomem; |
| 325 | cds_list_add(&list_entry->head, &list->head); |
| 326 | strncpy(list_entry->field.event_name, |
| 327 | event_desc->name, |
| 328 | LTTNG_UST_SYM_NAME_LEN); |
| 329 | list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 330 | list_entry->field.field_name[0] = '\0'; |
| 331 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; |
| 332 | if (!event_desc->loglevel) { |
| 333 | list_entry->field.loglevel = TRACE_DEFAULT; |
| 334 | } else { |
| 335 | list_entry->field.loglevel = *(*event_desc->loglevel); |
| 336 | } |
| 337 | list_entry->field.nowrite = 1; |
| 338 | } |
| 339 | |
| 340 | for (j = 0; j < event_desc->nr_fields; j++) { |
| 341 | const struct lttng_event_field *event_field = |
| 342 | &event_desc->fields[j]; |
| 343 | struct tp_field_list_entry *list_entry; |
| 344 | |
| 345 | list_entry = zmalloc(sizeof(*list_entry)); |
| 346 | if (!list_entry) |
| 347 | goto err_nomem; |
| 348 | cds_list_add(&list_entry->head, &list->head); |
| 349 | strncpy(list_entry->field.event_name, |
| 350 | event_desc->name, |
| 351 | LTTNG_UST_SYM_NAME_LEN); |
| 352 | list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 353 | strncpy(list_entry->field.field_name, |
| 354 | event_field->name, |
| 355 | LTTNG_UST_SYM_NAME_LEN); |
| 356 | list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 357 | switch (event_field->type.atype) { |
| 358 | case atype_integer: |
| 359 | list_entry->field.type = LTTNG_UST_FIELD_INTEGER; |
| 360 | break; |
| 361 | case atype_string: |
| 362 | list_entry->field.type = LTTNG_UST_FIELD_STRING; |
| 363 | break; |
| 364 | case atype_array_nestable: |
| 365 | if (event_field->type.u.array_nestable.elem_type->atype != atype_integer |
| 366 | || event_field->type.u.array_nestable.elem_type->u.integer.encoding == lttng_encode_none) |
| 367 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; |
| 368 | else |
| 369 | list_entry->field.type = LTTNG_UST_FIELD_STRING; |
| 370 | break; |
| 371 | case atype_sequence_nestable: |
| 372 | if (event_field->type.u.sequence_nestable.elem_type->atype != atype_integer |
| 373 | || event_field->type.u.sequence_nestable.elem_type->u.integer.encoding == lttng_encode_none) |
| 374 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; |
| 375 | else |
| 376 | list_entry->field.type = LTTNG_UST_FIELD_STRING; |
| 377 | break; |
| 378 | case atype_float: |
| 379 | list_entry->field.type = LTTNG_UST_FIELD_FLOAT; |
| 380 | break; |
| 381 | case atype_enum_nestable: |
| 382 | list_entry->field.type = LTTNG_UST_FIELD_ENUM; |
| 383 | break; |
| 384 | default: |
| 385 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; |
| 386 | } |
| 387 | if (!event_desc->loglevel) { |
| 388 | list_entry->field.loglevel = TRACE_DEFAULT; |
| 389 | } else { |
| 390 | list_entry->field.loglevel = *(*event_desc->loglevel); |
| 391 | } |
| 392 | list_entry->field.nowrite = event_field->nowrite; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | if (cds_list_empty(&list->head)) |
| 397 | list->iter = NULL; |
| 398 | else |
| 399 | list->iter = |
| 400 | cds_list_first_entry(&list->head, |
| 401 | struct tp_field_list_entry, head); |
| 402 | return 0; |
| 403 | |
| 404 | err_nomem: |
| 405 | lttng_probes_prune_field_list(list); |
| 406 | return -ENOMEM; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Return current iteration position, advance internal iterator to next. |
| 411 | * Return NULL if end of list. |
| 412 | */ |
| 413 | struct lttng_ust_field_iter * |
| 414 | lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list) |
| 415 | { |
| 416 | struct tp_field_list_entry *entry; |
| 417 | |
| 418 | if (!list->iter) |
| 419 | return NULL; |
| 420 | entry = list->iter; |
| 421 | if (entry->head.next == &list->head) |
| 422 | list->iter = NULL; |
| 423 | else |
| 424 | list->iter = cds_list_entry(entry->head.next, |
| 425 | struct tp_field_list_entry, head); |
| 426 | return &entry->field; |
| 427 | } |