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