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