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