| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | #include <urcu/compiler.h> |
| 28 | #include <lttng/ust-error.h> |
| 29 | |
| 30 | #include <common/common.h> |
| 31 | #include <common/sessiond-comm/sessiond-comm.h> |
| 32 | |
| 33 | #include "fd-limit.h" |
| 34 | #include "health.h" |
| 35 | #include "ust-app.h" |
| 36 | #include "ust-consumer.h" |
| 37 | #include "ust-ctl.h" |
| 38 | |
| 39 | /* Next available channel key. */ |
| 40 | static unsigned long next_channel_key; |
| 41 | |
| 42 | /* |
| 43 | * Return the atomically incremented value of next_channel_key. |
| 44 | */ |
| 45 | static inline unsigned long get_next_channel_key(void) |
| 46 | { |
| 47 | return uatomic_add_return(&next_channel_key, 1); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Return the consumer socket from the given consumer output with the right |
| 52 | * bitness. On error, returns NULL. |
| 53 | * |
| 54 | * The caller MUST acquire a rcu read side lock and keep it until the socket |
| 55 | * object reference is not needed anymore. |
| 56 | */ |
| 57 | static struct consumer_socket *find_consumer_socket_by_bitness(int bits, |
| 58 | struct consumer_output *consumer) |
| 59 | { |
| 60 | int consumer_fd; |
| 61 | struct consumer_socket *socket = NULL; |
| 62 | |
| 63 | switch (bits) { |
| 64 | case 64: |
| 65 | consumer_fd = uatomic_read(&ust_consumerd64_fd); |
| 66 | break; |
| 67 | case 32: |
| 68 | consumer_fd = uatomic_read(&ust_consumerd32_fd); |
| 69 | break; |
| 70 | default: |
| 71 | assert(0); |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | socket = consumer_find_socket(consumer_fd, consumer); |
| 76 | if (!socket) { |
| 77 | ERR("Consumer socket fd %d not found in consumer obj %p", |
| 78 | consumer_fd, consumer); |
| 79 | } |
| 80 | |
| 81 | end: |
| 82 | return socket; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Match function for the hash table lookup. |
| 87 | * |
| 88 | * It matches an ust app event based on three attributes which are the event |
| 89 | * name, the filter bytecode and the loglevel. |
| 90 | */ |
| 91 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
| 92 | { |
| 93 | struct ust_app_event *event; |
| 94 | const struct ust_app_ht_key *key; |
| 95 | |
| 96 | assert(node); |
| 97 | assert(_key); |
| 98 | |
| 99 | event = caa_container_of(node, struct ust_app_event, node.node); |
| 100 | key = _key; |
| 101 | |
| 102 | /* Match the 3 elements of the key: name, filter and loglevel. */ |
| 103 | |
| 104 | /* Event name */ |
| 105 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { |
| 106 | goto no_match; |
| 107 | } |
| 108 | |
| 109 | /* Event loglevel. */ |
| 110 | if (event->attr.loglevel != key->loglevel) { |
| 111 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
| 112 | && key->loglevel == 0 && event->attr.loglevel == -1) { |
| 113 | /* |
| 114 | * Match is accepted. This is because on event creation, the |
| 115 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and |
| 116 | * -1 are accepted for this loglevel type since 0 is the one set by |
| 117 | * the API when receiving an enable event. |
| 118 | */ |
| 119 | } else { |
| 120 | goto no_match; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* One of the filters is NULL, fail. */ |
| 125 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { |
| 126 | goto no_match; |
| 127 | } |
| 128 | |
| 129 | if (key->filter && event->filter) { |
| 130 | /* Both filters exists, check length followed by the bytecode. */ |
| 131 | if (event->filter->len != key->filter->len || |
| 132 | memcmp(event->filter->data, key->filter->data, |
| 133 | event->filter->len) != 0) { |
| 134 | goto no_match; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Match. */ |
| 139 | return 1; |
| 140 | |
| 141 | no_match: |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Unique add of an ust app event in the given ht. This uses the custom |
| 147 | * ht_match_ust_app_event match function and the event name as hash. |
| 148 | */ |
| 149 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
| 150 | struct ust_app_event *event) |
| 151 | { |
| 152 | struct cds_lfht_node *node_ptr; |
| 153 | struct ust_app_ht_key key; |
| 154 | struct lttng_ht *ht; |
| 155 | |
| 156 | assert(ua_chan); |
| 157 | assert(ua_chan->events); |
| 158 | assert(event); |
| 159 | |
| 160 | ht = ua_chan->events; |
| 161 | key.name = event->attr.name; |
| 162 | key.filter = event->filter; |
| 163 | key.loglevel = event->attr.loglevel; |
| 164 | |
| 165 | node_ptr = cds_lfht_add_unique(ht->ht, |
| 166 | ht->hash_fct(event->node.key, lttng_ht_seed), |
| 167 | ht_match_ust_app_event, &key, &event->node.node); |
| 168 | assert(node_ptr == &event->node.node); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Close the notify socket from the given RCU head object. This MUST be called |
| 173 | * through a call_rcu(). |
| 174 | */ |
| 175 | static void close_notify_sock_rcu(struct rcu_head *head) |
| 176 | { |
| 177 | int ret; |
| 178 | struct ust_app_notify_sock_obj *obj = |
| 179 | caa_container_of(head, struct ust_app_notify_sock_obj, head); |
| 180 | |
| 181 | /* Must have a valid fd here. */ |
| 182 | assert(obj->fd >= 0); |
| 183 | |
| 184 | ret = close(obj->fd); |
| 185 | if (ret) { |
| 186 | ERR("close notify sock %d RCU", obj->fd); |
| 187 | } |
| 188 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 189 | |
| 190 | free(obj); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Delete ust context safely. RCU read lock must be held before calling |
| 195 | * this function. |
| 196 | */ |
| 197 | static |
| 198 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx) |
| 199 | { |
| 200 | int ret; |
| 201 | |
| 202 | assert(ua_ctx); |
| 203 | |
| 204 | if (ua_ctx->obj) { |
| 205 | ret = ustctl_release_object(sock, ua_ctx->obj); |
| 206 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 207 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", |
| 208 | sock, ua_ctx->obj->handle, ret); |
| 209 | } |
| 210 | free(ua_ctx->obj); |
| 211 | } |
| 212 | free(ua_ctx); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Delete ust app event safely. RCU read lock must be held before calling |
| 217 | * this function. |
| 218 | */ |
| 219 | static |
| 220 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | assert(ua_event); |
| 225 | |
| 226 | free(ua_event->filter); |
| 227 | |
| 228 | if (ua_event->obj != NULL) { |
| 229 | ret = ustctl_release_object(sock, ua_event->obj); |
| 230 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 231 | ERR("UST app sock %d release event obj failed with ret %d", |
| 232 | sock, ret); |
| 233 | } |
| 234 | free(ua_event->obj); |
| 235 | } |
| 236 | free(ua_event); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Delete ust app stream safely. RCU read lock must be held before calling |
| 241 | * this function. |
| 242 | */ |
| 243 | static |
| 244 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream) |
| 245 | { |
| 246 | int ret; |
| 247 | |
| 248 | assert(stream); |
| 249 | |
| 250 | if (stream->obj) { |
| 251 | ret = ustctl_release_object(sock, stream->obj); |
| 252 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 253 | ERR("UST app sock %d release stream obj failed with ret %d", |
| 254 | sock, ret); |
| 255 | } |
| 256 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 257 | free(stream->obj); |
| 258 | } |
| 259 | free(stream); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Delete ust app channel safely. RCU read lock must be held before calling |
| 264 | * this function. |
| 265 | */ |
| 266 | static |
| 267 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
| 268 | struct ust_app *app) |
| 269 | { |
| 270 | int ret; |
| 271 | struct lttng_ht_iter iter; |
| 272 | struct ust_app_event *ua_event; |
| 273 | struct ust_app_ctx *ua_ctx; |
| 274 | struct ust_app_stream *stream, *stmp; |
| 275 | |
| 276 | assert(ua_chan); |
| 277 | |
| 278 | DBG3("UST app deleting channel %s", ua_chan->name); |
| 279 | |
| 280 | /* Wipe stream */ |
| 281 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 282 | cds_list_del(&stream->list); |
| 283 | delete_ust_app_stream(sock, stream); |
| 284 | } |
| 285 | |
| 286 | /* Wipe context */ |
| 287 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
| 288 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
| 289 | assert(!ret); |
| 290 | delete_ust_app_ctx(sock, ua_ctx); |
| 291 | } |
| 292 | lttng_ht_destroy(ua_chan->ctx); |
| 293 | |
| 294 | /* Wipe events */ |
| 295 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
| 296 | node.node) { |
| 297 | ret = lttng_ht_del(ua_chan->events, &iter); |
| 298 | assert(!ret); |
| 299 | delete_ust_app_event(sock, ua_event); |
| 300 | } |
| 301 | lttng_ht_destroy(ua_chan->events); |
| 302 | |
| 303 | /* Wipe and free registry. */ |
| 304 | ust_registry_channel_destroy(&ua_chan->session->registry, &ua_chan->registry); |
| 305 | |
| 306 | if (ua_chan->obj != NULL) { |
| 307 | /* Remove channel from application UST object descriptor. */ |
| 308 | iter.iter.node = &ua_chan->ust_objd_node.node; |
| 309 | lttng_ht_del(app->ust_objd, &iter); |
| 310 | ret = ustctl_release_object(sock, ua_chan->obj); |
| 311 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 312 | ERR("UST app sock %d release channel obj failed with ret %d", |
| 313 | sock, ret); |
| 314 | } |
| 315 | lttng_fd_put(LTTNG_FD_APPS, 2); |
| 316 | free(ua_chan->obj); |
| 317 | } |
| 318 | free(ua_chan); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * For a given application and session, push metadata to consumer. The session |
| 323 | * lock MUST be acquired here before calling this. |
| 324 | * |
| 325 | * Return 0 on success else a negative error. |
| 326 | */ |
| 327 | static int push_metadata(struct ust_app *app, struct ust_app_session *ua_sess) |
| 328 | { |
| 329 | int ret; |
| 330 | char *metadata_str = NULL; |
| 331 | size_t len, offset; |
| 332 | struct consumer_socket *socket; |
| 333 | |
| 334 | assert(app); |
| 335 | assert(ua_sess); |
| 336 | |
| 337 | if (!ua_sess->consumer || !ua_sess->metadata) { |
| 338 | /* No consumer means no stream associated so just return gracefully. */ |
| 339 | ret = 0; |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | rcu_read_lock(); |
| 344 | |
| 345 | /* Get consumer socket to use to push the metadata.*/ |
| 346 | socket = find_consumer_socket_by_bitness(app->bits_per_long, |
| 347 | ua_sess->consumer); |
| 348 | if (!socket) { |
| 349 | ret = -1; |
| 350 | goto error_rcu_unlock; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * TODO: Currently, we hold the socket lock around sampling of the next |
| 355 | * metadata segment to ensure we send metadata over the consumer socket in |
| 356 | * the correct order. This makes the registry lock nest inside the socket |
| 357 | * lock. |
| 358 | * |
| 359 | * Please note that this is a temporary measure: we should move this lock |
| 360 | * back into ust_consumer_push_metadata() when the consumer gets the |
| 361 | * ability to reorder the metadata it receives. |
| 362 | */ |
| 363 | pthread_mutex_lock(socket->lock); |
| 364 | pthread_mutex_lock(&ua_sess->registry.lock); |
| 365 | |
| 366 | offset = ua_sess->registry.metadata_len_sent; |
| 367 | len = ua_sess->registry.metadata_len - ua_sess->registry.metadata_len_sent; |
| 368 | if (len == 0) { |
| 369 | DBG3("No metadata to push for session id %d", ua_sess->id); |
| 370 | ret = 0; |
| 371 | goto error_reg_unlock; |
| 372 | } |
| 373 | assert(len > 0); |
| 374 | |
| 375 | /* Allocate only what we have to send. */ |
| 376 | metadata_str = zmalloc(len); |
| 377 | if (!metadata_str) { |
| 378 | PERROR("zmalloc ust app metadata string"); |
| 379 | ret = -ENOMEM; |
| 380 | goto error_reg_unlock; |
| 381 | } |
| 382 | /* Copy what we haven't send out. */ |
| 383 | memcpy(metadata_str, ua_sess->registry.metadata + offset, len); |
| 384 | |
| 385 | pthread_mutex_unlock(&ua_sess->registry.lock); |
| 386 | |
| 387 | ret = ust_consumer_push_metadata(socket, ua_sess, metadata_str, len, |
| 388 | offset); |
| 389 | if (ret < 0) { |
| 390 | pthread_mutex_unlock(socket->lock); |
| 391 | goto error_rcu_unlock; |
| 392 | } |
| 393 | |
| 394 | /* Update len sent of the registry. */ |
| 395 | pthread_mutex_lock(&ua_sess->registry.lock); |
| 396 | ua_sess->registry.metadata_len_sent += len; |
| 397 | pthread_mutex_unlock(&ua_sess->registry.lock); |
| 398 | pthread_mutex_unlock(socket->lock); |
| 399 | |
| 400 | rcu_read_unlock(); |
| 401 | free(metadata_str); |
| 402 | return 0; |
| 403 | |
| 404 | error_reg_unlock: |
| 405 | pthread_mutex_unlock(&ua_sess->registry.lock); |
| 406 | pthread_mutex_unlock(socket->lock); |
| 407 | error_rcu_unlock: |
| 408 | rcu_read_unlock(); |
| 409 | free(metadata_str); |
| 410 | end: |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Send to the consumer a close metadata command for the given session. Once |
| 416 | * done, the metadata channel is deleted and the session metadata pointer is |
| 417 | * nullified. The session lock MUST be acquired here unless the application is |
| 418 | * in the destroy path. |
| 419 | * |
| 420 | * Return 0 on success else a negative value. |
| 421 | */ |
| 422 | static int close_metadata(struct ust_app *app, struct ust_app_session *ua_sess) |
| 423 | { |
| 424 | int ret; |
| 425 | struct consumer_socket *socket; |
| 426 | |
| 427 | assert(app); |
| 428 | assert(ua_sess); |
| 429 | |
| 430 | /* Ignore if no metadata. Valid since it can be called on unregister. */ |
| 431 | if (!ua_sess->metadata) { |
| 432 | ret = 0; |
| 433 | goto error; |
| 434 | } |
| 435 | |
| 436 | rcu_read_lock(); |
| 437 | |
| 438 | /* Get consumer socket to use to push the metadata.*/ |
| 439 | socket = find_consumer_socket_by_bitness(app->bits_per_long, |
| 440 | ua_sess->consumer); |
| 441 | if (!socket) { |
| 442 | ret = -1; |
| 443 | goto error_rcu_unlock; |
| 444 | } |
| 445 | |
| 446 | ret = ust_consumer_close_metadata(socket, ua_sess->metadata); |
| 447 | if (ret < 0) { |
| 448 | goto error_rcu_unlock; |
| 449 | } |
| 450 | |
| 451 | error_rcu_unlock: |
| 452 | /* Destroy metadata on our side since we must not use it anymore. */ |
| 453 | delete_ust_app_channel(-1, ua_sess->metadata, app); |
| 454 | ua_sess->metadata = NULL; |
| 455 | |
| 456 | rcu_read_unlock(); |
| 457 | error: |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Delete ust app session safely. RCU read lock must be held before calling |
| 463 | * this function. |
| 464 | */ |
| 465 | static |
| 466 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
| 467 | struct ust_app *app) |
| 468 | { |
| 469 | int ret; |
| 470 | struct lttng_ht_iter iter; |
| 471 | struct ust_app_channel *ua_chan; |
| 472 | |
| 473 | assert(ua_sess); |
| 474 | |
| 475 | if (ua_sess->metadata) { |
| 476 | /* Push metadata for application before freeing the application. */ |
| 477 | (void) push_metadata(app, ua_sess); |
| 478 | |
| 479 | /* And ask to close it for this session. */ |
| 480 | (void) close_metadata(app, ua_sess); |
| 481 | } |
| 482 | |
| 483 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 484 | node.node) { |
| 485 | ret = lttng_ht_del(ua_sess->channels, &iter); |
| 486 | assert(!ret); |
| 487 | delete_ust_app_channel(sock, ua_chan, app); |
| 488 | } |
| 489 | lttng_ht_destroy(ua_sess->channels); |
| 490 | |
| 491 | ust_registry_session_destroy(&ua_sess->registry); |
| 492 | |
| 493 | if (ua_sess->handle != -1) { |
| 494 | ret = ustctl_release_handle(sock, ua_sess->handle); |
| 495 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 496 | ERR("UST app sock %d release session handle failed with ret %d", |
| 497 | sock, ret); |
| 498 | } |
| 499 | } |
| 500 | free(ua_sess); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Delete a traceable application structure from the global list. Never call |
| 505 | * this function outside of a call_rcu call. |
| 506 | */ |
| 507 | static |
| 508 | void delete_ust_app(struct ust_app *app) |
| 509 | { |
| 510 | int ret, sock; |
| 511 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
| 512 | |
| 513 | rcu_read_lock(); |
| 514 | |
| 515 | /* Delete ust app sessions info */ |
| 516 | sock = app->sock; |
| 517 | app->sock = -1; |
| 518 | |
| 519 | lttng_ht_destroy(app->sessions); |
| 520 | |
| 521 | /* Wipe sessions */ |
| 522 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
| 523 | teardown_node) { |
| 524 | /* Free every object in the session and the session. */ |
| 525 | delete_ust_app_session(sock, ua_sess, app); |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Wait until we have deleted the application from the sock hash table |
| 530 | * before closing this socket, otherwise an application could re-use the |
| 531 | * socket ID and race with the teardown, using the same hash table entry. |
| 532 | * |
| 533 | * It's OK to leave the close in call_rcu. We want it to stay unique for |
| 534 | * all RCU readers that could run concurrently with unregister app, |
| 535 | * therefore we _need_ to only close that socket after a grace period. So |
| 536 | * it should stay in this RCU callback. |
| 537 | * |
| 538 | * This close() is a very important step of the synchronization model so |
| 539 | * every modification to this function must be carefully reviewed. |
| 540 | */ |
| 541 | ret = close(sock); |
| 542 | if (ret) { |
| 543 | PERROR("close"); |
| 544 | } |
| 545 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 546 | |
| 547 | DBG2("UST app pid %d deleted", app->pid); |
| 548 | free(app); |
| 549 | |
| 550 | rcu_read_unlock(); |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * URCU intermediate call to delete an UST app. |
| 555 | */ |
| 556 | static |
| 557 | void delete_ust_app_rcu(struct rcu_head *head) |
| 558 | { |
| 559 | struct lttng_ht_node_ulong *node = |
| 560 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 561 | struct ust_app *app = |
| 562 | caa_container_of(node, struct ust_app, pid_n); |
| 563 | |
| 564 | DBG3("Call RCU deleting app PID %d", app->pid); |
| 565 | delete_ust_app(app); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Delete the session from the application ht and delete the data structure by |
| 570 | * freeing every object inside and releasing them. |
| 571 | */ |
| 572 | static void destroy_app_session(struct ust_app *app, |
| 573 | struct ust_app_session *ua_sess) |
| 574 | { |
| 575 | int ret; |
| 576 | struct lttng_ht_iter iter; |
| 577 | |
| 578 | assert(app); |
| 579 | assert(ua_sess); |
| 580 | |
| 581 | iter.iter.node = &ua_sess->node.node; |
| 582 | ret = lttng_ht_del(app->sessions, &iter); |
| 583 | if (ret) { |
| 584 | /* Already scheduled for teardown. */ |
| 585 | goto end; |
| 586 | } |
| 587 | |
| 588 | /* Once deleted, free the data structure. */ |
| 589 | delete_ust_app_session(app->sock, ua_sess, app); |
| 590 | |
| 591 | end: |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Alloc new UST app session. |
| 597 | */ |
| 598 | static |
| 599 | struct ust_app_session *alloc_ust_app_session(struct ust_app *app) |
| 600 | { |
| 601 | struct ust_app_session *ua_sess; |
| 602 | |
| 603 | /* Init most of the default value by allocating and zeroing */ |
| 604 | ua_sess = zmalloc(sizeof(struct ust_app_session)); |
| 605 | if (ua_sess == NULL) { |
| 606 | PERROR("malloc"); |
| 607 | goto error_free; |
| 608 | } |
| 609 | |
| 610 | ua_sess->handle = -1; |
| 611 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 612 | pthread_mutex_init(&ua_sess->lock, NULL); |
| 613 | if (ust_registry_session_init(&ua_sess->registry, app, |
| 614 | app->bits_per_long, |
| 615 | app->uint8_t_alignment, |
| 616 | app->uint16_t_alignment, |
| 617 | app->uint32_t_alignment, |
| 618 | app->uint64_t_alignment, |
| 619 | app->long_alignment, |
| 620 | app->byte_order)) { |
| 621 | goto error; |
| 622 | } |
| 623 | |
| 624 | return ua_sess; |
| 625 | |
| 626 | error: |
| 627 | free(ua_sess); |
| 628 | error_free: |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Alloc new UST app channel. |
| 634 | */ |
| 635 | static |
| 636 | struct ust_app_channel *alloc_ust_app_channel(char *name, |
| 637 | struct ust_app_session *ua_sess, |
| 638 | struct lttng_ust_channel_attr *attr) |
| 639 | { |
| 640 | struct ust_app_channel *ua_chan; |
| 641 | |
| 642 | /* Init most of the default value by allocating and zeroing */ |
| 643 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); |
| 644 | if (ua_chan == NULL) { |
| 645 | PERROR("malloc"); |
| 646 | goto error; |
| 647 | } |
| 648 | |
| 649 | /* Setup channel name */ |
| 650 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); |
| 651 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 652 | |
| 653 | ua_chan->enabled = 1; |
| 654 | ua_chan->handle = -1; |
| 655 | ua_chan->key = get_next_channel_key(); |
| 656 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 657 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 658 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); |
| 659 | |
| 660 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); |
| 661 | |
| 662 | /* Initialize UST registry. */ |
| 663 | ust_registry_channel_init(&ua_sess->registry, &ua_chan->registry); |
| 664 | |
| 665 | /* Copy attributes */ |
| 666 | if (attr) { |
| 667 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
| 668 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
| 669 | ua_chan->attr.num_subbuf = attr->num_subbuf; |
| 670 | ua_chan->attr.overwrite = attr->overwrite; |
| 671 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; |
| 672 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; |
| 673 | ua_chan->attr.output = attr->output; |
| 674 | } |
| 675 | /* By default, the channel is a per cpu channel. */ |
| 676 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; |
| 677 | |
| 678 | DBG3("UST app channel %s allocated", ua_chan->name); |
| 679 | |
| 680 | return ua_chan; |
| 681 | |
| 682 | error: |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Allocate and initialize a UST app stream. |
| 688 | * |
| 689 | * Return newly allocated stream pointer or NULL on error. |
| 690 | */ |
| 691 | struct ust_app_stream *ust_app_alloc_stream(void) |
| 692 | { |
| 693 | struct ust_app_stream *stream = NULL; |
| 694 | |
| 695 | stream = zmalloc(sizeof(*stream)); |
| 696 | if (stream == NULL) { |
| 697 | PERROR("zmalloc ust app stream"); |
| 698 | goto error; |
| 699 | } |
| 700 | |
| 701 | /* Zero could be a valid value for a handle so flag it to -1. */ |
| 702 | stream->handle = -1; |
| 703 | |
| 704 | error: |
| 705 | return stream; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Alloc new UST app event. |
| 710 | */ |
| 711 | static |
| 712 | struct ust_app_event *alloc_ust_app_event(char *name, |
| 713 | struct lttng_ust_event *attr) |
| 714 | { |
| 715 | struct ust_app_event *ua_event; |
| 716 | |
| 717 | /* Init most of the default value by allocating and zeroing */ |
| 718 | ua_event = zmalloc(sizeof(struct ust_app_event)); |
| 719 | if (ua_event == NULL) { |
| 720 | PERROR("malloc"); |
| 721 | goto error; |
| 722 | } |
| 723 | |
| 724 | ua_event->enabled = 1; |
| 725 | strncpy(ua_event->name, name, sizeof(ua_event->name)); |
| 726 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 727 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
| 728 | |
| 729 | /* Copy attributes */ |
| 730 | if (attr) { |
| 731 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); |
| 732 | } |
| 733 | |
| 734 | DBG3("UST app event %s allocated", ua_event->name); |
| 735 | |
| 736 | return ua_event; |
| 737 | |
| 738 | error: |
| 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Alloc new UST app context. |
| 744 | */ |
| 745 | static |
| 746 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx) |
| 747 | { |
| 748 | struct ust_app_ctx *ua_ctx; |
| 749 | |
| 750 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); |
| 751 | if (ua_ctx == NULL) { |
| 752 | goto error; |
| 753 | } |
| 754 | |
| 755 | if (uctx) { |
| 756 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); |
| 757 | } |
| 758 | |
| 759 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); |
| 760 | |
| 761 | error: |
| 762 | return ua_ctx; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Allocate a filter and copy the given original filter. |
| 767 | * |
| 768 | * Return allocated filter or NULL on error. |
| 769 | */ |
| 770 | static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter( |
| 771 | struct lttng_ust_filter_bytecode *orig_f) |
| 772 | { |
| 773 | struct lttng_ust_filter_bytecode *filter = NULL; |
| 774 | |
| 775 | /* Copy filter bytecode */ |
| 776 | filter = zmalloc(sizeof(*filter) + orig_f->len); |
| 777 | if (!filter) { |
| 778 | PERROR("zmalloc alloc ust app filter"); |
| 779 | goto error; |
| 780 | } |
| 781 | |
| 782 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
| 783 | |
| 784 | error: |
| 785 | return filter; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Find an ust_app using the sock and return it. RCU read side lock must be |
| 790 | * held before calling this helper function. |
| 791 | */ |
| 792 | static |
| 793 | struct ust_app *find_app_by_sock(int sock) |
| 794 | { |
| 795 | struct lttng_ht_node_ulong *node; |
| 796 | struct lttng_ht_iter iter; |
| 797 | |
| 798 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
| 799 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 800 | if (node == NULL) { |
| 801 | DBG2("UST app find by sock %d not found", sock); |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | return caa_container_of(node, struct ust_app, sock_n); |
| 806 | |
| 807 | error: |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Find an ust_app using the notify sock and return it. RCU read side lock must |
| 813 | * be held before calling this helper function. |
| 814 | */ |
| 815 | static struct ust_app *find_app_by_notify_sock(int sock) |
| 816 | { |
| 817 | struct lttng_ht_node_ulong *node; |
| 818 | struct lttng_ht_iter iter; |
| 819 | |
| 820 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), |
| 821 | &iter); |
| 822 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 823 | if (node == NULL) { |
| 824 | DBG2("UST app find by notify sock %d not found", sock); |
| 825 | goto error; |
| 826 | } |
| 827 | |
| 828 | return caa_container_of(node, struct ust_app, notify_sock_n); |
| 829 | |
| 830 | error: |
| 831 | return NULL; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Lookup for an ust app event based on event name, filter bytecode and the |
| 836 | * event loglevel. |
| 837 | * |
| 838 | * Return an ust_app_event object or NULL on error. |
| 839 | */ |
| 840 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
| 841 | char *name, struct lttng_ust_filter_bytecode *filter, int loglevel) |
| 842 | { |
| 843 | struct lttng_ht_iter iter; |
| 844 | struct lttng_ht_node_str *node; |
| 845 | struct ust_app_event *event = NULL; |
| 846 | struct ust_app_ht_key key; |
| 847 | |
| 848 | assert(name); |
| 849 | assert(ht); |
| 850 | |
| 851 | /* Setup key for event lookup. */ |
| 852 | key.name = name; |
| 853 | key.filter = filter; |
| 854 | key.loglevel = loglevel; |
| 855 | |
| 856 | /* Lookup using the event name as hash and a custom match fct. */ |
| 857 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), |
| 858 | ht_match_ust_app_event, &key, &iter.iter); |
| 859 | node = lttng_ht_iter_get_node_str(&iter); |
| 860 | if (node == NULL) { |
| 861 | goto end; |
| 862 | } |
| 863 | |
| 864 | event = caa_container_of(node, struct ust_app_event, node); |
| 865 | |
| 866 | end: |
| 867 | return event; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Create the channel context on the tracer. |
| 872 | * |
| 873 | * Called with UST app session lock held. |
| 874 | */ |
| 875 | static |
| 876 | int create_ust_channel_context(struct ust_app_channel *ua_chan, |
| 877 | struct ust_app_ctx *ua_ctx, struct ust_app *app) |
| 878 | { |
| 879 | int ret; |
| 880 | |
| 881 | health_code_update(); |
| 882 | |
| 883 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
| 884 | ua_chan->obj, &ua_ctx->obj); |
| 885 | if (ret < 0) { |
| 886 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 887 | ERR("UST app create channel context failed for app (pid: %d) " |
| 888 | "with ret %d", app->pid, ret); |
| 889 | } else { |
| 890 | DBG3("UST app disable event failed. Application is dead."); |
| 891 | } |
| 892 | goto error; |
| 893 | } |
| 894 | |
| 895 | ua_ctx->handle = ua_ctx->obj->handle; |
| 896 | |
| 897 | DBG2("UST app context handle %d created successfully for channel %s", |
| 898 | ua_ctx->handle, ua_chan->name); |
| 899 | |
| 900 | error: |
| 901 | health_code_update(); |
| 902 | return ret; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Set the filter on the tracer. |
| 907 | */ |
| 908 | static |
| 909 | int set_ust_event_filter(struct ust_app_event *ua_event, |
| 910 | struct ust_app *app) |
| 911 | { |
| 912 | int ret; |
| 913 | |
| 914 | health_code_update(); |
| 915 | |
| 916 | if (!ua_event->filter) { |
| 917 | ret = 0; |
| 918 | goto error; |
| 919 | } |
| 920 | |
| 921 | ret = ustctl_set_filter(app->sock, ua_event->filter, |
| 922 | ua_event->obj); |
| 923 | if (ret < 0) { |
| 924 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 925 | ERR("UST app event %s filter failed for app (pid: %d) " |
| 926 | "with ret %d", ua_event->attr.name, app->pid, ret); |
| 927 | } else { |
| 928 | DBG3("UST app filter event failed. Application is dead."); |
| 929 | } |
| 930 | goto error; |
| 931 | } |
| 932 | |
| 933 | DBG2("UST filter set successfully for event %s", ua_event->name); |
| 934 | |
| 935 | error: |
| 936 | health_code_update(); |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Disable the specified event on to UST tracer for the UST session. |
| 942 | */ |
| 943 | static int disable_ust_event(struct ust_app *app, |
| 944 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) |
| 945 | { |
| 946 | int ret; |
| 947 | |
| 948 | health_code_update(); |
| 949 | |
| 950 | ret = ustctl_disable(app->sock, ua_event->obj); |
| 951 | if (ret < 0) { |
| 952 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 953 | ERR("UST app event %s disable failed for app (pid: %d) " |
| 954 | "and session handle %d with ret %d", |
| 955 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
| 956 | } else { |
| 957 | DBG3("UST app disable event failed. Application is dead."); |
| 958 | } |
| 959 | goto error; |
| 960 | } |
| 961 | |
| 962 | DBG2("UST app event %s disabled successfully for app (pid: %d)", |
| 963 | ua_event->attr.name, app->pid); |
| 964 | |
| 965 | error: |
| 966 | health_code_update(); |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Disable the specified channel on to UST tracer for the UST session. |
| 972 | */ |
| 973 | static int disable_ust_channel(struct ust_app *app, |
| 974 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 975 | { |
| 976 | int ret; |
| 977 | |
| 978 | health_code_update(); |
| 979 | |
| 980 | ret = ustctl_disable(app->sock, ua_chan->obj); |
| 981 | if (ret < 0) { |
| 982 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 983 | ERR("UST app channel %s disable failed for app (pid: %d) " |
| 984 | "and session handle %d with ret %d", |
| 985 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 986 | } else { |
| 987 | DBG3("UST app disable channel failed. Application is dead."); |
| 988 | } |
| 989 | goto error; |
| 990 | } |
| 991 | |
| 992 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
| 993 | ua_chan->name, app->pid); |
| 994 | |
| 995 | error: |
| 996 | health_code_update(); |
| 997 | return ret; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Enable the specified channel on to UST tracer for the UST session. |
| 1002 | */ |
| 1003 | static int enable_ust_channel(struct ust_app *app, |
| 1004 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) |
| 1005 | { |
| 1006 | int ret; |
| 1007 | |
| 1008 | health_code_update(); |
| 1009 | |
| 1010 | ret = ustctl_enable(app->sock, ua_chan->obj); |
| 1011 | if (ret < 0) { |
| 1012 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1013 | ERR("UST app channel %s enable failed for app (pid: %d) " |
| 1014 | "and session handle %d with ret %d", |
| 1015 | ua_chan->name, app->pid, ua_sess->handle, ret); |
| 1016 | } else { |
| 1017 | DBG3("UST app enable channel failed. Application is dead."); |
| 1018 | } |
| 1019 | goto error; |
| 1020 | } |
| 1021 | |
| 1022 | ua_chan->enabled = 1; |
| 1023 | |
| 1024 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", |
| 1025 | ua_chan->name, app->pid); |
| 1026 | |
| 1027 | error: |
| 1028 | health_code_update(); |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Enable the specified event on to UST tracer for the UST session. |
| 1034 | */ |
| 1035 | static int enable_ust_event(struct ust_app *app, |
| 1036 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) |
| 1037 | { |
| 1038 | int ret; |
| 1039 | |
| 1040 | health_code_update(); |
| 1041 | |
| 1042 | ret = ustctl_enable(app->sock, ua_event->obj); |
| 1043 | if (ret < 0) { |
| 1044 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1045 | ERR("UST app event %s enable failed for app (pid: %d) " |
| 1046 | "and session handle %d with ret %d", |
| 1047 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
| 1048 | } else { |
| 1049 | DBG3("UST app enable event failed. Application is dead."); |
| 1050 | } |
| 1051 | goto error; |
| 1052 | } |
| 1053 | |
| 1054 | DBG2("UST app event %s enabled successfully for app (pid: %d)", |
| 1055 | ua_event->attr.name, app->pid); |
| 1056 | |
| 1057 | error: |
| 1058 | health_code_update(); |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Create the specified channel onto the UST tracer for a UST session. This |
| 1064 | * MUST be called with UST app session lock held. |
| 1065 | * |
| 1066 | * Return 0 on success. On error, a negative value is returned. |
| 1067 | */ |
| 1068 | static int create_ust_channel(struct ust_app *app, |
| 1069 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, |
| 1070 | struct consumer_output *consumer) |
| 1071 | { |
| 1072 | int ret; |
| 1073 | unsigned int nb_fd = 0; |
| 1074 | struct consumer_socket *socket; |
| 1075 | struct ust_app_stream *stream, *stmp; |
| 1076 | |
| 1077 | assert(app); |
| 1078 | assert(ua_sess); |
| 1079 | assert(ua_chan); |
| 1080 | assert(consumer); |
| 1081 | |
| 1082 | rcu_read_lock(); |
| 1083 | health_code_update(); |
| 1084 | |
| 1085 | /* Get the right consumer socket for the application. */ |
| 1086 | socket = find_consumer_socket_by_bitness(app->bits_per_long, consumer); |
| 1087 | if (!socket) { |
| 1088 | ret = -1; |
| 1089 | goto error; |
| 1090 | } |
| 1091 | |
| 1092 | health_code_update(); |
| 1093 | |
| 1094 | /* |
| 1095 | * Ask consumer to create channel. The consumer will return the number of |
| 1096 | * stream we have to expect. |
| 1097 | */ |
| 1098 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, consumer, socket); |
| 1099 | if (ret < 0) { |
| 1100 | goto error; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Compute the number of fd needed before receiving them. It must be 2 per |
| 1105 | * stream (2 being the default value here). |
| 1106 | */ |
| 1107 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; |
| 1108 | |
| 1109 | /* Reserve the amount of file descriptor we need. */ |
| 1110 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); |
| 1111 | if (ret < 0) { |
| 1112 | ERR("Exhausted number of available FD upon create channel"); |
| 1113 | goto error_fd_get; |
| 1114 | } |
| 1115 | |
| 1116 | health_code_update(); |
| 1117 | |
| 1118 | /* |
| 1119 | * Now get the channel from the consumer. This call wil populate the stream |
| 1120 | * list of that channel and set the ust object. |
| 1121 | */ |
| 1122 | ret = ust_consumer_get_channel(socket, ua_chan); |
| 1123 | if (ret < 0) { |
| 1124 | goto error_destroy; |
| 1125 | } |
| 1126 | |
| 1127 | /* Send channel to the application. */ |
| 1128 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); |
| 1129 | if (ret < 0) { |
| 1130 | goto error; |
| 1131 | } |
| 1132 | |
| 1133 | health_code_update(); |
| 1134 | |
| 1135 | /* Send all streams to application. */ |
| 1136 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
| 1137 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); |
| 1138 | if (ret < 0) { |
| 1139 | goto error; |
| 1140 | } |
| 1141 | /* We don't need the stream anymore once sent to the tracer. */ |
| 1142 | cds_list_del(&stream->list); |
| 1143 | delete_ust_app_stream(-1, stream); |
| 1144 | } |
| 1145 | |
| 1146 | /* Flag the channel that it is sent to the application. */ |
| 1147 | ua_chan->is_sent = 1; |
| 1148 | /* Assign session to channel. */ |
| 1149 | ua_chan->session = ua_sess; |
| 1150 | /* Initialize ust objd object using the received handle and add it. */ |
| 1151 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); |
| 1152 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); |
| 1153 | |
| 1154 | health_code_update(); |
| 1155 | |
| 1156 | /* If channel is not enabled, disable it on the tracer */ |
| 1157 | if (!ua_chan->enabled) { |
| 1158 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 1159 | if (ret < 0) { |
| 1160 | goto error; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | rcu_read_unlock(); |
| 1165 | return 0; |
| 1166 | |
| 1167 | error_destroy: |
| 1168 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); |
| 1169 | error_fd_get: |
| 1170 | /* |
| 1171 | * Initiate a destroy channel on the consumer since we had an error |
| 1172 | * handling it on our side. The return value is of no importance since we |
| 1173 | * already have a ret value set by the previous error that we need to |
| 1174 | * return. |
| 1175 | */ |
| 1176 | (void) ust_consumer_destroy_channel(socket, ua_chan); |
| 1177 | error: |
| 1178 | health_code_update(); |
| 1179 | rcu_read_unlock(); |
| 1180 | return ret; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Create the specified event onto the UST tracer for a UST session. |
| 1185 | * |
| 1186 | * Should be called with session mutex held. |
| 1187 | */ |
| 1188 | static |
| 1189 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, |
| 1190 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
| 1191 | { |
| 1192 | int ret = 0; |
| 1193 | |
| 1194 | health_code_update(); |
| 1195 | |
| 1196 | /* Create UST event on tracer */ |
| 1197 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
| 1198 | &ua_event->obj); |
| 1199 | if (ret < 0) { |
| 1200 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1201 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
| 1202 | ua_event->attr.name, app->pid, ret); |
| 1203 | } else { |
| 1204 | DBG3("UST app create event failed. Application is dead."); |
| 1205 | } |
| 1206 | goto error; |
| 1207 | } |
| 1208 | |
| 1209 | ua_event->handle = ua_event->obj->handle; |
| 1210 | |
| 1211 | DBG2("UST app event %s created successfully for pid:%d", |
| 1212 | ua_event->attr.name, app->pid); |
| 1213 | |
| 1214 | health_code_update(); |
| 1215 | |
| 1216 | /* Set filter if one is present. */ |
| 1217 | if (ua_event->filter) { |
| 1218 | ret = set_ust_event_filter(ua_event, app); |
| 1219 | if (ret < 0) { |
| 1220 | goto error; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /* If event not enabled, disable it on the tracer */ |
| 1225 | if (ua_event->enabled == 0) { |
| 1226 | ret = disable_ust_event(app, ua_sess, ua_event); |
| 1227 | if (ret < 0) { |
| 1228 | /* |
| 1229 | * If we hit an EPERM, something is wrong with our disable call. If |
| 1230 | * we get an EEXIST, there is a problem on the tracer side since we |
| 1231 | * just created it. |
| 1232 | */ |
| 1233 | switch (ret) { |
| 1234 | case -LTTNG_UST_ERR_PERM: |
| 1235 | /* Code flow problem */ |
| 1236 | assert(0); |
| 1237 | case -LTTNG_UST_ERR_EXIST: |
| 1238 | /* It's OK for our use case. */ |
| 1239 | ret = 0; |
| 1240 | break; |
| 1241 | default: |
| 1242 | break; |
| 1243 | } |
| 1244 | goto error; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | error: |
| 1249 | health_code_update(); |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | /* |
| 1254 | * Copy data between an UST app event and a LTT event. |
| 1255 | */ |
| 1256 | static void shadow_copy_event(struct ust_app_event *ua_event, |
| 1257 | struct ltt_ust_event *uevent) |
| 1258 | { |
| 1259 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
| 1260 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; |
| 1261 | |
| 1262 | ua_event->enabled = uevent->enabled; |
| 1263 | |
| 1264 | /* Copy event attributes */ |
| 1265 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); |
| 1266 | |
| 1267 | /* Copy filter bytecode */ |
| 1268 | if (uevent->filter) { |
| 1269 | ua_event->filter = alloc_copy_ust_app_filter(uevent->filter); |
| 1270 | /* Filter might be NULL here in case of ENONEM. */ |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Copy data between an UST app channel and a LTT channel. |
| 1276 | */ |
| 1277 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
| 1278 | struct ltt_ust_channel *uchan) |
| 1279 | { |
| 1280 | struct lttng_ht_iter iter; |
| 1281 | struct ltt_ust_event *uevent; |
| 1282 | struct ltt_ust_context *uctx; |
| 1283 | struct ust_app_event *ua_event; |
| 1284 | struct ust_app_ctx *ua_ctx; |
| 1285 | |
| 1286 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
| 1287 | |
| 1288 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); |
| 1289 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; |
| 1290 | |
| 1291 | /* Copy event attributes since the layout is different. */ |
| 1292 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; |
| 1293 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; |
| 1294 | ua_chan->attr.overwrite = uchan->attr.overwrite; |
| 1295 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; |
| 1296 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; |
| 1297 | ua_chan->attr.output = uchan->attr.output; |
| 1298 | /* |
| 1299 | * Note that the attribute channel type is not set since the channel on the |
| 1300 | * tracing registry side does not have this information. |
| 1301 | */ |
| 1302 | |
| 1303 | ua_chan->enabled = uchan->enabled; |
| 1304 | |
| 1305 | cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) { |
| 1306 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
| 1307 | if (ua_ctx == NULL) { |
| 1308 | continue; |
| 1309 | } |
| 1310 | lttng_ht_node_init_ulong(&ua_ctx->node, |
| 1311 | (unsigned long) ua_ctx->ctx.ctx); |
| 1312 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); |
| 1313 | } |
| 1314 | |
| 1315 | /* Copy all events from ltt ust channel to ust app channel */ |
| 1316 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
| 1317 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 1318 | uevent->filter, uevent->attr.loglevel); |
| 1319 | if (ua_event == NULL) { |
| 1320 | DBG2("UST event %s not found on shadow copy channel", |
| 1321 | uevent->attr.name); |
| 1322 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 1323 | if (ua_event == NULL) { |
| 1324 | continue; |
| 1325 | } |
| 1326 | shadow_copy_event(ua_event, uevent); |
| 1327 | add_unique_ust_app_event(ua_chan, ua_event); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
| 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * Copy data between a UST app session and a regular LTT session. |
| 1336 | */ |
| 1337 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
| 1338 | struct ltt_ust_session *usess, struct ust_app *app) |
| 1339 | { |
| 1340 | struct lttng_ht_node_str *ua_chan_node; |
| 1341 | struct lttng_ht_iter iter; |
| 1342 | struct ltt_ust_channel *uchan; |
| 1343 | struct ust_app_channel *ua_chan; |
| 1344 | time_t rawtime; |
| 1345 | struct tm *timeinfo; |
| 1346 | char datetime[16]; |
| 1347 | int ret; |
| 1348 | |
| 1349 | /* Get date and time for unique app path */ |
| 1350 | time(&rawtime); |
| 1351 | timeinfo = localtime(&rawtime); |
| 1352 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 1353 | |
| 1354 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
| 1355 | |
| 1356 | ua_sess->id = usess->id; |
| 1357 | ua_sess->uid = usess->uid; |
| 1358 | ua_sess->gid = usess->gid; |
| 1359 | |
| 1360 | ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid, |
| 1361 | datetime); |
| 1362 | if (ret < 0) { |
| 1363 | PERROR("asprintf UST shadow copy session"); |
| 1364 | /* TODO: We cannot return an error from here.. */ |
| 1365 | assert(0); |
| 1366 | } |
| 1367 | |
| 1368 | /* TODO: support all UST domain */ |
| 1369 | |
| 1370 | /* Iterate over all channels in global domain. */ |
| 1371 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
| 1372 | uchan, node.node) { |
| 1373 | struct lttng_ht_iter uiter; |
| 1374 | |
| 1375 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 1376 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 1377 | if (ua_chan_node != NULL) { |
| 1378 | /* Session exist. Contiuing. */ |
| 1379 | continue; |
| 1380 | } |
| 1381 | |
| 1382 | DBG2("Channel %s not found on shadow session copy, creating it", |
| 1383 | uchan->name); |
| 1384 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 1385 | if (ua_chan == NULL) { |
| 1386 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
| 1387 | continue; |
| 1388 | } |
| 1389 | shadow_copy_channel(ua_chan, uchan); |
| 1390 | /* |
| 1391 | * The concept of metadata channel does not exist on the tracing |
| 1392 | * registry side of the session daemon so this can only be a per CPU |
| 1393 | * channel and not metadata. |
| 1394 | */ |
| 1395 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; |
| 1396 | |
| 1397 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Lookup sesison wrapper. |
| 1403 | */ |
| 1404 | static |
| 1405 | void __lookup_session_by_app(struct ltt_ust_session *usess, |
| 1406 | struct ust_app *app, struct lttng_ht_iter *iter) |
| 1407 | { |
| 1408 | /* Get right UST app session from app */ |
| 1409 | lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter); |
| 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * Return ust app session from the app session hashtable using the UST session |
| 1414 | * id. |
| 1415 | */ |
| 1416 | static struct ust_app_session *lookup_session_by_app( |
| 1417 | struct ltt_ust_session *usess, struct ust_app *app) |
| 1418 | { |
| 1419 | struct lttng_ht_iter iter; |
| 1420 | struct lttng_ht_node_ulong *node; |
| 1421 | |
| 1422 | __lookup_session_by_app(usess, app, &iter); |
| 1423 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1424 | if (node == NULL) { |
| 1425 | goto error; |
| 1426 | } |
| 1427 | |
| 1428 | return caa_container_of(node, struct ust_app_session, node); |
| 1429 | |
| 1430 | error: |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * Create a session on the tracer side for the given app. |
| 1436 | * |
| 1437 | * On success, ua_sess_ptr is populated with the session pointer or else left |
| 1438 | * untouched. If the session was created, is_created is set to 1. On error, |
| 1439 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can |
| 1440 | * be NULL. |
| 1441 | * |
| 1442 | * Returns 0 on success or else a negative code which is either -ENOMEM or |
| 1443 | * -ENOTCONN which is the default code if the ustctl_create_session fails. |
| 1444 | */ |
| 1445 | static int create_ust_app_session(struct ltt_ust_session *usess, |
| 1446 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
| 1447 | int *is_created) |
| 1448 | { |
| 1449 | int ret, created = 0; |
| 1450 | struct ust_app_session *ua_sess; |
| 1451 | |
| 1452 | assert(usess); |
| 1453 | assert(app); |
| 1454 | assert(ua_sess_ptr); |
| 1455 | |
| 1456 | health_code_update(); |
| 1457 | |
| 1458 | ua_sess = lookup_session_by_app(usess, app); |
| 1459 | if (ua_sess == NULL) { |
| 1460 | DBG2("UST app pid: %d session id %d not found, creating it", |
| 1461 | app->pid, usess->id); |
| 1462 | ua_sess = alloc_ust_app_session(app); |
| 1463 | if (ua_sess == NULL) { |
| 1464 | /* Only malloc can failed so something is really wrong */ |
| 1465 | ret = -ENOMEM; |
| 1466 | goto error; |
| 1467 | } |
| 1468 | shadow_copy_session(ua_sess, usess, app); |
| 1469 | created = 1; |
| 1470 | } |
| 1471 | |
| 1472 | health_code_update(); |
| 1473 | |
| 1474 | if (ua_sess->handle == -1) { |
| 1475 | ret = ustctl_create_session(app->sock); |
| 1476 | if (ret < 0) { |
| 1477 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 1478 | ERR("Creating session for app pid %d with ret %d", |
| 1479 | app->pid, ret); |
| 1480 | } else { |
| 1481 | DBG("UST app creating session failed. Application is dead"); |
| 1482 | } |
| 1483 | delete_ust_app_session(-1, ua_sess, app); |
| 1484 | if (ret != -ENOMEM) { |
| 1485 | /* |
| 1486 | * Tracer is probably gone or got an internal error so let's |
| 1487 | * behave like it will soon unregister or not usable. |
| 1488 | */ |
| 1489 | ret = -ENOTCONN; |
| 1490 | } |
| 1491 | goto error; |
| 1492 | } |
| 1493 | |
| 1494 | ua_sess->handle = ret; |
| 1495 | |
| 1496 | /* Add ust app session to app's HT */ |
| 1497 | lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id); |
| 1498 | lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node); |
| 1499 | |
| 1500 | DBG2("UST app session created successfully with handle %d", ret); |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Assign consumer if not already set. For one application, there is only |
| 1505 | * one possible consumer has of now. |
| 1506 | */ |
| 1507 | if (!ua_sess->consumer) { |
| 1508 | ua_sess->consumer = usess->consumer; |
| 1509 | } |
| 1510 | |
| 1511 | *ua_sess_ptr = ua_sess; |
| 1512 | if (is_created) { |
| 1513 | *is_created = created; |
| 1514 | } |
| 1515 | |
| 1516 | /* Everything went well. */ |
| 1517 | ret = 0; |
| 1518 | |
| 1519 | error: |
| 1520 | health_code_update(); |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * Create a context for the channel on the tracer. |
| 1526 | * |
| 1527 | * Called with UST app session lock held. |
| 1528 | */ |
| 1529 | static |
| 1530 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, |
| 1531 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, |
| 1532 | struct ust_app *app) |
| 1533 | { |
| 1534 | int ret = 0; |
| 1535 | struct lttng_ht_iter iter; |
| 1536 | struct lttng_ht_node_ulong *node; |
| 1537 | struct ust_app_ctx *ua_ctx; |
| 1538 | |
| 1539 | DBG2("UST app adding context to channel %s", ua_chan->name); |
| 1540 | |
| 1541 | lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter); |
| 1542 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1543 | if (node != NULL) { |
| 1544 | ret = -EEXIST; |
| 1545 | goto error; |
| 1546 | } |
| 1547 | |
| 1548 | ua_ctx = alloc_ust_app_ctx(uctx); |
| 1549 | if (ua_ctx == NULL) { |
| 1550 | /* malloc failed */ |
| 1551 | ret = -1; |
| 1552 | goto error; |
| 1553 | } |
| 1554 | |
| 1555 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
| 1556 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); |
| 1557 | |
| 1558 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 1559 | if (ret < 0) { |
| 1560 | goto error; |
| 1561 | } |
| 1562 | |
| 1563 | error: |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Enable on the tracer side a ust app event for the session and channel. |
| 1569 | * |
| 1570 | * Called with UST app session lock held. |
| 1571 | */ |
| 1572 | static |
| 1573 | int enable_ust_app_event(struct ust_app_session *ua_sess, |
| 1574 | struct ust_app_event *ua_event, struct ust_app *app) |
| 1575 | { |
| 1576 | int ret; |
| 1577 | |
| 1578 | ret = enable_ust_event(app, ua_sess, ua_event); |
| 1579 | if (ret < 0) { |
| 1580 | goto error; |
| 1581 | } |
| 1582 | |
| 1583 | ua_event->enabled = 1; |
| 1584 | |
| 1585 | error: |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * Disable on the tracer side a ust app event for the session and channel. |
| 1591 | */ |
| 1592 | static int disable_ust_app_event(struct ust_app_session *ua_sess, |
| 1593 | struct ust_app_event *ua_event, struct ust_app *app) |
| 1594 | { |
| 1595 | int ret; |
| 1596 | |
| 1597 | ret = disable_ust_event(app, ua_sess, ua_event); |
| 1598 | if (ret < 0) { |
| 1599 | goto error; |
| 1600 | } |
| 1601 | |
| 1602 | ua_event->enabled = 0; |
| 1603 | |
| 1604 | error: |
| 1605 | return ret; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Lookup ust app channel for session and disable it on the tracer side. |
| 1610 | */ |
| 1611 | static |
| 1612 | int disable_ust_app_channel(struct ust_app_session *ua_sess, |
| 1613 | struct ust_app_channel *ua_chan, struct ust_app *app) |
| 1614 | { |
| 1615 | int ret; |
| 1616 | |
| 1617 | ret = disable_ust_channel(app, ua_sess, ua_chan); |
| 1618 | if (ret < 0) { |
| 1619 | goto error; |
| 1620 | } |
| 1621 | |
| 1622 | ua_chan->enabled = 0; |
| 1623 | |
| 1624 | error: |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * Lookup ust app channel for session and enable it on the tracer side. |
| 1630 | */ |
| 1631 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, |
| 1632 | struct ltt_ust_channel *uchan, struct ust_app *app) |
| 1633 | { |
| 1634 | int ret = 0; |
| 1635 | struct lttng_ht_iter iter; |
| 1636 | struct lttng_ht_node_str *ua_chan_node; |
| 1637 | struct ust_app_channel *ua_chan; |
| 1638 | |
| 1639 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 1640 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 1641 | if (ua_chan_node == NULL) { |
| 1642 | DBG2("Unable to find channel %s in ust session id %u", |
| 1643 | uchan->name, ua_sess->id); |
| 1644 | goto error; |
| 1645 | } |
| 1646 | |
| 1647 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 1648 | |
| 1649 | ret = enable_ust_channel(app, ua_sess, ua_chan); |
| 1650 | if (ret < 0) { |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | error: |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * Create UST app channel and create it on the tracer. Set ua_chanp of the |
| 1660 | * newly created channel if not NULL. |
| 1661 | * |
| 1662 | * Called with UST app session lock held. |
| 1663 | */ |
| 1664 | static int create_ust_app_channel(struct ust_app_session *ua_sess, |
| 1665 | struct ltt_ust_channel *uchan, struct ust_app *app, |
| 1666 | struct consumer_output *consumer, enum lttng_ust_chan_type type, |
| 1667 | struct ust_app_channel **ua_chanp) |
| 1668 | { |
| 1669 | int ret = 0; |
| 1670 | struct lttng_ht_iter iter; |
| 1671 | struct lttng_ht_node_str *ua_chan_node; |
| 1672 | struct ust_app_channel *ua_chan; |
| 1673 | |
| 1674 | /* Lookup channel in the ust app session */ |
| 1675 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 1676 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 1677 | if (ua_chan_node != NULL) { |
| 1678 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 1679 | goto end; |
| 1680 | } |
| 1681 | |
| 1682 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
| 1683 | if (ua_chan == NULL) { |
| 1684 | /* Only malloc can fail here */ |
| 1685 | ret = -ENOMEM; |
| 1686 | goto error; |
| 1687 | } |
| 1688 | shadow_copy_channel(ua_chan, uchan); |
| 1689 | |
| 1690 | /* Set channel type. */ |
| 1691 | ua_chan->attr.type = type; |
| 1692 | |
| 1693 | ret = create_ust_channel(app, ua_sess, ua_chan, consumer); |
| 1694 | if (ret < 0) { |
| 1695 | goto error; |
| 1696 | } |
| 1697 | |
| 1698 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
| 1699 | app->pid); |
| 1700 | |
| 1701 | /* Only add the channel if successful on the tracer side. */ |
| 1702 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
| 1703 | |
| 1704 | end: |
| 1705 | if (ua_chanp) { |
| 1706 | *ua_chanp = ua_chan; |
| 1707 | } |
| 1708 | |
| 1709 | /* Everything went well. */ |
| 1710 | return 0; |
| 1711 | |
| 1712 | error: |
| 1713 | delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app); |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Create UST app event and create it on the tracer side. |
| 1719 | * |
| 1720 | * Called with ust app session mutex held. |
| 1721 | */ |
| 1722 | static |
| 1723 | int create_ust_app_event(struct ust_app_session *ua_sess, |
| 1724 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, |
| 1725 | struct ust_app *app) |
| 1726 | { |
| 1727 | int ret = 0; |
| 1728 | struct ust_app_event *ua_event; |
| 1729 | |
| 1730 | /* Get event node */ |
| 1731 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 1732 | uevent->filter, uevent->attr.loglevel); |
| 1733 | if (ua_event != NULL) { |
| 1734 | ret = -EEXIST; |
| 1735 | goto end; |
| 1736 | } |
| 1737 | |
| 1738 | /* Does not exist so create one */ |
| 1739 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
| 1740 | if (ua_event == NULL) { |
| 1741 | /* Only malloc can failed so something is really wrong */ |
| 1742 | ret = -ENOMEM; |
| 1743 | goto end; |
| 1744 | } |
| 1745 | shadow_copy_event(ua_event, uevent); |
| 1746 | |
| 1747 | /* Create it on the tracer side */ |
| 1748 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
| 1749 | if (ret < 0) { |
| 1750 | /* Not found previously means that it does not exist on the tracer */ |
| 1751 | assert(ret != -LTTNG_UST_ERR_EXIST); |
| 1752 | goto error; |
| 1753 | } |
| 1754 | |
| 1755 | add_unique_ust_app_event(ua_chan, ua_event); |
| 1756 | |
| 1757 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
| 1758 | app->pid); |
| 1759 | |
| 1760 | end: |
| 1761 | return ret; |
| 1762 | |
| 1763 | error: |
| 1764 | /* Valid. Calling here is already in a read side lock */ |
| 1765 | delete_ust_app_event(-1, ua_event); |
| 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Create UST metadata and open it on the tracer side. |
| 1771 | * |
| 1772 | * Called with UST app session lock held. |
| 1773 | */ |
| 1774 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, |
| 1775 | struct ust_app *app, struct consumer_output *consumer) |
| 1776 | { |
| 1777 | int ret = 0; |
| 1778 | struct ust_app_channel *metadata; |
| 1779 | struct consumer_socket *socket; |
| 1780 | |
| 1781 | assert(ua_sess); |
| 1782 | assert(app); |
| 1783 | assert(consumer); |
| 1784 | |
| 1785 | if (ua_sess->metadata) { |
| 1786 | /* Already exist. Return success. */ |
| 1787 | goto end; |
| 1788 | } |
| 1789 | |
| 1790 | /* Allocate UST metadata */ |
| 1791 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
| 1792 | if (!metadata) { |
| 1793 | /* malloc() failed */ |
| 1794 | ret = -ENOMEM; |
| 1795 | goto error; |
| 1796 | } |
| 1797 | |
| 1798 | /* Set default attributes for metadata. */ |
| 1799 | metadata->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 1800 | metadata->attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 1801 | metadata->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 1802 | metadata->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 1803 | metadata->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 1804 | metadata->attr.output = LTTNG_UST_MMAP; |
| 1805 | metadata->attr.type = LTTNG_UST_CHAN_METADATA; |
| 1806 | |
| 1807 | /* Get the right consumer socket for the application. */ |
| 1808 | socket = find_consumer_socket_by_bitness(app->bits_per_long, consumer); |
| 1809 | if (!socket) { |
| 1810 | ret = -EINVAL; |
| 1811 | goto error_consumer; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Ask the metadata channel creation to the consumer. The metadata object |
| 1816 | * will be created by the consumer and kept their. However, the stream is |
| 1817 | * never added or monitored until we do a first push metadata to the |
| 1818 | * consumer. |
| 1819 | */ |
| 1820 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket); |
| 1821 | if (ret < 0) { |
| 1822 | goto error_consumer; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * The setup command will make the metadata stream be sent to the relayd, |
| 1827 | * if applicable, and the thread managing the metadatas. This is important |
| 1828 | * because after this point, if an error occurs, the only way the stream |
| 1829 | * can be deleted is to be monitored in the consumer. |
| 1830 | */ |
| 1831 | ret = ust_consumer_setup_metadata(socket, metadata); |
| 1832 | if (ret < 0) { |
| 1833 | goto error_consumer; |
| 1834 | } |
| 1835 | |
| 1836 | ua_sess->metadata = metadata; |
| 1837 | |
| 1838 | DBG2("UST metadata created for app pid %d", app->pid); |
| 1839 | |
| 1840 | end: |
| 1841 | return 0; |
| 1842 | error_consumer: |
| 1843 | delete_ust_app_channel(-1, metadata, app); |
| 1844 | error: |
| 1845 | return ret; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Return pointer to traceable apps list. |
| 1850 | */ |
| 1851 | struct lttng_ht *ust_app_get_ht(void) |
| 1852 | { |
| 1853 | return ust_app_ht; |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
| 1858 | * acquired before calling this function. |
| 1859 | */ |
| 1860 | struct ust_app *ust_app_find_by_pid(pid_t pid) |
| 1861 | { |
| 1862 | struct ust_app *app = NULL; |
| 1863 | struct lttng_ht_node_ulong *node; |
| 1864 | struct lttng_ht_iter iter; |
| 1865 | |
| 1866 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
| 1867 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1868 | if (node == NULL) { |
| 1869 | DBG2("UST app no found with pid %d", pid); |
| 1870 | goto error; |
| 1871 | } |
| 1872 | |
| 1873 | DBG2("Found UST app by pid %d", pid); |
| 1874 | |
| 1875 | app = caa_container_of(node, struct ust_app, pid_n); |
| 1876 | |
| 1877 | error: |
| 1878 | return app; |
| 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * Allocate and init an UST app object using the registration information and |
| 1883 | * the command socket. This is called when the command socket connects to the |
| 1884 | * session daemon. |
| 1885 | * |
| 1886 | * The object is returned on success or else NULL. |
| 1887 | */ |
| 1888 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
| 1889 | { |
| 1890 | struct ust_app *lta = NULL; |
| 1891 | |
| 1892 | assert(msg); |
| 1893 | assert(sock >= 0); |
| 1894 | |
| 1895 | DBG3("UST app creating application for socket %d", sock); |
| 1896 | |
| 1897 | if ((msg->bits_per_long == 64 && |
| 1898 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) |
| 1899 | || (msg->bits_per_long == 32 && |
| 1900 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { |
| 1901 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
| 1902 | "%d-bit long, but no consumerd for this size is available.\n", |
| 1903 | msg->name, msg->pid, msg->bits_per_long); |
| 1904 | goto error; |
| 1905 | } |
| 1906 | |
| 1907 | lta = zmalloc(sizeof(struct ust_app)); |
| 1908 | if (lta == NULL) { |
| 1909 | PERROR("malloc"); |
| 1910 | goto error; |
| 1911 | } |
| 1912 | |
| 1913 | lta->ppid = msg->ppid; |
| 1914 | lta->uid = msg->uid; |
| 1915 | lta->gid = msg->gid; |
| 1916 | |
| 1917 | lta->bits_per_long = msg->bits_per_long; |
| 1918 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
| 1919 | lta->uint16_t_alignment = msg->uint16_t_alignment; |
| 1920 | lta->uint32_t_alignment = msg->uint32_t_alignment; |
| 1921 | lta->uint64_t_alignment = msg->uint64_t_alignment; |
| 1922 | lta->long_alignment = msg->long_alignment; |
| 1923 | lta->byte_order = msg->byte_order; |
| 1924 | |
| 1925 | lta->v_major = msg->major; |
| 1926 | lta->v_minor = msg->minor; |
| 1927 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1928 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1929 | lta->notify_sock = -1; |
| 1930 | |
| 1931 | /* Copy name and make sure it's NULL terminated. */ |
| 1932 | strncpy(lta->name, msg->name, sizeof(lta->name)); |
| 1933 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; |
| 1934 | |
| 1935 | /* |
| 1936 | * Before this can be called, when receiving the registration information, |
| 1937 | * the application compatibility is checked. So, at this point, the |
| 1938 | * application can work with this session daemon. |
| 1939 | */ |
| 1940 | lta->compatible = 1; |
| 1941 | |
| 1942 | lta->pid = msg->pid; |
| 1943 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
| 1944 | lta->sock = sock; |
| 1945 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
| 1946 | |
| 1947 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
| 1948 | |
| 1949 | error: |
| 1950 | return lta; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * For a given application object, add it to every hash table. |
| 1955 | */ |
| 1956 | void ust_app_add(struct ust_app *app) |
| 1957 | { |
| 1958 | assert(app); |
| 1959 | assert(app->notify_sock >= 0); |
| 1960 | |
| 1961 | rcu_read_lock(); |
| 1962 | |
| 1963 | /* |
| 1964 | * On a re-registration, we want to kick out the previous registration of |
| 1965 | * that pid |
| 1966 | */ |
| 1967 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
| 1968 | |
| 1969 | /* |
| 1970 | * The socket _should_ be unique until _we_ call close. So, a add_unique |
| 1971 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was |
| 1972 | * already in the table. |
| 1973 | */ |
| 1974 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
| 1975 | |
| 1976 | /* Add application to the notify socket hash table. */ |
| 1977 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); |
| 1978 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); |
| 1979 | |
| 1980 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
| 1981 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
| 1982 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, |
| 1983 | app->v_minor); |
| 1984 | |
| 1985 | rcu_read_unlock(); |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * Set the application version into the object. |
| 1990 | * |
| 1991 | * Return 0 on success else a negative value either an errno code or a |
| 1992 | * LTTng-UST error code. |
| 1993 | */ |
| 1994 | int ust_app_version(struct ust_app *app) |
| 1995 | { |
| 1996 | int ret; |
| 1997 | |
| 1998 | assert(app); |
| 1999 | |
| 2000 | ret = ustctl_tracer_version(app->sock, &app->version); |
| 2001 | if (ret < 0) { |
| 2002 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
| 2003 | ERR("UST app %d verson failed with ret %d", app->sock, ret); |
| 2004 | } else { |
| 2005 | DBG3("UST app %d verion failed. Application is dead", app->sock); |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | return ret; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * Unregister app by removing it from the global traceable app list and freeing |
| 2014 | * the data struct. |
| 2015 | * |
| 2016 | * The socket is already closed at this point so no close to sock. |
| 2017 | */ |
| 2018 | void ust_app_unregister(int sock) |
| 2019 | { |
| 2020 | struct ust_app *lta; |
| 2021 | struct lttng_ht_node_ulong *node; |
| 2022 | struct lttng_ht_iter iter; |
| 2023 | struct ust_app_session *ua_sess; |
| 2024 | int ret; |
| 2025 | |
| 2026 | rcu_read_lock(); |
| 2027 | |
| 2028 | /* Get the node reference for a call_rcu */ |
| 2029 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
| 2030 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 2031 | assert(node); |
| 2032 | |
| 2033 | lta = caa_container_of(node, struct ust_app, sock_n); |
| 2034 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
| 2035 | |
| 2036 | /* Remove application from PID hash table */ |
| 2037 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
| 2038 | assert(!ret); |
| 2039 | |
| 2040 | /* |
| 2041 | * Remove application from notify hash table. The thread handling the |
| 2042 | * notify socket could have deleted the node so ignore on error because |
| 2043 | * either way it's valid. The close of that socket is handled by the other |
| 2044 | * thread. |
| 2045 | */ |
| 2046 | iter.iter.node = <a->notify_sock_n.node; |
| 2047 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 2048 | |
| 2049 | /* |
| 2050 | * Ignore return value since the node might have been removed before by an |
| 2051 | * add replace during app registration because the PID can be reassigned by |
| 2052 | * the OS. |
| 2053 | */ |
| 2054 | iter.iter.node = <a->pid_n.node; |
| 2055 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 2056 | if (ret) { |
| 2057 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", |
| 2058 | lta->pid); |
| 2059 | } |
| 2060 | |
| 2061 | /* Remove sessions so they are not visible during deletion.*/ |
| 2062 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
| 2063 | node.node) { |
| 2064 | ret = lttng_ht_del(lta->sessions, &iter); |
| 2065 | if (ret) { |
| 2066 | /* The session was already removed so scheduled for teardown. */ |
| 2067 | continue; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * Add session to list for teardown. This is safe since at this point we |
| 2072 | * are the only one using this list. |
| 2073 | */ |
| 2074 | pthread_mutex_lock(&ua_sess->lock); |
| 2075 | |
| 2076 | /* |
| 2077 | * Normally, this is done in the delete session process which is |
| 2078 | * executed in the call rcu below. However, upon registration we can't |
| 2079 | * afford to wait for the grace period before pushing data or else the |
| 2080 | * data pending feature can race between the unregistration and stop |
| 2081 | * command where the data pending command is sent *before* the grace |
| 2082 | * period ended. |
| 2083 | * |
| 2084 | * The close metadata below nullifies the metadata pointer in the |
| 2085 | * session so the delete session will NOT push/close a second time. |
| 2086 | */ |
| 2087 | (void) push_metadata(lta, ua_sess); |
| 2088 | (void) close_metadata(lta, ua_sess); |
| 2089 | |
| 2090 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
| 2091 | pthread_mutex_unlock(&ua_sess->lock); |
| 2092 | } |
| 2093 | |
| 2094 | /* Free memory */ |
| 2095 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); |
| 2096 | |
| 2097 | rcu_read_unlock(); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * Return traceable_app_count |
| 2103 | */ |
| 2104 | unsigned long ust_app_list_count(void) |
| 2105 | { |
| 2106 | unsigned long count; |
| 2107 | |
| 2108 | rcu_read_lock(); |
| 2109 | count = lttng_ht_get_count(ust_app_ht); |
| 2110 | rcu_read_unlock(); |
| 2111 | |
| 2112 | return count; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * Fill events array with all events name of all registered apps. |
| 2117 | */ |
| 2118 | int ust_app_list_events(struct lttng_event **events) |
| 2119 | { |
| 2120 | int ret, handle; |
| 2121 | size_t nbmem, count = 0; |
| 2122 | struct lttng_ht_iter iter; |
| 2123 | struct ust_app *app; |
| 2124 | struct lttng_event *tmp_event; |
| 2125 | |
| 2126 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 2127 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
| 2128 | if (tmp_event == NULL) { |
| 2129 | PERROR("zmalloc ust app events"); |
| 2130 | ret = -ENOMEM; |
| 2131 | goto error; |
| 2132 | } |
| 2133 | |
| 2134 | rcu_read_lock(); |
| 2135 | |
| 2136 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2137 | struct lttng_ust_tracepoint_iter uiter; |
| 2138 | |
| 2139 | health_code_update(); |
| 2140 | |
| 2141 | if (!app->compatible) { |
| 2142 | /* |
| 2143 | * TODO: In time, we should notice the caller of this error by |
| 2144 | * telling him that this is a version error. |
| 2145 | */ |
| 2146 | continue; |
| 2147 | } |
| 2148 | handle = ustctl_tracepoint_list(app->sock); |
| 2149 | if (handle < 0) { |
| 2150 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 2151 | ERR("UST app list events getting handle failed for app pid %d", |
| 2152 | app->pid); |
| 2153 | } |
| 2154 | continue; |
| 2155 | } |
| 2156 | |
| 2157 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
| 2158 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 2159 | /* Handle ustctl error. */ |
| 2160 | if (ret < 0) { |
| 2161 | free(tmp_event); |
| 2162 | if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) { |
| 2163 | ERR("UST app tp list get failed for app %d with ret %d", |
| 2164 | app->sock, ret); |
| 2165 | } else { |
| 2166 | DBG3("UST app tp list get failed. Application is dead"); |
| 2167 | } |
| 2168 | goto rcu_error; |
| 2169 | } |
| 2170 | |
| 2171 | health_code_update(); |
| 2172 | if (count >= nbmem) { |
| 2173 | /* In case the realloc fails, we free the memory */ |
| 2174 | void *ptr; |
| 2175 | |
| 2176 | DBG2("Reallocating event list from %zu to %zu entries", nbmem, |
| 2177 | 2 * nbmem); |
| 2178 | nbmem *= 2; |
| 2179 | ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event)); |
| 2180 | if (ptr == NULL) { |
| 2181 | PERROR("realloc ust app events"); |
| 2182 | free(tmp_event); |
| 2183 | ret = -ENOMEM; |
| 2184 | goto rcu_error; |
| 2185 | } |
| 2186 | tmp_event = ptr; |
| 2187 | } |
| 2188 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
| 2189 | tmp_event[count].loglevel = uiter.loglevel; |
| 2190 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; |
| 2191 | tmp_event[count].pid = app->pid; |
| 2192 | tmp_event[count].enabled = -1; |
| 2193 | count++; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | ret = count; |
| 2198 | *events = tmp_event; |
| 2199 | |
| 2200 | DBG2("UST app list events done (%zu events)", count); |
| 2201 | |
| 2202 | rcu_error: |
| 2203 | rcu_read_unlock(); |
| 2204 | error: |
| 2205 | health_code_update(); |
| 2206 | return ret; |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Fill events array with all events name of all registered apps. |
| 2211 | */ |
| 2212 | int ust_app_list_event_fields(struct lttng_event_field **fields) |
| 2213 | { |
| 2214 | int ret, handle; |
| 2215 | size_t nbmem, count = 0; |
| 2216 | struct lttng_ht_iter iter; |
| 2217 | struct ust_app *app; |
| 2218 | struct lttng_event_field *tmp_event; |
| 2219 | |
| 2220 | nbmem = UST_APP_EVENT_LIST_SIZE; |
| 2221 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
| 2222 | if (tmp_event == NULL) { |
| 2223 | PERROR("zmalloc ust app event fields"); |
| 2224 | ret = -ENOMEM; |
| 2225 | goto error; |
| 2226 | } |
| 2227 | |
| 2228 | rcu_read_lock(); |
| 2229 | |
| 2230 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2231 | struct lttng_ust_field_iter uiter; |
| 2232 | |
| 2233 | health_code_update(); |
| 2234 | |
| 2235 | if (!app->compatible) { |
| 2236 | /* |
| 2237 | * TODO: In time, we should notice the caller of this error by |
| 2238 | * telling him that this is a version error. |
| 2239 | */ |
| 2240 | continue; |
| 2241 | } |
| 2242 | handle = ustctl_tracepoint_field_list(app->sock); |
| 2243 | if (handle < 0) { |
| 2244 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
| 2245 | ERR("UST app list field getting handle failed for app pid %d", |
| 2246 | app->pid); |
| 2247 | } |
| 2248 | continue; |
| 2249 | } |
| 2250 | |
| 2251 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, |
| 2252 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
| 2253 | /* Handle ustctl error. */ |
| 2254 | if (ret < 0) { |
| 2255 | free(tmp_event); |
| 2256 | if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) { |
| 2257 | ERR("UST app tp list field failed for app %d with ret %d", |
| 2258 | app->sock, ret); |
| 2259 | } else { |
| 2260 | DBG3("UST app tp list field failed. Application is dead"); |
| 2261 | } |
| 2262 | goto rcu_error; |
| 2263 | } |
| 2264 | |
| 2265 | health_code_update(); |
| 2266 | if (count >= nbmem) { |
| 2267 | /* In case the realloc fails, we free the memory */ |
| 2268 | void *ptr; |
| 2269 | |
| 2270 | DBG2("Reallocating event field list from %zu to %zu entries", nbmem, |
| 2271 | 2 * nbmem); |
| 2272 | nbmem *= 2; |
| 2273 | ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field)); |
| 2274 | if (ptr == NULL) { |
| 2275 | PERROR("realloc ust app event fields"); |
| 2276 | free(tmp_event); |
| 2277 | ret = -ENOMEM; |
| 2278 | goto rcu_error; |
| 2279 | } |
| 2280 | tmp_event = ptr; |
| 2281 | } |
| 2282 | |
| 2283 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
| 2284 | tmp_event[count].type = uiter.type; |
| 2285 | tmp_event[count].nowrite = uiter.nowrite; |
| 2286 | |
| 2287 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 2288 | tmp_event[count].event.loglevel = uiter.loglevel; |
| 2289 | tmp_event[count].event.type = LTTNG_UST_TRACEPOINT; |
| 2290 | tmp_event[count].event.pid = app->pid; |
| 2291 | tmp_event[count].event.enabled = -1; |
| 2292 | count++; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | ret = count; |
| 2297 | *fields = tmp_event; |
| 2298 | |
| 2299 | DBG2("UST app list event fields done (%zu events)", count); |
| 2300 | |
| 2301 | rcu_error: |
| 2302 | rcu_read_unlock(); |
| 2303 | error: |
| 2304 | health_code_update(); |
| 2305 | return ret; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * Free and clean all traceable apps of the global list. |
| 2310 | */ |
| 2311 | void ust_app_clean_list(void) |
| 2312 | { |
| 2313 | int ret; |
| 2314 | struct ust_app *app; |
| 2315 | struct lttng_ht_iter iter; |
| 2316 | |
| 2317 | DBG2("UST app cleaning registered apps hash table"); |
| 2318 | |
| 2319 | rcu_read_lock(); |
| 2320 | |
| 2321 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2322 | ret = lttng_ht_del(ust_app_ht, &iter); |
| 2323 | assert(!ret); |
| 2324 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
| 2325 | } |
| 2326 | |
| 2327 | /* Cleanup socket hash table */ |
| 2328 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, |
| 2329 | sock_n.node) { |
| 2330 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
| 2331 | assert(!ret); |
| 2332 | } |
| 2333 | |
| 2334 | /* Cleanup notify socket hash table */ |
| 2335 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, |
| 2336 | notify_sock_n.node) { |
| 2337 | ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 2338 | assert(!ret); |
| 2339 | } |
| 2340 | |
| 2341 | /* Destroy is done only when the ht is empty */ |
| 2342 | lttng_ht_destroy(ust_app_ht); |
| 2343 | lttng_ht_destroy(ust_app_ht_by_sock); |
| 2344 | lttng_ht_destroy(ust_app_ht_by_notify_sock); |
| 2345 | |
| 2346 | rcu_read_unlock(); |
| 2347 | } |
| 2348 | |
| 2349 | /* |
| 2350 | * Init UST app hash table. |
| 2351 | */ |
| 2352 | void ust_app_ht_alloc(void) |
| 2353 | { |
| 2354 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2355 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2356 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * For a specific UST session, disable the channel for all registered apps. |
| 2361 | */ |
| 2362 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
| 2363 | struct ltt_ust_channel *uchan) |
| 2364 | { |
| 2365 | int ret = 0; |
| 2366 | struct lttng_ht_iter iter; |
| 2367 | struct lttng_ht_node_str *ua_chan_node; |
| 2368 | struct ust_app *app; |
| 2369 | struct ust_app_session *ua_sess; |
| 2370 | struct ust_app_channel *ua_chan; |
| 2371 | |
| 2372 | if (usess == NULL || uchan == NULL) { |
| 2373 | ERR("Disabling UST global channel with NULL values"); |
| 2374 | ret = -1; |
| 2375 | goto error; |
| 2376 | } |
| 2377 | |
| 2378 | DBG2("UST app disabling channel %s from global domain for session id %d", |
| 2379 | uchan->name, usess->id); |
| 2380 | |
| 2381 | rcu_read_lock(); |
| 2382 | |
| 2383 | /* For every registered applications */ |
| 2384 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2385 | struct lttng_ht_iter uiter; |
| 2386 | if (!app->compatible) { |
| 2387 | /* |
| 2388 | * TODO: In time, we should notice the caller of this error by |
| 2389 | * telling him that this is a version error. |
| 2390 | */ |
| 2391 | continue; |
| 2392 | } |
| 2393 | ua_sess = lookup_session_by_app(usess, app); |
| 2394 | if (ua_sess == NULL) { |
| 2395 | continue; |
| 2396 | } |
| 2397 | |
| 2398 | /* Get channel */ |
| 2399 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 2400 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 2401 | /* If the session if found for the app, the channel must be there */ |
| 2402 | assert(ua_chan_node); |
| 2403 | |
| 2404 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2405 | /* The channel must not be already disabled */ |
| 2406 | assert(ua_chan->enabled == 1); |
| 2407 | |
| 2408 | /* Disable channel onto application */ |
| 2409 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); |
| 2410 | if (ret < 0) { |
| 2411 | /* XXX: We might want to report this error at some point... */ |
| 2412 | continue; |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | rcu_read_unlock(); |
| 2417 | |
| 2418 | error: |
| 2419 | return ret; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * For a specific UST session, enable the channel for all registered apps. |
| 2424 | */ |
| 2425 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
| 2426 | struct ltt_ust_channel *uchan) |
| 2427 | { |
| 2428 | int ret = 0; |
| 2429 | struct lttng_ht_iter iter; |
| 2430 | struct ust_app *app; |
| 2431 | struct ust_app_session *ua_sess; |
| 2432 | |
| 2433 | if (usess == NULL || uchan == NULL) { |
| 2434 | ERR("Adding UST global channel to NULL values"); |
| 2435 | ret = -1; |
| 2436 | goto error; |
| 2437 | } |
| 2438 | |
| 2439 | DBG2("UST app enabling channel %s to global domain for session id %d", |
| 2440 | uchan->name, usess->id); |
| 2441 | |
| 2442 | rcu_read_lock(); |
| 2443 | |
| 2444 | /* For every registered applications */ |
| 2445 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2446 | if (!app->compatible) { |
| 2447 | /* |
| 2448 | * TODO: In time, we should notice the caller of this error by |
| 2449 | * telling him that this is a version error. |
| 2450 | */ |
| 2451 | continue; |
| 2452 | } |
| 2453 | ua_sess = lookup_session_by_app(usess, app); |
| 2454 | if (ua_sess == NULL) { |
| 2455 | continue; |
| 2456 | } |
| 2457 | |
| 2458 | /* Enable channel onto application */ |
| 2459 | ret = enable_ust_app_channel(ua_sess, uchan, app); |
| 2460 | if (ret < 0) { |
| 2461 | /* XXX: We might want to report this error at some point... */ |
| 2462 | continue; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | rcu_read_unlock(); |
| 2467 | |
| 2468 | error: |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
| 2473 | * Disable an event in a channel and for a specific session. |
| 2474 | */ |
| 2475 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
| 2476 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 2477 | { |
| 2478 | int ret = 0; |
| 2479 | struct lttng_ht_iter iter, uiter; |
| 2480 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; |
| 2481 | struct ust_app *app; |
| 2482 | struct ust_app_session *ua_sess; |
| 2483 | struct ust_app_channel *ua_chan; |
| 2484 | struct ust_app_event *ua_event; |
| 2485 | |
| 2486 | DBG("UST app disabling event %s for all apps in channel " |
| 2487 | "%s for session id %d", uevent->attr.name, uchan->name, usess->id); |
| 2488 | |
| 2489 | rcu_read_lock(); |
| 2490 | |
| 2491 | /* For all registered applications */ |
| 2492 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2493 | if (!app->compatible) { |
| 2494 | /* |
| 2495 | * TODO: In time, we should notice the caller of this error by |
| 2496 | * telling him that this is a version error. |
| 2497 | */ |
| 2498 | continue; |
| 2499 | } |
| 2500 | ua_sess = lookup_session_by_app(usess, app); |
| 2501 | if (ua_sess == NULL) { |
| 2502 | /* Next app */ |
| 2503 | continue; |
| 2504 | } |
| 2505 | |
| 2506 | /* Lookup channel in the ust app session */ |
| 2507 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 2508 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 2509 | if (ua_chan_node == NULL) { |
| 2510 | DBG2("Channel %s not found in session id %d for app pid %d." |
| 2511 | "Skipping", uchan->name, usess->id, app->pid); |
| 2512 | continue; |
| 2513 | } |
| 2514 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2515 | |
| 2516 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); |
| 2517 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); |
| 2518 | if (ua_event_node == NULL) { |
| 2519 | DBG2("Event %s not found in channel %s for app pid %d." |
| 2520 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
| 2521 | continue; |
| 2522 | } |
| 2523 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); |
| 2524 | |
| 2525 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 2526 | if (ret < 0) { |
| 2527 | /* XXX: Report error someday... */ |
| 2528 | continue; |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | rcu_read_unlock(); |
| 2533 | |
| 2534 | return ret; |
| 2535 | } |
| 2536 | |
| 2537 | /* |
| 2538 | * For a specific UST session and UST channel, the event for all |
| 2539 | * registered apps. |
| 2540 | */ |
| 2541 | int ust_app_disable_all_event_glb(struct ltt_ust_session *usess, |
| 2542 | struct ltt_ust_channel *uchan) |
| 2543 | { |
| 2544 | int ret = 0; |
| 2545 | struct lttng_ht_iter iter, uiter; |
| 2546 | struct lttng_ht_node_str *ua_chan_node; |
| 2547 | struct ust_app *app; |
| 2548 | struct ust_app_session *ua_sess; |
| 2549 | struct ust_app_channel *ua_chan; |
| 2550 | struct ust_app_event *ua_event; |
| 2551 | |
| 2552 | DBG("UST app disabling all event for all apps in channel " |
| 2553 | "%s for session id %d", uchan->name, usess->id); |
| 2554 | |
| 2555 | rcu_read_lock(); |
| 2556 | |
| 2557 | /* For all registered applications */ |
| 2558 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2559 | if (!app->compatible) { |
| 2560 | /* |
| 2561 | * TODO: In time, we should notice the caller of this error by |
| 2562 | * telling him that this is a version error. |
| 2563 | */ |
| 2564 | continue; |
| 2565 | } |
| 2566 | ua_sess = lookup_session_by_app(usess, app); |
| 2567 | if (!ua_sess) { |
| 2568 | /* The application has problem or is probably dead. */ |
| 2569 | continue; |
| 2570 | } |
| 2571 | |
| 2572 | /* Lookup channel in the ust app session */ |
| 2573 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 2574 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 2575 | /* If the channel is not found, there is a code flow error */ |
| 2576 | assert(ua_chan_node); |
| 2577 | |
| 2578 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2579 | |
| 2580 | /* Disable each events of channel */ |
| 2581 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
| 2582 | node.node) { |
| 2583 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 2584 | if (ret < 0) { |
| 2585 | /* XXX: Report error someday... */ |
| 2586 | continue; |
| 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | rcu_read_unlock(); |
| 2592 | |
| 2593 | return ret; |
| 2594 | } |
| 2595 | |
| 2596 | /* |
| 2597 | * For a specific UST session, create the channel for all registered apps. |
| 2598 | */ |
| 2599 | int ust_app_create_channel_glb(struct ltt_ust_session *usess, |
| 2600 | struct ltt_ust_channel *uchan) |
| 2601 | { |
| 2602 | int ret = 0, created; |
| 2603 | struct lttng_ht_iter iter; |
| 2604 | struct ust_app *app; |
| 2605 | struct ust_app_session *ua_sess = NULL; |
| 2606 | |
| 2607 | /* Very wrong code flow */ |
| 2608 | assert(usess); |
| 2609 | assert(uchan); |
| 2610 | |
| 2611 | DBG2("UST app adding channel %s to global domain for session id %d", |
| 2612 | uchan->name, usess->id); |
| 2613 | |
| 2614 | rcu_read_lock(); |
| 2615 | |
| 2616 | /* For every registered applications */ |
| 2617 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2618 | if (!app->compatible) { |
| 2619 | /* |
| 2620 | * TODO: In time, we should notice the caller of this error by |
| 2621 | * telling him that this is a version error. |
| 2622 | */ |
| 2623 | continue; |
| 2624 | } |
| 2625 | /* |
| 2626 | * Create session on the tracer side and add it to app session HT. Note |
| 2627 | * that if session exist, it will simply return a pointer to the ust |
| 2628 | * app session. |
| 2629 | */ |
| 2630 | ret = create_ust_app_session(usess, app, &ua_sess, &created); |
| 2631 | if (ret < 0) { |
| 2632 | switch (ret) { |
| 2633 | case -ENOTCONN: |
| 2634 | /* |
| 2635 | * The application's socket is not valid. Either a bad socket |
| 2636 | * or a timeout on it. We can't inform the caller that for a |
| 2637 | * specific app, the session failed so lets continue here. |
| 2638 | */ |
| 2639 | continue; |
| 2640 | case -ENOMEM: |
| 2641 | default: |
| 2642 | goto error_rcu_unlock; |
| 2643 | } |
| 2644 | } |
| 2645 | assert(ua_sess); |
| 2646 | |
| 2647 | pthread_mutex_lock(&ua_sess->lock); |
| 2648 | /* Create channel onto application. We don't need the chan ref. */ |
| 2649 | ret = create_ust_app_channel(ua_sess, uchan, app, usess->consumer, |
| 2650 | LTTNG_UST_CHAN_PER_CPU, NULL); |
| 2651 | pthread_mutex_unlock(&ua_sess->lock); |
| 2652 | if (ret < 0) { |
| 2653 | if (ret == -ENOMEM) { |
| 2654 | /* No more memory is a fatal error. Stop right now. */ |
| 2655 | goto error_rcu_unlock; |
| 2656 | } |
| 2657 | /* Cleanup the created session if it's the case. */ |
| 2658 | if (created) { |
| 2659 | destroy_app_session(app, ua_sess); |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | error_rcu_unlock: |
| 2665 | rcu_read_unlock(); |
| 2666 | return ret; |
| 2667 | } |
| 2668 | |
| 2669 | /* |
| 2670 | * Enable event for a specific session and channel on the tracer. |
| 2671 | */ |
| 2672 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
| 2673 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 2674 | { |
| 2675 | int ret = 0; |
| 2676 | struct lttng_ht_iter iter, uiter; |
| 2677 | struct lttng_ht_node_str *ua_chan_node; |
| 2678 | struct ust_app *app; |
| 2679 | struct ust_app_session *ua_sess; |
| 2680 | struct ust_app_channel *ua_chan; |
| 2681 | struct ust_app_event *ua_event; |
| 2682 | |
| 2683 | DBG("UST app enabling event %s for all apps for session id %d", |
| 2684 | uevent->attr.name, usess->id); |
| 2685 | |
| 2686 | /* |
| 2687 | * NOTE: At this point, this function is called only if the session and |
| 2688 | * channel passed are already created for all apps. and enabled on the |
| 2689 | * tracer also. |
| 2690 | */ |
| 2691 | |
| 2692 | rcu_read_lock(); |
| 2693 | |
| 2694 | /* For all registered applications */ |
| 2695 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2696 | if (!app->compatible) { |
| 2697 | /* |
| 2698 | * TODO: In time, we should notice the caller of this error by |
| 2699 | * telling him that this is a version error. |
| 2700 | */ |
| 2701 | continue; |
| 2702 | } |
| 2703 | ua_sess = lookup_session_by_app(usess, app); |
| 2704 | if (!ua_sess) { |
| 2705 | /* The application has problem or is probably dead. */ |
| 2706 | continue; |
| 2707 | } |
| 2708 | |
| 2709 | pthread_mutex_lock(&ua_sess->lock); |
| 2710 | |
| 2711 | /* Lookup channel in the ust app session */ |
| 2712 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 2713 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 2714 | /* If the channel is not found, there is a code flow error */ |
| 2715 | assert(ua_chan_node); |
| 2716 | |
| 2717 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2718 | |
| 2719 | /* Get event node */ |
| 2720 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 2721 | uevent->filter, uevent->attr.loglevel); |
| 2722 | if (ua_event == NULL) { |
| 2723 | DBG3("UST app enable event %s not found for app PID %d." |
| 2724 | "Skipping app", uevent->attr.name, app->pid); |
| 2725 | goto next_app; |
| 2726 | } |
| 2727 | |
| 2728 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 2729 | if (ret < 0) { |
| 2730 | pthread_mutex_unlock(&ua_sess->lock); |
| 2731 | goto error; |
| 2732 | } |
| 2733 | next_app: |
| 2734 | pthread_mutex_unlock(&ua_sess->lock); |
| 2735 | } |
| 2736 | |
| 2737 | error: |
| 2738 | rcu_read_unlock(); |
| 2739 | return ret; |
| 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | * For a specific existing UST session and UST channel, creates the event for |
| 2744 | * all registered apps. |
| 2745 | */ |
| 2746 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
| 2747 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
| 2748 | { |
| 2749 | int ret = 0; |
| 2750 | struct lttng_ht_iter iter, uiter; |
| 2751 | struct lttng_ht_node_str *ua_chan_node; |
| 2752 | struct ust_app *app; |
| 2753 | struct ust_app_session *ua_sess; |
| 2754 | struct ust_app_channel *ua_chan; |
| 2755 | |
| 2756 | DBG("UST app creating event %s for all apps for session id %d", |
| 2757 | uevent->attr.name, usess->id); |
| 2758 | |
| 2759 | rcu_read_lock(); |
| 2760 | |
| 2761 | /* For all registered applications */ |
| 2762 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 2763 | if (!app->compatible) { |
| 2764 | /* |
| 2765 | * TODO: In time, we should notice the caller of this error by |
| 2766 | * telling him that this is a version error. |
| 2767 | */ |
| 2768 | continue; |
| 2769 | } |
| 2770 | ua_sess = lookup_session_by_app(usess, app); |
| 2771 | if (!ua_sess) { |
| 2772 | /* The application has problem or is probably dead. */ |
| 2773 | continue; |
| 2774 | } |
| 2775 | |
| 2776 | pthread_mutex_lock(&ua_sess->lock); |
| 2777 | /* Lookup channel in the ust app session */ |
| 2778 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 2779 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 2780 | /* If the channel is not found, there is a code flow error */ |
| 2781 | assert(ua_chan_node); |
| 2782 | |
| 2783 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 2784 | |
| 2785 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
| 2786 | pthread_mutex_unlock(&ua_sess->lock); |
| 2787 | if (ret < 0) { |
| 2788 | if (ret != -LTTNG_UST_ERR_EXIST) { |
| 2789 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
| 2790 | break; |
| 2791 | } |
| 2792 | DBG2("UST app event %s already exist on app PID %d", |
| 2793 | uevent->attr.name, app->pid); |
| 2794 | continue; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | rcu_read_unlock(); |
| 2799 | |
| 2800 | return ret; |
| 2801 | } |
| 2802 | |
| 2803 | /* |
| 2804 | * Start tracing for a specific UST session and app. |
| 2805 | */ |
| 2806 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 2807 | { |
| 2808 | int ret = 0; |
| 2809 | struct ust_app_session *ua_sess; |
| 2810 | |
| 2811 | DBG("Starting tracing for ust app pid %d", app->pid); |
| 2812 | |
| 2813 | rcu_read_lock(); |
| 2814 | |
| 2815 | if (!app->compatible) { |
| 2816 | goto end; |
| 2817 | } |
| 2818 | |
| 2819 | ua_sess = lookup_session_by_app(usess, app); |
| 2820 | if (ua_sess == NULL) { |
| 2821 | /* The session is in teardown process. Ignore and continue. */ |
| 2822 | goto end; |
| 2823 | } |
| 2824 | |
| 2825 | pthread_mutex_lock(&ua_sess->lock); |
| 2826 | |
| 2827 | /* Upon restart, we skip the setup, already done */ |
| 2828 | if (ua_sess->started) { |
| 2829 | goto skip_setup; |
| 2830 | } |
| 2831 | |
| 2832 | /* Create directories if consumer is LOCAL and has a path defined. */ |
| 2833 | if (usess->consumer->type == CONSUMER_DST_LOCAL && |
| 2834 | strlen(usess->consumer->dst.trace_path) > 0) { |
| 2835 | ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, |
| 2836 | S_IRWXU | S_IRWXG, usess->uid, usess->gid); |
| 2837 | if (ret < 0) { |
| 2838 | if (ret != -EEXIST) { |
| 2839 | ERR("Trace directory creation error"); |
| 2840 | goto error_unlock; |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | /* Create the metadata for the application. */ |
| 2846 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); |
| 2847 | if (ret < 0) { |
| 2848 | goto error_unlock; |
| 2849 | } |
| 2850 | |
| 2851 | health_code_update(); |
| 2852 | |
| 2853 | skip_setup: |
| 2854 | /* This start the UST tracing */ |
| 2855 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
| 2856 | if (ret < 0) { |
| 2857 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2858 | ERR("Error starting tracing for app pid: %d (ret: %d)", |
| 2859 | app->pid, ret); |
| 2860 | } else { |
| 2861 | DBG("UST app start session failed. Application is dead."); |
| 2862 | } |
| 2863 | goto error_unlock; |
| 2864 | } |
| 2865 | |
| 2866 | /* Indicate that the session has been started once */ |
| 2867 | ua_sess->started = 1; |
| 2868 | |
| 2869 | pthread_mutex_unlock(&ua_sess->lock); |
| 2870 | |
| 2871 | health_code_update(); |
| 2872 | |
| 2873 | /* Quiescent wait after starting trace */ |
| 2874 | ret = ustctl_wait_quiescent(app->sock); |
| 2875 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2876 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 2877 | app->pid, ret); |
| 2878 | } |
| 2879 | |
| 2880 | end: |
| 2881 | rcu_read_unlock(); |
| 2882 | health_code_update(); |
| 2883 | return 0; |
| 2884 | |
| 2885 | error_unlock: |
| 2886 | pthread_mutex_unlock(&ua_sess->lock); |
| 2887 | rcu_read_unlock(); |
| 2888 | health_code_update(); |
| 2889 | return -1; |
| 2890 | } |
| 2891 | |
| 2892 | /* |
| 2893 | * Stop tracing for a specific UST session and app. |
| 2894 | */ |
| 2895 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 2896 | { |
| 2897 | int ret = 0; |
| 2898 | struct lttng_ht_iter iter; |
| 2899 | struct ust_app_session *ua_sess; |
| 2900 | struct ust_app_channel *ua_chan; |
| 2901 | |
| 2902 | DBG("Stopping tracing for ust app pid %d", app->pid); |
| 2903 | |
| 2904 | rcu_read_lock(); |
| 2905 | |
| 2906 | if (!app->compatible) { |
| 2907 | goto end_no_session; |
| 2908 | } |
| 2909 | |
| 2910 | ua_sess = lookup_session_by_app(usess, app); |
| 2911 | if (ua_sess == NULL) { |
| 2912 | goto end_no_session; |
| 2913 | } |
| 2914 | |
| 2915 | pthread_mutex_lock(&ua_sess->lock); |
| 2916 | |
| 2917 | /* |
| 2918 | * If started = 0, it means that stop trace has been called for a session |
| 2919 | * that was never started. It's possible since we can have a fail start |
| 2920 | * from either the application manager thread or the command thread. Simply |
| 2921 | * indicate that this is a stop error. |
| 2922 | */ |
| 2923 | if (!ua_sess->started) { |
| 2924 | goto error_rcu_unlock; |
| 2925 | } |
| 2926 | |
| 2927 | health_code_update(); |
| 2928 | |
| 2929 | /* This inhibits UST tracing */ |
| 2930 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
| 2931 | if (ret < 0) { |
| 2932 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2933 | ERR("Error stopping tracing for app pid: %d (ret: %d)", |
| 2934 | app->pid, ret); |
| 2935 | } else { |
| 2936 | DBG("UST app stop session failed. Application is dead."); |
| 2937 | } |
| 2938 | goto error_rcu_unlock; |
| 2939 | } |
| 2940 | |
| 2941 | health_code_update(); |
| 2942 | |
| 2943 | /* Quiescent wait after stopping trace */ |
| 2944 | ret = ustctl_wait_quiescent(app->sock); |
| 2945 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2946 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 2947 | app->pid, ret); |
| 2948 | } |
| 2949 | |
| 2950 | health_code_update(); |
| 2951 | |
| 2952 | /* Flushing buffers */ |
| 2953 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 2954 | node.node) { |
| 2955 | health_code_update(); |
| 2956 | assert(ua_chan->is_sent); |
| 2957 | ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj); |
| 2958 | if (ret < 0) { |
| 2959 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 2960 | ERR("UST app PID %d channel %s flush failed with ret %d", |
| 2961 | app->pid, ua_chan->name, ret); |
| 2962 | } else { |
| 2963 | DBG3("UST app failed to flush %s. Application is dead.", |
| 2964 | ua_chan->name); |
| 2965 | /* No need to continue. */ |
| 2966 | break; |
| 2967 | } |
| 2968 | /* Continuing flushing all buffers */ |
| 2969 | continue; |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | health_code_update(); |
| 2974 | |
| 2975 | ret = push_metadata(app, ua_sess); |
| 2976 | if (ret < 0) { |
| 2977 | goto error_rcu_unlock; |
| 2978 | } |
| 2979 | |
| 2980 | pthread_mutex_unlock(&ua_sess->lock); |
| 2981 | end_no_session: |
| 2982 | rcu_read_unlock(); |
| 2983 | health_code_update(); |
| 2984 | return 0; |
| 2985 | |
| 2986 | error_rcu_unlock: |
| 2987 | pthread_mutex_unlock(&ua_sess->lock); |
| 2988 | rcu_read_unlock(); |
| 2989 | health_code_update(); |
| 2990 | return -1; |
| 2991 | } |
| 2992 | |
| 2993 | /* |
| 2994 | * Destroy a specific UST session in apps. |
| 2995 | */ |
| 2996 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
| 2997 | { |
| 2998 | int ret; |
| 2999 | struct ust_app_session *ua_sess; |
| 3000 | struct lttng_ht_iter iter; |
| 3001 | struct lttng_ht_node_ulong *node; |
| 3002 | |
| 3003 | DBG("Destroy tracing for ust app pid %d", app->pid); |
| 3004 | |
| 3005 | rcu_read_lock(); |
| 3006 | |
| 3007 | if (!app->compatible) { |
| 3008 | goto end; |
| 3009 | } |
| 3010 | |
| 3011 | __lookup_session_by_app(usess, app, &iter); |
| 3012 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 3013 | if (node == NULL) { |
| 3014 | /* Session is being or is deleted. */ |
| 3015 | goto end; |
| 3016 | } |
| 3017 | ua_sess = caa_container_of(node, struct ust_app_session, node); |
| 3018 | |
| 3019 | health_code_update(); |
| 3020 | destroy_app_session(app, ua_sess); |
| 3021 | |
| 3022 | health_code_update(); |
| 3023 | |
| 3024 | /* Quiescent wait after stopping trace */ |
| 3025 | ret = ustctl_wait_quiescent(app->sock); |
| 3026 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3027 | ERR("UST app wait quiescent failed for app pid %d ret %d", |
| 3028 | app->pid, ret); |
| 3029 | } |
| 3030 | end: |
| 3031 | rcu_read_unlock(); |
| 3032 | health_code_update(); |
| 3033 | return 0; |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * Start tracing for the UST session. |
| 3038 | */ |
| 3039 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
| 3040 | { |
| 3041 | int ret = 0; |
| 3042 | struct lttng_ht_iter iter; |
| 3043 | struct ust_app *app; |
| 3044 | |
| 3045 | DBG("Starting all UST traces"); |
| 3046 | |
| 3047 | rcu_read_lock(); |
| 3048 | |
| 3049 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3050 | ret = ust_app_start_trace(usess, app); |
| 3051 | if (ret < 0) { |
| 3052 | /* Continue to next apps even on error */ |
| 3053 | continue; |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | rcu_read_unlock(); |
| 3058 | |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
| 3062 | /* |
| 3063 | * Start tracing for the UST session. |
| 3064 | */ |
| 3065 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) |
| 3066 | { |
| 3067 | int ret = 0; |
| 3068 | struct lttng_ht_iter iter; |
| 3069 | struct ust_app *app; |
| 3070 | |
| 3071 | DBG("Stopping all UST traces"); |
| 3072 | |
| 3073 | rcu_read_lock(); |
| 3074 | |
| 3075 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3076 | ret = ust_app_stop_trace(usess, app); |
| 3077 | if (ret < 0) { |
| 3078 | ERR("UST app stop trace failed with ret %d", ret); |
| 3079 | /* Continue to next apps even on error */ |
| 3080 | continue; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | rcu_read_unlock(); |
| 3085 | |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
| 3089 | /* |
| 3090 | * Destroy app UST session. |
| 3091 | */ |
| 3092 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) |
| 3093 | { |
| 3094 | int ret = 0; |
| 3095 | struct lttng_ht_iter iter; |
| 3096 | struct ust_app *app; |
| 3097 | |
| 3098 | DBG("Destroy all UST traces"); |
| 3099 | |
| 3100 | rcu_read_lock(); |
| 3101 | |
| 3102 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3103 | ret = destroy_trace(usess, app); |
| 3104 | if (ret < 0) { |
| 3105 | /* Continue to next apps even on error */ |
| 3106 | continue; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | rcu_read_unlock(); |
| 3111 | |
| 3112 | return 0; |
| 3113 | } |
| 3114 | |
| 3115 | /* |
| 3116 | * Add channels/events from UST global domain to registered apps at sock. |
| 3117 | */ |
| 3118 | void ust_app_global_update(struct ltt_ust_session *usess, int sock) |
| 3119 | { |
| 3120 | int ret = 0; |
| 3121 | struct lttng_ht_iter iter, uiter, iter_ctx; |
| 3122 | struct ust_app *app; |
| 3123 | struct ust_app_session *ua_sess = NULL; |
| 3124 | struct ust_app_channel *ua_chan; |
| 3125 | struct ust_app_event *ua_event; |
| 3126 | struct ust_app_ctx *ua_ctx; |
| 3127 | |
| 3128 | assert(usess); |
| 3129 | assert(sock >= 0); |
| 3130 | |
| 3131 | DBG2("UST app global update for app sock %d for session id %d", sock, |
| 3132 | usess->id); |
| 3133 | |
| 3134 | rcu_read_lock(); |
| 3135 | |
| 3136 | app = find_app_by_sock(sock); |
| 3137 | if (app == NULL) { |
| 3138 | /* |
| 3139 | * Application can be unregistered before so this is possible hence |
| 3140 | * simply stopping the update. |
| 3141 | */ |
| 3142 | DBG3("UST app update failed to find app sock %d", sock); |
| 3143 | goto error; |
| 3144 | } |
| 3145 | |
| 3146 | if (!app->compatible) { |
| 3147 | goto error; |
| 3148 | } |
| 3149 | |
| 3150 | ret = create_ust_app_session(usess, app, &ua_sess, NULL); |
| 3151 | if (ret < 0) { |
| 3152 | /* Tracer is probably gone or ENOMEM. */ |
| 3153 | goto error; |
| 3154 | } |
| 3155 | assert(ua_sess); |
| 3156 | |
| 3157 | pthread_mutex_lock(&ua_sess->lock); |
| 3158 | |
| 3159 | /* |
| 3160 | * We can iterate safely here over all UST app session since the create ust |
| 3161 | * app session above made a shadow copy of the UST global domain from the |
| 3162 | * ltt ust session. |
| 3163 | */ |
| 3164 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
| 3165 | node.node) { |
| 3166 | ret = create_ust_channel(app, ua_sess, ua_chan, usess->consumer); |
| 3167 | if (ret < 0) { |
| 3168 | /* |
| 3169 | * Stop everything. On error, the application failed, no more file |
| 3170 | * descriptor are available or ENOMEM so stopping here is the only |
| 3171 | * thing we can do for now. |
| 3172 | */ |
| 3173 | goto error_unlock; |
| 3174 | } |
| 3175 | |
| 3176 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx, |
| 3177 | node.node) { |
| 3178 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
| 3179 | if (ret < 0) { |
| 3180 | goto error_unlock; |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | |
| 3185 | /* For each events */ |
| 3186 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
| 3187 | node.node) { |
| 3188 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
| 3189 | if (ret < 0) { |
| 3190 | goto error_unlock; |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | pthread_mutex_unlock(&ua_sess->lock); |
| 3196 | |
| 3197 | if (usess->start_trace) { |
| 3198 | ret = ust_app_start_trace(usess, app); |
| 3199 | if (ret < 0) { |
| 3200 | goto error; |
| 3201 | } |
| 3202 | |
| 3203 | DBG2("UST trace started for app pid %d", app->pid); |
| 3204 | } |
| 3205 | |
| 3206 | /* Everything went well at this point. */ |
| 3207 | rcu_read_unlock(); |
| 3208 | return; |
| 3209 | |
| 3210 | error_unlock: |
| 3211 | pthread_mutex_unlock(&ua_sess->lock); |
| 3212 | error: |
| 3213 | if (ua_sess) { |
| 3214 | destroy_app_session(app, ua_sess); |
| 3215 | } |
| 3216 | rcu_read_unlock(); |
| 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * Add context to a specific channel for global UST domain. |
| 3222 | */ |
| 3223 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, |
| 3224 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) |
| 3225 | { |
| 3226 | int ret = 0; |
| 3227 | struct lttng_ht_node_str *ua_chan_node; |
| 3228 | struct lttng_ht_iter iter, uiter; |
| 3229 | struct ust_app_channel *ua_chan = NULL; |
| 3230 | struct ust_app_session *ua_sess; |
| 3231 | struct ust_app *app; |
| 3232 | |
| 3233 | rcu_read_lock(); |
| 3234 | |
| 3235 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3236 | if (!app->compatible) { |
| 3237 | /* |
| 3238 | * TODO: In time, we should notice the caller of this error by |
| 3239 | * telling him that this is a version error. |
| 3240 | */ |
| 3241 | continue; |
| 3242 | } |
| 3243 | ua_sess = lookup_session_by_app(usess, app); |
| 3244 | if (ua_sess == NULL) { |
| 3245 | continue; |
| 3246 | } |
| 3247 | |
| 3248 | pthread_mutex_lock(&ua_sess->lock); |
| 3249 | /* Lookup channel in the ust app session */ |
| 3250 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
| 3251 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
| 3252 | if (ua_chan_node == NULL) { |
| 3253 | goto next_app; |
| 3254 | } |
| 3255 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, |
| 3256 | node); |
| 3257 | ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app); |
| 3258 | if (ret < 0) { |
| 3259 | goto next_app; |
| 3260 | } |
| 3261 | next_app: |
| 3262 | pthread_mutex_unlock(&ua_sess->lock); |
| 3263 | } |
| 3264 | |
| 3265 | rcu_read_unlock(); |
| 3266 | return ret; |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * Enable event for a channel from a UST session for a specific PID. |
| 3271 | */ |
| 3272 | int ust_app_enable_event_pid(struct ltt_ust_session *usess, |
| 3273 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) |
| 3274 | { |
| 3275 | int ret = 0; |
| 3276 | struct lttng_ht_iter iter; |
| 3277 | struct lttng_ht_node_str *ua_chan_node; |
| 3278 | struct ust_app *app; |
| 3279 | struct ust_app_session *ua_sess; |
| 3280 | struct ust_app_channel *ua_chan; |
| 3281 | struct ust_app_event *ua_event; |
| 3282 | |
| 3283 | DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid); |
| 3284 | |
| 3285 | rcu_read_lock(); |
| 3286 | |
| 3287 | app = ust_app_find_by_pid(pid); |
| 3288 | if (app == NULL) { |
| 3289 | ERR("UST app enable event per PID %d not found", pid); |
| 3290 | ret = -1; |
| 3291 | goto end; |
| 3292 | } |
| 3293 | |
| 3294 | if (!app->compatible) { |
| 3295 | ret = 0; |
| 3296 | goto end; |
| 3297 | } |
| 3298 | |
| 3299 | ua_sess = lookup_session_by_app(usess, app); |
| 3300 | if (!ua_sess) { |
| 3301 | /* The application has problem or is probably dead. */ |
| 3302 | ret = 0; |
| 3303 | goto end; |
| 3304 | } |
| 3305 | |
| 3306 | pthread_mutex_lock(&ua_sess->lock); |
| 3307 | /* Lookup channel in the ust app session */ |
| 3308 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 3309 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 3310 | /* If the channel is not found, there is a code flow error */ |
| 3311 | assert(ua_chan_node); |
| 3312 | |
| 3313 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3314 | |
| 3315 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
| 3316 | uevent->filter, uevent->attr.loglevel); |
| 3317 | if (ua_event == NULL) { |
| 3318 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
| 3319 | if (ret < 0) { |
| 3320 | goto end_unlock; |
| 3321 | } |
| 3322 | } else { |
| 3323 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
| 3324 | if (ret < 0) { |
| 3325 | goto end_unlock; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | end_unlock: |
| 3330 | pthread_mutex_unlock(&ua_sess->lock); |
| 3331 | end: |
| 3332 | rcu_read_unlock(); |
| 3333 | return ret; |
| 3334 | } |
| 3335 | |
| 3336 | /* |
| 3337 | * Disable event for a channel from a UST session for a specific PID. |
| 3338 | */ |
| 3339 | int ust_app_disable_event_pid(struct ltt_ust_session *usess, |
| 3340 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) |
| 3341 | { |
| 3342 | int ret = 0; |
| 3343 | struct lttng_ht_iter iter; |
| 3344 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; |
| 3345 | struct ust_app *app; |
| 3346 | struct ust_app_session *ua_sess; |
| 3347 | struct ust_app_channel *ua_chan; |
| 3348 | struct ust_app_event *ua_event; |
| 3349 | |
| 3350 | DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid); |
| 3351 | |
| 3352 | rcu_read_lock(); |
| 3353 | |
| 3354 | app = ust_app_find_by_pid(pid); |
| 3355 | if (app == NULL) { |
| 3356 | ERR("UST app disable event per PID %d not found", pid); |
| 3357 | ret = -1; |
| 3358 | goto error; |
| 3359 | } |
| 3360 | |
| 3361 | if (!app->compatible) { |
| 3362 | ret = 0; |
| 3363 | goto error; |
| 3364 | } |
| 3365 | |
| 3366 | ua_sess = lookup_session_by_app(usess, app); |
| 3367 | if (!ua_sess) { |
| 3368 | /* The application has problem or is probably dead. */ |
| 3369 | goto error; |
| 3370 | } |
| 3371 | |
| 3372 | /* Lookup channel in the ust app session */ |
| 3373 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
| 3374 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); |
| 3375 | if (ua_chan_node == NULL) { |
| 3376 | /* Channel does not exist, skip disabling */ |
| 3377 | goto error; |
| 3378 | } |
| 3379 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
| 3380 | |
| 3381 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter); |
| 3382 | ua_event_node = lttng_ht_iter_get_node_str(&iter); |
| 3383 | if (ua_event_node == NULL) { |
| 3384 | /* Event does not exist, skip disabling */ |
| 3385 | goto error; |
| 3386 | } |
| 3387 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); |
| 3388 | |
| 3389 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
| 3390 | if (ret < 0) { |
| 3391 | goto error; |
| 3392 | } |
| 3393 | |
| 3394 | error: |
| 3395 | rcu_read_unlock(); |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
| 3399 | /* |
| 3400 | * Calibrate registered applications. |
| 3401 | */ |
| 3402 | int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) |
| 3403 | { |
| 3404 | int ret = 0; |
| 3405 | struct lttng_ht_iter iter; |
| 3406 | struct ust_app *app; |
| 3407 | |
| 3408 | rcu_read_lock(); |
| 3409 | |
| 3410 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
| 3411 | if (!app->compatible) { |
| 3412 | /* |
| 3413 | * TODO: In time, we should notice the caller of this error by |
| 3414 | * telling him that this is a version error. |
| 3415 | */ |
| 3416 | continue; |
| 3417 | } |
| 3418 | |
| 3419 | health_code_update(); |
| 3420 | |
| 3421 | ret = ustctl_calibrate(app->sock, calibrate); |
| 3422 | if (ret < 0) { |
| 3423 | switch (ret) { |
| 3424 | case -ENOSYS: |
| 3425 | /* Means that it's not implemented on the tracer side. */ |
| 3426 | ret = 0; |
| 3427 | break; |
| 3428 | default: |
| 3429 | DBG2("Calibrate app PID %d returned with error %d", |
| 3430 | app->pid, ret); |
| 3431 | break; |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | DBG("UST app global domain calibration finished"); |
| 3437 | |
| 3438 | rcu_read_unlock(); |
| 3439 | |
| 3440 | health_code_update(); |
| 3441 | |
| 3442 | return ret; |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * Receive registration and populate the given msg structure. |
| 3447 | * |
| 3448 | * On success return 0 else a negative value returned by the ustctl call. |
| 3449 | */ |
| 3450 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) |
| 3451 | { |
| 3452 | int ret; |
| 3453 | uint32_t pid, ppid, uid, gid; |
| 3454 | |
| 3455 | assert(msg); |
| 3456 | |
| 3457 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, |
| 3458 | &pid, &ppid, &uid, &gid, |
| 3459 | &msg->bits_per_long, |
| 3460 | &msg->uint8_t_alignment, |
| 3461 | &msg->uint16_t_alignment, |
| 3462 | &msg->uint32_t_alignment, |
| 3463 | &msg->uint64_t_alignment, |
| 3464 | &msg->long_alignment, |
| 3465 | &msg->byte_order, |
| 3466 | msg->name); |
| 3467 | if (ret < 0) { |
| 3468 | switch (-ret) { |
| 3469 | case EPIPE: |
| 3470 | case ECONNRESET: |
| 3471 | case LTTNG_UST_ERR_EXITING: |
| 3472 | DBG3("UST app recv reg message failed. Application died"); |
| 3473 | break; |
| 3474 | case LTTNG_UST_ERR_UNSUP_MAJOR: |
| 3475 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", |
| 3476 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, |
| 3477 | LTTNG_UST_ABI_MINOR_VERSION); |
| 3478 | break; |
| 3479 | default: |
| 3480 | ERR("UST app recv reg message failed with ret %d", ret); |
| 3481 | break; |
| 3482 | } |
| 3483 | goto error; |
| 3484 | } |
| 3485 | msg->pid = (pid_t) pid; |
| 3486 | msg->ppid = (pid_t) ppid; |
| 3487 | msg->uid = (uid_t) uid; |
| 3488 | msg->gid = (gid_t) gid; |
| 3489 | |
| 3490 | error: |
| 3491 | return ret; |
| 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * Return a ust app channel object using the application object and the channel |
| 3496 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
| 3497 | * lock MUST be acquired before calling this function. |
| 3498 | */ |
| 3499 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
| 3500 | int objd) |
| 3501 | { |
| 3502 | struct lttng_ht_node_ulong *node; |
| 3503 | struct lttng_ht_iter iter; |
| 3504 | struct ust_app_channel *ua_chan = NULL; |
| 3505 | |
| 3506 | assert(app); |
| 3507 | |
| 3508 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); |
| 3509 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 3510 | if (node == NULL) { |
| 3511 | DBG2("UST app channel find by objd %d not found", objd); |
| 3512 | goto error; |
| 3513 | } |
| 3514 | |
| 3515 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); |
| 3516 | |
| 3517 | error: |
| 3518 | return ua_chan; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * Reply to a register channel notification from an application on the notify |
| 3523 | * socket. The channel metadata is also created. |
| 3524 | * |
| 3525 | * The session UST registry lock is acquired in this function. |
| 3526 | * |
| 3527 | * On success 0 is returned else a negative value. |
| 3528 | */ |
| 3529 | static int reply_ust_register_channel(int sock, int sobjd, int cobjd, |
| 3530 | size_t nr_fields, struct ustctl_field *fields) |
| 3531 | { |
| 3532 | int ret, ret_code = 0; |
| 3533 | uint32_t chan_id, reg_count; |
| 3534 | enum ustctl_channel_header type; |
| 3535 | struct ust_app *app; |
| 3536 | struct ust_app_channel *ua_chan; |
| 3537 | struct ust_app_session *ua_sess; |
| 3538 | |
| 3539 | rcu_read_lock(); |
| 3540 | |
| 3541 | /* Lookup application. If not found, there is a code flow error. */ |
| 3542 | app = find_app_by_notify_sock(sock); |
| 3543 | if (!app) { |
| 3544 | DBG("Application socket %d is being teardown. Abort event notify", |
| 3545 | sock); |
| 3546 | ret = 0; |
| 3547 | goto error_rcu_unlock; |
| 3548 | } |
| 3549 | |
| 3550 | /* Lookup channel by UST object descriptor. Should always be found. */ |
| 3551 | ua_chan = find_channel_by_objd(app, cobjd); |
| 3552 | assert(ua_chan); |
| 3553 | assert(ua_chan->session); |
| 3554 | ua_sess = ua_chan->session; |
| 3555 | assert(ua_sess); |
| 3556 | |
| 3557 | pthread_mutex_lock(&ua_sess->registry.lock); |
| 3558 | |
| 3559 | if (ust_registry_is_max_id(ua_chan->session->registry.used_channel_id)) { |
| 3560 | ret_code = -1; |
| 3561 | chan_id = -1U; |
| 3562 | type = -1; |
| 3563 | goto reply; |
| 3564 | } |
| 3565 | |
| 3566 | /* Don't assign ID to metadata. */ |
| 3567 | if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) { |
| 3568 | chan_id = -1U; |
| 3569 | } else { |
| 3570 | chan_id = ust_registry_get_next_chan_id(&ua_chan->session->registry); |
| 3571 | } |
| 3572 | |
| 3573 | reg_count = ust_registry_get_event_count(&ua_chan->registry); |
| 3574 | if (reg_count < 31) { |
| 3575 | type = USTCTL_CHANNEL_HEADER_COMPACT; |
| 3576 | } else { |
| 3577 | type = USTCTL_CHANNEL_HEADER_LARGE; |
| 3578 | } |
| 3579 | |
| 3580 | ua_chan->registry.nr_ctx_fields = nr_fields; |
| 3581 | ua_chan->registry.ctx_fields = fields; |
| 3582 | ua_chan->registry.chan_id = chan_id; |
| 3583 | ua_chan->registry.header_type = type; |
| 3584 | |
| 3585 | /* Append to metadata */ |
| 3586 | if (!ret_code) { |
| 3587 | ret_code = ust_metadata_channel_statedump(&ua_chan->session->registry, |
| 3588 | &ua_chan->registry); |
| 3589 | if (ret_code) { |
| 3590 | ERR("Error appending channel metadata (errno = %d)", ret_code); |
| 3591 | goto reply; |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | reply: |
| 3596 | DBG3("UST app replying to register channel with id %u, type: %d, ret: %d", |
| 3597 | chan_id, type, ret_code); |
| 3598 | |
| 3599 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); |
| 3600 | if (ret < 0) { |
| 3601 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3602 | ERR("UST app reply channel failed with ret %d", ret); |
| 3603 | } else { |
| 3604 | DBG3("UST app reply channel failed. Application died"); |
| 3605 | } |
| 3606 | goto error; |
| 3607 | } |
| 3608 | |
| 3609 | error: |
| 3610 | pthread_mutex_unlock(&ua_sess->registry.lock); |
| 3611 | error_rcu_unlock: |
| 3612 | rcu_read_unlock(); |
| 3613 | return ret; |
| 3614 | } |
| 3615 | |
| 3616 | /* |
| 3617 | * Add event to the UST channel registry. When the event is added to the |
| 3618 | * registry, the metadata is also created. Once done, this replies to the |
| 3619 | * application with the appropriate error code. |
| 3620 | * |
| 3621 | * The session UST registry lock is acquired in the function. |
| 3622 | * |
| 3623 | * On success 0 is returned else a negative value. |
| 3624 | */ |
| 3625 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
| 3626 | char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel, |
| 3627 | char *model_emf_uri) |
| 3628 | { |
| 3629 | int ret, ret_code; |
| 3630 | uint32_t event_id = 0; |
| 3631 | struct ust_app *app; |
| 3632 | struct ust_app_channel *ua_chan; |
| 3633 | struct ust_app_session *ua_sess; |
| 3634 | |
| 3635 | rcu_read_lock(); |
| 3636 | |
| 3637 | /* Lookup application. If not found, there is a code flow error. */ |
| 3638 | app = find_app_by_notify_sock(sock); |
| 3639 | if (!app) { |
| 3640 | DBG("Application socket %d is being teardown. Abort event notify", |
| 3641 | sock); |
| 3642 | ret = 0; |
| 3643 | goto error_rcu_unlock; |
| 3644 | } |
| 3645 | |
| 3646 | /* Lookup channel by UST object descriptor. Should always be found. */ |
| 3647 | ua_chan = find_channel_by_objd(app, cobjd); |
| 3648 | assert(ua_chan); |
| 3649 | assert(ua_chan->session); |
| 3650 | ua_sess = ua_chan->session; |
| 3651 | |
| 3652 | pthread_mutex_lock(&ua_sess->registry.lock); |
| 3653 | |
| 3654 | ret_code = ust_registry_create_event(&ua_sess->registry, |
| 3655 | &ua_chan->registry, sobjd, cobjd, name, sig, nr_fields, fields, |
| 3656 | loglevel, model_emf_uri, &event_id); |
| 3657 | |
| 3658 | /* |
| 3659 | * The return value is returned to ustctl so in case of an error, the |
| 3660 | * application can be notified. In case of an error, it's important not to |
| 3661 | * return a negative error or else the application will get closed. |
| 3662 | */ |
| 3663 | ret = ustctl_reply_register_event(sock, event_id, ret_code); |
| 3664 | if (ret < 0) { |
| 3665 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3666 | ERR("UST app reply event failed with ret %d", ret); |
| 3667 | } else { |
| 3668 | DBG3("UST app reply event failed. Application died"); |
| 3669 | } |
| 3670 | /* |
| 3671 | * No need to wipe the create event since the application socket will |
| 3672 | * get close on error hence cleaning up everything by itself. |
| 3673 | */ |
| 3674 | goto error; |
| 3675 | } |
| 3676 | |
| 3677 | DBG3("UST registry event %s has been added successfully", name); |
| 3678 | |
| 3679 | error: |
| 3680 | pthread_mutex_unlock(&ua_sess->registry.lock); |
| 3681 | error_rcu_unlock: |
| 3682 | rcu_read_unlock(); |
| 3683 | return ret; |
| 3684 | } |
| 3685 | |
| 3686 | /* |
| 3687 | * Handle application notification through the given notify socket. |
| 3688 | * |
| 3689 | * Return 0 on success or else a negative value. |
| 3690 | */ |
| 3691 | int ust_app_recv_notify(int sock) |
| 3692 | { |
| 3693 | int ret; |
| 3694 | enum ustctl_notify_cmd cmd; |
| 3695 | |
| 3696 | DBG3("UST app receiving notify from sock %d", sock); |
| 3697 | |
| 3698 | ret = ustctl_recv_notify(sock, &cmd); |
| 3699 | if (ret < 0) { |
| 3700 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3701 | ERR("UST app recv notify failed with ret %d", ret); |
| 3702 | } else { |
| 3703 | DBG3("UST app recv notify failed. Application died"); |
| 3704 | } |
| 3705 | goto error; |
| 3706 | } |
| 3707 | |
| 3708 | switch (cmd) { |
| 3709 | case USTCTL_NOTIFY_CMD_EVENT: |
| 3710 | { |
| 3711 | int sobjd, cobjd, loglevel; |
| 3712 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
| 3713 | size_t nr_fields; |
| 3714 | struct ustctl_field *fields; |
| 3715 | |
| 3716 | DBG2("UST app ustctl register event received"); |
| 3717 | |
| 3718 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel, |
| 3719 | &sig, &nr_fields, &fields, &model_emf_uri); |
| 3720 | if (ret < 0) { |
| 3721 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3722 | ERR("UST app recv event failed with ret %d", ret); |
| 3723 | } else { |
| 3724 | DBG3("UST app recv event failed. Application died"); |
| 3725 | } |
| 3726 | goto error; |
| 3727 | } |
| 3728 | |
| 3729 | /* Add event to the UST registry coming from the notify socket. */ |
| 3730 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
| 3731 | fields, loglevel, model_emf_uri); |
| 3732 | if (ret < 0) { |
| 3733 | goto error; |
| 3734 | } |
| 3735 | |
| 3736 | break; |
| 3737 | } |
| 3738 | case USTCTL_NOTIFY_CMD_CHANNEL: |
| 3739 | { |
| 3740 | int sobjd, cobjd; |
| 3741 | size_t nr_fields; |
| 3742 | struct ustctl_field *fields; |
| 3743 | |
| 3744 | DBG2("UST app ustctl register channel received"); |
| 3745 | |
| 3746 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, |
| 3747 | &fields); |
| 3748 | if (ret < 0) { |
| 3749 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 3750 | ERR("UST app recv channel failed with ret %d", ret); |
| 3751 | } else { |
| 3752 | DBG3("UST app recv channel failed. Application died"); |
| 3753 | } |
| 3754 | goto error; |
| 3755 | } |
| 3756 | |
| 3757 | ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields, |
| 3758 | fields); |
| 3759 | if (ret < 0) { |
| 3760 | goto error; |
| 3761 | } |
| 3762 | |
| 3763 | break; |
| 3764 | } |
| 3765 | default: |
| 3766 | /* Should NEVER happen. */ |
| 3767 | assert(0); |
| 3768 | } |
| 3769 | |
| 3770 | error: |
| 3771 | return ret; |
| 3772 | } |
| 3773 | |
| 3774 | /* |
| 3775 | * Once the notify socket hangs up, this is called. First, it tries to find the |
| 3776 | * corresponding application. On failure, the call_rcu to close the socket is |
| 3777 | * executed. If an application is found, it tries to delete it from the notify |
| 3778 | * socket hash table. Whathever the result, it proceeds to the call_rcu. |
| 3779 | * |
| 3780 | * Note that an object needs to be allocated here so on ENOMEM failure, the |
| 3781 | * call RCU is not done but the rest of the cleanup is. |
| 3782 | */ |
| 3783 | void ust_app_notify_sock_unregister(int sock) |
| 3784 | { |
| 3785 | int err_enomem = 0; |
| 3786 | struct lttng_ht_iter iter; |
| 3787 | struct ust_app *app; |
| 3788 | struct ust_app_notify_sock_obj *obj; |
| 3789 | |
| 3790 | assert(sock >= 0); |
| 3791 | |
| 3792 | rcu_read_lock(); |
| 3793 | |
| 3794 | obj = zmalloc(sizeof(*obj)); |
| 3795 | if (!obj) { |
| 3796 | /* |
| 3797 | * An ENOMEM is kind of uncool. If this strikes we continue the |
| 3798 | * procedure but the call_rcu will not be called. In this case, we |
| 3799 | * accept the fd leak rather than possibly creating an unsynchronized |
| 3800 | * state between threads. |
| 3801 | * |
| 3802 | * TODO: The notify object should be created once the notify socket is |
| 3803 | * registered and stored independantely from the ust app object. The |
| 3804 | * tricky part is to synchronize the teardown of the application and |
| 3805 | * this notify object. Let's keep that in mind so we can avoid this |
| 3806 | * kind of shenanigans with ENOMEM in the teardown path. |
| 3807 | */ |
| 3808 | err_enomem = 1; |
| 3809 | } else { |
| 3810 | obj->fd = sock; |
| 3811 | } |
| 3812 | |
| 3813 | DBG("UST app notify socket unregister %d", sock); |
| 3814 | |
| 3815 | /* |
| 3816 | * Lookup application by notify socket. If this fails, this means that the |
| 3817 | * hash table delete has already been done by the application |
| 3818 | * unregistration process so we can safely close the notify socket in a |
| 3819 | * call RCU. |
| 3820 | */ |
| 3821 | app = find_app_by_notify_sock(sock); |
| 3822 | if (!app) { |
| 3823 | goto close_socket; |
| 3824 | } |
| 3825 | |
| 3826 | iter.iter.node = &app->notify_sock_n.node; |
| 3827 | |
| 3828 | /* |
| 3829 | * Whatever happens here either we fail or succeed, in both cases we have |
| 3830 | * to close the socket after a grace period to continue to the call RCU |
| 3831 | * here. If the deletion is successful, the application is not visible |
| 3832 | * anymore by other threads and is it fails it means that it was already |
| 3833 | * deleted from the hash table so either way we just have to close the |
| 3834 | * socket. |
| 3835 | */ |
| 3836 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); |
| 3837 | |
| 3838 | close_socket: |
| 3839 | rcu_read_unlock(); |
| 3840 | |
| 3841 | /* |
| 3842 | * Close socket after a grace period to avoid for the socket to be reused |
| 3843 | * before the application object is freed creating potential race between |
| 3844 | * threads trying to add unique in the global hash table. |
| 3845 | */ |
| 3846 | if (!err_enomem) { |
| 3847 | call_rcu(&obj->head, close_notify_sock_rcu); |
| 3848 | } |
| 3849 | } |