| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | #include <urcu/compiler.h> |
| 30 | #include <lttng/ust-error.h> |
| 31 | #include <signal.h> |
| 32 | |
| 33 | #include <common/common.h> |
| 34 | #include <common/sessiond-comm/sessiond-comm.h> |
| 35 | |
| 36 | #include "buffer-registry.h" |
| 37 | #include "fd-limit.h" |
| 38 | #include "health-sessiond.h" |
| 39 | #include "ust-app.h" |
| 40 | #include "ust-consumer.h" |
| 41 | #include "ust-ctl.h" |
| 42 | #include "utils.h" |
| 43 | |
| 44 | static |
| 45 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); |
| 46 | |
| 47 | /* Next available channel key. Access under next_channel_key_lock. */ |
| 48 | static uint64_t _next_channel_key; |
| 49 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; |
| 50 | |
| 51 | /* Next available session ID. Access under next_session_id_lock. */ |
| 52 | static uint64_t _next_session_id; |
| 53 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 54 | |
| 55 | /* |
| 56 | * Return the incremented value of next_channel_key. |
| 57 | */ |
| 58 | static uint64_t get_next_channel_key(void) |
| 59 | { |
| 60 | uint64_t ret; |
| 61 | |
| 62 | pthread_mutex_lock(&next_channel_key_lock); |
| 63 | ret = ++_next_channel_key; |
| 64 | pthread_mutex_unlock(&next_channel_key_lock); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Return the atomically incremented value of next_session_id. |
| 70 | */ |
| 71 | static uint64_t get_next_session_id(void) |
| 72 | { |
| 73 | uint64_t ret; |
| 74 | |
| 75 | pthread_mutex_lock(&next_session_id_lock); |
| 76 | ret = ++_next_session_id; |
| 77 | pthread_mutex_unlock(&next_session_id_lock); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static void copy_channel_attr_to_ustctl( |
| 82 | struct ustctl_consumer_channel_attr *attr, |
| 83 | struct lttng_ust_channel_attr *uattr) |
| 84 | { |
| 85 | /* Copy event attributes since the layout is different. */ |
| 86 | attr->subbuf_size = uattr->subbuf_size; |
| 87 | attr->num_subbuf = uattr->num_subbuf; |
| 88 | attr->overwrite = uattr->overwrite; |
| 89 | attr->switch_timer_interval = uattr->switch_timer_interval; |
| 90 | attr->read_timer_interval = uattr->read_timer_interval; |
| 91 | attr->output = uattr->output; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Match function for the hash table lookup. |
| 96 | * |
| 97 | * It matches an ust app event based on three attributes which are the event |
| 98 | * name, the filter bytecode and the loglevel. |
| 99 | */ |
| 100 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
| 101 | { |
| 102 | struct ust_app_event *event; |
| 103 | const struct ust_app_ht_key *key; |
| 104 | |
| 105 | assert(node); |
| 106 | assert(_key); |
| 107 | |
| 108 | event = caa_container_of(node, struct ust_app_event, node.node); |
| 109 | key = _key; |
| 110 | |
| 111 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
| 112 | |
| 113 | /* Event name */ |
| 114 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { |
| 115 | goto no_match; |
| 116 | } |
| 117 | |
| 118 | /* Event loglevel. */ |
| 119 | if (event->attr.loglevel != key->loglevel) { |
| 120 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
| 121 | && key->loglevel == 0 && event->attr.loglevel == -1) { |
| 122 | /* |
| 123 | * Match is accepted. This is because on event creation, the |
| 124 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and |
| 125 | * -1 are accepted for this loglevel type since 0 is the one set by |
| 126 | * the API when receiving an enable event. |
| 127 | */ |
| 128 | } else { |
| 129 | goto no_match; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* One of the filters is NULL, fail. */ |
| 134 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { |
| 135 | goto no_match; |
| 136 | } |
| 137 | |
| 138 | if (key->filter && event->filter) { |
| 139 | /* Both filters exists, check length followed by the bytecode. */ |
| 140 | if (event->filter->len != key->filter->len || |
| 141 | memcmp(event->filter->data, key->filter->data, |
| 142 | event->filter->len) != 0) { |
| 143 | goto no_match; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* One of the exclusions is NULL, fail. */ |
| 148 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { |
| 149 | goto no_match; |
| 150 | } |
| 151 | |
| 152 | if (key->exclusion && event->exclusion) { |
| 153 | /* Both exclusions exists, check count followed by the names. */ |
| 154 | if (event->exclusion->count != key->exclusion->count || |
| 155 | memcmp(event->exclusion->names, key->exclusion->names, |
| 156 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { |
| 157 | goto no_match; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* Match. */ |
| 163 | return 1; |
| 164 | |
| 165 | no_match: |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Unique add of an ust app event in the given ht. This uses the custom |
| 171 | * ht_match_ust_app_event match function and the event name as hash. |
| 172 | */ |
| 173 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
| 174 | struct ust_app_event *event) |
| 175 | { |
| 176 | struct cds_lfht_node *node_ptr; |
| 177 | struct ust_app_ht_key key; |
| 178 | struct lttng_ht *ht; |
| 179 | |
| 180 | assert(ua_chan); |
| 181 | assert(ua_chan->events); |
| 182 | assert(event); |
| 183 | |
| 184 | ht = ua_chan->events; |
| 185 | key.name = event->attr.name; |
| 186 | key.filter = event->filter; |
| 187 | key.loglevel = event->attr.loglevel; |
| 188 | key.exclusion = event->exclusion; |
| 189 | |
| 190 | node_ptr = cds_lfht_add_unique(ht->ht, |
| 191 | ht->hash_fct(event->node.key, lttng_ht_seed), |
| 192 | ht_match_ust_app_event, &key, &event->node.node); |
| 193 | assert(node_ptr == &event->node.node); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Close the notify socket from the given RCU head object. This MUST be called |
| 198 | * through a call_rcu(). |
| 199 | */ |
| 200 | static void close_notify_sock_rcu(struct rcu_head *head) |
| 201 | { |
| 202 | int ret; |
| 203 | struct ust_app_notify_sock_obj *obj = |
| 204 | caa_container_of(head, struct ust_app_notify_sock_obj, head); |
| 205 | |
| 206 | /* Must have a valid fd here. */ |
| 207 | assert(obj->fd >= 0); |
| 208 | |
| 209 | ret = close(obj->fd); |
| 210 | if (ret) { |
| 211 | ERR("close notify sock %d RCU", obj->fd); |
| 212 | } |
| 213 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 214 | |
| 215 | free(obj); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Return the session registry according to the buffer type of the given |
| 220 | * session. |
| 221 | * |
| 222 | * A registry per UID object MUST exists before calling this function or else |
| 223 | * it assert() if not found. RCU read side lock must be acquired. |
| 224 | */ |
| 225 | static struct ust_registry_session *get_session_registry( |
| 226 | struct ust_app_session *ua_sess) |
| 227 | { |
| 228 | struct ust_registry_session *registry = NULL; |
| 229 | |
| 230 | assert(ua_sess); |
| 231 | |
| 232 | switch (ua_sess->buffer_type) { |
| 233 | case LTTNG_BUFFER_PER_PID: |
| 234 | { |
| 235 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 236 | if (!reg_pid) { |
| 237 | goto error; |
| 238 | } |
| 239 | registry = reg_pid->registry->reg.ust; |
| 240 | break; |
| 241 | } |
| 242 | case LTTNG_BUFFER_PER_UID: |
| 243 | { |
| 244 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( |
| 245 | ua_sess->tracing_id, ua_sess->bits_per_long, ua_sess->uid); |
| 246 | if (!reg_uid) { |
| 247 | goto error; |
| 248 | } |
| 249 | registry = reg_uid->registry->reg.ust; |
| 250 | break; |
| 251 | } |
| 252 | default: |
| 253 | assert(0); |
| 254 | }; |
| 255 | |
| 256 | error: |
| 257 | return registry; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Delete ust context safely. RCU read lock must be held before calling |
| 262 | * this function. |
| 263 | */ |
| 264 | static |
| 265 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx) |
| 266 | { |
| 267 | int ret; |
| 268 | |
| 269 | assert(ua_ctx); |
| 270 | |
| 271 | if (ua_ctx->obj) { |
| 272 | ret = ustctl_release_object(sock, ua_ctx->obj); |
| 273 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 274 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", |
| 275 | sock, ua_ctx->obj->handle, ret); |
| 276 | } |
| 277 | free(ua_ctx->obj); |
| 278 | } |
| 279 | free(ua_ctx); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Delete ust app event safely. RCU read lock must be held before calling |
| 284 | * this function. |
| 285 | */ |
| 286 | static |
| 287 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event) |
| 288 | { |
| 289 | int ret; |
| 290 | |
| 291 | assert(ua_event); |
| 292 | |
| 293 | free(ua_event->filter); |
| 294 | if (ua_event->exclusion != NULL) |
| 295 | free(ua_event->exclusion); |
| 296 | if (ua_event->obj != NULL) { |
| 297 | ret = ustctl_release_object(sock, ua_event->obj); |
| 298 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 299 | ERR("UST app sock %d release event obj failed with ret %d", |
| 300 | sock, ret); |
| 301 | } |
| 302 | free(ua_event->obj); |
| 303 | } |
| 304 | free(ua_event); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Release ust data object of the given stream. |
| 309 | * |
| 310 | * Return 0 on success or else a negative value. |
| 311 | */ |
| 312 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream) |
| 313 | { |
| 314 | int ret = 0; |
| 315 | |
| 316 | assert(stream); |
| 317 | |
| 318 | if (stream->obj) { |
| 319 | ret = ustctl_release_object(sock, stream->obj); |
| 320 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 321 | ERR("UST app sock %d release stream obj failed with ret %d", |
| 322 | sock, ret); |
| 323 | } |
| 324 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 325 | free(stream->obj); |
| 326 | } |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Delete ust app stream safely. RCU read lock must be held before calling |
| 333 | * this function. |
| 334 | */ |
| 335 | static |
| 336 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream) |
| 337 | { |
| 338 | assert(stream); |
| 339 | |
| 340 | (void) release_ust_app_stream(sock, stream); |
| 341 | free(stream); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * We need to execute ht_destroy outside of RCU read-side critical |
| 346 | * section and outside of call_rcu thread, so we postpone its execution |
| 347 | * using ht_cleanup_push. It is simpler than to change the semantic of |
| 348 | * the many callers of delete_ust_app_session(). |
| 349 | */ |
| 350 | static |
| 351 | void delete_ust_app_channel_rcu(struct rcu_head *head) |
| 352 | { |
| 353 | struct ust_app_channel *ua_chan = |
| 354 | caa_container_of(head, struct ust_app_channel, rcu_head); |
| 355 | |
| 356 | ht_cleanup_push(ua_chan->ctx); |
| 357 | ht_cleanup_push(ua_chan->events); |
| 358 | free(ua_chan); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Delete ust app channel safely. RCU read lock must be held before calling |
| 363 | * this function. |
| 364 | */ |
| 365 | static |
| 366 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
| 367 | struct ust_app *app) |
| 368 | { |
| 369 | int ret; |
| 370 | struct lttng_ht_iter iter; |
| 371 | struct ust_app_event *ua_event; |
| 372 | struct ust_app_ctx *ua_ctx; |
| 373 | struct ust_app_stream *stream, *stmp; |
| 374 | struct ust_registry_session *registry; |
| 375 | |
| 376 | assert(ua_chan); |
| 377 | |
| 378 | DBG3("UST app deleting channel %s", ua_chan->name); |
| 379 | |
| 380 | /* Wipe stream */ |
| 381 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 382 | cds_list_del(&stream->list); |
| 383 | delete_ust_app_stream(sock, stream); |
| 384 | } |
| 385 | |
| 386 | /* Wipe context */ |
| 387 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
| 388 | cds_list_del(&ua_ctx->list); |
| 389 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
| 390 | assert(!ret); |
| 391 | delete_ust_app_ctx(sock, ua_ctx); |
| 392 | } |
| 393 | |
| 394 | /* Wipe events */ |
| 395 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
| 396 | node.node) { |
| 397 | ret = lttng_ht_del(ua_chan->events, &iter); |
| 398 | assert(!ret); |
| 399 | delete_ust_app_event(sock, ua_event); |
| 400 | } |
| 401 | |
| 402 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 403 | /* Wipe and free registry from session registry. */ |
| 404 | registry = get_session_registry(ua_chan->session); |
| 405 | if (registry) { |
| 406 | ust_registry_channel_del_free(registry, ua_chan->key); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (ua_chan->obj != NULL) { |
| 411 | /* Remove channel from application UST object descriptor. */ |
| 412 | iter.iter.node = &ua_chan->ust_objd_node.node; |
| 413 | ret = lttng_ht_del(app->ust_objd, &iter); |
| 414 | assert(!ret); |
| 415 | ret = ustctl_release_object(sock, ua_chan->obj); |
| 416 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 417 | ERR("UST app sock %d release channel obj failed with ret %d", |
| 418 | sock, ret); |
| 419 | } |
| 420 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 421 | free(ua_chan->obj); |
| 422 | } |
| 423 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Push metadata to consumer socket. |
| 428 | * |
| 429 | * RCU read-side lock must be held to guarantee existance of socket. |
| 430 | * Must be called with the ust app session lock held. |
| 431 | * Must be called with the registry lock held. |
| 432 | * |
| 433 | * On success, return the len of metadata pushed or else a negative value. |
| 434 | * Returning a -EPIPE return value means we could not send the metadata, |
| 435 | * but it can be caused by recoverable errors (e.g. the application has |
| 436 | * terminated concurrently). |
| 437 | */ |
| 438 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, |
| 439 | struct consumer_socket *socket, int send_zero_data) |
| 440 | { |
| 441 | int ret; |
| 442 | char *metadata_str = NULL; |
| 443 | size_t len, offset; |
| 444 | ssize_t ret_val; |
| 445 | |
| 446 | assert(registry); |
| 447 | assert(socket); |
| 448 | |
| 449 | /* |
| 450 | * Means that no metadata was assigned to the session. This can |
| 451 | * happens if no start has been done previously. |
| 452 | */ |
| 453 | if (!registry->metadata_key) { |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * On a push metadata error either the consumer is dead or the |
| 459 | * metadata channel has been destroyed because its endpoint |
| 460 | * might have died (e.g: relayd), or because the application has |
| 461 | * exited. If so, the metadata closed flag is set to 1 so we |
| 462 | * deny pushing metadata again which is not valid anymore on the |
| 463 | * consumer side. |
| 464 | */ |
| 465 | if (registry->metadata_closed) { |
| 466 | return -EPIPE; |
| 467 | } |
| 468 | |
| 469 | offset = registry->metadata_len_sent; |
| 470 | len = registry->metadata_len - registry->metadata_len_sent; |
| 471 | if (len == 0) { |
| 472 | DBG3("No metadata to push for metadata key %" PRIu64, |
| 473 | registry->metadata_key); |
| 474 | ret_val = len; |
| 475 | if (send_zero_data) { |
| 476 | DBG("No metadata to push"); |
| 477 | goto push_data; |
| 478 | } |
| 479 | goto end; |
| 480 | } |
| 481 | |
| 482 | /* Allocate only what we have to send. */ |
| 483 | metadata_str = zmalloc(len); |
| 484 | if (!metadata_str) { |
| 485 | PERROR("zmalloc ust app metadata string"); |
| 486 | ret_val = -ENOMEM; |
| 487 | goto error; |
| 488 | } |
| 489 | /* Copy what we haven't send out. */ |
| 490 | memcpy(metadata_str, registry->metadata + offset, len); |
| 491 | registry->metadata_len_sent += len; |
| 492 | |
| 493 | push_data: |
| 494 | ret = consumer_push_metadata(socket, registry->metadata_key, |
| 495 | metadata_str, len, offset); |
| 496 | if (ret < 0) { |
| 497 | /* |
| 498 | * There is an acceptable race here between the registry |
| 499 | * metadata key assignment and the creation on the |
| 500 | * consumer. The session daemon can concurrently push |
| 501 | * metadata for this registry while being created on the |
| 502 | * consumer since the metadata key of the registry is |
| 503 | * assigned *before* it is setup to avoid the consumer |
| 504 | * to ask for metadata that could possibly be not found |
| 505 | * in the session daemon. |
| 506 | * |
| 507 | * The metadata will get pushed either by the session |
| 508 | * being stopped or the consumer requesting metadata if |
| 509 | * that race is triggered. |
| 510 | */ |
| 511 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { |
| 512 | ret = 0; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Update back the actual metadata len sent since it |
| 517 | * failed here. |
| 518 | */ |
| 519 | registry->metadata_len_sent -= len; |
| 520 | ret_val = ret; |
| 521 | goto error_push; |
| 522 | } |
| 523 | |
| 524 | free(metadata_str); |
| 525 | return len; |
| 526 | |
| 527 | end: |
| 528 | error: |
| 529 | if (ret_val) { |
| 530 | /* |
| 531 | * On error, flag the registry that the metadata is |
| 532 | * closed. We were unable to push anything and this |
| 533 | * means that either the consumer is not responding or |
| 534 | * the metadata cache has been destroyed on the |
| 535 | * consumer. |
| 536 | */ |
| 537 | registry->metadata_closed = 1; |
| 538 | } |
| 539 | error_push: |
| 540 | free(metadata_str); |
| 541 | return ret_val; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * For a given application and session, push metadata to consumer. |
| 546 | * Either sock or consumer is required : if sock is NULL, the default |
| 547 | * socket to send the metadata is retrieved from consumer, if sock |
| 548 | * is not NULL we use it to send the metadata. |
| 549 | * RCU read-side lock must be held while calling this function, |
| 550 | * therefore ensuring existance of registry. It also ensures existance |
| 551 | * of socket throughout this function. |
| 552 | * |
| 553 | * Return 0 on success else a negative error. |
| 554 | * Returning a -EPIPE return value means we could not send the metadata, |
| 555 | * but it can be caused by recoverable errors (e.g. the application has |
| 556 | * terminated concurrently). |
| 557 | */ |
| 558 | static int push_metadata(struct ust_registry_session *registry, |
| 559 | struct consumer_output *consumer) |
| 560 | { |
| 561 | int ret_val; |
| 562 | ssize_t ret; |
| 563 | struct consumer_socket *socket; |
| 564 | |
| 565 | assert(registry); |
| 566 | assert(consumer); |
| 567 | |
| 568 | pthread_mutex_lock(®istry->lock); |
| 569 | if (registry->metadata_closed) { |
| 570 | ret_val = -EPIPE; |
| 571 | goto error; |
| 572 | } |
| 573 | |
| 574 | /* Get consumer socket to use to push the metadata.*/ |
| 575 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
| 576 | consumer); |
| 577 | if (!socket) { |
| 578 | ret_val = -1; |
| 579 | goto error; |
| 580 | } |
| 581 | |
| 582 | ret = ust_app_push_metadata(registry, socket, 0); |
| 583 | if (ret < 0) { |
| 584 | ret_val = ret; |
| 585 | goto error; |
| 586 | } |
| 587 | pthread_mutex_unlock(®istry->lock); |
| 588 | return 0; |
| 589 | |
| 590 | error: |
| 591 | pthread_mutex_unlock(®istry->lock); |
| 592 | return ret_val; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Send to the consumer a close metadata command for the given session. Once |
| 597 | * done, the metadata channel is deleted and the session metadata pointer is |
| 598 | * nullified. The session lock MUST be held unless the application is |
| 599 | * in the destroy path. |
| 600 | * |
| 601 | * Return 0 on success else a negative value. |
| 602 | */ |
| 603 | static int close_metadata(struct ust_registry_session *registry, |
| 604 | struct consumer_output *consumer) |
| 605 | { |
| 606 | int ret; |
| 607 | struct consumer_socket *socket; |
| 608 | |
| 609 | assert(registry); |
| 610 | assert(consumer); |
| 611 | |
| 612 | rcu_read_lock(); |
| 613 | |
| 614 | pthread_mutex_lock(®istry->lock); |
| 615 | |
| 616 | if (!registry->metadata_key || registry->metadata_closed) { |
| 617 | ret = 0; |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | /* Get consumer socket to use to push the metadata.*/ |
| 622 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
| 623 | consumer); |
| 624 | if (!socket) { |
| 625 | ret = -1; |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | ret = consumer_close_metadata(socket, registry->metadata_key); |
| 630 | if (ret < 0) { |
| 631 | goto error; |
| 632 | } |
| 633 | |
| 634 | error: |
| 635 | /* |
| 636 | * Metadata closed. Even on error this means that the consumer is not |
| 637 | * responding or not found so either way a second close should NOT be emit |
| 638 | * for this registry. |
| 639 | */ |
| 640 | registry->metadata_closed = 1; |
| 641 | end: |
| 642 | pthread_mutex_unlock(®istry->lock); |
| 643 | rcu_read_unlock(); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * We need to execute ht_destroy outside of RCU read-side critical |
| 649 | * section and outside of call_rcu thread, so we postpone its execution |
| 650 | * using ht_cleanup_push. It is simpler than to change the semantic of |
| 651 | * the many callers of delete_ust_app_session(). |
| 652 | */ |
| 653 | static |
| 654 | void delete_ust_app_session_rcu(struct rcu_head *head) |
| 655 | { |
| 656 | struct ust_app_session *ua_sess = |
| 657 | caa_container_of(head, struct ust_app_session, rcu_head); |
| 658 | |
| 659 | ht_cleanup_push(ua_sess->channels); |
| 660 | free(ua_sess); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Delete ust app session safely. RCU read lock must be held before calling |
| 665 | * this function. |
| 666 | */ |
| 667 | static |
| 668 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
| 669 | struct ust_app *app) |
| 670 | { |
| 671 | int ret; |
| 672 | struct lttng_ht_iter iter; |
| 673 | struct ust_app_channel *ua_chan; |
| 674 | struct ust_registry_session *registry; |
| 675 | |
| 676 | assert(ua_sess); |
| 677 | |
| 678 | pthread_mutex_lock(&ua_sess->lock); |
| 679 | |
| 680 | registry = get_session_registry(ua_sess); |
| 681 | if (registry) { |
| 682 | /* Push metadata for application before freeing the application. */ |
| 683 | (void) push_metadata(registry, ua_sess->consumer); |
| 684 | |
| 685 | /* |
| 686 | * Don't ask to close metadata for global per UID buffers. Close |
| 687 | * metadata only on destroy trace session in this case. Also, the |
| 688 | * previous push metadata could have flag the metadata registry to |
| 689 | * close so don't send a close command if closed. |
| 690 | */ |
| 691 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 692 | /* And ask to close it for this session registry. */ |
| 693 | (void) close_metadata(registry, ua_sess->consumer); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 698 | node.node) { |
| 699 | ret = lttng_ht_del(ua_sess->channels, &iter); |
| 700 | assert(!ret); |
| 701 | delete_ust_app_channel(sock, ua_chan, app); |
| 702 | } |
| 703 | |
| 704 | /* In case of per PID, the registry is kept in the session. */ |
| 705 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 706 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 707 | if (reg_pid) { |
| 708 | buffer_reg_pid_remove(reg_pid); |
| 709 | buffer_reg_pid_destroy(reg_pid); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (ua_sess->handle != -1) { |
| 714 | ret = ustctl_release_handle(sock, ua_sess->handle); |
| 715 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 716 | ERR("UST app sock %d release session handle failed with ret %d", |
| 717 | sock, ret); |
| 718 | } |
| 719 | } |
| 720 | pthread_mutex_unlock(&ua_sess->lock); |
| 721 | |
| 722 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Delete a traceable application structure from the global list. Never call |
| 727 | * this function outside of a call_rcu call. |
| 728 | * |
| 729 | * RCU read side lock should _NOT_ be held when calling this function. |
| 730 | */ |
| 731 | static |
| 732 | void delete_ust_app(struct ust_app *app) |
| 733 | { |
| 734 | int ret, sock; |
| 735 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
| 736 | |
| 737 | /* Delete ust app sessions info */ |
| 738 | sock = app->sock; |
| 739 | app->sock = -1; |
| 740 | |
| 741 | /* Wipe sessions */ |
| 742 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
| 743 | teardown_node) { |
| 744 | /* Free every object in the session and the session. */ |
| 745 | rcu_read_lock(); |
| 746 | delete_ust_app_session(sock, ua_sess, app); |
| 747 | rcu_read_unlock(); |
| 748 | } |
| 749 | |
| 750 | ht_cleanup_push(app->sessions); |
| 751 | ht_cleanup_push(app->ust_objd); |
| 752 | |
| 753 | /* |
| 754 | * Wait until we have deleted the application from the sock hash table |
| 755 | * before closing this socket, otherwise an application could re-use the |
| 756 | * socket ID and race with the teardown, using the same hash table entry. |
| 757 | * |
| 758 | * It's OK to leave the close in call_rcu. We want it to stay unique for |
| 759 | * all RCU readers that could run concurrently with unregister app, |
| 760 | * therefore we _need_ to only close that socket after a grace period. So |
| 761 | * it should stay in this RCU callback. |
| 762 | * |
| 763 | * This close() is a very important step of the synchronization model so |
| 764 | * every modification to this function must be carefully reviewed. |
| 765 | */ |
| 766 | ret = close(sock); |
| 767 | if (ret) { |
| 768 | PERROR("close"); |
| 769 | } |
| 770 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 771 | |
| 772 | DBG2("UST app pid %d deleted", app->pid); |
| 773 | free(app); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * URCU intermediate call to delete an UST app. |
| 778 | */ |
| 779 | static |
| 780 | void delete_ust_app_rcu(struct rcu_head *head) |
| 781 | { |
| 782 | struct lttng_ht_node_ulong *node = |
| 783 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 784 | struct ust_app *app = |
| 785 | caa_container_of(node, struct ust_app, pid_n); |
| 786 | |
| 787 | DBG3("Call RCU deleting app PID %d", app->pid); |
| 788 | delete_ust_app(app); |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Delete the session from the application ht and delete the data structure by |
| 793 | * freeing every object inside and releasing them. |
| 794 | */ |
| 795 | static void destroy_app_session(struct ust_app *app, |
| 796 | struct ust_app_session *ua_sess) |
| 797 | { |
| 798 | int ret; |
| 799 | struct lttng_ht_iter iter; |
| 800 | |
| 801 | assert(app); |
| 802 | assert(ua_sess); |
| 803 | |
| 804 | iter.iter.node = &ua_sess->node.node; |
| 805 | ret = lttng_ht_del(app->sessions, &iter); |
| 806 | if (ret) { |
| 807 | /* Already scheduled for teardown. */ |
| 808 | goto end; |
| 809 | } |
| 810 | |
| 811 | /* Once deleted, free the data structure. */ |
| 812 | delete_ust_app_session(app->sock, ua_sess, app); |
| 813 | |
| 814 | end: |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Alloc new UST app session. |
| 820 | */ |
| 821 | static |
| 822 | struct ust_app_session *alloc_ust_app_session(struct ust_app *app) |
| 823 | { |
| 824 | struct ust_app_session *ua_sess; |
| 825 | |
| 826 | /* Init most of the default value by allocating and zeroing */ |
| 827 | ua_sess = zmalloc(sizeof(struct ust_app_session)); |
| 828 | if (ua_sess == NULL) { |
| 829 | PERROR("malloc"); |
| 830 | goto error_free; |
| 831 | } |
| 832 | |
| 833 | ua_sess->handle = -1; |
| 834 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 835 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
| 836 | pthread_mutex_init(&ua_sess->lock, NULL); |
| 837 | |
| 838 | return ua_sess; |
| 839 | |
| 840 | error_free: |
| 841 | return NULL; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Alloc new UST app channel. |
| 846 | */ |
| 847 | static |
| 848 | struct ust_app_channel *alloc_ust_app_channel(char *name, |
| 849 | struct ust_app_session *ua_sess, |
| 850 | struct lttng_ust_channel_attr *attr) |
| 851 | { |
| 852 | struct ust_app_channel *ua_chan; |
| 853 | |
| 854 | /* Init most of the default value by allocating and zeroing */ |
| 855 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); |
| 856 | if (ua_chan == NULL) { |
| 857 | PERROR("malloc"); |
| 858 | goto error; |
| 859 | } |
| 860 | |
| 861 | /* Setup channel name */ |
| 862 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); |
| 863 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 864 | |
| 865 | ua_chan->enabled = 1; |
| 866 | ua_chan->handle = -1; |
| 867 | ua_chan->session = ua_sess; |
| 868 | ua_chan->key = get_next_channel_key(); |
| 869 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 870 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 871 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); |
| 872 | |
| 873 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); |
| 874 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
| 875 | |
| 876 | /* Copy attributes */ |
| 877 | if (attr) { |
| 878 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
| 879 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
| 880 | ua_chan->attr.num_subbuf = attr->num_subbuf; |
| 881 | ua_chan->attr.overwrite = attr->overwrite; |
| 882 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; |
| 883 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; |
| 884 | ua_chan->attr.output = attr->output; |
| 885 | } |
| 886 | /* By default, the channel is a per cpu channel. */ |
| 887 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; |
| 888 | |
| 889 | DBG3("UST app channel %s allocated", ua_chan->name); |
| 890 | |
| 891 | return ua_chan; |
| 892 | |
| 893 | error: |
| 894 | return NULL; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Allocate and initialize a UST app stream. |
| 899 | * |
| 900 | * Return newly allocated stream pointer or NULL on error. |
| 901 | */ |
| 902 | struct ust_app_stream *ust_app_alloc_stream(void) |
| 903 | { |
| 904 | struct ust_app_stream *stream = NULL; |
| 905 | |
| 906 | stream = zmalloc(sizeof(*stream)); |
| 907 | if (stream == NULL) { |
| 908 | PERROR("zmalloc ust app stream"); |
| 909 | goto error; |
| 910 | } |
| 911 | |
| 912 | /* Zero could be a valid value for a handle so flag it to -1. */ |
| 913 | stream->handle = -1; |
| 914 | |
| 915 | error: |
| 916 | return stream; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Alloc new UST app event. |
| 921 | */ |
| 922 | static |
| 923 | struct ust_app_event *alloc_ust_app_event(char *name, |
| 924 | struct lttng_ust_event *attr) |
| 925 | { |
| 926 | struct ust_app_event *ua_event; |
| 927 | |
| 928 | /* Init most of the default value by allocating and zeroing */ |
| 929 | ua_event = zmalloc(sizeof(struct ust_app_event)); |
| 930 | if (ua_event == NULL) { |
| 931 | PERROR("malloc"); |
| 932 | goto error; |
| 933 | } |
| 934 | |
| 935 | ua_event->enabled = 1; |
| 936 | strncpy(ua_event->name, name, sizeof(ua_event->name)); |
| 937 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 938 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
| 939 | |
| 940 | /* Copy attributes */ |
| 941 | if (attr) { |
| 942 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); |
| 943 | } |
| 944 | |
| 945 | DBG3("UST app event %s allocated", ua_event->name); |
| 946 | |
| 947 | return ua_event; |
| 948 | |
| 949 | error: |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Alloc new UST app context. |
| 955 | */ |
| 956 | static |
| 957 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx) |
| 958 | { |
| 959 | struct ust_app_ctx *ua_ctx; |
| 960 | |
| 961 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); |
| 962 | if (ua_ctx == NULL) { |
| 963 | goto error; |
| 964 | } |
| 965 | |
| 966 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
| 967 | |
| 968 | if (uctx) { |
| 969 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); |
| 970 | } |
| 971 | |
| 972 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); |
| 973 | |
| 974 | error: |
| 975 | return ua_ctx; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Allocate a filter and copy the given original filter. |
| 980 | * |
| 981 | * Return allocated filter or NULL on error. |
| 982 | */ |
| 983 | static struct lttng_filter_bytecode *copy_filter_bytecode( |
| 984 | struct lttng_filter_bytecode *orig_f) |
| 985 | { |
| 986 | struct lttng_filter_bytecode *filter = NULL; |
| 987 | |
| 988 | /* Copy filter bytecode */ |
| 989 | filter = zmalloc(sizeof(*filter) + orig_f->len); |
| 990 | if (!filter) { |
| 991 | PERROR("zmalloc alloc filter bytecode"); |
| 992 | goto error; |
| 993 | } |
| 994 | |
| 995 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
| 996 | |
| 997 | error: |
| 998 | return filter; |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Create a liblttng-ust filter bytecode from given bytecode. |
| 1003 | * |
| 1004 | * Return allocated filter or NULL on error. |
| 1005 | */ |
| 1006 | static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode( |
| 1007 | struct lttng_filter_bytecode *orig_f) |
| 1008 | { |
| 1009 | struct lttng_ust_filter_bytecode *filter = NULL; |
| 1010 | |
| 1011 | /* Copy filter bytecode */ |
| 1012 | filter = zmalloc(sizeof(*filter) + orig_f->len); |
| 1013 | if (!filter) { |
| 1014 | PERROR("zmalloc alloc ust filter bytecode"); |
| 1015 | goto error; |
| 1016 | } |
| 1017 | |
| 1018 | assert(sizeof(struct lttng_filter_bytecode) == |
| 1019 | sizeof(struct lttng_ust_filter_bytecode)); |
| 1020 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
| 1021 | error: |
| 1022 | return filter; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Find an ust_app using the sock and return it. RCU read side lock must be |
| 1027 | * held before calling this helper function. |
| 1028 | */ |
| 1029 | struct ust_app *ust_app_find_by_sock(int sock) |
| 1030 | { |
| 1031 | struct lttng_ht_node_ulong *node; |
| 1032 | struct lttng_ht_iter iter; |
| 1033 | |
| 1034 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
| 1035 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1036 | if (node == NULL) { |
| 1037 | DBG2("UST app find by sock %d not found", sock); |
| 1038 | goto error; |
| 1039 | } |
| 1040 | |
| 1041 | return caa_container_of(node, struct ust_app, sock_n); |
| 1042 | |
| 1043 | error: |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * Find an ust_app using the notify sock and return it. RCU read side lock must |
| 1049 | * be held before calling this helper function. |
| 1050 | */ |
| 1051 | static struct ust_app *find_app_by_notify_sock(int sock) |
| 1052 | { |
| 1053 | struct lttng_ht_node_ulong *node; |
| 1054 | struct lttng_ht_iter iter; |
| 1055 | |
| 1056 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), |
| 1057 | &iter); |
| 1058 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1059 | if (node == NULL) { |
| 1060 | DBG2("UST app find by notify sock %d not found", sock); |
| 1061 | goto error; |
| 1062 | } |
| 1063 | |
| 1064 | return caa_container_of(node, struct ust_app, notify_sock_n); |
| 1065 | |
| 1066 | error: |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Lookup for an ust app event based on event name, filter bytecode and the |
| 1072 | * event loglevel. |
| 1073 | * |
| 1074 | * Return an ust_app_event object or NULL on error. |
| 1075 | */ |
| 1076 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
| 1077 | char *name, struct lttng_filter_bytecode *filter, int loglevel, |
| 1078 | const struct lttng_event_exclusion *exclusion) |
| 1079 | { |
| 1080 | struct lttng_ht_iter iter; |
| 1081 | struct lttng_ht_node_str *node; |
| 1082 | struct ust_app_event *event = NULL; |
| 1083 | struct ust_app_ht_key key; |
| 1084 | |
| 1085 | assert(name); |
| 1086 | assert(ht); |
| 1087 | |
| 1088 | /* Setup key for event lookup. */ |
| 1089 | key.name = name; |
| 1090 | key.filter = filter; |
| 1091 | key.loglevel = loglevel; |
| 1092 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
| 1093 | key.exclusion = exclusion; |
| 1094 | |
| 1095 | /* Lookup using the event name as hash and a custom match fct. */ |
| 1096 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), |
| 1097 | ht_match_ust_app_event, &key, &iter.iter); |
| 1098 | node = lttng_ht_iter_get_node_str(&iter); |
| 1099 | if (node == NULL) { |
| 1100 | goto end; |
| 1101 | } |
| 1102 | |
| 1103 | event = caa_container_of(node, struct ust_app_event, node); |
| 1104 | |
| 1105 | end: |
| 1106 | return event; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Create the channel context on the tracer. |
| 1111 | * |
| 1112 | * Called with UST app session lock held. |
| 1113 | */ |
| 1114 | static |
| 1115 | int create_ust_channel_context(struct ust_app_channel *ua_chan, |
| 1116 | struct ust_app_ctx *ua_ctx, struct ust_app *app) |
| 1117 | { |
| 1118 | int ret; |
| 1119 | |
| 1120 | health_code_update(); |
| 1121 | |
| 1122 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
| 1123 | ua_chan->obj, &ua_ctx->obj); |
| 1124 | if (ret < 0) { |
| 1125 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1126 | ERR("UST app create channel context failed for app (pid: %d) " |
| 1127 | "with ret %d", app->pid, ret); |
| 1128 | } else { |
| 1129 | /* |
| 1130 | * This is normal behavior, an application can die during the |
| 1131 | * creation process. Don't report an error so the execution can |
| 1132 | * continue normally. |
| 1133 | */ |
| 1134 | ret = 0; |
| 1135 | DBG3("UST app disable event failed. Application is dead."); |
| 1136 | } |
| 1137 | goto error; |
| 1138 | } |
| 1139 | |
| 1140 | ua_ctx->handle = ua_ctx->obj->handle; |
| 1141 | |
| 1142 | DBG2("UST app context handle %d created successfully for channel %s", |
| 1143 | ua_ctx->handle, ua_chan->name); |
| 1144 | |
| 1145 | error: |
| 1146 | health_code_update(); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Set the filter on the tracer. |
| 1152 | */ |
| 1153 | static |
| 1154 | int set_ust_event_filter(struct ust_app_event *ua_event, |
| 1155 | struct ust_app *app) |
| 1156 | { |
| 1157 | int ret; |
| 1158 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
| 1159 | |
| 1160 | health_code_update(); |
| 1161 | |
| 1162 | if (!ua_event->filter) { |
| 1163 | ret = 0; |
| 1164 | goto error; |
| 1165 | } |
| 1166 | |
| 1167 | ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter); |
| 1168 | if (!ust_bytecode) { |
| 1169 | ret = -LTTNG_ERR_NOMEM; |
| 1170 | goto error; |
| 1171 | } |
| 1172 | ret = ustctl_set_filter(app->sock, ust_bytecode, |
| 1173 | ua_event->obj); |
| 1174 | if (ret < 0) { |
| 1175 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1176 | ERR("UST app event %s filter failed for app (pid: %d) " |
| 1177 | "with ret %d", ua_event->attr.name, app->pid, ret); |
| 1178 | } else { |
| 1179 | /* |
| 1180 | * This is normal behavior, an application can die during the |
| 1181 | * creation process. Don't report an error so the execution can |
| 1182 | * continue normally. |
| 1183 | */ |
| 1184 | ret = 0; |
| 1185 | DBG3("UST app filter event failed. Application is dead."); |
| 1186 | } |
| 1187 | goto error; |
| 1188 | } |
| 1189 | |
| 1190 | DBG2("UST filter set successfully for event %s", ua_event->name); |
| 1191 | |
| 1192 | error: |
| 1193 | health_code_update(); |
| 1194 | free(ust_bytecode); |
| 1195 | return ret; |
| 1196 | } |
| 1197 | |
| 1198 | static |
| 1199 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( |
| 1200 | struct lttng_event_exclusion *exclusion) |
| 1201 | { |
| 1202 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; |
| 1203 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + |
| 1204 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; |
| 1205 | |
| 1206 | ust_exclusion = zmalloc(exclusion_alloc_size); |
| 1207 | if (!ust_exclusion) { |
| 1208 | PERROR("malloc"); |
| 1209 | goto end; |
| 1210 | } |
| 1211 | |
| 1212 | assert(sizeof(struct lttng_event_exclusion) == |
| 1213 | sizeof(struct lttng_ust_event_exclusion)); |
| 1214 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); |
| 1215 | end: |
| 1216 | return ust_exclusion; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Set event exclusions on the tracer. |
| 1221 | */ |
| 1222 | static |
| 1223 | int set_ust_event_exclusion(struct ust_app_event *ua_event, |
| 1224 | struct ust_app *app) |
| 1225 | { |
| 1226 | int ret; |
| 1227 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; |
| 1228 | |
| 1229 | health_code_update(); |
| 1230 | |
| 1231 | if (!ua_event->exclusion || !ua_event->exclusion->count) { |
| 1232 | ret = 0; |
| 1233 | goto error; |
| 1234 | } |
| 1235 | |
| 1236 | ust_exclusion = create_ust_exclusion_from_exclusion( |
| 1237 | ua_event->exclusion); |
| 1238 | if (!ust_exclusion) { |
| 1239 | ret = -LTTNG_ERR_NOMEM; |
| 1240 | goto error; |
| 1241 | } |
| 1242 | ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj); |
| 1243 | if (ret < 0) { |
| 1244 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1245 | ERR("UST app event %s exclusions failed for app (pid: %d) " |
| 1246 | "with ret %d", ua_event->attr.name, app->pid, ret); |
| 1247 | } else { |
| 1248 | /* |
| 1249 | * This is normal behavior, an application can die during the |
| 1250 | * creation process. Don't report an error so the execution can |
| 1251 | * continue normally. |
| 1252 | */ |
| 1253 | ret = 0; |
| 1254 | DBG3("UST app event exclusion failed. Application is dead."); |
| 1255 | } |
| 1256 | goto error; |
| 1257 | } |
| 1258 | |
| 1259 | DBG2("UST exclusion set successfully for event %s", ua_event->name); |
| 1260 | |
| 1261 | error: |
| 1262 | health_code_update(); |
| 1263 | free(ust_exclusion); |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Disable the specified event on to UST tracer for the UST session. |
| 1269 | */ |
| 1270 | static int disable_ust_event(struct ust_app *app, |
| 1271 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) |
| 1272 | { |
| 1273 | int ret; |
| 1274 | |
| 1275 | health_code_update(); |
| 1276 | |
| 1277 | ret = ustctl_disable(app->sock, ua_event->obj); |
| 1278 | if (ret < 0) { |
| 1279 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1280 | ERR("UST app event %s disable failed for app (pid: %d) " |
| 1281 | "and session handle %d with ret %d", |
| 1282 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
| 1283 | } else { |
| 1284 | /* |
| 1285 | * This is normal behavior, an application can die during the |
| 1286 | * creation process. Don't report an error so the execution can |
| 1287 | * continue normally. |
| 1288 | */ |
| 1289 | ret = 0; |
| 1290 | DBG3("UST app disable event failed. Application is dead."); |
| 1291 | } |
| 1292 | goto error; |
| 1293 | } |
| 1294 | |
| 1295 | DBG2("UST app event %s disabled successfully for app (pid: %d)", |
| 1296 | ua_event->attr.name, app->pid); |
| 1297 | |
| 1298 | error: |
| 1299 | health_code_update(); |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * Disable the specified channel on to UST tracer for the UST session. |
| 1305 | */ |
| 1306 | static int disable_ust_channel(struct ust_app *app, |
| 1307 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 1308 | { |
| 1309 | int ret; |
| 1310 | |
| 1311 | health_code_update(); |
| 1312 | |
| 1313 | ret = ustctl_disable(app->sock, ua_chan->obj); |
| 1314 | if (ret < 0) { |
| 1315 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1316 | ERR("UST app channel %s disable failed for app (pid: %d) " |
| 1317 | "and session handle %d with ret %d", |
| 1318 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 1319 | } else { |
| 1320 | /* |
| 1321 | * This is normal behavior, an application can die during the |
| 1322 | * creation process. Don't report an error so the execution can |
| 1323 | * continue normally. |
| 1324 | */ |
| 1325 | ret = 0; |
| 1326 | DBG3("UST app disable channel failed. Application is dead."); |
| 1327 | } |
| 1328 | goto error; |
| 1329 | } |
| 1330 | |
| 1331 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
| 1332 | ua_chan->name, app->pid); |
| 1333 | |
| 1334 | error: |
| 1335 | health_code_update(); |
| 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * Enable the specified channel on to UST tracer for the UST session. |
| 1341 | */ |
| 1342 | static int enable_ust_channel(struct ust_app *app, |
| 1343 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 1344 | { |
| 1345 | int ret; |
| 1346 | |
| 1347 | health_code_update(); |
| 1348 | |
| 1349 | ret = ustctl_enable(app->sock, ua_chan->obj); |
| 1350 | if (ret < 0) { |
| 1351 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1352 | ERR("UST app channel %s enable failed for app (pid: %d) " |
| 1353 | "and session handle %d with ret %d", |
| 1354 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 1355 | } else { |
| 1356 | /* |
| 1357 | * This is normal behavior, an application can die during the |
| 1358 | * creation process. Don't report an error so the execution can |
| 1359 | * continue normally. |
| 1360 | */ |
| 1361 | ret = 0; |
| 1362 | DBG3("UST app enable channel failed. Application is dead."); |
| 1363 | } |
| 1364 | goto error; |
| 1365 | } |
| 1366 | |
| 1367 | ua_chan->enabled = 1; |
| 1368 | |
| 1369 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", |
| 1370 | ua_chan->name, app->pid); |
| 1371 | |
| 1372 | error: |
| 1373 | health_code_update(); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * Enable the specified event on to UST tracer for the UST session. |
| 1379 | */ |
| 1380 | static int enable_ust_event(struct ust_app *app, |
| 1381 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) |
| 1382 | { |
| 1383 | int ret; |
| 1384 | |
| 1385 | health_code_update(); |
| 1386 | |
| 1387 | ret = ustctl_enable(app->sock, ua_event->obj); |
| 1388 | if (ret < 0) { |
| 1389 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1390 | ERR("UST app event %s enable failed for app (pid: %d) " |
| 1391 | "and session handle %d with ret %d", |
| 1392 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
| 1393 | } else { |
| 1394 | /* |
| 1395 | * This is normal behavior, an application can die during the |
| 1396 | * creation process. Don't report an error so the execution can |
| 1397 | * continue normally. |
| 1398 | */ |
| 1399 | ret = 0; |
| 1400 | DBG3("UST app enable event failed. Application is dead."); |
| 1401 | } |
| 1402 | goto error; |
| 1403 | } |
| 1404 | |
| 1405 | DBG2("UST app event %s enabled successfully for app (pid: %d)", |
| 1406 | ua_event->attr.name, app->pid); |
| 1407 | |
| 1408 | error: |
| 1409 | health_code_update(); |
| 1410 | return ret; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * Send channel and stream buffer to application. |
| 1415 | * |
| 1416 | * Return 0 on success. On error, a negative value is returned. |
| 1417 | */ |
| 1418 | static int send_channel_pid_to_ust(struct ust_app *app, |
| 1419 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 1420 | { |
| 1421 | int ret; |
| 1422 | struct ust_app_stream *stream, *stmp; |
| 1423 | |
| 1424 | assert(app); |
| 1425 | assert(ua_sess); |
| 1426 | assert(ua_chan); |
| 1427 | |
| 1428 | health_code_update(); |
| 1429 | |
| 1430 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
| 1431 | app->sock); |
| 1432 | |
| 1433 | /* Send channel to the application. */ |
| 1434 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 1435 | if (ret < 0) { |
| 1436 | goto error; |
| 1437 | } |
| 1438 | |
| 1439 | health_code_update(); |
| 1440 | |
| 1441 | /* Send all streams to application. */ |
| 1442 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 1443 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); |
| 1444 | if (ret < 0) { |
| 1445 | goto error; |
| 1446 | } |
| 1447 | /* We don't need the stream anymore once sent to the tracer. */ |
| 1448 | cds_list_del(&stream->list); |
| 1449 | delete_ust_app_stream(-1, stream); |
| 1450 | } |
| 1451 | /* Flag the channel that it is sent to the application. */ |
| 1452 | ua_chan->is_sent = 1; |
| 1453 | |
| 1454 | error: |
| 1455 | health_code_update(); |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * Create the specified event onto the UST tracer for a UST session. |
| 1461 | * |
| 1462 | * Should be called with session mutex held. |
| 1463 | */ |
| 1464 | static |
| 1465 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, |
| 1466 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
| 1467 | { |
| 1468 | int ret = 0; |
| 1469 | |
| 1470 | health_code_update(); |
| 1471 | |
| 1472 | /* Create UST event on tracer */ |
| 1473 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
| 1474 | &ua_event->obj); |
| 1475 | if (ret < 0) { |
| 1476 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1477 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
| 1478 | ua_event->attr.name, app->pid, ret); |
| 1479 | } else { |
| 1480 | /* |
| 1481 | * This is normal behavior, an application can die during the |
| 1482 | * creation process. Don't report an error so the execution can |
| 1483 | * continue normally. |
| 1484 | */ |
| 1485 | ret = 0; |
| 1486 | DBG3("UST app create event failed. Application is dead."); |
| 1487 | } |
| 1488 | goto error; |
| 1489 | } |
| 1490 | |
| 1491 | ua_event->handle = ua_event->obj->handle; |
| 1492 | |
| 1493 | DBG2("UST app event %s created successfully for pid:%d", |
| 1494 | ua_event->attr.name, app->pid); |
| 1495 | |
| 1496 | health_code_update(); |
| 1497 | |
| 1498 | /* Set filter if one is present. */ |
| 1499 | if (ua_event->filter) { |
| 1500 | ret = set_ust_event_filter(ua_event, app); |
| 1501 | if (ret < 0) { |
| 1502 | goto error; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /* Set exclusions for the event */ |
| 1507 | if (ua_event->exclusion) { |
| 1508 | ret = set_ust_event_exclusion(ua_event, app); |
| 1509 | if (ret < 0) { |
| 1510 | goto error; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /* If event not enabled, disable it on the tracer */ |
| 1515 | if (ua_event->enabled) { |
| 1516 | /* |
| 1517 | * We now need to explicitly enable the event, since it |
| 1518 | * is now disabled at creation. |
| 1519 | */ |
| 1520 | ret = enable_ust_event(app, ua_sess, ua_event); |
| 1521 | if (ret < 0) { |
| 1522 | /* |
| 1523 | * If we hit an EPERM, something is wrong with our enable call. If |
| 1524 | * we get an EEXIST, there is a problem on the tracer side since we |
| 1525 | * just created it. |
| 1526 | */ |
| 1527 | switch (ret) { |
| 1528 | case -LTTNG_UST_ERR_PERM: |
| 1529 | /* Code flow problem */ |
| 1530 | assert(0); |
| 1531 | case -LTTNG_UST_ERR_EXIST: |
| 1532 | /* It's OK for our use case. */ |
| 1533 | ret = 0; |
| 1534 | break; |
| 1535 | default: |
| 1536 | break; |
| 1537 | } |
| 1538 | goto error; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | error: |
| 1543 | health_code_update(); |
| 1544 | return ret; |
| 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Copy data between an UST app event and a LTT event. |
| 1549 | */ |
| 1550 | static void shadow_copy_event(struct ust_app_event *ua_event, |
| 1551 | struct ltt_ust_event *uevent) |
| 1552 | { |
| 1553 | size_t exclusion_alloc_size; |
| 1554 | |
| 1555 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
| 1556 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 1557 | |
| 1558 | ua_event->enabled = uevent->enabled; |
| 1559 | |
| 1560 | /* Copy event attributes */ |
| 1561 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); |
| 1562 | |
| 1563 | /* Copy filter bytecode */ |
| 1564 | if (uevent->filter) { |
| 1565 | ua_event->filter = copy_filter_bytecode(uevent->filter); |
| 1566 | /* Filter might be NULL here in case of ENONEM. */ |
| 1567 | } |
| 1568 | |
| 1569 | /* Copy exclusion data */ |
| 1570 | if (uevent->exclusion) { |
| 1571 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
| 1572 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
| 1573 | ua_event->exclusion = zmalloc(exclusion_alloc_size); |
| 1574 | if (ua_event->exclusion == NULL) { |
| 1575 | PERROR("malloc"); |
| 1576 | } else { |
| 1577 | memcpy(ua_event->exclusion, uevent->exclusion, |
| 1578 | exclusion_alloc_size); |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * Copy data between an UST app channel and a LTT channel. |
| 1585 | */ |
| 1586 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
| 1587 | struct ltt_ust_channel *uchan) |
| 1588 | { |
| 1589 | struct lttng_ht_iter iter; |
| 1590 | struct ltt_ust_event *uevent; |
| 1591 | struct ltt_ust_context *uctx; |
| 1592 | struct ust_app_event *ua_event; |
| 1593 | struct ust_app_ctx *ua_ctx; |
| 1594 | |
| 1595 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
| 1596 | |
| 1597 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); |
| 1598 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 1599 | |
| 1600 | ua_chan->tracefile_size = uchan->tracefile_size; |
| 1601 | ua_chan->tracefile_count = uchan->tracefile_count; |
| 1602 | |
| 1603 | /* Copy event attributes since the layout is different. */ |
| 1604 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; |
| 1605 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; |
| 1606 | ua_chan->attr.overwrite = uchan->attr.overwrite; |
| 1607 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; |
| 1608 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; |
| 1609 | ua_chan->attr.output = uchan->attr.output; |
| 1610 | /* |
| 1611 | * Note that the attribute channel type is not set since the channel on the |
| 1612 | * tracing registry side does not have this information. |
| 1613 | */ |
| 1614 | |
| 1615 | ua_chan->enabled = uchan->enabled; |
| 1616 | ua_chan->tracing_channel_id = uchan->id; |
| 1617 | |
| 1618 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { |
| 1619 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
| 1620 | if (ua_ctx == NULL) { |
| 1621 | continue; |
| 1622 | } |
| 1623 | lttng_ht_node_init_ulong(&ua_ctx->node, |
| 1624 | (unsigned long) ua_ctx->ctx.ctx); |
| 1625 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
| 1626 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
| 1627 | } |
| 1628 | |
| 1629 | /* Copy all events from ltt ust channel to ust app channel */ |
| 1630 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
| 1631 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 1632 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
| 1633 | if (ua_event == NULL) { |
| 1634 | DBG2("UST event %s not found on shadow copy channel", |
| 1635 | uevent->attr.name); |
| 1636 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 1637 | if (ua_event == NULL) { |
| 1638 | continue; |
| 1639 | } |
| 1640 | shadow_copy_event(ua_event, uevent); |
| 1641 | add_unique_ust_app_event(ua_chan, ua_event); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Copy data between a UST app session and a regular LTT session. |
| 1650 | */ |
| 1651 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
| 1652 | struct ltt_ust_session *usess, struct ust_app *app) |
| 1653 | { |
| 1654 | struct lttng_ht_node_str *ua_chan_node; |
| 1655 | struct lttng_ht_iter iter; |
| 1656 | struct ltt_ust_channel *uchan; |
| 1657 | struct ust_app_channel *ua_chan; |
| 1658 | time_t rawtime; |
| 1659 | struct tm *timeinfo; |
| 1660 | char datetime[16]; |
| 1661 | int ret; |
| 1662 | char tmp_shm_path[PATH_MAX]; |
| 1663 | |
| 1664 | /* Get date and time for unique app path */ |
| 1665 | time(&rawtime); |
| 1666 | timeinfo = localtime(&rawtime); |
| 1667 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 1668 | |
| 1669 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
| 1670 | |
| 1671 | ua_sess->tracing_id = usess->id; |
| 1672 | ua_sess->id = get_next_session_id(); |
| 1673 | ua_sess->uid = app->uid; |
| 1674 | ua_sess->gid = app->gid; |
| 1675 | ua_sess->euid = usess->uid; |
| 1676 | ua_sess->egid = usess->gid; |
| 1677 | ua_sess->buffer_type = usess->buffer_type; |
| 1678 | ua_sess->bits_per_long = app->bits_per_long; |
| 1679 | /* There is only one consumer object per session possible. */ |
| 1680 | ua_sess->consumer = usess->consumer; |
| 1681 | ua_sess->output_traces = usess->output_traces; |
| 1682 | ua_sess->live_timer_interval = usess->live_timer_interval; |
| 1683 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
| 1684 | &usess->metadata_attr); |
| 1685 | |
| 1686 | switch (ua_sess->buffer_type) { |
| 1687 | case LTTNG_BUFFER_PER_PID: |
| 1688 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), |
| 1689 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
| 1690 | datetime); |
| 1691 | break; |
| 1692 | case LTTNG_BUFFER_PER_UID: |
| 1693 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), |
| 1694 | DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long); |
| 1695 | break; |
| 1696 | default: |
| 1697 | assert(0); |
| 1698 | goto error; |
| 1699 | } |
| 1700 | if (ret < 0) { |
| 1701 | PERROR("asprintf UST shadow copy session"); |
| 1702 | assert(0); |
| 1703 | goto error; |
| 1704 | } |
| 1705 | |
| 1706 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
| 1707 | sizeof(ua_sess->root_shm_path)); |
| 1708 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; |
| 1709 | strncpy(ua_sess->shm_path, usess->shm_path, |
| 1710 | sizeof(ua_sess->shm_path)); |
| 1711 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 1712 | if (ua_sess->shm_path[0]) { |
| 1713 | switch (ua_sess->buffer_type) { |
| 1714 | case LTTNG_BUFFER_PER_PID: |
| 1715 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), |
| 1716 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
| 1717 | app->name, app->pid, datetime); |
| 1718 | break; |
| 1719 | case LTTNG_BUFFER_PER_UID: |
| 1720 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), |
| 1721 | DEFAULT_UST_TRACE_UID_PATH, |
| 1722 | app->uid, app->bits_per_long); |
| 1723 | break; |
| 1724 | default: |
| 1725 | assert(0); |
| 1726 | goto error; |
| 1727 | } |
| 1728 | if (ret < 0) { |
| 1729 | PERROR("sprintf UST shadow copy session"); |
| 1730 | assert(0); |
| 1731 | goto error; |
| 1732 | } |
| 1733 | strncat(ua_sess->shm_path, tmp_shm_path, |
| 1734 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); |
| 1735 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 1736 | } |
| 1737 | |
| 1738 | /* Iterate over all channels in global domain. */ |
| 1739 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
| 1740 | uchan, node.node) { |
| 1741 | struct lttng_ht_iter uiter; |
| 1742 | |
| 1743 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 1744 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 1745 | if (ua_chan_node != NULL) { |
| 1746 | /* Session exist. Contiuing. */ |
| 1747 | continue; |
| 1748 | } |
| 1749 | |
| 1750 | DBG2("Channel %s not found on shadow session copy, creating it", |
| 1751 | uchan->name); |
| 1752 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 1753 | if (ua_chan == NULL) { |
| 1754 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
| 1755 | continue; |
| 1756 | } |
| 1757 | shadow_copy_channel(ua_chan, uchan); |
| 1758 | /* |
| 1759 | * The concept of metadata channel does not exist on the tracing |
| 1760 | * registry side of the session daemon so this can only be a per CPU |
| 1761 | * channel and not metadata. |
| 1762 | */ |
| 1763 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; |
| 1764 | |
| 1765 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 1766 | } |
| 1767 | |
| 1768 | error: |
| 1769 | return; |
| 1770 | } |
| 1771 | |
| 1772 | /* |
| 1773 | * Lookup sesison wrapper. |
| 1774 | */ |
| 1775 | static |
| 1776 | void __lookup_session_by_app(struct ltt_ust_session *usess, |
| 1777 | struct ust_app *app, struct lttng_ht_iter *iter) |
| 1778 | { |
| 1779 | /* Get right UST app session from app */ |
| 1780 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * Return ust app session from the app session hashtable using the UST session |
| 1785 | * id. |
| 1786 | */ |
| 1787 | static struct ust_app_session *lookup_session_by_app( |
| 1788 | struct ltt_ust_session *usess, struct ust_app *app) |
| 1789 | { |
| 1790 | struct lttng_ht_iter iter; |
| 1791 | struct lttng_ht_node_u64 *node; |
| 1792 | |
| 1793 | __lookup_session_by_app(usess, app, &iter); |
| 1794 | node = lttng_ht_iter_get_node_u64(&iter); |
| 1795 | if (node == NULL) { |
| 1796 | goto error; |
| 1797 | } |
| 1798 | |
| 1799 | return caa_container_of(node, struct ust_app_session, node); |
| 1800 | |
| 1801 | error: |
| 1802 | return NULL; |
| 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * Setup buffer registry per PID for the given session and application. If none |
| 1807 | * is found, a new one is created, added to the global registry and |
| 1808 | * initialized. If regp is valid, it's set with the newly created object. |
| 1809 | * |
| 1810 | * Return 0 on success or else a negative value. |
| 1811 | */ |
| 1812 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, |
| 1813 | struct ust_app *app, struct buffer_reg_pid **regp) |
| 1814 | { |
| 1815 | int ret = 0; |
| 1816 | struct buffer_reg_pid *reg_pid; |
| 1817 | |
| 1818 | assert(ua_sess); |
| 1819 | assert(app); |
| 1820 | |
| 1821 | rcu_read_lock(); |
| 1822 | |
| 1823 | reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 1824 | if (!reg_pid) { |
| 1825 | /* |
| 1826 | * This is the create channel path meaning that if there is NO |
| 1827 | * registry available, we have to create one for this session. |
| 1828 | */ |
| 1829 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
| 1830 | ua_sess->root_shm_path, ua_sess->shm_path); |
| 1831 | if (ret < 0) { |
| 1832 | goto error; |
| 1833 | } |
| 1834 | } else { |
| 1835 | goto end; |
| 1836 | } |
| 1837 | |
| 1838 | /* Initialize registry. */ |
| 1839 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, |
| 1840 | app->bits_per_long, app->uint8_t_alignment, |
| 1841 | app->uint16_t_alignment, app->uint32_t_alignment, |
| 1842 | app->uint64_t_alignment, app->long_alignment, |
| 1843 | app->byte_order, app->version.major, |
| 1844 | app->version.minor, reg_pid->root_shm_path, |
| 1845 | reg_pid->shm_path, |
| 1846 | ua_sess->euid, ua_sess->egid); |
| 1847 | if (ret < 0) { |
| 1848 | /* |
| 1849 | * reg_pid->registry->reg.ust is NULL upon error, so we need to |
| 1850 | * destroy the buffer registry, because it is always expected |
| 1851 | * that if the buffer registry can be found, its ust registry is |
| 1852 | * non-NULL. |
| 1853 | */ |
| 1854 | buffer_reg_pid_destroy(reg_pid); |
| 1855 | goto error; |
| 1856 | } |
| 1857 | |
| 1858 | buffer_reg_pid_add(reg_pid); |
| 1859 | |
| 1860 | DBG3("UST app buffer registry per PID created successfully"); |
| 1861 | |
| 1862 | end: |
| 1863 | if (regp) { |
| 1864 | *regp = reg_pid; |
| 1865 | } |
| 1866 | error: |
| 1867 | rcu_read_unlock(); |
| 1868 | return ret; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Setup buffer registry per UID for the given session and application. If none |
| 1873 | * is found, a new one is created, added to the global registry and |
| 1874 | * initialized. If regp is valid, it's set with the newly created object. |
| 1875 | * |
| 1876 | * Return 0 on success or else a negative value. |
| 1877 | */ |
| 1878 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, |
| 1879 | struct ust_app_session *ua_sess, |
| 1880 | struct ust_app *app, struct buffer_reg_uid **regp) |
| 1881 | { |
| 1882 | int ret = 0; |
| 1883 | struct buffer_reg_uid *reg_uid; |
| 1884 | |
| 1885 | assert(usess); |
| 1886 | assert(app); |
| 1887 | |
| 1888 | rcu_read_lock(); |
| 1889 | |
| 1890 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 1891 | if (!reg_uid) { |
| 1892 | /* |
| 1893 | * This is the create channel path meaning that if there is NO |
| 1894 | * registry available, we have to create one for this session. |
| 1895 | */ |
| 1896 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, |
| 1897 | LTTNG_DOMAIN_UST, ®_uid, |
| 1898 | ua_sess->root_shm_path, ua_sess->shm_path); |
| 1899 | if (ret < 0) { |
| 1900 | goto error; |
| 1901 | } |
| 1902 | } else { |
| 1903 | goto end; |
| 1904 | } |
| 1905 | |
| 1906 | /* Initialize registry. */ |
| 1907 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
| 1908 | app->bits_per_long, app->uint8_t_alignment, |
| 1909 | app->uint16_t_alignment, app->uint32_t_alignment, |
| 1910 | app->uint64_t_alignment, app->long_alignment, |
| 1911 | app->byte_order, app->version.major, |
| 1912 | app->version.minor, reg_uid->root_shm_path, |
| 1913 | reg_uid->shm_path, usess->uid, usess->gid); |
| 1914 | if (ret < 0) { |
| 1915 | /* |
| 1916 | * reg_uid->registry->reg.ust is NULL upon error, so we need to |
| 1917 | * destroy the buffer registry, because it is always expected |
| 1918 | * that if the buffer registry can be found, its ust registry is |
| 1919 | * non-NULL. |
| 1920 | */ |
| 1921 | buffer_reg_uid_destroy(reg_uid, NULL); |
| 1922 | goto error; |
| 1923 | } |
| 1924 | /* Add node to teardown list of the session. */ |
| 1925 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); |
| 1926 | |
| 1927 | buffer_reg_uid_add(reg_uid); |
| 1928 | |
| 1929 | DBG3("UST app buffer registry per UID created successfully"); |
| 1930 | end: |
| 1931 | if (regp) { |
| 1932 | *regp = reg_uid; |
| 1933 | } |
| 1934 | error: |
| 1935 | rcu_read_unlock(); |
| 1936 | return ret; |
| 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * Create a session on the tracer side for the given app. |
| 1941 | * |
| 1942 | * On success, ua_sess_ptr is populated with the session pointer or else left |
| 1943 | * untouched. If the session was created, is_created is set to 1. On error, |
| 1944 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can |
| 1945 | * be NULL. |
| 1946 | * |
| 1947 | * Returns 0 on success or else a negative code which is either -ENOMEM or |
| 1948 | * -ENOTCONN which is the default code if the ustctl_create_session fails. |
| 1949 | */ |
| 1950 | static int create_ust_app_session(struct ltt_ust_session *usess, |
| 1951 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
| 1952 | int *is_created) |
| 1953 | { |
| 1954 | int ret, created = 0; |
| 1955 | struct ust_app_session *ua_sess; |
| 1956 | |
| 1957 | assert(usess); |
| 1958 | assert(app); |
| 1959 | assert(ua_sess_ptr); |
| 1960 | |
| 1961 | health_code_update(); |
| 1962 | |
| 1963 | ua_sess = lookup_session_by_app(usess, app); |
| 1964 | if (ua_sess == NULL) { |
| 1965 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
| 1966 | app->pid, usess->id); |
| 1967 | ua_sess = alloc_ust_app_session(app); |
| 1968 | if (ua_sess == NULL) { |
| 1969 | /* Only malloc can failed so something is really wrong */ |
| 1970 | ret = -ENOMEM; |
| 1971 | goto error; |
| 1972 | } |
| 1973 | shadow_copy_session(ua_sess, usess, app); |
| 1974 | created = 1; |
| 1975 | } |
| 1976 | |
| 1977 | switch (usess->buffer_type) { |
| 1978 | case LTTNG_BUFFER_PER_PID: |
| 1979 | /* Init local registry. */ |
| 1980 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); |
| 1981 | if (ret < 0) { |
| 1982 | delete_ust_app_session(-1, ua_sess, app); |
| 1983 | goto error; |
| 1984 | } |
| 1985 | break; |
| 1986 | case LTTNG_BUFFER_PER_UID: |
| 1987 | /* Look for a global registry. If none exists, create one. */ |
| 1988 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
| 1989 | if (ret < 0) { |
| 1990 | delete_ust_app_session(-1, ua_sess, app); |
| 1991 | goto error; |
| 1992 | } |
| 1993 | break; |
| 1994 | default: |
| 1995 | assert(0); |
| 1996 | ret = -EINVAL; |
| 1997 | goto error; |
| 1998 | } |
| 1999 | |
| 2000 | health_code_update(); |
| 2001 | |
| 2002 | if (ua_sess->handle == -1) { |
| 2003 | ret = ustctl_create_session(app->sock); |
| 2004 | if (ret < 0) { |
| 2005 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2006 | ERR("Creating session for app pid %d with ret %d", |
| 2007 | app->pid, ret); |
| 2008 | } else { |
| 2009 | DBG("UST app creating session failed. Application is dead"); |
| 2010 | /* |
| 2011 | * This is normal behavior, an application can die during the |
| 2012 | * creation process. Don't report an error so the execution can |
| 2013 | * continue normally. This will get flagged ENOTCONN and the |
| 2014 | * caller will handle it. |
| 2015 | */ |
| 2016 | ret = 0; |
| 2017 | } |
| 2018 | delete_ust_app_session(-1, ua_sess, app); |
| 2019 | if (ret != -ENOMEM) { |
| 2020 | /* |
| 2021 | * Tracer is probably gone or got an internal error so let's |
| 2022 | * behave like it will soon unregister or not usable. |
| 2023 | */ |
| 2024 | ret = -ENOTCONN; |
| 2025 | } |
| 2026 | goto error; |
| 2027 | } |
| 2028 | |
| 2029 | ua_sess->handle = ret; |
| 2030 | |
| 2031 | /* Add ust app session to app's HT */ |
| 2032 | lttng_ht_node_init_u64(&ua_sess->node, |
| 2033 | ua_sess->tracing_id); |
| 2034 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); |
| 2035 | |
| 2036 | DBG2("UST app session created successfully with handle %d", ret); |
| 2037 | } |
| 2038 | |
| 2039 | *ua_sess_ptr = ua_sess; |
| 2040 | if (is_created) { |
| 2041 | *is_created = created; |
| 2042 | } |
| 2043 | |
| 2044 | /* Everything went well. */ |
| 2045 | ret = 0; |
| 2046 | |
| 2047 | error: |
| 2048 | health_code_update(); |
| 2049 | return ret; |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * Match function for a hash table lookup of ust_app_ctx. |
| 2054 | * |
| 2055 | * It matches an ust app context based on the context type and, in the case |
| 2056 | * of perf counters, their name. |
| 2057 | */ |
| 2058 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) |
| 2059 | { |
| 2060 | struct ust_app_ctx *ctx; |
| 2061 | const struct lttng_ust_context *key; |
| 2062 | |
| 2063 | assert(node); |
| 2064 | assert(_key); |
| 2065 | |
| 2066 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); |
| 2067 | key = _key; |
| 2068 | |
| 2069 | /* Context type */ |
| 2070 | if (ctx->ctx.ctx != key->ctx) { |
| 2071 | goto no_match; |
| 2072 | } |
| 2073 | |
| 2074 | /* Check the name in the case of perf thread counters. */ |
| 2075 | if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) { |
| 2076 | if (strncmp(key->u.perf_counter.name, |
| 2077 | ctx->ctx.u.perf_counter.name, |
| 2078 | sizeof(key->u.perf_counter.name))) { |
| 2079 | goto no_match; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* Match. */ |
| 2084 | return 1; |
| 2085 | |
| 2086 | no_match: |
| 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Lookup for an ust app context from an lttng_ust_context. |
| 2092 | * |
| 2093 | * Must be called while holding RCU read side lock. |
| 2094 | * Return an ust_app_ctx object or NULL on error. |
| 2095 | */ |
| 2096 | static |
| 2097 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, |
| 2098 | struct lttng_ust_context *uctx) |
| 2099 | { |
| 2100 | struct lttng_ht_iter iter; |
| 2101 | struct lttng_ht_node_ulong *node; |
| 2102 | struct ust_app_ctx *app_ctx = NULL; |
| 2103 | |
| 2104 | assert(uctx); |
| 2105 | assert(ht); |
| 2106 | |
| 2107 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ |
| 2108 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), |
| 2109 | ht_match_ust_app_ctx, uctx, &iter.iter); |
| 2110 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 2111 | if (!node) { |
| 2112 | goto end; |
| 2113 | } |
| 2114 | |
| 2115 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); |
| 2116 | |
| 2117 | end: |
| 2118 | return app_ctx; |
| 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * Create a context for the channel on the tracer. |
| 2123 | * |
| 2124 | * Called with UST app session lock held and a RCU read side lock. |
| 2125 | */ |
| 2126 | static |
| 2127 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, |
| 2128 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, |
| 2129 | struct ust_app *app) |
| 2130 | { |
| 2131 | int ret = 0; |
| 2132 | struct ust_app_ctx *ua_ctx; |
| 2133 | |
| 2134 | DBG2("UST app adding context to channel %s", ua_chan->name); |
| 2135 | |
| 2136 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
| 2137 | if (ua_ctx) { |
| 2138 | ret = -EEXIST; |
| 2139 | goto error; |
| 2140 | } |
| 2141 | |
| 2142 | ua_ctx = alloc_ust_app_ctx(uctx); |
| 2143 | if (ua_ctx == NULL) { |
| 2144 | /* malloc failed */ |
| 2145 | ret = -1; |
| 2146 | goto error; |
| 2147 | } |
| 2148 | |
| 2149 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
| 2150 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
| 2151 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
| 2152 | |
| 2153 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 2154 | if (ret < 0) { |
| 2155 | goto error; |
| 2156 | } |
| 2157 | |
| 2158 | error: |
| 2159 | return ret; |
| 2160 | } |
| 2161 | |
| 2162 | /* |
| 2163 | * Enable on the tracer side a ust app event for the session and channel. |
| 2164 | * |
| 2165 | * Called with UST app session lock held. |
| 2166 | */ |
| 2167 | static |
| 2168 | int enable_ust_app_event(struct ust_app_session *ua_sess, |
| 2169 | struct ust_app_event *ua_event, struct ust_app *app) |
| 2170 | { |
| 2171 | int ret; |
| 2172 | |
| 2173 | ret = enable_ust_event(app, ua_sess, ua_event); |
| 2174 | if (ret < 0) { |
| 2175 | goto error; |
| 2176 | } |
| 2177 | |
| 2178 | ua_event->enabled = 1; |
| 2179 | |
| 2180 | error: |
| 2181 | return ret; |
| 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | * Disable on the tracer side a ust app event for the session and channel. |
| 2186 | */ |
| 2187 | static int disable_ust_app_event(struct ust_app_session *ua_sess, |
| 2188 | struct ust_app_event *ua_event, struct ust_app *app) |
| 2189 | { |
| 2190 | int ret; |
| 2191 | |
| 2192 | ret = disable_ust_event(app, ua_sess, ua_event); |
| 2193 | if (ret < 0) { |
| 2194 | goto error; |
| 2195 | } |
| 2196 | |
| 2197 | ua_event->enabled = 0; |
| 2198 | |
| 2199 | error: |
| 2200 | return ret; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Lookup ust app channel for session and disable it on the tracer side. |
| 2205 | */ |
| 2206 | static |
| 2207 | int disable_ust_app_channel(struct ust_app_session *ua_sess, |
| 2208 | struct ust_app_channel *ua_chan, struct ust_app *app) |
| 2209 | { |
| 2210 | int ret; |
| 2211 | |
| 2212 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 2213 | if (ret < 0) { |
| 2214 | goto error; |
| 2215 | } |
| 2216 | |
| 2217 | ua_chan->enabled = 0; |
| 2218 | |
| 2219 | error: |
| 2220 | return ret; |
| 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * Lookup ust app channel for session and enable it on the tracer side. This |
| 2225 | * MUST be called with a RCU read side lock acquired. |
| 2226 | */ |
| 2227 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, |
| 2228 | struct ltt_ust_channel *uchan, struct ust_app *app) |
| 2229 | { |
| 2230 | int ret = 0; |
| 2231 | struct lttng_ht_iter iter; |
| 2232 | struct lttng_ht_node_str *ua_chan_node; |
| 2233 | struct ust_app_channel *ua_chan; |
| 2234 | |
| 2235 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 2236 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 2237 | if (ua_chan_node == NULL) { |
| 2238 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
| 2239 | uchan->name, ua_sess->tracing_id); |
| 2240 | goto error; |
| 2241 | } |
| 2242 | |
| 2243 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2244 | |
| 2245 | ret = enable_ust_channel(app, ua_sess, ua_chan); |
| 2246 | if (ret < 0) { |
| 2247 | goto error; |
| 2248 | } |
| 2249 | |
| 2250 | error: |
| 2251 | return ret; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Ask the consumer to create a channel and get it if successful. |
| 2256 | * |
| 2257 | * Return 0 on success or else a negative value. |
| 2258 | */ |
| 2259 | static int do_consumer_create_channel(struct ltt_ust_session *usess, |
| 2260 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, |
| 2261 | int bitness, struct ust_registry_session *registry) |
| 2262 | { |
| 2263 | int ret; |
| 2264 | unsigned int nb_fd = 0; |
| 2265 | struct consumer_socket *socket; |
| 2266 | |
| 2267 | assert(usess); |
| 2268 | assert(ua_sess); |
| 2269 | assert(ua_chan); |
| 2270 | assert(registry); |
| 2271 | |
| 2272 | rcu_read_lock(); |
| 2273 | health_code_update(); |
| 2274 | |
| 2275 | /* Get the right consumer socket for the application. */ |
| 2276 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); |
| 2277 | if (!socket) { |
| 2278 | ret = -EINVAL; |
| 2279 | goto error; |
| 2280 | } |
| 2281 | |
| 2282 | health_code_update(); |
| 2283 | |
| 2284 | /* Need one fd for the channel. */ |
| 2285 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 2286 | if (ret < 0) { |
| 2287 | ERR("Exhausted number of available FD upon create channel"); |
| 2288 | goto error; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Ask consumer to create channel. The consumer will return the number of |
| 2293 | * stream we have to expect. |
| 2294 | */ |
| 2295 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, |
| 2296 | registry); |
| 2297 | if (ret < 0) { |
| 2298 | goto error_ask; |
| 2299 | } |
| 2300 | |
| 2301 | /* |
| 2302 | * Compute the number of fd needed before receiving them. It must be 2 per |
| 2303 | * stream (2 being the default value here). |
| 2304 | */ |
| 2305 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; |
| 2306 | |
| 2307 | /* Reserve the amount of file descriptor we need. */ |
| 2308 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); |
| 2309 | if (ret < 0) { |
| 2310 | ERR("Exhausted number of available FD upon create channel"); |
| 2311 | goto error_fd_get_stream; |
| 2312 | } |
| 2313 | |
| 2314 | health_code_update(); |
| 2315 | |
| 2316 | /* |
| 2317 | * Now get the channel from the consumer. This call wil populate the stream |
| 2318 | * list of that channel and set the ust objects. |
| 2319 | */ |
| 2320 | if (usess->consumer->enabled) { |
| 2321 | ret = ust_consumer_get_channel(socket, ua_chan); |
| 2322 | if (ret < 0) { |
| 2323 | goto error_destroy; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | rcu_read_unlock(); |
| 2328 | return 0; |
| 2329 | |
| 2330 | error_destroy: |
| 2331 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); |
| 2332 | error_fd_get_stream: |
| 2333 | /* |
| 2334 | * Initiate a destroy channel on the consumer since we had an error |
| 2335 | * handling it on our side. The return value is of no importance since we |
| 2336 | * already have a ret value set by the previous error that we need to |
| 2337 | * return. |
| 2338 | */ |
| 2339 | (void) ust_consumer_destroy_channel(socket, ua_chan); |
| 2340 | error_ask: |
| 2341 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 2342 | error: |
| 2343 | health_code_update(); |
| 2344 | rcu_read_unlock(); |
| 2345 | return ret; |
| 2346 | } |
| 2347 | |
| 2348 | /* |
| 2349 | * Duplicate the ust data object of the ust app stream and save it in the |
| 2350 | * buffer registry stream. |
| 2351 | * |
| 2352 | * Return 0 on success or else a negative value. |
| 2353 | */ |
| 2354 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, |
| 2355 | struct ust_app_stream *stream) |
| 2356 | { |
| 2357 | int ret; |
| 2358 | |
| 2359 | assert(reg_stream); |
| 2360 | assert(stream); |
| 2361 | |
| 2362 | /* Reserve the amount of file descriptor we need. */ |
| 2363 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 2364 | if (ret < 0) { |
| 2365 | ERR("Exhausted number of available FD upon duplicate stream"); |
| 2366 | goto error; |
| 2367 | } |
| 2368 | |
| 2369 | /* Duplicate object for stream once the original is in the registry. */ |
| 2370 | ret = ustctl_duplicate_ust_object_data(&stream->obj, |
| 2371 | reg_stream->obj.ust); |
| 2372 | if (ret < 0) { |
| 2373 | ERR("Duplicate stream obj from %p to %p failed with ret %d", |
| 2374 | reg_stream->obj.ust, stream->obj, ret); |
| 2375 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 2376 | goto error; |
| 2377 | } |
| 2378 | stream->handle = stream->obj->handle; |
| 2379 | |
| 2380 | error: |
| 2381 | return ret; |
| 2382 | } |
| 2383 | |
| 2384 | /* |
| 2385 | * Duplicate the ust data object of the ust app. channel and save it in the |
| 2386 | * buffer registry channel. |
| 2387 | * |
| 2388 | * Return 0 on success or else a negative value. |
| 2389 | */ |
| 2390 | static int duplicate_channel_object(struct buffer_reg_channel *reg_chan, |
| 2391 | struct ust_app_channel *ua_chan) |
| 2392 | { |
| 2393 | int ret; |
| 2394 | |
| 2395 | assert(reg_chan); |
| 2396 | assert(ua_chan); |
| 2397 | |
| 2398 | /* Need two fds for the channel. */ |
| 2399 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 2400 | if (ret < 0) { |
| 2401 | ERR("Exhausted number of available FD upon duplicate channel"); |
| 2402 | goto error_fd_get; |
| 2403 | } |
| 2404 | |
| 2405 | /* Duplicate object for stream once the original is in the registry. */ |
| 2406 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust); |
| 2407 | if (ret < 0) { |
| 2408 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", |
| 2409 | reg_chan->obj.ust, ua_chan->obj, ret); |
| 2410 | goto error; |
| 2411 | } |
| 2412 | ua_chan->handle = ua_chan->obj->handle; |
| 2413 | |
| 2414 | return 0; |
| 2415 | |
| 2416 | error: |
| 2417 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 2418 | error_fd_get: |
| 2419 | return ret; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * For a given channel buffer registry, setup all streams of the given ust |
| 2424 | * application channel. |
| 2425 | * |
| 2426 | * Return 0 on success or else a negative value. |
| 2427 | */ |
| 2428 | static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan, |
| 2429 | struct ust_app_channel *ua_chan) |
| 2430 | { |
| 2431 | int ret = 0; |
| 2432 | struct ust_app_stream *stream, *stmp; |
| 2433 | |
| 2434 | assert(reg_chan); |
| 2435 | assert(ua_chan); |
| 2436 | |
| 2437 | DBG2("UST app setup buffer registry stream"); |
| 2438 | |
| 2439 | /* Send all streams to application. */ |
| 2440 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 2441 | struct buffer_reg_stream *reg_stream; |
| 2442 | |
| 2443 | ret = buffer_reg_stream_create(®_stream); |
| 2444 | if (ret < 0) { |
| 2445 | goto error; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Keep original pointer and nullify it in the stream so the delete |
| 2450 | * stream call does not release the object. |
| 2451 | */ |
| 2452 | reg_stream->obj.ust = stream->obj; |
| 2453 | stream->obj = NULL; |
| 2454 | buffer_reg_stream_add(reg_stream, reg_chan); |
| 2455 | |
| 2456 | /* We don't need the streams anymore. */ |
| 2457 | cds_list_del(&stream->list); |
| 2458 | delete_ust_app_stream(-1, stream); |
| 2459 | } |
| 2460 | |
| 2461 | error: |
| 2462 | return ret; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * Create a buffer registry channel for the given session registry and |
| 2467 | * application channel object. If regp pointer is valid, it's set with the |
| 2468 | * created object. Important, the created object is NOT added to the session |
| 2469 | * registry hash table. |
| 2470 | * |
| 2471 | * Return 0 on success else a negative value. |
| 2472 | */ |
| 2473 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 2474 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) |
| 2475 | { |
| 2476 | int ret; |
| 2477 | struct buffer_reg_channel *reg_chan = NULL; |
| 2478 | |
| 2479 | assert(reg_sess); |
| 2480 | assert(ua_chan); |
| 2481 | |
| 2482 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); |
| 2483 | |
| 2484 | /* Create buffer registry channel. */ |
| 2485 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, ®_chan); |
| 2486 | if (ret < 0) { |
| 2487 | goto error_create; |
| 2488 | } |
| 2489 | assert(reg_chan); |
| 2490 | reg_chan->consumer_key = ua_chan->key; |
| 2491 | reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
| 2492 | reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
| 2493 | |
| 2494 | /* Create and add a channel registry to session. */ |
| 2495 | ret = ust_registry_channel_add(reg_sess->reg.ust, |
| 2496 | ua_chan->tracing_channel_id); |
| 2497 | if (ret < 0) { |
| 2498 | goto error; |
| 2499 | } |
| 2500 | buffer_reg_channel_add(reg_sess, reg_chan); |
| 2501 | |
| 2502 | if (regp) { |
| 2503 | *regp = reg_chan; |
| 2504 | } |
| 2505 | |
| 2506 | return 0; |
| 2507 | |
| 2508 | error: |
| 2509 | /* Safe because the registry channel object was not added to any HT. */ |
| 2510 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); |
| 2511 | error_create: |
| 2512 | return ret; |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * Setup buffer registry channel for the given session registry and application |
| 2517 | * channel object. If regp pointer is valid, it's set with the created object. |
| 2518 | * |
| 2519 | * Return 0 on success else a negative value. |
| 2520 | */ |
| 2521 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 2522 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan) |
| 2523 | { |
| 2524 | int ret; |
| 2525 | |
| 2526 | assert(reg_sess); |
| 2527 | assert(reg_chan); |
| 2528 | assert(ua_chan); |
| 2529 | assert(ua_chan->obj); |
| 2530 | |
| 2531 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
| 2532 | |
| 2533 | /* Setup all streams for the registry. */ |
| 2534 | ret = setup_buffer_reg_streams(reg_chan, ua_chan); |
| 2535 | if (ret < 0) { |
| 2536 | goto error; |
| 2537 | } |
| 2538 | |
| 2539 | reg_chan->obj.ust = ua_chan->obj; |
| 2540 | ua_chan->obj = NULL; |
| 2541 | |
| 2542 | return 0; |
| 2543 | |
| 2544 | error: |
| 2545 | buffer_reg_channel_remove(reg_sess, reg_chan); |
| 2546 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); |
| 2547 | return ret; |
| 2548 | } |
| 2549 | |
| 2550 | /* |
| 2551 | * Send buffer registry channel to the application. |
| 2552 | * |
| 2553 | * Return 0 on success else a negative value. |
| 2554 | */ |
| 2555 | static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan, |
| 2556 | struct ust_app *app, struct ust_app_session *ua_sess, |
| 2557 | struct ust_app_channel *ua_chan) |
| 2558 | { |
| 2559 | int ret; |
| 2560 | struct buffer_reg_stream *reg_stream; |
| 2561 | |
| 2562 | assert(reg_chan); |
| 2563 | assert(app); |
| 2564 | assert(ua_sess); |
| 2565 | assert(ua_chan); |
| 2566 | |
| 2567 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); |
| 2568 | |
| 2569 | ret = duplicate_channel_object(reg_chan, ua_chan); |
| 2570 | if (ret < 0) { |
| 2571 | goto error; |
| 2572 | } |
| 2573 | |
| 2574 | /* Send channel to the application. */ |
| 2575 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 2576 | if (ret < 0) { |
| 2577 | goto error; |
| 2578 | } |
| 2579 | |
| 2580 | health_code_update(); |
| 2581 | |
| 2582 | /* Send all streams to application. */ |
| 2583 | pthread_mutex_lock(®_chan->stream_list_lock); |
| 2584 | cds_list_for_each_entry(reg_stream, ®_chan->streams, lnode) { |
| 2585 | struct ust_app_stream stream; |
| 2586 | |
| 2587 | ret = duplicate_stream_object(reg_stream, &stream); |
| 2588 | if (ret < 0) { |
| 2589 | goto error_stream_unlock; |
| 2590 | } |
| 2591 | |
| 2592 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); |
| 2593 | if (ret < 0) { |
| 2594 | (void) release_ust_app_stream(-1, &stream); |
| 2595 | goto error_stream_unlock; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * The return value is not important here. This function will output an |
| 2600 | * error if needed. |
| 2601 | */ |
| 2602 | (void) release_ust_app_stream(-1, &stream); |
| 2603 | } |
| 2604 | ua_chan->is_sent = 1; |
| 2605 | |
| 2606 | error_stream_unlock: |
| 2607 | pthread_mutex_unlock(®_chan->stream_list_lock); |
| 2608 | error: |
| 2609 | return ret; |
| 2610 | } |
| 2611 | |
| 2612 | /* |
| 2613 | * Create and send to the application the created buffers with per UID buffers. |
| 2614 | * |
| 2615 | * Return 0 on success else a negative value. |
| 2616 | */ |
| 2617 | static int create_channel_per_uid(struct ust_app *app, |
| 2618 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 2619 | struct ust_app_channel *ua_chan) |
| 2620 | { |
| 2621 | int ret; |
| 2622 | struct buffer_reg_uid *reg_uid; |
| 2623 | struct buffer_reg_channel *reg_chan; |
| 2624 | |
| 2625 | assert(app); |
| 2626 | assert(usess); |
| 2627 | assert(ua_sess); |
| 2628 | assert(ua_chan); |
| 2629 | |
| 2630 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); |
| 2631 | |
| 2632 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 2633 | /* |
| 2634 | * The session creation handles the creation of this global registry |
| 2635 | * object. If none can be find, there is a code flow problem or a |
| 2636 | * teardown race. |
| 2637 | */ |
| 2638 | assert(reg_uid); |
| 2639 | |
| 2640 | reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, |
| 2641 | reg_uid); |
| 2642 | if (!reg_chan) { |
| 2643 | /* Create the buffer registry channel object. */ |
| 2644 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, ®_chan); |
| 2645 | if (ret < 0) { |
| 2646 | ERR("Error creating the UST channel \"%s\" registry instance", |
| 2647 | ua_chan->name); |
| 2648 | goto error; |
| 2649 | } |
| 2650 | assert(reg_chan); |
| 2651 | |
| 2652 | /* |
| 2653 | * Create the buffers on the consumer side. This call populates the |
| 2654 | * ust app channel object with all streams and data object. |
| 2655 | */ |
| 2656 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, |
| 2657 | app->bits_per_long, reg_uid->registry->reg.ust); |
| 2658 | if (ret < 0) { |
| 2659 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
| 2660 | ua_chan->name); |
| 2661 | |
| 2662 | /* |
| 2663 | * Let's remove the previously created buffer registry channel so |
| 2664 | * it's not visible anymore in the session registry. |
| 2665 | */ |
| 2666 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, |
| 2667 | ua_chan->tracing_channel_id); |
| 2668 | buffer_reg_channel_remove(reg_uid->registry, reg_chan); |
| 2669 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); |
| 2670 | goto error; |
| 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * Setup the streams and add it to the session registry. |
| 2675 | */ |
| 2676 | ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan); |
| 2677 | if (ret < 0) { |
| 2678 | ERR("Error setting up UST channel \"%s\"", |
| 2679 | ua_chan->name); |
| 2680 | goto error; |
| 2681 | } |
| 2682 | |
| 2683 | } |
| 2684 | |
| 2685 | /* Send buffers to the application. */ |
| 2686 | ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan); |
| 2687 | if (ret < 0) { |
| 2688 | /* |
| 2689 | * Don't report error to the console, since it may be |
| 2690 | * caused by application concurrently exiting. |
| 2691 | */ |
| 2692 | goto error; |
| 2693 | } |
| 2694 | |
| 2695 | error: |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * Create and send to the application the created buffers with per PID buffers. |
| 2701 | * |
| 2702 | * Return 0 on success else a negative value. |
| 2703 | */ |
| 2704 | static int create_channel_per_pid(struct ust_app *app, |
| 2705 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 2706 | struct ust_app_channel *ua_chan) |
| 2707 | { |
| 2708 | int ret; |
| 2709 | struct ust_registry_session *registry; |
| 2710 | |
| 2711 | assert(app); |
| 2712 | assert(usess); |
| 2713 | assert(ua_sess); |
| 2714 | assert(ua_chan); |
| 2715 | |
| 2716 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); |
| 2717 | |
| 2718 | rcu_read_lock(); |
| 2719 | |
| 2720 | registry = get_session_registry(ua_sess); |
| 2721 | assert(registry); |
| 2722 | |
| 2723 | /* Create and add a new channel registry to session. */ |
| 2724 | ret = ust_registry_channel_add(registry, ua_chan->key); |
| 2725 | if (ret < 0) { |
| 2726 | ERR("Error creating the UST channel \"%s\" registry instance", |
| 2727 | ua_chan->name); |
| 2728 | goto error; |
| 2729 | } |
| 2730 | |
| 2731 | /* Create and get channel on the consumer side. */ |
| 2732 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, |
| 2733 | app->bits_per_long, registry); |
| 2734 | if (ret < 0) { |
| 2735 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
| 2736 | ua_chan->name); |
| 2737 | goto error; |
| 2738 | } |
| 2739 | |
| 2740 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); |
| 2741 | if (ret < 0) { |
| 2742 | /* |
| 2743 | * Don't report error to the console, since it may be |
| 2744 | * caused by application concurrently exiting. |
| 2745 | */ |
| 2746 | goto error; |
| 2747 | } |
| 2748 | |
| 2749 | error: |
| 2750 | rcu_read_unlock(); |
| 2751 | return ret; |
| 2752 | } |
| 2753 | |
| 2754 | /* |
| 2755 | * From an already allocated ust app channel, create the channel buffers if |
| 2756 | * need and send it to the application. This MUST be called with a RCU read |
| 2757 | * side lock acquired. |
| 2758 | * |
| 2759 | * Return 0 on success or else a negative value. |
| 2760 | */ |
| 2761 | static int do_create_channel(struct ust_app *app, |
| 2762 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 2763 | struct ust_app_channel *ua_chan) |
| 2764 | { |
| 2765 | int ret; |
| 2766 | |
| 2767 | assert(app); |
| 2768 | assert(usess); |
| 2769 | assert(ua_sess); |
| 2770 | assert(ua_chan); |
| 2771 | |
| 2772 | /* Handle buffer type before sending the channel to the application. */ |
| 2773 | switch (usess->buffer_type) { |
| 2774 | case LTTNG_BUFFER_PER_UID: |
| 2775 | { |
| 2776 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); |
| 2777 | if (ret < 0) { |
| 2778 | goto error; |
| 2779 | } |
| 2780 | break; |
| 2781 | } |
| 2782 | case LTTNG_BUFFER_PER_PID: |
| 2783 | { |
| 2784 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); |
| 2785 | if (ret < 0) { |
| 2786 | goto error; |
| 2787 | } |
| 2788 | break; |
| 2789 | } |
| 2790 | default: |
| 2791 | assert(0); |
| 2792 | ret = -EINVAL; |
| 2793 | goto error; |
| 2794 | } |
| 2795 | |
| 2796 | /* Initialize ust objd object using the received handle and add it. */ |
| 2797 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); |
| 2798 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); |
| 2799 | |
| 2800 | /* If channel is not enabled, disable it on the tracer */ |
| 2801 | if (!ua_chan->enabled) { |
| 2802 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 2803 | if (ret < 0) { |
| 2804 | goto error; |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | error: |
| 2809 | return ret; |
| 2810 | } |
| 2811 | |
| 2812 | /* |
| 2813 | * Create UST app channel and create it on the tracer. Set ua_chanp of the |
| 2814 | * newly created channel if not NULL. |
| 2815 | * |
| 2816 | * Called with UST app session lock and RCU read-side lock held. |
| 2817 | * |
| 2818 | * Return 0 on success or else a negative value. |
| 2819 | */ |
| 2820 | static int create_ust_app_channel(struct ust_app_session *ua_sess, |
| 2821 | struct ltt_ust_channel *uchan, struct ust_app *app, |
| 2822 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
| 2823 | struct ust_app_channel **ua_chanp) |
| 2824 | { |
| 2825 | int ret = 0; |
| 2826 | struct lttng_ht_iter iter; |
| 2827 | struct lttng_ht_node_str *ua_chan_node; |
| 2828 | struct ust_app_channel *ua_chan; |
| 2829 | |
| 2830 | /* Lookup channel in the ust app session */ |
| 2831 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 2832 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 2833 | if (ua_chan_node != NULL) { |
| 2834 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2835 | goto end; |
| 2836 | } |
| 2837 | |
| 2838 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 2839 | if (ua_chan == NULL) { |
| 2840 | /* Only malloc can fail here */ |
| 2841 | ret = -ENOMEM; |
| 2842 | goto error_alloc; |
| 2843 | } |
| 2844 | shadow_copy_channel(ua_chan, uchan); |
| 2845 | |
| 2846 | /* Set channel type. */ |
| 2847 | ua_chan->attr.type = type; |
| 2848 | |
| 2849 | ret = do_create_channel(app, usess, ua_sess, ua_chan); |
| 2850 | if (ret < 0) { |
| 2851 | goto error; |
| 2852 | } |
| 2853 | |
| 2854 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
| 2855 | app->pid); |
| 2856 | |
| 2857 | /* Only add the channel if successful on the tracer side. */ |
| 2858 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 2859 | |
| 2860 | end: |
| 2861 | if (ua_chanp) { |
| 2862 | *ua_chanp = ua_chan; |
| 2863 | } |
| 2864 | |
| 2865 | /* Everything went well. */ |
| 2866 | return 0; |
| 2867 | |
| 2868 | error: |
| 2869 | delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app); |
| 2870 | error_alloc: |
| 2871 | return ret; |
| 2872 | } |
| 2873 | |
| 2874 | /* |
| 2875 | * Create UST app event and create it on the tracer side. |
| 2876 | * |
| 2877 | * Called with ust app session mutex held. |
| 2878 | */ |
| 2879 | static |
| 2880 | int create_ust_app_event(struct ust_app_session *ua_sess, |
| 2881 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, |
| 2882 | struct ust_app *app) |
| 2883 | { |
| 2884 | int ret = 0; |
| 2885 | struct ust_app_event *ua_event; |
| 2886 | |
| 2887 | /* Get event node */ |
| 2888 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 2889 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
| 2890 | if (ua_event != NULL) { |
| 2891 | ret = -EEXIST; |
| 2892 | goto end; |
| 2893 | } |
| 2894 | |
| 2895 | /* Does not exist so create one */ |
| 2896 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 2897 | if (ua_event == NULL) { |
| 2898 | /* Only malloc can failed so something is really wrong */ |
| 2899 | ret = -ENOMEM; |
| 2900 | goto end; |
| 2901 | } |
| 2902 | shadow_copy_event(ua_event, uevent); |
| 2903 | |
| 2904 | /* Create it on the tracer side */ |
| 2905 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
| 2906 | if (ret < 0) { |
| 2907 | /* Not found previously means that it does not exist on the tracer */ |
| 2908 | assert(ret != -LTTNG_UST_ERR_EXIST); |
| 2909 | goto error; |
| 2910 | } |
| 2911 | |
| 2912 | add_unique_ust_app_event(ua_chan, ua_event); |
| 2913 | |
| 2914 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
| 2915 | app->pid); |
| 2916 | |
| 2917 | end: |
| 2918 | return ret; |
| 2919 | |
| 2920 | error: |
| 2921 | /* Valid. Calling here is already in a read side lock */ |
| 2922 | delete_ust_app_event(-1, ua_event); |
| 2923 | return ret; |
| 2924 | } |
| 2925 | |
| 2926 | /* |
| 2927 | * Create UST metadata and open it on the tracer side. |
| 2928 | * |
| 2929 | * Called with UST app session lock held and RCU read side lock. |
| 2930 | */ |
| 2931 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, |
| 2932 | struct ust_app *app, struct consumer_output *consumer) |
| 2933 | { |
| 2934 | int ret = 0; |
| 2935 | struct ust_app_channel *metadata; |
| 2936 | struct consumer_socket *socket; |
| 2937 | struct ust_registry_session *registry; |
| 2938 | |
| 2939 | assert(ua_sess); |
| 2940 | assert(app); |
| 2941 | assert(consumer); |
| 2942 | |
| 2943 | registry = get_session_registry(ua_sess); |
| 2944 | assert(registry); |
| 2945 | |
| 2946 | pthread_mutex_lock(®istry->lock); |
| 2947 | |
| 2948 | /* Metadata already exists for this registry or it was closed previously */ |
| 2949 | if (registry->metadata_key || registry->metadata_closed) { |
| 2950 | ret = 0; |
| 2951 | goto error; |
| 2952 | } |
| 2953 | |
| 2954 | /* Allocate UST metadata */ |
| 2955 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
| 2956 | if (!metadata) { |
| 2957 | /* malloc() failed */ |
| 2958 | ret = -ENOMEM; |
| 2959 | goto error; |
| 2960 | } |
| 2961 | |
| 2962 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
| 2963 | |
| 2964 | /* Need one fd for the channel. */ |
| 2965 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 2966 | if (ret < 0) { |
| 2967 | ERR("Exhausted number of available FD upon create metadata"); |
| 2968 | goto error; |
| 2969 | } |
| 2970 | |
| 2971 | /* Get the right consumer socket for the application. */ |
| 2972 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); |
| 2973 | if (!socket) { |
| 2974 | ret = -EINVAL; |
| 2975 | goto error_consumer; |
| 2976 | } |
| 2977 | |
| 2978 | /* |
| 2979 | * Keep metadata key so we can identify it on the consumer side. Assign it |
| 2980 | * to the registry *before* we ask the consumer so we avoid the race of the |
| 2981 | * consumer requesting the metadata and the ask_channel call on our side |
| 2982 | * did not returned yet. |
| 2983 | */ |
| 2984 | registry->metadata_key = metadata->key; |
| 2985 | |
| 2986 | /* |
| 2987 | * Ask the metadata channel creation to the consumer. The metadata object |
| 2988 | * will be created by the consumer and kept their. However, the stream is |
| 2989 | * never added or monitored until we do a first push metadata to the |
| 2990 | * consumer. |
| 2991 | */ |
| 2992 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
| 2993 | registry); |
| 2994 | if (ret < 0) { |
| 2995 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 2996 | registry->metadata_key = 0; |
| 2997 | goto error_consumer; |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | * The setup command will make the metadata stream be sent to the relayd, |
| 3002 | * if applicable, and the thread managing the metadatas. This is important |
| 3003 | * because after this point, if an error occurs, the only way the stream |
| 3004 | * can be deleted is to be monitored in the consumer. |
| 3005 | */ |
| 3006 | ret = consumer_setup_metadata(socket, metadata->key); |
| 3007 | if (ret < 0) { |
| 3008 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 3009 | registry->metadata_key = 0; |
| 3010 | goto error_consumer; |
| 3011 | } |
| 3012 | |
| 3013 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
| 3014 | metadata->key, app->pid); |
| 3015 | |
| 3016 | error_consumer: |
| 3017 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3018 | delete_ust_app_channel(-1, metadata, app); |
| 3019 | error: |
| 3020 | pthread_mutex_unlock(®istry->lock); |
| 3021 | return ret; |
| 3022 | } |
| 3023 | |
| 3024 | /* |
| 3025 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
| 3026 | * acquired before calling this function. |
| 3027 | */ |
| 3028 | struct ust_app *ust_app_find_by_pid(pid_t pid) |
| 3029 | { |
| 3030 | struct ust_app *app = NULL; |
| 3031 | struct lttng_ht_node_ulong *node; |
| 3032 | struct lttng_ht_iter iter; |
| 3033 | |
| 3034 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
| 3035 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 3036 | if (node == NULL) { |
| 3037 | DBG2("UST app no found with pid %d", pid); |
| 3038 | goto error; |
| 3039 | } |
| 3040 | |
| 3041 | DBG2("Found UST app by pid %d", pid); |
| 3042 | |
| 3043 | app = caa_container_of(node, struct ust_app, pid_n); |
| 3044 | |
| 3045 | error: |
| 3046 | return app; |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * Allocate and init an UST app object using the registration information and |
| 3051 | * the command socket. This is called when the command socket connects to the |
| 3052 | * session daemon. |
| 3053 | * |
| 3054 | * The object is returned on success or else NULL. |
| 3055 | */ |
| 3056 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
| 3057 | { |
| 3058 | struct ust_app *lta = NULL; |
| 3059 | |
| 3060 | assert(msg); |
| 3061 | assert(sock >= 0); |
| 3062 | |
| 3063 | DBG3("UST app creating application for socket %d", sock); |
| 3064 | |
| 3065 | if ((msg->bits_per_long == 64 && |
| 3066 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) |
| 3067 | || (msg->bits_per_long == 32 && |
| 3068 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { |
| 3069 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
| 3070 | "%d-bit long, but no consumerd for this size is available.\n", |
| 3071 | msg->name, msg->pid, msg->bits_per_long); |
| 3072 | goto error; |
| 3073 | } |
| 3074 | |
| 3075 | lta = zmalloc(sizeof(struct ust_app)); |
| 3076 | if (lta == NULL) { |
| 3077 | PERROR("malloc"); |
| 3078 | goto error; |
| 3079 | } |
| 3080 | |
| 3081 | lta->ppid = msg->ppid; |
| 3082 | lta->uid = msg->uid; |
| 3083 | lta->gid = msg->gid; |
| 3084 | |
| 3085 | lta->bits_per_long = msg->bits_per_long; |
| 3086 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
| 3087 | lta->uint16_t_alignment = msg->uint16_t_alignment; |
| 3088 | lta->uint32_t_alignment = msg->uint32_t_alignment; |
| 3089 | lta->uint64_t_alignment = msg->uint64_t_alignment; |
| 3090 | lta->long_alignment = msg->long_alignment; |
| 3091 | lta->byte_order = msg->byte_order; |
| 3092 | |
| 3093 | lta->v_major = msg->major; |
| 3094 | lta->v_minor = msg->minor; |
| 3095 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3096 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 3097 | lta->notify_sock = -1; |
| 3098 | |
| 3099 | /* Copy name and make sure it's NULL terminated. */ |
| 3100 | strncpy(lta->name, msg->name, sizeof(lta->name)); |
| 3101 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; |
| 3102 | |
| 3103 | /* |
| 3104 | * Before this can be called, when receiving the registration information, |
| 3105 | * the application compatibility is checked. So, at this point, the |
| 3106 | * application can work with this session daemon. |
| 3107 | */ |
| 3108 | lta->compatible = 1; |
| 3109 | |
| 3110 | lta->pid = msg->pid; |
| 3111 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
| 3112 | lta->sock = sock; |
| 3113 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
| 3114 | |
| 3115 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
| 3116 | |
| 3117 | error: |
| 3118 | return lta; |
| 3119 | } |
| 3120 | |
| 3121 | /* |
| 3122 | * For a given application object, add it to every hash table. |
| 3123 | */ |
| 3124 | void ust_app_add(struct ust_app *app) |
| 3125 | { |
| 3126 | assert(app); |
| 3127 | assert(app->notify_sock >= 0); |
| 3128 | |
| 3129 | rcu_read_lock(); |
| 3130 | |
| 3131 | /* |
| 3132 | * On a re-registration, we want to kick out the previous registration of |
| 3133 | * that pid |
| 3134 | */ |
| 3135 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
| 3136 | |
| 3137 | /* |
| 3138 | * The socket _should_ be unique until _we_ call close. So, a add_unique |
| 3139 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was |
| 3140 | * already in the table. |
| 3141 | */ |
| 3142 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
| 3143 | |
| 3144 | /* Add application to the notify socket hash table. */ |
| 3145 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); |
| 3146 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); |
| 3147 | |
| 3148 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
| 3149 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
| 3150 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, |
| 3151 | app->v_minor); |
| 3152 | |
| 3153 | rcu_read_unlock(); |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Set the application version into the object. |
| 3158 | * |
| 3159 | * Return 0 on success else a negative value either an errno code or a |
| 3160 | * LTTng-UST error code. |
| 3161 | */ |
| 3162 | int ust_app_version(struct ust_app *app) |
| 3163 | { |
| 3164 | int ret; |
| 3165 | |
| 3166 | assert(app); |
| 3167 | |
| 3168 | ret = ustctl_tracer_version(app->sock, &app->version); |
| 3169 | if (ret < 0) { |
| 3170 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 3171 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
| 3172 | } else { |
| 3173 | DBG3("UST app %d version failed. Application is dead", app->sock); |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | return ret; |
| 3178 | } |
| 3179 | |
| 3180 | /* |
| 3181 | * Unregister app by removing it from the global traceable app list and freeing |
| 3182 | * the data struct. |
| 3183 | * |
| 3184 | * The socket is already closed at this point so no close to sock. |
| 3185 | */ |
| 3186 | void ust_app_unregister(int sock) |
| 3187 | { |
| 3188 | struct ust_app *lta; |
| 3189 | struct lttng_ht_node_ulong *node; |
| 3190 | struct lttng_ht_iter ust_app_sock_iter; |
| 3191 | struct lttng_ht_iter iter; |
| 3192 | struct ust_app_session *ua_sess; |
| 3193 | int ret; |
| 3194 | |
| 3195 | rcu_read_lock(); |
| 3196 | |
| 3197 | /* Get the node reference for a call_rcu */ |
| 3198 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
| 3199 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); |
| 3200 | assert(node); |
| 3201 | |
| 3202 | lta = caa_container_of(node, struct ust_app, sock_n); |
| 3203 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
| 3204 | |
| 3205 | /* |
| 3206 | * For per-PID buffers, perform "push metadata" and flush all |
| 3207 | * application streams before removing app from hash tables, |
| 3208 | * ensuring proper behavior of data_pending check. |
| 3209 | * Remove sessions so they are not visible during deletion. |
| 3210 | */ |
| 3211 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
| 3212 | node.node) { |
| 3213 | struct ust_registry_session *registry; |
| 3214 | |
| 3215 | ret = lttng_ht_del(lta->sessions, &iter); |
| 3216 | if (ret) { |
| 3217 | /* The session was already removed so scheduled for teardown. */ |
| 3218 | continue; |
| 3219 | } |
| 3220 | |
| 3221 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 3222 | (void) ust_app_flush_app_session(lta, ua_sess); |
| 3223 | } |
| 3224 | |
| 3225 | /* |
| 3226 | * Add session to list for teardown. This is safe since at this point we |
| 3227 | * are the only one using this list. |
| 3228 | */ |
| 3229 | pthread_mutex_lock(&ua_sess->lock); |
| 3230 | |
| 3231 | /* |
| 3232 | * Normally, this is done in the delete session process which is |
| 3233 | * executed in the call rcu below. However, upon registration we can't |
| 3234 | * afford to wait for the grace period before pushing data or else the |
| 3235 | * data pending feature can race between the unregistration and stop |
| 3236 | * command where the data pending command is sent *before* the grace |
| 3237 | * period ended. |
| 3238 | * |
| 3239 | * The close metadata below nullifies the metadata pointer in the |
| 3240 | * session so the delete session will NOT push/close a second time. |
| 3241 | */ |
| 3242 | registry = get_session_registry(ua_sess); |
| 3243 | if (registry) { |
| 3244 | /* Push metadata for application before freeing the application. */ |
| 3245 | (void) push_metadata(registry, ua_sess->consumer); |
| 3246 | |
| 3247 | /* |
| 3248 | * Don't ask to close metadata for global per UID buffers. Close |
| 3249 | * metadata only on destroy trace session in this case. Also, the |
| 3250 | * previous push metadata could have flag the metadata registry to |
| 3251 | * close so don't send a close command if closed. |
| 3252 | */ |
| 3253 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 3254 | /* And ask to close it for this session registry. */ |
| 3255 | (void) close_metadata(registry, ua_sess->consumer); |
| 3256 | } |
| 3257 | } |
| 3258 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
| 3259 | |
| 3260 | pthread_mutex_unlock(&ua_sess->lock); |
| 3261 | } |
| 3262 | |
| 3263 | /* Remove application from PID hash table */ |
| 3264 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); |
| 3265 | assert(!ret); |
| 3266 | |
| 3267 | /* |
| 3268 | * Remove application from notify hash table. The thread handling the |
| 3269 | * notify socket could have deleted the node so ignore on error because |
| 3270 | * either way it's valid. The close of that socket is handled by the other |
| 3271 | * thread. |
| 3272 | */ |
| 3273 | iter.iter.node = <a->notify_sock_n.node; |
| 3274 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 3275 | |
| 3276 | /* |
| 3277 | * Ignore return value since the node might have been removed before by an |
| 3278 | * add replace during app registration because the PID can be reassigned by |
| 3279 | * the OS. |
| 3280 | */ |
| 3281 | iter.iter.node = <a->pid_n.node; |
| 3282 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 3283 | if (ret) { |
| 3284 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", |
| 3285 | lta->pid); |
| 3286 | } |
| 3287 | |
| 3288 | /* Free memory */ |
| 3289 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); |
| 3290 | |
| 3291 | rcu_read_unlock(); |
| 3292 | return; |
| 3293 | } |
| 3294 | |
| 3295 | /* |
| 3296 | * Fill events array with all events name of all registered apps. |
| 3297 | */ |
| 3298 | int ust_app_list_events(struct lttng_event **events) |
| 3299 | { |
| 3300 | int ret, handle; |
| 3301 | size_t nbmem, count = 0; |
| 3302 | struct lttng_ht_iter iter; |
| 3303 | struct ust_app *app; |
| 3304 | struct lttng_event *tmp_event; |
| 3305 | |
| 3306 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 3307 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
| 3308 | if (tmp_event == NULL) { |
| 3309 | PERROR("zmalloc ust app events"); |
| 3310 | ret = -ENOMEM; |
| 3311 | goto error; |
| 3312 | } |
| 3313 | |
| 3314 | rcu_read_lock(); |
| 3315 | |
| 3316 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3317 | struct lttng_ust_tracepoint_iter uiter; |
| 3318 | |
| 3319 | health_code_update(); |
| 3320 | |
| 3321 | if (!app->compatible) { |
| 3322 | /* |
| 3323 | * TODO: In time, we should notice the caller of this error by |
| 3324 | * telling him that this is a version error. |
| 3325 | */ |
| 3326 | continue; |
| 3327 | } |
| 3328 | handle = ustctl_tracepoint_list(app->sock); |
| 3329 | if (handle < 0) { |
| 3330 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 3331 | ERR("UST app list events getting handle failed for app pid %d", |
| 3332 | app->pid); |
| 3333 | } |
| 3334 | continue; |
| 3335 | } |
| 3336 | |
| 3337 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
| 3338 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 3339 | /* Handle ustctl error. */ |
| 3340 | if (ret < 0) { |
| 3341 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 3342 | ERR("UST app tp list get failed for app %d with ret %d", |
| 3343 | app->sock, ret); |
| 3344 | } else { |
| 3345 | DBG3("UST app tp list get failed. Application is dead"); |
| 3346 | /* |
| 3347 | * This is normal behavior, an application can die during the |
| 3348 | * creation process. Don't report an error so the execution can |
| 3349 | * continue normally. Continue normal execution. |
| 3350 | */ |
| 3351 | break; |
| 3352 | } |
| 3353 | free(tmp_event); |
| 3354 | goto rcu_error; |
| 3355 | } |
| 3356 | |
| 3357 | health_code_update(); |
| 3358 | if (count >= nbmem) { |
| 3359 | /* In case the realloc fails, we free the memory */ |
| 3360 | struct lttng_event *new_tmp_event; |
| 3361 | size_t new_nbmem; |
| 3362 | |
| 3363 | new_nbmem = nbmem << 1; |
| 3364 | DBG2("Reallocating event list from %zu to %zu entries", |
| 3365 | nbmem, new_nbmem); |
| 3366 | new_tmp_event = realloc(tmp_event, |
| 3367 | new_nbmem * sizeof(struct lttng_event)); |
| 3368 | if (new_tmp_event == NULL) { |
| 3369 | PERROR("realloc ust app events"); |
| 3370 | free(tmp_event); |
| 3371 | ret = -ENOMEM; |
| 3372 | goto rcu_error; |
| 3373 | } |
| 3374 | /* Zero the new memory */ |
| 3375 | memset(new_tmp_event + nbmem, 0, |
| 3376 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); |
| 3377 | nbmem = new_nbmem; |
| 3378 | tmp_event = new_tmp_event; |
| 3379 | } |
| 3380 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
| 3381 | tmp_event[count].loglevel = uiter.loglevel; |
| 3382 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; |
| 3383 | tmp_event[count].pid = app->pid; |
| 3384 | tmp_event[count].enabled = -1; |
| 3385 | count++; |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | ret = count; |
| 3390 | *events = tmp_event; |
| 3391 | |
| 3392 | DBG2("UST app list events done (%zu events)", count); |
| 3393 | |
| 3394 | rcu_error: |
| 3395 | rcu_read_unlock(); |
| 3396 | error: |
| 3397 | health_code_update(); |
| 3398 | return ret; |
| 3399 | } |
| 3400 | |
| 3401 | /* |
| 3402 | * Fill events array with all events name of all registered apps. |
| 3403 | */ |
| 3404 | int ust_app_list_event_fields(struct lttng_event_field **fields) |
| 3405 | { |
| 3406 | int ret, handle; |
| 3407 | size_t nbmem, count = 0; |
| 3408 | struct lttng_ht_iter iter; |
| 3409 | struct ust_app *app; |
| 3410 | struct lttng_event_field *tmp_event; |
| 3411 | |
| 3412 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 3413 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
| 3414 | if (tmp_event == NULL) { |
| 3415 | PERROR("zmalloc ust app event fields"); |
| 3416 | ret = -ENOMEM; |
| 3417 | goto error; |
| 3418 | } |
| 3419 | |
| 3420 | rcu_read_lock(); |
| 3421 | |
| 3422 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3423 | struct lttng_ust_field_iter uiter; |
| 3424 | |
| 3425 | health_code_update(); |
| 3426 | |
| 3427 | if (!app->compatible) { |
| 3428 | /* |
| 3429 | * TODO: In time, we should notice the caller of this error by |
| 3430 | * telling him that this is a version error. |
| 3431 | */ |
| 3432 | continue; |
| 3433 | } |
| 3434 | handle = ustctl_tracepoint_field_list(app->sock); |
| 3435 | if (handle < 0) { |
| 3436 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 3437 | ERR("UST app list field getting handle failed for app pid %d", |
| 3438 | app->pid); |
| 3439 | } |
| 3440 | continue; |
| 3441 | } |
| 3442 | |
| 3443 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, |
| 3444 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 3445 | /* Handle ustctl error. */ |
| 3446 | if (ret < 0) { |
| 3447 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 3448 | ERR("UST app tp list field failed for app %d with ret %d", |
| 3449 | app->sock, ret); |
| 3450 | } else { |
| 3451 | DBG3("UST app tp list field failed. Application is dead"); |
| 3452 | /* |
| 3453 | * This is normal behavior, an application can die during the |
| 3454 | * creation process. Don't report an error so the execution can |
| 3455 | * continue normally. Reset list and count for next app. |
| 3456 | */ |
| 3457 | break; |
| 3458 | } |
| 3459 | free(tmp_event); |
| 3460 | goto rcu_error; |
| 3461 | } |
| 3462 | |
| 3463 | health_code_update(); |
| 3464 | if (count >= nbmem) { |
| 3465 | /* In case the realloc fails, we free the memory */ |
| 3466 | struct lttng_event_field *new_tmp_event; |
| 3467 | size_t new_nbmem; |
| 3468 | |
| 3469 | new_nbmem = nbmem << 1; |
| 3470 | DBG2("Reallocating event field list from %zu to %zu entries", |
| 3471 | nbmem, new_nbmem); |
| 3472 | new_tmp_event = realloc(tmp_event, |
| 3473 | new_nbmem * sizeof(struct lttng_event_field)); |
| 3474 | if (new_tmp_event == NULL) { |
| 3475 | PERROR("realloc ust app event fields"); |
| 3476 | free(tmp_event); |
| 3477 | ret = -ENOMEM; |
| 3478 | goto rcu_error; |
| 3479 | } |
| 3480 | /* Zero the new memory */ |
| 3481 | memset(new_tmp_event + nbmem, 0, |
| 3482 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); |
| 3483 | nbmem = new_nbmem; |
| 3484 | tmp_event = new_tmp_event; |
| 3485 | } |
| 3486 | |
| 3487 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
| 3488 | /* Mapping between these enums matches 1 to 1. */ |
| 3489 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; |
| 3490 | tmp_event[count].nowrite = uiter.nowrite; |
| 3491 | |
| 3492 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 3493 | tmp_event[count].event.loglevel = uiter.loglevel; |
| 3494 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
| 3495 | tmp_event[count].event.pid = app->pid; |
| 3496 | tmp_event[count].event.enabled = -1; |
| 3497 | count++; |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | ret = count; |
| 3502 | *fields = tmp_event; |
| 3503 | |
| 3504 | DBG2("UST app list event fields done (%zu events)", count); |
| 3505 | |
| 3506 | rcu_error: |
| 3507 | rcu_read_unlock(); |
| 3508 | error: |
| 3509 | health_code_update(); |
| 3510 | return ret; |
| 3511 | } |
| 3512 | |
| 3513 | /* |
| 3514 | * Free and clean all traceable apps of the global list. |
| 3515 | * |
| 3516 | * Should _NOT_ be called with RCU read-side lock held. |
| 3517 | */ |
| 3518 | void ust_app_clean_list(void) |
| 3519 | { |
| 3520 | int ret; |
| 3521 | struct ust_app *app; |
| 3522 | struct lttng_ht_iter iter; |
| 3523 | |
| 3524 | DBG2("UST app cleaning registered apps hash table"); |
| 3525 | |
| 3526 | rcu_read_lock(); |
| 3527 | |
| 3528 | if (ust_app_ht) { |
| 3529 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3530 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 3531 | assert(!ret); |
| 3532 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | /* Cleanup socket hash table */ |
| 3537 | if (ust_app_ht_by_sock) { |
| 3538 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, |
| 3539 | sock_n.node) { |
| 3540 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
| 3541 | assert(!ret); |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | /* Cleanup notify socket hash table */ |
| 3546 | if (ust_app_ht_by_notify_sock) { |
| 3547 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, |
| 3548 | notify_sock_n.node) { |
| 3549 | ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 3550 | assert(!ret); |
| 3551 | } |
| 3552 | } |
| 3553 | rcu_read_unlock(); |
| 3554 | |
| 3555 | /* Destroy is done only when the ht is empty */ |
| 3556 | if (ust_app_ht) { |
| 3557 | ht_cleanup_push(ust_app_ht); |
| 3558 | } |
| 3559 | if (ust_app_ht_by_sock) { |
| 3560 | ht_cleanup_push(ust_app_ht_by_sock); |
| 3561 | } |
| 3562 | if (ust_app_ht_by_notify_sock) { |
| 3563 | ht_cleanup_push(ust_app_ht_by_notify_sock); |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | /* |
| 3568 | * Init UST app hash table. |
| 3569 | */ |
| 3570 | int ust_app_ht_alloc(void) |
| 3571 | { |
| 3572 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 3573 | if (!ust_app_ht) { |
| 3574 | return -1; |
| 3575 | } |
| 3576 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 3577 | if (!ust_app_ht_by_sock) { |
| 3578 | return -1; |
| 3579 | } |
| 3580 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 3581 | if (!ust_app_ht_by_notify_sock) { |
| 3582 | return -1; |
| 3583 | } |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | /* |
| 3588 | * For a specific UST session, disable the channel for all registered apps. |
| 3589 | */ |
| 3590 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
| 3591 | struct ltt_ust_channel *uchan) |
| 3592 | { |
| 3593 | int ret = 0; |
| 3594 | struct lttng_ht_iter iter; |
| 3595 | struct lttng_ht_node_str *ua_chan_node; |
| 3596 | struct ust_app *app; |
| 3597 | struct ust_app_session *ua_sess; |
| 3598 | struct ust_app_channel *ua_chan; |
| 3599 | |
| 3600 | if (usess == NULL || uchan == NULL) { |
| 3601 | ERR("Disabling UST global channel with NULL values"); |
| 3602 | ret = -1; |
| 3603 | goto error; |
| 3604 | } |
| 3605 | |
| 3606 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
| 3607 | uchan->name, usess->id); |
| 3608 | |
| 3609 | rcu_read_lock(); |
| 3610 | |
| 3611 | /* For every registered applications */ |
| 3612 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3613 | struct lttng_ht_iter uiter; |
| 3614 | if (!app->compatible) { |
| 3615 | /* |
| 3616 | * TODO: In time, we should notice the caller of this error by |
| 3617 | * telling him that this is a version error. |
| 3618 | */ |
| 3619 | continue; |
| 3620 | } |
| 3621 | ua_sess = lookup_session_by_app(usess, app); |
| 3622 | if (ua_sess == NULL) { |
| 3623 | continue; |
| 3624 | } |
| 3625 | |
| 3626 | /* Get channel */ |
| 3627 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 3628 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 3629 | /* If the session if found for the app, the channel must be there */ |
| 3630 | assert(ua_chan_node); |
| 3631 | |
| 3632 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3633 | /* The channel must not be already disabled */ |
| 3634 | assert(ua_chan->enabled == 1); |
| 3635 | |
| 3636 | /* Disable channel onto application */ |
| 3637 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); |
| 3638 | if (ret < 0) { |
| 3639 | /* XXX: We might want to report this error at some point... */ |
| 3640 | continue; |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | rcu_read_unlock(); |
| 3645 | |
| 3646 | error: |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
| 3650 | /* |
| 3651 | * For a specific UST session, enable the channel for all registered apps. |
| 3652 | */ |
| 3653 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
| 3654 | struct ltt_ust_channel *uchan) |
| 3655 | { |
| 3656 | int ret = 0; |
| 3657 | struct lttng_ht_iter iter; |
| 3658 | struct ust_app *app; |
| 3659 | struct ust_app_session *ua_sess; |
| 3660 | |
| 3661 | if (usess == NULL || uchan == NULL) { |
| 3662 | ERR("Adding UST global channel to NULL values"); |
| 3663 | ret = -1; |
| 3664 | goto error; |
| 3665 | } |
| 3666 | |
| 3667 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
| 3668 | uchan->name, usess->id); |
| 3669 | |
| 3670 | rcu_read_lock(); |
| 3671 | |
| 3672 | /* For every registered applications */ |
| 3673 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3674 | if (!app->compatible) { |
| 3675 | /* |
| 3676 | * TODO: In time, we should notice the caller of this error by |
| 3677 | * telling him that this is a version error. |
| 3678 | */ |
| 3679 | continue; |
| 3680 | } |
| 3681 | ua_sess = lookup_session_by_app(usess, app); |
| 3682 | if (ua_sess == NULL) { |
| 3683 | continue; |
| 3684 | } |
| 3685 | |
| 3686 | /* Enable channel onto application */ |
| 3687 | ret = enable_ust_app_channel(ua_sess, uchan, app); |
| 3688 | if (ret < 0) { |
| 3689 | /* XXX: We might want to report this error at some point... */ |
| 3690 | continue; |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | rcu_read_unlock(); |
| 3695 | |
| 3696 | error: |
| 3697 | return ret; |
| 3698 | } |
| 3699 | |
| 3700 | /* |
| 3701 | * Disable an event in a channel and for a specific session. |
| 3702 | */ |
| 3703 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
| 3704 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 3705 | { |
| 3706 | int ret = 0; |
| 3707 | struct lttng_ht_iter iter, uiter; |
| 3708 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; |
| 3709 | struct ust_app *app; |
| 3710 | struct ust_app_session *ua_sess; |
| 3711 | struct ust_app_channel *ua_chan; |
| 3712 | struct ust_app_event *ua_event; |
| 3713 | |
| 3714 | DBG("UST app disabling event %s for all apps in channel " |
| 3715 | "%s for session id %" PRIu64, |
| 3716 | uevent->attr.name, uchan->name, usess->id); |
| 3717 | |
| 3718 | rcu_read_lock(); |
| 3719 | |
| 3720 | /* For all registered applications */ |
| 3721 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3722 | if (!app->compatible) { |
| 3723 | /* |
| 3724 | * TODO: In time, we should notice the caller of this error by |
| 3725 | * telling him that this is a version error. |
| 3726 | */ |
| 3727 | continue; |
| 3728 | } |
| 3729 | ua_sess = lookup_session_by_app(usess, app); |
| 3730 | if (ua_sess == NULL) { |
| 3731 | /* Next app */ |
| 3732 | continue; |
| 3733 | } |
| 3734 | |
| 3735 | /* Lookup channel in the ust app session */ |
| 3736 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 3737 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 3738 | if (ua_chan_node == NULL) { |
| 3739 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
| 3740 | "Skipping", uchan->name, usess->id, app->pid); |
| 3741 | continue; |
| 3742 | } |
| 3743 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3744 | |
| 3745 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); |
| 3746 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); |
| 3747 | if (ua_event_node == NULL) { |
| 3748 | DBG2("Event %s not found in channel %s for app pid %d." |
| 3749 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
| 3750 | continue; |
| 3751 | } |
| 3752 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); |
| 3753 | |
| 3754 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 3755 | if (ret < 0) { |
| 3756 | /* XXX: Report error someday... */ |
| 3757 | continue; |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | rcu_read_unlock(); |
| 3762 | |
| 3763 | return ret; |
| 3764 | } |
| 3765 | |
| 3766 | /* |
| 3767 | * For a specific UST session, create the channel for all registered apps. |
| 3768 | */ |
| 3769 | int ust_app_create_channel_glb(struct ltt_ust_session *usess, |
| 3770 | struct ltt_ust_channel *uchan) |
| 3771 | { |
| 3772 | int ret = 0, created; |
| 3773 | struct lttng_ht_iter iter; |
| 3774 | struct ust_app *app; |
| 3775 | struct ust_app_session *ua_sess = NULL; |
| 3776 | |
| 3777 | /* Very wrong code flow */ |
| 3778 | assert(usess); |
| 3779 | assert(uchan); |
| 3780 | |
| 3781 | DBG2("UST app adding channel %s to UST domain for session id %" PRIu64, |
| 3782 | uchan->name, usess->id); |
| 3783 | |
| 3784 | rcu_read_lock(); |
| 3785 | |
| 3786 | /* For every registered applications */ |
| 3787 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3788 | if (!app->compatible) { |
| 3789 | /* |
| 3790 | * TODO: In time, we should notice the caller of this error by |
| 3791 | * telling him that this is a version error. |
| 3792 | */ |
| 3793 | continue; |
| 3794 | } |
| 3795 | if (!trace_ust_pid_tracker_lookup(usess, app->pid)) { |
| 3796 | /* Skip. */ |
| 3797 | continue; |
| 3798 | } |
| 3799 | |
| 3800 | /* |
| 3801 | * Create session on the tracer side and add it to app session HT. Note |
| 3802 | * that if session exist, it will simply return a pointer to the ust |
| 3803 | * app session. |
| 3804 | */ |
| 3805 | ret = create_ust_app_session(usess, app, &ua_sess, &created); |
| 3806 | if (ret < 0) { |
| 3807 | switch (ret) { |
| 3808 | case -ENOTCONN: |
| 3809 | /* |
| 3810 | * The application's socket is not valid. Either a bad socket |
| 3811 | * or a timeout on it. We can't inform the caller that for a |
| 3812 | * specific app, the session failed so lets continue here. |
| 3813 | */ |
| 3814 | continue; |
| 3815 | case -ENOMEM: |
| 3816 | default: |
| 3817 | goto error_rcu_unlock; |
| 3818 | } |
| 3819 | } |
| 3820 | assert(ua_sess); |
| 3821 | |
| 3822 | pthread_mutex_lock(&ua_sess->lock); |
| 3823 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, |
| 3824 | sizeof(uchan->name))) { |
| 3825 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr); |
| 3826 | ret = 0; |
| 3827 | } else { |
| 3828 | /* Create channel onto application. We don't need the chan ref. */ |
| 3829 | ret = create_ust_app_channel(ua_sess, uchan, app, |
| 3830 | LTTNG_UST_CHAN_PER_CPU, usess, NULL); |
| 3831 | } |
| 3832 | pthread_mutex_unlock(&ua_sess->lock); |
| 3833 | if (ret < 0) { |
| 3834 | if (ret == -ENOMEM) { |
| 3835 | /* No more memory is a fatal error. Stop right now. */ |
| 3836 | goto error_rcu_unlock; |
| 3837 | } |
| 3838 | /* Cleanup the created session if it's the case. */ |
| 3839 | if (created) { |
| 3840 | destroy_app_session(app, ua_sess); |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | error_rcu_unlock: |
| 3846 | rcu_read_unlock(); |
| 3847 | return ret; |
| 3848 | } |
| 3849 | |
| 3850 | /* |
| 3851 | * Enable event for a specific session and channel on the tracer. |
| 3852 | */ |
| 3853 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
| 3854 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 3855 | { |
| 3856 | int ret = 0; |
| 3857 | struct lttng_ht_iter iter, uiter; |
| 3858 | struct lttng_ht_node_str *ua_chan_node; |
| 3859 | struct ust_app *app; |
| 3860 | struct ust_app_session *ua_sess; |
| 3861 | struct ust_app_channel *ua_chan; |
| 3862 | struct ust_app_event *ua_event; |
| 3863 | |
| 3864 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
| 3865 | uevent->attr.name, usess->id); |
| 3866 | |
| 3867 | /* |
| 3868 | * NOTE: At this point, this function is called only if the session and |
| 3869 | * channel passed are already created for all apps. and enabled on the |
| 3870 | * tracer also. |
| 3871 | */ |
| 3872 | |
| 3873 | rcu_read_lock(); |
| 3874 | |
| 3875 | /* For all registered applications */ |
| 3876 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3877 | if (!app->compatible) { |
| 3878 | /* |
| 3879 | * TODO: In time, we should notice the caller of this error by |
| 3880 | * telling him that this is a version error. |
| 3881 | */ |
| 3882 | continue; |
| 3883 | } |
| 3884 | ua_sess = lookup_session_by_app(usess, app); |
| 3885 | if (!ua_sess) { |
| 3886 | /* The application has problem or is probably dead. */ |
| 3887 | continue; |
| 3888 | } |
| 3889 | |
| 3890 | pthread_mutex_lock(&ua_sess->lock); |
| 3891 | |
| 3892 | /* Lookup channel in the ust app session */ |
| 3893 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 3894 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 3895 | /* If the channel is not found, there is a code flow error */ |
| 3896 | assert(ua_chan_node); |
| 3897 | |
| 3898 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3899 | |
| 3900 | /* Get event node */ |
| 3901 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 3902 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
| 3903 | if (ua_event == NULL) { |
| 3904 | DBG3("UST app enable event %s not found for app PID %d." |
| 3905 | "Skipping app", uevent->attr.name, app->pid); |
| 3906 | goto next_app; |
| 3907 | } |
| 3908 | |
| 3909 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 3910 | if (ret < 0) { |
| 3911 | pthread_mutex_unlock(&ua_sess->lock); |
| 3912 | goto error; |
| 3913 | } |
| 3914 | next_app: |
| 3915 | pthread_mutex_unlock(&ua_sess->lock); |
| 3916 | } |
| 3917 | |
| 3918 | error: |
| 3919 | rcu_read_unlock(); |
| 3920 | return ret; |
| 3921 | } |
| 3922 | |
| 3923 | /* |
| 3924 | * For a specific existing UST session and UST channel, creates the event for |
| 3925 | * all registered apps. |
| 3926 | */ |
| 3927 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
| 3928 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 3929 | { |
| 3930 | int ret = 0; |
| 3931 | struct lttng_ht_iter iter, uiter; |
| 3932 | struct lttng_ht_node_str *ua_chan_node; |
| 3933 | struct ust_app *app; |
| 3934 | struct ust_app_session *ua_sess; |
| 3935 | struct ust_app_channel *ua_chan; |
| 3936 | |
| 3937 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
| 3938 | uevent->attr.name, usess->id); |
| 3939 | |
| 3940 | rcu_read_lock(); |
| 3941 | |
| 3942 | /* For all registered applications */ |
| 3943 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3944 | if (!app->compatible) { |
| 3945 | /* |
| 3946 | * TODO: In time, we should notice the caller of this error by |
| 3947 | * telling him that this is a version error. |
| 3948 | */ |
| 3949 | continue; |
| 3950 | } |
| 3951 | ua_sess = lookup_session_by_app(usess, app); |
| 3952 | if (!ua_sess) { |
| 3953 | /* The application has problem or is probably dead. */ |
| 3954 | continue; |
| 3955 | } |
| 3956 | |
| 3957 | pthread_mutex_lock(&ua_sess->lock); |
| 3958 | /* Lookup channel in the ust app session */ |
| 3959 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 3960 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 3961 | /* If the channel is not found, there is a code flow error */ |
| 3962 | assert(ua_chan_node); |
| 3963 | |
| 3964 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3965 | |
| 3966 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
| 3967 | pthread_mutex_unlock(&ua_sess->lock); |
| 3968 | if (ret < 0) { |
| 3969 | if (ret != -LTTNG_UST_ERR_EXIST) { |
| 3970 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
| 3971 | break; |
| 3972 | } |
| 3973 | DBG2("UST app event %s already exist on app PID %d", |
| 3974 | uevent->attr.name, app->pid); |
| 3975 | continue; |
| 3976 | } |
| 3977 | } |
| 3978 | |
| 3979 | rcu_read_unlock(); |
| 3980 | |
| 3981 | return ret; |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Start tracing for a specific UST session and app. |
| 3986 | */ |
| 3987 | static |
| 3988 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 3989 | { |
| 3990 | int ret = 0; |
| 3991 | struct ust_app_session *ua_sess; |
| 3992 | |
| 3993 | DBG("Starting tracing for ust app pid %d", app->pid); |
| 3994 | |
| 3995 | rcu_read_lock(); |
| 3996 | |
| 3997 | if (!app->compatible) { |
| 3998 | goto end; |
| 3999 | } |
| 4000 | |
| 4001 | ua_sess = lookup_session_by_app(usess, app); |
| 4002 | if (ua_sess == NULL) { |
| 4003 | /* The session is in teardown process. Ignore and continue. */ |
| 4004 | goto end; |
| 4005 | } |
| 4006 | |
| 4007 | pthread_mutex_lock(&ua_sess->lock); |
| 4008 | |
| 4009 | /* Upon restart, we skip the setup, already done */ |
| 4010 | if (ua_sess->started) { |
| 4011 | goto skip_setup; |
| 4012 | } |
| 4013 | |
| 4014 | /* Create directories if consumer is LOCAL and has a path defined. */ |
| 4015 | if (usess->consumer->type == CONSUMER_DST_LOCAL && |
| 4016 | strlen(usess->consumer->dst.trace_path) > 0) { |
| 4017 | ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, |
| 4018 | S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid); |
| 4019 | if (ret < 0) { |
| 4020 | if (ret != -EEXIST) { |
| 4021 | ERR("Trace directory creation error"); |
| 4022 | goto error_unlock; |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | /* |
| 4028 | * Create the metadata for the application. This returns gracefully if a |
| 4029 | * metadata was already set for the session. |
| 4030 | */ |
| 4031 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); |
| 4032 | if (ret < 0) { |
| 4033 | goto error_unlock; |
| 4034 | } |
| 4035 | |
| 4036 | health_code_update(); |
| 4037 | |
| 4038 | skip_setup: |
| 4039 | /* This start the UST tracing */ |
| 4040 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
| 4041 | if (ret < 0) { |
| 4042 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4043 | ERR("Error starting tracing for app pid: %d (ret: %d)", |
| 4044 | app->pid, ret); |
| 4045 | } else { |
| 4046 | DBG("UST app start session failed. Application is dead."); |
| 4047 | /* |
| 4048 | * This is normal behavior, an application can die during the |
| 4049 | * creation process. Don't report an error so the execution can |
| 4050 | * continue normally. |
| 4051 | */ |
| 4052 | pthread_mutex_unlock(&ua_sess->lock); |
| 4053 | goto end; |
| 4054 | } |
| 4055 | goto error_unlock; |
| 4056 | } |
| 4057 | |
| 4058 | /* Indicate that the session has been started once */ |
| 4059 | ua_sess->started = 1; |
| 4060 | |
| 4061 | pthread_mutex_unlock(&ua_sess->lock); |
| 4062 | |
| 4063 | health_code_update(); |
| 4064 | |
| 4065 | /* Quiescent wait after starting trace */ |
| 4066 | ret = ustctl_wait_quiescent(app->sock); |
| 4067 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4068 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 4069 | app->pid, ret); |
| 4070 | } |
| 4071 | |
| 4072 | end: |
| 4073 | rcu_read_unlock(); |
| 4074 | health_code_update(); |
| 4075 | return 0; |
| 4076 | |
| 4077 | error_unlock: |
| 4078 | pthread_mutex_unlock(&ua_sess->lock); |
| 4079 | rcu_read_unlock(); |
| 4080 | health_code_update(); |
| 4081 | return -1; |
| 4082 | } |
| 4083 | |
| 4084 | /* |
| 4085 | * Stop tracing for a specific UST session and app. |
| 4086 | */ |
| 4087 | static |
| 4088 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 4089 | { |
| 4090 | int ret = 0; |
| 4091 | struct ust_app_session *ua_sess; |
| 4092 | struct ust_registry_session *registry; |
| 4093 | |
| 4094 | DBG("Stopping tracing for ust app pid %d", app->pid); |
| 4095 | |
| 4096 | rcu_read_lock(); |
| 4097 | |
| 4098 | if (!app->compatible) { |
| 4099 | goto end_no_session; |
| 4100 | } |
| 4101 | |
| 4102 | ua_sess = lookup_session_by_app(usess, app); |
| 4103 | if (ua_sess == NULL) { |
| 4104 | goto end_no_session; |
| 4105 | } |
| 4106 | |
| 4107 | pthread_mutex_lock(&ua_sess->lock); |
| 4108 | |
| 4109 | /* |
| 4110 | * If started = 0, it means that stop trace has been called for a session |
| 4111 | * that was never started. It's possible since we can have a fail start |
| 4112 | * from either the application manager thread or the command thread. Simply |
| 4113 | * indicate that this is a stop error. |
| 4114 | */ |
| 4115 | if (!ua_sess->started) { |
| 4116 | goto error_rcu_unlock; |
| 4117 | } |
| 4118 | |
| 4119 | health_code_update(); |
| 4120 | |
| 4121 | /* This inhibits UST tracing */ |
| 4122 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
| 4123 | if (ret < 0) { |
| 4124 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4125 | ERR("Error stopping tracing for app pid: %d (ret: %d)", |
| 4126 | app->pid, ret); |
| 4127 | } else { |
| 4128 | DBG("UST app stop session failed. Application is dead."); |
| 4129 | /* |
| 4130 | * This is normal behavior, an application can die during the |
| 4131 | * creation process. Don't report an error so the execution can |
| 4132 | * continue normally. |
| 4133 | */ |
| 4134 | goto end_unlock; |
| 4135 | } |
| 4136 | goto error_rcu_unlock; |
| 4137 | } |
| 4138 | |
| 4139 | health_code_update(); |
| 4140 | |
| 4141 | /* Quiescent wait after stopping trace */ |
| 4142 | ret = ustctl_wait_quiescent(app->sock); |
| 4143 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4144 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 4145 | app->pid, ret); |
| 4146 | } |
| 4147 | |
| 4148 | health_code_update(); |
| 4149 | |
| 4150 | registry = get_session_registry(ua_sess); |
| 4151 | assert(registry); |
| 4152 | |
| 4153 | /* Push metadata for application before freeing the application. */ |
| 4154 | (void) push_metadata(registry, ua_sess->consumer); |
| 4155 | |
| 4156 | end_unlock: |
| 4157 | pthread_mutex_unlock(&ua_sess->lock); |
| 4158 | end_no_session: |
| 4159 | rcu_read_unlock(); |
| 4160 | health_code_update(); |
| 4161 | return 0; |
| 4162 | |
| 4163 | error_rcu_unlock: |
| 4164 | pthread_mutex_unlock(&ua_sess->lock); |
| 4165 | rcu_read_unlock(); |
| 4166 | health_code_update(); |
| 4167 | return -1; |
| 4168 | } |
| 4169 | |
| 4170 | static |
| 4171 | int ust_app_flush_app_session(struct ust_app *app, |
| 4172 | struct ust_app_session *ua_sess) |
| 4173 | { |
| 4174 | int ret, retval = 0; |
| 4175 | struct lttng_ht_iter iter; |
| 4176 | struct ust_app_channel *ua_chan; |
| 4177 | struct consumer_socket *socket; |
| 4178 | |
| 4179 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
| 4180 | |
| 4181 | rcu_read_lock(); |
| 4182 | |
| 4183 | if (!app->compatible) { |
| 4184 | goto end_not_compatible; |
| 4185 | } |
| 4186 | |
| 4187 | pthread_mutex_lock(&ua_sess->lock); |
| 4188 | |
| 4189 | health_code_update(); |
| 4190 | |
| 4191 | /* Flushing buffers */ |
| 4192 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 4193 | ua_sess->consumer); |
| 4194 | |
| 4195 | /* Flush buffers and push metadata. */ |
| 4196 | switch (ua_sess->buffer_type) { |
| 4197 | case LTTNG_BUFFER_PER_PID: |
| 4198 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 4199 | node.node) { |
| 4200 | health_code_update(); |
| 4201 | assert(ua_chan->is_sent); |
| 4202 | ret = consumer_flush_channel(socket, ua_chan->key); |
| 4203 | if (ret) { |
| 4204 | ERR("Error flushing consumer channel"); |
| 4205 | retval = -1; |
| 4206 | continue; |
| 4207 | } |
| 4208 | } |
| 4209 | break; |
| 4210 | case LTTNG_BUFFER_PER_UID: |
| 4211 | default: |
| 4212 | assert(0); |
| 4213 | break; |
| 4214 | } |
| 4215 | |
| 4216 | health_code_update(); |
| 4217 | |
| 4218 | pthread_mutex_unlock(&ua_sess->lock); |
| 4219 | |
| 4220 | end_not_compatible: |
| 4221 | rcu_read_unlock(); |
| 4222 | health_code_update(); |
| 4223 | return retval; |
| 4224 | } |
| 4225 | |
| 4226 | /* |
| 4227 | * Flush buffers for all applications for a specific UST session. |
| 4228 | * Called with UST session lock held. |
| 4229 | */ |
| 4230 | static |
| 4231 | int ust_app_flush_session(struct ltt_ust_session *usess) |
| 4232 | |
| 4233 | { |
| 4234 | int ret = 0; |
| 4235 | |
| 4236 | DBG("Flushing session buffers for all ust apps"); |
| 4237 | |
| 4238 | rcu_read_lock(); |
| 4239 | |
| 4240 | /* Flush buffers and push metadata. */ |
| 4241 | switch (usess->buffer_type) { |
| 4242 | case LTTNG_BUFFER_PER_UID: |
| 4243 | { |
| 4244 | struct buffer_reg_uid *reg; |
| 4245 | struct lttng_ht_iter iter; |
| 4246 | |
| 4247 | /* Flush all per UID buffers associated to that session. */ |
| 4248 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 4249 | struct ust_registry_session *ust_session_reg; |
| 4250 | struct buffer_reg_channel *reg_chan; |
| 4251 | struct consumer_socket *socket; |
| 4252 | |
| 4253 | /* Get consumer socket to use to push the metadata.*/ |
| 4254 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 4255 | usess->consumer); |
| 4256 | if (!socket) { |
| 4257 | /* Ignore request if no consumer is found for the session. */ |
| 4258 | continue; |
| 4259 | } |
| 4260 | |
| 4261 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 4262 | reg_chan, node.node) { |
| 4263 | /* |
| 4264 | * The following call will print error values so the return |
| 4265 | * code is of little importance because whatever happens, we |
| 4266 | * have to try them all. |
| 4267 | */ |
| 4268 | (void) consumer_flush_channel(socket, reg_chan->consumer_key); |
| 4269 | } |
| 4270 | |
| 4271 | ust_session_reg = reg->registry->reg.ust; |
| 4272 | /* Push metadata. */ |
| 4273 | (void) push_metadata(ust_session_reg, usess->consumer); |
| 4274 | } |
| 4275 | break; |
| 4276 | } |
| 4277 | case LTTNG_BUFFER_PER_PID: |
| 4278 | { |
| 4279 | struct ust_app_session *ua_sess; |
| 4280 | struct lttng_ht_iter iter; |
| 4281 | struct ust_app *app; |
| 4282 | |
| 4283 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4284 | ua_sess = lookup_session_by_app(usess, app); |
| 4285 | if (ua_sess == NULL) { |
| 4286 | continue; |
| 4287 | } |
| 4288 | (void) ust_app_flush_app_session(app, ua_sess); |
| 4289 | } |
| 4290 | break; |
| 4291 | } |
| 4292 | default: |
| 4293 | ret = -1; |
| 4294 | assert(0); |
| 4295 | break; |
| 4296 | } |
| 4297 | |
| 4298 | rcu_read_unlock(); |
| 4299 | health_code_update(); |
| 4300 | return ret; |
| 4301 | } |
| 4302 | |
| 4303 | /* |
| 4304 | * Destroy a specific UST session in apps. |
| 4305 | */ |
| 4306 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 4307 | { |
| 4308 | int ret; |
| 4309 | struct ust_app_session *ua_sess; |
| 4310 | struct lttng_ht_iter iter; |
| 4311 | struct lttng_ht_node_u64 *node; |
| 4312 | |
| 4313 | DBG("Destroy tracing for ust app pid %d", app->pid); |
| 4314 | |
| 4315 | rcu_read_lock(); |
| 4316 | |
| 4317 | if (!app->compatible) { |
| 4318 | goto end; |
| 4319 | } |
| 4320 | |
| 4321 | __lookup_session_by_app(usess, app, &iter); |
| 4322 | node = lttng_ht_iter_get_node_u64(&iter); |
| 4323 | if (node == NULL) { |
| 4324 | /* Session is being or is deleted. */ |
| 4325 | goto end; |
| 4326 | } |
| 4327 | ua_sess = caa_container_of(node, struct ust_app_session, node); |
| 4328 | |
| 4329 | health_code_update(); |
| 4330 | destroy_app_session(app, ua_sess); |
| 4331 | |
| 4332 | health_code_update(); |
| 4333 | |
| 4334 | /* Quiescent wait after stopping trace */ |
| 4335 | ret = ustctl_wait_quiescent(app->sock); |
| 4336 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4337 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 4338 | app->pid, ret); |
| 4339 | } |
| 4340 | end: |
| 4341 | rcu_read_unlock(); |
| 4342 | health_code_update(); |
| 4343 | return 0; |
| 4344 | } |
| 4345 | |
| 4346 | /* |
| 4347 | * Start tracing for the UST session. |
| 4348 | */ |
| 4349 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
| 4350 | { |
| 4351 | int ret = 0; |
| 4352 | struct lttng_ht_iter iter; |
| 4353 | struct ust_app *app; |
| 4354 | |
| 4355 | DBG("Starting all UST traces"); |
| 4356 | |
| 4357 | rcu_read_lock(); |
| 4358 | |
| 4359 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4360 | ret = ust_app_start_trace(usess, app); |
| 4361 | if (ret < 0) { |
| 4362 | /* Continue to next apps even on error */ |
| 4363 | continue; |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | rcu_read_unlock(); |
| 4368 | |
| 4369 | return 0; |
| 4370 | } |
| 4371 | |
| 4372 | /* |
| 4373 | * Start tracing for the UST session. |
| 4374 | * Called with UST session lock held. |
| 4375 | */ |
| 4376 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) |
| 4377 | { |
| 4378 | int ret = 0; |
| 4379 | struct lttng_ht_iter iter; |
| 4380 | struct ust_app *app; |
| 4381 | |
| 4382 | DBG("Stopping all UST traces"); |
| 4383 | |
| 4384 | rcu_read_lock(); |
| 4385 | |
| 4386 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4387 | ret = ust_app_stop_trace(usess, app); |
| 4388 | if (ret < 0) { |
| 4389 | /* Continue to next apps even on error */ |
| 4390 | continue; |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | (void) ust_app_flush_session(usess); |
| 4395 | |
| 4396 | rcu_read_unlock(); |
| 4397 | |
| 4398 | return 0; |
| 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * Destroy app UST session. |
| 4403 | */ |
| 4404 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) |
| 4405 | { |
| 4406 | int ret = 0; |
| 4407 | struct lttng_ht_iter iter; |
| 4408 | struct ust_app *app; |
| 4409 | |
| 4410 | DBG("Destroy all UST traces"); |
| 4411 | |
| 4412 | rcu_read_lock(); |
| 4413 | |
| 4414 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4415 | ret = destroy_trace(usess, app); |
| 4416 | if (ret < 0) { |
| 4417 | /* Continue to next apps even on error */ |
| 4418 | continue; |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | rcu_read_unlock(); |
| 4423 | |
| 4424 | return 0; |
| 4425 | } |
| 4426 | |
| 4427 | static |
| 4428 | void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app) |
| 4429 | { |
| 4430 | int ret = 0; |
| 4431 | struct lttng_ht_iter iter, uiter; |
| 4432 | struct ust_app_session *ua_sess = NULL; |
| 4433 | struct ust_app_channel *ua_chan; |
| 4434 | struct ust_app_event *ua_event; |
| 4435 | struct ust_app_ctx *ua_ctx; |
| 4436 | int is_created = 0; |
| 4437 | |
| 4438 | ret = create_ust_app_session(usess, app, &ua_sess, &is_created); |
| 4439 | if (ret < 0) { |
| 4440 | /* Tracer is probably gone or ENOMEM. */ |
| 4441 | goto error; |
| 4442 | } |
| 4443 | if (!is_created) { |
| 4444 | /* App session already created. */ |
| 4445 | goto end; |
| 4446 | } |
| 4447 | assert(ua_sess); |
| 4448 | |
| 4449 | pthread_mutex_lock(&ua_sess->lock); |
| 4450 | |
| 4451 | /* |
| 4452 | * We can iterate safely here over all UST app session since the create ust |
| 4453 | * app session above made a shadow copy of the UST global domain from the |
| 4454 | * ltt ust session. |
| 4455 | */ |
| 4456 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 4457 | node.node) { |
| 4458 | ret = do_create_channel(app, usess, ua_sess, ua_chan); |
| 4459 | if (ret < 0) { |
| 4460 | /* |
| 4461 | * Stop everything. On error, the application failed, no more |
| 4462 | * file descriptor are available or ENOMEM so stopping here is |
| 4463 | * the only thing we can do for now. |
| 4464 | */ |
| 4465 | goto error_unlock; |
| 4466 | } |
| 4467 | |
| 4468 | /* |
| 4469 | * Add context using the list so they are enabled in the same order the |
| 4470 | * user added them. |
| 4471 | */ |
| 4472 | cds_list_for_each_entry(ua_ctx, &ua_chan->ctx_list, list) { |
| 4473 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 4474 | if (ret < 0) { |
| 4475 | goto error_unlock; |
| 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | /* For each events */ |
| 4481 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
| 4482 | node.node) { |
| 4483 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
| 4484 | if (ret < 0) { |
| 4485 | goto error_unlock; |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | pthread_mutex_unlock(&ua_sess->lock); |
| 4491 | |
| 4492 | if (usess->active) { |
| 4493 | ret = ust_app_start_trace(usess, app); |
| 4494 | if (ret < 0) { |
| 4495 | goto error; |
| 4496 | } |
| 4497 | |
| 4498 | DBG2("UST trace started for app pid %d", app->pid); |
| 4499 | } |
| 4500 | end: |
| 4501 | /* Everything went well at this point. */ |
| 4502 | return; |
| 4503 | |
| 4504 | error_unlock: |
| 4505 | pthread_mutex_unlock(&ua_sess->lock); |
| 4506 | error: |
| 4507 | if (ua_sess) { |
| 4508 | destroy_app_session(app, ua_sess); |
| 4509 | } |
| 4510 | return; |
| 4511 | } |
| 4512 | |
| 4513 | static |
| 4514 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) |
| 4515 | { |
| 4516 | struct ust_app_session *ua_sess; |
| 4517 | |
| 4518 | ua_sess = lookup_session_by_app(usess, app); |
| 4519 | if (ua_sess == NULL) { |
| 4520 | return; |
| 4521 | } |
| 4522 | destroy_app_session(app, ua_sess); |
| 4523 | } |
| 4524 | |
| 4525 | /* |
| 4526 | * Add channels/events from UST global domain to registered apps at sock. |
| 4527 | * |
| 4528 | * Called with session lock held. |
| 4529 | * Called with RCU read-side lock held. |
| 4530 | */ |
| 4531 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) |
| 4532 | { |
| 4533 | assert(usess); |
| 4534 | |
| 4535 | DBG2("UST app global update for app sock %d for session id %" PRIu64, |
| 4536 | app->sock, usess->id); |
| 4537 | |
| 4538 | if (!app->compatible) { |
| 4539 | return; |
| 4540 | } |
| 4541 | |
| 4542 | if (trace_ust_pid_tracker_lookup(usess, app->pid)) { |
| 4543 | ust_app_global_create(usess, app); |
| 4544 | } else { |
| 4545 | ust_app_global_destroy(usess, app); |
| 4546 | } |
| 4547 | } |
| 4548 | |
| 4549 | /* |
| 4550 | * Called with session lock held. |
| 4551 | */ |
| 4552 | void ust_app_global_update_all(struct ltt_ust_session *usess) |
| 4553 | { |
| 4554 | struct lttng_ht_iter iter; |
| 4555 | struct ust_app *app; |
| 4556 | |
| 4557 | rcu_read_lock(); |
| 4558 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4559 | ust_app_global_update(usess, app); |
| 4560 | } |
| 4561 | rcu_read_unlock(); |
| 4562 | } |
| 4563 | |
| 4564 | /* |
| 4565 | * Add context to a specific channel for global UST domain. |
| 4566 | */ |
| 4567 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, |
| 4568 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) |
| 4569 | { |
| 4570 | int ret = 0; |
| 4571 | struct lttng_ht_node_str *ua_chan_node; |
| 4572 | struct lttng_ht_iter iter, uiter; |
| 4573 | struct ust_app_channel *ua_chan = NULL; |
| 4574 | struct ust_app_session *ua_sess; |
| 4575 | struct ust_app *app; |
| 4576 | |
| 4577 | rcu_read_lock(); |
| 4578 | |
| 4579 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4580 | if (!app->compatible) { |
| 4581 | /* |
| 4582 | * TODO: In time, we should notice the caller of this error by |
| 4583 | * telling him that this is a version error. |
| 4584 | */ |
| 4585 | continue; |
| 4586 | } |
| 4587 | ua_sess = lookup_session_by_app(usess, app); |
| 4588 | if (ua_sess == NULL) { |
| 4589 | continue; |
| 4590 | } |
| 4591 | |
| 4592 | pthread_mutex_lock(&ua_sess->lock); |
| 4593 | /* Lookup channel in the ust app session */ |
| 4594 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 4595 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 4596 | if (ua_chan_node == NULL) { |
| 4597 | goto next_app; |
| 4598 | } |
| 4599 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, |
| 4600 | node); |
| 4601 | ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app); |
| 4602 | if (ret < 0) { |
| 4603 | goto next_app; |
| 4604 | } |
| 4605 | next_app: |
| 4606 | pthread_mutex_unlock(&ua_sess->lock); |
| 4607 | } |
| 4608 | |
| 4609 | rcu_read_unlock(); |
| 4610 | return ret; |
| 4611 | } |
| 4612 | |
| 4613 | /* |
| 4614 | * Enable event for a channel from a UST session for a specific PID. |
| 4615 | */ |
| 4616 | int ust_app_enable_event_pid(struct ltt_ust_session *usess, |
| 4617 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) |
| 4618 | { |
| 4619 | int ret = 0; |
| 4620 | struct lttng_ht_iter iter; |
| 4621 | struct lttng_ht_node_str *ua_chan_node; |
| 4622 | struct ust_app *app; |
| 4623 | struct ust_app_session *ua_sess; |
| 4624 | struct ust_app_channel *ua_chan; |
| 4625 | struct ust_app_event *ua_event; |
| 4626 | |
| 4627 | DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid); |
| 4628 | |
| 4629 | rcu_read_lock(); |
| 4630 | |
| 4631 | app = ust_app_find_by_pid(pid); |
| 4632 | if (app == NULL) { |
| 4633 | ERR("UST app enable event per PID %d not found", pid); |
| 4634 | ret = -1; |
| 4635 | goto end; |
| 4636 | } |
| 4637 | |
| 4638 | if (!app->compatible) { |
| 4639 | ret = 0; |
| 4640 | goto end; |
| 4641 | } |
| 4642 | |
| 4643 | ua_sess = lookup_session_by_app(usess, app); |
| 4644 | if (!ua_sess) { |
| 4645 | /* The application has problem or is probably dead. */ |
| 4646 | ret = 0; |
| 4647 | goto end; |
| 4648 | } |
| 4649 | |
| 4650 | pthread_mutex_lock(&ua_sess->lock); |
| 4651 | /* Lookup channel in the ust app session */ |
| 4652 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 4653 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 4654 | /* If the channel is not found, there is a code flow error */ |
| 4655 | assert(ua_chan_node); |
| 4656 | |
| 4657 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 4658 | |
| 4659 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 4660 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
| 4661 | if (ua_event == NULL) { |
| 4662 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
| 4663 | if (ret < 0) { |
| 4664 | goto end_unlock; |
| 4665 | } |
| 4666 | } else { |
| 4667 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 4668 | if (ret < 0) { |
| 4669 | goto end_unlock; |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | end_unlock: |
| 4674 | pthread_mutex_unlock(&ua_sess->lock); |
| 4675 | end: |
| 4676 | rcu_read_unlock(); |
| 4677 | return ret; |
| 4678 | } |
| 4679 | |
| 4680 | /* |
| 4681 | * Calibrate registered applications. |
| 4682 | */ |
| 4683 | int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) |
| 4684 | { |
| 4685 | int ret = 0; |
| 4686 | struct lttng_ht_iter iter; |
| 4687 | struct ust_app *app; |
| 4688 | |
| 4689 | rcu_read_lock(); |
| 4690 | |
| 4691 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 4692 | if (!app->compatible) { |
| 4693 | /* |
| 4694 | * TODO: In time, we should notice the caller of this error by |
| 4695 | * telling him that this is a version error. |
| 4696 | */ |
| 4697 | continue; |
| 4698 | } |
| 4699 | |
| 4700 | health_code_update(); |
| 4701 | |
| 4702 | ret = ustctl_calibrate(app->sock, calibrate); |
| 4703 | if (ret < 0) { |
| 4704 | switch (ret) { |
| 4705 | case -ENOSYS: |
| 4706 | /* Means that it's not implemented on the tracer side. */ |
| 4707 | ret = 0; |
| 4708 | break; |
| 4709 | default: |
| 4710 | DBG2("Calibrate app PID %d returned with error %d", |
| 4711 | app->pid, ret); |
| 4712 | break; |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | DBG("UST app global domain calibration finished"); |
| 4718 | |
| 4719 | rcu_read_unlock(); |
| 4720 | |
| 4721 | health_code_update(); |
| 4722 | |
| 4723 | return ret; |
| 4724 | } |
| 4725 | |
| 4726 | /* |
| 4727 | * Receive registration and populate the given msg structure. |
| 4728 | * |
| 4729 | * On success return 0 else a negative value returned by the ustctl call. |
| 4730 | */ |
| 4731 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) |
| 4732 | { |
| 4733 | int ret; |
| 4734 | uint32_t pid, ppid, uid, gid; |
| 4735 | |
| 4736 | assert(msg); |
| 4737 | |
| 4738 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, |
| 4739 | &pid, &ppid, &uid, &gid, |
| 4740 | &msg->bits_per_long, |
| 4741 | &msg->uint8_t_alignment, |
| 4742 | &msg->uint16_t_alignment, |
| 4743 | &msg->uint32_t_alignment, |
| 4744 | &msg->uint64_t_alignment, |
| 4745 | &msg->long_alignment, |
| 4746 | &msg->byte_order, |
| 4747 | msg->name); |
| 4748 | if (ret < 0) { |
| 4749 | switch (-ret) { |
| 4750 | case EPIPE: |
| 4751 | case ECONNRESET: |
| 4752 | case LTTNG_UST_ERR_EXITING: |
| 4753 | DBG3("UST app recv reg message failed. Application died"); |
| 4754 | break; |
| 4755 | case LTTNG_UST_ERR_UNSUP_MAJOR: |
| 4756 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", |
| 4757 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, |
| 4758 | LTTNG_UST_ABI_MINOR_VERSION); |
| 4759 | break; |
| 4760 | default: |
| 4761 | ERR("UST app recv reg message failed with ret %d", ret); |
| 4762 | break; |
| 4763 | } |
| 4764 | goto error; |
| 4765 | } |
| 4766 | msg->pid = (pid_t) pid; |
| 4767 | msg->ppid = (pid_t) ppid; |
| 4768 | msg->uid = (uid_t) uid; |
| 4769 | msg->gid = (gid_t) gid; |
| 4770 | |
| 4771 | error: |
| 4772 | return ret; |
| 4773 | } |
| 4774 | |
| 4775 | /* |
| 4776 | * Return a ust app channel object using the application object and the channel |
| 4777 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
| 4778 | * lock MUST be acquired before calling this function. |
| 4779 | */ |
| 4780 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
| 4781 | int objd) |
| 4782 | { |
| 4783 | struct lttng_ht_node_ulong *node; |
| 4784 | struct lttng_ht_iter iter; |
| 4785 | struct ust_app_channel *ua_chan = NULL; |
| 4786 | |
| 4787 | assert(app); |
| 4788 | |
| 4789 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); |
| 4790 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 4791 | if (node == NULL) { |
| 4792 | DBG2("UST app channel find by objd %d not found", objd); |
| 4793 | goto error; |
| 4794 | } |
| 4795 | |
| 4796 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); |
| 4797 | |
| 4798 | error: |
| 4799 | return ua_chan; |
| 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * Reply to a register channel notification from an application on the notify |
| 4804 | * socket. The channel metadata is also created. |
| 4805 | * |
| 4806 | * The session UST registry lock is acquired in this function. |
| 4807 | * |
| 4808 | * On success 0 is returned else a negative value. |
| 4809 | */ |
| 4810 | static int reply_ust_register_channel(int sock, int sobjd, int cobjd, |
| 4811 | size_t nr_fields, struct ustctl_field *fields) |
| 4812 | { |
| 4813 | int ret, ret_code = 0; |
| 4814 | uint32_t chan_id, reg_count; |
| 4815 | uint64_t chan_reg_key; |
| 4816 | enum ustctl_channel_header type; |
| 4817 | struct ust_app *app; |
| 4818 | struct ust_app_channel *ua_chan; |
| 4819 | struct ust_app_session *ua_sess; |
| 4820 | struct ust_registry_session *registry; |
| 4821 | struct ust_registry_channel *chan_reg; |
| 4822 | |
| 4823 | rcu_read_lock(); |
| 4824 | |
| 4825 | /* Lookup application. If not found, there is a code flow error. */ |
| 4826 | app = find_app_by_notify_sock(sock); |
| 4827 | if (!app) { |
| 4828 | DBG("Application socket %d is being teardown. Abort event notify", |
| 4829 | sock); |
| 4830 | ret = 0; |
| 4831 | free(fields); |
| 4832 | goto error_rcu_unlock; |
| 4833 | } |
| 4834 | |
| 4835 | /* Lookup channel by UST object descriptor. */ |
| 4836 | ua_chan = find_channel_by_objd(app, cobjd); |
| 4837 | if (!ua_chan) { |
| 4838 | DBG("Application channel is being teardown. Abort event notify"); |
| 4839 | ret = 0; |
| 4840 | free(fields); |
| 4841 | goto error_rcu_unlock; |
| 4842 | } |
| 4843 | |
| 4844 | assert(ua_chan->session); |
| 4845 | ua_sess = ua_chan->session; |
| 4846 | |
| 4847 | /* Get right session registry depending on the session buffer type. */ |
| 4848 | registry = get_session_registry(ua_sess); |
| 4849 | assert(registry); |
| 4850 | |
| 4851 | /* Depending on the buffer type, a different channel key is used. */ |
| 4852 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 4853 | chan_reg_key = ua_chan->tracing_channel_id; |
| 4854 | } else { |
| 4855 | chan_reg_key = ua_chan->key; |
| 4856 | } |
| 4857 | |
| 4858 | pthread_mutex_lock(®istry->lock); |
| 4859 | |
| 4860 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); |
| 4861 | assert(chan_reg); |
| 4862 | |
| 4863 | if (!chan_reg->register_done) { |
| 4864 | reg_count = ust_registry_get_event_count(chan_reg); |
| 4865 | if (reg_count < 31) { |
| 4866 | type = USTCTL_CHANNEL_HEADER_COMPACT; |
| 4867 | } else { |
| 4868 | type = USTCTL_CHANNEL_HEADER_LARGE; |
| 4869 | } |
| 4870 | |
| 4871 | chan_reg->nr_ctx_fields = nr_fields; |
| 4872 | chan_reg->ctx_fields = fields; |
| 4873 | chan_reg->header_type = type; |
| 4874 | } else { |
| 4875 | /* Get current already assigned values. */ |
| 4876 | type = chan_reg->header_type; |
| 4877 | free(fields); |
| 4878 | /* Set to NULL so the error path does not do a double free. */ |
| 4879 | fields = NULL; |
| 4880 | } |
| 4881 | /* Channel id is set during the object creation. */ |
| 4882 | chan_id = chan_reg->chan_id; |
| 4883 | |
| 4884 | /* Append to metadata */ |
| 4885 | if (!chan_reg->metadata_dumped) { |
| 4886 | ret_code = ust_metadata_channel_statedump(registry, chan_reg); |
| 4887 | if (ret_code) { |
| 4888 | ERR("Error appending channel metadata (errno = %d)", ret_code); |
| 4889 | goto reply; |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | reply: |
| 4894 | DBG3("UST app replying to register channel key %" PRIu64 |
| 4895 | " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type, |
| 4896 | ret_code); |
| 4897 | |
| 4898 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); |
| 4899 | if (ret < 0) { |
| 4900 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4901 | ERR("UST app reply channel failed with ret %d", ret); |
| 4902 | } else { |
| 4903 | DBG3("UST app reply channel failed. Application died"); |
| 4904 | } |
| 4905 | goto error; |
| 4906 | } |
| 4907 | |
| 4908 | /* This channel registry registration is completed. */ |
| 4909 | chan_reg->register_done = 1; |
| 4910 | |
| 4911 | error: |
| 4912 | pthread_mutex_unlock(®istry->lock); |
| 4913 | error_rcu_unlock: |
| 4914 | rcu_read_unlock(); |
| 4915 | if (ret) { |
| 4916 | free(fields); |
| 4917 | } |
| 4918 | return ret; |
| 4919 | } |
| 4920 | |
| 4921 | /* |
| 4922 | * Add event to the UST channel registry. When the event is added to the |
| 4923 | * registry, the metadata is also created. Once done, this replies to the |
| 4924 | * application with the appropriate error code. |
| 4925 | * |
| 4926 | * The session UST registry lock is acquired in the function. |
| 4927 | * |
| 4928 | * On success 0 is returned else a negative value. |
| 4929 | */ |
| 4930 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
| 4931 | char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel, |
| 4932 | char *model_emf_uri) |
| 4933 | { |
| 4934 | int ret, ret_code; |
| 4935 | uint32_t event_id = 0; |
| 4936 | uint64_t chan_reg_key; |
| 4937 | struct ust_app *app; |
| 4938 | struct ust_app_channel *ua_chan; |
| 4939 | struct ust_app_session *ua_sess; |
| 4940 | struct ust_registry_session *registry; |
| 4941 | |
| 4942 | rcu_read_lock(); |
| 4943 | |
| 4944 | /* Lookup application. If not found, there is a code flow error. */ |
| 4945 | app = find_app_by_notify_sock(sock); |
| 4946 | if (!app) { |
| 4947 | DBG("Application socket %d is being teardown. Abort event notify", |
| 4948 | sock); |
| 4949 | ret = 0; |
| 4950 | free(sig); |
| 4951 | free(fields); |
| 4952 | free(model_emf_uri); |
| 4953 | goto error_rcu_unlock; |
| 4954 | } |
| 4955 | |
| 4956 | /* Lookup channel by UST object descriptor. */ |
| 4957 | ua_chan = find_channel_by_objd(app, cobjd); |
| 4958 | if (!ua_chan) { |
| 4959 | DBG("Application channel is being teardown. Abort event notify"); |
| 4960 | ret = 0; |
| 4961 | free(sig); |
| 4962 | free(fields); |
| 4963 | free(model_emf_uri); |
| 4964 | goto error_rcu_unlock; |
| 4965 | } |
| 4966 | |
| 4967 | assert(ua_chan->session); |
| 4968 | ua_sess = ua_chan->session; |
| 4969 | |
| 4970 | registry = get_session_registry(ua_sess); |
| 4971 | assert(registry); |
| 4972 | |
| 4973 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 4974 | chan_reg_key = ua_chan->tracing_channel_id; |
| 4975 | } else { |
| 4976 | chan_reg_key = ua_chan->key; |
| 4977 | } |
| 4978 | |
| 4979 | pthread_mutex_lock(®istry->lock); |
| 4980 | |
| 4981 | /* |
| 4982 | * From this point on, this call acquires the ownership of the sig, fields |
| 4983 | * and model_emf_uri meaning any free are done inside it if needed. These |
| 4984 | * three variables MUST NOT be read/write after this. |
| 4985 | */ |
| 4986 | ret_code = ust_registry_create_event(registry, chan_reg_key, |
| 4987 | sobjd, cobjd, name, sig, nr_fields, fields, loglevel, |
| 4988 | model_emf_uri, ua_sess->buffer_type, &event_id, |
| 4989 | app); |
| 4990 | |
| 4991 | /* |
| 4992 | * The return value is returned to ustctl so in case of an error, the |
| 4993 | * application can be notified. In case of an error, it's important not to |
| 4994 | * return a negative error or else the application will get closed. |
| 4995 | */ |
| 4996 | ret = ustctl_reply_register_event(sock, event_id, ret_code); |
| 4997 | if (ret < 0) { |
| 4998 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 4999 | ERR("UST app reply event failed with ret %d", ret); |
| 5000 | } else { |
| 5001 | DBG3("UST app reply event failed. Application died"); |
| 5002 | } |
| 5003 | /* |
| 5004 | * No need to wipe the create event since the application socket will |
| 5005 | * get close on error hence cleaning up everything by itself. |
| 5006 | */ |
| 5007 | goto error; |
| 5008 | } |
| 5009 | |
| 5010 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
| 5011 | name, event_id); |
| 5012 | |
| 5013 | error: |
| 5014 | pthread_mutex_unlock(®istry->lock); |
| 5015 | error_rcu_unlock: |
| 5016 | rcu_read_unlock(); |
| 5017 | return ret; |
| 5018 | } |
| 5019 | |
| 5020 | /* |
| 5021 | * Handle application notification through the given notify socket. |
| 5022 | * |
| 5023 | * Return 0 on success or else a negative value. |
| 5024 | */ |
| 5025 | int ust_app_recv_notify(int sock) |
| 5026 | { |
| 5027 | int ret; |
| 5028 | enum ustctl_notify_cmd cmd; |
| 5029 | |
| 5030 | DBG3("UST app receiving notify from sock %d", sock); |
| 5031 | |
| 5032 | ret = ustctl_recv_notify(sock, &cmd); |
| 5033 | if (ret < 0) { |
| 5034 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 5035 | ERR("UST app recv notify failed with ret %d", ret); |
| 5036 | } else { |
| 5037 | DBG3("UST app recv notify failed. Application died"); |
| 5038 | } |
| 5039 | goto error; |
| 5040 | } |
| 5041 | |
| 5042 | switch (cmd) { |
| 5043 | case USTCTL_NOTIFY_CMD_EVENT: |
| 5044 | { |
| 5045 | int sobjd, cobjd, loglevel; |
| 5046 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
| 5047 | size_t nr_fields; |
| 5048 | struct ustctl_field *fields; |
| 5049 | |
| 5050 | DBG2("UST app ustctl register event received"); |
| 5051 | |
| 5052 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel, |
| 5053 | &sig, &nr_fields, &fields, &model_emf_uri); |
| 5054 | if (ret < 0) { |
| 5055 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 5056 | ERR("UST app recv event failed with ret %d", ret); |
| 5057 | } else { |
| 5058 | DBG3("UST app recv event failed. Application died"); |
| 5059 | } |
| 5060 | goto error; |
| 5061 | } |
| 5062 | |
| 5063 | /* |
| 5064 | * Add event to the UST registry coming from the notify socket. This |
| 5065 | * call will free if needed the sig, fields and model_emf_uri. This |
| 5066 | * code path loses the ownsership of these variables and transfer them |
| 5067 | * to the this function. |
| 5068 | */ |
| 5069 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
| 5070 | fields, loglevel, model_emf_uri); |
| 5071 | if (ret < 0) { |
| 5072 | goto error; |
| 5073 | } |
| 5074 | |
| 5075 | break; |
| 5076 | } |
| 5077 | case USTCTL_NOTIFY_CMD_CHANNEL: |
| 5078 | { |
| 5079 | int sobjd, cobjd; |
| 5080 | size_t nr_fields; |
| 5081 | struct ustctl_field *fields; |
| 5082 | |
| 5083 | DBG2("UST app ustctl register channel received"); |
| 5084 | |
| 5085 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, |
| 5086 | &fields); |
| 5087 | if (ret < 0) { |
| 5088 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 5089 | ERR("UST app recv channel failed with ret %d", ret); |
| 5090 | } else { |
| 5091 | DBG3("UST app recv channel failed. Application died"); |
| 5092 | } |
| 5093 | goto error; |
| 5094 | } |
| 5095 | |
| 5096 | /* |
| 5097 | * The fields ownership are transfered to this function call meaning |
| 5098 | * that if needed it will be freed. After this, it's invalid to access |
| 5099 | * fields or clean it up. |
| 5100 | */ |
| 5101 | ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields, |
| 5102 | fields); |
| 5103 | if (ret < 0) { |
| 5104 | goto error; |
| 5105 | } |
| 5106 | |
| 5107 | break; |
| 5108 | } |
| 5109 | default: |
| 5110 | /* Should NEVER happen. */ |
| 5111 | assert(0); |
| 5112 | } |
| 5113 | |
| 5114 | error: |
| 5115 | return ret; |
| 5116 | } |
| 5117 | |
| 5118 | /* |
| 5119 | * Once the notify socket hangs up, this is called. First, it tries to find the |
| 5120 | * corresponding application. On failure, the call_rcu to close the socket is |
| 5121 | * executed. If an application is found, it tries to delete it from the notify |
| 5122 | * socket hash table. Whathever the result, it proceeds to the call_rcu. |
| 5123 | * |
| 5124 | * Note that an object needs to be allocated here so on ENOMEM failure, the |
| 5125 | * call RCU is not done but the rest of the cleanup is. |
| 5126 | */ |
| 5127 | void ust_app_notify_sock_unregister(int sock) |
| 5128 | { |
| 5129 | int err_enomem = 0; |
| 5130 | struct lttng_ht_iter iter; |
| 5131 | struct ust_app *app; |
| 5132 | struct ust_app_notify_sock_obj *obj; |
| 5133 | |
| 5134 | assert(sock >= 0); |
| 5135 | |
| 5136 | rcu_read_lock(); |
| 5137 | |
| 5138 | obj = zmalloc(sizeof(*obj)); |
| 5139 | if (!obj) { |
| 5140 | /* |
| 5141 | * An ENOMEM is kind of uncool. If this strikes we continue the |
| 5142 | * procedure but the call_rcu will not be called. In this case, we |
| 5143 | * accept the fd leak rather than possibly creating an unsynchronized |
| 5144 | * state between threads. |
| 5145 | * |
| 5146 | * TODO: The notify object should be created once the notify socket is |
| 5147 | * registered and stored independantely from the ust app object. The |
| 5148 | * tricky part is to synchronize the teardown of the application and |
| 5149 | * this notify object. Let's keep that in mind so we can avoid this |
| 5150 | * kind of shenanigans with ENOMEM in the teardown path. |
| 5151 | */ |
| 5152 | err_enomem = 1; |
| 5153 | } else { |
| 5154 | obj->fd = sock; |
| 5155 | } |
| 5156 | |
| 5157 | DBG("UST app notify socket unregister %d", sock); |
| 5158 | |
| 5159 | /* |
| 5160 | * Lookup application by notify socket. If this fails, this means that the |
| 5161 | * hash table delete has already been done by the application |
| 5162 | * unregistration process so we can safely close the notify socket in a |
| 5163 | * call RCU. |
| 5164 | */ |
| 5165 | app = find_app_by_notify_sock(sock); |
| 5166 | if (!app) { |
| 5167 | goto close_socket; |
| 5168 | } |
| 5169 | |
| 5170 | iter.iter.node = &app->notify_sock_n.node; |
| 5171 | |
| 5172 | /* |
| 5173 | * Whatever happens here either we fail or succeed, in both cases we have |
| 5174 | * to close the socket after a grace period to continue to the call RCU |
| 5175 | * here. If the deletion is successful, the application is not visible |
| 5176 | * anymore by other threads and is it fails it means that it was already |
| 5177 | * deleted from the hash table so either way we just have to close the |
| 5178 | * socket. |
| 5179 | */ |
| 5180 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 5181 | |
| 5182 | close_socket: |
| 5183 | rcu_read_unlock(); |
| 5184 | |
| 5185 | /* |
| 5186 | * Close socket after a grace period to avoid for the socket to be reused |
| 5187 | * before the application object is freed creating potential race between |
| 5188 | * threads trying to add unique in the global hash table. |
| 5189 | */ |
| 5190 | if (!err_enomem) { |
| 5191 | call_rcu(&obj->head, close_notify_sock_rcu); |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | /* |
| 5196 | * Destroy a ust app data structure and free its memory. |
| 5197 | */ |
| 5198 | void ust_app_destroy(struct ust_app *app) |
| 5199 | { |
| 5200 | if (!app) { |
| 5201 | return; |
| 5202 | } |
| 5203 | |
| 5204 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
| 5205 | } |
| 5206 | |
| 5207 | /* |
| 5208 | * Take a snapshot for a given UST session. The snapshot is sent to the given |
| 5209 | * output. |
| 5210 | * |
| 5211 | * Return 0 on success or else a negative value. |
| 5212 | */ |
| 5213 | int ust_app_snapshot_record(struct ltt_ust_session *usess, |
| 5214 | struct snapshot_output *output, int wait, |
| 5215 | uint64_t nb_packets_per_stream) |
| 5216 | { |
| 5217 | int ret = 0; |
| 5218 | unsigned int snapshot_done = 0; |
| 5219 | struct lttng_ht_iter iter; |
| 5220 | struct ust_app *app; |
| 5221 | char pathname[PATH_MAX]; |
| 5222 | |
| 5223 | assert(usess); |
| 5224 | assert(output); |
| 5225 | |
| 5226 | rcu_read_lock(); |
| 5227 | |
| 5228 | switch (usess->buffer_type) { |
| 5229 | case LTTNG_BUFFER_PER_UID: |
| 5230 | { |
| 5231 | struct buffer_reg_uid *reg; |
| 5232 | |
| 5233 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 5234 | struct buffer_reg_channel *reg_chan; |
| 5235 | struct consumer_socket *socket; |
| 5236 | |
| 5237 | /* Get consumer socket to use to push the metadata.*/ |
| 5238 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 5239 | usess->consumer); |
| 5240 | if (!socket) { |
| 5241 | ret = -EINVAL; |
| 5242 | goto error; |
| 5243 | } |
| 5244 | |
| 5245 | memset(pathname, 0, sizeof(pathname)); |
| 5246 | ret = snprintf(pathname, sizeof(pathname), |
| 5247 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, |
| 5248 | reg->uid, reg->bits_per_long); |
| 5249 | if (ret < 0) { |
| 5250 | PERROR("snprintf snapshot path"); |
| 5251 | goto error; |
| 5252 | } |
| 5253 | |
| 5254 | /* Add the UST default trace dir to path. */ |
| 5255 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 5256 | reg_chan, node.node) { |
| 5257 | ret = consumer_snapshot_channel(socket, reg_chan->consumer_key, |
| 5258 | output, 0, usess->uid, usess->gid, pathname, wait, |
| 5259 | nb_packets_per_stream); |
| 5260 | if (ret < 0) { |
| 5261 | goto error; |
| 5262 | } |
| 5263 | } |
| 5264 | ret = consumer_snapshot_channel(socket, |
| 5265 | reg->registry->reg.ust->metadata_key, output, 1, |
| 5266 | usess->uid, usess->gid, pathname, wait, 0); |
| 5267 | if (ret < 0) { |
| 5268 | goto error; |
| 5269 | } |
| 5270 | snapshot_done = 1; |
| 5271 | } |
| 5272 | break; |
| 5273 | } |
| 5274 | case LTTNG_BUFFER_PER_PID: |
| 5275 | { |
| 5276 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5277 | struct consumer_socket *socket; |
| 5278 | struct lttng_ht_iter chan_iter; |
| 5279 | struct ust_app_channel *ua_chan; |
| 5280 | struct ust_app_session *ua_sess; |
| 5281 | struct ust_registry_session *registry; |
| 5282 | |
| 5283 | ua_sess = lookup_session_by_app(usess, app); |
| 5284 | if (!ua_sess) { |
| 5285 | /* Session not associated with this app. */ |
| 5286 | continue; |
| 5287 | } |
| 5288 | |
| 5289 | /* Get the right consumer socket for the application. */ |
| 5290 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 5291 | output->consumer); |
| 5292 | if (!socket) { |
| 5293 | ret = -EINVAL; |
| 5294 | goto error; |
| 5295 | } |
| 5296 | |
| 5297 | /* Add the UST default trace dir to path. */ |
| 5298 | memset(pathname, 0, sizeof(pathname)); |
| 5299 | ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", |
| 5300 | ua_sess->path); |
| 5301 | if (ret < 0) { |
| 5302 | PERROR("snprintf snapshot path"); |
| 5303 | goto error; |
| 5304 | } |
| 5305 | |
| 5306 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 5307 | ua_chan, node.node) { |
| 5308 | ret = consumer_snapshot_channel(socket, ua_chan->key, output, |
| 5309 | 0, ua_sess->euid, ua_sess->egid, pathname, wait, |
| 5310 | nb_packets_per_stream); |
| 5311 | if (ret < 0) { |
| 5312 | goto error; |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | registry = get_session_registry(ua_sess); |
| 5317 | assert(registry); |
| 5318 | ret = consumer_snapshot_channel(socket, registry->metadata_key, output, |
| 5319 | 1, ua_sess->euid, ua_sess->egid, pathname, wait, 0); |
| 5320 | if (ret < 0) { |
| 5321 | goto error; |
| 5322 | } |
| 5323 | snapshot_done = 1; |
| 5324 | } |
| 5325 | break; |
| 5326 | } |
| 5327 | default: |
| 5328 | assert(0); |
| 5329 | break; |
| 5330 | } |
| 5331 | |
| 5332 | if (!snapshot_done) { |
| 5333 | /* |
| 5334 | * If no snapshot was made and we are not in the error path, this means |
| 5335 | * that there are no buffers thus no (prior) application to snapshot |
| 5336 | * data from so we have simply NO data. |
| 5337 | */ |
| 5338 | ret = -ENODATA; |
| 5339 | } |
| 5340 | |
| 5341 | error: |
| 5342 | rcu_read_unlock(); |
| 5343 | return ret; |
| 5344 | } |
| 5345 | |
| 5346 | /* |
| 5347 | * Return the size taken by one more packet per stream. |
| 5348 | */ |
| 5349 | uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess, |
| 5350 | uint64_t cur_nr_packets) |
| 5351 | { |
| 5352 | uint64_t tot_size = 0; |
| 5353 | struct ust_app *app; |
| 5354 | struct lttng_ht_iter iter; |
| 5355 | |
| 5356 | assert(usess); |
| 5357 | |
| 5358 | switch (usess->buffer_type) { |
| 5359 | case LTTNG_BUFFER_PER_UID: |
| 5360 | { |
| 5361 | struct buffer_reg_uid *reg; |
| 5362 | |
| 5363 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 5364 | struct buffer_reg_channel *reg_chan; |
| 5365 | |
| 5366 | rcu_read_lock(); |
| 5367 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 5368 | reg_chan, node.node) { |
| 5369 | if (cur_nr_packets >= reg_chan->num_subbuf) { |
| 5370 | /* |
| 5371 | * Don't take channel into account if we |
| 5372 | * already grab all its packets. |
| 5373 | */ |
| 5374 | continue; |
| 5375 | } |
| 5376 | tot_size += reg_chan->subbuf_size * reg_chan->stream_count; |
| 5377 | } |
| 5378 | rcu_read_unlock(); |
| 5379 | } |
| 5380 | break; |
| 5381 | } |
| 5382 | case LTTNG_BUFFER_PER_PID: |
| 5383 | { |
| 5384 | rcu_read_lock(); |
| 5385 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5386 | struct ust_app_channel *ua_chan; |
| 5387 | struct ust_app_session *ua_sess; |
| 5388 | struct lttng_ht_iter chan_iter; |
| 5389 | |
| 5390 | ua_sess = lookup_session_by_app(usess, app); |
| 5391 | if (!ua_sess) { |
| 5392 | /* Session not associated with this app. */ |
| 5393 | continue; |
| 5394 | } |
| 5395 | |
| 5396 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 5397 | ua_chan, node.node) { |
| 5398 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
| 5399 | /* |
| 5400 | * Don't take channel into account if we |
| 5401 | * already grab all its packets. |
| 5402 | */ |
| 5403 | continue; |
| 5404 | } |
| 5405 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; |
| 5406 | } |
| 5407 | } |
| 5408 | rcu_read_unlock(); |
| 5409 | break; |
| 5410 | } |
| 5411 | default: |
| 5412 | assert(0); |
| 5413 | break; |
| 5414 | } |
| 5415 | |
| 5416 | return tot_size; |
| 5417 | } |