| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <pthread.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | #include <urcu/compiler.h> |
| 22 | #include <signal.h> |
| 23 | |
| 24 | #include <common/bytecode/bytecode.h> |
| 25 | #include <common/compat/errno.h> |
| 26 | #include <common/common.h> |
| 27 | #include <common/hashtable/utils.h> |
| 28 | #include <common/shm.h> |
| 29 | #include <lttng/lttng-error.h> |
| 30 | #include <lttng/event-rule/event-rule.h> |
| 31 | #include <lttng/event-rule/event-rule-internal.h> |
| 32 | #include <lttng/event-rule/tracepoint.h> |
| 33 | #include <lttng/condition/condition.h> |
| 34 | #include <lttng/condition/on-event-internal.h> |
| 35 | #include <lttng/condition/on-event.h> |
| 36 | #include <lttng/map/map.h> |
| 37 | #include <lttng/map/map-internal.h> |
| 38 | #include <lttng/map/map-query-internal.h> |
| 39 | #include <lttng/map-key.h> |
| 40 | #include <lttng/map-key-internal.h> |
| 41 | #include <lttng/trigger/trigger-internal.h> |
| 42 | #include <common/sessiond-comm/sessiond-comm.h> |
| 43 | |
| 44 | #include "buffer-registry.h" |
| 45 | #include "condition-internal.h" |
| 46 | #include "fd-limit.h" |
| 47 | #include "health-sessiond.h" |
| 48 | #include "ust-app.h" |
| 49 | #include "ust-consumer.h" |
| 50 | #include "lttng-ust-ctl.h" |
| 51 | #include "lttng-ust-error.h" |
| 52 | #include "utils.h" |
| 53 | #include "session.h" |
| 54 | #include "lttng-sessiond.h" |
| 55 | #include "notification-thread-commands.h" |
| 56 | #include "rotate.h" |
| 57 | #include "event.h" |
| 58 | #include "event-notifier-error-accounting.h" |
| 59 | #include "map.h" |
| 60 | |
| 61 | |
| 62 | struct lttng_ht *ust_app_ht; |
| 63 | struct lttng_ht *ust_app_ht_by_sock; |
| 64 | struct lttng_ht *ust_app_ht_by_notify_sock; |
| 65 | |
| 66 | static |
| 67 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); |
| 68 | |
| 69 | /* Next available channel key. Access under next_channel_key_lock. */ |
| 70 | static uint64_t _next_channel_key; |
| 71 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; |
| 72 | |
| 73 | /* Next available map key. Access under next_map_key_lock. */ |
| 74 | static uint64_t _next_map_key; |
| 75 | static pthread_mutex_t next_map_key_lock = PTHREAD_MUTEX_INITIALIZER; |
| 76 | |
| 77 | /* Next available session ID. Access under next_session_id_lock. */ |
| 78 | static uint64_t _next_session_id; |
| 79 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 80 | |
| 81 | /* |
| 82 | * Return the incremented value of next_channel_key. |
| 83 | */ |
| 84 | static uint64_t get_next_channel_key(void) |
| 85 | { |
| 86 | uint64_t ret; |
| 87 | |
| 88 | pthread_mutex_lock(&next_channel_key_lock); |
| 89 | ret = ++_next_channel_key; |
| 90 | pthread_mutex_unlock(&next_channel_key_lock); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Return the incremented value of next_map_key. |
| 96 | */ |
| 97 | static uint64_t get_next_map_key(void) |
| 98 | { |
| 99 | uint64_t ret; |
| 100 | |
| 101 | pthread_mutex_lock(&next_map_key_lock); |
| 102 | ret = ++_next_map_key; |
| 103 | pthread_mutex_unlock(&next_map_key_lock); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Return the atomically incremented value of next_session_id. |
| 109 | */ |
| 110 | static uint64_t get_next_session_id(void) |
| 111 | { |
| 112 | uint64_t ret; |
| 113 | |
| 114 | pthread_mutex_lock(&next_session_id_lock); |
| 115 | ret = ++_next_session_id; |
| 116 | pthread_mutex_unlock(&next_session_id_lock); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static void copy_channel_attr_to_ustctl( |
| 121 | struct ustctl_consumer_channel_attr *attr, |
| 122 | struct lttng_ust_channel_attr *uattr) |
| 123 | { |
| 124 | /* Copy event attributes since the layout is different. */ |
| 125 | attr->subbuf_size = uattr->subbuf_size; |
| 126 | attr->num_subbuf = uattr->num_subbuf; |
| 127 | attr->overwrite = uattr->overwrite; |
| 128 | attr->switch_timer_interval = uattr->switch_timer_interval; |
| 129 | attr->read_timer_interval = uattr->read_timer_interval; |
| 130 | attr->output = uattr->output; |
| 131 | attr->blocking_timeout = uattr->u.s.blocking_timeout; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Match function for the hash table lookup. |
| 136 | * |
| 137 | * It matches an ust app event based on three attributes which are the event |
| 138 | * name, the filter bytecode and the loglevel. |
| 139 | */ |
| 140 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
| 141 | { |
| 142 | struct ust_app_event *event; |
| 143 | const struct ust_app_ht_key *key; |
| 144 | int ev_loglevel_value; |
| 145 | |
| 146 | assert(node); |
| 147 | assert(_key); |
| 148 | |
| 149 | event = caa_container_of(node, struct ust_app_event, node.node); |
| 150 | key = _key; |
| 151 | ev_loglevel_value = event->attr.loglevel; |
| 152 | |
| 153 | /* |
| 154 | * Match the 5 elements of the key: |
| 155 | * tracer token, name, filter, loglevel, exclusions |
| 156 | */ |
| 157 | |
| 158 | if (event->attr.token != key->tracer_token) { |
| 159 | goto no_match; |
| 160 | } |
| 161 | |
| 162 | /* Event name */ |
| 163 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { |
| 164 | goto no_match; |
| 165 | } |
| 166 | |
| 167 | /* Event loglevel. */ |
| 168 | if (ev_loglevel_value != key->loglevel_type) { |
| 169 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
| 170 | && key->loglevel_type == 0 && |
| 171 | ev_loglevel_value == -1) { |
| 172 | /* |
| 173 | * Match is accepted. This is because on event creation, the |
| 174 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and |
| 175 | * -1 are accepted for this loglevel type since 0 is the one set by |
| 176 | * the API when receiving an enable event. |
| 177 | */ |
| 178 | } else { |
| 179 | goto no_match; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* One of the filters is NULL, fail. */ |
| 184 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { |
| 185 | goto no_match; |
| 186 | } |
| 187 | |
| 188 | if (key->filter && event->filter) { |
| 189 | /* Both filters exists, check length followed by the bytecode. */ |
| 190 | if (event->filter->len != key->filter->len || |
| 191 | memcmp(event->filter->data, key->filter->data, |
| 192 | event->filter->len) != 0) { |
| 193 | goto no_match; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* One of the exclusions is NULL, fail. */ |
| 198 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { |
| 199 | goto no_match; |
| 200 | } |
| 201 | |
| 202 | if (key->exclusion && event->exclusion) { |
| 203 | /* Both exclusions exists, check count followed by the names. */ |
| 204 | if (event->exclusion->count != key->exclusion->count || |
| 205 | memcmp(event->exclusion->names, key->exclusion->names, |
| 206 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { |
| 207 | goto no_match; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* Match. */ |
| 213 | return 1; |
| 214 | |
| 215 | no_match: |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Unique add of an ust app event in the given ht. This uses the custom |
| 221 | * ht_match_ust_app_event match function and the event name as hash. |
| 222 | */ |
| 223 | static void add_unique_ust_app_event(struct lttng_ht *events_ht, |
| 224 | struct ust_app_event *event) |
| 225 | { |
| 226 | struct cds_lfht_node *node_ptr; |
| 227 | struct ust_app_ht_key key; |
| 228 | |
| 229 | assert(events_ht); |
| 230 | assert(event); |
| 231 | |
| 232 | key.name = event->attr.name; |
| 233 | key.filter = event->filter; |
| 234 | key.loglevel_type = event->attr.loglevel; |
| 235 | key.exclusion = event->exclusion; |
| 236 | key.tracer_token = event->attr.token; |
| 237 | |
| 238 | node_ptr = cds_lfht_add_unique(events_ht->ht, |
| 239 | events_ht->hash_fct(event->node.key, lttng_ht_seed), |
| 240 | ht_match_ust_app_event, &key, &event->node.node); |
| 241 | assert(node_ptr == &event->node.node); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Close the notify socket from the given RCU head object. This MUST be called |
| 246 | * through a call_rcu(). |
| 247 | */ |
| 248 | static void close_notify_sock_rcu(struct rcu_head *head) |
| 249 | { |
| 250 | int ret; |
| 251 | struct ust_app_notify_sock_obj *obj = |
| 252 | caa_container_of(head, struct ust_app_notify_sock_obj, head); |
| 253 | |
| 254 | /* Must have a valid fd here. */ |
| 255 | assert(obj->fd >= 0); |
| 256 | |
| 257 | ret = close(obj->fd); |
| 258 | if (ret) { |
| 259 | ERR("close notify sock %d RCU", obj->fd); |
| 260 | } |
| 261 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 262 | |
| 263 | free(obj); |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Return the session registry according to the buffer type of the given |
| 268 | * session. |
| 269 | * |
| 270 | * A registry per UID object MUST exists before calling this function or else |
| 271 | * it assert() if not found. RCU read side lock must be acquired. |
| 272 | */ |
| 273 | static struct ust_registry_session *get_session_registry( |
| 274 | struct ust_app_session *ua_sess) |
| 275 | { |
| 276 | struct ust_registry_session *registry = NULL; |
| 277 | |
| 278 | assert(ua_sess); |
| 279 | |
| 280 | switch (ua_sess->buffer_type) { |
| 281 | case LTTNG_BUFFER_PER_PID: |
| 282 | { |
| 283 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 284 | if (!reg_pid) { |
| 285 | goto error; |
| 286 | } |
| 287 | registry = reg_pid->registry->reg.ust; |
| 288 | break; |
| 289 | } |
| 290 | case LTTNG_BUFFER_PER_UID: |
| 291 | { |
| 292 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( |
| 293 | ua_sess->tracing_id, ua_sess->bits_per_long, |
| 294 | lttng_credentials_get_uid(&ua_sess->real_credentials)); |
| 295 | if (!reg_uid) { |
| 296 | goto error; |
| 297 | } |
| 298 | registry = reg_uid->registry->reg.ust; |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | assert(0); |
| 303 | }; |
| 304 | |
| 305 | error: |
| 306 | return registry; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Delete ust context safely. RCU read lock must be held before calling |
| 311 | * this function. |
| 312 | */ |
| 313 | static |
| 314 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, |
| 315 | struct ust_app *app) |
| 316 | { |
| 317 | int ret; |
| 318 | |
| 319 | assert(ua_ctx); |
| 320 | |
| 321 | if (ua_ctx->obj) { |
| 322 | pthread_mutex_lock(&app->sock_lock); |
| 323 | ret = ustctl_release_object(sock, ua_ctx->obj); |
| 324 | pthread_mutex_unlock(&app->sock_lock); |
| 325 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 326 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", |
| 327 | sock, ua_ctx->obj->handle, ret); |
| 328 | } |
| 329 | free(ua_ctx->obj); |
| 330 | } |
| 331 | free(ua_ctx); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Delete ust app event safely. RCU read lock must be held before calling |
| 336 | * this function. |
| 337 | */ |
| 338 | static |
| 339 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event, |
| 340 | struct ust_app *app) |
| 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | assert(ua_event); |
| 345 | |
| 346 | free(ua_event->filter); |
| 347 | if (ua_event->exclusion != NULL) |
| 348 | free(ua_event->exclusion); |
| 349 | if (ua_event->obj != NULL) { |
| 350 | pthread_mutex_lock(&app->sock_lock); |
| 351 | ret = ustctl_release_object(sock, ua_event->obj); |
| 352 | pthread_mutex_unlock(&app->sock_lock); |
| 353 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 354 | ERR("UST app sock %d release event obj failed with ret %d", |
| 355 | sock, ret); |
| 356 | } |
| 357 | free(ua_event->obj); |
| 358 | } |
| 359 | free(ua_event); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called |
| 364 | * through a call_rcu(). |
| 365 | */ |
| 366 | static |
| 367 | void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head) |
| 368 | { |
| 369 | struct ust_app_event_notifier_rule *obj = caa_container_of( |
| 370 | head, struct ust_app_event_notifier_rule, rcu_head); |
| 371 | |
| 372 | free(obj); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Delete ust app event notifier rule safely. |
| 377 | */ |
| 378 | static void delete_ust_app_event_notifier_rule(int sock, |
| 379 | struct ust_app_event_notifier_rule *ua_event_notifier_rule, |
| 380 | struct ust_app *app) |
| 381 | { |
| 382 | int ret; |
| 383 | |
| 384 | assert(ua_event_notifier_rule); |
| 385 | |
| 386 | if (ua_event_notifier_rule->exclusion != NULL) { |
| 387 | free(ua_event_notifier_rule->exclusion); |
| 388 | } |
| 389 | |
| 390 | if (ua_event_notifier_rule->obj != NULL) { |
| 391 | pthread_mutex_lock(&app->sock_lock); |
| 392 | ret = ustctl_release_object(sock, ua_event_notifier_rule->obj); |
| 393 | pthread_mutex_unlock(&app->sock_lock); |
| 394 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 395 | ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d", |
| 396 | app->name, (int) app->ppid, ret); |
| 397 | } |
| 398 | |
| 399 | free(ua_event_notifier_rule->obj); |
| 400 | } |
| 401 | |
| 402 | lttng_trigger_put(ua_event_notifier_rule->trigger); |
| 403 | call_rcu(&ua_event_notifier_rule->rcu_head, |
| 404 | free_ust_app_event_notifier_rule_rcu); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Release ust data object of the given stream. |
| 409 | * |
| 410 | * Return 0 on success or else a negative value. |
| 411 | */ |
| 412 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, |
| 413 | struct ust_app *app) |
| 414 | { |
| 415 | int ret = 0; |
| 416 | |
| 417 | assert(stream); |
| 418 | |
| 419 | if (stream->obj) { |
| 420 | pthread_mutex_lock(&app->sock_lock); |
| 421 | ret = ustctl_release_object(sock, stream->obj); |
| 422 | pthread_mutex_unlock(&app->sock_lock); |
| 423 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 424 | ERR("UST app sock %d release stream obj failed with ret %d", |
| 425 | sock, ret); |
| 426 | } |
| 427 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 428 | free(stream->obj); |
| 429 | } |
| 430 | |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Release ust data object of the given map_counter. |
| 436 | * |
| 437 | * Return 0 on success or else a negative value. |
| 438 | */ |
| 439 | static int release_ust_app_map_counter(int sock, struct ust_app_map_counter *map_counter, |
| 440 | struct ust_app *app) |
| 441 | { |
| 442 | int ret = 0; |
| 443 | |
| 444 | assert(map_counter); |
| 445 | |
| 446 | if (map_counter->obj) { |
| 447 | pthread_mutex_lock(&app->sock_lock); |
| 448 | ret = ustctl_release_object(sock, map_counter->obj); |
| 449 | pthread_mutex_unlock(&app->sock_lock); |
| 450 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 451 | ERR("UST app sock %d release map_counter obj failed with ret %d", |
| 452 | sock, ret); |
| 453 | } |
| 454 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 455 | free(map_counter->obj); |
| 456 | } |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Delete ust app stream safely. RCU read lock must be held before calling |
| 463 | * this function. |
| 464 | */ |
| 465 | static |
| 466 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream, |
| 467 | struct ust_app *app) |
| 468 | { |
| 469 | assert(stream); |
| 470 | |
| 471 | (void) release_ust_app_stream(sock, stream, app); |
| 472 | free(stream); |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Delete ust app map_counter safely. RCU read lock must be held before calling |
| 477 | * this function. |
| 478 | */ |
| 479 | static |
| 480 | void delete_ust_app_map_counter(int sock, struct ust_app_map_counter *map_counter, |
| 481 | struct ust_app *app) |
| 482 | { |
| 483 | assert(map_counter); |
| 484 | |
| 485 | (void) release_ust_app_map_counter(sock, map_counter, app); |
| 486 | free(map_counter); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * We need to execute ht_destroy outside of RCU read-side critical |
| 491 | * section and outside of call_rcu thread, so we postpone its execution |
| 492 | * using ht_cleanup_push. It is simpler than to change the semantic of |
| 493 | * the many callers of delete_ust_app_session(). |
| 494 | */ |
| 495 | static |
| 496 | void delete_ust_app_channel_rcu(struct rcu_head *head) |
| 497 | { |
| 498 | struct ust_app_channel *ua_chan = |
| 499 | caa_container_of(head, struct ust_app_channel, rcu_head); |
| 500 | |
| 501 | ht_cleanup_push(ua_chan->ctx); |
| 502 | ht_cleanup_push(ua_chan->events); |
| 503 | free(ua_chan); |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * We need to execute ht_destroy outside of RCU read-side critical |
| 508 | * section and outside of call_rcu thread, so we postpone its execution |
| 509 | * using ht_cleanup_push. It is simpler than to change the semantic of |
| 510 | * the many callers of delete_ust_app_session(). |
| 511 | */ |
| 512 | static |
| 513 | void delete_ust_app_map_rcu(struct rcu_head *head) |
| 514 | { |
| 515 | struct ust_app_map *ua_map = |
| 516 | caa_container_of(head, struct ust_app_map, rcu_head); |
| 517 | |
| 518 | ht_cleanup_push(ua_map->events); |
| 519 | free(ua_map); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Extract the lost packet or discarded events counter when the channel is |
| 524 | * being deleted and store the value in the parent channel so we can |
| 525 | * access it from lttng list and at stop/destroy. |
| 526 | * |
| 527 | * The session list lock must be held by the caller. |
| 528 | */ |
| 529 | static |
| 530 | void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan) |
| 531 | { |
| 532 | uint64_t discarded = 0, lost = 0; |
| 533 | struct ltt_session *session; |
| 534 | struct ltt_ust_channel *uchan; |
| 535 | |
| 536 | if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | rcu_read_lock(); |
| 541 | session = session_find_by_id(ua_chan->session->tracing_id); |
| 542 | if (!session || !session->ust_session) { |
| 543 | /* |
| 544 | * Not finding the session is not an error because there are |
| 545 | * multiple ways the channels can be torn down. |
| 546 | * |
| 547 | * 1) The session daemon can initiate the destruction of the |
| 548 | * ust app session after receiving a destroy command or |
| 549 | * during its shutdown/teardown. |
| 550 | * 2) The application, since we are in per-pid tracing, is |
| 551 | * unregistering and tearing down its ust app session. |
| 552 | * |
| 553 | * Both paths are protected by the session list lock which |
| 554 | * ensures that the accounting of lost packets and discarded |
| 555 | * events is done exactly once. The session is then unpublished |
| 556 | * from the session list, resulting in this condition. |
| 557 | */ |
| 558 | goto end; |
| 559 | } |
| 560 | |
| 561 | if (ua_chan->attr.overwrite) { |
| 562 | consumer_get_lost_packets(ua_chan->session->tracing_id, |
| 563 | ua_chan->key, session->ust_session->consumer, |
| 564 | &lost); |
| 565 | } else { |
| 566 | consumer_get_discarded_events(ua_chan->session->tracing_id, |
| 567 | ua_chan->key, session->ust_session->consumer, |
| 568 | &discarded); |
| 569 | } |
| 570 | uchan = trace_ust_find_channel_by_name( |
| 571 | session->ust_session->domain_global.channels, |
| 572 | ua_chan->name); |
| 573 | if (!uchan) { |
| 574 | ERR("Missing UST channel to store discarded counters"); |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | uchan->per_pid_closed_app_discarded += discarded; |
| 579 | uchan->per_pid_closed_app_lost += lost; |
| 580 | |
| 581 | end: |
| 582 | rcu_read_unlock(); |
| 583 | if (session) { |
| 584 | session_put(session); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Delete ust app channel safely. RCU read lock must be held before calling |
| 590 | * this function. |
| 591 | * |
| 592 | * The session list lock must be held by the caller. |
| 593 | */ |
| 594 | static |
| 595 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
| 596 | struct ust_app *app) |
| 597 | { |
| 598 | int ret; |
| 599 | struct lttng_ht_iter iter; |
| 600 | struct ust_app_event *ua_event; |
| 601 | struct ust_app_ctx *ua_ctx; |
| 602 | struct ust_app_stream *stream, *stmp; |
| 603 | struct ust_registry_session *registry; |
| 604 | |
| 605 | assert(ua_chan); |
| 606 | |
| 607 | DBG3("UST app deleting channel %s", ua_chan->name); |
| 608 | |
| 609 | /* Wipe stream */ |
| 610 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 611 | cds_list_del(&stream->list); |
| 612 | delete_ust_app_stream(sock, stream, app); |
| 613 | } |
| 614 | |
| 615 | /* Wipe context */ |
| 616 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
| 617 | cds_list_del(&ua_ctx->list); |
| 618 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
| 619 | assert(!ret); |
| 620 | delete_ust_app_ctx(sock, ua_ctx, app); |
| 621 | } |
| 622 | |
| 623 | /* Wipe events */ |
| 624 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
| 625 | node.node) { |
| 626 | ret = lttng_ht_del(ua_chan->events, &iter); |
| 627 | assert(!ret); |
| 628 | delete_ust_app_event(sock, ua_event, app); |
| 629 | } |
| 630 | |
| 631 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 632 | /* Wipe and free registry from session registry. */ |
| 633 | registry = get_session_registry(ua_chan->session); |
| 634 | if (registry) { |
| 635 | ust_registry_channel_del_free(registry, ua_chan->key, |
| 636 | sock >= 0); |
| 637 | } |
| 638 | /* |
| 639 | * A negative socket can be used by the caller when |
| 640 | * cleaning-up a ua_chan in an error path. Skip the |
| 641 | * accounting in this case. |
| 642 | */ |
| 643 | if (sock >= 0) { |
| 644 | save_per_pid_lost_discarded_counters(ua_chan); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (ua_chan->obj != NULL) { |
| 649 | /* Remove channel from application UST object descriptor. */ |
| 650 | iter.iter.node = &ua_chan->ust_objd_node.node; |
| 651 | ret = lttng_ht_del(app->ust_chan_objd, &iter); |
| 652 | assert(!ret); |
| 653 | pthread_mutex_lock(&app->sock_lock); |
| 654 | ret = ustctl_release_object(sock, ua_chan->obj); |
| 655 | pthread_mutex_unlock(&app->sock_lock); |
| 656 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 657 | ERR("UST app sock %d release channel obj failed with ret %d", |
| 658 | sock, ret); |
| 659 | } |
| 660 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 661 | free(ua_chan->obj); |
| 662 | } |
| 663 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
| 664 | } |
| 665 | |
| 666 | static |
| 667 | void copy_ust_app_map_values(int sock, struct ust_app_map *ua_map, |
| 668 | struct ust_app *app) |
| 669 | { |
| 670 | struct ltt_ust_map_dead_pid_kv_values *kv_pair_list = ua_map->dead_app_kv_values; |
| 671 | struct ust_registry_session *ust_reg_sess; |
| 672 | struct ust_registry_map *ust_reg_map; |
| 673 | struct ust_registry_map_index_ht_entry *map_index_entry; |
| 674 | struct lttng_ht_iter key_iter; |
| 675 | struct lttng_ht *dead_app_kv_values; |
| 676 | |
| 677 | assert(app->buffer_type == LTTNG_BUFFER_PER_PID); |
| 678 | ust_reg_sess = get_session_registry(ua_map->session); |
| 679 | |
| 680 | pthread_mutex_lock(&ust_reg_sess->lock); |
| 681 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); |
| 682 | pthread_mutex_unlock(&ust_reg_sess->lock); |
| 683 | assert(ust_reg_map); |
| 684 | |
| 685 | DBG("Aggregating dead map values"); |
| 686 | |
| 687 | pthread_mutex_lock(&kv_pair_list->lock); |
| 688 | |
| 689 | if (app->bits_per_long == 32) { |
| 690 | dead_app_kv_values = kv_pair_list->dead_app_kv_values_32bits; |
| 691 | } else { |
| 692 | dead_app_kv_values = kv_pair_list->dead_app_kv_values_64bits; |
| 693 | } |
| 694 | |
| 695 | /* Iterate over all the formated_key -> counter index */ |
| 696 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, |
| 697 | &key_iter.iter, map_index_entry, node.node) { |
| 698 | bool overflow = 0, underflow = 0; |
| 699 | int64_t local_value = 0; |
| 700 | int ret; |
| 701 | size_t dimension_indexes[1] = {map_index_entry->index}; |
| 702 | |
| 703 | ret = ustctl_counter_aggregate(ua_map->map_handle, |
| 704 | dimension_indexes, &local_value, &overflow, |
| 705 | &underflow); |
| 706 | if (ret) { |
| 707 | ERR("Error getting counter value from the tracer: key = '%s'", |
| 708 | map_index_entry->formated_key); |
| 709 | ret = -1; |
| 710 | goto end; |
| 711 | } |
| 712 | |
| 713 | map_add_or_increment_map_values(dead_app_kv_values, |
| 714 | map_index_entry->formated_key, local_value, |
| 715 | underflow, overflow); |
| 716 | |
| 717 | } |
| 718 | |
| 719 | end: |
| 720 | pthread_mutex_unlock(&kv_pair_list->lock); |
| 721 | return; |
| 722 | } |
| 723 | /* |
| 724 | * Delete ust app map safely. RCU read lock must be held before calling |
| 725 | * this function. |
| 726 | * |
| 727 | * The session list lock must be held by the caller. |
| 728 | */ |
| 729 | static |
| 730 | void delete_ust_app_map(int sock, struct ust_app_map *ua_map, |
| 731 | struct ust_app *app) |
| 732 | { |
| 733 | int ret; |
| 734 | struct lttng_ht_iter iter; |
| 735 | struct ust_app_event *ua_event; |
| 736 | struct ust_app_map_counter *map_counter, *ctmp; |
| 737 | struct ust_registry_session *registry; |
| 738 | |
| 739 | assert(ua_map); |
| 740 | |
| 741 | DBG3("UST app deleting map %s", ua_map->name); |
| 742 | |
| 743 | /* Wipe stream */ |
| 744 | cds_list_for_each_entry_safe(map_counter, ctmp, &ua_map->counters.head, list) { |
| 745 | cds_list_del(&map_counter->list); |
| 746 | delete_ust_app_map_counter(sock, map_counter, app); |
| 747 | } |
| 748 | |
| 749 | /* Wipe events */ |
| 750 | cds_lfht_for_each_entry(ua_map->events->ht, &iter.iter, ua_event, |
| 751 | node.node) { |
| 752 | ret = lttng_ht_del(ua_map->events, &iter); |
| 753 | assert(!ret); |
| 754 | delete_ust_app_event(sock, ua_event, app); |
| 755 | } |
| 756 | |
| 757 | if (ua_map->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 758 | /* Wipe and free registry from session registry. */ |
| 759 | registry = get_session_registry(ua_map->session); |
| 760 | if (registry) { |
| 761 | ust_registry_map_del_free(registry, ua_map->key); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (ua_map->obj != NULL) { |
| 766 | /* Remove map from application UST object descriptor. */ |
| 767 | iter.iter.node = &ua_map->ust_objd_node.node; |
| 768 | ret = lttng_ht_del(app->ust_map_objd, &iter); |
| 769 | assert(!ret); |
| 770 | pthread_mutex_lock(&app->sock_lock); |
| 771 | ret = ustctl_release_object(sock, ua_map->obj); |
| 772 | pthread_mutex_unlock(&app->sock_lock); |
| 773 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 774 | ERR("UST app sock %d release map obj failed with ret %d", |
| 775 | sock, ret); |
| 776 | } |
| 777 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 778 | free(ua_map->obj); |
| 779 | } |
| 780 | call_rcu(&ua_map->rcu_head, delete_ust_app_map_rcu); |
| 781 | } |
| 782 | |
| 783 | int ust_app_register_done(struct ust_app *app) |
| 784 | { |
| 785 | int ret; |
| 786 | |
| 787 | pthread_mutex_lock(&app->sock_lock); |
| 788 | ret = ustctl_register_done(app->sock); |
| 789 | pthread_mutex_unlock(&app->sock_lock); |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data) |
| 794 | { |
| 795 | int ret, sock; |
| 796 | |
| 797 | if (app) { |
| 798 | pthread_mutex_lock(&app->sock_lock); |
| 799 | sock = app->sock; |
| 800 | } else { |
| 801 | sock = -1; |
| 802 | } |
| 803 | ret = ustctl_release_object(sock, data); |
| 804 | if (app) { |
| 805 | pthread_mutex_unlock(&app->sock_lock); |
| 806 | } |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Push metadata to consumer socket. |
| 812 | * |
| 813 | * RCU read-side lock must be held to guarantee existance of socket. |
| 814 | * Must be called with the ust app session lock held. |
| 815 | * Must be called with the registry lock held. |
| 816 | * |
| 817 | * On success, return the len of metadata pushed or else a negative value. |
| 818 | * Returning a -EPIPE return value means we could not send the metadata, |
| 819 | * but it can be caused by recoverable errors (e.g. the application has |
| 820 | * terminated concurrently). |
| 821 | */ |
| 822 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, |
| 823 | struct consumer_socket *socket, int send_zero_data) |
| 824 | { |
| 825 | int ret; |
| 826 | char *metadata_str = NULL; |
| 827 | size_t len, offset, new_metadata_len_sent; |
| 828 | ssize_t ret_val; |
| 829 | uint64_t metadata_key, metadata_version; |
| 830 | |
| 831 | assert(registry); |
| 832 | assert(socket); |
| 833 | |
| 834 | metadata_key = registry->metadata_key; |
| 835 | |
| 836 | /* |
| 837 | * Means that no metadata was assigned to the session. This can |
| 838 | * happens if no start has been done previously. |
| 839 | */ |
| 840 | if (!metadata_key) { |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | offset = registry->metadata_len_sent; |
| 845 | len = registry->metadata_len - registry->metadata_len_sent; |
| 846 | new_metadata_len_sent = registry->metadata_len; |
| 847 | metadata_version = registry->metadata_version; |
| 848 | if (len == 0) { |
| 849 | DBG3("No metadata to push for metadata key %" PRIu64, |
| 850 | registry->metadata_key); |
| 851 | ret_val = len; |
| 852 | if (send_zero_data) { |
| 853 | DBG("No metadata to push"); |
| 854 | goto push_data; |
| 855 | } |
| 856 | goto end; |
| 857 | } |
| 858 | |
| 859 | /* Allocate only what we have to send. */ |
| 860 | metadata_str = zmalloc(len); |
| 861 | if (!metadata_str) { |
| 862 | PERROR("zmalloc ust app metadata string"); |
| 863 | ret_val = -ENOMEM; |
| 864 | goto error; |
| 865 | } |
| 866 | /* Copy what we haven't sent out. */ |
| 867 | memcpy(metadata_str, registry->metadata + offset, len); |
| 868 | |
| 869 | push_data: |
| 870 | pthread_mutex_unlock(®istry->lock); |
| 871 | /* |
| 872 | * We need to unlock the registry while we push metadata to |
| 873 | * break a circular dependency between the consumerd metadata |
| 874 | * lock and the sessiond registry lock. Indeed, pushing metadata |
| 875 | * to the consumerd awaits that it gets pushed all the way to |
| 876 | * relayd, but doing so requires grabbing the metadata lock. If |
| 877 | * a concurrent metadata request is being performed by |
| 878 | * consumerd, this can try to grab the registry lock on the |
| 879 | * sessiond while holding the metadata lock on the consumer |
| 880 | * daemon. Those push and pull schemes are performed on two |
| 881 | * different bidirectionnal communication sockets. |
| 882 | */ |
| 883 | ret = consumer_push_metadata(socket, metadata_key, |
| 884 | metadata_str, len, offset, metadata_version); |
| 885 | pthread_mutex_lock(®istry->lock); |
| 886 | if (ret < 0) { |
| 887 | /* |
| 888 | * There is an acceptable race here between the registry |
| 889 | * metadata key assignment and the creation on the |
| 890 | * consumer. The session daemon can concurrently push |
| 891 | * metadata for this registry while being created on the |
| 892 | * consumer since the metadata key of the registry is |
| 893 | * assigned *before* it is setup to avoid the consumer |
| 894 | * to ask for metadata that could possibly be not found |
| 895 | * in the session daemon. |
| 896 | * |
| 897 | * The metadata will get pushed either by the session |
| 898 | * being stopped or the consumer requesting metadata if |
| 899 | * that race is triggered. |
| 900 | */ |
| 901 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { |
| 902 | ret = 0; |
| 903 | } else { |
| 904 | ERR("Error pushing metadata to consumer"); |
| 905 | } |
| 906 | ret_val = ret; |
| 907 | goto error_push; |
| 908 | } else { |
| 909 | /* |
| 910 | * Metadata may have been concurrently pushed, since |
| 911 | * we're not holding the registry lock while pushing to |
| 912 | * consumer. This is handled by the fact that we send |
| 913 | * the metadata content, size, and the offset at which |
| 914 | * that metadata belongs. This may arrive out of order |
| 915 | * on the consumer side, and the consumer is able to |
| 916 | * deal with overlapping fragments. The consumer |
| 917 | * supports overlapping fragments, which must be |
| 918 | * contiguous starting from offset 0. We keep the |
| 919 | * largest metadata_len_sent value of the concurrent |
| 920 | * send. |
| 921 | */ |
| 922 | registry->metadata_len_sent = |
| 923 | max_t(size_t, registry->metadata_len_sent, |
| 924 | new_metadata_len_sent); |
| 925 | } |
| 926 | free(metadata_str); |
| 927 | return len; |
| 928 | |
| 929 | end: |
| 930 | error: |
| 931 | if (ret_val) { |
| 932 | /* |
| 933 | * On error, flag the registry that the metadata is |
| 934 | * closed. We were unable to push anything and this |
| 935 | * means that either the consumer is not responding or |
| 936 | * the metadata cache has been destroyed on the |
| 937 | * consumer. |
| 938 | */ |
| 939 | registry->metadata_closed = 1; |
| 940 | } |
| 941 | error_push: |
| 942 | free(metadata_str); |
| 943 | return ret_val; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * For a given application and session, push metadata to consumer. |
| 948 | * Either sock or consumer is required : if sock is NULL, the default |
| 949 | * socket to send the metadata is retrieved from consumer, if sock |
| 950 | * is not NULL we use it to send the metadata. |
| 951 | * RCU read-side lock must be held while calling this function, |
| 952 | * therefore ensuring existance of registry. It also ensures existance |
| 953 | * of socket throughout this function. |
| 954 | * |
| 955 | * Return 0 on success else a negative error. |
| 956 | * Returning a -EPIPE return value means we could not send the metadata, |
| 957 | * but it can be caused by recoverable errors (e.g. the application has |
| 958 | * terminated concurrently). |
| 959 | */ |
| 960 | static int push_metadata(struct ust_registry_session *registry, |
| 961 | struct consumer_output *consumer) |
| 962 | { |
| 963 | int ret_val; |
| 964 | ssize_t ret; |
| 965 | struct consumer_socket *socket; |
| 966 | |
| 967 | assert(registry); |
| 968 | assert(consumer); |
| 969 | |
| 970 | pthread_mutex_lock(®istry->lock); |
| 971 | if (registry->metadata_closed) { |
| 972 | ret_val = -EPIPE; |
| 973 | goto error; |
| 974 | } |
| 975 | |
| 976 | /* Get consumer socket to use to push the metadata.*/ |
| 977 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
| 978 | consumer); |
| 979 | if (!socket) { |
| 980 | ret_val = -1; |
| 981 | goto error; |
| 982 | } |
| 983 | |
| 984 | ret = ust_app_push_metadata(registry, socket, 0); |
| 985 | if (ret < 0) { |
| 986 | ret_val = ret; |
| 987 | goto error; |
| 988 | } |
| 989 | pthread_mutex_unlock(®istry->lock); |
| 990 | return 0; |
| 991 | |
| 992 | error: |
| 993 | pthread_mutex_unlock(®istry->lock); |
| 994 | return ret_val; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Send to the consumer a close metadata command for the given session. Once |
| 999 | * done, the metadata channel is deleted and the session metadata pointer is |
| 1000 | * nullified. The session lock MUST be held unless the application is |
| 1001 | * in the destroy path. |
| 1002 | * |
| 1003 | * Do not hold the registry lock while communicating with the consumerd, because |
| 1004 | * doing so causes inter-process deadlocks between consumerd and sessiond with |
| 1005 | * the metadata request notification. |
| 1006 | * |
| 1007 | * Return 0 on success else a negative value. |
| 1008 | */ |
| 1009 | static int close_metadata(struct ust_registry_session *registry, |
| 1010 | struct consumer_output *consumer) |
| 1011 | { |
| 1012 | int ret; |
| 1013 | struct consumer_socket *socket; |
| 1014 | uint64_t metadata_key; |
| 1015 | bool registry_was_already_closed; |
| 1016 | |
| 1017 | assert(registry); |
| 1018 | assert(consumer); |
| 1019 | |
| 1020 | rcu_read_lock(); |
| 1021 | |
| 1022 | pthread_mutex_lock(®istry->lock); |
| 1023 | metadata_key = registry->metadata_key; |
| 1024 | registry_was_already_closed = registry->metadata_closed; |
| 1025 | if (metadata_key != 0) { |
| 1026 | /* |
| 1027 | * Metadata closed. Even on error this means that the consumer |
| 1028 | * is not responding or not found so either way a second close |
| 1029 | * should NOT be emit for this registry. |
| 1030 | */ |
| 1031 | registry->metadata_closed = 1; |
| 1032 | } |
| 1033 | pthread_mutex_unlock(®istry->lock); |
| 1034 | |
| 1035 | if (metadata_key == 0 || registry_was_already_closed) { |
| 1036 | ret = 0; |
| 1037 | goto end; |
| 1038 | } |
| 1039 | |
| 1040 | /* Get consumer socket to use to push the metadata.*/ |
| 1041 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
| 1042 | consumer); |
| 1043 | if (!socket) { |
| 1044 | ret = -1; |
| 1045 | goto end; |
| 1046 | } |
| 1047 | |
| 1048 | ret = consumer_close_metadata(socket, metadata_key); |
| 1049 | if (ret < 0) { |
| 1050 | goto end; |
| 1051 | } |
| 1052 | |
| 1053 | end: |
| 1054 | rcu_read_unlock(); |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * We need to execute ht_destroy outside of RCU read-side critical |
| 1060 | * section and outside of call_rcu thread, so we postpone its execution |
| 1061 | * using ht_cleanup_push. It is simpler than to change the semantic of |
| 1062 | * the many callers of delete_ust_app_session(). |
| 1063 | */ |
| 1064 | static |
| 1065 | void delete_ust_app_session_rcu(struct rcu_head *head) |
| 1066 | { |
| 1067 | struct ust_app_session *ua_sess = |
| 1068 | caa_container_of(head, struct ust_app_session, rcu_head); |
| 1069 | |
| 1070 | ht_cleanup_push(ua_sess->channels); |
| 1071 | ht_cleanup_push(ua_sess->maps); |
| 1072 | free(ua_sess); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Delete ust app session safely. RCU read lock must be held before calling |
| 1077 | * this function. |
| 1078 | * |
| 1079 | * The session list lock must be held by the caller. |
| 1080 | */ |
| 1081 | static |
| 1082 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
| 1083 | struct ust_app *app) |
| 1084 | { |
| 1085 | int ret; |
| 1086 | struct lttng_ht_iter iter; |
| 1087 | struct ust_app_channel *ua_chan; |
| 1088 | struct ust_app_map *ua_map; |
| 1089 | struct ust_registry_session *registry; |
| 1090 | |
| 1091 | assert(ua_sess); |
| 1092 | |
| 1093 | pthread_mutex_lock(&ua_sess->lock); |
| 1094 | |
| 1095 | assert(!ua_sess->deleted); |
| 1096 | ua_sess->deleted = true; |
| 1097 | |
| 1098 | registry = get_session_registry(ua_sess); |
| 1099 | /* Registry can be null on error path during initialization. */ |
| 1100 | if (registry) { |
| 1101 | /* Push metadata for application before freeing the application. */ |
| 1102 | (void) push_metadata(registry, ua_sess->consumer); |
| 1103 | |
| 1104 | /* |
| 1105 | * Don't ask to close metadata for global per UID buffers. Close |
| 1106 | * metadata only on destroy trace session in this case. Also, the |
| 1107 | * previous push metadata could have flag the metadata registry to |
| 1108 | * close so don't send a close command if closed. |
| 1109 | */ |
| 1110 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 1111 | /* And ask to close it for this session registry. */ |
| 1112 | (void) close_metadata(registry, ua_sess->consumer); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 1117 | node.node) { |
| 1118 | ret = lttng_ht_del(ua_sess->channels, &iter); |
| 1119 | assert(!ret); |
| 1120 | delete_ust_app_channel(sock, ua_chan, app); |
| 1121 | } |
| 1122 | |
| 1123 | cds_lfht_for_each_entry(ua_sess->maps->ht, &iter.iter, ua_map, |
| 1124 | node.node) { |
| 1125 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 1126 | copy_ust_app_map_values(sock, ua_map, app); |
| 1127 | } |
| 1128 | ret = lttng_ht_del(ua_sess->maps, &iter); |
| 1129 | assert(!ret); |
| 1130 | delete_ust_app_map(sock, ua_map, app); |
| 1131 | } |
| 1132 | |
| 1133 | /* In case of per PID, the registry is kept in the session. */ |
| 1134 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 1135 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 1136 | if (reg_pid) { |
| 1137 | /* |
| 1138 | * Registry can be null on error path during |
| 1139 | * initialization. |
| 1140 | */ |
| 1141 | buffer_reg_pid_remove(reg_pid); |
| 1142 | buffer_reg_pid_destroy(reg_pid); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | if (ua_sess->handle != -1) { |
| 1147 | pthread_mutex_lock(&app->sock_lock); |
| 1148 | ret = ustctl_release_handle(sock, ua_sess->handle); |
| 1149 | pthread_mutex_unlock(&app->sock_lock); |
| 1150 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1151 | ERR("UST app sock %d release session handle failed with ret %d", |
| 1152 | sock, ret); |
| 1153 | } |
| 1154 | /* Remove session from application UST object descriptor. */ |
| 1155 | iter.iter.node = &ua_sess->ust_objd_node.node; |
| 1156 | ret = lttng_ht_del(app->ust_sessions_objd, &iter); |
| 1157 | assert(!ret); |
| 1158 | } |
| 1159 | |
| 1160 | pthread_mutex_unlock(&ua_sess->lock); |
| 1161 | |
| 1162 | consumer_output_put(ua_sess->consumer); |
| 1163 | |
| 1164 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * Delete a traceable application structure from the global list. Never call |
| 1169 | * this function outside of a call_rcu call. |
| 1170 | * |
| 1171 | * RCU read side lock should _NOT_ be held when calling this function. |
| 1172 | */ |
| 1173 | static |
| 1174 | void delete_ust_app(struct ust_app *app) |
| 1175 | { |
| 1176 | int ret, sock; |
| 1177 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
| 1178 | struct lttng_ht_iter iter; |
| 1179 | struct ust_app_event_notifier_rule *event_notifier_rule; |
| 1180 | bool event_notifier_write_fd_is_open; |
| 1181 | |
| 1182 | /* |
| 1183 | * The session list lock must be held during this function to guarantee |
| 1184 | * the existence of ua_sess. |
| 1185 | */ |
| 1186 | session_lock_list(); |
| 1187 | /* Delete ust app sessions info */ |
| 1188 | sock = app->sock; |
| 1189 | app->sock = -1; |
| 1190 | |
| 1191 | /* Wipe sessions */ |
| 1192 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
| 1193 | teardown_node) { |
| 1194 | /* Free every object in the session and the session. */ |
| 1195 | rcu_read_lock(); |
| 1196 | delete_ust_app_session(sock, ua_sess, app); |
| 1197 | rcu_read_unlock(); |
| 1198 | } |
| 1199 | |
| 1200 | /* Remove the event notifier rules associated with this app. */ |
| 1201 | rcu_read_lock(); |
| 1202 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, |
| 1203 | &iter.iter, event_notifier_rule, node.node) { |
| 1204 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter); |
| 1205 | assert(!ret); |
| 1206 | |
| 1207 | delete_ust_app_event_notifier_rule( |
| 1208 | app->sock, event_notifier_rule, app); |
| 1209 | } |
| 1210 | |
| 1211 | rcu_read_unlock(); |
| 1212 | |
| 1213 | ht_cleanup_push(app->sessions); |
| 1214 | ht_cleanup_push(app->ust_sessions_objd); |
| 1215 | ht_cleanup_push(app->ust_chan_objd); |
| 1216 | ht_cleanup_push(app->ust_map_objd); |
| 1217 | ht_cleanup_push(app->token_to_event_notifier_rule_ht); |
| 1218 | |
| 1219 | /* |
| 1220 | * This could be NULL if the event notifier setup failed (e.g the app |
| 1221 | * was killed or the tracer does not support this feature). |
| 1222 | */ |
| 1223 | if (app->event_notifier_group.object) { |
| 1224 | enum lttng_error_code ret_code; |
| 1225 | enum event_notifier_error_accounting_status status; |
| 1226 | |
| 1227 | const int event_notifier_read_fd = lttng_pipe_get_readfd( |
| 1228 | app->event_notifier_group.event_pipe); |
| 1229 | |
| 1230 | ret_code = notification_thread_command_remove_tracer_event_source( |
| 1231 | notification_thread_handle, |
| 1232 | event_notifier_read_fd); |
| 1233 | if (ret_code != LTTNG_OK) { |
| 1234 | ERR("Failed to remove application tracer event source from notification thread"); |
| 1235 | } |
| 1236 | |
| 1237 | status = event_notifier_error_accounting_unregister_app(app); |
| 1238 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 1239 | ERR("Error unregistering app from event notifier error accounting"); |
| 1240 | } |
| 1241 | |
| 1242 | ustctl_release_object(sock, app->event_notifier_group.object); |
| 1243 | free(app->event_notifier_group.object); |
| 1244 | } |
| 1245 | |
| 1246 | event_notifier_write_fd_is_open = lttng_pipe_is_write_open( |
| 1247 | app->event_notifier_group.event_pipe); |
| 1248 | lttng_pipe_destroy(app->event_notifier_group.event_pipe); |
| 1249 | /* |
| 1250 | * Release the file descriptors reserved for the event notifier pipe. |
| 1251 | * The app could be destroyed before the write end of the pipe could be |
| 1252 | * passed to the application (and closed). In that case, both file |
| 1253 | * descriptors must be released. |
| 1254 | */ |
| 1255 | lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1); |
| 1256 | |
| 1257 | /* |
| 1258 | * Wait until we have deleted the application from the sock hash table |
| 1259 | * before closing this socket, otherwise an application could re-use the |
| 1260 | * socket ID and race with the teardown, using the same hash table entry. |
| 1261 | * |
| 1262 | * It's OK to leave the close in call_rcu. We want it to stay unique for |
| 1263 | * all RCU readers that could run concurrently with unregister app, |
| 1264 | * therefore we _need_ to only close that socket after a grace period. So |
| 1265 | * it should stay in this RCU callback. |
| 1266 | * |
| 1267 | * This close() is a very important step of the synchronization model so |
| 1268 | * every modification to this function must be carefully reviewed. |
| 1269 | */ |
| 1270 | ret = close(sock); |
| 1271 | if (ret) { |
| 1272 | PERROR("close"); |
| 1273 | } |
| 1274 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1275 | |
| 1276 | DBG2("UST app pid %d deleted", app->pid); |
| 1277 | free(app); |
| 1278 | session_unlock_list(); |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * URCU intermediate call to delete an UST app. |
| 1283 | */ |
| 1284 | static |
| 1285 | void delete_ust_app_rcu(struct rcu_head *head) |
| 1286 | { |
| 1287 | struct lttng_ht_node_ulong *node = |
| 1288 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 1289 | struct ust_app *app = |
| 1290 | caa_container_of(node, struct ust_app, pid_n); |
| 1291 | |
| 1292 | DBG3("Call RCU deleting app PID %d", app->pid); |
| 1293 | delete_ust_app(app); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Delete the session from the application ht and delete the data structure by |
| 1298 | * freeing every object inside and releasing them. |
| 1299 | * |
| 1300 | * The session list lock must be held by the caller. |
| 1301 | */ |
| 1302 | static void destroy_app_session(struct ust_app *app, |
| 1303 | struct ust_app_session *ua_sess) |
| 1304 | { |
| 1305 | int ret; |
| 1306 | struct lttng_ht_iter iter; |
| 1307 | |
| 1308 | assert(app); |
| 1309 | assert(ua_sess); |
| 1310 | |
| 1311 | iter.iter.node = &ua_sess->node.node; |
| 1312 | ret = lttng_ht_del(app->sessions, &iter); |
| 1313 | if (ret) { |
| 1314 | /* Already scheduled for teardown. */ |
| 1315 | goto end; |
| 1316 | } |
| 1317 | |
| 1318 | /* Once deleted, free the data structure. */ |
| 1319 | delete_ust_app_session(app->sock, ua_sess, app); |
| 1320 | |
| 1321 | end: |
| 1322 | return; |
| 1323 | } |
| 1324 | |
| 1325 | /* |
| 1326 | * Alloc new UST app session. |
| 1327 | */ |
| 1328 | static |
| 1329 | struct ust_app_session *alloc_ust_app_session(void) |
| 1330 | { |
| 1331 | struct ust_app_session *ua_sess; |
| 1332 | |
| 1333 | /* Init most of the default value by allocating and zeroing */ |
| 1334 | ua_sess = zmalloc(sizeof(struct ust_app_session)); |
| 1335 | if (ua_sess == NULL) { |
| 1336 | PERROR("malloc"); |
| 1337 | goto error_free; |
| 1338 | } |
| 1339 | |
| 1340 | ua_sess->handle = -1; |
| 1341 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1342 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
| 1343 | ua_sess->maps = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1344 | pthread_mutex_init(&ua_sess->lock, NULL); |
| 1345 | |
| 1346 | return ua_sess; |
| 1347 | |
| 1348 | error_free: |
| 1349 | return NULL; |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * Alloc new UST app channel. |
| 1354 | */ |
| 1355 | static |
| 1356 | struct ust_app_channel *alloc_ust_app_channel(const char *name, |
| 1357 | struct ust_app_session *ua_sess, |
| 1358 | struct lttng_ust_channel_attr *attr) |
| 1359 | { |
| 1360 | struct ust_app_channel *ua_chan; |
| 1361 | |
| 1362 | /* Init most of the default value by allocating and zeroing */ |
| 1363 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); |
| 1364 | if (ua_chan == NULL) { |
| 1365 | PERROR("malloc"); |
| 1366 | goto error; |
| 1367 | } |
| 1368 | |
| 1369 | /* Setup channel name */ |
| 1370 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); |
| 1371 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 1372 | |
| 1373 | ua_chan->enabled = 1; |
| 1374 | ua_chan->handle = -1; |
| 1375 | ua_chan->session = ua_sess; |
| 1376 | ua_chan->key = get_next_channel_key(); |
| 1377 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1378 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1379 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); |
| 1380 | |
| 1381 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); |
| 1382 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
| 1383 | |
| 1384 | /* Copy attributes */ |
| 1385 | if (attr) { |
| 1386 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
| 1387 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
| 1388 | ua_chan->attr.num_subbuf = attr->num_subbuf; |
| 1389 | ua_chan->attr.overwrite = attr->overwrite; |
| 1390 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; |
| 1391 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; |
| 1392 | ua_chan->attr.output = attr->output; |
| 1393 | ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout; |
| 1394 | } |
| 1395 | /* By default, the channel is a per cpu channel. */ |
| 1396 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; |
| 1397 | |
| 1398 | DBG3("UST app channel %s allocated", ua_chan->name); |
| 1399 | |
| 1400 | return ua_chan; |
| 1401 | |
| 1402 | error: |
| 1403 | return NULL; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Alloc new UST app map. |
| 1408 | */ |
| 1409 | static |
| 1410 | struct ust_app_map *alloc_ust_app_map(const char *name, |
| 1411 | struct ust_app_session *ua_sess) |
| 1412 | { |
| 1413 | struct ust_app_map *ua_map; |
| 1414 | |
| 1415 | /* Init most of the default value by allocating and zeroing */ |
| 1416 | ua_map = zmalloc(sizeof(struct ust_app_map)); |
| 1417 | if (ua_map == NULL) { |
| 1418 | PERROR("malloc"); |
| 1419 | goto error; |
| 1420 | } |
| 1421 | |
| 1422 | /* Setup map name */ |
| 1423 | strncpy(ua_map->name, name, sizeof(ua_map->name)); |
| 1424 | ua_map->name[sizeof(ua_map->name) - 1] = '\0'; |
| 1425 | |
| 1426 | ua_map->enabled = 1; |
| 1427 | ua_map->handle = -1; |
| 1428 | ua_map->session = ua_sess; |
| 1429 | ua_map->key = get_next_map_key(); |
| 1430 | ua_map->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 1431 | lttng_ht_node_init_str(&ua_map->node, ua_map->name); |
| 1432 | |
| 1433 | CDS_INIT_LIST_HEAD(&ua_map->counters.head); |
| 1434 | |
| 1435 | DBG3("UST app map %s allocated", ua_map->name); |
| 1436 | |
| 1437 | return ua_map; |
| 1438 | |
| 1439 | error: |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * Allocate and initialize a UST app stream. |
| 1445 | * |
| 1446 | * Return newly allocated stream pointer or NULL on error. |
| 1447 | */ |
| 1448 | struct ust_app_stream *ust_app_alloc_stream(void) |
| 1449 | { |
| 1450 | struct ust_app_stream *stream = NULL; |
| 1451 | |
| 1452 | stream = zmalloc(sizeof(*stream)); |
| 1453 | if (stream == NULL) { |
| 1454 | PERROR("zmalloc ust app stream"); |
| 1455 | goto error; |
| 1456 | } |
| 1457 | |
| 1458 | /* Zero could be a valid value for a handle so flag it to -1. */ |
| 1459 | stream->handle = -1; |
| 1460 | |
| 1461 | error: |
| 1462 | return stream; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Allocate and initialize a UST app map_counter. |
| 1467 | * |
| 1468 | * Return newly allocated map_counter pointer or NULL on error. |
| 1469 | */ |
| 1470 | struct ust_app_map_counter *ust_app_alloc_map_counter(void) |
| 1471 | { |
| 1472 | struct ust_app_map_counter *map_counter = NULL; |
| 1473 | |
| 1474 | map_counter = zmalloc(sizeof(*map_counter)); |
| 1475 | if (map_counter == NULL) { |
| 1476 | PERROR("zmalloc ust app map_counter"); |
| 1477 | goto error; |
| 1478 | } |
| 1479 | |
| 1480 | /* Zero could be a valid value for a handle so flag it to -1. */ |
| 1481 | map_counter->handle = -1; |
| 1482 | |
| 1483 | error: |
| 1484 | return map_counter; |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * Alloc new UST app event. |
| 1489 | */ |
| 1490 | static |
| 1491 | struct ust_app_event *alloc_ust_app_event(char *name, |
| 1492 | struct lttng_ust_event *attr) |
| 1493 | { |
| 1494 | struct ust_app_event *ua_event; |
| 1495 | |
| 1496 | /* Init most of the default value by allocating and zeroing */ |
| 1497 | ua_event = zmalloc(sizeof(struct ust_app_event)); |
| 1498 | if (ua_event == NULL) { |
| 1499 | PERROR("Failed to allocate ust_app_event structure"); |
| 1500 | goto error; |
| 1501 | } |
| 1502 | |
| 1503 | ua_event->enabled = 1; |
| 1504 | strncpy(ua_event->name, name, sizeof(ua_event->name)); |
| 1505 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 1506 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
| 1507 | |
| 1508 | /* Copy attributes */ |
| 1509 | if (attr) { |
| 1510 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); |
| 1511 | } |
| 1512 | |
| 1513 | DBG3("UST app event %s allocated", ua_event->name); |
| 1514 | |
| 1515 | return ua_event; |
| 1516 | |
| 1517 | error: |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | /* |
| 1523 | * Allocate a new UST app event notifier rule. |
| 1524 | */ |
| 1525 | static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule( |
| 1526 | struct lttng_trigger *trigger) |
| 1527 | { |
| 1528 | enum lttng_event_rule_generate_exclusions_status |
| 1529 | generate_exclusion_status; |
| 1530 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; |
| 1531 | struct lttng_condition *condition = NULL; |
| 1532 | const struct lttng_event_rule *event_rule = NULL; |
| 1533 | |
| 1534 | ua_event_notifier_rule = zmalloc(sizeof(struct ust_app_event_notifier_rule)); |
| 1535 | if (ua_event_notifier_rule == NULL) { |
| 1536 | PERROR("Failed to allocate ust_app_event_notifier_rule structure"); |
| 1537 | goto error; |
| 1538 | } |
| 1539 | |
| 1540 | ua_event_notifier_rule->enabled = 1; |
| 1541 | ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger); |
| 1542 | lttng_ht_node_init_u64(&ua_event_notifier_rule->node, |
| 1543 | ua_event_notifier_rule->token); |
| 1544 | |
| 1545 | condition = lttng_trigger_get_condition(trigger); |
| 1546 | assert(condition); |
| 1547 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); |
| 1548 | |
| 1549 | assert(LTTNG_CONDITION_STATUS_OK == lttng_condition_on_event_get_rule(condition, &event_rule)); |
| 1550 | assert(event_rule); |
| 1551 | |
| 1552 | /* Acquire the event notifier's reference to the trigger. */ |
| 1553 | lttng_trigger_get(trigger); |
| 1554 | |
| 1555 | ua_event_notifier_rule->trigger = trigger; |
| 1556 | ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
| 1557 | generate_exclusion_status = lttng_event_rule_generate_exclusions( |
| 1558 | event_rule, &ua_event_notifier_rule->exclusion); |
| 1559 | switch (generate_exclusion_status) { |
| 1560 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK: |
| 1561 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE: |
| 1562 | break; |
| 1563 | default: |
| 1564 | /* Error occured. */ |
| 1565 | ERR("Failed to generate exclusions from trigger while allocating an event notifier rule"); |
| 1566 | goto error_put_trigger; |
| 1567 | } |
| 1568 | |
| 1569 | DBG3("UST app event notifier rule allocated: token = %" PRIu64, |
| 1570 | ua_event_notifier_rule->token); |
| 1571 | |
| 1572 | return ua_event_notifier_rule; |
| 1573 | |
| 1574 | error_put_trigger: |
| 1575 | lttng_trigger_put(trigger); |
| 1576 | error: |
| 1577 | free(ua_event_notifier_rule); |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Alloc new UST app context. |
| 1583 | */ |
| 1584 | static |
| 1585 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) |
| 1586 | { |
| 1587 | struct ust_app_ctx *ua_ctx; |
| 1588 | |
| 1589 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); |
| 1590 | if (ua_ctx == NULL) { |
| 1591 | goto error; |
| 1592 | } |
| 1593 | |
| 1594 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
| 1595 | |
| 1596 | if (uctx) { |
| 1597 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); |
| 1598 | if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { |
| 1599 | char *provider_name = NULL, *ctx_name = NULL; |
| 1600 | |
| 1601 | provider_name = strdup(uctx->u.app_ctx.provider_name); |
| 1602 | ctx_name = strdup(uctx->u.app_ctx.ctx_name); |
| 1603 | if (!provider_name || !ctx_name) { |
| 1604 | free(provider_name); |
| 1605 | free(ctx_name); |
| 1606 | goto error; |
| 1607 | } |
| 1608 | |
| 1609 | ua_ctx->ctx.u.app_ctx.provider_name = provider_name; |
| 1610 | ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); |
| 1615 | return ua_ctx; |
| 1616 | error: |
| 1617 | free(ua_ctx); |
| 1618 | return NULL; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * Create a liblttng-ust filter bytecode from given bytecode. |
| 1623 | * |
| 1624 | * Return allocated filter or NULL on error. |
| 1625 | */ |
| 1626 | static struct lttng_ust_filter_bytecode * |
| 1627 | create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) |
| 1628 | { |
| 1629 | struct lttng_ust_filter_bytecode *filter = NULL; |
| 1630 | |
| 1631 | /* Copy filter bytecode. */ |
| 1632 | filter = zmalloc(sizeof(*filter) + orig_f->len); |
| 1633 | if (!filter) { |
| 1634 | PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); |
| 1635 | goto error; |
| 1636 | } |
| 1637 | |
| 1638 | assert(sizeof(struct lttng_bytecode) == |
| 1639 | sizeof(struct lttng_ust_filter_bytecode)); |
| 1640 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
| 1641 | error: |
| 1642 | return filter; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Create a liblttng-ust capture bytecode from given bytecode. |
| 1647 | * |
| 1648 | * Return allocated filter or NULL on error. |
| 1649 | */ |
| 1650 | static struct lttng_ust_capture_bytecode * |
| 1651 | create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) |
| 1652 | { |
| 1653 | struct lttng_ust_capture_bytecode *capture = NULL; |
| 1654 | |
| 1655 | /* Copy capture bytecode. */ |
| 1656 | capture = zmalloc(sizeof(*capture) + orig_f->len); |
| 1657 | if (!capture) { |
| 1658 | PERROR("Failed to allocate lttng_ust_capture_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); |
| 1659 | goto error; |
| 1660 | } |
| 1661 | |
| 1662 | assert(sizeof(struct lttng_bytecode) == |
| 1663 | sizeof(struct lttng_ust_capture_bytecode)); |
| 1664 | memcpy(capture, orig_f, sizeof(*capture) + orig_f->len); |
| 1665 | error: |
| 1666 | return capture; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Find an ust_app using the sock and return it. RCU read side lock must be |
| 1671 | * held before calling this helper function. |
| 1672 | */ |
| 1673 | struct ust_app *ust_app_find_by_sock(int sock) |
| 1674 | { |
| 1675 | struct lttng_ht_node_ulong *node; |
| 1676 | struct lttng_ht_iter iter; |
| 1677 | |
| 1678 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
| 1679 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1680 | if (node == NULL) { |
| 1681 | DBG2("UST app find by sock %d not found", sock); |
| 1682 | goto error; |
| 1683 | } |
| 1684 | |
| 1685 | return caa_container_of(node, struct ust_app, sock_n); |
| 1686 | |
| 1687 | error: |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Find an ust_app using the notify sock and return it. RCU read side lock must |
| 1693 | * be held before calling this helper function. |
| 1694 | */ |
| 1695 | static struct ust_app *find_app_by_notify_sock(int sock) |
| 1696 | { |
| 1697 | struct lttng_ht_node_ulong *node; |
| 1698 | struct lttng_ht_iter iter; |
| 1699 | |
| 1700 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), |
| 1701 | &iter); |
| 1702 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1703 | if (node == NULL) { |
| 1704 | DBG2("UST app find by notify sock %d not found", sock); |
| 1705 | goto error; |
| 1706 | } |
| 1707 | |
| 1708 | return caa_container_of(node, struct ust_app, notify_sock_n); |
| 1709 | |
| 1710 | error: |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * Lookup for an ust app event based on event name, filter bytecode and the |
| 1716 | * event loglevel. |
| 1717 | * |
| 1718 | * Return an ust_app_event object or NULL on error. |
| 1719 | */ |
| 1720 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
| 1721 | const char *name, const struct lttng_bytecode *filter, |
| 1722 | int loglevel_value, |
| 1723 | const struct lttng_event_exclusion *exclusion, |
| 1724 | uint64_t tracer_token) |
| 1725 | { |
| 1726 | struct lttng_ht_iter iter; |
| 1727 | struct lttng_ht_node_str *node; |
| 1728 | struct ust_app_event *event = NULL; |
| 1729 | struct ust_app_ht_key key; |
| 1730 | |
| 1731 | assert(name); |
| 1732 | assert(ht); |
| 1733 | |
| 1734 | /* Setup key for event lookup. */ |
| 1735 | key.name = name; |
| 1736 | key.filter = filter; |
| 1737 | key.loglevel_type = loglevel_value; |
| 1738 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
| 1739 | key.exclusion = exclusion; |
| 1740 | key.tracer_token = tracer_token; |
| 1741 | |
| 1742 | /* Lookup using the event name as hash and a custom match fct. */ |
| 1743 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), |
| 1744 | ht_match_ust_app_event, &key, &iter.iter); |
| 1745 | node = lttng_ht_iter_get_node_str(&iter); |
| 1746 | if (node == NULL) { |
| 1747 | goto end; |
| 1748 | } |
| 1749 | |
| 1750 | event = caa_container_of(node, struct ust_app_event, node); |
| 1751 | |
| 1752 | end: |
| 1753 | return event; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * Look-up an event notifier rule based on its token id. |
| 1758 | * |
| 1759 | * Must be called with the RCU read lock held. |
| 1760 | * Return an ust_app_event_notifier_rule object or NULL on error. |
| 1761 | */ |
| 1762 | static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule( |
| 1763 | struct lttng_ht *ht, uint64_t token) |
| 1764 | { |
| 1765 | struct lttng_ht_iter iter; |
| 1766 | struct lttng_ht_node_u64 *node; |
| 1767 | struct ust_app_event_notifier_rule *event_notifier_rule = NULL; |
| 1768 | |
| 1769 | assert(ht); |
| 1770 | |
| 1771 | lttng_ht_lookup(ht, &token, &iter); |
| 1772 | node = lttng_ht_iter_get_node_u64(&iter); |
| 1773 | if (node == NULL) { |
| 1774 | DBG2("UST app event notifier rule token not found: token = %" PRIu64, |
| 1775 | token); |
| 1776 | goto end; |
| 1777 | } |
| 1778 | |
| 1779 | event_notifier_rule = caa_container_of( |
| 1780 | node, struct ust_app_event_notifier_rule, node); |
| 1781 | end: |
| 1782 | return event_notifier_rule; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Create the channel context on the tracer. |
| 1787 | * |
| 1788 | * Called with UST app session lock held. |
| 1789 | */ |
| 1790 | static |
| 1791 | int create_ust_channel_context(struct ust_app_channel *ua_chan, |
| 1792 | struct ust_app_ctx *ua_ctx, struct ust_app *app) |
| 1793 | { |
| 1794 | int ret; |
| 1795 | |
| 1796 | health_code_update(); |
| 1797 | |
| 1798 | pthread_mutex_lock(&app->sock_lock); |
| 1799 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
| 1800 | ua_chan->obj, &ua_ctx->obj); |
| 1801 | pthread_mutex_unlock(&app->sock_lock); |
| 1802 | if (ret < 0) { |
| 1803 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1804 | ERR("UST app create channel context failed for app (pid: %d) " |
| 1805 | "with ret %d", app->pid, ret); |
| 1806 | } else { |
| 1807 | /* |
| 1808 | * This is normal behavior, an application can die during the |
| 1809 | * creation process. Don't report an error so the execution can |
| 1810 | * continue normally. |
| 1811 | */ |
| 1812 | ret = 0; |
| 1813 | DBG3("UST app add context failed. Application is dead."); |
| 1814 | } |
| 1815 | goto error; |
| 1816 | } |
| 1817 | |
| 1818 | ua_ctx->handle = ua_ctx->obj->handle; |
| 1819 | |
| 1820 | DBG2("UST app context handle %d created successfully for channel %s", |
| 1821 | ua_ctx->handle, ua_chan->name); |
| 1822 | |
| 1823 | error: |
| 1824 | health_code_update(); |
| 1825 | return ret; |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Set the filter on the tracer. |
| 1830 | */ |
| 1831 | static int set_ust_object_filter(struct ust_app *app, |
| 1832 | const struct lttng_bytecode *bytecode, |
| 1833 | struct lttng_ust_object_data *ust_object) |
| 1834 | { |
| 1835 | int ret; |
| 1836 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
| 1837 | |
| 1838 | health_code_update(); |
| 1839 | |
| 1840 | ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode); |
| 1841 | if (!ust_bytecode) { |
| 1842 | ret = -LTTNG_ERR_NOMEM; |
| 1843 | goto error; |
| 1844 | } |
| 1845 | pthread_mutex_lock(&app->sock_lock); |
| 1846 | ret = ustctl_set_filter(app->sock, ust_bytecode, |
| 1847 | ust_object); |
| 1848 | pthread_mutex_unlock(&app->sock_lock); |
| 1849 | if (ret < 0) { |
| 1850 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1851 | ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d", |
| 1852 | ust_object, app->pid, ret); |
| 1853 | } else { |
| 1854 | /* |
| 1855 | * This is normal behavior, an application can die during the |
| 1856 | * creation process. Don't report an error so the execution can |
| 1857 | * continue normally. |
| 1858 | */ |
| 1859 | ret = 0; |
| 1860 | DBG3("Failed to set UST app object filter. Application is dead."); |
| 1861 | } |
| 1862 | goto error; |
| 1863 | } |
| 1864 | |
| 1865 | DBG2("UST filter successfully set: object = %p", ust_object); |
| 1866 | |
| 1867 | error: |
| 1868 | health_code_update(); |
| 1869 | free(ust_bytecode); |
| 1870 | return ret; |
| 1871 | } |
| 1872 | |
| 1873 | /* |
| 1874 | * Set a capture bytecode for the passed object. |
| 1875 | * The sequence number enforces the ordering at runtime and on reception of |
| 1876 | * the captured payloads. |
| 1877 | */ |
| 1878 | static int set_ust_capture(struct ust_app *app, |
| 1879 | const struct lttng_bytecode *bytecode, |
| 1880 | unsigned int capture_seqnum, |
| 1881 | struct lttng_ust_object_data *ust_object) |
| 1882 | { |
| 1883 | int ret; |
| 1884 | struct lttng_ust_capture_bytecode *ust_bytecode = NULL; |
| 1885 | |
| 1886 | health_code_update(); |
| 1887 | |
| 1888 | ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode); |
| 1889 | if (!ust_bytecode) { |
| 1890 | ret = -LTTNG_ERR_NOMEM; |
| 1891 | goto error; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * Set the sequence number to ensure the capture of fields is ordered. |
| 1896 | */ |
| 1897 | ust_bytecode->seqnum = capture_seqnum; |
| 1898 | |
| 1899 | pthread_mutex_lock(&app->sock_lock); |
| 1900 | ret = ustctl_set_capture(app->sock, ust_bytecode, |
| 1901 | ust_object); |
| 1902 | pthread_mutex_unlock(&app->sock_lock); |
| 1903 | if (ret < 0) { |
| 1904 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1905 | ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d", |
| 1906 | ust_object, app->pid, ret); |
| 1907 | } else { |
| 1908 | /* |
| 1909 | * This is normal behavior, an application can die during the |
| 1910 | * creation process. Don't report an error so the execution can |
| 1911 | * continue normally. |
| 1912 | */ |
| 1913 | ret = 0; |
| 1914 | DBG3("Failed to set UST app object capture. Application is dead."); |
| 1915 | } |
| 1916 | |
| 1917 | goto error; |
| 1918 | } |
| 1919 | |
| 1920 | DBG2("UST capture successfully set: object = %p", ust_object); |
| 1921 | |
| 1922 | error: |
| 1923 | health_code_update(); |
| 1924 | free(ust_bytecode); |
| 1925 | return ret; |
| 1926 | } |
| 1927 | |
| 1928 | static |
| 1929 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( |
| 1930 | const struct lttng_event_exclusion *exclusion) |
| 1931 | { |
| 1932 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; |
| 1933 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + |
| 1934 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; |
| 1935 | |
| 1936 | ust_exclusion = zmalloc(exclusion_alloc_size); |
| 1937 | if (!ust_exclusion) { |
| 1938 | PERROR("malloc"); |
| 1939 | goto end; |
| 1940 | } |
| 1941 | |
| 1942 | assert(sizeof(struct lttng_event_exclusion) == |
| 1943 | sizeof(struct lttng_ust_event_exclusion)); |
| 1944 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); |
| 1945 | end: |
| 1946 | return ust_exclusion; |
| 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * Set event exclusions on the tracer. |
| 1951 | */ |
| 1952 | static int set_ust_object_exclusions(struct ust_app *app, |
| 1953 | const struct lttng_event_exclusion *exclusions, |
| 1954 | struct lttng_ust_object_data *ust_object) |
| 1955 | { |
| 1956 | int ret; |
| 1957 | struct lttng_ust_event_exclusion *ust_exclusions = NULL; |
| 1958 | |
| 1959 | assert(exclusions && exclusions->count > 0); |
| 1960 | |
| 1961 | health_code_update(); |
| 1962 | |
| 1963 | ust_exclusions = create_ust_exclusion_from_exclusion( |
| 1964 | exclusions); |
| 1965 | if (!ust_exclusions) { |
| 1966 | ret = -LTTNG_ERR_NOMEM; |
| 1967 | goto error; |
| 1968 | } |
| 1969 | pthread_mutex_lock(&app->sock_lock); |
| 1970 | ret = ustctl_set_exclusion(app->sock, ust_exclusions, ust_object); |
| 1971 | pthread_mutex_unlock(&app->sock_lock); |
| 1972 | if (ret < 0) { |
| 1973 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1974 | ERR("Failed to set UST app exclusions for object %p of app (pid: %d) " |
| 1975 | "with ret %d", ust_object, app->pid, ret); |
| 1976 | } else { |
| 1977 | /* |
| 1978 | * This is normal behavior, an application can die during the |
| 1979 | * creation process. Don't report an error so the execution can |
| 1980 | * continue normally. |
| 1981 | */ |
| 1982 | ret = 0; |
| 1983 | DBG3("Failed to set UST app object exclusions. Application is dead."); |
| 1984 | } |
| 1985 | goto error; |
| 1986 | } |
| 1987 | |
| 1988 | DBG2("UST exclusions set successfully for object %p", ust_object); |
| 1989 | |
| 1990 | error: |
| 1991 | health_code_update(); |
| 1992 | free(ust_exclusions); |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * Disable the specified event on to UST tracer for the UST session. |
| 1998 | */ |
| 1999 | static int disable_ust_object(struct ust_app *app, |
| 2000 | struct lttng_ust_object_data *object) |
| 2001 | { |
| 2002 | int ret; |
| 2003 | |
| 2004 | health_code_update(); |
| 2005 | |
| 2006 | pthread_mutex_lock(&app->sock_lock); |
| 2007 | ret = ustctl_disable(app->sock, object); |
| 2008 | pthread_mutex_unlock(&app->sock_lock); |
| 2009 | if (ret < 0) { |
| 2010 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2011 | ERR("Failed to disable UST app object %p app (pid: %d) with ret %d", |
| 2012 | object, app->pid, ret); |
| 2013 | } else { |
| 2014 | /* |
| 2015 | * This is normal behavior, an application can die during the |
| 2016 | * creation process. Don't report an error so the execution can |
| 2017 | * continue normally. |
| 2018 | */ |
| 2019 | ret = 0; |
| 2020 | DBG3("Failed to disable UST app object. Application is dead."); |
| 2021 | } |
| 2022 | goto error; |
| 2023 | } |
| 2024 | |
| 2025 | DBG2("UST app object %p disabled successfully for app (pid: %d)", |
| 2026 | object, app->pid); |
| 2027 | |
| 2028 | error: |
| 2029 | health_code_update(); |
| 2030 | return ret; |
| 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | * Disable the specified channel on to UST tracer for the UST session. |
| 2035 | */ |
| 2036 | static int disable_ust_channel(struct ust_app *app, |
| 2037 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 2038 | { |
| 2039 | int ret; |
| 2040 | |
| 2041 | health_code_update(); |
| 2042 | |
| 2043 | pthread_mutex_lock(&app->sock_lock); |
| 2044 | ret = ustctl_disable(app->sock, ua_chan->obj); |
| 2045 | pthread_mutex_unlock(&app->sock_lock); |
| 2046 | if (ret < 0) { |
| 2047 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2048 | ERR("UST app channel %s disable failed for app (pid: %d) " |
| 2049 | "and session handle %d with ret %d", |
| 2050 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 2051 | } else { |
| 2052 | /* |
| 2053 | * This is normal behavior, an application can die during the |
| 2054 | * creation process. Don't report an error so the execution can |
| 2055 | * continue normally. |
| 2056 | */ |
| 2057 | ret = 0; |
| 2058 | DBG3("UST app disable channel failed. Application is dead."); |
| 2059 | } |
| 2060 | goto error; |
| 2061 | } |
| 2062 | |
| 2063 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
| 2064 | ua_chan->name, app->pid); |
| 2065 | |
| 2066 | error: |
| 2067 | health_code_update(); |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * Disable the specified map on to UST tracer for the UST session. |
| 2073 | */ |
| 2074 | static int disable_ust_map(struct ust_app *app, |
| 2075 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) |
| 2076 | { |
| 2077 | int ret; |
| 2078 | |
| 2079 | health_code_update(); |
| 2080 | |
| 2081 | pthread_mutex_lock(&app->sock_lock); |
| 2082 | ret = ustctl_disable(app->sock, ua_map->obj); |
| 2083 | pthread_mutex_unlock(&app->sock_lock); |
| 2084 | if (ret < 0) { |
| 2085 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2086 | ERR("UST app map %s disable failed for app (pid: %d) " |
| 2087 | "and session handle %d with ret %d", |
| 2088 | ua_map->name, app->pid, ua_sess->handle, ret); |
| 2089 | } else { |
| 2090 | /* |
| 2091 | * This is normal behavior, an application can die during the |
| 2092 | * creation process. Don't report an error so the execution can |
| 2093 | * continue normally. |
| 2094 | */ |
| 2095 | ret = 0; |
| 2096 | DBG3("UST app disable map failed. Application is dead."); |
| 2097 | } |
| 2098 | goto error; |
| 2099 | } |
| 2100 | |
| 2101 | DBG2("UST app map %s disabled successfully for app (pid: %d)", |
| 2102 | ua_map->name, app->pid); |
| 2103 | |
| 2104 | error: |
| 2105 | health_code_update(); |
| 2106 | return ret; |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * Enable the specified channel on to UST tracer for the UST session. |
| 2111 | */ |
| 2112 | static int enable_ust_channel(struct ust_app *app, |
| 2113 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 2114 | { |
| 2115 | int ret; |
| 2116 | |
| 2117 | health_code_update(); |
| 2118 | |
| 2119 | pthread_mutex_lock(&app->sock_lock); |
| 2120 | ret = ustctl_enable(app->sock, ua_chan->obj); |
| 2121 | pthread_mutex_unlock(&app->sock_lock); |
| 2122 | if (ret < 0) { |
| 2123 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2124 | ERR("UST app channel %s enable failed for app (pid: %d) " |
| 2125 | "and session handle %d with ret %d", |
| 2126 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 2127 | } else { |
| 2128 | /* |
| 2129 | * This is normal behavior, an application can die during the |
| 2130 | * creation process. Don't report an error so the execution can |
| 2131 | * continue normally. |
| 2132 | */ |
| 2133 | ret = 0; |
| 2134 | DBG3("UST app enable channel failed. Application is dead."); |
| 2135 | } |
| 2136 | goto error; |
| 2137 | } |
| 2138 | |
| 2139 | ua_chan->enabled = 1; |
| 2140 | |
| 2141 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", |
| 2142 | ua_chan->name, app->pid); |
| 2143 | |
| 2144 | error: |
| 2145 | health_code_update(); |
| 2146 | return ret; |
| 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * Enable the specified map on to UST tracer for the UST session. |
| 2151 | */ |
| 2152 | static int enable_ust_map(struct ust_app *app, |
| 2153 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) |
| 2154 | { |
| 2155 | int ret; |
| 2156 | |
| 2157 | health_code_update(); |
| 2158 | |
| 2159 | pthread_mutex_lock(&app->sock_lock); |
| 2160 | ret = ustctl_enable(app->sock, ua_map->obj); |
| 2161 | pthread_mutex_unlock(&app->sock_lock); |
| 2162 | if (ret < 0) { |
| 2163 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2164 | ERR("UST app map %s enable failed for app (pid: %d) " |
| 2165 | "and session handle %d with ret %d", |
| 2166 | ua_map->name, app->pid, ua_sess->handle, ret); |
| 2167 | } else { |
| 2168 | /* |
| 2169 | * This is normal behavior, an application can die during the |
| 2170 | * creation process. Don't report an error so the execution can |
| 2171 | * continue normally. |
| 2172 | */ |
| 2173 | ret = 0; |
| 2174 | DBG3("UST app enable map failed. Application is dead."); |
| 2175 | } |
| 2176 | goto error; |
| 2177 | } |
| 2178 | |
| 2179 | ua_map->enabled = 1; |
| 2180 | |
| 2181 | DBG2("UST app map %s enabled successfully for app (pid: %d)", |
| 2182 | ua_map->name, app->pid); |
| 2183 | |
| 2184 | error: |
| 2185 | health_code_update(); |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * Enable the specified event on to UST tracer for the UST session. |
| 2191 | */ |
| 2192 | static int enable_ust_object( |
| 2193 | struct ust_app *app, struct lttng_ust_object_data *ust_object) |
| 2194 | { |
| 2195 | int ret; |
| 2196 | |
| 2197 | health_code_update(); |
| 2198 | |
| 2199 | pthread_mutex_lock(&app->sock_lock); |
| 2200 | ret = ustctl_enable(app->sock, ust_object); |
| 2201 | pthread_mutex_unlock(&app->sock_lock); |
| 2202 | if (ret < 0) { |
| 2203 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2204 | ERR("UST app enable failed for object %p app (pid: %d) with ret %d", |
| 2205 | ust_object, app->pid, ret); |
| 2206 | } else { |
| 2207 | /* |
| 2208 | * This is normal behavior, an application can die during the |
| 2209 | * creation process. Don't report an error so the execution can |
| 2210 | * continue normally. |
| 2211 | */ |
| 2212 | ret = 0; |
| 2213 | DBG3("Failed to enable UST app object. Application is dead."); |
| 2214 | } |
| 2215 | goto error; |
| 2216 | } |
| 2217 | |
| 2218 | DBG2("UST app object %p enabled successfully for app (pid: %d)", |
| 2219 | ust_object, app->pid); |
| 2220 | |
| 2221 | error: |
| 2222 | health_code_update(); |
| 2223 | return ret; |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * Send channel and stream buffer to application. |
| 2228 | * |
| 2229 | * Return 0 on success. On error, a negative value is returned. |
| 2230 | */ |
| 2231 | static int send_channel_pid_to_ust(struct ust_app *app, |
| 2232 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 2233 | { |
| 2234 | int ret; |
| 2235 | struct ust_app_stream *stream, *stmp; |
| 2236 | |
| 2237 | assert(app); |
| 2238 | assert(ua_sess); |
| 2239 | assert(ua_chan); |
| 2240 | |
| 2241 | health_code_update(); |
| 2242 | |
| 2243 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
| 2244 | app->sock); |
| 2245 | |
| 2246 | /* Send channel to the application. */ |
| 2247 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 2248 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2249 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 2250 | goto error; |
| 2251 | } else if (ret < 0) { |
| 2252 | goto error; |
| 2253 | } |
| 2254 | |
| 2255 | health_code_update(); |
| 2256 | |
| 2257 | /* Send all streams to application. */ |
| 2258 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 2259 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); |
| 2260 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 2261 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 2262 | goto error; |
| 2263 | } else if (ret < 0) { |
| 2264 | goto error; |
| 2265 | } |
| 2266 | /* We don't need the stream anymore once sent to the tracer. */ |
| 2267 | cds_list_del(&stream->list); |
| 2268 | delete_ust_app_stream(-1, stream, app); |
| 2269 | } |
| 2270 | /* Flag the channel that it is sent to the application. */ |
| 2271 | ua_chan->is_sent = 1; |
| 2272 | |
| 2273 | error: |
| 2274 | health_code_update(); |
| 2275 | return ret; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Send map and stream buffer to application. |
| 2280 | * |
| 2281 | * Return 0 on success. On error, a negative value is returned. |
| 2282 | */ |
| 2283 | static int send_map_pid_to_ust(struct ust_app *app, |
| 2284 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) |
| 2285 | { |
| 2286 | int ret; |
| 2287 | struct ust_app_map_counter *counter, *ctmp; |
| 2288 | |
| 2289 | assert(app); |
| 2290 | assert(ua_sess); |
| 2291 | assert(ua_map); |
| 2292 | |
| 2293 | health_code_update(); |
| 2294 | |
| 2295 | DBG("UST app sending map %s to UST app sock %d", ua_map->name, |
| 2296 | app->sock); |
| 2297 | |
| 2298 | /* Send map to the application. */ |
| 2299 | pthread_mutex_lock(&app->sock_lock); |
| 2300 | ret = ustctl_send_counter_data_to_ust(app->sock, |
| 2301 | ua_sess->handle, ua_map->obj); |
| 2302 | pthread_mutex_unlock(&app->sock_lock); |
| 2303 | assert(ret == 0); |
| 2304 | |
| 2305 | ua_map->handle = ua_map->obj->handle; |
| 2306 | |
| 2307 | health_code_update(); |
| 2308 | |
| 2309 | /* Send all streams to application. */ |
| 2310 | cds_list_for_each_entry_safe(counter, ctmp, &ua_map->counters.head, list) { |
| 2311 | pthread_mutex_lock(&app->sock_lock); |
| 2312 | // Do send the per cpu counter here |
| 2313 | ret = ustctl_send_counter_cpu_data_to_ust(app->sock, |
| 2314 | ua_map->obj, counter->obj); |
| 2315 | pthread_mutex_unlock(&app->sock_lock); |
| 2316 | assert(ret == 0); |
| 2317 | |
| 2318 | /* We don't need the stream anymore once sent to the tracer. */ |
| 2319 | cds_list_del(&counter->list); |
| 2320 | delete_ust_app_map_counter(-1, counter, app); |
| 2321 | } |
| 2322 | /* Flag the map that it is sent to the application. */ |
| 2323 | ua_map->is_sent = 1; |
| 2324 | |
| 2325 | health_code_update(); |
| 2326 | return ret; |
| 2327 | } |
| 2328 | |
| 2329 | /* |
| 2330 | * Create the specified event onto the UST tracer for a UST session. |
| 2331 | * |
| 2332 | * Should be called with session mutex held. |
| 2333 | */ |
| 2334 | static |
| 2335 | int create_ust_channel_event(struct ust_app *app, struct ust_app_session *ua_sess, |
| 2336 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
| 2337 | { |
| 2338 | int ret = 0; |
| 2339 | |
| 2340 | health_code_update(); |
| 2341 | |
| 2342 | /* Create UST event on tracer */ |
| 2343 | pthread_mutex_lock(&app->sock_lock); |
| 2344 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
| 2345 | &ua_event->obj); |
| 2346 | pthread_mutex_unlock(&app->sock_lock); |
| 2347 | if (ret < 0) { |
| 2348 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2349 | abort(); |
| 2350 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
| 2351 | ua_event->attr.name, app->pid, ret); |
| 2352 | } else { |
| 2353 | /* |
| 2354 | * This is normal behavior, an application can die during the |
| 2355 | * creation process. Don't report an error so the execution can |
| 2356 | * continue normally. |
| 2357 | */ |
| 2358 | ret = 0; |
| 2359 | DBG3("UST app create event failed. Application is dead."); |
| 2360 | } |
| 2361 | goto error; |
| 2362 | } |
| 2363 | |
| 2364 | ua_event->handle = ua_event->obj->handle; |
| 2365 | |
| 2366 | DBG2("UST app event %s created successfully for pid:%d object: %p", |
| 2367 | ua_event->attr.name, app->pid, ua_event->obj); |
| 2368 | |
| 2369 | health_code_update(); |
| 2370 | |
| 2371 | /* Set filter if one is present. */ |
| 2372 | if (ua_event->filter) { |
| 2373 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
| 2374 | if (ret < 0) { |
| 2375 | goto error; |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | /* Set exclusions for the event */ |
| 2380 | if (ua_event->exclusion) { |
| 2381 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
| 2382 | if (ret < 0) { |
| 2383 | goto error; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | /* If event not enabled, disable it on the tracer */ |
| 2388 | if (ua_event->enabled) { |
| 2389 | /* |
| 2390 | * We now need to explicitly enable the event, since it |
| 2391 | * is now disabled at creation. |
| 2392 | */ |
| 2393 | ret = enable_ust_object(app, ua_event->obj); |
| 2394 | if (ret < 0) { |
| 2395 | /* |
| 2396 | * If we hit an EPERM, something is wrong with our enable call. If |
| 2397 | * we get an EEXIST, there is a problem on the tracer side since we |
| 2398 | * just created it. |
| 2399 | */ |
| 2400 | switch (ret) { |
| 2401 | case -LTTNG_UST_ERR_PERM: |
| 2402 | /* Code flow problem */ |
| 2403 | assert(0); |
| 2404 | case -LTTNG_UST_ERR_EXIST: |
| 2405 | /* It's OK for our use case. */ |
| 2406 | ret = 0; |
| 2407 | break; |
| 2408 | default: |
| 2409 | break; |
| 2410 | } |
| 2411 | goto error; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | error: |
| 2416 | health_code_update(); |
| 2417 | return ret; |
| 2418 | } |
| 2419 | |
| 2420 | static |
| 2421 | void add_key_token(struct lttng_ust_key_token *ust_key_token, |
| 2422 | const struct lttng_map_key_token *key_token) |
| 2423 | { |
| 2424 | switch (key_token->type) { |
| 2425 | case LTTNG_MAP_KEY_TOKEN_TYPE_STRING: |
| 2426 | { |
| 2427 | const struct lttng_map_key_token_string *str_token; |
| 2428 | str_token = (typeof(str_token)) key_token; |
| 2429 | |
| 2430 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_STRING; |
| 2431 | strncpy(ust_key_token->arg.string, str_token->string, |
| 2432 | LTTNG_UST_KEY_TOKEN_STRING_LEN_MAX); |
| 2433 | |
| 2434 | break; |
| 2435 | } |
| 2436 | case LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE: |
| 2437 | { |
| 2438 | const struct lttng_map_key_token_variable *var_token; |
| 2439 | var_token = (typeof(var_token)) key_token; |
| 2440 | switch (var_token->type) { |
| 2441 | case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME: |
| 2442 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_EVENT_NAME; |
| 2443 | break; |
| 2444 | case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_PROVIDER_NAME: |
| 2445 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_PROVIDER_NAME; |
| 2446 | break; |
| 2447 | default: |
| 2448 | abort(); |
| 2449 | } |
| 2450 | |
| 2451 | break; |
| 2452 | } |
| 2453 | default: |
| 2454 | abort(); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * Create the specified event onto the UST tracer for a UST session. |
| 2460 | * |
| 2461 | * Should be called with session mutex held. |
| 2462 | */ |
| 2463 | static |
| 2464 | int create_ust_map_event(struct ust_app *app, struct ust_app_session *ua_sess, |
| 2465 | struct ust_app_map *ua_map, const struct lttng_map_key *key, |
| 2466 | struct ust_app_event *ua_event) |
| 2467 | { |
| 2468 | int ret = 0; |
| 2469 | unsigned int i, key_token_count; |
| 2470 | enum lttng_map_key_status status; |
| 2471 | struct lttng_ust_counter_event counter_event = {0}; |
| 2472 | |
| 2473 | health_code_update(); |
| 2474 | |
| 2475 | memcpy(&counter_event.event, &ua_event->attr, sizeof(struct lttng_ust_event)); |
| 2476 | |
| 2477 | status = lttng_map_key_get_token_count(key, &key_token_count); |
| 2478 | if (status != LTTNG_MAP_KEY_STATUS_OK) { |
| 2479 | ret = LTTNG_ERR_UNK; |
| 2480 | goto error; |
| 2481 | } |
| 2482 | |
| 2483 | assert(key_token_count > 0); |
| 2484 | |
| 2485 | counter_event.key.nr_dimensions = 1; |
| 2486 | counter_event.key.key_dimensions[0].nr_key_tokens = key_token_count; |
| 2487 | |
| 2488 | if (key_token_count > LTTNG_UST_NR_KEY_TOKEN) { |
| 2489 | ERR("Too many key tokens for UST tracer: token count = %u token count max =%u", |
| 2490 | key_token_count, LTTNG_UST_NR_KEY_TOKEN); |
| 2491 | ret = LTTNG_ERR_INVALID; |
| 2492 | goto error; |
| 2493 | } |
| 2494 | |
| 2495 | for (i = 0; i < key_token_count; i++) { |
| 2496 | const struct lttng_map_key_token *token = |
| 2497 | lttng_map_key_get_token_at_index(key, i); |
| 2498 | |
| 2499 | add_key_token(&counter_event.key.key_dimensions[0].key_tokens[i], |
| 2500 | token); |
| 2501 | } |
| 2502 | |
| 2503 | /* Create UST event on tracer */ |
| 2504 | pthread_mutex_lock(&app->sock_lock); |
| 2505 | ret = ustctl_counter_create_event(app->sock, &counter_event, ua_map->obj, |
| 2506 | &ua_event->obj); |
| 2507 | pthread_mutex_unlock(&app->sock_lock); |
| 2508 | if (ret < 0) { |
| 2509 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2510 | abort(); |
| 2511 | ERR("Error ustctl counter create event %s for app pid: %d with ret %d", |
| 2512 | ua_event->attr.name, app->pid, ret); |
| 2513 | } else { |
| 2514 | /* |
| 2515 | * This is normal behavior, an application can die during the |
| 2516 | * creation process. Don't report an error so the execution can |
| 2517 | * continue normally. |
| 2518 | */ |
| 2519 | ret = 0; |
| 2520 | DBG3("UST app counter create event failed. Application is dead."); |
| 2521 | } |
| 2522 | goto error; |
| 2523 | } |
| 2524 | |
| 2525 | ua_event->handle = ua_event->obj->handle; |
| 2526 | |
| 2527 | DBG2("UST app map event %s created successfully for pid:%d object: %p", |
| 2528 | ua_event->attr.name, app->pid, ua_event->obj); |
| 2529 | |
| 2530 | health_code_update(); |
| 2531 | |
| 2532 | /* Set filter if one is present. */ |
| 2533 | if (ua_event->filter) { |
| 2534 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
| 2535 | if (ret < 0) { |
| 2536 | goto error; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | /* Set exclusions for the event */ |
| 2541 | if (ua_event->exclusion) { |
| 2542 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
| 2543 | if (ret < 0) { |
| 2544 | goto error; |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | /* If event not enabled, disable it on the tracer */ |
| 2549 | if (ua_event->enabled) { |
| 2550 | /* |
| 2551 | * We now need to explicitly enable the event, since it |
| 2552 | * is now disabled at creation. |
| 2553 | */ |
| 2554 | ret = enable_ust_object(app, ua_event->obj); |
| 2555 | if (ret < 0) { |
| 2556 | /* |
| 2557 | * If we hit an EPERM, something is wrong with our enable call. If |
| 2558 | * we get an EEXIST, there is a problem on the tracer side since we |
| 2559 | * just created it. |
| 2560 | */ |
| 2561 | switch (ret) { |
| 2562 | case -LTTNG_UST_ERR_PERM: |
| 2563 | /* Code flow problem */ |
| 2564 | assert(0); |
| 2565 | case -LTTNG_UST_ERR_EXIST: |
| 2566 | /* It's OK for our use case. */ |
| 2567 | ret = 0; |
| 2568 | break; |
| 2569 | default: |
| 2570 | break; |
| 2571 | } |
| 2572 | goto error; |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | error: |
| 2577 | health_code_update(); |
| 2578 | return ret; |
| 2579 | } |
| 2580 | |
| 2581 | static int init_ust_event_from_event_rule( |
| 2582 | const struct lttng_event_rule *rule, |
| 2583 | struct lttng_ust_event *event) |
| 2584 | { |
| 2585 | enum lttng_event_rule_status status; |
| 2586 | enum lttng_ust_loglevel_type ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
| 2587 | int loglevel = -1, ret = 0; |
| 2588 | const char *pattern; |
| 2589 | |
| 2590 | /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */ |
| 2591 | assert(lttng_event_rule_get_type(rule) == |
| 2592 | LTTNG_EVENT_RULE_TYPE_TRACEPOINT); |
| 2593 | |
| 2594 | if (lttng_event_rule_targets_agent_domain(rule)) { |
| 2595 | /* |
| 2596 | * Special event for agents |
| 2597 | * The actual meat of the event is in the filter that will be |
| 2598 | * attached later on. |
| 2599 | * Set the default values for the agent event. |
| 2600 | */ |
| 2601 | pattern = event_get_default_agent_ust_name( |
| 2602 | lttng_event_rule_get_domain_type(rule)); |
| 2603 | loglevel = 0; |
| 2604 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
| 2605 | } else { |
| 2606 | const struct lttng_log_level_rule *log_level_rule; |
| 2607 | |
| 2608 | status = lttng_event_rule_tracepoint_get_pattern(rule, &pattern); |
| 2609 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 2610 | /* At this point, this is a fatal error. */ |
| 2611 | abort(); |
| 2612 | } |
| 2613 | |
| 2614 | status = lttng_event_rule_tracepoint_get_log_level_rule( |
| 2615 | rule, &log_level_rule); |
| 2616 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { |
| 2617 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
| 2618 | } else if (status == LTTNG_EVENT_RULE_STATUS_OK) { |
| 2619 | enum lttng_log_level_rule_status llr_status; |
| 2620 | |
| 2621 | switch (lttng_log_level_rule_get_type(log_level_rule)) { |
| 2622 | case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY: |
| 2623 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; |
| 2624 | llr_status = lttng_log_level_rule_exactly_get_level(log_level_rule, &loglevel); |
| 2625 | break; |
| 2626 | case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS: |
| 2627 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; |
| 2628 | llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(log_level_rule, &loglevel); |
| 2629 | break; |
| 2630 | default: |
| 2631 | abort(); |
| 2632 | } |
| 2633 | |
| 2634 | assert(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK); |
| 2635 | } else { |
| 2636 | /* At this point this is a fatal error */ |
| 2637 | assert(0); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | event->instrumentation = LTTNG_UST_TRACEPOINT; |
| 2642 | ret = lttng_strncpy(event->name, pattern, |
| 2643 | LTTNG_UST_SYM_NAME_LEN - 1); |
| 2644 | if (ret) { |
| 2645 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", |
| 2646 | pattern); |
| 2647 | goto end; |
| 2648 | } |
| 2649 | |
| 2650 | event->loglevel_type = ust_loglevel_type; |
| 2651 | event->loglevel = loglevel; |
| 2652 | end: |
| 2653 | return ret; |
| 2654 | } |
| 2655 | |
| 2656 | /* |
| 2657 | * Create the specified event notifier against the user space tracer of a |
| 2658 | * given application. |
| 2659 | */ |
| 2660 | static int create_ust_event_notifier(struct ust_app *app, |
| 2661 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) |
| 2662 | { |
| 2663 | int ret = 0; |
| 2664 | struct lttng_ust_event_notifier event_notifier = {0}; |
| 2665 | const struct lttng_condition *condition = NULL; |
| 2666 | const struct lttng_event_rule *event_rule = NULL; |
| 2667 | unsigned int capture_bytecode_count = 0, i; |
| 2668 | enum lttng_condition_status cond_status; |
| 2669 | |
| 2670 | health_code_update(); |
| 2671 | assert(app->event_notifier_group.object); |
| 2672 | |
| 2673 | condition = lttng_trigger_get_const_condition( |
| 2674 | ua_event_notifier_rule->trigger); |
| 2675 | assert(condition); |
| 2676 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); |
| 2677 | |
| 2678 | lttng_condition_on_event_get_rule(condition, &event_rule); |
| 2679 | assert(event_rule); |
| 2680 | assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT); |
| 2681 | |
| 2682 | init_ust_event_from_event_rule(event_rule, &event_notifier.event); |
| 2683 | event_notifier.event.token = ua_event_notifier_rule->token; |
| 2684 | event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index; |
| 2685 | |
| 2686 | /* Create UST event notifier against the tracer. */ |
| 2687 | pthread_mutex_lock(&app->sock_lock); |
| 2688 | ret = ustctl_create_event_notifier(app->sock, &event_notifier, |
| 2689 | app->event_notifier_group.object, |
| 2690 | &ua_event_notifier_rule->obj); |
| 2691 | pthread_mutex_unlock(&app->sock_lock); |
| 2692 | if (ret < 0) { |
| 2693 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2694 | ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d", |
| 2695 | event_notifier.event.name, app->name, |
| 2696 | app->ppid, ret); |
| 2697 | } else { |
| 2698 | /* |
| 2699 | * This is normal behavior, an application can die |
| 2700 | * during the creation process. Don't report an error so |
| 2701 | * the execution can continue normally. |
| 2702 | */ |
| 2703 | ret = 0; |
| 2704 | DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)", |
| 2705 | app->name, app->ppid); |
| 2706 | } |
| 2707 | |
| 2708 | goto error; |
| 2709 | } |
| 2710 | |
| 2711 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; |
| 2712 | |
| 2713 | DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p", |
| 2714 | event_notifier.event.name, app->name, app->ppid, |
| 2715 | ua_event_notifier_rule->obj); |
| 2716 | |
| 2717 | health_code_update(); |
| 2718 | |
| 2719 | /* Set filter if one is present. */ |
| 2720 | if (ua_event_notifier_rule->filter) { |
| 2721 | ret = set_ust_object_filter(app, ua_event_notifier_rule->filter, |
| 2722 | ua_event_notifier_rule->obj); |
| 2723 | if (ret < 0) { |
| 2724 | goto error; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | /* Set exclusions for the event. */ |
| 2729 | if (ua_event_notifier_rule->exclusion) { |
| 2730 | ret = set_ust_object_exclusions(app, |
| 2731 | ua_event_notifier_rule->exclusion, |
| 2732 | ua_event_notifier_rule->obj); |
| 2733 | if (ret < 0) { |
| 2734 | goto error; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | /* Set the capture bytecodes. */ |
| 2739 | cond_status = lttng_condition_on_event_get_capture_descriptor_count( |
| 2740 | condition, &capture_bytecode_count); |
| 2741 | assert(cond_status == LTTNG_CONDITION_STATUS_OK); |
| 2742 | |
| 2743 | for (i = 0; i < capture_bytecode_count; i++) { |
| 2744 | const struct lttng_bytecode *capture_bytecode = |
| 2745 | lttng_condition_on_event_get_capture_bytecode_at_index( |
| 2746 | condition, i); |
| 2747 | |
| 2748 | ret = set_ust_capture(app, capture_bytecode, i, |
| 2749 | ua_event_notifier_rule->obj); |
| 2750 | if (ret < 0) { |
| 2751 | goto error; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /* |
| 2756 | * We now need to explicitly enable the event, since it |
| 2757 | * is disabled at creation. |
| 2758 | */ |
| 2759 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); |
| 2760 | if (ret < 0) { |
| 2761 | /* |
| 2762 | * If we hit an EPERM, something is wrong with our enable call. |
| 2763 | * If we get an EEXIST, there is a problem on the tracer side |
| 2764 | * since we just created it. |
| 2765 | */ |
| 2766 | switch (ret) { |
| 2767 | case -LTTNG_UST_ERR_PERM: |
| 2768 | /* Code flow problem. */ |
| 2769 | abort(); |
| 2770 | case -LTTNG_UST_ERR_EXIST: |
| 2771 | /* It's OK for our use case. */ |
| 2772 | ret = 0; |
| 2773 | break; |
| 2774 | default: |
| 2775 | break; |
| 2776 | } |
| 2777 | |
| 2778 | goto error; |
| 2779 | } |
| 2780 | |
| 2781 | ua_event_notifier_rule->enabled = true; |
| 2782 | |
| 2783 | error: |
| 2784 | health_code_update(); |
| 2785 | return ret; |
| 2786 | } |
| 2787 | |
| 2788 | /* |
| 2789 | * Copy data between an UST app event and a LTT event. |
| 2790 | */ |
| 2791 | static void shadow_copy_event(struct ust_app_event *ua_event, |
| 2792 | struct ltt_ust_event *uevent) |
| 2793 | { |
| 2794 | size_t exclusion_alloc_size; |
| 2795 | |
| 2796 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
| 2797 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 2798 | |
| 2799 | ua_event->enabled = uevent->enabled; |
| 2800 | |
| 2801 | /* Copy event attributes */ |
| 2802 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); |
| 2803 | |
| 2804 | /* Copy filter bytecode */ |
| 2805 | if (uevent->filter) { |
| 2806 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
| 2807 | /* Filter might be NULL here in case of ENONEM. */ |
| 2808 | } |
| 2809 | |
| 2810 | /* Copy exclusion data */ |
| 2811 | if (uevent->exclusion) { |
| 2812 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
| 2813 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
| 2814 | ua_event->exclusion = zmalloc(exclusion_alloc_size); |
| 2815 | if (ua_event->exclusion == NULL) { |
| 2816 | PERROR("malloc"); |
| 2817 | } else { |
| 2818 | memcpy(ua_event->exclusion, uevent->exclusion, |
| 2819 | exclusion_alloc_size); |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | /* |
| 2825 | * Copy data between an UST app channel and a LTT channel. |
| 2826 | */ |
| 2827 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
| 2828 | struct ltt_ust_channel *uchan) |
| 2829 | { |
| 2830 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
| 2831 | |
| 2832 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); |
| 2833 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 2834 | |
| 2835 | ua_chan->tracefile_size = uchan->tracefile_size; |
| 2836 | ua_chan->tracefile_count = uchan->tracefile_count; |
| 2837 | |
| 2838 | /* Copy event attributes since the layout is different. */ |
| 2839 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; |
| 2840 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; |
| 2841 | ua_chan->attr.overwrite = uchan->attr.overwrite; |
| 2842 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; |
| 2843 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; |
| 2844 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
| 2845 | ua_chan->attr.output = uchan->attr.output; |
| 2846 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
| 2847 | |
| 2848 | /* |
| 2849 | * Note that the attribute channel type is not set since the channel on the |
| 2850 | * tracing registry side does not have this information. |
| 2851 | */ |
| 2852 | |
| 2853 | ua_chan->enabled = uchan->enabled; |
| 2854 | ua_chan->tracing_channel_id = uchan->id; |
| 2855 | |
| 2856 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
| 2857 | } |
| 2858 | |
| 2859 | /* |
| 2860 | * Copy data between a UST app session and a regular LTT session. |
| 2861 | */ |
| 2862 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
| 2863 | struct ltt_ust_session *usess, struct ust_app *app) |
| 2864 | { |
| 2865 | struct tm *timeinfo; |
| 2866 | char datetime[16]; |
| 2867 | int ret; |
| 2868 | char tmp_shm_path[PATH_MAX]; |
| 2869 | |
| 2870 | timeinfo = localtime(&app->registration_time); |
| 2871 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 2872 | |
| 2873 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
| 2874 | |
| 2875 | ua_sess->tracing_id = usess->id; |
| 2876 | ua_sess->id = get_next_session_id(); |
| 2877 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
| 2878 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); |
| 2879 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); |
| 2880 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); |
| 2881 | ua_sess->buffer_type = usess->buffer_type; |
| 2882 | ua_sess->bits_per_long = app->bits_per_long; |
| 2883 | |
| 2884 | /* There is only one consumer object per session possible. */ |
| 2885 | consumer_output_get(usess->consumer); |
| 2886 | ua_sess->consumer = usess->consumer; |
| 2887 | |
| 2888 | ua_sess->output_traces = usess->output_traces; |
| 2889 | ua_sess->live_timer_interval = usess->live_timer_interval; |
| 2890 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
| 2891 | &usess->metadata_attr); |
| 2892 | |
| 2893 | switch (ua_sess->buffer_type) { |
| 2894 | case LTTNG_BUFFER_PER_PID: |
| 2895 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), |
| 2896 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
| 2897 | datetime); |
| 2898 | break; |
| 2899 | case LTTNG_BUFFER_PER_UID: |
| 2900 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), |
| 2901 | DEFAULT_UST_TRACE_UID_PATH, |
| 2902 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
| 2903 | app->bits_per_long); |
| 2904 | break; |
| 2905 | default: |
| 2906 | assert(0); |
| 2907 | goto error; |
| 2908 | } |
| 2909 | if (ret < 0) { |
| 2910 | PERROR("asprintf UST shadow copy session"); |
| 2911 | assert(0); |
| 2912 | goto error; |
| 2913 | } |
| 2914 | |
| 2915 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
| 2916 | sizeof(ua_sess->root_shm_path)); |
| 2917 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; |
| 2918 | strncpy(ua_sess->shm_path, usess->shm_path, |
| 2919 | sizeof(ua_sess->shm_path)); |
| 2920 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 2921 | if (ua_sess->shm_path[0]) { |
| 2922 | switch (ua_sess->buffer_type) { |
| 2923 | case LTTNG_BUFFER_PER_PID: |
| 2924 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), |
| 2925 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
| 2926 | app->name, app->pid, datetime); |
| 2927 | break; |
| 2928 | case LTTNG_BUFFER_PER_UID: |
| 2929 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), |
| 2930 | "/" DEFAULT_UST_TRACE_UID_PATH, |
| 2931 | app->uid, app->bits_per_long); |
| 2932 | break; |
| 2933 | default: |
| 2934 | assert(0); |
| 2935 | goto error; |
| 2936 | } |
| 2937 | if (ret < 0) { |
| 2938 | PERROR("sprintf UST shadow copy session"); |
| 2939 | assert(0); |
| 2940 | goto error; |
| 2941 | } |
| 2942 | strncat(ua_sess->shm_path, tmp_shm_path, |
| 2943 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); |
| 2944 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; |
| 2945 | } |
| 2946 | return; |
| 2947 | |
| 2948 | error: |
| 2949 | consumer_output_put(ua_sess->consumer); |
| 2950 | } |
| 2951 | |
| 2952 | /* |
| 2953 | * Lookup sesison wrapper. |
| 2954 | */ |
| 2955 | static |
| 2956 | void __lookup_session_by_app(const struct ltt_ust_session *usess, |
| 2957 | struct ust_app *app, struct lttng_ht_iter *iter) |
| 2958 | { |
| 2959 | /* Get right UST app session from app */ |
| 2960 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
| 2961 | } |
| 2962 | |
| 2963 | /* |
| 2964 | * Return ust app session from the app session hashtable using the UST session |
| 2965 | * id. |
| 2966 | */ |
| 2967 | static struct ust_app_session *lookup_session_by_app( |
| 2968 | const struct ltt_ust_session *usess, struct ust_app *app) |
| 2969 | { |
| 2970 | struct lttng_ht_iter iter; |
| 2971 | struct lttng_ht_node_u64 *node; |
| 2972 | |
| 2973 | __lookup_session_by_app(usess, app, &iter); |
| 2974 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2975 | if (node == NULL) { |
| 2976 | goto error; |
| 2977 | } |
| 2978 | |
| 2979 | return caa_container_of(node, struct ust_app_session, node); |
| 2980 | |
| 2981 | error: |
| 2982 | return NULL; |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * Setup buffer registry per PID for the given session and application. If none |
| 2987 | * is found, a new one is created, added to the global registry and |
| 2988 | * initialized. If regp is valid, it's set with the newly created object. |
| 2989 | * |
| 2990 | * Return 0 on success or else a negative value. |
| 2991 | */ |
| 2992 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, |
| 2993 | struct ust_app *app, struct buffer_reg_pid **regp) |
| 2994 | { |
| 2995 | int ret = 0; |
| 2996 | struct buffer_reg_pid *reg_pid; |
| 2997 | |
| 2998 | assert(ua_sess); |
| 2999 | assert(app); |
| 3000 | |
| 3001 | rcu_read_lock(); |
| 3002 | |
| 3003 | reg_pid = buffer_reg_pid_find(ua_sess->id); |
| 3004 | if (!reg_pid) { |
| 3005 | /* |
| 3006 | * This is the create channel path meaning that if there is NO |
| 3007 | * registry available, we have to create one for this session. |
| 3008 | */ |
| 3009 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
| 3010 | ua_sess->root_shm_path, ua_sess->shm_path); |
| 3011 | if (ret < 0) { |
| 3012 | goto error; |
| 3013 | } |
| 3014 | } else { |
| 3015 | goto end; |
| 3016 | } |
| 3017 | |
| 3018 | /* Initialize registry. */ |
| 3019 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, |
| 3020 | app->bits_per_long, app->uint8_t_alignment, |
| 3021 | app->uint16_t_alignment, app->uint32_t_alignment, |
| 3022 | app->uint64_t_alignment, app->long_alignment, |
| 3023 | app->byte_order, app->version.major, app->version.minor, |
| 3024 | reg_pid->root_shm_path, reg_pid->shm_path, |
| 3025 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 3026 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 3027 | ua_sess->tracing_id, |
| 3028 | app->uid); |
| 3029 | if (ret < 0) { |
| 3030 | /* |
| 3031 | * reg_pid->registry->reg.ust is NULL upon error, so we need to |
| 3032 | * destroy the buffer registry, because it is always expected |
| 3033 | * that if the buffer registry can be found, its ust registry is |
| 3034 | * non-NULL. |
| 3035 | */ |
| 3036 | buffer_reg_pid_destroy(reg_pid); |
| 3037 | goto error; |
| 3038 | } |
| 3039 | |
| 3040 | buffer_reg_pid_add(reg_pid); |
| 3041 | |
| 3042 | DBG3("UST app buffer registry per PID created successfully"); |
| 3043 | |
| 3044 | end: |
| 3045 | if (regp) { |
| 3046 | *regp = reg_pid; |
| 3047 | } |
| 3048 | error: |
| 3049 | rcu_read_unlock(); |
| 3050 | return ret; |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | * Setup buffer registry per UID for the given session and application. If none |
| 3055 | * is found, a new one is created, added to the global registry and |
| 3056 | * initialized. If regp is valid, it's set with the newly created object. |
| 3057 | * |
| 3058 | * Return 0 on success or else a negative value. |
| 3059 | */ |
| 3060 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, |
| 3061 | struct ust_app_session *ua_sess, |
| 3062 | struct ust_app *app, struct buffer_reg_uid **regp) |
| 3063 | { |
| 3064 | int ret = 0; |
| 3065 | struct buffer_reg_uid *reg_uid; |
| 3066 | |
| 3067 | assert(usess); |
| 3068 | assert(app); |
| 3069 | |
| 3070 | rcu_read_lock(); |
| 3071 | |
| 3072 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 3073 | if (!reg_uid) { |
| 3074 | /* |
| 3075 | * This is the create channel path meaning that if there is NO |
| 3076 | * registry available, we have to create one for this session. |
| 3077 | */ |
| 3078 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, |
| 3079 | LTTNG_DOMAIN_UST, ®_uid, |
| 3080 | ua_sess->root_shm_path, ua_sess->shm_path); |
| 3081 | if (ret < 0) { |
| 3082 | goto error; |
| 3083 | } |
| 3084 | } else { |
| 3085 | goto end; |
| 3086 | } |
| 3087 | |
| 3088 | /* Initialize registry. */ |
| 3089 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
| 3090 | app->bits_per_long, app->uint8_t_alignment, |
| 3091 | app->uint16_t_alignment, app->uint32_t_alignment, |
| 3092 | app->uint64_t_alignment, app->long_alignment, |
| 3093 | app->byte_order, app->version.major, |
| 3094 | app->version.minor, reg_uid->root_shm_path, |
| 3095 | reg_uid->shm_path, usess->uid, usess->gid, |
| 3096 | ua_sess->tracing_id, app->uid); |
| 3097 | if (ret < 0) { |
| 3098 | /* |
| 3099 | * reg_uid->registry->reg.ust is NULL upon error, so we need to |
| 3100 | * destroy the buffer registry, because it is always expected |
| 3101 | * that if the buffer registry can be found, its ust registry is |
| 3102 | * non-NULL. |
| 3103 | */ |
| 3104 | buffer_reg_uid_destroy(reg_uid, NULL); |
| 3105 | goto error; |
| 3106 | } |
| 3107 | /* Add node to teardown list of the session. */ |
| 3108 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); |
| 3109 | |
| 3110 | buffer_reg_uid_add(reg_uid); |
| 3111 | |
| 3112 | DBG3("UST app buffer registry per UID created successfully"); |
| 3113 | end: |
| 3114 | if (regp) { |
| 3115 | *regp = reg_uid; |
| 3116 | } |
| 3117 | error: |
| 3118 | rcu_read_unlock(); |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
| 3122 | /* |
| 3123 | * Create a session on the tracer side for the given app. |
| 3124 | * |
| 3125 | * On success, ua_sess_ptr is populated with the session pointer or else left |
| 3126 | * untouched. If the session was created, is_created is set to 1. On error, |
| 3127 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can |
| 3128 | * be NULL. |
| 3129 | * |
| 3130 | * Returns 0 on success or else a negative code which is either -ENOMEM or |
| 3131 | * -ENOTCONN which is the default code if the ustctl_create_session fails. |
| 3132 | */ |
| 3133 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
| 3134 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
| 3135 | int *is_created) |
| 3136 | { |
| 3137 | int ret, created = 0; |
| 3138 | struct ust_app_session *ua_sess; |
| 3139 | |
| 3140 | assert(usess); |
| 3141 | assert(app); |
| 3142 | assert(ua_sess_ptr); |
| 3143 | |
| 3144 | health_code_update(); |
| 3145 | |
| 3146 | ua_sess = lookup_session_by_app(usess, app); |
| 3147 | if (ua_sess == NULL) { |
| 3148 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
| 3149 | app->pid, usess->id); |
| 3150 | ua_sess = alloc_ust_app_session(); |
| 3151 | if (ua_sess == NULL) { |
| 3152 | /* Only malloc can failed so something is really wrong */ |
| 3153 | ret = -ENOMEM; |
| 3154 | goto error; |
| 3155 | } |
| 3156 | shadow_copy_session(ua_sess, usess, app); |
| 3157 | created = 1; |
| 3158 | } |
| 3159 | |
| 3160 | switch (usess->buffer_type) { |
| 3161 | case LTTNG_BUFFER_PER_PID: |
| 3162 | /* Init local registry. */ |
| 3163 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); |
| 3164 | if (ret < 0) { |
| 3165 | delete_ust_app_session(-1, ua_sess, app); |
| 3166 | goto error; |
| 3167 | } |
| 3168 | break; |
| 3169 | case LTTNG_BUFFER_PER_UID: |
| 3170 | /* Look for a global registry. If none exists, create one. */ |
| 3171 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
| 3172 | if (ret < 0) { |
| 3173 | delete_ust_app_session(-1, ua_sess, app); |
| 3174 | goto error; |
| 3175 | } |
| 3176 | break; |
| 3177 | default: |
| 3178 | assert(0); |
| 3179 | ret = -EINVAL; |
| 3180 | goto error; |
| 3181 | } |
| 3182 | |
| 3183 | health_code_update(); |
| 3184 | |
| 3185 | if (ua_sess->handle == -1) { |
| 3186 | pthread_mutex_lock(&app->sock_lock); |
| 3187 | ret = ustctl_create_session(app->sock); |
| 3188 | pthread_mutex_unlock(&app->sock_lock); |
| 3189 | if (ret < 0) { |
| 3190 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3191 | ERR("Creating session for app pid %d with ret %d", |
| 3192 | app->pid, ret); |
| 3193 | } else { |
| 3194 | DBG("UST app creating session failed. Application is dead"); |
| 3195 | /* |
| 3196 | * This is normal behavior, an application can die during the |
| 3197 | * creation process. Don't report an error so the execution can |
| 3198 | * continue normally. This will get flagged ENOTCONN and the |
| 3199 | * caller will handle it. |
| 3200 | */ |
| 3201 | ret = 0; |
| 3202 | } |
| 3203 | delete_ust_app_session(-1, ua_sess, app); |
| 3204 | if (ret != -ENOMEM) { |
| 3205 | /* |
| 3206 | * Tracer is probably gone or got an internal error so let's |
| 3207 | * behave like it will soon unregister or not usable. |
| 3208 | */ |
| 3209 | ret = -ENOTCONN; |
| 3210 | } |
| 3211 | goto error; |
| 3212 | } |
| 3213 | |
| 3214 | ua_sess->handle = ret; |
| 3215 | |
| 3216 | /* Add ust app session to app's HT */ |
| 3217 | lttng_ht_node_init_u64(&ua_sess->node, |
| 3218 | ua_sess->tracing_id); |
| 3219 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); |
| 3220 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
| 3221 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, |
| 3222 | &ua_sess->ust_objd_node); |
| 3223 | |
| 3224 | DBG2("UST app session created successfully with handle %d", ret); |
| 3225 | } |
| 3226 | |
| 3227 | *ua_sess_ptr = ua_sess; |
| 3228 | if (is_created) { |
| 3229 | *is_created = created; |
| 3230 | } |
| 3231 | |
| 3232 | /* Everything went well. */ |
| 3233 | ret = 0; |
| 3234 | |
| 3235 | error: |
| 3236 | health_code_update(); |
| 3237 | return ret; |
| 3238 | } |
| 3239 | |
| 3240 | /* |
| 3241 | * Match function for a hash table lookup of ust_app_ctx. |
| 3242 | * |
| 3243 | * It matches an ust app context based on the context type and, in the case |
| 3244 | * of perf counters, their name. |
| 3245 | */ |
| 3246 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) |
| 3247 | { |
| 3248 | struct ust_app_ctx *ctx; |
| 3249 | const struct lttng_ust_context_attr *key; |
| 3250 | |
| 3251 | assert(node); |
| 3252 | assert(_key); |
| 3253 | |
| 3254 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); |
| 3255 | key = _key; |
| 3256 | |
| 3257 | /* Context type */ |
| 3258 | if (ctx->ctx.ctx != key->ctx) { |
| 3259 | goto no_match; |
| 3260 | } |
| 3261 | |
| 3262 | switch(key->ctx) { |
| 3263 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: |
| 3264 | if (strncmp(key->u.perf_counter.name, |
| 3265 | ctx->ctx.u.perf_counter.name, |
| 3266 | sizeof(key->u.perf_counter.name))) { |
| 3267 | goto no_match; |
| 3268 | } |
| 3269 | break; |
| 3270 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
| 3271 | if (strcmp(key->u.app_ctx.provider_name, |
| 3272 | ctx->ctx.u.app_ctx.provider_name) || |
| 3273 | strcmp(key->u.app_ctx.ctx_name, |
| 3274 | ctx->ctx.u.app_ctx.ctx_name)) { |
| 3275 | goto no_match; |
| 3276 | } |
| 3277 | break; |
| 3278 | default: |
| 3279 | break; |
| 3280 | } |
| 3281 | |
| 3282 | /* Match. */ |
| 3283 | return 1; |
| 3284 | |
| 3285 | no_match: |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
| 3289 | /* |
| 3290 | * Lookup for an ust app context from an lttng_ust_context. |
| 3291 | * |
| 3292 | * Must be called while holding RCU read side lock. |
| 3293 | * Return an ust_app_ctx object or NULL on error. |
| 3294 | */ |
| 3295 | static |
| 3296 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, |
| 3297 | struct lttng_ust_context_attr *uctx) |
| 3298 | { |
| 3299 | struct lttng_ht_iter iter; |
| 3300 | struct lttng_ht_node_ulong *node; |
| 3301 | struct ust_app_ctx *app_ctx = NULL; |
| 3302 | |
| 3303 | assert(uctx); |
| 3304 | assert(ht); |
| 3305 | |
| 3306 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ |
| 3307 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), |
| 3308 | ht_match_ust_app_ctx, uctx, &iter.iter); |
| 3309 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 3310 | if (!node) { |
| 3311 | goto end; |
| 3312 | } |
| 3313 | |
| 3314 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); |
| 3315 | |
| 3316 | end: |
| 3317 | return app_ctx; |
| 3318 | } |
| 3319 | |
| 3320 | /* |
| 3321 | * Create a context for the channel on the tracer. |
| 3322 | * |
| 3323 | * Called with UST app session lock held and a RCU read side lock. |
| 3324 | */ |
| 3325 | static |
| 3326 | int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
| 3327 | struct lttng_ust_context_attr *uctx, |
| 3328 | struct ust_app *app) |
| 3329 | { |
| 3330 | int ret = 0; |
| 3331 | struct ust_app_ctx *ua_ctx; |
| 3332 | |
| 3333 | DBG2("UST app adding context to channel %s", ua_chan->name); |
| 3334 | |
| 3335 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
| 3336 | if (ua_ctx) { |
| 3337 | ret = -EEXIST; |
| 3338 | goto error; |
| 3339 | } |
| 3340 | |
| 3341 | ua_ctx = alloc_ust_app_ctx(uctx); |
| 3342 | if (ua_ctx == NULL) { |
| 3343 | /* malloc failed */ |
| 3344 | ret = -ENOMEM; |
| 3345 | goto error; |
| 3346 | } |
| 3347 | |
| 3348 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
| 3349 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
| 3350 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
| 3351 | |
| 3352 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 3353 | if (ret < 0) { |
| 3354 | goto error; |
| 3355 | } |
| 3356 | |
| 3357 | error: |
| 3358 | return ret; |
| 3359 | } |
| 3360 | |
| 3361 | /* |
| 3362 | * Enable on the tracer side a ust app event for the session and channel. |
| 3363 | * |
| 3364 | * Called with UST app session lock held. |
| 3365 | */ |
| 3366 | static |
| 3367 | int enable_ust_app_event(struct ust_app_session *ua_sess, |
| 3368 | struct ust_app_event *ua_event, struct ust_app *app) |
| 3369 | { |
| 3370 | int ret; |
| 3371 | |
| 3372 | ret = enable_ust_object(app, ua_event->obj); |
| 3373 | if (ret < 0) { |
| 3374 | goto error; |
| 3375 | } |
| 3376 | |
| 3377 | ua_event->enabled = 1; |
| 3378 | |
| 3379 | error: |
| 3380 | return ret; |
| 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * Disable on the tracer side a ust app event for the session and channel. |
| 3385 | */ |
| 3386 | static int disable_ust_app_event(struct ust_app_session *ua_sess, |
| 3387 | struct ust_app_event *ua_event, struct ust_app *app) |
| 3388 | { |
| 3389 | int ret; |
| 3390 | |
| 3391 | ret = disable_ust_object(app, ua_event->obj); |
| 3392 | if (ret < 0) { |
| 3393 | goto error; |
| 3394 | } |
| 3395 | |
| 3396 | ua_event->enabled = 0; |
| 3397 | |
| 3398 | error: |
| 3399 | return ret; |
| 3400 | } |
| 3401 | |
| 3402 | /* |
| 3403 | * Lookup ust app channel for session and disable it on the tracer side. |
| 3404 | */ |
| 3405 | static |
| 3406 | int disable_ust_app_channel(struct ust_app_session *ua_sess, |
| 3407 | struct ust_app_channel *ua_chan, struct ust_app *app) |
| 3408 | { |
| 3409 | int ret; |
| 3410 | |
| 3411 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 3412 | if (ret < 0) { |
| 3413 | goto error; |
| 3414 | } |
| 3415 | |
| 3416 | ua_chan->enabled = 0; |
| 3417 | |
| 3418 | error: |
| 3419 | return ret; |
| 3420 | } |
| 3421 | |
| 3422 | /* |
| 3423 | * Lookup ust app map for session and disable it on the tracer side. |
| 3424 | */ |
| 3425 | static |
| 3426 | int disable_ust_app_map(struct ust_app_session *ua_sess, |
| 3427 | struct ust_app_map *ua_map, struct ust_app *app) |
| 3428 | { |
| 3429 | int ret; |
| 3430 | |
| 3431 | ret = disable_ust_map(app, ua_sess, ua_map); |
| 3432 | if (ret < 0) { |
| 3433 | goto error; |
| 3434 | } |
| 3435 | |
| 3436 | ua_map->enabled = 0; |
| 3437 | |
| 3438 | error: |
| 3439 | return ret; |
| 3440 | } |
| 3441 | |
| 3442 | /* |
| 3443 | * Lookup ust app channel for session and enable it on the tracer side. This |
| 3444 | * MUST be called with a RCU read side lock acquired. |
| 3445 | */ |
| 3446 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, |
| 3447 | struct ltt_ust_channel *uchan, struct ust_app *app) |
| 3448 | { |
| 3449 | int ret = 0; |
| 3450 | struct lttng_ht_iter iter; |
| 3451 | struct lttng_ht_node_str *ua_chan_node; |
| 3452 | struct ust_app_channel *ua_chan; |
| 3453 | |
| 3454 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 3455 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 3456 | if (ua_chan_node == NULL) { |
| 3457 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
| 3458 | uchan->name, ua_sess->tracing_id); |
| 3459 | goto error; |
| 3460 | } |
| 3461 | |
| 3462 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3463 | |
| 3464 | ret = enable_ust_channel(app, ua_sess, ua_chan); |
| 3465 | if (ret < 0) { |
| 3466 | goto error; |
| 3467 | } |
| 3468 | |
| 3469 | error: |
| 3470 | return ret; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Lookup ust app map for session and enable it on the tracer side. This |
| 3475 | * MUST be called with a RCU read side lock acquired. |
| 3476 | */ |
| 3477 | static int enable_ust_app_map(struct ust_app_session *ua_sess, |
| 3478 | struct ltt_ust_map *umap, struct ust_app *app) |
| 3479 | { |
| 3480 | int ret = 0; |
| 3481 | struct lttng_ht_iter iter; |
| 3482 | struct lttng_ht_node_str *ua_map_node; |
| 3483 | struct ust_app_map *ua_map; |
| 3484 | |
| 3485 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &iter); |
| 3486 | ua_map_node = lttng_ht_iter_get_node_str(&iter); |
| 3487 | if (ua_map_node == NULL) { |
| 3488 | DBG2("Unable to find map %s in ust session id %" PRIu64, |
| 3489 | umap->name, ua_sess->tracing_id); |
| 3490 | goto error; |
| 3491 | } |
| 3492 | |
| 3493 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 3494 | |
| 3495 | ret = enable_ust_map(app, ua_sess, ua_map); |
| 3496 | if (ret < 0) { |
| 3497 | goto error; |
| 3498 | } |
| 3499 | |
| 3500 | error: |
| 3501 | return ret; |
| 3502 | } |
| 3503 | |
| 3504 | /* |
| 3505 | * Ask the consumer to create a channel and get it if successful. |
| 3506 | * |
| 3507 | * Called with UST app session lock held. |
| 3508 | * |
| 3509 | * Return 0 on success or else a negative value. |
| 3510 | */ |
| 3511 | static int do_consumer_create_channel(struct ltt_ust_session *usess, |
| 3512 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, |
| 3513 | int bitness, struct ust_registry_session *registry, |
| 3514 | uint64_t trace_archive_id) |
| 3515 | { |
| 3516 | int ret; |
| 3517 | unsigned int nb_fd = 0; |
| 3518 | struct consumer_socket *socket; |
| 3519 | |
| 3520 | assert(usess); |
| 3521 | assert(ua_sess); |
| 3522 | assert(ua_chan); |
| 3523 | assert(registry); |
| 3524 | |
| 3525 | rcu_read_lock(); |
| 3526 | health_code_update(); |
| 3527 | |
| 3528 | /* Get the right consumer socket for the application. */ |
| 3529 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); |
| 3530 | if (!socket) { |
| 3531 | ret = -EINVAL; |
| 3532 | goto error; |
| 3533 | } |
| 3534 | |
| 3535 | health_code_update(); |
| 3536 | |
| 3537 | /* Need one fd for the channel. */ |
| 3538 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3539 | if (ret < 0) { |
| 3540 | ERR("Exhausted number of available FD upon create channel"); |
| 3541 | goto error; |
| 3542 | } |
| 3543 | |
| 3544 | /* |
| 3545 | * Ask consumer to create channel. The consumer will return the number of |
| 3546 | * stream we have to expect. |
| 3547 | */ |
| 3548 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, |
| 3549 | registry, usess->current_trace_chunk); |
| 3550 | if (ret < 0) { |
| 3551 | goto error_ask; |
| 3552 | } |
| 3553 | |
| 3554 | /* |
| 3555 | * Compute the number of fd needed before receiving them. It must be 2 per |
| 3556 | * stream (2 being the default value here). |
| 3557 | */ |
| 3558 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; |
| 3559 | |
| 3560 | /* Reserve the amount of file descriptor we need. */ |
| 3561 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); |
| 3562 | if (ret < 0) { |
| 3563 | ERR("Exhausted number of available FD upon create channel"); |
| 3564 | goto error_fd_get_stream; |
| 3565 | } |
| 3566 | |
| 3567 | health_code_update(); |
| 3568 | |
| 3569 | /* |
| 3570 | * Now get the channel from the consumer. This call wil populate the stream |
| 3571 | * list of that channel and set the ust objects. |
| 3572 | */ |
| 3573 | if (usess->consumer->enabled) { |
| 3574 | ret = ust_consumer_get_channel(socket, ua_chan); |
| 3575 | if (ret < 0) { |
| 3576 | goto error_destroy; |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | rcu_read_unlock(); |
| 3581 | return 0; |
| 3582 | |
| 3583 | error_destroy: |
| 3584 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); |
| 3585 | error_fd_get_stream: |
| 3586 | /* |
| 3587 | * Initiate a destroy channel on the consumer since we had an error |
| 3588 | * handling it on our side. The return value is of no importance since we |
| 3589 | * already have a ret value set by the previous error that we need to |
| 3590 | * return. |
| 3591 | */ |
| 3592 | (void) ust_consumer_destroy_channel(socket, ua_chan); |
| 3593 | error_ask: |
| 3594 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3595 | error: |
| 3596 | health_code_update(); |
| 3597 | rcu_read_unlock(); |
| 3598 | return ret; |
| 3599 | } |
| 3600 | |
| 3601 | static int create_map_object(struct ltt_ust_session *usess, |
| 3602 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) |
| 3603 | { |
| 3604 | int i, ret, nr_counter_cpu; |
| 3605 | struct ustctl_counter_dimension dimension[1] = {0}; |
| 3606 | struct ustctl_daemon_counter *daemon_counter; |
| 3607 | struct lttng_ust_object_data **counter_cpus; |
| 3608 | enum ustctl_counter_bitness bitness; |
| 3609 | int *counter_cpu_fds; |
| 3610 | |
| 3611 | assert(usess); |
| 3612 | assert(ua_sess); |
| 3613 | assert(ua_map); |
| 3614 | assert(ua_map->bucket_count > 0); |
| 3615 | |
| 3616 | DBG("Creating UST map \"%s\"", ua_map->name); |
| 3617 | |
| 3618 | if (ua_map->bitness == LTTNG_MAP_BITNESS_32BITS) { |
| 3619 | bitness = USTCTL_COUNTER_BITNESS_32; |
| 3620 | } else { |
| 3621 | bitness = USTCTL_COUNTER_BITNESS_64; |
| 3622 | } |
| 3623 | |
| 3624 | nr_counter_cpu = ustctl_get_nr_cpu_per_counter(); |
| 3625 | counter_cpu_fds = zmalloc(nr_counter_cpu * sizeof(*counter_cpu_fds)); |
| 3626 | if (!counter_cpu_fds) { |
| 3627 | ret = -1; |
| 3628 | goto end; |
| 3629 | } |
| 3630 | |
| 3631 | counter_cpus = zmalloc(nr_counter_cpu * sizeof(**counter_cpus)); |
| 3632 | if (!counter_cpus) { |
| 3633 | ret = -1; |
| 3634 | goto free_cpu_fds; |
| 3635 | } |
| 3636 | |
| 3637 | /* Need one fd for each cpu counter of the map. */ |
| 3638 | ret = lttng_fd_get(LTTNG_FD_APPS, nr_counter_cpu); |
| 3639 | if (ret < 0) { |
| 3640 | ERR("Exhausted number of available FD upon create map"); |
| 3641 | goto free_cpu_counters; |
| 3642 | } |
| 3643 | |
| 3644 | for (i = 0; i < nr_counter_cpu; i++) { |
| 3645 | counter_cpu_fds[i] = shm_create_anonymous("ust-map-counter"); |
| 3646 | if (counter_cpu_fds[i] < 0) { |
| 3647 | ERR("Error creating anonymous shared memory object"); |
| 3648 | ret = -1; |
| 3649 | goto error; |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | dimension[0].size = ua_map->bucket_count; |
| 3654 | dimension[0].has_underflow = false; |
| 3655 | dimension[0].has_overflow = false; |
| 3656 | |
| 3657 | daemon_counter = ustctl_create_counter(1, dimension, 0, -1, |
| 3658 | nr_counter_cpu, counter_cpu_fds, |
| 3659 | bitness, |
| 3660 | USTCTL_COUNTER_ARITHMETIC_MODULAR, |
| 3661 | USTCTL_COUNTER_ALLOC_PER_CPU, |
| 3662 | ua_map->coalesce_hits); |
| 3663 | assert(daemon_counter); |
| 3664 | |
| 3665 | DBG("Created daemon counter succesfully"); |
| 3666 | |
| 3667 | ua_map->map_handle = daemon_counter; |
| 3668 | |
| 3669 | ret = ustctl_create_counter_data(daemon_counter, &ua_map->obj); |
| 3670 | assert(ret == 0); |
| 3671 | DBG("Created counter data succesfully"); |
| 3672 | |
| 3673 | for (i = 0; i < nr_counter_cpu; i++) { |
| 3674 | struct ust_app_map_counter *counter; |
| 3675 | |
| 3676 | /* Create UST counter */ |
| 3677 | counter = ust_app_alloc_map_counter(); |
| 3678 | if (counter == NULL) { |
| 3679 | ret = -ENOMEM; |
| 3680 | goto release_counters; |
| 3681 | } |
| 3682 | |
| 3683 | ret = ustctl_create_counter_cpu_data(daemon_counter, i, |
| 3684 | &counter->obj); |
| 3685 | if (ret < 0) { |
| 3686 | ERR("Creating map counter cpu data"); |
| 3687 | free(counter); |
| 3688 | goto error; |
| 3689 | } |
| 3690 | |
| 3691 | cds_list_add_tail(&counter->list, &ua_map->counters.head); |
| 3692 | ua_map->counters.count++; |
| 3693 | |
| 3694 | DBG2("UST app map counter %d created successfully", |
| 3695 | ua_map->counters.count); |
| 3696 | } |
| 3697 | |
| 3698 | ret = 0; |
| 3699 | goto end; |
| 3700 | |
| 3701 | error: |
| 3702 | release_counters: |
| 3703 | //TODO |
| 3704 | free_cpu_counters: |
| 3705 | free(counter_cpus); |
| 3706 | |
| 3707 | free_cpu_fds: |
| 3708 | free(counter_cpu_fds); |
| 3709 | end: |
| 3710 | return ret; |
| 3711 | } |
| 3712 | |
| 3713 | /* |
| 3714 | * Duplicate the ust data object of the ust app stream and save it in the |
| 3715 | * buffer registry stream. |
| 3716 | * |
| 3717 | * Return 0 on success or else a negative value. |
| 3718 | */ |
| 3719 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, |
| 3720 | struct ust_app_stream *stream) |
| 3721 | { |
| 3722 | int ret; |
| 3723 | |
| 3724 | assert(reg_stream); |
| 3725 | assert(stream); |
| 3726 | |
| 3727 | /* Duplicating a stream requires 2 new fds. Reserve them. */ |
| 3728 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 3729 | if (ret < 0) { |
| 3730 | ERR("Exhausted number of available FD upon duplicate stream"); |
| 3731 | goto error; |
| 3732 | } |
| 3733 | |
| 3734 | /* Duplicate object for stream once the original is in the registry. */ |
| 3735 | ret = ustctl_duplicate_ust_object_data(&stream->obj, |
| 3736 | reg_stream->obj.ust); |
| 3737 | if (ret < 0) { |
| 3738 | ERR("Duplicate stream obj from %p to %p failed with ret %d", |
| 3739 | reg_stream->obj.ust, stream->obj, ret); |
| 3740 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 3741 | goto error; |
| 3742 | } |
| 3743 | stream->handle = stream->obj->handle; |
| 3744 | |
| 3745 | error: |
| 3746 | return ret; |
| 3747 | } |
| 3748 | |
| 3749 | /* |
| 3750 | * Duplicate the ust data object of the ust app map_counter and save it in the |
| 3751 | * buffer registry map_counter. |
| 3752 | * |
| 3753 | * Return 0 on success or else a negative value. |
| 3754 | */ |
| 3755 | static int duplicate_map_counter_object(struct buffer_reg_map_counter *reg_map_counter, |
| 3756 | struct ust_app_map_counter *map_counter) |
| 3757 | { |
| 3758 | int ret; |
| 3759 | |
| 3760 | assert(reg_map_counter); |
| 3761 | assert(map_counter); |
| 3762 | |
| 3763 | /* Duplicating a map_counter requires 2 new fds. Reserve them. */ |
| 3764 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 3765 | if (ret < 0) { |
| 3766 | ERR("Exhausted number of available FD upon duplicate map_counter"); |
| 3767 | goto error; |
| 3768 | } |
| 3769 | |
| 3770 | /* Duplicate object for map_counter once the original is in the registry. */ |
| 3771 | ret = ustctl_duplicate_ust_object_data(&map_counter->obj, |
| 3772 | reg_map_counter->obj.ust); |
| 3773 | if (ret < 0) { |
| 3774 | ERR("Duplicate map_counter obj from %p to %p failed with ret %d", |
| 3775 | reg_map_counter->obj.ust, map_counter->obj, ret); |
| 3776 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 3777 | goto error; |
| 3778 | } |
| 3779 | map_counter->handle = map_counter->obj->handle; |
| 3780 | |
| 3781 | error: |
| 3782 | return ret; |
| 3783 | } |
| 3784 | |
| 3785 | /* |
| 3786 | * Duplicate the ust data object of the ust app. channel and save it in the |
| 3787 | * buffer registry channel. |
| 3788 | * |
| 3789 | * Return 0 on success or else a negative value. |
| 3790 | */ |
| 3791 | static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan, |
| 3792 | struct ust_app_channel *ua_chan) |
| 3793 | { |
| 3794 | int ret; |
| 3795 | |
| 3796 | assert(buf_reg_chan); |
| 3797 | assert(ua_chan); |
| 3798 | |
| 3799 | /* Duplicating a channel requires 1 new fd. Reserve it. */ |
| 3800 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3801 | if (ret < 0) { |
| 3802 | ERR("Exhausted number of available FD upon duplicate channel"); |
| 3803 | goto error_fd_get; |
| 3804 | } |
| 3805 | |
| 3806 | /* Duplicate object for stream once the original is in the registry. */ |
| 3807 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust); |
| 3808 | if (ret < 0) { |
| 3809 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", |
| 3810 | buf_reg_chan->obj.ust, ua_chan->obj, ret); |
| 3811 | goto error; |
| 3812 | } |
| 3813 | ua_chan->handle = ua_chan->obj->handle; |
| 3814 | |
| 3815 | return 0; |
| 3816 | |
| 3817 | error: |
| 3818 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3819 | error_fd_get: |
| 3820 | return ret; |
| 3821 | } |
| 3822 | |
| 3823 | /* |
| 3824 | * Duplicate the ust data object of the ust app. map and save it in the |
| 3825 | * buffer registry map. |
| 3826 | * |
| 3827 | * Return 0 on success or else a negative value. |
| 3828 | */ |
| 3829 | static int duplicate_map_object(struct buffer_reg_map *buf_reg_map, |
| 3830 | struct ust_app_map *ua_map) |
| 3831 | { |
| 3832 | int ret; |
| 3833 | |
| 3834 | assert(buf_reg_map); |
| 3835 | assert(ua_map); |
| 3836 | |
| 3837 | /* Duplicating a map requires 1 new fd. Reserve it. */ |
| 3838 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 3839 | if (ret < 0) { |
| 3840 | ERR("Exhausted number of available FD upon duplicate map"); |
| 3841 | goto error_fd_get; |
| 3842 | } |
| 3843 | |
| 3844 | /* Duplicate object for stream once the original is in the registry. */ |
| 3845 | ret = ustctl_duplicate_ust_object_data(&ua_map->obj, buf_reg_map->obj.ust); |
| 3846 | if (ret < 0) { |
| 3847 | ERR("Duplicate map obj from %p to %p failed with ret: %d", |
| 3848 | buf_reg_map->obj.ust, ua_map->obj, ret); |
| 3849 | goto error; |
| 3850 | } |
| 3851 | ua_map->handle = ua_map->obj->handle; |
| 3852 | |
| 3853 | return 0; |
| 3854 | |
| 3855 | error: |
| 3856 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 3857 | error_fd_get: |
| 3858 | return ret; |
| 3859 | } |
| 3860 | |
| 3861 | /* |
| 3862 | * For a given channel buffer registry, setup all streams of the given ust |
| 3863 | * application channel. |
| 3864 | * |
| 3865 | * Return 0 on success or else a negative value. |
| 3866 | */ |
| 3867 | static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan, |
| 3868 | struct ust_app_channel *ua_chan, |
| 3869 | struct ust_app *app) |
| 3870 | { |
| 3871 | int ret = 0; |
| 3872 | struct ust_app_stream *stream, *stmp; |
| 3873 | |
| 3874 | assert(buf_reg_chan); |
| 3875 | assert(ua_chan); |
| 3876 | |
| 3877 | DBG2("UST app setup buffer registry stream"); |
| 3878 | |
| 3879 | /* Send all streams to application. */ |
| 3880 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 3881 | struct buffer_reg_stream *reg_stream; |
| 3882 | |
| 3883 | ret = buffer_reg_stream_create(®_stream); |
| 3884 | if (ret < 0) { |
| 3885 | goto error; |
| 3886 | } |
| 3887 | |
| 3888 | /* |
| 3889 | * Keep original pointer and nullify it in the stream so the delete |
| 3890 | * stream call does not release the object. |
| 3891 | */ |
| 3892 | reg_stream->obj.ust = stream->obj; |
| 3893 | stream->obj = NULL; |
| 3894 | buffer_reg_stream_add(reg_stream, buf_reg_chan); |
| 3895 | |
| 3896 | /* We don't need the streams anymore. */ |
| 3897 | cds_list_del(&stream->list); |
| 3898 | delete_ust_app_stream(-1, stream, app); |
| 3899 | } |
| 3900 | |
| 3901 | error: |
| 3902 | return ret; |
| 3903 | } |
| 3904 | |
| 3905 | /* |
| 3906 | * For a given map buffer registry, setup all counters of the given ust |
| 3907 | * application map. |
| 3908 | * |
| 3909 | * Return 0 on success or else a negative value. |
| 3910 | */ |
| 3911 | static int setup_buffer_reg_map_counters(struct buffer_reg_map *buf_reg_map, |
| 3912 | struct ust_app_map *ua_map, |
| 3913 | struct ust_app *app) |
| 3914 | { |
| 3915 | int ret = 0; |
| 3916 | struct ust_app_map_counter *counter, *stmp; |
| 3917 | |
| 3918 | assert(buf_reg_map); |
| 3919 | assert(ua_map); |
| 3920 | |
| 3921 | DBG2("UST app setup buffer registry counter"); |
| 3922 | |
| 3923 | /* Send all counters to application. */ |
| 3924 | cds_list_for_each_entry_safe(counter, stmp, &ua_map->counters.head, list) { |
| 3925 | struct buffer_reg_map_counter *reg_counter; |
| 3926 | |
| 3927 | ret = buffer_reg_map_counter_create(®_counter); |
| 3928 | if (ret < 0) { |
| 3929 | goto error; |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Keep original pointer and nullify it in the counter so the delete |
| 3934 | * counter call does not release the object. |
| 3935 | */ |
| 3936 | reg_counter->obj.ust = counter->obj; |
| 3937 | counter->obj = NULL; |
| 3938 | buffer_reg_map_counter_add(reg_counter, buf_reg_map); |
| 3939 | |
| 3940 | /* We don't need the counters anymore. */ |
| 3941 | cds_list_del(&counter->list); |
| 3942 | delete_ust_app_map_counter(-1, counter, app); |
| 3943 | } |
| 3944 | |
| 3945 | error: |
| 3946 | return ret; |
| 3947 | } |
| 3948 | |
| 3949 | /* |
| 3950 | * Create a buffer registry channel for the given session registry and |
| 3951 | * application channel object. If regp pointer is valid, it's set with the |
| 3952 | * created object. Important, the created object is NOT added to the session |
| 3953 | * registry hash table. |
| 3954 | * |
| 3955 | * Return 0 on success else a negative value. |
| 3956 | */ |
| 3957 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 3958 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) |
| 3959 | { |
| 3960 | int ret; |
| 3961 | struct buffer_reg_channel *buf_reg_chan = NULL; |
| 3962 | |
| 3963 | assert(reg_sess); |
| 3964 | assert(ua_chan); |
| 3965 | |
| 3966 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); |
| 3967 | |
| 3968 | /* Create buffer registry channel. */ |
| 3969 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan); |
| 3970 | if (ret < 0) { |
| 3971 | goto error_create; |
| 3972 | } |
| 3973 | assert(buf_reg_chan); |
| 3974 | buf_reg_chan->consumer_key = ua_chan->key; |
| 3975 | buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
| 3976 | buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
| 3977 | |
| 3978 | /* Create and add a channel registry to session. */ |
| 3979 | ret = ust_registry_channel_add(reg_sess->reg.ust, |
| 3980 | ua_chan->tracing_channel_id); |
| 3981 | if (ret < 0) { |
| 3982 | goto error; |
| 3983 | } |
| 3984 | buffer_reg_channel_add(reg_sess, buf_reg_chan); |
| 3985 | |
| 3986 | if (regp) { |
| 3987 | *regp = buf_reg_chan; |
| 3988 | } |
| 3989 | |
| 3990 | return 0; |
| 3991 | |
| 3992 | error: |
| 3993 | /* Safe because the registry channel object was not added to any HT. */ |
| 3994 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 3995 | error_create: |
| 3996 | return ret; |
| 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Create a buffer registry map for the given session registry and |
| 4001 | * application map object. If regp pointer is valid, it's set with the |
| 4002 | * created object. Important, the created object is NOT added to the session |
| 4003 | * registry hash table. |
| 4004 | * |
| 4005 | * Return 0 on success else a negative value. |
| 4006 | */ |
| 4007 | static int create_buffer_reg_map(struct buffer_reg_session *reg_sess, |
| 4008 | struct ust_app_map *ua_map, struct buffer_reg_map **regp) |
| 4009 | { |
| 4010 | int ret; |
| 4011 | struct buffer_reg_map *buf_reg_map = NULL; |
| 4012 | |
| 4013 | assert(reg_sess); |
| 4014 | assert(ua_map); |
| 4015 | |
| 4016 | DBG2("UST app creating buffer registry map for %s", ua_map->name); |
| 4017 | |
| 4018 | /* Create buffer registry map. */ |
| 4019 | ret = buffer_reg_map_create(ua_map->tracing_map_id, &buf_reg_map); |
| 4020 | if (ret < 0) { |
| 4021 | goto error_create; |
| 4022 | } |
| 4023 | assert(buf_reg_map); |
| 4024 | |
| 4025 | /* Create and add a map registry to session. */ |
| 4026 | ret = ust_registry_map_add(reg_sess->reg.ust, ua_map->tracing_map_id); |
| 4027 | if (ret < 0) { |
| 4028 | goto error; |
| 4029 | } |
| 4030 | buffer_reg_map_add(reg_sess, buf_reg_map); |
| 4031 | |
| 4032 | if (regp) { |
| 4033 | *regp = buf_reg_map; |
| 4034 | } |
| 4035 | |
| 4036 | return 0; |
| 4037 | |
| 4038 | error: |
| 4039 | /* Safe because the registry map object was not added to any HT. */ |
| 4040 | buffer_reg_map_destroy(buf_reg_map, LTTNG_DOMAIN_UST); |
| 4041 | error_create: |
| 4042 | return ret; |
| 4043 | } |
| 4044 | |
| 4045 | /* |
| 4046 | * Setup buffer registry channel for the given session registry and application |
| 4047 | * channel object. If regp pointer is valid, it's set with the created object. |
| 4048 | * |
| 4049 | * Return 0 on success else a negative value. |
| 4050 | */ |
| 4051 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
| 4052 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan, |
| 4053 | struct ust_app *app) |
| 4054 | { |
| 4055 | int ret; |
| 4056 | |
| 4057 | assert(reg_sess); |
| 4058 | assert(buf_reg_chan); |
| 4059 | assert(ua_chan); |
| 4060 | assert(ua_chan->obj); |
| 4061 | |
| 4062 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
| 4063 | |
| 4064 | /* Setup all streams for the registry. */ |
| 4065 | ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app); |
| 4066 | if (ret < 0) { |
| 4067 | goto error; |
| 4068 | } |
| 4069 | |
| 4070 | buf_reg_chan->obj.ust = ua_chan->obj; |
| 4071 | ua_chan->obj = NULL; |
| 4072 | |
| 4073 | return 0; |
| 4074 | |
| 4075 | error: |
| 4076 | buffer_reg_channel_remove(reg_sess, buf_reg_chan); |
| 4077 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 4078 | return ret; |
| 4079 | } |
| 4080 | |
| 4081 | /* |
| 4082 | * Setup buffer registry map for the given session registry and application |
| 4083 | * map object. If regp pointer is valid, it's set with the created object. |
| 4084 | * |
| 4085 | * Return 0 on success else a negative value. |
| 4086 | */ |
| 4087 | static int setup_buffer_reg_map(struct buffer_reg_session *reg_sess, |
| 4088 | struct ust_app_map *ua_map, struct buffer_reg_map *buf_reg_map, |
| 4089 | struct ust_app *app) |
| 4090 | { |
| 4091 | int ret; |
| 4092 | |
| 4093 | assert(reg_sess); |
| 4094 | assert(buf_reg_map); |
| 4095 | assert(ua_map); |
| 4096 | assert(ua_map->obj); |
| 4097 | |
| 4098 | DBG2("UST app setup buffer registry map for %s", ua_map->name); |
| 4099 | |
| 4100 | /* Setup all counters for the registry. */ |
| 4101 | ret = setup_buffer_reg_map_counters(buf_reg_map, ua_map, app); |
| 4102 | if (ret < 0) { |
| 4103 | goto error; |
| 4104 | } |
| 4105 | |
| 4106 | buf_reg_map->obj.ust = ua_map->obj; |
| 4107 | ua_map->obj = NULL; |
| 4108 | buf_reg_map->daemon_counter = ua_map->map_handle; |
| 4109 | ua_map->map_handle = NULL; |
| 4110 | |
| 4111 | return 0; |
| 4112 | |
| 4113 | error: |
| 4114 | buffer_reg_map_remove(reg_sess, buf_reg_map); |
| 4115 | buffer_reg_map_destroy(buf_reg_map, LTTNG_DOMAIN_UST); |
| 4116 | return ret; |
| 4117 | } |
| 4118 | |
| 4119 | /* |
| 4120 | * Send buffer registry channel to the application. |
| 4121 | * |
| 4122 | * Return 0 on success else a negative value. |
| 4123 | */ |
| 4124 | static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan, |
| 4125 | struct ust_app *app, struct ust_app_session *ua_sess, |
| 4126 | struct ust_app_channel *ua_chan) |
| 4127 | { |
| 4128 | int ret; |
| 4129 | struct buffer_reg_stream *reg_stream; |
| 4130 | |
| 4131 | assert(buf_reg_chan); |
| 4132 | assert(app); |
| 4133 | assert(ua_sess); |
| 4134 | assert(ua_chan); |
| 4135 | |
| 4136 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); |
| 4137 | |
| 4138 | ret = duplicate_channel_object(buf_reg_chan, ua_chan); |
| 4139 | if (ret < 0) { |
| 4140 | goto error; |
| 4141 | } |
| 4142 | |
| 4143 | /* Send channel to the application. */ |
| 4144 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 4145 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4146 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 4147 | goto error; |
| 4148 | } else if (ret < 0) { |
| 4149 | goto error; |
| 4150 | } |
| 4151 | |
| 4152 | health_code_update(); |
| 4153 | |
| 4154 | /* Send all streams to application. */ |
| 4155 | pthread_mutex_lock(&buf_reg_chan->stream_list_lock); |
| 4156 | cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) { |
| 4157 | struct ust_app_stream stream; |
| 4158 | |
| 4159 | ret = duplicate_stream_object(reg_stream, &stream); |
| 4160 | if (ret < 0) { |
| 4161 | goto error_stream_unlock; |
| 4162 | } |
| 4163 | |
| 4164 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); |
| 4165 | if (ret < 0) { |
| 4166 | (void) release_ust_app_stream(-1, &stream, app); |
| 4167 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4168 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 4169 | } |
| 4170 | goto error_stream_unlock; |
| 4171 | } |
| 4172 | |
| 4173 | /* |
| 4174 | * The return value is not important here. This function will output an |
| 4175 | * error if needed. |
| 4176 | */ |
| 4177 | (void) release_ust_app_stream(-1, &stream, app); |
| 4178 | } |
| 4179 | ua_chan->is_sent = 1; |
| 4180 | |
| 4181 | error_stream_unlock: |
| 4182 | pthread_mutex_unlock(&buf_reg_chan->stream_list_lock); |
| 4183 | error: |
| 4184 | return ret; |
| 4185 | } |
| 4186 | |
| 4187 | /* |
| 4188 | * Send buffer registry map to the application. |
| 4189 | * |
| 4190 | * Return 0 on success else a negative value. |
| 4191 | */ |
| 4192 | static int send_map_uid_to_ust(struct buffer_reg_map *buf_reg_map, |
| 4193 | struct ust_app *app, struct ust_app_session *ua_sess, |
| 4194 | struct ust_app_map *ua_map) |
| 4195 | { |
| 4196 | int ret; |
| 4197 | struct buffer_reg_map_counter *reg_map_counter; |
| 4198 | |
| 4199 | assert(buf_reg_map); |
| 4200 | assert(app); |
| 4201 | assert(ua_sess); |
| 4202 | assert(ua_map); |
| 4203 | |
| 4204 | DBG("UST app sending buffer registry map to ust sock %d", app->sock); |
| 4205 | |
| 4206 | ret = duplicate_map_object(buf_reg_map, ua_map); |
| 4207 | if (ret < 0) { |
| 4208 | goto error; |
| 4209 | } |
| 4210 | |
| 4211 | /* Send map to the application. */ |
| 4212 | |
| 4213 | pthread_mutex_lock(&app->sock_lock); |
| 4214 | ret = ustctl_send_counter_data_to_ust(app->sock, |
| 4215 | ua_sess->handle, ua_map->obj); |
| 4216 | pthread_mutex_unlock(&app->sock_lock); |
| 4217 | assert(ret == 0); |
| 4218 | if (ret < 0) { |
| 4219 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4220 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 4221 | } |
| 4222 | goto error_map_counter_unlock; |
| 4223 | } |
| 4224 | |
| 4225 | ua_map->handle = ua_map->obj->handle; |
| 4226 | |
| 4227 | health_code_update(); |
| 4228 | |
| 4229 | /* Send all map_counters to application. */ |
| 4230 | pthread_mutex_lock(&buf_reg_map->counter_list_lock); |
| 4231 | cds_list_for_each_entry(reg_map_counter, &buf_reg_map->counters, lnode) { |
| 4232 | struct ust_app_map_counter map_counter; |
| 4233 | |
| 4234 | ret = duplicate_map_counter_object(reg_map_counter, &map_counter); |
| 4235 | if (ret < 0) { |
| 4236 | goto error_map_counter_unlock; |
| 4237 | } |
| 4238 | |
| 4239 | pthread_mutex_lock(&app->sock_lock); |
| 4240 | // Do send the per cpu counter here |
| 4241 | ret = ustctl_send_counter_cpu_data_to_ust(app->sock, |
| 4242 | ua_map->obj, map_counter.obj); |
| 4243 | pthread_mutex_unlock(&app->sock_lock); |
| 4244 | assert(ret == 0); |
| 4245 | if (ret < 0) { |
| 4246 | (void) release_ust_app_map_counter(-1, &map_counter, app); |
| 4247 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
| 4248 | ret = -ENOTCONN; /* Caused by app exiting. */ |
| 4249 | } |
| 4250 | goto error_map_counter_unlock; |
| 4251 | } |
| 4252 | |
| 4253 | /* |
| 4254 | * The return value is not important here. This function will |
| 4255 | * output an error if needed. |
| 4256 | */ |
| 4257 | (void) release_ust_app_map_counter(-1, &map_counter, app); |
| 4258 | } |
| 4259 | ua_map->is_sent = 1; |
| 4260 | |
| 4261 | error_map_counter_unlock: |
| 4262 | pthread_mutex_unlock(&buf_reg_map->counter_list_lock); |
| 4263 | error: |
| 4264 | return ret; |
| 4265 | } |
| 4266 | |
| 4267 | /* |
| 4268 | * Create and send to the application the created buffers with per UID buffers. |
| 4269 | * |
| 4270 | * This MUST be called with a RCU read side lock acquired. |
| 4271 | * The session list lock and the session's lock must be acquired. |
| 4272 | * |
| 4273 | * Return 0 on success else a negative value. |
| 4274 | */ |
| 4275 | static int create_channel_per_uid(struct ust_app *app, |
| 4276 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4277 | struct ust_app_channel *ua_chan) |
| 4278 | { |
| 4279 | int ret; |
| 4280 | struct buffer_reg_uid *reg_uid; |
| 4281 | struct buffer_reg_channel *buf_reg_chan; |
| 4282 | struct ltt_session *session = NULL; |
| 4283 | enum lttng_error_code notification_ret; |
| 4284 | struct ust_registry_channel *ust_reg_chan; |
| 4285 | |
| 4286 | assert(app); |
| 4287 | assert(usess); |
| 4288 | assert(ua_sess); |
| 4289 | assert(ua_chan); |
| 4290 | |
| 4291 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); |
| 4292 | |
| 4293 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 4294 | /* |
| 4295 | * The session creation handles the creation of this global registry |
| 4296 | * object. If none can be find, there is a code flow problem or a |
| 4297 | * teardown race. |
| 4298 | */ |
| 4299 | assert(reg_uid); |
| 4300 | |
| 4301 | buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, |
| 4302 | reg_uid); |
| 4303 | if (buf_reg_chan) { |
| 4304 | goto send_channel; |
| 4305 | } |
| 4306 | |
| 4307 | /* Create the buffer registry channel object. */ |
| 4308 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan); |
| 4309 | if (ret < 0) { |
| 4310 | ERR("Error creating the UST channel \"%s\" registry instance", |
| 4311 | ua_chan->name); |
| 4312 | goto error; |
| 4313 | } |
| 4314 | |
| 4315 | session = session_find_by_id(ua_sess->tracing_id); |
| 4316 | assert(session); |
| 4317 | assert(pthread_mutex_trylock(&session->lock)); |
| 4318 | assert(session_trylock_list()); |
| 4319 | |
| 4320 | /* |
| 4321 | * Create the buffers on the consumer side. This call populates the |
| 4322 | * ust app channel object with all streams and data object. |
| 4323 | */ |
| 4324 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, |
| 4325 | app->bits_per_long, reg_uid->registry->reg.ust, |
| 4326 | session->most_recent_chunk_id.value); |
| 4327 | if (ret < 0) { |
| 4328 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
| 4329 | ua_chan->name); |
| 4330 | |
| 4331 | /* |
| 4332 | * Let's remove the previously created buffer registry channel so |
| 4333 | * it's not visible anymore in the session registry. |
| 4334 | */ |
| 4335 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, |
| 4336 | ua_chan->tracing_channel_id, false); |
| 4337 | buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan); |
| 4338 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
| 4339 | goto error; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
| 4343 | * Setup the streams and add it to the session registry. |
| 4344 | */ |
| 4345 | ret = setup_buffer_reg_channel(reg_uid->registry, |
| 4346 | ua_chan, buf_reg_chan, app); |
| 4347 | if (ret < 0) { |
| 4348 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); |
| 4349 | goto error; |
| 4350 | } |
| 4351 | |
| 4352 | /* Notify the notification subsystem of the channel's creation. */ |
| 4353 | pthread_mutex_lock(®_uid->registry->reg.ust->lock); |
| 4354 | ust_reg_chan = ust_registry_channel_find(reg_uid->registry->reg.ust, |
| 4355 | ua_chan->tracing_channel_id); |
| 4356 | assert(ust_reg_chan); |
| 4357 | ust_reg_chan->consumer_key = ua_chan->key; |
| 4358 | ust_reg_chan = NULL; |
| 4359 | pthread_mutex_unlock(®_uid->registry->reg.ust->lock); |
| 4360 | |
| 4361 | notification_ret = notification_thread_command_add_channel( |
| 4362 | notification_thread_handle, session->name, |
| 4363 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 4364 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 4365 | ua_chan->name, |
| 4366 | ua_chan->key, LTTNG_DOMAIN_UST, |
| 4367 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
| 4368 | if (notification_ret != LTTNG_OK) { |
| 4369 | ret = - (int) notification_ret; |
| 4370 | ERR("Failed to add channel to notification thread"); |
| 4371 | goto error; |
| 4372 | } |
| 4373 | |
| 4374 | send_channel: |
| 4375 | /* Send buffers to the application. */ |
| 4376 | ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan); |
| 4377 | if (ret < 0) { |
| 4378 | if (ret != -ENOTCONN) { |
| 4379 | ERR("Error sending channel to application"); |
| 4380 | } |
| 4381 | goto error; |
| 4382 | } |
| 4383 | |
| 4384 | error: |
| 4385 | if (session) { |
| 4386 | session_put(session); |
| 4387 | } |
| 4388 | return ret; |
| 4389 | } |
| 4390 | |
| 4391 | /* |
| 4392 | * Create and send to the application the created buffers with per UID buffers. |
| 4393 | * |
| 4394 | * This MUST be called with a RCU read side lock acquired. |
| 4395 | * The session list lock and the session's lock must be acquired. |
| 4396 | * |
| 4397 | * Return 0 on success else a negative value. |
| 4398 | */ |
| 4399 | static int create_map_per_uid(struct ust_app *app, |
| 4400 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4401 | struct ust_app_map *ua_map) |
| 4402 | { |
| 4403 | int ret; |
| 4404 | struct buffer_reg_uid *buffer_reg_uid; |
| 4405 | struct buffer_reg_map *buffer_reg_map; |
| 4406 | struct ltt_session *session = NULL; |
| 4407 | struct ust_registry_map *ust_reg_map; |
| 4408 | |
| 4409 | assert(app); |
| 4410 | assert(usess); |
| 4411 | assert(ua_sess); |
| 4412 | assert(ua_map); |
| 4413 | |
| 4414 | DBG("UST app creating map %s with per UID buffers", ua_map->name); |
| 4415 | |
| 4416 | buffer_reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 4417 | /* |
| 4418 | * The session creation handles the creation of this global registry |
| 4419 | * object. If none can be find, there is a code flow problem or a |
| 4420 | * teardown race. |
| 4421 | */ |
| 4422 | assert(buffer_reg_uid); |
| 4423 | |
| 4424 | buffer_reg_map = buffer_reg_map_find(ua_map->tracing_map_id, |
| 4425 | buffer_reg_uid); |
| 4426 | if (buffer_reg_map) { |
| 4427 | goto send_map; |
| 4428 | } |
| 4429 | |
| 4430 | /* Create the buffer registry map object. */ |
| 4431 | ret = create_buffer_reg_map(buffer_reg_uid->registry, ua_map, |
| 4432 | &buffer_reg_map); |
| 4433 | if (ret < 0) { |
| 4434 | ERR("Error creating the UST map \"%s\" registry instance", |
| 4435 | ua_map->name); |
| 4436 | goto error; |
| 4437 | } |
| 4438 | |
| 4439 | session = session_find_by_id(ua_sess->tracing_id); |
| 4440 | assert(session); |
| 4441 | assert(pthread_mutex_trylock(&session->lock)); |
| 4442 | assert(session_trylock_list()); |
| 4443 | |
| 4444 | /* |
| 4445 | */ |
| 4446 | ret = create_map_object(usess, ua_sess, ua_map); |
| 4447 | assert(ret == 0); |
| 4448 | if (ret < 0) { |
| 4449 | ERR("Error creating UST map object: map_name = \"%s\"", ua_map->name); |
| 4450 | goto error; |
| 4451 | } |
| 4452 | |
| 4453 | /* |
| 4454 | * Setup the streams and add it to the session registry. |
| 4455 | */ |
| 4456 | ret = setup_buffer_reg_map(buffer_reg_uid->registry, ua_map, |
| 4457 | buffer_reg_map, app); |
| 4458 | if (ret < 0) { |
| 4459 | ERR("Error setting up UST map \"%s\"", ua_map->name); |
| 4460 | goto error; |
| 4461 | } |
| 4462 | |
| 4463 | /* Notify the notification subsystem of the map's creation. */ |
| 4464 | pthread_mutex_lock(&buffer_reg_uid->registry->reg.ust->lock); |
| 4465 | ust_reg_map = ust_registry_map_find(buffer_reg_uid->registry->reg.ust, |
| 4466 | ua_map->tracing_map_id); |
| 4467 | assert(ust_reg_map); |
| 4468 | ust_reg_map = NULL; |
| 4469 | pthread_mutex_unlock(&buffer_reg_uid->registry->reg.ust->lock); |
| 4470 | |
| 4471 | send_map: |
| 4472 | /* Send buffers to the application. */ |
| 4473 | ret = send_map_uid_to_ust(buffer_reg_map, app, ua_sess, ua_map); |
| 4474 | if (ret < 0) { |
| 4475 | if (ret != -ENOTCONN) { |
| 4476 | ERR("Error sending map to application"); |
| 4477 | } |
| 4478 | goto error; |
| 4479 | } |
| 4480 | |
| 4481 | error: |
| 4482 | if (session) { |
| 4483 | session_put(session); |
| 4484 | } |
| 4485 | return ret; |
| 4486 | } |
| 4487 | |
| 4488 | //static int destroy_map_per_uid(struct ust_app *app, |
| 4489 | // struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4490 | // struct ust_app_map *ua_map) |
| 4491 | //{ |
| 4492 | // int ret; |
| 4493 | // struct buffer_reg_uid *buffer_reg_uid; |
| 4494 | // struct buffer_reg_map *buffer_reg_map; |
| 4495 | // |
| 4496 | // assert(app); |
| 4497 | // assert(usess); |
| 4498 | // assert(ua_sess); |
| 4499 | // assert(ua_map); |
| 4500 | // |
| 4501 | // DBG("UST app destroy map %s with per UID buffers", ua_map->name); |
| 4502 | // |
| 4503 | // buffer_reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); |
| 4504 | // /* |
| 4505 | // * The session creation handles the creation of this global registry |
| 4506 | // * object. If none can be find, there is a code flow problem or a |
| 4507 | // * teardown race. |
| 4508 | // */ |
| 4509 | // assert(buffer_reg_uid); |
| 4510 | // |
| 4511 | // buffer_reg_map = buffer_reg_map_find(ua_map->tracing_map_id, |
| 4512 | // buffer_reg_uid); |
| 4513 | // if (!buffer_reg_map) { |
| 4514 | // ERR("Can't find map in buffer registry: map-name = '%s', uid = %d", |
| 4515 | // ua_map->name, app->uid); |
| 4516 | // ret = -1; |
| 4517 | // goto end; |
| 4518 | // } |
| 4519 | // |
| 4520 | // buffer_reg_map_destroy(buffer_reg_map, LTTNG_DOMAIN_UST); |
| 4521 | // |
| 4522 | // ret = 0; |
| 4523 | //end: |
| 4524 | // return ret; |
| 4525 | //} |
| 4526 | |
| 4527 | /* |
| 4528 | * Create and send to the application the created buffers with per PID buffers. |
| 4529 | * |
| 4530 | * Called with UST app session lock held. |
| 4531 | * The session list lock and the session's lock must be acquired. |
| 4532 | * |
| 4533 | * Return 0 on success else a negative value. |
| 4534 | */ |
| 4535 | static int create_channel_per_pid(struct ust_app *app, |
| 4536 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4537 | struct ust_app_channel *ua_chan) |
| 4538 | { |
| 4539 | int ret; |
| 4540 | struct ust_registry_session *registry; |
| 4541 | enum lttng_error_code cmd_ret; |
| 4542 | struct ltt_session *session = NULL; |
| 4543 | uint64_t chan_reg_key; |
| 4544 | struct ust_registry_channel *ust_reg_chan; |
| 4545 | |
| 4546 | assert(app); |
| 4547 | assert(usess); |
| 4548 | assert(ua_sess); |
| 4549 | assert(ua_chan); |
| 4550 | |
| 4551 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); |
| 4552 | |
| 4553 | rcu_read_lock(); |
| 4554 | |
| 4555 | registry = get_session_registry(ua_sess); |
| 4556 | /* The UST app session lock is held, registry shall not be null. */ |
| 4557 | assert(registry); |
| 4558 | |
| 4559 | /* Create and add a new channel registry to session. */ |
| 4560 | ret = ust_registry_channel_add(registry, ua_chan->key); |
| 4561 | if (ret < 0) { |
| 4562 | ERR("Error creating the UST channel \"%s\" registry instance", |
| 4563 | ua_chan->name); |
| 4564 | goto error; |
| 4565 | } |
| 4566 | |
| 4567 | session = session_find_by_id(ua_sess->tracing_id); |
| 4568 | assert(session); |
| 4569 | |
| 4570 | assert(pthread_mutex_trylock(&session->lock)); |
| 4571 | assert(session_trylock_list()); |
| 4572 | |
| 4573 | /* Create and get channel on the consumer side. */ |
| 4574 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, |
| 4575 | app->bits_per_long, registry, |
| 4576 | session->most_recent_chunk_id.value); |
| 4577 | if (ret < 0) { |
| 4578 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
| 4579 | ua_chan->name); |
| 4580 | goto error_remove_from_registry; |
| 4581 | } |
| 4582 | |
| 4583 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); |
| 4584 | if (ret < 0) { |
| 4585 | if (ret != -ENOTCONN) { |
| 4586 | ERR("Error sending channel to application"); |
| 4587 | } |
| 4588 | goto error_remove_from_registry; |
| 4589 | } |
| 4590 | |
| 4591 | chan_reg_key = ua_chan->key; |
| 4592 | pthread_mutex_lock(®istry->lock); |
| 4593 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
| 4594 | assert(ust_reg_chan); |
| 4595 | ust_reg_chan->consumer_key = ua_chan->key; |
| 4596 | pthread_mutex_unlock(®istry->lock); |
| 4597 | |
| 4598 | cmd_ret = notification_thread_command_add_channel( |
| 4599 | notification_thread_handle, session->name, |
| 4600 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 4601 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 4602 | ua_chan->name, |
| 4603 | ua_chan->key, LTTNG_DOMAIN_UST, |
| 4604 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
| 4605 | if (cmd_ret != LTTNG_OK) { |
| 4606 | ret = - (int) cmd_ret; |
| 4607 | ERR("Failed to add channel to notification thread"); |
| 4608 | goto error_remove_from_registry; |
| 4609 | } |
| 4610 | |
| 4611 | error_remove_from_registry: |
| 4612 | if (ret) { |
| 4613 | ust_registry_channel_del_free(registry, ua_chan->key, false); |
| 4614 | } |
| 4615 | error: |
| 4616 | rcu_read_unlock(); |
| 4617 | if (session) { |
| 4618 | session_put(session); |
| 4619 | } |
| 4620 | return ret; |
| 4621 | } |
| 4622 | |
| 4623 | /* |
| 4624 | * Create and send to the application the created buffers with per PID buffers. |
| 4625 | * |
| 4626 | * Called with UST app session lock held. |
| 4627 | * The session list lock and the session's lock must be acquired. |
| 4628 | * |
| 4629 | * Return 0 on success else a negative value. |
| 4630 | */ |
| 4631 | static int create_map_per_pid(struct ust_app *app, |
| 4632 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4633 | struct ust_app_map *ua_map) |
| 4634 | { |
| 4635 | int ret; |
| 4636 | struct ust_registry_session *registry; |
| 4637 | struct ltt_session *session = NULL; |
| 4638 | uint64_t map_reg_key; |
| 4639 | struct ust_registry_map *ust_reg_map; |
| 4640 | |
| 4641 | assert(app); |
| 4642 | assert(usess); |
| 4643 | assert(ua_sess); |
| 4644 | assert(ua_map); |
| 4645 | |
| 4646 | DBG("UST app creating map %s with per PID buffers", ua_map->name); |
| 4647 | |
| 4648 | rcu_read_lock(); |
| 4649 | |
| 4650 | registry = get_session_registry(ua_sess); |
| 4651 | /* The UST app session lock is held, registry shall not be null. */ |
| 4652 | assert(registry); |
| 4653 | |
| 4654 | /* Create and add a new map registry to session. */ |
| 4655 | ret = ust_registry_map_add(registry, ua_map->key); |
| 4656 | if (ret < 0) { |
| 4657 | ERR("Error creating the UST map \"%s\" registry instance", |
| 4658 | ua_map->name); |
| 4659 | goto error; |
| 4660 | } |
| 4661 | |
| 4662 | session = session_find_by_id(ua_sess->tracing_id); |
| 4663 | assert(session); |
| 4664 | |
| 4665 | assert(pthread_mutex_trylock(&session->lock)); |
| 4666 | assert(session_trylock_list()); |
| 4667 | |
| 4668 | /* Create and get map. */ |
| 4669 | ret = create_map_object(usess, ua_sess, ua_map); |
| 4670 | if (ret < 0) { |
| 4671 | ERR("Error creating UST map object: map_name = \"%s\" ", |
| 4672 | ua_map->name); |
| 4673 | goto error_remove_from_registry; |
| 4674 | } |
| 4675 | |
| 4676 | ret = send_map_pid_to_ust(app, ua_sess, ua_map); |
| 4677 | if (ret < 0) { |
| 4678 | if (ret != -ENOTCONN) { |
| 4679 | ERR("Error sending map to application"); |
| 4680 | } |
| 4681 | goto error_remove_from_registry; |
| 4682 | } |
| 4683 | |
| 4684 | map_reg_key = ua_map->key; |
| 4685 | pthread_mutex_lock(®istry->lock); |
| 4686 | ust_reg_map = ust_registry_map_find(registry, map_reg_key); |
| 4687 | assert(ust_reg_map); |
| 4688 | pthread_mutex_unlock(®istry->lock); |
| 4689 | |
| 4690 | error_remove_from_registry: |
| 4691 | if (ret) { |
| 4692 | ust_registry_map_del_free(registry, ua_map->key); |
| 4693 | } |
| 4694 | error: |
| 4695 | rcu_read_unlock(); |
| 4696 | if (session) { |
| 4697 | session_put(session); |
| 4698 | } |
| 4699 | return ret; |
| 4700 | } |
| 4701 | |
| 4702 | /* |
| 4703 | * From an already allocated ust app channel, create the channel buffers if |
| 4704 | * needed and send them to the application. This MUST be called with a RCU read |
| 4705 | * side lock acquired. |
| 4706 | * |
| 4707 | * Called with UST app session lock held. |
| 4708 | * |
| 4709 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
| 4710 | * the application exited concurrently. |
| 4711 | */ |
| 4712 | static int ust_app_channel_send(struct ust_app *app, |
| 4713 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4714 | struct ust_app_channel *ua_chan) |
| 4715 | { |
| 4716 | int ret; |
| 4717 | |
| 4718 | assert(app); |
| 4719 | assert(usess); |
| 4720 | assert(usess->active); |
| 4721 | assert(ua_sess); |
| 4722 | assert(ua_chan); |
| 4723 | |
| 4724 | /* Handle buffer type before sending the channel to the application. */ |
| 4725 | switch (usess->buffer_type) { |
| 4726 | case LTTNG_BUFFER_PER_UID: |
| 4727 | { |
| 4728 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); |
| 4729 | if (ret < 0) { |
| 4730 | goto error; |
| 4731 | } |
| 4732 | break; |
| 4733 | } |
| 4734 | case LTTNG_BUFFER_PER_PID: |
| 4735 | { |
| 4736 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); |
| 4737 | if (ret < 0) { |
| 4738 | goto error; |
| 4739 | } |
| 4740 | break; |
| 4741 | } |
| 4742 | default: |
| 4743 | assert(0); |
| 4744 | ret = -EINVAL; |
| 4745 | goto error; |
| 4746 | } |
| 4747 | |
| 4748 | /* Initialize ust objd object using the received handle and add it. */ |
| 4749 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); |
| 4750 | lttng_ht_add_unique_ulong(app->ust_chan_objd, &ua_chan->ust_objd_node); |
| 4751 | |
| 4752 | /* If channel is not enabled, disable it on the tracer */ |
| 4753 | if (!ua_chan->enabled) { |
| 4754 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 4755 | if (ret < 0) { |
| 4756 | goto error; |
| 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | error: |
| 4761 | return ret; |
| 4762 | } |
| 4763 | |
| 4764 | /* |
| 4765 | * From an already allocated ust app map, create the map buffers if |
| 4766 | * needed and send them to the application. This MUST be called with a RCU read |
| 4767 | * side lock acquired. |
| 4768 | * |
| 4769 | * Called with UST app session lock held. |
| 4770 | * |
| 4771 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
| 4772 | * the application exited concurrently. |
| 4773 | */ |
| 4774 | static int ust_app_map_send(struct ust_app *app, |
| 4775 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
| 4776 | struct ust_app_map *ua_map) |
| 4777 | { |
| 4778 | int ret; |
| 4779 | |
| 4780 | assert(app); |
| 4781 | assert(usess); |
| 4782 | assert(usess->active); |
| 4783 | assert(ua_sess); |
| 4784 | assert(ua_map); |
| 4785 | |
| 4786 | /* Handle buffer type before sending the map to the application. */ |
| 4787 | switch (usess->buffer_type) { |
| 4788 | case LTTNG_BUFFER_PER_UID: |
| 4789 | { |
| 4790 | ret = create_map_per_uid(app, usess, ua_sess, ua_map); |
| 4791 | if (ret < 0) { |
| 4792 | goto error; |
| 4793 | } |
| 4794 | break; |
| 4795 | } |
| 4796 | case LTTNG_BUFFER_PER_PID: |
| 4797 | { |
| 4798 | ret = create_map_per_pid(app, usess, ua_sess, ua_map); |
| 4799 | if (ret < 0) { |
| 4800 | goto error; |
| 4801 | } |
| 4802 | break; |
| 4803 | } |
| 4804 | default: |
| 4805 | assert(0); |
| 4806 | ret = -EINVAL; |
| 4807 | goto error; |
| 4808 | } |
| 4809 | |
| 4810 | /* Initialize ust objd object using the received handle and add it. */ |
| 4811 | lttng_ht_node_init_ulong(&ua_map->ust_objd_node, ua_map->handle); |
| 4812 | lttng_ht_add_unique_ulong(app->ust_map_objd, &ua_map->ust_objd_node); |
| 4813 | |
| 4814 | /* If map is not enabled, disable it on the tracer */ |
| 4815 | if (!ua_map->enabled) { |
| 4816 | ret = disable_ust_map(app, ua_sess, ua_map); |
| 4817 | if (ret < 0) { |
| 4818 | goto error; |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | error: |
| 4823 | return ret; |
| 4824 | } |
| 4825 | |
| 4826 | /* |
| 4827 | * Create UST app channel and return it through ua_chanp if not NULL. |
| 4828 | * |
| 4829 | * Called with UST app session lock and RCU read-side lock held. |
| 4830 | * |
| 4831 | * Return 0 on success or else a negative value. |
| 4832 | */ |
| 4833 | static int ust_app_channel_allocate(struct ust_app_session *ua_sess, |
| 4834 | struct ltt_ust_channel *uchan, |
| 4835 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
| 4836 | struct ust_app_channel **ua_chanp) |
| 4837 | { |
| 4838 | int ret = 0; |
| 4839 | struct lttng_ht_iter iter; |
| 4840 | struct lttng_ht_node_str *ua_chan_node; |
| 4841 | struct ust_app_channel *ua_chan; |
| 4842 | |
| 4843 | /* Lookup channel in the ust app session */ |
| 4844 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 4845 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 4846 | if (ua_chan_node != NULL) { |
| 4847 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 4848 | goto end; |
| 4849 | } |
| 4850 | |
| 4851 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 4852 | if (ua_chan == NULL) { |
| 4853 | /* Only malloc can fail here */ |
| 4854 | ret = -ENOMEM; |
| 4855 | goto error; |
| 4856 | } |
| 4857 | shadow_copy_channel(ua_chan, uchan); |
| 4858 | |
| 4859 | /* Set channel type. */ |
| 4860 | ua_chan->attr.type = type; |
| 4861 | |
| 4862 | /* Only add the channel if successful on the tracer side. */ |
| 4863 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 4864 | end: |
| 4865 | if (ua_chanp) { |
| 4866 | *ua_chanp = ua_chan; |
| 4867 | } |
| 4868 | |
| 4869 | /* Everything went well. */ |
| 4870 | return 0; |
| 4871 | |
| 4872 | error: |
| 4873 | return ret; |
| 4874 | } |
| 4875 | |
| 4876 | /* |
| 4877 | * Create UST app map and return it through ua_mapp if not NULL. |
| 4878 | * |
| 4879 | * Called with UST app session lock and RCU read-side lock held. |
| 4880 | * |
| 4881 | * Return 0 on success or else a negative value. |
| 4882 | */ |
| 4883 | static int ust_app_map_allocate(struct ust_app_session *ua_sess, |
| 4884 | struct ltt_ust_map *umap, |
| 4885 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
| 4886 | struct ust_app_map **ua_mapp) |
| 4887 | { |
| 4888 | int ret = 0; |
| 4889 | struct lttng_ht_iter iter; |
| 4890 | struct lttng_ht_node_str *ua_map_node; |
| 4891 | struct ust_app_map *ua_map; |
| 4892 | |
| 4893 | DBG("Allocating map id = %"PRIu64, umap->id); |
| 4894 | |
| 4895 | /* Lookup map in the ust app session */ |
| 4896 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &iter); |
| 4897 | ua_map_node = lttng_ht_iter_get_node_str(&iter); |
| 4898 | if (ua_map_node != NULL) { |
| 4899 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 4900 | goto end; |
| 4901 | } |
| 4902 | |
| 4903 | ua_map = alloc_ust_app_map(umap->name, ua_sess); |
| 4904 | if (ua_map == NULL) { |
| 4905 | /* Only malloc can fail here */ |
| 4906 | ret = -ENOMEM; |
| 4907 | goto error; |
| 4908 | } |
| 4909 | //shadow_copy_map(ua_map, umap); |
| 4910 | ua_map->tracing_map_id = umap->id; |
| 4911 | ua_map->coalesce_hits = umap->coalesce_hits; |
| 4912 | ua_map->dead_app_kv_values = &umap->dead_app_kv_values; |
| 4913 | ua_map->bitness = umap->bitness; |
| 4914 | |
| 4915 | /* Set map type. */ |
| 4916 | //ua_map->attr.type = type; |
| 4917 | ua_map->bucket_count = umap->bucket_count; |
| 4918 | |
| 4919 | /* Only add the map if successful on the tracer side. */ |
| 4920 | lttng_ht_add_unique_str(ua_sess->maps, &ua_map->node); |
| 4921 | end: |
| 4922 | if (ua_mapp) { |
| 4923 | *ua_mapp = ua_map; |
| 4924 | } |
| 4925 | |
| 4926 | /* Everything went well. */ |
| 4927 | return 0; |
| 4928 | |
| 4929 | error: |
| 4930 | return ret; |
| 4931 | } |
| 4932 | |
| 4933 | /* |
| 4934 | * Create UST app event and create it on the tracer side. |
| 4935 | * |
| 4936 | * Must be called with the RCU read side lock held. |
| 4937 | * Called with ust app session mutex held. |
| 4938 | */ |
| 4939 | static |
| 4940 | int create_ust_app_channel_event(struct ust_app_session *ua_sess, |
| 4941 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, |
| 4942 | struct ust_app *app) |
| 4943 | { |
| 4944 | int ret = 0; |
| 4945 | struct ust_app_event *ua_event; |
| 4946 | |
| 4947 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 4948 | if (ua_event == NULL) { |
| 4949 | /* Only failure mode of alloc_ust_app_event(). */ |
| 4950 | ret = -ENOMEM; |
| 4951 | goto end; |
| 4952 | } |
| 4953 | shadow_copy_event(ua_event, uevent); |
| 4954 | |
| 4955 | /* Create it on the tracer side */ |
| 4956 | ret = create_ust_channel_event(app, ua_sess, ua_chan, ua_event); |
| 4957 | if (ret < 0) { |
| 4958 | /* |
| 4959 | * Not found previously means that it does not exist on the |
| 4960 | * tracer. If the application reports that the event existed, |
| 4961 | * it means there is a bug in the sessiond or lttng-ust |
| 4962 | * (or corruption, etc.) |
| 4963 | */ |
| 4964 | if (ret == -LTTNG_UST_ERR_EXIST) { |
| 4965 | ERR("Tracer for application reported that an event being created already existed: " |
| 4966 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", |
| 4967 | uevent->attr.name, |
| 4968 | app->pid, app->ppid, app->uid, |
| 4969 | app->gid); |
| 4970 | } |
| 4971 | goto error; |
| 4972 | } |
| 4973 | |
| 4974 | add_unique_ust_app_event(ua_chan->events, ua_event); |
| 4975 | |
| 4976 | DBG2("UST app create event completed: app = '%s' (ppid: %d)", |
| 4977 | app->name, app->ppid); |
| 4978 | |
| 4979 | end: |
| 4980 | return ret; |
| 4981 | |
| 4982 | error: |
| 4983 | /* Valid. Calling here is already in a read side lock */ |
| 4984 | delete_ust_app_event(-1, ua_event, app); |
| 4985 | return ret; |
| 4986 | } |
| 4987 | |
| 4988 | /* |
| 4989 | * Create UST app event and create it on the tracer side. |
| 4990 | * |
| 4991 | * Must be called with the RCU read side lock held. |
| 4992 | * Called with ust app session mutex held. |
| 4993 | */ |
| 4994 | static |
| 4995 | int create_ust_app_map_event(struct ust_app_session *ua_sess, |
| 4996 | struct ust_app_map *ua_map, struct ltt_ust_event *uevent, |
| 4997 | struct ust_app *app) |
| 4998 | { |
| 4999 | int ret = 0; |
| 5000 | uint64_t map_reg_key; |
| 5001 | struct ust_app_event *ua_event; |
| 5002 | struct ust_registry_session *registry; |
| 5003 | |
| 5004 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 5005 | if (ua_event == NULL) { |
| 5006 | /* Only failure mode of alloc_ust_app_event(). */ |
| 5007 | ret = -ENOMEM; |
| 5008 | goto end; |
| 5009 | } |
| 5010 | shadow_copy_event(ua_event, uevent); |
| 5011 | |
| 5012 | registry = get_session_registry(ua_sess); |
| 5013 | if (!registry) { |
| 5014 | DBG("Application session is being torn down. Abort event notify"); |
| 5015 | ret = 0; |
| 5016 | goto error; |
| 5017 | } |
| 5018 | |
| 5019 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 5020 | map_reg_key = ua_map->tracing_map_id; |
| 5021 | } else { |
| 5022 | map_reg_key = ua_map->key; |
| 5023 | } |
| 5024 | |
| 5025 | pthread_mutex_lock(®istry->lock); |
| 5026 | ret = ust_registry_map_add_token_key_mapping(registry, map_reg_key, |
| 5027 | uevent->attr.token, uevent->key); |
| 5028 | assert(ret == 0); |
| 5029 | pthread_mutex_unlock(®istry->lock); |
| 5030 | |
| 5031 | /* Create it on the tracer side */ |
| 5032 | ret = create_ust_map_event(app, ua_sess, ua_map, uevent->key, ua_event); |
| 5033 | if (ret < 0) { |
| 5034 | /* |
| 5035 | * Not found previously means that it does not exist on the |
| 5036 | * tracer. If the application reports that the event existed, |
| 5037 | * it means there is a bug in the sessiond or lttng-ust |
| 5038 | * (or corruption, etc.) |
| 5039 | */ |
| 5040 | if (ret == -LTTNG_UST_ERR_EXIST) { |
| 5041 | ERR("Tracer for application reported that an event being created already existed: " |
| 5042 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", |
| 5043 | uevent->attr.name, |
| 5044 | app->pid, app->ppid, app->uid, |
| 5045 | app->gid); |
| 5046 | } |
| 5047 | |
| 5048 | /* |
| 5049 | * FIXME: frdeso: remove key from tokey->key mapping. |
| 5050 | */ |
| 5051 | goto error; |
| 5052 | } |
| 5053 | |
| 5054 | add_unique_ust_app_event(ua_map->events, ua_event); |
| 5055 | |
| 5056 | DBG2("UST app create event completed: app = '%s', tracer token = %"PRIu64" (ppid: %d)", |
| 5057 | app->name, uevent->attr.token, app->ppid); |
| 5058 | |
| 5059 | end: |
| 5060 | return ret; |
| 5061 | |
| 5062 | error: |
| 5063 | /* Valid. Calling here is already in a read side lock */ |
| 5064 | delete_ust_app_event(-1, ua_event, app); |
| 5065 | return ret; |
| 5066 | } |
| 5067 | |
| 5068 | /* |
| 5069 | * Create UST app event notifier rule and create it on the tracer side. |
| 5070 | * |
| 5071 | * Must be called with the RCU read side lock held. |
| 5072 | * Called with ust app session mutex held. |
| 5073 | */ |
| 5074 | static |
| 5075 | int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, |
| 5076 | struct ust_app *app) |
| 5077 | { |
| 5078 | int ret = 0; |
| 5079 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; |
| 5080 | |
| 5081 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
| 5082 | if (ua_event_notifier_rule == NULL) { |
| 5083 | ret = -ENOMEM; |
| 5084 | goto end; |
| 5085 | } |
| 5086 | |
| 5087 | /* Create it on the tracer side. */ |
| 5088 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); |
| 5089 | if (ret < 0) { |
| 5090 | /* |
| 5091 | * Not found previously means that it does not exist on the |
| 5092 | * tracer. If the application reports that the event existed, |
| 5093 | * it means there is a bug in the sessiond or lttng-ust |
| 5094 | * (or corruption, etc.) |
| 5095 | */ |
| 5096 | if (ret == -LTTNG_UST_ERR_EXIST) { |
| 5097 | ERR("Tracer for application reported that an event notifier being created already exists: " |
| 5098 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", |
| 5099 | lttng_trigger_get_tracer_token(trigger), |
| 5100 | app->pid, app->ppid, app->uid, |
| 5101 | app->gid); |
| 5102 | } |
| 5103 | goto error; |
| 5104 | } |
| 5105 | |
| 5106 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, |
| 5107 | &ua_event_notifier_rule->node); |
| 5108 | |
| 5109 | DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64, |
| 5110 | app->name, app->ppid, lttng_trigger_get_tracer_token(trigger)); |
| 5111 | |
| 5112 | goto end; |
| 5113 | |
| 5114 | error: |
| 5115 | /* The RCU read side lock is already being held by the caller. */ |
| 5116 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); |
| 5117 | end: |
| 5118 | return ret; |
| 5119 | } |
| 5120 | |
| 5121 | /* |
| 5122 | * Create UST metadata and open it on the tracer side. |
| 5123 | * |
| 5124 | * Called with UST app session lock held and RCU read side lock. |
| 5125 | */ |
| 5126 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, |
| 5127 | struct ust_app *app, struct consumer_output *consumer) |
| 5128 | { |
| 5129 | int ret = 0; |
| 5130 | struct ust_app_channel *metadata; |
| 5131 | struct consumer_socket *socket; |
| 5132 | struct ust_registry_session *registry; |
| 5133 | struct ltt_session *session = NULL; |
| 5134 | |
| 5135 | assert(ua_sess); |
| 5136 | assert(app); |
| 5137 | assert(consumer); |
| 5138 | |
| 5139 | registry = get_session_registry(ua_sess); |
| 5140 | /* The UST app session is held registry shall not be null. */ |
| 5141 | assert(registry); |
| 5142 | |
| 5143 | pthread_mutex_lock(®istry->lock); |
| 5144 | |
| 5145 | /* Metadata already exists for this registry or it was closed previously */ |
| 5146 | if (registry->metadata_key || registry->metadata_closed) { |
| 5147 | ret = 0; |
| 5148 | goto error; |
| 5149 | } |
| 5150 | |
| 5151 | /* Allocate UST metadata */ |
| 5152 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
| 5153 | if (!metadata) { |
| 5154 | /* malloc() failed */ |
| 5155 | ret = -ENOMEM; |
| 5156 | goto error; |
| 5157 | } |
| 5158 | |
| 5159 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
| 5160 | |
| 5161 | /* Need one fd for the channel. */ |
| 5162 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 5163 | if (ret < 0) { |
| 5164 | ERR("Exhausted number of available FD upon create metadata"); |
| 5165 | goto error; |
| 5166 | } |
| 5167 | |
| 5168 | /* Get the right consumer socket for the application. */ |
| 5169 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); |
| 5170 | if (!socket) { |
| 5171 | ret = -EINVAL; |
| 5172 | goto error_consumer; |
| 5173 | } |
| 5174 | |
| 5175 | /* |
| 5176 | * Keep metadata key so we can identify it on the consumer side. Assign it |
| 5177 | * to the registry *before* we ask the consumer so we avoid the race of the |
| 5178 | * consumer requesting the metadata and the ask_channel call on our side |
| 5179 | * did not returned yet. |
| 5180 | */ |
| 5181 | registry->metadata_key = metadata->key; |
| 5182 | |
| 5183 | session = session_find_by_id(ua_sess->tracing_id); |
| 5184 | assert(session); |
| 5185 | |
| 5186 | assert(pthread_mutex_trylock(&session->lock)); |
| 5187 | assert(session_trylock_list()); |
| 5188 | |
| 5189 | /* |
| 5190 | * Ask the metadata channel creation to the consumer. The metadata object |
| 5191 | * will be created by the consumer and kept their. However, the stream is |
| 5192 | * never added or monitored until we do a first push metadata to the |
| 5193 | * consumer. |
| 5194 | */ |
| 5195 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
| 5196 | registry, session->current_trace_chunk); |
| 5197 | if (ret < 0) { |
| 5198 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 5199 | registry->metadata_key = 0; |
| 5200 | goto error_consumer; |
| 5201 | } |
| 5202 | |
| 5203 | /* |
| 5204 | * The setup command will make the metadata stream be sent to the relayd, |
| 5205 | * if applicable, and the thread managing the metadatas. This is important |
| 5206 | * because after this point, if an error occurs, the only way the stream |
| 5207 | * can be deleted is to be monitored in the consumer. |
| 5208 | */ |
| 5209 | ret = consumer_setup_metadata(socket, metadata->key); |
| 5210 | if (ret < 0) { |
| 5211 | /* Nullify the metadata key so we don't try to close it later on. */ |
| 5212 | registry->metadata_key = 0; |
| 5213 | goto error_consumer; |
| 5214 | } |
| 5215 | |
| 5216 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
| 5217 | metadata->key, app->pid); |
| 5218 | |
| 5219 | error_consumer: |
| 5220 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 5221 | delete_ust_app_channel(-1, metadata, app); |
| 5222 | error: |
| 5223 | pthread_mutex_unlock(®istry->lock); |
| 5224 | if (session) { |
| 5225 | session_put(session); |
| 5226 | } |
| 5227 | return ret; |
| 5228 | } |
| 5229 | |
| 5230 | /* |
| 5231 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
| 5232 | * acquired before calling this function. |
| 5233 | */ |
| 5234 | struct ust_app *ust_app_find_by_pid(pid_t pid) |
| 5235 | { |
| 5236 | struct ust_app *app = NULL; |
| 5237 | struct lttng_ht_node_ulong *node; |
| 5238 | struct lttng_ht_iter iter; |
| 5239 | |
| 5240 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
| 5241 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 5242 | if (node == NULL) { |
| 5243 | DBG2("UST app no found with pid %d", pid); |
| 5244 | goto error; |
| 5245 | } |
| 5246 | |
| 5247 | DBG2("Found UST app by pid %d", pid); |
| 5248 | |
| 5249 | app = caa_container_of(node, struct ust_app, pid_n); |
| 5250 | |
| 5251 | error: |
| 5252 | return app; |
| 5253 | } |
| 5254 | |
| 5255 | /* |
| 5256 | * Allocate and init an UST app object using the registration information and |
| 5257 | * the command socket. This is called when the command socket connects to the |
| 5258 | * session daemon. |
| 5259 | * |
| 5260 | * The object is returned on success or else NULL. |
| 5261 | */ |
| 5262 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
| 5263 | { |
| 5264 | int ret; |
| 5265 | struct ust_app *lta = NULL; |
| 5266 | struct lttng_pipe *event_notifier_event_source_pipe = NULL; |
| 5267 | |
| 5268 | assert(msg); |
| 5269 | assert(sock >= 0); |
| 5270 | |
| 5271 | DBG3("UST app creating application for socket %d", sock); |
| 5272 | |
| 5273 | if ((msg->bits_per_long == 64 && |
| 5274 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) |
| 5275 | || (msg->bits_per_long == 32 && |
| 5276 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { |
| 5277 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
| 5278 | "%d-bit long, but no consumerd for this size is available.\n", |
| 5279 | msg->name, msg->pid, msg->bits_per_long); |
| 5280 | goto error; |
| 5281 | } |
| 5282 | |
| 5283 | /* |
| 5284 | * Reserve the two file descriptors of the event source pipe. The write |
| 5285 | * end will be closed once it is passed to the application, at which |
| 5286 | * point a single 'put' will be performed. |
| 5287 | */ |
| 5288 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
| 5289 | if (ret) { |
| 5290 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)", |
| 5291 | msg->name, (int) msg->ppid); |
| 5292 | goto error; |
| 5293 | } |
| 5294 | |
| 5295 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 5296 | if (!event_notifier_event_source_pipe) { |
| 5297 | PERROR("Failed to open application event source pipe: '%s' (ppid = %d)", |
| 5298 | msg->name, msg->ppid); |
| 5299 | goto error; |
| 5300 | } |
| 5301 | |
| 5302 | lta = zmalloc(sizeof(struct ust_app)); |
| 5303 | if (lta == NULL) { |
| 5304 | PERROR("malloc"); |
| 5305 | goto error_free_pipe; |
| 5306 | } |
| 5307 | |
| 5308 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
| 5309 | |
| 5310 | lta->ppid = msg->ppid; |
| 5311 | lta->uid = msg->uid; |
| 5312 | lta->gid = msg->gid; |
| 5313 | |
| 5314 | lta->bits_per_long = msg->bits_per_long; |
| 5315 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
| 5316 | lta->uint16_t_alignment = msg->uint16_t_alignment; |
| 5317 | lta->uint32_t_alignment = msg->uint32_t_alignment; |
| 5318 | lta->uint64_t_alignment = msg->uint64_t_alignment; |
| 5319 | lta->long_alignment = msg->long_alignment; |
| 5320 | lta->byte_order = msg->byte_order; |
| 5321 | |
| 5322 | lta->v_major = msg->major; |
| 5323 | lta->v_minor = msg->minor; |
| 5324 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 5325 | lta->ust_chan_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5326 | lta->ust_map_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5327 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5328 | lta->notify_sock = -1; |
| 5329 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 5330 | |
| 5331 | /* Copy name and make sure it's NULL terminated. */ |
| 5332 | strncpy(lta->name, msg->name, sizeof(lta->name)); |
| 5333 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; |
| 5334 | |
| 5335 | /* |
| 5336 | * Before this can be called, when receiving the registration information, |
| 5337 | * the application compatibility is checked. So, at this point, the |
| 5338 | * application can work with this session daemon. |
| 5339 | */ |
| 5340 | lta->compatible = 1; |
| 5341 | |
| 5342 | lta->pid = msg->pid; |
| 5343 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
| 5344 | lta->sock = sock; |
| 5345 | pthread_mutex_init(<a->sock_lock, NULL); |
| 5346 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
| 5347 | |
| 5348 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
| 5349 | return lta; |
| 5350 | |
| 5351 | error_free_pipe: |
| 5352 | lttng_pipe_destroy(event_notifier_event_source_pipe); |
| 5353 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 5354 | error: |
| 5355 | return NULL; |
| 5356 | } |
| 5357 | |
| 5358 | /* |
| 5359 | * For a given application object, add it to every hash table. |
| 5360 | */ |
| 5361 | void ust_app_add(struct ust_app *app) |
| 5362 | { |
| 5363 | assert(app); |
| 5364 | assert(app->notify_sock >= 0); |
| 5365 | |
| 5366 | app->registration_time = time(NULL); |
| 5367 | |
| 5368 | rcu_read_lock(); |
| 5369 | |
| 5370 | /* |
| 5371 | * On a re-registration, we want to kick out the previous registration of |
| 5372 | * that pid |
| 5373 | */ |
| 5374 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
| 5375 | |
| 5376 | /* |
| 5377 | * The socket _should_ be unique until _we_ call close. So, a add_unique |
| 5378 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was |
| 5379 | * already in the table. |
| 5380 | */ |
| 5381 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
| 5382 | |
| 5383 | /* Add application to the notify socket hash table. */ |
| 5384 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); |
| 5385 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); |
| 5386 | |
| 5387 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
| 5388 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
| 5389 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, |
| 5390 | app->v_minor); |
| 5391 | |
| 5392 | rcu_read_unlock(); |
| 5393 | } |
| 5394 | |
| 5395 | /* |
| 5396 | * Set the application version into the object. |
| 5397 | * |
| 5398 | * Return 0 on success else a negative value either an errno code or a |
| 5399 | * LTTng-UST error code. |
| 5400 | */ |
| 5401 | int ust_app_version(struct ust_app *app) |
| 5402 | { |
| 5403 | int ret; |
| 5404 | |
| 5405 | assert(app); |
| 5406 | |
| 5407 | pthread_mutex_lock(&app->sock_lock); |
| 5408 | ret = ustctl_tracer_version(app->sock, &app->version); |
| 5409 | pthread_mutex_unlock(&app->sock_lock); |
| 5410 | if (ret < 0) { |
| 5411 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 5412 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
| 5413 | } else { |
| 5414 | DBG3("UST app %d version failed. Application is dead", app->sock); |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | return ret; |
| 5419 | } |
| 5420 | |
| 5421 | /* |
| 5422 | * Setup the base event notifier group. |
| 5423 | * |
| 5424 | * Return 0 on success else a negative value either an errno code or a |
| 5425 | * LTTng-UST error code. |
| 5426 | */ |
| 5427 | int ust_app_setup_event_notifier_group(struct ust_app *app) |
| 5428 | { |
| 5429 | int ret; |
| 5430 | int event_pipe_write_fd; |
| 5431 | struct lttng_ust_object_data *event_notifier_group = NULL; |
| 5432 | enum lttng_error_code lttng_ret; |
| 5433 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
| 5434 | |
| 5435 | assert(app); |
| 5436 | |
| 5437 | /* Get the write side of the pipe. */ |
| 5438 | event_pipe_write_fd = lttng_pipe_get_writefd( |
| 5439 | app->event_notifier_group.event_pipe); |
| 5440 | |
| 5441 | pthread_mutex_lock(&app->sock_lock); |
| 5442 | ret = ustctl_create_event_notifier_group(app->sock, |
| 5443 | event_pipe_write_fd, &event_notifier_group); |
| 5444 | pthread_mutex_unlock(&app->sock_lock); |
| 5445 | if (ret < 0) { |
| 5446 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 5447 | ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d", |
| 5448 | ret, app->sock, event_pipe_write_fd); |
| 5449 | } else { |
| 5450 | DBG("Failed to create application event notifier group (application is dead): app socket fd = %d", |
| 5451 | app->sock); |
| 5452 | } |
| 5453 | |
| 5454 | goto error; |
| 5455 | } |
| 5456 | |
| 5457 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
| 5458 | if (ret) { |
| 5459 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)", |
| 5460 | app->name, app->ppid); |
| 5461 | goto error; |
| 5462 | } |
| 5463 | |
| 5464 | /* |
| 5465 | * Release the file descriptor that was reserved for the write-end of |
| 5466 | * the pipe. |
| 5467 | */ |
| 5468 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 5469 | |
| 5470 | lttng_ret = notification_thread_command_add_tracer_event_source( |
| 5471 | notification_thread_handle, |
| 5472 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe), |
| 5473 | LTTNG_DOMAIN_UST); |
| 5474 | if (lttng_ret != LTTNG_OK) { |
| 5475 | ERR("Failed to add tracer event source to notification thread"); |
| 5476 | ret = - 1; |
| 5477 | goto error; |
| 5478 | } |
| 5479 | |
| 5480 | /* Assign handle only when the complete setup is valid. */ |
| 5481 | app->event_notifier_group.object = event_notifier_group; |
| 5482 | |
| 5483 | event_notifier_error_accounting_status = event_notifier_error_accounting_register_app(app); |
| 5484 | if (event_notifier_error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 5485 | ERR("Failed to setup event notifier error accounting for app"); |
| 5486 | ret = -1; |
| 5487 | goto error; |
| 5488 | } |
| 5489 | |
| 5490 | return ret; |
| 5491 | |
| 5492 | error: |
| 5493 | ustctl_release_object(app->sock, app->event_notifier_group.object); |
| 5494 | free(app->event_notifier_group.object); |
| 5495 | return ret; |
| 5496 | } |
| 5497 | |
| 5498 | /* |
| 5499 | * Unregister app by removing it from the global traceable app list and freeing |
| 5500 | * the data struct. |
| 5501 | * |
| 5502 | * The socket is already closed at this point so no close to sock. |
| 5503 | */ |
| 5504 | void ust_app_unregister(int sock) |
| 5505 | { |
| 5506 | struct ust_app *lta; |
| 5507 | struct lttng_ht_node_ulong *node; |
| 5508 | struct lttng_ht_iter ust_app_sock_iter; |
| 5509 | struct lttng_ht_iter iter; |
| 5510 | struct ust_app_session *ua_sess; |
| 5511 | int ret; |
| 5512 | |
| 5513 | rcu_read_lock(); |
| 5514 | |
| 5515 | /* Get the node reference for a call_rcu */ |
| 5516 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
| 5517 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); |
| 5518 | assert(node); |
| 5519 | |
| 5520 | lta = caa_container_of(node, struct ust_app, sock_n); |
| 5521 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
| 5522 | |
| 5523 | /* |
| 5524 | * For per-PID buffers, perform "push metadata" and flush all |
| 5525 | * application streams before removing app from hash tables, |
| 5526 | * ensuring proper behavior of data_pending check. |
| 5527 | * Remove sessions so they are not visible during deletion. |
| 5528 | */ |
| 5529 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
| 5530 | node.node) { |
| 5531 | struct ust_registry_session *registry; |
| 5532 | |
| 5533 | ret = lttng_ht_del(lta->sessions, &iter); |
| 5534 | if (ret) { |
| 5535 | /* The session was already removed so scheduled for teardown. */ |
| 5536 | continue; |
| 5537 | } |
| 5538 | |
| 5539 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 5540 | (void) ust_app_flush_app_session(lta, ua_sess); |
| 5541 | } |
| 5542 | |
| 5543 | /* |
| 5544 | * Add session to list for teardown. This is safe since at this point we |
| 5545 | * are the only one using this list. |
| 5546 | */ |
| 5547 | pthread_mutex_lock(&ua_sess->lock); |
| 5548 | |
| 5549 | if (ua_sess->deleted) { |
| 5550 | pthread_mutex_unlock(&ua_sess->lock); |
| 5551 | continue; |
| 5552 | } |
| 5553 | |
| 5554 | /* |
| 5555 | * Normally, this is done in the delete session process which is |
| 5556 | * executed in the call rcu below. However, upon registration we can't |
| 5557 | * afford to wait for the grace period before pushing data or else the |
| 5558 | * data pending feature can race between the unregistration and stop |
| 5559 | * command where the data pending command is sent *before* the grace |
| 5560 | * period ended. |
| 5561 | * |
| 5562 | * The close metadata below nullifies the metadata pointer in the |
| 5563 | * session so the delete session will NOT push/close a second time. |
| 5564 | */ |
| 5565 | registry = get_session_registry(ua_sess); |
| 5566 | if (registry) { |
| 5567 | /* Push metadata for application before freeing the application. */ |
| 5568 | (void) push_metadata(registry, ua_sess->consumer); |
| 5569 | |
| 5570 | /* |
| 5571 | * Don't ask to close metadata for global per UID buffers. Close |
| 5572 | * metadata only on destroy trace session in this case. Also, the |
| 5573 | * previous push metadata could have flag the metadata registry to |
| 5574 | * close so don't send a close command if closed. |
| 5575 | */ |
| 5576 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
| 5577 | /* And ask to close it for this session registry. */ |
| 5578 | (void) close_metadata(registry, ua_sess->consumer); |
| 5579 | } |
| 5580 | } |
| 5581 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
| 5582 | |
| 5583 | pthread_mutex_unlock(&ua_sess->lock); |
| 5584 | } |
| 5585 | |
| 5586 | /* Remove application from PID hash table */ |
| 5587 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); |
| 5588 | assert(!ret); |
| 5589 | |
| 5590 | /* |
| 5591 | * Remove application from notify hash table. The thread handling the |
| 5592 | * notify socket could have deleted the node so ignore on error because |
| 5593 | * either way it's valid. The close of that socket is handled by the |
| 5594 | * apps_notify_thread. |
| 5595 | */ |
| 5596 | iter.iter.node = <a->notify_sock_n.node; |
| 5597 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 5598 | |
| 5599 | /* |
| 5600 | * Ignore return value since the node might have been removed before by an |
| 5601 | * add replace during app registration because the PID can be reassigned by |
| 5602 | * the OS. |
| 5603 | */ |
| 5604 | iter.iter.node = <a->pid_n.node; |
| 5605 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 5606 | if (ret) { |
| 5607 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", |
| 5608 | lta->pid); |
| 5609 | } |
| 5610 | |
| 5611 | /* Free memory */ |
| 5612 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); |
| 5613 | |
| 5614 | rcu_read_unlock(); |
| 5615 | return; |
| 5616 | } |
| 5617 | |
| 5618 | /* |
| 5619 | * Fill events array with all events name of all registered apps. |
| 5620 | */ |
| 5621 | int ust_app_list_events(struct lttng_event **events) |
| 5622 | { |
| 5623 | int ret, handle; |
| 5624 | size_t nbmem, count = 0; |
| 5625 | struct lttng_ht_iter iter; |
| 5626 | struct ust_app *app; |
| 5627 | struct lttng_event *tmp_event; |
| 5628 | |
| 5629 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 5630 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
| 5631 | if (tmp_event == NULL) { |
| 5632 | PERROR("zmalloc ust app events"); |
| 5633 | ret = -ENOMEM; |
| 5634 | goto error; |
| 5635 | } |
| 5636 | |
| 5637 | rcu_read_lock(); |
| 5638 | |
| 5639 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5640 | struct lttng_ust_tracepoint_iter uiter; |
| 5641 | |
| 5642 | health_code_update(); |
| 5643 | |
| 5644 | if (!app->compatible) { |
| 5645 | /* |
| 5646 | * TODO: In time, we should notice the caller of this error by |
| 5647 | * telling him that this is a version error. |
| 5648 | */ |
| 5649 | continue; |
| 5650 | } |
| 5651 | pthread_mutex_lock(&app->sock_lock); |
| 5652 | handle = ustctl_tracepoint_list(app->sock); |
| 5653 | if (handle < 0) { |
| 5654 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 5655 | ERR("UST app list events getting handle failed for app pid %d", |
| 5656 | app->pid); |
| 5657 | } |
| 5658 | pthread_mutex_unlock(&app->sock_lock); |
| 5659 | continue; |
| 5660 | } |
| 5661 | |
| 5662 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
| 5663 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 5664 | /* Handle ustctl error. */ |
| 5665 | if (ret < 0) { |
| 5666 | int release_ret; |
| 5667 | |
| 5668 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 5669 | ERR("UST app tp list get failed for app %d with ret %d", |
| 5670 | app->sock, ret); |
| 5671 | } else { |
| 5672 | DBG3("UST app tp list get failed. Application is dead"); |
| 5673 | /* |
| 5674 | * This is normal behavior, an application can die during the |
| 5675 | * creation process. Don't report an error so the execution can |
| 5676 | * continue normally. Continue normal execution. |
| 5677 | */ |
| 5678 | break; |
| 5679 | } |
| 5680 | free(tmp_event); |
| 5681 | release_ret = ustctl_release_handle(app->sock, handle); |
| 5682 | if (release_ret < 0 && |
| 5683 | release_ret != -LTTNG_UST_ERR_EXITING && |
| 5684 | release_ret != -EPIPE) { |
| 5685 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
| 5686 | } |
| 5687 | pthread_mutex_unlock(&app->sock_lock); |
| 5688 | goto rcu_error; |
| 5689 | } |
| 5690 | |
| 5691 | health_code_update(); |
| 5692 | if (count >= nbmem) { |
| 5693 | /* In case the realloc fails, we free the memory */ |
| 5694 | struct lttng_event *new_tmp_event; |
| 5695 | size_t new_nbmem; |
| 5696 | |
| 5697 | new_nbmem = nbmem << 1; |
| 5698 | DBG2("Reallocating event list from %zu to %zu entries", |
| 5699 | nbmem, new_nbmem); |
| 5700 | new_tmp_event = realloc(tmp_event, |
| 5701 | new_nbmem * sizeof(struct lttng_event)); |
| 5702 | if (new_tmp_event == NULL) { |
| 5703 | int release_ret; |
| 5704 | |
| 5705 | PERROR("realloc ust app events"); |
| 5706 | free(tmp_event); |
| 5707 | ret = -ENOMEM; |
| 5708 | release_ret = ustctl_release_handle(app->sock, handle); |
| 5709 | if (release_ret < 0 && |
| 5710 | release_ret != -LTTNG_UST_ERR_EXITING && |
| 5711 | release_ret != -EPIPE) { |
| 5712 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
| 5713 | } |
| 5714 | pthread_mutex_unlock(&app->sock_lock); |
| 5715 | goto rcu_error; |
| 5716 | } |
| 5717 | /* Zero the new memory */ |
| 5718 | memset(new_tmp_event + nbmem, 0, |
| 5719 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); |
| 5720 | nbmem = new_nbmem; |
| 5721 | tmp_event = new_tmp_event; |
| 5722 | } |
| 5723 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
| 5724 | tmp_event[count].loglevel = uiter.loglevel; |
| 5725 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; |
| 5726 | tmp_event[count].pid = app->pid; |
| 5727 | tmp_event[count].enabled = -1; |
| 5728 | count++; |
| 5729 | } |
| 5730 | ret = ustctl_release_handle(app->sock, handle); |
| 5731 | pthread_mutex_unlock(&app->sock_lock); |
| 5732 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 5733 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | ret = count; |
| 5738 | *events = tmp_event; |
| 5739 | |
| 5740 | DBG2("UST app list events done (%zu events)", count); |
| 5741 | |
| 5742 | rcu_error: |
| 5743 | rcu_read_unlock(); |
| 5744 | error: |
| 5745 | health_code_update(); |
| 5746 | return ret; |
| 5747 | } |
| 5748 | |
| 5749 | /* |
| 5750 | * Fill events array with all events name of all registered apps. |
| 5751 | */ |
| 5752 | int ust_app_list_event_fields(struct lttng_event_field **fields) |
| 5753 | { |
| 5754 | int ret, handle; |
| 5755 | size_t nbmem, count = 0; |
| 5756 | struct lttng_ht_iter iter; |
| 5757 | struct ust_app *app; |
| 5758 | struct lttng_event_field *tmp_event; |
| 5759 | |
| 5760 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 5761 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
| 5762 | if (tmp_event == NULL) { |
| 5763 | PERROR("zmalloc ust app event fields"); |
| 5764 | ret = -ENOMEM; |
| 5765 | goto error; |
| 5766 | } |
| 5767 | |
| 5768 | rcu_read_lock(); |
| 5769 | |
| 5770 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5771 | struct lttng_ust_field_iter uiter; |
| 5772 | |
| 5773 | health_code_update(); |
| 5774 | |
| 5775 | if (!app->compatible) { |
| 5776 | /* |
| 5777 | * TODO: In time, we should notice the caller of this error by |
| 5778 | * telling him that this is a version error. |
| 5779 | */ |
| 5780 | continue; |
| 5781 | } |
| 5782 | pthread_mutex_lock(&app->sock_lock); |
| 5783 | handle = ustctl_tracepoint_field_list(app->sock); |
| 5784 | if (handle < 0) { |
| 5785 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 5786 | ERR("UST app list field getting handle failed for app pid %d", |
| 5787 | app->pid); |
| 5788 | } |
| 5789 | pthread_mutex_unlock(&app->sock_lock); |
| 5790 | continue; |
| 5791 | } |
| 5792 | |
| 5793 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, |
| 5794 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 5795 | /* Handle ustctl error. */ |
| 5796 | if (ret < 0) { |
| 5797 | int release_ret; |
| 5798 | |
| 5799 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 5800 | ERR("UST app tp list field failed for app %d with ret %d", |
| 5801 | app->sock, ret); |
| 5802 | } else { |
| 5803 | DBG3("UST app tp list field failed. Application is dead"); |
| 5804 | /* |
| 5805 | * This is normal behavior, an application can die during the |
| 5806 | * creation process. Don't report an error so the execution can |
| 5807 | * continue normally. Reset list and count for next app. |
| 5808 | */ |
| 5809 | break; |
| 5810 | } |
| 5811 | free(tmp_event); |
| 5812 | release_ret = ustctl_release_handle(app->sock, handle); |
| 5813 | pthread_mutex_unlock(&app->sock_lock); |
| 5814 | if (release_ret < 0 && |
| 5815 | release_ret != -LTTNG_UST_ERR_EXITING && |
| 5816 | release_ret != -EPIPE) { |
| 5817 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
| 5818 | } |
| 5819 | goto rcu_error; |
| 5820 | } |
| 5821 | |
| 5822 | health_code_update(); |
| 5823 | if (count >= nbmem) { |
| 5824 | /* In case the realloc fails, we free the memory */ |
| 5825 | struct lttng_event_field *new_tmp_event; |
| 5826 | size_t new_nbmem; |
| 5827 | |
| 5828 | new_nbmem = nbmem << 1; |
| 5829 | DBG2("Reallocating event field list from %zu to %zu entries", |
| 5830 | nbmem, new_nbmem); |
| 5831 | new_tmp_event = realloc(tmp_event, |
| 5832 | new_nbmem * sizeof(struct lttng_event_field)); |
| 5833 | if (new_tmp_event == NULL) { |
| 5834 | int release_ret; |
| 5835 | |
| 5836 | PERROR("realloc ust app event fields"); |
| 5837 | free(tmp_event); |
| 5838 | ret = -ENOMEM; |
| 5839 | release_ret = ustctl_release_handle(app->sock, handle); |
| 5840 | pthread_mutex_unlock(&app->sock_lock); |
| 5841 | if (release_ret && |
| 5842 | release_ret != -LTTNG_UST_ERR_EXITING && |
| 5843 | release_ret != -EPIPE) { |
| 5844 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
| 5845 | } |
| 5846 | goto rcu_error; |
| 5847 | } |
| 5848 | /* Zero the new memory */ |
| 5849 | memset(new_tmp_event + nbmem, 0, |
| 5850 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); |
| 5851 | nbmem = new_nbmem; |
| 5852 | tmp_event = new_tmp_event; |
| 5853 | } |
| 5854 | |
| 5855 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
| 5856 | /* Mapping between these enums matches 1 to 1. */ |
| 5857 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; |
| 5858 | tmp_event[count].nowrite = uiter.nowrite; |
| 5859 | |
| 5860 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 5861 | tmp_event[count].event.loglevel = uiter.loglevel; |
| 5862 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
| 5863 | tmp_event[count].event.pid = app->pid; |
| 5864 | tmp_event[count].event.enabled = -1; |
| 5865 | count++; |
| 5866 | } |
| 5867 | ret = ustctl_release_handle(app->sock, handle); |
| 5868 | pthread_mutex_unlock(&app->sock_lock); |
| 5869 | if (ret < 0 && |
| 5870 | ret != -LTTNG_UST_ERR_EXITING && |
| 5871 | ret != -EPIPE) { |
| 5872 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | ret = count; |
| 5877 | *fields = tmp_event; |
| 5878 | |
| 5879 | DBG2("UST app list event fields done (%zu events)", count); |
| 5880 | |
| 5881 | rcu_error: |
| 5882 | rcu_read_unlock(); |
| 5883 | error: |
| 5884 | health_code_update(); |
| 5885 | return ret; |
| 5886 | } |
| 5887 | |
| 5888 | /* |
| 5889 | * Free and clean all traceable apps of the global list. |
| 5890 | * |
| 5891 | * Should _NOT_ be called with RCU read-side lock held. |
| 5892 | */ |
| 5893 | void ust_app_clean_list(void) |
| 5894 | { |
| 5895 | int ret; |
| 5896 | struct ust_app *app; |
| 5897 | struct lttng_ht_iter iter; |
| 5898 | |
| 5899 | DBG2("UST app cleaning registered apps hash table"); |
| 5900 | |
| 5901 | rcu_read_lock(); |
| 5902 | |
| 5903 | /* Cleanup notify socket hash table */ |
| 5904 | if (ust_app_ht_by_notify_sock) { |
| 5905 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, |
| 5906 | notify_sock_n.node) { |
| 5907 | /* |
| 5908 | * Assert that all notifiers are gone as all triggers |
| 5909 | * are unregistered prior to this clean-up. |
| 5910 | */ |
| 5911 | assert(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); |
| 5912 | |
| 5913 | ust_app_notify_sock_unregister(app->notify_sock); |
| 5914 | } |
| 5915 | } |
| 5916 | |
| 5917 | if (ust_app_ht) { |
| 5918 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5919 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 5920 | assert(!ret); |
| 5921 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
| 5922 | } |
| 5923 | } |
| 5924 | |
| 5925 | /* Cleanup socket hash table */ |
| 5926 | if (ust_app_ht_by_sock) { |
| 5927 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, |
| 5928 | sock_n.node) { |
| 5929 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
| 5930 | assert(!ret); |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | rcu_read_unlock(); |
| 5935 | |
| 5936 | /* Destroy is done only when the ht is empty */ |
| 5937 | if (ust_app_ht) { |
| 5938 | ht_cleanup_push(ust_app_ht); |
| 5939 | } |
| 5940 | if (ust_app_ht_by_sock) { |
| 5941 | ht_cleanup_push(ust_app_ht_by_sock); |
| 5942 | } |
| 5943 | if (ust_app_ht_by_notify_sock) { |
| 5944 | ht_cleanup_push(ust_app_ht_by_notify_sock); |
| 5945 | } |
| 5946 | } |
| 5947 | |
| 5948 | /* |
| 5949 | * Init UST app hash table. |
| 5950 | */ |
| 5951 | int ust_app_ht_alloc(void) |
| 5952 | { |
| 5953 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5954 | if (!ust_app_ht) { |
| 5955 | return -1; |
| 5956 | } |
| 5957 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5958 | if (!ust_app_ht_by_sock) { |
| 5959 | return -1; |
| 5960 | } |
| 5961 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 5962 | if (!ust_app_ht_by_notify_sock) { |
| 5963 | return -1; |
| 5964 | } |
| 5965 | return 0; |
| 5966 | } |
| 5967 | |
| 5968 | /* |
| 5969 | * For a specific UST session, disable the channel for all registered apps. |
| 5970 | */ |
| 5971 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
| 5972 | struct ltt_ust_channel *uchan) |
| 5973 | { |
| 5974 | int ret = 0; |
| 5975 | struct lttng_ht_iter iter; |
| 5976 | struct lttng_ht_node_str *ua_chan_node; |
| 5977 | struct ust_app *app; |
| 5978 | struct ust_app_session *ua_sess; |
| 5979 | struct ust_app_channel *ua_chan; |
| 5980 | |
| 5981 | assert(usess->active); |
| 5982 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
| 5983 | uchan->name, usess->id); |
| 5984 | |
| 5985 | rcu_read_lock(); |
| 5986 | |
| 5987 | /* For every registered applications */ |
| 5988 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 5989 | struct lttng_ht_iter uiter; |
| 5990 | if (!app->compatible) { |
| 5991 | /* |
| 5992 | * TODO: In time, we should notice the caller of this error by |
| 5993 | * telling him that this is a version error. |
| 5994 | */ |
| 5995 | continue; |
| 5996 | } |
| 5997 | ua_sess = lookup_session_by_app(usess, app); |
| 5998 | if (ua_sess == NULL) { |
| 5999 | continue; |
| 6000 | } |
| 6001 | |
| 6002 | /* Get channel */ |
| 6003 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 6004 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 6005 | /* If the session if found for the app, the channel must be there */ |
| 6006 | assert(ua_chan_node); |
| 6007 | |
| 6008 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 6009 | /* The channel must not be already disabled */ |
| 6010 | assert(ua_chan->enabled == 1); |
| 6011 | |
| 6012 | /* Disable channel onto application */ |
| 6013 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); |
| 6014 | if (ret < 0) { |
| 6015 | /* XXX: We might want to report this error at some point... */ |
| 6016 | continue; |
| 6017 | } |
| 6018 | } |
| 6019 | |
| 6020 | rcu_read_unlock(); |
| 6021 | return ret; |
| 6022 | } |
| 6023 | |
| 6024 | /* |
| 6025 | * For a specific UST session, disable the channel for all registered apps. |
| 6026 | */ |
| 6027 | int ust_app_disable_map_glb(struct ltt_ust_session *usess, |
| 6028 | struct ltt_ust_map *umap) |
| 6029 | { |
| 6030 | int ret = 0; |
| 6031 | struct lttng_ht_iter iter; |
| 6032 | struct lttng_ht_node_str *ua_map_node; |
| 6033 | struct ust_app *app; |
| 6034 | struct ust_app_session *ua_sess; |
| 6035 | struct ust_app_map *ua_map; |
| 6036 | |
| 6037 | assert(usess->active); |
| 6038 | DBG2("UST app disabling map %s from global domain for session id %" PRIu64, |
| 6039 | umap->name, usess->id); |
| 6040 | |
| 6041 | rcu_read_lock(); |
| 6042 | |
| 6043 | /* For every registered applications */ |
| 6044 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6045 | struct lttng_ht_iter uiter; |
| 6046 | if (!app->compatible) { |
| 6047 | /* |
| 6048 | * TODO: In time, we should notice the caller of this error by |
| 6049 | * telling him that this is a version error. |
| 6050 | */ |
| 6051 | continue; |
| 6052 | } |
| 6053 | ua_sess = lookup_session_by_app(usess, app); |
| 6054 | if (ua_sess == NULL) { |
| 6055 | continue; |
| 6056 | } |
| 6057 | |
| 6058 | /* Get map */ |
| 6059 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); |
| 6060 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); |
| 6061 | /* If the session if found for the app, the map must be there */ |
| 6062 | assert(ua_map_node); |
| 6063 | |
| 6064 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 6065 | /* The map must not be already disabled */ |
| 6066 | assert(ua_map->enabled == 1); |
| 6067 | |
| 6068 | /* Disable map onto application */ |
| 6069 | ret = disable_ust_app_map(ua_sess, ua_map, app); |
| 6070 | if (ret < 0) { |
| 6071 | /* XXX: We might want to report this error at some point... */ |
| 6072 | continue; |
| 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | rcu_read_unlock(); |
| 6077 | return ret; |
| 6078 | } |
| 6079 | |
| 6080 | /* |
| 6081 | * For a specific UST session, enable the channel for all registered apps. |
| 6082 | */ |
| 6083 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
| 6084 | struct ltt_ust_channel *uchan) |
| 6085 | { |
| 6086 | int ret = 0; |
| 6087 | struct lttng_ht_iter iter; |
| 6088 | struct ust_app *app; |
| 6089 | struct ust_app_session *ua_sess; |
| 6090 | |
| 6091 | assert(usess->active); |
| 6092 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
| 6093 | uchan->name, usess->id); |
| 6094 | |
| 6095 | rcu_read_lock(); |
| 6096 | |
| 6097 | /* For every registered applications */ |
| 6098 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6099 | if (!app->compatible) { |
| 6100 | /* |
| 6101 | * TODO: In time, we should notice the caller of this error by |
| 6102 | * telling him that this is a version error. |
| 6103 | */ |
| 6104 | continue; |
| 6105 | } |
| 6106 | ua_sess = lookup_session_by_app(usess, app); |
| 6107 | if (ua_sess == NULL) { |
| 6108 | continue; |
| 6109 | } |
| 6110 | |
| 6111 | /* Enable channel onto application */ |
| 6112 | ret = enable_ust_app_channel(ua_sess, uchan, app); |
| 6113 | if (ret < 0) { |
| 6114 | /* XXX: We might want to report this error at some point... */ |
| 6115 | continue; |
| 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | rcu_read_unlock(); |
| 6120 | return ret; |
| 6121 | } |
| 6122 | |
| 6123 | /* |
| 6124 | * For a specific UST session, enable the map for all registered apps. |
| 6125 | */ |
| 6126 | int ust_app_enable_map_glb(struct ltt_ust_session *usess, |
| 6127 | struct ltt_ust_map *umap) |
| 6128 | { |
| 6129 | int ret = 0; |
| 6130 | struct lttng_ht_iter iter; |
| 6131 | struct ust_app *app; |
| 6132 | struct ust_app_session *ua_sess; |
| 6133 | |
| 6134 | assert(usess->active); |
| 6135 | DBG2("UST app enabling map %s to global domain for session id %" PRIu64, |
| 6136 | umap->name, usess->id); |
| 6137 | |
| 6138 | rcu_read_lock(); |
| 6139 | |
| 6140 | /* For every registered applications */ |
| 6141 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6142 | if (!app->compatible) { |
| 6143 | /* |
| 6144 | * TODO: In time, we should notice the caller of this error by |
| 6145 | * telling him that this is a version error. |
| 6146 | */ |
| 6147 | continue; |
| 6148 | } |
| 6149 | ua_sess = lookup_session_by_app(usess, app); |
| 6150 | if (ua_sess == NULL) { |
| 6151 | continue; |
| 6152 | } |
| 6153 | |
| 6154 | /* Enable map onto application */ |
| 6155 | ret = enable_ust_app_map(ua_sess, umap, app); |
| 6156 | if (ret < 0) { |
| 6157 | /* XXX: We might want to report this error at some point... */ |
| 6158 | continue; |
| 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | rcu_read_unlock(); |
| 6163 | return ret; |
| 6164 | } |
| 6165 | |
| 6166 | /* |
| 6167 | * Disable an event in a channel and for a specific session. |
| 6168 | */ |
| 6169 | int ust_app_disable_channel_event_glb(struct ltt_ust_session *usess, |
| 6170 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 6171 | { |
| 6172 | int ret = 0; |
| 6173 | struct lttng_ht_iter iter, uiter; |
| 6174 | struct lttng_ht_node_str *ua_chan_node; |
| 6175 | struct ust_app *app; |
| 6176 | struct ust_app_session *ua_sess; |
| 6177 | struct ust_app_channel *ua_chan; |
| 6178 | struct ust_app_event *ua_event; |
| 6179 | |
| 6180 | assert(usess->active); |
| 6181 | DBG("UST app disabling event %s for all apps in channel " |
| 6182 | "%s for session id %" PRIu64, |
| 6183 | uevent->attr.name, uchan->name, usess->id); |
| 6184 | |
| 6185 | rcu_read_lock(); |
| 6186 | |
| 6187 | /* For all registered applications */ |
| 6188 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6189 | if (!app->compatible) { |
| 6190 | /* |
| 6191 | * TODO: In time, we should notice the caller of this error by |
| 6192 | * telling him that this is a version error. |
| 6193 | */ |
| 6194 | continue; |
| 6195 | } |
| 6196 | ua_sess = lookup_session_by_app(usess, app); |
| 6197 | if (ua_sess == NULL) { |
| 6198 | /* Next app */ |
| 6199 | continue; |
| 6200 | } |
| 6201 | |
| 6202 | /* Lookup channel in the ust app session */ |
| 6203 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 6204 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 6205 | if (ua_chan_node == NULL) { |
| 6206 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
| 6207 | "Skipping", uchan->name, usess->id, app->pid); |
| 6208 | continue; |
| 6209 | } |
| 6210 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 6211 | |
| 6212 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 6213 | uevent->filter, uevent->attr.loglevel, |
| 6214 | uevent->exclusion, uevent->attr.token); |
| 6215 | if (ua_event == NULL) { |
| 6216 | DBG2("Event %s not found in channel %s for app pid %d." |
| 6217 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
| 6218 | continue; |
| 6219 | } |
| 6220 | |
| 6221 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 6222 | if (ret < 0) { |
| 6223 | /* XXX: Report error someday... */ |
| 6224 | continue; |
| 6225 | } |
| 6226 | } |
| 6227 | |
| 6228 | rcu_read_unlock(); |
| 6229 | return ret; |
| 6230 | } |
| 6231 | |
| 6232 | /* |
| 6233 | * Disable an event in a map and for a specific session. |
| 6234 | */ |
| 6235 | int ust_app_disable_map_event_glb(struct ltt_ust_session *usess, |
| 6236 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) |
| 6237 | { |
| 6238 | int ret = 0; |
| 6239 | struct lttng_ht_iter iter, uiter; |
| 6240 | struct lttng_ht_node_str *ua_map_node; |
| 6241 | struct ust_app *app; |
| 6242 | struct ust_app_session *ua_sess; |
| 6243 | struct ust_app_map *ua_map; |
| 6244 | struct ust_app_event *ua_event; |
| 6245 | |
| 6246 | assert(usess->active); |
| 6247 | DBG("UST app disabling event %s for all apps in map " |
| 6248 | "%s for session id %" PRIu64, |
| 6249 | uevent->attr.name, umap->name, usess->id); |
| 6250 | |
| 6251 | rcu_read_lock(); |
| 6252 | |
| 6253 | /* For all registered applications */ |
| 6254 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6255 | if (!app->compatible) { |
| 6256 | /* |
| 6257 | * TODO: In time, we should notice the caller of this error by |
| 6258 | * telling him that this is a version error. |
| 6259 | */ |
| 6260 | continue; |
| 6261 | } |
| 6262 | ua_sess = lookup_session_by_app(usess, app); |
| 6263 | if (ua_sess == NULL) { |
| 6264 | /* Next app */ |
| 6265 | continue; |
| 6266 | } |
| 6267 | |
| 6268 | /* Lookup map in the ust app session */ |
| 6269 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); |
| 6270 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); |
| 6271 | if (ua_map_node == NULL) { |
| 6272 | DBG2("map %s not found in session id %" PRIu64 " for app pid %d." |
| 6273 | "Skipping", umap->name, usess->id, app->pid); |
| 6274 | continue; |
| 6275 | } |
| 6276 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 6277 | |
| 6278 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, |
| 6279 | uevent->filter, uevent->attr.loglevel, |
| 6280 | uevent->exclusion, uevent->attr.token); |
| 6281 | if (ua_event == NULL) { |
| 6282 | DBG2("Event %s not found in map %s for app pid %d." |
| 6283 | "Skipping", uevent->attr.name, umap->name, app->pid); |
| 6284 | continue; |
| 6285 | } |
| 6286 | |
| 6287 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 6288 | if (ret < 0) { |
| 6289 | /* XXX: Report error someday... */ |
| 6290 | continue; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | rcu_read_unlock(); |
| 6295 | return ret; |
| 6296 | } |
| 6297 | |
| 6298 | /* The ua_sess lock must be held by the caller. */ |
| 6299 | static |
| 6300 | int ust_app_channel_create(struct ltt_ust_session *usess, |
| 6301 | struct ust_app_session *ua_sess, |
| 6302 | struct ltt_ust_channel *uchan, struct ust_app *app, |
| 6303 | struct ust_app_channel **_ua_chan) |
| 6304 | { |
| 6305 | int ret = 0; |
| 6306 | struct ust_app_channel *ua_chan = NULL; |
| 6307 | |
| 6308 | assert(ua_sess); |
| 6309 | ASSERT_LOCKED(ua_sess->lock); |
| 6310 | |
| 6311 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, |
| 6312 | sizeof(uchan->name))) { |
| 6313 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
| 6314 | &uchan->attr); |
| 6315 | ret = 0; |
| 6316 | } else { |
| 6317 | struct ltt_ust_context *uctx = NULL; |
| 6318 | |
| 6319 | /* |
| 6320 | * Create channel onto application and synchronize its |
| 6321 | * configuration. |
| 6322 | */ |
| 6323 | ret = ust_app_channel_allocate(ua_sess, uchan, |
| 6324 | LTTNG_UST_CHAN_PER_CPU, usess, |
| 6325 | &ua_chan); |
| 6326 | if (ret < 0) { |
| 6327 | goto error; |
| 6328 | } |
| 6329 | |
| 6330 | ret = ust_app_channel_send(app, usess, |
| 6331 | ua_sess, ua_chan); |
| 6332 | if (ret) { |
| 6333 | goto error; |
| 6334 | } |
| 6335 | |
| 6336 | /* Add contexts. */ |
| 6337 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { |
| 6338 | ret = create_ust_app_channel_context(ua_chan, |
| 6339 | &uctx->ctx, app); |
| 6340 | if (ret) { |
| 6341 | goto error; |
| 6342 | } |
| 6343 | } |
| 6344 | } |
| 6345 | |
| 6346 | error: |
| 6347 | if (ret < 0) { |
| 6348 | switch (ret) { |
| 6349 | case -ENOTCONN: |
| 6350 | /* |
| 6351 | * The application's socket is not valid. Either a bad socket |
| 6352 | * or a timeout on it. We can't inform the caller that for a |
| 6353 | * specific app, the session failed so lets continue here. |
| 6354 | */ |
| 6355 | ret = 0; /* Not an error. */ |
| 6356 | break; |
| 6357 | case -ENOMEM: |
| 6358 | default: |
| 6359 | break; |
| 6360 | } |
| 6361 | } |
| 6362 | |
| 6363 | if (ret == 0 && _ua_chan) { |
| 6364 | /* |
| 6365 | * Only return the application's channel on success. Note |
| 6366 | * that the channel can still be part of the application's |
| 6367 | * channel hashtable on error. |
| 6368 | */ |
| 6369 | *_ua_chan = ua_chan; |
| 6370 | } |
| 6371 | return ret; |
| 6372 | } |
| 6373 | |
| 6374 | /* The ua_sess lock must be held by the caller. */ |
| 6375 | static |
| 6376 | int ust_app_map_create(struct ltt_ust_session *usess, |
| 6377 | struct ust_app_session *ua_sess, |
| 6378 | struct ltt_ust_map *umap, struct ust_app *app, |
| 6379 | struct ust_app_map **_ua_map) |
| 6380 | { |
| 6381 | int ret = 0; |
| 6382 | struct ust_app_map *ua_map = NULL; |
| 6383 | |
| 6384 | assert(ua_sess); |
| 6385 | ASSERT_LOCKED(ua_sess->lock); |
| 6386 | |
| 6387 | /* |
| 6388 | * Create map onto application and synchronize its |
| 6389 | * configuration. |
| 6390 | */ |
| 6391 | ret = ust_app_map_allocate(ua_sess, umap, |
| 6392 | LTTNG_UST_CHAN_PER_CPU, usess, |
| 6393 | &ua_map); |
| 6394 | if (ret < 0) { |
| 6395 | goto error; |
| 6396 | } |
| 6397 | |
| 6398 | ret = ust_app_map_send(app, usess, ua_sess, ua_map); |
| 6399 | if (ret) { |
| 6400 | goto error; |
| 6401 | } |
| 6402 | |
| 6403 | error: |
| 6404 | if (ret < 0) { |
| 6405 | switch (ret) { |
| 6406 | case -ENOTCONN: |
| 6407 | /* |
| 6408 | * The application's socket is not valid. Either a bad socket |
| 6409 | * or a timeout on it. We can't inform the caller that for a |
| 6410 | * specific app, the session failed so lets continue here. |
| 6411 | */ |
| 6412 | ret = 0; /* Not an error. */ |
| 6413 | break; |
| 6414 | case -ENOMEM: |
| 6415 | default: |
| 6416 | break; |
| 6417 | } |
| 6418 | } |
| 6419 | |
| 6420 | if (ret == 0 && _ua_map) { |
| 6421 | /* |
| 6422 | * Only return the application's map on success. Note |
| 6423 | * that the map can still be part of the application's |
| 6424 | * map hashtable on error. |
| 6425 | */ |
| 6426 | *_ua_map = ua_map; |
| 6427 | } |
| 6428 | return ret; |
| 6429 | } |
| 6430 | |
| 6431 | /* |
| 6432 | * Enable event for a specific session and channel on the tracer. |
| 6433 | */ |
| 6434 | int ust_app_enable_channel_event_glb(struct ltt_ust_session *usess, |
| 6435 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 6436 | { |
| 6437 | int ret = 0; |
| 6438 | struct lttng_ht_iter iter, uiter; |
| 6439 | struct lttng_ht_node_str *ua_chan_node; |
| 6440 | struct ust_app *app; |
| 6441 | struct ust_app_session *ua_sess; |
| 6442 | struct ust_app_channel *ua_chan; |
| 6443 | struct ust_app_event *ua_event; |
| 6444 | |
| 6445 | assert(usess->active); |
| 6446 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
| 6447 | uevent->attr.name, usess->id); |
| 6448 | |
| 6449 | /* |
| 6450 | * NOTE: At this point, this function is called only if the session and |
| 6451 | * channel passed are already created for all apps. and enabled on the |
| 6452 | * tracer also. |
| 6453 | */ |
| 6454 | |
| 6455 | rcu_read_lock(); |
| 6456 | |
| 6457 | /* For all registered applications */ |
| 6458 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6459 | if (!app->compatible) { |
| 6460 | /* |
| 6461 | * TODO: In time, we should notice the caller of this error by |
| 6462 | * telling him that this is a version error. |
| 6463 | */ |
| 6464 | continue; |
| 6465 | } |
| 6466 | ua_sess = lookup_session_by_app(usess, app); |
| 6467 | if (!ua_sess) { |
| 6468 | /* The application has problem or is probably dead. */ |
| 6469 | continue; |
| 6470 | } |
| 6471 | |
| 6472 | pthread_mutex_lock(&ua_sess->lock); |
| 6473 | |
| 6474 | if (ua_sess->deleted) { |
| 6475 | pthread_mutex_unlock(&ua_sess->lock); |
| 6476 | continue; |
| 6477 | } |
| 6478 | |
| 6479 | /* Lookup channel in the ust app session */ |
| 6480 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 6481 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 6482 | /* |
| 6483 | * It is possible that the channel cannot be found is |
| 6484 | * the channel/event creation occurs concurrently with |
| 6485 | * an application exit. |
| 6486 | */ |
| 6487 | if (!ua_chan_node) { |
| 6488 | pthread_mutex_unlock(&ua_sess->lock); |
| 6489 | continue; |
| 6490 | } |
| 6491 | |
| 6492 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 6493 | |
| 6494 | /* Get event node */ |
| 6495 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 6496 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, |
| 6497 | uevent->attr.token); |
| 6498 | if (ua_event == NULL) { |
| 6499 | DBG3("UST app enable event %s not found for app PID %d." |
| 6500 | "Skipping app", uevent->attr.name, app->pid); |
| 6501 | goto next_app; |
| 6502 | } |
| 6503 | |
| 6504 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 6505 | if (ret < 0) { |
| 6506 | pthread_mutex_unlock(&ua_sess->lock); |
| 6507 | goto error; |
| 6508 | } |
| 6509 | next_app: |
| 6510 | pthread_mutex_unlock(&ua_sess->lock); |
| 6511 | } |
| 6512 | |
| 6513 | error: |
| 6514 | rcu_read_unlock(); |
| 6515 | return ret; |
| 6516 | } |
| 6517 | |
| 6518 | /* |
| 6519 | * Enable event for a specific session and map on the tracer. |
| 6520 | */ |
| 6521 | int ust_app_enable_map_event_glb(struct ltt_ust_session *usess, |
| 6522 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) |
| 6523 | { |
| 6524 | int ret = 0; |
| 6525 | struct lttng_ht_iter iter, uiter; |
| 6526 | struct lttng_ht_node_str *ua_map_node; |
| 6527 | struct ust_app *app; |
| 6528 | struct ust_app_session *ua_sess; |
| 6529 | struct ust_app_map *ua_map; |
| 6530 | struct ust_app_event *ua_event; |
| 6531 | |
| 6532 | assert(usess->active); |
| 6533 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
| 6534 | uevent->attr.name, usess->id); |
| 6535 | |
| 6536 | /* |
| 6537 | * NOTE: At this point, this function is called only if the session and |
| 6538 | * map passed are already created for all apps. and enabled on the |
| 6539 | * tracer also. |
| 6540 | */ |
| 6541 | |
| 6542 | rcu_read_lock(); |
| 6543 | |
| 6544 | /* For all registered applications */ |
| 6545 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6546 | if (!app->compatible) { |
| 6547 | /* |
| 6548 | * TODO: In time, we should notice the caller of this error by |
| 6549 | * telling him that this is a version error. |
| 6550 | */ |
| 6551 | continue; |
| 6552 | } |
| 6553 | ua_sess = lookup_session_by_app(usess, app); |
| 6554 | if (!ua_sess) { |
| 6555 | /* The application has problem or is probably dead. */ |
| 6556 | continue; |
| 6557 | } |
| 6558 | |
| 6559 | pthread_mutex_lock(&ua_sess->lock); |
| 6560 | |
| 6561 | if (ua_sess->deleted) { |
| 6562 | pthread_mutex_unlock(&ua_sess->lock); |
| 6563 | continue; |
| 6564 | } |
| 6565 | |
| 6566 | /* Lookup map in the ust app session */ |
| 6567 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); |
| 6568 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); |
| 6569 | /* |
| 6570 | * It is possible that the map cannot be found is |
| 6571 | * the map/event creation occurs concurrently with |
| 6572 | * an application exit. |
| 6573 | */ |
| 6574 | if (!ua_map_node) { |
| 6575 | pthread_mutex_unlock(&ua_sess->lock); |
| 6576 | continue; |
| 6577 | } |
| 6578 | |
| 6579 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 6580 | |
| 6581 | /* Get event node */ |
| 6582 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, |
| 6583 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, |
| 6584 | uevent->attr.token); |
| 6585 | if (ua_event == NULL) { |
| 6586 | DBG3("UST app enable event %s not found for app PID %d." |
| 6587 | "Skipping app", uevent->attr.name, app->pid); |
| 6588 | goto next_app; |
| 6589 | } |
| 6590 | |
| 6591 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 6592 | if (ret < 0) { |
| 6593 | pthread_mutex_unlock(&ua_sess->lock); |
| 6594 | goto error; |
| 6595 | } |
| 6596 | next_app: |
| 6597 | pthread_mutex_unlock(&ua_sess->lock); |
| 6598 | } |
| 6599 | |
| 6600 | error: |
| 6601 | rcu_read_unlock(); |
| 6602 | return ret; |
| 6603 | } |
| 6604 | |
| 6605 | /* |
| 6606 | * For a specific existing UST session and UST channel, creates the event for |
| 6607 | * all registered apps. |
| 6608 | */ |
| 6609 | int ust_app_create_channel_event_glb(struct ltt_ust_session *usess, |
| 6610 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 6611 | { |
| 6612 | int ret = 0; |
| 6613 | struct lttng_ht_iter iter, uiter; |
| 6614 | struct lttng_ht_node_str *ua_chan_node; |
| 6615 | struct ust_app *app; |
| 6616 | struct ust_app_session *ua_sess; |
| 6617 | struct ust_app_channel *ua_chan; |
| 6618 | |
| 6619 | assert(usess->active); |
| 6620 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
| 6621 | uevent->attr.name, usess->id); |
| 6622 | |
| 6623 | rcu_read_lock(); |
| 6624 | |
| 6625 | /* For all registered applications */ |
| 6626 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 6627 | if (!app->compatible) { |
| 6628 | /* |
| 6629 | * TODO: In time, we should notice the caller of this error by |
| 6630 | * telling him that this is a version error. |
| 6631 | */ |
| 6632 | continue; |
| 6633 | } |
| 6634 | ua_sess = lookup_session_by_app(usess, app); |
| 6635 | if (!ua_sess) { |
| 6636 | /* The application has problem or is probably dead. */ |
| 6637 | continue; |
| 6638 | } |
| 6639 | |
| 6640 | pthread_mutex_lock(&ua_sess->lock); |
| 6641 | |
| 6642 | if (ua_sess->deleted) { |
| 6643 | pthread_mutex_unlock(&ua_sess->lock); |
| 6644 | continue; |
| 6645 | } |
| 6646 | |
| 6647 | /* Lookup channel in the ust app session */ |
| 6648 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 6649 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 6650 | /* If the channel is not found, there is a code flow error */ |
| 6651 | assert(ua_chan_node); |
| 6652 | |
| 6653 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 6654 | |
| 6655 | ret = create_ust_app_channel_event(ua_sess, ua_chan, uevent, app); |
| 6656 | pthread_mutex_unlock(&ua_sess->lock); |
| 6657 | if (ret < 0) { |
| 6658 | if (ret != -LTTNG_UST_ERR_EXIST) { |
| 6659 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
| 6660 | break; |
| 6661 | } |
| 6662 | DBG2("UST app event %s already exist on app PID %d", |
| 6663 | uevent->attr.name, app->pid); |
| 6664 | continue; |
| 6665 | } |
| 6666 | } |
| 6667 | |
| 6668 | rcu_read_unlock(); |
| 6669 | return ret; |
| 6670 | } |
| 6671 | |
| 6672 | static |
| 6673 | int snapshot_key_values(struct ustctl_daemon_counter *map_handle, |
| 6674 | struct lttng_ht *key_to_bucket_index_ht, int cpu, |
| 6675 | const char *key_filter, struct lttng_ht *values) |
| 6676 | { |
| 6677 | int ret; |
| 6678 | struct lttng_ht_iter key_iter; |
| 6679 | struct ust_registry_map_index_ht_entry *map_index_entry; |
| 6680 | |
| 6681 | /* Iterate over all the formated_key -> counter index */ |
| 6682 | cds_lfht_for_each_entry(key_to_bucket_index_ht->ht, |
| 6683 | &key_iter.iter, map_index_entry, node.node) { |
| 6684 | bool overflow = 0, underflow = 0; |
| 6685 | int64_t local_value = 0; |
| 6686 | size_t dimension_indexes[1] = {map_index_entry->index}; |
| 6687 | |
| 6688 | if (key_filter && strcmp(key_filter, |
| 6689 | map_index_entry->formated_key) != 0) { |
| 6690 | continue; |
| 6691 | } |
| 6692 | |
| 6693 | ret = ustctl_counter_read(map_handle, |
| 6694 | dimension_indexes, cpu, &local_value, |
| 6695 | &overflow, &underflow); |
| 6696 | if (ret) { |
| 6697 | ERR("Error getting counter value from the tracer: key = '%s'", |
| 6698 | map_index_entry->formated_key); |
| 6699 | ret = -1; |
| 6700 | goto end; |
| 6701 | } |
| 6702 | |
| 6703 | map_add_or_increment_map_values(values, |
| 6704 | map_index_entry->formated_key, local_value, |
| 6705 | underflow, overflow); |
| 6706 | } |
| 6707 | ret = 0; |
| 6708 | end: |
| 6709 | return ret; |
| 6710 | } |
| 6711 | |
| 6712 | static |
| 6713 | int ust_app_map_list_values_per_uid_with_bitness_and_cpu( |
| 6714 | const struct ltt_ust_session *usess, |
| 6715 | const struct ltt_ust_map *umap, |
| 6716 | uint32_t app_bitness, |
| 6717 | uint32_t cpu, |
| 6718 | const char *key_filter, |
| 6719 | struct lttng_ht *values) |
| 6720 | { |
| 6721 | int ret = 0; |
| 6722 | struct lttng_ht_iter iter; |
| 6723 | struct buffer_reg_uid *buf_reg_uid; |
| 6724 | struct buffer_reg_map *buf_reg_map; |
| 6725 | struct ust_registry_session *ust_reg_sess; |
| 6726 | struct lttng_ht_node_u64 *ust_reg_map_node; |
| 6727 | struct ust_registry_map *ust_reg_map; |
| 6728 | |
| 6729 | buf_reg_uid = buffer_reg_uid_find(usess->id, app_bitness, usess->uid); |
| 6730 | if (!buf_reg_uid) { |
| 6731 | /* |
| 6732 | * Buffer registry entry for uid not found. Probably no app for |
| 6733 | * this UID at the moment. |
| 6734 | */ |
| 6735 | DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64", bitness = %"PRIu32", uid = %d", |
| 6736 | usess->id, app_bitness, usess->uid); |
| 6737 | /* |
| 6738 | * Not an error. Leave the key value pair unchanged and return. |
| 6739 | */ |
| 6740 | ret = 0; |
| 6741 | goto end; |
| 6742 | } |
| 6743 | |
| 6744 | buf_reg_map = buffer_reg_map_find(umap->id, buf_reg_uid); |
| 6745 | if (!buf_reg_uid) { |
| 6746 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, |
| 6747 | umap->id); |
| 6748 | ret = -1; |
| 6749 | goto end; |
| 6750 | } |
| 6751 | |
| 6752 | ust_reg_sess = buf_reg_uid->registry->reg.ust; |
| 6753 | |
| 6754 | /* Get the ust_reg map object from the registry */ |
| 6755 | // FIXME: frdeso: This can be changed to ust_registry_map_find() right? |
| 6756 | |
| 6757 | lttng_ht_lookup(ust_reg_sess->maps, (void *) &umap->id, &iter); |
| 6758 | ust_reg_map_node = lttng_ht_iter_get_node_u64(&iter); |
| 6759 | if (!ust_reg_map_node) { |
| 6760 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, |
| 6761 | umap->id); |
| 6762 | ret = -1; |
| 6763 | goto end; |
| 6764 | } |
| 6765 | ust_reg_map = caa_container_of(ust_reg_map_node, |
| 6766 | struct ust_registry_map, node); |
| 6767 | |
| 6768 | ret = snapshot_key_values(buf_reg_map->daemon_counter, |
| 6769 | ust_reg_map->key_string_to_bucket_index_ht, |
| 6770 | cpu, key_filter, values); |
| 6771 | if (ret) { |
| 6772 | abort(); |
| 6773 | } |
| 6774 | |
| 6775 | |
| 6776 | ret = 0; |
| 6777 | end: |
| 6778 | return ret; |
| 6779 | } |
| 6780 | |
| 6781 | static |
| 6782 | int ust_app_map_list_values_per_uid(const struct ltt_ust_session *usess, |
| 6783 | const struct ltt_ust_map *umap, |
| 6784 | const struct lttng_map_query *query, |
| 6785 | struct lttng_map_content *map_content) |
| 6786 | { |
| 6787 | int i, ret = 0; |
| 6788 | enum lttng_map_query_status map_query_status; |
| 6789 | const char *key_filter; |
| 6790 | struct lttng_ht *values = NULL; |
| 6791 | bool sum_cpus = lttng_map_query_get_config_sum_by_cpu(query); |
| 6792 | enum lttng_map_query_config_buffer config_buffer; |
| 6793 | enum lttng_map_query_config_cpu config_cpu; |
| 6794 | int selected_cpu; |
| 6795 | |
| 6796 | map_query_status = lttng_map_query_get_key_filter(query, &key_filter); |
| 6797 | if (map_query_status == LTTNG_MAP_QUERY_STATUS_NONE) { |
| 6798 | key_filter = NULL; |
| 6799 | } else if (map_query_status != LTTNG_MAP_QUERY_STATUS_OK) { |
| 6800 | ret = -1; |
| 6801 | goto end; |
| 6802 | } |
| 6803 | |
| 6804 | config_cpu = lttng_map_query_get_config_cpu(query); |
| 6805 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { |
| 6806 | unsigned int count; |
| 6807 | map_query_status = lttng_map_query_get_cpu_count(query, &count); |
| 6808 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 6809 | assert(count == 1); |
| 6810 | |
| 6811 | map_query_status = lttng_map_query_get_cpu_at_index(query, 0, |
| 6812 | &selected_cpu); |
| 6813 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 6814 | } |
| 6815 | |
| 6816 | config_buffer = lttng_map_query_get_config_buffer(query); |
| 6817 | if (config_buffer == LTTNG_MAP_QUERY_CONFIG_BUFFER_UST_UID_SUBSET) { |
| 6818 | unsigned int count; |
| 6819 | uid_t selected_uid; |
| 6820 | |
| 6821 | map_query_status = lttng_map_query_get_uid_count(query, &count); |
| 6822 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 6823 | assert(count == 1); |
| 6824 | |
| 6825 | map_query_status = lttng_map_query_get_uid_at_index(query, 0, |
| 6826 | &selected_uid); |
| 6827 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 6828 | |
| 6829 | if (selected_uid != usess->uid) { |
| 6830 | ret = 0; |
| 6831 | goto end; |
| 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | if (sum_cpus) { |
| 6836 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 6837 | } |
| 6838 | |
| 6839 | for (i = 0; i < umap->nr_cpu; i++) { |
| 6840 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { |
| 6841 | if (selected_cpu != i) { |
| 6842 | continue; |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | if (!sum_cpus) { |
| 6847 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 6848 | } |
| 6849 | |
| 6850 | ret = ust_app_map_list_values_per_uid_with_bitness_and_cpu( |
| 6851 | usess, umap, 32, i, key_filter, |
| 6852 | values); |
| 6853 | if (ret) { |
| 6854 | abort(); |
| 6855 | } |
| 6856 | |
| 6857 | ret = ust_app_map_list_values_per_uid_with_bitness_and_cpu( |
| 6858 | usess, umap, 64, i, key_filter, |
| 6859 | values); |
| 6860 | if (ret) { |
| 6861 | abort(); |
| 6862 | } |
| 6863 | if (!sum_cpus) { |
| 6864 | ret = map_new_content_section(map_content, |
| 6865 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID, |
| 6866 | sum_cpus, usess->uid, i, values); |
| 6867 | if (ret) { |
| 6868 | abort(); |
| 6869 | } |
| 6870 | |
| 6871 | lttng_ht_destroy(values); |
| 6872 | } |
| 6873 | } |
| 6874 | |
| 6875 | if (sum_cpus) { |
| 6876 | ret = map_new_content_section(map_content, |
| 6877 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID, |
| 6878 | sum_cpus, usess->uid, 0, values); |
| 6879 | if (ret) { |
| 6880 | abort(); |
| 6881 | } |
| 6882 | lttng_ht_destroy(values); |
| 6883 | } |
| 6884 | |
| 6885 | end: |
| 6886 | return ret; |
| 6887 | } |
| 6888 | |
| 6889 | static |
| 6890 | int append_dead_app_kv(struct ltt_ust_map *umap, |
| 6891 | const char *key_filter, |
| 6892 | struct lttng_map_content *map_content) |
| 6893 | { |
| 6894 | int ret; |
| 6895 | struct lttng_ht *dead_app_kv_ht; |
| 6896 | struct map_kv_ht_entry *kv_entry; |
| 6897 | struct lttng_ht_iter key_iter; |
| 6898 | |
| 6899 | struct lttng_ht *values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 6900 | |
| 6901 | pthread_mutex_lock(&(umap->dead_app_kv_values.lock)); |
| 6902 | |
| 6903 | assert(umap->dead_app_kv_values.dead_app_kv_values_64bits); |
| 6904 | dead_app_kv_ht = umap->dead_app_kv_values.dead_app_kv_values_64bits; |
| 6905 | |
| 6906 | cds_lfht_for_each_entry(dead_app_kv_ht->ht, &key_iter.iter, kv_entry, |
| 6907 | node.node) { |
| 6908 | if (key_filter && strcmp(key_filter, kv_entry->key) != 0) { |
| 6909 | continue; |
| 6910 | } |
| 6911 | map_add_or_increment_map_values(values, kv_entry->key, |
| 6912 | kv_entry->value, kv_entry->has_underflowed, |
| 6913 | kv_entry->has_overflowed); |
| 6914 | } |
| 6915 | |
| 6916 | assert(umap->dead_app_kv_values.dead_app_kv_values_32bits); |
| 6917 | |
| 6918 | dead_app_kv_ht = umap->dead_app_kv_values.dead_app_kv_values_32bits; |
| 6919 | cds_lfht_for_each_entry(dead_app_kv_ht->ht, &key_iter.iter, kv_entry, |
| 6920 | node.node) { |
| 6921 | if (key_filter && strcmp(key_filter, kv_entry->key) != 0) { |
| 6922 | continue; |
| 6923 | } |
| 6924 | map_add_or_increment_map_values(values, kv_entry->key, |
| 6925 | kv_entry->value, kv_entry->has_underflowed, |
| 6926 | kv_entry->has_overflowed); |
| 6927 | } |
| 6928 | |
| 6929 | pthread_mutex_unlock(&umap->dead_app_kv_values.lock); |
| 6930 | |
| 6931 | ret = map_new_content_section(map_content, |
| 6932 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID_AGGREGATED, |
| 6933 | true, 0, 0, values); |
| 6934 | |
| 6935 | lttng_ht_destroy(values); |
| 6936 | if (ret) { |
| 6937 | ERR("Error appending deadapp kv"); |
| 6938 | goto end; |
| 6939 | } |
| 6940 | |
| 6941 | |
| 6942 | ret = 0; |
| 6943 | |
| 6944 | end: |
| 6945 | return ret; |
| 6946 | } |
| 6947 | |
| 6948 | static |
| 6949 | int ust_app_map_list_values_per_pid_with_bitness_and_cpu( |
| 6950 | const struct ltt_ust_session *usess, |
| 6951 | struct ust_app *app, |
| 6952 | struct ltt_ust_map *umap, |
| 6953 | uint32_t app_bitness, |
| 6954 | uint32_t cpu, |
| 6955 | const char *key_filter, |
| 6956 | struct lttng_ht *values) |
| 6957 | { |
| 6958 | int ret = 0; |
| 6959 | |
| 6960 | struct lttng_ht_iter map_iter; |
| 6961 | struct lttng_ht_node_str *ua_map_node; |
| 6962 | struct ust_app_map *ua_map; |
| 6963 | struct ust_app_session *ua_sess; |
| 6964 | struct ust_registry_session *ust_reg_sess; |
| 6965 | struct ust_registry_map *ust_reg_map; |
| 6966 | |
| 6967 | if (app->bits_per_long != app_bitness) { |
| 6968 | ret = 0; |
| 6969 | goto end;; |
| 6970 | } |
| 6971 | |
| 6972 | ua_sess = lookup_session_by_app(usess, app); |
| 6973 | if (!ua_sess) { |
| 6974 | /* Session not associated with this app. */ |
| 6975 | ret = 0; |
| 6976 | goto end;; |
| 6977 | } |
| 6978 | |
| 6979 | ust_reg_sess = get_session_registry(ua_sess); |
| 6980 | if (!ust_reg_sess) { |
| 6981 | DBG("Application session is being torn down. Skip application."); |
| 6982 | ret = 0; |
| 6983 | goto end;; |
| 6984 | } |
| 6985 | |
| 6986 | /* Lookup map in the ust app session */ |
| 6987 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &map_iter); |
| 6988 | ua_map_node = lttng_ht_iter_get_node_str(&map_iter); |
| 6989 | |
| 6990 | assert(ua_map_node != NULL); |
| 6991 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 6992 | |
| 6993 | pthread_mutex_lock(&ust_reg_sess->lock); |
| 6994 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); |
| 6995 | pthread_mutex_unlock(&ust_reg_sess->lock); |
| 6996 | assert(ust_reg_map); |
| 6997 | |
| 6998 | ret = snapshot_key_values(ua_map->map_handle, |
| 6999 | ust_reg_map->key_string_to_bucket_index_ht, |
| 7000 | cpu, key_filter, values); |
| 7001 | if (ret) { |
| 7002 | ERR("Error snapshoting the content of map"); |
| 7003 | goto end; |
| 7004 | } |
| 7005 | |
| 7006 | end: |
| 7007 | return ret; |
| 7008 | } |
| 7009 | |
| 7010 | static |
| 7011 | int ust_app_map_list_values_per_pid(const struct ltt_ust_session *usess, |
| 7012 | struct ltt_ust_map *umap, |
| 7013 | const struct lttng_map_query *query, |
| 7014 | struct lttng_map_content *map_content) |
| 7015 | { |
| 7016 | enum lttng_map_query_status map_query_status; |
| 7017 | const char *key_filter; |
| 7018 | struct lttng_ht *values; |
| 7019 | bool sum_cpus = lttng_map_query_get_config_sum_by_cpu(query); |
| 7020 | bool sum_pids = lttng_map_query_get_config_sum_by_pid(query); |
| 7021 | enum lttng_map_query_config_cpu config_cpu; |
| 7022 | int selected_cpu, i, ret = 0; |
| 7023 | struct lttng_ht_iter app_iter; |
| 7024 | struct ust_app *app; |
| 7025 | |
| 7026 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 7027 | |
| 7028 | map_query_status = lttng_map_query_get_key_filter(query, &key_filter); |
| 7029 | if (map_query_status == LTTNG_MAP_QUERY_STATUS_NONE) { |
| 7030 | key_filter = NULL; |
| 7031 | } else if (map_query_status != LTTNG_MAP_QUERY_STATUS_OK) { |
| 7032 | ret = -1; |
| 7033 | goto end; |
| 7034 | } |
| 7035 | |
| 7036 | config_cpu = lttng_map_query_get_config_cpu(query); |
| 7037 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { |
| 7038 | unsigned int count; |
| 7039 | map_query_status = lttng_map_query_get_cpu_count(query, &count); |
| 7040 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 7041 | assert(count == 1); |
| 7042 | |
| 7043 | map_query_status = lttng_map_query_get_cpu_at_index(query, 0, |
| 7044 | &selected_cpu); |
| 7045 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); |
| 7046 | } |
| 7047 | |
| 7048 | /* Sum all cpus and pids on the same table. */ |
| 7049 | if (sum_cpus && sum_pids) { |
| 7050 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 7051 | } |
| 7052 | |
| 7053 | if (!sum_cpus && sum_pids) { |
| 7054 | /* Iterate over all currently registered apps. */ |
| 7055 | for (i = 0; i < umap->nr_cpu; i++) { |
| 7056 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 7057 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { |
| 7058 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( |
| 7059 | usess, app, umap, 32, i, key_filter, values); |
| 7060 | if (ret) { |
| 7061 | abort(); |
| 7062 | } |
| 7063 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( |
| 7064 | usess, app, umap, 64, i, key_filter, values); |
| 7065 | if (ret) { |
| 7066 | abort(); |
| 7067 | } |
| 7068 | } |
| 7069 | ret = map_new_content_section(map_content, |
| 7070 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, |
| 7071 | sum_cpus, app->pid, i, values); |
| 7072 | if (ret) { |
| 7073 | abort(); |
| 7074 | } |
| 7075 | |
| 7076 | lttng_ht_destroy(values); |
| 7077 | } |
| 7078 | } else { |
| 7079 | /* Iterate over all currently registered apps. */ |
| 7080 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { |
| 7081 | |
| 7082 | if (sum_cpus && !sum_pids) { |
| 7083 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 7084 | } |
| 7085 | |
| 7086 | for (i = 0; i < umap->nr_cpu; i++) { |
| 7087 | |
| 7088 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { |
| 7089 | if (selected_cpu != i) { |
| 7090 | continue; |
| 7091 | } |
| 7092 | } |
| 7093 | |
| 7094 | if (!sum_cpus && !sum_pids) { |
| 7095 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 7096 | } |
| 7097 | |
| 7098 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( |
| 7099 | usess, app, umap, 32, i, key_filter, values); |
| 7100 | if (ret) { |
| 7101 | abort(); |
| 7102 | } |
| 7103 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( |
| 7104 | usess, app, umap, 64, i, key_filter, values); |
| 7105 | if (ret) { |
| 7106 | abort(); |
| 7107 | } |
| 7108 | |
| 7109 | if (!sum_cpus && !sum_pids) { |
| 7110 | ret = map_new_content_section(map_content, |
| 7111 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, |
| 7112 | sum_cpus, app->pid, i, values); |
| 7113 | if (ret) { |
| 7114 | abort(); |
| 7115 | } |
| 7116 | |
| 7117 | lttng_ht_destroy(values); |
| 7118 | } |
| 7119 | } |
| 7120 | if (sum_cpus && !sum_pids) { |
| 7121 | ret = map_new_content_section(map_content, |
| 7122 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, |
| 7123 | sum_cpus, app->pid, i, values); |
| 7124 | if (ret) { |
| 7125 | abort(); |
| 7126 | } |
| 7127 | |
| 7128 | lttng_ht_destroy(values); |
| 7129 | } |
| 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | if (sum_cpus && sum_pids) { |
| 7134 | ret = map_new_content_section(map_content, |
| 7135 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, |
| 7136 | sum_cpus, 0, 0, values); |
| 7137 | if (ret) { |
| 7138 | abort(); |
| 7139 | } |
| 7140 | lttng_ht_destroy(values); |
| 7141 | } |
| 7142 | |
| 7143 | /* Append dead app aggregated key-value pairs. */ |
| 7144 | ret = append_dead_app_kv(umap, key_filter, map_content); |
| 7145 | if (ret) { |
| 7146 | ERR("Error appending values from dead apps map"); |
| 7147 | goto end; |
| 7148 | } |
| 7149 | |
| 7150 | end: |
| 7151 | return ret; |
| 7152 | } |
| 7153 | |
| 7154 | int ust_app_map_list_values(const struct ltt_ust_session *usess, |
| 7155 | struct ltt_ust_map *umap, |
| 7156 | const struct lttng_map_query *query, |
| 7157 | struct lttng_map_content **map_content) |
| 7158 | { |
| 7159 | int ret; |
| 7160 | struct lttng_map_content *local_map_content = NULL; |
| 7161 | |
| 7162 | local_map_content = lttng_map_content_create(usess->buffer_type); |
| 7163 | if (!local_map_content) { |
| 7164 | ERR("Error creating a map content list"); |
| 7165 | ret = -1; |
| 7166 | goto end; |
| 7167 | } |
| 7168 | rcu_read_lock(); |
| 7169 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 7170 | ret = ust_app_map_list_values_per_uid(usess, umap, query, |
| 7171 | local_map_content); |
| 7172 | if (ret) { |
| 7173 | ERR("Error adding per-uid map value"); |
| 7174 | ret = -1; |
| 7175 | goto end; |
| 7176 | } |
| 7177 | } else { |
| 7178 | ret = ust_app_map_list_values_per_pid(usess, umap, query, |
| 7179 | local_map_content); |
| 7180 | if (ret) { |
| 7181 | ERR("Error adding per-pid map value"); |
| 7182 | ret = -1; |
| 7183 | goto end; |
| 7184 | } |
| 7185 | } |
| 7186 | *map_content = local_map_content; |
| 7187 | local_map_content = NULL; |
| 7188 | ret = 0; |
| 7189 | end: |
| 7190 | rcu_read_unlock(); |
| 7191 | |
| 7192 | lttng_map_content_destroy(local_map_content); |
| 7193 | return ret; |
| 7194 | } |
| 7195 | |
| 7196 | /* |
| 7197 | * For a specific existing UST session and UST map, creates the event for |
| 7198 | * all registered apps. |
| 7199 | */ |
| 7200 | int ust_app_create_map_event_glb(struct ltt_ust_session *usess, |
| 7201 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) |
| 7202 | { |
| 7203 | int ret = 0; |
| 7204 | struct lttng_ht_iter iter, uiter; |
| 7205 | struct lttng_ht_node_str *ua_map_node; |
| 7206 | struct ust_app *app; |
| 7207 | struct ust_app_session *ua_sess; |
| 7208 | struct ust_app_map *ua_map; |
| 7209 | |
| 7210 | assert(usess->active); |
| 7211 | DBG("UST app creating event %s in map %s for all apps for session id %" PRIu64, |
| 7212 | uevent->attr.name, umap->name, usess->id); |
| 7213 | |
| 7214 | rcu_read_lock(); |
| 7215 | |
| 7216 | /* For all registered applications */ |
| 7217 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 7218 | if (!app->compatible) { |
| 7219 | /* |
| 7220 | * TODO: In time, we should notice the caller of this error by |
| 7221 | * telling him that this is a version error. |
| 7222 | */ |
| 7223 | continue; |
| 7224 | } |
| 7225 | ua_sess = lookup_session_by_app(usess, app); |
| 7226 | if (!ua_sess) { |
| 7227 | /* The application has problem or is probably dead. */ |
| 7228 | continue; |
| 7229 | } |
| 7230 | |
| 7231 | pthread_mutex_lock(&ua_sess->lock); |
| 7232 | |
| 7233 | if (ua_sess->deleted) { |
| 7234 | pthread_mutex_unlock(&ua_sess->lock); |
| 7235 | continue; |
| 7236 | } |
| 7237 | |
| 7238 | /* Lookup map in the ust app session */ |
| 7239 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); |
| 7240 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); |
| 7241 | /* If the map is not found, there is a code flow error */ |
| 7242 | assert(ua_map_node); |
| 7243 | |
| 7244 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 7245 | |
| 7246 | ret = create_ust_app_map_event(ua_sess, ua_map, uevent, app); |
| 7247 | assert(!ret); |
| 7248 | pthread_mutex_unlock(&ua_sess->lock); |
| 7249 | if (ret < 0) { |
| 7250 | if (ret != -LTTNG_UST_ERR_EXIST) { |
| 7251 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
| 7252 | break; |
| 7253 | } |
| 7254 | DBG2("UST app event %s already exist on app PID %d", |
| 7255 | uevent->attr.name, app->pid); |
| 7256 | continue; |
| 7257 | } |
| 7258 | } |
| 7259 | |
| 7260 | rcu_read_unlock(); |
| 7261 | return ret; |
| 7262 | } |
| 7263 | |
| 7264 | /* |
| 7265 | * Start tracing for a specific UST session and app. |
| 7266 | * |
| 7267 | * Called with UST app session lock held. |
| 7268 | * |
| 7269 | */ |
| 7270 | static |
| 7271 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 7272 | { |
| 7273 | int ret = 0; |
| 7274 | struct ust_app_session *ua_sess; |
| 7275 | |
| 7276 | DBG("Starting tracing for ust app pid %d", app->pid); |
| 7277 | |
| 7278 | rcu_read_lock(); |
| 7279 | |
| 7280 | if (!app->compatible) { |
| 7281 | goto end; |
| 7282 | } |
| 7283 | |
| 7284 | ua_sess = lookup_session_by_app(usess, app); |
| 7285 | if (ua_sess == NULL) { |
| 7286 | /* The session is in teardown process. Ignore and continue. */ |
| 7287 | goto end; |
| 7288 | } |
| 7289 | |
| 7290 | pthread_mutex_lock(&ua_sess->lock); |
| 7291 | |
| 7292 | if (ua_sess->deleted) { |
| 7293 | pthread_mutex_unlock(&ua_sess->lock); |
| 7294 | goto end; |
| 7295 | } |
| 7296 | |
| 7297 | if (ua_sess->enabled) { |
| 7298 | pthread_mutex_unlock(&ua_sess->lock); |
| 7299 | goto end; |
| 7300 | } |
| 7301 | |
| 7302 | /* Upon restart, we skip the setup, already done */ |
| 7303 | if (ua_sess->started) { |
| 7304 | goto skip_setup; |
| 7305 | } |
| 7306 | |
| 7307 | health_code_update(); |
| 7308 | |
| 7309 | skip_setup: |
| 7310 | /* This starts the UST tracing */ |
| 7311 | pthread_mutex_lock(&app->sock_lock); |
| 7312 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
| 7313 | pthread_mutex_unlock(&app->sock_lock); |
| 7314 | if (ret < 0) { |
| 7315 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 7316 | ERR("Error starting tracing for app pid: %d (ret: %d)", |
| 7317 | app->pid, ret); |
| 7318 | } else { |
| 7319 | DBG("UST app start session failed. Application is dead."); |
| 7320 | /* |
| 7321 | * This is normal behavior, an application can die during the |
| 7322 | * creation process. Don't report an error so the execution can |
| 7323 | * continue normally. |
| 7324 | */ |
| 7325 | pthread_mutex_unlock(&ua_sess->lock); |
| 7326 | goto end; |
| 7327 | } |
| 7328 | goto error_unlock; |
| 7329 | } |
| 7330 | |
| 7331 | /* Indicate that the session has been started once */ |
| 7332 | ua_sess->started = 1; |
| 7333 | ua_sess->enabled = 1; |
| 7334 | |
| 7335 | pthread_mutex_unlock(&ua_sess->lock); |
| 7336 | |
| 7337 | health_code_update(); |
| 7338 | |
| 7339 | /* Quiescent wait after starting trace */ |
| 7340 | pthread_mutex_lock(&app->sock_lock); |
| 7341 | ret = ustctl_wait_quiescent(app->sock); |
| 7342 | pthread_mutex_unlock(&app->sock_lock); |
| 7343 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 7344 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 7345 | app->pid, ret); |
| 7346 | } |
| 7347 | |
| 7348 | end: |
| 7349 | rcu_read_unlock(); |
| 7350 | health_code_update(); |
| 7351 | return 0; |
| 7352 | |
| 7353 | error_unlock: |
| 7354 | pthread_mutex_unlock(&ua_sess->lock); |
| 7355 | rcu_read_unlock(); |
| 7356 | health_code_update(); |
| 7357 | return -1; |
| 7358 | } |
| 7359 | |
| 7360 | /* |
| 7361 | * Stop tracing for a specific UST session and app. |
| 7362 | */ |
| 7363 | static |
| 7364 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 7365 | { |
| 7366 | int ret = 0; |
| 7367 | struct ust_app_session *ua_sess; |
| 7368 | struct ust_registry_session *registry; |
| 7369 | |
| 7370 | DBG("Stopping tracing for ust app pid %d", app->pid); |
| 7371 | |
| 7372 | rcu_read_lock(); |
| 7373 | |
| 7374 | if (!app->compatible) { |
| 7375 | goto end_no_session; |
| 7376 | } |
| 7377 | |
| 7378 | ua_sess = lookup_session_by_app(usess, app); |
| 7379 | if (ua_sess == NULL) { |
| 7380 | goto end_no_session; |
| 7381 | } |
| 7382 | |
| 7383 | pthread_mutex_lock(&ua_sess->lock); |
| 7384 | |
| 7385 | if (ua_sess->deleted) { |
| 7386 | pthread_mutex_unlock(&ua_sess->lock); |
| 7387 | goto end_no_session; |
| 7388 | } |
| 7389 | |
| 7390 | /* |
| 7391 | * If started = 0, it means that stop trace has been called for a session |
| 7392 | * that was never started. It's possible since we can have a fail start |
| 7393 | * from either the application manager thread or the command thread. Simply |
| 7394 | * indicate that this is a stop error. |
| 7395 | */ |
| 7396 | if (!ua_sess->started) { |
| 7397 | goto error_rcu_unlock; |
| 7398 | } |
| 7399 | |
| 7400 | health_code_update(); |
| 7401 | |
| 7402 | /* This inhibits UST tracing */ |
| 7403 | pthread_mutex_lock(&app->sock_lock); |
| 7404 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
| 7405 | pthread_mutex_unlock(&app->sock_lock); |
| 7406 | if (ret < 0) { |
| 7407 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 7408 | ERR("Error stopping tracing for app pid: %d (ret: %d)", |
| 7409 | app->pid, ret); |
| 7410 | } else { |
| 7411 | DBG("UST app stop session failed. Application is dead."); |
| 7412 | /* |
| 7413 | * This is normal behavior, an application can die during the |
| 7414 | * creation process. Don't report an error so the execution can |
| 7415 | * continue normally. |
| 7416 | */ |
| 7417 | goto end_unlock; |
| 7418 | } |
| 7419 | goto error_rcu_unlock; |
| 7420 | } |
| 7421 | |
| 7422 | health_code_update(); |
| 7423 | ua_sess->enabled = 0; |
| 7424 | |
| 7425 | /* Quiescent wait after stopping trace */ |
| 7426 | pthread_mutex_lock(&app->sock_lock); |
| 7427 | ret = ustctl_wait_quiescent(app->sock); |
| 7428 | pthread_mutex_unlock(&app->sock_lock); |
| 7429 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 7430 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 7431 | app->pid, ret); |
| 7432 | } |
| 7433 | |
| 7434 | health_code_update(); |
| 7435 | |
| 7436 | registry = get_session_registry(ua_sess); |
| 7437 | |
| 7438 | /* The UST app session is held registry shall not be null. */ |
| 7439 | assert(registry); |
| 7440 | |
| 7441 | /* Push metadata for application before freeing the application. */ |
| 7442 | (void) push_metadata(registry, ua_sess->consumer); |
| 7443 | |
| 7444 | end_unlock: |
| 7445 | pthread_mutex_unlock(&ua_sess->lock); |
| 7446 | end_no_session: |
| 7447 | rcu_read_unlock(); |
| 7448 | health_code_update(); |
| 7449 | return 0; |
| 7450 | |
| 7451 | error_rcu_unlock: |
| 7452 | pthread_mutex_unlock(&ua_sess->lock); |
| 7453 | rcu_read_unlock(); |
| 7454 | health_code_update(); |
| 7455 | return -1; |
| 7456 | } |
| 7457 | |
| 7458 | static |
| 7459 | int ust_app_flush_app_session(struct ust_app *app, |
| 7460 | struct ust_app_session *ua_sess) |
| 7461 | { |
| 7462 | int ret, retval = 0; |
| 7463 | struct lttng_ht_iter iter; |
| 7464 | struct ust_app_channel *ua_chan; |
| 7465 | struct consumer_socket *socket; |
| 7466 | |
| 7467 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
| 7468 | |
| 7469 | rcu_read_lock(); |
| 7470 | |
| 7471 | if (!app->compatible) { |
| 7472 | goto end_not_compatible; |
| 7473 | } |
| 7474 | |
| 7475 | pthread_mutex_lock(&ua_sess->lock); |
| 7476 | |
| 7477 | if (ua_sess->deleted) { |
| 7478 | goto end_deleted; |
| 7479 | } |
| 7480 | |
| 7481 | health_code_update(); |
| 7482 | |
| 7483 | /* Flushing buffers */ |
| 7484 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 7485 | ua_sess->consumer); |
| 7486 | |
| 7487 | /* Flush buffers and push metadata. */ |
| 7488 | switch (ua_sess->buffer_type) { |
| 7489 | case LTTNG_BUFFER_PER_PID: |
| 7490 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 7491 | node.node) { |
| 7492 | health_code_update(); |
| 7493 | ret = consumer_flush_channel(socket, ua_chan->key); |
| 7494 | if (ret) { |
| 7495 | ERR("Error flushing consumer channel"); |
| 7496 | retval = -1; |
| 7497 | continue; |
| 7498 | } |
| 7499 | } |
| 7500 | break; |
| 7501 | case LTTNG_BUFFER_PER_UID: |
| 7502 | default: |
| 7503 | assert(0); |
| 7504 | break; |
| 7505 | } |
| 7506 | |
| 7507 | health_code_update(); |
| 7508 | |
| 7509 | end_deleted: |
| 7510 | pthread_mutex_unlock(&ua_sess->lock); |
| 7511 | |
| 7512 | end_not_compatible: |
| 7513 | rcu_read_unlock(); |
| 7514 | health_code_update(); |
| 7515 | return retval; |
| 7516 | } |
| 7517 | |
| 7518 | /* |
| 7519 | * Flush buffers for all applications for a specific UST session. |
| 7520 | * Called with UST session lock held. |
| 7521 | */ |
| 7522 | static |
| 7523 | int ust_app_flush_session(struct ltt_ust_session *usess) |
| 7524 | |
| 7525 | { |
| 7526 | int ret = 0; |
| 7527 | |
| 7528 | DBG("Flushing session buffers for all ust apps"); |
| 7529 | |
| 7530 | rcu_read_lock(); |
| 7531 | |
| 7532 | /* Flush buffers and push metadata. */ |
| 7533 | switch (usess->buffer_type) { |
| 7534 | case LTTNG_BUFFER_PER_UID: |
| 7535 | { |
| 7536 | struct buffer_reg_uid *reg; |
| 7537 | struct lttng_ht_iter iter; |
| 7538 | |
| 7539 | /* Flush all per UID buffers associated to that session. */ |
| 7540 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 7541 | struct ust_registry_session *ust_session_reg; |
| 7542 | struct buffer_reg_channel *buf_reg_chan; |
| 7543 | struct consumer_socket *socket; |
| 7544 | |
| 7545 | /* Get consumer socket to use to push the metadata.*/ |
| 7546 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 7547 | usess->consumer); |
| 7548 | if (!socket) { |
| 7549 | /* Ignore request if no consumer is found for the session. */ |
| 7550 | continue; |
| 7551 | } |
| 7552 | |
| 7553 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 7554 | buf_reg_chan, node.node) { |
| 7555 | /* |
| 7556 | * The following call will print error values so the return |
| 7557 | * code is of little importance because whatever happens, we |
| 7558 | * have to try them all. |
| 7559 | */ |
| 7560 | (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key); |
| 7561 | } |
| 7562 | |
| 7563 | ust_session_reg = reg->registry->reg.ust; |
| 7564 | /* Push metadata. */ |
| 7565 | (void) push_metadata(ust_session_reg, usess->consumer); |
| 7566 | } |
| 7567 | break; |
| 7568 | } |
| 7569 | case LTTNG_BUFFER_PER_PID: |
| 7570 | { |
| 7571 | struct ust_app_session *ua_sess; |
| 7572 | struct lttng_ht_iter iter; |
| 7573 | struct ust_app *app; |
| 7574 | |
| 7575 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 7576 | ua_sess = lookup_session_by_app(usess, app); |
| 7577 | if (ua_sess == NULL) { |
| 7578 | continue; |
| 7579 | } |
| 7580 | (void) ust_app_flush_app_session(app, ua_sess); |
| 7581 | } |
| 7582 | break; |
| 7583 | } |
| 7584 | default: |
| 7585 | ret = -1; |
| 7586 | assert(0); |
| 7587 | break; |
| 7588 | } |
| 7589 | |
| 7590 | rcu_read_unlock(); |
| 7591 | health_code_update(); |
| 7592 | return ret; |
| 7593 | } |
| 7594 | |
| 7595 | static |
| 7596 | int ust_app_clear_quiescent_app_session(struct ust_app *app, |
| 7597 | struct ust_app_session *ua_sess) |
| 7598 | { |
| 7599 | int ret = 0; |
| 7600 | struct lttng_ht_iter iter; |
| 7601 | struct ust_app_channel *ua_chan; |
| 7602 | struct consumer_socket *socket; |
| 7603 | |
| 7604 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); |
| 7605 | |
| 7606 | rcu_read_lock(); |
| 7607 | |
| 7608 | if (!app->compatible) { |
| 7609 | goto end_not_compatible; |
| 7610 | } |
| 7611 | |
| 7612 | pthread_mutex_lock(&ua_sess->lock); |
| 7613 | |
| 7614 | if (ua_sess->deleted) { |
| 7615 | goto end_unlock; |
| 7616 | } |
| 7617 | |
| 7618 | health_code_update(); |
| 7619 | |
| 7620 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 7621 | ua_sess->consumer); |
| 7622 | if (!socket) { |
| 7623 | ERR("Failed to find consumer (%" PRIu32 ") socket", |
| 7624 | app->bits_per_long); |
| 7625 | ret = -1; |
| 7626 | goto end_unlock; |
| 7627 | } |
| 7628 | |
| 7629 | /* Clear quiescent state. */ |
| 7630 | switch (ua_sess->buffer_type) { |
| 7631 | case LTTNG_BUFFER_PER_PID: |
| 7632 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, |
| 7633 | ua_chan, node.node) { |
| 7634 | health_code_update(); |
| 7635 | ret = consumer_clear_quiescent_channel(socket, |
| 7636 | ua_chan->key); |
| 7637 | if (ret) { |
| 7638 | ERR("Error clearing quiescent state for consumer channel"); |
| 7639 | ret = -1; |
| 7640 | continue; |
| 7641 | } |
| 7642 | } |
| 7643 | break; |
| 7644 | case LTTNG_BUFFER_PER_UID: |
| 7645 | default: |
| 7646 | assert(0); |
| 7647 | ret = -1; |
| 7648 | break; |
| 7649 | } |
| 7650 | |
| 7651 | health_code_update(); |
| 7652 | |
| 7653 | end_unlock: |
| 7654 | pthread_mutex_unlock(&ua_sess->lock); |
| 7655 | |
| 7656 | end_not_compatible: |
| 7657 | rcu_read_unlock(); |
| 7658 | health_code_update(); |
| 7659 | return ret; |
| 7660 | } |
| 7661 | |
| 7662 | /* |
| 7663 | * Clear quiescent state in each stream for all applications for a |
| 7664 | * specific UST session. |
| 7665 | * Called with UST session lock held. |
| 7666 | */ |
| 7667 | static |
| 7668 | int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) |
| 7669 | |
| 7670 | { |
| 7671 | int ret = 0; |
| 7672 | |
| 7673 | DBG("Clearing stream quiescent state for all ust apps"); |
| 7674 | |
| 7675 | rcu_read_lock(); |
| 7676 | |
| 7677 | switch (usess->buffer_type) { |
| 7678 | case LTTNG_BUFFER_PER_UID: |
| 7679 | { |
| 7680 | struct lttng_ht_iter iter; |
| 7681 | struct buffer_reg_uid *reg; |
| 7682 | |
| 7683 | /* |
| 7684 | * Clear quiescent for all per UID buffers associated to |
| 7685 | * that session. |
| 7686 | */ |
| 7687 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 7688 | struct consumer_socket *socket; |
| 7689 | struct buffer_reg_channel *buf_reg_chan; |
| 7690 | |
| 7691 | /* Get associated consumer socket.*/ |
| 7692 | socket = consumer_find_socket_by_bitness( |
| 7693 | reg->bits_per_long, usess->consumer); |
| 7694 | if (!socket) { |
| 7695 | /* |
| 7696 | * Ignore request if no consumer is found for |
| 7697 | * the session. |
| 7698 | */ |
| 7699 | continue; |
| 7700 | } |
| 7701 | |
| 7702 | cds_lfht_for_each_entry(reg->registry->channels->ht, |
| 7703 | &iter.iter, buf_reg_chan, node.node) { |
| 7704 | /* |
| 7705 | * The following call will print error values so |
| 7706 | * the return code is of little importance |
| 7707 | * because whatever happens, we have to try them |
| 7708 | * all. |
| 7709 | */ |
| 7710 | (void) consumer_clear_quiescent_channel(socket, |
| 7711 | buf_reg_chan->consumer_key); |
| 7712 | } |
| 7713 | } |
| 7714 | break; |
| 7715 | } |
| 7716 | case LTTNG_BUFFER_PER_PID: |
| 7717 | { |
| 7718 | struct ust_app_session *ua_sess; |
| 7719 | struct lttng_ht_iter iter; |
| 7720 | struct ust_app *app; |
| 7721 | |
| 7722 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
| 7723 | pid_n.node) { |
| 7724 | ua_sess = lookup_session_by_app(usess, app); |
| 7725 | if (ua_sess == NULL) { |
| 7726 | continue; |
| 7727 | } |
| 7728 | (void) ust_app_clear_quiescent_app_session(app, |
| 7729 | ua_sess); |
| 7730 | } |
| 7731 | break; |
| 7732 | } |
| 7733 | default: |
| 7734 | ret = -1; |
| 7735 | assert(0); |
| 7736 | break; |
| 7737 | } |
| 7738 | |
| 7739 | rcu_read_unlock(); |
| 7740 | health_code_update(); |
| 7741 | return ret; |
| 7742 | } |
| 7743 | |
| 7744 | /* |
| 7745 | * Destroy a specific UST session in apps. |
| 7746 | */ |
| 7747 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 7748 | { |
| 7749 | int ret; |
| 7750 | struct ust_app_session *ua_sess; |
| 7751 | struct lttng_ht_iter iter; |
| 7752 | struct lttng_ht_node_u64 *node; |
| 7753 | |
| 7754 | DBG("Destroy tracing for ust app pid %d", app->pid); |
| 7755 | |
| 7756 | rcu_read_lock(); |
| 7757 | |
| 7758 | if (!app->compatible) { |
| 7759 | goto end; |
| 7760 | } |
| 7761 | |
| 7762 | __lookup_session_by_app(usess, app, &iter); |
| 7763 | node = lttng_ht_iter_get_node_u64(&iter); |
| 7764 | if (node == NULL) { |
| 7765 | /* Session is being or is deleted. */ |
| 7766 | goto end; |
| 7767 | } |
| 7768 | ua_sess = caa_container_of(node, struct ust_app_session, node); |
| 7769 | |
| 7770 | health_code_update(); |
| 7771 | destroy_app_session(app, ua_sess); |
| 7772 | |
| 7773 | health_code_update(); |
| 7774 | |
| 7775 | /* Quiescent wait after stopping trace */ |
| 7776 | pthread_mutex_lock(&app->sock_lock); |
| 7777 | ret = ustctl_wait_quiescent(app->sock); |
| 7778 | pthread_mutex_unlock(&app->sock_lock); |
| 7779 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 7780 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 7781 | app->pid, ret); |
| 7782 | } |
| 7783 | end: |
| 7784 | rcu_read_unlock(); |
| 7785 | health_code_update(); |
| 7786 | return 0; |
| 7787 | } |
| 7788 | |
| 7789 | /* |
| 7790 | * Start tracing for the UST session. |
| 7791 | */ |
| 7792 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
| 7793 | { |
| 7794 | struct lttng_ht_iter iter; |
| 7795 | struct ust_app *app; |
| 7796 | |
| 7797 | DBG("Starting all UST traces"); |
| 7798 | |
| 7799 | /* |
| 7800 | * Even though the start trace might fail, flag this session active so |
| 7801 | * other application coming in are started by default. |
| 7802 | */ |
| 7803 | usess->active = 1; |
| 7804 | |
| 7805 | rcu_read_lock(); |
| 7806 | |
| 7807 | /* |
| 7808 | * In a start-stop-start use-case, we need to clear the quiescent state |
| 7809 | * of each channel set by the prior stop command, thus ensuring that a |
| 7810 | * following stop or destroy is sure to grab a timestamp_end near those |
| 7811 | * operations, even if the packet is empty. |
| 7812 | */ |
| 7813 | (void) ust_app_clear_quiescent_session(usess); |
| 7814 | |
| 7815 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 7816 | ust_app_global_update(usess, app); |
| 7817 | } |
| 7818 | |
| 7819 | rcu_read_unlock(); |
| 7820 | |
| 7821 | return 0; |
| 7822 | } |
| 7823 | |
| 7824 | /* |
| 7825 | * Start tracing for the UST session. |
| 7826 | * Called with UST session lock held. |
| 7827 | */ |
| 7828 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) |
| 7829 | { |
| 7830 | int ret = 0; |
| 7831 | struct lttng_ht_iter iter; |
| 7832 | struct ust_app *app; |
| 7833 | |
| 7834 | DBG("Stopping all UST traces"); |
| 7835 | |
| 7836 | /* |
| 7837 | * Even though the stop trace might fail, flag this session inactive so |
| 7838 | * other application coming in are not started by default. |
| 7839 | */ |
| 7840 | usess->active = 0; |
| 7841 | |
| 7842 | rcu_read_lock(); |
| 7843 | |
| 7844 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 7845 | ret = ust_app_stop_trace(usess, app); |
| 7846 | if (ret < 0) { |
| 7847 | /* Continue to next apps even on error */ |
| 7848 | continue; |
| 7849 | } |
| 7850 | } |
| 7851 | |
| 7852 | (void) ust_app_flush_session(usess); |
| 7853 | |
| 7854 | rcu_read_unlock(); |
| 7855 | |
| 7856 | return 0; |
| 7857 | } |
| 7858 | |
| 7859 | /* |
| 7860 | * Destroy app UST session. |
| 7861 | */ |
| 7862 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) |
| 7863 | { |
| 7864 | int ret = 0; |
| 7865 | struct lttng_ht_iter iter; |
| 7866 | struct ust_app *app; |
| 7867 | |
| 7868 | DBG("Destroy all UST traces"); |
| 7869 | |
| 7870 | rcu_read_lock(); |
| 7871 | |
| 7872 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 7873 | ret = destroy_trace(usess, app); |
| 7874 | if (ret < 0) { |
| 7875 | /* Continue to next apps even on error */ |
| 7876 | continue; |
| 7877 | } |
| 7878 | } |
| 7879 | |
| 7880 | rcu_read_unlock(); |
| 7881 | |
| 7882 | return 0; |
| 7883 | } |
| 7884 | |
| 7885 | /* The ua_sess lock must be held by the caller. */ |
| 7886 | static |
| 7887 | int find_or_create_ust_app_channel( |
| 7888 | struct ltt_ust_session *usess, |
| 7889 | struct ust_app_session *ua_sess, |
| 7890 | struct ust_app *app, |
| 7891 | struct ltt_ust_channel *uchan, |
| 7892 | struct ust_app_channel **ua_chan) |
| 7893 | { |
| 7894 | int ret = 0; |
| 7895 | struct lttng_ht_iter iter; |
| 7896 | struct lttng_ht_node_str *ua_chan_node; |
| 7897 | |
| 7898 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); |
| 7899 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 7900 | if (ua_chan_node) { |
| 7901 | *ua_chan = caa_container_of(ua_chan_node, |
| 7902 | struct ust_app_channel, node); |
| 7903 | goto end; |
| 7904 | } |
| 7905 | |
| 7906 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); |
| 7907 | if (ret) { |
| 7908 | goto end; |
| 7909 | } |
| 7910 | end: |
| 7911 | return ret; |
| 7912 | } |
| 7913 | |
| 7914 | /* The ua_sess lock must be held by the caller. */ |
| 7915 | static |
| 7916 | int find_or_create_ust_app_map( |
| 7917 | struct ltt_ust_session *usess, |
| 7918 | struct ust_app_session *ua_sess, |
| 7919 | struct ust_app *app, |
| 7920 | struct ltt_ust_map *umap, |
| 7921 | struct ust_app_map **ua_map) |
| 7922 | { |
| 7923 | int ret = 0; |
| 7924 | struct lttng_ht_iter iter; |
| 7925 | struct lttng_ht_node_str *ua_map_node; |
| 7926 | |
| 7927 | lttng_ht_lookup(ua_sess->maps, (void *) umap->name, &iter); |
| 7928 | ua_map_node = lttng_ht_iter_get_node_str(&iter); |
| 7929 | if (ua_map_node) { |
| 7930 | *ua_map = caa_container_of(ua_map_node, |
| 7931 | struct ust_app_map, node); |
| 7932 | goto end; |
| 7933 | } |
| 7934 | |
| 7935 | DBG("UST map id = %"PRIu64" not found. Creating it.", umap->id); |
| 7936 | ret = ust_app_map_create(usess, ua_sess, umap, app, ua_map); |
| 7937 | if (ret) { |
| 7938 | goto end; |
| 7939 | } |
| 7940 | end: |
| 7941 | return ret; |
| 7942 | } |
| 7943 | |
| 7944 | static |
| 7945 | int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, |
| 7946 | struct ltt_ust_event *uevent, struct ust_app_session *ua_sess, |
| 7947 | struct ust_app *app) |
| 7948 | { |
| 7949 | int ret = 0; |
| 7950 | struct ust_app_event *ua_event = NULL; |
| 7951 | |
| 7952 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 7953 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, |
| 7954 | uevent->attr.token); |
| 7955 | if (!ua_event) { |
| 7956 | ret = create_ust_app_channel_event(ua_sess, ua_chan, uevent, app); |
| 7957 | |
| 7958 | if (ret < 0) { |
| 7959 | goto end; |
| 7960 | } |
| 7961 | } else { |
| 7962 | if (ua_event->enabled != uevent->enabled) { |
| 7963 | ret = uevent->enabled ? |
| 7964 | enable_ust_app_event(ua_sess, ua_event, app) : |
| 7965 | disable_ust_app_event(ua_sess, ua_event, app); |
| 7966 | } |
| 7967 | } |
| 7968 | |
| 7969 | end: |
| 7970 | return ret; |
| 7971 | } |
| 7972 | |
| 7973 | /* Called with RCU read-side lock held. */ |
| 7974 | static |
| 7975 | int ust_app_map_synchronize_event(struct ust_app_map *ua_map, |
| 7976 | struct ltt_ust_event *uevent, struct ust_app_session *ua_sess, |
| 7977 | struct ust_app *app) |
| 7978 | { |
| 7979 | int ret = 0; |
| 7980 | struct ust_app_event *ua_event = NULL; |
| 7981 | |
| 7982 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, |
| 7983 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, |
| 7984 | uevent->attr.token); |
| 7985 | if (!ua_event) { |
| 7986 | ret = create_ust_app_map_event(ua_sess, ua_map, uevent, app); |
| 7987 | if (ret < 0) { |
| 7988 | goto end; |
| 7989 | } |
| 7990 | } else { |
| 7991 | if (ua_event->enabled != uevent->enabled) { |
| 7992 | ret = uevent->enabled ? |
| 7993 | enable_ust_app_event(ua_sess, ua_event, app) : |
| 7994 | disable_ust_app_event(ua_sess, ua_event, app); |
| 7995 | } |
| 7996 | } |
| 7997 | |
| 7998 | end: |
| 7999 | return ret; |
| 8000 | } |
| 8001 | |
| 8002 | static |
| 8003 | void ust_app_synchronize_event_notifier_rules(struct ust_app *app) |
| 8004 | { |
| 8005 | int ret = 0; |
| 8006 | enum lttng_error_code ret_code; |
| 8007 | enum lttng_trigger_status t_status; |
| 8008 | struct lttng_ht_iter app_trigger_iter; |
| 8009 | struct lttng_triggers *triggers = NULL; |
| 8010 | struct ust_app_event_notifier_rule *event_notifier_rule; |
| 8011 | unsigned int count, i; |
| 8012 | |
| 8013 | /* |
| 8014 | * Currrently, registering or unregistering a trigger with an |
| 8015 | * event rule condition causes a full synchronization of the event |
| 8016 | * notifiers. |
| 8017 | * |
| 8018 | * The first step attempts to add an event notifier for all registered |
| 8019 | * triggers that apply to the user space tracers. Then, the |
| 8020 | * application's event notifiers rules are all checked against the list |
| 8021 | * of registered triggers. Any event notifier that doesn't have a |
| 8022 | * matching trigger can be assumed to have been disabled. |
| 8023 | * |
| 8024 | * All of this is inefficient, but is put in place to get the feature |
| 8025 | * rolling as it is simpler at this moment. It will be optimized Soon™ |
| 8026 | * to allow the state of enabled |
| 8027 | * event notifiers to be synchronized in a piece-wise way. |
| 8028 | */ |
| 8029 | |
| 8030 | /* Get all triggers using uid 0 (root) */ |
| 8031 | ret_code = notification_thread_command_list_triggers( |
| 8032 | notification_thread_handle, 0, &triggers); |
| 8033 | if (ret_code != LTTNG_OK) { |
| 8034 | ret = -1; |
| 8035 | goto end; |
| 8036 | } |
| 8037 | |
| 8038 | assert(triggers); |
| 8039 | |
| 8040 | t_status = lttng_triggers_get_count(triggers, &count); |
| 8041 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { |
| 8042 | ret = -1; |
| 8043 | goto end; |
| 8044 | } |
| 8045 | |
| 8046 | for (i = 0; i < count; i++) { |
| 8047 | const struct lttng_condition *condition; |
| 8048 | const struct lttng_event_rule *event_rule; |
| 8049 | struct lttng_trigger *trigger; |
| 8050 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; |
| 8051 | enum lttng_condition_status condition_status; |
| 8052 | uint64_t token; |
| 8053 | |
| 8054 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); |
| 8055 | assert(trigger); |
| 8056 | |
| 8057 | token = lttng_trigger_get_tracer_token(trigger); |
| 8058 | condition = lttng_trigger_get_const_condition(trigger); |
| 8059 | |
| 8060 | if (!lttng_trigger_needs_tracer_notifier(trigger)) { |
| 8061 | continue; |
| 8062 | } |
| 8063 | |
| 8064 | condition_status = lttng_condition_on_event_get_rule(condition, &event_rule); |
| 8065 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 8066 | |
| 8067 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { |
| 8068 | /* Skip kernel related triggers. */ |
| 8069 | continue; |
| 8070 | } |
| 8071 | |
| 8072 | /* |
| 8073 | * Find or create the associated token event rule. The caller |
| 8074 | * holds the RCU read lock, so this is safe to call without |
| 8075 | * explicitly acquiring it here. |
| 8076 | */ |
| 8077 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( |
| 8078 | app->token_to_event_notifier_rule_ht, token); |
| 8079 | if (!looked_up_event_notifier_rule) { |
| 8080 | ret = create_ust_app_event_notifier_rule(trigger, app); |
| 8081 | if (ret < 0) { |
| 8082 | goto end; |
| 8083 | } |
| 8084 | } |
| 8085 | } |
| 8086 | |
| 8087 | rcu_read_lock(); |
| 8088 | /* Remove all unknown event sources from the app. */ |
| 8089 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, |
| 8090 | &app_trigger_iter.iter, event_notifier_rule, |
| 8091 | node.node) { |
| 8092 | const uint64_t app_token = event_notifier_rule->token; |
| 8093 | bool found = false; |
| 8094 | |
| 8095 | /* |
| 8096 | * Check if the app event trigger still exists on the |
| 8097 | * notification side. |
| 8098 | */ |
| 8099 | for (i = 0; i < count; i++) { |
| 8100 | uint64_t notification_thread_token; |
| 8101 | const struct lttng_trigger *trigger = |
| 8102 | lttng_triggers_get_at_index( |
| 8103 | triggers, i); |
| 8104 | |
| 8105 | assert(trigger); |
| 8106 | |
| 8107 | notification_thread_token = |
| 8108 | lttng_trigger_get_tracer_token(trigger); |
| 8109 | |
| 8110 | if (notification_thread_token == app_token) { |
| 8111 | found = true; |
| 8112 | break; |
| 8113 | } |
| 8114 | } |
| 8115 | |
| 8116 | if (found) { |
| 8117 | /* Still valid. */ |
| 8118 | continue; |
| 8119 | } |
| 8120 | |
| 8121 | /* |
| 8122 | * This trigger was unregistered, disable it on the tracer's |
| 8123 | * side. |
| 8124 | */ |
| 8125 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, |
| 8126 | &app_trigger_iter); |
| 8127 | assert(ret == 0); |
| 8128 | |
| 8129 | /* Callee logs errors. */ |
| 8130 | (void) disable_ust_object(app, event_notifier_rule->obj); |
| 8131 | |
| 8132 | delete_ust_app_event_notifier_rule( |
| 8133 | app->sock, event_notifier_rule, app); |
| 8134 | } |
| 8135 | |
| 8136 | rcu_read_unlock(); |
| 8137 | |
| 8138 | end: |
| 8139 | lttng_triggers_destroy(triggers); |
| 8140 | return; |
| 8141 | } |
| 8142 | |
| 8143 | /* |
| 8144 | * Called with RCU read-side lock held. |
| 8145 | */ |
| 8146 | static |
| 8147 | void ust_app_synchronize_all_channels(struct ltt_ust_session *usess, |
| 8148 | struct ust_app_session *ua_sess, |
| 8149 | struct ust_app *app) |
| 8150 | { |
| 8151 | int ret = 0; |
| 8152 | struct cds_lfht_iter uchan_iter; |
| 8153 | struct ltt_ust_channel *uchan; |
| 8154 | |
| 8155 | assert(usess); |
| 8156 | assert(ua_sess); |
| 8157 | assert(app); |
| 8158 | |
| 8159 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, |
| 8160 | uchan, node.node) { |
| 8161 | struct ust_app_channel *ua_chan; |
| 8162 | struct cds_lfht_iter uevent_iter; |
| 8163 | struct ltt_ust_event *uevent; |
| 8164 | |
| 8165 | /* |
| 8166 | * Search for a matching ust_app_channel. If none is found, |
| 8167 | * create it. Creating the channel will cause the ua_chan |
| 8168 | * structure to be allocated, the channel buffers to be |
| 8169 | * allocated (if necessary) and sent to the application, and |
| 8170 | * all enabled contexts will be added to the channel. |
| 8171 | */ |
| 8172 | ret = find_or_create_ust_app_channel(usess, ua_sess, |
| 8173 | app, uchan, &ua_chan); |
| 8174 | if (ret) { |
| 8175 | /* Tracer is probably gone or ENOMEM. */ |
| 8176 | goto end; |
| 8177 | } |
| 8178 | |
| 8179 | if (!ua_chan) { |
| 8180 | /* ua_chan will be NULL for the metadata channel */ |
| 8181 | continue; |
| 8182 | } |
| 8183 | |
| 8184 | cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent, |
| 8185 | node.node) { |
| 8186 | ret = ust_app_channel_synchronize_event(ua_chan, |
| 8187 | uevent, ua_sess, app); |
| 8188 | if (ret) { |
| 8189 | goto end; |
| 8190 | } |
| 8191 | } |
| 8192 | |
| 8193 | if (ua_chan->enabled != uchan->enabled) { |
| 8194 | ret = uchan->enabled ? |
| 8195 | enable_ust_app_channel(ua_sess, uchan, app) : |
| 8196 | disable_ust_app_channel(ua_sess, ua_chan, app); |
| 8197 | if (ret) { |
| 8198 | goto end; |
| 8199 | } |
| 8200 | } |
| 8201 | } |
| 8202 | end: |
| 8203 | return; |
| 8204 | } |
| 8205 | |
| 8206 | /* |
| 8207 | * Called with RCU read-side lock held. |
| 8208 | */ |
| 8209 | static |
| 8210 | void ust_app_synchronize_all_maps(struct ltt_ust_session *usess, |
| 8211 | struct ust_app_session *ua_sess, |
| 8212 | struct ust_app *app) |
| 8213 | { |
| 8214 | int ret = 0; |
| 8215 | struct cds_lfht_iter umap_iter; |
| 8216 | struct ltt_ust_map *umap; |
| 8217 | |
| 8218 | assert(usess); |
| 8219 | assert(ua_sess); |
| 8220 | assert(app); |
| 8221 | |
| 8222 | cds_lfht_for_each_entry(usess->domain_global.maps->ht, &umap_iter, |
| 8223 | umap, node.node) { |
| 8224 | struct ust_app_map *ua_map; |
| 8225 | struct cds_lfht_iter uevent_iter; |
| 8226 | struct ltt_ust_event *uevent; |
| 8227 | |
| 8228 | DBG("Synchronizing UST map id = %"PRIu64, umap->id); |
| 8229 | |
| 8230 | ret = find_or_create_ust_app_map(usess, ua_sess, |
| 8231 | app, umap, &ua_map); |
| 8232 | if (ret) { |
| 8233 | /* Tracer is probably gone or ENOMEM. */ |
| 8234 | goto end; |
| 8235 | } |
| 8236 | |
| 8237 | DBG("Synchronizing all events of UST map id = %"PRIu64, umap->id); |
| 8238 | cds_lfht_for_each_entry(umap->events->ht, &uevent_iter, uevent, |
| 8239 | node.node) { |
| 8240 | ret = ust_app_map_synchronize_event(ua_map, |
| 8241 | uevent, ua_sess, app); |
| 8242 | if (ret) { |
| 8243 | goto end; |
| 8244 | } |
| 8245 | } |
| 8246 | |
| 8247 | if (ua_map->enabled != umap->enabled) { |
| 8248 | if (umap->enabled) { |
| 8249 | DBG("Map disabled on the tracer side but shouldn't"); |
| 8250 | ret = enable_ust_app_map(ua_sess, umap, app); |
| 8251 | } else { |
| 8252 | DBG("Map enabled on the tracer side but shouldn't"); |
| 8253 | ret = disable_ust_app_map(ua_sess, ua_map, app); |
| 8254 | } |
| 8255 | if (ret) { |
| 8256 | goto end; |
| 8257 | } |
| 8258 | } |
| 8259 | } |
| 8260 | end: |
| 8261 | return; |
| 8262 | } |
| 8263 | |
| 8264 | /* |
| 8265 | * The caller must ensure that the application is compatible and is tracked |
| 8266 | * by the process attribute trackers. |
| 8267 | */ |
| 8268 | static |
| 8269 | void ust_app_synchronize(struct ltt_ust_session *usess, |
| 8270 | struct ust_app *app) |
| 8271 | { |
| 8272 | int ret = 0; |
| 8273 | struct ust_app_session *ua_sess = NULL; |
| 8274 | |
| 8275 | /* |
| 8276 | * The application's configuration should only be synchronized for |
| 8277 | * active sessions. |
| 8278 | */ |
| 8279 | assert(usess->active); |
| 8280 | |
| 8281 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL); |
| 8282 | if (ret < 0) { |
| 8283 | /* Tracer is probably gone or ENOMEM. */ |
| 8284 | goto error; |
| 8285 | } |
| 8286 | assert(ua_sess); |
| 8287 | |
| 8288 | |
| 8289 | rcu_read_lock(); |
| 8290 | |
| 8291 | pthread_mutex_lock(&ua_sess->lock); |
| 8292 | if (ua_sess->deleted) { |
| 8293 | pthread_mutex_unlock(&ua_sess->lock); |
| 8294 | goto end; |
| 8295 | } |
| 8296 | ust_app_synchronize_all_channels(usess, ua_sess, app); |
| 8297 | ust_app_synchronize_all_maps(usess, ua_sess, app); |
| 8298 | |
| 8299 | /* |
| 8300 | * Create the metadata for the application. This returns gracefully if a |
| 8301 | * metadata was already set for the session. |
| 8302 | * |
| 8303 | * The metadata channel must be created after the data channels as the |
| 8304 | * consumer daemon assumes this ordering. When interacting with a relay |
| 8305 | * daemon, the consumer will use this assumption to send the |
| 8306 | * "STREAMS_SENT" message to the relay daemon. |
| 8307 | */ |
| 8308 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); |
| 8309 | if (ret < 0) { |
| 8310 | goto error_unlock; |
| 8311 | } |
| 8312 | |
| 8313 | rcu_read_unlock(); |
| 8314 | |
| 8315 | end: |
| 8316 | pthread_mutex_unlock(&ua_sess->lock); |
| 8317 | /* Everything went well at this point. */ |
| 8318 | return; |
| 8319 | |
| 8320 | error_unlock: |
| 8321 | rcu_read_unlock(); |
| 8322 | pthread_mutex_unlock(&ua_sess->lock); |
| 8323 | error: |
| 8324 | if (ua_sess) { |
| 8325 | destroy_app_session(app, ua_sess); |
| 8326 | } |
| 8327 | return; |
| 8328 | } |
| 8329 | |
| 8330 | static |
| 8331 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) |
| 8332 | { |
| 8333 | struct ust_app_session *ua_sess; |
| 8334 | |
| 8335 | ua_sess = lookup_session_by_app(usess, app); |
| 8336 | if (ua_sess == NULL) { |
| 8337 | return; |
| 8338 | } |
| 8339 | destroy_app_session(app, ua_sess); |
| 8340 | } |
| 8341 | |
| 8342 | /* |
| 8343 | * Add channels/events from UST global domain to registered apps at sock. |
| 8344 | * |
| 8345 | * Called with session lock held. |
| 8346 | * Called with RCU read-side lock held. |
| 8347 | */ |
| 8348 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) |
| 8349 | { |
| 8350 | assert(usess); |
| 8351 | assert(usess->active); |
| 8352 | |
| 8353 | DBG2("UST app global update for app sock %d for session id %" PRIu64, |
| 8354 | app->sock, usess->id); |
| 8355 | |
| 8356 | if (!app->compatible) { |
| 8357 | return; |
| 8358 | } |
| 8359 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, |
| 8360 | usess, app->pid) && |
| 8361 | trace_ust_id_tracker_lookup( |
| 8362 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, |
| 8363 | usess, app->uid) && |
| 8364 | trace_ust_id_tracker_lookup( |
| 8365 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, |
| 8366 | usess, app->gid)) { |
| 8367 | /* |
| 8368 | * Synchronize the application's internal tracing configuration |
| 8369 | * and start tracing. |
| 8370 | */ |
| 8371 | ust_app_synchronize(usess, app); |
| 8372 | ust_app_start_trace(usess, app); |
| 8373 | } else { |
| 8374 | ust_app_global_destroy(usess, app); |
| 8375 | } |
| 8376 | } |
| 8377 | |
| 8378 | /* |
| 8379 | * Add all event notifiers to an application. |
| 8380 | * |
| 8381 | * Called with session lock held. |
| 8382 | * Called with RCU read-side lock held. |
| 8383 | */ |
| 8384 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) |
| 8385 | { |
| 8386 | DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)", |
| 8387 | app->name, app->ppid); |
| 8388 | |
| 8389 | if (!app->compatible) { |
| 8390 | return; |
| 8391 | } |
| 8392 | |
| 8393 | if (app->event_notifier_group.object == NULL) { |
| 8394 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)", |
| 8395 | app->name, app->ppid); |
| 8396 | return; |
| 8397 | } |
| 8398 | |
| 8399 | ust_app_synchronize_event_notifier_rules(app); |
| 8400 | } |
| 8401 | |
| 8402 | /* |
| 8403 | * Called with session lock held. |
| 8404 | */ |
| 8405 | void ust_app_global_update_all(struct ltt_ust_session *usess) |
| 8406 | { |
| 8407 | struct lttng_ht_iter iter; |
| 8408 | struct ust_app *app; |
| 8409 | |
| 8410 | rcu_read_lock(); |
| 8411 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 8412 | ust_app_global_update(usess, app); |
| 8413 | } |
| 8414 | rcu_read_unlock(); |
| 8415 | } |
| 8416 | |
| 8417 | void ust_app_global_update_all_event_notifier_rules(void) |
| 8418 | { |
| 8419 | struct lttng_ht_iter iter; |
| 8420 | struct ust_app *app; |
| 8421 | |
| 8422 | rcu_read_lock(); |
| 8423 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 8424 | ust_app_global_update_event_notifier_rules(app); |
| 8425 | } |
| 8426 | |
| 8427 | rcu_read_unlock(); |
| 8428 | } |
| 8429 | |
| 8430 | void ust_app_update_event_notifier_error_count(struct lttng_trigger *trigger) |
| 8431 | { |
| 8432 | uint64_t error_count = 0; |
| 8433 | enum event_notifier_error_accounting_status status; |
| 8434 | struct lttng_condition *condition = lttng_trigger_get_condition(trigger); |
| 8435 | |
| 8436 | status = event_notifier_error_accounting_get_count(trigger, &error_count); |
| 8437 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 8438 | ERR("Error getting trigger error count."); |
| 8439 | } |
| 8440 | |
| 8441 | lttng_condition_on_event_set_error_count(condition, error_count); |
| 8442 | } |
| 8443 | |
| 8444 | /* |
| 8445 | * Add context to a specific channel for global UST domain. |
| 8446 | */ |
| 8447 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, |
| 8448 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) |
| 8449 | { |
| 8450 | int ret = 0; |
| 8451 | struct lttng_ht_node_str *ua_chan_node; |
| 8452 | struct lttng_ht_iter iter, uiter; |
| 8453 | struct ust_app_channel *ua_chan = NULL; |
| 8454 | struct ust_app_session *ua_sess; |
| 8455 | struct ust_app *app; |
| 8456 | |
| 8457 | assert(usess->active); |
| 8458 | |
| 8459 | rcu_read_lock(); |
| 8460 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 8461 | if (!app->compatible) { |
| 8462 | /* |
| 8463 | * TODO: In time, we should notice the caller of this error by |
| 8464 | * telling him that this is a version error. |
| 8465 | */ |
| 8466 | continue; |
| 8467 | } |
| 8468 | ua_sess = lookup_session_by_app(usess, app); |
| 8469 | if (ua_sess == NULL) { |
| 8470 | continue; |
| 8471 | } |
| 8472 | |
| 8473 | pthread_mutex_lock(&ua_sess->lock); |
| 8474 | |
| 8475 | if (ua_sess->deleted) { |
| 8476 | pthread_mutex_unlock(&ua_sess->lock); |
| 8477 | continue; |
| 8478 | } |
| 8479 | |
| 8480 | /* Lookup channel in the ust app session */ |
| 8481 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 8482 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 8483 | if (ua_chan_node == NULL) { |
| 8484 | goto next_app; |
| 8485 | } |
| 8486 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, |
| 8487 | node); |
| 8488 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
| 8489 | if (ret < 0) { |
| 8490 | goto next_app; |
| 8491 | } |
| 8492 | next_app: |
| 8493 | pthread_mutex_unlock(&ua_sess->lock); |
| 8494 | } |
| 8495 | |
| 8496 | rcu_read_unlock(); |
| 8497 | return ret; |
| 8498 | } |
| 8499 | |
| 8500 | /* |
| 8501 | * Receive registration and populate the given msg structure. |
| 8502 | * |
| 8503 | * On success return 0 else a negative value returned by the ustctl call. |
| 8504 | */ |
| 8505 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) |
| 8506 | { |
| 8507 | int ret; |
| 8508 | uint32_t pid, ppid, uid, gid; |
| 8509 | |
| 8510 | assert(msg); |
| 8511 | |
| 8512 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, |
| 8513 | &pid, &ppid, &uid, &gid, |
| 8514 | &msg->bits_per_long, |
| 8515 | &msg->uint8_t_alignment, |
| 8516 | &msg->uint16_t_alignment, |
| 8517 | &msg->uint32_t_alignment, |
| 8518 | &msg->uint64_t_alignment, |
| 8519 | &msg->long_alignment, |
| 8520 | &msg->byte_order, |
| 8521 | msg->name); |
| 8522 | if (ret < 0) { |
| 8523 | switch (-ret) { |
| 8524 | case EPIPE: |
| 8525 | case ECONNRESET: |
| 8526 | case LTTNG_UST_ERR_EXITING: |
| 8527 | DBG3("UST app recv reg message failed. Application died"); |
| 8528 | break; |
| 8529 | case LTTNG_UST_ERR_UNSUP_MAJOR: |
| 8530 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", |
| 8531 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, |
| 8532 | LTTNG_UST_ABI_MINOR_VERSION); |
| 8533 | break; |
| 8534 | default: |
| 8535 | ERR("UST app recv reg message failed with ret %d", ret); |
| 8536 | break; |
| 8537 | } |
| 8538 | goto error; |
| 8539 | } |
| 8540 | msg->pid = (pid_t) pid; |
| 8541 | msg->ppid = (pid_t) ppid; |
| 8542 | msg->uid = (uid_t) uid; |
| 8543 | msg->gid = (gid_t) gid; |
| 8544 | |
| 8545 | error: |
| 8546 | return ret; |
| 8547 | } |
| 8548 | |
| 8549 | /* |
| 8550 | * Return a ust app session object using the application object and the |
| 8551 | * session object descriptor has a key. If not found, NULL is returned. |
| 8552 | * A RCU read side lock MUST be acquired when calling this function. |
| 8553 | */ |
| 8554 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, |
| 8555 | int objd) |
| 8556 | { |
| 8557 | struct lttng_ht_node_ulong *node; |
| 8558 | struct lttng_ht_iter iter; |
| 8559 | struct ust_app_session *ua_sess = NULL; |
| 8560 | |
| 8561 | assert(app); |
| 8562 | |
| 8563 | lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter); |
| 8564 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 8565 | if (node == NULL) { |
| 8566 | DBG2("UST app session find by objd %d not found", objd); |
| 8567 | goto error; |
| 8568 | } |
| 8569 | |
| 8570 | ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node); |
| 8571 | |
| 8572 | error: |
| 8573 | return ua_sess; |
| 8574 | } |
| 8575 | |
| 8576 | /* |
| 8577 | * Return a ust app channel object using the application object and the channel |
| 8578 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
| 8579 | * lock MUST be acquired before calling this function. |
| 8580 | */ |
| 8581 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
| 8582 | int objd) |
| 8583 | { |
| 8584 | struct lttng_ht_node_ulong *node; |
| 8585 | struct lttng_ht_iter iter; |
| 8586 | struct ust_app_channel *ua_chan = NULL; |
| 8587 | |
| 8588 | assert(app); |
| 8589 | |
| 8590 | lttng_ht_lookup(app->ust_chan_objd, (void *)((unsigned long) objd), &iter); |
| 8591 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 8592 | if (node == NULL) { |
| 8593 | DBG2("UST app channel find by objd %d not found", objd); |
| 8594 | goto error; |
| 8595 | } |
| 8596 | |
| 8597 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); |
| 8598 | |
| 8599 | error: |
| 8600 | return ua_chan; |
| 8601 | } |
| 8602 | |
| 8603 | /* |
| 8604 | * Return a ust app map object using the application object and the map |
| 8605 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
| 8606 | * lock MUST be acquired before calling this function. |
| 8607 | */ |
| 8608 | static struct ust_app_map *find_map_by_objd(struct ust_app *app, |
| 8609 | int objd) |
| 8610 | { |
| 8611 | struct lttng_ht_node_ulong *node; |
| 8612 | struct lttng_ht_iter iter; |
| 8613 | struct ust_app_map *ua_map = NULL; |
| 8614 | |
| 8615 | assert(app); |
| 8616 | |
| 8617 | lttng_ht_lookup(app->ust_map_objd, (void *)((unsigned long) objd), &iter); |
| 8618 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 8619 | if (node == NULL) { |
| 8620 | DBG2("UST app map find by objd %d not found", objd); |
| 8621 | goto error; |
| 8622 | } |
| 8623 | |
| 8624 | ua_map = caa_container_of(node, struct ust_app_map, ust_objd_node); |
| 8625 | |
| 8626 | error: |
| 8627 | return ua_map; |
| 8628 | } |
| 8629 | |
| 8630 | /* |
| 8631 | * Reply to a register channel notification from an application on the notify |
| 8632 | * socket. The channel metadata is also created. |
| 8633 | * |
| 8634 | * The session UST registry lock is acquired in this function. |
| 8635 | * |
| 8636 | * On success 0 is returned else a negative value. |
| 8637 | */ |
| 8638 | static int reply_ust_register_channel(int sock, int cobjd, |
| 8639 | size_t nr_fields, struct ustctl_field *fields) |
| 8640 | { |
| 8641 | int ret, ret_code = 0; |
| 8642 | uint32_t chan_id; |
| 8643 | uint64_t chan_reg_key; |
| 8644 | enum ustctl_channel_header type; |
| 8645 | struct ust_app *app; |
| 8646 | struct ust_app_channel *ua_chan; |
| 8647 | struct ust_app_session *ua_sess; |
| 8648 | struct ust_registry_session *registry; |
| 8649 | struct ust_registry_channel *ust_reg_chan; |
| 8650 | |
| 8651 | rcu_read_lock(); |
| 8652 | |
| 8653 | /* Lookup application. If not found, there is a code flow error. */ |
| 8654 | app = find_app_by_notify_sock(sock); |
| 8655 | if (!app) { |
| 8656 | DBG("Application socket %d is being torn down. Abort event notify", |
| 8657 | sock); |
| 8658 | ret = 0; |
| 8659 | goto error_rcu_unlock; |
| 8660 | } |
| 8661 | |
| 8662 | /* Lookup channel by UST object descriptor. */ |
| 8663 | ua_chan = find_channel_by_objd(app, cobjd); |
| 8664 | if (!ua_chan) { |
| 8665 | DBG("Application channel is being torn down. Abort event notify"); |
| 8666 | ret = 0; |
| 8667 | goto error_rcu_unlock; |
| 8668 | } |
| 8669 | |
| 8670 | assert(ua_chan->session); |
| 8671 | ua_sess = ua_chan->session; |
| 8672 | |
| 8673 | /* Get right session registry depending on the session buffer type. */ |
| 8674 | registry = get_session_registry(ua_sess); |
| 8675 | if (!registry) { |
| 8676 | DBG("Application session is being torn down. Abort event notify"); |
| 8677 | ret = 0; |
| 8678 | goto error_rcu_unlock; |
| 8679 | }; |
| 8680 | |
| 8681 | /* Depending on the buffer type, a different channel key is used. */ |
| 8682 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 8683 | chan_reg_key = ua_chan->tracing_channel_id; |
| 8684 | } else { |
| 8685 | chan_reg_key = ua_chan->key; |
| 8686 | } |
| 8687 | |
| 8688 | pthread_mutex_lock(®istry->lock); |
| 8689 | |
| 8690 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
| 8691 | assert(ust_reg_chan); |
| 8692 | |
| 8693 | if (!ust_reg_chan->register_done) { |
| 8694 | /* |
| 8695 | * TODO: eventually use the registry event count for |
| 8696 | * this channel to better guess header type for per-pid |
| 8697 | * buffers. |
| 8698 | */ |
| 8699 | type = USTCTL_CHANNEL_HEADER_LARGE; |
| 8700 | ust_reg_chan->nr_ctx_fields = nr_fields; |
| 8701 | ust_reg_chan->ctx_fields = fields; |
| 8702 | fields = NULL; |
| 8703 | ust_reg_chan->header_type = type; |
| 8704 | } else { |
| 8705 | /* Get current already assigned values. */ |
| 8706 | type = ust_reg_chan->header_type; |
| 8707 | } |
| 8708 | /* Channel id is set during the object creation. */ |
| 8709 | chan_id = ust_reg_chan->chan_id; |
| 8710 | |
| 8711 | /* Append to metadata */ |
| 8712 | if (!ust_reg_chan->metadata_dumped) { |
| 8713 | ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan); |
| 8714 | if (ret_code) { |
| 8715 | ERR("Error appending channel metadata (errno = %d)", ret_code); |
| 8716 | goto reply; |
| 8717 | } |
| 8718 | } |
| 8719 | |
| 8720 | reply: |
| 8721 | DBG3("UST app replying to register channel key %" PRIu64 |
| 8722 | " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type, |
| 8723 | ret_code); |
| 8724 | |
| 8725 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); |
| 8726 | if (ret < 0) { |
| 8727 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 8728 | ERR("UST app reply channel failed with ret %d", ret); |
| 8729 | } else { |
| 8730 | DBG3("UST app reply channel failed. Application died"); |
| 8731 | } |
| 8732 | goto error; |
| 8733 | } |
| 8734 | |
| 8735 | /* This channel registry registration is completed. */ |
| 8736 | ust_reg_chan->register_done = 1; |
| 8737 | |
| 8738 | error: |
| 8739 | pthread_mutex_unlock(®istry->lock); |
| 8740 | error_rcu_unlock: |
| 8741 | rcu_read_unlock(); |
| 8742 | free(fields); |
| 8743 | return ret; |
| 8744 | } |
| 8745 | |
| 8746 | static int add_event_ust_chan_registry(int sock, struct ust_app *ua, |
| 8747 | struct ust_app_channel *ua_chan, int sobjd, int cobjd, char *name, |
| 8748 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
| 8749 | int loglevel_value, char *model_emf_uri) |
| 8750 | { |
| 8751 | int ret, ret_code; |
| 8752 | uint32_t event_id = 0; |
| 8753 | uint64_t chan_reg_key; |
| 8754 | struct ust_app_session *ua_sess; |
| 8755 | struct ust_registry_session *registry; |
| 8756 | /* |
| 8757 | * The counter index is unused for channel events. It's only used for |
| 8758 | * map events. |
| 8759 | */ |
| 8760 | uint64_t counter_index = 0; |
| 8761 | |
| 8762 | assert(ua_chan->session); |
| 8763 | ua_sess = ua_chan->session; |
| 8764 | |
| 8765 | registry = get_session_registry(ua_sess); |
| 8766 | if (!registry) { |
| 8767 | DBG("Application session is being torn down. Abort event notify"); |
| 8768 | ret = 0; |
| 8769 | goto error; |
| 8770 | } |
| 8771 | |
| 8772 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 8773 | chan_reg_key = ua_chan->tracing_channel_id; |
| 8774 | } else { |
| 8775 | chan_reg_key = ua_chan->key; |
| 8776 | } |
| 8777 | |
| 8778 | pthread_mutex_lock(®istry->lock); |
| 8779 | |
| 8780 | /* |
| 8781 | * From this point on, this call acquires the ownership of the sig, fields |
| 8782 | * and model_emf_uri meaning any free are done inside it if needed. These |
| 8783 | * three variables MUST NOT be read/write after this. |
| 8784 | */ |
| 8785 | ret_code = ust_registry_chan_create_event(registry, chan_reg_key, |
| 8786 | sobjd, cobjd, name, sig, nr_fields, fields, |
| 8787 | loglevel_value, model_emf_uri, ua_sess->buffer_type, |
| 8788 | &event_id, ua); |
| 8789 | sig = NULL; |
| 8790 | fields = NULL; |
| 8791 | model_emf_uri = NULL; |
| 8792 | |
| 8793 | /* |
| 8794 | * The return value is returned to ustctl so in case of an error, the |
| 8795 | * application can be notified. In case of an error, it's important not to |
| 8796 | * return a negative error or else the application will get closed. |
| 8797 | */ |
| 8798 | ret = ustctl_reply_register_event(sock, event_id, counter_index, ret_code); |
| 8799 | if (ret < 0) { |
| 8800 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 8801 | ERR("UST app reply event failed with ret %d", ret); |
| 8802 | } else { |
| 8803 | DBG3("UST app reply event failed. Application died"); |
| 8804 | } |
| 8805 | /* |
| 8806 | * No need to wipe the create event since the application socket will |
| 8807 | * get close on error hence cleaning up everything by itself. |
| 8808 | */ |
| 8809 | goto error; |
| 8810 | } |
| 8811 | |
| 8812 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
| 8813 | name, event_id); |
| 8814 | |
| 8815 | error: |
| 8816 | pthread_mutex_unlock(®istry->lock); |
| 8817 | return ret; |
| 8818 | } |
| 8819 | |
| 8820 | static int add_event_ust_map_registry(int sock, struct ust_app *ua, |
| 8821 | struct ust_app_map *ua_map, int sobjd, int cobjd, char *name, |
| 8822 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
| 8823 | int loglevel_value, char *model_emf_uri, uint64_t tracer_token) |
| 8824 | { |
| 8825 | int ret, ret_code; |
| 8826 | uint64_t map_reg_key, counter_index; |
| 8827 | struct ust_app_session *ua_sess; |
| 8828 | struct ust_registry_session *registry; |
| 8829 | |
| 8830 | assert(ua_map->session); |
| 8831 | ua_sess = ua_map->session; |
| 8832 | |
| 8833 | registry = get_session_registry(ua_sess); |
| 8834 | if (!registry) { |
| 8835 | DBG("Application session is being torn down. Abort event notify"); |
| 8836 | ret = 0; |
| 8837 | goto error; |
| 8838 | } |
| 8839 | |
| 8840 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 8841 | map_reg_key = ua_map->tracing_map_id; |
| 8842 | } else { |
| 8843 | map_reg_key = ua_map->key; |
| 8844 | } |
| 8845 | |
| 8846 | pthread_mutex_lock(®istry->lock); |
| 8847 | |
| 8848 | /* |
| 8849 | * From this point on, this call acquires the ownership of the sig, fields |
| 8850 | * and model_emf_uri meaning any free are done inside it if needed. These |
| 8851 | * three variables MUST NOT be read/write after this. |
| 8852 | */ |
| 8853 | DBG("Registry_map_create_event on map=%"PRIu64" with token=%"PRIu64, |
| 8854 | map_reg_key, tracer_token); |
| 8855 | ret_code = ust_registry_map_create_event(registry, map_reg_key, |
| 8856 | sobjd, cobjd, name, sig, nr_fields, fields, |
| 8857 | loglevel_value, model_emf_uri, ua_sess->buffer_type, |
| 8858 | tracer_token, &counter_index, ua); |
| 8859 | assert(!ret_code); |
| 8860 | |
| 8861 | sig = NULL; |
| 8862 | fields = NULL; |
| 8863 | model_emf_uri = NULL; |
| 8864 | |
| 8865 | /* |
| 8866 | * The return value is returned to ustctl so in case of an error, the |
| 8867 | * application can be notified. In case of an error, it's important not to |
| 8868 | * return a negative error or else the application will get closed. |
| 8869 | */ |
| 8870 | ret = ustctl_reply_register_event(sock, counter_index, counter_index, |
| 8871 | ret_code); |
| 8872 | if (ret < 0) { |
| 8873 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 8874 | ERR("UST app reply event failed with ret %d", ret); |
| 8875 | } else { |
| 8876 | DBG3("UST app reply event failed. Application died"); |
| 8877 | } |
| 8878 | /* |
| 8879 | * No need to wipe the create event since the application socket will |
| 8880 | * get close on error hence cleaning up everything by itself. |
| 8881 | */ |
| 8882 | goto error; |
| 8883 | } |
| 8884 | |
| 8885 | DBG3("UST registry map event %s with counter index %" PRIu64 " added successfully", |
| 8886 | name, counter_index); |
| 8887 | |
| 8888 | error: |
| 8889 | pthread_mutex_unlock(®istry->lock); |
| 8890 | return ret; |
| 8891 | } |
| 8892 | |
| 8893 | |
| 8894 | /* |
| 8895 | * Add event to the UST channel registry. When the event is added to the |
| 8896 | * registry, the metadata is also created. Once done, this replies to the |
| 8897 | * application with the appropriate error code. |
| 8898 | * |
| 8899 | * The session UST registry lock is acquired in the function. |
| 8900 | * |
| 8901 | * On success 0 is returned else a negative value. |
| 8902 | */ |
| 8903 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
| 8904 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
| 8905 | int loglevel_value, char *model_emf_uri, uint64_t tracer_token) |
| 8906 | { |
| 8907 | int ret; |
| 8908 | struct ust_app *app; |
| 8909 | struct ust_app_channel *ua_chan = NULL; |
| 8910 | struct ust_app_map *ua_map = NULL; |
| 8911 | |
| 8912 | rcu_read_lock(); |
| 8913 | |
| 8914 | /* Lookup application. If not found, there is a code flow error. */ |
| 8915 | app = find_app_by_notify_sock(sock); |
| 8916 | if (!app) { |
| 8917 | DBG("Application socket %d is being torn down. Abort event notify", |
| 8918 | sock); |
| 8919 | ret = 0; |
| 8920 | goto end; |
| 8921 | } |
| 8922 | |
| 8923 | /* Lookup channel by UST object descriptor. */ |
| 8924 | ua_chan = find_channel_by_objd(app, cobjd); |
| 8925 | if (ua_chan) { |
| 8926 | ret = add_event_ust_chan_registry(sock, app, ua_chan, sobjd, cobjd, |
| 8927 | name, sig, nr_fields, fields, loglevel_value, |
| 8928 | model_emf_uri); |
| 8929 | if (ret) { |
| 8930 | ERR("Error adding channel event to registry: event_name = '%s'", name); |
| 8931 | } |
| 8932 | goto found; |
| 8933 | } |
| 8934 | |
| 8935 | /* Lookup map by UST object descriptor. */ |
| 8936 | ua_map = find_map_by_objd(app, cobjd); |
| 8937 | if (ua_map) { |
| 8938 | ret = add_event_ust_map_registry(sock, app, ua_map, sobjd, cobjd, |
| 8939 | name, sig, nr_fields, fields, loglevel_value, |
| 8940 | model_emf_uri, tracer_token); |
| 8941 | if (ret) { |
| 8942 | ERR("Error adding map event to registry: event_name = '%s'", name); |
| 8943 | goto end; |
| 8944 | } |
| 8945 | goto found; |
| 8946 | } |
| 8947 | |
| 8948 | if (!ua_chan && !ua_map) { |
| 8949 | DBG("Application event container is being torn down. Abort event notify"); |
| 8950 | ret = 0; |
| 8951 | goto end; |
| 8952 | } |
| 8953 | |
| 8954 | found: |
| 8955 | ret = 0; |
| 8956 | |
| 8957 | end: |
| 8958 | rcu_read_unlock(); |
| 8959 | return ret; |
| 8960 | } |
| 8961 | |
| 8962 | /* |
| 8963 | * Add enum to the UST session registry. Once done, this replies to the |
| 8964 | * application with the appropriate error code. |
| 8965 | * |
| 8966 | * The session UST registry lock is acquired within this function. |
| 8967 | * |
| 8968 | * On success 0 is returned else a negative value. |
| 8969 | */ |
| 8970 | static int add_enum_ust_registry(int sock, int sobjd, char *name, |
| 8971 | struct ustctl_enum_entry *entries, size_t nr_entries) |
| 8972 | { |
| 8973 | int ret = 0, ret_code; |
| 8974 | struct ust_app *app; |
| 8975 | struct ust_app_session *ua_sess; |
| 8976 | struct ust_registry_session *registry; |
| 8977 | uint64_t enum_id = -1ULL; |
| 8978 | |
| 8979 | rcu_read_lock(); |
| 8980 | |
| 8981 | /* Lookup application. If not found, there is a code flow error. */ |
| 8982 | app = find_app_by_notify_sock(sock); |
| 8983 | if (!app) { |
| 8984 | /* Return an error since this is not an error */ |
| 8985 | DBG("Application socket %d is being torn down. Aborting enum registration", |
| 8986 | sock); |
| 8987 | free(entries); |
| 8988 | goto error_rcu_unlock; |
| 8989 | } |
| 8990 | |
| 8991 | /* Lookup session by UST object descriptor. */ |
| 8992 | ua_sess = find_session_by_objd(app, sobjd); |
| 8993 | if (!ua_sess) { |
| 8994 | /* Return an error since this is not an error */ |
| 8995 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
| 8996 | free(entries); |
| 8997 | goto error_rcu_unlock; |
| 8998 | } |
| 8999 | |
| 9000 | registry = get_session_registry(ua_sess); |
| 9001 | if (!registry) { |
| 9002 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); |
| 9003 | free(entries); |
| 9004 | goto error_rcu_unlock; |
| 9005 | } |
| 9006 | |
| 9007 | pthread_mutex_lock(®istry->lock); |
| 9008 | |
| 9009 | /* |
| 9010 | * From this point on, the callee acquires the ownership of |
| 9011 | * entries. The variable entries MUST NOT be read/written after |
| 9012 | * call. |
| 9013 | */ |
| 9014 | ret_code = ust_registry_create_or_find_enum(registry, sobjd, name, |
| 9015 | entries, nr_entries, &enum_id); |
| 9016 | entries = NULL; |
| 9017 | |
| 9018 | /* |
| 9019 | * The return value is returned to ustctl so in case of an error, the |
| 9020 | * application can be notified. In case of an error, it's important not to |
| 9021 | * return a negative error or else the application will get closed. |
| 9022 | */ |
| 9023 | ret = ustctl_reply_register_enum(sock, enum_id, ret_code); |
| 9024 | if (ret < 0) { |
| 9025 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 9026 | ERR("UST app reply enum failed with ret %d", ret); |
| 9027 | } else { |
| 9028 | DBG3("UST app reply enum failed. Application died"); |
| 9029 | } |
| 9030 | /* |
| 9031 | * No need to wipe the create enum since the application socket will |
| 9032 | * get close on error hence cleaning up everything by itself. |
| 9033 | */ |
| 9034 | goto error; |
| 9035 | } |
| 9036 | |
| 9037 | DBG3("UST registry enum %s added successfully or already found", name); |
| 9038 | |
| 9039 | error: |
| 9040 | pthread_mutex_unlock(®istry->lock); |
| 9041 | error_rcu_unlock: |
| 9042 | rcu_read_unlock(); |
| 9043 | return ret; |
| 9044 | } |
| 9045 | |
| 9046 | /* |
| 9047 | * Handle application notification through the given notify socket. |
| 9048 | * |
| 9049 | * Return 0 on success or else a negative value. |
| 9050 | */ |
| 9051 | int ust_app_recv_notify(int sock) |
| 9052 | { |
| 9053 | int ret; |
| 9054 | enum ustctl_notify_cmd cmd; |
| 9055 | |
| 9056 | DBG3("UST app receiving notify from sock %d", sock); |
| 9057 | |
| 9058 | ret = ustctl_recv_notify(sock, &cmd); |
| 9059 | if (ret < 0) { |
| 9060 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 9061 | ERR("UST app recv notify failed with ret %d", ret); |
| 9062 | } else { |
| 9063 | DBG3("UST app recv notify failed. Application died"); |
| 9064 | } |
| 9065 | goto error; |
| 9066 | } |
| 9067 | |
| 9068 | switch (cmd) { |
| 9069 | case USTCTL_NOTIFY_CMD_EVENT: |
| 9070 | { |
| 9071 | int sobjd, cobjd, loglevel_value; |
| 9072 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
| 9073 | size_t nr_fields; |
| 9074 | uint64_t tracer_token = 0; |
| 9075 | struct ustctl_field *fields; |
| 9076 | |
| 9077 | DBG2("UST app ustctl register event received"); |
| 9078 | |
| 9079 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, |
| 9080 | &loglevel_value, &sig, &nr_fields, &fields, |
| 9081 | &model_emf_uri, &tracer_token); |
| 9082 | if (ret < 0) { |
| 9083 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 9084 | ERR("UST app recv event failed with ret %d", ret); |
| 9085 | } else { |
| 9086 | DBG3("UST app recv event failed. Application died"); |
| 9087 | } |
| 9088 | goto error; |
| 9089 | } |
| 9090 | |
| 9091 | /* |
| 9092 | * Add event to the UST registry coming from the notify socket. This |
| 9093 | * call will free if needed the sig, fields and model_emf_uri. This |
| 9094 | * code path loses the ownsership of these variables and transfer them |
| 9095 | * to the this function. |
| 9096 | */ |
| 9097 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
| 9098 | fields, loglevel_value, model_emf_uri, tracer_token); |
| 9099 | if (ret < 0) { |
| 9100 | goto error; |
| 9101 | } |
| 9102 | |
| 9103 | break; |
| 9104 | } |
| 9105 | case USTCTL_NOTIFY_CMD_CHANNEL: |
| 9106 | { |
| 9107 | int sobjd, cobjd; |
| 9108 | size_t nr_fields; |
| 9109 | struct ustctl_field *fields; |
| 9110 | |
| 9111 | DBG2("UST app ustctl register channel received"); |
| 9112 | |
| 9113 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, |
| 9114 | &fields); |
| 9115 | if (ret < 0) { |
| 9116 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 9117 | ERR("UST app recv channel failed with ret %d", ret); |
| 9118 | } else { |
| 9119 | DBG3("UST app recv channel failed. Application died"); |
| 9120 | } |
| 9121 | goto error; |
| 9122 | } |
| 9123 | |
| 9124 | /* |
| 9125 | * The fields ownership are transfered to this function call meaning |
| 9126 | * that if needed it will be freed. After this, it's invalid to access |
| 9127 | * fields or clean it up. |
| 9128 | */ |
| 9129 | ret = reply_ust_register_channel(sock, cobjd, nr_fields, |
| 9130 | fields); |
| 9131 | if (ret < 0) { |
| 9132 | goto error; |
| 9133 | } |
| 9134 | |
| 9135 | break; |
| 9136 | } |
| 9137 | case USTCTL_NOTIFY_CMD_ENUM: |
| 9138 | { |
| 9139 | int sobjd; |
| 9140 | char name[LTTNG_UST_SYM_NAME_LEN]; |
| 9141 | size_t nr_entries; |
| 9142 | struct ustctl_enum_entry *entries; |
| 9143 | |
| 9144 | DBG2("UST app ustctl register enum received"); |
| 9145 | |
| 9146 | ret = ustctl_recv_register_enum(sock, &sobjd, name, |
| 9147 | &entries, &nr_entries); |
| 9148 | if (ret < 0) { |
| 9149 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 9150 | ERR("UST app recv enum failed with ret %d", ret); |
| 9151 | } else { |
| 9152 | DBG3("UST app recv enum failed. Application died"); |
| 9153 | } |
| 9154 | goto error; |
| 9155 | } |
| 9156 | |
| 9157 | /* Callee assumes ownership of entries */ |
| 9158 | ret = add_enum_ust_registry(sock, sobjd, name, |
| 9159 | entries, nr_entries); |
| 9160 | if (ret < 0) { |
| 9161 | goto error; |
| 9162 | } |
| 9163 | |
| 9164 | break; |
| 9165 | } |
| 9166 | default: |
| 9167 | /* Should NEVER happen. */ |
| 9168 | assert(0); |
| 9169 | } |
| 9170 | |
| 9171 | error: |
| 9172 | return ret; |
| 9173 | } |
| 9174 | |
| 9175 | /* |
| 9176 | * Once the notify socket hangs up, this is called. First, it tries to find the |
| 9177 | * corresponding application. On failure, the call_rcu to close the socket is |
| 9178 | * executed. If an application is found, it tries to delete it from the notify |
| 9179 | * socket hash table. Whathever the result, it proceeds to the call_rcu. |
| 9180 | * |
| 9181 | * Note that an object needs to be allocated here so on ENOMEM failure, the |
| 9182 | * call RCU is not done but the rest of the cleanup is. |
| 9183 | */ |
| 9184 | void ust_app_notify_sock_unregister(int sock) |
| 9185 | { |
| 9186 | int err_enomem = 0; |
| 9187 | struct lttng_ht_iter iter; |
| 9188 | struct ust_app *app; |
| 9189 | struct ust_app_notify_sock_obj *obj; |
| 9190 | |
| 9191 | assert(sock >= 0); |
| 9192 | |
| 9193 | rcu_read_lock(); |
| 9194 | |
| 9195 | obj = zmalloc(sizeof(*obj)); |
| 9196 | if (!obj) { |
| 9197 | /* |
| 9198 | * An ENOMEM is kind of uncool. If this strikes we continue the |
| 9199 | * procedure but the call_rcu will not be called. In this case, we |
| 9200 | * accept the fd leak rather than possibly creating an unsynchronized |
| 9201 | * state between threads. |
| 9202 | * |
| 9203 | * TODO: The notify object should be created once the notify socket is |
| 9204 | * registered and stored independantely from the ust app object. The |
| 9205 | * tricky part is to synchronize the teardown of the application and |
| 9206 | * this notify object. Let's keep that in mind so we can avoid this |
| 9207 | * kind of shenanigans with ENOMEM in the teardown path. |
| 9208 | */ |
| 9209 | err_enomem = 1; |
| 9210 | } else { |
| 9211 | obj->fd = sock; |
| 9212 | } |
| 9213 | |
| 9214 | DBG("UST app notify socket unregister %d", sock); |
| 9215 | |
| 9216 | /* |
| 9217 | * Lookup application by notify socket. If this fails, this means that the |
| 9218 | * hash table delete has already been done by the application |
| 9219 | * unregistration process so we can safely close the notify socket in a |
| 9220 | * call RCU. |
| 9221 | */ |
| 9222 | app = find_app_by_notify_sock(sock); |
| 9223 | if (!app) { |
| 9224 | goto close_socket; |
| 9225 | } |
| 9226 | |
| 9227 | iter.iter.node = &app->notify_sock_n.node; |
| 9228 | |
| 9229 | /* |
| 9230 | * Whatever happens here either we fail or succeed, in both cases we have |
| 9231 | * to close the socket after a grace period to continue to the call RCU |
| 9232 | * here. If the deletion is successful, the application is not visible |
| 9233 | * anymore by other threads and is it fails it means that it was already |
| 9234 | * deleted from the hash table so either way we just have to close the |
| 9235 | * socket. |
| 9236 | */ |
| 9237 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 9238 | |
| 9239 | close_socket: |
| 9240 | rcu_read_unlock(); |
| 9241 | |
| 9242 | /* |
| 9243 | * Close socket after a grace period to avoid for the socket to be reused |
| 9244 | * before the application object is freed creating potential race between |
| 9245 | * threads trying to add unique in the global hash table. |
| 9246 | */ |
| 9247 | if (!err_enomem) { |
| 9248 | call_rcu(&obj->head, close_notify_sock_rcu); |
| 9249 | } |
| 9250 | } |
| 9251 | |
| 9252 | /* |
| 9253 | * Destroy a ust app data structure and free its memory. |
| 9254 | */ |
| 9255 | void ust_app_destroy(struct ust_app *app) |
| 9256 | { |
| 9257 | if (!app) { |
| 9258 | return; |
| 9259 | } |
| 9260 | |
| 9261 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
| 9262 | } |
| 9263 | |
| 9264 | /* |
| 9265 | * Take a snapshot for a given UST session. The snapshot is sent to the given |
| 9266 | * output. |
| 9267 | * |
| 9268 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
| 9269 | */ |
| 9270 | enum lttng_error_code ust_app_snapshot_record( |
| 9271 | const struct ltt_ust_session *usess, |
| 9272 | const struct consumer_output *output, int wait, |
| 9273 | uint64_t nb_packets_per_stream) |
| 9274 | { |
| 9275 | int ret = 0; |
| 9276 | enum lttng_error_code status = LTTNG_OK; |
| 9277 | struct lttng_ht_iter iter; |
| 9278 | struct ust_app *app; |
| 9279 | char *trace_path = NULL; |
| 9280 | |
| 9281 | assert(usess); |
| 9282 | assert(output); |
| 9283 | |
| 9284 | rcu_read_lock(); |
| 9285 | |
| 9286 | switch (usess->buffer_type) { |
| 9287 | case LTTNG_BUFFER_PER_UID: |
| 9288 | { |
| 9289 | struct buffer_reg_uid *reg; |
| 9290 | |
| 9291 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 9292 | struct buffer_reg_channel *buf_reg_chan; |
| 9293 | struct consumer_socket *socket; |
| 9294 | char pathname[PATH_MAX]; |
| 9295 | size_t consumer_path_offset = 0; |
| 9296 | |
| 9297 | if (!reg->registry->reg.ust->metadata_key) { |
| 9298 | /* Skip since no metadata is present */ |
| 9299 | continue; |
| 9300 | } |
| 9301 | |
| 9302 | /* Get consumer socket to use to push the metadata.*/ |
| 9303 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 9304 | usess->consumer); |
| 9305 | if (!socket) { |
| 9306 | status = LTTNG_ERR_INVALID; |
| 9307 | goto error; |
| 9308 | } |
| 9309 | |
| 9310 | memset(pathname, 0, sizeof(pathname)); |
| 9311 | ret = snprintf(pathname, sizeof(pathname), |
| 9312 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, |
| 9313 | reg->uid, reg->bits_per_long); |
| 9314 | if (ret < 0) { |
| 9315 | PERROR("snprintf snapshot path"); |
| 9316 | status = LTTNG_ERR_INVALID; |
| 9317 | goto error; |
| 9318 | } |
| 9319 | /* Free path allowed on previous iteration. */ |
| 9320 | free(trace_path); |
| 9321 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
| 9322 | &consumer_path_offset); |
| 9323 | if (!trace_path) { |
| 9324 | status = LTTNG_ERR_INVALID; |
| 9325 | goto error; |
| 9326 | } |
| 9327 | /* Add the UST default trace dir to path. */ |
| 9328 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 9329 | buf_reg_chan, node.node) { |
| 9330 | status = consumer_snapshot_channel(socket, |
| 9331 | buf_reg_chan->consumer_key, |
| 9332 | output, 0, usess->uid, |
| 9333 | usess->gid, &trace_path[consumer_path_offset], wait, |
| 9334 | nb_packets_per_stream); |
| 9335 | if (status != LTTNG_OK) { |
| 9336 | goto error; |
| 9337 | } |
| 9338 | } |
| 9339 | status = consumer_snapshot_channel(socket, |
| 9340 | reg->registry->reg.ust->metadata_key, output, 1, |
| 9341 | usess->uid, usess->gid, &trace_path[consumer_path_offset], |
| 9342 | wait, 0); |
| 9343 | if (status != LTTNG_OK) { |
| 9344 | goto error; |
| 9345 | } |
| 9346 | } |
| 9347 | break; |
| 9348 | } |
| 9349 | case LTTNG_BUFFER_PER_PID: |
| 9350 | { |
| 9351 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9352 | struct consumer_socket *socket; |
| 9353 | struct lttng_ht_iter chan_iter; |
| 9354 | struct ust_app_channel *ua_chan; |
| 9355 | struct ust_app_session *ua_sess; |
| 9356 | struct ust_registry_session *registry; |
| 9357 | char pathname[PATH_MAX]; |
| 9358 | size_t consumer_path_offset = 0; |
| 9359 | |
| 9360 | ua_sess = lookup_session_by_app(usess, app); |
| 9361 | if (!ua_sess) { |
| 9362 | /* Session not associated with this app. */ |
| 9363 | continue; |
| 9364 | } |
| 9365 | |
| 9366 | /* Get the right consumer socket for the application. */ |
| 9367 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 9368 | output); |
| 9369 | if (!socket) { |
| 9370 | status = LTTNG_ERR_INVALID; |
| 9371 | goto error; |
| 9372 | } |
| 9373 | |
| 9374 | /* Add the UST default trace dir to path. */ |
| 9375 | memset(pathname, 0, sizeof(pathname)); |
| 9376 | ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", |
| 9377 | ua_sess->path); |
| 9378 | if (ret < 0) { |
| 9379 | status = LTTNG_ERR_INVALID; |
| 9380 | PERROR("snprintf snapshot path"); |
| 9381 | goto error; |
| 9382 | } |
| 9383 | /* Free path allowed on previous iteration. */ |
| 9384 | free(trace_path); |
| 9385 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
| 9386 | &consumer_path_offset); |
| 9387 | if (!trace_path) { |
| 9388 | status = LTTNG_ERR_INVALID; |
| 9389 | goto error; |
| 9390 | } |
| 9391 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 9392 | ua_chan, node.node) { |
| 9393 | status = consumer_snapshot_channel(socket, |
| 9394 | ua_chan->key, output, 0, |
| 9395 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 9396 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 9397 | &trace_path[consumer_path_offset], wait, |
| 9398 | nb_packets_per_stream); |
| 9399 | switch (status) { |
| 9400 | case LTTNG_OK: |
| 9401 | break; |
| 9402 | case LTTNG_ERR_CHAN_NOT_FOUND: |
| 9403 | continue; |
| 9404 | default: |
| 9405 | goto error; |
| 9406 | } |
| 9407 | } |
| 9408 | |
| 9409 | registry = get_session_registry(ua_sess); |
| 9410 | if (!registry) { |
| 9411 | DBG("Application session is being torn down. Skip application."); |
| 9412 | continue; |
| 9413 | } |
| 9414 | status = consumer_snapshot_channel(socket, |
| 9415 | registry->metadata_key, output, 1, |
| 9416 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 9417 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 9418 | &trace_path[consumer_path_offset], wait, 0); |
| 9419 | switch (status) { |
| 9420 | case LTTNG_OK: |
| 9421 | break; |
| 9422 | case LTTNG_ERR_CHAN_NOT_FOUND: |
| 9423 | continue; |
| 9424 | default: |
| 9425 | goto error; |
| 9426 | } |
| 9427 | } |
| 9428 | break; |
| 9429 | } |
| 9430 | default: |
| 9431 | assert(0); |
| 9432 | break; |
| 9433 | } |
| 9434 | |
| 9435 | error: |
| 9436 | free(trace_path); |
| 9437 | rcu_read_unlock(); |
| 9438 | return status; |
| 9439 | } |
| 9440 | |
| 9441 | /* |
| 9442 | * Return the size taken by one more packet per stream. |
| 9443 | */ |
| 9444 | uint64_t ust_app_get_size_one_more_packet_per_stream( |
| 9445 | const struct ltt_ust_session *usess, uint64_t cur_nr_packets) |
| 9446 | { |
| 9447 | uint64_t tot_size = 0; |
| 9448 | struct ust_app *app; |
| 9449 | struct lttng_ht_iter iter; |
| 9450 | |
| 9451 | assert(usess); |
| 9452 | |
| 9453 | switch (usess->buffer_type) { |
| 9454 | case LTTNG_BUFFER_PER_UID: |
| 9455 | { |
| 9456 | struct buffer_reg_uid *reg; |
| 9457 | |
| 9458 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 9459 | struct buffer_reg_channel *buf_reg_chan; |
| 9460 | |
| 9461 | rcu_read_lock(); |
| 9462 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 9463 | buf_reg_chan, node.node) { |
| 9464 | if (cur_nr_packets >= buf_reg_chan->num_subbuf) { |
| 9465 | /* |
| 9466 | * Don't take channel into account if we |
| 9467 | * already grab all its packets. |
| 9468 | */ |
| 9469 | continue; |
| 9470 | } |
| 9471 | tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count; |
| 9472 | } |
| 9473 | rcu_read_unlock(); |
| 9474 | } |
| 9475 | break; |
| 9476 | } |
| 9477 | case LTTNG_BUFFER_PER_PID: |
| 9478 | { |
| 9479 | rcu_read_lock(); |
| 9480 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9481 | struct ust_app_channel *ua_chan; |
| 9482 | struct ust_app_session *ua_sess; |
| 9483 | struct lttng_ht_iter chan_iter; |
| 9484 | |
| 9485 | ua_sess = lookup_session_by_app(usess, app); |
| 9486 | if (!ua_sess) { |
| 9487 | /* Session not associated with this app. */ |
| 9488 | continue; |
| 9489 | } |
| 9490 | |
| 9491 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 9492 | ua_chan, node.node) { |
| 9493 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
| 9494 | /* |
| 9495 | * Don't take channel into account if we |
| 9496 | * already grab all its packets. |
| 9497 | */ |
| 9498 | continue; |
| 9499 | } |
| 9500 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; |
| 9501 | } |
| 9502 | } |
| 9503 | rcu_read_unlock(); |
| 9504 | break; |
| 9505 | } |
| 9506 | default: |
| 9507 | assert(0); |
| 9508 | break; |
| 9509 | } |
| 9510 | |
| 9511 | return tot_size; |
| 9512 | } |
| 9513 | |
| 9514 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, |
| 9515 | struct cds_list_head *buffer_reg_uid_list, |
| 9516 | struct consumer_output *consumer, uint64_t uchan_id, |
| 9517 | int overwrite, uint64_t *discarded, uint64_t *lost) |
| 9518 | { |
| 9519 | int ret; |
| 9520 | uint64_t consumer_chan_key; |
| 9521 | |
| 9522 | *discarded = 0; |
| 9523 | *lost = 0; |
| 9524 | |
| 9525 | ret = buffer_reg_uid_consumer_channel_key( |
| 9526 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
| 9527 | if (ret < 0) { |
| 9528 | /* Not found */ |
| 9529 | ret = 0; |
| 9530 | goto end; |
| 9531 | } |
| 9532 | |
| 9533 | if (overwrite) { |
| 9534 | ret = consumer_get_lost_packets(ust_session_id, |
| 9535 | consumer_chan_key, consumer, lost); |
| 9536 | } else { |
| 9537 | ret = consumer_get_discarded_events(ust_session_id, |
| 9538 | consumer_chan_key, consumer, discarded); |
| 9539 | } |
| 9540 | |
| 9541 | end: |
| 9542 | return ret; |
| 9543 | } |
| 9544 | |
| 9545 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, |
| 9546 | struct ltt_ust_channel *uchan, |
| 9547 | struct consumer_output *consumer, int overwrite, |
| 9548 | uint64_t *discarded, uint64_t *lost) |
| 9549 | { |
| 9550 | int ret = 0; |
| 9551 | struct lttng_ht_iter iter; |
| 9552 | struct lttng_ht_node_str *ua_chan_node; |
| 9553 | struct ust_app *app; |
| 9554 | struct ust_app_session *ua_sess; |
| 9555 | struct ust_app_channel *ua_chan; |
| 9556 | |
| 9557 | *discarded = 0; |
| 9558 | *lost = 0; |
| 9559 | |
| 9560 | rcu_read_lock(); |
| 9561 | /* |
| 9562 | * Iterate over every registered applications. Sum counters for |
| 9563 | * all applications containing requested session and channel. |
| 9564 | */ |
| 9565 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9566 | struct lttng_ht_iter uiter; |
| 9567 | |
| 9568 | ua_sess = lookup_session_by_app(usess, app); |
| 9569 | if (ua_sess == NULL) { |
| 9570 | continue; |
| 9571 | } |
| 9572 | |
| 9573 | /* Get channel */ |
| 9574 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
| 9575 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 9576 | /* If the session is found for the app, the channel must be there */ |
| 9577 | assert(ua_chan_node); |
| 9578 | |
| 9579 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 9580 | |
| 9581 | if (overwrite) { |
| 9582 | uint64_t _lost; |
| 9583 | |
| 9584 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, |
| 9585 | consumer, &_lost); |
| 9586 | if (ret < 0) { |
| 9587 | break; |
| 9588 | } |
| 9589 | (*lost) += _lost; |
| 9590 | } else { |
| 9591 | uint64_t _discarded; |
| 9592 | |
| 9593 | ret = consumer_get_discarded_events(usess->id, |
| 9594 | ua_chan->key, consumer, &_discarded); |
| 9595 | if (ret < 0) { |
| 9596 | break; |
| 9597 | } |
| 9598 | (*discarded) += _discarded; |
| 9599 | } |
| 9600 | } |
| 9601 | |
| 9602 | rcu_read_unlock(); |
| 9603 | return ret; |
| 9604 | } |
| 9605 | |
| 9606 | static |
| 9607 | int ust_app_regenerate_statedump(struct ltt_ust_session *usess, |
| 9608 | struct ust_app *app) |
| 9609 | { |
| 9610 | int ret = 0; |
| 9611 | struct ust_app_session *ua_sess; |
| 9612 | |
| 9613 | DBG("Regenerating the metadata for ust app pid %d", app->pid); |
| 9614 | |
| 9615 | rcu_read_lock(); |
| 9616 | |
| 9617 | ua_sess = lookup_session_by_app(usess, app); |
| 9618 | if (ua_sess == NULL) { |
| 9619 | /* The session is in teardown process. Ignore and continue. */ |
| 9620 | goto end; |
| 9621 | } |
| 9622 | |
| 9623 | pthread_mutex_lock(&ua_sess->lock); |
| 9624 | |
| 9625 | if (ua_sess->deleted) { |
| 9626 | goto end_unlock; |
| 9627 | } |
| 9628 | |
| 9629 | pthread_mutex_lock(&app->sock_lock); |
| 9630 | ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle); |
| 9631 | pthread_mutex_unlock(&app->sock_lock); |
| 9632 | |
| 9633 | end_unlock: |
| 9634 | pthread_mutex_unlock(&ua_sess->lock); |
| 9635 | |
| 9636 | end: |
| 9637 | rcu_read_unlock(); |
| 9638 | health_code_update(); |
| 9639 | return ret; |
| 9640 | } |
| 9641 | |
| 9642 | /* |
| 9643 | * Regenerate the statedump for each app in the session. |
| 9644 | */ |
| 9645 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) |
| 9646 | { |
| 9647 | int ret = 0; |
| 9648 | struct lttng_ht_iter iter; |
| 9649 | struct ust_app *app; |
| 9650 | |
| 9651 | DBG("Regenerating the metadata for all UST apps"); |
| 9652 | |
| 9653 | rcu_read_lock(); |
| 9654 | |
| 9655 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9656 | if (!app->compatible) { |
| 9657 | continue; |
| 9658 | } |
| 9659 | |
| 9660 | ret = ust_app_regenerate_statedump(usess, app); |
| 9661 | if (ret < 0) { |
| 9662 | /* Continue to the next app even on error */ |
| 9663 | continue; |
| 9664 | } |
| 9665 | } |
| 9666 | |
| 9667 | rcu_read_unlock(); |
| 9668 | |
| 9669 | return 0; |
| 9670 | } |
| 9671 | |
| 9672 | /* |
| 9673 | * Rotate all the channels of a session. |
| 9674 | * |
| 9675 | * Return LTTNG_OK on success or else an LTTng error code. |
| 9676 | */ |
| 9677 | enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) |
| 9678 | { |
| 9679 | int ret; |
| 9680 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 9681 | struct lttng_ht_iter iter; |
| 9682 | struct ust_app *app; |
| 9683 | struct ltt_ust_session *usess = session->ust_session; |
| 9684 | |
| 9685 | assert(usess); |
| 9686 | |
| 9687 | rcu_read_lock(); |
| 9688 | |
| 9689 | switch (usess->buffer_type) { |
| 9690 | case LTTNG_BUFFER_PER_UID: |
| 9691 | { |
| 9692 | struct buffer_reg_uid *reg; |
| 9693 | |
| 9694 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 9695 | struct buffer_reg_channel *buf_reg_chan; |
| 9696 | struct consumer_socket *socket; |
| 9697 | |
| 9698 | if (!reg->registry->reg.ust->metadata_key) { |
| 9699 | /* Skip since no metadata is present */ |
| 9700 | continue; |
| 9701 | } |
| 9702 | |
| 9703 | /* Get consumer socket to use to push the metadata.*/ |
| 9704 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 9705 | usess->consumer); |
| 9706 | if (!socket) { |
| 9707 | cmd_ret = LTTNG_ERR_INVALID; |
| 9708 | goto error; |
| 9709 | } |
| 9710 | |
| 9711 | /* Rotate the data channels. */ |
| 9712 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 9713 | buf_reg_chan, node.node) { |
| 9714 | ret = consumer_rotate_channel(socket, |
| 9715 | buf_reg_chan->consumer_key, |
| 9716 | usess->uid, usess->gid, |
| 9717 | usess->consumer, |
| 9718 | /* is_metadata_channel */ false); |
| 9719 | if (ret < 0) { |
| 9720 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 9721 | goto error; |
| 9722 | } |
| 9723 | } |
| 9724 | |
| 9725 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); |
| 9726 | |
| 9727 | ret = consumer_rotate_channel(socket, |
| 9728 | reg->registry->reg.ust->metadata_key, |
| 9729 | usess->uid, usess->gid, |
| 9730 | usess->consumer, |
| 9731 | /* is_metadata_channel */ true); |
| 9732 | if (ret < 0) { |
| 9733 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 9734 | goto error; |
| 9735 | } |
| 9736 | } |
| 9737 | break; |
| 9738 | } |
| 9739 | case LTTNG_BUFFER_PER_PID: |
| 9740 | { |
| 9741 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9742 | struct consumer_socket *socket; |
| 9743 | struct lttng_ht_iter chan_iter; |
| 9744 | struct ust_app_channel *ua_chan; |
| 9745 | struct ust_app_session *ua_sess; |
| 9746 | struct ust_registry_session *registry; |
| 9747 | |
| 9748 | ua_sess = lookup_session_by_app(usess, app); |
| 9749 | if (!ua_sess) { |
| 9750 | /* Session not associated with this app. */ |
| 9751 | continue; |
| 9752 | } |
| 9753 | |
| 9754 | /* Get the right consumer socket for the application. */ |
| 9755 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 9756 | usess->consumer); |
| 9757 | if (!socket) { |
| 9758 | cmd_ret = LTTNG_ERR_INVALID; |
| 9759 | goto error; |
| 9760 | } |
| 9761 | |
| 9762 | registry = get_session_registry(ua_sess); |
| 9763 | if (!registry) { |
| 9764 | DBG("Application session is being torn down. Skip application."); |
| 9765 | continue; |
| 9766 | } |
| 9767 | |
| 9768 | /* Rotate the data channels. */ |
| 9769 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 9770 | ua_chan, node.node) { |
| 9771 | ret = consumer_rotate_channel(socket, |
| 9772 | ua_chan->key, |
| 9773 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 9774 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 9775 | ua_sess->consumer, |
| 9776 | /* is_metadata_channel */ false); |
| 9777 | if (ret < 0) { |
| 9778 | /* Per-PID buffer and application going away. */ |
| 9779 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) |
| 9780 | continue; |
| 9781 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 9782 | goto error; |
| 9783 | } |
| 9784 | } |
| 9785 | |
| 9786 | /* Rotate the metadata channel. */ |
| 9787 | (void) push_metadata(registry, usess->consumer); |
| 9788 | ret = consumer_rotate_channel(socket, |
| 9789 | registry->metadata_key, |
| 9790 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
| 9791 | lttng_credentials_get_gid(&ua_sess->effective_credentials), |
| 9792 | ua_sess->consumer, |
| 9793 | /* is_metadata_channel */ true); |
| 9794 | if (ret < 0) { |
| 9795 | /* Per-PID buffer and application going away. */ |
| 9796 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) |
| 9797 | continue; |
| 9798 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 9799 | goto error; |
| 9800 | } |
| 9801 | } |
| 9802 | break; |
| 9803 | } |
| 9804 | default: |
| 9805 | assert(0); |
| 9806 | break; |
| 9807 | } |
| 9808 | |
| 9809 | cmd_ret = LTTNG_OK; |
| 9810 | |
| 9811 | error: |
| 9812 | rcu_read_unlock(); |
| 9813 | return cmd_ret; |
| 9814 | } |
| 9815 | |
| 9816 | enum lttng_error_code ust_app_create_channel_subdirectories( |
| 9817 | const struct ltt_ust_session *usess) |
| 9818 | { |
| 9819 | enum lttng_error_code ret = LTTNG_OK; |
| 9820 | struct lttng_ht_iter iter; |
| 9821 | enum lttng_trace_chunk_status chunk_status; |
| 9822 | char *pathname_index; |
| 9823 | int fmt_ret; |
| 9824 | |
| 9825 | assert(usess->current_trace_chunk); |
| 9826 | rcu_read_lock(); |
| 9827 | |
| 9828 | switch (usess->buffer_type) { |
| 9829 | case LTTNG_BUFFER_PER_UID: |
| 9830 | { |
| 9831 | struct buffer_reg_uid *reg; |
| 9832 | |
| 9833 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 9834 | fmt_ret = asprintf(&pathname_index, |
| 9835 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, |
| 9836 | reg->uid, reg->bits_per_long); |
| 9837 | if (fmt_ret < 0) { |
| 9838 | ERR("Failed to format channel index directory"); |
| 9839 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 9840 | goto error; |
| 9841 | } |
| 9842 | |
| 9843 | /* |
| 9844 | * Create the index subdirectory which will take care |
| 9845 | * of implicitly creating the channel's path. |
| 9846 | */ |
| 9847 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 9848 | usess->current_trace_chunk, |
| 9849 | pathname_index); |
| 9850 | free(pathname_index); |
| 9851 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 9852 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 9853 | goto error; |
| 9854 | } |
| 9855 | } |
| 9856 | break; |
| 9857 | } |
| 9858 | case LTTNG_BUFFER_PER_PID: |
| 9859 | { |
| 9860 | struct ust_app *app; |
| 9861 | |
| 9862 | /* |
| 9863 | * Create the toplevel ust/ directory in case no apps are running. |
| 9864 | */ |
| 9865 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 9866 | usess->current_trace_chunk, |
| 9867 | DEFAULT_UST_TRACE_DIR); |
| 9868 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 9869 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 9870 | goto error; |
| 9871 | } |
| 9872 | |
| 9873 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
| 9874 | pid_n.node) { |
| 9875 | struct ust_app_session *ua_sess; |
| 9876 | struct ust_registry_session *registry; |
| 9877 | |
| 9878 | ua_sess = lookup_session_by_app(usess, app); |
| 9879 | if (!ua_sess) { |
| 9880 | /* Session not associated with this app. */ |
| 9881 | continue; |
| 9882 | } |
| 9883 | |
| 9884 | registry = get_session_registry(ua_sess); |
| 9885 | if (!registry) { |
| 9886 | DBG("Application session is being torn down. Skip application."); |
| 9887 | continue; |
| 9888 | } |
| 9889 | |
| 9890 | fmt_ret = asprintf(&pathname_index, |
| 9891 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
| 9892 | ua_sess->path); |
| 9893 | if (fmt_ret < 0) { |
| 9894 | ERR("Failed to format channel index directory"); |
| 9895 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 9896 | goto error; |
| 9897 | } |
| 9898 | /* |
| 9899 | * Create the index subdirectory which will take care |
| 9900 | * of implicitly creating the channel's path. |
| 9901 | */ |
| 9902 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 9903 | usess->current_trace_chunk, |
| 9904 | pathname_index); |
| 9905 | free(pathname_index); |
| 9906 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 9907 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 9908 | goto error; |
| 9909 | } |
| 9910 | } |
| 9911 | break; |
| 9912 | } |
| 9913 | default: |
| 9914 | abort(); |
| 9915 | } |
| 9916 | |
| 9917 | ret = LTTNG_OK; |
| 9918 | error: |
| 9919 | rcu_read_unlock(); |
| 9920 | return ret; |
| 9921 | } |
| 9922 | |
| 9923 | /* |
| 9924 | * Clear all the channels of a session. |
| 9925 | * |
| 9926 | * Return LTTNG_OK on success or else an LTTng error code. |
| 9927 | */ |
| 9928 | static |
| 9929 | enum lttng_error_code ust_app_clear_session_channels(struct ltt_session *session) |
| 9930 | { |
| 9931 | int ret; |
| 9932 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 9933 | struct lttng_ht_iter iter; |
| 9934 | struct ust_app *app; |
| 9935 | struct ltt_ust_session *usess = session->ust_session; |
| 9936 | |
| 9937 | assert(usess); |
| 9938 | |
| 9939 | rcu_read_lock(); |
| 9940 | |
| 9941 | if (usess->active) { |
| 9942 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); |
| 9943 | cmd_ret = LTTNG_ERR_FATAL; |
| 9944 | goto end; |
| 9945 | } |
| 9946 | |
| 9947 | switch (usess->buffer_type) { |
| 9948 | case LTTNG_BUFFER_PER_UID: |
| 9949 | { |
| 9950 | struct buffer_reg_uid *reg; |
| 9951 | |
| 9952 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
| 9953 | struct buffer_reg_channel *buf_reg_chan; |
| 9954 | struct consumer_socket *socket; |
| 9955 | |
| 9956 | /* Get consumer socket to use to push the metadata.*/ |
| 9957 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, |
| 9958 | usess->consumer); |
| 9959 | if (!socket) { |
| 9960 | cmd_ret = LTTNG_ERR_INVALID; |
| 9961 | goto error_socket; |
| 9962 | } |
| 9963 | |
| 9964 | /* Clear the data channels. */ |
| 9965 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
| 9966 | buf_reg_chan, node.node) { |
| 9967 | ret = consumer_clear_channel(socket, |
| 9968 | buf_reg_chan->consumer_key); |
| 9969 | if (ret < 0) { |
| 9970 | goto error; |
| 9971 | } |
| 9972 | } |
| 9973 | |
| 9974 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); |
| 9975 | |
| 9976 | /* |
| 9977 | * Clear the metadata channel. |
| 9978 | * Metadata channel is not cleared per se but we still need to |
| 9979 | * perform a rotation operation on it behind the scene. |
| 9980 | */ |
| 9981 | ret = consumer_clear_channel(socket, |
| 9982 | reg->registry->reg.ust->metadata_key); |
| 9983 | if (ret < 0) { |
| 9984 | goto error; |
| 9985 | } |
| 9986 | } |
| 9987 | break; |
| 9988 | } |
| 9989 | case LTTNG_BUFFER_PER_PID: |
| 9990 | { |
| 9991 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 9992 | struct consumer_socket *socket; |
| 9993 | struct lttng_ht_iter chan_iter; |
| 9994 | struct ust_app_channel *ua_chan; |
| 9995 | struct ust_app_session *ua_sess; |
| 9996 | struct ust_registry_session *registry; |
| 9997 | |
| 9998 | ua_sess = lookup_session_by_app(usess, app); |
| 9999 | if (!ua_sess) { |
| 10000 | /* Session not associated with this app. */ |
| 10001 | continue; |
| 10002 | } |
| 10003 | |
| 10004 | /* Get the right consumer socket for the application. */ |
| 10005 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
| 10006 | usess->consumer); |
| 10007 | if (!socket) { |
| 10008 | cmd_ret = LTTNG_ERR_INVALID; |
| 10009 | goto error_socket; |
| 10010 | } |
| 10011 | |
| 10012 | registry = get_session_registry(ua_sess); |
| 10013 | if (!registry) { |
| 10014 | DBG("Application session is being torn down. Skip application."); |
| 10015 | continue; |
| 10016 | } |
| 10017 | |
| 10018 | /* Clear the data channels. */ |
| 10019 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
| 10020 | ua_chan, node.node) { |
| 10021 | ret = consumer_clear_channel(socket, ua_chan->key); |
| 10022 | if (ret < 0) { |
| 10023 | /* Per-PID buffer and application going away. */ |
| 10024 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
| 10025 | continue; |
| 10026 | } |
| 10027 | goto error; |
| 10028 | } |
| 10029 | } |
| 10030 | |
| 10031 | (void) push_metadata(registry, usess->consumer); |
| 10032 | |
| 10033 | /* |
| 10034 | * Clear the metadata channel. |
| 10035 | * Metadata channel is not cleared per se but we still need to |
| 10036 | * perform rotation operation on it behind the scene. |
| 10037 | */ |
| 10038 | ret = consumer_clear_channel(socket, registry->metadata_key); |
| 10039 | if (ret < 0) { |
| 10040 | /* Per-PID buffer and application going away. */ |
| 10041 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
| 10042 | continue; |
| 10043 | } |
| 10044 | goto error; |
| 10045 | } |
| 10046 | } |
| 10047 | break; |
| 10048 | } |
| 10049 | default: |
| 10050 | assert(0); |
| 10051 | break; |
| 10052 | } |
| 10053 | |
| 10054 | cmd_ret = LTTNG_OK; |
| 10055 | goto end; |
| 10056 | |
| 10057 | error: |
| 10058 | switch (-ret) { |
| 10059 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: |
| 10060 | cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; |
| 10061 | break; |
| 10062 | default: |
| 10063 | cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; |
| 10064 | } |
| 10065 | |
| 10066 | error_socket: |
| 10067 | end: |
| 10068 | rcu_read_unlock(); |
| 10069 | return cmd_ret; |
| 10070 | } |
| 10071 | |
| 10072 | static |
| 10073 | enum lttng_error_code ust_app_clear_session_maps_per_uid( |
| 10074 | struct ltt_ust_session *usess, struct ltt_ust_map *umap, |
| 10075 | uint32_t app_bitness) |
| 10076 | { |
| 10077 | struct lttng_ht_iter iter; |
| 10078 | struct buffer_reg_uid *buf_reg_uid; |
| 10079 | struct buffer_reg_map *buf_reg_map; |
| 10080 | struct ust_registry_session *ust_reg_sess; |
| 10081 | struct lttng_ht_node_u64 *ust_reg_map_node; |
| 10082 | struct ust_registry_map *ust_reg_map; |
| 10083 | struct ust_registry_map_index_ht_entry *map_index_entry; |
| 10084 | enum lttng_error_code status; |
| 10085 | |
| 10086 | buf_reg_uid = buffer_reg_uid_find(usess->id, app_bitness, usess->uid); |
| 10087 | if (!buf_reg_uid) { |
| 10088 | /* |
| 10089 | * Buffer registry entry for uid not found. Probably no app for |
| 10090 | * this UID at the moment. |
| 10091 | */ |
| 10092 | DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64", bitness = %"PRIu32", uid = %d", |
| 10093 | usess->id, app_bitness, usess->uid); |
| 10094 | /* |
| 10095 | * Not an error. Leave the key value pair unchanged and return. |
| 10096 | */ |
| 10097 | status = LTTNG_OK; |
| 10098 | goto end; |
| 10099 | } |
| 10100 | |
| 10101 | buf_reg_map = buffer_reg_map_find(umap->id, buf_reg_uid); |
| 10102 | if (!buf_reg_uid) { |
| 10103 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, |
| 10104 | umap->id); |
| 10105 | status = LTTNG_ERR_UNK; |
| 10106 | goto end; |
| 10107 | } |
| 10108 | |
| 10109 | ust_reg_sess = buf_reg_uid->registry->reg.ust; |
| 10110 | |
| 10111 | /* Get the ust_reg map object from the registry */ |
| 10112 | // FIXME: frdeso: This can be changed to ust_registry_map_find() right? |
| 10113 | |
| 10114 | lttng_ht_lookup(ust_reg_sess->maps, (void *) &umap->id, &iter); |
| 10115 | ust_reg_map_node = lttng_ht_iter_get_node_u64(&iter); |
| 10116 | if (!ust_reg_map_node) { |
| 10117 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, |
| 10118 | umap->id); |
| 10119 | status = LTTNG_ERR_UNK; |
| 10120 | goto end; |
| 10121 | } |
| 10122 | ust_reg_map = caa_container_of(ust_reg_map_node, |
| 10123 | struct ust_registry_map, node); |
| 10124 | |
| 10125 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, |
| 10126 | &iter.iter, map_index_entry, node.node) { |
| 10127 | int ret; |
| 10128 | size_t dimension_indexes[1] = {map_index_entry->index}; |
| 10129 | |
| 10130 | ret = ustctl_counter_clear(buf_reg_map->daemon_counter, dimension_indexes); |
| 10131 | if (ret) { |
| 10132 | ERR("clearing counter index %"PRIu64, map_index_entry->index); |
| 10133 | //fixme: frdeso: convert ust errors to tools errors |
| 10134 | status = LTTNG_ERR_UNK; |
| 10135 | goto end; |
| 10136 | } |
| 10137 | } |
| 10138 | |
| 10139 | status = LTTNG_OK; |
| 10140 | |
| 10141 | end: |
| 10142 | return status; |
| 10143 | } |
| 10144 | |
| 10145 | static |
| 10146 | enum lttng_error_code ust_app_clear_session_maps_per_pid( |
| 10147 | struct ltt_ust_session *usess, struct ltt_ust_map *umap, |
| 10148 | uint32_t app_bitness) |
| 10149 | { |
| 10150 | struct lttng_ht_iter app_iter; |
| 10151 | enum lttng_error_code status; |
| 10152 | struct ust_app *app; |
| 10153 | struct map_kv_ht_entry *kv_entry; |
| 10154 | struct lttng_ht_iter iter; |
| 10155 | |
| 10156 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { |
| 10157 | struct lttng_ht_iter map_iter, key_iter; |
| 10158 | struct lttng_ht_node_str *ua_map_node; |
| 10159 | struct ust_app_map *ua_map; |
| 10160 | struct ust_app_session *ua_sess; |
| 10161 | struct ust_registry_session *ust_reg_sess; |
| 10162 | struct ust_registry_map *ust_reg_map; |
| 10163 | struct ust_registry_map_index_ht_entry *map_index_entry; |
| 10164 | |
| 10165 | if (app->bits_per_long != app_bitness) { |
| 10166 | continue; |
| 10167 | } |
| 10168 | |
| 10169 | ua_sess = lookup_session_by_app(usess, app); |
| 10170 | if (!ua_sess) { |
| 10171 | /* Session not associated with this app. */ |
| 10172 | continue; |
| 10173 | } |
| 10174 | |
| 10175 | ust_reg_sess = get_session_registry(ua_sess); |
| 10176 | if (!ust_reg_sess) { |
| 10177 | DBG("Application session is being torn down. Skip application."); |
| 10178 | continue; |
| 10179 | } |
| 10180 | |
| 10181 | /* Lookup map in the ust app session */ |
| 10182 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &map_iter); |
| 10183 | ua_map_node = lttng_ht_iter_get_node_str(&map_iter); |
| 10184 | |
| 10185 | assert(ua_map_node != NULL); |
| 10186 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
| 10187 | |
| 10188 | pthread_mutex_lock(&ust_reg_sess->lock); |
| 10189 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); |
| 10190 | pthread_mutex_unlock(&ust_reg_sess->lock); |
| 10191 | assert(ust_reg_map); |
| 10192 | |
| 10193 | /* Iterate over all the formated_key -> counter index */ |
| 10194 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, |
| 10195 | &key_iter.iter, map_index_entry, node.node) { |
| 10196 | |
| 10197 | int ret; |
| 10198 | size_t dimension_indexes[1] = {map_index_entry->index}; |
| 10199 | |
| 10200 | ret = ustctl_counter_clear(ua_map->map_handle, |
| 10201 | dimension_indexes); |
| 10202 | if (ret) { |
| 10203 | ERR("clearing counter index %"PRIu64, map_index_entry->index); |
| 10204 | //fixme: frdeso: convert ust errors to tools errors |
| 10205 | status = LTTNG_ERR_UNK; |
| 10206 | goto end; |
| 10207 | } |
| 10208 | } |
| 10209 | } |
| 10210 | |
| 10211 | /* |
| 10212 | * Emptying the dead app key values. |
| 10213 | */ |
| 10214 | pthread_mutex_lock(&umap->dead_app_kv_values.lock); |
| 10215 | |
| 10216 | if (app_bitness == 32) { |
| 10217 | cds_lfht_for_each_entry(umap->dead_app_kv_values.dead_app_kv_values_32bits->ht, |
| 10218 | &iter.iter, kv_entry, node.node) { |
| 10219 | kv_entry->value = 0; |
| 10220 | } |
| 10221 | } else { |
| 10222 | |
| 10223 | cds_lfht_for_each_entry(umap->dead_app_kv_values.dead_app_kv_values_64bits->ht, |
| 10224 | &iter.iter, kv_entry, node.node) { |
| 10225 | kv_entry->value = 0; |
| 10226 | } |
| 10227 | } |
| 10228 | |
| 10229 | pthread_mutex_unlock(&umap->dead_app_kv_values.lock); |
| 10230 | |
| 10231 | status = LTTNG_OK; |
| 10232 | end: |
| 10233 | return status; |
| 10234 | } |
| 10235 | |
| 10236 | static |
| 10237 | enum lttng_error_code ust_app_clear_session_maps(struct ltt_session *session) |
| 10238 | { |
| 10239 | struct ltt_ust_session *usess = session->ust_session; |
| 10240 | enum lttng_error_code status; |
| 10241 | struct lttng_ht_iter iter; |
| 10242 | struct ltt_ust_map *umap; |
| 10243 | |
| 10244 | cds_lfht_for_each_entry(usess->domain_global.maps->ht, &iter.iter, |
| 10245 | umap, node.node) { |
| 10246 | |
| 10247 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 10248 | status = ust_app_clear_session_maps_per_uid(session->ust_session, |
| 10249 | umap, 32); |
| 10250 | assert(status == LTTNG_OK); |
| 10251 | |
| 10252 | status = ust_app_clear_session_maps_per_uid(session->ust_session, |
| 10253 | umap, 64); |
| 10254 | assert(status == LTTNG_OK); |
| 10255 | //fixme:frdeso:error handling |
| 10256 | } else { |
| 10257 | status = ust_app_clear_session_maps_per_pid(session->ust_session, |
| 10258 | umap, 32); |
| 10259 | assert(status == LTTNG_OK); |
| 10260 | |
| 10261 | status = ust_app_clear_session_maps_per_pid(session->ust_session, |
| 10262 | umap, 64); |
| 10263 | assert(status == LTTNG_OK); |
| 10264 | //fixme:frdeso:error handling |
| 10265 | // |
| 10266 | // |
| 10267 | } |
| 10268 | |
| 10269 | } |
| 10270 | |
| 10271 | return LTTNG_OK; |
| 10272 | } |
| 10273 | |
| 10274 | enum lttng_error_code ust_app_clear_session(struct ltt_session *session) |
| 10275 | { |
| 10276 | enum lttng_error_code cmd_ret; |
| 10277 | |
| 10278 | |
| 10279 | cmd_ret = ust_app_clear_session_channels(session); |
| 10280 | if (cmd_ret != LTTNG_OK) { |
| 10281 | ERR("Clearing session's channels"); |
| 10282 | goto end; |
| 10283 | } |
| 10284 | |
| 10285 | cmd_ret = ust_app_clear_session_maps(session); |
| 10286 | if (cmd_ret != LTTNG_OK) { |
| 10287 | ERR("Clearing session's maps"); |
| 10288 | goto end; |
| 10289 | } |
| 10290 | end: |
| 10291 | return cmd_ret; |
| 10292 | } |
| 10293 | |
| 10294 | /* |
| 10295 | * This function skips the metadata channel as the begin/end timestamps of a |
| 10296 | * metadata packet are useless. |
| 10297 | * |
| 10298 | * Moreover, opening a packet after a "clear" will cause problems for live |
| 10299 | * sessions as it will introduce padding that was not part of the first trace |
| 10300 | * chunk. The relay daemon expects the content of the metadata stream of |
| 10301 | * successive metadata trace chunks to be strict supersets of one another. |
| 10302 | * |
| 10303 | * For example, flushing a packet at the beginning of the metadata stream of |
| 10304 | * a trace chunk resulting from a "clear" session command will cause the |
| 10305 | * size of the metadata stream of the new trace chunk to not match the size of |
| 10306 | * the metadata stream of the original chunk. This will confuse the relay |
| 10307 | * daemon as the same "offset" in a metadata stream will no longer point |
| 10308 | * to the same content. |
| 10309 | */ |
| 10310 | enum lttng_error_code ust_app_open_packets(struct ltt_session *session) |
| 10311 | { |
| 10312 | enum lttng_error_code ret = LTTNG_OK; |
| 10313 | struct lttng_ht_iter iter; |
| 10314 | struct ltt_ust_session *usess = session->ust_session; |
| 10315 | |
| 10316 | assert(usess); |
| 10317 | |
| 10318 | rcu_read_lock(); |
| 10319 | |
| 10320 | switch (usess->buffer_type) { |
| 10321 | case LTTNG_BUFFER_PER_UID: |
| 10322 | { |
| 10323 | struct buffer_reg_uid *reg; |
| 10324 | |
| 10325 | cds_list_for_each_entry ( |
| 10326 | reg, &usess->buffer_reg_uid_list, lnode) { |
| 10327 | struct buffer_reg_channel *buf_reg_chan; |
| 10328 | struct consumer_socket *socket; |
| 10329 | |
| 10330 | socket = consumer_find_socket_by_bitness( |
| 10331 | reg->bits_per_long, usess->consumer); |
| 10332 | if (!socket) { |
| 10333 | ret = LTTNG_ERR_FATAL; |
| 10334 | goto error; |
| 10335 | } |
| 10336 | |
| 10337 | cds_lfht_for_each_entry(reg->registry->channels->ht, |
| 10338 | &iter.iter, buf_reg_chan, node.node) { |
| 10339 | const int open_ret = |
| 10340 | consumer_open_channel_packets( |
| 10341 | socket, |
| 10342 | buf_reg_chan->consumer_key); |
| 10343 | |
| 10344 | if (open_ret < 0) { |
| 10345 | ret = LTTNG_ERR_UNK; |
| 10346 | goto error; |
| 10347 | } |
| 10348 | } |
| 10349 | } |
| 10350 | break; |
| 10351 | } |
| 10352 | case LTTNG_BUFFER_PER_PID: |
| 10353 | { |
| 10354 | struct ust_app *app; |
| 10355 | |
| 10356 | cds_lfht_for_each_entry ( |
| 10357 | ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 10358 | struct consumer_socket *socket; |
| 10359 | struct lttng_ht_iter chan_iter; |
| 10360 | struct ust_app_channel *ua_chan; |
| 10361 | struct ust_app_session *ua_sess; |
| 10362 | struct ust_registry_session *registry; |
| 10363 | |
| 10364 | ua_sess = lookup_session_by_app(usess, app); |
| 10365 | if (!ua_sess) { |
| 10366 | /* Session not associated with this app. */ |
| 10367 | continue; |
| 10368 | } |
| 10369 | |
| 10370 | /* Get the right consumer socket for the application. */ |
| 10371 | socket = consumer_find_socket_by_bitness( |
| 10372 | app->bits_per_long, usess->consumer); |
| 10373 | if (!socket) { |
| 10374 | ret = LTTNG_ERR_FATAL; |
| 10375 | goto error; |
| 10376 | } |
| 10377 | |
| 10378 | registry = get_session_registry(ua_sess); |
| 10379 | if (!registry) { |
| 10380 | DBG("Application session is being torn down. Skip application."); |
| 10381 | continue; |
| 10382 | } |
| 10383 | |
| 10384 | cds_lfht_for_each_entry(ua_sess->channels->ht, |
| 10385 | &chan_iter.iter, ua_chan, node.node) { |
| 10386 | const int open_ret = |
| 10387 | consumer_open_channel_packets( |
| 10388 | socket, |
| 10389 | ua_chan->key); |
| 10390 | |
| 10391 | if (open_ret < 0) { |
| 10392 | /* |
| 10393 | * Per-PID buffer and application going |
| 10394 | * away. |
| 10395 | */ |
| 10396 | if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
| 10397 | continue; |
| 10398 | } |
| 10399 | |
| 10400 | ret = LTTNG_ERR_UNK; |
| 10401 | goto error; |
| 10402 | } |
| 10403 | } |
| 10404 | } |
| 10405 | break; |
| 10406 | } |
| 10407 | default: |
| 10408 | abort(); |
| 10409 | break; |
| 10410 | } |
| 10411 | |
| 10412 | error: |
| 10413 | rcu_read_unlock(); |
| 10414 | return ret; |
| 10415 | } |