| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Holds LTTng per-session event registry. |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <assert.h> |
| 12 | #include <errno.h> |
| 13 | #include <limits.h> |
| 14 | #include <pthread.h> |
| 15 | #include <sys/shm.h> |
| 16 | #include <sys/ipc.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stddef.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <time.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <unistd.h> |
| 23 | #include <dlfcn.h> |
| 24 | #include <lttng/ust-endian.h> |
| 25 | |
| 26 | #include <urcu/arch.h> |
| 27 | #include <urcu/compiler.h> |
| 28 | #include <urcu/hlist.h> |
| 29 | #include <urcu/list.h> |
| 30 | #include <urcu/uatomic.h> |
| 31 | |
| 32 | #include <lttng/tracepoint.h> |
| 33 | #include <lttng/ust-events.h> |
| 34 | |
| 35 | #include "common/logging.h" |
| 36 | #include "common/macros.h" |
| 37 | #include <lttng/ust-ctl.h> |
| 38 | #include "common/ustcomm.h" |
| 39 | #include "common/ust-fd.h" |
| 40 | #include "common/dynamic-type.h" |
| 41 | #include "common/ust-context-provider.h" |
| 42 | #include "lttng-ust-uuid.h" |
| 43 | |
| 44 | #include "common/tracepoint.h" |
| 45 | #include "common/strutils.h" |
| 46 | #include "lttng-bytecode.h" |
| 47 | #include "lttng-tracer.h" |
| 48 | #include "lttng-tracer-core.h" |
| 49 | #include "lttng-ust-statedump.h" |
| 50 | #include "context-internal.h" |
| 51 | #include "lib/lttng-ust/events.h" |
| 52 | #include "common/ringbuffer/shm.h" |
| 53 | #include "common/ringbuffer/frontend_types.h" |
| 54 | #include "common/ringbuffer/frontend.h" |
| 55 | #include "common/counter/counter.h" |
| 56 | #include "common/jhash.h" |
| 57 | #include <lttng/ust-abi.h> |
| 58 | #include "context-provider-internal.h" |
| 59 | |
| 60 | /* |
| 61 | * All operations within this file are called by the communication |
| 62 | * thread, under ust_lock protection. |
| 63 | */ |
| 64 | |
| 65 | static CDS_LIST_HEAD(sessions); |
| 66 | static CDS_LIST_HEAD(event_notifier_groups); |
| 67 | |
| 68 | struct cds_list_head *lttng_get_sessions(void) |
| 69 | { |
| 70 | return &sessions; |
| 71 | } |
| 72 | |
| 73 | static void _lttng_event_destroy(struct lttng_ust_event_common *event); |
| 74 | static void _lttng_enum_destroy(struct lttng_enum *_enum); |
| 75 | |
| 76 | static |
| 77 | void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session *session); |
| 78 | static |
| 79 | void lttng_session_sync_event_enablers(struct lttng_ust_session *session); |
| 80 | static |
| 81 | void lttng_event_notifier_group_sync_enablers( |
| 82 | struct lttng_event_notifier_group *event_notifier_group); |
| 83 | static |
| 84 | void lttng_enabler_destroy(struct lttng_enabler *enabler); |
| 85 | |
| 86 | bool lttng_ust_validate_event_name(const struct lttng_ust_event_desc *desc) |
| 87 | { |
| 88 | if (strlen(desc->probe_desc->provider_name) + 1 + |
| 89 | strlen(desc->event_name) >= LTTNG_UST_ABI_SYM_NAME_LEN) |
| 90 | return false; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void lttng_ust_format_event_name(const struct lttng_ust_event_desc *desc, |
| 95 | char *name) |
| 96 | { |
| 97 | strcpy(name, desc->probe_desc->provider_name); |
| 98 | strcat(name, ":"); |
| 99 | strcat(name, desc->event_name); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Called with ust lock held. |
| 104 | */ |
| 105 | int lttng_session_active(void) |
| 106 | { |
| 107 | struct lttng_ust_session_private *iter; |
| 108 | |
| 109 | cds_list_for_each_entry(iter, &sessions, node) { |
| 110 | if (iter->pub->active) |
| 111 | return 1; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | int lttng_loglevel_match(int loglevel, |
| 118 | unsigned int has_loglevel, |
| 119 | enum lttng_ust_abi_loglevel_type req_type, |
| 120 | int req_loglevel) |
| 121 | { |
| 122 | if (!has_loglevel) |
| 123 | loglevel = TRACE_DEFAULT; |
| 124 | switch (req_type) { |
| 125 | case LTTNG_UST_ABI_LOGLEVEL_RANGE: |
| 126 | if (loglevel <= req_loglevel |
| 127 | || (req_loglevel == -1 && loglevel <= TRACE_DEBUG)) |
| 128 | return 1; |
| 129 | else |
| 130 | return 0; |
| 131 | case LTTNG_UST_ABI_LOGLEVEL_SINGLE: |
| 132 | if (loglevel == req_loglevel |
| 133 | || (req_loglevel == -1 && loglevel <= TRACE_DEBUG)) |
| 134 | return 1; |
| 135 | else |
| 136 | return 0; |
| 137 | case LTTNG_UST_ABI_LOGLEVEL_ALL: |
| 138 | default: |
| 139 | if (loglevel <= TRACE_DEBUG) |
| 140 | return 1; |
| 141 | else |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | struct lttng_ust_session *lttng_session_create(void) |
| 147 | { |
| 148 | struct lttng_ust_session *session; |
| 149 | struct lttng_ust_session_private *session_priv; |
| 150 | int i; |
| 151 | |
| 152 | session = zmalloc(sizeof(struct lttng_ust_session)); |
| 153 | if (!session) |
| 154 | return NULL; |
| 155 | session->struct_size = sizeof(struct lttng_ust_session); |
| 156 | session_priv = zmalloc(sizeof(struct lttng_ust_session_private)); |
| 157 | if (!session_priv) { |
| 158 | free(session); |
| 159 | return NULL; |
| 160 | } |
| 161 | session->priv = session_priv; |
| 162 | session_priv->pub = session; |
| 163 | if (lttng_context_init_all(&session->priv->ctx)) { |
| 164 | free(session_priv); |
| 165 | free(session); |
| 166 | return NULL; |
| 167 | } |
| 168 | CDS_INIT_LIST_HEAD(&session->priv->chan_head); |
| 169 | CDS_INIT_LIST_HEAD(&session->priv->events_head); |
| 170 | CDS_INIT_LIST_HEAD(&session->priv->enums_head); |
| 171 | CDS_INIT_LIST_HEAD(&session->priv->enablers_head); |
| 172 | for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++) |
| 173 | CDS_INIT_HLIST_HEAD(&session->priv->events_ht.table[i]); |
| 174 | for (i = 0; i < LTTNG_UST_ENUM_HT_SIZE; i++) |
| 175 | CDS_INIT_HLIST_HEAD(&session->priv->enums_ht.table[i]); |
| 176 | cds_list_add(&session->priv->node, &sessions); |
| 177 | return session; |
| 178 | } |
| 179 | |
| 180 | struct lttng_counter *lttng_ust_counter_create( |
| 181 | const char *counter_transport_name, |
| 182 | size_t number_dimensions, const struct lttng_counter_dimension *dimensions) |
| 183 | { |
| 184 | struct lttng_counter_transport *counter_transport = NULL; |
| 185 | struct lttng_counter *counter = NULL; |
| 186 | |
| 187 | counter_transport = lttng_counter_transport_find(counter_transport_name); |
| 188 | if (!counter_transport) |
| 189 | goto notransport; |
| 190 | counter = zmalloc(sizeof(struct lttng_counter)); |
| 191 | if (!counter) |
| 192 | goto nomem; |
| 193 | |
| 194 | counter->ops = &counter_transport->ops; |
| 195 | counter->transport = counter_transport; |
| 196 | |
| 197 | counter->counter = counter->ops->counter_create( |
| 198 | number_dimensions, dimensions, 0, |
| 199 | -1, 0, NULL, false); |
| 200 | if (!counter->counter) { |
| 201 | goto create_error; |
| 202 | } |
| 203 | |
| 204 | return counter; |
| 205 | |
| 206 | create_error: |
| 207 | free(counter); |
| 208 | nomem: |
| 209 | notransport: |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | static |
| 214 | void lttng_ust_counter_destroy(struct lttng_counter *counter) |
| 215 | { |
| 216 | counter->ops->counter_destroy(counter->counter); |
| 217 | free(counter); |
| 218 | } |
| 219 | |
| 220 | struct lttng_event_notifier_group *lttng_event_notifier_group_create(void) |
| 221 | { |
| 222 | struct lttng_event_notifier_group *event_notifier_group; |
| 223 | int i; |
| 224 | |
| 225 | event_notifier_group = zmalloc(sizeof(struct lttng_event_notifier_group)); |
| 226 | if (!event_notifier_group) |
| 227 | return NULL; |
| 228 | |
| 229 | /* Add all contexts. */ |
| 230 | if (lttng_context_init_all(&event_notifier_group->ctx)) { |
| 231 | free(event_notifier_group); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | CDS_INIT_LIST_HEAD(&event_notifier_group->enablers_head); |
| 236 | CDS_INIT_LIST_HEAD(&event_notifier_group->event_notifiers_head); |
| 237 | for (i = 0; i < LTTNG_UST_EVENT_NOTIFIER_HT_SIZE; i++) |
| 238 | CDS_INIT_HLIST_HEAD(&event_notifier_group->event_notifiers_ht.table[i]); |
| 239 | |
| 240 | cds_list_add(&event_notifier_group->node, &event_notifier_groups); |
| 241 | |
| 242 | return event_notifier_group; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Only used internally at session destruction. |
| 247 | */ |
| 248 | static |
| 249 | void _lttng_channel_unmap(struct lttng_ust_channel_buffer *lttng_chan) |
| 250 | { |
| 251 | struct lttng_ust_ring_buffer_channel *chan; |
| 252 | struct lttng_ust_shm_handle *handle; |
| 253 | |
| 254 | cds_list_del(<tng_chan->priv->node); |
| 255 | lttng_destroy_context(lttng_chan->priv->ctx); |
| 256 | chan = lttng_chan->priv->rb_chan; |
| 257 | handle = chan->handle; |
| 258 | channel_destroy(chan, handle, 0); |
| 259 | free(lttng_chan->parent); |
| 260 | free(lttng_chan->priv); |
| 261 | free(lttng_chan); |
| 262 | } |
| 263 | |
| 264 | static |
| 265 | void register_event(struct lttng_ust_event_common *event) |
| 266 | { |
| 267 | int ret; |
| 268 | const struct lttng_ust_event_desc *desc; |
| 269 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 270 | |
| 271 | assert(event->priv->registered == 0); |
| 272 | desc = event->priv->desc; |
| 273 | lttng_ust_format_event_name(desc, name); |
| 274 | ret = lttng_ust_tp_probe_register_queue_release(name, |
| 275 | desc->probe_callback, |
| 276 | event, desc->signature); |
| 277 | WARN_ON_ONCE(ret); |
| 278 | if (!ret) |
| 279 | event->priv->registered = 1; |
| 280 | } |
| 281 | |
| 282 | static |
| 283 | void unregister_event(struct lttng_ust_event_common *event) |
| 284 | { |
| 285 | int ret; |
| 286 | const struct lttng_ust_event_desc *desc; |
| 287 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 288 | |
| 289 | assert(event->priv->registered == 1); |
| 290 | desc = event->priv->desc; |
| 291 | lttng_ust_format_event_name(desc, name); |
| 292 | ret = lttng_ust_tp_probe_unregister_queue_release(name, |
| 293 | desc->probe_callback, |
| 294 | event); |
| 295 | WARN_ON_ONCE(ret); |
| 296 | if (!ret) |
| 297 | event->priv->registered = 0; |
| 298 | } |
| 299 | |
| 300 | static |
| 301 | void _lttng_event_unregister(struct lttng_ust_event_common *event) |
| 302 | { |
| 303 | if (event->priv->registered) |
| 304 | unregister_event(event); |
| 305 | } |
| 306 | |
| 307 | void lttng_session_destroy(struct lttng_ust_session *session) |
| 308 | { |
| 309 | struct lttng_ust_channel_buffer_private *chan, *tmpchan; |
| 310 | struct lttng_ust_event_recorder_private *event_recorder_priv, *tmpevent_recorder_priv; |
| 311 | struct lttng_enum *_enum, *tmp_enum; |
| 312 | struct lttng_event_enabler *event_enabler, *event_tmpenabler; |
| 313 | |
| 314 | CMM_ACCESS_ONCE(session->active) = 0; |
| 315 | cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) { |
| 316 | _lttng_event_unregister(event_recorder_priv->parent.pub); |
| 317 | } |
| 318 | lttng_ust_urcu_synchronize_rcu(); /* Wait for in-flight events to complete */ |
| 319 | lttng_ust_tp_probe_prune_release_queue(); |
| 320 | cds_list_for_each_entry_safe(event_enabler, event_tmpenabler, |
| 321 | &session->priv->enablers_head, node) |
| 322 | lttng_event_enabler_destroy(event_enabler); |
| 323 | cds_list_for_each_entry_safe(event_recorder_priv, tmpevent_recorder_priv, |
| 324 | &session->priv->events_head, node) |
| 325 | _lttng_event_destroy(event_recorder_priv->parent.pub); |
| 326 | cds_list_for_each_entry_safe(_enum, tmp_enum, |
| 327 | &session->priv->enums_head, node) |
| 328 | _lttng_enum_destroy(_enum); |
| 329 | cds_list_for_each_entry_safe(chan, tmpchan, &session->priv->chan_head, node) |
| 330 | _lttng_channel_unmap(chan->pub); |
| 331 | cds_list_del(&session->priv->node); |
| 332 | lttng_destroy_context(session->priv->ctx); |
| 333 | free(session->priv); |
| 334 | free(session); |
| 335 | } |
| 336 | |
| 337 | void lttng_event_notifier_group_destroy( |
| 338 | struct lttng_event_notifier_group *event_notifier_group) |
| 339 | { |
| 340 | int close_ret; |
| 341 | struct lttng_event_notifier_enabler *notifier_enabler, *tmpnotifier_enabler; |
| 342 | struct lttng_ust_event_notifier_private *event_notifier_priv, *tmpevent_notifier_priv; |
| 343 | |
| 344 | if (!event_notifier_group) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | cds_list_for_each_entry(event_notifier_priv, |
| 349 | &event_notifier_group->event_notifiers_head, node) |
| 350 | _lttng_event_unregister(event_notifier_priv->parent.pub); |
| 351 | |
| 352 | lttng_ust_urcu_synchronize_rcu(); |
| 353 | |
| 354 | cds_list_for_each_entry_safe(notifier_enabler, tmpnotifier_enabler, |
| 355 | &event_notifier_group->enablers_head, node) |
| 356 | lttng_event_notifier_enabler_destroy(notifier_enabler); |
| 357 | |
| 358 | cds_list_for_each_entry_safe(event_notifier_priv, tmpevent_notifier_priv, |
| 359 | &event_notifier_group->event_notifiers_head, node) |
| 360 | _lttng_event_destroy(event_notifier_priv->parent.pub); |
| 361 | |
| 362 | if (event_notifier_group->error_counter) |
| 363 | lttng_ust_counter_destroy(event_notifier_group->error_counter); |
| 364 | |
| 365 | /* Close the notification fd to the listener of event_notifiers. */ |
| 366 | |
| 367 | lttng_ust_lock_fd_tracker(); |
| 368 | close_ret = close(event_notifier_group->notification_fd); |
| 369 | if (!close_ret) { |
| 370 | lttng_ust_delete_fd_from_tracker( |
| 371 | event_notifier_group->notification_fd); |
| 372 | } else { |
| 373 | PERROR("close"); |
| 374 | abort(); |
| 375 | } |
| 376 | lttng_ust_unlock_fd_tracker(); |
| 377 | |
| 378 | cds_list_del(&event_notifier_group->node); |
| 379 | lttng_destroy_context(event_notifier_group->ctx); |
| 380 | free(event_notifier_group); |
| 381 | } |
| 382 | |
| 383 | static |
| 384 | void lttng_enabler_destroy(struct lttng_enabler *enabler) |
| 385 | { |
| 386 | struct lttng_ust_bytecode_node *filter_node, *tmp_filter_node; |
| 387 | struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node; |
| 388 | |
| 389 | if (!enabler) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | /* Destroy filter bytecode */ |
| 394 | cds_list_for_each_entry_safe(filter_node, tmp_filter_node, |
| 395 | &enabler->filter_bytecode_head, node) { |
| 396 | free(filter_node); |
| 397 | } |
| 398 | |
| 399 | /* Destroy excluders */ |
| 400 | cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node, |
| 401 | &enabler->excluder_head, node) { |
| 402 | free(excluder_node); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void lttng_event_notifier_enabler_destroy(struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 407 | { |
| 408 | if (!event_notifier_enabler) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | cds_list_del(&event_notifier_enabler->node); |
| 413 | |
| 414 | lttng_enabler_destroy(lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)); |
| 415 | |
| 416 | free(event_notifier_enabler); |
| 417 | } |
| 418 | |
| 419 | static |
| 420 | int lttng_enum_create(const struct lttng_ust_enum_desc *desc, |
| 421 | struct lttng_ust_session *session) |
| 422 | { |
| 423 | const char *enum_name = desc->name; |
| 424 | struct lttng_enum *_enum; |
| 425 | struct cds_hlist_head *head; |
| 426 | int ret = 0; |
| 427 | size_t name_len = strlen(enum_name); |
| 428 | uint32_t hash; |
| 429 | int notify_socket; |
| 430 | |
| 431 | /* Check if this enum is already registered for this session. */ |
| 432 | hash = jhash(enum_name, name_len, 0); |
| 433 | head = &session->priv->enums_ht.table[hash & (LTTNG_UST_ENUM_HT_SIZE - 1)]; |
| 434 | |
| 435 | _enum = lttng_ust_enum_get_from_desc(session, desc); |
| 436 | if (_enum) { |
| 437 | ret = -EEXIST; |
| 438 | goto exist; |
| 439 | } |
| 440 | |
| 441 | notify_socket = lttng_get_notify_socket(session->priv->owner); |
| 442 | if (notify_socket < 0) { |
| 443 | ret = notify_socket; |
| 444 | goto socket_error; |
| 445 | } |
| 446 | |
| 447 | _enum = zmalloc(sizeof(*_enum)); |
| 448 | if (!_enum) { |
| 449 | ret = -ENOMEM; |
| 450 | goto cache_error; |
| 451 | } |
| 452 | _enum->session = session; |
| 453 | _enum->desc = desc; |
| 454 | |
| 455 | ret = ustcomm_register_enum(notify_socket, |
| 456 | session->priv->objd, |
| 457 | enum_name, |
| 458 | desc->nr_entries, |
| 459 | desc->entries, |
| 460 | &_enum->id); |
| 461 | if (ret < 0) { |
| 462 | DBG("Error (%d) registering enumeration to sessiond", ret); |
| 463 | goto sessiond_register_error; |
| 464 | } |
| 465 | cds_list_add(&_enum->node, &session->priv->enums_head); |
| 466 | cds_hlist_add_head(&_enum->hlist, head); |
| 467 | return 0; |
| 468 | |
| 469 | sessiond_register_error: |
| 470 | free(_enum); |
| 471 | cache_error: |
| 472 | socket_error: |
| 473 | exist: |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static |
| 478 | int lttng_create_enum_check(const struct lttng_ust_type_common *type, |
| 479 | struct lttng_ust_session *session) |
| 480 | { |
| 481 | switch (type->type) { |
| 482 | case lttng_ust_type_enum: |
| 483 | { |
| 484 | const struct lttng_ust_enum_desc *enum_desc; |
| 485 | int ret; |
| 486 | |
| 487 | enum_desc = lttng_ust_get_type_enum(type)->desc; |
| 488 | ret = lttng_enum_create(enum_desc, session); |
| 489 | if (ret && ret != -EEXIST) { |
| 490 | DBG("Unable to create enum error: (%d)", ret); |
| 491 | return ret; |
| 492 | } |
| 493 | break; |
| 494 | } |
| 495 | case lttng_ust_type_dynamic: |
| 496 | { |
| 497 | const struct lttng_ust_event_field *tag_field_generic; |
| 498 | const struct lttng_ust_enum_desc *enum_desc; |
| 499 | int ret; |
| 500 | |
| 501 | tag_field_generic = lttng_ust_dynamic_type_tag_field(); |
| 502 | enum_desc = lttng_ust_get_type_enum(tag_field_generic->type)->desc; |
| 503 | ret = lttng_enum_create(enum_desc, session); |
| 504 | if (ret && ret != -EEXIST) { |
| 505 | DBG("Unable to create enum error: (%d)", ret); |
| 506 | return ret; |
| 507 | } |
| 508 | break; |
| 509 | } |
| 510 | default: |
| 511 | /* TODO: nested types when they become supported. */ |
| 512 | break; |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static |
| 518 | int lttng_create_all_event_enums(size_t nr_fields, |
| 519 | const struct lttng_ust_event_field * const *event_fields, |
| 520 | struct lttng_ust_session *session) |
| 521 | { |
| 522 | size_t i; |
| 523 | int ret; |
| 524 | |
| 525 | /* For each field, ensure enum is part of the session. */ |
| 526 | for (i = 0; i < nr_fields; i++) { |
| 527 | const struct lttng_ust_type_common *type = event_fields[i]->type; |
| 528 | |
| 529 | ret = lttng_create_enum_check(type, session); |
| 530 | if (ret) |
| 531 | return ret; |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static |
| 537 | int lttng_create_all_ctx_enums(size_t nr_fields, |
| 538 | struct lttng_ust_ctx_field *ctx_fields, |
| 539 | struct lttng_ust_session *session) |
| 540 | { |
| 541 | size_t i; |
| 542 | int ret; |
| 543 | |
| 544 | /* For each field, ensure enum is part of the session. */ |
| 545 | for (i = 0; i < nr_fields; i++) { |
| 546 | const struct lttng_ust_type_common *type = ctx_fields[i].event_field->type; |
| 547 | |
| 548 | ret = lttng_create_enum_check(type, session); |
| 549 | if (ret) |
| 550 | return ret; |
| 551 | } |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Ensure that a state-dump will be performed for this session at the end |
| 557 | * of the current handle_message(). |
| 558 | */ |
| 559 | int lttng_session_statedump(struct lttng_ust_session *session) |
| 560 | { |
| 561 | session->priv->statedump_pending = 1; |
| 562 | lttng_ust_sockinfo_session_enabled(session->priv->owner); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int lttng_session_enable(struct lttng_ust_session *session) |
| 567 | { |
| 568 | int ret = 0; |
| 569 | struct lttng_ust_channel_buffer_private *chan; |
| 570 | int notify_socket; |
| 571 | |
| 572 | if (session->active) { |
| 573 | ret = -EBUSY; |
| 574 | goto end; |
| 575 | } |
| 576 | |
| 577 | notify_socket = lttng_get_notify_socket(session->priv->owner); |
| 578 | if (notify_socket < 0) |
| 579 | return notify_socket; |
| 580 | |
| 581 | /* Set transient enabler state to "enabled" */ |
| 582 | session->priv->tstate = 1; |
| 583 | |
| 584 | /* We need to sync enablers with session before activation. */ |
| 585 | lttng_session_sync_event_enablers(session); |
| 586 | |
| 587 | /* |
| 588 | * Snapshot the number of events per channel to know the type of header |
| 589 | * we need to use. |
| 590 | */ |
| 591 | cds_list_for_each_entry(chan, &session->priv->chan_head, node) { |
| 592 | struct lttng_ust_ctx *ctx; |
| 593 | struct lttng_ust_ctx_field *fields = NULL; |
| 594 | size_t nr_fields = 0; |
| 595 | uint32_t chan_id; |
| 596 | |
| 597 | /* don't change it if session stop/restart */ |
| 598 | if (chan->header_type) |
| 599 | continue; |
| 600 | ctx = chan->ctx; |
| 601 | if (ctx) { |
| 602 | nr_fields = ctx->nr_fields; |
| 603 | fields = ctx->fields; |
| 604 | ret = lttng_create_all_ctx_enums(nr_fields, fields, |
| 605 | session); |
| 606 | if (ret < 0) { |
| 607 | DBG("Error (%d) adding enum to session", ret); |
| 608 | return ret; |
| 609 | } |
| 610 | } |
| 611 | ret = ustcomm_register_channel(notify_socket, |
| 612 | session, |
| 613 | session->priv->objd, |
| 614 | chan->parent.objd, |
| 615 | nr_fields, |
| 616 | fields, |
| 617 | &chan_id, |
| 618 | &chan->header_type); |
| 619 | if (ret) { |
| 620 | DBG("Error (%d) registering channel to sessiond", ret); |
| 621 | return ret; |
| 622 | } |
| 623 | if (chan_id != chan->id) { |
| 624 | DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)", |
| 625 | chan_id, chan->id); |
| 626 | return -EINVAL; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* Set atomically the state to "active" */ |
| 631 | CMM_ACCESS_ONCE(session->active) = 1; |
| 632 | CMM_ACCESS_ONCE(session->priv->been_active) = 1; |
| 633 | |
| 634 | ret = lttng_session_statedump(session); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | end: |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | int lttng_session_disable(struct lttng_ust_session *session) |
| 642 | { |
| 643 | int ret = 0; |
| 644 | |
| 645 | if (!session->active) { |
| 646 | ret = -EBUSY; |
| 647 | goto end; |
| 648 | } |
| 649 | /* Set atomically the state to "inactive" */ |
| 650 | CMM_ACCESS_ONCE(session->active) = 0; |
| 651 | |
| 652 | /* Set transient enabler state to "disabled" */ |
| 653 | session->priv->tstate = 0; |
| 654 | lttng_session_sync_event_enablers(session); |
| 655 | end: |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | int lttng_channel_enable(struct lttng_ust_channel_common *lttng_channel) |
| 660 | { |
| 661 | int ret = 0; |
| 662 | |
| 663 | if (lttng_channel->enabled) { |
| 664 | ret = -EBUSY; |
| 665 | goto end; |
| 666 | } |
| 667 | /* Set transient enabler state to "enabled" */ |
| 668 | lttng_channel->priv->tstate = 1; |
| 669 | lttng_session_sync_event_enablers(lttng_channel->session); |
| 670 | /* Set atomically the state to "enabled" */ |
| 671 | CMM_ACCESS_ONCE(lttng_channel->enabled) = 1; |
| 672 | end: |
| 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | int lttng_channel_disable(struct lttng_ust_channel_common *lttng_channel) |
| 677 | { |
| 678 | int ret = 0; |
| 679 | |
| 680 | if (!lttng_channel->enabled) { |
| 681 | ret = -EBUSY; |
| 682 | goto end; |
| 683 | } |
| 684 | /* Set atomically the state to "disabled" */ |
| 685 | CMM_ACCESS_ONCE(lttng_channel->enabled) = 0; |
| 686 | /* Set transient enabler state to "enabled" */ |
| 687 | lttng_channel->priv->tstate = 0; |
| 688 | lttng_session_sync_event_enablers(lttng_channel->session); |
| 689 | end: |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static inline |
| 694 | struct cds_hlist_head *borrow_hash_table_bucket( |
| 695 | struct cds_hlist_head *hash_table, |
| 696 | unsigned int hash_table_size, |
| 697 | const struct lttng_ust_event_desc *desc) |
| 698 | { |
| 699 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 700 | size_t name_len; |
| 701 | uint32_t hash; |
| 702 | |
| 703 | lttng_ust_format_event_name(desc, name); |
| 704 | name_len = strlen(name); |
| 705 | |
| 706 | hash = jhash(name, name_len, 0); |
| 707 | return &hash_table[hash & (hash_table_size - 1)]; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Supports event creation while tracing session is active. |
| 712 | */ |
| 713 | static |
| 714 | int lttng_event_recorder_create(const struct lttng_ust_event_desc *desc, |
| 715 | struct lttng_ust_channel_buffer *chan) |
| 716 | { |
| 717 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 718 | struct lttng_ust_event_recorder *event_recorder; |
| 719 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 720 | struct lttng_ust_session *session = chan->parent->session; |
| 721 | struct cds_hlist_head *head; |
| 722 | int ret = 0; |
| 723 | int notify_socket, loglevel; |
| 724 | const char *uri; |
| 725 | |
| 726 | head = borrow_hash_table_bucket(chan->parent->session->priv->events_ht.table, |
| 727 | LTTNG_UST_EVENT_HT_SIZE, desc); |
| 728 | |
| 729 | notify_socket = lttng_get_notify_socket(session->priv->owner); |
| 730 | if (notify_socket < 0) { |
| 731 | ret = notify_socket; |
| 732 | goto socket_error; |
| 733 | } |
| 734 | |
| 735 | ret = lttng_create_all_event_enums(desc->nr_fields, desc->fields, |
| 736 | session); |
| 737 | if (ret < 0) { |
| 738 | DBG("Error (%d) adding enum to session", ret); |
| 739 | goto create_enum_error; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Check if loglevel match. Refuse to connect event if not. |
| 744 | */ |
| 745 | event_recorder = zmalloc(sizeof(struct lttng_ust_event_recorder)); |
| 746 | if (!event_recorder) { |
| 747 | ret = -ENOMEM; |
| 748 | goto cache_error; |
| 749 | } |
| 750 | event_recorder->struct_size = sizeof(struct lttng_ust_event_recorder); |
| 751 | |
| 752 | event_recorder->parent = zmalloc(sizeof(struct lttng_ust_event_common)); |
| 753 | if (!event_recorder->parent) { |
| 754 | ret = -ENOMEM; |
| 755 | goto parent_error; |
| 756 | } |
| 757 | event_recorder->parent->struct_size = sizeof(struct lttng_ust_event_common); |
| 758 | event_recorder->parent->type = LTTNG_UST_EVENT_TYPE_RECORDER; |
| 759 | event_recorder->parent->child = event_recorder; |
| 760 | |
| 761 | event_recorder_priv = zmalloc(sizeof(struct lttng_ust_event_recorder_private)); |
| 762 | if (!event_recorder_priv) { |
| 763 | ret = -ENOMEM; |
| 764 | goto priv_error; |
| 765 | } |
| 766 | event_recorder->priv = event_recorder_priv; |
| 767 | event_recorder_priv->pub = event_recorder; |
| 768 | event_recorder->parent->priv = &event_recorder_priv->parent; |
| 769 | event_recorder_priv->parent.pub = event_recorder->parent; |
| 770 | |
| 771 | event_recorder->chan = chan; |
| 772 | |
| 773 | /* Event will be enabled by enabler sync. */ |
| 774 | event_recorder->parent->run_filter = lttng_ust_interpret_event_filter; |
| 775 | event_recorder->parent->enabled = 0; |
| 776 | event_recorder->parent->priv->registered = 0; |
| 777 | CDS_INIT_LIST_HEAD(&event_recorder->parent->priv->filter_bytecode_runtime_head); |
| 778 | CDS_INIT_LIST_HEAD(&event_recorder->parent->priv->enablers_ref_head); |
| 779 | event_recorder->parent->priv->desc = desc; |
| 780 | |
| 781 | if (desc->loglevel) |
| 782 | loglevel = *(*desc->loglevel); |
| 783 | else |
| 784 | loglevel = TRACE_DEFAULT; |
| 785 | if (desc->model_emf_uri) |
| 786 | uri = *(desc->model_emf_uri); |
| 787 | else |
| 788 | uri = NULL; |
| 789 | |
| 790 | lttng_ust_format_event_name(desc, name); |
| 791 | |
| 792 | /* Fetch event ID from sessiond */ |
| 793 | ret = ustcomm_register_event(notify_socket, |
| 794 | session, |
| 795 | session->priv->objd, |
| 796 | chan->priv->parent.objd, |
| 797 | name, |
| 798 | loglevel, |
| 799 | desc->signature, |
| 800 | desc->nr_fields, |
| 801 | desc->fields, |
| 802 | uri, |
| 803 | &event_recorder->priv->id); |
| 804 | if (ret < 0) { |
| 805 | DBG("Error (%d) registering event to sessiond", ret); |
| 806 | goto sessiond_register_error; |
| 807 | } |
| 808 | |
| 809 | cds_list_add(&event_recorder_priv->node, &chan->parent->session->priv->events_head); |
| 810 | cds_hlist_add_head(&event_recorder_priv->hlist, head); |
| 811 | return 0; |
| 812 | |
| 813 | sessiond_register_error: |
| 814 | free(event_recorder_priv); |
| 815 | priv_error: |
| 816 | free(event_recorder->parent); |
| 817 | parent_error: |
| 818 | free(event_recorder); |
| 819 | cache_error: |
| 820 | create_enum_error: |
| 821 | socket_error: |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static |
| 826 | int lttng_event_notifier_create(const struct lttng_ust_event_desc *desc, |
| 827 | uint64_t token, uint64_t error_counter_index, |
| 828 | struct lttng_event_notifier_group *event_notifier_group) |
| 829 | { |
| 830 | struct lttng_ust_event_notifier *event_notifier; |
| 831 | struct lttng_ust_event_notifier_private *event_notifier_priv; |
| 832 | struct cds_hlist_head *head; |
| 833 | int ret = 0; |
| 834 | |
| 835 | /* |
| 836 | * Get the hashtable bucket the created lttng_event_notifier object |
| 837 | * should be inserted. |
| 838 | */ |
| 839 | head = borrow_hash_table_bucket( |
| 840 | event_notifier_group->event_notifiers_ht.table, |
| 841 | LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc); |
| 842 | |
| 843 | event_notifier = zmalloc(sizeof(struct lttng_ust_event_notifier)); |
| 844 | if (!event_notifier) { |
| 845 | ret = -ENOMEM; |
| 846 | goto error; |
| 847 | } |
| 848 | event_notifier->struct_size = sizeof(struct lttng_ust_event_notifier); |
| 849 | |
| 850 | event_notifier->parent = zmalloc(sizeof(struct lttng_ust_event_common)); |
| 851 | if (!event_notifier->parent) { |
| 852 | ret = -ENOMEM; |
| 853 | goto parent_error; |
| 854 | } |
| 855 | event_notifier->parent->struct_size = sizeof(struct lttng_ust_event_common); |
| 856 | event_notifier->parent->type = LTTNG_UST_EVENT_TYPE_NOTIFIER; |
| 857 | event_notifier->parent->child = event_notifier; |
| 858 | |
| 859 | event_notifier_priv = zmalloc(sizeof(struct lttng_ust_event_notifier_private)); |
| 860 | if (!event_notifier_priv) { |
| 861 | ret = -ENOMEM; |
| 862 | goto priv_error; |
| 863 | } |
| 864 | event_notifier->priv = event_notifier_priv; |
| 865 | event_notifier_priv->pub = event_notifier; |
| 866 | event_notifier->parent->priv = &event_notifier_priv->parent; |
| 867 | event_notifier_priv->parent.pub = event_notifier->parent; |
| 868 | |
| 869 | event_notifier_priv->group = event_notifier_group; |
| 870 | event_notifier_priv->parent.user_token = token; |
| 871 | event_notifier_priv->error_counter_index = error_counter_index; |
| 872 | |
| 873 | /* Event notifier will be enabled by enabler sync. */ |
| 874 | event_notifier->parent->run_filter = lttng_ust_interpret_event_filter; |
| 875 | event_notifier->parent->enabled = 0; |
| 876 | event_notifier_priv->parent.registered = 0; |
| 877 | |
| 878 | CDS_INIT_LIST_HEAD(&event_notifier->parent->priv->filter_bytecode_runtime_head); |
| 879 | CDS_INIT_LIST_HEAD(&event_notifier->priv->capture_bytecode_runtime_head); |
| 880 | CDS_INIT_LIST_HEAD(&event_notifier_priv->parent.enablers_ref_head); |
| 881 | event_notifier_priv->parent.desc = desc; |
| 882 | event_notifier->notification_send = lttng_event_notifier_notification_send; |
| 883 | |
| 884 | cds_list_add(&event_notifier_priv->node, |
| 885 | &event_notifier_group->event_notifiers_head); |
| 886 | cds_hlist_add_head(&event_notifier_priv->hlist, head); |
| 887 | |
| 888 | return 0; |
| 889 | |
| 890 | priv_error: |
| 891 | free(event_notifier->parent); |
| 892 | parent_error: |
| 893 | free(event_notifier); |
| 894 | error: |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | static |
| 899 | int lttng_desc_match_star_glob_enabler(const struct lttng_ust_event_desc *desc, |
| 900 | struct lttng_enabler *enabler) |
| 901 | { |
| 902 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 903 | int loglevel = 0; |
| 904 | unsigned int has_loglevel = 0; |
| 905 | |
| 906 | lttng_ust_format_event_name(desc, name); |
| 907 | assert(enabler->format_type == LTTNG_ENABLER_FORMAT_STAR_GLOB); |
| 908 | if (!strutils_star_glob_match(enabler->event_param.name, SIZE_MAX, |
| 909 | name, SIZE_MAX)) |
| 910 | return 0; |
| 911 | if (desc->loglevel) { |
| 912 | loglevel = *(*desc->loglevel); |
| 913 | has_loglevel = 1; |
| 914 | } |
| 915 | if (!lttng_loglevel_match(loglevel, |
| 916 | has_loglevel, |
| 917 | enabler->event_param.loglevel_type, |
| 918 | enabler->event_param.loglevel)) |
| 919 | return 0; |
| 920 | return 1; |
| 921 | } |
| 922 | |
| 923 | static |
| 924 | int lttng_desc_match_event_enabler(const struct lttng_ust_event_desc *desc, |
| 925 | struct lttng_enabler *enabler) |
| 926 | { |
| 927 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 928 | int loglevel = 0; |
| 929 | unsigned int has_loglevel = 0; |
| 930 | |
| 931 | lttng_ust_format_event_name(desc, name); |
| 932 | assert(enabler->format_type == LTTNG_ENABLER_FORMAT_EVENT); |
| 933 | if (strcmp(name, enabler->event_param.name)) |
| 934 | return 0; |
| 935 | if (desc->loglevel) { |
| 936 | loglevel = *(*desc->loglevel); |
| 937 | has_loglevel = 1; |
| 938 | } |
| 939 | if (!lttng_loglevel_match(loglevel, |
| 940 | has_loglevel, |
| 941 | enabler->event_param.loglevel_type, |
| 942 | enabler->event_param.loglevel)) |
| 943 | return 0; |
| 944 | return 1; |
| 945 | } |
| 946 | |
| 947 | static |
| 948 | int lttng_desc_match_enabler(const struct lttng_ust_event_desc *desc, |
| 949 | struct lttng_enabler *enabler) |
| 950 | { |
| 951 | switch (enabler->format_type) { |
| 952 | case LTTNG_ENABLER_FORMAT_STAR_GLOB: |
| 953 | { |
| 954 | struct lttng_ust_excluder_node *excluder; |
| 955 | |
| 956 | if (!lttng_desc_match_star_glob_enabler(desc, enabler)) { |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * If the matching event matches with an excluder, |
| 962 | * return 'does not match' |
| 963 | */ |
| 964 | cds_list_for_each_entry(excluder, &enabler->excluder_head, node) { |
| 965 | int count; |
| 966 | |
| 967 | for (count = 0; count < excluder->excluder.count; count++) { |
| 968 | int len; |
| 969 | char *excluder_name; |
| 970 | |
| 971 | excluder_name = (char *) (excluder->excluder.names) |
| 972 | + count * LTTNG_UST_ABI_SYM_NAME_LEN; |
| 973 | len = strnlen(excluder_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
| 974 | if (len > 0) { |
| 975 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 976 | |
| 977 | lttng_ust_format_event_name(desc, name); |
| 978 | if (strutils_star_glob_match(excluder_name, len, name, SIZE_MAX)) { |
| 979 | return 0; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | return 1; |
| 985 | } |
| 986 | case LTTNG_ENABLER_FORMAT_EVENT: |
| 987 | return lttng_desc_match_event_enabler(desc, enabler); |
| 988 | default: |
| 989 | return -EINVAL; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | static |
| 994 | int lttng_event_enabler_match_event(struct lttng_event_enabler *event_enabler, |
| 995 | struct lttng_ust_event_recorder *event_recorder) |
| 996 | { |
| 997 | if (lttng_desc_match_enabler(event_recorder->parent->priv->desc, |
| 998 | lttng_event_enabler_as_enabler(event_enabler)) |
| 999 | && event_recorder->chan == event_enabler->chan) |
| 1000 | return 1; |
| 1001 | else |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static |
| 1006 | int lttng_event_notifier_enabler_match_event_notifier( |
| 1007 | struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 1008 | struct lttng_ust_event_notifier *event_notifier) |
| 1009 | { |
| 1010 | int desc_matches = lttng_desc_match_enabler(event_notifier->priv->parent.desc, |
| 1011 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)); |
| 1012 | |
| 1013 | if (desc_matches && event_notifier->priv->group == event_notifier_enabler->group && |
| 1014 | event_notifier->priv->parent.user_token == event_notifier_enabler->user_token) |
| 1015 | return 1; |
| 1016 | else |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static |
| 1021 | struct lttng_enabler_ref *lttng_enabler_ref( |
| 1022 | struct cds_list_head *enabler_ref_list, |
| 1023 | struct lttng_enabler *enabler) |
| 1024 | { |
| 1025 | struct lttng_enabler_ref *enabler_ref; |
| 1026 | |
| 1027 | cds_list_for_each_entry(enabler_ref, enabler_ref_list, node) { |
| 1028 | if (enabler_ref->ref == enabler) |
| 1029 | return enabler_ref; |
| 1030 | } |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Create struct lttng_event if it is missing and present in the list of |
| 1036 | * tracepoint probes. |
| 1037 | */ |
| 1038 | static |
| 1039 | void lttng_create_event_recorder_if_missing(struct lttng_event_enabler *event_enabler) |
| 1040 | { |
| 1041 | struct lttng_ust_session *session = event_enabler->chan->parent->session; |
| 1042 | struct lttng_ust_registered_probe *reg_probe; |
| 1043 | const struct lttng_ust_event_desc *desc; |
| 1044 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 1045 | int i; |
| 1046 | struct cds_list_head *probe_list; |
| 1047 | |
| 1048 | probe_list = lttng_get_probe_list_head(); |
| 1049 | /* |
| 1050 | * For each probe event, if we find that a probe event matches |
| 1051 | * our enabler, create an associated lttng_event if not |
| 1052 | * already present. |
| 1053 | */ |
| 1054 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
| 1055 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; |
| 1056 | |
| 1057 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 1058 | int ret; |
| 1059 | bool found = false; |
| 1060 | struct cds_hlist_head *head; |
| 1061 | struct cds_hlist_node *node; |
| 1062 | |
| 1063 | desc = probe_desc->event_desc[i]; |
| 1064 | if (!lttng_desc_match_enabler(desc, |
| 1065 | lttng_event_enabler_as_enabler(event_enabler))) |
| 1066 | continue; |
| 1067 | |
| 1068 | head = borrow_hash_table_bucket( |
| 1069 | session->priv->events_ht.table, |
| 1070 | LTTNG_UST_EVENT_HT_SIZE, desc); |
| 1071 | |
| 1072 | cds_hlist_for_each_entry(event_recorder_priv, node, head, hlist) { |
| 1073 | if (event_recorder_priv->parent.desc == desc |
| 1074 | && event_recorder_priv->pub->chan == event_enabler->chan) { |
| 1075 | found = true; |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | if (found) |
| 1080 | continue; |
| 1081 | |
| 1082 | /* |
| 1083 | * We need to create an event for this |
| 1084 | * event probe. |
| 1085 | */ |
| 1086 | ret = lttng_event_recorder_create(probe_desc->event_desc[i], |
| 1087 | event_enabler->chan); |
| 1088 | if (ret) { |
| 1089 | DBG("Unable to create event \"%s:%s\", error %d\n", |
| 1090 | probe_desc->provider_name, |
| 1091 | probe_desc->event_desc[i]->event_name, ret); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | static |
| 1098 | void probe_provider_event_for_each(const struct lttng_ust_probe_desc *provider_desc, |
| 1099 | void (*event_func)(struct lttng_ust_event_common *event)) |
| 1100 | { |
| 1101 | struct cds_hlist_node *node, *tmp_node; |
| 1102 | struct cds_list_head *sessionsp; |
| 1103 | unsigned int i; |
| 1104 | |
| 1105 | /* Get handle on list of sessions. */ |
| 1106 | sessionsp = lttng_get_sessions(); |
| 1107 | |
| 1108 | /* |
| 1109 | * Iterate over all events in the probe provider descriptions and |
| 1110 | * sessions to queue the unregistration of the events. |
| 1111 | */ |
| 1112 | for (i = 0; i < provider_desc->nr_events; i++) { |
| 1113 | const struct lttng_ust_event_desc *event_desc; |
| 1114 | struct lttng_event_notifier_group *event_notifier_group; |
| 1115 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 1116 | struct lttng_ust_event_notifier_private *event_notifier_priv; |
| 1117 | struct lttng_ust_session_private *session_priv; |
| 1118 | struct cds_hlist_head *head; |
| 1119 | |
| 1120 | event_desc = provider_desc->event_desc[i]; |
| 1121 | |
| 1122 | /* |
| 1123 | * Iterate over all session to find the current event |
| 1124 | * description. |
| 1125 | */ |
| 1126 | cds_list_for_each_entry(session_priv, sessionsp, node) { |
| 1127 | /* |
| 1128 | * Get the list of events in the hashtable bucket and |
| 1129 | * iterate to find the event matching this descriptor. |
| 1130 | */ |
| 1131 | head = borrow_hash_table_bucket( |
| 1132 | session_priv->events_ht.table, |
| 1133 | LTTNG_UST_EVENT_HT_SIZE, event_desc); |
| 1134 | |
| 1135 | cds_hlist_for_each_entry_safe(event_recorder_priv, node, tmp_node, head, hlist) { |
| 1136 | if (event_desc == event_recorder_priv->parent.desc) { |
| 1137 | event_func(event_recorder_priv->parent.pub); |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Iterate over all event_notifier groups to find the current event |
| 1145 | * description. |
| 1146 | */ |
| 1147 | cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) { |
| 1148 | /* |
| 1149 | * Get the list of event_notifiers in the hashtable bucket and |
| 1150 | * iterate to find the event_notifier matching this |
| 1151 | * descriptor. |
| 1152 | */ |
| 1153 | head = borrow_hash_table_bucket( |
| 1154 | event_notifier_group->event_notifiers_ht.table, |
| 1155 | LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, event_desc); |
| 1156 | |
| 1157 | cds_hlist_for_each_entry_safe(event_notifier_priv, node, tmp_node, head, hlist) { |
| 1158 | if (event_desc == event_notifier_priv->parent.desc) { |
| 1159 | event_func(event_notifier_priv->parent.pub); |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | static |
| 1168 | void _event_enum_destroy(struct lttng_ust_event_common *event) |
| 1169 | { |
| 1170 | |
| 1171 | switch (event->type) { |
| 1172 | case LTTNG_UST_EVENT_TYPE_RECORDER: |
| 1173 | { |
| 1174 | struct lttng_ust_event_recorder *event_recorder = event->child; |
| 1175 | struct lttng_ust_session *session = event_recorder->chan->parent->session; |
| 1176 | unsigned int i; |
| 1177 | |
| 1178 | /* Destroy enums of the current event. */ |
| 1179 | for (i = 0; i < event_recorder->parent->priv->desc->nr_fields; i++) { |
| 1180 | const struct lttng_ust_enum_desc *enum_desc; |
| 1181 | const struct lttng_ust_event_field *field; |
| 1182 | struct lttng_enum *curr_enum; |
| 1183 | |
| 1184 | field = event_recorder->parent->priv->desc->fields[i]; |
| 1185 | switch (field->type->type) { |
| 1186 | case lttng_ust_type_enum: |
| 1187 | enum_desc = lttng_ust_get_type_enum(field->type)->desc; |
| 1188 | break; |
| 1189 | default: |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
| 1193 | curr_enum = lttng_ust_enum_get_from_desc(session, enum_desc); |
| 1194 | if (curr_enum) { |
| 1195 | _lttng_enum_destroy(curr_enum); |
| 1196 | } |
| 1197 | } |
| 1198 | break; |
| 1199 | } |
| 1200 | case LTTNG_UST_EVENT_TYPE_NOTIFIER: |
| 1201 | break; |
| 1202 | default: |
| 1203 | abort(); |
| 1204 | } |
| 1205 | /* Destroy event. */ |
| 1206 | _lttng_event_destroy(event); |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Iterate over all the UST sessions to unregister and destroy all probes from |
| 1211 | * the probe provider descriptor received as argument. Must me called with the |
| 1212 | * ust_lock held. |
| 1213 | */ |
| 1214 | void lttng_probe_provider_unregister_events( |
| 1215 | const struct lttng_ust_probe_desc *provider_desc) |
| 1216 | { |
| 1217 | /* |
| 1218 | * Iterate over all events in the probe provider descriptions and sessions |
| 1219 | * to queue the unregistration of the events. |
| 1220 | */ |
| 1221 | probe_provider_event_for_each(provider_desc, _lttng_event_unregister); |
| 1222 | |
| 1223 | /* Wait for grace period. */ |
| 1224 | lttng_ust_urcu_synchronize_rcu(); |
| 1225 | /* Prune the unregistration queue. */ |
| 1226 | lttng_ust_tp_probe_prune_release_queue(); |
| 1227 | |
| 1228 | /* |
| 1229 | * It is now safe to destroy the events and remove them from the event list |
| 1230 | * and hashtables. |
| 1231 | */ |
| 1232 | probe_provider_event_for_each(provider_desc, _event_enum_destroy); |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Create events associated with an event enabler (if not already present), |
| 1237 | * and add backward reference from the event to the enabler. |
| 1238 | */ |
| 1239 | static |
| 1240 | int lttng_event_enabler_ref_event_recorders(struct lttng_event_enabler *event_enabler) |
| 1241 | { |
| 1242 | struct lttng_ust_session *session = event_enabler->chan->parent->session; |
| 1243 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 1244 | |
| 1245 | if (!lttng_event_enabler_as_enabler(event_enabler)->enabled) |
| 1246 | goto end; |
| 1247 | |
| 1248 | /* First ensure that probe events are created for this enabler. */ |
| 1249 | lttng_create_event_recorder_if_missing(event_enabler); |
| 1250 | |
| 1251 | /* For each event matching enabler in session event list. */ |
| 1252 | cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) { |
| 1253 | struct lttng_enabler_ref *enabler_ref; |
| 1254 | |
| 1255 | if (!lttng_event_enabler_match_event(event_enabler, event_recorder_priv->pub)) |
| 1256 | continue; |
| 1257 | |
| 1258 | enabler_ref = lttng_enabler_ref(&event_recorder_priv->parent.enablers_ref_head, |
| 1259 | lttng_event_enabler_as_enabler(event_enabler)); |
| 1260 | if (!enabler_ref) { |
| 1261 | /* |
| 1262 | * If no backward ref, create it. |
| 1263 | * Add backward ref from event to enabler. |
| 1264 | */ |
| 1265 | enabler_ref = zmalloc(sizeof(*enabler_ref)); |
| 1266 | if (!enabler_ref) |
| 1267 | return -ENOMEM; |
| 1268 | enabler_ref->ref = lttng_event_enabler_as_enabler( |
| 1269 | event_enabler); |
| 1270 | cds_list_add(&enabler_ref->node, |
| 1271 | &event_recorder_priv->parent.enablers_ref_head); |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Link filter bytecodes if not linked yet. |
| 1276 | */ |
| 1277 | lttng_enabler_link_bytecode(event_recorder_priv->parent.desc, |
| 1278 | &session->priv->ctx, |
| 1279 | &event_recorder_priv->parent.filter_bytecode_runtime_head, |
| 1280 | <tng_event_enabler_as_enabler(event_enabler)->filter_bytecode_head); |
| 1281 | |
| 1282 | /* TODO: merge event context. */ |
| 1283 | } |
| 1284 | end: |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Called at library load: connect the probe on all enablers matching |
| 1290 | * this event. |
| 1291 | * Called with session mutex held. |
| 1292 | */ |
| 1293 | int lttng_fix_pending_events(void) |
| 1294 | { |
| 1295 | struct lttng_ust_session_private *session_priv; |
| 1296 | |
| 1297 | cds_list_for_each_entry(session_priv, &sessions, node) { |
| 1298 | lttng_session_lazy_sync_event_enablers(session_priv->pub); |
| 1299 | } |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | int lttng_fix_pending_event_notifiers(void) |
| 1304 | { |
| 1305 | struct lttng_event_notifier_group *event_notifier_group; |
| 1306 | |
| 1307 | cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) { |
| 1308 | lttng_event_notifier_group_sync_enablers(event_notifier_group); |
| 1309 | } |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * For each session of the owner thread, execute pending statedump. |
| 1315 | * Only dump state for the sessions owned by the caller thread, because |
| 1316 | * we don't keep ust_lock across the entire iteration. |
| 1317 | */ |
| 1318 | void lttng_handle_pending_statedump(void *owner) |
| 1319 | { |
| 1320 | struct lttng_ust_session_private *session_priv; |
| 1321 | |
| 1322 | /* Execute state dump */ |
| 1323 | do_lttng_ust_statedump(owner); |
| 1324 | |
| 1325 | /* Clear pending state dump */ |
| 1326 | if (ust_lock()) { |
| 1327 | goto end; |
| 1328 | } |
| 1329 | cds_list_for_each_entry(session_priv, &sessions, node) { |
| 1330 | if (session_priv->owner != owner) |
| 1331 | continue; |
| 1332 | if (!session_priv->statedump_pending) |
| 1333 | continue; |
| 1334 | session_priv->statedump_pending = 0; |
| 1335 | } |
| 1336 | end: |
| 1337 | ust_unlock(); |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | static |
| 1342 | void _lttng_event_destroy(struct lttng_ust_event_common *event) |
| 1343 | { |
| 1344 | struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref; |
| 1345 | |
| 1346 | lttng_free_event_filter_runtime(event); |
| 1347 | /* Free event enabler refs */ |
| 1348 | cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref, |
| 1349 | &event->priv->enablers_ref_head, node) |
| 1350 | free(enabler_ref); |
| 1351 | |
| 1352 | switch (event->type) { |
| 1353 | case LTTNG_UST_EVENT_TYPE_RECORDER: |
| 1354 | { |
| 1355 | struct lttng_ust_event_recorder *event_recorder = event->child; |
| 1356 | |
| 1357 | /* Remove from event list. */ |
| 1358 | cds_list_del(&event_recorder->priv->node); |
| 1359 | /* Remove from event hash table. */ |
| 1360 | cds_hlist_del(&event_recorder->priv->hlist); |
| 1361 | |
| 1362 | lttng_destroy_context(event_recorder->priv->ctx); |
| 1363 | free(event_recorder->parent); |
| 1364 | free(event_recorder->priv); |
| 1365 | free(event_recorder); |
| 1366 | break; |
| 1367 | } |
| 1368 | case LTTNG_UST_EVENT_TYPE_NOTIFIER: |
| 1369 | { |
| 1370 | struct lttng_ust_event_notifier *event_notifier = event->child; |
| 1371 | |
| 1372 | /* Remove from event list. */ |
| 1373 | cds_list_del(&event_notifier->priv->node); |
| 1374 | /* Remove from event hash table. */ |
| 1375 | cds_hlist_del(&event_notifier->priv->hlist); |
| 1376 | |
| 1377 | free(event_notifier->priv); |
| 1378 | free(event_notifier->parent); |
| 1379 | free(event_notifier); |
| 1380 | break; |
| 1381 | } |
| 1382 | default: |
| 1383 | abort(); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | static |
| 1388 | void _lttng_enum_destroy(struct lttng_enum *_enum) |
| 1389 | { |
| 1390 | cds_list_del(&_enum->node); |
| 1391 | cds_hlist_del(&_enum->hlist); |
| 1392 | free(_enum); |
| 1393 | } |
| 1394 | |
| 1395 | void lttng_ust_abi_events_exit(void) |
| 1396 | { |
| 1397 | struct lttng_ust_session_private *session_priv, *tmpsession_priv; |
| 1398 | |
| 1399 | cds_list_for_each_entry_safe(session_priv, tmpsession_priv, &sessions, node) |
| 1400 | lttng_session_destroy(session_priv->pub); |
| 1401 | } |
| 1402 | |
| 1403 | /* |
| 1404 | * Enabler management. |
| 1405 | */ |
| 1406 | struct lttng_event_enabler *lttng_event_enabler_create( |
| 1407 | enum lttng_enabler_format_type format_type, |
| 1408 | struct lttng_ust_abi_event *event_param, |
| 1409 | struct lttng_ust_channel_buffer *chan) |
| 1410 | { |
| 1411 | struct lttng_event_enabler *event_enabler; |
| 1412 | |
| 1413 | event_enabler = zmalloc(sizeof(*event_enabler)); |
| 1414 | if (!event_enabler) |
| 1415 | return NULL; |
| 1416 | event_enabler->base.format_type = format_type; |
| 1417 | CDS_INIT_LIST_HEAD(&event_enabler->base.filter_bytecode_head); |
| 1418 | CDS_INIT_LIST_HEAD(&event_enabler->base.excluder_head); |
| 1419 | memcpy(&event_enabler->base.event_param, event_param, |
| 1420 | sizeof(event_enabler->base.event_param)); |
| 1421 | event_enabler->chan = chan; |
| 1422 | /* ctx left NULL */ |
| 1423 | event_enabler->base.enabled = 0; |
| 1424 | cds_list_add(&event_enabler->node, &event_enabler->chan->parent->session->priv->enablers_head); |
| 1425 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session); |
| 1426 | |
| 1427 | return event_enabler; |
| 1428 | } |
| 1429 | |
| 1430 | struct lttng_event_notifier_enabler *lttng_event_notifier_enabler_create( |
| 1431 | struct lttng_event_notifier_group *event_notifier_group, |
| 1432 | enum lttng_enabler_format_type format_type, |
| 1433 | struct lttng_ust_abi_event_notifier *event_notifier_param) |
| 1434 | { |
| 1435 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 1436 | |
| 1437 | event_notifier_enabler = zmalloc(sizeof(*event_notifier_enabler)); |
| 1438 | if (!event_notifier_enabler) |
| 1439 | return NULL; |
| 1440 | event_notifier_enabler->base.format_type = format_type; |
| 1441 | CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.filter_bytecode_head); |
| 1442 | CDS_INIT_LIST_HEAD(&event_notifier_enabler->capture_bytecode_head); |
| 1443 | CDS_INIT_LIST_HEAD(&event_notifier_enabler->base.excluder_head); |
| 1444 | |
| 1445 | event_notifier_enabler->user_token = event_notifier_param->event.token; |
| 1446 | event_notifier_enabler->error_counter_index = event_notifier_param->error_counter_index; |
| 1447 | event_notifier_enabler->num_captures = 0; |
| 1448 | |
| 1449 | memcpy(&event_notifier_enabler->base.event_param.name, |
| 1450 | event_notifier_param->event.name, |
| 1451 | sizeof(event_notifier_enabler->base.event_param.name)); |
| 1452 | event_notifier_enabler->base.event_param.instrumentation = |
| 1453 | event_notifier_param->event.instrumentation; |
| 1454 | event_notifier_enabler->base.event_param.loglevel = |
| 1455 | event_notifier_param->event.loglevel; |
| 1456 | event_notifier_enabler->base.event_param.loglevel_type = |
| 1457 | event_notifier_param->event.loglevel_type; |
| 1458 | |
| 1459 | event_notifier_enabler->base.enabled = 0; |
| 1460 | event_notifier_enabler->group = event_notifier_group; |
| 1461 | |
| 1462 | cds_list_add(&event_notifier_enabler->node, |
| 1463 | &event_notifier_group->enablers_head); |
| 1464 | |
| 1465 | lttng_event_notifier_group_sync_enablers(event_notifier_group); |
| 1466 | |
| 1467 | return event_notifier_enabler; |
| 1468 | } |
| 1469 | |
| 1470 | int lttng_event_enabler_enable(struct lttng_event_enabler *event_enabler) |
| 1471 | { |
| 1472 | lttng_event_enabler_as_enabler(event_enabler)->enabled = 1; |
| 1473 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session); |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler) |
| 1479 | { |
| 1480 | lttng_event_enabler_as_enabler(event_enabler)->enabled = 0; |
| 1481 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session); |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static |
| 1487 | void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler *enabler, |
| 1488 | struct lttng_ust_bytecode_node **bytecode) |
| 1489 | { |
| 1490 | (*bytecode)->enabler = enabler; |
| 1491 | cds_list_add_tail(&(*bytecode)->node, &enabler->filter_bytecode_head); |
| 1492 | /* Take ownership of bytecode */ |
| 1493 | *bytecode = NULL; |
| 1494 | } |
| 1495 | |
| 1496 | int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler *event_enabler, |
| 1497 | struct lttng_ust_bytecode_node **bytecode) |
| 1498 | { |
| 1499 | _lttng_enabler_attach_filter_bytecode( |
| 1500 | lttng_event_enabler_as_enabler(event_enabler), bytecode); |
| 1501 | |
| 1502 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session); |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | static |
| 1507 | void _lttng_enabler_attach_exclusion(struct lttng_enabler *enabler, |
| 1508 | struct lttng_ust_excluder_node **excluder) |
| 1509 | { |
| 1510 | (*excluder)->enabler = enabler; |
| 1511 | cds_list_add_tail(&(*excluder)->node, &enabler->excluder_head); |
| 1512 | /* Take ownership of excluder */ |
| 1513 | *excluder = NULL; |
| 1514 | } |
| 1515 | |
| 1516 | int lttng_event_enabler_attach_exclusion(struct lttng_event_enabler *event_enabler, |
| 1517 | struct lttng_ust_excluder_node **excluder) |
| 1518 | { |
| 1519 | _lttng_enabler_attach_exclusion( |
| 1520 | lttng_event_enabler_as_enabler(event_enabler), excluder); |
| 1521 | |
| 1522 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->parent->session); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | int lttng_event_notifier_enabler_enable( |
| 1527 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 1528 | { |
| 1529 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 1; |
| 1530 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | int lttng_event_notifier_enabler_disable( |
| 1536 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 1537 | { |
| 1538 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 0; |
| 1539 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | int lttng_event_notifier_enabler_attach_filter_bytecode( |
| 1545 | struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 1546 | struct lttng_ust_bytecode_node **bytecode) |
| 1547 | { |
| 1548 | _lttng_enabler_attach_filter_bytecode( |
| 1549 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler), |
| 1550 | bytecode); |
| 1551 | |
| 1552 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | int lttng_event_notifier_enabler_attach_capture_bytecode( |
| 1557 | struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 1558 | struct lttng_ust_bytecode_node **bytecode) |
| 1559 | { |
| 1560 | (*bytecode)->enabler = lttng_event_notifier_enabler_as_enabler( |
| 1561 | event_notifier_enabler); |
| 1562 | cds_list_add_tail(&(*bytecode)->node, |
| 1563 | &event_notifier_enabler->capture_bytecode_head); |
| 1564 | /* Take ownership of bytecode */ |
| 1565 | *bytecode = NULL; |
| 1566 | event_notifier_enabler->num_captures++; |
| 1567 | |
| 1568 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | int lttng_event_notifier_enabler_attach_exclusion( |
| 1573 | struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 1574 | struct lttng_ust_excluder_node **excluder) |
| 1575 | { |
| 1576 | _lttng_enabler_attach_exclusion( |
| 1577 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler), |
| 1578 | excluder); |
| 1579 | |
| 1580 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | int lttng_attach_context(struct lttng_ust_abi_context *context_param, |
| 1585 | union lttng_ust_abi_args *uargs, |
| 1586 | struct lttng_ust_ctx **ctx, struct lttng_ust_session *session) |
| 1587 | { |
| 1588 | /* |
| 1589 | * We cannot attach a context after trace has been started for a |
| 1590 | * session because the metadata does not allow expressing this |
| 1591 | * information outside of the original channel scope. |
| 1592 | */ |
| 1593 | if (session->priv->been_active) |
| 1594 | return -EPERM; |
| 1595 | |
| 1596 | switch (context_param->ctx) { |
| 1597 | case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID: |
| 1598 | return lttng_add_pthread_id_to_ctx(ctx); |
| 1599 | case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER: |
| 1600 | { |
| 1601 | struct lttng_ust_abi_perf_counter_ctx *perf_ctx_param; |
| 1602 | |
| 1603 | perf_ctx_param = &context_param->u.perf_counter; |
| 1604 | return lttng_add_perf_counter_to_ctx( |
| 1605 | perf_ctx_param->type, |
| 1606 | perf_ctx_param->config, |
| 1607 | perf_ctx_param->name, |
| 1608 | ctx); |
| 1609 | } |
| 1610 | case LTTNG_UST_ABI_CONTEXT_VTID: |
| 1611 | return lttng_add_vtid_to_ctx(ctx); |
| 1612 | case LTTNG_UST_ABI_CONTEXT_VPID: |
| 1613 | return lttng_add_vpid_to_ctx(ctx); |
| 1614 | case LTTNG_UST_ABI_CONTEXT_PROCNAME: |
| 1615 | return lttng_add_procname_to_ctx(ctx); |
| 1616 | case LTTNG_UST_ABI_CONTEXT_IP: |
| 1617 | return lttng_add_ip_to_ctx(ctx); |
| 1618 | case LTTNG_UST_ABI_CONTEXT_CPU_ID: |
| 1619 | return lttng_add_cpu_id_to_ctx(ctx); |
| 1620 | case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT: |
| 1621 | return lttng_ust_add_app_context_to_ctx_rcu(uargs->app_context.ctxname, |
| 1622 | ctx); |
| 1623 | case LTTNG_UST_ABI_CONTEXT_CGROUP_NS: |
| 1624 | return lttng_add_cgroup_ns_to_ctx(ctx); |
| 1625 | case LTTNG_UST_ABI_CONTEXT_IPC_NS: |
| 1626 | return lttng_add_ipc_ns_to_ctx(ctx); |
| 1627 | case LTTNG_UST_ABI_CONTEXT_MNT_NS: |
| 1628 | return lttng_add_mnt_ns_to_ctx(ctx); |
| 1629 | case LTTNG_UST_ABI_CONTEXT_NET_NS: |
| 1630 | return lttng_add_net_ns_to_ctx(ctx); |
| 1631 | case LTTNG_UST_ABI_CONTEXT_PID_NS: |
| 1632 | return lttng_add_pid_ns_to_ctx(ctx); |
| 1633 | case LTTNG_UST_ABI_CONTEXT_TIME_NS: |
| 1634 | return lttng_add_time_ns_to_ctx(ctx); |
| 1635 | case LTTNG_UST_ABI_CONTEXT_USER_NS: |
| 1636 | return lttng_add_user_ns_to_ctx(ctx); |
| 1637 | case LTTNG_UST_ABI_CONTEXT_UTS_NS: |
| 1638 | return lttng_add_uts_ns_to_ctx(ctx); |
| 1639 | case LTTNG_UST_ABI_CONTEXT_VUID: |
| 1640 | return lttng_add_vuid_to_ctx(ctx); |
| 1641 | case LTTNG_UST_ABI_CONTEXT_VEUID: |
| 1642 | return lttng_add_veuid_to_ctx(ctx); |
| 1643 | case LTTNG_UST_ABI_CONTEXT_VSUID: |
| 1644 | return lttng_add_vsuid_to_ctx(ctx); |
| 1645 | case LTTNG_UST_ABI_CONTEXT_VGID: |
| 1646 | return lttng_add_vgid_to_ctx(ctx); |
| 1647 | case LTTNG_UST_ABI_CONTEXT_VEGID: |
| 1648 | return lttng_add_vegid_to_ctx(ctx); |
| 1649 | case LTTNG_UST_ABI_CONTEXT_VSGID: |
| 1650 | return lttng_add_vsgid_to_ctx(ctx); |
| 1651 | default: |
| 1652 | return -EINVAL; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | int lttng_event_enabler_attach_context( |
| 1657 | struct lttng_event_enabler *enabler __attribute__((unused)), |
| 1658 | struct lttng_ust_abi_context *context_param __attribute__((unused))) |
| 1659 | { |
| 1660 | return -ENOSYS; |
| 1661 | } |
| 1662 | |
| 1663 | void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler) |
| 1664 | { |
| 1665 | if (!event_enabler) { |
| 1666 | return; |
| 1667 | } |
| 1668 | cds_list_del(&event_enabler->node); |
| 1669 | |
| 1670 | lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler)); |
| 1671 | |
| 1672 | lttng_destroy_context(event_enabler->ctx); |
| 1673 | free(event_enabler); |
| 1674 | } |
| 1675 | |
| 1676 | /* |
| 1677 | * lttng_session_sync_event_enablers should be called just before starting a |
| 1678 | * session. |
| 1679 | */ |
| 1680 | static |
| 1681 | void lttng_session_sync_event_enablers(struct lttng_ust_session *session) |
| 1682 | { |
| 1683 | struct lttng_event_enabler *event_enabler; |
| 1684 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 1685 | |
| 1686 | cds_list_for_each_entry(event_enabler, &session->priv->enablers_head, node) |
| 1687 | lttng_event_enabler_ref_event_recorders(event_enabler); |
| 1688 | /* |
| 1689 | * For each event, if at least one of its enablers is enabled, |
| 1690 | * and its channel and session transient states are enabled, we |
| 1691 | * enable the event, else we disable it. |
| 1692 | */ |
| 1693 | cds_list_for_each_entry(event_recorder_priv, &session->priv->events_head, node) { |
| 1694 | struct lttng_enabler_ref *enabler_ref; |
| 1695 | struct lttng_ust_bytecode_runtime *runtime; |
| 1696 | int enabled = 0, has_enablers_without_filter_bytecode = 0; |
| 1697 | int nr_filters = 0; |
| 1698 | |
| 1699 | /* Enable events */ |
| 1700 | cds_list_for_each_entry(enabler_ref, |
| 1701 | &event_recorder_priv->parent.enablers_ref_head, node) { |
| 1702 | if (enabler_ref->ref->enabled) { |
| 1703 | enabled = 1; |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | /* |
| 1708 | * Enabled state is based on union of enablers, with |
| 1709 | * intesection of session and channel transient enable |
| 1710 | * states. |
| 1711 | */ |
| 1712 | enabled = enabled && session->priv->tstate && event_recorder_priv->pub->chan->priv->parent.tstate; |
| 1713 | |
| 1714 | CMM_STORE_SHARED(event_recorder_priv->pub->parent->enabled, enabled); |
| 1715 | /* |
| 1716 | * Sync tracepoint registration with event enabled |
| 1717 | * state. |
| 1718 | */ |
| 1719 | if (enabled) { |
| 1720 | if (!event_recorder_priv->parent.registered) |
| 1721 | register_event(event_recorder_priv->parent.pub); |
| 1722 | } else { |
| 1723 | if (event_recorder_priv->parent.registered) |
| 1724 | unregister_event(event_recorder_priv->parent.pub); |
| 1725 | } |
| 1726 | |
| 1727 | /* Check if has enablers without bytecode enabled */ |
| 1728 | cds_list_for_each_entry(enabler_ref, |
| 1729 | &event_recorder_priv->parent.enablers_ref_head, node) { |
| 1730 | if (enabler_ref->ref->enabled |
| 1731 | && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 1732 | has_enablers_without_filter_bytecode = 1; |
| 1733 | break; |
| 1734 | } |
| 1735 | } |
| 1736 | event_recorder_priv->parent.has_enablers_without_filter_bytecode = |
| 1737 | has_enablers_without_filter_bytecode; |
| 1738 | |
| 1739 | /* Enable filters */ |
| 1740 | cds_list_for_each_entry(runtime, |
| 1741 | &event_recorder_priv->parent.filter_bytecode_runtime_head, node) { |
| 1742 | lttng_bytecode_sync_state(runtime); |
| 1743 | nr_filters++; |
| 1744 | } |
| 1745 | CMM_STORE_SHARED(event_recorder_priv->parent.pub->eval_filter, |
| 1746 | !(has_enablers_without_filter_bytecode || !nr_filters)); |
| 1747 | } |
| 1748 | lttng_ust_tp_probe_prune_release_queue(); |
| 1749 | } |
| 1750 | |
| 1751 | /* Support for event notifier is introduced by probe provider major version 2. */ |
| 1752 | static |
| 1753 | bool lttng_ust_probe_supports_event_notifier(const struct lttng_ust_probe_desc *probe_desc) |
| 1754 | { |
| 1755 | return probe_desc->major >= 2; |
| 1756 | } |
| 1757 | |
| 1758 | static |
| 1759 | void lttng_create_event_notifier_if_missing( |
| 1760 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 1761 | { |
| 1762 | struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group; |
| 1763 | struct lttng_ust_registered_probe *reg_probe; |
| 1764 | struct cds_list_head *probe_list; |
| 1765 | int i; |
| 1766 | |
| 1767 | probe_list = lttng_get_probe_list_head(); |
| 1768 | |
| 1769 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
| 1770 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; |
| 1771 | |
| 1772 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 1773 | int ret; |
| 1774 | bool found = false; |
| 1775 | const struct lttng_ust_event_desc *desc; |
| 1776 | struct lttng_ust_event_notifier_private *event_notifier_priv; |
| 1777 | struct cds_hlist_head *head; |
| 1778 | struct cds_hlist_node *node; |
| 1779 | |
| 1780 | desc = probe_desc->event_desc[i]; |
| 1781 | |
| 1782 | if (!lttng_desc_match_enabler(desc, |
| 1783 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler))) |
| 1784 | continue; |
| 1785 | |
| 1786 | /* |
| 1787 | * Given the current event_notifier group, get the bucket that |
| 1788 | * the target event_notifier would be if it was already |
| 1789 | * created. |
| 1790 | */ |
| 1791 | head = borrow_hash_table_bucket( |
| 1792 | event_notifier_group->event_notifiers_ht.table, |
| 1793 | LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc); |
| 1794 | |
| 1795 | cds_hlist_for_each_entry(event_notifier_priv, node, head, hlist) { |
| 1796 | /* |
| 1797 | * Check if event_notifier already exists by checking |
| 1798 | * if the event_notifier and enabler share the same |
| 1799 | * description and id. |
| 1800 | */ |
| 1801 | if (event_notifier_priv->parent.desc == desc && |
| 1802 | event_notifier_priv->parent.user_token == event_notifier_enabler->user_token) { |
| 1803 | found = true; |
| 1804 | break; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | if (found) |
| 1809 | continue; |
| 1810 | |
| 1811 | /* Check that the probe supports event notifiers, else report the error. */ |
| 1812 | if (!lttng_ust_probe_supports_event_notifier(probe_desc)) { |
| 1813 | ERR("Probe \"%s\" contains event \"%s:%s\" which matches an enabled event notifier, " |
| 1814 | "but its version (%u.%u) is too old and does not implement event notifiers. " |
| 1815 | "It needs to be recompiled against a newer version of LTTng-UST, otherwise " |
| 1816 | "this event will not generate any notification.", |
| 1817 | probe_desc->provider_name, |
| 1818 | probe_desc->provider_name, desc->event_name, |
| 1819 | probe_desc->major, |
| 1820 | probe_desc->minor); |
| 1821 | continue; |
| 1822 | } |
| 1823 | /* |
| 1824 | * We need to create a event_notifier for this event probe. |
| 1825 | */ |
| 1826 | ret = lttng_event_notifier_create(desc, |
| 1827 | event_notifier_enabler->user_token, |
| 1828 | event_notifier_enabler->error_counter_index, |
| 1829 | event_notifier_group); |
| 1830 | if (ret) { |
| 1831 | DBG("Unable to create event_notifier \"%s:%s\", error %d\n", |
| 1832 | probe_desc->provider_name, |
| 1833 | probe_desc->event_desc[i]->event_name, ret); |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Create event_notifiers associated with a event_notifier enabler (if not already present). |
| 1841 | */ |
| 1842 | static |
| 1843 | int lttng_event_notifier_enabler_ref_event_notifiers( |
| 1844 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 1845 | { |
| 1846 | struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group; |
| 1847 | struct lttng_ust_event_notifier_private *event_notifier_priv; |
| 1848 | |
| 1849 | /* |
| 1850 | * Only try to create event_notifiers for enablers that are enabled, the user |
| 1851 | * might still be attaching filter or exclusion to the |
| 1852 | * event_notifier_enabler. |
| 1853 | */ |
| 1854 | if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled) |
| 1855 | goto end; |
| 1856 | |
| 1857 | /* First, ensure that probe event_notifiers are created for this enabler. */ |
| 1858 | lttng_create_event_notifier_if_missing(event_notifier_enabler); |
| 1859 | |
| 1860 | /* Link the created event_notifier with its associated enabler. */ |
| 1861 | cds_list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) { |
| 1862 | struct lttng_enabler_ref *enabler_ref; |
| 1863 | |
| 1864 | if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler, event_notifier_priv->pub)) |
| 1865 | continue; |
| 1866 | |
| 1867 | enabler_ref = lttng_enabler_ref(&event_notifier_priv->parent.enablers_ref_head, |
| 1868 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)); |
| 1869 | if (!enabler_ref) { |
| 1870 | /* |
| 1871 | * If no backward ref, create it. |
| 1872 | * Add backward ref from event_notifier to enabler. |
| 1873 | */ |
| 1874 | enabler_ref = zmalloc(sizeof(*enabler_ref)); |
| 1875 | if (!enabler_ref) |
| 1876 | return -ENOMEM; |
| 1877 | |
| 1878 | enabler_ref->ref = lttng_event_notifier_enabler_as_enabler( |
| 1879 | event_notifier_enabler); |
| 1880 | cds_list_add(&enabler_ref->node, |
| 1881 | &event_notifier_priv->parent.enablers_ref_head); |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Link filter bytecodes if not linked yet. |
| 1886 | */ |
| 1887 | lttng_enabler_link_bytecode(event_notifier_priv->parent.desc, |
| 1888 | &event_notifier_group->ctx, |
| 1889 | &event_notifier_priv->parent.filter_bytecode_runtime_head, |
| 1890 | <tng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head); |
| 1891 | |
| 1892 | /* |
| 1893 | * Link capture bytecodes if not linked yet. |
| 1894 | */ |
| 1895 | lttng_enabler_link_bytecode(event_notifier_priv->parent.desc, |
| 1896 | &event_notifier_group->ctx, &event_notifier_priv->capture_bytecode_runtime_head, |
| 1897 | &event_notifier_enabler->capture_bytecode_head); |
| 1898 | |
| 1899 | event_notifier_priv->num_captures = event_notifier_enabler->num_captures; |
| 1900 | } |
| 1901 | end: |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | static |
| 1906 | void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group) |
| 1907 | { |
| 1908 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 1909 | struct lttng_ust_event_notifier_private *event_notifier_priv; |
| 1910 | |
| 1911 | cds_list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node) |
| 1912 | lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler); |
| 1913 | |
| 1914 | /* |
| 1915 | * For each event_notifier, if at least one of its enablers is enabled, |
| 1916 | * we enable the event_notifier, else we disable it. |
| 1917 | */ |
| 1918 | cds_list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) { |
| 1919 | struct lttng_enabler_ref *enabler_ref; |
| 1920 | struct lttng_ust_bytecode_runtime *runtime; |
| 1921 | int enabled = 0, has_enablers_without_filter_bytecode = 0; |
| 1922 | int nr_filters = 0, nr_captures = 0; |
| 1923 | |
| 1924 | /* Enable event_notifiers */ |
| 1925 | cds_list_for_each_entry(enabler_ref, |
| 1926 | &event_notifier_priv->parent.enablers_ref_head, node) { |
| 1927 | if (enabler_ref->ref->enabled) { |
| 1928 | enabled = 1; |
| 1929 | break; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | CMM_STORE_SHARED(event_notifier_priv->pub->parent->enabled, enabled); |
| 1934 | /* |
| 1935 | * Sync tracepoint registration with event_notifier enabled |
| 1936 | * state. |
| 1937 | */ |
| 1938 | if (enabled) { |
| 1939 | if (!event_notifier_priv->parent.registered) |
| 1940 | register_event(event_notifier_priv->parent.pub); |
| 1941 | } else { |
| 1942 | if (event_notifier_priv->parent.registered) |
| 1943 | unregister_event(event_notifier_priv->parent.pub); |
| 1944 | } |
| 1945 | |
| 1946 | /* Check if has enablers without bytecode enabled */ |
| 1947 | cds_list_for_each_entry(enabler_ref, |
| 1948 | &event_notifier_priv->parent.enablers_ref_head, node) { |
| 1949 | if (enabler_ref->ref->enabled |
| 1950 | && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 1951 | has_enablers_without_filter_bytecode = 1; |
| 1952 | break; |
| 1953 | } |
| 1954 | } |
| 1955 | event_notifier_priv->parent.has_enablers_without_filter_bytecode = |
| 1956 | has_enablers_without_filter_bytecode; |
| 1957 | |
| 1958 | /* Enable filters */ |
| 1959 | cds_list_for_each_entry(runtime, |
| 1960 | &event_notifier_priv->parent.filter_bytecode_runtime_head, node) { |
| 1961 | lttng_bytecode_sync_state(runtime); |
| 1962 | nr_filters++; |
| 1963 | } |
| 1964 | CMM_STORE_SHARED(event_notifier_priv->parent.pub->eval_filter, |
| 1965 | !(has_enablers_without_filter_bytecode || !nr_filters)); |
| 1966 | |
| 1967 | /* Enable captures. */ |
| 1968 | cds_list_for_each_entry(runtime, |
| 1969 | &event_notifier_priv->capture_bytecode_runtime_head, node) { |
| 1970 | lttng_bytecode_sync_state(runtime); |
| 1971 | nr_captures++; |
| 1972 | } |
| 1973 | CMM_STORE_SHARED(event_notifier_priv->pub->eval_capture, |
| 1974 | !!nr_captures); |
| 1975 | } |
| 1976 | lttng_ust_tp_probe_prune_release_queue(); |
| 1977 | } |
| 1978 | |
| 1979 | /* |
| 1980 | * Apply enablers to session events, adding events to session if need |
| 1981 | * be. It is required after each modification applied to an active |
| 1982 | * session, and right before session "start". |
| 1983 | * "lazy" sync means we only sync if required. |
| 1984 | */ |
| 1985 | static |
| 1986 | void lttng_session_lazy_sync_event_enablers(struct lttng_ust_session *session) |
| 1987 | { |
| 1988 | /* We can skip if session is not active */ |
| 1989 | if (!session->active) |
| 1990 | return; |
| 1991 | lttng_session_sync_event_enablers(session); |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * Update all sessions with the given app context. |
| 1996 | * Called with ust lock held. |
| 1997 | * This is invoked when an application context gets loaded/unloaded. It |
| 1998 | * ensures the context callbacks are in sync with the application |
| 1999 | * context (either app context callbacks, or dummy callbacks). |
| 2000 | */ |
| 2001 | void lttng_ust_context_set_session_provider(const char *name, |
| 2002 | size_t (*get_size)(void *priv, size_t offset), |
| 2003 | void (*record)(void *priv, struct lttng_ust_ring_buffer_ctx *ctx, |
| 2004 | struct lttng_ust_channel_buffer *chan), |
| 2005 | void (*get_value)(void *priv, struct lttng_ust_ctx_value *value), |
| 2006 | void *priv) |
| 2007 | { |
| 2008 | struct lttng_ust_session_private *session_priv; |
| 2009 | |
| 2010 | cds_list_for_each_entry(session_priv, &sessions, node) { |
| 2011 | struct lttng_ust_channel_buffer_private *chan; |
| 2012 | struct lttng_ust_event_recorder_private *event_recorder_priv; |
| 2013 | int ret; |
| 2014 | |
| 2015 | ret = lttng_ust_context_set_provider_rcu(&session_priv->ctx, |
| 2016 | name, get_size, record, get_value, priv); |
| 2017 | if (ret) |
| 2018 | abort(); |
| 2019 | cds_list_for_each_entry(chan, &session_priv->chan_head, node) { |
| 2020 | ret = lttng_ust_context_set_provider_rcu(&chan->ctx, |
| 2021 | name, get_size, record, get_value, priv); |
| 2022 | if (ret) |
| 2023 | abort(); |
| 2024 | } |
| 2025 | cds_list_for_each_entry(event_recorder_priv, &session_priv->events_head, node) { |
| 2026 | ret = lttng_ust_context_set_provider_rcu(&event_recorder_priv->ctx, |
| 2027 | name, get_size, record, get_value, priv); |
| 2028 | if (ret) |
| 2029 | abort(); |
| 2030 | } |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * Update all event_notifier groups with the given app context. |
| 2036 | * Called with ust lock held. |
| 2037 | * This is invoked when an application context gets loaded/unloaded. It |
| 2038 | * ensures the context callbacks are in sync with the application |
| 2039 | * context (either app context callbacks, or dummy callbacks). |
| 2040 | */ |
| 2041 | void lttng_ust_context_set_event_notifier_group_provider(const char *name, |
| 2042 | size_t (*get_size)(void *priv, size_t offset), |
| 2043 | void (*record)(void *priv, struct lttng_ust_ring_buffer_ctx *ctx, |
| 2044 | struct lttng_ust_channel_buffer *chan), |
| 2045 | void (*get_value)(void *priv, struct lttng_ust_ctx_value *value), |
| 2046 | void *priv) |
| 2047 | { |
| 2048 | struct lttng_event_notifier_group *event_notifier_group; |
| 2049 | |
| 2050 | cds_list_for_each_entry(event_notifier_group, &event_notifier_groups, node) { |
| 2051 | int ret; |
| 2052 | |
| 2053 | ret = lttng_ust_context_set_provider_rcu( |
| 2054 | &event_notifier_group->ctx, |
| 2055 | name, get_size, record, get_value, priv); |
| 2056 | if (ret) |
| 2057 | abort(); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | int lttng_ust_session_uuid_validate(struct lttng_ust_session *session, |
| 2062 | unsigned char *uuid) |
| 2063 | { |
| 2064 | if (!session) |
| 2065 | return 0; |
| 2066 | /* Compare UUID with session. */ |
| 2067 | if (session->priv->uuid_set) { |
| 2068 | if (memcmp(session->priv->uuid, uuid, LTTNG_UST_UUID_LEN)) { |
| 2069 | return -1; |
| 2070 | } |
| 2071 | } else { |
| 2072 | memcpy(session->priv->uuid, uuid, LTTNG_UST_UUID_LEN); |
| 2073 | session->priv->uuid_set = true; |
| 2074 | } |
| 2075 | return 0; |
| 2076 | |
| 2077 | } |