| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include "agent-thread.hpp" |
| 11 | #include "agent.hpp" |
| 12 | #include "buffer-registry.hpp" |
| 13 | #include "channel.hpp" |
| 14 | #include "cmd.hpp" |
| 15 | #include "consumer.hpp" |
| 16 | #include "event-notifier-error-accounting.hpp" |
| 17 | #include "event.hpp" |
| 18 | #include "health-sessiond.hpp" |
| 19 | #include "kernel-consumer.hpp" |
| 20 | #include "kernel.hpp" |
| 21 | #include "lttng-sessiond.hpp" |
| 22 | #include "lttng-syscall.hpp" |
| 23 | #include "notification-thread-commands.hpp" |
| 24 | #include "notification-thread.hpp" |
| 25 | #include "rotation-thread.hpp" |
| 26 | #include "session.hpp" |
| 27 | #include "timer.hpp" |
| 28 | #include "tracker.hpp" |
| 29 | #include "utils.hpp" |
| 30 | |
| 31 | #include <common/buffer-view.hpp> |
| 32 | #include <common/common.hpp> |
| 33 | #include <common/compat/string.hpp> |
| 34 | #include <common/defaults.hpp> |
| 35 | #include <common/dynamic-buffer.hpp> |
| 36 | #include <common/kernel-ctl/kernel-ctl.hpp> |
| 37 | #include <common/payload-view.hpp> |
| 38 | #include <common/payload.hpp> |
| 39 | #include <common/relayd/relayd.hpp> |
| 40 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 41 | #include <common/string-utils/string-utils.hpp> |
| 42 | #include <common/trace-chunk.hpp> |
| 43 | #include <common/urcu.hpp> |
| 44 | #include <common/utils.hpp> |
| 45 | |
| 46 | #include <lttng/action/action-internal.hpp> |
| 47 | #include <lttng/action/action.h> |
| 48 | #include <lttng/channel-internal.hpp> |
| 49 | #include <lttng/channel.h> |
| 50 | #include <lttng/condition/condition-internal.hpp> |
| 51 | #include <lttng/condition/condition.h> |
| 52 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 53 | #include <lttng/condition/event-rule-matches.h> |
| 54 | #include <lttng/error-query-internal.hpp> |
| 55 | #include <lttng/event-internal.hpp> |
| 56 | #include <lttng/event-rule/event-rule-internal.hpp> |
| 57 | #include <lttng/event-rule/event-rule.h> |
| 58 | #include <lttng/location-internal.hpp> |
| 59 | #include <lttng/lttng-error.h> |
| 60 | #include <lttng/rotate-internal.hpp> |
| 61 | #include <lttng/session-descriptor-internal.hpp> |
| 62 | #include <lttng/session-internal.hpp> |
| 63 | #include <lttng/tracker.h> |
| 64 | #include <lttng/trigger/trigger-internal.hpp> |
| 65 | #include <lttng/userspace-probe-internal.hpp> |
| 66 | |
| 67 | #include <algorithm> |
| 68 | #include <inttypes.h> |
| 69 | #include <stdio.h> |
| 70 | #include <sys/stat.h> |
| 71 | #include <urcu/list.h> |
| 72 | #include <urcu/uatomic.h> |
| 73 | |
| 74 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
| 75 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 |
| 76 | |
| 77 | namespace lsu = lttng::sessiond::ust; |
| 78 | |
| 79 | static enum lttng_error_code wait_on_path(void *path); |
| 80 | |
| 81 | namespace { |
| 82 | struct cmd_destroy_session_reply_context { |
| 83 | int reply_sock_fd; |
| 84 | bool implicit_rotation_on_destroy; |
| 85 | /* |
| 86 | * Indicates whether or not an error occurred while launching the |
| 87 | * destruction of a session. |
| 88 | */ |
| 89 | enum lttng_error_code destruction_status; |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * Command completion handler that is used by the destroy command |
| 94 | * when a session that has a non-default shm_path is being destroyed. |
| 95 | * |
| 96 | * See comment in cmd_destroy_session() for the rationale. |
| 97 | */ |
| 98 | struct destroy_completion_handler { |
| 99 | struct cmd_completion_handler handler; |
| 100 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; |
| 101 | } destroy_completion_handler = { |
| 102 | .handler = { .run = wait_on_path, .data = destroy_completion_handler.shm_path }, |
| 103 | .shm_path = { 0 }, |
| 104 | }; |
| 105 | |
| 106 | /* |
| 107 | * Used to keep a unique index for each relayd socket created where this value |
| 108 | * is associated with streams on the consumer so it can match the right relayd |
| 109 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
| 110 | * held. |
| 111 | */ |
| 112 | pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
| 113 | uint64_t relayd_net_seq_idx; |
| 114 | } /* namespace */ |
| 115 | |
| 116 | static struct cmd_completion_handler *current_completion_handler; |
| 117 | static int validate_ust_event_name(const char *); |
| 118 | static int cmd_enable_event_internal(struct ltt_session *session, |
| 119 | const struct lttng_domain *domain, |
| 120 | char *channel_name, |
| 121 | struct lttng_event *event, |
| 122 | char *filter_expression, |
| 123 | struct lttng_bytecode *filter, |
| 124 | struct lttng_event_exclusion *exclusion, |
| 125 | int wpipe); |
| 126 | static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session, |
| 127 | const struct lttng_domain *domain, |
| 128 | const struct lttng_channel *_attr, |
| 129 | int wpipe); |
| 130 | |
| 131 | /* |
| 132 | * Create a session path used by list_lttng_sessions for the case that the |
| 133 | * session consumer is on the network. |
| 134 | */ |
| 135 | static int build_network_session_path(char *dst, size_t size, struct ltt_session *session) |
| 136 | { |
| 137 | int ret, kdata_port, udata_port; |
| 138 | struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr; |
| 139 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; |
| 140 | |
| 141 | LTTNG_ASSERT(session); |
| 142 | LTTNG_ASSERT(dst); |
| 143 | |
| 144 | memset(tmp_urls, 0, sizeof(tmp_urls)); |
| 145 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); |
| 146 | |
| 147 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; |
| 148 | |
| 149 | if (session->kernel_session && session->kernel_session->consumer) { |
| 150 | kuri = &session->kernel_session->consumer->dst.net.control; |
| 151 | kdata_port = session->kernel_session->consumer->dst.net.data.port; |
| 152 | } |
| 153 | |
| 154 | if (session->ust_session && session->ust_session->consumer) { |
| 155 | uuri = &session->ust_session->consumer->dst.net.control; |
| 156 | udata_port = session->ust_session->consumer->dst.net.data.port; |
| 157 | } |
| 158 | |
| 159 | if (uuri == nullptr && kuri == nullptr) { |
| 160 | uri = &session->consumer->dst.net.control; |
| 161 | kdata_port = session->consumer->dst.net.data.port; |
| 162 | } else if (kuri && uuri) { |
| 163 | ret = uri_compare(kuri, uuri); |
| 164 | if (ret) { |
| 165 | /* Not Equal */ |
| 166 | uri = kuri; |
| 167 | /* Build uuri URL string */ |
| 168 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); |
| 169 | if (ret < 0) { |
| 170 | goto error; |
| 171 | } |
| 172 | } else { |
| 173 | uri = kuri; |
| 174 | } |
| 175 | } else if (kuri && uuri == nullptr) { |
| 176 | uri = kuri; |
| 177 | } else if (uuri && kuri == nullptr) { |
| 178 | uri = uuri; |
| 179 | } |
| 180 | |
| 181 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); |
| 182 | if (ret < 0) { |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Do we have a UST url set. If yes, this means we have both kernel and UST |
| 188 | * to print. |
| 189 | */ |
| 190 | if (*tmp_uurl != '\0') { |
| 191 | ret = snprintf(dst, |
| 192 | size, |
| 193 | "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
| 194 | tmp_urls, |
| 195 | kdata_port, |
| 196 | tmp_uurl, |
| 197 | udata_port); |
| 198 | } else { |
| 199 | int dport; |
| 200 | if (kuri || (!kuri && !uuri)) { |
| 201 | dport = kdata_port; |
| 202 | } else { |
| 203 | /* No kernel URI, use the UST port. */ |
| 204 | dport = udata_port; |
| 205 | } |
| 206 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); |
| 207 | } |
| 208 | |
| 209 | error: |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Get run-time attributes if the session has been started (discarded events, |
| 215 | * lost packets). |
| 216 | */ |
| 217 | static int get_kernel_runtime_stats(struct ltt_session *session, |
| 218 | struct ltt_kernel_channel *kchan, |
| 219 | uint64_t *discarded_events, |
| 220 | uint64_t *lost_packets) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | if (!session->has_been_started) { |
| 225 | ret = 0; |
| 226 | *discarded_events = 0; |
| 227 | *lost_packets = 0; |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | ret = consumer_get_discarded_events( |
| 232 | session->id, kchan->key, session->kernel_session->consumer, discarded_events); |
| 233 | if (ret < 0) { |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | ret = consumer_get_lost_packets( |
| 238 | session->id, kchan->key, session->kernel_session->consumer, lost_packets); |
| 239 | if (ret < 0) { |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Get run-time attributes if the session has been started (discarded events, |
| 249 | * lost packets). |
| 250 | */ |
| 251 | static int get_ust_runtime_stats(struct ltt_session *session, |
| 252 | struct ltt_ust_channel *uchan, |
| 253 | uint64_t *discarded_events, |
| 254 | uint64_t *lost_packets) |
| 255 | { |
| 256 | int ret; |
| 257 | struct ltt_ust_session *usess; |
| 258 | |
| 259 | if (!discarded_events || !lost_packets) { |
| 260 | ret = -1; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | usess = session->ust_session; |
| 265 | LTTNG_ASSERT(discarded_events); |
| 266 | LTTNG_ASSERT(lost_packets); |
| 267 | |
| 268 | if (!usess || !session->has_been_started) { |
| 269 | *discarded_events = 0; |
| 270 | *lost_packets = 0; |
| 271 | ret = 0; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 276 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, |
| 277 | &usess->buffer_reg_uid_list, |
| 278 | usess->consumer, |
| 279 | uchan->id, |
| 280 | uchan->attr.overwrite, |
| 281 | discarded_events, |
| 282 | lost_packets); |
| 283 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 284 | ret = ust_app_pid_get_channel_runtime_stats(usess, |
| 285 | uchan, |
| 286 | usess->consumer, |
| 287 | uchan->attr.overwrite, |
| 288 | discarded_events, |
| 289 | lost_packets); |
| 290 | if (ret < 0) { |
| 291 | goto end; |
| 292 | } |
| 293 | *discarded_events += uchan->per_pid_closed_app_discarded; |
| 294 | *lost_packets += uchan->per_pid_closed_app_lost; |
| 295 | } else { |
| 296 | ERR("Unsupported buffer type"); |
| 297 | abort(); |
| 298 | ret = -1; |
| 299 | goto end; |
| 300 | } |
| 301 | |
| 302 | end: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Create a list of agent domain events. |
| 308 | * |
| 309 | * Return number of events in list on success or else a negative value. |
| 310 | */ |
| 311 | static enum lttng_error_code list_lttng_agent_events(struct agent *agt, |
| 312 | struct lttng_payload *reply_payload, |
| 313 | unsigned int *nb_events) |
| 314 | { |
| 315 | enum lttng_error_code ret_code; |
| 316 | int ret = 0; |
| 317 | unsigned int local_nb_events = 0; |
| 318 | struct agent_event *event; |
| 319 | struct lttng_ht_iter iter; |
| 320 | unsigned long agent_event_count; |
| 321 | |
| 322 | assert(agt); |
| 323 | assert(reply_payload); |
| 324 | |
| 325 | DBG3("Listing agent events"); |
| 326 | |
| 327 | agent_event_count = lttng_ht_get_count(agt->events); |
| 328 | if (agent_event_count == 0) { |
| 329 | /* Early exit. */ |
| 330 | goto end; |
| 331 | } |
| 332 | |
| 333 | if (agent_event_count > UINT_MAX) { |
| 334 | ret_code = LTTNG_ERR_OVERFLOW; |
| 335 | goto error; |
| 336 | } |
| 337 | |
| 338 | local_nb_events = (unsigned int) agent_event_count; |
| 339 | |
| 340 | { |
| 341 | lttng::urcu::read_lock_guard read_lock; |
| 342 | |
| 343 | cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) { |
| 344 | struct lttng_event *tmp_event = lttng_event_create(); |
| 345 | |
| 346 | if (!tmp_event) { |
| 347 | ret_code = LTTNG_ERR_NOMEM; |
| 348 | goto error; |
| 349 | } |
| 350 | |
| 351 | if (lttng_strncpy(tmp_event->name, event->name, sizeof(tmp_event->name))) { |
| 352 | lttng_event_destroy(tmp_event); |
| 353 | ret_code = LTTNG_ERR_FATAL; |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | tmp_event->name[sizeof(tmp_event->name) - 1] = '\0'; |
| 358 | tmp_event->enabled = !!event->enabled_count; |
| 359 | tmp_event->loglevel = event->loglevel_value; |
| 360 | tmp_event->loglevel_type = event->loglevel_type; |
| 361 | |
| 362 | ret = lttng_event_serialize(tmp_event, |
| 363 | 0, |
| 364 | nullptr, |
| 365 | event->filter_expression, |
| 366 | 0, |
| 367 | nullptr, |
| 368 | reply_payload); |
| 369 | lttng_event_destroy(tmp_event); |
| 370 | if (ret) { |
| 371 | ret_code = LTTNG_ERR_FATAL; |
| 372 | goto error; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | end: |
| 377 | ret_code = LTTNG_OK; |
| 378 | *nb_events = local_nb_events; |
| 379 | error: |
| 380 | return ret_code; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Create a list of ust global domain events. |
| 385 | */ |
| 386 | static enum lttng_error_code list_lttng_ust_global_events(char *channel_name, |
| 387 | struct ltt_ust_domain_global *ust_global, |
| 388 | struct lttng_payload *reply_payload, |
| 389 | unsigned int *nb_events) |
| 390 | { |
| 391 | enum lttng_error_code ret_code; |
| 392 | int ret; |
| 393 | struct lttng_ht_iter iter; |
| 394 | struct lttng_ht_node_str *node; |
| 395 | struct ltt_ust_channel *uchan; |
| 396 | struct ltt_ust_event *uevent; |
| 397 | unsigned long channel_event_count; |
| 398 | unsigned int local_nb_events = 0; |
| 399 | |
| 400 | assert(reply_payload); |
| 401 | assert(nb_events); |
| 402 | |
| 403 | DBG("Listing UST global events for channel %s", channel_name); |
| 404 | |
| 405 | lttng::urcu::read_lock_guard read_lock; |
| 406 | |
| 407 | lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter); |
| 408 | node = lttng_ht_iter_get_node_str(&iter); |
| 409 | if (node == nullptr) { |
| 410 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
| 415 | |
| 416 | channel_event_count = lttng_ht_get_count(uchan->events); |
| 417 | if (channel_event_count == 0) { |
| 418 | /* Early exit. */ |
| 419 | ret_code = LTTNG_OK; |
| 420 | goto end; |
| 421 | } |
| 422 | |
| 423 | if (channel_event_count > UINT_MAX) { |
| 424 | ret_code = LTTNG_ERR_OVERFLOW; |
| 425 | goto error; |
| 426 | } |
| 427 | |
| 428 | local_nb_events = (unsigned int) channel_event_count; |
| 429 | |
| 430 | DBG3("Listing UST global %d events", *nb_events); |
| 431 | |
| 432 | cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) { |
| 433 | struct lttng_event *tmp_event = nullptr; |
| 434 | |
| 435 | if (uevent->internal) { |
| 436 | /* This event should remain hidden from clients */ |
| 437 | local_nb_events--; |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | tmp_event = lttng_event_create(); |
| 442 | if (!tmp_event) { |
| 443 | ret_code = LTTNG_ERR_NOMEM; |
| 444 | goto error; |
| 445 | } |
| 446 | |
| 447 | if (lttng_strncpy(tmp_event->name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN)) { |
| 448 | ret_code = LTTNG_ERR_FATAL; |
| 449 | lttng_event_destroy(tmp_event); |
| 450 | goto error; |
| 451 | } |
| 452 | |
| 453 | tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 454 | tmp_event->enabled = uevent->enabled; |
| 455 | |
| 456 | switch (uevent->attr.instrumentation) { |
| 457 | case LTTNG_UST_ABI_TRACEPOINT: |
| 458 | tmp_event->type = LTTNG_EVENT_TRACEPOINT; |
| 459 | break; |
| 460 | case LTTNG_UST_ABI_PROBE: |
| 461 | tmp_event->type = LTTNG_EVENT_PROBE; |
| 462 | break; |
| 463 | case LTTNG_UST_ABI_FUNCTION: |
| 464 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | tmp_event->loglevel = uevent->attr.loglevel; |
| 469 | switch (uevent->attr.loglevel_type) { |
| 470 | case LTTNG_UST_ABI_LOGLEVEL_ALL: |
| 471 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 472 | break; |
| 473 | case LTTNG_UST_ABI_LOGLEVEL_RANGE: |
| 474 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 475 | break; |
| 476 | case LTTNG_UST_ABI_LOGLEVEL_SINGLE: |
| 477 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 478 | break; |
| 479 | } |
| 480 | if (uevent->filter) { |
| 481 | tmp_event->filter = 1; |
| 482 | } |
| 483 | if (uevent->exclusion) { |
| 484 | tmp_event->exclusion = 1; |
| 485 | } |
| 486 | |
| 487 | std::vector<const char *> exclusion_names; |
| 488 | if (uevent->exclusion) { |
| 489 | for (int i = 0; i < uevent->exclusion->count; i++) { |
| 490 | exclusion_names.emplace_back( |
| 491 | LTTNG_EVENT_EXCLUSION_NAME_AT(uevent->exclusion, i)); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * We do not care about the filter bytecode and the fd from the |
| 497 | * userspace_probe_location. |
| 498 | */ |
| 499 | ret = lttng_event_serialize(tmp_event, |
| 500 | exclusion_names.size(), |
| 501 | exclusion_names.size() ? exclusion_names.data() : |
| 502 | nullptr, |
| 503 | uevent->filter_expression, |
| 504 | 0, |
| 505 | nullptr, |
| 506 | reply_payload); |
| 507 | lttng_event_destroy(tmp_event); |
| 508 | if (ret) { |
| 509 | ret_code = LTTNG_ERR_FATAL; |
| 510 | goto error; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | end: |
| 515 | /* nb_events is already set at this point. */ |
| 516 | ret_code = LTTNG_OK; |
| 517 | *nb_events = local_nb_events; |
| 518 | error: |
| 519 | return ret_code; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Fill lttng_event array of all kernel events in the channel. |
| 524 | */ |
| 525 | static enum lttng_error_code list_lttng_kernel_events(char *channel_name, |
| 526 | struct ltt_kernel_session *kernel_session, |
| 527 | struct lttng_payload *reply_payload, |
| 528 | unsigned int *nb_events) |
| 529 | { |
| 530 | enum lttng_error_code ret_code; |
| 531 | int ret; |
| 532 | struct ltt_kernel_event *event; |
| 533 | struct ltt_kernel_channel *kchan; |
| 534 | |
| 535 | assert(reply_payload); |
| 536 | |
| 537 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); |
| 538 | if (kchan == nullptr) { |
| 539 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 540 | goto end; |
| 541 | } |
| 542 | |
| 543 | *nb_events = kchan->event_count; |
| 544 | |
| 545 | DBG("Listing events for channel %s", kchan->channel->name); |
| 546 | |
| 547 | if (*nb_events == 0) { |
| 548 | ret_code = LTTNG_OK; |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | /* Kernel channels */ |
| 553 | cds_list_for_each_entry (event, &kchan->events_list.head, list) { |
| 554 | struct lttng_event *tmp_event = lttng_event_create(); |
| 555 | |
| 556 | if (!tmp_event) { |
| 557 | ret_code = LTTNG_ERR_NOMEM; |
| 558 | goto end; |
| 559 | } |
| 560 | |
| 561 | if (lttng_strncpy(tmp_event->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) { |
| 562 | lttng_event_destroy(tmp_event); |
| 563 | ret_code = LTTNG_ERR_FATAL; |
| 564 | goto end; |
| 565 | } |
| 566 | |
| 567 | tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 568 | tmp_event->enabled = event->enabled; |
| 569 | tmp_event->filter = (unsigned char) !!event->filter_expression; |
| 570 | |
| 571 | switch (event->event->instrumentation) { |
| 572 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 573 | tmp_event->type = LTTNG_EVENT_TRACEPOINT; |
| 574 | break; |
| 575 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 576 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 577 | memcpy(&tmp_event->attr.probe, |
| 578 | &event->event->u.kprobe, |
| 579 | sizeof(struct lttng_kernel_abi_kprobe)); |
| 580 | break; |
| 581 | case LTTNG_KERNEL_ABI_KPROBE: |
| 582 | tmp_event->type = LTTNG_EVENT_PROBE; |
| 583 | memcpy(&tmp_event->attr.probe, |
| 584 | &event->event->u.kprobe, |
| 585 | sizeof(struct lttng_kernel_abi_kprobe)); |
| 586 | break; |
| 587 | case LTTNG_KERNEL_ABI_UPROBE: |
| 588 | tmp_event->type = LTTNG_EVENT_USERSPACE_PROBE; |
| 589 | break; |
| 590 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 591 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 592 | memcpy(&(tmp_event->attr.ftrace), |
| 593 | &event->event->u.ftrace, |
| 594 | sizeof(struct lttng_kernel_abi_function)); |
| 595 | break; |
| 596 | case LTTNG_KERNEL_ABI_NOOP: |
| 597 | tmp_event->type = LTTNG_EVENT_NOOP; |
| 598 | break; |
| 599 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 600 | tmp_event->type = LTTNG_EVENT_SYSCALL; |
| 601 | break; |
| 602 | case LTTNG_KERNEL_ABI_ALL: |
| 603 | /* fall-through. */ |
| 604 | default: |
| 605 | abort(); |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | if (event->userspace_probe_location) { |
| 610 | struct lttng_userspace_probe_location *location_copy = |
| 611 | lttng_userspace_probe_location_copy( |
| 612 | event->userspace_probe_location); |
| 613 | |
| 614 | if (!location_copy) { |
| 615 | lttng_event_destroy(tmp_event); |
| 616 | ret_code = LTTNG_ERR_NOMEM; |
| 617 | goto end; |
| 618 | } |
| 619 | |
| 620 | ret = lttng_event_set_userspace_probe_location(tmp_event, location_copy); |
| 621 | if (ret) { |
| 622 | lttng_event_destroy(tmp_event); |
| 623 | lttng_userspace_probe_location_destroy(location_copy); |
| 624 | ret_code = LTTNG_ERR_INVALID; |
| 625 | goto end; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | ret = lttng_event_serialize( |
| 630 | tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload); |
| 631 | lttng_event_destroy(tmp_event); |
| 632 | if (ret) { |
| 633 | ret_code = LTTNG_ERR_FATAL; |
| 634 | goto end; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | ret_code = LTTNG_OK; |
| 639 | end: |
| 640 | return ret_code; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Add URI so the consumer output object. Set the correct path depending on the |
| 645 | * domain adding the default trace directory. |
| 646 | */ |
| 647 | static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session, |
| 648 | struct consumer_output *consumer, |
| 649 | struct lttng_uri *uri, |
| 650 | enum lttng_domain_type domain) |
| 651 | { |
| 652 | int ret; |
| 653 | enum lttng_error_code ret_code = LTTNG_OK; |
| 654 | |
| 655 | LTTNG_ASSERT(uri); |
| 656 | |
| 657 | if (consumer == nullptr) { |
| 658 | DBG("No consumer detected. Don't add URI. Stopping."); |
| 659 | ret_code = LTTNG_ERR_NO_CONSUMER; |
| 660 | goto error; |
| 661 | } |
| 662 | |
| 663 | switch (domain) { |
| 664 | case LTTNG_DOMAIN_KERNEL: |
| 665 | ret = lttng_strncpy(consumer->domain_subdir, |
| 666 | DEFAULT_KERNEL_TRACE_DIR, |
| 667 | sizeof(consumer->domain_subdir)); |
| 668 | break; |
| 669 | case LTTNG_DOMAIN_UST: |
| 670 | ret = lttng_strncpy(consumer->domain_subdir, |
| 671 | DEFAULT_UST_TRACE_DIR, |
| 672 | sizeof(consumer->domain_subdir)); |
| 673 | break; |
| 674 | default: |
| 675 | /* |
| 676 | * This case is possible is we try to add the URI to the global |
| 677 | * tracing session consumer object which in this case there is |
| 678 | * no subdir. |
| 679 | */ |
| 680 | memset(consumer->domain_subdir, 0, sizeof(consumer->domain_subdir)); |
| 681 | ret = 0; |
| 682 | } |
| 683 | if (ret) { |
| 684 | ERR("Failed to initialize consumer output domain subdirectory"); |
| 685 | ret_code = LTTNG_ERR_FATAL; |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | switch (uri->dtype) { |
| 690 | case LTTNG_DST_IPV4: |
| 691 | case LTTNG_DST_IPV6: |
| 692 | DBG2("Setting network URI to consumer"); |
| 693 | |
| 694 | if (consumer->type == CONSUMER_DST_NET) { |
| 695 | if ((uri->stype == LTTNG_STREAM_CONTROL && |
| 696 | consumer->dst.net.control_isset) || |
| 697 | (uri->stype == LTTNG_STREAM_DATA && consumer->dst.net.data_isset)) { |
| 698 | ret_code = LTTNG_ERR_URL_EXIST; |
| 699 | goto error; |
| 700 | } |
| 701 | } else { |
| 702 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
| 703 | } |
| 704 | |
| 705 | /* Set URI into consumer output object */ |
| 706 | ret = consumer_set_network_uri(session, consumer, uri); |
| 707 | if (ret < 0) { |
| 708 | ret_code = (lttng_error_code) -ret; |
| 709 | goto error; |
| 710 | } else if (ret == 1) { |
| 711 | /* |
| 712 | * URI was the same in the consumer so we do not append the subdir |
| 713 | * again so to not duplicate output dir. |
| 714 | */ |
| 715 | ret_code = LTTNG_OK; |
| 716 | goto error; |
| 717 | } |
| 718 | break; |
| 719 | case LTTNG_DST_PATH: |
| 720 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
| 721 | ret_code = LTTNG_ERR_INVALID; |
| 722 | goto error; |
| 723 | } |
| 724 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); |
| 725 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
| 726 | |
| 727 | ret = lttng_strncpy(consumer->dst.session_root_path, |
| 728 | uri->dst.path, |
| 729 | sizeof(consumer->dst.session_root_path)); |
| 730 | if (ret) { |
| 731 | ret_code = LTTNG_ERR_FATAL; |
| 732 | goto error; |
| 733 | } |
| 734 | consumer->type = CONSUMER_DST_LOCAL; |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | ret_code = LTTNG_OK; |
| 739 | error: |
| 740 | return ret_code; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Init tracing by creating trace directory and sending fds kernel consumer. |
| 745 | */ |
| 746 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
| 747 | { |
| 748 | int ret = 0; |
| 749 | struct lttng_ht_iter iter; |
| 750 | struct consumer_socket *socket; |
| 751 | |
| 752 | LTTNG_ASSERT(session); |
| 753 | |
| 754 | if (session->consumer_fds_sent == 0 && session->consumer != nullptr) { |
| 755 | lttng::urcu::read_lock_guard read_lock; |
| 756 | |
| 757 | cds_lfht_for_each_entry ( |
| 758 | session->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 759 | pthread_mutex_lock(socket->lock); |
| 760 | ret = kernel_consumer_send_session(socket, session); |
| 761 | pthread_mutex_unlock(socket->lock); |
| 762 | if (ret < 0) { |
| 763 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 764 | goto error; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | error: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Create a socket to the relayd using the URI. |
| 775 | * |
| 776 | * On success, the relayd_sock pointer is set to the created socket. |
| 777 | * Else, it remains untouched and an LTTng error code is returned. |
| 778 | */ |
| 779 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
| 780 | struct lttcomm_relayd_sock **relayd_sock, |
| 781 | struct consumer_output *consumer) |
| 782 | { |
| 783 | int ret; |
| 784 | enum lttng_error_code status = LTTNG_OK; |
| 785 | struct lttcomm_relayd_sock *rsock; |
| 786 | |
| 787 | rsock = lttcomm_alloc_relayd_sock( |
| 788 | uri, RELAYD_VERSION_COMM_MAJOR, RELAYD_VERSION_COMM_MINOR); |
| 789 | if (!rsock) { |
| 790 | status = LTTNG_ERR_FATAL; |
| 791 | goto error; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Connect to relayd so we can proceed with a session creation. This call |
| 796 | * can possibly block for an arbitrary amount of time to set the health |
| 797 | * state to be in poll execution. |
| 798 | */ |
| 799 | health_poll_entry(); |
| 800 | ret = relayd_connect(rsock); |
| 801 | health_poll_exit(); |
| 802 | if (ret < 0) { |
| 803 | ERR("Unable to reach lttng-relayd"); |
| 804 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 805 | goto free_sock; |
| 806 | } |
| 807 | |
| 808 | /* Create socket for control stream. */ |
| 809 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 810 | uint64_t result_flags; |
| 811 | |
| 812 | DBG3("Creating relayd stream socket from URI"); |
| 813 | |
| 814 | /* Check relayd version */ |
| 815 | ret = relayd_version_check(rsock); |
| 816 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
| 817 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 818 | goto close_sock; |
| 819 | } else if (ret < 0) { |
| 820 | ERR("Unable to reach lttng-relayd"); |
| 821 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 822 | goto close_sock; |
| 823 | } |
| 824 | consumer->relay_major_version = rsock->major; |
| 825 | consumer->relay_minor_version = rsock->minor; |
| 826 | ret = relayd_get_configuration(rsock, 0, &result_flags); |
| 827 | if (ret < 0) { |
| 828 | ERR("Unable to get relayd configuration"); |
| 829 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 830 | goto close_sock; |
| 831 | } |
| 832 | if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) { |
| 833 | consumer->relay_allows_clear = true; |
| 834 | } |
| 835 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
| 836 | DBG3("Creating relayd data socket from URI"); |
| 837 | } else { |
| 838 | /* Command is not valid */ |
| 839 | ERR("Relayd invalid stream type: %d", uri->stype); |
| 840 | status = LTTNG_ERR_INVALID; |
| 841 | goto close_sock; |
| 842 | } |
| 843 | |
| 844 | *relayd_sock = rsock; |
| 845 | |
| 846 | return status; |
| 847 | |
| 848 | close_sock: |
| 849 | /* The returned value is not useful since we are on an error path. */ |
| 850 | (void) relayd_close(rsock); |
| 851 | free_sock: |
| 852 | free(rsock); |
| 853 | error: |
| 854 | return status; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Connect to the relayd using URI and send the socket to the right consumer. |
| 859 | * |
| 860 | * The consumer socket lock must be held by the caller. |
| 861 | * |
| 862 | * Returns LTTNG_OK on success or an LTTng error code on failure. |
| 863 | */ |
| 864 | static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id, |
| 865 | struct lttng_uri *relayd_uri, |
| 866 | struct consumer_output *consumer, |
| 867 | struct consumer_socket *consumer_sock, |
| 868 | const char *session_name, |
| 869 | const char *hostname, |
| 870 | const char *base_path, |
| 871 | int session_live_timer, |
| 872 | const uint64_t *current_chunk_id, |
| 873 | time_t session_creation_time, |
| 874 | bool session_name_contains_creation_time) |
| 875 | { |
| 876 | int ret; |
| 877 | struct lttcomm_relayd_sock *rsock = nullptr; |
| 878 | enum lttng_error_code status; |
| 879 | |
| 880 | /* Connect to relayd and make version check if uri is the control. */ |
| 881 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
| 882 | if (status != LTTNG_OK) { |
| 883 | goto relayd_comm_error; |
| 884 | } |
| 885 | LTTNG_ASSERT(rsock); |
| 886 | |
| 887 | /* Set the network sequence index if not set. */ |
| 888 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
| 889 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 890 | /* |
| 891 | * Increment net_seq_idx because we are about to transfer the |
| 892 | * new relayd socket to the consumer. |
| 893 | * Assign unique key so the consumer can match streams. |
| 894 | */ |
| 895 | consumer->net_seq_index = ++relayd_net_seq_idx; |
| 896 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 897 | } |
| 898 | |
| 899 | /* Send relayd socket to consumer. */ |
| 900 | ret = consumer_send_relayd_socket(consumer_sock, |
| 901 | rsock, |
| 902 | consumer, |
| 903 | relayd_uri->stype, |
| 904 | session_id, |
| 905 | session_name, |
| 906 | hostname, |
| 907 | base_path, |
| 908 | session_live_timer, |
| 909 | current_chunk_id, |
| 910 | session_creation_time, |
| 911 | session_name_contains_creation_time); |
| 912 | if (ret < 0) { |
| 913 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
| 914 | goto close_sock; |
| 915 | } |
| 916 | |
| 917 | /* Flag that the corresponding socket was sent. */ |
| 918 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { |
| 919 | consumer_sock->control_sock_sent = 1; |
| 920 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
| 921 | consumer_sock->data_sock_sent = 1; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Close socket which was dup on the consumer side. The session daemon does |
| 926 | * NOT keep track of the relayd socket(s) once transfer to the consumer. |
| 927 | */ |
| 928 | |
| 929 | close_sock: |
| 930 | if (status != LTTNG_OK) { |
| 931 | /* |
| 932 | * The consumer output for this session should not be used anymore |
| 933 | * since the relayd connection failed thus making any tracing or/and |
| 934 | * streaming not usable. |
| 935 | */ |
| 936 | consumer->enabled = false; |
| 937 | } |
| 938 | (void) relayd_close(rsock); |
| 939 | free(rsock); |
| 940 | |
| 941 | relayd_comm_error: |
| 942 | return status; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Send both relayd sockets to a specific consumer and domain. This is a |
| 947 | * helper function to facilitate sending the information to the consumer for a |
| 948 | * session. |
| 949 | * |
| 950 | * The consumer socket lock must be held by the caller. |
| 951 | * |
| 952 | * Returns LTTNG_OK, or an LTTng error code on failure. |
| 953 | */ |
| 954 | static enum lttng_error_code send_consumer_relayd_sockets(unsigned int session_id, |
| 955 | struct consumer_output *consumer, |
| 956 | struct consumer_socket *sock, |
| 957 | const char *session_name, |
| 958 | const char *hostname, |
| 959 | const char *base_path, |
| 960 | int session_live_timer, |
| 961 | const uint64_t *current_chunk_id, |
| 962 | time_t session_creation_time, |
| 963 | bool session_name_contains_creation_time) |
| 964 | { |
| 965 | enum lttng_error_code status = LTTNG_OK; |
| 966 | |
| 967 | LTTNG_ASSERT(consumer); |
| 968 | LTTNG_ASSERT(sock); |
| 969 | |
| 970 | /* Sending control relayd socket. */ |
| 971 | if (!sock->control_sock_sent) { |
| 972 | status = send_consumer_relayd_socket(session_id, |
| 973 | &consumer->dst.net.control, |
| 974 | consumer, |
| 975 | sock, |
| 976 | session_name, |
| 977 | hostname, |
| 978 | base_path, |
| 979 | session_live_timer, |
| 980 | current_chunk_id, |
| 981 | session_creation_time, |
| 982 | session_name_contains_creation_time); |
| 983 | if (status != LTTNG_OK) { |
| 984 | goto error; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* Sending data relayd socket. */ |
| 989 | if (!sock->data_sock_sent) { |
| 990 | status = send_consumer_relayd_socket(session_id, |
| 991 | &consumer->dst.net.data, |
| 992 | consumer, |
| 993 | sock, |
| 994 | session_name, |
| 995 | hostname, |
| 996 | base_path, |
| 997 | session_live_timer, |
| 998 | current_chunk_id, |
| 999 | session_creation_time, |
| 1000 | session_name_contains_creation_time); |
| 1001 | if (status != LTTNG_OK) { |
| 1002 | goto error; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | error: |
| 1007 | return status; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Setup relayd connections for a tracing session. First creates the socket to |
| 1012 | * the relayd and send them to the right domain consumer. Consumer type MUST be |
| 1013 | * network. |
| 1014 | */ |
| 1015 | int cmd_setup_relayd(struct ltt_session *session) |
| 1016 | { |
| 1017 | int ret = LTTNG_OK; |
| 1018 | struct ltt_ust_session *usess; |
| 1019 | struct ltt_kernel_session *ksess; |
| 1020 | struct consumer_socket *socket; |
| 1021 | struct lttng_ht_iter iter; |
| 1022 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
| 1023 | |
| 1024 | LTTNG_ASSERT(session); |
| 1025 | |
| 1026 | usess = session->ust_session; |
| 1027 | ksess = session->kernel_session; |
| 1028 | |
| 1029 | DBG("Setting relayd for session %s", session->name); |
| 1030 | |
| 1031 | if (session->current_trace_chunk) { |
| 1032 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( |
| 1033 | session->current_trace_chunk, ¤t_chunk_id.value); |
| 1034 | |
| 1035 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1036 | current_chunk_id.is_set = true; |
| 1037 | } else { |
| 1038 | ERR("Failed to get current trace chunk id"); |
| 1039 | ret = LTTNG_ERR_UNK; |
| 1040 | goto error; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET && |
| 1045 | usess->consumer->enabled) { |
| 1046 | /* For each consumer socket, send relayd sockets */ |
| 1047 | lttng::urcu::read_lock_guard read_lock; |
| 1048 | |
| 1049 | cds_lfht_for_each_entry ( |
| 1050 | usess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1051 | pthread_mutex_lock(socket->lock); |
| 1052 | ret = send_consumer_relayd_sockets( |
| 1053 | session->id, |
| 1054 | usess->consumer, |
| 1055 | socket, |
| 1056 | session->name, |
| 1057 | session->hostname, |
| 1058 | session->base_path, |
| 1059 | session->live_timer, |
| 1060 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 1061 | session->creation_time, |
| 1062 | session->name_contains_creation_time); |
| 1063 | pthread_mutex_unlock(socket->lock); |
| 1064 | if (ret != LTTNG_OK) { |
| 1065 | goto error; |
| 1066 | } |
| 1067 | /* Session is now ready for network streaming. */ |
| 1068 | session->net_handle = 1; |
| 1069 | } |
| 1070 | |
| 1071 | session->consumer->relay_major_version = usess->consumer->relay_major_version; |
| 1072 | session->consumer->relay_minor_version = usess->consumer->relay_minor_version; |
| 1073 | session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear; |
| 1074 | } |
| 1075 | |
| 1076 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET && |
| 1077 | ksess->consumer->enabled) { |
| 1078 | lttng::urcu::read_lock_guard read_lock; |
| 1079 | |
| 1080 | cds_lfht_for_each_entry ( |
| 1081 | ksess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1082 | pthread_mutex_lock(socket->lock); |
| 1083 | ret = send_consumer_relayd_sockets( |
| 1084 | session->id, |
| 1085 | ksess->consumer, |
| 1086 | socket, |
| 1087 | session->name, |
| 1088 | session->hostname, |
| 1089 | session->base_path, |
| 1090 | session->live_timer, |
| 1091 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 1092 | session->creation_time, |
| 1093 | session->name_contains_creation_time); |
| 1094 | pthread_mutex_unlock(socket->lock); |
| 1095 | if (ret != LTTNG_OK) { |
| 1096 | goto error; |
| 1097 | } |
| 1098 | /* Session is now ready for network streaming. */ |
| 1099 | session->net_handle = 1; |
| 1100 | } |
| 1101 | |
| 1102 | session->consumer->relay_major_version = ksess->consumer->relay_major_version; |
| 1103 | session->consumer->relay_minor_version = ksess->consumer->relay_minor_version; |
| 1104 | session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear; |
| 1105 | } |
| 1106 | |
| 1107 | error: |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Start a kernel session by opening all necessary streams. |
| 1113 | */ |
| 1114 | int start_kernel_session(struct ltt_kernel_session *ksess) |
| 1115 | { |
| 1116 | int ret; |
| 1117 | struct ltt_kernel_channel *kchan; |
| 1118 | |
| 1119 | /* Open kernel metadata */ |
| 1120 | if (ksess->metadata == nullptr && ksess->output_traces) { |
| 1121 | ret = kernel_open_metadata(ksess); |
| 1122 | if (ret < 0) { |
| 1123 | ret = LTTNG_ERR_KERN_META_FAIL; |
| 1124 | goto error; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | /* Open kernel metadata stream */ |
| 1129 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
| 1130 | ret = kernel_open_metadata_stream(ksess); |
| 1131 | if (ret < 0) { |
| 1132 | ERR("Kernel create metadata stream failed"); |
| 1133 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 1134 | goto error; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /* For each channel */ |
| 1139 | cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) { |
| 1140 | if (kchan->stream_count == 0) { |
| 1141 | ret = kernel_open_channel_stream(kchan); |
| 1142 | if (ret < 0) { |
| 1143 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 1144 | goto error; |
| 1145 | } |
| 1146 | /* Update the stream global counter */ |
| 1147 | ksess->stream_count_global += ret; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | /* Setup kernel consumer socket and send fds to it */ |
| 1152 | ret = init_kernel_tracing(ksess); |
| 1153 | if (ret != 0) { |
| 1154 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 1155 | goto error; |
| 1156 | } |
| 1157 | |
| 1158 | /* This start the kernel tracing */ |
| 1159 | ret = kernel_start_session(ksess); |
| 1160 | if (ret < 0) { |
| 1161 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 1162 | goto error; |
| 1163 | } |
| 1164 | |
| 1165 | /* Quiescent wait after starting trace */ |
| 1166 | kernel_wait_quiescent(); |
| 1167 | |
| 1168 | ksess->active = true; |
| 1169 | |
| 1170 | ret = LTTNG_OK; |
| 1171 | |
| 1172 | error: |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | int stop_kernel_session(struct ltt_kernel_session *ksess) |
| 1177 | { |
| 1178 | struct ltt_kernel_channel *kchan; |
| 1179 | bool error_occurred = false; |
| 1180 | int ret; |
| 1181 | |
| 1182 | if (!ksess || !ksess->active) { |
| 1183 | return LTTNG_OK; |
| 1184 | } |
| 1185 | DBG("Stopping kernel tracing"); |
| 1186 | |
| 1187 | ret = kernel_stop_session(ksess); |
| 1188 | if (ret < 0) { |
| 1189 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
| 1190 | goto error; |
| 1191 | } |
| 1192 | |
| 1193 | kernel_wait_quiescent(); |
| 1194 | |
| 1195 | /* Flush metadata after stopping (if exists) */ |
| 1196 | if (ksess->metadata_stream_fd >= 0) { |
| 1197 | ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd); |
| 1198 | if (ret < 0) { |
| 1199 | ERR("Kernel metadata flush failed"); |
| 1200 | error_occurred = true; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* Flush all buffers after stopping */ |
| 1205 | cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) { |
| 1206 | ret = kernel_flush_buffer(kchan); |
| 1207 | if (ret < 0) { |
| 1208 | ERR("Kernel flush buffer error"); |
| 1209 | error_occurred = true; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | ksess->active = false; |
| 1214 | if (error_occurred) { |
| 1215 | ret = LTTNG_ERR_UNK; |
| 1216 | } else { |
| 1217 | ret = LTTNG_OK; |
| 1218 | } |
| 1219 | error: |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
| 1225 | */ |
| 1226 | int cmd_disable_channel(struct ltt_session *session, |
| 1227 | enum lttng_domain_type domain, |
| 1228 | char *channel_name) |
| 1229 | { |
| 1230 | int ret; |
| 1231 | struct ltt_ust_session *usess; |
| 1232 | |
| 1233 | usess = session->ust_session; |
| 1234 | |
| 1235 | lttng::urcu::read_lock_guard read_lock; |
| 1236 | |
| 1237 | switch (domain) { |
| 1238 | case LTTNG_DOMAIN_KERNEL: |
| 1239 | { |
| 1240 | ret = channel_kernel_disable(session->kernel_session, channel_name); |
| 1241 | if (ret != LTTNG_OK) { |
| 1242 | goto error; |
| 1243 | } |
| 1244 | |
| 1245 | kernel_wait_quiescent(); |
| 1246 | break; |
| 1247 | } |
| 1248 | case LTTNG_DOMAIN_UST: |
| 1249 | { |
| 1250 | struct ltt_ust_channel *uchan; |
| 1251 | struct lttng_ht *chan_ht; |
| 1252 | |
| 1253 | chan_ht = usess->domain_global.channels; |
| 1254 | |
| 1255 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 1256 | if (uchan == nullptr) { |
| 1257 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1258 | goto error; |
| 1259 | } |
| 1260 | |
| 1261 | ret = channel_ust_disable(usess, uchan); |
| 1262 | if (ret != LTTNG_OK) { |
| 1263 | goto error; |
| 1264 | } |
| 1265 | break; |
| 1266 | } |
| 1267 | default: |
| 1268 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1269 | goto error; |
| 1270 | } |
| 1271 | |
| 1272 | ret = LTTNG_OK; |
| 1273 | |
| 1274 | error: |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. |
| 1280 | * |
| 1281 | * The wpipe arguments is used as a notifier for the kernel thread. |
| 1282 | */ |
| 1283 | int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe) |
| 1284 | { |
| 1285 | int ret; |
| 1286 | size_t channel_len; |
| 1287 | ssize_t sock_recv_len; |
| 1288 | struct lttng_channel *channel = nullptr; |
| 1289 | struct lttng_buffer_view view; |
| 1290 | struct lttng_dynamic_buffer channel_buffer; |
| 1291 | const struct lttng_domain command_domain = cmd_ctx->lsm.domain; |
| 1292 | |
| 1293 | lttng_dynamic_buffer_init(&channel_buffer); |
| 1294 | channel_len = (size_t) cmd_ctx->lsm.u.channel.length; |
| 1295 | ret = lttng_dynamic_buffer_set_size(&channel_buffer, channel_len); |
| 1296 | if (ret) { |
| 1297 | ret = LTTNG_ERR_NOMEM; |
| 1298 | goto end; |
| 1299 | } |
| 1300 | |
| 1301 | sock_recv_len = lttcomm_recv_unix_sock(sock, channel_buffer.data, channel_len); |
| 1302 | if (sock_recv_len < 0 || sock_recv_len != channel_len) { |
| 1303 | ERR("Failed to receive \"enable channel\" command payload"); |
| 1304 | ret = LTTNG_ERR_INVALID; |
| 1305 | goto end; |
| 1306 | } |
| 1307 | |
| 1308 | view = lttng_buffer_view_from_dynamic_buffer(&channel_buffer, 0, channel_len); |
| 1309 | if (!lttng_buffer_view_is_valid(&view)) { |
| 1310 | ret = LTTNG_ERR_INVALID; |
| 1311 | goto end; |
| 1312 | } |
| 1313 | |
| 1314 | if (lttng_channel_create_from_buffer(&view, &channel) != channel_len) { |
| 1315 | ERR("Invalid channel payload received in \"enable channel\" command"); |
| 1316 | ret = LTTNG_ERR_INVALID; |
| 1317 | goto end; |
| 1318 | } |
| 1319 | |
| 1320 | ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe); |
| 1321 | |
| 1322 | end: |
| 1323 | lttng_dynamic_buffer_reset(&channel_buffer); |
| 1324 | lttng_channel_destroy(channel); |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
| 1328 | static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session, |
| 1329 | const struct lttng_domain *domain, |
| 1330 | const struct lttng_channel *_attr, |
| 1331 | int wpipe) |
| 1332 | { |
| 1333 | enum lttng_error_code ret_code; |
| 1334 | struct ltt_ust_session *usess = session->ust_session; |
| 1335 | struct lttng_ht *chan_ht; |
| 1336 | size_t len; |
| 1337 | struct lttng_channel *attr = nullptr; |
| 1338 | |
| 1339 | LTTNG_ASSERT(session); |
| 1340 | LTTNG_ASSERT(_attr); |
| 1341 | LTTNG_ASSERT(domain); |
| 1342 | |
| 1343 | lttng::urcu::read_lock_guard read_lock; |
| 1344 | |
| 1345 | attr = lttng_channel_copy(_attr); |
| 1346 | if (!attr) { |
| 1347 | ret_code = LTTNG_ERR_NOMEM; |
| 1348 | goto end; |
| 1349 | } |
| 1350 | |
| 1351 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
| 1352 | |
| 1353 | /* Validate channel name */ |
| 1354 | if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) { |
| 1355 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1356 | goto end; |
| 1357 | } |
| 1358 | |
| 1359 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
| 1360 | |
| 1361 | /* |
| 1362 | * If the session is a live session, remove the switch timer, the |
| 1363 | * live timer does the same thing but sends also synchronisation |
| 1364 | * beacons for inactive streams. |
| 1365 | */ |
| 1366 | if (session->live_timer > 0) { |
| 1367 | attr->attr.live_timer_interval = session->live_timer; |
| 1368 | attr->attr.switch_timer_interval = 0; |
| 1369 | } |
| 1370 | |
| 1371 | /* Check for feature support */ |
| 1372 | switch (domain->type) { |
| 1373 | case LTTNG_DOMAIN_KERNEL: |
| 1374 | { |
| 1375 | if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) { |
| 1376 | /* Sampling position of buffer is not supported */ |
| 1377 | WARN("Kernel tracer does not support buffer monitoring. " |
| 1378 | "Setting the monitor interval timer to 0 " |
| 1379 | "(disabled) for channel '%s' of session '%s'", |
| 1380 | attr->name, |
| 1381 | session->name); |
| 1382 | lttng_channel_set_monitor_timer_interval(attr, 0); |
| 1383 | } |
| 1384 | break; |
| 1385 | } |
| 1386 | case LTTNG_DOMAIN_UST: |
| 1387 | break; |
| 1388 | case LTTNG_DOMAIN_JUL: |
| 1389 | case LTTNG_DOMAIN_LOG4J: |
| 1390 | case LTTNG_DOMAIN_PYTHON: |
| 1391 | if (!agent_tracing_is_enabled()) { |
| 1392 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); |
| 1393 | ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED; |
| 1394 | goto error; |
| 1395 | } |
| 1396 | break; |
| 1397 | default: |
| 1398 | ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1399 | goto error; |
| 1400 | } |
| 1401 | |
| 1402 | switch (domain->type) { |
| 1403 | case LTTNG_DOMAIN_KERNEL: |
| 1404 | { |
| 1405 | struct ltt_kernel_channel *kchan; |
| 1406 | |
| 1407 | kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session); |
| 1408 | if (kchan == nullptr) { |
| 1409 | /* |
| 1410 | * Don't try to create a channel if the session has been started at |
| 1411 | * some point in time before. The tracer does not allow it. |
| 1412 | */ |
| 1413 | if (session->has_been_started) { |
| 1414 | ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1415 | goto error; |
| 1416 | } |
| 1417 | |
| 1418 | if (session->snapshot.nb_output > 0 || session->snapshot_mode) { |
| 1419 | /* Enforce mmap output for snapshot sessions. */ |
| 1420 | attr->attr.output = LTTNG_EVENT_MMAP; |
| 1421 | } |
| 1422 | ret_code = channel_kernel_create(session->kernel_session, attr, wpipe); |
| 1423 | if (attr->name[0] != '\0') { |
| 1424 | session->kernel_session->has_non_default_channel = 1; |
| 1425 | } |
| 1426 | } else { |
| 1427 | ret_code = channel_kernel_enable(session->kernel_session, kchan); |
| 1428 | } |
| 1429 | |
| 1430 | if (ret_code != LTTNG_OK) { |
| 1431 | goto error; |
| 1432 | } |
| 1433 | |
| 1434 | kernel_wait_quiescent(); |
| 1435 | break; |
| 1436 | } |
| 1437 | case LTTNG_DOMAIN_UST: |
| 1438 | case LTTNG_DOMAIN_JUL: |
| 1439 | case LTTNG_DOMAIN_LOG4J: |
| 1440 | case LTTNG_DOMAIN_PYTHON: |
| 1441 | { |
| 1442 | struct ltt_ust_channel *uchan; |
| 1443 | |
| 1444 | /* |
| 1445 | * FIXME |
| 1446 | * |
| 1447 | * Current agent implementation limitations force us to allow |
| 1448 | * only one channel at once in "agent" subdomains. Each |
| 1449 | * subdomain has a default channel name which must be strictly |
| 1450 | * adhered to. |
| 1451 | */ |
| 1452 | if (domain->type == LTTNG_DOMAIN_JUL) { |
| 1453 | if (strncmp(attr->name, |
| 1454 | DEFAULT_JUL_CHANNEL_NAME, |
| 1455 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1456 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1457 | goto error; |
| 1458 | } |
| 1459 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { |
| 1460 | if (strncmp(attr->name, |
| 1461 | DEFAULT_LOG4J_CHANNEL_NAME, |
| 1462 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1463 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1464 | goto error; |
| 1465 | } |
| 1466 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { |
| 1467 | if (strncmp(attr->name, |
| 1468 | DEFAULT_PYTHON_CHANNEL_NAME, |
| 1469 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1470 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1471 | goto error; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | chan_ht = usess->domain_global.channels; |
| 1476 | |
| 1477 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
| 1478 | if (uchan == nullptr) { |
| 1479 | /* |
| 1480 | * Don't try to create a channel if the session has been started at |
| 1481 | * some point in time before. The tracer does not allow it. |
| 1482 | */ |
| 1483 | if (session->has_been_started) { |
| 1484 | ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1485 | goto error; |
| 1486 | } |
| 1487 | |
| 1488 | ret_code = channel_ust_create(usess, attr, domain->buf_type); |
| 1489 | if (attr->name[0] != '\0') { |
| 1490 | usess->has_non_default_channel = 1; |
| 1491 | } |
| 1492 | } else { |
| 1493 | ret_code = channel_ust_enable(usess, uchan); |
| 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | default: |
| 1498 | ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1499 | goto error; |
| 1500 | } |
| 1501 | |
| 1502 | if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { |
| 1503 | session->has_non_mmap_channel = true; |
| 1504 | } |
| 1505 | error: |
| 1506 | end: |
| 1507 | lttng_channel_destroy(attr); |
| 1508 | return ret_code; |
| 1509 | } |
| 1510 | |
| 1511 | enum lttng_error_code |
| 1512 | cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session, |
| 1513 | enum lttng_domain_type domain, |
| 1514 | enum lttng_process_attr process_attr, |
| 1515 | enum lttng_tracking_policy *policy) |
| 1516 | { |
| 1517 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1518 | const struct process_attr_tracker *tracker; |
| 1519 | |
| 1520 | switch (domain) { |
| 1521 | case LTTNG_DOMAIN_KERNEL: |
| 1522 | if (!session->kernel_session) { |
| 1523 | ret_code = LTTNG_ERR_INVALID; |
| 1524 | goto end; |
| 1525 | } |
| 1526 | tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr); |
| 1527 | break; |
| 1528 | case LTTNG_DOMAIN_UST: |
| 1529 | if (!session->ust_session) { |
| 1530 | ret_code = LTTNG_ERR_INVALID; |
| 1531 | goto end; |
| 1532 | } |
| 1533 | tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr); |
| 1534 | break; |
| 1535 | default: |
| 1536 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1537 | goto end; |
| 1538 | } |
| 1539 | if (tracker) { |
| 1540 | *policy = process_attr_tracker_get_tracking_policy(tracker); |
| 1541 | } else { |
| 1542 | ret_code = LTTNG_ERR_INVALID; |
| 1543 | } |
| 1544 | end: |
| 1545 | return ret_code; |
| 1546 | } |
| 1547 | |
| 1548 | enum lttng_error_code |
| 1549 | cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session, |
| 1550 | enum lttng_domain_type domain, |
| 1551 | enum lttng_process_attr process_attr, |
| 1552 | enum lttng_tracking_policy policy) |
| 1553 | { |
| 1554 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1555 | |
| 1556 | switch (policy) { |
| 1557 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: |
| 1558 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: |
| 1559 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: |
| 1560 | break; |
| 1561 | default: |
| 1562 | ret_code = LTTNG_ERR_INVALID; |
| 1563 | goto end; |
| 1564 | } |
| 1565 | |
| 1566 | switch (domain) { |
| 1567 | case LTTNG_DOMAIN_KERNEL: |
| 1568 | if (!session->kernel_session) { |
| 1569 | ret_code = LTTNG_ERR_INVALID; |
| 1570 | goto end; |
| 1571 | } |
| 1572 | ret_code = kernel_process_attr_tracker_set_tracking_policy( |
| 1573 | session->kernel_session, process_attr, policy); |
| 1574 | break; |
| 1575 | case LTTNG_DOMAIN_UST: |
| 1576 | if (!session->ust_session) { |
| 1577 | ret_code = LTTNG_ERR_INVALID; |
| 1578 | goto end; |
| 1579 | } |
| 1580 | ret_code = trace_ust_process_attr_tracker_set_tracking_policy( |
| 1581 | session->ust_session, process_attr, policy); |
| 1582 | break; |
| 1583 | default: |
| 1584 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1585 | break; |
| 1586 | } |
| 1587 | end: |
| 1588 | return ret_code; |
| 1589 | } |
| 1590 | |
| 1591 | enum lttng_error_code |
| 1592 | cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session, |
| 1593 | enum lttng_domain_type domain, |
| 1594 | enum lttng_process_attr process_attr, |
| 1595 | const struct process_attr_value *value) |
| 1596 | { |
| 1597 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1598 | |
| 1599 | switch (domain) { |
| 1600 | case LTTNG_DOMAIN_KERNEL: |
| 1601 | if (!session->kernel_session) { |
| 1602 | ret_code = LTTNG_ERR_INVALID; |
| 1603 | goto end; |
| 1604 | } |
| 1605 | ret_code = kernel_process_attr_tracker_inclusion_set_add_value( |
| 1606 | session->kernel_session, process_attr, value); |
| 1607 | break; |
| 1608 | case LTTNG_DOMAIN_UST: |
| 1609 | if (!session->ust_session) { |
| 1610 | ret_code = LTTNG_ERR_INVALID; |
| 1611 | goto end; |
| 1612 | } |
| 1613 | ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value( |
| 1614 | session->ust_session, process_attr, value); |
| 1615 | break; |
| 1616 | default: |
| 1617 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1618 | break; |
| 1619 | } |
| 1620 | end: |
| 1621 | return ret_code; |
| 1622 | } |
| 1623 | |
| 1624 | enum lttng_error_code |
| 1625 | cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session, |
| 1626 | enum lttng_domain_type domain, |
| 1627 | enum lttng_process_attr process_attr, |
| 1628 | const struct process_attr_value *value) |
| 1629 | { |
| 1630 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1631 | |
| 1632 | switch (domain) { |
| 1633 | case LTTNG_DOMAIN_KERNEL: |
| 1634 | if (!session->kernel_session) { |
| 1635 | ret_code = LTTNG_ERR_INVALID; |
| 1636 | goto end; |
| 1637 | } |
| 1638 | ret_code = kernel_process_attr_tracker_inclusion_set_remove_value( |
| 1639 | session->kernel_session, process_attr, value); |
| 1640 | break; |
| 1641 | case LTTNG_DOMAIN_UST: |
| 1642 | if (!session->ust_session) { |
| 1643 | ret_code = LTTNG_ERR_INVALID; |
| 1644 | goto end; |
| 1645 | } |
| 1646 | ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value( |
| 1647 | session->ust_session, process_attr, value); |
| 1648 | break; |
| 1649 | default: |
| 1650 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1651 | break; |
| 1652 | } |
| 1653 | end: |
| 1654 | return ret_code; |
| 1655 | } |
| 1656 | |
| 1657 | enum lttng_error_code |
| 1658 | cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session, |
| 1659 | enum lttng_domain_type domain, |
| 1660 | enum lttng_process_attr process_attr, |
| 1661 | struct lttng_process_attr_values **values) |
| 1662 | { |
| 1663 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1664 | const struct process_attr_tracker *tracker; |
| 1665 | enum process_attr_tracker_status status; |
| 1666 | |
| 1667 | switch (domain) { |
| 1668 | case LTTNG_DOMAIN_KERNEL: |
| 1669 | if (!session->kernel_session) { |
| 1670 | ret_code = LTTNG_ERR_INVALID; |
| 1671 | goto end; |
| 1672 | } |
| 1673 | tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr); |
| 1674 | break; |
| 1675 | case LTTNG_DOMAIN_UST: |
| 1676 | if (!session->ust_session) { |
| 1677 | ret_code = LTTNG_ERR_INVALID; |
| 1678 | goto end; |
| 1679 | } |
| 1680 | tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr); |
| 1681 | break; |
| 1682 | default: |
| 1683 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1684 | goto end; |
| 1685 | } |
| 1686 | |
| 1687 | if (!tracker) { |
| 1688 | ret_code = LTTNG_ERR_INVALID; |
| 1689 | goto end; |
| 1690 | } |
| 1691 | |
| 1692 | status = process_attr_tracker_get_inclusion_set(tracker, values); |
| 1693 | switch (status) { |
| 1694 | case PROCESS_ATTR_TRACKER_STATUS_OK: |
| 1695 | ret_code = LTTNG_OK; |
| 1696 | break; |
| 1697 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: |
| 1698 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; |
| 1699 | break; |
| 1700 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: |
| 1701 | ret_code = LTTNG_ERR_NOMEM; |
| 1702 | break; |
| 1703 | default: |
| 1704 | ret_code = LTTNG_ERR_UNK; |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | end: |
| 1709 | return ret_code; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Command LTTNG_DISABLE_EVENT processed by the client thread. |
| 1714 | */ |
| 1715 | int cmd_disable_event(struct command_ctx *cmd_ctx, |
| 1716 | struct lttng_event *event, |
| 1717 | char *filter_expression, |
| 1718 | struct lttng_bytecode *bytecode, |
| 1719 | struct lttng_event_exclusion *exclusion) |
| 1720 | { |
| 1721 | int ret; |
| 1722 | const char *event_name; |
| 1723 | const struct ltt_session *session = cmd_ctx->session; |
| 1724 | const char *channel_name = cmd_ctx->lsm.u.disable.channel_name; |
| 1725 | const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type; |
| 1726 | |
| 1727 | DBG("Disable event command for event \'%s\'", event->name); |
| 1728 | |
| 1729 | /* |
| 1730 | * Filter and exclusions are simply not handled by the |
| 1731 | * disable event command at this time. |
| 1732 | * |
| 1733 | * FIXME |
| 1734 | */ |
| 1735 | (void) filter_expression; |
| 1736 | (void) exclusion; |
| 1737 | |
| 1738 | /* Ignore the presence of filter or exclusion for the event */ |
| 1739 | event->filter = 0; |
| 1740 | event->exclusion = 0; |
| 1741 | |
| 1742 | event_name = event->name; |
| 1743 | |
| 1744 | lttng::urcu::read_lock_guard read_lock; |
| 1745 | |
| 1746 | /* Error out on unhandled search criteria */ |
| 1747 | if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid || |
| 1748 | event->filter || event->exclusion) { |
| 1749 | ret = LTTNG_ERR_UNK; |
| 1750 | goto error; |
| 1751 | } |
| 1752 | |
| 1753 | switch (domain) { |
| 1754 | case LTTNG_DOMAIN_KERNEL: |
| 1755 | { |
| 1756 | struct ltt_kernel_channel *kchan; |
| 1757 | struct ltt_kernel_session *ksess; |
| 1758 | |
| 1759 | ksess = session->kernel_session; |
| 1760 | |
| 1761 | /* |
| 1762 | * If a non-default channel has been created in the |
| 1763 | * session, explicitely require that -c chan_name needs |
| 1764 | * to be provided. |
| 1765 | */ |
| 1766 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { |
| 1767 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1768 | goto error_unlock; |
| 1769 | } |
| 1770 | |
| 1771 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 1772 | if (kchan == nullptr) { |
| 1773 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 1774 | goto error_unlock; |
| 1775 | } |
| 1776 | |
| 1777 | switch (event->type) { |
| 1778 | case LTTNG_EVENT_ALL: |
| 1779 | case LTTNG_EVENT_TRACEPOINT: |
| 1780 | case LTTNG_EVENT_SYSCALL: |
| 1781 | case LTTNG_EVENT_PROBE: |
| 1782 | case LTTNG_EVENT_FUNCTION: |
| 1783 | case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */ |
| 1784 | if (event_name[0] == '\0') { |
| 1785 | ret = event_kernel_disable_event(kchan, nullptr, event->type); |
| 1786 | } else { |
| 1787 | ret = event_kernel_disable_event(kchan, event_name, event->type); |
| 1788 | } |
| 1789 | if (ret != LTTNG_OK) { |
| 1790 | goto error_unlock; |
| 1791 | } |
| 1792 | break; |
| 1793 | default: |
| 1794 | ret = LTTNG_ERR_UNK; |
| 1795 | goto error_unlock; |
| 1796 | } |
| 1797 | |
| 1798 | kernel_wait_quiescent(); |
| 1799 | break; |
| 1800 | } |
| 1801 | case LTTNG_DOMAIN_UST: |
| 1802 | { |
| 1803 | struct ltt_ust_channel *uchan; |
| 1804 | struct ltt_ust_session *usess; |
| 1805 | |
| 1806 | usess = session->ust_session; |
| 1807 | |
| 1808 | if (validate_ust_event_name(event_name)) { |
| 1809 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
| 1810 | goto error_unlock; |
| 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * If a non-default channel has been created in the |
| 1815 | * session, explicitly require that -c chan_name needs |
| 1816 | * to be provided. |
| 1817 | */ |
| 1818 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1819 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1820 | goto error_unlock; |
| 1821 | } |
| 1822 | |
| 1823 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name); |
| 1824 | if (uchan == nullptr) { |
| 1825 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1826 | goto error_unlock; |
| 1827 | } |
| 1828 | |
| 1829 | switch (event->type) { |
| 1830 | case LTTNG_EVENT_ALL: |
| 1831 | /* |
| 1832 | * An empty event name means that everything |
| 1833 | * should be disabled. |
| 1834 | */ |
| 1835 | if (event->name[0] == '\0') { |
| 1836 | ret = event_ust_disable_all_tracepoints(usess, uchan); |
| 1837 | } else { |
| 1838 | ret = event_ust_disable_tracepoint(usess, uchan, event_name); |
| 1839 | } |
| 1840 | if (ret != LTTNG_OK) { |
| 1841 | goto error_unlock; |
| 1842 | } |
| 1843 | break; |
| 1844 | default: |
| 1845 | ret = LTTNG_ERR_UNK; |
| 1846 | goto error_unlock; |
| 1847 | } |
| 1848 | |
| 1849 | DBG3("Disable UST event %s in channel %s completed", event_name, channel_name); |
| 1850 | break; |
| 1851 | } |
| 1852 | case LTTNG_DOMAIN_LOG4J: |
| 1853 | case LTTNG_DOMAIN_JUL: |
| 1854 | case LTTNG_DOMAIN_PYTHON: |
| 1855 | { |
| 1856 | struct agent *agt; |
| 1857 | struct ltt_ust_session *usess = session->ust_session; |
| 1858 | |
| 1859 | LTTNG_ASSERT(usess); |
| 1860 | |
| 1861 | switch (event->type) { |
| 1862 | case LTTNG_EVENT_ALL: |
| 1863 | break; |
| 1864 | default: |
| 1865 | ret = LTTNG_ERR_UNK; |
| 1866 | goto error_unlock; |
| 1867 | } |
| 1868 | |
| 1869 | agt = trace_ust_find_agent(usess, domain); |
| 1870 | if (!agt) { |
| 1871 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; |
| 1872 | goto error_unlock; |
| 1873 | } |
| 1874 | /* |
| 1875 | * An empty event name means that everything |
| 1876 | * should be disabled. |
| 1877 | */ |
| 1878 | if (event->name[0] == '\0') { |
| 1879 | ret = event_agent_disable_all(usess, agt); |
| 1880 | } else { |
| 1881 | ret = event_agent_disable(usess, agt, event_name); |
| 1882 | } |
| 1883 | if (ret != LTTNG_OK) { |
| 1884 | goto error_unlock; |
| 1885 | } |
| 1886 | |
| 1887 | break; |
| 1888 | } |
| 1889 | default: |
| 1890 | ret = LTTNG_ERR_UND; |
| 1891 | goto error_unlock; |
| 1892 | } |
| 1893 | |
| 1894 | ret = LTTNG_OK; |
| 1895 | |
| 1896 | error_unlock: |
| 1897 | error: |
| 1898 | free(exclusion); |
| 1899 | free(bytecode); |
| 1900 | free(filter_expression); |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | /* |
| 1905 | * Command LTTNG_ADD_CONTEXT processed by the client thread. |
| 1906 | */ |
| 1907 | int cmd_add_context(struct command_ctx *cmd_ctx, |
| 1908 | const struct lttng_event_context *event_context, |
| 1909 | int kwpipe) |
| 1910 | { |
| 1911 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
| 1912 | const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type; |
| 1913 | const struct ltt_session *session = cmd_ctx->session; |
| 1914 | const char *channel_name = cmd_ctx->lsm.u.context.channel_name; |
| 1915 | |
| 1916 | /* |
| 1917 | * Don't try to add a context if the session has been started at |
| 1918 | * some point in time before. The tracer does not allow it and would |
| 1919 | * result in a corrupted trace. |
| 1920 | */ |
| 1921 | if (cmd_ctx->session->has_been_started) { |
| 1922 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1923 | goto end; |
| 1924 | } |
| 1925 | |
| 1926 | switch (domain) { |
| 1927 | case LTTNG_DOMAIN_KERNEL: |
| 1928 | LTTNG_ASSERT(session->kernel_session); |
| 1929 | |
| 1930 | if (session->kernel_session->channel_count == 0) { |
| 1931 | /* Create default channel */ |
| 1932 | ret = channel_kernel_create(session->kernel_session, nullptr, kwpipe); |
| 1933 | if (ret != LTTNG_OK) { |
| 1934 | goto error; |
| 1935 | } |
| 1936 | chan_kern_created = 1; |
| 1937 | } |
| 1938 | /* Add kernel context to kernel tracer */ |
| 1939 | ret = context_kernel_add(session->kernel_session, event_context, channel_name); |
| 1940 | if (ret != LTTNG_OK) { |
| 1941 | goto error; |
| 1942 | } |
| 1943 | break; |
| 1944 | case LTTNG_DOMAIN_JUL: |
| 1945 | case LTTNG_DOMAIN_LOG4J: |
| 1946 | { |
| 1947 | /* |
| 1948 | * Validate channel name. |
| 1949 | * If no channel name is given and the domain is JUL or LOG4J, |
| 1950 | * set it to the appropriate domain-specific channel name. If |
| 1951 | * a name is provided but does not match the expexted channel |
| 1952 | * name, return an error. |
| 1953 | */ |
| 1954 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && |
| 1955 | strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME) != 0) { |
| 1956 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1957 | goto error; |
| 1958 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && |
| 1959 | strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME) != 0) { |
| 1960 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1961 | goto error; |
| 1962 | } |
| 1963 | } |
| 1964 | /* fall through */ |
| 1965 | case LTTNG_DOMAIN_UST: |
| 1966 | { |
| 1967 | struct ltt_ust_session *usess = session->ust_session; |
| 1968 | unsigned int chan_count; |
| 1969 | |
| 1970 | LTTNG_ASSERT(usess); |
| 1971 | |
| 1972 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
| 1973 | if (chan_count == 0) { |
| 1974 | struct lttng_channel *attr; |
| 1975 | /* Create default channel */ |
| 1976 | attr = channel_new_default_attr(domain, usess->buffer_type); |
| 1977 | if (attr == nullptr) { |
| 1978 | ret = LTTNG_ERR_FATAL; |
| 1979 | goto error; |
| 1980 | } |
| 1981 | |
| 1982 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
| 1983 | if (ret != LTTNG_OK) { |
| 1984 | free(attr); |
| 1985 | goto error; |
| 1986 | } |
| 1987 | channel_attr_destroy(attr); |
| 1988 | chan_ust_created = 1; |
| 1989 | } |
| 1990 | |
| 1991 | ret = context_ust_add(usess, domain, event_context, channel_name); |
| 1992 | if (ret != LTTNG_OK) { |
| 1993 | goto error; |
| 1994 | } |
| 1995 | break; |
| 1996 | } |
| 1997 | default: |
| 1998 | ret = LTTNG_ERR_UND; |
| 1999 | goto error; |
| 2000 | } |
| 2001 | |
| 2002 | ret = LTTNG_OK; |
| 2003 | goto end; |
| 2004 | |
| 2005 | error: |
| 2006 | if (chan_kern_created) { |
| 2007 | struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name( |
| 2008 | DEFAULT_CHANNEL_NAME, session->kernel_session); |
| 2009 | /* Created previously, this should NOT fail. */ |
| 2010 | LTTNG_ASSERT(kchan); |
| 2011 | kernel_destroy_channel(kchan); |
| 2012 | } |
| 2013 | |
| 2014 | if (chan_ust_created) { |
| 2015 | struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name( |
| 2016 | session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME); |
| 2017 | /* Created previously, this should NOT fail. */ |
| 2018 | LTTNG_ASSERT(uchan); |
| 2019 | /* Remove from the channel list of the session. */ |
| 2020 | trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan); |
| 2021 | trace_ust_destroy_channel(uchan); |
| 2022 | } |
| 2023 | end: |
| 2024 | return ret; |
| 2025 | } |
| 2026 | |
| 2027 | static inline bool name_starts_with(const char *name, const char *prefix) |
| 2028 | { |
| 2029 | const size_t max_cmp_len = std::min(strlen(prefix), (size_t) LTTNG_SYMBOL_NAME_LEN); |
| 2030 | |
| 2031 | return !strncmp(name, prefix, max_cmp_len); |
| 2032 | } |
| 2033 | |
| 2034 | /* Perform userspace-specific event name validation */ |
| 2035 | static int validate_ust_event_name(const char *name) |
| 2036 | { |
| 2037 | int ret = 0; |
| 2038 | |
| 2039 | if (!name) { |
| 2040 | ret = -1; |
| 2041 | goto end; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * Check name against all internal UST event component namespaces used |
| 2046 | * by the agents. |
| 2047 | */ |
| 2048 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || |
| 2049 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || |
| 2050 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { |
| 2051 | ret = -1; |
| 2052 | } |
| 2053 | |
| 2054 | end: |
| 2055 | return ret; |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Internal version of cmd_enable_event() with a supplemental |
| 2060 | * "internal_event" flag which is used to enable internal events which should |
| 2061 | * be hidden from clients. Such events are used in the agent implementation to |
| 2062 | * enable the events through which all "agent" events are funeled. |
| 2063 | */ |
| 2064 | static int _cmd_enable_event(struct ltt_session *session, |
| 2065 | const struct lttng_domain *domain, |
| 2066 | char *channel_name, |
| 2067 | struct lttng_event *event, |
| 2068 | char *filter_expression, |
| 2069 | struct lttng_bytecode *filter, |
| 2070 | struct lttng_event_exclusion *exclusion, |
| 2071 | int wpipe, |
| 2072 | bool internal_event) |
| 2073 | { |
| 2074 | int ret = 0, channel_created = 0; |
| 2075 | struct lttng_channel *attr = nullptr; |
| 2076 | |
| 2077 | LTTNG_ASSERT(session); |
| 2078 | LTTNG_ASSERT(event); |
| 2079 | LTTNG_ASSERT(channel_name); |
| 2080 | |
| 2081 | /* If we have a filter, we must have its filter expression */ |
| 2082 | LTTNG_ASSERT(!(!!filter_expression ^ !!filter)); |
| 2083 | |
| 2084 | /* Normalize event name as a globbing pattern */ |
| 2085 | strutils_normalize_star_glob_pattern(event->name); |
| 2086 | |
| 2087 | /* Normalize exclusion names as globbing patterns */ |
| 2088 | if (exclusion) { |
| 2089 | size_t i; |
| 2090 | |
| 2091 | for (i = 0; i < exclusion->count; i++) { |
| 2092 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); |
| 2093 | |
| 2094 | strutils_normalize_star_glob_pattern(name); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | lttng::urcu::read_lock_guard read_lock; |
| 2099 | |
| 2100 | switch (domain->type) { |
| 2101 | case LTTNG_DOMAIN_KERNEL: |
| 2102 | { |
| 2103 | struct ltt_kernel_channel *kchan; |
| 2104 | |
| 2105 | /* |
| 2106 | * If a non-default channel has been created in the |
| 2107 | * session, explicitely require that -c chan_name needs |
| 2108 | * to be provided. |
| 2109 | */ |
| 2110 | if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') { |
| 2111 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 2112 | goto error; |
| 2113 | } |
| 2114 | |
| 2115 | kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session); |
| 2116 | if (kchan == nullptr) { |
| 2117 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL); |
| 2118 | if (attr == nullptr) { |
| 2119 | ret = LTTNG_ERR_FATAL; |
| 2120 | goto error; |
| 2121 | } |
| 2122 | if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) { |
| 2123 | ret = LTTNG_ERR_INVALID; |
| 2124 | goto error; |
| 2125 | } |
| 2126 | |
| 2127 | ret = cmd_enable_channel_internal(session, domain, attr, wpipe); |
| 2128 | if (ret != LTTNG_OK) { |
| 2129 | goto error; |
| 2130 | } |
| 2131 | channel_created = 1; |
| 2132 | } |
| 2133 | |
| 2134 | /* Get the newly created kernel channel pointer */ |
| 2135 | kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session); |
| 2136 | if (kchan == nullptr) { |
| 2137 | /* This sould not happen... */ |
| 2138 | ret = LTTNG_ERR_FATAL; |
| 2139 | goto error; |
| 2140 | } |
| 2141 | |
| 2142 | switch (event->type) { |
| 2143 | case LTTNG_EVENT_ALL: |
| 2144 | { |
| 2145 | char *filter_expression_a = nullptr; |
| 2146 | struct lttng_bytecode *filter_a = nullptr; |
| 2147 | |
| 2148 | /* |
| 2149 | * We need to duplicate filter_expression and filter, |
| 2150 | * because ownership is passed to first enable |
| 2151 | * event. |
| 2152 | */ |
| 2153 | if (filter_expression) { |
| 2154 | filter_expression_a = strdup(filter_expression); |
| 2155 | if (!filter_expression_a) { |
| 2156 | ret = LTTNG_ERR_FATAL; |
| 2157 | goto error; |
| 2158 | } |
| 2159 | } |
| 2160 | if (filter) { |
| 2161 | filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len); |
| 2162 | if (!filter_a) { |
| 2163 | free(filter_expression_a); |
| 2164 | ret = LTTNG_ERR_FATAL; |
| 2165 | goto error; |
| 2166 | } |
| 2167 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); |
| 2168 | } |
| 2169 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
| 2170 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2171 | /* We have passed ownership */ |
| 2172 | filter_expression = nullptr; |
| 2173 | filter = nullptr; |
| 2174 | if (ret != LTTNG_OK) { |
| 2175 | if (channel_created) { |
| 2176 | /* Let's not leak a useless channel. */ |
| 2177 | kernel_destroy_channel(kchan); |
| 2178 | } |
| 2179 | free(filter_expression_a); |
| 2180 | free(filter_a); |
| 2181 | goto error; |
| 2182 | } |
| 2183 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ |
| 2184 | ret = event_kernel_enable_event( |
| 2185 | kchan, event, filter_expression_a, filter_a); |
| 2186 | /* We have passed ownership */ |
| 2187 | filter_expression_a = nullptr; |
| 2188 | filter_a = nullptr; |
| 2189 | if (ret != LTTNG_OK) { |
| 2190 | goto error; |
| 2191 | } |
| 2192 | break; |
| 2193 | } |
| 2194 | case LTTNG_EVENT_PROBE: |
| 2195 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 2196 | case LTTNG_EVENT_FUNCTION: |
| 2197 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 2198 | case LTTNG_EVENT_TRACEPOINT: |
| 2199 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2200 | /* We have passed ownership */ |
| 2201 | filter_expression = nullptr; |
| 2202 | filter = nullptr; |
| 2203 | if (ret != LTTNG_OK) { |
| 2204 | if (channel_created) { |
| 2205 | /* Let's not leak a useless channel. */ |
| 2206 | kernel_destroy_channel(kchan); |
| 2207 | } |
| 2208 | goto error; |
| 2209 | } |
| 2210 | break; |
| 2211 | case LTTNG_EVENT_SYSCALL: |
| 2212 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2213 | /* We have passed ownership */ |
| 2214 | filter_expression = nullptr; |
| 2215 | filter = nullptr; |
| 2216 | if (ret != LTTNG_OK) { |
| 2217 | goto error; |
| 2218 | } |
| 2219 | break; |
| 2220 | default: |
| 2221 | ret = LTTNG_ERR_UNK; |
| 2222 | goto error; |
| 2223 | } |
| 2224 | |
| 2225 | kernel_wait_quiescent(); |
| 2226 | break; |
| 2227 | } |
| 2228 | case LTTNG_DOMAIN_UST: |
| 2229 | { |
| 2230 | struct ltt_ust_channel *uchan; |
| 2231 | struct ltt_ust_session *usess = session->ust_session; |
| 2232 | |
| 2233 | LTTNG_ASSERT(usess); |
| 2234 | |
| 2235 | /* |
| 2236 | * If a non-default channel has been created in the |
| 2237 | * session, explicitely require that -c chan_name needs |
| 2238 | * to be provided. |
| 2239 | */ |
| 2240 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 2241 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 2242 | goto error; |
| 2243 | } |
| 2244 | |
| 2245 | /* Get channel from global UST domain */ |
| 2246 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name); |
| 2247 | if (uchan == nullptr) { |
| 2248 | /* Create default channel */ |
| 2249 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type); |
| 2250 | if (attr == nullptr) { |
| 2251 | ret = LTTNG_ERR_FATAL; |
| 2252 | goto error; |
| 2253 | } |
| 2254 | if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) { |
| 2255 | ret = LTTNG_ERR_INVALID; |
| 2256 | goto error; |
| 2257 | } |
| 2258 | |
| 2259 | ret = cmd_enable_channel_internal(session, domain, attr, wpipe); |
| 2260 | if (ret != LTTNG_OK) { |
| 2261 | goto error; |
| 2262 | } |
| 2263 | |
| 2264 | /* Get the newly created channel reference back */ |
| 2265 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 2266 | channel_name); |
| 2267 | LTTNG_ASSERT(uchan); |
| 2268 | } |
| 2269 | |
| 2270 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
| 2271 | /* |
| 2272 | * Don't allow users to add UST events to channels which |
| 2273 | * are assigned to a userspace subdomain (JUL, Log4J, |
| 2274 | * Python, etc.). |
| 2275 | */ |
| 2276 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; |
| 2277 | goto error; |
| 2278 | } |
| 2279 | |
| 2280 | if (!internal_event) { |
| 2281 | /* |
| 2282 | * Ensure the event name is not reserved for internal |
| 2283 | * use. |
| 2284 | */ |
| 2285 | ret = validate_ust_event_name(event->name); |
| 2286 | if (ret) { |
| 2287 | WARN("Userspace event name %s failed validation.", event->name); |
| 2288 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
| 2289 | goto error; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | /* At this point, the session and channel exist on the tracer */ |
| 2294 | ret = event_ust_enable_tracepoint( |
| 2295 | usess, uchan, event, filter_expression, filter, exclusion, internal_event); |
| 2296 | /* We have passed ownership */ |
| 2297 | filter_expression = nullptr; |
| 2298 | filter = nullptr; |
| 2299 | exclusion = nullptr; |
| 2300 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
| 2301 | goto already_enabled; |
| 2302 | } else if (ret != LTTNG_OK) { |
| 2303 | goto error; |
| 2304 | } |
| 2305 | break; |
| 2306 | } |
| 2307 | case LTTNG_DOMAIN_LOG4J: |
| 2308 | case LTTNG_DOMAIN_JUL: |
| 2309 | case LTTNG_DOMAIN_PYTHON: |
| 2310 | { |
| 2311 | const char *default_event_name, *default_chan_name; |
| 2312 | struct agent *agt; |
| 2313 | struct lttng_event uevent; |
| 2314 | struct lttng_domain tmp_dom; |
| 2315 | struct ltt_ust_session *usess = session->ust_session; |
| 2316 | |
| 2317 | LTTNG_ASSERT(usess); |
| 2318 | |
| 2319 | if (!agent_tracing_is_enabled()) { |
| 2320 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); |
| 2321 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; |
| 2322 | goto error; |
| 2323 | } |
| 2324 | |
| 2325 | agt = trace_ust_find_agent(usess, domain->type); |
| 2326 | if (!agt) { |
| 2327 | agt = agent_create(domain->type); |
| 2328 | if (!agt) { |
| 2329 | ret = LTTNG_ERR_NOMEM; |
| 2330 | goto error; |
| 2331 | } |
| 2332 | agent_add(agt, usess->agents); |
| 2333 | } |
| 2334 | |
| 2335 | /* Create the default tracepoint. */ |
| 2336 | memset(&uevent, 0, sizeof(uevent)); |
| 2337 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
| 2338 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 2339 | uevent.loglevel = -1; |
| 2340 | default_event_name = event_get_default_agent_ust_name(domain->type); |
| 2341 | if (!default_event_name) { |
| 2342 | ret = LTTNG_ERR_FATAL; |
| 2343 | goto error; |
| 2344 | } |
| 2345 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
| 2346 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
| 2347 | |
| 2348 | /* |
| 2349 | * The domain type is changed because we are about to enable the |
| 2350 | * default channel and event for the JUL domain that are hardcoded. |
| 2351 | * This happens in the UST domain. |
| 2352 | */ |
| 2353 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); |
| 2354 | tmp_dom.type = LTTNG_DOMAIN_UST; |
| 2355 | |
| 2356 | switch (domain->type) { |
| 2357 | case LTTNG_DOMAIN_LOG4J: |
| 2358 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
| 2359 | break; |
| 2360 | case LTTNG_DOMAIN_JUL: |
| 2361 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
| 2362 | break; |
| 2363 | case LTTNG_DOMAIN_PYTHON: |
| 2364 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; |
| 2365 | break; |
| 2366 | default: |
| 2367 | /* The switch/case we are in makes this impossible */ |
| 2368 | abort(); |
| 2369 | } |
| 2370 | |
| 2371 | { |
| 2372 | char *filter_expression_copy = nullptr; |
| 2373 | struct lttng_bytecode *filter_copy = nullptr; |
| 2374 | |
| 2375 | if (filter) { |
| 2376 | const size_t filter_size = |
| 2377 | sizeof(struct lttng_bytecode) + filter->len; |
| 2378 | |
| 2379 | filter_copy = zmalloc<lttng_bytecode>(filter_size); |
| 2380 | if (!filter_copy) { |
| 2381 | ret = LTTNG_ERR_NOMEM; |
| 2382 | goto error; |
| 2383 | } |
| 2384 | memcpy(filter_copy, filter, filter_size); |
| 2385 | |
| 2386 | filter_expression_copy = strdup(filter_expression); |
| 2387 | if (!filter_expression) { |
| 2388 | ret = LTTNG_ERR_NOMEM; |
| 2389 | } |
| 2390 | |
| 2391 | if (!filter_expression_copy || !filter_copy) { |
| 2392 | free(filter_expression_copy); |
| 2393 | free(filter_copy); |
| 2394 | goto error; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | ret = cmd_enable_event_internal(session, |
| 2399 | &tmp_dom, |
| 2400 | (char *) default_chan_name, |
| 2401 | &uevent, |
| 2402 | filter_expression_copy, |
| 2403 | filter_copy, |
| 2404 | nullptr, |
| 2405 | wpipe); |
| 2406 | } |
| 2407 | |
| 2408 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
| 2409 | goto already_enabled; |
| 2410 | } else if (ret != LTTNG_OK) { |
| 2411 | goto error; |
| 2412 | } |
| 2413 | |
| 2414 | /* The wild card * means that everything should be enabled. */ |
| 2415 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { |
| 2416 | ret = event_agent_enable_all(usess, agt, event, filter, filter_expression); |
| 2417 | } else { |
| 2418 | ret = event_agent_enable(usess, agt, event, filter, filter_expression); |
| 2419 | } |
| 2420 | filter = nullptr; |
| 2421 | filter_expression = nullptr; |
| 2422 | if (ret != LTTNG_OK) { |
| 2423 | goto error; |
| 2424 | } |
| 2425 | |
| 2426 | break; |
| 2427 | } |
| 2428 | default: |
| 2429 | ret = LTTNG_ERR_UND; |
| 2430 | goto error; |
| 2431 | } |
| 2432 | |
| 2433 | ret = LTTNG_OK; |
| 2434 | |
| 2435 | already_enabled: |
| 2436 | error: |
| 2437 | free(filter_expression); |
| 2438 | free(filter); |
| 2439 | free(exclusion); |
| 2440 | channel_attr_destroy(attr); |
| 2441 | return ret; |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * Command LTTNG_ENABLE_EVENT processed by the client thread. |
| 2446 | * We own filter, exclusion, and filter_expression. |
| 2447 | */ |
| 2448 | int cmd_enable_event(struct command_ctx *cmd_ctx, |
| 2449 | struct lttng_event *event, |
| 2450 | char *filter_expression, |
| 2451 | struct lttng_event_exclusion *exclusion, |
| 2452 | struct lttng_bytecode *bytecode, |
| 2453 | int wpipe) |
| 2454 | { |
| 2455 | int ret; |
| 2456 | /* |
| 2457 | * Copied to ensure proper alignment since 'lsm' is a packed structure. |
| 2458 | */ |
| 2459 | const lttng_domain command_domain = cmd_ctx->lsm.domain; |
| 2460 | |
| 2461 | /* |
| 2462 | * The ownership of the following parameters is transferred to |
| 2463 | * _cmd_enable_event: |
| 2464 | * |
| 2465 | * - filter_expression, |
| 2466 | * - bytecode, |
| 2467 | * - exclusion |
| 2468 | */ |
| 2469 | ret = _cmd_enable_event(cmd_ctx->session, |
| 2470 | &command_domain, |
| 2471 | cmd_ctx->lsm.u.enable.channel_name, |
| 2472 | event, |
| 2473 | filter_expression, |
| 2474 | bytecode, |
| 2475 | exclusion, |
| 2476 | wpipe, |
| 2477 | false); |
| 2478 | filter_expression = nullptr; |
| 2479 | bytecode = nullptr; |
| 2480 | exclusion = nullptr; |
| 2481 | return ret; |
| 2482 | } |
| 2483 | |
| 2484 | /* |
| 2485 | * Enable an event which is internal to LTTng. An internal should |
| 2486 | * never be made visible to clients and are immune to checks such as |
| 2487 | * reserved names. |
| 2488 | */ |
| 2489 | static int cmd_enable_event_internal(struct ltt_session *session, |
| 2490 | const struct lttng_domain *domain, |
| 2491 | char *channel_name, |
| 2492 | struct lttng_event *event, |
| 2493 | char *filter_expression, |
| 2494 | struct lttng_bytecode *filter, |
| 2495 | struct lttng_event_exclusion *exclusion, |
| 2496 | int wpipe) |
| 2497 | { |
| 2498 | return _cmd_enable_event(session, |
| 2499 | domain, |
| 2500 | channel_name, |
| 2501 | event, |
| 2502 | filter_expression, |
| 2503 | filter, |
| 2504 | exclusion, |
| 2505 | wpipe, |
| 2506 | true); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. |
| 2511 | */ |
| 2512 | enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain, |
| 2513 | struct lttng_payload *reply_payload) |
| 2514 | { |
| 2515 | enum lttng_error_code ret_code; |
| 2516 | int ret; |
| 2517 | ssize_t i, nb_events = 0; |
| 2518 | struct lttng_event *events = nullptr; |
| 2519 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2520 | size_t reply_command_header_offset; |
| 2521 | |
| 2522 | assert(reply_payload); |
| 2523 | |
| 2524 | /* Reserve space for command reply header. */ |
| 2525 | reply_command_header_offset = reply_payload->buffer.size; |
| 2526 | ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer, |
| 2527 | reply_command_header_offset + |
| 2528 | sizeof(struct lttcomm_list_command_header)); |
| 2529 | if (ret) { |
| 2530 | ret_code = LTTNG_ERR_NOMEM; |
| 2531 | goto error; |
| 2532 | } |
| 2533 | |
| 2534 | switch (domain) { |
| 2535 | case LTTNG_DOMAIN_KERNEL: |
| 2536 | nb_events = kernel_list_events(&events); |
| 2537 | if (nb_events < 0) { |
| 2538 | ret_code = LTTNG_ERR_KERN_LIST_FAIL; |
| 2539 | goto error; |
| 2540 | } |
| 2541 | break; |
| 2542 | case LTTNG_DOMAIN_UST: |
| 2543 | nb_events = ust_app_list_events(&events); |
| 2544 | if (nb_events < 0) { |
| 2545 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2546 | goto error; |
| 2547 | } |
| 2548 | break; |
| 2549 | case LTTNG_DOMAIN_LOG4J: |
| 2550 | case LTTNG_DOMAIN_JUL: |
| 2551 | case LTTNG_DOMAIN_PYTHON: |
| 2552 | nb_events = agent_list_events(&events, domain); |
| 2553 | if (nb_events < 0) { |
| 2554 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2555 | goto error; |
| 2556 | } |
| 2557 | break; |
| 2558 | default: |
| 2559 | ret_code = LTTNG_ERR_UND; |
| 2560 | goto error; |
| 2561 | } |
| 2562 | |
| 2563 | for (i = 0; i < nb_events; i++) { |
| 2564 | ret = lttng_event_serialize( |
| 2565 | &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload); |
| 2566 | if (ret) { |
| 2567 | ret_code = LTTNG_ERR_NOMEM; |
| 2568 | goto error; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | if (nb_events > UINT32_MAX) { |
| 2573 | ERR("Tracepoint count would overflow the tracepoint listing command's reply"); |
| 2574 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2575 | goto error; |
| 2576 | } |
| 2577 | |
| 2578 | /* Update command reply header. */ |
| 2579 | reply_command_header.count = (uint32_t) nb_events; |
| 2580 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 2581 | &reply_command_header, |
| 2582 | sizeof(reply_command_header)); |
| 2583 | |
| 2584 | ret_code = LTTNG_OK; |
| 2585 | error: |
| 2586 | free(events); |
| 2587 | return ret_code; |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. |
| 2592 | */ |
| 2593 | enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
| 2594 | struct lttng_payload *reply) |
| 2595 | { |
| 2596 | enum lttng_error_code ret_code; |
| 2597 | int ret; |
| 2598 | unsigned int i, nb_fields; |
| 2599 | struct lttng_event_field *fields = nullptr; |
| 2600 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2601 | size_t reply_command_header_offset; |
| 2602 | |
| 2603 | assert(reply); |
| 2604 | |
| 2605 | /* Reserve space for command reply header. */ |
| 2606 | reply_command_header_offset = reply->buffer.size; |
| 2607 | ret = lttng_dynamic_buffer_set_size(&reply->buffer, |
| 2608 | reply_command_header_offset + |
| 2609 | sizeof(struct lttcomm_list_command_header)); |
| 2610 | if (ret) { |
| 2611 | ret_code = LTTNG_ERR_NOMEM; |
| 2612 | goto error; |
| 2613 | } |
| 2614 | |
| 2615 | switch (domain) { |
| 2616 | case LTTNG_DOMAIN_UST: |
| 2617 | ret = ust_app_list_event_fields(&fields); |
| 2618 | if (ret < 0) { |
| 2619 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2620 | goto error; |
| 2621 | } |
| 2622 | |
| 2623 | break; |
| 2624 | case LTTNG_DOMAIN_KERNEL: |
| 2625 | default: /* fall-through */ |
| 2626 | ret_code = LTTNG_ERR_UND; |
| 2627 | goto error; |
| 2628 | } |
| 2629 | |
| 2630 | nb_fields = ret; |
| 2631 | |
| 2632 | for (i = 0; i < nb_fields; i++) { |
| 2633 | ret = lttng_event_field_serialize(&fields[i], reply); |
| 2634 | if (ret) { |
| 2635 | ret_code = LTTNG_ERR_NOMEM; |
| 2636 | goto error; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | if (nb_fields > UINT32_MAX) { |
| 2641 | ERR("Tracepoint field count would overflow the tracepoint field listing command's reply"); |
| 2642 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2643 | goto error; |
| 2644 | } |
| 2645 | |
| 2646 | /* Update command reply header. */ |
| 2647 | reply_command_header.count = (uint32_t) nb_fields; |
| 2648 | |
| 2649 | memcpy(reply->buffer.data + reply_command_header_offset, |
| 2650 | &reply_command_header, |
| 2651 | sizeof(reply_command_header)); |
| 2652 | |
| 2653 | ret_code = LTTNG_OK; |
| 2654 | |
| 2655 | error: |
| 2656 | free(fields); |
| 2657 | return ret_code; |
| 2658 | } |
| 2659 | |
| 2660 | enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload) |
| 2661 | { |
| 2662 | enum lttng_error_code ret_code; |
| 2663 | ssize_t nb_events, i; |
| 2664 | int ret; |
| 2665 | struct lttng_event *events = nullptr; |
| 2666 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2667 | size_t reply_command_header_offset; |
| 2668 | |
| 2669 | assert(reply_payload); |
| 2670 | |
| 2671 | /* Reserve space for command reply header. */ |
| 2672 | reply_command_header_offset = reply_payload->buffer.size; |
| 2673 | ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer, |
| 2674 | reply_command_header_offset + |
| 2675 | sizeof(struct lttcomm_list_command_header)); |
| 2676 | if (ret) { |
| 2677 | ret_code = LTTNG_ERR_NOMEM; |
| 2678 | goto end; |
| 2679 | } |
| 2680 | |
| 2681 | nb_events = syscall_table_list(&events); |
| 2682 | if (nb_events < 0) { |
| 2683 | ret_code = (enum lttng_error_code) - nb_events; |
| 2684 | goto end; |
| 2685 | } |
| 2686 | |
| 2687 | for (i = 0; i < nb_events; i++) { |
| 2688 | ret = lttng_event_serialize( |
| 2689 | &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload); |
| 2690 | if (ret) { |
| 2691 | ret_code = LTTNG_ERR_NOMEM; |
| 2692 | goto end; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (nb_events > UINT32_MAX) { |
| 2697 | ERR("Syscall count would overflow the syscall listing command's reply"); |
| 2698 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2699 | goto end; |
| 2700 | } |
| 2701 | |
| 2702 | /* Update command reply header. */ |
| 2703 | reply_command_header.count = (uint32_t) nb_events; |
| 2704 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 2705 | &reply_command_header, |
| 2706 | sizeof(reply_command_header)); |
| 2707 | |
| 2708 | ret_code = LTTNG_OK; |
| 2709 | end: |
| 2710 | free(events); |
| 2711 | return ret_code; |
| 2712 | } |
| 2713 | |
| 2714 | /* |
| 2715 | * Command LTTNG_START_TRACE processed by the client thread. |
| 2716 | * |
| 2717 | * Called with session mutex held. |
| 2718 | */ |
| 2719 | int cmd_start_trace(struct ltt_session *session) |
| 2720 | { |
| 2721 | enum lttng_error_code ret; |
| 2722 | unsigned long nb_chan = 0; |
| 2723 | struct ltt_kernel_session *ksession; |
| 2724 | struct ltt_ust_session *usess; |
| 2725 | const bool session_rotated_after_last_stop = session->rotated_after_last_stop; |
| 2726 | const bool session_cleared_after_last_stop = session->cleared_after_last_stop; |
| 2727 | |
| 2728 | LTTNG_ASSERT(session); |
| 2729 | |
| 2730 | /* Ease our life a bit ;) */ |
| 2731 | ksession = session->kernel_session; |
| 2732 | usess = session->ust_session; |
| 2733 | |
| 2734 | /* Is the session already started? */ |
| 2735 | if (session->active) { |
| 2736 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 2737 | /* Perform nothing */ |
| 2738 | goto end; |
| 2739 | } |
| 2740 | |
| 2741 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING && |
| 2742 | !session->current_trace_chunk) { |
| 2743 | /* |
| 2744 | * A rotation was launched while the session was stopped and |
| 2745 | * it has not been completed yet. It is not possible to start |
| 2746 | * the session since starting the session here would require a |
| 2747 | * rotation from "NULL" to a new trace chunk. That rotation |
| 2748 | * would overlap with the ongoing rotation, which is not |
| 2749 | * supported. |
| 2750 | */ |
| 2751 | WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing", |
| 2752 | session->name); |
| 2753 | ret = LTTNG_ERR_ROTATION_PENDING; |
| 2754 | goto error; |
| 2755 | } |
| 2756 | |
| 2757 | /* |
| 2758 | * Starting a session without channel is useless since after that it's not |
| 2759 | * possible to enable channel thus inform the client. |
| 2760 | */ |
| 2761 | if (usess && usess->domain_global.channels) { |
| 2762 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); |
| 2763 | } |
| 2764 | if (ksession) { |
| 2765 | nb_chan += ksession->channel_count; |
| 2766 | } |
| 2767 | if (!nb_chan) { |
| 2768 | ret = LTTNG_ERR_NO_CHANNEL; |
| 2769 | goto error; |
| 2770 | } |
| 2771 | |
| 2772 | session->active = true; |
| 2773 | session->rotated_after_last_stop = false; |
| 2774 | session->cleared_after_last_stop = false; |
| 2775 | if (session->output_traces && !session->current_trace_chunk) { |
| 2776 | if (!session->has_been_started) { |
| 2777 | struct lttng_trace_chunk *trace_chunk; |
| 2778 | |
| 2779 | DBG("Creating initial trace chunk of session \"%s\"", session->name); |
| 2780 | trace_chunk = |
| 2781 | session_create_new_trace_chunk(session, nullptr, nullptr, nullptr); |
| 2782 | if (!trace_chunk) { |
| 2783 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 2784 | goto error; |
| 2785 | } |
| 2786 | LTTNG_ASSERT(!session->current_trace_chunk); |
| 2787 | ret = (lttng_error_code) session_set_trace_chunk( |
| 2788 | session, trace_chunk, nullptr); |
| 2789 | lttng_trace_chunk_put(trace_chunk); |
| 2790 | if (ret) { |
| 2791 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 2792 | goto error; |
| 2793 | } |
| 2794 | } else { |
| 2795 | DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk", |
| 2796 | session->name); |
| 2797 | /* |
| 2798 | * Rotate existing streams into the new chunk. |
| 2799 | * This is a "quiet" rotation has no client has |
| 2800 | * explicitly requested this operation. |
| 2801 | * |
| 2802 | * There is also no need to wait for the rotation |
| 2803 | * to complete as it will happen immediately. No data |
| 2804 | * was produced as the session was stopped, so the |
| 2805 | * rotation should happen on reception of the command. |
| 2806 | */ |
| 2807 | ret = (lttng_error_code) cmd_rotate_session( |
| 2808 | session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); |
| 2809 | if (ret != LTTNG_OK) { |
| 2810 | goto error; |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | /* Kernel tracing */ |
| 2816 | if (ksession != nullptr) { |
| 2817 | DBG("Start kernel tracing session %s", session->name); |
| 2818 | ret = (lttng_error_code) start_kernel_session(ksession); |
| 2819 | if (ret != LTTNG_OK) { |
| 2820 | goto error; |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | /* Flag session that trace should start automatically */ |
| 2825 | if (usess) { |
| 2826 | int int_ret = ust_app_start_trace_all(usess); |
| 2827 | |
| 2828 | if (int_ret < 0) { |
| 2829 | ret = LTTNG_ERR_UST_START_FAIL; |
| 2830 | goto error; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * Open a packet in every stream of the session to ensure that viewers |
| 2836 | * can correctly identify the boundaries of the periods during which |
| 2837 | * tracing was active for this session. |
| 2838 | */ |
| 2839 | ret = session_open_packets(session); |
| 2840 | if (ret != LTTNG_OK) { |
| 2841 | goto error; |
| 2842 | } |
| 2843 | |
| 2844 | /* |
| 2845 | * Clear the flag that indicates that a rotation was done while the |
| 2846 | * session was stopped. |
| 2847 | */ |
| 2848 | session->rotated_after_last_stop = false; |
| 2849 | |
| 2850 | if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) { |
| 2851 | int int_ret = timer_session_rotation_schedule_timer_start( |
| 2852 | session, session->rotate_timer_period); |
| 2853 | |
| 2854 | if (int_ret < 0) { |
| 2855 | ERR("Failed to enable rotate timer"); |
| 2856 | ret = LTTNG_ERR_UNK; |
| 2857 | goto error; |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | ret = LTTNG_OK; |
| 2862 | |
| 2863 | error: |
| 2864 | if (ret == LTTNG_OK) { |
| 2865 | /* Flag this after a successful start. */ |
| 2866 | session->has_been_started = true; |
| 2867 | } else { |
| 2868 | session->active = false; |
| 2869 | /* Restore initial state on error. */ |
| 2870 | session->rotated_after_last_stop = session_rotated_after_last_stop; |
| 2871 | session->cleared_after_last_stop = session_cleared_after_last_stop; |
| 2872 | } |
| 2873 | end: |
| 2874 | return ret; |
| 2875 | } |
| 2876 | |
| 2877 | /* |
| 2878 | * Command LTTNG_STOP_TRACE processed by the client thread. |
| 2879 | */ |
| 2880 | int cmd_stop_trace(struct ltt_session *session) |
| 2881 | { |
| 2882 | int ret; |
| 2883 | struct ltt_kernel_session *ksession; |
| 2884 | struct ltt_ust_session *usess; |
| 2885 | |
| 2886 | LTTNG_ASSERT(session); |
| 2887 | |
| 2888 | DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
| 2889 | /* Short cut */ |
| 2890 | ksession = session->kernel_session; |
| 2891 | usess = session->ust_session; |
| 2892 | |
| 2893 | /* Session is not active. Skip everything and inform the client. */ |
| 2894 | if (!session->active) { |
| 2895 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
| 2896 | goto error; |
| 2897 | } |
| 2898 | |
| 2899 | ret = stop_kernel_session(ksession); |
| 2900 | if (ret != LTTNG_OK) { |
| 2901 | goto error; |
| 2902 | } |
| 2903 | |
| 2904 | if (usess && usess->active) { |
| 2905 | ret = ust_app_stop_trace_all(usess); |
| 2906 | if (ret < 0) { |
| 2907 | ret = LTTNG_ERR_UST_STOP_FAIL; |
| 2908 | goto error; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
| 2913 | /* Flag inactive after a successful stop. */ |
| 2914 | session->active = false; |
| 2915 | ret = LTTNG_OK; |
| 2916 | |
| 2917 | error: |
| 2918 | return ret; |
| 2919 | } |
| 2920 | |
| 2921 | /* |
| 2922 | * Set the base_path of the session only if subdir of a control uris is set. |
| 2923 | * Return LTTNG_OK on success, otherwise LTTNG_ERR_*. |
| 2924 | */ |
| 2925 | static int |
| 2926 | set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris) |
| 2927 | { |
| 2928 | int ret; |
| 2929 | size_t i; |
| 2930 | |
| 2931 | for (i = 0; i < nb_uri; i++) { |
| 2932 | if (uris[i].stype != LTTNG_STREAM_CONTROL || uris[i].subdir[0] == '\0') { |
| 2933 | /* Not interested in these URIs */ |
| 2934 | continue; |
| 2935 | } |
| 2936 | |
| 2937 | if (session->base_path != nullptr) { |
| 2938 | free(session->base_path); |
| 2939 | session->base_path = nullptr; |
| 2940 | } |
| 2941 | |
| 2942 | /* Set session base_path */ |
| 2943 | session->base_path = strdup(uris[i].subdir); |
| 2944 | if (!session->base_path) { |
| 2945 | PERROR("Failed to copy base path \"%s\" to session \"%s\"", |
| 2946 | uris[i].subdir, |
| 2947 | session->name); |
| 2948 | ret = LTTNG_ERR_NOMEM; |
| 2949 | goto error; |
| 2950 | } |
| 2951 | DBG2("Setting base path \"%s\" for session \"%s\"", |
| 2952 | session->base_path, |
| 2953 | session->name); |
| 2954 | } |
| 2955 | ret = LTTNG_OK; |
| 2956 | error: |
| 2957 | return ret; |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. |
| 2962 | */ |
| 2963 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris) |
| 2964 | { |
| 2965 | int ret, i; |
| 2966 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2967 | struct ltt_ust_session *usess = session->ust_session; |
| 2968 | |
| 2969 | LTTNG_ASSERT(session); |
| 2970 | LTTNG_ASSERT(uris); |
| 2971 | LTTNG_ASSERT(nb_uri > 0); |
| 2972 | |
| 2973 | /* Can't set consumer URI if the session is active. */ |
| 2974 | if (session->active) { |
| 2975 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 2976 | goto error; |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Set the session base path if any. This is done inside |
| 2981 | * cmd_set_consumer_uri to preserve backward compatibility of the |
| 2982 | * previous session creation api vs the session descriptor api. |
| 2983 | */ |
| 2984 | ret = set_session_base_path_from_uris(session, nb_uri, uris); |
| 2985 | if (ret != LTTNG_OK) { |
| 2986 | goto error; |
| 2987 | } |
| 2988 | |
| 2989 | /* Set the "global" consumer URIs */ |
| 2990 | for (i = 0; i < nb_uri; i++) { |
| 2991 | ret = add_uri_to_consumer(session, session->consumer, &uris[i], LTTNG_DOMAIN_NONE); |
| 2992 | if (ret != LTTNG_OK) { |
| 2993 | goto error; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | /* Set UST session URIs */ |
| 2998 | if (session->ust_session) { |
| 2999 | for (i = 0; i < nb_uri; i++) { |
| 3000 | ret = add_uri_to_consumer(session, |
| 3001 | session->ust_session->consumer, |
| 3002 | &uris[i], |
| 3003 | LTTNG_DOMAIN_UST); |
| 3004 | if (ret != LTTNG_OK) { |
| 3005 | goto error; |
| 3006 | } |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | /* Set kernel session URIs */ |
| 3011 | if (session->kernel_session) { |
| 3012 | for (i = 0; i < nb_uri; i++) { |
| 3013 | ret = add_uri_to_consumer(session, |
| 3014 | session->kernel_session->consumer, |
| 3015 | &uris[i], |
| 3016 | LTTNG_DOMAIN_KERNEL); |
| 3017 | if (ret != LTTNG_OK) { |
| 3018 | goto error; |
| 3019 | } |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Make sure to set the session in output mode after we set URI since a |
| 3025 | * session can be created without URL (thus flagged in no output mode). |
| 3026 | */ |
| 3027 | session->output_traces = 1; |
| 3028 | if (ksess) { |
| 3029 | ksess->output_traces = 1; |
| 3030 | } |
| 3031 | |
| 3032 | if (usess) { |
| 3033 | usess->output_traces = 1; |
| 3034 | } |
| 3035 | |
| 3036 | /* All good! */ |
| 3037 | ret = LTTNG_OK; |
| 3038 | |
| 3039 | error: |
| 3040 | return ret; |
| 3041 | } |
| 3042 | |
| 3043 | static enum lttng_error_code |
| 3044 | set_session_output_from_descriptor(struct ltt_session *session, |
| 3045 | const struct lttng_session_descriptor *descriptor) |
| 3046 | { |
| 3047 | int ret; |
| 3048 | enum lttng_error_code ret_code = LTTNG_OK; |
| 3049 | enum lttng_session_descriptor_type session_type = |
| 3050 | lttng_session_descriptor_get_type(descriptor); |
| 3051 | enum lttng_session_descriptor_output_type output_type = |
| 3052 | lttng_session_descriptor_get_output_type(descriptor); |
| 3053 | struct lttng_uri uris[2] = {}; |
| 3054 | size_t uri_count = 0; |
| 3055 | |
| 3056 | switch (output_type) { |
| 3057 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: |
| 3058 | goto end; |
| 3059 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: |
| 3060 | lttng_session_descriptor_get_local_output_uri(descriptor, &uris[0]); |
| 3061 | uri_count = 1; |
| 3062 | break; |
| 3063 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: |
| 3064 | lttng_session_descriptor_get_network_output_uris(descriptor, &uris[0], &uris[1]); |
| 3065 | uri_count = 2; |
| 3066 | break; |
| 3067 | default: |
| 3068 | ret_code = LTTNG_ERR_INVALID; |
| 3069 | goto end; |
| 3070 | } |
| 3071 | |
| 3072 | switch (session_type) { |
| 3073 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: |
| 3074 | { |
| 3075 | struct snapshot_output *new_output = nullptr; |
| 3076 | |
| 3077 | new_output = snapshot_output_alloc(); |
| 3078 | if (!new_output) { |
| 3079 | ret_code = LTTNG_ERR_NOMEM; |
| 3080 | goto end; |
| 3081 | } |
| 3082 | |
| 3083 | ret = snapshot_output_init_with_uri(session, |
| 3084 | DEFAULT_SNAPSHOT_MAX_SIZE, |
| 3085 | nullptr, |
| 3086 | uris, |
| 3087 | uri_count, |
| 3088 | session->consumer, |
| 3089 | new_output, |
| 3090 | &session->snapshot); |
| 3091 | if (ret < 0) { |
| 3092 | ret_code = (ret == -ENOMEM) ? LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; |
| 3093 | snapshot_output_destroy(new_output); |
| 3094 | goto end; |
| 3095 | } |
| 3096 | snapshot_add_output(&session->snapshot, new_output); |
| 3097 | break; |
| 3098 | } |
| 3099 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: |
| 3100 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: |
| 3101 | { |
| 3102 | ret_code = (lttng_error_code) cmd_set_consumer_uri(session, uri_count, uris); |
| 3103 | break; |
| 3104 | } |
| 3105 | default: |
| 3106 | ret_code = LTTNG_ERR_INVALID; |
| 3107 | goto end; |
| 3108 | } |
| 3109 | end: |
| 3110 | return ret_code; |
| 3111 | } |
| 3112 | |
| 3113 | static enum lttng_error_code |
| 3114 | cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor, |
| 3115 | const lttng_sock_cred *creds, |
| 3116 | const char *home_path) |
| 3117 | { |
| 3118 | int ret; |
| 3119 | enum lttng_error_code ret_code; |
| 3120 | const char *session_name; |
| 3121 | struct ltt_session *new_session = nullptr; |
| 3122 | enum lttng_session_descriptor_status descriptor_status; |
| 3123 | |
| 3124 | session_lock_list(); |
| 3125 | if (home_path) { |
| 3126 | if (*home_path != '/') { |
| 3127 | ERR("Home path provided by client is not absolute"); |
| 3128 | ret_code = LTTNG_ERR_INVALID; |
| 3129 | goto end; |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | descriptor_status = lttng_session_descriptor_get_session_name(descriptor, &session_name); |
| 3134 | switch (descriptor_status) { |
| 3135 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: |
| 3136 | break; |
| 3137 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: |
| 3138 | session_name = nullptr; |
| 3139 | break; |
| 3140 | default: |
| 3141 | ret_code = LTTNG_ERR_INVALID; |
| 3142 | goto end; |
| 3143 | } |
| 3144 | |
| 3145 | ret_code = session_create(session_name, creds->uid, creds->gid, &new_session); |
| 3146 | if (ret_code != LTTNG_OK) { |
| 3147 | goto end; |
| 3148 | } |
| 3149 | |
| 3150 | ret_code = notification_thread_command_add_session(the_notification_thread_handle, |
| 3151 | new_session->id, |
| 3152 | new_session->name, |
| 3153 | new_session->uid, |
| 3154 | new_session->gid); |
| 3155 | if (ret_code != LTTNG_OK) { |
| 3156 | goto end; |
| 3157 | } |
| 3158 | |
| 3159 | /* Announce the session's destruction to the notification thread when it is destroyed. */ |
| 3160 | ret = session_add_destroy_notifier( |
| 3161 | new_session, |
| 3162 | [](const struct ltt_session *session, void *user_data __attribute__((unused))) { |
| 3163 | (void) notification_thread_command_remove_session( |
| 3164 | the_notification_thread_handle, session->id); |
| 3165 | }, |
| 3166 | nullptr); |
| 3167 | if (ret) { |
| 3168 | PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s", |
| 3169 | new_session->name); |
| 3170 | ret = LTTNG_ERR_NOMEM; |
| 3171 | goto end; |
| 3172 | } |
| 3173 | |
| 3174 | if (!session_name) { |
| 3175 | ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name); |
| 3176 | if (ret) { |
| 3177 | ret_code = LTTNG_ERR_SESSION_FAIL; |
| 3178 | goto end; |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | if (!lttng_session_descriptor_is_output_destination_initialized(descriptor)) { |
| 3183 | /* |
| 3184 | * Only include the session's creation time in the output |
| 3185 | * destination if the name of the session itself was |
| 3186 | * not auto-generated. |
| 3187 | */ |
| 3188 | ret_code = lttng_session_descriptor_set_default_output( |
| 3189 | descriptor, |
| 3190 | session_name ? &new_session->creation_time : nullptr, |
| 3191 | home_path); |
| 3192 | if (ret_code != LTTNG_OK) { |
| 3193 | goto end; |
| 3194 | } |
| 3195 | } else { |
| 3196 | new_session->has_user_specified_directory = |
| 3197 | lttng_session_descriptor_has_output_directory(descriptor); |
| 3198 | } |
| 3199 | |
| 3200 | switch (lttng_session_descriptor_get_type(descriptor)) { |
| 3201 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: |
| 3202 | new_session->snapshot_mode = 1; |
| 3203 | break; |
| 3204 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: |
| 3205 | new_session->live_timer = |
| 3206 | lttng_session_descriptor_live_get_timer_interval(descriptor); |
| 3207 | break; |
| 3208 | default: |
| 3209 | break; |
| 3210 | } |
| 3211 | |
| 3212 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
| 3213 | if (ret_code != LTTNG_OK) { |
| 3214 | goto end; |
| 3215 | } |
| 3216 | new_session->consumer->enabled = true; |
| 3217 | ret_code = LTTNG_OK; |
| 3218 | end: |
| 3219 | /* Release reference provided by the session_create function. */ |
| 3220 | session_put(new_session); |
| 3221 | if (ret_code != LTTNG_OK && new_session) { |
| 3222 | /* Release the global reference on error. */ |
| 3223 | session_destroy(new_session); |
| 3224 | } |
| 3225 | session_unlock_list(); |
| 3226 | return ret_code; |
| 3227 | } |
| 3228 | |
| 3229 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, |
| 3230 | int sock, |
| 3231 | struct lttng_session_descriptor **return_descriptor) |
| 3232 | { |
| 3233 | int ret; |
| 3234 | size_t payload_size; |
| 3235 | struct lttng_dynamic_buffer payload; |
| 3236 | struct lttng_buffer_view home_dir_view; |
| 3237 | struct lttng_buffer_view session_descriptor_view; |
| 3238 | struct lttng_session_descriptor *session_descriptor = nullptr; |
| 3239 | enum lttng_error_code ret_code; |
| 3240 | |
| 3241 | lttng_dynamic_buffer_init(&payload); |
| 3242 | if (cmd_ctx->lsm.u.create_session.home_dir_size >= LTTNG_PATH_MAX) { |
| 3243 | ret_code = LTTNG_ERR_INVALID; |
| 3244 | goto error; |
| 3245 | } |
| 3246 | if (cmd_ctx->lsm.u.create_session.session_descriptor_size > |
| 3247 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { |
| 3248 | ret_code = LTTNG_ERR_INVALID; |
| 3249 | goto error; |
| 3250 | } |
| 3251 | |
| 3252 | payload_size = cmd_ctx->lsm.u.create_session.home_dir_size + |
| 3253 | cmd_ctx->lsm.u.create_session.session_descriptor_size; |
| 3254 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); |
| 3255 | if (ret) { |
| 3256 | ret_code = LTTNG_ERR_NOMEM; |
| 3257 | goto error; |
| 3258 | } |
| 3259 | |
| 3260 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
| 3261 | if (ret <= 0) { |
| 3262 | ERR("Reception of session descriptor failed, aborting."); |
| 3263 | ret_code = LTTNG_ERR_SESSION_FAIL; |
| 3264 | goto error; |
| 3265 | } |
| 3266 | |
| 3267 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
| 3268 | &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size); |
| 3269 | if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 && |
| 3270 | !lttng_buffer_view_is_valid(&home_dir_view)) { |
| 3271 | ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory"); |
| 3272 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; |
| 3273 | goto error; |
| 3274 | } |
| 3275 | |
| 3276 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( |
| 3277 | &payload, |
| 3278 | cmd_ctx->lsm.u.create_session.home_dir_size, |
| 3279 | cmd_ctx->lsm.u.create_session.session_descriptor_size); |
| 3280 | if (!lttng_buffer_view_is_valid(&session_descriptor_view)) { |
| 3281 | ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor"); |
| 3282 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; |
| 3283 | goto error; |
| 3284 | } |
| 3285 | |
| 3286 | ret = lttng_session_descriptor_create_from_buffer(&session_descriptor_view, |
| 3287 | &session_descriptor); |
| 3288 | if (ret < 0) { |
| 3289 | ERR("Failed to create session descriptor from payload of \"create session\" command"); |
| 3290 | ret_code = LTTNG_ERR_INVALID; |
| 3291 | goto error; |
| 3292 | } |
| 3293 | |
| 3294 | /* |
| 3295 | * Sets the descriptor's auto-generated properties (name, output) if |
| 3296 | * needed. |
| 3297 | */ |
| 3298 | ret_code = cmd_create_session_from_descriptor(session_descriptor, |
| 3299 | &cmd_ctx->creds, |
| 3300 | home_dir_view.size ? home_dir_view.data : |
| 3301 | nullptr); |
| 3302 | if (ret_code != LTTNG_OK) { |
| 3303 | goto error; |
| 3304 | } |
| 3305 | |
| 3306 | ret_code = LTTNG_OK; |
| 3307 | *return_descriptor = session_descriptor; |
| 3308 | session_descriptor = nullptr; |
| 3309 | error: |
| 3310 | lttng_dynamic_buffer_reset(&payload); |
| 3311 | lttng_session_descriptor_destroy(session_descriptor); |
| 3312 | return ret_code; |
| 3313 | } |
| 3314 | |
| 3315 | static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context) |
| 3316 | { |
| 3317 | int ret; |
| 3318 | ssize_t comm_ret; |
| 3319 | const struct cmd_destroy_session_reply_context *reply_context = |
| 3320 | (cmd_destroy_session_reply_context *) _reply_context; |
| 3321 | struct lttng_dynamic_buffer payload; |
| 3322 | struct lttcomm_session_destroy_command_header cmd_header; |
| 3323 | struct lttng_trace_archive_location *location = nullptr; |
| 3324 | struct lttcomm_lttng_msg llm = { |
| 3325 | .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION, |
| 3326 | .ret_code = reply_context->destruction_status, |
| 3327 | .pid = UINT32_MAX, |
| 3328 | .cmd_header_size = sizeof(struct lttcomm_session_destroy_command_header), |
| 3329 | .data_size = 0, |
| 3330 | .fd_count = 0, |
| 3331 | }; |
| 3332 | size_t payload_size_before_location; |
| 3333 | |
| 3334 | lttng_dynamic_buffer_init(&payload); |
| 3335 | |
| 3336 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); |
| 3337 | if (ret) { |
| 3338 | ERR("Failed to append session destruction message"); |
| 3339 | goto error; |
| 3340 | } |
| 3341 | |
| 3342 | cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ? |
| 3343 | session->rotation_state : |
| 3344 | LTTNG_ROTATION_STATE_NO_ROTATION); |
| 3345 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, sizeof(cmd_header)); |
| 3346 | if (ret) { |
| 3347 | ERR("Failed to append session destruction command header"); |
| 3348 | goto error; |
| 3349 | } |
| 3350 | |
| 3351 | if (!reply_context->implicit_rotation_on_destroy) { |
| 3352 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", |
| 3353 | session->name); |
| 3354 | goto send_reply; |
| 3355 | } |
| 3356 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { |
| 3357 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", |
| 3358 | session->name); |
| 3359 | goto send_reply; |
| 3360 | } |
| 3361 | |
| 3362 | location = session_get_trace_archive_location(session); |
| 3363 | if (!location) { |
| 3364 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", |
| 3365 | session->name); |
| 3366 | goto error; |
| 3367 | } |
| 3368 | |
| 3369 | payload_size_before_location = payload.size; |
| 3370 | comm_ret = lttng_trace_archive_location_serialize(location, &payload); |
| 3371 | lttng_trace_archive_location_put(location); |
| 3372 | if (comm_ret < 0) { |
| 3373 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", |
| 3374 | session->name); |
| 3375 | goto error; |
| 3376 | } |
| 3377 | /* Update the message to indicate the location's length. */ |
| 3378 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = |
| 3379 | payload.size - payload_size_before_location; |
| 3380 | send_reply: |
| 3381 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, payload.data, payload.size); |
| 3382 | if (comm_ret != (ssize_t) payload.size) { |
| 3383 | ERR("Failed to send result of the destruction of session \"%s\" to client", |
| 3384 | session->name); |
| 3385 | } |
| 3386 | error: |
| 3387 | ret = close(reply_context->reply_sock_fd); |
| 3388 | if (ret) { |
| 3389 | PERROR("Failed to close client socket in deferred session destroy reply"); |
| 3390 | } |
| 3391 | lttng_dynamic_buffer_reset(&payload); |
| 3392 | free(_reply_context); |
| 3393 | } |
| 3394 | |
| 3395 | /* |
| 3396 | * Command LTTNG_DESTROY_SESSION processed by the client thread. |
| 3397 | * |
| 3398 | * Called with session lock held. |
| 3399 | */ |
| 3400 | int cmd_destroy_session(struct ltt_session *session, int *sock_fd) |
| 3401 | { |
| 3402 | int ret; |
| 3403 | enum lttng_error_code destruction_last_error = LTTNG_OK; |
| 3404 | struct cmd_destroy_session_reply_context *reply_context = nullptr; |
| 3405 | |
| 3406 | if (sock_fd) { |
| 3407 | reply_context = zmalloc<cmd_destroy_session_reply_context>(); |
| 3408 | if (!reply_context) { |
| 3409 | ret = LTTNG_ERR_NOMEM; |
| 3410 | goto end; |
| 3411 | } |
| 3412 | |
| 3413 | reply_context->reply_sock_fd = *sock_fd; |
| 3414 | } |
| 3415 | |
| 3416 | /* Safety net */ |
| 3417 | LTTNG_ASSERT(session); |
| 3418 | |
| 3419 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id); |
| 3420 | if (session->active) { |
| 3421 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", |
| 3422 | session->name); |
| 3423 | ret = cmd_stop_trace(session); |
| 3424 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
| 3425 | /* Carry on with the destruction of the session. */ |
| 3426 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", |
| 3427 | session->name, |
| 3428 | lttng_strerror(-ret)); |
| 3429 | destruction_last_error = (lttng_error_code) ret; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | if (session->rotation_schedule_timer_enabled) { |
| 3434 | if (timer_session_rotation_schedule_timer_stop(session)) { |
| 3435 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", |
| 3436 | session->name); |
| 3437 | destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR; |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | if (session->rotate_size) { |
| 3442 | try { |
| 3443 | the_rotation_thread_handle->unsubscribe_session_consumed_size_rotation( |
| 3444 | *session); |
| 3445 | } catch (const std::exception& e) { |
| 3446 | /* Continue the destruction of the session anyway. */ |
| 3447 | ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s", |
| 3448 | e.what()); |
| 3449 | } |
| 3450 | |
| 3451 | session->rotate_size = 0; |
| 3452 | } |
| 3453 | |
| 3454 | if (session->rotated && session->current_trace_chunk && session->output_traces) { |
| 3455 | /* |
| 3456 | * Perform a last rotation on destruction if rotations have |
| 3457 | * occurred during the session's lifetime. |
| 3458 | */ |
| 3459 | ret = cmd_rotate_session( |
| 3460 | session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); |
| 3461 | if (ret != LTTNG_OK) { |
| 3462 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", |
| 3463 | session->name, |
| 3464 | lttng_strerror(-ret)); |
| 3465 | destruction_last_error = (lttng_error_code) -ret; |
| 3466 | } |
| 3467 | if (reply_context) { |
| 3468 | reply_context->implicit_rotation_on_destroy = true; |
| 3469 | } |
| 3470 | } else if (session->has_been_started && session->current_trace_chunk) { |
| 3471 | /* |
| 3472 | * The user has not triggered a session rotation. However, to |
| 3473 | * ensure all data has been consumed, the session is rotated |
| 3474 | * to a 'null' trace chunk before it is destroyed. |
| 3475 | * |
| 3476 | * This is a "quiet" rotation meaning that no notification is |
| 3477 | * emitted and no renaming of the current trace chunk takes |
| 3478 | * place. |
| 3479 | */ |
| 3480 | ret = cmd_rotate_session( |
| 3481 | session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); |
| 3482 | /* |
| 3483 | * Rotation operations may not be supported by the kernel |
| 3484 | * tracer. Hence, do not consider this implicit rotation as |
| 3485 | * a session destruction error. The library has already stopped |
| 3486 | * the session and waited for pending data; there is nothing |
| 3487 | * left to do but complete the destruction of the session. |
| 3488 | */ |
| 3489 | if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) { |
| 3490 | ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s", |
| 3491 | session->name, |
| 3492 | lttng_strerror(ret)); |
| 3493 | destruction_last_error = (lttng_error_code) -ret; |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | if (session->shm_path[0]) { |
| 3498 | /* |
| 3499 | * When a session is created with an explicit shm_path, |
| 3500 | * the consumer daemon will create its shared memory files |
| 3501 | * at that location and will *not* unlink them. This is normal |
| 3502 | * as the intention of that feature is to make it possible |
| 3503 | * to retrieve the content of those files should a crash occur. |
| 3504 | * |
| 3505 | * To ensure the content of those files can be used, the |
| 3506 | * sessiond daemon will replicate the content of the metadata |
| 3507 | * cache in a metadata file. |
| 3508 | * |
| 3509 | * On clean-up, it is expected that the consumer daemon will |
| 3510 | * unlink the shared memory files and that the session daemon |
| 3511 | * will unlink the metadata file. Then, the session's directory |
| 3512 | * in the shm path can be removed. |
| 3513 | * |
| 3514 | * Unfortunately, a flaw in the design of the sessiond's and |
| 3515 | * consumerd's tear down of channels makes it impossible to |
| 3516 | * determine when the sessiond _and_ the consumerd have both |
| 3517 | * destroyed their representation of a channel. For one, the |
| 3518 | * unlinking, close, and rmdir happen in deferred 'call_rcu' |
| 3519 | * callbacks in both daemons. |
| 3520 | * |
| 3521 | * However, it is also impossible for the sessiond to know when |
| 3522 | * the consumer daemon is done destroying its channel(s) since |
| 3523 | * it occurs as a reaction to the closing of the channel's file |
| 3524 | * descriptor. There is no resulting communication initiated |
| 3525 | * from the consumerd to the sessiond to confirm that the |
| 3526 | * operation is completed (and was successful). |
| 3527 | * |
| 3528 | * Until this is all fixed, the session daemon checks for the |
| 3529 | * removal of the session's shm path which makes it possible |
| 3530 | * to safely advertise a session as having been destroyed. |
| 3531 | * |
| 3532 | * Prior to this fix, it was not possible to reliably save |
| 3533 | * a session making use of the --shm-path option, destroy it, |
| 3534 | * and load it again. This is because the creation of the |
| 3535 | * session would fail upon seeing the session's shm path |
| 3536 | * already in existence. |
| 3537 | * |
| 3538 | * Note that none of the error paths in the check for the |
| 3539 | * directory's existence return an error. This is normal |
| 3540 | * as there isn't much that can be done. The session will |
| 3541 | * be destroyed properly, except that we can't offer the |
| 3542 | * guarantee that the same session can be re-created. |
| 3543 | */ |
| 3544 | current_completion_handler = &destroy_completion_handler.handler; |
| 3545 | ret = lttng_strncpy(destroy_completion_handler.shm_path, |
| 3546 | session->shm_path, |
| 3547 | sizeof(destroy_completion_handler.shm_path)); |
| 3548 | LTTNG_ASSERT(!ret); |
| 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | * The session is destroyed. However, note that the command context |
| 3553 | * still holds a reference to the session, thus delaying its destruction |
| 3554 | * _at least_ up to the point when that reference is released. |
| 3555 | */ |
| 3556 | session_destroy(session); |
| 3557 | if (reply_context) { |
| 3558 | reply_context->destruction_status = destruction_last_error; |
| 3559 | ret = session_add_destroy_notifier( |
| 3560 | session, cmd_destroy_session_reply, (void *) reply_context); |
| 3561 | if (ret) { |
| 3562 | ret = LTTNG_ERR_FATAL; |
| 3563 | goto end; |
| 3564 | } else { |
| 3565 | *sock_fd = -1; |
| 3566 | } |
| 3567 | } |
| 3568 | ret = LTTNG_OK; |
| 3569 | end: |
| 3570 | return ret; |
| 3571 | } |
| 3572 | |
| 3573 | /* |
| 3574 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. |
| 3575 | */ |
| 3576 | int cmd_register_consumer(struct ltt_session *session, |
| 3577 | enum lttng_domain_type domain, |
| 3578 | const char *sock_path, |
| 3579 | struct consumer_data *cdata) |
| 3580 | { |
| 3581 | int ret, sock; |
| 3582 | struct consumer_socket *socket = nullptr; |
| 3583 | |
| 3584 | LTTNG_ASSERT(session); |
| 3585 | LTTNG_ASSERT(cdata); |
| 3586 | LTTNG_ASSERT(sock_path); |
| 3587 | |
| 3588 | switch (domain) { |
| 3589 | case LTTNG_DOMAIN_KERNEL: |
| 3590 | { |
| 3591 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3592 | |
| 3593 | LTTNG_ASSERT(ksess); |
| 3594 | |
| 3595 | /* Can't register a consumer if there is already one */ |
| 3596 | if (ksess->consumer_fds_sent != 0) { |
| 3597 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 3598 | goto error; |
| 3599 | } |
| 3600 | |
| 3601 | sock = lttcomm_connect_unix_sock(sock_path); |
| 3602 | if (sock < 0) { |
| 3603 | ret = LTTNG_ERR_CONNECT_FAIL; |
| 3604 | goto error; |
| 3605 | } |
| 3606 | cdata->cmd_sock = sock; |
| 3607 | |
| 3608 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
| 3609 | if (socket == nullptr) { |
| 3610 | ret = close(sock); |
| 3611 | if (ret < 0) { |
| 3612 | PERROR("close register consumer"); |
| 3613 | } |
| 3614 | cdata->cmd_sock = -1; |
| 3615 | ret = LTTNG_ERR_FATAL; |
| 3616 | goto error; |
| 3617 | } |
| 3618 | |
| 3619 | socket->lock = zmalloc<pthread_mutex_t>(); |
| 3620 | if (socket->lock == nullptr) { |
| 3621 | PERROR("zmalloc pthread mutex"); |
| 3622 | ret = LTTNG_ERR_FATAL; |
| 3623 | goto error; |
| 3624 | } |
| 3625 | |
| 3626 | pthread_mutex_init(socket->lock, nullptr); |
| 3627 | socket->registered = 1; |
| 3628 | |
| 3629 | lttng::urcu::read_lock_guard read_lock; |
| 3630 | consumer_add_socket(socket, ksess->consumer); |
| 3631 | |
| 3632 | pthread_mutex_lock(&cdata->pid_mutex); |
| 3633 | cdata->pid = -1; |
| 3634 | pthread_mutex_unlock(&cdata->pid_mutex); |
| 3635 | |
| 3636 | break; |
| 3637 | } |
| 3638 | default: |
| 3639 | /* TODO: Userspace tracing */ |
| 3640 | ret = LTTNG_ERR_UND; |
| 3641 | goto error; |
| 3642 | } |
| 3643 | |
| 3644 | return LTTNG_OK; |
| 3645 | |
| 3646 | error: |
| 3647 | if (socket) { |
| 3648 | consumer_destroy_socket(socket); |
| 3649 | } |
| 3650 | return ret; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * Command LTTNG_LIST_DOMAINS processed by the client thread. |
| 3655 | */ |
| 3656 | ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains) |
| 3657 | { |
| 3658 | int ret, index = 0; |
| 3659 | ssize_t nb_dom = 0; |
| 3660 | struct agent *agt; |
| 3661 | struct lttng_ht_iter iter; |
| 3662 | |
| 3663 | if (session->kernel_session != nullptr) { |
| 3664 | DBG3("Listing domains found kernel domain"); |
| 3665 | nb_dom++; |
| 3666 | } |
| 3667 | |
| 3668 | if (session->ust_session != nullptr) { |
| 3669 | DBG3("Listing domains found UST global domain"); |
| 3670 | nb_dom++; |
| 3671 | |
| 3672 | lttng::urcu::read_lock_guard read_lock; |
| 3673 | |
| 3674 | cds_lfht_for_each_entry ( |
| 3675 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3676 | if (agt->being_used) { |
| 3677 | nb_dom++; |
| 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | if (!nb_dom) { |
| 3683 | goto end; |
| 3684 | } |
| 3685 | |
| 3686 | *domains = calloc<lttng_domain>(nb_dom); |
| 3687 | if (*domains == nullptr) { |
| 3688 | ret = LTTNG_ERR_FATAL; |
| 3689 | goto error; |
| 3690 | } |
| 3691 | |
| 3692 | if (session->kernel_session != nullptr) { |
| 3693 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; |
| 3694 | |
| 3695 | /* Kernel session buffer type is always GLOBAL */ |
| 3696 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; |
| 3697 | |
| 3698 | index++; |
| 3699 | } |
| 3700 | |
| 3701 | if (session->ust_session != nullptr) { |
| 3702 | (*domains)[index].type = LTTNG_DOMAIN_UST; |
| 3703 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
| 3704 | index++; |
| 3705 | |
| 3706 | { |
| 3707 | lttng::urcu::read_lock_guard read_lock; |
| 3708 | |
| 3709 | cds_lfht_for_each_entry ( |
| 3710 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3711 | if (agt->being_used) { |
| 3712 | (*domains)[index].type = agt->domain; |
| 3713 | (*domains)[index].buf_type = |
| 3714 | session->ust_session->buffer_type; |
| 3715 | index++; |
| 3716 | } |
| 3717 | } |
| 3718 | } |
| 3719 | } |
| 3720 | end: |
| 3721 | return nb_dom; |
| 3722 | |
| 3723 | error: |
| 3724 | /* Return negative value to differentiate return code */ |
| 3725 | return -ret; |
| 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Command LTTNG_LIST_CHANNELS processed by the client thread. |
| 3730 | */ |
| 3731 | enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain, |
| 3732 | struct ltt_session *session, |
| 3733 | struct lttng_payload *payload) |
| 3734 | { |
| 3735 | int ret = 0; |
| 3736 | unsigned int i = 0; |
| 3737 | struct lttcomm_list_command_header cmd_header = {}; |
| 3738 | size_t cmd_header_offset; |
| 3739 | enum lttng_error_code ret_code; |
| 3740 | |
| 3741 | assert(session); |
| 3742 | assert(payload); |
| 3743 | |
| 3744 | DBG("Listing channels for session %s", session->name); |
| 3745 | |
| 3746 | cmd_header_offset = payload->buffer.size; |
| 3747 | |
| 3748 | /* Reserve space for command reply header. */ |
| 3749 | ret = lttng_dynamic_buffer_set_size(&payload->buffer, |
| 3750 | cmd_header_offset + sizeof(cmd_header)); |
| 3751 | if (ret) { |
| 3752 | ret_code = LTTNG_ERR_NOMEM; |
| 3753 | goto end; |
| 3754 | } |
| 3755 | |
| 3756 | switch (domain) { |
| 3757 | case LTTNG_DOMAIN_KERNEL: |
| 3758 | { |
| 3759 | /* Kernel channels */ |
| 3760 | struct ltt_kernel_channel *kchan; |
| 3761 | if (session->kernel_session != nullptr) { |
| 3762 | cds_list_for_each_entry ( |
| 3763 | kchan, &session->kernel_session->channel_list.head, list) { |
| 3764 | uint64_t discarded_events, lost_packets; |
| 3765 | struct lttng_channel_extended *extended; |
| 3766 | |
| 3767 | extended = (struct lttng_channel_extended *) |
| 3768 | kchan->channel->attr.extended.ptr; |
| 3769 | |
| 3770 | ret = get_kernel_runtime_stats( |
| 3771 | session, kchan, &discarded_events, &lost_packets); |
| 3772 | if (ret < 0) { |
| 3773 | ret_code = LTTNG_ERR_UNK; |
| 3774 | goto end; |
| 3775 | } |
| 3776 | |
| 3777 | /* |
| 3778 | * Update the discarded_events and lost_packets |
| 3779 | * count for the channel |
| 3780 | */ |
| 3781 | extended->discarded_events = discarded_events; |
| 3782 | extended->lost_packets = lost_packets; |
| 3783 | |
| 3784 | ret = lttng_channel_serialize(kchan->channel, &payload->buffer); |
| 3785 | if (ret) { |
| 3786 | ERR("Failed to serialize lttng_channel: channel name = '%s'", |
| 3787 | kchan->channel->name); |
| 3788 | ret_code = LTTNG_ERR_UNK; |
| 3789 | goto end; |
| 3790 | } |
| 3791 | |
| 3792 | i++; |
| 3793 | } |
| 3794 | } |
| 3795 | break; |
| 3796 | } |
| 3797 | case LTTNG_DOMAIN_UST: |
| 3798 | { |
| 3799 | struct lttng_ht_iter iter; |
| 3800 | struct ltt_ust_channel *uchan; |
| 3801 | |
| 3802 | { |
| 3803 | lttng::urcu::read_lock_guard read_lock; |
| 3804 | |
| 3805 | cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht, |
| 3806 | &iter.iter, |
| 3807 | uchan, |
| 3808 | node.node) { |
| 3809 | uint64_t discarded_events = 0, lost_packets = 0; |
| 3810 | struct lttng_channel *channel = nullptr; |
| 3811 | struct lttng_channel_extended *extended; |
| 3812 | |
| 3813 | channel = trace_ust_channel_to_lttng_channel(uchan); |
| 3814 | if (!channel) { |
| 3815 | ret_code = LTTNG_ERR_NOMEM; |
| 3816 | goto end; |
| 3817 | } |
| 3818 | |
| 3819 | extended = (struct lttng_channel_extended *) |
| 3820 | channel->attr.extended.ptr; |
| 3821 | |
| 3822 | ret = get_ust_runtime_stats( |
| 3823 | session, uchan, &discarded_events, &lost_packets); |
| 3824 | if (ret < 0) { |
| 3825 | lttng_channel_destroy(channel); |
| 3826 | ret_code = LTTNG_ERR_UNK; |
| 3827 | goto end; |
| 3828 | } |
| 3829 | |
| 3830 | extended->discarded_events = discarded_events; |
| 3831 | extended->lost_packets = lost_packets; |
| 3832 | |
| 3833 | ret = lttng_channel_serialize(channel, &payload->buffer); |
| 3834 | if (ret) { |
| 3835 | ERR("Failed to serialize lttng_channel: channel name = '%s'", |
| 3836 | channel->name); |
| 3837 | lttng_channel_destroy(channel); |
| 3838 | ret_code = LTTNG_ERR_UNK; |
| 3839 | goto end; |
| 3840 | } |
| 3841 | |
| 3842 | lttng_channel_destroy(channel); |
| 3843 | i++; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | break; |
| 3848 | } |
| 3849 | default: |
| 3850 | break; |
| 3851 | } |
| 3852 | |
| 3853 | if (i > UINT32_MAX) { |
| 3854 | ERR("Channel count would overflow the channel listing command's reply"); |
| 3855 | ret_code = LTTNG_ERR_OVERFLOW; |
| 3856 | goto end; |
| 3857 | } |
| 3858 | |
| 3859 | /* Update command reply header. */ |
| 3860 | cmd_header.count = (uint32_t) i; |
| 3861 | memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header)); |
| 3862 | ret_code = LTTNG_OK; |
| 3863 | |
| 3864 | end: |
| 3865 | return ret_code; |
| 3866 | } |
| 3867 | |
| 3868 | /* |
| 3869 | * Command LTTNG_LIST_EVENTS processed by the client thread. |
| 3870 | */ |
| 3871 | enum lttng_error_code cmd_list_events(enum lttng_domain_type domain, |
| 3872 | struct ltt_session *session, |
| 3873 | char *channel_name, |
| 3874 | struct lttng_payload *reply_payload) |
| 3875 | { |
| 3876 | int buffer_resize_ret; |
| 3877 | enum lttng_error_code ret_code = LTTNG_OK; |
| 3878 | struct lttcomm_list_command_header reply_command_header = {}; |
| 3879 | size_t reply_command_header_offset; |
| 3880 | unsigned int nb_events = 0; |
| 3881 | |
| 3882 | assert(reply_payload); |
| 3883 | |
| 3884 | /* Reserve space for command reply header. */ |
| 3885 | reply_command_header_offset = reply_payload->buffer.size; |
| 3886 | buffer_resize_ret = lttng_dynamic_buffer_set_size( |
| 3887 | &reply_payload->buffer, |
| 3888 | reply_command_header_offset + sizeof(struct lttcomm_list_command_header)); |
| 3889 | if (buffer_resize_ret) { |
| 3890 | ret_code = LTTNG_ERR_NOMEM; |
| 3891 | goto end; |
| 3892 | } |
| 3893 | |
| 3894 | switch (domain) { |
| 3895 | case LTTNG_DOMAIN_KERNEL: |
| 3896 | if (session->kernel_session != nullptr) { |
| 3897 | ret_code = list_lttng_kernel_events( |
| 3898 | channel_name, session->kernel_session, reply_payload, &nb_events); |
| 3899 | } |
| 3900 | |
| 3901 | break; |
| 3902 | case LTTNG_DOMAIN_UST: |
| 3903 | { |
| 3904 | if (session->ust_session != nullptr) { |
| 3905 | ret_code = |
| 3906 | list_lttng_ust_global_events(channel_name, |
| 3907 | &session->ust_session->domain_global, |
| 3908 | reply_payload, |
| 3909 | &nb_events); |
| 3910 | } |
| 3911 | |
| 3912 | break; |
| 3913 | } |
| 3914 | case LTTNG_DOMAIN_LOG4J: |
| 3915 | case LTTNG_DOMAIN_JUL: |
| 3916 | case LTTNG_DOMAIN_PYTHON: |
| 3917 | if (session->ust_session) { |
| 3918 | struct lttng_ht_iter iter; |
| 3919 | struct agent *agt; |
| 3920 | |
| 3921 | lttng::urcu::read_lock_guard read_lock; |
| 3922 | |
| 3923 | cds_lfht_for_each_entry ( |
| 3924 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3925 | if (agt->domain == domain) { |
| 3926 | ret_code = list_lttng_agent_events( |
| 3927 | agt, reply_payload, &nb_events); |
| 3928 | break; |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | break; |
| 3933 | default: |
| 3934 | ret_code = LTTNG_ERR_UND; |
| 3935 | break; |
| 3936 | } |
| 3937 | |
| 3938 | if (nb_events > UINT32_MAX) { |
| 3939 | ret_code = LTTNG_ERR_OVERFLOW; |
| 3940 | goto end; |
| 3941 | } |
| 3942 | |
| 3943 | /* Update command reply header. */ |
| 3944 | reply_command_header.count = (uint32_t) nb_events; |
| 3945 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 3946 | &reply_command_header, |
| 3947 | sizeof(reply_command_header)); |
| 3948 | |
| 3949 | end: |
| 3950 | return ret_code; |
| 3951 | } |
| 3952 | |
| 3953 | /* |
| 3954 | * Using the session list, filled a lttng_session array to send back to the |
| 3955 | * client for session listing. |
| 3956 | * |
| 3957 | * The session list lock MUST be acquired before calling this function. Use |
| 3958 | * session_lock_list() and session_unlock_list(). |
| 3959 | */ |
| 3960 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
| 3961 | size_t session_count, |
| 3962 | uid_t uid, |
| 3963 | gid_t gid) |
| 3964 | { |
| 3965 | int ret; |
| 3966 | unsigned int i = 0; |
| 3967 | struct ltt_session *session; |
| 3968 | struct ltt_session_list *list = session_get_list(); |
| 3969 | struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]); |
| 3970 | |
| 3971 | DBG("Getting all available session for UID %d GID %d", uid, gid); |
| 3972 | /* |
| 3973 | * Iterate over session list and append data after the control struct in |
| 3974 | * the buffer. |
| 3975 | */ |
| 3976 | cds_list_for_each_entry (session, &list->head, list) { |
| 3977 | if (!session_get(session)) { |
| 3978 | continue; |
| 3979 | } |
| 3980 | /* |
| 3981 | * Only list the sessions the user can control. |
| 3982 | */ |
| 3983 | if (!session_access_ok(session, uid) || session->destroyed) { |
| 3984 | session_put(session); |
| 3985 | continue; |
| 3986 | } |
| 3987 | |
| 3988 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3989 | struct ltt_ust_session *usess = session->ust_session; |
| 3990 | |
| 3991 | if (session->consumer->type == CONSUMER_DST_NET || |
| 3992 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || |
| 3993 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { |
| 3994 | ret = build_network_session_path( |
| 3995 | sessions[i].path, sizeof(sessions[i].path), session); |
| 3996 | } else { |
| 3997 | ret = snprintf(sessions[i].path, |
| 3998 | sizeof(sessions[i].path), |
| 3999 | "%s", |
| 4000 | session->consumer->dst.session_root_path); |
| 4001 | } |
| 4002 | if (ret < 0) { |
| 4003 | PERROR("snprintf session path"); |
| 4004 | session_put(session); |
| 4005 | continue; |
| 4006 | } |
| 4007 | |
| 4008 | strncpy(sessions[i].name, session->name, NAME_MAX); |
| 4009 | sessions[i].name[NAME_MAX - 1] = '\0'; |
| 4010 | sessions[i].enabled = session->active; |
| 4011 | sessions[i].snapshot_mode = session->snapshot_mode; |
| 4012 | sessions[i].live_timer_interval = session->live_timer; |
| 4013 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
| 4014 | extended[i].creation_time.is_set = 1; |
| 4015 | i++; |
| 4016 | session_put(session); |
| 4017 | } |
| 4018 | } |
| 4019 | |
| 4020 | /* |
| 4021 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
| 4022 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
| 4023 | */ |
| 4024 | int cmd_data_pending(struct ltt_session *session) |
| 4025 | { |
| 4026 | int ret; |
| 4027 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 4028 | struct ltt_ust_session *usess = session->ust_session; |
| 4029 | |
| 4030 | LTTNG_ASSERT(session); |
| 4031 | |
| 4032 | DBG("Data pending for session %s", session->name); |
| 4033 | |
| 4034 | /* Session MUST be stopped to ask for data availability. */ |
| 4035 | if (session->active) { |
| 4036 | ret = LTTNG_ERR_SESSION_STARTED; |
| 4037 | goto error; |
| 4038 | } else { |
| 4039 | /* |
| 4040 | * If stopped, just make sure we've started before else the above call |
| 4041 | * will always send that there is data pending. |
| 4042 | * |
| 4043 | * The consumer assumes that when the data pending command is received, |
| 4044 | * the trace has been started before or else no output data is written |
| 4045 | * by the streams which is a condition for data pending. So, this is |
| 4046 | * *VERY* important that we don't ask the consumer before a start |
| 4047 | * trace. |
| 4048 | */ |
| 4049 | if (!session->has_been_started) { |
| 4050 | ret = 0; |
| 4051 | goto error; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | /* A rotation is still pending, we have to wait. */ |
| 4056 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 4057 | DBG("Rotate still pending for session %s", session->name); |
| 4058 | ret = 1; |
| 4059 | goto error; |
| 4060 | } |
| 4061 | |
| 4062 | if (ksess && ksess->consumer) { |
| 4063 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
| 4064 | if (ret == 1) { |
| 4065 | /* Data is still being extracted for the kernel. */ |
| 4066 | goto error; |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | if (usess && usess->consumer) { |
| 4071 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
| 4072 | if (ret == 1) { |
| 4073 | /* Data is still being extracted for the kernel. */ |
| 4074 | goto error; |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | /* Data is ready to be read by a viewer */ |
| 4079 | ret = 0; |
| 4080 | |
| 4081 | error: |
| 4082 | return ret; |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. |
| 4087 | * |
| 4088 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4089 | */ |
| 4090 | int cmd_snapshot_add_output(struct ltt_session *session, |
| 4091 | const struct lttng_snapshot_output *output, |
| 4092 | uint32_t *id) |
| 4093 | { |
| 4094 | int ret; |
| 4095 | struct snapshot_output *new_output; |
| 4096 | |
| 4097 | LTTNG_ASSERT(session); |
| 4098 | LTTNG_ASSERT(output); |
| 4099 | |
| 4100 | DBG("Cmd snapshot add output for session %s", session->name); |
| 4101 | |
| 4102 | /* |
| 4103 | * Can't create an output if the session is not set in no-output mode. |
| 4104 | */ |
| 4105 | if (session->output_traces) { |
| 4106 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4107 | goto error; |
| 4108 | } |
| 4109 | |
| 4110 | if (session->has_non_mmap_channel) { |
| 4111 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; |
| 4112 | goto error; |
| 4113 | } |
| 4114 | |
| 4115 | /* Only one output is allowed until we have the "tee" feature. */ |
| 4116 | if (session->snapshot.nb_output == 1) { |
| 4117 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; |
| 4118 | goto error; |
| 4119 | } |
| 4120 | |
| 4121 | new_output = snapshot_output_alloc(); |
| 4122 | if (!new_output) { |
| 4123 | ret = LTTNG_ERR_NOMEM; |
| 4124 | goto error; |
| 4125 | } |
| 4126 | |
| 4127 | ret = snapshot_output_init(session, |
| 4128 | output->max_size, |
| 4129 | output->name, |
| 4130 | output->ctrl_url, |
| 4131 | output->data_url, |
| 4132 | session->consumer, |
| 4133 | new_output, |
| 4134 | &session->snapshot); |
| 4135 | if (ret < 0) { |
| 4136 | if (ret == -ENOMEM) { |
| 4137 | ret = LTTNG_ERR_NOMEM; |
| 4138 | } else { |
| 4139 | ret = LTTNG_ERR_INVALID; |
| 4140 | } |
| 4141 | goto free_error; |
| 4142 | } |
| 4143 | |
| 4144 | snapshot_add_output(&session->snapshot, new_output); |
| 4145 | if (id) { |
| 4146 | *id = new_output->id; |
| 4147 | } |
| 4148 | |
| 4149 | return LTTNG_OK; |
| 4150 | |
| 4151 | free_error: |
| 4152 | snapshot_output_destroy(new_output); |
| 4153 | error: |
| 4154 | return ret; |
| 4155 | } |
| 4156 | |
| 4157 | /* |
| 4158 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. |
| 4159 | * |
| 4160 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4161 | */ |
| 4162 | int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output) |
| 4163 | { |
| 4164 | int ret; |
| 4165 | struct snapshot_output *sout = nullptr; |
| 4166 | |
| 4167 | LTTNG_ASSERT(session); |
| 4168 | LTTNG_ASSERT(output); |
| 4169 | |
| 4170 | lttng::urcu::read_lock_guard read_lock; |
| 4171 | |
| 4172 | /* |
| 4173 | * Permission denied to create an output if the session is not |
| 4174 | * set in no output mode. |
| 4175 | */ |
| 4176 | if (session->output_traces) { |
| 4177 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4178 | goto error; |
| 4179 | } |
| 4180 | |
| 4181 | if (output->id) { |
| 4182 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", |
| 4183 | output->id, |
| 4184 | session->name); |
| 4185 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); |
| 4186 | } else if (*output->name != '\0') { |
| 4187 | DBG("Cmd snapshot del output name %s for session %s", output->name, session->name); |
| 4188 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); |
| 4189 | } |
| 4190 | if (!sout) { |
| 4191 | ret = LTTNG_ERR_INVALID; |
| 4192 | goto error; |
| 4193 | } |
| 4194 | |
| 4195 | snapshot_delete_output(&session->snapshot, sout); |
| 4196 | snapshot_output_destroy(sout); |
| 4197 | ret = LTTNG_OK; |
| 4198 | |
| 4199 | error: |
| 4200 | return ret; |
| 4201 | } |
| 4202 | |
| 4203 | /* |
| 4204 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. |
| 4205 | * |
| 4206 | * If no output is available, outputs is untouched and 0 is returned. |
| 4207 | * |
| 4208 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. |
| 4209 | */ |
| 4210 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, |
| 4211 | struct lttng_snapshot_output **outputs) |
| 4212 | { |
| 4213 | int ret, idx = 0; |
| 4214 | struct lttng_snapshot_output *list = nullptr; |
| 4215 | struct lttng_ht_iter iter; |
| 4216 | struct snapshot_output *output; |
| 4217 | |
| 4218 | LTTNG_ASSERT(session); |
| 4219 | LTTNG_ASSERT(outputs); |
| 4220 | |
| 4221 | DBG("Cmd snapshot list outputs for session %s", session->name); |
| 4222 | |
| 4223 | /* |
| 4224 | * Permission denied to create an output if the session is not |
| 4225 | * set in no output mode. |
| 4226 | */ |
| 4227 | if (session->output_traces) { |
| 4228 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4229 | goto end; |
| 4230 | } |
| 4231 | |
| 4232 | if (session->snapshot.nb_output == 0) { |
| 4233 | ret = 0; |
| 4234 | goto end; |
| 4235 | } |
| 4236 | |
| 4237 | list = calloc<lttng_snapshot_output>(session->snapshot.nb_output); |
| 4238 | if (!list) { |
| 4239 | ret = -LTTNG_ERR_NOMEM; |
| 4240 | goto end; |
| 4241 | } |
| 4242 | |
| 4243 | /* Copy list from session to the new list object. */ |
| 4244 | { |
| 4245 | lttng::urcu::read_lock_guard read_lock; |
| 4246 | |
| 4247 | cds_lfht_for_each_entry ( |
| 4248 | session->snapshot.output_ht->ht, &iter.iter, output, node.node) { |
| 4249 | LTTNG_ASSERT(output->consumer); |
| 4250 | list[idx].id = output->id; |
| 4251 | list[idx].max_size = output->max_size; |
| 4252 | if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) { |
| 4253 | ret = -LTTNG_ERR_INVALID; |
| 4254 | goto error; |
| 4255 | } |
| 4256 | |
| 4257 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
| 4258 | if (lttng_strncpy(list[idx].ctrl_url, |
| 4259 | output->consumer->dst.session_root_path, |
| 4260 | sizeof(list[idx].ctrl_url))) { |
| 4261 | ret = -LTTNG_ERR_INVALID; |
| 4262 | goto error; |
| 4263 | } |
| 4264 | } else { |
| 4265 | /* Control URI. */ |
| 4266 | ret = uri_to_str_url(&output->consumer->dst.net.control, |
| 4267 | list[idx].ctrl_url, |
| 4268 | sizeof(list[idx].ctrl_url)); |
| 4269 | if (ret < 0) { |
| 4270 | ret = -LTTNG_ERR_NOMEM; |
| 4271 | goto error; |
| 4272 | } |
| 4273 | |
| 4274 | /* Data URI. */ |
| 4275 | ret = uri_to_str_url(&output->consumer->dst.net.data, |
| 4276 | list[idx].data_url, |
| 4277 | sizeof(list[idx].data_url)); |
| 4278 | if (ret < 0) { |
| 4279 | ret = -LTTNG_ERR_NOMEM; |
| 4280 | goto error; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | idx++; |
| 4285 | } |
| 4286 | } |
| 4287 | |
| 4288 | *outputs = list; |
| 4289 | list = nullptr; |
| 4290 | ret = session->snapshot.nb_output; |
| 4291 | error: |
| 4292 | free(list); |
| 4293 | end: |
| 4294 | return ret; |
| 4295 | } |
| 4296 | |
| 4297 | /* |
| 4298 | * Check if we can regenerate the metadata for this session. |
| 4299 | * Only kernel, UST per-uid and non-live sessions are supported. |
| 4300 | * |
| 4301 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. |
| 4302 | */ |
| 4303 | static int check_regenerate_metadata_support(struct ltt_session *session) |
| 4304 | { |
| 4305 | int ret; |
| 4306 | |
| 4307 | LTTNG_ASSERT(session); |
| 4308 | |
| 4309 | if (session->live_timer != 0) { |
| 4310 | ret = LTTNG_ERR_LIVE_SESSION; |
| 4311 | goto end; |
| 4312 | } |
| 4313 | if (!session->active) { |
| 4314 | ret = LTTNG_ERR_SESSION_NOT_STARTED; |
| 4315 | goto end; |
| 4316 | } |
| 4317 | if (session->ust_session) { |
| 4318 | switch (session->ust_session->buffer_type) { |
| 4319 | case LTTNG_BUFFER_PER_UID: |
| 4320 | break; |
| 4321 | case LTTNG_BUFFER_PER_PID: |
| 4322 | ret = LTTNG_ERR_PER_PID_SESSION; |
| 4323 | goto end; |
| 4324 | default: |
| 4325 | abort(); |
| 4326 | ret = LTTNG_ERR_UNK; |
| 4327 | goto end; |
| 4328 | } |
| 4329 | } |
| 4330 | if (session->consumer->type == CONSUMER_DST_NET && |
| 4331 | session->consumer->relay_minor_version < 8) { |
| 4332 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 4333 | goto end; |
| 4334 | } |
| 4335 | ret = 0; |
| 4336 | |
| 4337 | end: |
| 4338 | return ret; |
| 4339 | } |
| 4340 | |
| 4341 | /* |
| 4342 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
| 4343 | * |
| 4344 | * Ask the consumer to truncate the existing metadata file(s) and |
| 4345 | * then regenerate the metadata. Live and per-pid sessions are not |
| 4346 | * supported and return an error. |
| 4347 | * |
| 4348 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4349 | */ |
| 4350 | int cmd_regenerate_metadata(struct ltt_session *session) |
| 4351 | { |
| 4352 | int ret; |
| 4353 | |
| 4354 | LTTNG_ASSERT(session); |
| 4355 | |
| 4356 | ret = check_regenerate_metadata_support(session); |
| 4357 | if (ret) { |
| 4358 | goto end; |
| 4359 | } |
| 4360 | |
| 4361 | if (session->kernel_session) { |
| 4362 | ret = kernctl_session_regenerate_metadata(session->kernel_session->fd); |
| 4363 | if (ret < 0) { |
| 4364 | ERR("Failed to regenerate the kernel metadata"); |
| 4365 | goto end; |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | if (session->ust_session) { |
| 4370 | ret = trace_ust_regenerate_metadata(session->ust_session); |
| 4371 | if (ret < 0) { |
| 4372 | ERR("Failed to regenerate the UST metadata"); |
| 4373 | goto end; |
| 4374 | } |
| 4375 | } |
| 4376 | DBG("Cmd metadata regenerate for session %s", session->name); |
| 4377 | ret = LTTNG_OK; |
| 4378 | |
| 4379 | end: |
| 4380 | return ret; |
| 4381 | } |
| 4382 | |
| 4383 | /* |
| 4384 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. |
| 4385 | * |
| 4386 | * Ask the tracer to regenerate a new statedump. |
| 4387 | * |
| 4388 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4389 | */ |
| 4390 | int cmd_regenerate_statedump(struct ltt_session *session) |
| 4391 | { |
| 4392 | int ret; |
| 4393 | |
| 4394 | LTTNG_ASSERT(session); |
| 4395 | |
| 4396 | if (!session->active) { |
| 4397 | ret = LTTNG_ERR_SESSION_NOT_STARTED; |
| 4398 | goto end; |
| 4399 | } |
| 4400 | |
| 4401 | if (session->kernel_session) { |
| 4402 | ret = kernctl_session_regenerate_statedump(session->kernel_session->fd); |
| 4403 | /* |
| 4404 | * Currently, the statedump in kernel can only fail if out |
| 4405 | * of memory. |
| 4406 | */ |
| 4407 | if (ret < 0) { |
| 4408 | if (ret == -ENOMEM) { |
| 4409 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; |
| 4410 | } else { |
| 4411 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; |
| 4412 | } |
| 4413 | ERR("Failed to regenerate the kernel statedump"); |
| 4414 | goto end; |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | if (session->ust_session) { |
| 4419 | ret = ust_app_regenerate_statedump_all(session->ust_session); |
| 4420 | /* |
| 4421 | * Currently, the statedump in UST always returns 0. |
| 4422 | */ |
| 4423 | if (ret < 0) { |
| 4424 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; |
| 4425 | ERR("Failed to regenerate the UST statedump"); |
| 4426 | goto end; |
| 4427 | } |
| 4428 | } |
| 4429 | DBG("Cmd regenerate statedump for session %s", session->name); |
| 4430 | ret = LTTNG_OK; |
| 4431 | |
| 4432 | end: |
| 4433 | return ret; |
| 4434 | } |
| 4435 | |
| 4436 | static enum lttng_error_code |
| 4437 | synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread, |
| 4438 | struct lttng_trigger *trigger, |
| 4439 | const struct lttng_credentials *cmd_creds) |
| 4440 | { |
| 4441 | enum lttng_error_code ret_code; |
| 4442 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
| 4443 | const char *trigger_name; |
| 4444 | uid_t trigger_owner; |
| 4445 | enum lttng_trigger_status trigger_status; |
| 4446 | const enum lttng_domain_type trigger_domain = |
| 4447 | lttng_trigger_get_underlying_domain_type_restriction(trigger); |
| 4448 | |
| 4449 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4450 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4451 | |
| 4452 | LTTNG_ASSERT(condition); |
| 4453 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 4454 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 4455 | |
| 4456 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4457 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4458 | |
| 4459 | session_lock_list(); |
| 4460 | switch (trigger_domain) { |
| 4461 | case LTTNG_DOMAIN_KERNEL: |
| 4462 | { |
| 4463 | ret_code = kernel_register_event_notifier(trigger, cmd_creds); |
| 4464 | if (ret_code != LTTNG_OK) { |
| 4465 | enum lttng_error_code notif_thread_unregister_ret; |
| 4466 | |
| 4467 | notif_thread_unregister_ret = |
| 4468 | notification_thread_command_unregister_trigger(notification_thread, |
| 4469 | trigger); |
| 4470 | |
| 4471 | if (notif_thread_unregister_ret != LTTNG_OK) { |
| 4472 | /* Return the original error code. */ |
| 4473 | ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4474 | trigger_name, |
| 4475 | (int) trigger_owner, |
| 4476 | ret_code); |
| 4477 | } |
| 4478 | |
| 4479 | goto end_unlock_session_list; |
| 4480 | } |
| 4481 | break; |
| 4482 | } |
| 4483 | case LTTNG_DOMAIN_UST: |
| 4484 | ust_app_global_update_all_event_notifier_rules(); |
| 4485 | break; |
| 4486 | case LTTNG_DOMAIN_JUL: |
| 4487 | case LTTNG_DOMAIN_LOG4J: |
| 4488 | case LTTNG_DOMAIN_PYTHON: |
| 4489 | { |
| 4490 | /* Agent domains. */ |
| 4491 | struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain); |
| 4492 | |
| 4493 | if (!agt) { |
| 4494 | agt = agent_create(trigger_domain); |
| 4495 | if (!agt) { |
| 4496 | ret_code = LTTNG_ERR_NOMEM; |
| 4497 | goto end_unlock_session_list; |
| 4498 | } |
| 4499 | |
| 4500 | agent_add(agt, the_trigger_agents_ht_by_domain); |
| 4501 | } |
| 4502 | |
| 4503 | ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt); |
| 4504 | if (ret_code != LTTNG_OK) { |
| 4505 | goto end_unlock_session_list; |
| 4506 | } |
| 4507 | |
| 4508 | break; |
| 4509 | } |
| 4510 | case LTTNG_DOMAIN_NONE: |
| 4511 | default: |
| 4512 | abort(); |
| 4513 | } |
| 4514 | |
| 4515 | ret_code = LTTNG_OK; |
| 4516 | end_unlock_session_list: |
| 4517 | session_unlock_list(); |
| 4518 | return ret_code; |
| 4519 | } |
| 4520 | |
| 4521 | enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds, |
| 4522 | struct lttng_trigger *trigger, |
| 4523 | bool is_trigger_anonymous, |
| 4524 | struct notification_thread_handle *notification_thread, |
| 4525 | struct lttng_trigger **return_trigger) |
| 4526 | { |
| 4527 | enum lttng_error_code ret_code; |
| 4528 | const char *trigger_name; |
| 4529 | uid_t trigger_owner; |
| 4530 | enum lttng_trigger_status trigger_status; |
| 4531 | |
| 4532 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4533 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4534 | |
| 4535 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4536 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4537 | |
| 4538 | DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4539 | trigger_name, |
| 4540 | (int) trigger_owner, |
| 4541 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4542 | |
| 4543 | /* |
| 4544 | * Validate the trigger credentials against the command credentials. |
| 4545 | * Only the root user can register a trigger with non-matching |
| 4546 | * credentials. |
| 4547 | */ |
| 4548 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) { |
| 4549 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4550 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4551 | trigger_name, |
| 4552 | (int) trigger_owner, |
| 4553 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4554 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4555 | goto end; |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | /* |
| 4560 | * The bytecode generation also serves as a validation step for the |
| 4561 | * bytecode expressions. |
| 4562 | */ |
| 4563 | ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds); |
| 4564 | if (ret_code != LTTNG_OK) { |
| 4565 | ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4566 | trigger_name, |
| 4567 | (int) trigger_owner, |
| 4568 | ret_code); |
| 4569 | goto end; |
| 4570 | } |
| 4571 | |
| 4572 | /* |
| 4573 | * A reference to the trigger is acquired by the notification thread. |
| 4574 | * It is safe to return the same trigger to the caller since it the |
| 4575 | * other user holds a reference. |
| 4576 | * |
| 4577 | * The trigger is modified during the execution of the |
| 4578 | * "register trigger" command. However, by the time the command returns, |
| 4579 | * it is safe to use without any locking as its properties are |
| 4580 | * immutable. |
| 4581 | */ |
| 4582 | ret_code = notification_thread_command_register_trigger( |
| 4583 | notification_thread, trigger, is_trigger_anonymous); |
| 4584 | if (ret_code != LTTNG_OK) { |
| 4585 | DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4586 | trigger_name, |
| 4587 | (int) trigger_owner, |
| 4588 | ret_code); |
| 4589 | goto end; |
| 4590 | } |
| 4591 | |
| 4592 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4593 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4594 | |
| 4595 | /* |
| 4596 | * Synchronize tracers if the trigger adds an event notifier. |
| 4597 | */ |
| 4598 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
| 4599 | ret_code = synchronize_tracer_notifier_register( |
| 4600 | notification_thread, trigger, cmd_creds); |
| 4601 | if (ret_code != LTTNG_OK) { |
| 4602 | ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code)); |
| 4603 | goto end; |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | /* |
| 4608 | * Return an updated trigger to the client. |
| 4609 | * |
| 4610 | * Since a modified version of the same trigger is returned, acquire a |
| 4611 | * reference to the trigger so the caller doesn't have to care if those |
| 4612 | * are distinct instances or not. |
| 4613 | */ |
| 4614 | if (ret_code == LTTNG_OK) { |
| 4615 | lttng_trigger_get(trigger); |
| 4616 | *return_trigger = trigger; |
| 4617 | /* Ownership of trigger was transferred to caller. */ |
| 4618 | trigger = nullptr; |
| 4619 | } |
| 4620 | end: |
| 4621 | return ret_code; |
| 4622 | } |
| 4623 | |
| 4624 | static enum lttng_error_code |
| 4625 | synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger) |
| 4626 | { |
| 4627 | enum lttng_error_code ret_code; |
| 4628 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
| 4629 | const enum lttng_domain_type trigger_domain = |
| 4630 | lttng_trigger_get_underlying_domain_type_restriction(trigger); |
| 4631 | |
| 4632 | LTTNG_ASSERT(condition); |
| 4633 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 4634 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 4635 | |
| 4636 | session_lock_list(); |
| 4637 | switch (trigger_domain) { |
| 4638 | case LTTNG_DOMAIN_KERNEL: |
| 4639 | ret_code = kernel_unregister_event_notifier(trigger); |
| 4640 | if (ret_code != LTTNG_OK) { |
| 4641 | goto end_unlock_session_list; |
| 4642 | } |
| 4643 | |
| 4644 | break; |
| 4645 | case LTTNG_DOMAIN_UST: |
| 4646 | ust_app_global_update_all_event_notifier_rules(); |
| 4647 | break; |
| 4648 | case LTTNG_DOMAIN_JUL: |
| 4649 | case LTTNG_DOMAIN_LOG4J: |
| 4650 | case LTTNG_DOMAIN_PYTHON: |
| 4651 | { |
| 4652 | /* Agent domains. */ |
| 4653 | struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain); |
| 4654 | |
| 4655 | /* |
| 4656 | * This trigger was never registered in the first place. Calling |
| 4657 | * this function under those circumstances is an internal error. |
| 4658 | */ |
| 4659 | LTTNG_ASSERT(agt); |
| 4660 | ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt); |
| 4661 | if (ret_code != LTTNG_OK) { |
| 4662 | goto end_unlock_session_list; |
| 4663 | } |
| 4664 | |
| 4665 | break; |
| 4666 | } |
| 4667 | case LTTNG_DOMAIN_NONE: |
| 4668 | default: |
| 4669 | abort(); |
| 4670 | } |
| 4671 | |
| 4672 | ret_code = LTTNG_OK; |
| 4673 | |
| 4674 | end_unlock_session_list: |
| 4675 | session_unlock_list(); |
| 4676 | return ret_code; |
| 4677 | } |
| 4678 | |
| 4679 | enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds, |
| 4680 | const struct lttng_trigger *trigger, |
| 4681 | struct notification_thread_handle *notification_thread) |
| 4682 | { |
| 4683 | enum lttng_error_code ret_code; |
| 4684 | const char *trigger_name; |
| 4685 | uid_t trigger_owner; |
| 4686 | enum lttng_trigger_status trigger_status; |
| 4687 | struct lttng_trigger *sessiond_trigger = nullptr; |
| 4688 | |
| 4689 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4690 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4691 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4692 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4693 | |
| 4694 | DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4695 | trigger_name, |
| 4696 | (int) trigger_owner, |
| 4697 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4698 | |
| 4699 | /* |
| 4700 | * Validate the trigger credentials against the command credentials. |
| 4701 | * Only the root user can unregister a trigger with non-matching |
| 4702 | * credentials. |
| 4703 | */ |
| 4704 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) { |
| 4705 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4706 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4707 | trigger_name, |
| 4708 | (int) trigger_owner, |
| 4709 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4710 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4711 | goto end; |
| 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | /* Fetch the sessiond side trigger object. */ |
| 4716 | ret_code = notification_thread_command_get_trigger( |
| 4717 | notification_thread, trigger, &sessiond_trigger); |
| 4718 | if (ret_code != LTTNG_OK) { |
| 4719 | DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4720 | trigger_name, |
| 4721 | (int) trigger_owner, |
| 4722 | ret_code); |
| 4723 | goto end; |
| 4724 | } |
| 4725 | |
| 4726 | LTTNG_ASSERT(sessiond_trigger); |
| 4727 | |
| 4728 | /* |
| 4729 | * From this point on, no matter what, consider the trigger |
| 4730 | * unregistered. |
| 4731 | * |
| 4732 | * We set the unregistered state of the sessiond side trigger object in |
| 4733 | * the client thread since we want to minimize the possibility of the |
| 4734 | * notification thread being stalled due to a long execution of an |
| 4735 | * action that required the trigger lock. |
| 4736 | */ |
| 4737 | lttng_trigger_set_as_unregistered(sessiond_trigger); |
| 4738 | |
| 4739 | ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger); |
| 4740 | if (ret_code != LTTNG_OK) { |
| 4741 | DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4742 | trigger_name, |
| 4743 | (int) trigger_owner, |
| 4744 | ret_code); |
| 4745 | goto end; |
| 4746 | } |
| 4747 | |
| 4748 | /* |
| 4749 | * Synchronize tracers if the trigger removes an event notifier. |
| 4750 | * Do this even if the trigger unregistration failed to at least stop |
| 4751 | * the tracers from producing notifications associated with this |
| 4752 | * event notifier. |
| 4753 | */ |
| 4754 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
| 4755 | ret_code = synchronize_tracer_notifier_unregister(trigger); |
| 4756 | if (ret_code != LTTNG_OK) { |
| 4757 | ERR("Error unregistering trigger to tracer."); |
| 4758 | goto end; |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | end: |
| 4763 | lttng_trigger_put(sessiond_trigger); |
| 4764 | return ret_code; |
| 4765 | } |
| 4766 | |
| 4767 | enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx, |
| 4768 | struct notification_thread_handle *notification_thread, |
| 4769 | struct lttng_triggers **return_triggers) |
| 4770 | { |
| 4771 | int ret; |
| 4772 | enum lttng_error_code ret_code; |
| 4773 | struct lttng_triggers *triggers = nullptr; |
| 4774 | |
| 4775 | /* Get the set of triggers from the notification thread. */ |
| 4776 | ret_code = notification_thread_command_list_triggers( |
| 4777 | notification_thread, cmd_ctx->creds.uid, &triggers); |
| 4778 | if (ret_code != LTTNG_OK) { |
| 4779 | goto end; |
| 4780 | } |
| 4781 | |
| 4782 | ret = lttng_triggers_remove_hidden_triggers(triggers); |
| 4783 | if (ret) { |
| 4784 | ret_code = LTTNG_ERR_UNK; |
| 4785 | goto end; |
| 4786 | } |
| 4787 | |
| 4788 | *return_triggers = triggers; |
| 4789 | triggers = nullptr; |
| 4790 | ret_code = LTTNG_OK; |
| 4791 | end: |
| 4792 | lttng_triggers_destroy(triggers); |
| 4793 | return ret_code; |
| 4794 | } |
| 4795 | |
| 4796 | enum lttng_error_code |
| 4797 | cmd_execute_error_query(const struct lttng_credentials *cmd_creds, |
| 4798 | const struct lttng_error_query *query, |
| 4799 | struct lttng_error_query_results **_results, |
| 4800 | struct notification_thread_handle *notification_thread) |
| 4801 | { |
| 4802 | enum lttng_error_code ret_code; |
| 4803 | const struct lttng_trigger *query_target_trigger; |
| 4804 | const struct lttng_action *query_target_action = nullptr; |
| 4805 | struct lttng_trigger *matching_trigger = nullptr; |
| 4806 | const char *trigger_name; |
| 4807 | uid_t trigger_owner; |
| 4808 | enum lttng_trigger_status trigger_status; |
| 4809 | struct lttng_error_query_results *results = nullptr; |
| 4810 | |
| 4811 | switch (lttng_error_query_get_target_type(query)) { |
| 4812 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: |
| 4813 | query_target_trigger = lttng_error_query_trigger_borrow_target(query); |
| 4814 | break; |
| 4815 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
| 4816 | query_target_trigger = lttng_error_query_condition_borrow_target(query); |
| 4817 | break; |
| 4818 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
| 4819 | query_target_trigger = lttng_error_query_action_borrow_trigger_target(query); |
| 4820 | break; |
| 4821 | default: |
| 4822 | abort(); |
| 4823 | } |
| 4824 | |
| 4825 | LTTNG_ASSERT(query_target_trigger); |
| 4826 | |
| 4827 | ret_code = notification_thread_command_get_trigger( |
| 4828 | notification_thread, query_target_trigger, &matching_trigger); |
| 4829 | if (ret_code != LTTNG_OK) { |
| 4830 | goto end; |
| 4831 | } |
| 4832 | |
| 4833 | /* No longer needed. */ |
| 4834 | query_target_trigger = nullptr; |
| 4835 | |
| 4836 | if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) { |
| 4837 | /* Get the sessiond-side version of the target action. */ |
| 4838 | query_target_action = |
| 4839 | lttng_error_query_action_borrow_action_target(query, matching_trigger); |
| 4840 | } |
| 4841 | |
| 4842 | trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name); |
| 4843 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4844 | trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner); |
| 4845 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4846 | |
| 4847 | results = lttng_error_query_results_create(); |
| 4848 | if (!results) { |
| 4849 | ret_code = LTTNG_ERR_NOMEM; |
| 4850 | goto end; |
| 4851 | } |
| 4852 | |
| 4853 | DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4854 | trigger_name, |
| 4855 | (int) trigger_owner, |
| 4856 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4857 | |
| 4858 | /* |
| 4859 | * Validate the trigger credentials against the command credentials. |
| 4860 | * Only the root user can target a trigger with non-matching |
| 4861 | * credentials. |
| 4862 | */ |
| 4863 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger), |
| 4864 | cmd_creds)) { |
| 4865 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4866 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4867 | trigger_name, |
| 4868 | (int) trigger_owner, |
| 4869 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4870 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4871 | goto end; |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | switch (lttng_error_query_get_target_type(query)) { |
| 4876 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: |
| 4877 | trigger_status = lttng_trigger_add_error_results(matching_trigger, results); |
| 4878 | |
| 4879 | switch (trigger_status) { |
| 4880 | case LTTNG_TRIGGER_STATUS_OK: |
| 4881 | break; |
| 4882 | default: |
| 4883 | ret_code = LTTNG_ERR_UNK; |
| 4884 | goto end; |
| 4885 | } |
| 4886 | |
| 4887 | break; |
| 4888 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
| 4889 | { |
| 4890 | trigger_status = |
| 4891 | lttng_trigger_condition_add_error_results(matching_trigger, results); |
| 4892 | |
| 4893 | switch (trigger_status) { |
| 4894 | case LTTNG_TRIGGER_STATUS_OK: |
| 4895 | break; |
| 4896 | default: |
| 4897 | ret_code = LTTNG_ERR_UNK; |
| 4898 | goto end; |
| 4899 | } |
| 4900 | |
| 4901 | break; |
| 4902 | } |
| 4903 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
| 4904 | { |
| 4905 | const enum lttng_action_status action_status = |
| 4906 | lttng_action_add_error_query_results(query_target_action, results); |
| 4907 | |
| 4908 | switch (action_status) { |
| 4909 | case LTTNG_ACTION_STATUS_OK: |
| 4910 | break; |
| 4911 | default: |
| 4912 | ret_code = LTTNG_ERR_UNK; |
| 4913 | goto end; |
| 4914 | } |
| 4915 | |
| 4916 | break; |
| 4917 | } |
| 4918 | default: |
| 4919 | abort(); |
| 4920 | break; |
| 4921 | } |
| 4922 | |
| 4923 | *_results = results; |
| 4924 | results = nullptr; |
| 4925 | ret_code = LTTNG_OK; |
| 4926 | end: |
| 4927 | lttng_trigger_put(matching_trigger); |
| 4928 | lttng_error_query_results_destroy(results); |
| 4929 | return ret_code; |
| 4930 | } |
| 4931 | |
| 4932 | /* |
| 4933 | * Send relayd sockets from snapshot output to consumer. Ignore request if the |
| 4934 | * snapshot output is *not* set with a remote destination. |
| 4935 | * |
| 4936 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
| 4937 | */ |
| 4938 | static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output, |
| 4939 | const struct ltt_session *session) |
| 4940 | { |
| 4941 | enum lttng_error_code status = LTTNG_OK; |
| 4942 | struct lttng_ht_iter iter; |
| 4943 | struct consumer_socket *socket; |
| 4944 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
| 4945 | const char *base_path; |
| 4946 | |
| 4947 | LTTNG_ASSERT(output); |
| 4948 | LTTNG_ASSERT(session); |
| 4949 | |
| 4950 | DBG2("Set relayd object from snapshot output"); |
| 4951 | |
| 4952 | if (session->current_trace_chunk) { |
| 4953 | enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id( |
| 4954 | session->current_trace_chunk, ¤t_chunk_id.value); |
| 4955 | |
| 4956 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4957 | current_chunk_id.is_set = true; |
| 4958 | } else { |
| 4959 | ERR("Failed to get current trace chunk id"); |
| 4960 | status = LTTNG_ERR_UNK; |
| 4961 | goto error; |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | /* Ignore if snapshot consumer output is not network. */ |
| 4966 | if (output->type != CONSUMER_DST_NET) { |
| 4967 | goto error; |
| 4968 | } |
| 4969 | |
| 4970 | /* |
| 4971 | * The snapshot record URI base path overrides the session |
| 4972 | * base path. |
| 4973 | */ |
| 4974 | if (output->dst.net.control.subdir[0] != '\0') { |
| 4975 | base_path = output->dst.net.control.subdir; |
| 4976 | } else { |
| 4977 | base_path = session->base_path; |
| 4978 | } |
| 4979 | |
| 4980 | /* |
| 4981 | * For each consumer socket, create and send the relayd object of the |
| 4982 | * snapshot output. |
| 4983 | */ |
| 4984 | { |
| 4985 | lttng::urcu::read_lock_guard read_lock; |
| 4986 | |
| 4987 | cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) { |
| 4988 | pthread_mutex_lock(socket->lock); |
| 4989 | status = send_consumer_relayd_sockets( |
| 4990 | session->id, |
| 4991 | output, |
| 4992 | socket, |
| 4993 | session->name, |
| 4994 | session->hostname, |
| 4995 | base_path, |
| 4996 | session->live_timer, |
| 4997 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 4998 | session->creation_time, |
| 4999 | session->name_contains_creation_time); |
| 5000 | pthread_mutex_unlock(socket->lock); |
| 5001 | if (status != LTTNG_OK) { |
| 5002 | goto error; |
| 5003 | } |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | error: |
| 5008 | return status; |
| 5009 | } |
| 5010 | |
| 5011 | /* |
| 5012 | * Record a kernel snapshot. |
| 5013 | * |
| 5014 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
| 5015 | */ |
| 5016 | static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess, |
| 5017 | const struct consumer_output *output, |
| 5018 | const struct ltt_session *session, |
| 5019 | uint64_t nb_packets_per_stream) |
| 5020 | { |
| 5021 | enum lttng_error_code status; |
| 5022 | |
| 5023 | LTTNG_ASSERT(ksess); |
| 5024 | LTTNG_ASSERT(output); |
| 5025 | LTTNG_ASSERT(session); |
| 5026 | |
| 5027 | status = kernel_snapshot_record(ksess, output, nb_packets_per_stream); |
| 5028 | return status; |
| 5029 | } |
| 5030 | |
| 5031 | /* |
| 5032 | * Record a UST snapshot. |
| 5033 | * |
| 5034 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
| 5035 | */ |
| 5036 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
| 5037 | const struct consumer_output *output, |
| 5038 | const struct ltt_session *session, |
| 5039 | uint64_t nb_packets_per_stream) |
| 5040 | { |
| 5041 | enum lttng_error_code status; |
| 5042 | |
| 5043 | LTTNG_ASSERT(usess); |
| 5044 | LTTNG_ASSERT(output); |
| 5045 | LTTNG_ASSERT(session); |
| 5046 | |
| 5047 | status = ust_app_snapshot_record(usess, output, nb_packets_per_stream); |
| 5048 | return status; |
| 5049 | } |
| 5050 | |
| 5051 | static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session, |
| 5052 | uint64_t cur_nr_packets) |
| 5053 | { |
| 5054 | uint64_t tot_size = 0; |
| 5055 | |
| 5056 | if (session->kernel_session) { |
| 5057 | struct ltt_kernel_channel *chan; |
| 5058 | const struct ltt_kernel_session *ksess = session->kernel_session; |
| 5059 | |
| 5060 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 5061 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
| 5062 | /* |
| 5063 | * Don't take channel into account if we |
| 5064 | * already grab all its packets. |
| 5065 | */ |
| 5066 | continue; |
| 5067 | } |
| 5068 | tot_size += chan->channel->attr.subbuf_size * chan->stream_count; |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | if (session->ust_session) { |
| 5073 | const struct ltt_ust_session *usess = session->ust_session; |
| 5074 | |
| 5075 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets); |
| 5076 | } |
| 5077 | |
| 5078 | return tot_size; |
| 5079 | } |
| 5080 | |
| 5081 | /* |
| 5082 | * Calculate the number of packets we can grab from each stream that |
| 5083 | * fits within the overall snapshot max size. |
| 5084 | * |
| 5085 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is |
| 5086 | * the number of packets per stream. |
| 5087 | * |
| 5088 | * TODO: this approach is not perfect: we consider the worse case |
| 5089 | * (packet filling the sub-buffers) as an upper bound, but we could do |
| 5090 | * better if we do this calculation while we actually grab the packet |
| 5091 | * content: we would know how much padding we don't actually store into |
| 5092 | * the file. |
| 5093 | * |
| 5094 | * This algorithm is currently bounded by the number of packets per |
| 5095 | * stream. |
| 5096 | * |
| 5097 | * Since we call this algorithm before actually grabbing the data, it's |
| 5098 | * an approximation: for instance, applications could appear/disappear |
| 5099 | * in between this call and actually grabbing data. |
| 5100 | */ |
| 5101 | static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
| 5102 | uint64_t max_size) |
| 5103 | { |
| 5104 | int64_t size_left; |
| 5105 | uint64_t cur_nb_packets = 0; |
| 5106 | |
| 5107 | if (!max_size) { |
| 5108 | return 0; /* Infinite */ |
| 5109 | } |
| 5110 | |
| 5111 | size_left = max_size; |
| 5112 | for (;;) { |
| 5113 | uint64_t one_more_packet_tot_size; |
| 5114 | |
| 5115 | one_more_packet_tot_size = |
| 5116 | get_session_size_one_more_packet_per_stream(session, cur_nb_packets); |
| 5117 | if (!one_more_packet_tot_size) { |
| 5118 | /* We are already grabbing all packets. */ |
| 5119 | break; |
| 5120 | } |
| 5121 | size_left -= one_more_packet_tot_size; |
| 5122 | if (size_left < 0) { |
| 5123 | break; |
| 5124 | } |
| 5125 | cur_nb_packets++; |
| 5126 | } |
| 5127 | if (!cur_nb_packets && size_left != max_size) { |
| 5128 | /* Not enough room to grab one packet of each stream, error. */ |
| 5129 | return -1; |
| 5130 | } |
| 5131 | return cur_nb_packets; |
| 5132 | } |
| 5133 | |
| 5134 | static enum lttng_error_code snapshot_record(struct ltt_session *session, |
| 5135 | const struct snapshot_output *snapshot_output) |
| 5136 | { |
| 5137 | int64_t nb_packets_per_stream; |
| 5138 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
| 5139 | int ret; |
| 5140 | enum lttng_error_code ret_code = LTTNG_OK; |
| 5141 | struct lttng_trace_chunk *snapshot_trace_chunk; |
| 5142 | struct consumer_output *original_ust_consumer_output = nullptr; |
| 5143 | struct consumer_output *original_kernel_consumer_output = nullptr; |
| 5144 | struct consumer_output *snapshot_ust_consumer_output = nullptr; |
| 5145 | struct consumer_output *snapshot_kernel_consumer_output = nullptr; |
| 5146 | |
| 5147 | ret = snprintf(snapshot_chunk_name, |
| 5148 | sizeof(snapshot_chunk_name), |
| 5149 | "%s-%s-%" PRIu64, |
| 5150 | snapshot_output->name, |
| 5151 | snapshot_output->datetime, |
| 5152 | snapshot_output->nb_snapshot); |
| 5153 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
| 5154 | ERR("Failed to format snapshot name"); |
| 5155 | ret_code = LTTNG_ERR_INVALID; |
| 5156 | goto error; |
| 5157 | } |
| 5158 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", |
| 5159 | snapshot_output->name, |
| 5160 | session->name, |
| 5161 | snapshot_chunk_name); |
| 5162 | if (!session->kernel_session && !session->ust_session) { |
| 5163 | ERR("Failed to record snapshot as no channels exist"); |
| 5164 | ret_code = LTTNG_ERR_NO_CHANNEL; |
| 5165 | goto error; |
| 5166 | } |
| 5167 | |
| 5168 | if (session->kernel_session) { |
| 5169 | original_kernel_consumer_output = session->kernel_session->consumer; |
| 5170 | snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer); |
| 5171 | strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name); |
| 5172 | |
| 5173 | /* Copy the original domain subdir. */ |
| 5174 | strcpy(snapshot_kernel_consumer_output->domain_subdir, |
| 5175 | original_kernel_consumer_output->domain_subdir); |
| 5176 | |
| 5177 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, |
| 5178 | original_kernel_consumer_output); |
| 5179 | if (ret < 0) { |
| 5180 | ERR("Failed to copy consumer sockets from snapshot output configuration"); |
| 5181 | ret_code = LTTNG_ERR_NOMEM; |
| 5182 | goto error; |
| 5183 | } |
| 5184 | ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session); |
| 5185 | if (ret_code != LTTNG_OK) { |
| 5186 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); |
| 5187 | goto error; |
| 5188 | } |
| 5189 | session->kernel_session->consumer = snapshot_kernel_consumer_output; |
| 5190 | } |
| 5191 | if (session->ust_session) { |
| 5192 | original_ust_consumer_output = session->ust_session->consumer; |
| 5193 | snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer); |
| 5194 | strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name); |
| 5195 | |
| 5196 | /* Copy the original domain subdir. */ |
| 5197 | strcpy(snapshot_ust_consumer_output->domain_subdir, |
| 5198 | original_ust_consumer_output->domain_subdir); |
| 5199 | |
| 5200 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, |
| 5201 | original_ust_consumer_output); |
| 5202 | if (ret < 0) { |
| 5203 | ERR("Failed to copy consumer sockets from snapshot output configuration"); |
| 5204 | ret_code = LTTNG_ERR_NOMEM; |
| 5205 | goto error; |
| 5206 | } |
| 5207 | ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session); |
| 5208 | if (ret_code != LTTNG_OK) { |
| 5209 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); |
| 5210 | goto error; |
| 5211 | } |
| 5212 | session->ust_session->consumer = snapshot_ust_consumer_output; |
| 5213 | } |
| 5214 | |
| 5215 | snapshot_trace_chunk = session_create_new_trace_chunk( |
| 5216 | session, |
| 5217 | snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output, |
| 5218 | consumer_output_get_base_path(snapshot_output->consumer), |
| 5219 | snapshot_chunk_name); |
| 5220 | if (!snapshot_trace_chunk) { |
| 5221 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
| 5222 | session->name); |
| 5223 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; |
| 5224 | goto error; |
| 5225 | } |
| 5226 | LTTNG_ASSERT(!session->current_trace_chunk); |
| 5227 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr); |
| 5228 | lttng_trace_chunk_put(snapshot_trace_chunk); |
| 5229 | snapshot_trace_chunk = nullptr; |
| 5230 | if (ret) { |
| 5231 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
| 5232 | session->name); |
| 5233 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5234 | goto error; |
| 5235 | } |
| 5236 | |
| 5237 | nb_packets_per_stream = |
| 5238 | get_session_nb_packets_per_stream(session, snapshot_output->max_size); |
| 5239 | if (nb_packets_per_stream < 0) { |
| 5240 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
| 5241 | goto error_close_trace_chunk; |
| 5242 | } |
| 5243 | |
| 5244 | if (session->kernel_session) { |
| 5245 | ret_code = record_kernel_snapshot(session->kernel_session, |
| 5246 | snapshot_kernel_consumer_output, |
| 5247 | session, |
| 5248 | nb_packets_per_stream); |
| 5249 | if (ret_code != LTTNG_OK) { |
| 5250 | goto error_close_trace_chunk; |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | if (session->ust_session) { |
| 5255 | ret_code = record_ust_snapshot(session->ust_session, |
| 5256 | snapshot_ust_consumer_output, |
| 5257 | session, |
| 5258 | nb_packets_per_stream); |
| 5259 | if (ret_code != LTTNG_OK) { |
| 5260 | goto error_close_trace_chunk; |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | error_close_trace_chunk: |
| 5265 | if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) { |
| 5266 | ERR("Failed to release the current trace chunk of session \"%s\"", session->name); |
| 5267 | ret_code = LTTNG_ERR_UNK; |
| 5268 | } |
| 5269 | |
| 5270 | if (session_close_trace_chunk(session, |
| 5271 | snapshot_trace_chunk, |
| 5272 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, |
| 5273 | nullptr)) { |
| 5274 | /* |
| 5275 | * Don't goto end; make sure the chunk is closed for the session |
| 5276 | * to allow future snapshots. |
| 5277 | */ |
| 5278 | ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name); |
| 5279 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5280 | } |
| 5281 | |
| 5282 | lttng_trace_chunk_put(snapshot_trace_chunk); |
| 5283 | snapshot_trace_chunk = nullptr; |
| 5284 | error: |
| 5285 | if (original_ust_consumer_output) { |
| 5286 | session->ust_session->consumer = original_ust_consumer_output; |
| 5287 | } |
| 5288 | if (original_kernel_consumer_output) { |
| 5289 | session->kernel_session->consumer = original_kernel_consumer_output; |
| 5290 | } |
| 5291 | consumer_output_put(snapshot_ust_consumer_output); |
| 5292 | consumer_output_put(snapshot_kernel_consumer_output); |
| 5293 | return ret_code; |
| 5294 | } |
| 5295 | |
| 5296 | /* |
| 5297 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. |
| 5298 | * |
| 5299 | * The wait parameter is ignored so this call always wait for the snapshot to |
| 5300 | * complete before returning. |
| 5301 | * |
| 5302 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 5303 | */ |
| 5304 | int cmd_snapshot_record(struct ltt_session *session, |
| 5305 | const struct lttng_snapshot_output *output, |
| 5306 | int wait __attribute__((unused))) |
| 5307 | { |
| 5308 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5309 | int ret; |
| 5310 | unsigned int snapshot_success = 0; |
| 5311 | char datetime[16]; |
| 5312 | struct snapshot_output *tmp_output = nullptr; |
| 5313 | |
| 5314 | LTTNG_ASSERT(session); |
| 5315 | LTTNG_ASSERT(output); |
| 5316 | |
| 5317 | DBG("Cmd snapshot record for session %s", session->name); |
| 5318 | |
| 5319 | /* Get the datetime for the snapshot output directory. */ |
| 5320 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime)); |
| 5321 | if (!ret) { |
| 5322 | cmd_ret = LTTNG_ERR_INVALID; |
| 5323 | goto error; |
| 5324 | } |
| 5325 | |
| 5326 | /* |
| 5327 | * Permission denied to create an output if the session is not |
| 5328 | * set in no output mode. |
| 5329 | */ |
| 5330 | if (session->output_traces) { |
| 5331 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 5332 | goto error; |
| 5333 | } |
| 5334 | |
| 5335 | /* The session needs to be started at least once. */ |
| 5336 | if (!session->has_been_started) { |
| 5337 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
| 5338 | goto error; |
| 5339 | } |
| 5340 | |
| 5341 | /* Use temporary output for the session. */ |
| 5342 | if (*output->ctrl_url != '\0') { |
| 5343 | tmp_output = snapshot_output_alloc(); |
| 5344 | if (!tmp_output) { |
| 5345 | cmd_ret = LTTNG_ERR_NOMEM; |
| 5346 | goto error; |
| 5347 | } |
| 5348 | |
| 5349 | ret = snapshot_output_init(session, |
| 5350 | output->max_size, |
| 5351 | output->name, |
| 5352 | output->ctrl_url, |
| 5353 | output->data_url, |
| 5354 | session->consumer, |
| 5355 | tmp_output, |
| 5356 | nullptr); |
| 5357 | if (ret < 0) { |
| 5358 | if (ret == -ENOMEM) { |
| 5359 | cmd_ret = LTTNG_ERR_NOMEM; |
| 5360 | } else { |
| 5361 | cmd_ret = LTTNG_ERR_INVALID; |
| 5362 | } |
| 5363 | goto error; |
| 5364 | } |
| 5365 | /* Use the global session count for the temporary snapshot. */ |
| 5366 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
| 5367 | |
| 5368 | /* Use the global datetime */ |
| 5369 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
| 5370 | cmd_ret = snapshot_record(session, tmp_output); |
| 5371 | if (cmd_ret != LTTNG_OK) { |
| 5372 | goto error; |
| 5373 | } |
| 5374 | snapshot_success = 1; |
| 5375 | } else { |
| 5376 | struct snapshot_output *sout; |
| 5377 | struct lttng_ht_iter iter; |
| 5378 | |
| 5379 | lttng::urcu::read_lock_guard read_lock; |
| 5380 | |
| 5381 | cds_lfht_for_each_entry ( |
| 5382 | session->snapshot.output_ht->ht, &iter.iter, sout, node.node) { |
| 5383 | struct snapshot_output output_copy; |
| 5384 | |
| 5385 | /* |
| 5386 | * Make a local copy of the output and override output |
| 5387 | * parameters with those provided as part of the |
| 5388 | * command. |
| 5389 | */ |
| 5390 | memcpy(&output_copy, sout, sizeof(output_copy)); |
| 5391 | |
| 5392 | if (output->max_size != (uint64_t) -1ULL) { |
| 5393 | output_copy.max_size = output->max_size; |
| 5394 | } |
| 5395 | |
| 5396 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
| 5397 | memcpy(output_copy.datetime, datetime, sizeof(datetime)); |
| 5398 | |
| 5399 | /* Use temporary name. */ |
| 5400 | if (*output->name != '\0') { |
| 5401 | if (lttng_strncpy(output_copy.name, |
| 5402 | output->name, |
| 5403 | sizeof(output_copy.name))) { |
| 5404 | cmd_ret = LTTNG_ERR_INVALID; |
| 5405 | goto error; |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | cmd_ret = snapshot_record(session, &output_copy); |
| 5410 | if (cmd_ret != LTTNG_OK) { |
| 5411 | goto error; |
| 5412 | } |
| 5413 | |
| 5414 | snapshot_success = 1; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | if (snapshot_success) { |
| 5419 | session->snapshot.nb_snapshot++; |
| 5420 | } else { |
| 5421 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
| 5422 | } |
| 5423 | |
| 5424 | error: |
| 5425 | if (tmp_output) { |
| 5426 | snapshot_output_destroy(tmp_output); |
| 5427 | } |
| 5428 | |
| 5429 | return cmd_ret; |
| 5430 | } |
| 5431 | |
| 5432 | /* |
| 5433 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. |
| 5434 | */ |
| 5435 | int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path) |
| 5436 | { |
| 5437 | /* Safety net */ |
| 5438 | LTTNG_ASSERT(session); |
| 5439 | |
| 5440 | /* |
| 5441 | * Can only set shm path before session is started. |
| 5442 | */ |
| 5443 | if (session->has_been_started) { |
| 5444 | return LTTNG_ERR_SESSION_STARTED; |
| 5445 | } |
| 5446 | |
| 5447 | strncpy(session->shm_path, shm_path, sizeof(session->shm_path)); |
| 5448 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; |
| 5449 | |
| 5450 | return LTTNG_OK; |
| 5451 | } |
| 5452 | |
| 5453 | /* |
| 5454 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. |
| 5455 | * |
| 5456 | * Ask the consumer to rotate the session output directory. |
| 5457 | * The session lock must be held. |
| 5458 | * |
| 5459 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
| 5460 | */ |
| 5461 | int cmd_rotate_session(struct ltt_session *session, |
| 5462 | struct lttng_rotate_session_return *rotate_return, |
| 5463 | bool quiet_rotation, |
| 5464 | enum lttng_trace_chunk_command_type command) |
| 5465 | { |
| 5466 | int ret; |
| 5467 | uint64_t ongoing_rotation_chunk_id; |
| 5468 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5469 | struct lttng_trace_chunk *chunk_being_archived = nullptr; |
| 5470 | struct lttng_trace_chunk *new_trace_chunk = nullptr; |
| 5471 | enum lttng_trace_chunk_status chunk_status; |
| 5472 | bool failed_to_rotate = false; |
| 5473 | enum lttng_error_code rotation_fail_code = LTTNG_OK; |
| 5474 | |
| 5475 | LTTNG_ASSERT(session); |
| 5476 | |
| 5477 | if (!session->has_been_started) { |
| 5478 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
| 5479 | goto end; |
| 5480 | } |
| 5481 | |
| 5482 | /* |
| 5483 | * Explicit rotation is not supported for live sessions. |
| 5484 | * However, live sessions can perform a quiet rotation on |
| 5485 | * destroy. |
| 5486 | * Rotation is not supported for snapshot traces (no output). |
| 5487 | */ |
| 5488 | if ((!quiet_rotation && session->live_timer) || !session->output_traces) { |
| 5489 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
| 5490 | goto end; |
| 5491 | } |
| 5492 | |
| 5493 | /* Unsupported feature in lttng-relayd before 2.11. */ |
| 5494 | if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET && |
| 5495 | (session->consumer->relay_major_version == 2 && |
| 5496 | session->consumer->relay_minor_version < 11)) { |
| 5497 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
| 5498 | goto end; |
| 5499 | } |
| 5500 | |
| 5501 | /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */ |
| 5502 | if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) { |
| 5503 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL; |
| 5504 | goto end; |
| 5505 | } |
| 5506 | |
| 5507 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 5508 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
| 5509 | session->name); |
| 5510 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
| 5511 | goto end; |
| 5512 | } |
| 5513 | |
| 5514 | /* |
| 5515 | * After a stop, we only allow one rotation to occur, the other ones are |
| 5516 | * useless until a new start. |
| 5517 | */ |
| 5518 | if (session->rotated_after_last_stop) { |
| 5519 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", |
| 5520 | session->name); |
| 5521 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
| 5522 | goto end; |
| 5523 | } |
| 5524 | |
| 5525 | /* |
| 5526 | * After a stop followed by a clear, disallow following rotations a they would |
| 5527 | * generate empty chunks. |
| 5528 | */ |
| 5529 | if (session->cleared_after_last_stop) { |
| 5530 | DBG("Session \"%s\" was already cleared after stop, refusing rotation", |
| 5531 | session->name); |
| 5532 | cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR; |
| 5533 | goto end; |
| 5534 | } |
| 5535 | |
| 5536 | if (session->active) { |
| 5537 | new_trace_chunk = |
| 5538 | session_create_new_trace_chunk(session, nullptr, nullptr, nullptr); |
| 5539 | if (!new_trace_chunk) { |
| 5540 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 5541 | goto error; |
| 5542 | } |
| 5543 | } |
| 5544 | |
| 5545 | /* |
| 5546 | * The current trace chunk becomes the chunk being archived. |
| 5547 | * |
| 5548 | * After this point, "chunk_being_archived" must absolutely |
| 5549 | * be closed on the consumer(s), otherwise it will never be |
| 5550 | * cleaned-up, which will result in a leak. |
| 5551 | */ |
| 5552 | ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived); |
| 5553 | if (ret) { |
| 5554 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5555 | goto error; |
| 5556 | } |
| 5557 | |
| 5558 | if (session->kernel_session) { |
| 5559 | cmd_ret = kernel_rotate_session(session); |
| 5560 | if (cmd_ret != LTTNG_OK) { |
| 5561 | failed_to_rotate = true; |
| 5562 | rotation_fail_code = cmd_ret; |
| 5563 | } |
| 5564 | } |
| 5565 | if (session->ust_session) { |
| 5566 | cmd_ret = ust_app_rotate_session(session); |
| 5567 | if (cmd_ret != LTTNG_OK) { |
| 5568 | failed_to_rotate = true; |
| 5569 | rotation_fail_code = cmd_ret; |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | if (!session->active) { |
| 5574 | session->rotated_after_last_stop = true; |
| 5575 | } |
| 5576 | |
| 5577 | if (!chunk_being_archived) { |
| 5578 | DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check", |
| 5579 | session->name); |
| 5580 | if (failed_to_rotate) { |
| 5581 | cmd_ret = rotation_fail_code; |
| 5582 | goto error; |
| 5583 | } |
| 5584 | cmd_ret = LTTNG_OK; |
| 5585 | goto end; |
| 5586 | } |
| 5587 | |
| 5588 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
| 5589 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id); |
| 5590 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 5591 | |
| 5592 | ret = session_close_trace_chunk( |
| 5593 | session, chunk_being_archived, command, session->last_chunk_path); |
| 5594 | if (ret) { |
| 5595 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5596 | goto error; |
| 5597 | } |
| 5598 | |
| 5599 | if (failed_to_rotate) { |
| 5600 | cmd_ret = rotation_fail_code; |
| 5601 | goto error; |
| 5602 | } |
| 5603 | |
| 5604 | session->quiet_rotation = quiet_rotation; |
| 5605 | ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER); |
| 5606 | if (ret) { |
| 5607 | cmd_ret = LTTNG_ERR_UNK; |
| 5608 | goto error; |
| 5609 | } |
| 5610 | |
| 5611 | if (rotate_return) { |
| 5612 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
| 5613 | } |
| 5614 | |
| 5615 | session->chunk_being_archived = chunk_being_archived; |
| 5616 | chunk_being_archived = nullptr; |
| 5617 | if (!quiet_rotation) { |
| 5618 | ret = notification_thread_command_session_rotation_ongoing( |
| 5619 | the_notification_thread_handle, session->id, ongoing_rotation_chunk_id); |
| 5620 | if (ret != LTTNG_OK) { |
| 5621 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", |
| 5622 | session->name); |
| 5623 | cmd_ret = (lttng_error_code) ret; |
| 5624 | } |
| 5625 | } |
| 5626 | |
| 5627 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
| 5628 | session->name, |
| 5629 | ongoing_rotation_chunk_id); |
| 5630 | end: |
| 5631 | lttng_trace_chunk_put(new_trace_chunk); |
| 5632 | lttng_trace_chunk_put(chunk_being_archived); |
| 5633 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
| 5634 | return ret; |
| 5635 | error: |
| 5636 | if (session_reset_rotation_state(*session, LTTNG_ROTATION_STATE_ERROR)) { |
| 5637 | ERR("Failed to reset rotation state of session \"%s\"", session->name); |
| 5638 | } |
| 5639 | goto end; |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
| 5644 | * |
| 5645 | * Check if the session has finished its rotation. |
| 5646 | * |
| 5647 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
| 5648 | */ |
| 5649 | int cmd_rotate_get_info(struct ltt_session *session, |
| 5650 | struct lttng_rotation_get_info_return *info_return, |
| 5651 | uint64_t rotation_id) |
| 5652 | { |
| 5653 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5654 | enum lttng_rotation_state rotation_state; |
| 5655 | |
| 5656 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, |
| 5657 | session->name, |
| 5658 | session->most_recent_chunk_id.value); |
| 5659 | |
| 5660 | if (session->chunk_being_archived) { |
| 5661 | enum lttng_trace_chunk_status chunk_status; |
| 5662 | uint64_t chunk_id; |
| 5663 | |
| 5664 | chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id); |
| 5665 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 5666 | |
| 5667 | rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING : |
| 5668 | LTTNG_ROTATION_STATE_EXPIRED; |
| 5669 | } else { |
| 5670 | if (session->last_archived_chunk_id.is_set && |
| 5671 | rotation_id != session->last_archived_chunk_id.value) { |
| 5672 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; |
| 5673 | } else { |
| 5674 | rotation_state = session->rotation_state; |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | switch (rotation_state) { |
| 5679 | case LTTNG_ROTATION_STATE_NO_ROTATION: |
| 5680 | DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"", |
| 5681 | session->name); |
| 5682 | goto end; |
| 5683 | case LTTNG_ROTATION_STATE_EXPIRED: |
| 5684 | DBG("Reporting that the rotation state of rotation id %" PRIu64 |
| 5685 | " of session \"%s\" has expired", |
| 5686 | rotation_id, |
| 5687 | session->name); |
| 5688 | break; |
| 5689 | case LTTNG_ROTATION_STATE_ONGOING: |
| 5690 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
| 5691 | rotation_id, |
| 5692 | session->name); |
| 5693 | break; |
| 5694 | case LTTNG_ROTATION_STATE_COMPLETED: |
| 5695 | { |
| 5696 | int fmt_ret; |
| 5697 | char *chunk_path; |
| 5698 | char *current_tracing_path_reply; |
| 5699 | size_t current_tracing_path_reply_len; |
| 5700 | |
| 5701 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
| 5702 | rotation_id, |
| 5703 | session->name); |
| 5704 | |
| 5705 | switch (session_get_consumer_destination_type(session)) { |
| 5706 | case CONSUMER_DST_LOCAL: |
| 5707 | current_tracing_path_reply = info_return->location.local.absolute_path; |
| 5708 | current_tracing_path_reply_len = |
| 5709 | sizeof(info_return->location.local.absolute_path); |
| 5710 | info_return->location_type = |
| 5711 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
| 5712 | fmt_ret = asprintf(&chunk_path, |
| 5713 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", |
| 5714 | session_get_base_path(session), |
| 5715 | session->last_archived_chunk_name); |
| 5716 | if (fmt_ret == -1) { |
| 5717 | PERROR("Failed to format the path of the last archived trace chunk"); |
| 5718 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5719 | cmd_ret = LTTNG_ERR_UNK; |
| 5720 | goto end; |
| 5721 | } |
| 5722 | break; |
| 5723 | case CONSUMER_DST_NET: |
| 5724 | { |
| 5725 | uint16_t ctrl_port, data_port; |
| 5726 | |
| 5727 | current_tracing_path_reply = info_return->location.relay.relative_path; |
| 5728 | current_tracing_path_reply_len = |
| 5729 | sizeof(info_return->location.relay.relative_path); |
| 5730 | /* Currently the only supported relay protocol. */ |
| 5731 | info_return->location.relay.protocol = |
| 5732 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
| 5733 | |
| 5734 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
| 5735 | session_get_net_consumer_hostname(session), |
| 5736 | sizeof(info_return->location.relay.host)); |
| 5737 | if (fmt_ret) { |
| 5738 | ERR("Failed to copy host name to rotate_get_info reply"); |
| 5739 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5740 | cmd_ret = LTTNG_ERR_SET_URL; |
| 5741 | goto end; |
| 5742 | } |
| 5743 | |
| 5744 | session_get_net_consumer_ports(session, &ctrl_port, &data_port); |
| 5745 | info_return->location.relay.ports.control = ctrl_port; |
| 5746 | info_return->location.relay.ports.data = data_port; |
| 5747 | info_return->location_type = |
| 5748 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
| 5749 | chunk_path = strdup(session->last_chunk_path); |
| 5750 | if (!chunk_path) { |
| 5751 | ERR("Failed to allocate the path of the last archived trace chunk"); |
| 5752 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5753 | cmd_ret = LTTNG_ERR_UNK; |
| 5754 | goto end; |
| 5755 | } |
| 5756 | break; |
| 5757 | } |
| 5758 | default: |
| 5759 | abort(); |
| 5760 | } |
| 5761 | |
| 5762 | fmt_ret = lttng_strncpy( |
| 5763 | current_tracing_path_reply, chunk_path, current_tracing_path_reply_len); |
| 5764 | free(chunk_path); |
| 5765 | if (fmt_ret) { |
| 5766 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); |
| 5767 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5768 | cmd_ret = LTTNG_ERR_UNK; |
| 5769 | goto end; |
| 5770 | } |
| 5771 | |
| 5772 | break; |
| 5773 | } |
| 5774 | case LTTNG_ROTATION_STATE_ERROR: |
| 5775 | DBG("Reporting that an error occurred during rotation %" PRIu64 |
| 5776 | " of session \"%s\"", |
| 5777 | rotation_id, |
| 5778 | session->name); |
| 5779 | break; |
| 5780 | default: |
| 5781 | abort(); |
| 5782 | } |
| 5783 | |
| 5784 | cmd_ret = LTTNG_OK; |
| 5785 | end: |
| 5786 | info_return->status = (int32_t) rotation_state; |
| 5787 | return cmd_ret; |
| 5788 | } |
| 5789 | |
| 5790 | /* |
| 5791 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. |
| 5792 | * |
| 5793 | * Configure the automatic rotation parameters. |
| 5794 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
| 5795 | * 'activate' to false means deactivate the rotation schedule and validate that |
| 5796 | * 'new_value' has the same value as the currently active value. |
| 5797 | * |
| 5798 | * Return LTTNG_OK on success or else a positive LTTNG_ERR code. |
| 5799 | */ |
| 5800 | int cmd_rotation_set_schedule(struct ltt_session *session, |
| 5801 | bool activate, |
| 5802 | enum lttng_rotation_schedule_type schedule_type, |
| 5803 | uint64_t new_value) |
| 5804 | { |
| 5805 | int ret; |
| 5806 | uint64_t *parameter_value; |
| 5807 | |
| 5808 | LTTNG_ASSERT(session); |
| 5809 | |
| 5810 | DBG("Cmd rotate set schedule session %s", session->name); |
| 5811 | |
| 5812 | if (session->live_timer || !session->output_traces) { |
| 5813 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
| 5814 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
| 5815 | goto end; |
| 5816 | } |
| 5817 | |
| 5818 | switch (schedule_type) { |
| 5819 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 5820 | parameter_value = &session->rotate_size; |
| 5821 | break; |
| 5822 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 5823 | parameter_value = &session->rotate_timer_period; |
| 5824 | if (new_value >= UINT_MAX) { |
| 5825 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 |
| 5826 | " > %u (UINT_MAX)", |
| 5827 | new_value, |
| 5828 | UINT_MAX); |
| 5829 | ret = LTTNG_ERR_INVALID; |
| 5830 | goto end; |
| 5831 | } |
| 5832 | break; |
| 5833 | default: |
| 5834 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); |
| 5835 | ret = LTTNG_ERR_INVALID; |
| 5836 | goto end; |
| 5837 | } |
| 5838 | |
| 5839 | /* Improper use of the API. */ |
| 5840 | if (new_value == -1ULL) { |
| 5841 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); |
| 5842 | ret = LTTNG_ERR_INVALID; |
| 5843 | goto end; |
| 5844 | } |
| 5845 | |
| 5846 | /* |
| 5847 | * As indicated in struct ltt_session's comments, a value of == 0 means |
| 5848 | * this schedule rotation type is not in use. |
| 5849 | * |
| 5850 | * Reject the command if we were asked to activate a schedule that was |
| 5851 | * already active. |
| 5852 | */ |
| 5853 | if (activate && *parameter_value != 0) { |
| 5854 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); |
| 5855 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; |
| 5856 | goto end; |
| 5857 | } |
| 5858 | |
| 5859 | /* |
| 5860 | * Reject the command if we were asked to deactivate a schedule that was |
| 5861 | * not active. |
| 5862 | */ |
| 5863 | if (!activate && *parameter_value == 0) { |
| 5864 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); |
| 5865 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; |
| 5866 | goto end; |
| 5867 | } |
| 5868 | |
| 5869 | /* |
| 5870 | * Reject the command if we were asked to deactivate a schedule that |
| 5871 | * doesn't exist. |
| 5872 | */ |
| 5873 | if (!activate && *parameter_value != new_value) { |
| 5874 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); |
| 5875 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; |
| 5876 | goto end; |
| 5877 | } |
| 5878 | |
| 5879 | *parameter_value = activate ? new_value : 0; |
| 5880 | |
| 5881 | switch (schedule_type) { |
| 5882 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 5883 | if (activate && session->active) { |
| 5884 | /* |
| 5885 | * Only start the timer if the session is active, |
| 5886 | * otherwise it will be started when the session starts. |
| 5887 | */ |
| 5888 | ret = timer_session_rotation_schedule_timer_start(session, new_value); |
| 5889 | if (ret) { |
| 5890 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
| 5891 | ret = LTTNG_ERR_UNK; |
| 5892 | goto end; |
| 5893 | } |
| 5894 | } else { |
| 5895 | ret = timer_session_rotation_schedule_timer_stop(session); |
| 5896 | if (ret) { |
| 5897 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); |
| 5898 | ret = LTTNG_ERR_UNK; |
| 5899 | goto end; |
| 5900 | } |
| 5901 | } |
| 5902 | break; |
| 5903 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 5904 | if (activate) { |
| 5905 | try { |
| 5906 | the_rotation_thread_handle->subscribe_session_consumed_size_rotation( |
| 5907 | *session, new_value); |
| 5908 | } catch (const std::exception& e) { |
| 5909 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s", |
| 5910 | e.what()); |
| 5911 | ret = LTTNG_ERR_UNK; |
| 5912 | goto end; |
| 5913 | } |
| 5914 | } else { |
| 5915 | try { |
| 5916 | the_rotation_thread_handle |
| 5917 | ->unsubscribe_session_consumed_size_rotation(*session); |
| 5918 | } catch (const std::exception& e) { |
| 5919 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s", |
| 5920 | e.what()); |
| 5921 | ret = LTTNG_ERR_UNK; |
| 5922 | goto end; |
| 5923 | } |
| 5924 | } |
| 5925 | break; |
| 5926 | default: |
| 5927 | /* Would have been caught before. */ |
| 5928 | abort(); |
| 5929 | } |
| 5930 | |
| 5931 | ret = LTTNG_OK; |
| 5932 | |
| 5933 | goto end; |
| 5934 | |
| 5935 | end: |
| 5936 | return ret; |
| 5937 | } |
| 5938 | |
| 5939 | /* Wait for a given path to be removed before continuing. */ |
| 5940 | static enum lttng_error_code wait_on_path(void *path_data) |
| 5941 | { |
| 5942 | const char *shm_path = (const char *) path_data; |
| 5943 | |
| 5944 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", |
| 5945 | shm_path); |
| 5946 | while (true) { |
| 5947 | int ret; |
| 5948 | struct stat st; |
| 5949 | |
| 5950 | ret = stat(shm_path, &st); |
| 5951 | if (ret) { |
| 5952 | if (errno != ENOENT) { |
| 5953 | PERROR("stat() returned an error while checking for the existence of the shm path"); |
| 5954 | } else { |
| 5955 | DBG("shm path no longer exists, completing the destruction of session"); |
| 5956 | } |
| 5957 | break; |
| 5958 | } else { |
| 5959 | if (!S_ISDIR(st.st_mode)) { |
| 5960 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", |
| 5961 | shm_path); |
| 5962 | break; |
| 5963 | } |
| 5964 | } |
| 5965 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); |
| 5966 | } |
| 5967 | return LTTNG_OK; |
| 5968 | } |
| 5969 | |
| 5970 | /* |
| 5971 | * Returns a pointer to a handler to run on completion of a command. |
| 5972 | * Returns NULL if no handler has to be run for the last command executed. |
| 5973 | */ |
| 5974 | const struct cmd_completion_handler *cmd_pop_completion_handler() |
| 5975 | { |
| 5976 | struct cmd_completion_handler *handler = current_completion_handler; |
| 5977 | |
| 5978 | current_completion_handler = nullptr; |
| 5979 | return handler; |
| 5980 | } |
| 5981 | |
| 5982 | /* |
| 5983 | * Init command subsystem. |
| 5984 | */ |
| 5985 | void cmd_init() |
| 5986 | { |
| 5987 | /* |
| 5988 | * Set network sequence index to 1 for streams to match a relayd |
| 5989 | * socket on the consumer side. |
| 5990 | */ |
| 5991 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 5992 | relayd_net_seq_idx = 1; |
| 5993 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 5994 | |
| 5995 | DBG("Command subsystem initialized"); |
| 5996 | } |