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