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