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