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