| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | |
| 11 | #include "buffer-registry.hpp" |
| 12 | #include "condition-internal.hpp" |
| 13 | #include "event-notifier-error-accounting.hpp" |
| 14 | #include "event.hpp" |
| 15 | #include "fd-limit.hpp" |
| 16 | #include "field.hpp" |
| 17 | #include "health-sessiond.hpp" |
| 18 | #include "lttng-sessiond.hpp" |
| 19 | #include "lttng-ust-ctl.hpp" |
| 20 | #include "lttng-ust-error.hpp" |
| 21 | #include "notification-thread-commands.hpp" |
| 22 | #include "session.hpp" |
| 23 | #include "ust-app.hpp" |
| 24 | #include "ust-consumer.hpp" |
| 25 | #include "ust-field-quirks.hpp" |
| 26 | #include "utils.hpp" |
| 27 | |
| 28 | #include <common/bytecode/bytecode.hpp> |
| 29 | #include <common/common.hpp> |
| 30 | #include <common/compat/errno.hpp> |
| 31 | #include <common/exception.hpp> |
| 32 | #include <common/format.hpp> |
| 33 | #include <common/hashtable/utils.hpp> |
| 34 | #include <common/make-unique.hpp> |
| 35 | #include <common/pthread-lock.hpp> |
| 36 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 37 | #include <common/urcu.hpp> |
| 38 | |
| 39 | #include <lttng/condition/condition.h> |
| 40 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 41 | #include <lttng/condition/event-rule-matches.h> |
| 42 | #include <lttng/event-rule/event-rule-internal.hpp> |
| 43 | #include <lttng/event-rule/event-rule.h> |
| 44 | #include <lttng/event-rule/user-tracepoint.h> |
| 45 | #include <lttng/trigger/trigger-internal.hpp> |
| 46 | |
| 47 | #include <errno.h> |
| 48 | #include <fcntl.h> |
| 49 | #include <inttypes.h> |
| 50 | #include <pthread.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #include <sys/mman.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <sys/types.h> |
| 58 | #include <unistd.h> |
| 59 | #include <urcu/compiler.h> |
| 60 | #include <vector> |
| 61 | |
| 62 | namespace lsu = lttng::sessiond::ust; |
| 63 | namespace lst = lttng::sessiond::trace; |
| 64 | |
| 65 | struct lttng_ht *ust_app_ht; |
| 66 | struct lttng_ht *ust_app_ht_by_sock; |
| 67 | struct lttng_ht *ust_app_ht_by_notify_sock; |
| 68 | |
| 69 | static int ust_app_flush_app_session(ust_app& app, ust_app_session& ua_sess); |
| 70 | |
| 71 | /* Next available channel key. Access under next_channel_key_lock. */ |
| 72 | static uint64_t _next_channel_key; |
| 73 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; |
| 74 | |
| 75 | /* Next available session ID. Access under next_session_id_lock. */ |
| 76 | static uint64_t _next_session_id; |
| 77 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 78 | |
| 79 | /* |
| 80 | * Return the session registry according to the buffer type of the given |
| 81 | * session. |
| 82 | * |
| 83 | * A registry per UID object MUST exists before calling this function or else |
| 84 | * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired. |
| 85 | */ |
| 86 | lsu::registry_session *ust_app_get_session_registry(const ust_app_session::identifier& ua_sess_id) |
| 87 | { |
| 88 | lsu::registry_session *registry = nullptr; |
| 89 | |
| 90 | switch (ua_sess_id.allocation_policy) { |
| 91 | case ust_app_session::identifier::buffer_allocation_policy::PER_PID: |
| 92 | { |
| 93 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess_id.id); |
| 94 | if (!reg_pid) { |
| 95 | goto error; |
| 96 | } |
| 97 | registry = reg_pid->registry->reg.ust; |
| 98 | break; |
| 99 | } |
| 100 | case ust_app_session::identifier::buffer_allocation_policy::PER_UID: |
| 101 | { |
| 102 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( |
| 103 | ua_sess_id.session_id, |
| 104 | ua_sess_id.abi == ust_app_session::identifier::application_abi::ABI_32 ? |
| 105 | 32 : |
| 106 | 64, |
| 107 | lttng_credentials_get_uid(&ua_sess_id.app_credentials)); |
| 108 | if (!reg_uid) { |
| 109 | goto error; |
| 110 | } |
| 111 | registry = reg_uid->registry->reg.ust; |
| 112 | break; |
| 113 | } |
| 114 | default: |
| 115 | abort(); |
| 116 | }; |
| 117 | |
| 118 | error: |
| 119 | return registry; |
| 120 | } |
| 121 | |
| 122 | namespace { |
| 123 | lsu::registry_session::locked_ref |
| 124 | get_locked_session_registry(const ust_app_session::identifier& identifier) |
| 125 | { |
| 126 | auto session = ust_app_get_session_registry(identifier); |
| 127 | if (session) { |
| 128 | pthread_mutex_lock(&session->_lock); |
| 129 | } |
| 130 | |
| 131 | return lsu::registry_session::locked_ref{ session }; |
| 132 | } |
| 133 | } /* namespace */ |
| 134 | |
| 135 | /* |
| 136 | * Return the incremented value of next_channel_key. |
| 137 | */ |
| 138 | static uint64_t get_next_channel_key() |
| 139 | { |
| 140 | uint64_t ret; |
| 141 | |
| 142 | pthread_mutex_lock(&next_channel_key_lock); |
| 143 | ret = ++_next_channel_key; |
| 144 | pthread_mutex_unlock(&next_channel_key_lock); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Return the atomically incremented value of next_session_id. |
| 150 | */ |
| 151 | static uint64_t get_next_session_id() |
| 152 | { |
| 153 | uint64_t ret; |
| 154 | |
| 155 | pthread_mutex_lock(&next_session_id_lock); |
| 156 | ret = ++_next_session_id; |
| 157 | pthread_mutex_unlock(&next_session_id_lock); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr *attr, |
| 162 | struct lttng_ust_abi_channel_attr *uattr) |
| 163 | { |
| 164 | /* Copy event attributes since the layout is different. */ |
| 165 | attr->subbuf_size = uattr->subbuf_size; |
| 166 | attr->num_subbuf = uattr->num_subbuf; |
| 167 | attr->overwrite = uattr->overwrite; |
| 168 | attr->switch_timer_interval = uattr->switch_timer_interval; |
| 169 | attr->read_timer_interval = uattr->read_timer_interval; |
| 170 | attr->output = (lttng_ust_abi_output) uattr->output; |
| 171 | attr->blocking_timeout = uattr->u.s.blocking_timeout; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Match function for the hash table lookup. |
| 176 | * |
| 177 | * It matches an ust app event based on three attributes which are the event |
| 178 | * name, the filter bytecode and the loglevel. |
| 179 | */ |
| 180 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
| 181 | { |
| 182 | LTTNG_ASSERT(node); |
| 183 | LTTNG_ASSERT(_key); |
| 184 | |
| 185 | auto *event = lttng_ht_node_container_of(node, &ust_app_event::node); |
| 186 | const auto *key = (ust_app_ht_key *) _key; |
| 187 | |
| 188 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
| 189 | |
| 190 | /* Event name */ |
| 191 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { |
| 192 | goto no_match; |
| 193 | } |
| 194 | |
| 195 | /* Event loglevel. */ |
| 196 | if (!loglevels_match(event->attr.loglevel_type, |
| 197 | event->attr.loglevel, |
| 198 | key->loglevel_type, |
| 199 | key->loglevel_value, |
| 200 | LTTNG_UST_ABI_LOGLEVEL_ALL)) { |
| 201 | goto no_match; |
| 202 | } |
| 203 | |
| 204 | /* One of the filters is NULL, fail. */ |
| 205 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { |
| 206 | goto no_match; |
| 207 | } |
| 208 | |
| 209 | if (key->filter && event->filter) { |
| 210 | /* Both filters exists, check length followed by the bytecode. */ |
| 211 | if (event->filter->len != key->filter->len || |
| 212 | memcmp(event->filter->data, key->filter->data, event->filter->len) != 0) { |
| 213 | goto no_match; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* One of the exclusions is NULL, fail. */ |
| 218 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { |
| 219 | goto no_match; |
| 220 | } |
| 221 | |
| 222 | if (key->exclusion && event->exclusion) { |
| 223 | /* Both exclusions exists, check count followed by the names. */ |
| 224 | if (event->exclusion->count != key->exclusion->count || |
| 225 | memcmp(event->exclusion->names, |
| 226 | key->exclusion->names, |
| 227 | event->exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) != 0) { |
| 228 | goto no_match; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* Match. */ |
| 233 | return 1; |
| 234 | |
| 235 | no_match: |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Unique add of an ust app event in the given ht. This uses the custom |
| 241 | * ht_match_ust_app_event match function and the event name as hash. |
| 242 | */ |
| 243 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, struct ust_app_event *event) |
| 244 | { |
| 245 | struct cds_lfht_node *node_ptr; |
| 246 | struct ust_app_ht_key key; |
| 247 | struct lttng_ht *ht; |
| 248 | |
| 249 | LTTNG_ASSERT(ua_chan); |
| 250 | LTTNG_ASSERT(ua_chan->events); |
| 251 | LTTNG_ASSERT(event); |
| 252 | |
| 253 | ht = ua_chan->events; |
| 254 | key.name = event->attr.name; |
| 255 | key.filter = event->filter; |
| 256 | key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel_type; |
| 257 | key.loglevel_value = event->attr.loglevel; |
| 258 | key.exclusion = event->exclusion; |
| 259 | |
| 260 | node_ptr = cds_lfht_add_unique(ht->ht, |
| 261 | ht->hash_fct(event->node.key, lttng_ht_seed), |
| 262 | ht_match_ust_app_event, |
| 263 | &key, |
| 264 | &event->node.node); |
| 265 | LTTNG_ASSERT(node_ptr == &event->node.node); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Close the notify socket from the given RCU head object. This MUST be called |
| 270 | * through a call_rcu(). |
| 271 | */ |
| 272 | static void close_notify_sock_rcu(struct rcu_head *head) |
| 273 | { |
| 274 | int ret; |
| 275 | struct ust_app_notify_sock_obj *obj = |
| 276 | lttng::utils::container_of(head, &ust_app_notify_sock_obj::head); |
| 277 | |
| 278 | /* Must have a valid fd here. */ |
| 279 | LTTNG_ASSERT(obj->fd >= 0); |
| 280 | |
| 281 | ret = close(obj->fd); |
| 282 | if (ret) { |
| 283 | ERR("close notify sock %d RCU", obj->fd); |
| 284 | } |
| 285 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 286 | |
| 287 | free(obj); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Delete ust context safely. RCU read lock must be held before calling |
| 292 | * this function. |
| 293 | */ |
| 294 | static void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, struct ust_app *app) |
| 295 | { |
| 296 | int ret; |
| 297 | |
| 298 | LTTNG_ASSERT(ua_ctx); |
| 299 | ASSERT_RCU_READ_LOCKED(); |
| 300 | |
| 301 | if (ua_ctx->obj) { |
| 302 | pthread_mutex_lock(&app->sock_lock); |
| 303 | ret = lttng_ust_ctl_release_object(sock, ua_ctx->obj); |
| 304 | pthread_mutex_unlock(&app->sock_lock); |
| 305 | if (ret < 0) { |
| 306 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 307 | DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d", |
| 308 | app->pid, |
| 309 | app->sock); |
| 310 | } else if (ret == -EAGAIN) { |
| 311 | WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d", |
| 312 | app->pid, |
| 313 | app->sock); |
| 314 | } else { |
| 315 | ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d", |
| 316 | ua_ctx->obj->handle, |
| 317 | ret, |
| 318 | app->pid, |
| 319 | app->sock); |
| 320 | } |
| 321 | } |
| 322 | free(ua_ctx->obj); |
| 323 | } |
| 324 | |
| 325 | if (ua_ctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) { |
| 326 | free(ua_ctx->ctx.u.app_ctx.provider_name); |
| 327 | free(ua_ctx->ctx.u.app_ctx.ctx_name); |
| 328 | } |
| 329 | |
| 330 | free(ua_ctx); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Delete ust app event safely. RCU read lock must be held before calling |
| 335 | * this function. |
| 336 | */ |
| 337 | static void delete_ust_app_event(int sock, struct ust_app_event *ua_event, struct ust_app *app) |
| 338 | { |
| 339 | int ret; |
| 340 | |
| 341 | LTTNG_ASSERT(ua_event); |
| 342 | ASSERT_RCU_READ_LOCKED(); |
| 343 | |
| 344 | free(ua_event->filter); |
| 345 | if (ua_event->exclusion != nullptr) |
| 346 | free(ua_event->exclusion); |
| 347 | if (ua_event->obj != nullptr) { |
| 348 | pthread_mutex_lock(&app->sock_lock); |
| 349 | ret = lttng_ust_ctl_release_object(sock, ua_event->obj); |
| 350 | pthread_mutex_unlock(&app->sock_lock); |
| 351 | if (ret < 0) { |
| 352 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 353 | DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d", |
| 354 | app->pid, |
| 355 | app->sock); |
| 356 | } else if (ret == -EAGAIN) { |
| 357 | WARN("UST app release event failed. Communication time out: pid = %d, sock = %d", |
| 358 | app->pid, |
| 359 | app->sock); |
| 360 | } else { |
| 361 | ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d", |
| 362 | ret, |
| 363 | app->pid, |
| 364 | app->sock); |
| 365 | } |
| 366 | } |
| 367 | free(ua_event->obj); |
| 368 | } |
| 369 | free(ua_event); |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called |
| 374 | * through a call_rcu(). |
| 375 | */ |
| 376 | static void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head) |
| 377 | { |
| 378 | struct ust_app_event_notifier_rule *obj = |
| 379 | lttng::utils::container_of(head, &ust_app_event_notifier_rule::rcu_head); |
| 380 | |
| 381 | free(obj); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Delete ust app event notifier rule safely. |
| 386 | */ |
| 387 | static void delete_ust_app_event_notifier_rule( |
| 388 | int sock, struct ust_app_event_notifier_rule *ua_event_notifier_rule, struct ust_app *app) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | LTTNG_ASSERT(ua_event_notifier_rule); |
| 393 | |
| 394 | if (ua_event_notifier_rule->exclusion != nullptr) { |
| 395 | free(ua_event_notifier_rule->exclusion); |
| 396 | } |
| 397 | |
| 398 | if (ua_event_notifier_rule->obj != nullptr) { |
| 399 | pthread_mutex_lock(&app->sock_lock); |
| 400 | ret = lttng_ust_ctl_release_object(sock, ua_event_notifier_rule->obj); |
| 401 | pthread_mutex_unlock(&app->sock_lock); |
| 402 | if (ret < 0) { |
| 403 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 404 | DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d", |
| 405 | app->pid, |
| 406 | app->sock); |
| 407 | } else if (ret == -EAGAIN) { |
| 408 | WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d", |
| 409 | app->pid, |
| 410 | app->sock); |
| 411 | } else { |
| 412 | ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d", |
| 413 | ret, |
| 414 | app->pid, |
| 415 | app->sock); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | free(ua_event_notifier_rule->obj); |
| 420 | } |
| 421 | |
| 422 | lttng_trigger_put(ua_event_notifier_rule->trigger); |
| 423 | call_rcu(&ua_event_notifier_rule->rcu_head, free_ust_app_event_notifier_rule_rcu); |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Release ust data object of the given stream. |
| 428 | * |
| 429 | * Return 0 on success or else a negative value. |
| 430 | */ |
| 431 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app) |
| 432 | { |
| 433 | int ret = 0; |
| 434 | |
| 435 | LTTNG_ASSERT(stream); |
| 436 | |
| 437 | if (stream->obj) { |
| 438 | pthread_mutex_lock(&app->sock_lock); |
| 439 | ret = lttng_ust_ctl_release_object(sock, stream->obj); |
| 440 | pthread_mutex_unlock(&app->sock_lock); |
| 441 | if (ret < 0) { |
| 442 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 443 | DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d", |
| 444 | app->pid, |
| 445 | app->sock); |
| 446 | } else if (ret == -EAGAIN) { |
| 447 | WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d", |
| 448 | app->pid, |
| 449 | app->sock); |
| 450 | } else { |
| 451 | ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d", |
| 452 | ret, |
| 453 | app->pid, |
| 454 | app->sock); |
| 455 | } |
| 456 | } |
| 457 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 458 | free(stream->obj); |
| 459 | } |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Delete ust app stream safely. RCU read lock must be held before calling |
| 466 | * this function. |
| 467 | */ |
| 468 | static void delete_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app) |
| 469 | { |
| 470 | LTTNG_ASSERT(stream); |
| 471 | ASSERT_RCU_READ_LOCKED(); |
| 472 | |
| 473 | (void) release_ust_app_stream(sock, stream, app); |
| 474 | free(stream); |
| 475 | } |
| 476 | |
| 477 | static void delete_ust_app_channel_rcu(struct rcu_head *head) |
| 478 | { |
| 479 | struct ust_app_channel *ua_chan = |
| 480 | lttng::utils::container_of(head, &ust_app_channel::rcu_head); |
| 481 | |
| 482 | lttng_ht_destroy(ua_chan->ctx); |
| 483 | lttng_ht_destroy(ua_chan->events); |
| 484 | free(ua_chan); |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Extract the lost packet or discarded events counter when the channel is |
| 489 | * being deleted and store the value in the parent channel so we can |
| 490 | * access it from lttng list and at stop/destroy. |
| 491 | * |
| 492 | * The session list lock must be held by the caller. |
| 493 | */ |
| 494 | static void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan) |
| 495 | { |
| 496 | uint64_t discarded = 0, lost = 0; |
| 497 | struct ltt_ust_channel *uchan; |
| 498 | |
| 499 | if (ua_chan->attr.type != LTTNG_UST_ABI_CHAN_PER_CPU) { |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | const lttng::urcu::read_lock_guard read_lock; |
| 504 | |
| 505 | try { |
| 506 | const auto session = ltt_session::find_session(ua_chan->session->tracing_id); |
| 507 | |
| 508 | if (!session->ust_session) { |
| 509 | /* |
| 510 | * Not finding the session is not an error because there are |
| 511 | * multiple ways the channels can be torn down. |
| 512 | * |
| 513 | * 1) The session daemon can initiate the destruction of the |
| 514 | * ust app session after receiving a destroy command or |
| 515 | * during its shutdown/teardown. |
| 516 | * 2) The application, since we are in per-pid tracing, is |
| 517 | * unregistering and tearing down its ust app session. |
| 518 | * |
| 519 | * Both paths are protected by the session list lock which |
| 520 | * ensures that the accounting of lost packets and discarded |
| 521 | * events is done exactly once. The session is then unpublished |
| 522 | * from the session list, resulting in this condition. |
| 523 | */ |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if (ua_chan->attr.overwrite) { |
| 528 | consumer_get_lost_packets(ua_chan->session->tracing_id, |
| 529 | ua_chan->key, |
| 530 | session->ust_session->consumer, |
| 531 | &lost); |
| 532 | } else { |
| 533 | consumer_get_discarded_events(ua_chan->session->tracing_id, |
| 534 | ua_chan->key, |
| 535 | session->ust_session->consumer, |
| 536 | &discarded); |
| 537 | } |
| 538 | uchan = trace_ust_find_channel_by_name(session->ust_session->domain_global.channels, |
| 539 | ua_chan->name); |
| 540 | if (!uchan) { |
| 541 | ERR("Missing UST channel to store discarded counters"); |
| 542 | return; |
| 543 | } |
| 544 | } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) { |
| 545 | DBG_FMT("Failed to save per-pid lost/discarded counters: {}, location='{}'", |
| 546 | ex.what(), |
| 547 | ex.source_location); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | uchan->per_pid_closed_app_discarded += discarded; |
| 552 | uchan->per_pid_closed_app_lost += lost; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Delete ust app channel safely. RCU read lock must be held before calling |
| 557 | * this function. |
| 558 | * |
| 559 | * The session list lock must be held by the caller. |
| 560 | */ |
| 561 | static void delete_ust_app_channel(int sock, |
| 562 | struct ust_app_channel *ua_chan, |
| 563 | struct ust_app *app, |
| 564 | const lsu::registry_session::locked_ref& locked_registry) |
| 565 | { |
| 566 | int ret; |
| 567 | |
| 568 | LTTNG_ASSERT(ua_chan); |
| 569 | ASSERT_RCU_READ_LOCKED(); |
| 570 | |
| 571 | DBG3("UST app deleting channel %s", ua_chan->name); |
| 572 | |
| 573 | /* Wipe stream */ |
| 574 | for (auto *stream : |
| 575 | lttng::urcu::list_iteration_adapter<ust_app_stream, &ust_app_stream::list>( |
| 576 | ua_chan->streams.head)) { |
| 577 | cds_list_del(&stream->list); |
| 578 | delete_ust_app_stream(sock, stream, app); |
| 579 | } |
| 580 | |
| 581 | /* Wipe context */ |
| 582 | for (auto ua_ctx : |
| 583 | lttng::urcu::lfht_iteration_adapter<ust_app_ctx, |
| 584 | decltype(ust_app_ctx::node), |
| 585 | &ust_app_ctx::node>(*ua_chan->ctx->ht)) { |
| 586 | cds_list_del(&ua_ctx->list); |
| 587 | ret = cds_lfht_del(ua_chan->ctx->ht, &ua_ctx->node.node); |
| 588 | LTTNG_ASSERT(!ret); |
| 589 | delete_ust_app_ctx(sock, ua_ctx, app); |
| 590 | } |
| 591 | |
| 592 | /* Wipe events */ |
| 593 | for (auto ua_event : |
| 594 | lttng::urcu::lfht_iteration_adapter<ust_app_event, |
| 595 | decltype(ust_app_event::node), |
| 596 | &ust_app_event::node>(*ua_chan->events->ht)) { |
| 597 | ret = cds_lfht_del(ua_chan->events->ht, &ua_event->node.node); |
| 598 | LTTNG_ASSERT(!ret); |
| 599 | delete_ust_app_event(sock, ua_event, app); |
| 600 | } |
| 601 | |
| 602 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 603 | /* Wipe and free registry from session registry. */ |
| 604 | if (locked_registry) { |
| 605 | try { |
| 606 | locked_registry->remove_channel(ua_chan->key, sock >= 0); |
| 607 | } catch (const std::exception& ex) { |
| 608 | DBG("Could not find channel for removal: %s", ex.what()); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * A negative socket can be used by the caller when |
| 614 | * cleaning-up a ua_chan in an error path. Skip the |
| 615 | * accounting in this case. |
| 616 | */ |
| 617 | if (sock >= 0) { |
| 618 | save_per_pid_lost_discarded_counters(ua_chan); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (ua_chan->obj != nullptr) { |
| 623 | lttng_ht_iter iter; |
| 624 | |
| 625 | /* Remove channel from application UST object descriptor. */ |
| 626 | iter.iter.node = &ua_chan->ust_objd_node.node; |
| 627 | ret = lttng_ht_del(app->ust_objd, &iter); |
| 628 | LTTNG_ASSERT(!ret); |
| 629 | pthread_mutex_lock(&app->sock_lock); |
| 630 | ret = lttng_ust_ctl_release_object(sock, ua_chan->obj); |
| 631 | pthread_mutex_unlock(&app->sock_lock); |
| 632 | if (ret < 0) { |
| 633 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 634 | DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d", |
| 635 | ua_chan->name, |
| 636 | app->pid, |
| 637 | app->sock); |
| 638 | } else if (ret == -EAGAIN) { |
| 639 | WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d", |
| 640 | ua_chan->name, |
| 641 | app->pid, |
| 642 | app->sock); |
| 643 | } else { |
| 644 | ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d", |
| 645 | ua_chan->name, |
| 646 | ret, |
| 647 | app->pid, |
| 648 | app->sock); |
| 649 | } |
| 650 | } |
| 651 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 652 | free(ua_chan->obj); |
| 653 | } |
| 654 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
| 655 | } |
| 656 | |
| 657 | int ust_app_register_done(struct ust_app *app) |
| 658 | { |
| 659 | int ret; |
| 660 | |
| 661 | pthread_mutex_lock(&app->sock_lock); |
| 662 | ret = lttng_ust_ctl_register_done(app->sock); |
| 663 | pthread_mutex_unlock(&app->sock_lock); |
| 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data) |
| 668 | { |
| 669 | int ret, sock; |
| 670 | |
| 671 | if (app) { |
| 672 | pthread_mutex_lock(&app->sock_lock); |
| 673 | sock = app->sock; |
| 674 | } else { |
| 675 | sock = -1; |
| 676 | } |
| 677 | ret = lttng_ust_ctl_release_object(sock, data); |
| 678 | if (app) { |
| 679 | pthread_mutex_unlock(&app->sock_lock); |
| 680 | } |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Push metadata to consumer socket. |
| 686 | * |
| 687 | * RCU read-side lock must be held to guarantee existence of socket. |
| 688 | * Must be called with the ust app session lock held. |
| 689 | * Must be called with the registry lock held. |
| 690 | * |
| 691 | * On success, return the len of metadata pushed or else a negative value. |
| 692 | * Returning a -EPIPE return value means we could not send the metadata, |
| 693 | * but it can be caused by recoverable errors (e.g. the application has |
| 694 | * terminated concurrently). |
| 695 | */ |
| 696 | ssize_t ust_app_push_metadata(const lsu::registry_session::locked_ref& locked_registry, |
| 697 | struct consumer_socket *socket, |
| 698 | int send_zero_data) |
| 699 | { |
| 700 | int ret; |
| 701 | char *metadata_str = nullptr; |
| 702 | size_t len, offset, new_metadata_len_sent; |
| 703 | ssize_t ret_val; |
| 704 | uint64_t metadata_key, metadata_version; |
| 705 | |
| 706 | LTTNG_ASSERT(locked_registry); |
| 707 | LTTNG_ASSERT(socket); |
| 708 | ASSERT_RCU_READ_LOCKED(); |
| 709 | |
| 710 | metadata_key = locked_registry->_metadata_key; |
| 711 | |
| 712 | /* |
| 713 | * Means that no metadata was assigned to the session. This can |
| 714 | * happens if no start has been done previously. |
| 715 | */ |
| 716 | if (!metadata_key) { |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | offset = locked_registry->_metadata_len_sent; |
| 721 | len = locked_registry->_metadata_len - locked_registry->_metadata_len_sent; |
| 722 | new_metadata_len_sent = locked_registry->_metadata_len; |
| 723 | metadata_version = locked_registry->_metadata_version; |
| 724 | if (len == 0) { |
| 725 | DBG3("No metadata to push for metadata key %" PRIu64, |
| 726 | locked_registry->_metadata_key); |
| 727 | ret_val = len; |
| 728 | if (send_zero_data) { |
| 729 | DBG("No metadata to push"); |
| 730 | goto push_data; |
| 731 | } |
| 732 | goto end; |
| 733 | } |
| 734 | |
| 735 | /* Allocate only what we have to send. */ |
| 736 | metadata_str = calloc<char>(len); |
| 737 | if (!metadata_str) { |
| 738 | PERROR("zmalloc ust app metadata string"); |
| 739 | ret_val = -ENOMEM; |
| 740 | goto error; |
| 741 | } |
| 742 | /* Copy what we haven't sent out. */ |
| 743 | memcpy(metadata_str, locked_registry->_metadata + offset, len); |
| 744 | |
| 745 | push_data: |
| 746 | pthread_mutex_unlock(&locked_registry->_lock); |
| 747 | /* |
| 748 | * We need to unlock the registry while we push metadata to |
| 749 | * break a circular dependency between the consumerd metadata |
| 750 | * lock and the sessiond registry lock. Indeed, pushing metadata |
| 751 | * to the consumerd awaits that it gets pushed all the way to |
| 752 | * relayd, but doing so requires grabbing the metadata lock. If |
| 753 | * a concurrent metadata request is being performed by |
| 754 | * consumerd, this can try to grab the registry lock on the |
| 755 | * sessiond while holding the metadata lock on the consumer |
| 756 | * daemon. Those push and pull schemes are performed on two |
| 757 | * different bidirectionnal communication sockets. |
| 758 | */ |
| 759 | ret = consumer_push_metadata( |
| 760 | socket, metadata_key, metadata_str, len, offset, metadata_version); |
| 761 | pthread_mutex_lock(&locked_registry->_lock); |
| 762 | if (ret < 0) { |
| 763 | /* |
| 764 | * There is an acceptable race here between the registry |
| 765 | * metadata key assignment and the creation on the |
| 766 | * consumer. The session daemon can concurrently push |
| 767 | * metadata for this registry while being created on the |
| 768 | * consumer since the metadata key of the registry is |
| 769 | * assigned *before* it is setup to avoid the consumer |
| 770 | * to ask for metadata that could possibly be not found |
| 771 | * in the session daemon. |
| 772 | * |
| 773 | * The metadata will get pushed either by the session |
| 774 | * being stopped or the consumer requesting metadata if |
| 775 | * that race is triggered. |
| 776 | */ |
| 777 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { |
| 778 | ret = 0; |
| 779 | } else { |
| 780 | ERR("Error pushing metadata to consumer"); |
| 781 | } |
| 782 | ret_val = ret; |
| 783 | goto error_push; |
| 784 | } else { |
| 785 | /* |
| 786 | * Metadata may have been concurrently pushed, since |
| 787 | * we're not holding the registry lock while pushing to |
| 788 | * consumer. This is handled by the fact that we send |
| 789 | * the metadata content, size, and the offset at which |
| 790 | * that metadata belongs. This may arrive out of order |
| 791 | * on the consumer side, and the consumer is able to |
| 792 | * deal with overlapping fragments. The consumer |
| 793 | * supports overlapping fragments, which must be |
| 794 | * contiguous starting from offset 0. We keep the |
| 795 | * largest metadata_len_sent value of the concurrent |
| 796 | * send. |
| 797 | */ |
| 798 | locked_registry->_metadata_len_sent = |
| 799 | std::max(locked_registry->_metadata_len_sent, new_metadata_len_sent); |
| 800 | } |
| 801 | free(metadata_str); |
| 802 | return len; |
| 803 | |
| 804 | end: |
| 805 | error: |
| 806 | if (ret_val) { |
| 807 | /* |
| 808 | * On error, flag the registry that the metadata is |
| 809 | * closed. We were unable to push anything and this |
| 810 | * means that either the consumer is not responding or |
| 811 | * the metadata cache has been destroyed on the |
| 812 | * consumer. |
| 813 | */ |
| 814 | locked_registry->_metadata_closed = true; |
| 815 | } |
| 816 | error_push: |
| 817 | free(metadata_str); |
| 818 | return ret_val; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * For a given application and session, push metadata to consumer. |
| 823 | * Either sock or consumer is required : if sock is NULL, the default |
| 824 | * socket to send the metadata is retrieved from consumer, if sock |
| 825 | * is not NULL we use it to send the metadata. |
| 826 | * RCU read-side lock must be held while calling this function, |
| 827 | * therefore ensuring existence of registry. It also ensures existence |
| 828 | * of socket throughout this function. |
| 829 | * |
| 830 | * Return 0 on success else a negative error. |
| 831 | * Returning a -EPIPE return value means we could not send the metadata, |
| 832 | * but it can be caused by recoverable errors (e.g. the application has |
| 833 | * terminated concurrently). |
| 834 | */ |
| 835 | static int push_metadata(const lsu::registry_session::locked_ref& locked_registry, |
| 836 | struct consumer_output *consumer) |
| 837 | { |
| 838 | int ret_val; |
| 839 | ssize_t ret; |
| 840 | struct consumer_socket *socket; |
| 841 | |
| 842 | LTTNG_ASSERT(locked_registry); |
| 843 | LTTNG_ASSERT(consumer); |
| 844 | ASSERT_RCU_READ_LOCKED(); |
| 845 | |
| 846 | if (locked_registry->_metadata_closed) { |
| 847 | ret_val = -EPIPE; |
| 848 | goto error; |
| 849 | } |
| 850 | |
| 851 | /* Get consumer socket to use to push the metadata.*/ |
| 852 | socket = consumer_find_socket_by_bitness(locked_registry->abi.bits_per_long, consumer); |
| 853 | if (!socket) { |
| 854 | ret_val = -1; |
| 855 | goto error; |
| 856 | } |
| 857 | |
| 858 | ret = ust_app_push_metadata(locked_registry, socket, 0); |
| 859 | if (ret < 0) { |
| 860 | ret_val = ret; |
| 861 | goto error; |
| 862 | } |
| 863 | return 0; |
| 864 | |
| 865 | error: |
| 866 | return ret_val; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Send to the consumer a close metadata command for the given session. Once |
| 871 | * done, the metadata channel is deleted and the session metadata pointer is |
| 872 | * nullified. The session lock MUST be held unless the application is |
| 873 | * in the destroy path. |
| 874 | * |
| 875 | * Do not hold the registry lock while communicating with the consumerd, because |
| 876 | * doing so causes inter-process deadlocks between consumerd and sessiond with |
| 877 | * the metadata request notification. |
| 878 | * |
| 879 | * Return 0 on success else a negative value. |
| 880 | */ |
| 881 | static int close_metadata(uint64_t metadata_key, |
| 882 | unsigned int consumer_bitness, |
| 883 | struct consumer_output *consumer) |
| 884 | { |
| 885 | int ret; |
| 886 | struct consumer_socket *socket; |
| 887 | const lttng::urcu::read_lock_guard read_lock_guard; |
| 888 | |
| 889 | LTTNG_ASSERT(consumer); |
| 890 | |
| 891 | /* Get consumer socket to use to push the metadata. */ |
| 892 | socket = consumer_find_socket_by_bitness(consumer_bitness, consumer); |
| 893 | if (!socket) { |
| 894 | ret = -1; |
| 895 | goto end; |
| 896 | } |
| 897 | |
| 898 | ret = consumer_close_metadata(socket, metadata_key); |
| 899 | if (ret < 0) { |
| 900 | goto end; |
| 901 | } |
| 902 | |
| 903 | end: |
| 904 | return ret; |
| 905 | } |
| 906 | |
| 907 | static void delete_ust_app_session_rcu(struct rcu_head *head) |
| 908 | { |
| 909 | struct ust_app_session *ua_sess = |
| 910 | lttng::utils::container_of(head, &ust_app_session::rcu_head); |
| 911 | |
| 912 | lttng_ht_destroy(ua_sess->channels); |
| 913 | delete ua_sess; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * Delete ust app session safely. RCU read lock must be held before calling |
| 918 | * this function. |
| 919 | * |
| 920 | * The session list lock must be held by the caller. |
| 921 | */ |
| 922 | static void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, struct ust_app *app) |
| 923 | { |
| 924 | LTTNG_ASSERT(ua_sess); |
| 925 | ASSERT_RCU_READ_LOCKED(); |
| 926 | |
| 927 | /* Locked for the duration of the function. */ |
| 928 | auto locked_ua_sess = ua_sess->lock(); |
| 929 | |
| 930 | LTTNG_ASSERT(!ua_sess->deleted); |
| 931 | ua_sess->deleted = true; |
| 932 | |
| 933 | auto locked_registry = get_locked_session_registry(locked_ua_sess->get_identifier()); |
| 934 | /* Registry can be null on error path during initialization. */ |
| 935 | if (locked_registry) { |
| 936 | /* Push metadata for application before freeing the application. */ |
| 937 | (void) push_metadata(locked_registry, ua_sess->consumer); |
| 938 | } |
| 939 | |
| 940 | for (auto *ua_chan : |
| 941 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 942 | decltype(ust_app_channel::node), |
| 943 | &ust_app_channel::node>(*ua_sess->channels->ht)) { |
| 944 | const auto ret = cds_lfht_del(ua_sess->channels->ht, &ua_chan->node.node); |
| 945 | LTTNG_ASSERT(ret == 0); |
| 946 | delete_ust_app_channel(sock, ua_chan, app, locked_registry); |
| 947 | } |
| 948 | |
| 949 | if (locked_registry) { |
| 950 | /* |
| 951 | * Don't ask to close metadata for global per UID buffers. Close |
| 952 | * metadata only on destroy trace session in this case. Also, the |
| 953 | * previous push metadata could have flag the metadata registry to |
| 954 | * close so don't send a close command if closed. |
| 955 | */ |
| 956 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 957 | const auto metadata_key = locked_registry->_metadata_key; |
| 958 | const auto consumer_bitness = locked_registry->abi.bits_per_long; |
| 959 | |
| 960 | if (!locked_registry->_metadata_closed && metadata_key != 0) { |
| 961 | locked_registry->_metadata_closed = true; |
| 962 | } |
| 963 | |
| 964 | /* Release lock before communication, see comments in close_metadata(). */ |
| 965 | locked_registry.reset(); |
| 966 | (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | /* In case of per PID, the registry is kept in the session. */ |
| 971 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 972 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 973 | if (reg_pid) { |
| 974 | /* |
| 975 | * Registry can be null on error path during |
| 976 | * initialization. |
| 977 | */ |
| 978 | buffer_reg_pid_remove(reg_pid); |
| 979 | buffer_reg_pid_destroy(reg_pid); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (ua_sess->handle != -1) { |
| 984 | pthread_mutex_lock(&app->sock_lock); |
| 985 | auto ret = lttng_ust_ctl_release_handle(sock, ua_sess->handle); |
| 986 | pthread_mutex_unlock(&app->sock_lock); |
| 987 | if (ret < 0) { |
| 988 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 989 | DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d", |
| 990 | app->pid, |
| 991 | app->sock); |
| 992 | } else if (ret == -EAGAIN) { |
| 993 | WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d", |
| 994 | app->pid, |
| 995 | app->sock); |
| 996 | } else { |
| 997 | ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d", |
| 998 | ret, |
| 999 | app->pid, |
| 1000 | app->sock); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /* Remove session from application UST object descriptor. */ |
| 1005 | ret = cds_lfht_del(app->ust_sessions_objd->ht, &ua_sess->ust_objd_node.node); |
| 1006 | LTTNG_ASSERT(!ret); |
| 1007 | } |
| 1008 | |
| 1009 | consumer_output_put(ua_sess->consumer); |
| 1010 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Delete a traceable application structure from the global list. Never call |
| 1015 | * this function outside of a call_rcu call. |
| 1016 | */ |
| 1017 | static void delete_ust_app(struct ust_app *app) |
| 1018 | { |
| 1019 | int ret, sock; |
| 1020 | bool event_notifier_write_fd_is_open; |
| 1021 | |
| 1022 | /* |
| 1023 | * The session list lock must be held during this function to guarantee |
| 1024 | * the existence of ua_sess. |
| 1025 | */ |
| 1026 | const auto list_lock = lttng::sessiond::lock_session_list(); |
| 1027 | /* Delete ust app sessions info */ |
| 1028 | sock = app->sock; |
| 1029 | app->sock = -1; |
| 1030 | |
| 1031 | /* Wipe sessions */ |
| 1032 | { |
| 1033 | const lttng::urcu::read_lock_guard read_lock; |
| 1034 | |
| 1035 | for (const auto ua_sess : app->sessions_to_teardown) { |
| 1036 | /* Free every object in the session and the session. */ |
| 1037 | delete_ust_app_session(sock, ua_sess, app); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /* Remove the event notifier rules associated with this app. */ |
| 1042 | { |
| 1043 | const lttng::urcu::read_lock_guard read_lock; |
| 1044 | |
| 1045 | for (auto *event_notifier_rule : |
| 1046 | lttng::urcu::lfht_iteration_adapter<ust_app_event_notifier_rule, |
| 1047 | decltype(ust_app_event_notifier_rule::node), |
| 1048 | &ust_app_event_notifier_rule::node>( |
| 1049 | *app->token_to_event_notifier_rule_ht->ht)) { |
| 1050 | ret = cds_lfht_del(app->token_to_event_notifier_rule_ht->ht, |
| 1051 | &event_notifier_rule->node.node); |
| 1052 | LTTNG_ASSERT(!ret); |
| 1053 | |
| 1054 | delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | lttng_ht_destroy(app->sessions); |
| 1059 | lttng_ht_destroy(app->ust_sessions_objd); |
| 1060 | lttng_ht_destroy(app->ust_objd); |
| 1061 | lttng_ht_destroy(app->token_to_event_notifier_rule_ht); |
| 1062 | |
| 1063 | /* |
| 1064 | * This could be NULL if the event notifier setup failed (e.g the app |
| 1065 | * was killed or the tracer does not support this feature). |
| 1066 | */ |
| 1067 | if (app->event_notifier_group.object) { |
| 1068 | enum lttng_error_code ret_code; |
| 1069 | enum event_notifier_error_accounting_status status; |
| 1070 | |
| 1071 | const int event_notifier_read_fd = |
| 1072 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe); |
| 1073 | |
| 1074 | ret_code = notification_thread_command_remove_tracer_event_source( |
| 1075 | the_notification_thread_handle, event_notifier_read_fd); |
| 1076 | if (ret_code != LTTNG_OK) { |
| 1077 | ERR("Failed to remove application tracer event source from notification thread"); |
| 1078 | } |
| 1079 | |
| 1080 | status = event_notifier_error_accounting_unregister_app(app); |
| 1081 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 1082 | ERR("Error unregistering app from event notifier error accounting"); |
| 1083 | } |
| 1084 | |
| 1085 | lttng_ust_ctl_release_object(sock, app->event_notifier_group.object); |
| 1086 | free(app->event_notifier_group.object); |
| 1087 | } |
| 1088 | |
| 1089 | event_notifier_write_fd_is_open = |
| 1090 | lttng_pipe_is_write_open(app->event_notifier_group.event_pipe); |
| 1091 | lttng_pipe_destroy(app->event_notifier_group.event_pipe); |
| 1092 | /* |
| 1093 | * Release the file descriptors reserved for the event notifier pipe. |
| 1094 | * The app could be destroyed before the write end of the pipe could be |
| 1095 | * passed to the application (and closed). In that case, both file |
| 1096 | * descriptors must be released. |
| 1097 | */ |
| 1098 | lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1); |
| 1099 | |
| 1100 | /* |
| 1101 | * Wait until we have deleted the application from the sock hash table |
| 1102 | * before closing this socket, otherwise an application could re-use the |
| 1103 | * socket ID and race with the teardown, using the same hash table entry. |
| 1104 | * |
| 1105 | * It's OK to leave the close in call_rcu. We want it to stay unique for |
| 1106 | * all RCU readers that could run concurrently with unregister app, |
| 1107 | * therefore we _need_ to only close that socket after a grace period. So |
| 1108 | * it should stay in this RCU callback. |
| 1109 | * |
| 1110 | * This close() is a very important step of the synchronization model so |
| 1111 | * every modification to this function must be carefully reviewed. |
| 1112 | */ |
| 1113 | ret = close(sock); |
| 1114 | if (ret) { |
| 1115 | PERROR("close"); |
| 1116 | } |
| 1117 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1118 | |
| 1119 | DBG2("UST app pid %d deleted", app->pid); |
| 1120 | delete app; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * URCU intermediate call to delete an UST app. |
| 1125 | */ |
| 1126 | static void delete_ust_app_rcu(struct rcu_head *head) |
| 1127 | { |
| 1128 | struct lttng_ht_node_ulong *node = |
| 1129 | lttng::utils::container_of(head, <tng_ht_node_ulong::head); |
| 1130 | struct ust_app *app = lttng::utils::container_of(node, &ust_app::pid_n); |
| 1131 | |
| 1132 | DBG3("Call RCU deleting app PID %d", app->pid); |
| 1133 | delete_ust_app(app); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Delete the session from the application ht and delete the data structure by |
| 1138 | * freeing every object inside and releasing them. |
| 1139 | * |
| 1140 | * The session list lock must be held by the caller. |
| 1141 | */ |
| 1142 | static void destroy_app_session(struct ust_app *app, struct ust_app_session *ua_sess) |
| 1143 | { |
| 1144 | int ret; |
| 1145 | struct lttng_ht_iter iter; |
| 1146 | |
| 1147 | LTTNG_ASSERT(app); |
| 1148 | LTTNG_ASSERT(ua_sess); |
| 1149 | |
| 1150 | iter.iter.node = &ua_sess->node.node; |
| 1151 | ret = lttng_ht_del(app->sessions, &iter); |
| 1152 | if (ret) { |
| 1153 | /* Already scheduled for teardown. */ |
| 1154 | goto end; |
| 1155 | } |
| 1156 | |
| 1157 | /* Once deleted, free the data structure. */ |
| 1158 | delete_ust_app_session(app->sock, ua_sess, app); |
| 1159 | |
| 1160 | end: |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Alloc new UST app session. |
| 1166 | */ |
| 1167 | static struct ust_app_session *alloc_ust_app_session() |
| 1168 | { |
| 1169 | struct ust_app_session *ua_sess; |
| 1170 | |
| 1171 | /* Init most of the default value by allocating and zeroing */ |
| 1172 | ua_sess = new ust_app_session; |
| 1173 | if (ua_sess == nullptr) { |
| 1174 | PERROR("malloc"); |
| 1175 | goto error_free; |
| 1176 | } |
| 1177 | |
| 1178 | ua_sess->handle = -1; |
| 1179 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1180 | ua_sess->metadata_attr.type = LTTNG_UST_ABI_CHAN_METADATA; |
| 1181 | |
| 1182 | return ua_sess; |
| 1183 | |
| 1184 | error_free: |
| 1185 | return nullptr; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Alloc new UST app channel. |
| 1190 | */ |
| 1191 | static struct ust_app_channel * |
| 1192 | alloc_ust_app_channel(const char *name, |
| 1193 | const ust_app_session::locked_weak_ref& ua_sess, |
| 1194 | struct lttng_ust_abi_channel_attr *attr) |
| 1195 | { |
| 1196 | struct ust_app_channel *ua_chan; |
| 1197 | |
| 1198 | /* Init most of the default value by allocating and zeroing */ |
| 1199 | ua_chan = zmalloc<ust_app_channel>(); |
| 1200 | if (ua_chan == nullptr) { |
| 1201 | PERROR("malloc"); |
| 1202 | goto error; |
| 1203 | } |
| 1204 | |
| 1205 | /* Setup channel name */ |
| 1206 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); |
| 1207 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 1208 | |
| 1209 | ua_chan->enabled = true; |
| 1210 | ua_chan->handle = -1; |
| 1211 | ua_chan->session = &ua_sess.get(); |
| 1212 | ua_chan->key = get_next_channel_key(); |
| 1213 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1214 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1215 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); |
| 1216 | |
| 1217 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); |
| 1218 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
| 1219 | |
| 1220 | /* Copy attributes */ |
| 1221 | if (attr) { |
| 1222 | /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */ |
| 1223 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
| 1224 | ua_chan->attr.num_subbuf = attr->num_subbuf; |
| 1225 | ua_chan->attr.overwrite = attr->overwrite; |
| 1226 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; |
| 1227 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; |
| 1228 | ua_chan->attr.output = (lttng_ust_abi_output) attr->output; |
| 1229 | ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout; |
| 1230 | } |
| 1231 | /* By default, the channel is a per cpu channel. */ |
| 1232 | ua_chan->attr.type = LTTNG_UST_ABI_CHAN_PER_CPU; |
| 1233 | |
| 1234 | DBG3("UST app channel %s allocated", ua_chan->name); |
| 1235 | |
| 1236 | return ua_chan; |
| 1237 | |
| 1238 | error: |
| 1239 | return nullptr; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Allocate and initialize a UST app stream. |
| 1244 | * |
| 1245 | * Return newly allocated stream pointer or NULL on error. |
| 1246 | */ |
| 1247 | struct ust_app_stream *ust_app_alloc_stream() |
| 1248 | { |
| 1249 | struct ust_app_stream *stream = nullptr; |
| 1250 | |
| 1251 | stream = zmalloc<ust_app_stream>(); |
| 1252 | if (stream == nullptr) { |
| 1253 | PERROR("zmalloc ust app stream"); |
| 1254 | goto error; |
| 1255 | } |
| 1256 | |
| 1257 | /* Zero could be a valid value for a handle so flag it to -1. */ |
| 1258 | stream->handle = -1; |
| 1259 | |
| 1260 | error: |
| 1261 | return stream; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Alloc new UST app event. |
| 1266 | */ |
| 1267 | static struct ust_app_event *alloc_ust_app_event(char *name, struct lttng_ust_abi_event *attr) |
| 1268 | { |
| 1269 | struct ust_app_event *ua_event; |
| 1270 | |
| 1271 | /* Init most of the default value by allocating and zeroing */ |
| 1272 | ua_event = zmalloc<ust_app_event>(); |
| 1273 | if (ua_event == nullptr) { |
| 1274 | PERROR("Failed to allocate ust_app_event structure"); |
| 1275 | goto error; |
| 1276 | } |
| 1277 | |
| 1278 | ua_event->enabled = true; |
| 1279 | strncpy(ua_event->name, name, sizeof(ua_event->name)); |
| 1280 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 1281 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
| 1282 | |
| 1283 | /* Copy attributes */ |
| 1284 | if (attr) { |
| 1285 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); |
| 1286 | } |
| 1287 | |
| 1288 | DBG3("UST app event %s allocated", ua_event->name); |
| 1289 | |
| 1290 | return ua_event; |
| 1291 | |
| 1292 | error: |
| 1293 | return nullptr; |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Allocate a new UST app event notifier rule. |
| 1298 | */ |
| 1299 | static struct ust_app_event_notifier_rule * |
| 1300 | alloc_ust_app_event_notifier_rule(struct lttng_trigger *trigger) |
| 1301 | { |
| 1302 | enum lttng_event_rule_generate_exclusions_status generate_exclusion_status; |
| 1303 | enum lttng_condition_status cond_status; |
| 1304 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; |
| 1305 | struct lttng_condition *condition = nullptr; |
| 1306 | const struct lttng_event_rule *event_rule = nullptr; |
| 1307 | |
| 1308 | ua_event_notifier_rule = zmalloc<ust_app_event_notifier_rule>(); |
| 1309 | if (ua_event_notifier_rule == nullptr) { |
| 1310 | PERROR("Failed to allocate ust_app_event_notifier_rule structure"); |
| 1311 | goto error; |
| 1312 | } |
| 1313 | |
| 1314 | ua_event_notifier_rule->enabled = true; |
| 1315 | ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger); |
| 1316 | lttng_ht_node_init_u64(&ua_event_notifier_rule->node, ua_event_notifier_rule->token); |
| 1317 | |
| 1318 | condition = lttng_trigger_get_condition(trigger); |
| 1319 | LTTNG_ASSERT(condition); |
| 1320 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 1321 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 1322 | |
| 1323 | cond_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
| 1324 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
| 1325 | LTTNG_ASSERT(event_rule); |
| 1326 | |
| 1327 | ua_event_notifier_rule->error_counter_index = |
| 1328 | lttng_condition_event_rule_matches_get_error_counter_index(condition); |
| 1329 | /* Acquire the event notifier's reference to the trigger. */ |
| 1330 | lttng_trigger_get(trigger); |
| 1331 | |
| 1332 | ua_event_notifier_rule->trigger = trigger; |
| 1333 | ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
| 1334 | generate_exclusion_status = lttng_event_rule_generate_exclusions( |
| 1335 | event_rule, &ua_event_notifier_rule->exclusion); |
| 1336 | switch (generate_exclusion_status) { |
| 1337 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK: |
| 1338 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE: |
| 1339 | break; |
| 1340 | default: |
| 1341 | /* Error occurred. */ |
| 1342 | ERR("Failed to generate exclusions from trigger while allocating an event notifier rule"); |
| 1343 | goto error_put_trigger; |
| 1344 | } |
| 1345 | |
| 1346 | DBG3("UST app event notifier rule allocated: token = %" PRIu64, |
| 1347 | ua_event_notifier_rule->token); |
| 1348 | |
| 1349 | return ua_event_notifier_rule; |
| 1350 | |
| 1351 | error_put_trigger: |
| 1352 | lttng_trigger_put(trigger); |
| 1353 | error: |
| 1354 | free(ua_event_notifier_rule); |
| 1355 | return nullptr; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * Alloc new UST app context. |
| 1360 | */ |
| 1361 | static struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) |
| 1362 | { |
| 1363 | struct ust_app_ctx *ua_ctx; |
| 1364 | |
| 1365 | ua_ctx = zmalloc<ust_app_ctx>(); |
| 1366 | if (ua_ctx == nullptr) { |
| 1367 | goto error; |
| 1368 | } |
| 1369 | |
| 1370 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
| 1371 | |
| 1372 | if (uctx) { |
| 1373 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); |
| 1374 | if (uctx->ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) { |
| 1375 | char *provider_name = nullptr, *ctx_name = nullptr; |
| 1376 | |
| 1377 | provider_name = strdup(uctx->u.app_ctx.provider_name); |
| 1378 | ctx_name = strdup(uctx->u.app_ctx.ctx_name); |
| 1379 | if (!provider_name || !ctx_name) { |
| 1380 | free(provider_name); |
| 1381 | free(ctx_name); |
| 1382 | goto error; |
| 1383 | } |
| 1384 | |
| 1385 | ua_ctx->ctx.u.app_ctx.provider_name = provider_name; |
| 1386 | ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); |
| 1391 | return ua_ctx; |
| 1392 | error: |
| 1393 | free(ua_ctx); |
| 1394 | return nullptr; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Create a liblttng-ust filter bytecode from given bytecode. |
| 1399 | * |
| 1400 | * Return allocated filter or NULL on error. |
| 1401 | */ |
| 1402 | static struct lttng_ust_abi_filter_bytecode * |
| 1403 | create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) |
| 1404 | { |
| 1405 | struct lttng_ust_abi_filter_bytecode *filter = nullptr; |
| 1406 | |
| 1407 | /* Copy filter bytecode. */ |
| 1408 | filter = zmalloc<lttng_ust_abi_filter_bytecode>(sizeof(*filter) + orig_f->len); |
| 1409 | if (!filter) { |
| 1410 | PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 |
| 1411 | " bytes", |
| 1412 | orig_f->len); |
| 1413 | goto error; |
| 1414 | } |
| 1415 | |
| 1416 | LTTNG_ASSERT(sizeof(struct lttng_bytecode) == sizeof(struct lttng_ust_abi_filter_bytecode)); |
| 1417 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
| 1418 | error: |
| 1419 | return filter; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Create a liblttng-ust capture bytecode from given bytecode. |
| 1424 | * |
| 1425 | * Return allocated filter or NULL on error. |
| 1426 | */ |
| 1427 | static struct lttng_ust_abi_capture_bytecode * |
| 1428 | create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) |
| 1429 | { |
| 1430 | struct lttng_ust_abi_capture_bytecode *capture = nullptr; |
| 1431 | |
| 1432 | /* Copy capture bytecode. */ |
| 1433 | capture = zmalloc<lttng_ust_abi_capture_bytecode>(sizeof(*capture) + orig_f->len); |
| 1434 | if (!capture) { |
| 1435 | PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32 |
| 1436 | " bytes", |
| 1437 | orig_f->len); |
| 1438 | goto error; |
| 1439 | } |
| 1440 | |
| 1441 | LTTNG_ASSERT(sizeof(struct lttng_bytecode) == |
| 1442 | sizeof(struct lttng_ust_abi_capture_bytecode)); |
| 1443 | memcpy(capture, orig_f, sizeof(*capture) + orig_f->len); |
| 1444 | error: |
| 1445 | return capture; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * Find an ust_app using the sock and return it. RCU read side lock must be |
| 1450 | * held before calling this helper function. |
| 1451 | */ |
| 1452 | struct ust_app *ust_app_find_by_sock(int sock) |
| 1453 | { |
| 1454 | struct lttng_ht_node_ulong *node; |
| 1455 | struct lttng_ht_iter iter; |
| 1456 | |
| 1457 | ASSERT_RCU_READ_LOCKED(); |
| 1458 | |
| 1459 | lttng_ht_lookup(ust_app_ht_by_sock, (void *) ((unsigned long) sock), &iter); |
| 1460 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 1461 | if (node == nullptr) { |
| 1462 | DBG2("UST app find by sock %d not found", sock); |
| 1463 | goto error; |
| 1464 | } |
| 1465 | |
| 1466 | return lttng::utils::container_of(node, &ust_app::sock_n); |
| 1467 | |
| 1468 | error: |
| 1469 | return nullptr; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Find an ust_app using the notify sock and return it. RCU read side lock must |
| 1474 | * be held before calling this helper function. |
| 1475 | */ |
| 1476 | static struct ust_app *find_app_by_notify_sock(int sock) |
| 1477 | { |
| 1478 | struct lttng_ht_node_ulong *node; |
| 1479 | struct lttng_ht_iter iter; |
| 1480 | |
| 1481 | ASSERT_RCU_READ_LOCKED(); |
| 1482 | |
| 1483 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *) ((unsigned long) sock), &iter); |
| 1484 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 1485 | if (node == nullptr) { |
| 1486 | DBG2("UST app find by notify sock %d not found", sock); |
| 1487 | goto error; |
| 1488 | } |
| 1489 | |
| 1490 | return lttng::utils::container_of(node, &ust_app::notify_sock_n); |
| 1491 | |
| 1492 | error: |
| 1493 | return nullptr; |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Lookup for an ust app event based on event name, filter bytecode and the |
| 1498 | * event loglevel. |
| 1499 | * |
| 1500 | * Return an ust_app_event object or NULL on error. |
| 1501 | */ |
| 1502 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
| 1503 | const char *name, |
| 1504 | const struct lttng_bytecode *filter, |
| 1505 | lttng_ust_abi_loglevel_type loglevel_type, |
| 1506 | int loglevel_value, |
| 1507 | const struct lttng_event_exclusion *exclusion) |
| 1508 | { |
| 1509 | struct lttng_ht_iter iter; |
| 1510 | struct lttng_ht_node_str *node; |
| 1511 | struct ust_app_event *event = nullptr; |
| 1512 | struct ust_app_ht_key key; |
| 1513 | |
| 1514 | LTTNG_ASSERT(name); |
| 1515 | LTTNG_ASSERT(ht); |
| 1516 | |
| 1517 | /* Setup key for event lookup. */ |
| 1518 | key.name = name; |
| 1519 | key.filter = filter; |
| 1520 | key.loglevel_type = loglevel_type; |
| 1521 | key.loglevel_value = loglevel_value; |
| 1522 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
| 1523 | key.exclusion = exclusion; |
| 1524 | |
| 1525 | /* Lookup using the event name as hash and a custom match fct. */ |
| 1526 | cds_lfht_lookup(ht->ht, |
| 1527 | ht->hash_fct((void *) name, lttng_ht_seed), |
| 1528 | ht_match_ust_app_event, |
| 1529 | &key, |
| 1530 | &iter.iter); |
| 1531 | node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
| 1532 | if (node == nullptr) { |
| 1533 | goto end; |
| 1534 | } |
| 1535 | |
| 1536 | event = lttng::utils::container_of(node, &ust_app_event::node); |
| 1537 | |
| 1538 | end: |
| 1539 | return event; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Look-up an event notifier rule based on its token id. |
| 1544 | * |
| 1545 | * Must be called with the RCU read lock held. |
| 1546 | * Return an ust_app_event_notifier_rule object or NULL on error. |
| 1547 | */ |
| 1548 | static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule(struct lttng_ht *ht, |
| 1549 | uint64_t token) |
| 1550 | { |
| 1551 | struct lttng_ht_iter iter; |
| 1552 | struct lttng_ht_node_u64 *node; |
| 1553 | struct ust_app_event_notifier_rule *event_notifier_rule = nullptr; |
| 1554 | |
| 1555 | LTTNG_ASSERT(ht); |
| 1556 | ASSERT_RCU_READ_LOCKED(); |
| 1557 | |
| 1558 | lttng_ht_lookup(ht, &token, &iter); |
| 1559 | node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter); |
| 1560 | if (node == nullptr) { |
| 1561 | DBG2("UST app event notifier rule token not found: token = %" PRIu64, token); |
| 1562 | goto end; |
| 1563 | } |
| 1564 | |
| 1565 | event_notifier_rule = lttng::utils::container_of(node, &ust_app_event_notifier_rule::node); |
| 1566 | end: |
| 1567 | return event_notifier_rule; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * Create the channel context on the tracer. |
| 1572 | * |
| 1573 | * Called with UST app session lock held. |
| 1574 | */ |
| 1575 | static int create_ust_channel_context(struct ust_app_channel *ua_chan, |
| 1576 | struct ust_app_ctx *ua_ctx, |
| 1577 | struct ust_app *app) |
| 1578 | { |
| 1579 | int ret; |
| 1580 | |
| 1581 | health_code_update(); |
| 1582 | |
| 1583 | pthread_mutex_lock(&app->sock_lock); |
| 1584 | ret = lttng_ust_ctl_add_context(app->sock, &ua_ctx->ctx, ua_chan->obj, &ua_ctx->obj); |
| 1585 | pthread_mutex_unlock(&app->sock_lock); |
| 1586 | if (ret < 0) { |
| 1587 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1588 | ret = 0; |
| 1589 | DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d", |
| 1590 | app->pid, |
| 1591 | app->sock); |
| 1592 | } else if (ret == -EAGAIN) { |
| 1593 | ret = 0; |
| 1594 | WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d", |
| 1595 | app->pid, |
| 1596 | app->sock); |
| 1597 | } else { |
| 1598 | ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d", |
| 1599 | ret, |
| 1600 | app->pid, |
| 1601 | app->sock); |
| 1602 | } |
| 1603 | goto error; |
| 1604 | } |
| 1605 | |
| 1606 | ua_ctx->handle = ua_ctx->obj->handle; |
| 1607 | |
| 1608 | DBG2("UST app context handle %d created successfully for channel %s", |
| 1609 | ua_ctx->handle, |
| 1610 | ua_chan->name); |
| 1611 | |
| 1612 | error: |
| 1613 | health_code_update(); |
| 1614 | return ret; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * Set the filter on the tracer. |
| 1619 | */ |
| 1620 | static int set_ust_object_filter(struct ust_app *app, |
| 1621 | const struct lttng_bytecode *bytecode, |
| 1622 | struct lttng_ust_abi_object_data *ust_object) |
| 1623 | { |
| 1624 | int ret; |
| 1625 | struct lttng_ust_abi_filter_bytecode *ust_bytecode = nullptr; |
| 1626 | |
| 1627 | health_code_update(); |
| 1628 | |
| 1629 | ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode); |
| 1630 | if (!ust_bytecode) { |
| 1631 | ret = -LTTNG_ERR_NOMEM; |
| 1632 | goto error; |
| 1633 | } |
| 1634 | pthread_mutex_lock(&app->sock_lock); |
| 1635 | ret = lttng_ust_ctl_set_filter(app->sock, ust_bytecode, ust_object); |
| 1636 | pthread_mutex_unlock(&app->sock_lock); |
| 1637 | if (ret < 0) { |
| 1638 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1639 | ret = 0; |
| 1640 | DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d", |
| 1641 | app->pid, |
| 1642 | app->sock); |
| 1643 | } else if (ret == -EAGAIN) { |
| 1644 | ret = 0; |
| 1645 | WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d", |
| 1646 | app->pid, |
| 1647 | app->sock); |
| 1648 | } else { |
| 1649 | ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p", |
| 1650 | ret, |
| 1651 | app->pid, |
| 1652 | app->sock, |
| 1653 | ust_object); |
| 1654 | } |
| 1655 | goto error; |
| 1656 | } |
| 1657 | |
| 1658 | DBG2("UST filter successfully set: object = %p", ust_object); |
| 1659 | |
| 1660 | error: |
| 1661 | health_code_update(); |
| 1662 | free(ust_bytecode); |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * Set a capture bytecode for the passed object. |
| 1668 | * The sequence number enforces the ordering at runtime and on reception of |
| 1669 | * the captured payloads. |
| 1670 | */ |
| 1671 | static int set_ust_capture(struct ust_app *app, |
| 1672 | const struct lttng_bytecode *bytecode, |
| 1673 | unsigned int capture_seqnum, |
| 1674 | struct lttng_ust_abi_object_data *ust_object) |
| 1675 | { |
| 1676 | int ret; |
| 1677 | struct lttng_ust_abi_capture_bytecode *ust_bytecode = nullptr; |
| 1678 | |
| 1679 | health_code_update(); |
| 1680 | |
| 1681 | ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode); |
| 1682 | if (!ust_bytecode) { |
| 1683 | ret = -LTTNG_ERR_NOMEM; |
| 1684 | goto error; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Set the sequence number to ensure the capture of fields is ordered. |
| 1689 | */ |
| 1690 | ust_bytecode->seqnum = capture_seqnum; |
| 1691 | |
| 1692 | pthread_mutex_lock(&app->sock_lock); |
| 1693 | ret = lttng_ust_ctl_set_capture(app->sock, ust_bytecode, ust_object); |
| 1694 | pthread_mutex_unlock(&app->sock_lock); |
| 1695 | if (ret < 0) { |
| 1696 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1697 | ret = 0; |
| 1698 | DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d", |
| 1699 | app->pid, |
| 1700 | app->sock); |
| 1701 | } else if (ret == -EAGAIN) { |
| 1702 | ret = 0; |
| 1703 | DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d", |
| 1704 | app->pid, |
| 1705 | app->sock); |
| 1706 | } else { |
| 1707 | ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d", |
| 1708 | ret, |
| 1709 | app->pid, |
| 1710 | app->sock); |
| 1711 | } |
| 1712 | |
| 1713 | goto error; |
| 1714 | } |
| 1715 | |
| 1716 | DBG2("UST capture successfully set: object = %p", ust_object); |
| 1717 | |
| 1718 | error: |
| 1719 | health_code_update(); |
| 1720 | free(ust_bytecode); |
| 1721 | return ret; |
| 1722 | } |
| 1723 | |
| 1724 | static struct lttng_ust_abi_event_exclusion * |
| 1725 | create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion *exclusion) |
| 1726 | { |
| 1727 | struct lttng_ust_abi_event_exclusion *ust_exclusion = nullptr; |
| 1728 | const size_t exclusion_alloc_size = sizeof(struct lttng_ust_abi_event_exclusion) + |
| 1729 | LTTNG_UST_ABI_SYM_NAME_LEN * exclusion->count; |
| 1730 | |
| 1731 | ust_exclusion = zmalloc<lttng_ust_abi_event_exclusion>(exclusion_alloc_size); |
| 1732 | if (!ust_exclusion) { |
| 1733 | PERROR("malloc"); |
| 1734 | goto end; |
| 1735 | } |
| 1736 | |
| 1737 | LTTNG_ASSERT(sizeof(struct lttng_event_exclusion) == |
| 1738 | sizeof(struct lttng_ust_abi_event_exclusion)); |
| 1739 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); |
| 1740 | end: |
| 1741 | return ust_exclusion; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Set event exclusions on the tracer. |
| 1746 | */ |
| 1747 | static int set_ust_object_exclusions(struct ust_app *app, |
| 1748 | const struct lttng_event_exclusion *exclusions, |
| 1749 | struct lttng_ust_abi_object_data *ust_object) |
| 1750 | { |
| 1751 | int ret; |
| 1752 | struct lttng_ust_abi_event_exclusion *ust_exclusions = nullptr; |
| 1753 | |
| 1754 | LTTNG_ASSERT(exclusions && exclusions->count > 0); |
| 1755 | |
| 1756 | health_code_update(); |
| 1757 | |
| 1758 | ust_exclusions = create_ust_exclusion_from_exclusion(exclusions); |
| 1759 | if (!ust_exclusions) { |
| 1760 | ret = -LTTNG_ERR_NOMEM; |
| 1761 | goto error; |
| 1762 | } |
| 1763 | pthread_mutex_lock(&app->sock_lock); |
| 1764 | ret = lttng_ust_ctl_set_exclusion(app->sock, ust_exclusions, ust_object); |
| 1765 | pthread_mutex_unlock(&app->sock_lock); |
| 1766 | if (ret < 0) { |
| 1767 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1768 | ret = 0; |
| 1769 | DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d", |
| 1770 | app->pid, |
| 1771 | app->sock); |
| 1772 | } else if (ret == -EAGAIN) { |
| 1773 | ret = 0; |
| 1774 | WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d", |
| 1775 | app->pid, |
| 1776 | app->sock); |
| 1777 | } else { |
| 1778 | ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p", |
| 1779 | ret, |
| 1780 | app->pid, |
| 1781 | app->sock, |
| 1782 | ust_object); |
| 1783 | } |
| 1784 | goto error; |
| 1785 | } |
| 1786 | |
| 1787 | DBG2("UST exclusions set successfully for object %p", ust_object); |
| 1788 | |
| 1789 | error: |
| 1790 | health_code_update(); |
| 1791 | free(ust_exclusions); |
| 1792 | return ret; |
| 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | * Disable the specified event on to UST tracer for the UST session. |
| 1797 | */ |
| 1798 | static int disable_ust_object(struct ust_app *app, struct lttng_ust_abi_object_data *object) |
| 1799 | { |
| 1800 | int ret; |
| 1801 | |
| 1802 | health_code_update(); |
| 1803 | |
| 1804 | pthread_mutex_lock(&app->sock_lock); |
| 1805 | ret = lttng_ust_ctl_disable(app->sock, object); |
| 1806 | pthread_mutex_unlock(&app->sock_lock); |
| 1807 | if (ret < 0) { |
| 1808 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1809 | ret = 0; |
| 1810 | DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d", |
| 1811 | app->pid, |
| 1812 | app->sock); |
| 1813 | } else if (ret == -EAGAIN) { |
| 1814 | ret = 0; |
| 1815 | WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d", |
| 1816 | app->pid, |
| 1817 | app->sock); |
| 1818 | } else { |
| 1819 | ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p", |
| 1820 | ret, |
| 1821 | app->pid, |
| 1822 | app->sock, |
| 1823 | object); |
| 1824 | } |
| 1825 | goto error; |
| 1826 | } |
| 1827 | |
| 1828 | DBG2("UST app object %p disabled successfully for app: pid = %d", object, app->pid); |
| 1829 | |
| 1830 | error: |
| 1831 | health_code_update(); |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | * Disable the specified channel on to UST tracer for the UST session. |
| 1837 | */ |
| 1838 | static int disable_ust_channel(struct ust_app *app, |
| 1839 | const ust_app_session::locked_weak_ref& ua_sess, |
| 1840 | struct ust_app_channel *ua_chan) |
| 1841 | { |
| 1842 | int ret; |
| 1843 | |
| 1844 | health_code_update(); |
| 1845 | |
| 1846 | pthread_mutex_lock(&app->sock_lock); |
| 1847 | ret = lttng_ust_ctl_disable(app->sock, ua_chan->obj); |
| 1848 | pthread_mutex_unlock(&app->sock_lock); |
| 1849 | if (ret < 0) { |
| 1850 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1851 | ret = 0; |
| 1852 | DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d", |
| 1853 | app->pid, |
| 1854 | app->sock); |
| 1855 | } else if (ret == -EAGAIN) { |
| 1856 | ret = 0; |
| 1857 | WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d", |
| 1858 | app->pid, |
| 1859 | app->sock); |
| 1860 | } else { |
| 1861 | ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d", |
| 1862 | ua_chan->name, |
| 1863 | ua_sess->handle, |
| 1864 | ret, |
| 1865 | app->pid, |
| 1866 | app->sock); |
| 1867 | } |
| 1868 | goto error; |
| 1869 | } |
| 1870 | |
| 1871 | DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan->name, app->pid); |
| 1872 | |
| 1873 | error: |
| 1874 | health_code_update(); |
| 1875 | return ret; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Enable the specified channel on to UST tracer for the UST session. |
| 1880 | */ |
| 1881 | static int enable_ust_channel(struct ust_app *app, |
| 1882 | const ust_app_session::locked_weak_ref& ua_sess, |
| 1883 | struct ust_app_channel *ua_chan) |
| 1884 | { |
| 1885 | int ret; |
| 1886 | |
| 1887 | health_code_update(); |
| 1888 | |
| 1889 | pthread_mutex_lock(&app->sock_lock); |
| 1890 | ret = lttng_ust_ctl_enable(app->sock, ua_chan->obj); |
| 1891 | pthread_mutex_unlock(&app->sock_lock); |
| 1892 | if (ret < 0) { |
| 1893 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1894 | ret = 0; |
| 1895 | DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d", |
| 1896 | ua_chan->name, |
| 1897 | app->pid, |
| 1898 | app->sock); |
| 1899 | } else if (ret == -EAGAIN) { |
| 1900 | ret = 0; |
| 1901 | WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d", |
| 1902 | ua_chan->name, |
| 1903 | app->pid, |
| 1904 | app->sock); |
| 1905 | } else { |
| 1906 | ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d", |
| 1907 | ua_chan->name, |
| 1908 | ua_sess->handle, |
| 1909 | ret, |
| 1910 | app->pid, |
| 1911 | app->sock); |
| 1912 | } |
| 1913 | goto error; |
| 1914 | } |
| 1915 | |
| 1916 | ua_chan->enabled = true; |
| 1917 | |
| 1918 | DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan->name, app->pid); |
| 1919 | |
| 1920 | error: |
| 1921 | health_code_update(); |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * Enable the specified event on to UST tracer for the UST session. |
| 1927 | */ |
| 1928 | static int enable_ust_object(struct ust_app *app, struct lttng_ust_abi_object_data *ust_object) |
| 1929 | { |
| 1930 | int ret; |
| 1931 | |
| 1932 | health_code_update(); |
| 1933 | |
| 1934 | pthread_mutex_lock(&app->sock_lock); |
| 1935 | ret = lttng_ust_ctl_enable(app->sock, ust_object); |
| 1936 | pthread_mutex_unlock(&app->sock_lock); |
| 1937 | if (ret < 0) { |
| 1938 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1939 | ret = 0; |
| 1940 | DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d", |
| 1941 | app->pid, |
| 1942 | app->sock); |
| 1943 | } else if (ret == -EAGAIN) { |
| 1944 | ret = 0; |
| 1945 | WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d", |
| 1946 | app->pid, |
| 1947 | app->sock); |
| 1948 | } else { |
| 1949 | ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p", |
| 1950 | ret, |
| 1951 | app->pid, |
| 1952 | app->sock, |
| 1953 | ust_object); |
| 1954 | } |
| 1955 | goto error; |
| 1956 | } |
| 1957 | |
| 1958 | DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object, app->pid); |
| 1959 | |
| 1960 | error: |
| 1961 | health_code_update(); |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * Send channel and stream buffer to application. |
| 1967 | * |
| 1968 | * Return 0 on success. On error, a negative value is returned. |
| 1969 | */ |
| 1970 | static int send_channel_pid_to_ust(struct ust_app *app, |
| 1971 | struct ust_app_session *ua_sess, |
| 1972 | struct ust_app_channel *ua_chan) |
| 1973 | { |
| 1974 | int ret; |
| 1975 | |
| 1976 | LTTNG_ASSERT(app); |
| 1977 | LTTNG_ASSERT(ua_sess); |
| 1978 | LTTNG_ASSERT(ua_chan); |
| 1979 | |
| 1980 | health_code_update(); |
| 1981 | |
| 1982 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, app->sock); |
| 1983 | |
| 1984 | /* Send channel to the application. */ |
| 1985 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 1986 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 1987 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 1988 | goto error; |
| 1989 | } else if (ret == -EAGAIN) { |
| 1990 | /* Caused by timeout. */ |
| 1991 | WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 |
| 1992 | "\".", |
| 1993 | app->pid, |
| 1994 | ua_chan->name, |
| 1995 | ua_sess->tracing_id); |
| 1996 | /* Treat this the same way as an application that is exiting. */ |
| 1997 | ret = -ENOTCONN; |
| 1998 | goto error; |
| 1999 | } else if (ret < 0) { |
| 2000 | goto error; |
| 2001 | } |
| 2002 | |
| 2003 | health_code_update(); |
| 2004 | |
| 2005 | /* Send all streams to application. */ |
| 2006 | for (auto *stream : |
| 2007 | lttng::urcu::list_iteration_adapter<ust_app_stream, &ust_app_stream::list>( |
| 2008 | ua_chan->streams.head)) { |
| 2009 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); |
| 2010 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2011 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 2012 | goto error; |
| 2013 | } else if (ret == -EAGAIN) { |
| 2014 | /* Caused by timeout. */ |
| 2015 | WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 |
| 2016 | "\".", |
| 2017 | app->pid, |
| 2018 | stream->name, |
| 2019 | ua_chan->name, |
| 2020 | ua_sess->tracing_id); |
| 2021 | /* |
| 2022 | * Treat this the same way as an application that is |
| 2023 | * exiting. |
| 2024 | */ |
| 2025 | ret = -ENOTCONN; |
| 2026 | } else if (ret < 0) { |
| 2027 | goto error; |
| 2028 | } |
| 2029 | /* We don't need the stream anymore once sent to the tracer. */ |
| 2030 | cds_list_del(&stream->list); |
| 2031 | delete_ust_app_stream(-1, stream, app); |
| 2032 | } |
| 2033 | |
| 2034 | error: |
| 2035 | health_code_update(); |
| 2036 | return ret; |
| 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * Create the specified event onto the UST tracer for a UST session. |
| 2041 | * |
| 2042 | * Should be called with session mutex held. |
| 2043 | */ |
| 2044 | static int create_ust_event(struct ust_app *app, |
| 2045 | struct ust_app_channel *ua_chan, |
| 2046 | struct ust_app_event *ua_event) |
| 2047 | { |
| 2048 | int ret = 0; |
| 2049 | |
| 2050 | health_code_update(); |
| 2051 | |
| 2052 | /* Create UST event on tracer */ |
| 2053 | pthread_mutex_lock(&app->sock_lock); |
| 2054 | ret = lttng_ust_ctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, &ua_event->obj); |
| 2055 | pthread_mutex_unlock(&app->sock_lock); |
| 2056 | if (ret < 0) { |
| 2057 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2058 | ret = 0; |
| 2059 | DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d", |
| 2060 | app->pid, |
| 2061 | app->sock); |
| 2062 | } else if (ret == -EAGAIN) { |
| 2063 | ret = 0; |
| 2064 | WARN("UST app create event failed. Communication time out: pid = %d, sock = %d", |
| 2065 | app->pid, |
| 2066 | app->sock); |
| 2067 | } else { |
| 2068 | ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d", |
| 2069 | ua_event->attr.name, |
| 2070 | ret, |
| 2071 | app->pid, |
| 2072 | app->sock); |
| 2073 | } |
| 2074 | goto error; |
| 2075 | } |
| 2076 | |
| 2077 | ua_event->handle = ua_event->obj->handle; |
| 2078 | |
| 2079 | DBG2("UST app event %s created successfully for pid:%d object = %p", |
| 2080 | ua_event->attr.name, |
| 2081 | app->pid, |
| 2082 | ua_event->obj); |
| 2083 | |
| 2084 | health_code_update(); |
| 2085 | |
| 2086 | /* Set filter if one is present. */ |
| 2087 | if (ua_event->filter) { |
| 2088 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
| 2089 | if (ret < 0) { |
| 2090 | goto error; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* Set exclusions for the event */ |
| 2095 | if (ua_event->exclusion) { |
| 2096 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
| 2097 | if (ret < 0) { |
| 2098 | goto error; |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | /* If event not enabled, disable it on the tracer */ |
| 2103 | if (ua_event->enabled) { |
| 2104 | /* |
| 2105 | * We now need to explicitly enable the event, since it |
| 2106 | * is now disabled at creation. |
| 2107 | */ |
| 2108 | ret = enable_ust_object(app, ua_event->obj); |
| 2109 | if (ret < 0) { |
| 2110 | /* |
| 2111 | * If we hit an EPERM, something is wrong with our enable call. If |
| 2112 | * we get an EEXIST, there is a problem on the tracer side since we |
| 2113 | * just created it. |
| 2114 | */ |
| 2115 | switch (ret) { |
| 2116 | case -LTTNG_UST_ERR_PERM: |
| 2117 | /* Code flow problem */ |
| 2118 | abort(); |
| 2119 | case -LTTNG_UST_ERR_EXIST: |
| 2120 | /* It's OK for our use case. */ |
| 2121 | ret = 0; |
| 2122 | break; |
| 2123 | default: |
| 2124 | break; |
| 2125 | } |
| 2126 | goto error; |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | error: |
| 2131 | health_code_update(); |
| 2132 | return ret; |
| 2133 | } |
| 2134 | |
| 2135 | static int |
| 2136 | init_ust_event_notifier_from_event_rule(const struct lttng_event_rule *rule, |
| 2137 | struct lttng_ust_abi_event_notifier *event_notifier) |
| 2138 | { |
| 2139 | enum lttng_event_rule_status status; |
| 2140 | enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
| 2141 | int loglevel = -1, ret = 0; |
| 2142 | const char *pattern; |
| 2143 | |
| 2144 | memset(event_notifier, 0, sizeof(*event_notifier)); |
| 2145 | |
| 2146 | if (lttng_event_rule_targets_agent_domain(rule)) { |
| 2147 | /* |
| 2148 | * Special event for agents |
| 2149 | * The actual meat of the event is in the filter that will be |
| 2150 | * attached later on. |
| 2151 | * Set the default values for the agent event. |
| 2152 | */ |
| 2153 | pattern = event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule)); |
| 2154 | loglevel = 0; |
| 2155 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
| 2156 | } else { |
| 2157 | const struct lttng_log_level_rule *log_level_rule; |
| 2158 | |
| 2159 | LTTNG_ASSERT(lttng_event_rule_get_type(rule) == |
| 2160 | LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT); |
| 2161 | |
| 2162 | status = lttng_event_rule_user_tracepoint_get_name_pattern(rule, &pattern); |
| 2163 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 2164 | /* At this point, this is a fatal error. */ |
| 2165 | abort(); |
| 2166 | } |
| 2167 | |
| 2168 | status = lttng_event_rule_user_tracepoint_get_log_level_rule(rule, &log_level_rule); |
| 2169 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { |
| 2170 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
| 2171 | } else if (status == LTTNG_EVENT_RULE_STATUS_OK) { |
| 2172 | enum lttng_log_level_rule_status llr_status; |
| 2173 | |
| 2174 | switch (lttng_log_level_rule_get_type(log_level_rule)) { |
| 2175 | case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY: |
| 2176 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE; |
| 2177 | llr_status = lttng_log_level_rule_exactly_get_level(log_level_rule, |
| 2178 | &loglevel); |
| 2179 | break; |
| 2180 | case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS: |
| 2181 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE; |
| 2182 | llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level( |
| 2183 | log_level_rule, &loglevel); |
| 2184 | break; |
| 2185 | default: |
| 2186 | abort(); |
| 2187 | } |
| 2188 | |
| 2189 | LTTNG_ASSERT(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK); |
| 2190 | } else { |
| 2191 | /* At this point this is a fatal error. */ |
| 2192 | abort(); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | event_notifier->event.instrumentation = LTTNG_UST_ABI_TRACEPOINT; |
| 2197 | ret = lttng_strncpy( |
| 2198 | event_notifier->event.name, pattern, sizeof(event_notifier->event.name)); |
| 2199 | if (ret) { |
| 2200 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern); |
| 2201 | goto end; |
| 2202 | } |
| 2203 | |
| 2204 | event_notifier->event.loglevel_type = ust_loglevel_type; |
| 2205 | event_notifier->event.loglevel = loglevel; |
| 2206 | end: |
| 2207 | return ret; |
| 2208 | } |
| 2209 | |
| 2210 | /* |
| 2211 | * Create the specified event notifier against the user space tracer of a |
| 2212 | * given application. |
| 2213 | */ |
| 2214 | static int create_ust_event_notifier(struct ust_app *app, |
| 2215 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) |
| 2216 | { |
| 2217 | int ret = 0; |
| 2218 | enum lttng_condition_status condition_status; |
| 2219 | const struct lttng_condition *condition = nullptr; |
| 2220 | struct lttng_ust_abi_event_notifier event_notifier; |
| 2221 | const struct lttng_event_rule *event_rule = nullptr; |
| 2222 | unsigned int capture_bytecode_count = 0, i; |
| 2223 | enum lttng_condition_status cond_status; |
| 2224 | enum lttng_event_rule_type event_rule_type; |
| 2225 | |
| 2226 | health_code_update(); |
| 2227 | LTTNG_ASSERT(app->event_notifier_group.object); |
| 2228 | |
| 2229 | condition = lttng_trigger_get_const_condition(ua_event_notifier_rule->trigger); |
| 2230 | LTTNG_ASSERT(condition); |
| 2231 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 2232 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 2233 | |
| 2234 | condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
| 2235 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 2236 | |
| 2237 | LTTNG_ASSERT(event_rule); |
| 2238 | |
| 2239 | event_rule_type = lttng_event_rule_get_type(event_rule); |
| 2240 | LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT || |
| 2241 | event_rule_type == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING || |
| 2242 | event_rule_type == LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING || |
| 2243 | event_rule_type == LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING || |
| 2244 | event_rule_type == LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING); |
| 2245 | |
| 2246 | init_ust_event_notifier_from_event_rule(event_rule, &event_notifier); |
| 2247 | event_notifier.event.token = ua_event_notifier_rule->token; |
| 2248 | event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index; |
| 2249 | |
| 2250 | /* Create UST event notifier against the tracer. */ |
| 2251 | pthread_mutex_lock(&app->sock_lock); |
| 2252 | ret = lttng_ust_ctl_create_event_notifier(app->sock, |
| 2253 | &event_notifier, |
| 2254 | app->event_notifier_group.object, |
| 2255 | &ua_event_notifier_rule->obj); |
| 2256 | pthread_mutex_unlock(&app->sock_lock); |
| 2257 | if (ret < 0) { |
| 2258 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2259 | ret = 0; |
| 2260 | DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d", |
| 2261 | app->pid, |
| 2262 | app->sock); |
| 2263 | } else if (ret == -EAGAIN) { |
| 2264 | ret = 0; |
| 2265 | WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d", |
| 2266 | app->pid, |
| 2267 | app->sock); |
| 2268 | } else { |
| 2269 | ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d", |
| 2270 | event_notifier.event.name, |
| 2271 | ret, |
| 2272 | app->pid, |
| 2273 | app->sock); |
| 2274 | } |
| 2275 | goto error; |
| 2276 | } |
| 2277 | |
| 2278 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; |
| 2279 | |
| 2280 | DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p", |
| 2281 | event_notifier.event.name, |
| 2282 | app->name, |
| 2283 | app->pid, |
| 2284 | ua_event_notifier_rule->obj); |
| 2285 | |
| 2286 | health_code_update(); |
| 2287 | |
| 2288 | /* Set filter if one is present. */ |
| 2289 | if (ua_event_notifier_rule->filter) { |
| 2290 | ret = set_ust_object_filter( |
| 2291 | app, ua_event_notifier_rule->filter, ua_event_notifier_rule->obj); |
| 2292 | if (ret < 0) { |
| 2293 | goto error; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | /* Set exclusions for the event. */ |
| 2298 | if (ua_event_notifier_rule->exclusion) { |
| 2299 | ret = set_ust_object_exclusions( |
| 2300 | app, ua_event_notifier_rule->exclusion, ua_event_notifier_rule->obj); |
| 2301 | if (ret < 0) { |
| 2302 | goto error; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* Set the capture bytecodes. */ |
| 2307 | cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count( |
| 2308 | condition, &capture_bytecode_count); |
| 2309 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
| 2310 | |
| 2311 | for (i = 0; i < capture_bytecode_count; i++) { |
| 2312 | const struct lttng_bytecode *capture_bytecode = |
| 2313 | lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition, |
| 2314 | i); |
| 2315 | |
| 2316 | ret = set_ust_capture(app, capture_bytecode, i, ua_event_notifier_rule->obj); |
| 2317 | if (ret < 0) { |
| 2318 | goto error; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * We now need to explicitly enable the event, since it |
| 2324 | * is disabled at creation. |
| 2325 | */ |
| 2326 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); |
| 2327 | if (ret < 0) { |
| 2328 | /* |
| 2329 | * If we hit an EPERM, something is wrong with our enable call. |
| 2330 | * If we get an EEXIST, there is a problem on the tracer side |
| 2331 | * since we just created it. |
| 2332 | */ |
| 2333 | switch (ret) { |
| 2334 | case -LTTNG_UST_ERR_PERM: |
| 2335 | /* Code flow problem. */ |
| 2336 | abort(); |
| 2337 | case -LTTNG_UST_ERR_EXIST: |
| 2338 | /* It's OK for our use case. */ |
| 2339 | ret = 0; |
| 2340 | break; |
| 2341 | default: |
| 2342 | break; |
| 2343 | } |
| 2344 | |
| 2345 | goto error; |
| 2346 | } |
| 2347 | |
| 2348 | ua_event_notifier_rule->enabled = true; |
| 2349 | |
| 2350 | error: |
| 2351 | health_code_update(); |
| 2352 | return ret; |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * Copy data between an UST app event and a LTT event. |
| 2357 | */ |
| 2358 | static void shadow_copy_event(struct ust_app_event *ua_event, struct ltt_ust_event *uevent) |
| 2359 | { |
| 2360 | size_t exclusion_alloc_size; |
| 2361 | |
| 2362 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
| 2363 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 2364 | |
| 2365 | ua_event->enabled = uevent->enabled; |
| 2366 | |
| 2367 | /* Copy event attributes */ |
| 2368 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); |
| 2369 | |
| 2370 | /* Copy filter bytecode */ |
| 2371 | if (uevent->filter) { |
| 2372 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
| 2373 | /* Filter might be NULL here in case of ENONEM. */ |
| 2374 | } |
| 2375 | |
| 2376 | /* Copy exclusion data */ |
| 2377 | if (uevent->exclusion) { |
| 2378 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
| 2379 | LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count; |
| 2380 | ua_event->exclusion = zmalloc<lttng_event_exclusion>(exclusion_alloc_size); |
| 2381 | if (ua_event->exclusion == nullptr) { |
| 2382 | PERROR("malloc"); |
| 2383 | } else { |
| 2384 | memcpy(ua_event->exclusion, uevent->exclusion, exclusion_alloc_size); |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* |
| 2390 | * Copy data between an UST app channel and a LTT channel. |
| 2391 | */ |
| 2392 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, struct ltt_ust_channel *uchan) |
| 2393 | { |
| 2394 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
| 2395 | |
| 2396 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); |
| 2397 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 2398 | |
| 2399 | ua_chan->tracefile_size = uchan->tracefile_size; |
| 2400 | ua_chan->tracefile_count = uchan->tracefile_count; |
| 2401 | |
| 2402 | /* Copy event attributes since the layout is different. */ |
| 2403 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; |
| 2404 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; |
| 2405 | ua_chan->attr.overwrite = uchan->attr.overwrite; |
| 2406 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; |
| 2407 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; |
| 2408 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
| 2409 | ua_chan->attr.output = (lttng_ust_abi_output) uchan->attr.output; |
| 2410 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
| 2411 | |
| 2412 | /* |
| 2413 | * Note that the attribute channel type is not set since the channel on the |
| 2414 | * tracing registry side does not have this information. |
| 2415 | */ |
| 2416 | |
| 2417 | ua_chan->enabled = uchan->enabled; |
| 2418 | ua_chan->tracing_channel_id = uchan->id; |
| 2419 | |
| 2420 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * Copy data between a UST app session and a regular LTT session. |
| 2425 | */ |
| 2426 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
| 2427 | struct ltt_ust_session *usess, |
| 2428 | struct ust_app *app) |
| 2429 | { |
| 2430 | struct tm *timeinfo; |
| 2431 | char datetime[16]; |
| 2432 | int ret; |
| 2433 | char tmp_shm_path[PATH_MAX]; |
| 2434 | |
| 2435 | timeinfo = localtime(&app->registration_time); |
| 2436 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 2437 | |
| 2438 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
| 2439 | |
| 2440 | ua_sess->tracing_id = usess->id; |
| 2441 | ua_sess->id = get_next_session_id(); |
| 2442 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
| 2443 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); |
| 2444 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); |
| 2445 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); |
| 2446 | ua_sess->buffer_type = usess->buffer_type; |
| 2447 | ua_sess->bits_per_long = app->abi.bits_per_long; |
| 2448 | |
| 2449 | /* There is only one consumer object per session possible. */ |
| 2450 | consumer_output_get(usess->consumer); |
| 2451 | ua_sess->consumer = usess->consumer; |
| 2452 | |
| 2453 | ua_sess->output_traces = usess->output_traces; |
| 2454 | ua_sess->live_timer_interval = usess->live_timer_interval; |
| 2455 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &usess->metadata_attr); |
| 2456 | |
| 2457 | switch (ua_sess->buffer_type) { |
| 2458 | case LTTNG_BUFFER_PER_PID: |
| 2459 | ret = snprintf(ua_sess->path, |
| 2460 | sizeof(ua_sess->path), |
| 2461 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
| 2462 | app->name, |
| 2463 | app->pid, |
| 2464 | datetime); |
| 2465 | break; |
| 2466 | case LTTNG_BUFFER_PER_UID: |
| 2467 | ret = snprintf(ua_sess->path, |
| 2468 | sizeof(ua_sess->path), |
| 2469 | DEFAULT_UST_TRACE_UID_PATH, |
| 2470 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
| 2471 | app->abi.bits_per_long); |
| 2472 | break; |
| 2473 | default: |
| 2474 | abort(); |
| 2475 | goto error; |
| 2476 | } |
| 2477 | if (ret < 0) { |
| 2478 | PERROR("asprintf UST shadow copy session"); |
| 2479 | abort(); |
| 2480 | goto error; |
| 2481 | } |
| 2482 | |
| 2483 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, sizeof(ua_sess->root_shm_path)); |
| 2484 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; |
| 2485 | strncpy(ua_sess->shm_path, usess->shm_path, sizeof(ua_sess->shm_path)); |
| 2486 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 2487 | if (ua_sess->shm_path[0]) { |
| 2488 | switch (ua_sess->buffer_type) { |
| 2489 | case LTTNG_BUFFER_PER_PID: |
| 2490 | ret = snprintf(tmp_shm_path, |
| 2491 | sizeof(tmp_shm_path), |
| 2492 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
| 2493 | app->name, |
| 2494 | app->pid, |
| 2495 | datetime); |
| 2496 | break; |
| 2497 | case LTTNG_BUFFER_PER_UID: |
| 2498 | ret = snprintf(tmp_shm_path, |
| 2499 | sizeof(tmp_shm_path), |
| 2500 | "/" DEFAULT_UST_TRACE_UID_PATH, |
| 2501 | app->uid, |
| 2502 | app->abi.bits_per_long); |
| 2503 | break; |
| 2504 | default: |
| 2505 | abort(); |
| 2506 | goto error; |
| 2507 | } |
| 2508 | if (ret < 0) { |
| 2509 | PERROR("sprintf UST shadow copy session"); |
| 2510 | abort(); |
| 2511 | goto error; |
| 2512 | } |
| 2513 | strncat(ua_sess->shm_path, |
| 2514 | tmp_shm_path, |
| 2515 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); |
| 2516 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 2517 | } |
| 2518 | return; |
| 2519 | |
| 2520 | error: |
| 2521 | consumer_output_put(ua_sess->consumer); |
| 2522 | } |
| 2523 | |
| 2524 | /* |
| 2525 | * Lookup sesison wrapper. |
| 2526 | */ |
| 2527 | static void |
| 2528 | __lookup_session_by_app(const ltt_ust_session *usess, const ust_app *app, lttng_ht_iter *iter) |
| 2529 | { |
| 2530 | /* Get right UST app session from app */ |
| 2531 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | * Return ust app session from the app session hashtable using the UST session |
| 2536 | * id. |
| 2537 | */ |
| 2538 | ust_app_session *ust_app_lookup_app_session(const struct ltt_ust_session *usess, |
| 2539 | const struct ust_app *app) |
| 2540 | { |
| 2541 | struct lttng_ht_iter iter; |
| 2542 | struct lttng_ht_node_u64 *node; |
| 2543 | |
| 2544 | __lookup_session_by_app(usess, app, &iter); |
| 2545 | node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter); |
| 2546 | if (node == nullptr) { |
| 2547 | goto error; |
| 2548 | } |
| 2549 | |
| 2550 | return lttng::utils::container_of(node, &ust_app_session::node); |
| 2551 | |
| 2552 | error: |
| 2553 | return nullptr; |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * Setup buffer registry per PID for the given session and application. If none |
| 2558 | * is found, a new one is created, added to the global registry and |
| 2559 | * initialized. If regp is valid, it's set with the newly created object. |
| 2560 | * |
| 2561 | * Return 0 on success or else a negative value. |
| 2562 | */ |
| 2563 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, |
| 2564 | struct ust_app *app, |
| 2565 | struct buffer_reg_pid **regp) |
| 2566 | { |
| 2567 | int ret = 0; |
| 2568 | struct buffer_reg_pid *reg_pid; |
| 2569 | |
| 2570 | LTTNG_ASSERT(ua_sess); |
| 2571 | LTTNG_ASSERT(app); |
| 2572 | |
| 2573 | const lttng::urcu::read_lock_guard read_lock; |
| 2574 | |
| 2575 | reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 2576 | if (!reg_pid) { |
| 2577 | /* |
| 2578 | * This is the create channel path meaning that if there is NO |
| 2579 | * registry available, we have to create one for this session. |
| 2580 | */ |
| 2581 | ret = buffer_reg_pid_create( |
| 2582 | ua_sess->id, ®_pid, ua_sess->root_shm_path, ua_sess->shm_path); |
| 2583 | if (ret < 0) { |
| 2584 | goto error; |
| 2585 | } |
| 2586 | } else { |
| 2587 | goto end; |
| 2588 | } |
| 2589 | |
| 2590 | /* Initialize registry. */ |
| 2591 | reg_pid->registry->reg.ust = ust_registry_session_per_pid_create( |
| 2592 | app, |
| 2593 | app->abi, |
| 2594 | app->version.major, |
| 2595 | app->version.minor, |
| 2596 | reg_pid->root_shm_path, |
| 2597 | reg_pid->shm_path, |
| 2598 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 2599 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 2600 | ua_sess->tracing_id); |
| 2601 | if (!reg_pid->registry->reg.ust) { |
| 2602 | /* |
| 2603 | * reg_pid->registry->reg.ust is NULL upon error, so we need to |
| 2604 | * destroy the buffer registry, because it is always expected |
| 2605 | * that if the buffer registry can be found, its ust registry is |
| 2606 | * non-NULL. |
| 2607 | */ |
| 2608 | buffer_reg_pid_destroy(reg_pid); |
| 2609 | goto error; |
| 2610 | } |
| 2611 | |
| 2612 | buffer_reg_pid_add(reg_pid); |
| 2613 | |
| 2614 | DBG3("UST app buffer registry per PID created successfully"); |
| 2615 | |
| 2616 | end: |
| 2617 | if (regp) { |
| 2618 | *regp = reg_pid; |
| 2619 | } |
| 2620 | error: |
| 2621 | return ret; |
| 2622 | } |
| 2623 | |
| 2624 | /* |
| 2625 | * Setup buffer registry per UID for the given session and application. If none |
| 2626 | * is found, a new one is created, added to the global registry and |
| 2627 | * initialized. If regp is valid, it's set with the newly created object. |
| 2628 | * |
| 2629 | * Return 0 on success or else a negative value. |
| 2630 | */ |
| 2631 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, |
| 2632 | struct ust_app_session *ua_sess, |
| 2633 | struct ust_app *app, |
| 2634 | struct buffer_reg_uid **regp) |
| 2635 | { |
| 2636 | int ret = 0; |
| 2637 | struct buffer_reg_uid *reg_uid; |
| 2638 | |
| 2639 | LTTNG_ASSERT(usess); |
| 2640 | LTTNG_ASSERT(app); |
| 2641 | |
| 2642 | const lttng::urcu::read_lock_guard read_lock; |
| 2643 | |
| 2644 | reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid); |
| 2645 | if (!reg_uid) { |
| 2646 | /* |
| 2647 | * This is the create channel path meaning that if there is NO |
| 2648 | * registry available, we have to create one for this session. |
| 2649 | */ |
| 2650 | ret = buffer_reg_uid_create(usess->id, |
| 2651 | app->abi.bits_per_long, |
| 2652 | app->uid, |
| 2653 | LTTNG_DOMAIN_UST, |
| 2654 | ®_uid, |
| 2655 | ua_sess->root_shm_path, |
| 2656 | ua_sess->shm_path); |
| 2657 | if (ret < 0) { |
| 2658 | goto error; |
| 2659 | } |
| 2660 | } else { |
| 2661 | goto end; |
| 2662 | } |
| 2663 | |
| 2664 | /* Initialize registry. */ |
| 2665 | reg_uid->registry->reg.ust = ust_registry_session_per_uid_create(app->abi, |
| 2666 | app->version.major, |
| 2667 | app->version.minor, |
| 2668 | reg_uid->root_shm_path, |
| 2669 | reg_uid->shm_path, |
| 2670 | usess->uid, |
| 2671 | usess->gid, |
| 2672 | ua_sess->tracing_id, |
| 2673 | app->uid); |
| 2674 | if (!reg_uid->registry->reg.ust) { |
| 2675 | /* |
| 2676 | * reg_uid->registry->reg.ust is NULL upon error, so we need to |
| 2677 | * destroy the buffer registry, because it is always expected |
| 2678 | * that if the buffer registry can be found, its ust registry is |
| 2679 | * non-NULL. |
| 2680 | */ |
| 2681 | buffer_reg_uid_destroy(reg_uid, nullptr); |
| 2682 | goto error; |
| 2683 | } |
| 2684 | |
| 2685 | /* Add node to teardown list of the session. */ |
| 2686 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); |
| 2687 | |
| 2688 | buffer_reg_uid_add(reg_uid); |
| 2689 | |
| 2690 | DBG3("UST app buffer registry per UID created successfully"); |
| 2691 | end: |
| 2692 | if (regp) { |
| 2693 | *regp = reg_uid; |
| 2694 | } |
| 2695 | error: |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * Create a session on the tracer side for the given app. |
| 2701 | * |
| 2702 | * On success, ua_sess_ptr is populated with the session pointer or else left |
| 2703 | * untouched. If the session was created, is_created is set to 1. On error, |
| 2704 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can |
| 2705 | * be NULL. |
| 2706 | * |
| 2707 | * Returns 0 on success or else a negative code which is either -ENOMEM or |
| 2708 | * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails. |
| 2709 | */ |
| 2710 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
| 2711 | struct ust_app *app, |
| 2712 | struct ust_app_session **ua_sess_ptr, |
| 2713 | int *is_created) |
| 2714 | { |
| 2715 | int ret, created = 0; |
| 2716 | struct ust_app_session *ua_sess; |
| 2717 | |
| 2718 | LTTNG_ASSERT(usess); |
| 2719 | LTTNG_ASSERT(app); |
| 2720 | LTTNG_ASSERT(ua_sess_ptr); |
| 2721 | |
| 2722 | health_code_update(); |
| 2723 | |
| 2724 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 2725 | if (ua_sess == nullptr) { |
| 2726 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
| 2727 | app->pid, |
| 2728 | usess->id); |
| 2729 | ua_sess = alloc_ust_app_session(); |
| 2730 | if (ua_sess == nullptr) { |
| 2731 | /* Only malloc can failed so something is really wrong */ |
| 2732 | ret = -ENOMEM; |
| 2733 | goto error; |
| 2734 | } |
| 2735 | shadow_copy_session(ua_sess, usess, app); |
| 2736 | created = 1; |
| 2737 | } |
| 2738 | |
| 2739 | switch (usess->buffer_type) { |
| 2740 | case LTTNG_BUFFER_PER_PID: |
| 2741 | /* Init local registry. */ |
| 2742 | ret = setup_buffer_reg_pid(ua_sess, app, nullptr); |
| 2743 | if (ret < 0) { |
| 2744 | delete_ust_app_session(-1, ua_sess, app); |
| 2745 | goto error; |
| 2746 | } |
| 2747 | break; |
| 2748 | case LTTNG_BUFFER_PER_UID: |
| 2749 | /* Look for a global registry. If none exists, create one. */ |
| 2750 | ret = setup_buffer_reg_uid(usess, ua_sess, app, nullptr); |
| 2751 | if (ret < 0) { |
| 2752 | delete_ust_app_session(-1, ua_sess, app); |
| 2753 | goto error; |
| 2754 | } |
| 2755 | break; |
| 2756 | default: |
| 2757 | abort(); |
| 2758 | ret = -EINVAL; |
| 2759 | goto error; |
| 2760 | } |
| 2761 | |
| 2762 | health_code_update(); |
| 2763 | |
| 2764 | if (ua_sess->handle == -1) { |
| 2765 | pthread_mutex_lock(&app->sock_lock); |
| 2766 | ret = lttng_ust_ctl_create_session(app->sock); |
| 2767 | pthread_mutex_unlock(&app->sock_lock); |
| 2768 | if (ret < 0) { |
| 2769 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2770 | DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d", |
| 2771 | app->pid, |
| 2772 | app->sock); |
| 2773 | ret = 0; |
| 2774 | } else if (ret == -EAGAIN) { |
| 2775 | DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d", |
| 2776 | app->pid, |
| 2777 | app->sock); |
| 2778 | ret = 0; |
| 2779 | } else { |
| 2780 | ERR("UST app creating session failed with ret %d: pid = %d, sock =%d", |
| 2781 | ret, |
| 2782 | app->pid, |
| 2783 | app->sock); |
| 2784 | } |
| 2785 | delete_ust_app_session(-1, ua_sess, app); |
| 2786 | if (ret != -ENOMEM) { |
| 2787 | /* |
| 2788 | * Tracer is probably gone or got an internal error so let's |
| 2789 | * behave like it will soon unregister or not usable. |
| 2790 | */ |
| 2791 | ret = -ENOTCONN; |
| 2792 | } |
| 2793 | goto error; |
| 2794 | } |
| 2795 | |
| 2796 | ua_sess->handle = ret; |
| 2797 | |
| 2798 | /* Add ust app session to app's HT */ |
| 2799 | lttng_ht_node_init_u64(&ua_sess->node, ua_sess->tracing_id); |
| 2800 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); |
| 2801 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
| 2802 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, &ua_sess->ust_objd_node); |
| 2803 | |
| 2804 | DBG2("UST app session created successfully with handle %d", ret); |
| 2805 | } |
| 2806 | |
| 2807 | *ua_sess_ptr = ua_sess; |
| 2808 | if (is_created) { |
| 2809 | *is_created = created; |
| 2810 | } |
| 2811 | |
| 2812 | /* Everything went well. */ |
| 2813 | ret = 0; |
| 2814 | |
| 2815 | error: |
| 2816 | health_code_update(); |
| 2817 | return ret; |
| 2818 | } |
| 2819 | |
| 2820 | /* |
| 2821 | * Match function for a hash table lookup of ust_app_ctx. |
| 2822 | * |
| 2823 | * It matches an ust app context based on the context type and, in the case |
| 2824 | * of perf counters, their name. |
| 2825 | */ |
| 2826 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) |
| 2827 | { |
| 2828 | LTTNG_ASSERT(node); |
| 2829 | LTTNG_ASSERT(_key); |
| 2830 | |
| 2831 | auto *ctx = lttng_ht_node_container_of(node, &ust_app_ctx::node); |
| 2832 | const auto *key = (lttng_ust_context_attr *) _key; |
| 2833 | |
| 2834 | /* Context type */ |
| 2835 | if (ctx->ctx.ctx != key->ctx) { |
| 2836 | goto no_match; |
| 2837 | } |
| 2838 | |
| 2839 | switch (key->ctx) { |
| 2840 | case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER: |
| 2841 | if (strncmp(key->u.perf_counter.name, |
| 2842 | ctx->ctx.u.perf_counter.name, |
| 2843 | sizeof(key->u.perf_counter.name)) != 0) { |
| 2844 | goto no_match; |
| 2845 | } |
| 2846 | break; |
| 2847 | case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT: |
| 2848 | if (strcmp(key->u.app_ctx.provider_name, ctx->ctx.u.app_ctx.provider_name) != 0 || |
| 2849 | strcmp(key->u.app_ctx.ctx_name, ctx->ctx.u.app_ctx.ctx_name) != 0) { |
| 2850 | goto no_match; |
| 2851 | } |
| 2852 | break; |
| 2853 | default: |
| 2854 | break; |
| 2855 | } |
| 2856 | |
| 2857 | /* Match. */ |
| 2858 | return 1; |
| 2859 | |
| 2860 | no_match: |
| 2861 | return 0; |
| 2862 | } |
| 2863 | |
| 2864 | /* |
| 2865 | * Lookup for an ust app context from an lttng_ust_context. |
| 2866 | * |
| 2867 | * Must be called while holding RCU read side lock. |
| 2868 | * Return an ust_app_ctx object or NULL on error. |
| 2869 | */ |
| 2870 | static struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, |
| 2871 | struct lttng_ust_context_attr *uctx) |
| 2872 | { |
| 2873 | struct lttng_ht_iter iter; |
| 2874 | struct lttng_ht_node_ulong *node; |
| 2875 | struct ust_app_ctx *app_ctx = nullptr; |
| 2876 | |
| 2877 | LTTNG_ASSERT(uctx); |
| 2878 | LTTNG_ASSERT(ht); |
| 2879 | ASSERT_RCU_READ_LOCKED(); |
| 2880 | |
| 2881 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ |
| 2882 | cds_lfht_lookup(ht->ht, |
| 2883 | ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), |
| 2884 | ht_match_ust_app_ctx, |
| 2885 | uctx, |
| 2886 | &iter.iter); |
| 2887 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 2888 | if (!node) { |
| 2889 | goto end; |
| 2890 | } |
| 2891 | |
| 2892 | app_ctx = lttng::utils::container_of(node, &ust_app_ctx::node); |
| 2893 | |
| 2894 | end: |
| 2895 | return app_ctx; |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * Create a context for the channel on the tracer. |
| 2900 | * |
| 2901 | * Called with UST app session lock held and a RCU read side lock. |
| 2902 | */ |
| 2903 | static int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
| 2904 | struct lttng_ust_context_attr *uctx, |
| 2905 | struct ust_app *app) |
| 2906 | { |
| 2907 | int ret = 0; |
| 2908 | struct ust_app_ctx *ua_ctx; |
| 2909 | |
| 2910 | ASSERT_RCU_READ_LOCKED(); |
| 2911 | |
| 2912 | DBG2("UST app adding context to channel %s", ua_chan->name); |
| 2913 | |
| 2914 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
| 2915 | if (ua_ctx) { |
| 2916 | ret = -EEXIST; |
| 2917 | goto error; |
| 2918 | } |
| 2919 | |
| 2920 | ua_ctx = alloc_ust_app_ctx(uctx); |
| 2921 | if (ua_ctx == nullptr) { |
| 2922 | /* malloc failed */ |
| 2923 | ret = -ENOMEM; |
| 2924 | goto error; |
| 2925 | } |
| 2926 | |
| 2927 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
| 2928 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
| 2929 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
| 2930 | |
| 2931 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 2932 | if (ret < 0) { |
| 2933 | goto error; |
| 2934 | } |
| 2935 | |
| 2936 | error: |
| 2937 | return ret; |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * Enable on the tracer side a ust app event for the session and channel. |
| 2942 | * |
| 2943 | * Called with UST app session lock held. |
| 2944 | */ |
| 2945 | static int enable_ust_app_event(struct ust_app_event *ua_event, struct ust_app *app) |
| 2946 | { |
| 2947 | int ret; |
| 2948 | |
| 2949 | ret = enable_ust_object(app, ua_event->obj); |
| 2950 | if (ret < 0) { |
| 2951 | goto error; |
| 2952 | } |
| 2953 | |
| 2954 | ua_event->enabled = true; |
| 2955 | |
| 2956 | error: |
| 2957 | return ret; |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | * Disable on the tracer side a ust app event for the session and channel. |
| 2962 | */ |
| 2963 | static int disable_ust_app_event(struct ust_app_event *ua_event, struct ust_app *app) |
| 2964 | { |
| 2965 | int ret; |
| 2966 | |
| 2967 | ret = disable_ust_object(app, ua_event->obj); |
| 2968 | if (ret < 0) { |
| 2969 | goto error; |
| 2970 | } |
| 2971 | |
| 2972 | ua_event->enabled = false; |
| 2973 | |
| 2974 | error: |
| 2975 | return ret; |
| 2976 | } |
| 2977 | |
| 2978 | /* |
| 2979 | * Lookup ust app channel for session and disable it on the tracer side. |
| 2980 | */ |
| 2981 | static int disable_ust_app_channel(const ust_app_session::locked_weak_ref& ua_sess, |
| 2982 | struct ust_app_channel *ua_chan, |
| 2983 | struct ust_app *app) |
| 2984 | { |
| 2985 | int ret; |
| 2986 | |
| 2987 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 2988 | if (ret < 0) { |
| 2989 | goto error; |
| 2990 | } |
| 2991 | |
| 2992 | ua_chan->enabled = false; |
| 2993 | |
| 2994 | error: |
| 2995 | return ret; |
| 2996 | } |
| 2997 | |
| 2998 | /* |
| 2999 | * Lookup ust app channel for session and enable it on the tracer side. This |
| 3000 | * MUST be called with a RCU read side lock acquired. |
| 3001 | */ |
| 3002 | static int enable_ust_app_channel(const ust_app_session::locked_weak_ref& ua_sess, |
| 3003 | struct ltt_ust_channel *uchan, |
| 3004 | struct ust_app *app) |
| 3005 | { |
| 3006 | int ret = 0; |
| 3007 | struct lttng_ht_iter iter; |
| 3008 | struct lttng_ht_node_str *ua_chan_node; |
| 3009 | struct ust_app_channel *ua_chan; |
| 3010 | |
| 3011 | ASSERT_RCU_READ_LOCKED(); |
| 3012 | |
| 3013 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); |
| 3014 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
| 3015 | if (ua_chan_node == nullptr) { |
| 3016 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
| 3017 | uchan->name, |
| 3018 | ua_sess->tracing_id); |
| 3019 | goto error; |
| 3020 | } |
| 3021 | |
| 3022 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 3023 | |
| 3024 | ret = enable_ust_channel(app, ua_sess, ua_chan); |
| 3025 | if (ret < 0) { |
| 3026 | goto error; |
| 3027 | } |
| 3028 | |
| 3029 | error: |
| 3030 | return ret; |
| 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Ask the consumer to create a channel and get it if successful. |
| 3035 | * |
| 3036 | * Called with UST app session lock held. |
| 3037 | * |
| 3038 | * Return 0 on success or else a negative value. |
| 3039 | */ |
| 3040 | static int do_consumer_create_channel(struct ltt_ust_session *usess, |
| 3041 | struct ust_app_session *ua_sess, |
| 3042 | struct ust_app_channel *ua_chan, |
| 3043 | int bitness, |
| 3044 | lsu::registry_session *registry) |
| 3045 | { |
| 3046 | int ret; |
| 3047 | unsigned int nb_fd = 0; |
| 3048 | struct consumer_socket *socket; |
| 3049 | |
| 3050 | LTTNG_ASSERT(usess); |
| 3051 | LTTNG_ASSERT(ua_sess); |
| 3052 | LTTNG_ASSERT(ua_chan); |
| 3053 | LTTNG_ASSERT(registry); |
| 3054 | |
| 3055 | const lttng::urcu::read_lock_guard read_lock; |
| 3056 | health_code_update(); |
| 3057 | |
| 3058 | /* Get the right consumer socket for the application. */ |
| 3059 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); |
| 3060 | if (!socket) { |
| 3061 | ret = -EINVAL; |
| 3062 | goto error; |
| 3063 | } |
| 3064 | |
| 3065 | health_code_update(); |
| 3066 | |
| 3067 | /* Need one fd for the channel. */ |
| 3068 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3069 | if (ret < 0) { |
| 3070 | ERR("Exhausted number of available FD upon create channel"); |
| 3071 | goto error; |
| 3072 | } |
| 3073 | |
| 3074 | /* |
| 3075 | * Ask consumer to create channel. The consumer will return the number of |
| 3076 | * stream we have to expect. |
| 3077 | */ |
| 3078 | ret = ust_consumer_ask_channel( |
| 3079 | ua_sess, ua_chan, usess->consumer, socket, registry, usess->current_trace_chunk); |
| 3080 | if (ret < 0) { |
| 3081 | goto error_ask; |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * Compute the number of fd needed before receiving them. It must be 2 per |
| 3086 | * stream (2 being the default value here). |
| 3087 | */ |
| 3088 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; |
| 3089 | |
| 3090 | /* Reserve the amount of file descriptor we need. */ |
| 3091 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); |
| 3092 | if (ret < 0) { |
| 3093 | ERR("Exhausted number of available FD upon create channel"); |
| 3094 | goto error_fd_get_stream; |
| 3095 | } |
| 3096 | |
| 3097 | health_code_update(); |
| 3098 | |
| 3099 | /* |
| 3100 | * Now get the channel from the consumer. This call will populate the stream |
| 3101 | * list of that channel and set the ust objects. |
| 3102 | */ |
| 3103 | if (usess->consumer->enabled) { |
| 3104 | ret = ust_consumer_get_channel(socket, ua_chan); |
| 3105 | if (ret < 0) { |
| 3106 | goto error_destroy; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | return 0; |
| 3111 | |
| 3112 | error_destroy: |
| 3113 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); |
| 3114 | error_fd_get_stream: |
| 3115 | /* |
| 3116 | * Initiate a destroy channel on the consumer since we had an error |
| 3117 | * handling it on our side. The return value is of no importance since we |
| 3118 | * already have a ret value set by the previous error that we need to |
| 3119 | * return. |
| 3120 | */ |
| 3121 | (void) ust_consumer_destroy_channel(socket, ua_chan); |
| 3122 | error_ask: |
| 3123 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3124 | error: |
| 3125 | health_code_update(); |
| 3126 | return ret; |
| 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * Duplicate the ust data object of the ust app stream and save it in the |
| 3131 | * buffer registry stream. |
| 3132 | * |
| 3133 | * Return 0 on success or else a negative value. |
| 3134 | */ |
| 3135 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, |
| 3136 | struct ust_app_stream *stream) |
| 3137 | { |
| 3138 | int ret; |
| 3139 | |
| 3140 | LTTNG_ASSERT(reg_stream); |
| 3141 | LTTNG_ASSERT(stream); |
| 3142 | |
| 3143 | /* Duplicating a stream requires 2 new fds. Reserve them. */ |
| 3144 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 3145 | if (ret < 0) { |
| 3146 | ERR("Exhausted number of available FD upon duplicate stream"); |
| 3147 | goto error; |
| 3148 | } |
| 3149 | |
| 3150 | /* Duplicate object for stream once the original is in the registry. */ |
| 3151 | ret = lttng_ust_ctl_duplicate_ust_object_data(&stream->obj, reg_stream->obj.ust); |
| 3152 | if (ret < 0) { |
| 3153 | ERR("Duplicate stream obj from %p to %p failed with ret %d", |
| 3154 | reg_stream->obj.ust, |
| 3155 | stream->obj, |
| 3156 | ret); |
| 3157 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 3158 | goto error; |
| 3159 | } |
| 3160 | stream->handle = stream->obj->handle; |
| 3161 | |
| 3162 | error: |
| 3163 | return ret; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * Duplicate the ust data object of the ust app. channel and save it in the |
| 3168 | * buffer registry channel. |
| 3169 | * |
| 3170 | * Return 0 on success or else a negative value. |
| 3171 | */ |
| 3172 | static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan, |
| 3173 | struct ust_app_channel *ua_chan) |
| 3174 | { |
| 3175 | int ret; |
| 3176 | |
| 3177 | LTTNG_ASSERT(buf_reg_chan); |
| 3178 | LTTNG_ASSERT(ua_chan); |
| 3179 | |
| 3180 | /* Duplicating a channel requires 1 new fd. Reserve it. */ |
| 3181 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3182 | if (ret < 0) { |
| 3183 | ERR("Exhausted number of available FD upon duplicate channel"); |
| 3184 | goto error_fd_get; |
| 3185 | } |
| 3186 | |
| 3187 | /* Duplicate object for stream once the original is in the registry. */ |
| 3188 | ret = lttng_ust_ctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust); |
| 3189 | if (ret < 0) { |
| 3190 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", |
| 3191 | buf_reg_chan->obj.ust, |
| 3192 | ua_chan->obj, |
| 3193 | ret); |
| 3194 | goto error; |
| 3195 | } |
| 3196 | ua_chan->handle = ua_chan->obj->handle; |
| 3197 | |
| 3198 | return 0; |
| 3199 | |
| 3200 | error: |
| 3201 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3202 | error_fd_get: |
| 3203 | return ret; |
| 3204 | } |
| 3205 | |
| 3206 | /* |
| 3207 | * For a given channel buffer registry, setup all streams of the given ust |
| 3208 | * application channel. |
| 3209 | * |
| 3210 | * Return 0 on success or else a negative value. |
| 3211 | */ |
| 3212 | static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan, |
| 3213 | struct ust_app_channel *ua_chan, |
| 3214 | struct ust_app *app) |
| 3215 | { |
| 3216 | int ret = 0; |
| 3217 | |
| 3218 | LTTNG_ASSERT(buf_reg_chan); |
| 3219 | LTTNG_ASSERT(ua_chan); |
| 3220 | |
| 3221 | DBG2("UST app setup buffer registry stream"); |
| 3222 | |
| 3223 | /* Send all streams to application. */ |
| 3224 | for (auto *stream : |
| 3225 | lttng::urcu::list_iteration_adapter<ust_app_stream, &ust_app_stream::list>( |
| 3226 | ua_chan->streams.head)) { |
| 3227 | struct buffer_reg_stream *reg_stream; |
| 3228 | |
| 3229 | ret = buffer_reg_stream_create(®_stream); |
| 3230 | if (ret < 0) { |
| 3231 | goto error; |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * Keep original pointer and nullify it in the stream so the delete |
| 3236 | * stream call does not release the object. |
| 3237 | */ |
| 3238 | reg_stream->obj.ust = stream->obj; |
| 3239 | stream->obj = nullptr; |
| 3240 | buffer_reg_stream_add(reg_stream, buf_reg_chan); |
| 3241 | |
| 3242 | /* We don't need the streams anymore. */ |
| 3243 | cds_list_del(&stream->list); |
| 3244 | delete_ust_app_stream(-1, stream, app); |
| 3245 | } |
| 3246 | |
| 3247 | error: |
| 3248 | return ret; |
| 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | * Create a buffer registry channel for the given session registry and |
| 3253 | * application channel object. If regp pointer is valid, it's set with the |
| 3254 | * created object. Important, the created object is NOT added to the session |
| 3255 | * registry hash table. |
| 3256 | * |
| 3257 | * Return 0 on success else a negative value. |
| 3258 | */ |
| 3259 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 3260 | struct ust_app_channel *ua_chan, |
| 3261 | struct buffer_reg_channel **regp) |
| 3262 | { |
| 3263 | int ret; |
| 3264 | struct buffer_reg_channel *buf_reg_chan = nullptr; |
| 3265 | |
| 3266 | LTTNG_ASSERT(reg_sess); |
| 3267 | LTTNG_ASSERT(ua_chan); |
| 3268 | |
| 3269 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); |
| 3270 | |
| 3271 | /* Create buffer registry channel. */ |
| 3272 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan); |
| 3273 | if (ret < 0) { |
| 3274 | goto error_create; |
| 3275 | } |
| 3276 | LTTNG_ASSERT(buf_reg_chan); |
| 3277 | buf_reg_chan->consumer_key = ua_chan->key; |
| 3278 | buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
| 3279 | buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
| 3280 | |
| 3281 | /* Create and add a channel registry to session. */ |
| 3282 | try { |
| 3283 | reg_sess->reg.ust->add_channel(ua_chan->tracing_channel_id); |
| 3284 | } catch (const std::exception& ex) { |
| 3285 | ERR("Failed to add a channel registry to userspace registry session: %s", |
| 3286 | ex.what()); |
| 3287 | ret = -1; |
| 3288 | goto error; |
| 3289 | } |
| 3290 | |
| 3291 | buffer_reg_channel_add(reg_sess, buf_reg_chan); |
| 3292 | |
| 3293 | if (regp) { |
| 3294 | *regp = buf_reg_chan; |
| 3295 | } |
| 3296 | |
| 3297 | return 0; |
| 3298 | |
| 3299 | error: |
| 3300 | /* Safe because the registry channel object was not added to any HT. */ |
| 3301 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 3302 | error_create: |
| 3303 | return ret; |
| 3304 | } |
| 3305 | |
| 3306 | /* |
| 3307 | * Setup buffer registry channel for the given session registry and application |
| 3308 | * channel object. If regp pointer is valid, it's set with the created object. |
| 3309 | * |
| 3310 | * Return 0 on success else a negative value. |
| 3311 | */ |
| 3312 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 3313 | struct ust_app_channel *ua_chan, |
| 3314 | struct buffer_reg_channel *buf_reg_chan, |
| 3315 | struct ust_app *app) |
| 3316 | { |
| 3317 | int ret; |
| 3318 | |
| 3319 | LTTNG_ASSERT(reg_sess); |
| 3320 | LTTNG_ASSERT(buf_reg_chan); |
| 3321 | LTTNG_ASSERT(ua_chan); |
| 3322 | LTTNG_ASSERT(ua_chan->obj); |
| 3323 | |
| 3324 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
| 3325 | |
| 3326 | /* Setup all streams for the registry. */ |
| 3327 | ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app); |
| 3328 | if (ret < 0) { |
| 3329 | goto error; |
| 3330 | } |
| 3331 | |
| 3332 | buf_reg_chan->obj.ust = ua_chan->obj; |
| 3333 | ua_chan->obj = nullptr; |
| 3334 | |
| 3335 | return 0; |
| 3336 | |
| 3337 | error: |
| 3338 | buffer_reg_channel_remove(reg_sess, buf_reg_chan); |
| 3339 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 3340 | return ret; |
| 3341 | } |
| 3342 | |
| 3343 | /* |
| 3344 | * Send buffer registry channel to the application. |
| 3345 | * |
| 3346 | * Return 0 on success else a negative value. |
| 3347 | */ |
| 3348 | static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan, |
| 3349 | struct ust_app *app, |
| 3350 | struct ust_app_session *ua_sess, |
| 3351 | struct ust_app_channel *ua_chan) |
| 3352 | { |
| 3353 | int ret; |
| 3354 | |
| 3355 | LTTNG_ASSERT(buf_reg_chan); |
| 3356 | LTTNG_ASSERT(app); |
| 3357 | LTTNG_ASSERT(ua_sess); |
| 3358 | LTTNG_ASSERT(ua_chan); |
| 3359 | |
| 3360 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); |
| 3361 | |
| 3362 | ret = duplicate_channel_object(buf_reg_chan, ua_chan); |
| 3363 | if (ret < 0) { |
| 3364 | goto error; |
| 3365 | } |
| 3366 | |
| 3367 | /* Send channel to the application. */ |
| 3368 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 3369 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 3370 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 3371 | goto error; |
| 3372 | } else if (ret == -EAGAIN) { |
| 3373 | /* Caused by timeout. */ |
| 3374 | WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 |
| 3375 | "\".", |
| 3376 | app->pid, |
| 3377 | ua_chan->name, |
| 3378 | ua_sess->tracing_id); |
| 3379 | /* Treat this the same way as an application that is exiting. */ |
| 3380 | ret = -ENOTCONN; |
| 3381 | goto error; |
| 3382 | } else if (ret < 0) { |
| 3383 | goto error; |
| 3384 | } |
| 3385 | |
| 3386 | health_code_update(); |
| 3387 | |
| 3388 | /* Send all streams to application. */ |
| 3389 | pthread_mutex_lock(&buf_reg_chan->stream_list_lock); |
| 3390 | for (auto *reg_stream : |
| 3391 | lttng::urcu::list_iteration_adapter<buffer_reg_stream, &buffer_reg_stream::lnode>( |
| 3392 | buf_reg_chan->streams)) { |
| 3393 | struct ust_app_stream stream = {}; |
| 3394 | |
| 3395 | ret = duplicate_stream_object(reg_stream, &stream); |
| 3396 | if (ret < 0) { |
| 3397 | goto error_stream_unlock; |
| 3398 | } |
| 3399 | |
| 3400 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); |
| 3401 | if (ret < 0) { |
| 3402 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 3403 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 3404 | } else if (ret == -EAGAIN) { |
| 3405 | /* |
| 3406 | * Caused by timeout. |
| 3407 | * Treat this the same way as an application |
| 3408 | * that is exiting. |
| 3409 | */ |
| 3410 | WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64 |
| 3411 | "\".", |
| 3412 | app->pid, |
| 3413 | ua_chan->name, |
| 3414 | ua_sess->tracing_id); |
| 3415 | ret = -ENOTCONN; |
| 3416 | } |
| 3417 | (void) release_ust_app_stream(-1, &stream, app); |
| 3418 | goto error_stream_unlock; |
| 3419 | } |
| 3420 | |
| 3421 | /* |
| 3422 | * The return value is not important here. This function will output an |
| 3423 | * error if needed. |
| 3424 | */ |
| 3425 | (void) release_ust_app_stream(-1, &stream, app); |
| 3426 | } |
| 3427 | |
| 3428 | error_stream_unlock: |
| 3429 | pthread_mutex_unlock(&buf_reg_chan->stream_list_lock); |
| 3430 | error: |
| 3431 | return ret; |
| 3432 | } |
| 3433 | |
| 3434 | /* |
| 3435 | * Create and send to the application the created buffers with per UID buffers. |
| 3436 | * |
| 3437 | * This MUST be called with a RCU read side lock acquired. |
| 3438 | * The session list lock and the session's lock must be acquired. |
| 3439 | * |
| 3440 | * Return 0 on success else a negative value. |
| 3441 | */ |
| 3442 | static int create_channel_per_uid(struct ust_app *app, |
| 3443 | struct ltt_ust_session *usess, |
| 3444 | struct ust_app_session *ua_sess, |
| 3445 | struct ust_app_channel *ua_chan) |
| 3446 | { |
| 3447 | int ret; |
| 3448 | struct buffer_reg_uid *reg_uid; |
| 3449 | struct buffer_reg_channel *buf_reg_chan; |
| 3450 | enum lttng_error_code notification_ret; |
| 3451 | |
| 3452 | LTTNG_ASSERT(app); |
| 3453 | LTTNG_ASSERT(usess); |
| 3454 | LTTNG_ASSERT(ua_sess); |
| 3455 | LTTNG_ASSERT(ua_chan); |
| 3456 | ASSERT_RCU_READ_LOCKED(); |
| 3457 | |
| 3458 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); |
| 3459 | |
| 3460 | /* Guaranteed to exist; will not throw. */ |
| 3461 | const auto session = ltt_session::find_session(ua_sess->tracing_id); |
| 3462 | ASSERT_SESSION_LIST_LOCKED(); |
| 3463 | |
| 3464 | reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid); |
| 3465 | /* |
| 3466 | * The session creation handles the creation of this global registry |
| 3467 | * object. If none can be find, there is a code flow problem or a |
| 3468 | * teardown race. |
| 3469 | */ |
| 3470 | LTTNG_ASSERT(reg_uid); |
| 3471 | |
| 3472 | buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, reg_uid); |
| 3473 | if (buf_reg_chan) { |
| 3474 | goto send_channel; |
| 3475 | } |
| 3476 | |
| 3477 | /* Create the buffer registry channel object. */ |
| 3478 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan); |
| 3479 | if (ret < 0) { |
| 3480 | ERR("Error creating the UST channel \"%s\" registry instance", ua_chan->name); |
| 3481 | goto error; |
| 3482 | } |
| 3483 | |
| 3484 | /* |
| 3485 | * Create the buffers on the consumer side. This call populates the |
| 3486 | * ust app channel object with all streams and data object. |
| 3487 | */ |
| 3488 | ret = do_consumer_create_channel( |
| 3489 | usess, ua_sess, ua_chan, app->abi.bits_per_long, reg_uid->registry->reg.ust); |
| 3490 | if (ret < 0) { |
| 3491 | ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name); |
| 3492 | |
| 3493 | /* |
| 3494 | * Let's remove the previously created buffer registry channel so |
| 3495 | * it's not visible anymore in the session registry. |
| 3496 | */ |
| 3497 | auto locked_registry = reg_uid->registry->reg.ust->lock(); |
| 3498 | try { |
| 3499 | locked_registry->remove_channel(ua_chan->tracing_channel_id, false); |
| 3500 | } catch (const std::exception& ex) { |
| 3501 | DBG("Could not find channel for removal: %s", ex.what()); |
| 3502 | } |
| 3503 | buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan); |
| 3504 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 3505 | goto error; |
| 3506 | } |
| 3507 | |
| 3508 | /* |
| 3509 | * Setup the streams and add it to the session registry. |
| 3510 | */ |
| 3511 | ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, buf_reg_chan, app); |
| 3512 | if (ret < 0) { |
| 3513 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); |
| 3514 | goto error; |
| 3515 | } |
| 3516 | |
| 3517 | { |
| 3518 | auto locked_registry = reg_uid->registry->reg.ust->lock(); |
| 3519 | auto& ust_reg_chan = locked_registry->channel(ua_chan->tracing_channel_id); |
| 3520 | |
| 3521 | ust_reg_chan._consumer_key = ua_chan->key; |
| 3522 | } |
| 3523 | |
| 3524 | /* Notify the notification subsystem of the channel's creation. */ |
| 3525 | notification_ret = notification_thread_command_add_channel( |
| 3526 | the_notification_thread_handle, |
| 3527 | session->id, |
| 3528 | ua_chan->name, |
| 3529 | ua_chan->key, |
| 3530 | LTTNG_DOMAIN_UST, |
| 3531 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
| 3532 | if (notification_ret != LTTNG_OK) { |
| 3533 | ret = -(int) notification_ret; |
| 3534 | ERR("Failed to add channel to notification thread"); |
| 3535 | goto error; |
| 3536 | } |
| 3537 | |
| 3538 | send_channel: |
| 3539 | /* Send buffers to the application. */ |
| 3540 | ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan); |
| 3541 | if (ret < 0) { |
| 3542 | if (ret != -ENOTCONN) { |
| 3543 | ERR("Error sending channel to application"); |
| 3544 | } |
| 3545 | goto error; |
| 3546 | } |
| 3547 | |
| 3548 | error: |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
| 3552 | /* |
| 3553 | * Create and send to the application the created buffers with per PID buffers. |
| 3554 | * |
| 3555 | * Called with UST app session lock held. |
| 3556 | * The session list lock and the session's lock must be acquired. |
| 3557 | * |
| 3558 | * Return 0 on success else a negative value. |
| 3559 | */ |
| 3560 | static int create_channel_per_pid(struct ust_app *app, |
| 3561 | struct ltt_ust_session *usess, |
| 3562 | const ust_app_session::locked_weak_ref& ua_sess, |
| 3563 | struct ust_app_channel *ua_chan) |
| 3564 | { |
| 3565 | int ret; |
| 3566 | lsu::registry_session *registry; |
| 3567 | enum lttng_error_code cmd_ret; |
| 3568 | uint64_t chan_reg_key; |
| 3569 | |
| 3570 | LTTNG_ASSERT(app); |
| 3571 | LTTNG_ASSERT(usess); |
| 3572 | LTTNG_ASSERT(ua_chan); |
| 3573 | |
| 3574 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); |
| 3575 | |
| 3576 | const lttng::urcu::read_lock_guard read_lock; |
| 3577 | |
| 3578 | registry = ust_app_get_session_registry(ua_sess->get_identifier()); |
| 3579 | /* The UST app session lock is held, registry shall not be null. */ |
| 3580 | LTTNG_ASSERT(registry); |
| 3581 | |
| 3582 | /* Guaranteed to exist; will not throw. */ |
| 3583 | const auto session = ltt_session::find_session(ua_sess->tracing_id); |
| 3584 | ASSERT_LOCKED(session->_lock); |
| 3585 | ASSERT_SESSION_LIST_LOCKED(); |
| 3586 | |
| 3587 | /* Create and add a new channel registry to session. */ |
| 3588 | try { |
| 3589 | registry->add_channel(ua_chan->key); |
| 3590 | } catch (const std::exception& ex) { |
| 3591 | ERR("Error creating the UST channel \"%s\" registry instance: %s", |
| 3592 | ua_chan->name, |
| 3593 | ex.what()); |
| 3594 | ret = -1; |
| 3595 | goto error; |
| 3596 | } |
| 3597 | |
| 3598 | /* Create and get channel on the consumer side. */ |
| 3599 | ret = do_consumer_create_channel( |
| 3600 | usess, &ua_sess.get(), ua_chan, app->abi.bits_per_long, registry); |
| 3601 | if (ret < 0) { |
| 3602 | ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name); |
| 3603 | goto error_remove_from_registry; |
| 3604 | } |
| 3605 | |
| 3606 | ret = send_channel_pid_to_ust(app, &ua_sess.get(), ua_chan); |
| 3607 | if (ret < 0) { |
| 3608 | if (ret != -ENOTCONN) { |
| 3609 | ERR("Error sending channel to application"); |
| 3610 | } |
| 3611 | goto error_remove_from_registry; |
| 3612 | } |
| 3613 | |
| 3614 | chan_reg_key = ua_chan->key; |
| 3615 | { |
| 3616 | auto locked_registry = registry->lock(); |
| 3617 | |
| 3618 | auto& ust_reg_chan = locked_registry->channel(chan_reg_key); |
| 3619 | ust_reg_chan._consumer_key = ua_chan->key; |
| 3620 | } |
| 3621 | |
| 3622 | cmd_ret = notification_thread_command_add_channel(the_notification_thread_handle, |
| 3623 | session->id, |
| 3624 | ua_chan->name, |
| 3625 | ua_chan->key, |
| 3626 | LTTNG_DOMAIN_UST, |
| 3627 | ua_chan->attr.subbuf_size * |
| 3628 | ua_chan->attr.num_subbuf); |
| 3629 | if (cmd_ret != LTTNG_OK) { |
| 3630 | ret = -(int) cmd_ret; |
| 3631 | ERR("Failed to add channel to notification thread"); |
| 3632 | goto error_remove_from_registry; |
| 3633 | } |
| 3634 | |
| 3635 | error_remove_from_registry: |
| 3636 | if (ret) { |
| 3637 | try { |
| 3638 | auto locked_registry = registry->lock(); |
| 3639 | locked_registry->remove_channel(ua_chan->key, false); |
| 3640 | } catch (const std::exception& ex) { |
| 3641 | DBG("Could not find channel for removal: %s", ex.what()); |
| 3642 | } |
| 3643 | } |
| 3644 | error: |
| 3645 | return ret; |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | * From an already allocated ust app channel, create the channel buffers if |
| 3650 | * needed and send them to the application. This MUST be called with a RCU read |
| 3651 | * side lock acquired. |
| 3652 | * |
| 3653 | * Called with UST app session lock held. |
| 3654 | * |
| 3655 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
| 3656 | * the application exited concurrently. |
| 3657 | */ |
| 3658 | static int ust_app_channel_send(struct ust_app *app, |
| 3659 | struct ltt_ust_session *usess, |
| 3660 | const ust_app_session::locked_weak_ref& ua_sess, |
| 3661 | struct ust_app_channel *ua_chan) |
| 3662 | { |
| 3663 | int ret; |
| 3664 | |
| 3665 | LTTNG_ASSERT(app); |
| 3666 | LTTNG_ASSERT(usess); |
| 3667 | LTTNG_ASSERT(usess->active); |
| 3668 | LTTNG_ASSERT(ua_chan); |
| 3669 | ASSERT_RCU_READ_LOCKED(); |
| 3670 | |
| 3671 | /* Handle buffer type before sending the channel to the application. */ |
| 3672 | switch (usess->buffer_type) { |
| 3673 | case LTTNG_BUFFER_PER_UID: |
| 3674 | { |
| 3675 | ret = create_channel_per_uid(app, usess, &ua_sess.get(), ua_chan); |
| 3676 | if (ret < 0) { |
| 3677 | goto error; |
| 3678 | } |
| 3679 | break; |
| 3680 | } |
| 3681 | case LTTNG_BUFFER_PER_PID: |
| 3682 | { |
| 3683 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); |
| 3684 | if (ret < 0) { |
| 3685 | goto error; |
| 3686 | } |
| 3687 | break; |
| 3688 | } |
| 3689 | default: |
| 3690 | abort(); |
| 3691 | ret = -EINVAL; |
| 3692 | goto error; |
| 3693 | } |
| 3694 | |
| 3695 | /* Initialize ust objd object using the received handle and add it. */ |
| 3696 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); |
| 3697 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); |
| 3698 | |
| 3699 | /* If channel is not enabled, disable it on the tracer */ |
| 3700 | if (!ua_chan->enabled) { |
| 3701 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 3702 | if (ret < 0) { |
| 3703 | goto error; |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | error: |
| 3708 | return ret; |
| 3709 | } |
| 3710 | |
| 3711 | /* |
| 3712 | * Create UST app channel and return it through ua_chanp if not NULL. |
| 3713 | * |
| 3714 | * Called with UST app session lock and RCU read-side lock held. |
| 3715 | * |
| 3716 | * Return 0 on success or else a negative value. |
| 3717 | */ |
| 3718 | static int ust_app_channel_allocate(const ust_app_session::locked_weak_ref& ua_sess, |
| 3719 | struct ltt_ust_channel *uchan, |
| 3720 | enum lttng_ust_abi_chan_type type, |
| 3721 | struct ltt_ust_session *usess __attribute__((unused)), |
| 3722 | struct ust_app_channel **ua_chanp) |
| 3723 | { |
| 3724 | int ret = 0; |
| 3725 | struct lttng_ht_iter iter; |
| 3726 | struct lttng_ht_node_str *ua_chan_node; |
| 3727 | struct ust_app_channel *ua_chan; |
| 3728 | |
| 3729 | ASSERT_RCU_READ_LOCKED(); |
| 3730 | |
| 3731 | /* Lookup channel in the ust app session */ |
| 3732 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); |
| 3733 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
| 3734 | if (ua_chan_node != nullptr) { |
| 3735 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 3736 | goto end; |
| 3737 | } |
| 3738 | |
| 3739 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 3740 | if (ua_chan == nullptr) { |
| 3741 | /* Only malloc can fail here */ |
| 3742 | ret = -ENOMEM; |
| 3743 | goto error; |
| 3744 | } |
| 3745 | shadow_copy_channel(ua_chan, uchan); |
| 3746 | |
| 3747 | /* Set channel type. */ |
| 3748 | ua_chan->attr.type = type; |
| 3749 | |
| 3750 | /* Only add the channel if successful on the tracer side. */ |
| 3751 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 3752 | end: |
| 3753 | if (ua_chanp) { |
| 3754 | *ua_chanp = ua_chan; |
| 3755 | } |
| 3756 | |
| 3757 | /* Everything went well. */ |
| 3758 | return 0; |
| 3759 | |
| 3760 | error: |
| 3761 | return ret; |
| 3762 | } |
| 3763 | |
| 3764 | /* |
| 3765 | * Create UST app event and create it on the tracer side. |
| 3766 | * |
| 3767 | * Must be called with the RCU read side lock held. |
| 3768 | * Called with ust app session mutex held. |
| 3769 | */ |
| 3770 | static int create_ust_app_event(struct ust_app_channel *ua_chan, |
| 3771 | struct ltt_ust_event *uevent, |
| 3772 | struct ust_app *app) |
| 3773 | { |
| 3774 | int ret = 0; |
| 3775 | struct ust_app_event *ua_event; |
| 3776 | |
| 3777 | ASSERT_RCU_READ_LOCKED(); |
| 3778 | |
| 3779 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 3780 | if (ua_event == nullptr) { |
| 3781 | /* Only failure mode of alloc_ust_app_event(). */ |
| 3782 | ret = -ENOMEM; |
| 3783 | goto end; |
| 3784 | } |
| 3785 | shadow_copy_event(ua_event, uevent); |
| 3786 | |
| 3787 | /* Create it on the tracer side */ |
| 3788 | ret = create_ust_event(app, ua_chan, ua_event); |
| 3789 | if (ret < 0) { |
| 3790 | /* |
| 3791 | * Not found previously means that it does not exist on the |
| 3792 | * tracer. If the application reports that the event existed, |
| 3793 | * it means there is a bug in the sessiond or lttng-ust |
| 3794 | * (or corruption, etc.) |
| 3795 | */ |
| 3796 | if (ret == -LTTNG_UST_ERR_EXIST) { |
| 3797 | ERR("Tracer for application reported that an event being created already existed: " |
| 3798 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", |
| 3799 | uevent->attr.name, |
| 3800 | app->pid, |
| 3801 | app->ppid, |
| 3802 | app->uid, |
| 3803 | app->gid); |
| 3804 | } |
| 3805 | goto error; |
| 3806 | } |
| 3807 | |
| 3808 | add_unique_ust_app_event(ua_chan, ua_event); |
| 3809 | |
| 3810 | DBG2("UST app create event completed: app = '%s' pid = %d", app->name, app->pid); |
| 3811 | |
| 3812 | end: |
| 3813 | return ret; |
| 3814 | |
| 3815 | error: |
| 3816 | /* Valid. Calling here is already in a read side lock */ |
| 3817 | delete_ust_app_event(-1, ua_event, app); |
| 3818 | return ret; |
| 3819 | } |
| 3820 | |
| 3821 | /* |
| 3822 | * Create UST app event notifier rule and create it on the tracer side. |
| 3823 | * |
| 3824 | * Must be called with the RCU read side lock held. |
| 3825 | * Called with ust app session mutex held. |
| 3826 | */ |
| 3827 | static int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, struct ust_app *app) |
| 3828 | { |
| 3829 | int ret = 0; |
| 3830 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; |
| 3831 | |
| 3832 | ASSERT_RCU_READ_LOCKED(); |
| 3833 | |
| 3834 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
| 3835 | if (ua_event_notifier_rule == nullptr) { |
| 3836 | ret = -ENOMEM; |
| 3837 | goto end; |
| 3838 | } |
| 3839 | |
| 3840 | /* Create it on the tracer side. */ |
| 3841 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); |
| 3842 | if (ret < 0) { |
| 3843 | /* |
| 3844 | * Not found previously means that it does not exist on the |
| 3845 | * tracer. If the application reports that the event existed, |
| 3846 | * it means there is a bug in the sessiond or lttng-ust |
| 3847 | * (or corruption, etc.) |
| 3848 | */ |
| 3849 | if (ret == -LTTNG_UST_ERR_EXIST) { |
| 3850 | ERR("Tracer for application reported that an event notifier being created already exists: " |
| 3851 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", |
| 3852 | lttng_trigger_get_tracer_token(trigger), |
| 3853 | app->pid, |
| 3854 | app->ppid, |
| 3855 | app->uid, |
| 3856 | app->gid); |
| 3857 | } |
| 3858 | goto error; |
| 3859 | } |
| 3860 | |
| 3861 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, |
| 3862 | &ua_event_notifier_rule->node); |
| 3863 | |
| 3864 | DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64, |
| 3865 | app->name, |
| 3866 | app->pid, |
| 3867 | lttng_trigger_get_tracer_token(trigger)); |
| 3868 | |
| 3869 | goto end; |
| 3870 | |
| 3871 | error: |
| 3872 | /* The RCU read side lock is already being held by the caller. */ |
| 3873 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); |
| 3874 | end: |
| 3875 | return ret; |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * Create UST metadata and open it on the tracer side. |
| 3880 | * |
| 3881 | * Called with UST app session lock held and RCU read side lock. |
| 3882 | */ |
| 3883 | static int create_ust_app_metadata(const ust_app_session::locked_weak_ref& ua_sess, |
| 3884 | struct ust_app *app, |
| 3885 | struct consumer_output *consumer) |
| 3886 | { |
| 3887 | int ret = 0; |
| 3888 | struct ust_app_channel *metadata; |
| 3889 | struct consumer_socket *socket; |
| 3890 | |
| 3891 | LTTNG_ASSERT(app); |
| 3892 | LTTNG_ASSERT(consumer); |
| 3893 | ASSERT_RCU_READ_LOCKED(); |
| 3894 | |
| 3895 | auto locked_registry = get_locked_session_registry(ua_sess->get_identifier()); |
| 3896 | /* The UST app session is held registry shall not be null. */ |
| 3897 | LTTNG_ASSERT(locked_registry); |
| 3898 | |
| 3899 | /* Guaranteed to exist; will not throw. */ |
| 3900 | const auto session = ltt_session::find_session(ua_sess->tracing_id); |
| 3901 | ASSERT_LOCKED(session->_lock); |
| 3902 | ASSERT_SESSION_LIST_LOCKED(); |
| 3903 | |
| 3904 | /* Metadata already exists for this registry or it was closed previously */ |
| 3905 | if (locked_registry->_metadata_key || locked_registry->_metadata_closed) { |
| 3906 | ret = 0; |
| 3907 | goto error; |
| 3908 | } |
| 3909 | |
| 3910 | /* Allocate UST metadata */ |
| 3911 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, nullptr); |
| 3912 | if (!metadata) { |
| 3913 | /* malloc() failed */ |
| 3914 | ret = -ENOMEM; |
| 3915 | goto error; |
| 3916 | } |
| 3917 | |
| 3918 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
| 3919 | |
| 3920 | /* Need one fd for the channel. */ |
| 3921 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3922 | if (ret < 0) { |
| 3923 | ERR("Exhausted number of available FD upon create metadata"); |
| 3924 | goto error; |
| 3925 | } |
| 3926 | |
| 3927 | /* Get the right consumer socket for the application. */ |
| 3928 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, consumer); |
| 3929 | if (!socket) { |
| 3930 | ret = -EINVAL; |
| 3931 | goto error_consumer; |
| 3932 | } |
| 3933 | |
| 3934 | /* |
| 3935 | * Keep metadata key so we can identify it on the consumer side. Assign it |
| 3936 | * to the registry *before* we ask the consumer so we avoid the race of the |
| 3937 | * consumer requesting the metadata and the ask_channel call on our side |
| 3938 | * did not returned yet. |
| 3939 | */ |
| 3940 | locked_registry->_metadata_key = metadata->key; |
| 3941 | |
| 3942 | /* |
| 3943 | * Ask the metadata channel creation to the consumer. The metadata object |
| 3944 | * will be created by the consumer and kept their. However, the stream is |
| 3945 | * never added or monitored until we do a first push metadata to the |
| 3946 | * consumer. |
| 3947 | */ |
| 3948 | ret = ust_consumer_ask_channel(&ua_sess.get(), |
| 3949 | metadata, |
| 3950 | consumer, |
| 3951 | socket, |
| 3952 | locked_registry.get(), |
| 3953 | session->current_trace_chunk); |
| 3954 | if (ret < 0) { |
| 3955 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 3956 | locked_registry->_metadata_key = 0; |
| 3957 | goto error_consumer; |
| 3958 | } |
| 3959 | |
| 3960 | /* |
| 3961 | * The setup command will make the metadata stream be sent to the relayd, |
| 3962 | * if applicable, and the thread managing the metadatas. This is important |
| 3963 | * because after this point, if an error occurs, the only way the stream |
| 3964 | * can be deleted is to be monitored in the consumer. |
| 3965 | */ |
| 3966 | ret = consumer_setup_metadata(socket, metadata->key); |
| 3967 | if (ret < 0) { |
| 3968 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 3969 | locked_registry->_metadata_key = 0; |
| 3970 | goto error_consumer; |
| 3971 | } |
| 3972 | |
| 3973 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", metadata->key, app->pid); |
| 3974 | |
| 3975 | error_consumer: |
| 3976 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3977 | delete_ust_app_channel(-1, metadata, app, locked_registry); |
| 3978 | error: |
| 3979 | return ret; |
| 3980 | } |
| 3981 | |
| 3982 | /* |
| 3983 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
| 3984 | * acquired before calling this function. |
| 3985 | */ |
| 3986 | struct ust_app *ust_app_find_by_pid(pid_t pid) |
| 3987 | { |
| 3988 | struct ust_app *app = nullptr; |
| 3989 | struct lttng_ht_node_ulong *node; |
| 3990 | struct lttng_ht_iter iter; |
| 3991 | |
| 3992 | lttng_ht_lookup(ust_app_ht, (void *) ((unsigned long) pid), &iter); |
| 3993 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 3994 | if (node == nullptr) { |
| 3995 | DBG2("UST app no found with pid %d", pid); |
| 3996 | goto error; |
| 3997 | } |
| 3998 | |
| 3999 | DBG2("Found UST app by pid %d", pid); |
| 4000 | |
| 4001 | app = lttng::utils::container_of(node, &ust_app::pid_n); |
| 4002 | |
| 4003 | error: |
| 4004 | return app; |
| 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | * Allocate and init an UST app object using the registration information and |
| 4009 | * the command socket. This is called when the command socket connects to the |
| 4010 | * session daemon. |
| 4011 | * |
| 4012 | * The object is returned on success or else NULL. |
| 4013 | */ |
| 4014 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
| 4015 | { |
| 4016 | int ret; |
| 4017 | struct ust_app *lta = nullptr; |
| 4018 | struct lttng_pipe *event_notifier_event_source_pipe = nullptr; |
| 4019 | |
| 4020 | LTTNG_ASSERT(msg); |
| 4021 | LTTNG_ASSERT(sock >= 0); |
| 4022 | |
| 4023 | DBG3("UST app creating application for socket %d", sock); |
| 4024 | |
| 4025 | if ((msg->bits_per_long == 64 && (uatomic_read(&the_ust_consumerd64_fd) == -EINVAL)) || |
| 4026 | (msg->bits_per_long == 32 && (uatomic_read(&the_ust_consumerd32_fd) == -EINVAL))) { |
| 4027 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
| 4028 | "%d-bit long, but no consumerd for this size is available.\n", |
| 4029 | msg->name, |
| 4030 | msg->pid, |
| 4031 | msg->bits_per_long); |
| 4032 | goto error; |
| 4033 | } |
| 4034 | |
| 4035 | /* |
| 4036 | * Reserve the two file descriptors of the event source pipe. The write |
| 4037 | * end will be closed once it is passed to the application, at which |
| 4038 | * point a single 'put' will be performed. |
| 4039 | */ |
| 4040 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 4041 | if (ret) { |
| 4042 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d", |
| 4043 | msg->name, |
| 4044 | (int) msg->pid); |
| 4045 | goto error; |
| 4046 | } |
| 4047 | |
| 4048 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 4049 | if (!event_notifier_event_source_pipe) { |
| 4050 | PERROR("Failed to open application event source pipe: '%s' (pid = %d)", |
| 4051 | msg->name, |
| 4052 | msg->pid); |
| 4053 | goto error; |
| 4054 | } |
| 4055 | |
| 4056 | try { |
| 4057 | lta = new ust_app; |
| 4058 | } catch (const std::bad_alloc&) { |
| 4059 | ERR_FMT("Failed to allocate ust application instance: name=`{}`, pid={}, uid={}", |
| 4060 | msg->name, |
| 4061 | msg->pid, |
| 4062 | msg->uid); |
| 4063 | goto error_free_pipe; |
| 4064 | } |
| 4065 | |
| 4066 | urcu_ref_init(<a->ref); |
| 4067 | |
| 4068 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
| 4069 | |
| 4070 | lta->ppid = msg->ppid; |
| 4071 | lta->uid = msg->uid; |
| 4072 | lta->gid = msg->gid; |
| 4073 | |
| 4074 | lta->abi = { |
| 4075 | .bits_per_long = msg->bits_per_long, |
| 4076 | .long_alignment = msg->long_alignment, |
| 4077 | .uint8_t_alignment = msg->uint8_t_alignment, |
| 4078 | .uint16_t_alignment = msg->uint16_t_alignment, |
| 4079 | .uint32_t_alignment = msg->uint32_t_alignment, |
| 4080 | .uint64_t_alignment = msg->uint64_t_alignment, |
| 4081 | .byte_order = msg->byte_order == LITTLE_ENDIAN ? |
| 4082 | lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_ : |
| 4083 | lttng::sessiond::trace::byte_order::BIG_ENDIAN_, |
| 4084 | }; |
| 4085 | |
| 4086 | lta->v_major = msg->major; |
| 4087 | lta->v_minor = msg->minor; |
| 4088 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 4089 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 4090 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 4091 | lta->notify_sock = -1; |
| 4092 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 4093 | |
| 4094 | /* Copy name and make sure it's NULL terminated. */ |
| 4095 | strncpy(lta->name, msg->name, sizeof(lta->name)); |
| 4096 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; |
| 4097 | |
| 4098 | /* |
| 4099 | * Before this can be called, when receiving the registration information, |
| 4100 | * the application compatibility is checked. So, at this point, the |
| 4101 | * application can work with this session daemon. |
| 4102 | */ |
| 4103 | lta->compatible = 1; |
| 4104 | |
| 4105 | lta->pid = msg->pid; |
| 4106 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
| 4107 | lta->sock = sock; |
| 4108 | pthread_mutex_init(<a->sock_lock, nullptr); |
| 4109 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
| 4110 | |
| 4111 | return lta; |
| 4112 | |
| 4113 | error_free_pipe: |
| 4114 | lttng_pipe_destroy(event_notifier_event_source_pipe); |
| 4115 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 4116 | error: |
| 4117 | return nullptr; |
| 4118 | } |
| 4119 | |
| 4120 | /* |
| 4121 | * For a given application object, add it to every hash table. |
| 4122 | */ |
| 4123 | void ust_app_add(struct ust_app *app) |
| 4124 | { |
| 4125 | LTTNG_ASSERT(app); |
| 4126 | LTTNG_ASSERT(app->notify_sock >= 0); |
| 4127 | |
| 4128 | app->registration_time = time(nullptr); |
| 4129 | |
| 4130 | const lttng::urcu::read_lock_guard read_lock; |
| 4131 | |
| 4132 | /* |
| 4133 | * On a re-registration, we want to kick out the previous registration of |
| 4134 | * that pid |
| 4135 | */ |
| 4136 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
| 4137 | |
| 4138 | /* |
| 4139 | * The socket _should_ be unique until _we_ call close. So, a add_unique |
| 4140 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was |
| 4141 | * already in the table. |
| 4142 | */ |
| 4143 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
| 4144 | |
| 4145 | /* Add application to the notify socket hash table. */ |
| 4146 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); |
| 4147 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); |
| 4148 | |
| 4149 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s " |
| 4150 | "notify_sock =%d (version %d.%d)", |
| 4151 | app->pid, |
| 4152 | app->ppid, |
| 4153 | app->uid, |
| 4154 | app->gid, |
| 4155 | app->sock, |
| 4156 | app->name, |
| 4157 | app->notify_sock, |
| 4158 | app->v_major, |
| 4159 | app->v_minor); |
| 4160 | } |
| 4161 | |
| 4162 | /* |
| 4163 | * Set the application version into the object. |
| 4164 | * |
| 4165 | * Return 0 on success else a negative value either an errno code or a |
| 4166 | * LTTng-UST error code. |
| 4167 | */ |
| 4168 | int ust_app_version(struct ust_app *app) |
| 4169 | { |
| 4170 | int ret; |
| 4171 | |
| 4172 | LTTNG_ASSERT(app); |
| 4173 | |
| 4174 | pthread_mutex_lock(&app->sock_lock); |
| 4175 | ret = lttng_ust_ctl_tracer_version(app->sock, &app->version); |
| 4176 | pthread_mutex_unlock(&app->sock_lock); |
| 4177 | if (ret < 0) { |
| 4178 | if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) { |
| 4179 | DBG3("UST app version failed. Application is dead: pid = %d, sock = %d", |
| 4180 | app->pid, |
| 4181 | app->sock); |
| 4182 | } else if (ret == -EAGAIN) { |
| 4183 | WARN("UST app version failed. Communication time out: pid = %d, sock = %d", |
| 4184 | app->pid, |
| 4185 | app->sock); |
| 4186 | } else { |
| 4187 | ERR("UST app version failed with ret %d: pid = %d, sock = %d", |
| 4188 | ret, |
| 4189 | app->pid, |
| 4190 | app->sock); |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | return ret; |
| 4195 | } |
| 4196 | |
| 4197 | bool ust_app_supports_notifiers(const struct ust_app *app) |
| 4198 | { |
| 4199 | return app->v_major >= 9; |
| 4200 | } |
| 4201 | |
| 4202 | bool ust_app_supports_counters(const struct ust_app *app) |
| 4203 | { |
| 4204 | return app->v_major >= 9; |
| 4205 | } |
| 4206 | |
| 4207 | /* |
| 4208 | * Setup the base event notifier group. |
| 4209 | * |
| 4210 | * Return 0 on success else a negative value either an errno code or a |
| 4211 | * LTTng-UST error code. |
| 4212 | */ |
| 4213 | int ust_app_setup_event_notifier_group(struct ust_app *app) |
| 4214 | { |
| 4215 | int ret; |
| 4216 | int event_pipe_write_fd; |
| 4217 | struct lttng_ust_abi_object_data *event_notifier_group = nullptr; |
| 4218 | enum lttng_error_code lttng_ret; |
| 4219 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
| 4220 | |
| 4221 | LTTNG_ASSERT(app); |
| 4222 | |
| 4223 | if (!ust_app_supports_notifiers(app)) { |
| 4224 | ret = -ENOSYS; |
| 4225 | goto error; |
| 4226 | } |
| 4227 | |
| 4228 | /* Get the write side of the pipe. */ |
| 4229 | event_pipe_write_fd = lttng_pipe_get_writefd(app->event_notifier_group.event_pipe); |
| 4230 | |
| 4231 | pthread_mutex_lock(&app->sock_lock); |
| 4232 | ret = lttng_ust_ctl_create_event_notifier_group( |
| 4233 | app->sock, event_pipe_write_fd, &event_notifier_group); |
| 4234 | pthread_mutex_unlock(&app->sock_lock); |
| 4235 | if (ret < 0) { |
| 4236 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4237 | ret = 0; |
| 4238 | DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d", |
| 4239 | app->pid, |
| 4240 | app->sock); |
| 4241 | } else if (ret == -EAGAIN) { |
| 4242 | ret = 0; |
| 4243 | WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d", |
| 4244 | app->pid, |
| 4245 | app->sock); |
| 4246 | } else { |
| 4247 | ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d", |
| 4248 | ret, |
| 4249 | app->pid, |
| 4250 | app->sock, |
| 4251 | event_pipe_write_fd); |
| 4252 | } |
| 4253 | goto error; |
| 4254 | } |
| 4255 | |
| 4256 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
| 4257 | if (ret) { |
| 4258 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)", |
| 4259 | app->name, |
| 4260 | app->pid); |
| 4261 | goto error; |
| 4262 | } |
| 4263 | |
| 4264 | /* |
| 4265 | * Release the file descriptor that was reserved for the write-end of |
| 4266 | * the pipe. |
| 4267 | */ |
| 4268 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 4269 | |
| 4270 | lttng_ret = notification_thread_command_add_tracer_event_source( |
| 4271 | the_notification_thread_handle, |
| 4272 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe), |
| 4273 | LTTNG_DOMAIN_UST); |
| 4274 | if (lttng_ret != LTTNG_OK) { |
| 4275 | ERR("Failed to add tracer event source to notification thread"); |
| 4276 | ret = -1; |
| 4277 | goto error; |
| 4278 | } |
| 4279 | |
| 4280 | /* Assign handle only when the complete setup is valid. */ |
| 4281 | app->event_notifier_group.object = event_notifier_group; |
| 4282 | |
| 4283 | event_notifier_error_accounting_status = event_notifier_error_accounting_register_app(app); |
| 4284 | switch (event_notifier_error_accounting_status) { |
| 4285 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK: |
| 4286 | break; |
| 4287 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED: |
| 4288 | DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d", |
| 4289 | app->sock, |
| 4290 | app->name, |
| 4291 | (int) app->pid); |
| 4292 | ret = 0; |
| 4293 | goto error_accounting; |
| 4294 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD: |
| 4295 | DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d", |
| 4296 | app->sock, |
| 4297 | app->name, |
| 4298 | (int) app->pid); |
| 4299 | ret = 0; |
| 4300 | goto error_accounting; |
| 4301 | default: |
| 4302 | ERR("Failed to setup event notifier error accounting for app"); |
| 4303 | ret = -1; |
| 4304 | goto error_accounting; |
| 4305 | } |
| 4306 | |
| 4307 | return ret; |
| 4308 | |
| 4309 | error_accounting: |
| 4310 | lttng_ret = notification_thread_command_remove_tracer_event_source( |
| 4311 | the_notification_thread_handle, |
| 4312 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe)); |
| 4313 | if (lttng_ret != LTTNG_OK) { |
| 4314 | ERR("Failed to remove application tracer event source from notification thread"); |
| 4315 | } |
| 4316 | |
| 4317 | error: |
| 4318 | lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object); |
| 4319 | free(app->event_notifier_group.object); |
| 4320 | app->event_notifier_group.object = nullptr; |
| 4321 | return ret; |
| 4322 | } |
| 4323 | |
| 4324 | static void ust_app_unregister(ust_app& app) |
| 4325 | { |
| 4326 | const lttng::urcu::read_lock_guard read_lock; |
| 4327 | |
| 4328 | /* |
| 4329 | * For per-PID buffers, perform "push metadata" and flush all |
| 4330 | * application streams before removing app from hash tables, |
| 4331 | * ensuring proper behavior of data_pending check. |
| 4332 | * Remove sessions so they are not visible during deletion. |
| 4333 | */ |
| 4334 | for (auto *ua_sess : |
| 4335 | lttng::urcu::lfht_iteration_adapter<ust_app_session, |
| 4336 | decltype(ust_app_session::node), |
| 4337 | &ust_app_session::node>(*app.sessions->ht)) { |
| 4338 | const auto del_ret = cds_lfht_del(app.sessions->ht, &ua_sess->node.node); |
| 4339 | if (del_ret) { |
| 4340 | /* The session was already removed so scheduled for teardown. */ |
| 4341 | continue; |
| 4342 | } |
| 4343 | |
| 4344 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 4345 | (void) ust_app_flush_app_session(app, *ua_sess); |
| 4346 | } |
| 4347 | |
| 4348 | /* |
| 4349 | * Add session to list for teardown. This is safe since at this point we |
| 4350 | * are the only one using this list. |
| 4351 | */ |
| 4352 | auto locked_ua_sess = ua_sess->lock(); |
| 4353 | |
| 4354 | if (ua_sess->deleted) { |
| 4355 | continue; |
| 4356 | } |
| 4357 | |
| 4358 | /* |
| 4359 | * Normally, this is done in the delete session process which is |
| 4360 | * executed in the call rcu below. However, upon registration we can't |
| 4361 | * afford to wait for the grace period before pushing data or else the |
| 4362 | * data pending feature can race between the unregistration and stop |
| 4363 | * command where the data pending command is sent *before* the grace |
| 4364 | * period ended. |
| 4365 | * |
| 4366 | * The close metadata below nullifies the metadata pointer in the |
| 4367 | * session so the delete session will NOT push/close a second time. |
| 4368 | */ |
| 4369 | auto locked_registry = |
| 4370 | get_locked_session_registry(locked_ua_sess->get_identifier()); |
| 4371 | if (locked_registry) { |
| 4372 | /* Push metadata for application before freeing the application. */ |
| 4373 | (void) push_metadata(locked_registry, ua_sess->consumer); |
| 4374 | |
| 4375 | /* |
| 4376 | * Don't ask to close metadata for global per UID buffers. Close |
| 4377 | * metadata only on destroy trace session in this case. Also, the |
| 4378 | * previous push metadata could have flag the metadata registry to |
| 4379 | * close so don't send a close command if closed. |
| 4380 | */ |
| 4381 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 4382 | const auto metadata_key = locked_registry->_metadata_key; |
| 4383 | const auto consumer_bitness = locked_registry->abi.bits_per_long; |
| 4384 | |
| 4385 | if (!locked_registry->_metadata_closed && metadata_key != 0) { |
| 4386 | locked_registry->_metadata_closed = true; |
| 4387 | } |
| 4388 | |
| 4389 | /* Release lock before communication, see comments in |
| 4390 | * close_metadata(). */ |
| 4391 | locked_registry.reset(); |
| 4392 | (void) close_metadata( |
| 4393 | metadata_key, consumer_bitness, ua_sess->consumer); |
| 4394 | } else { |
| 4395 | locked_registry.reset(); |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | app.sessions_to_teardown.emplace_back(ua_sess); |
| 4400 | } |
| 4401 | |
| 4402 | /* |
| 4403 | * Remove application from notify hash table. The thread handling the |
| 4404 | * notify socket could have deleted the node so ignore on error because |
| 4405 | * either way it's valid. The close of that socket is handled by the |
| 4406 | * apps_notify_thread. |
| 4407 | */ |
| 4408 | (void) cds_lfht_del(ust_app_ht_by_notify_sock->ht, &app.notify_sock_n.node); |
| 4409 | |
| 4410 | /* |
| 4411 | * Ignore return value since the node might have been removed before by an |
| 4412 | * add replace during app registration because the PID can be reassigned by |
| 4413 | * the OS. |
| 4414 | */ |
| 4415 | if (cds_lfht_del(ust_app_ht->ht, &app.pid_n.node)) { |
| 4416 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", app.pid); |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | /* |
| 4421 | * Unregister app by removing it from the global traceable app list and freeing |
| 4422 | * the data struct. |
| 4423 | * |
| 4424 | * The socket is already closed at this point, so there is no need to close it. |
| 4425 | */ |
| 4426 | void ust_app_unregister_by_socket(int sock_fd) |
| 4427 | { |
| 4428 | struct lttng_ht_node_ulong *node; |
| 4429 | struct lttng_ht_iter ust_app_sock_iter; |
| 4430 | int ret; |
| 4431 | |
| 4432 | const lttng::urcu::read_lock_guard read_lock; |
| 4433 | |
| 4434 | /* Get the node reference for a call_rcu */ |
| 4435 | lttng_ht_lookup(ust_app_ht_by_sock, (void *) ((unsigned long) sock_fd), &ust_app_sock_iter); |
| 4436 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&ust_app_sock_iter); |
| 4437 | assert(node); |
| 4438 | |
| 4439 | auto *app = lttng::utils::container_of(node, &ust_app::sock_n); |
| 4440 | |
| 4441 | DBG_FMT("Application unregistering after socket activity: app={}, socket_fd={}", |
| 4442 | *app, |
| 4443 | sock_fd); |
| 4444 | |
| 4445 | /* Remove application from socket hash table */ |
| 4446 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); |
| 4447 | assert(!ret); |
| 4448 | |
| 4449 | /* |
| 4450 | * The socket is closed: release its reference to the application |
| 4451 | * to trigger its eventual teardown. |
| 4452 | */ |
| 4453 | ust_app_put(app); |
| 4454 | } |
| 4455 | |
| 4456 | /* |
| 4457 | * Fill events array with all events name of all registered apps. |
| 4458 | */ |
| 4459 | int ust_app_list_events(struct lttng_event **events) |
| 4460 | { |
| 4461 | int ret, handle; |
| 4462 | size_t nbmem, count = 0; |
| 4463 | struct lttng_event *tmp_event; |
| 4464 | |
| 4465 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 4466 | tmp_event = calloc<lttng_event>(nbmem); |
| 4467 | if (tmp_event == nullptr) { |
| 4468 | PERROR("zmalloc ust app events"); |
| 4469 | ret = -ENOMEM; |
| 4470 | goto error; |
| 4471 | } |
| 4472 | |
| 4473 | /* Iterate on all apps. */ |
| 4474 | for (auto *app : |
| 4475 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 4476 | *ust_app_ht->ht)) { |
| 4477 | struct lttng_ust_abi_tracepoint_iter uiter; |
| 4478 | |
| 4479 | health_code_update(); |
| 4480 | |
| 4481 | if (!app->compatible) { |
| 4482 | /* |
| 4483 | * TODO: In time, we should notice the caller of this error by |
| 4484 | * telling him that this is a version error. |
| 4485 | */ |
| 4486 | continue; |
| 4487 | } |
| 4488 | |
| 4489 | pthread_mutex_lock(&app->sock_lock); |
| 4490 | handle = lttng_ust_ctl_tracepoint_list(app->sock); |
| 4491 | if (handle < 0) { |
| 4492 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 4493 | ERR("UST app list events getting handle failed for app pid %d", |
| 4494 | app->pid); |
| 4495 | } |
| 4496 | pthread_mutex_unlock(&app->sock_lock); |
| 4497 | continue; |
| 4498 | } |
| 4499 | |
| 4500 | while ((ret = lttng_ust_ctl_tracepoint_list_get(app->sock, handle, &uiter)) != |
| 4501 | -LTTNG_UST_ERR_NOENT) { |
| 4502 | /* Handle ustctl error. */ |
| 4503 | if (ret < 0) { |
| 4504 | int release_ret; |
| 4505 | |
| 4506 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 4507 | ERR("UST app tp list get failed for app %d with ret %d", |
| 4508 | app->sock, |
| 4509 | ret); |
| 4510 | } else { |
| 4511 | DBG3("UST app tp list get failed. Application is dead"); |
| 4512 | break; |
| 4513 | } |
| 4514 | |
| 4515 | free(tmp_event); |
| 4516 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
| 4517 | if (release_ret < 0 && release_ret != -LTTNG_UST_ERR_EXITING && |
| 4518 | release_ret != -EPIPE) { |
| 4519 | ERR("Error releasing app handle for app %d with ret %d", |
| 4520 | app->sock, |
| 4521 | release_ret); |
| 4522 | } |
| 4523 | |
| 4524 | pthread_mutex_unlock(&app->sock_lock); |
| 4525 | goto rcu_error; |
| 4526 | } |
| 4527 | |
| 4528 | health_code_update(); |
| 4529 | if (count >= nbmem) { |
| 4530 | /* In case the realloc fails, we free the memory */ |
| 4531 | struct lttng_event *new_tmp_event; |
| 4532 | size_t new_nbmem; |
| 4533 | |
| 4534 | new_nbmem = nbmem << 1; |
| 4535 | DBG2("Reallocating event list from %zu to %zu entries", |
| 4536 | nbmem, |
| 4537 | new_nbmem); |
| 4538 | new_tmp_event = (lttng_event *) realloc( |
| 4539 | tmp_event, new_nbmem * sizeof(struct lttng_event)); |
| 4540 | if (new_tmp_event == nullptr) { |
| 4541 | int release_ret; |
| 4542 | |
| 4543 | PERROR("realloc ust app events"); |
| 4544 | free(tmp_event); |
| 4545 | ret = -ENOMEM; |
| 4546 | release_ret = |
| 4547 | lttng_ust_ctl_release_handle(app->sock, handle); |
| 4548 | if (release_ret < 0 && |
| 4549 | release_ret != -LTTNG_UST_ERR_EXITING && |
| 4550 | release_ret != -EPIPE) { |
| 4551 | ERR("Error releasing app handle for app %d with ret %d", |
| 4552 | app->sock, |
| 4553 | release_ret); |
| 4554 | } |
| 4555 | |
| 4556 | pthread_mutex_unlock(&app->sock_lock); |
| 4557 | goto rcu_error; |
| 4558 | } |
| 4559 | /* Zero the new memory */ |
| 4560 | memset(new_tmp_event + nbmem, |
| 4561 | 0, |
| 4562 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); |
| 4563 | nbmem = new_nbmem; |
| 4564 | tmp_event = new_tmp_event; |
| 4565 | } |
| 4566 | |
| 4567 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN); |
| 4568 | tmp_event[count].loglevel = uiter.loglevel; |
| 4569 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT; |
| 4570 | tmp_event[count].pid = app->pid; |
| 4571 | tmp_event[count].enabled = -1; |
| 4572 | count++; |
| 4573 | } |
| 4574 | |
| 4575 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
| 4576 | pthread_mutex_unlock(&app->sock_lock); |
| 4577 | if (ret < 0) { |
| 4578 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4579 | DBG3("Error releasing app handle. Application died: pid = %d, sock = %d", |
| 4580 | app->pid, |
| 4581 | app->sock); |
| 4582 | } else if (ret == -EAGAIN) { |
| 4583 | WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d", |
| 4584 | app->pid, |
| 4585 | app->sock); |
| 4586 | } else { |
| 4587 | ERR("Error releasing app handle with ret %d: pid = %d, sock = %d", |
| 4588 | ret, |
| 4589 | app->pid, |
| 4590 | app->sock); |
| 4591 | } |
| 4592 | } |
| 4593 | } |
| 4594 | |
| 4595 | ret = count; |
| 4596 | *events = tmp_event; |
| 4597 | |
| 4598 | DBG2("UST app list events done (%zu events)", count); |
| 4599 | |
| 4600 | rcu_error: |
| 4601 | error: |
| 4602 | health_code_update(); |
| 4603 | return ret; |
| 4604 | } |
| 4605 | |
| 4606 | /* |
| 4607 | * Fill events array with all events name of all registered apps. |
| 4608 | */ |
| 4609 | int ust_app_list_event_fields(struct lttng_event_field **fields) |
| 4610 | { |
| 4611 | int ret, handle; |
| 4612 | size_t nbmem, count = 0; |
| 4613 | struct lttng_event_field *tmp_event; |
| 4614 | |
| 4615 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 4616 | tmp_event = calloc<lttng_event_field>(nbmem); |
| 4617 | if (tmp_event == nullptr) { |
| 4618 | PERROR("zmalloc ust app event fields"); |
| 4619 | ret = -ENOMEM; |
| 4620 | goto error; |
| 4621 | } |
| 4622 | |
| 4623 | /* Iterate on all apps. */ |
| 4624 | for (auto *app : |
| 4625 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 4626 | *ust_app_ht->ht)) { |
| 4627 | struct lttng_ust_abi_field_iter uiter; |
| 4628 | |
| 4629 | health_code_update(); |
| 4630 | |
| 4631 | if (!app->compatible) { |
| 4632 | /* |
| 4633 | * TODO: In time, we should notice the caller of this error by |
| 4634 | * telling him that this is a version error. |
| 4635 | */ |
| 4636 | continue; |
| 4637 | } |
| 4638 | |
| 4639 | pthread_mutex_lock(&app->sock_lock); |
| 4640 | handle = lttng_ust_ctl_tracepoint_field_list(app->sock); |
| 4641 | if (handle < 0) { |
| 4642 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 4643 | ERR("UST app list field getting handle failed for app pid %d", |
| 4644 | app->pid); |
| 4645 | } |
| 4646 | pthread_mutex_unlock(&app->sock_lock); |
| 4647 | continue; |
| 4648 | } |
| 4649 | |
| 4650 | while ((ret = lttng_ust_ctl_tracepoint_field_list_get(app->sock, handle, &uiter)) != |
| 4651 | -LTTNG_UST_ERR_NOENT) { |
| 4652 | /* Handle ustctl error. */ |
| 4653 | if (ret < 0) { |
| 4654 | int release_ret; |
| 4655 | |
| 4656 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 4657 | ERR("UST app tp list field failed for app %d with ret %d", |
| 4658 | app->sock, |
| 4659 | ret); |
| 4660 | } else { |
| 4661 | DBG3("UST app tp list field failed. Application is dead"); |
| 4662 | break; |
| 4663 | } |
| 4664 | |
| 4665 | free(tmp_event); |
| 4666 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
| 4667 | pthread_mutex_unlock(&app->sock_lock); |
| 4668 | if (release_ret < 0 && release_ret != -LTTNG_UST_ERR_EXITING && |
| 4669 | release_ret != -EPIPE) { |
| 4670 | ERR("Error releasing app handle for app %d with ret %d", |
| 4671 | app->sock, |
| 4672 | release_ret); |
| 4673 | } |
| 4674 | |
| 4675 | goto rcu_error; |
| 4676 | } |
| 4677 | |
| 4678 | health_code_update(); |
| 4679 | if (count >= nbmem) { |
| 4680 | /* In case the realloc fails, we free the memory */ |
| 4681 | struct lttng_event_field *new_tmp_event; |
| 4682 | size_t new_nbmem; |
| 4683 | |
| 4684 | new_nbmem = nbmem << 1; |
| 4685 | DBG2("Reallocating event field list from %zu to %zu entries", |
| 4686 | nbmem, |
| 4687 | new_nbmem); |
| 4688 | new_tmp_event = (lttng_event_field *) realloc( |
| 4689 | tmp_event, new_nbmem * sizeof(struct lttng_event_field)); |
| 4690 | if (new_tmp_event == nullptr) { |
| 4691 | int release_ret; |
| 4692 | |
| 4693 | PERROR("realloc ust app event fields"); |
| 4694 | free(tmp_event); |
| 4695 | ret = -ENOMEM; |
| 4696 | release_ret = |
| 4697 | lttng_ust_ctl_release_handle(app->sock, handle); |
| 4698 | pthread_mutex_unlock(&app->sock_lock); |
| 4699 | if (release_ret && release_ret != -LTTNG_UST_ERR_EXITING && |
| 4700 | release_ret != -EPIPE) { |
| 4701 | ERR("Error releasing app handle for app %d with ret %d", |
| 4702 | app->sock, |
| 4703 | release_ret); |
| 4704 | } |
| 4705 | |
| 4706 | goto rcu_error; |
| 4707 | } |
| 4708 | |
| 4709 | /* Zero the new memory */ |
| 4710 | memset(new_tmp_event + nbmem, |
| 4711 | 0, |
| 4712 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); |
| 4713 | nbmem = new_nbmem; |
| 4714 | tmp_event = new_tmp_event; |
| 4715 | } |
| 4716 | |
| 4717 | memcpy(tmp_event[count].field_name, |
| 4718 | uiter.field_name, |
| 4719 | LTTNG_UST_ABI_SYM_NAME_LEN); |
| 4720 | /* Mapping between these enums matches 1 to 1. */ |
| 4721 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; |
| 4722 | tmp_event[count].nowrite = uiter.nowrite; |
| 4723 | |
| 4724 | memcpy(tmp_event[count].event.name, |
| 4725 | uiter.event_name, |
| 4726 | LTTNG_UST_ABI_SYM_NAME_LEN); |
| 4727 | tmp_event[count].event.loglevel = uiter.loglevel; |
| 4728 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
| 4729 | tmp_event[count].event.pid = app->pid; |
| 4730 | tmp_event[count].event.enabled = -1; |
| 4731 | count++; |
| 4732 | } |
| 4733 | |
| 4734 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
| 4735 | pthread_mutex_unlock(&app->sock_lock); |
| 4736 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 4737 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | ret = count; |
| 4742 | *fields = tmp_event; |
| 4743 | |
| 4744 | DBG2("UST app list event fields done (%zu events)", count); |
| 4745 | |
| 4746 | rcu_error: |
| 4747 | error: |
| 4748 | health_code_update(); |
| 4749 | return ret; |
| 4750 | } |
| 4751 | |
| 4752 | /* |
| 4753 | * Free and clean all traceable apps of the global list. |
| 4754 | */ |
| 4755 | void ust_app_clean_list() |
| 4756 | { |
| 4757 | int ret; |
| 4758 | DBG2("UST app cleaning registered apps hash table"); |
| 4759 | |
| 4760 | /* Cleanup notify socket hash table */ |
| 4761 | if (ust_app_ht_by_notify_sock) { |
| 4762 | for (auto *app : |
| 4763 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 4764 | decltype(ust_app::notify_sock_n), |
| 4765 | &ust_app::notify_sock_n>( |
| 4766 | *ust_app_ht_by_notify_sock->ht)) { |
| 4767 | /* |
| 4768 | * Assert that all notifiers are gone as all triggers |
| 4769 | * are unregistered prior to this clean-up. |
| 4770 | */ |
| 4771 | LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); |
| 4772 | ust_app_notify_sock_unregister(app->notify_sock); |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | /* Cleanup socket hash table */ |
| 4777 | if (ust_app_ht_by_sock) { |
| 4778 | const lttng::urcu::read_lock_guard read_lock; |
| 4779 | |
| 4780 | for (auto *app : lttng::urcu::lfht_iteration_adapter<ust_app, |
| 4781 | decltype(ust_app::sock_n), |
| 4782 | &ust_app::sock_n>( |
| 4783 | *ust_app_ht_by_sock->ht)) { |
| 4784 | ret = cds_lfht_del(ust_app_ht_by_sock->ht, &app->sock_n.node); |
| 4785 | LTTNG_ASSERT(!ret); |
| 4786 | ust_app_put(app); |
| 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | /* Destroy is done only when the ht is empty */ |
| 4791 | if (ust_app_ht) { |
| 4792 | lttng_ht_destroy(ust_app_ht); |
| 4793 | } |
| 4794 | if (ust_app_ht_by_sock) { |
| 4795 | lttng_ht_destroy(ust_app_ht_by_sock); |
| 4796 | } |
| 4797 | if (ust_app_ht_by_notify_sock) { |
| 4798 | lttng_ht_destroy(ust_app_ht_by_notify_sock); |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * Init UST app hash table. |
| 4804 | */ |
| 4805 | int ust_app_ht_alloc() |
| 4806 | { |
| 4807 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 4808 | if (!ust_app_ht) { |
| 4809 | return -1; |
| 4810 | } |
| 4811 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 4812 | if (!ust_app_ht_by_sock) { |
| 4813 | return -1; |
| 4814 | } |
| 4815 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 4816 | if (!ust_app_ht_by_notify_sock) { |
| 4817 | return -1; |
| 4818 | } |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | /* |
| 4823 | * For a specific UST session, disable the channel for all registered apps. |
| 4824 | */ |
| 4825 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan) |
| 4826 | { |
| 4827 | int ret = 0; |
| 4828 | struct lttng_ht_node_str *ua_chan_node; |
| 4829 | struct ust_app_session *ua_sess; |
| 4830 | struct ust_app_channel *ua_chan; |
| 4831 | |
| 4832 | LTTNG_ASSERT(usess->active); |
| 4833 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
| 4834 | uchan->name, |
| 4835 | usess->id); |
| 4836 | |
| 4837 | /* Iterate on all apps. */ |
| 4838 | for (auto *app : |
| 4839 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 4840 | *ust_app_ht->ht)) { |
| 4841 | struct lttng_ht_iter uiter; |
| 4842 | if (!app->compatible) { |
| 4843 | /* |
| 4844 | * TODO: In time, we should notice the caller of this error by |
| 4845 | * telling him that this is a version error. |
| 4846 | */ |
| 4847 | continue; |
| 4848 | } |
| 4849 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 4850 | if (ua_sess == nullptr) { |
| 4851 | continue; |
| 4852 | } |
| 4853 | |
| 4854 | /* Get channel */ |
| 4855 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 4856 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 4857 | /* If the session if found for the app, the channel must be there */ |
| 4858 | LTTNG_ASSERT(ua_chan_node); |
| 4859 | |
| 4860 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 4861 | /* The channel must not be already disabled */ |
| 4862 | LTTNG_ASSERT(ua_chan->enabled); |
| 4863 | |
| 4864 | /* Disable channel onto application */ |
| 4865 | ret = disable_ust_app_channel(ua_sess->lock(), ua_chan, app); |
| 4866 | if (ret < 0) { |
| 4867 | /* XXX: We might want to report this error at some point... */ |
| 4868 | continue; |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | return ret; |
| 4873 | } |
| 4874 | |
| 4875 | /* |
| 4876 | * For a specific UST session, enable the channel for all registered apps. |
| 4877 | */ |
| 4878 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan) |
| 4879 | { |
| 4880 | int ret = 0; |
| 4881 | struct ust_app_session *ua_sess; |
| 4882 | |
| 4883 | LTTNG_ASSERT(usess->active); |
| 4884 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
| 4885 | uchan->name, |
| 4886 | usess->id); |
| 4887 | |
| 4888 | /* For every registered applications */ |
| 4889 | for (auto *app : |
| 4890 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 4891 | *ust_app_ht->ht)) { |
| 4892 | if (!app->compatible) { |
| 4893 | /* |
| 4894 | * TODO: In time, we should notice the caller of this error by |
| 4895 | * telling him that this is a version error. |
| 4896 | */ |
| 4897 | continue; |
| 4898 | } |
| 4899 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 4900 | if (ua_sess == nullptr) { |
| 4901 | continue; |
| 4902 | } |
| 4903 | |
| 4904 | /* Enable channel onto application */ |
| 4905 | ret = enable_ust_app_channel(ua_sess->lock(), uchan, app); |
| 4906 | if (ret < 0) { |
| 4907 | /* XXX: We might want to report this error at some point... */ |
| 4908 | continue; |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | return ret; |
| 4913 | } |
| 4914 | |
| 4915 | /* |
| 4916 | * Disable an event in a channel and for a specific session. |
| 4917 | */ |
| 4918 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
| 4919 | struct ltt_ust_channel *uchan, |
| 4920 | struct ltt_ust_event *uevent) |
| 4921 | { |
| 4922 | int ret = 0; |
| 4923 | struct lttng_ht_iter uiter; |
| 4924 | struct lttng_ht_node_str *ua_chan_node; |
| 4925 | struct ust_app_session *ua_sess; |
| 4926 | struct ust_app_channel *ua_chan; |
| 4927 | struct ust_app_event *ua_event; |
| 4928 | |
| 4929 | LTTNG_ASSERT(usess->active); |
| 4930 | DBG("UST app disabling event %s for all apps in channel " |
| 4931 | "%s for session id %" PRIu64, |
| 4932 | uevent->attr.name, |
| 4933 | uchan->name, |
| 4934 | usess->id); |
| 4935 | |
| 4936 | /* Iterate on all apps. */ |
| 4937 | for (auto *app : |
| 4938 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 4939 | *ust_app_ht->ht)) { |
| 4940 | if (!app->compatible) { |
| 4941 | continue; |
| 4942 | } |
| 4943 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 4944 | if (ua_sess == nullptr) { |
| 4945 | /* Next app */ |
| 4946 | continue; |
| 4947 | } |
| 4948 | |
| 4949 | /* Lookup channel in the ust app session */ |
| 4950 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 4951 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 4952 | if (ua_chan_node == nullptr) { |
| 4953 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
| 4954 | "Skipping", |
| 4955 | uchan->name, |
| 4956 | usess->id, |
| 4957 | app->pid); |
| 4958 | continue; |
| 4959 | } |
| 4960 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 4961 | |
| 4962 | ua_event = find_ust_app_event( |
| 4963 | ua_chan->events, |
| 4964 | uevent->attr.name, |
| 4965 | uevent->filter, |
| 4966 | (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type, |
| 4967 | uevent->attr.loglevel, |
| 4968 | uevent->exclusion); |
| 4969 | if (ua_event == nullptr) { |
| 4970 | DBG2("Event %s not found in channel %s for app pid %d." |
| 4971 | "Skipping", |
| 4972 | uevent->attr.name, |
| 4973 | uchan->name, |
| 4974 | app->pid); |
| 4975 | continue; |
| 4976 | } |
| 4977 | |
| 4978 | ret = disable_ust_app_event(ua_event, app); |
| 4979 | if (ret < 0) { |
| 4980 | continue; |
| 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | return ret; |
| 4985 | } |
| 4986 | |
| 4987 | /* The ua_sess lock must be held by the caller. */ |
| 4988 | static int ust_app_channel_create(struct ltt_ust_session *usess, |
| 4989 | const ust_app_session::locked_weak_ref& ua_sess, |
| 4990 | struct ltt_ust_channel *uchan, |
| 4991 | struct ust_app *app, |
| 4992 | struct ust_app_channel **_ua_chan) |
| 4993 | { |
| 4994 | int ret = 0; |
| 4995 | struct ust_app_channel *ua_chan = nullptr; |
| 4996 | |
| 4997 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, sizeof(uchan->name))) { |
| 4998 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr); |
| 4999 | ret = 0; |
| 5000 | } else { |
| 5001 | /* |
| 5002 | * Create channel onto application and synchronize its |
| 5003 | * configuration. |
| 5004 | */ |
| 5005 | ret = ust_app_channel_allocate( |
| 5006 | ua_sess, uchan, LTTNG_UST_ABI_CHAN_PER_CPU, usess, &ua_chan); |
| 5007 | if (ret < 0) { |
| 5008 | goto error; |
| 5009 | } |
| 5010 | |
| 5011 | ret = ust_app_channel_send(app, usess, ua_sess, ua_chan); |
| 5012 | if (ret) { |
| 5013 | goto error; |
| 5014 | } |
| 5015 | |
| 5016 | /* Add contexts. */ |
| 5017 | for (auto *uctx : |
| 5018 | lttng::urcu::list_iteration_adapter<ltt_ust_context, <t_ust_context::list>( |
| 5019 | uchan->ctx_list)) { |
| 5020 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
| 5021 | if (ret) { |
| 5022 | goto error; |
| 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | error: |
| 5028 | if (ret < 0) { |
| 5029 | switch (ret) { |
| 5030 | case -ENOTCONN: |
| 5031 | /* |
| 5032 | * The application's socket is not valid. Either a bad socket |
| 5033 | * or a timeout on it. We can't inform the caller that for a |
| 5034 | * specific app, the session failed so lets continue here. |
| 5035 | */ |
| 5036 | ret = 0; /* Not an error. */ |
| 5037 | break; |
| 5038 | case -ENOMEM: |
| 5039 | default: |
| 5040 | break; |
| 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | if (ret == 0 && _ua_chan) { |
| 5045 | /* |
| 5046 | * Only return the application's channel on success. Note |
| 5047 | * that the channel can still be part of the application's |
| 5048 | * channel hashtable on error. |
| 5049 | */ |
| 5050 | *_ua_chan = ua_chan; |
| 5051 | } |
| 5052 | return ret; |
| 5053 | } |
| 5054 | |
| 5055 | /* |
| 5056 | * Enable event for a specific session and channel on the tracer. |
| 5057 | */ |
| 5058 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
| 5059 | struct ltt_ust_channel *uchan, |
| 5060 | struct ltt_ust_event *uevent) |
| 5061 | { |
| 5062 | int ret = 0; |
| 5063 | struct lttng_ht_iter uiter; |
| 5064 | struct lttng_ht_node_str *ua_chan_node; |
| 5065 | struct ust_app_session *ua_sess; |
| 5066 | struct ust_app_channel *ua_chan; |
| 5067 | struct ust_app_event *ua_event; |
| 5068 | |
| 5069 | LTTNG_ASSERT(usess->active); |
| 5070 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
| 5071 | uevent->attr.name, |
| 5072 | usess->id); |
| 5073 | |
| 5074 | /* |
| 5075 | * NOTE: At this point, this function is called only if the session and |
| 5076 | * channel passed are already created for all apps. and enabled on the |
| 5077 | * tracer also. |
| 5078 | */ |
| 5079 | |
| 5080 | /* Iterate on all apps. */ |
| 5081 | for (auto *app : |
| 5082 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 5083 | *ust_app_ht->ht)) { |
| 5084 | if (!app->compatible) { |
| 5085 | /* |
| 5086 | * TODO: In time, we should notice the caller of this error by |
| 5087 | * telling him that this is a version error. |
| 5088 | */ |
| 5089 | continue; |
| 5090 | } |
| 5091 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 5092 | if (!ua_sess) { |
| 5093 | /* The application has problem or is probably dead. */ |
| 5094 | continue; |
| 5095 | } |
| 5096 | |
| 5097 | auto locked_ua_sess = ua_sess->lock(); |
| 5098 | if (ua_sess->deleted) { |
| 5099 | continue; |
| 5100 | } |
| 5101 | |
| 5102 | /* Lookup channel in the ust app session */ |
| 5103 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 5104 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 5105 | /* |
| 5106 | * It is possible that the channel cannot be found is |
| 5107 | * the channel/event creation occurs concurrently with |
| 5108 | * an application exit. |
| 5109 | */ |
| 5110 | if (!ua_chan_node) { |
| 5111 | continue; |
| 5112 | } |
| 5113 | |
| 5114 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 5115 | |
| 5116 | /* Get event node */ |
| 5117 | ua_event = find_ust_app_event( |
| 5118 | ua_chan->events, |
| 5119 | uevent->attr.name, |
| 5120 | uevent->filter, |
| 5121 | (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type, |
| 5122 | uevent->attr.loglevel, |
| 5123 | uevent->exclusion); |
| 5124 | if (ua_event == nullptr) { |
| 5125 | DBG3("UST app enable event %s not found for app PID %d." |
| 5126 | "Skipping app", |
| 5127 | uevent->attr.name, |
| 5128 | app->pid); |
| 5129 | continue; |
| 5130 | } |
| 5131 | |
| 5132 | ret = enable_ust_app_event(ua_event, app); |
| 5133 | if (ret < 0) { |
| 5134 | goto error; |
| 5135 | } |
| 5136 | } |
| 5137 | error: |
| 5138 | return ret; |
| 5139 | } |
| 5140 | |
| 5141 | /* |
| 5142 | * For a specific existing UST session and UST channel, creates the event for |
| 5143 | * all registered apps. |
| 5144 | */ |
| 5145 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
| 5146 | struct ltt_ust_channel *uchan, |
| 5147 | struct ltt_ust_event *uevent) |
| 5148 | { |
| 5149 | int ret = 0; |
| 5150 | struct lttng_ht_iter uiter; |
| 5151 | struct lttng_ht_node_str *ua_chan_node; |
| 5152 | struct ust_app_session *ua_sess; |
| 5153 | struct ust_app_channel *ua_chan; |
| 5154 | |
| 5155 | LTTNG_ASSERT(usess->active); |
| 5156 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
| 5157 | uevent->attr.name, |
| 5158 | usess->id); |
| 5159 | |
| 5160 | /* Iterate on all apps. */ |
| 5161 | for (auto *app : |
| 5162 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 5163 | *ust_app_ht->ht)) { |
| 5164 | if (!app->compatible) { |
| 5165 | /* |
| 5166 | * TODO: In time, we should notice the caller of this error by |
| 5167 | * telling him that this is a version error. |
| 5168 | */ |
| 5169 | continue; |
| 5170 | } |
| 5171 | |
| 5172 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 5173 | if (!ua_sess) { |
| 5174 | /* The application has problem or is probably dead. */ |
| 5175 | continue; |
| 5176 | } |
| 5177 | |
| 5178 | auto locked_ua_sess = ua_sess->lock(); |
| 5179 | |
| 5180 | if (locked_ua_sess->deleted) { |
| 5181 | continue; |
| 5182 | } |
| 5183 | |
| 5184 | /* Lookup channel in the ust app session */ |
| 5185 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 5186 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 5187 | /* If the channel is not found, there is a code flow error */ |
| 5188 | LTTNG_ASSERT(ua_chan_node); |
| 5189 | |
| 5190 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 5191 | |
| 5192 | ret = create_ust_app_event(ua_chan, uevent, app); |
| 5193 | if (ret < 0) { |
| 5194 | if (ret != -LTTNG_UST_ERR_EXIST) { |
| 5195 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
| 5196 | break; |
| 5197 | } |
| 5198 | |
| 5199 | DBG2("UST app event %s already exist on app PID %d", |
| 5200 | uevent->attr.name, |
| 5201 | app->pid); |
| 5202 | continue; |
| 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | return ret; |
| 5207 | } |
| 5208 | |
| 5209 | /* |
| 5210 | * Start tracing for a specific UST session and app. |
| 5211 | * |
| 5212 | * Called with UST app session lock held. |
| 5213 | * |
| 5214 | */ |
| 5215 | static int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 5216 | { |
| 5217 | int ret = 0; |
| 5218 | struct ust_app_session *ua_sess; |
| 5219 | |
| 5220 | DBG("Starting tracing for ust app pid %d", app->pid); |
| 5221 | |
| 5222 | const lttng::urcu::read_lock_guard read_lock; |
| 5223 | const auto update_health_code_on_exit = |
| 5224 | lttng::make_scope_exit([]() noexcept { health_code_update(); }); |
| 5225 | |
| 5226 | if (!app->compatible) { |
| 5227 | return 0; |
| 5228 | } |
| 5229 | |
| 5230 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 5231 | if (ua_sess == nullptr) { |
| 5232 | /* The session is in teardown process. Ignore and continue. */ |
| 5233 | return 0; |
| 5234 | } |
| 5235 | |
| 5236 | auto locked_ua_sess = ua_sess->lock(); |
| 5237 | |
| 5238 | if (locked_ua_sess->deleted) { |
| 5239 | return 0; |
| 5240 | } |
| 5241 | |
| 5242 | if (locked_ua_sess->enabled) { |
| 5243 | return 0; |
| 5244 | } |
| 5245 | |
| 5246 | /* This starts the UST tracing */ |
| 5247 | pthread_mutex_lock(&app->sock_lock); |
| 5248 | ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle); |
| 5249 | pthread_mutex_unlock(&app->sock_lock); |
| 5250 | if (ret < 0) { |
| 5251 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 5252 | DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d", |
| 5253 | app->pid, |
| 5254 | app->sock); |
| 5255 | return 0; |
| 5256 | } else if (ret == -EAGAIN) { |
| 5257 | WARN("UST app start session failed. Communication time out: pid = %d, sock = %d", |
| 5258 | app->pid, |
| 5259 | app->sock); |
| 5260 | return 0; |
| 5261 | |
| 5262 | } else { |
| 5263 | ERR("UST app start session failed with ret %d: pid = %d, sock = %d", |
| 5264 | ret, |
| 5265 | app->pid, |
| 5266 | app->sock); |
| 5267 | } |
| 5268 | |
| 5269 | return -1; |
| 5270 | } |
| 5271 | |
| 5272 | /* Indicate that the session has been started once */ |
| 5273 | ua_sess->started = true; |
| 5274 | ua_sess->enabled = true; |
| 5275 | |
| 5276 | health_code_update(); |
| 5277 | |
| 5278 | /* Quiescent wait after starting trace */ |
| 5279 | pthread_mutex_lock(&app->sock_lock); |
| 5280 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
| 5281 | pthread_mutex_unlock(&app->sock_lock); |
| 5282 | if (ret < 0) { |
| 5283 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 5284 | DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d", |
| 5285 | app->pid, |
| 5286 | app->sock); |
| 5287 | } else if (ret == -EAGAIN) { |
| 5288 | WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d", |
| 5289 | app->pid, |
| 5290 | app->sock); |
| 5291 | } else { |
| 5292 | ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d", |
| 5293 | ret, |
| 5294 | app->pid, |
| 5295 | app->sock); |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | return 0; |
| 5300 | } |
| 5301 | |
| 5302 | /* |
| 5303 | * Stop tracing for a specific UST session and app. |
| 5304 | */ |
| 5305 | static int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 5306 | { |
| 5307 | int ret = 0; |
| 5308 | struct ust_app_session *ua_sess; |
| 5309 | |
| 5310 | DBG("Stopping tracing for ust app pid %d", app->pid); |
| 5311 | |
| 5312 | const lttng::urcu::read_lock_guard read_lock; |
| 5313 | const auto update_health_code_on_exit = |
| 5314 | lttng::make_scope_exit([]() noexcept { health_code_update(); }); |
| 5315 | |
| 5316 | if (!app->compatible) { |
| 5317 | return 0; |
| 5318 | } |
| 5319 | |
| 5320 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 5321 | if (ua_sess == nullptr) { |
| 5322 | return 0; |
| 5323 | } |
| 5324 | |
| 5325 | auto locked_ua_sess = ua_sess->lock(); |
| 5326 | |
| 5327 | if (ua_sess->deleted) { |
| 5328 | return 0; |
| 5329 | } |
| 5330 | |
| 5331 | /* |
| 5332 | * If started = 0, it means that stop trace has been called for a session |
| 5333 | * that was never started. It's possible since we can have a fail start |
| 5334 | * from either the application manager thread or the command thread. Simply |
| 5335 | * indicate that this is a stop error. |
| 5336 | */ |
| 5337 | if (!ua_sess->started) { |
| 5338 | return -1; |
| 5339 | } |
| 5340 | |
| 5341 | health_code_update(); |
| 5342 | |
| 5343 | /* This inhibits UST tracing */ |
| 5344 | pthread_mutex_lock(&app->sock_lock); |
| 5345 | ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle); |
| 5346 | pthread_mutex_unlock(&app->sock_lock); |
| 5347 | if (ret < 0) { |
| 5348 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 5349 | DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d", |
| 5350 | app->pid, |
| 5351 | app->sock); |
| 5352 | return 0; |
| 5353 | } else if (ret == -EAGAIN) { |
| 5354 | WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d", |
| 5355 | app->pid, |
| 5356 | app->sock); |
| 5357 | return 0; |
| 5358 | |
| 5359 | } else { |
| 5360 | ERR("UST app stop session failed with ret %d: pid = %d, sock = %d", |
| 5361 | ret, |
| 5362 | app->pid, |
| 5363 | app->sock); |
| 5364 | } |
| 5365 | |
| 5366 | return -1; |
| 5367 | } |
| 5368 | |
| 5369 | health_code_update(); |
| 5370 | ua_sess->enabled = false; |
| 5371 | |
| 5372 | /* Quiescent wait after stopping trace */ |
| 5373 | pthread_mutex_lock(&app->sock_lock); |
| 5374 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
| 5375 | pthread_mutex_unlock(&app->sock_lock); |
| 5376 | if (ret < 0) { |
| 5377 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 5378 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
| 5379 | app->pid, |
| 5380 | app->sock); |
| 5381 | } else if (ret == -EAGAIN) { |
| 5382 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
| 5383 | app->pid, |
| 5384 | app->sock); |
| 5385 | } else { |
| 5386 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
| 5387 | ret, |
| 5388 | app->pid, |
| 5389 | app->sock); |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | health_code_update(); |
| 5394 | |
| 5395 | { |
| 5396 | auto locked_registry = |
| 5397 | get_locked_session_registry(locked_ua_sess->get_identifier()); |
| 5398 | |
| 5399 | /* The UST app session is held registry shall not be null. */ |
| 5400 | LTTNG_ASSERT(locked_registry); |
| 5401 | |
| 5402 | /* Push metadata for application before freeing the application. */ |
| 5403 | (void) push_metadata(locked_registry, ua_sess->consumer); |
| 5404 | } |
| 5405 | |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
| 5409 | static int ust_app_flush_app_session(ust_app& app, ust_app_session& ua_sess) |
| 5410 | { |
| 5411 | int ret, retval = 0; |
| 5412 | struct consumer_socket *socket; |
| 5413 | |
| 5414 | const auto update_health_code_on_exit = |
| 5415 | lttng::make_scope_exit([]() noexcept { health_code_update(); }); |
| 5416 | |
| 5417 | DBG("Flushing app session buffers for ust app pid %d", app.pid); |
| 5418 | |
| 5419 | if (!app.compatible) { |
| 5420 | return 0; |
| 5421 | } |
| 5422 | |
| 5423 | const auto locked_ua_sess = ua_sess.lock(); |
| 5424 | if (locked_ua_sess->deleted) { |
| 5425 | return 0; |
| 5426 | } |
| 5427 | |
| 5428 | health_code_update(); |
| 5429 | |
| 5430 | /* Flushing buffers */ |
| 5431 | socket = consumer_find_socket_by_bitness(app.abi.bits_per_long, ua_sess.consumer); |
| 5432 | |
| 5433 | /* Flush buffers and push metadata. */ |
| 5434 | switch (ua_sess.buffer_type) { |
| 5435 | case LTTNG_BUFFER_PER_PID: |
| 5436 | { |
| 5437 | for (auto *ua_chan : |
| 5438 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 5439 | decltype(ust_app_channel::node), |
| 5440 | &ust_app_channel::node>( |
| 5441 | *ua_sess.channels->ht)) { |
| 5442 | health_code_update(); |
| 5443 | ret = consumer_flush_channel(socket, ua_chan->key); |
| 5444 | if (ret) { |
| 5445 | ERR("Error flushing consumer channel"); |
| 5446 | retval = -1; |
| 5447 | continue; |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | break; |
| 5452 | } |
| 5453 | case LTTNG_BUFFER_PER_UID: |
| 5454 | default: |
| 5455 | abort(); |
| 5456 | break; |
| 5457 | } |
| 5458 | |
| 5459 | return retval; |
| 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * Flush buffers for all applications for a specific UST session. |
| 5464 | * Called with UST session lock held. |
| 5465 | */ |
| 5466 | static int ust_app_flush_session(struct ltt_ust_session *usess) |
| 5467 | |
| 5468 | { |
| 5469 | int ret = 0; |
| 5470 | |
| 5471 | DBG("Flushing session buffers for all ust apps"); |
| 5472 | |
| 5473 | /* Flush buffers and push metadata. */ |
| 5474 | switch (usess->buffer_type) { |
| 5475 | case LTTNG_BUFFER_PER_UID: |
| 5476 | { |
| 5477 | /* Flush all per UID buffers associated to that session. */ |
| 5478 | for (auto *reg : |
| 5479 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 5480 | usess->buffer_reg_uid_list)) { |
| 5481 | const lttng::urcu::read_lock_guard read_lock; |
| 5482 | lsu::registry_session *ust_session_reg; |
| 5483 | struct consumer_socket *socket; |
| 5484 | |
| 5485 | /* Get consumer socket to use to push the metadata.*/ |
| 5486 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 5487 | usess->consumer); |
| 5488 | if (!socket) { |
| 5489 | /* Ignore request if no consumer is found for the session. */ |
| 5490 | continue; |
| 5491 | } |
| 5492 | |
| 5493 | for (auto *buf_reg_chan : |
| 5494 | lttng::urcu::lfht_iteration_adapter<buffer_reg_channel, |
| 5495 | decltype(buffer_reg_channel::node), |
| 5496 | &buffer_reg_channel::node>( |
| 5497 | *reg->registry->channels->ht)) { |
| 5498 | /* |
| 5499 | * The following call will print error values so the return |
| 5500 | * code is of little importance because whatever happens, we |
| 5501 | * have to try them all. |
| 5502 | */ |
| 5503 | (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key); |
| 5504 | } |
| 5505 | |
| 5506 | ust_session_reg = reg->registry->reg.ust; |
| 5507 | /* Push metadata. */ |
| 5508 | auto locked_registry = ust_session_reg->lock(); |
| 5509 | (void) push_metadata(locked_registry, usess->consumer); |
| 5510 | } |
| 5511 | |
| 5512 | break; |
| 5513 | } |
| 5514 | case LTTNG_BUFFER_PER_PID: |
| 5515 | { |
| 5516 | /* Iterate on all apps. */ |
| 5517 | for (auto *app : |
| 5518 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 5519 | decltype(ust_app::pid_n), |
| 5520 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 5521 | auto *ua_sess = ust_app_lookup_app_session(usess, app); |
| 5522 | if (ua_sess == nullptr) { |
| 5523 | continue; |
| 5524 | } |
| 5525 | |
| 5526 | (void) ust_app_flush_app_session(*app, *ua_sess); |
| 5527 | } |
| 5528 | |
| 5529 | break; |
| 5530 | } |
| 5531 | default: |
| 5532 | ret = -1; |
| 5533 | abort(); |
| 5534 | break; |
| 5535 | } |
| 5536 | |
| 5537 | health_code_update(); |
| 5538 | return ret; |
| 5539 | } |
| 5540 | |
| 5541 | static int ust_app_clear_quiescent_app_session(struct ust_app *app, struct ust_app_session *ua_sess) |
| 5542 | { |
| 5543 | int ret = 0; |
| 5544 | struct consumer_socket *socket; |
| 5545 | |
| 5546 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); |
| 5547 | |
| 5548 | const lttng::urcu::read_lock_guard read_lock; |
| 5549 | const auto update_health_code_on_exit = |
| 5550 | lttng::make_scope_exit([]() noexcept { health_code_update(); }); |
| 5551 | |
| 5552 | if (!app->compatible) { |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
| 5556 | const auto locked_ua_sess = ua_sess->lock(); |
| 5557 | if (locked_ua_sess->deleted) { |
| 5558 | return 0; |
| 5559 | } |
| 5560 | |
| 5561 | health_code_update(); |
| 5562 | |
| 5563 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, ua_sess->consumer); |
| 5564 | if (!socket) { |
| 5565 | ERR("Failed to find consumer (%" PRIu32 ") socket", app->abi.bits_per_long); |
| 5566 | return -1; |
| 5567 | } |
| 5568 | |
| 5569 | /* Clear quiescent state. */ |
| 5570 | switch (ua_sess->buffer_type) { |
| 5571 | case LTTNG_BUFFER_PER_PID: |
| 5572 | for (auto *ua_chan : |
| 5573 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 5574 | decltype(ust_app_channel::node), |
| 5575 | &ust_app_channel::node>( |
| 5576 | *ua_sess->channels->ht)) { |
| 5577 | health_code_update(); |
| 5578 | ret = consumer_clear_quiescent_channel(socket, ua_chan->key); |
| 5579 | if (ret) { |
| 5580 | ERR("Error clearing quiescent state for consumer channel"); |
| 5581 | ret = -1; |
| 5582 | continue; |
| 5583 | } |
| 5584 | } |
| 5585 | break; |
| 5586 | case LTTNG_BUFFER_PER_UID: |
| 5587 | default: |
| 5588 | abort(); |
| 5589 | ret = -1; |
| 5590 | break; |
| 5591 | } |
| 5592 | |
| 5593 | return ret; |
| 5594 | } |
| 5595 | |
| 5596 | /* |
| 5597 | * Clear quiescent state in each stream for all applications for a |
| 5598 | * specific UST session. |
| 5599 | * Called with UST session lock held. |
| 5600 | */ |
| 5601 | static int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) |
| 5602 | |
| 5603 | { |
| 5604 | int ret = 0; |
| 5605 | |
| 5606 | DBG("Clearing stream quiescent state for all ust apps"); |
| 5607 | |
| 5608 | switch (usess->buffer_type) { |
| 5609 | case LTTNG_BUFFER_PER_UID: |
| 5610 | { |
| 5611 | /* |
| 5612 | * Clear quiescent for all per UID buffers associated to |
| 5613 | * that session. |
| 5614 | */ |
| 5615 | for (auto *reg : |
| 5616 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 5617 | usess->buffer_reg_uid_list)) { |
| 5618 | struct consumer_socket *socket; |
| 5619 | const lttng::urcu::read_lock_guard read_lock; |
| 5620 | |
| 5621 | /* Get associated consumer socket.*/ |
| 5622 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 5623 | usess->consumer); |
| 5624 | if (!socket) { |
| 5625 | /* |
| 5626 | * Ignore request if no consumer is found for |
| 5627 | * the session. |
| 5628 | */ |
| 5629 | continue; |
| 5630 | } |
| 5631 | |
| 5632 | for (auto *buf_reg_chan : |
| 5633 | lttng::urcu::lfht_iteration_adapter<buffer_reg_channel, |
| 5634 | decltype(buffer_reg_channel::node), |
| 5635 | &buffer_reg_channel::node>( |
| 5636 | *reg->registry->channels->ht)) { |
| 5637 | /* |
| 5638 | * The following call will print error values so |
| 5639 | * the return code is of little importance |
| 5640 | * because whatever happens, we have to try them |
| 5641 | * all. |
| 5642 | */ |
| 5643 | (void) consumer_clear_quiescent_channel(socket, |
| 5644 | buf_reg_chan->consumer_key); |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | break; |
| 5649 | } |
| 5650 | case LTTNG_BUFFER_PER_PID: |
| 5651 | { |
| 5652 | /* Iterate on all apps. */ |
| 5653 | for (auto *app : |
| 5654 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 5655 | decltype(ust_app::pid_n), |
| 5656 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 5657 | auto *ua_sess = ust_app_lookup_app_session(usess, app); |
| 5658 | if (ua_sess == nullptr) { |
| 5659 | continue; |
| 5660 | } |
| 5661 | |
| 5662 | (void) ust_app_clear_quiescent_app_session(app, ua_sess); |
| 5663 | } |
| 5664 | |
| 5665 | break; |
| 5666 | } |
| 5667 | default: |
| 5668 | ret = -1; |
| 5669 | abort(); |
| 5670 | break; |
| 5671 | } |
| 5672 | |
| 5673 | health_code_update(); |
| 5674 | return ret; |
| 5675 | } |
| 5676 | |
| 5677 | /* |
| 5678 | * Destroy a specific UST session in apps. |
| 5679 | */ |
| 5680 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 5681 | { |
| 5682 | int ret; |
| 5683 | struct ust_app_session *ua_sess; |
| 5684 | struct lttng_ht_iter iter; |
| 5685 | struct lttng_ht_node_u64 *node; |
| 5686 | |
| 5687 | DBG("Destroy tracing for ust app pid %d", app->pid); |
| 5688 | |
| 5689 | const lttng::urcu::read_lock_guard read_lock; |
| 5690 | |
| 5691 | if (!app->compatible) { |
| 5692 | goto end; |
| 5693 | } |
| 5694 | |
| 5695 | __lookup_session_by_app(usess, app, &iter); |
| 5696 | node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter); |
| 5697 | if (node == nullptr) { |
| 5698 | /* Session is being or is deleted. */ |
| 5699 | goto end; |
| 5700 | } |
| 5701 | ua_sess = lttng::utils::container_of(node, &ust_app_session::node); |
| 5702 | |
| 5703 | health_code_update(); |
| 5704 | destroy_app_session(app, ua_sess); |
| 5705 | |
| 5706 | health_code_update(); |
| 5707 | |
| 5708 | /* Quiescent wait after stopping trace */ |
| 5709 | pthread_mutex_lock(&app->sock_lock); |
| 5710 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
| 5711 | pthread_mutex_unlock(&app->sock_lock); |
| 5712 | if (ret < 0) { |
| 5713 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 5714 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
| 5715 | app->pid, |
| 5716 | app->sock); |
| 5717 | } else if (ret == -EAGAIN) { |
| 5718 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
| 5719 | app->pid, |
| 5720 | app->sock); |
| 5721 | } else { |
| 5722 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
| 5723 | ret, |
| 5724 | app->pid, |
| 5725 | app->sock); |
| 5726 | } |
| 5727 | } |
| 5728 | end: |
| 5729 | health_code_update(); |
| 5730 | return 0; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
| 5734 | * Start tracing for the UST session. |
| 5735 | */ |
| 5736 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
| 5737 | { |
| 5738 | DBG("Starting all UST traces"); |
| 5739 | |
| 5740 | /* |
| 5741 | * Even though the start trace might fail, flag this session active so |
| 5742 | * other application coming in are started by default. |
| 5743 | */ |
| 5744 | usess->active = true; |
| 5745 | |
| 5746 | /* |
| 5747 | * In a start-stop-start use-case, we need to clear the quiescent state |
| 5748 | * of each channel set by the prior stop command, thus ensuring that a |
| 5749 | * following stop or destroy is sure to grab a timestamp_end near those |
| 5750 | * operations, even if the packet is empty. |
| 5751 | */ |
| 5752 | (void) ust_app_clear_quiescent_session(usess); |
| 5753 | |
| 5754 | /* Iterate on all apps. */ |
| 5755 | for (auto *app : |
| 5756 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 5757 | *ust_app_ht->ht)) { |
| 5758 | ust_app_global_update(usess, app); |
| 5759 | } |
| 5760 | |
| 5761 | return 0; |
| 5762 | } |
| 5763 | |
| 5764 | /* |
| 5765 | * Start tracing for the UST session. |
| 5766 | * Called with UST session lock held. |
| 5767 | */ |
| 5768 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) |
| 5769 | { |
| 5770 | int ret = 0; |
| 5771 | |
| 5772 | DBG("Stopping all UST traces"); |
| 5773 | |
| 5774 | /* |
| 5775 | * Even though the stop trace might fail, flag this session inactive so |
| 5776 | * other application coming in are not started by default. |
| 5777 | */ |
| 5778 | usess->active = false; |
| 5779 | |
| 5780 | /* Iterate on all apps. */ |
| 5781 | for (auto *app : |
| 5782 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 5783 | *ust_app_ht->ht)) { |
| 5784 | ret = ust_app_stop_trace(usess, app); |
| 5785 | if (ret < 0) { |
| 5786 | /* Continue to next apps even on error */ |
| 5787 | continue; |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | (void) ust_app_flush_session(usess); |
| 5792 | |
| 5793 | return 0; |
| 5794 | } |
| 5795 | |
| 5796 | /* |
| 5797 | * Destroy app UST session. |
| 5798 | */ |
| 5799 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) |
| 5800 | { |
| 5801 | DBG("Destroy all UST traces"); |
| 5802 | |
| 5803 | /* Iterate on all apps. */ |
| 5804 | for (auto *app : |
| 5805 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 5806 | *ust_app_ht->ht)) { |
| 5807 | (void) destroy_trace(usess, app); |
| 5808 | } |
| 5809 | |
| 5810 | return 0; |
| 5811 | } |
| 5812 | |
| 5813 | /* The ua_sess lock must be held by the caller. */ |
| 5814 | static int find_or_create_ust_app_channel(struct ltt_ust_session *usess, |
| 5815 | const ust_app_session::locked_weak_ref& ua_sess, |
| 5816 | struct ust_app *app, |
| 5817 | struct ltt_ust_channel *uchan, |
| 5818 | struct ust_app_channel **ua_chan) |
| 5819 | { |
| 5820 | int ret = 0; |
| 5821 | struct lttng_ht_iter iter; |
| 5822 | struct lttng_ht_node_str *ua_chan_node; |
| 5823 | |
| 5824 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); |
| 5825 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
| 5826 | if (ua_chan_node) { |
| 5827 | *ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 5828 | goto end; |
| 5829 | } |
| 5830 | |
| 5831 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); |
| 5832 | if (ret) { |
| 5833 | goto end; |
| 5834 | } |
| 5835 | end: |
| 5836 | return ret; |
| 5837 | } |
| 5838 | |
| 5839 | static int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, |
| 5840 | struct ltt_ust_event *uevent, |
| 5841 | struct ust_app *app) |
| 5842 | { |
| 5843 | int ret = 0; |
| 5844 | struct ust_app_event *ua_event = nullptr; |
| 5845 | |
| 5846 | ua_event = find_ust_app_event(ua_chan->events, |
| 5847 | uevent->attr.name, |
| 5848 | uevent->filter, |
| 5849 | (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type, |
| 5850 | uevent->attr.loglevel, |
| 5851 | uevent->exclusion); |
| 5852 | if (!ua_event) { |
| 5853 | ret = create_ust_app_event(ua_chan, uevent, app); |
| 5854 | if (ret < 0) { |
| 5855 | goto end; |
| 5856 | } |
| 5857 | } else { |
| 5858 | if (ua_event->enabled != uevent->enabled) { |
| 5859 | ret = uevent->enabled ? enable_ust_app_event(ua_event, app) : |
| 5860 | disable_ust_app_event(ua_event, app); |
| 5861 | } |
| 5862 | } |
| 5863 | |
| 5864 | end: |
| 5865 | return ret; |
| 5866 | } |
| 5867 | |
| 5868 | /* Called with RCU read-side lock held. */ |
| 5869 | static void ust_app_synchronize_event_notifier_rules(struct ust_app *app) |
| 5870 | { |
| 5871 | int ret = 0; |
| 5872 | enum lttng_error_code ret_code; |
| 5873 | enum lttng_trigger_status t_status; |
| 5874 | struct lttng_triggers *triggers = nullptr; |
| 5875 | unsigned int count, i; |
| 5876 | |
| 5877 | ASSERT_RCU_READ_LOCKED(); |
| 5878 | |
| 5879 | if (!ust_app_supports_notifiers(app)) { |
| 5880 | goto end; |
| 5881 | } |
| 5882 | |
| 5883 | /* |
| 5884 | * Currrently, registering or unregistering a trigger with an |
| 5885 | * event rule condition causes a full synchronization of the event |
| 5886 | * notifiers. |
| 5887 | * |
| 5888 | * The first step attempts to add an event notifier for all registered |
| 5889 | * triggers that apply to the user space tracers. Then, the |
| 5890 | * application's event notifiers rules are all checked against the list |
| 5891 | * of registered triggers. Any event notifier that doesn't have a |
| 5892 | * matching trigger can be assumed to have been disabled. |
| 5893 | * |
| 5894 | * All of this is inefficient, but is put in place to get the feature |
| 5895 | * rolling as it is simpler at this moment. It will be optimized Soon™ |
| 5896 | * to allow the state of enabled |
| 5897 | * event notifiers to be synchronized in a piece-wise way. |
| 5898 | */ |
| 5899 | |
| 5900 | /* Get all triggers using uid 0 (root) */ |
| 5901 | ret_code = notification_thread_command_list_triggers( |
| 5902 | the_notification_thread_handle, 0, &triggers); |
| 5903 | if (ret_code != LTTNG_OK) { |
| 5904 | goto end; |
| 5905 | } |
| 5906 | |
| 5907 | LTTNG_ASSERT(triggers); |
| 5908 | |
| 5909 | t_status = lttng_triggers_get_count(triggers, &count); |
| 5910 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { |
| 5911 | goto end; |
| 5912 | } |
| 5913 | |
| 5914 | for (i = 0; i < count; i++) { |
| 5915 | const struct lttng_condition *condition; |
| 5916 | const struct lttng_event_rule *event_rule; |
| 5917 | struct lttng_trigger *trigger; |
| 5918 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; |
| 5919 | enum lttng_condition_status condition_status; |
| 5920 | uint64_t token; |
| 5921 | |
| 5922 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); |
| 5923 | LTTNG_ASSERT(trigger); |
| 5924 | |
| 5925 | token = lttng_trigger_get_tracer_token(trigger); |
| 5926 | condition = lttng_trigger_get_const_condition(trigger); |
| 5927 | |
| 5928 | if (lttng_condition_get_type(condition) != |
| 5929 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) { |
| 5930 | /* Does not apply */ |
| 5931 | continue; |
| 5932 | } |
| 5933 | |
| 5934 | condition_status = |
| 5935 | lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
| 5936 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 5937 | |
| 5938 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { |
| 5939 | /* Skip kernel related triggers. */ |
| 5940 | continue; |
| 5941 | } |
| 5942 | |
| 5943 | /* |
| 5944 | * Find or create the associated token event rule. The caller |
| 5945 | * holds the RCU read lock, so this is safe to call without |
| 5946 | * explicitly acquiring it here. |
| 5947 | */ |
| 5948 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( |
| 5949 | app->token_to_event_notifier_rule_ht, token); |
| 5950 | if (!looked_up_event_notifier_rule) { |
| 5951 | ret = create_ust_app_event_notifier_rule(trigger, app); |
| 5952 | if (ret < 0) { |
| 5953 | goto end; |
| 5954 | } |
| 5955 | } |
| 5956 | } |
| 5957 | |
| 5958 | /* Remove all unknown event sources from the app. */ |
| 5959 | for (auto *event_notifier_rule : |
| 5960 | lttng::urcu::lfht_iteration_adapter<ust_app_event_notifier_rule, |
| 5961 | decltype(ust_app_event_notifier_rule::node), |
| 5962 | &ust_app_event_notifier_rule::node>( |
| 5963 | *app->token_to_event_notifier_rule_ht->ht)) { |
| 5964 | const uint64_t app_token = event_notifier_rule->token; |
| 5965 | bool found = false; |
| 5966 | |
| 5967 | /* |
| 5968 | * Check if the app event trigger still exists on the |
| 5969 | * notification side. |
| 5970 | */ |
| 5971 | for (i = 0; i < count; i++) { |
| 5972 | uint64_t notification_thread_token; |
| 5973 | const struct lttng_trigger *trigger = |
| 5974 | lttng_triggers_get_at_index(triggers, i); |
| 5975 | |
| 5976 | LTTNG_ASSERT(trigger); |
| 5977 | |
| 5978 | notification_thread_token = lttng_trigger_get_tracer_token(trigger); |
| 5979 | |
| 5980 | if (notification_thread_token == app_token) { |
| 5981 | found = true; |
| 5982 | break; |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | if (found) { |
| 5987 | /* Still valid. */ |
| 5988 | continue; |
| 5989 | } |
| 5990 | |
| 5991 | /* |
| 5992 | * This trigger was unregistered, disable it on the tracer's |
| 5993 | * side. |
| 5994 | */ |
| 5995 | ret = cds_lfht_del(app->token_to_event_notifier_rule_ht->ht, |
| 5996 | &event_notifier_rule->node.node); |
| 5997 | LTTNG_ASSERT(ret == 0); |
| 5998 | |
| 5999 | /* Callee logs errors. */ |
| 6000 | (void) disable_ust_object(app, event_notifier_rule->obj); |
| 6001 | delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app); |
| 6002 | } |
| 6003 | |
| 6004 | end: |
| 6005 | lttng_triggers_destroy(triggers); |
| 6006 | return; |
| 6007 | } |
| 6008 | |
| 6009 | /* |
| 6010 | * RCU read lock must be held by the caller. |
| 6011 | */ |
| 6012 | static void ust_app_synchronize_all_channels(struct ltt_ust_session *usess, |
| 6013 | const ust_app_session::locked_weak_ref& ua_sess, |
| 6014 | struct ust_app *app) |
| 6015 | { |
| 6016 | LTTNG_ASSERT(usess); |
| 6017 | LTTNG_ASSERT(app); |
| 6018 | ASSERT_RCU_READ_LOCKED(); |
| 6019 | |
| 6020 | for (auto *uchan : lttng::urcu::lfht_iteration_adapter<ltt_ust_channel, |
| 6021 | decltype(ltt_ust_channel::node), |
| 6022 | <t_ust_channel::node>( |
| 6023 | *usess->domain_global.channels->ht)) { |
| 6024 | struct ust_app_channel *ua_chan; |
| 6025 | |
| 6026 | /* |
| 6027 | * Search for a matching ust_app_channel. If none is found, |
| 6028 | * create it. Creating the channel will cause the ua_chan |
| 6029 | * structure to be allocated, the channel buffers to be |
| 6030 | * allocated (if necessary) and sent to the application, and |
| 6031 | * all enabled contexts will be added to the channel. |
| 6032 | */ |
| 6033 | int ret = find_or_create_ust_app_channel(usess, ua_sess, app, uchan, &ua_chan); |
| 6034 | if (ret) { |
| 6035 | /* Tracer is probably gone or ENOMEM. */ |
| 6036 | goto end; |
| 6037 | } |
| 6038 | |
| 6039 | if (!ua_chan) { |
| 6040 | /* ua_chan will be NULL for the metadata channel */ |
| 6041 | continue; |
| 6042 | } |
| 6043 | |
| 6044 | for (auto *uevent : |
| 6045 | lttng::urcu::lfht_iteration_adapter<ltt_ust_event, |
| 6046 | decltype(ltt_ust_event::node), |
| 6047 | <t_ust_event::node>( |
| 6048 | *uchan->events->ht)) { |
| 6049 | ret = ust_app_channel_synchronize_event(ua_chan, uevent, app); |
| 6050 | if (ret) { |
| 6051 | goto end; |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | if (ua_chan->enabled != uchan->enabled) { |
| 6056 | ret = uchan->enabled ? enable_ust_app_channel(ua_sess, uchan, app) : |
| 6057 | disable_ust_app_channel(ua_sess, ua_chan, app); |
| 6058 | if (ret) { |
| 6059 | goto end; |
| 6060 | } |
| 6061 | } |
| 6062 | } |
| 6063 | end: |
| 6064 | return; |
| 6065 | } |
| 6066 | |
| 6067 | /* |
| 6068 | * The caller must ensure that the application is compatible and is tracked |
| 6069 | * by the process attribute trackers. |
| 6070 | */ |
| 6071 | static void ust_app_synchronize(struct ltt_ust_session *usess, struct ust_app *app) |
| 6072 | { |
| 6073 | int ret = 0; |
| 6074 | struct ust_app_session *ua_sess = nullptr; |
| 6075 | |
| 6076 | /* |
| 6077 | * The application's configuration should only be synchronized for |
| 6078 | * active sessions. |
| 6079 | */ |
| 6080 | LTTNG_ASSERT(usess->active); |
| 6081 | |
| 6082 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, nullptr); |
| 6083 | if (ret < 0) { |
| 6084 | /* Tracer is probably gone or ENOMEM. */ |
| 6085 | return; |
| 6086 | } |
| 6087 | |
| 6088 | LTTNG_ASSERT(ua_sess); |
| 6089 | |
| 6090 | const auto locked_ua_sess = ua_sess->lock(); |
| 6091 | if (locked_ua_sess->deleted) { |
| 6092 | return; |
| 6093 | } |
| 6094 | |
| 6095 | { |
| 6096 | const lttng::urcu::read_lock_guard read_lock; |
| 6097 | |
| 6098 | ust_app_synchronize_all_channels(usess, locked_ua_sess, app); |
| 6099 | |
| 6100 | /* |
| 6101 | * Create the metadata for the application. This returns gracefully if a |
| 6102 | * metadata was already set for the session. |
| 6103 | * |
| 6104 | * The metadata channel must be created after the data channels as the |
| 6105 | * consumer daemon assumes this ordering. When interacting with a relay |
| 6106 | * daemon, the consumer will use this assumption to send the |
| 6107 | * "STREAMS_SENT" message to the relay daemon. |
| 6108 | */ |
| 6109 | ret = create_ust_app_metadata(locked_ua_sess, app, usess->consumer); |
| 6110 | if (ret < 0) { |
| 6111 | ERR("Metadata creation failed for app sock %d for session id %" PRIu64, |
| 6112 | app->sock, |
| 6113 | usess->id); |
| 6114 | } |
| 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | static void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) |
| 6119 | { |
| 6120 | struct ust_app_session *ua_sess; |
| 6121 | |
| 6122 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 6123 | if (ua_sess == nullptr) { |
| 6124 | return; |
| 6125 | } |
| 6126 | destroy_app_session(app, ua_sess); |
| 6127 | } |
| 6128 | |
| 6129 | /* |
| 6130 | * Add channels/events from UST global domain to registered apps at sock. |
| 6131 | * |
| 6132 | * Called with session lock held. |
| 6133 | * Called with RCU read-side lock held. |
| 6134 | */ |
| 6135 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) |
| 6136 | { |
| 6137 | LTTNG_ASSERT(usess); |
| 6138 | LTTNG_ASSERT(usess->active); |
| 6139 | ASSERT_RCU_READ_LOCKED(); |
| 6140 | |
| 6141 | DBG2("UST app global update for app sock %d for session id %" PRIu64, app->sock, usess->id); |
| 6142 | |
| 6143 | if (!app->compatible) { |
| 6144 | return; |
| 6145 | } |
| 6146 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, usess, app->pid) && |
| 6147 | trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, usess, app->uid) && |
| 6148 | trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, usess, app->gid)) { |
| 6149 | /* |
| 6150 | * Synchronize the application's internal tracing configuration |
| 6151 | * and start tracing. |
| 6152 | */ |
| 6153 | ust_app_synchronize(usess, app); |
| 6154 | ust_app_start_trace(usess, app); |
| 6155 | } else { |
| 6156 | ust_app_global_destroy(usess, app); |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | /* |
| 6161 | * Add all event notifiers to an application. |
| 6162 | * |
| 6163 | * Called with session lock held. |
| 6164 | * Called with RCU read-side lock held. |
| 6165 | */ |
| 6166 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) |
| 6167 | { |
| 6168 | ASSERT_RCU_READ_LOCKED(); |
| 6169 | |
| 6170 | DBG2("UST application global event notifier rules update: app = '%s', pid = %d", |
| 6171 | app->name, |
| 6172 | app->pid); |
| 6173 | |
| 6174 | if (!app->compatible || !ust_app_supports_notifiers(app)) { |
| 6175 | return; |
| 6176 | } |
| 6177 | |
| 6178 | if (app->event_notifier_group.object == nullptr) { |
| 6179 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d", |
| 6180 | app->name, |
| 6181 | app->pid); |
| 6182 | return; |
| 6183 | } |
| 6184 | |
| 6185 | ust_app_synchronize_event_notifier_rules(app); |
| 6186 | } |
| 6187 | |
| 6188 | /* |
| 6189 | * Called with session lock held. |
| 6190 | */ |
| 6191 | void ust_app_global_update_all(struct ltt_ust_session *usess) |
| 6192 | { |
| 6193 | /* Iterate on all apps. */ |
| 6194 | for (auto *app : |
| 6195 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 6196 | *ust_app_ht->ht)) { |
| 6197 | ust_app_global_update(usess, app); |
| 6198 | } |
| 6199 | } |
| 6200 | |
| 6201 | void ust_app_global_update_all_event_notifier_rules() |
| 6202 | { |
| 6203 | /* Iterate on all apps. */ |
| 6204 | for (auto *app : |
| 6205 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 6206 | *ust_app_ht->ht)) { |
| 6207 | ust_app_global_update_event_notifier_rules(app); |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | /* |
| 6212 | * Add context to a specific channel for global UST domain. |
| 6213 | */ |
| 6214 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, |
| 6215 | struct ltt_ust_channel *uchan, |
| 6216 | struct ltt_ust_context *uctx) |
| 6217 | { |
| 6218 | int ret = 0; |
| 6219 | struct lttng_ht_node_str *ua_chan_node; |
| 6220 | struct lttng_ht_iter uiter; |
| 6221 | struct ust_app_channel *ua_chan = nullptr; |
| 6222 | struct ust_app_session *ua_sess; |
| 6223 | |
| 6224 | LTTNG_ASSERT(usess->active); |
| 6225 | |
| 6226 | /* Iterate on all apps. */ |
| 6227 | for (auto *app : |
| 6228 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 6229 | *ust_app_ht->ht)) { |
| 6230 | if (!app->compatible) { |
| 6231 | /* |
| 6232 | * TODO: In time, we should notice the caller of this error by |
| 6233 | * telling him that this is a version error. |
| 6234 | */ |
| 6235 | continue; |
| 6236 | } |
| 6237 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 6238 | if (ua_sess == nullptr) { |
| 6239 | continue; |
| 6240 | } |
| 6241 | |
| 6242 | const auto locked_ua_sess = ua_sess->lock(); |
| 6243 | if (locked_ua_sess->deleted) { |
| 6244 | continue; |
| 6245 | } |
| 6246 | |
| 6247 | /* Lookup channel in the ust app session */ |
| 6248 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 6249 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 6250 | if (ua_chan_node == nullptr) { |
| 6251 | continue; |
| 6252 | } |
| 6253 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 6254 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
| 6255 | if (ret < 0) { |
| 6256 | continue; |
| 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | return ret; |
| 6261 | } |
| 6262 | |
| 6263 | /* |
| 6264 | * Receive registration and populate the given msg structure. |
| 6265 | * |
| 6266 | * On success return 0 else a negative value returned by the ustctl call. |
| 6267 | */ |
| 6268 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) |
| 6269 | { |
| 6270 | int ret; |
| 6271 | uint32_t pid, ppid, uid, gid; |
| 6272 | |
| 6273 | LTTNG_ASSERT(msg); |
| 6274 | |
| 6275 | ret = lttng_ust_ctl_recv_reg_msg(sock, |
| 6276 | &msg->type, |
| 6277 | &msg->major, |
| 6278 | &msg->minor, |
| 6279 | &pid, |
| 6280 | &ppid, |
| 6281 | &uid, |
| 6282 | &gid, |
| 6283 | &msg->bits_per_long, |
| 6284 | &msg->uint8_t_alignment, |
| 6285 | &msg->uint16_t_alignment, |
| 6286 | &msg->uint32_t_alignment, |
| 6287 | &msg->uint64_t_alignment, |
| 6288 | &msg->long_alignment, |
| 6289 | &msg->byte_order, |
| 6290 | msg->name); |
| 6291 | if (ret < 0) { |
| 6292 | switch (-ret) { |
| 6293 | case EPIPE: |
| 6294 | case ECONNRESET: |
| 6295 | case LTTNG_UST_ERR_EXITING: |
| 6296 | DBG3("UST app recv reg message failed. Application died"); |
| 6297 | break; |
| 6298 | case LTTNG_UST_ERR_UNSUP_MAJOR: |
| 6299 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", |
| 6300 | msg->major, |
| 6301 | msg->minor, |
| 6302 | LTTNG_UST_ABI_MAJOR_VERSION, |
| 6303 | LTTNG_UST_ABI_MINOR_VERSION); |
| 6304 | break; |
| 6305 | default: |
| 6306 | ERR("UST app recv reg message failed with ret %d", ret); |
| 6307 | break; |
| 6308 | } |
| 6309 | goto error; |
| 6310 | } |
| 6311 | msg->pid = (pid_t) pid; |
| 6312 | msg->ppid = (pid_t) ppid; |
| 6313 | msg->uid = (uid_t) uid; |
| 6314 | msg->gid = (gid_t) gid; |
| 6315 | |
| 6316 | error: |
| 6317 | return ret; |
| 6318 | } |
| 6319 | |
| 6320 | /* |
| 6321 | * Return a ust app session object using the application object and the |
| 6322 | * session object descriptor has a key. If not found, NULL is returned. |
| 6323 | * A RCU read side lock MUST be acquired when calling this function. |
| 6324 | */ |
| 6325 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, int objd) |
| 6326 | { |
| 6327 | struct lttng_ht_node_ulong *node; |
| 6328 | struct lttng_ht_iter iter; |
| 6329 | struct ust_app_session *ua_sess = nullptr; |
| 6330 | |
| 6331 | LTTNG_ASSERT(app); |
| 6332 | ASSERT_RCU_READ_LOCKED(); |
| 6333 | |
| 6334 | lttng_ht_lookup(app->ust_sessions_objd, (void *) ((unsigned long) objd), &iter); |
| 6335 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 6336 | if (node == nullptr) { |
| 6337 | DBG2("UST app session find by objd %d not found", objd); |
| 6338 | goto error; |
| 6339 | } |
| 6340 | |
| 6341 | ua_sess = lttng::utils::container_of(node, &ust_app_session::ust_objd_node); |
| 6342 | |
| 6343 | error: |
| 6344 | return ua_sess; |
| 6345 | } |
| 6346 | |
| 6347 | /* |
| 6348 | * Return a ust app channel object using the application object and the channel |
| 6349 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
| 6350 | * lock MUST be acquired before calling this function. |
| 6351 | */ |
| 6352 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, int objd) |
| 6353 | { |
| 6354 | struct lttng_ht_node_ulong *node; |
| 6355 | struct lttng_ht_iter iter; |
| 6356 | struct ust_app_channel *ua_chan = nullptr; |
| 6357 | |
| 6358 | LTTNG_ASSERT(app); |
| 6359 | ASSERT_RCU_READ_LOCKED(); |
| 6360 | |
| 6361 | lttng_ht_lookup(app->ust_objd, (void *) ((unsigned long) objd), &iter); |
| 6362 | node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter); |
| 6363 | if (node == nullptr) { |
| 6364 | DBG2("UST app channel find by objd %d not found", objd); |
| 6365 | goto error; |
| 6366 | } |
| 6367 | |
| 6368 | ua_chan = lttng::utils::container_of(node, &ust_app_channel::ust_objd_node); |
| 6369 | |
| 6370 | error: |
| 6371 | return ua_chan; |
| 6372 | } |
| 6373 | |
| 6374 | /* |
| 6375 | * Reply to a register channel notification from an application on the notify |
| 6376 | * socket. The channel metadata is also created. |
| 6377 | * |
| 6378 | * The session UST registry lock is acquired in this function. |
| 6379 | * |
| 6380 | * On success 0 is returned else a negative value. |
| 6381 | */ |
| 6382 | static int handle_app_register_channel_notification(int sock, |
| 6383 | int cobjd, |
| 6384 | struct lttng_ust_ctl_field *raw_context_fields, |
| 6385 | size_t context_field_count) |
| 6386 | { |
| 6387 | int ret, ret_code = 0; |
| 6388 | uint32_t chan_id; |
| 6389 | uint64_t chan_reg_key; |
| 6390 | struct ust_app *app; |
| 6391 | struct ust_app_channel *ua_chan; |
| 6392 | struct ust_app_session *ua_sess; |
| 6393 | auto ust_ctl_context_fields = |
| 6394 | lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::memory::free>( |
| 6395 | raw_context_fields); |
| 6396 | |
| 6397 | const lttng::urcu::read_lock_guard read_lock_guard; |
| 6398 | |
| 6399 | /* Lookup application. If not found, there is a code flow error. */ |
| 6400 | app = find_app_by_notify_sock(sock); |
| 6401 | if (!app) { |
| 6402 | DBG("Application socket %d is being torn down. Abort event notify", sock); |
| 6403 | return -1; |
| 6404 | } |
| 6405 | |
| 6406 | /* Lookup channel by UST object descriptor. */ |
| 6407 | ua_chan = find_channel_by_objd(app, cobjd); |
| 6408 | if (!ua_chan) { |
| 6409 | DBG("Application channel is being torn down. Abort event notify"); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
| 6413 | LTTNG_ASSERT(ua_chan->session); |
| 6414 | ua_sess = ua_chan->session; |
| 6415 | |
| 6416 | /* Get right session registry depending on the session buffer type. */ |
| 6417 | |
| 6418 | /* |
| 6419 | * HACK: ua_sess is already locked by the client thread. This is called |
| 6420 | * in the context of the handling of a notification from the application. |
| 6421 | */ |
| 6422 | auto locked_ua_sess = ust_app_session::make_locked_weak_ref(*ua_sess); |
| 6423 | auto locked_registry_session = |
| 6424 | get_locked_session_registry(locked_ua_sess->get_identifier()); |
| 6425 | locked_ua_sess.release(); |
| 6426 | if (!locked_registry_session) { |
| 6427 | DBG("Application session is being torn down. Abort event notify"); |
| 6428 | return 0; |
| 6429 | }; |
| 6430 | |
| 6431 | /* Depending on the buffer type, a different channel key is used. */ |
| 6432 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 6433 | chan_reg_key = ua_chan->tracing_channel_id; |
| 6434 | } else { |
| 6435 | chan_reg_key = ua_chan->key; |
| 6436 | } |
| 6437 | |
| 6438 | auto& ust_reg_chan = locked_registry_session->channel(chan_reg_key); |
| 6439 | |
| 6440 | /* Channel id is set during the object creation. */ |
| 6441 | chan_id = ust_reg_chan.id; |
| 6442 | |
| 6443 | /* |
| 6444 | * The application returns the typing information of the channel's |
| 6445 | * context fields. In per-PID buffering mode, this is the first and only |
| 6446 | * time we get this information. It is our chance to finalize the |
| 6447 | * initialiation of the channel and serialize it's layout's description |
| 6448 | * to the trace's metadata. |
| 6449 | * |
| 6450 | * However, in per-UID buffering mode, every application will provide |
| 6451 | * this information (redundantly). The first time will allow us to |
| 6452 | * complete the initialization. The following times, we simply validate |
| 6453 | * that all apps provide the same typing for the context fields as a |
| 6454 | * sanity check. |
| 6455 | */ |
| 6456 | try { |
| 6457 | auto app_context_fields = lsu::create_trace_fields_from_ust_ctl_fields( |
| 6458 | *locked_registry_session, |
| 6459 | ust_ctl_context_fields.get(), |
| 6460 | context_field_count, |
| 6461 | lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT, |
| 6462 | lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS); |
| 6463 | |
| 6464 | if (!ust_reg_chan.is_registered()) { |
| 6465 | lst::type::cuptr event_context = app_context_fields.size() ? |
| 6466 | lttng::make_unique<lst::structure_type>( |
| 6467 | 0, std::move(app_context_fields)) : |
| 6468 | nullptr; |
| 6469 | |
| 6470 | ust_reg_chan.event_context(std::move(event_context)); |
| 6471 | } else { |
| 6472 | /* |
| 6473 | * Validate that the context fields match between |
| 6474 | * registry and newcoming application. |
| 6475 | */ |
| 6476 | bool context_fields_match; |
| 6477 | const auto *previous_event_context = ust_reg_chan.event_context(); |
| 6478 | |
| 6479 | if (!previous_event_context) { |
| 6480 | context_fields_match = app_context_fields.size() == 0; |
| 6481 | } else { |
| 6482 | const lst::structure_type app_event_context_struct( |
| 6483 | 0, std::move(app_context_fields)); |
| 6484 | |
| 6485 | context_fields_match = *previous_event_context == |
| 6486 | app_event_context_struct; |
| 6487 | } |
| 6488 | |
| 6489 | if (!context_fields_match) { |
| 6490 | ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d", |
| 6491 | app->pid, |
| 6492 | app->sock); |
| 6493 | ret_code = -EINVAL; |
| 6494 | goto reply; |
| 6495 | } |
| 6496 | } |
| 6497 | } catch (const std::exception& ex) { |
| 6498 | ERR("Failed to handle application context: %s", ex.what()); |
| 6499 | ret_code = -EINVAL; |
| 6500 | goto reply; |
| 6501 | } |
| 6502 | |
| 6503 | reply: |
| 6504 | DBG3("UST app replying to register channel key %" PRIu64 " with id %u, ret = %d", |
| 6505 | chan_reg_key, |
| 6506 | chan_id, |
| 6507 | ret_code); |
| 6508 | |
| 6509 | ret = lttng_ust_ctl_reply_register_channel( |
| 6510 | sock, |
| 6511 | chan_id, |
| 6512 | ust_reg_chan.header_type_ == lst::stream_class::header_type::COMPACT ? |
| 6513 | LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT : |
| 6514 | LTTNG_UST_CTL_CHANNEL_HEADER_LARGE, |
| 6515 | ret_code); |
| 6516 | if (ret < 0) { |
| 6517 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6518 | DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d", |
| 6519 | app->pid, |
| 6520 | app->sock); |
| 6521 | } else if (ret == -EAGAIN) { |
| 6522 | WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d", |
| 6523 | app->pid, |
| 6524 | app->sock); |
| 6525 | } else { |
| 6526 | ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d", |
| 6527 | ret, |
| 6528 | app->pid, |
| 6529 | app->sock); |
| 6530 | } |
| 6531 | |
| 6532 | return ret; |
| 6533 | } |
| 6534 | |
| 6535 | /* This channel registry's registration is completed. */ |
| 6536 | ust_reg_chan.set_as_registered(); |
| 6537 | |
| 6538 | return ret; |
| 6539 | } |
| 6540 | |
| 6541 | /* |
| 6542 | * Add event to the UST channel registry. When the event is added to the |
| 6543 | * registry, the metadata is also created. Once done, this replies to the |
| 6544 | * application with the appropriate error code. |
| 6545 | * |
| 6546 | * The session UST registry lock is acquired in the function. |
| 6547 | * |
| 6548 | * On success 0 is returned else a negative value. |
| 6549 | */ |
| 6550 | static int add_event_ust_registry(int sock, |
| 6551 | int sobjd, |
| 6552 | int cobjd, |
| 6553 | const char *name, |
| 6554 | char *raw_signature, |
| 6555 | size_t nr_fields, |
| 6556 | struct lttng_ust_ctl_field *raw_fields, |
| 6557 | int loglevel_value, |
| 6558 | char *raw_model_emf_uri) |
| 6559 | { |
| 6560 | int ret, ret_code; |
| 6561 | lsu::event_id event_id = 0; |
| 6562 | uint64_t chan_reg_key; |
| 6563 | struct ust_app *app; |
| 6564 | struct ust_app_channel *ua_chan; |
| 6565 | struct ust_app_session *ua_sess; |
| 6566 | const lttng::urcu::read_lock_guard rcu_lock; |
| 6567 | auto signature = lttng::make_unique_wrapper<char, lttng::memory::free>(raw_signature); |
| 6568 | auto fields = |
| 6569 | lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::memory::free>(raw_fields); |
| 6570 | auto model_emf_uri = |
| 6571 | lttng::make_unique_wrapper<char, lttng::memory::free>(raw_model_emf_uri); |
| 6572 | |
| 6573 | /* Lookup application. If not found, there is a code flow error. */ |
| 6574 | app = find_app_by_notify_sock(sock); |
| 6575 | if (!app) { |
| 6576 | DBG("Application socket %d is being torn down. Abort event notify", sock); |
| 6577 | return -1; |
| 6578 | } |
| 6579 | |
| 6580 | /* Lookup channel by UST object descriptor. */ |
| 6581 | ua_chan = find_channel_by_objd(app, cobjd); |
| 6582 | if (!ua_chan) { |
| 6583 | DBG("Application channel is being torn down. Abort event notify"); |
| 6584 | return 0; |
| 6585 | } |
| 6586 | |
| 6587 | LTTNG_ASSERT(ua_chan->session); |
| 6588 | ua_sess = ua_chan->session; |
| 6589 | |
| 6590 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 6591 | chan_reg_key = ua_chan->tracing_channel_id; |
| 6592 | } else { |
| 6593 | chan_reg_key = ua_chan->key; |
| 6594 | } |
| 6595 | |
| 6596 | { |
| 6597 | auto locked_registry = get_locked_session_registry(ua_sess->get_identifier()); |
| 6598 | if (locked_registry) { |
| 6599 | /* |
| 6600 | * From this point on, this call acquires the ownership of the signature, |
| 6601 | * fields and model_emf_uri meaning any free are done inside it if needed. |
| 6602 | * These three variables MUST NOT be read/write after this. |
| 6603 | */ |
| 6604 | try { |
| 6605 | auto& channel = locked_registry->channel(chan_reg_key); |
| 6606 | |
| 6607 | /* id is set on success. */ |
| 6608 | channel.add_event( |
| 6609 | sobjd, |
| 6610 | cobjd, |
| 6611 | name, |
| 6612 | signature.get(), |
| 6613 | lsu::create_trace_fields_from_ust_ctl_fields( |
| 6614 | *locked_registry, |
| 6615 | fields.get(), |
| 6616 | nr_fields, |
| 6617 | lst::field_location::root::EVENT_RECORD_PAYLOAD, |
| 6618 | lsu::ctl_field_quirks:: |
| 6619 | UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS), |
| 6620 | loglevel_value, |
| 6621 | model_emf_uri.get() ? |
| 6622 | nonstd::optional<std::string>(model_emf_uri.get()) : |
| 6623 | nonstd::nullopt, |
| 6624 | ua_sess->buffer_type, |
| 6625 | *app, |
| 6626 | event_id); |
| 6627 | ret_code = 0; |
| 6628 | } catch (const std::exception& ex) { |
| 6629 | ERR("Failed to add event `%s` to registry session: %s", |
| 6630 | name, |
| 6631 | ex.what()); |
| 6632 | /* Inform the application of the error; don't return directly. */ |
| 6633 | ret_code = -EINVAL; |
| 6634 | } |
| 6635 | } else { |
| 6636 | DBG("Application session is being torn down. Abort event notify"); |
| 6637 | return 0; |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | /* |
| 6642 | * The return value is returned to ustctl so in case of an error, the |
| 6643 | * application can be notified. In case of an error, it's important not to |
| 6644 | * return a negative error or else the application will get closed. |
| 6645 | */ |
| 6646 | ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code); |
| 6647 | if (ret < 0) { |
| 6648 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6649 | DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.", |
| 6650 | app->pid, |
| 6651 | app->sock); |
| 6652 | } else if (ret == -EAGAIN) { |
| 6653 | WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d", |
| 6654 | app->pid, |
| 6655 | app->sock); |
| 6656 | } else { |
| 6657 | ERR("UST app reply event failed with ret %d: pid = %d, sock = %d", |
| 6658 | ret, |
| 6659 | app->pid, |
| 6660 | app->sock); |
| 6661 | } |
| 6662 | /* |
| 6663 | * No need to wipe the create event since the application socket will |
| 6664 | * get close on error hence cleaning up everything by itself. |
| 6665 | */ |
| 6666 | return ret; |
| 6667 | } |
| 6668 | |
| 6669 | DBG_FMT("UST registry event successfully added: name={}, id={}", name, event_id); |
| 6670 | return ret; |
| 6671 | } |
| 6672 | |
| 6673 | /* |
| 6674 | * Add enum to the UST session registry. Once done, this replies to the |
| 6675 | * application with the appropriate error code. |
| 6676 | * |
| 6677 | * The session UST registry lock is acquired within this function. |
| 6678 | * |
| 6679 | * On success 0 is returned else a negative value. |
| 6680 | */ |
| 6681 | static int add_enum_ust_registry(int sock, |
| 6682 | int sobjd, |
| 6683 | const char *name, |
| 6684 | struct lttng_ust_ctl_enum_entry *raw_entries, |
| 6685 | size_t nr_entries) |
| 6686 | { |
| 6687 | int ret = 0; |
| 6688 | struct ust_app *app; |
| 6689 | struct ust_app_session *ua_sess; |
| 6690 | uint64_t enum_id = -1ULL; |
| 6691 | const lttng::urcu::read_lock_guard read_lock_guard; |
| 6692 | auto entries = |
| 6693 | lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::memory::free>( |
| 6694 | raw_entries); |
| 6695 | |
| 6696 | /* Lookup application. If not found, there is a code flow error. */ |
| 6697 | app = find_app_by_notify_sock(sock); |
| 6698 | if (!app) { |
| 6699 | /* Return an error since this is not an error */ |
| 6700 | DBG("Application socket %d is being torn down. Aborting enum registration", sock); |
| 6701 | return -1; |
| 6702 | } |
| 6703 | |
| 6704 | /* Lookup session by UST object descriptor. */ |
| 6705 | ua_sess = find_session_by_objd(app, sobjd); |
| 6706 | if (!ua_sess) { |
| 6707 | /* Return an error since this is not an error */ |
| 6708 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
| 6709 | return 0; |
| 6710 | } |
| 6711 | |
| 6712 | auto locked_registry = get_locked_session_registry(ua_sess->get_identifier()); |
| 6713 | if (!locked_registry) { |
| 6714 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); |
| 6715 | return 0; |
| 6716 | } |
| 6717 | |
| 6718 | /* |
| 6719 | * From this point on, the callee acquires the ownership of |
| 6720 | * entries. The variable entries MUST NOT be read/written after |
| 6721 | * call. |
| 6722 | */ |
| 6723 | int application_reply_code; |
| 6724 | try { |
| 6725 | locked_registry->create_or_find_enum( |
| 6726 | sobjd, name, entries.release(), nr_entries, &enum_id); |
| 6727 | application_reply_code = 0; |
| 6728 | } catch (const std::exception& ex) { |
| 6729 | ERR("%s: %s", |
| 6730 | lttng::format( |
| 6731 | "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}", |
| 6732 | *app, |
| 6733 | name) |
| 6734 | .c_str(), |
| 6735 | ex.what()); |
| 6736 | application_reply_code = -1; |
| 6737 | } |
| 6738 | |
| 6739 | /* |
| 6740 | * The return value is returned to ustctl so in case of an error, the |
| 6741 | * application can be notified. In case of an error, it's important not to |
| 6742 | * return a negative error or else the application will get closed. |
| 6743 | */ |
| 6744 | ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code); |
| 6745 | if (ret < 0) { |
| 6746 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6747 | DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d", |
| 6748 | app->pid, |
| 6749 | app->sock); |
| 6750 | } else if (ret == -EAGAIN) { |
| 6751 | WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d", |
| 6752 | app->pid, |
| 6753 | app->sock); |
| 6754 | } else { |
| 6755 | ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d", |
| 6756 | ret, |
| 6757 | app->pid, |
| 6758 | app->sock); |
| 6759 | } |
| 6760 | /* |
| 6761 | * No need to wipe the create enum since the application socket will |
| 6762 | * get close on error hence cleaning up everything by itself. |
| 6763 | */ |
| 6764 | return ret; |
| 6765 | } |
| 6766 | |
| 6767 | DBG3("UST registry enum %s added successfully or already found", name); |
| 6768 | return 0; |
| 6769 | } |
| 6770 | |
| 6771 | /* |
| 6772 | * Handle application notification through the given notify socket. |
| 6773 | * |
| 6774 | * Return 0 on success or else a negative value. |
| 6775 | */ |
| 6776 | int ust_app_recv_notify(int sock) |
| 6777 | { |
| 6778 | int ret; |
| 6779 | enum lttng_ust_ctl_notify_cmd cmd; |
| 6780 | |
| 6781 | DBG3("UST app receiving notify from sock %d", sock); |
| 6782 | |
| 6783 | ret = lttng_ust_ctl_recv_notify(sock, &cmd); |
| 6784 | if (ret < 0) { |
| 6785 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6786 | DBG3("UST app recv notify failed. Application died: sock = %d", sock); |
| 6787 | } else if (ret == -EAGAIN) { |
| 6788 | WARN("UST app recv notify failed. Communication time out: sock = %d", sock); |
| 6789 | } else { |
| 6790 | ERR("UST app recv notify failed with ret %d: sock = %d", ret, sock); |
| 6791 | } |
| 6792 | goto error; |
| 6793 | } |
| 6794 | |
| 6795 | switch (cmd) { |
| 6796 | case LTTNG_UST_CTL_NOTIFY_CMD_EVENT: |
| 6797 | { |
| 6798 | int sobjd, cobjd, loglevel_value; |
| 6799 | char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri; |
| 6800 | size_t nr_fields; |
| 6801 | uint64_t tracer_token = 0; |
| 6802 | struct lttng_ust_ctl_field *fields; |
| 6803 | |
| 6804 | DBG2("UST app ustctl register event received"); |
| 6805 | |
| 6806 | ret = lttng_ust_ctl_recv_register_event(sock, |
| 6807 | &sobjd, |
| 6808 | &cobjd, |
| 6809 | name, |
| 6810 | &loglevel_value, |
| 6811 | &sig, |
| 6812 | &nr_fields, |
| 6813 | &fields, |
| 6814 | &model_emf_uri, |
| 6815 | &tracer_token); |
| 6816 | if (ret < 0) { |
| 6817 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6818 | DBG3("UST app recv event failed. Application died: sock = %d", |
| 6819 | sock); |
| 6820 | } else if (ret == -EAGAIN) { |
| 6821 | WARN("UST app recv event failed. Communication time out: sock = %d", |
| 6822 | sock); |
| 6823 | } else { |
| 6824 | ERR("UST app recv event failed with ret %d: sock = %d", ret, sock); |
| 6825 | } |
| 6826 | goto error; |
| 6827 | } |
| 6828 | |
| 6829 | { |
| 6830 | const lttng::urcu::read_lock_guard rcu_lock; |
| 6831 | const struct ust_app *app = find_app_by_notify_sock(sock); |
| 6832 | if (!app) { |
| 6833 | DBG("Application socket %d is being torn down. Abort event notify", |
| 6834 | sock); |
| 6835 | ret = -1; |
| 6836 | goto error; |
| 6837 | } |
| 6838 | } |
| 6839 | |
| 6840 | if ((!fields && nr_fields > 0) || (fields && nr_fields == 0)) { |
| 6841 | ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu", |
| 6842 | fields, |
| 6843 | nr_fields); |
| 6844 | ret = -1; |
| 6845 | free(fields); |
| 6846 | goto error; |
| 6847 | } |
| 6848 | |
| 6849 | /* |
| 6850 | * Add event to the UST registry coming from the notify socket. This |
| 6851 | * call will free if needed the sig, fields and model_emf_uri. This |
| 6852 | * code path loses the ownsership of these variables and transfer them |
| 6853 | * to the this function. |
| 6854 | */ |
| 6855 | ret = add_event_ust_registry(sock, |
| 6856 | sobjd, |
| 6857 | cobjd, |
| 6858 | name, |
| 6859 | sig, |
| 6860 | nr_fields, |
| 6861 | fields, |
| 6862 | loglevel_value, |
| 6863 | model_emf_uri); |
| 6864 | if (ret < 0) { |
| 6865 | goto error; |
| 6866 | } |
| 6867 | |
| 6868 | break; |
| 6869 | } |
| 6870 | case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL: |
| 6871 | { |
| 6872 | int sobjd, cobjd; |
| 6873 | size_t field_count; |
| 6874 | struct lttng_ust_ctl_field *context_fields; |
| 6875 | |
| 6876 | DBG2("UST app ustctl register channel received"); |
| 6877 | |
| 6878 | ret = lttng_ust_ctl_recv_register_channel( |
| 6879 | sock, &sobjd, &cobjd, &field_count, &context_fields); |
| 6880 | if (ret < 0) { |
| 6881 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6882 | DBG3("UST app recv channel failed. Application died: sock = %d", |
| 6883 | sock); |
| 6884 | } else if (ret == -EAGAIN) { |
| 6885 | WARN("UST app recv channel failed. Communication time out: sock = %d", |
| 6886 | sock); |
| 6887 | } else { |
| 6888 | ERR("UST app recv channel failed with ret %d: sock = %d", |
| 6889 | ret, |
| 6890 | sock); |
| 6891 | } |
| 6892 | goto error; |
| 6893 | } |
| 6894 | |
| 6895 | /* |
| 6896 | * The fields ownership are transfered to this function call meaning |
| 6897 | * that if needed it will be freed. After this, it's invalid to access |
| 6898 | * fields or clean them up. |
| 6899 | */ |
| 6900 | ret = handle_app_register_channel_notification( |
| 6901 | sock, cobjd, context_fields, field_count); |
| 6902 | if (ret < 0) { |
| 6903 | goto error; |
| 6904 | } |
| 6905 | |
| 6906 | break; |
| 6907 | } |
| 6908 | case LTTNG_UST_CTL_NOTIFY_CMD_ENUM: |
| 6909 | { |
| 6910 | int sobjd; |
| 6911 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 6912 | size_t nr_entries; |
| 6913 | struct lttng_ust_ctl_enum_entry *entries; |
| 6914 | |
| 6915 | DBG2("UST app ustctl register enum received"); |
| 6916 | |
| 6917 | ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name, &entries, &nr_entries); |
| 6918 | if (ret < 0) { |
| 6919 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 6920 | DBG3("UST app recv enum failed. Application died: sock = %d", sock); |
| 6921 | } else if (ret == -EAGAIN) { |
| 6922 | WARN("UST app recv enum failed. Communication time out: sock = %d", |
| 6923 | sock); |
| 6924 | } else { |
| 6925 | ERR("UST app recv enum failed with ret %d: sock = %d", ret, sock); |
| 6926 | } |
| 6927 | goto error; |
| 6928 | } |
| 6929 | |
| 6930 | /* Callee assumes ownership of entries. */ |
| 6931 | ret = add_enum_ust_registry(sock, sobjd, name, entries, nr_entries); |
| 6932 | if (ret < 0) { |
| 6933 | goto error; |
| 6934 | } |
| 6935 | |
| 6936 | break; |
| 6937 | } |
| 6938 | case LTTNG_UST_CTL_NOTIFY_CMD_KEY: |
| 6939 | { |
| 6940 | DBG2("UST app ustctl register key received"); |
| 6941 | ret = -LTTNG_UST_ERR_NOSYS; |
| 6942 | // TODO |
| 6943 | goto error; |
| 6944 | } |
| 6945 | default: |
| 6946 | /* Should NEVER happen. */ |
| 6947 | abort(); |
| 6948 | } |
| 6949 | |
| 6950 | error: |
| 6951 | return ret; |
| 6952 | } |
| 6953 | |
| 6954 | /* |
| 6955 | * Once the notify socket hangs up, this is called. First, it tries to find the |
| 6956 | * corresponding application. On failure, the call_rcu to close the socket is |
| 6957 | * executed. If an application is found, it tries to delete it from the notify |
| 6958 | * socket hash table. Whathever the result, it proceeds to the call_rcu. |
| 6959 | * |
| 6960 | * Note that an object needs to be allocated here so on ENOMEM failure, the |
| 6961 | * call RCU is not done but the rest of the cleanup is. |
| 6962 | */ |
| 6963 | void ust_app_notify_sock_unregister(int sock) |
| 6964 | { |
| 6965 | int err_enomem = 0; |
| 6966 | struct lttng_ht_iter iter; |
| 6967 | struct ust_app *app; |
| 6968 | struct ust_app_notify_sock_obj *obj; |
| 6969 | |
| 6970 | LTTNG_ASSERT(sock >= 0); |
| 6971 | |
| 6972 | const lttng::urcu::read_lock_guard read_lock; |
| 6973 | |
| 6974 | obj = zmalloc<ust_app_notify_sock_obj>(); |
| 6975 | if (!obj) { |
| 6976 | /* |
| 6977 | * An ENOMEM is kind of uncool. If this strikes we continue the |
| 6978 | * procedure but the call_rcu will not be called. In this case, we |
| 6979 | * accept the fd leak rather than possibly creating an unsynchronized |
| 6980 | * state between threads. |
| 6981 | * |
| 6982 | * TODO: The notify object should be created once the notify socket is |
| 6983 | * registered and stored independantely from the ust app object. The |
| 6984 | * tricky part is to synchronize the teardown of the application and |
| 6985 | * this notify object. Let's keep that in mind so we can avoid this |
| 6986 | * kind of shenanigans with ENOMEM in the teardown path. |
| 6987 | */ |
| 6988 | err_enomem = 1; |
| 6989 | } else { |
| 6990 | obj->fd = sock; |
| 6991 | } |
| 6992 | |
| 6993 | DBG("UST app notify socket unregister %d", sock); |
| 6994 | |
| 6995 | /* |
| 6996 | * Lookup application by notify socket. If this fails, this means that the |
| 6997 | * hash table delete has already been done by the application |
| 6998 | * unregistration process so we can safely close the notify socket in a |
| 6999 | * call RCU. |
| 7000 | */ |
| 7001 | app = find_app_by_notify_sock(sock); |
| 7002 | if (!app) { |
| 7003 | goto close_socket; |
| 7004 | } |
| 7005 | |
| 7006 | iter.iter.node = &app->notify_sock_n.node; |
| 7007 | |
| 7008 | /* |
| 7009 | * Whatever happens here either we fail or succeed, in both cases we have |
| 7010 | * to close the socket after a grace period to continue to the call RCU |
| 7011 | * here. If the deletion is successful, the application is not visible |
| 7012 | * anymore by other threads and is it fails it means that it was already |
| 7013 | * deleted from the hash table so either way we just have to close the |
| 7014 | * socket. |
| 7015 | */ |
| 7016 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 7017 | |
| 7018 | close_socket: |
| 7019 | |
| 7020 | /* |
| 7021 | * Close socket after a grace period to avoid for the socket to be reused |
| 7022 | * before the application object is freed creating potential race between |
| 7023 | * threads trying to add unique in the global hash table. |
| 7024 | */ |
| 7025 | if (!err_enomem) { |
| 7026 | call_rcu(&obj->head, close_notify_sock_rcu); |
| 7027 | } |
| 7028 | } |
| 7029 | |
| 7030 | /* |
| 7031 | * Destroy a ust app data structure and free its memory. |
| 7032 | */ |
| 7033 | static void ust_app_destroy(ust_app& app) |
| 7034 | { |
| 7035 | call_rcu(&app.pid_n.head, delete_ust_app_rcu); |
| 7036 | } |
| 7037 | |
| 7038 | /* |
| 7039 | * Take a snapshot for a given UST session. The snapshot is sent to the given |
| 7040 | * output. |
| 7041 | * |
| 7042 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
| 7043 | */ |
| 7044 | enum lttng_error_code ust_app_snapshot_record(const struct ltt_ust_session *usess, |
| 7045 | const struct consumer_output *output, |
| 7046 | uint64_t nb_packets_per_stream) |
| 7047 | { |
| 7048 | int ret = 0; |
| 7049 | enum lttng_error_code status = LTTNG_OK; |
| 7050 | char *trace_path = nullptr; |
| 7051 | |
| 7052 | LTTNG_ASSERT(usess); |
| 7053 | LTTNG_ASSERT(output); |
| 7054 | |
| 7055 | switch (usess->buffer_type) { |
| 7056 | case LTTNG_BUFFER_PER_UID: |
| 7057 | { |
| 7058 | const lttng::urcu::read_lock_guard read_lock; |
| 7059 | |
| 7060 | for (auto *reg : |
| 7061 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 7062 | usess->buffer_reg_uid_list)) { |
| 7063 | struct consumer_socket *socket; |
| 7064 | char pathname[PATH_MAX]; |
| 7065 | size_t consumer_path_offset = 0; |
| 7066 | |
| 7067 | if (!reg->registry->reg.ust->_metadata_key) { |
| 7068 | /* Skip since no metadata is present */ |
| 7069 | continue; |
| 7070 | } |
| 7071 | |
| 7072 | /* Get consumer socket to use to push the metadata.*/ |
| 7073 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 7074 | usess->consumer); |
| 7075 | if (!socket) { |
| 7076 | status = LTTNG_ERR_INVALID; |
| 7077 | goto error; |
| 7078 | } |
| 7079 | |
| 7080 | memset(pathname, 0, sizeof(pathname)); |
| 7081 | ret = snprintf(pathname, |
| 7082 | sizeof(pathname), |
| 7083 | DEFAULT_UST_TRACE_UID_PATH, |
| 7084 | reg->uid, |
| 7085 | reg->bits_per_long); |
| 7086 | if (ret < 0) { |
| 7087 | PERROR("snprintf snapshot path"); |
| 7088 | status = LTTNG_ERR_INVALID; |
| 7089 | goto error; |
| 7090 | } |
| 7091 | /* Free path allowed on previous iteration. */ |
| 7092 | free(trace_path); |
| 7093 | trace_path = setup_channel_trace_path( |
| 7094 | usess->consumer, pathname, &consumer_path_offset); |
| 7095 | if (!trace_path) { |
| 7096 | status = LTTNG_ERR_INVALID; |
| 7097 | goto error; |
| 7098 | } |
| 7099 | /* Add the UST default trace dir to path. */ |
| 7100 | for (auto *buf_reg_chan : |
| 7101 | lttng::urcu::lfht_iteration_adapter<buffer_reg_channel, |
| 7102 | decltype(buffer_reg_channel::node), |
| 7103 | &buffer_reg_channel::node>( |
| 7104 | *reg->registry->channels->ht)) { |
| 7105 | status = |
| 7106 | consumer_snapshot_channel(socket, |
| 7107 | buf_reg_chan->consumer_key, |
| 7108 | output, |
| 7109 | 0, |
| 7110 | &trace_path[consumer_path_offset], |
| 7111 | nb_packets_per_stream); |
| 7112 | if (status != LTTNG_OK) { |
| 7113 | goto error; |
| 7114 | } |
| 7115 | } |
| 7116 | status = consumer_snapshot_channel(socket, |
| 7117 | reg->registry->reg.ust->_metadata_key, |
| 7118 | output, |
| 7119 | 1, |
| 7120 | &trace_path[consumer_path_offset], |
| 7121 | 0); |
| 7122 | if (status != LTTNG_OK) { |
| 7123 | goto error; |
| 7124 | } |
| 7125 | } |
| 7126 | |
| 7127 | break; |
| 7128 | } |
| 7129 | case LTTNG_BUFFER_PER_PID: |
| 7130 | { |
| 7131 | /* Iterate on all apps. */ |
| 7132 | for (auto *app : |
| 7133 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 7134 | decltype(ust_app::pid_n), |
| 7135 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 7136 | struct consumer_socket *socket; |
| 7137 | struct ust_app_session *ua_sess; |
| 7138 | lsu::registry_session *registry; |
| 7139 | char pathname[PATH_MAX]; |
| 7140 | size_t consumer_path_offset = 0; |
| 7141 | |
| 7142 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 7143 | if (!ua_sess) { |
| 7144 | /* Session not associated with this app. */ |
| 7145 | continue; |
| 7146 | } |
| 7147 | |
| 7148 | /* Get the right consumer socket for the application. */ |
| 7149 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, output); |
| 7150 | if (!socket) { |
| 7151 | status = LTTNG_ERR_INVALID; |
| 7152 | goto error; |
| 7153 | } |
| 7154 | |
| 7155 | /* Add the UST default trace dir to path. */ |
| 7156 | memset(pathname, 0, sizeof(pathname)); |
| 7157 | ret = snprintf(pathname, sizeof(pathname), "%s", ua_sess->path); |
| 7158 | if (ret < 0) { |
| 7159 | status = LTTNG_ERR_INVALID; |
| 7160 | PERROR("snprintf snapshot path"); |
| 7161 | goto error; |
| 7162 | } |
| 7163 | /* Free path allowed on previous iteration. */ |
| 7164 | free(trace_path); |
| 7165 | trace_path = setup_channel_trace_path( |
| 7166 | usess->consumer, pathname, &consumer_path_offset); |
| 7167 | if (!trace_path) { |
| 7168 | status = LTTNG_ERR_INVALID; |
| 7169 | goto error; |
| 7170 | } |
| 7171 | |
| 7172 | for (auto *ua_chan : |
| 7173 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 7174 | decltype(ust_app_channel::node), |
| 7175 | &ust_app_channel::node>( |
| 7176 | *ua_sess->channels->ht)) { |
| 7177 | status = |
| 7178 | consumer_snapshot_channel(socket, |
| 7179 | ua_chan->key, |
| 7180 | output, |
| 7181 | 0, |
| 7182 | &trace_path[consumer_path_offset], |
| 7183 | nb_packets_per_stream); |
| 7184 | switch (status) { |
| 7185 | case LTTNG_OK: |
| 7186 | break; |
| 7187 | case LTTNG_ERR_CHAN_NOT_FOUND: |
| 7188 | continue; |
| 7189 | default: |
| 7190 | goto error; |
| 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | registry = ust_app_get_session_registry(ua_sess->get_identifier()); |
| 7195 | if (!registry) { |
| 7196 | DBG("Application session is being torn down. Skip application."); |
| 7197 | continue; |
| 7198 | } |
| 7199 | status = consumer_snapshot_channel(socket, |
| 7200 | registry->_metadata_key, |
| 7201 | output, |
| 7202 | 1, |
| 7203 | &trace_path[consumer_path_offset], |
| 7204 | 0); |
| 7205 | switch (status) { |
| 7206 | case LTTNG_OK: |
| 7207 | break; |
| 7208 | case LTTNG_ERR_CHAN_NOT_FOUND: |
| 7209 | continue; |
| 7210 | default: |
| 7211 | goto error; |
| 7212 | } |
| 7213 | } |
| 7214 | break; |
| 7215 | } |
| 7216 | default: |
| 7217 | abort(); |
| 7218 | break; |
| 7219 | } |
| 7220 | |
| 7221 | error: |
| 7222 | free(trace_path); |
| 7223 | return status; |
| 7224 | } |
| 7225 | |
| 7226 | /* |
| 7227 | * Return the size taken by one more packet per stream. |
| 7228 | */ |
| 7229 | uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess, |
| 7230 | uint64_t cur_nr_packets) |
| 7231 | { |
| 7232 | uint64_t tot_size = 0; |
| 7233 | |
| 7234 | LTTNG_ASSERT(usess); |
| 7235 | |
| 7236 | switch (usess->buffer_type) { |
| 7237 | case LTTNG_BUFFER_PER_UID: |
| 7238 | { |
| 7239 | for (auto *reg : |
| 7240 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 7241 | usess->buffer_reg_uid_list)) { |
| 7242 | for (auto *buf_reg_chan : |
| 7243 | lttng::urcu::lfht_iteration_adapter<buffer_reg_channel, |
| 7244 | decltype(buffer_reg_channel::node), |
| 7245 | &buffer_reg_channel::node>( |
| 7246 | *reg->registry->channels->ht)) { |
| 7247 | if (cur_nr_packets >= buf_reg_chan->num_subbuf) { |
| 7248 | /* |
| 7249 | * Don't take channel into account if we |
| 7250 | * already grab all its packets. |
| 7251 | */ |
| 7252 | continue; |
| 7253 | } |
| 7254 | tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count; |
| 7255 | } |
| 7256 | } |
| 7257 | break; |
| 7258 | } |
| 7259 | case LTTNG_BUFFER_PER_PID: |
| 7260 | { |
| 7261 | /* Iterate on all apps. */ |
| 7262 | for (auto *app : |
| 7263 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 7264 | decltype(ust_app::pid_n), |
| 7265 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 7266 | const auto *ua_sess = ust_app_lookup_app_session(usess, app); |
| 7267 | if (!ua_sess) { |
| 7268 | /* Session not associated with this app. */ |
| 7269 | continue; |
| 7270 | } |
| 7271 | |
| 7272 | for (auto *ua_chan : |
| 7273 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 7274 | decltype(ust_app_channel::node), |
| 7275 | &ust_app_channel::node>( |
| 7276 | *ua_sess->channels->ht)) { |
| 7277 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
| 7278 | /* |
| 7279 | * Don't take channel into account if we |
| 7280 | * already grab all its packets. |
| 7281 | */ |
| 7282 | continue; |
| 7283 | } |
| 7284 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; |
| 7285 | } |
| 7286 | } |
| 7287 | break; |
| 7288 | } |
| 7289 | default: |
| 7290 | abort(); |
| 7291 | break; |
| 7292 | } |
| 7293 | |
| 7294 | return tot_size; |
| 7295 | } |
| 7296 | |
| 7297 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, |
| 7298 | struct cds_list_head *buffer_reg_uid_list, |
| 7299 | struct consumer_output *consumer, |
| 7300 | uint64_t uchan_id, |
| 7301 | int overwrite, |
| 7302 | uint64_t *discarded, |
| 7303 | uint64_t *lost) |
| 7304 | { |
| 7305 | int ret; |
| 7306 | uint64_t consumer_chan_key; |
| 7307 | |
| 7308 | *discarded = 0; |
| 7309 | *lost = 0; |
| 7310 | |
| 7311 | ret = buffer_reg_uid_consumer_channel_key( |
| 7312 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
| 7313 | if (ret < 0) { |
| 7314 | /* Not found */ |
| 7315 | ret = 0; |
| 7316 | goto end; |
| 7317 | } |
| 7318 | |
| 7319 | if (overwrite) { |
| 7320 | ret = consumer_get_lost_packets(ust_session_id, consumer_chan_key, consumer, lost); |
| 7321 | } else { |
| 7322 | ret = consumer_get_discarded_events( |
| 7323 | ust_session_id, consumer_chan_key, consumer, discarded); |
| 7324 | } |
| 7325 | |
| 7326 | end: |
| 7327 | return ret; |
| 7328 | } |
| 7329 | |
| 7330 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, |
| 7331 | struct ltt_ust_channel *uchan, |
| 7332 | struct consumer_output *consumer, |
| 7333 | int overwrite, |
| 7334 | uint64_t *discarded, |
| 7335 | uint64_t *lost) |
| 7336 | { |
| 7337 | int ret = 0; |
| 7338 | struct lttng_ht_node_str *ua_chan_node; |
| 7339 | struct ust_app_session *ua_sess; |
| 7340 | struct ust_app_channel *ua_chan; |
| 7341 | |
| 7342 | *discarded = 0; |
| 7343 | *lost = 0; |
| 7344 | |
| 7345 | /* |
| 7346 | * Iterate over every registered applications. Sum counters for |
| 7347 | * all applications containing requested session and channel. |
| 7348 | */ |
| 7349 | for (auto *app : |
| 7350 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 7351 | *ust_app_ht->ht)) { |
| 7352 | struct lttng_ht_iter uiter; |
| 7353 | |
| 7354 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 7355 | if (ua_sess == nullptr) { |
| 7356 | continue; |
| 7357 | } |
| 7358 | |
| 7359 | /* Get channel */ |
| 7360 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 7361 | ua_chan_node = lttng_ht_iter_get_node<lttng_ht_node_str>(&uiter); |
| 7362 | /* If the session is found for the app, the channel must be there */ |
| 7363 | LTTNG_ASSERT(ua_chan_node); |
| 7364 | |
| 7365 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
| 7366 | |
| 7367 | if (overwrite) { |
| 7368 | uint64_t _lost; |
| 7369 | |
| 7370 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, consumer, &_lost); |
| 7371 | if (ret < 0) { |
| 7372 | break; |
| 7373 | } |
| 7374 | (*lost) += _lost; |
| 7375 | } else { |
| 7376 | uint64_t _discarded; |
| 7377 | |
| 7378 | ret = consumer_get_discarded_events( |
| 7379 | usess->id, ua_chan->key, consumer, &_discarded); |
| 7380 | if (ret < 0) { |
| 7381 | break; |
| 7382 | } |
| 7383 | (*discarded) += _discarded; |
| 7384 | } |
| 7385 | } |
| 7386 | |
| 7387 | return ret; |
| 7388 | } |
| 7389 | |
| 7390 | static int ust_app_regenerate_statedump(struct ltt_ust_session *usess, struct ust_app *app) |
| 7391 | { |
| 7392 | int ret = 0; |
| 7393 | struct ust_app_session *ua_sess; |
| 7394 | |
| 7395 | DBG("Regenerating the metadata for ust app pid %d", app->pid); |
| 7396 | |
| 7397 | const lttng::urcu::read_lock_guard read_lock; |
| 7398 | const auto update_health_code_on_exit = |
| 7399 | lttng::make_scope_exit([]() noexcept { health_code_update(); }); |
| 7400 | |
| 7401 | ua_sess = ust_app_lookup_app_session(usess, app); |
| 7402 | if (ua_sess == nullptr) { |
| 7403 | /* The session is in teardown process. Ignore and continue. */ |
| 7404 | return 0; |
| 7405 | } |
| 7406 | |
| 7407 | const auto locked_ua_sess = ua_sess->lock(); |
| 7408 | if (locked_ua_sess->deleted) { |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
| 7412 | pthread_mutex_lock(&app->sock_lock); |
| 7413 | ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle); |
| 7414 | pthread_mutex_unlock(&app->sock_lock); |
| 7415 | return ret; |
| 7416 | } |
| 7417 | |
| 7418 | /* |
| 7419 | * Regenerate the statedump for each app in the session. |
| 7420 | */ |
| 7421 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) |
| 7422 | { |
| 7423 | DBG("Regenerating the metadata for all UST apps"); |
| 7424 | |
| 7425 | /* Iterate on all apps. */ |
| 7426 | for (auto *app : |
| 7427 | lttng::urcu::lfht_iteration_adapter<ust_app, decltype(ust_app::pid_n), &ust_app::pid_n>( |
| 7428 | *ust_app_ht->ht)) { |
| 7429 | if (!app->compatible) { |
| 7430 | continue; |
| 7431 | } |
| 7432 | |
| 7433 | (void) ust_app_regenerate_statedump(usess, app); |
| 7434 | } |
| 7435 | |
| 7436 | return 0; |
| 7437 | } |
| 7438 | |
| 7439 | /* |
| 7440 | * Rotate all the channels of a session. |
| 7441 | * |
| 7442 | * Return LTTNG_OK on success or else an LTTng error code. |
| 7443 | */ |
| 7444 | enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session) |
| 7445 | { |
| 7446 | int ret; |
| 7447 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 7448 | struct ltt_ust_session *usess = session->ust_session; |
| 7449 | |
| 7450 | LTTNG_ASSERT(usess); |
| 7451 | |
| 7452 | switch (usess->buffer_type) { |
| 7453 | case LTTNG_BUFFER_PER_UID: |
| 7454 | { |
| 7455 | for (auto *reg : |
| 7456 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 7457 | usess->buffer_reg_uid_list)) { |
| 7458 | struct consumer_socket *socket; |
| 7459 | const lttng::urcu::read_lock_guard read_lock; |
| 7460 | |
| 7461 | /* Get consumer socket to use to push the metadata.*/ |
| 7462 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 7463 | usess->consumer); |
| 7464 | if (!socket) { |
| 7465 | cmd_ret = LTTNG_ERR_INVALID; |
| 7466 | goto error; |
| 7467 | } |
| 7468 | |
| 7469 | /* Rotate the data channels. */ |
| 7470 | for (auto *buf_reg_chan : |
| 7471 | lttng::urcu::lfht_iteration_adapter<buffer_reg_channel, |
| 7472 | decltype(buffer_reg_channel::node), |
| 7473 | &buffer_reg_channel::node>( |
| 7474 | *reg->registry->channels->ht)) { |
| 7475 | ret = consumer_rotate_channel(socket, |
| 7476 | buf_reg_chan->consumer_key, |
| 7477 | usess->consumer, |
| 7478 | /* is_metadata_channel */ false); |
| 7479 | if (ret < 0) { |
| 7480 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 7481 | goto error; |
| 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | /* |
| 7486 | * The metadata channel might not be present. |
| 7487 | * |
| 7488 | * Consumer stream allocation can be done |
| 7489 | * asynchronously and can fail on intermediary |
| 7490 | * operations (i.e add context) and lead to data |
| 7491 | * channels created with no metadata channel. |
| 7492 | */ |
| 7493 | if (!reg->registry->reg.ust->_metadata_key) { |
| 7494 | /* Skip since no metadata is present. */ |
| 7495 | continue; |
| 7496 | } |
| 7497 | |
| 7498 | { |
| 7499 | auto locked_registry = reg->registry->reg.ust->lock(); |
| 7500 | (void) push_metadata(locked_registry, usess->consumer); |
| 7501 | } |
| 7502 | |
| 7503 | ret = consumer_rotate_channel(socket, |
| 7504 | reg->registry->reg.ust->_metadata_key, |
| 7505 | usess->consumer, |
| 7506 | /* is_metadata_channel */ true); |
| 7507 | if (ret < 0) { |
| 7508 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 7509 | goto error; |
| 7510 | } |
| 7511 | } |
| 7512 | break; |
| 7513 | } |
| 7514 | case LTTNG_BUFFER_PER_PID: |
| 7515 | { |
| 7516 | /* Iterate on all apps. */ |
| 7517 | for (auto raw_app : |
| 7518 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 7519 | decltype(ust_app::pid_n), |
| 7520 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 7521 | struct consumer_socket *socket; |
| 7522 | struct ust_app_session *ua_sess; |
| 7523 | lsu::registry_session *registry; |
| 7524 | bool app_reference_taken; |
| 7525 | |
| 7526 | app_reference_taken = ust_app_get(*raw_app); |
| 7527 | if (!app_reference_taken) { |
| 7528 | /* Application unregistered concurrently, skip it. */ |
| 7529 | DBG("Could not get application reference as it is being torn down; skipping application"); |
| 7530 | continue; |
| 7531 | } |
| 7532 | |
| 7533 | ust_app_reference app(raw_app); |
| 7534 | raw_app = nullptr; |
| 7535 | |
| 7536 | ua_sess = ust_app_lookup_app_session(usess, app.get()); |
| 7537 | if (!ua_sess) { |
| 7538 | /* Session not associated with this app. */ |
| 7539 | continue; |
| 7540 | } |
| 7541 | |
| 7542 | /* Get the right consumer socket for the application. */ |
| 7543 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
| 7544 | usess->consumer); |
| 7545 | if (!socket) { |
| 7546 | cmd_ret = LTTNG_ERR_INVALID; |
| 7547 | goto error; |
| 7548 | } |
| 7549 | |
| 7550 | registry = ust_app_get_session_registry(ua_sess->get_identifier()); |
| 7551 | LTTNG_ASSERT(registry); |
| 7552 | |
| 7553 | /* Rotate the data channels. */ |
| 7554 | for (auto *ua_chan : |
| 7555 | lttng::urcu::lfht_iteration_adapter<ust_app_channel, |
| 7556 | decltype(ust_app_channel::node), |
| 7557 | &ust_app_channel::node>( |
| 7558 | *ua_sess->channels->ht)) { |
| 7559 | ret = consumer_rotate_channel(socket, |
| 7560 | ua_chan->key, |
| 7561 | ua_sess->consumer, |
| 7562 | /* is_metadata_channel */ false); |
| 7563 | if (ret < 0) { |
| 7564 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 7565 | goto error; |
| 7566 | } |
| 7567 | } |
| 7568 | |
| 7569 | /* Rotate the metadata channel. */ |
| 7570 | { |
| 7571 | auto locked_registry = registry->lock(); |
| 7572 | |
| 7573 | (void) push_metadata(locked_registry, usess->consumer); |
| 7574 | } |
| 7575 | |
| 7576 | ret = consumer_rotate_channel(socket, |
| 7577 | registry->_metadata_key, |
| 7578 | ua_sess->consumer, |
| 7579 | /* is_metadata_channel */ true); |
| 7580 | if (ret < 0) { |
| 7581 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 7582 | goto error; |
| 7583 | } |
| 7584 | } |
| 7585 | |
| 7586 | break; |
| 7587 | } |
| 7588 | default: |
| 7589 | abort(); |
| 7590 | break; |
| 7591 | } |
| 7592 | |
| 7593 | cmd_ret = LTTNG_OK; |
| 7594 | |
| 7595 | error: |
| 7596 | return cmd_ret; |
| 7597 | } |
| 7598 | |
| 7599 | enum lttng_error_code ust_app_create_channel_subdirectories(const struct ltt_ust_session *usess) |
| 7600 | { |
| 7601 | enum lttng_error_code ret = LTTNG_OK; |
| 7602 | enum lttng_trace_chunk_status chunk_status; |
| 7603 | char *pathname_index; |
| 7604 | int fmt_ret; |
| 7605 | |
| 7606 | LTTNG_ASSERT(usess->current_trace_chunk); |
| 7607 | |
| 7608 | switch (usess->buffer_type) { |
| 7609 | case LTTNG_BUFFER_PER_UID: |
| 7610 | { |
| 7611 | const lttng::urcu::read_lock_guard read_lock; |
| 7612 | |
| 7613 | for (auto *reg : |
| 7614 | lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>( |
| 7615 | usess->buffer_reg_uid_list)) { |
| 7616 | fmt_ret = asprintf(&pathname_index, |
| 7617 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH |
| 7618 | "/" DEFAULT_INDEX_DIR, |
| 7619 | reg->uid, |
| 7620 | reg->bits_per_long); |
| 7621 | if (fmt_ret < 0) { |
| 7622 | ERR("Failed to format channel index directory"); |
| 7623 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 7624 | goto error; |
| 7625 | } |
| 7626 | |
| 7627 | /* |
| 7628 | * Create the index subdirectory which will take care |
| 7629 | * of implicitly creating the channel's path. |
| 7630 | */ |
| 7631 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 7632 | usess->current_trace_chunk, pathname_index); |
| 7633 | free(pathname_index); |
| 7634 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 7635 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 7636 | goto error; |
| 7637 | } |
| 7638 | } |
| 7639 | break; |
| 7640 | } |
| 7641 | case LTTNG_BUFFER_PER_PID: |
| 7642 | { |
| 7643 | /* |
| 7644 | * Create the toplevel ust/ directory in case no apps are running. |
| 7645 | */ |
| 7646 | chunk_status = lttng_trace_chunk_create_subdirectory(usess->current_trace_chunk, |
| 7647 | DEFAULT_UST_TRACE_DIR); |
| 7648 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 7649 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 7650 | goto error; |
| 7651 | } |
| 7652 | |
| 7653 | /* Iterate on all apps. */ |
| 7654 | for (auto *app : |
| 7655 | lttng::urcu::lfht_iteration_adapter<ust_app, |
| 7656 | decltype(ust_app::pid_n), |
| 7657 | &ust_app::pid_n>(*ust_app_ht->ht)) { |
| 7658 | const auto ua_sess = ust_app_lookup_app_session(usess, app); |
| 7659 | if (!ua_sess) { |
| 7660 | /* Session not associated with this app. */ |
| 7661 | continue; |
| 7662 | } |
| 7663 | |
| 7664 | const auto registry = |
| 7665 | ust_app_get_session_registry(ua_sess->get_identifier()); |
| 7666 | if (!registry) { |
| 7667 | DBG("Application session is being torn down. Skip application."); |
| 7668 | continue; |
| 7669 | } |
| 7670 | |
| 7671 | fmt_ret = asprintf(&pathname_index, |
| 7672 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
| 7673 | ua_sess->path); |
| 7674 | if (fmt_ret < 0) { |
| 7675 | ERR("Failed to format channel index directory"); |
| 7676 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 7677 | goto error; |
| 7678 | } |
| 7679 | /* |
| 7680 | * Create the index subdirectory which will take care |
| 7681 | * of implicitly creating the channel's path. |
| 7682 | */ |
| 7683 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 7684 | usess->current_trace_chunk, pathname_index); |
| 7685 | free(pathname_index); |
| 7686 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 7687 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 7688 | goto error; |
| 7689 | } |
| 7690 | } |
| 7691 | break; |
| 7692 | } |
| 7693 | default: |
| 7694 | abort(); |
| 7695 | } |
| 7696 | |
| 7697 | ret = LTTNG_OK; |
| 7698 | error: |
| 7699 | return ret; |
| 7700 | } |
| 7701 | |
| 7702 | /* |
| 7703 | * Clear all the channels of a session. |
| 7704 | * |
| 7705 | * Return LTTNG_OK on success or else an LTTng error code. |
| 7706 | */ |
| 7707 | enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session) |
| 7708 | { |
| 7709 | const ltt_ust_session& usess = *session->ust_session; |
| 7710 | |
| 7711 | if (usess.active) { |
| 7712 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); |
| 7713 | return LTTNG_ERR_FATAL; |
| 7714 | } |
| 7715 | |
| 7716 | const auto channel_keys = session->user_space_consumer_channel_keys(); |
| 7717 | for (auto it = channel_keys.begin(); it != channel_keys.end(); ++it) { |
| 7718 | const auto key = *it; |
| 7719 | |
| 7720 | const auto consumer_socket = consumer_find_socket_by_bitness( |
| 7721 | key.bitness == |
| 7722 | lttng::sessiond::user_space_consumer_channel_keys:: |
| 7723 | consumer_bitness::ABI_32 ? |
| 7724 | 32 : |
| 7725 | 64, |
| 7726 | usess.consumer); |
| 7727 | |
| 7728 | if (key.type == |
| 7729 | lttng::sessiond::user_space_consumer_channel_keys::channel_type::METADATA) { |
| 7730 | (void) push_metadata(it.get_registry_session()->lock(), usess.consumer); |
| 7731 | } |
| 7732 | |
| 7733 | const auto clean_ret = consumer_clear_channel(consumer_socket, key.key_value); |
| 7734 | if (clean_ret < 0) { |
| 7735 | if (clean_ret == -LTTCOMM_CONSUMERD_CHAN_NOT_FOUND && |
| 7736 | usess.buffer_type == LTTNG_BUFFER_PER_PID) { |
| 7737 | continue; |
| 7738 | } |
| 7739 | |
| 7740 | if (clean_ret == -LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED) { |
| 7741 | return LTTNG_ERR_CLEAR_RELAY_DISALLOWED; |
| 7742 | } |
| 7743 | |
| 7744 | return LTTNG_ERR_CLEAR_FAIL_CONSUMER; |
| 7745 | } |
| 7746 | } |
| 7747 | |
| 7748 | return LTTNG_OK; |
| 7749 | } |
| 7750 | |
| 7751 | /* |
| 7752 | * This function skips the metadata channel as the begin/end timestamps of a |
| 7753 | * metadata packet are useless. |
| 7754 | * |
| 7755 | * Moreover, opening a packet after a "clear" will cause problems for live |
| 7756 | * sessions as it will introduce padding that was not part of the first trace |
| 7757 | * chunk. The relay daemon expects the content of the metadata stream of |
| 7758 | * successive metadata trace chunks to be strict supersets of one another. |
| 7759 | * |
| 7760 | * For example, flushing a packet at the beginning of the metadata stream of |
| 7761 | * a trace chunk resulting from a "clear" session command will cause the |
| 7762 | * size of the metadata stream of the new trace chunk to not match the size of |
| 7763 | * the metadata stream of the original chunk. This will confuse the relay |
| 7764 | * daemon as the same "offset" in a metadata stream will no longer point |
| 7765 | * to the same content. |
| 7766 | */ |
| 7767 | enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session) |
| 7768 | { |
| 7769 | const ltt_ust_session& usess = *session->ust_session; |
| 7770 | |
| 7771 | for (const auto key : session->user_space_consumer_channel_keys()) { |
| 7772 | if (key.type != |
| 7773 | lttng::sessiond::user_space_consumer_channel_keys::channel_type::DATA) { |
| 7774 | continue; |
| 7775 | } |
| 7776 | |
| 7777 | const auto socket = consumer_find_socket_by_bitness( |
| 7778 | key.bitness == |
| 7779 | lttng::sessiond::user_space_consumer_channel_keys:: |
| 7780 | consumer_bitness::ABI_32 ? |
| 7781 | 32 : |
| 7782 | 64, |
| 7783 | usess.consumer); |
| 7784 | |
| 7785 | const auto open_ret = consumer_open_channel_packets(socket, key.key_value); |
| 7786 | if (open_ret < 0) { |
| 7787 | /* Per-PID buffer and application going away. */ |
| 7788 | if (open_ret == -LTTCOMM_CONSUMERD_CHAN_NOT_FOUND && |
| 7789 | usess.buffer_type == LTTNG_BUFFER_PER_PID) { |
| 7790 | continue; |
| 7791 | } |
| 7792 | |
| 7793 | return LTTNG_ERR_UNK; |
| 7794 | } |
| 7795 | } |
| 7796 | |
| 7797 | return LTTNG_OK; |
| 7798 | } |
| 7799 | |
| 7800 | lsu::ctl_field_quirks ust_app::ctl_field_quirks() const |
| 7801 | { |
| 7802 | /* |
| 7803 | * Application contexts are expressed as variants. LTTng-UST announces |
| 7804 | * those by registering an enumeration named `..._tag`. It then registers a |
| 7805 | * variant as part of the event context that contains the various possible |
| 7806 | * types. |
| 7807 | * |
| 7808 | * Unfortunately, the names used in the enumeration and variant don't |
| 7809 | * match: the enumeration names are all prefixed with an underscore while |
| 7810 | * the variant type tag fields aren't. |
| 7811 | * |
| 7812 | * While the CTF 1.8.3 specification mentions that |
| 7813 | * underscores *should* (not *must*) be removed by CTF readers. Babeltrace |
| 7814 | * 1.x (and possibly others) expect a perfect match between the names used |
| 7815 | * by tags and variants. |
| 7816 | * |
| 7817 | * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled, |
| 7818 | * the variant's fields are modified to match the mappings of its tag. |
| 7819 | * |
| 7820 | * From ABI version >= 10.x, the variant fields and tag mapping names |
| 7821 | * correctly match, making this quirk unnecessary. |
| 7822 | */ |
| 7823 | return v_major <= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS : |
| 7824 | lsu::ctl_field_quirks::NONE; |
| 7825 | } |
| 7826 | |
| 7827 | static void ust_app_release(urcu_ref *ref) |
| 7828 | { |
| 7829 | auto& app = *lttng::utils::container_of(ref, &ust_app::ref); |
| 7830 | |
| 7831 | ust_app_unregister(app); |
| 7832 | ust_app_destroy(app); |
| 7833 | } |
| 7834 | |
| 7835 | bool ust_app_get(ust_app& app) |
| 7836 | { |
| 7837 | return urcu_ref_get_unless_zero(&app.ref); |
| 7838 | } |
| 7839 | |
| 7840 | void ust_app_put(struct ust_app *app) |
| 7841 | { |
| 7842 | if (!app) { |
| 7843 | return; |
| 7844 | } |
| 7845 | |
| 7846 | urcu_ref_put(&app->ref, ust_app_release); |
| 7847 | } |
| 7848 | |
| 7849 | lttng_ht *ust_app_get_all() |
| 7850 | { |
| 7851 | return ust_app_ht; |
| 7852 | } |