event-notifier-error-accounting.cpp event-notifier-error-accounting.hpp \
action-executor.cpp action-executor.hpp\
trigger-error-query.cpp \
+ ctl-utils.hpp \
field.hpp field.cpp \
clock-class.hpp clock-class.cpp \
event-class.hpp event-class.cpp \
const struct action_work_item *work_item,
struct action_work_subitem *item)
{
- int ret = 0;
const char *session_name;
enum lttng_action_status action_status;
- struct ltt_session *session;
enum lttng_error_code cmd_ret;
struct lttng_action *action = item->action;
action_status = lttng_action_start_session_get_session_name(action, &session_name);
if (action_status != LTTNG_ACTION_STATUS_OK) {
ERR("Failed to get session name from `%s` action", get_action_name(action));
- ret = -1;
- goto end;
+ return -1;
}
/*
get_action_name(action),
get_trigger_name(work_item->trigger));
lttng_action_increase_execution_failure_count(action);
- goto end;
+ return 0;
}
- session_lock_list();
- session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
- if (!session) {
- DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
- session_name,
- get_action_name(action),
- get_trigger_name(work_item->trigger));
+ /*
+ * Mind the order of the declaration of list_lock vs target_session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::locked_ref session;
+
+ try {
+ session = ltt_session::find_locked_session(
+ LTTNG_OPTIONAL_GET(item->context.session_id));
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ DBG_FMT("Failed to execution trigger action: {}, action=`{}`, trigger_name=`{}`, location='{}'",
+ ex.what(),
+ session_name,
+ get_action_name(action),
+ get_trigger_name(work_item->trigger),
+ ex.source_location);
lttng_action_increase_execution_failure_count(action);
- goto error_unlock_list;
+ return 0;
}
- session_lock(session);
if (session->destroyed) {
DBG("Session `%s` with id = %" PRIu64
" is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
session->id,
get_action_name(action),
get_trigger_name(work_item->trigger));
- goto error_unlock_session;
+ return 0;
}
- if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
- goto error_unlock_session;
+ if (!is_trigger_allowed_for_session(work_item->trigger, session.get())) {
+ return 0;
}
- cmd_ret = (lttng_error_code) cmd_start_trace(session);
+ cmd_ret = (lttng_error_code) cmd_start_trace(session.get());
switch (cmd_ret) {
case LTTNG_OK:
DBG("Successfully started session `%s` on behalf of trigger `%s`",
break;
}
-error_unlock_session:
- session_unlock(session);
- session_put(session);
-error_unlock_list:
- session_unlock_list();
-end:
- return ret;
+ return 0;
}
static int action_executor_stop_session_handler(struct action_executor *executor
const struct action_work_item *work_item,
struct action_work_subitem *item)
{
- int ret = 0;
const char *session_name;
enum lttng_action_status action_status;
- struct ltt_session *session;
enum lttng_error_code cmd_ret;
struct lttng_action *action = item->action;
action_status = lttng_action_stop_session_get_session_name(action, &session_name);
if (action_status != LTTNG_ACTION_STATUS_OK) {
ERR("Failed to get session name from `%s` action", get_action_name(action));
- ret = -1;
- goto end;
+ return -1;
}
/*
get_action_name(action),
get_trigger_name(work_item->trigger));
lttng_action_increase_execution_failure_count(action);
- goto end;
+ return 0;
}
- session_lock_list();
- session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
- if (!session) {
- DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
- session_name,
- get_action_name(action),
- get_trigger_name(work_item->trigger));
+ /*
+ * Mind the order of the declaration of list_lock vs target_session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::locked_ref session;
+
+ try {
+ session = ltt_session::find_locked_session(
+ LTTNG_OPTIONAL_GET(item->context.session_id));
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ DBG_FMT("Failed to execution trigger action: {}, action=`{}`, trigger_name=`{}`, location='{}'",
+ ex.what(),
+ session_name,
+ get_action_name(action),
+ get_trigger_name(work_item->trigger),
+ ex.source_location);
lttng_action_increase_execution_failure_count(action);
- goto error_unlock_list;
+ return 0;
}
- session_lock(session);
if (session->destroyed) {
DBG("Session `%s` with id = %" PRIu64
" is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
session->id,
get_action_name(action),
get_trigger_name(work_item->trigger));
- goto error_unlock_session;
+ return 0;
}
- if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
- goto error_unlock_session;
+ if (!is_trigger_allowed_for_session(work_item->trigger, session.get())) {
+ return 0;
}
- cmd_ret = (lttng_error_code) cmd_stop_trace(session);
+ cmd_ret = (lttng_error_code) cmd_stop_trace(session.get());
switch (cmd_ret) {
case LTTNG_OK:
DBG("Successfully stopped session `%s` on behalf of trigger `%s`",
break;
}
-error_unlock_session:
- session_unlock(session);
- session_put(session);
-error_unlock_list:
- session_unlock_list();
-end:
- return ret;
+ return 0;
}
static int action_executor_rotate_session_handler(struct action_executor *executor
const struct action_work_item *work_item,
struct action_work_subitem *item)
{
- int ret = 0;
const char *session_name;
enum lttng_action_status action_status;
- struct ltt_session *session;
enum lttng_error_code cmd_ret;
struct lttng_action *action = item->action;
action_status = lttng_action_rotate_session_get_session_name(action, &session_name);
if (action_status != LTTNG_ACTION_STATUS_OK) {
ERR("Failed to get session name from `%s` action", get_action_name(action));
- ret = -1;
- goto end;
+ return -1;
}
/*
get_action_name(action),
get_trigger_name(work_item->trigger));
lttng_action_increase_execution_failure_count(action);
- goto end;
+ return 0;
}
- session_lock_list();
- session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
- if (!session) {
- DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
- session_name,
- get_action_name(action),
- get_trigger_name(work_item->trigger));
+ /*
+ * Mind the order of the declaration of list_lock vs target_session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::locked_ref session;
+
+ try {
+ session = ltt_session::find_locked_session(
+ LTTNG_OPTIONAL_GET(item->context.session_id));
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ DBG_FMT("Failed to execution trigger action: {}, action=`{}`, trigger_name=`{}`, location='{}'",
+ ex.what(),
+ session_name,
+ get_action_name(action),
+ get_trigger_name(work_item->trigger),
+ ex.source_location);
lttng_action_increase_execution_failure_count(action);
- goto error_unlock_list;
+ return 0;
}
- session_lock(session);
if (session->destroyed) {
DBG("Session `%s` with id = %" PRIu64
" is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
session->id,
get_action_name(action),
get_trigger_name(work_item->trigger));
- goto error_unlock_session;
+ return 0;
}
- if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
- goto error_unlock_session;
+ if (!is_trigger_allowed_for_session(work_item->trigger, session.get())) {
+ return 0;
}
cmd_ret = (lttng_error_code) cmd_rotate_session(
- session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
+ session.get(), nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
switch (cmd_ret) {
case LTTNG_OK:
DBG("Successfully started rotation of session `%s` on behalf of trigger `%s`",
break;
}
-error_unlock_session:
- session_unlock(session);
- session_put(session);
-error_unlock_list:
- session_unlock_list();
-end:
- return ret;
+ return 0;
}
static int action_executor_snapshot_session_handler(struct action_executor *executor
const struct action_work_item *work_item,
struct action_work_subitem *item)
{
- int ret = 0;
const char *session_name;
enum lttng_action_status action_status;
- struct ltt_session *session;
lttng_snapshot_output default_snapshot_output;
const struct lttng_snapshot_output *snapshot_output = &default_snapshot_output;
enum lttng_error_code cmd_ret;
get_action_name(action),
get_trigger_name(work_item->trigger));
lttng_action_increase_execution_failure_count(action);
- goto end;
+ return 0;
}
action_status = lttng_action_snapshot_session_get_session_name(action, &session_name);
if (action_status != LTTNG_ACTION_STATUS_OK) {
ERR("Failed to get session name from `%s` action", get_action_name(action));
- ret = -1;
- goto end;
+ return -1;
}
action_status = lttng_action_snapshot_session_get_output(action, &snapshot_output);
if (action_status != LTTNG_ACTION_STATUS_OK && action_status != LTTNG_ACTION_STATUS_UNSET) {
ERR("Failed to get output from `%s` action", get_action_name(action));
- ret = -1;
- goto end;
+ return -1;
}
- session_lock_list();
- session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
- if (!session) {
- DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
- session_name,
- get_action_name(action),
- get_trigger_name(work_item->trigger));
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::locked_ref session;
+
+ try {
+ session = ltt_session::find_locked_session(
+ LTTNG_OPTIONAL_GET(item->context.session_id));
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ DBG_FMT("Failed to execution trigger action: {}, action=`{}`, trigger_name=`{}`, location='{}'",
+ ex.what(),
+ session_name,
+ get_action_name(action),
+ get_trigger_name(work_item->trigger),
+ ex.source_location);
lttng_action_increase_execution_failure_count(action);
- goto error_unlock_list;
+ return 0;
}
- session_lock(session);
if (session->destroyed) {
DBG("Session `%s` with id = %" PRIu64
" is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
session->id,
get_action_name(action),
get_trigger_name(work_item->trigger));
- goto error_unlock_session;
+ return 0;
}
- if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
- goto error_unlock_session;
+ if (!is_trigger_allowed_for_session(work_item->trigger, session.get())) {
+ return 0;
}
- cmd_ret = (lttng_error_code) cmd_snapshot_record(session, snapshot_output, 0);
+ cmd_ret = (lttng_error_code) cmd_snapshot_record(session.get(), snapshot_output, 0);
switch (cmd_ret) {
case LTTNG_OK:
DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%s`",
break;
}
-error_unlock_session:
- session_unlock(session);
- session_put(session);
-error_unlock_list:
- session_unlock_list();
-end:
- return ret;
+ return 0;
}
static int action_executor_list_handler(struct action_executor *executor __attribute__((unused)),
* the agent application's configuration is
* updated.
*/
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
/*
* Update the newly registered applications's
/* Removing from the poll set. */
ret = lttng_poll_del(&events, new_app_socket_fd);
if (ret < 0) {
- session_unlock_list();
goto error;
}
continue;
/* Publish the new agent app. */
agent_add_app(new_app);
-
- session_unlock_list();
} else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
/* Removing from the poll set */
ret = lttng_poll_del(&events, pollfd);
#include <common/fd-handle.hpp>
#include <common/payload-view.hpp>
#include <common/payload.hpp>
+#include <common/pthread-lock.hpp>
+#include <common/scope-exit.hpp>
#include <common/sessiond-comm/sessiond-comm.hpp>
#include <common/tracker.hpp>
#include <common/unix.hpp>
#include <sys/stat.h>
#include <unistd.h>
+namespace ls = lttng::sessiond;
+
namespace {
bool is_root;
* Setup the outgoing data buffer for the response (llm) by allocating the
* right amount of memory and copying the original information from the lsm
* structure.
- *
- * Return 0 on success, negative value on error.
*/
-static int setup_lttng_msg(struct command_ctx *cmd_ctx,
- const void *payload_buf,
- size_t payload_len,
- const void *cmd_header_buf,
- size_t cmd_header_len)
+static void setup_lttng_msg(struct command_ctx *cmd_ctx,
+ const void *payload_buf,
+ size_t payload_len,
+ const void *cmd_header_buf,
+ size_t cmd_header_len)
{
- int ret = 0;
- const size_t header_len = sizeof(struct lttcomm_lttng_msg);
- const size_t total_msg_size = header_len + cmd_header_len + payload_len;
+ const auto header_len = sizeof(struct lttcomm_lttng_msg);
+ const auto total_msg_size = header_len + cmd_header_len + payload_len;
lttcomm_lttng_msg llm{};
llm.cmd_type = cmd_ctx->lsm.cmd_type;
llm.cmd_header_size = (uint32_t) cmd_header_len;
llm.data_size = (uint32_t) payload_len;
- ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
- if (ret) {
- goto end;
- }
+ const auto zero_ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
+ LTTNG_ASSERT(zero_ret == 0);
lttng_dynamic_pointer_array_clear(&cmd_ctx->reply_payload._fd_handles);
cmd_ctx->lttng_msg_size = total_msg_size;
/* Append reply header. */
- ret = lttng_dynamic_buffer_append(&cmd_ctx->reply_payload.buffer, &llm, sizeof(llm));
- if (ret) {
- goto end;
+ if (lttng_dynamic_buffer_append(&cmd_ctx->reply_payload.buffer, &llm, sizeof(llm))) {
+ LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
+ "Failed to append the reply header to a client reply", sizeof(llm));
}
/* Append command header. */
if (cmd_header_len) {
- ret = lttng_dynamic_buffer_append(
- &cmd_ctx->reply_payload.buffer, cmd_header_buf, cmd_header_len);
- if (ret) {
- goto end;
+ if (lttng_dynamic_buffer_append(
+ &cmd_ctx->reply_payload.buffer, cmd_header_buf, cmd_header_len)) {
+ LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
+ "Failed to append the command header to a client reply",
+ cmd_header_len);
}
}
/* Append payload. */
if (payload_len) {
- ret = lttng_dynamic_buffer_append(
- &cmd_ctx->reply_payload.buffer, payload_buf, payload_len);
- if (ret) {
- goto end;
+ if (lttng_dynamic_buffer_append(
+ &cmd_ctx->reply_payload.buffer, payload_buf, payload_len)) {
+ LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
+ "Failed to append the payload to a client reply", payload_len);
}
}
-
-end:
- return ret;
}
-static int setup_empty_lttng_msg(struct command_ctx *cmd_ctx)
+static void setup_empty_lttng_msg(struct command_ctx *cmd_ctx)
{
- int ret;
const struct lttcomm_lttng_msg llm = {};
- ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
- if (ret) {
- goto end;
- }
+ const auto zero_ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
+ LTTNG_ASSERT(zero_ret == 0);
/* Append place-holder reply header. */
- ret = lttng_dynamic_buffer_append(&cmd_ctx->reply_payload.buffer, &llm, sizeof(llm));
- if (ret) {
- goto end;
+ if (lttng_dynamic_buffer_append(&cmd_ctx->reply_payload.buffer, &llm, sizeof(llm))) {
+ LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
+ "Failed to append the reply header to a client reply", sizeof(llm));
}
cmd_ctx->lttng_msg_size = sizeof(llm);
-end:
- return ret;
}
static void update_lttng_msg(struct command_ctx *cmd_ctx, size_t cmd_header_len, size_t payload_len)
return i;
}
-static enum lttng_error_code receive_lttng_trigger(struct command_ctx *cmd_ctx,
- int sock,
- int *sock_error,
- struct lttng_trigger **_trigger)
+static lttng::ctl::trigger
+receive_lttng_trigger(struct command_ctx *cmd_ctx, int sock, int *sock_error)
{
int ret;
size_t trigger_len;
ssize_t sock_recv_len;
- enum lttng_error_code ret_code;
struct lttng_payload trigger_payload;
struct lttng_trigger *trigger = nullptr;
lttng_payload_init(&trigger_payload);
+ const auto reset_payload_on_exit = lttng::make_scope_exit(
+ [&trigger_payload]() noexcept { lttng_payload_reset(&trigger_payload); });
+
trigger_len = (size_t) cmd_ctx->lsm.u.trigger.length;
ret = lttng_dynamic_buffer_set_size(&trigger_payload.buffer, trigger_len);
if (ret) {
- ret_code = LTTNG_ERR_NOMEM;
- goto end;
+ LTTNG_THROW_CTL("Failed to allocate buffer for trigger receptio", LTTNG_ERR_NOMEM);
}
sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_payload.buffer.data, trigger_len);
if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
- ERR("Failed to receive trigger in command payload");
*sock_error = 1;
- ret_code = LTTNG_ERR_INVALID_PROTOCOL;
- goto end;
+ LTTNG_THROW_PROTOCOL_ERROR("Failed to receive trigger in command payload");
}
/* Receive fds, if any. */
sock_recv_len = lttcomm_recv_payload_fds_unix_sock(
sock, cmd_ctx->lsm.fd_count, &trigger_payload);
if (sock_recv_len > 0 && sock_recv_len != cmd_ctx->lsm.fd_count * sizeof(int)) {
- ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
- cmd_ctx->lsm.fd_count,
- (int) ret);
- ret_code = LTTNG_ERR_INVALID_PROTOCOL;
*sock_error = 1;
- goto end;
+ LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
+ "Failed to receive all file descriptors for trigger in command payload: expected_fd_count={}, ret={}",
+ [cmd_ctx]() { return cmd_ctx->lsm.fd_count; }(),
+ sock_recv_len));
} else if (sock_recv_len <= 0) {
- ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
- cmd_ctx->lsm.fd_count,
- (int) ret);
- ret_code = LTTNG_ERR_FATAL;
*sock_error = 1;
- goto end;
+ LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
+ "Failed to receive file descriptors for trigger in command payload: expected_fd_count={}, ret={}",
+ [cmd_ctx]() { return cmd_ctx->lsm.fd_count; }(),
+ sock_recv_len));
}
}
struct lttng_payload_view view =
lttng_payload_view_from_payload(&trigger_payload, 0, -1);
- if (lttng_trigger_create_from_payload(&view, &trigger) != trigger_len) {
- ERR("Invalid trigger received as part of command payload");
- ret_code = LTTNG_ERR_INVALID_TRIGGER;
+ const auto trigger_create_ret = lttng_trigger_create_from_payload(&view, &trigger);
+
+ if (trigger_create_ret != trigger_len) {
lttng_trigger_put(trigger);
- goto end;
+ LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
+ "Trigger of unexpected size received as part of command payload: expected_size={}, actual_size={}",
+ trigger_len,
+ trigger_create_ret));
+ } else if (trigger_create_ret < 0) {
+ LTTNG_THROW_CTL("Failed to allocate trigger", LTTNG_ERR_NOMEM);
}
}
- *_trigger = trigger;
- ret_code = LTTNG_OK;
-
-end:
- lttng_payload_reset(&trigger_payload);
- return ret_code;
+ return lttng::ctl::trigger(trigger);
}
static enum lttng_error_code receive_lttng_error_query(struct command_ctx *cmd_ctx,
/*
* Version of setup_lttng_msg() without command header.
*/
-static int
+static void
setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx, void *payload_buf, size_t payload_len)
{
- return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, nullptr, 0);
+ setup_lttng_msg(cmd_ctx, payload_buf, payload_len, nullptr, 0);
}
/*
bool need_consumerd;
if (!lttcomm_sessiond_command_is_valid((lttcomm_sessiond_command) cmd_ctx->lsm.cmd_type)) {
- ERR("Unknown client command received: command id = %" PRIu32,
- cmd_ctx->lsm.cmd_type);
- ret = LTTNG_ERR_UND;
- goto error;
+ /* The lambda is used since fmt can't bind a packed field. */
+ LTTNG_THROW_CTL(fmt::format("Unknown client command: command_id={}",
+ [&cmd_ctx]() { return cmd_ctx->lsm.cmd_type; }()),
+ LTTNG_ERR_UND);
}
- DBG("Processing client command '%s\' (%d)",
- lttcomm_sessiond_command_str((lttcomm_sessiond_command) cmd_ctx->lsm.cmd_type),
- cmd_ctx->lsm.cmd_type);
+ DBG_FMT("Processing client command: name=`{}`, id={}",
+ lttcomm_sessiond_command_str((lttcomm_sessiond_command) cmd_ctx->lsm.cmd_type),
+ [&cmd_ctx]() { return cmd_ctx->lsm.cmd_type; }());
*sock_error = 0;
if (the_config.no_kernel && need_domain &&
cmd_ctx->lsm.domain.type == LTTNG_DOMAIN_KERNEL) {
if (!is_root) {
- ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
+ LTTNG_THROW_CTL(
+ "Can't run a kernel-domain command since the session daemon is not running as root",
+ LTTNG_ERR_NEED_ROOT_SESSIOND);
} else {
- ret = LTTNG_ERR_KERN_NA;
+ LTTNG_THROW_CTL(
+ "Can't run a kernel-domain command since kernel tracing is disabled",
+ LTTNG_ERR_KERN_NA);
}
- goto error;
}
/* Deny register consumer if we already have a spawned consumer. */
if (cmd_ctx->lsm.cmd_type == LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER) {
- pthread_mutex_lock(&the_kconsumer_data.pid_mutex);
+ const lttng::pthread::lock_guard kconsumer_lock(the_kconsumer_data.pid_mutex);
+
if (the_kconsumer_data.pid > 0) {
- ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
- pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
- goto error;
+ LTTNG_THROW_CTL(
+ "Can't register a consumer since a kernel-domain consumer was already launched",
+ LTTNG_ERR_KERN_CONSUMER_FAIL);
}
- pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
}
/*
break;
default:
/* Setup lttng message with no payload */
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, nullptr, 0);
- if (ret < 0) {
- /* This label does not try to unlock the session */
- goto init_setup_error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, nullptr, 0);
}
+ /*
+ * The list lock is only acquired when processing a command that is applied
+ * against a session. As such, a unique_lock that holds the list lock is move()'d to
+ * list_lock during the execution of those commands. The list lock is then released
+ * as the instance leaves this scope.
+ *
+ * Mind the order of the declaration of list_lock vs target_session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ std::unique_lock<std::mutex> list_lock;
+ /*
+ * A locked_ref is typically "never null" (hence its name). However, due to the
+ * structure of this function, target_session remains null for commands that don't
+ * have a target session.
+ */
+ ltt_session::locked_ref target_session;
+
/* Commands that DO NOT need a session. */
switch (cmd_ctx->lsm.cmd_type) {
case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT:
* for now, because the per-session lock does not
* handle teardown properly.
*/
- session_lock_list();
- cmd_ctx->session = session_find_by_name(cmd_ctx->lsm.session.name);
- if (cmd_ctx->session == nullptr) {
- ret = LTTNG_ERR_SESS_NOT_FOUND;
- goto error;
- } else {
- /* Acquire lock for the session */
- session_lock(cmd_ctx->session);
- }
+ list_lock = lttng::sessiond::lock_session_list();
+ try {
+ target_session =
+ ltt_session::find_locked_session(cmd_ctx->lsm.session.name);
+ } catch (...) {
+ std::throw_with_nested(lttng::ctl::error(
+ fmt::format(
+ "Target session of command doesn't exist: command='{}'",
+ lttcomm_sessiond_command_str(
+ (lttcomm_sessiond_command) cmd_ctx->lsm.cmd_type)),
+ LTTNG_ERR_SESS_NOT_FOUND,
+ LTTNG_SOURCE_LOCATION()));
+ }
+
+ LTTNG_ASSERT(target_session);
break;
}
case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT:
switch (cmd_ctx->lsm.domain.type) {
case LTTNG_DOMAIN_KERNEL:
- if (!cmd_ctx->session->kernel_session) {
- ret = LTTNG_ERR_NO_CHANNEL;
- goto error;
+ if (!target_session->kernel_session) {
+ return LTTNG_ERR_NO_CHANNEL;
}
break;
case LTTNG_DOMAIN_JUL:
case LTTNG_DOMAIN_LOG4J:
case LTTNG_DOMAIN_PYTHON:
case LTTNG_DOMAIN_UST:
- if (!cmd_ctx->session->ust_session) {
- ret = LTTNG_ERR_NO_CHANNEL;
+ if (!target_session->ust_session) {
+ return LTTNG_ERR_NO_CHANNEL;
goto error;
}
break;
/* Need a session for kernel command */
if (need_tracing_session) {
- if (cmd_ctx->session->kernel_session == nullptr) {
- ret = create_kernel_session(cmd_ctx->session);
+ if (target_session->kernel_session == nullptr) {
+ ret = create_kernel_session(target_session.get());
if (ret != LTTNG_OK) {
ret = LTTNG_ERR_KERN_SESS_FAIL;
goto error;
* the consumer output of the session if exist.
*/
ret = consumer_create_socket(&the_kconsumer_data,
- cmd_ctx->session->kernel_session->consumer);
+ target_session->kernel_session->consumer);
if (ret < 0) {
goto error;
}
if (need_tracing_session) {
/* Create UST session if none exist. */
- if (cmd_ctx->session->ust_session == nullptr) {
+ if (target_session->ust_session == nullptr) {
lttng_domain domain = cmd_ctx->lsm.domain;
- ret = create_ust_session(cmd_ctx->session, &domain);
+ ret = create_ust_session(target_session.get(), &domain);
if (ret != LTTNG_OK) {
goto error;
}
* since it was set above and can ONLY be set in this thread.
*/
ret = consumer_create_socket(&the_ustconsumer64_data,
- cmd_ctx->session->ust_session->consumer);
+ target_session->ust_session->consumer);
if (ret < 0) {
goto error;
}
* since it was set above and can ONLY be set in this thread.
*/
ret = consumer_create_socket(&the_ustconsumer32_data,
- cmd_ctx->session->ust_session->consumer);
+ target_session->ust_session->consumer);
if (ret < 0) {
goto error;
}
* The root user can interact with all sessions.
*/
if (need_tracing_session) {
- if (!session_access_ok(cmd_ctx->session,
+ if (!session_access_ok(target_session.get(),
LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds)) ||
- cmd_ctx->session->destroyed) {
+ target_session->destroyed) {
ret = LTTNG_ERR_EPERM;
goto error;
}
* Send relayd information to consumer as soon as we have a domain and a
* session defined.
*/
- if (cmd_ctx->session && need_domain) {
+ if (target_session && need_domain) {
/*
* Setup relayd if not done yet. If the relayd information was already
* sent to the consumer, this call will gracefully return.
*/
- ret = cmd_setup_relayd(cmd_ctx->session);
+ ret = cmd_setup_relayd(target_session.get());
if (ret != LTTNG_OK) {
goto error;
}
goto error;
}
- ret = cmd_add_context(cmd_ctx, event_context, the_kernel_poll_pipe[1]);
+ ret = cmd_add_context(
+ cmd_ctx, target_session, event_context, the_kernel_poll_pipe[1]);
lttng_event_context_destroy(event_context);
break;
}
case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL:
{
- ret = cmd_disable_channel(cmd_ctx->session,
+ ret = cmd_disable_channel(target_session.get(),
cmd_ctx->lsm.domain.type,
cmd_ctx->lsm.u.disable.channel_name);
break;
}
case LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL:
{
- ret = cmd_enable_channel(cmd_ctx, *sock, the_kernel_poll_pipe[1]);
+ ret = cmd_enable_channel(cmd_ctx, target_session, *sock, the_kernel_poll_pipe[1]);
break;
}
case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
if (add_value) {
ret = cmd_process_attr_tracker_inclusion_set_add_value(
- cmd_ctx->session, domain_type, process_attr, value);
+ target_session.get(), domain_type, process_attr, value);
} else {
ret = cmd_process_attr_tracker_inclusion_set_remove_value(
- cmd_ctx->session, domain_type, process_attr, value);
+ target_session.get(), domain_type, process_attr, value);
}
process_attr_value_destroy(value);
error_add_remove_tracker_value:
.process_attr_tracker_get_tracking_policy.process_attr;
ret = cmd_process_attr_tracker_get_tracking_policy(
- cmd_ctx->session, domain_type, process_attr, &tracking_policy);
+ target_session.get(), domain_type, process_attr, &tracking_policy);
if (ret != LTTNG_OK) {
goto error;
}
uint32_t tracking_policy_u32 = tracking_policy;
- ret = setup_lttng_msg_no_cmd_header(
- cmd_ctx, &tracking_policy_u32, sizeof(uint32_t));
- if (ret < 0) {
- ret = LTTNG_ERR_NOMEM;
- goto error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &tracking_policy_u32, sizeof(uint32_t));
+
ret = LTTNG_OK;
break;
}
.process_attr_tracker_set_tracking_policy.process_attr;
ret = cmd_process_attr_tracker_set_tracking_policy(
- cmd_ctx->session, domain_type, process_attr, tracking_policy);
+ target_session.get(), domain_type, process_attr, tracking_policy);
if (ret != LTTNG_OK) {
goto error;
}
cmd_ctx->lsm.u.process_attr_tracker_get_inclusion_set.process_attr;
ret = cmd_process_attr_tracker_get_inclusion_set(
- cmd_ctx->session, domain_type, process_attr, &values);
+ target_session.get(), domain_type, process_attr, &values);
if (ret != LTTNG_OK) {
goto error;
}
goto error_tracker_get_inclusion_set;
}
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, reply.data, reply.size);
- if (ret < 0) {
- ret = LTTNG_ERR_NOMEM;
- goto error_tracker_get_inclusion_set;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, reply.data, reply.size);
ret = LTTNG_OK;
error_tracker_get_inclusion_set:
*/
ret = cmd_ctx->lsm.cmd_type == LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT ?
cmd_enable_event(cmd_ctx,
+ target_session,
event,
filter_expression,
exclusions,
bytecode,
the_kernel_poll_pipe[1]) :
- cmd_disable_event(cmd_ctx, event, filter_expression, bytecode, exclusions);
+ cmd_disable_event(cmd_ctx,
+ target_session,
+ event,
+ filter_expression,
+ bytecode,
+ exclusions);
lttng_event_destroy(event);
break;
}
size_t payload_size;
const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
- session_lock_list();
+ list_lock = lttng::sessiond::lock_session_list();
ret_code = cmd_list_tracepoints(cmd_ctx->lsm.domain.type, &cmd_ctx->reply_payload);
- session_unlock_list();
if (ret_code != LTTNG_OK) {
ret = (int) ret_code;
goto error;
size_t payload_size;
const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
- session_lock_list();
+ list_lock = lttng::sessiond::lock_session_list();
ret_code = cmd_list_tracepoint_fields(cmd_ctx->lsm.domain.type,
&cmd_ctx->reply_payload);
- session_unlock_list();
+
if (ret_code != LTTNG_OK) {
ret = (int) ret_code;
goto error;
size_t payload_size;
const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
goto error;
}
- ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
+ ret = cmd_set_consumer_uri(target_session.get(), nb_uri, uris);
free(uris);
if (ret != LTTNG_OK) {
goto error;
* enabled time or size-based rotations, we have to make sure
* the kernel tracer supports it.
*/
- if (!cmd_ctx->session->has_been_started && cmd_ctx->session->kernel_session &&
- (cmd_ctx->session->rotate_timer_period || cmd_ctx->session->rotate_size) &&
+ if (!target_session->has_been_started && target_session->kernel_session &&
+ (target_session->rotate_timer_period || target_session->rotate_size) &&
!check_rotate_compatible()) {
DBG("Kernel tracer version is not compatible with the rotation feature");
ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
goto error;
}
- ret = cmd_start_trace(cmd_ctx->session);
+ ret = cmd_start_trace(target_session.get());
break;
}
case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE:
{
- ret = cmd_stop_trace(cmd_ctx->session);
+ ret = cmd_stop_trace(target_session.get());
break;
}
case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION:
{
- ret = cmd_destroy_session(cmd_ctx->session, sock);
+ ret = cmd_destroy_session(target_session.get(), sock);
break;
}
case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS:
ssize_t nb_dom;
struct lttng_domain *domains = nullptr;
- nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
+ nb_dom = cmd_list_domains(target_session.get(), &domains);
if (nb_dom < 0) {
/* Return value is a negative lttng_error_code. */
ret = -nb_dom;
goto error;
}
- ret = setup_lttng_msg_no_cmd_header(
+ setup_lttng_msg_no_cmd_header(
cmd_ctx, domains, nb_dom * sizeof(struct lttng_domain));
free(domains);
- if (ret < 0) {
- goto setup_error;
- }
-
ret = LTTNG_OK;
break;
}
size_t payload_size;
const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
ret_code = cmd_list_channels(
- cmd_ctx->lsm.domain.type, cmd_ctx->session, &cmd_ctx->reply_payload);
+ cmd_ctx->lsm.domain.type, target_session.get(), &cmd_ctx->reply_payload);
if (ret_code != LTTNG_OK) {
ret = (int) ret_code;
goto error;
size_t payload_size;
const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
ret_code = cmd_list_events(cmd_ctx->lsm.domain.type,
- cmd_ctx->session,
+ target_session.get(),
cmd_ctx->lsm.u.list.channel_name,
&cmd_ctx->reply_payload);
if (ret_code != LTTNG_OK) {
lttng_session *sessions_payload = nullptr;
size_t payload_len = 0;
- session_lock_list();
+ list_lock = lttng::sessiond::lock_session_list();
nr_sessions = lttng_sessions_count(LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
(sizeof(struct lttng_session_extended) * nr_sessions);
sessions_payload = zmalloc<lttng_session>(payload_len);
if (!sessions_payload) {
- session_unlock_list();
- ret = -ENOMEM;
- goto setup_error;
+ LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
+ "Failed to allocate session list reply payload",
+ payload_len);
}
cmd_list_lttng_sessions(sessions_payload,
LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
}
- session_unlock_list();
-
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, payload_len);
+ setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, payload_len);
free(sessions_payload);
- if (ret < 0) {
- goto setup_error;
- }
-
ret = LTTNG_OK;
break;
}
goto error;
}
- ret = cmd_register_consumer(
- cmd_ctx->session, cmd_ctx->lsm.domain.type, cmd_ctx->lsm.u.reg.path, cdata);
+ ret = cmd_register_consumer(target_session.get(),
+ cmd_ctx->lsm.domain.type,
+ cmd_ctx->lsm.u.reg.path,
+ cdata);
break;
}
case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS:
}
u_status = (uint32_t) status;
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &u_status, 4);
- if (ret < 0) {
- goto error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &u_status, 4);
ret = LTTNG_OK;
break;
int pending_ret;
uint8_t pending_ret_byte;
- pending_ret = cmd_data_pending(cmd_ctx->session);
+ pending_ret = cmd_data_pending(target_session.get());
/*
* FIXME
pending_ret_byte = (uint8_t) pending_ret;
/* 1 byte to return whether or not data is pending */
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &pending_ret_byte, 1);
-
- if (ret < 0) {
- goto setup_error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &pending_ret_byte, 1);
ret = LTTNG_OK;
break;
struct lttcomm_lttng_output_id reply;
lttng_snapshot_output output = cmd_ctx->lsm.u.snapshot_output.output;
- ret = cmd_snapshot_add_output(cmd_ctx->session, &output, &snapshot_id);
+ ret = cmd_snapshot_add_output(target_session.get(), &output, &snapshot_id);
if (ret != LTTNG_OK) {
goto error;
}
reply.id = snapshot_id;
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply, sizeof(reply));
- if (ret < 0) {
- goto setup_error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &reply, sizeof(reply));
/* Copy output list into message payload */
ret = LTTNG_OK;
case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT:
{
lttng_snapshot_output output = cmd_ctx->lsm.u.snapshot_output.output;
- ret = cmd_snapshot_del_output(cmd_ctx->session, &output);
+ ret = cmd_snapshot_del_output(target_session.get(), &output);
break;
}
case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT:
ssize_t nb_output;
struct lttng_snapshot_output *outputs = nullptr;
- nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
+ nb_output = cmd_snapshot_list_outputs(target_session.get(), &outputs);
if (nb_output < 0) {
ret = -nb_output;
goto error;
}
LTTNG_ASSERT((nb_output > 0 && outputs) || nb_output == 0);
- ret = setup_lttng_msg_no_cmd_header(
+ setup_lttng_msg_no_cmd_header(
cmd_ctx, outputs, nb_output * sizeof(struct lttng_snapshot_output));
free(outputs);
- if (ret < 0) {
- goto setup_error;
- }
-
ret = LTTNG_OK;
break;
}
case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD:
{
lttng_snapshot_output output = cmd_ctx->lsm.u.snapshot_record.output;
- ret = cmd_snapshot_record(cmd_ctx->session, &output, 0); // RFC: set to zero since
- // it's ignored by
- // cmd_snapshot_record
+ ret = cmd_snapshot_record(target_session.get(), &output, 0); // RFC: set to zero
+ // since it's ignored
+ // by
+ // cmd_snapshot_record
break;
}
case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT:
{
struct lttng_dynamic_buffer payload;
- struct lttng_session_descriptor *return_descriptor = nullptr;
lttng_dynamic_buffer_init(&payload);
- ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor);
- if (ret != LTTNG_OK) {
- goto error;
- }
- ret = lttng_session_descriptor_serialize(return_descriptor, &payload);
- if (ret) {
- ERR("Failed to serialize session descriptor in reply to \"create session\" command");
- lttng_session_descriptor_destroy(return_descriptor);
- ret = LTTNG_ERR_NOMEM;
- goto error;
- }
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data, payload.size);
+ lttng::ctl::session_descriptor reply_session_descriptor = [cmd_ctx, sock]() {
+ lttng_session_descriptor *raw_descriptor;
+ const auto create_ret = cmd_create_session(cmd_ctx, *sock, &raw_descriptor);
+ if (create_ret != LTTNG_OK) {
+ LTTNG_THROW_CTL("Failed to create session", create_ret);
+ }
+
+ return lttng::ctl::session_descriptor(raw_descriptor);
+ }();
+
+ ret = lttng_session_descriptor_serialize(reply_session_descriptor.get(), &payload);
if (ret) {
- lttng_session_descriptor_destroy(return_descriptor);
- ret = LTTNG_ERR_NOMEM;
- goto error;
+ LTTNG_THROW_CTL(
+ "Failed to serialize session descriptor in reply to \"create session\" command",
+ LTTNG_ERR_NOMEM);
}
+
+ setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data, payload.size);
+
lttng_dynamic_buffer_reset(&payload);
- lttng_session_descriptor_destroy(return_descriptor);
ret = LTTNG_OK;
break;
}
}
case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH:
{
- ret = cmd_set_session_shm_path(cmd_ctx->session,
+ ret = cmd_set_session_shm_path(target_session.get(),
cmd_ctx->lsm.u.set_shm_path.shm_path);
break;
}
case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA:
{
- ret = cmd_regenerate_metadata(cmd_ctx->session);
+ ret = cmd_regenerate_metadata(target_session.get());
break;
}
case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP:
{
- ret = cmd_regenerate_statedump(cmd_ctx->session);
+ ret = cmd_regenerate_statedump(target_session.get());
break;
}
case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER:
{
- struct lttng_trigger *payload_trigger;
- struct lttng_trigger *return_trigger;
size_t original_reply_payload_size;
size_t reply_payload_size;
const struct lttng_credentials cmd_creds = {
.gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
};
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
- ret = receive_lttng_trigger(cmd_ctx, *sock, sock_error, &payload_trigger);
+ auto payload_trigger = receive_lttng_trigger(cmd_ctx, *sock, sock_error);
if (ret != LTTNG_OK) {
goto error;
}
original_reply_payload_size = cmd_ctx->reply_payload.buffer.size;
- ret = cmd_register_trigger(&cmd_creds,
- payload_trigger,
- cmd_ctx->lsm.u.trigger.is_trigger_anonymous,
- the_notification_thread_handle,
- &return_trigger);
- if (ret != LTTNG_OK) {
- lttng_trigger_put(payload_trigger);
- goto error;
- }
+ auto return_trigger =
+ cmd_register_trigger(&cmd_creds,
+ payload_trigger.get(),
+ cmd_ctx->lsm.u.trigger.is_trigger_anonymous,
+ the_notification_thread_handle);
- ret = lttng_trigger_serialize(return_trigger, &cmd_ctx->reply_payload);
- lttng_trigger_put(payload_trigger);
- lttng_trigger_put(return_trigger);
+ ret = lttng_trigger_serialize(return_trigger.get(), &cmd_ctx->reply_payload);
if (ret) {
- ERR("Failed to serialize trigger in reply to \"register trigger\" command");
- ret = LTTNG_ERR_NOMEM;
- goto error;
+ LTTNG_THROW_CTL(
+ "Failed to serialize trigger in reply to \"register trigger\" command",
+ LTTNG_ERR_NOMEM);
}
reply_payload_size =
}
case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER:
{
- struct lttng_trigger *payload_trigger;
const struct lttng_credentials cmd_creds = {
.uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
.gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
};
- ret = receive_lttng_trigger(cmd_ctx, *sock, sock_error, &payload_trigger);
- if (ret != LTTNG_OK) {
- goto error;
- }
+ auto payload_trigger = receive_lttng_trigger(cmd_ctx, *sock, sock_error);
ret = cmd_unregister_trigger(
- &cmd_creds, payload_trigger, the_notification_thread_handle);
- lttng_trigger_put(payload_trigger);
+ &cmd_creds, payload_trigger.get(), the_notification_thread_handle);
break;
}
case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION:
{
struct lttng_rotate_session_return rotate_return;
- DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
+ DBG("Client rotate session \"%s\"", target_session->name);
memset(&rotate_return, 0, sizeof(rotate_return));
- if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
+ if (target_session->kernel_session && !check_rotate_compatible()) {
DBG("Kernel tracer version is not compatible with the rotation feature");
ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
goto error;
}
- ret = cmd_rotate_session(cmd_ctx->session,
+ ret = cmd_rotate_session(target_session.get(),
&rotate_return,
false,
LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
goto error;
}
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return, sizeof(rotate_return));
- if (ret < 0) {
- ret = -ret;
- goto error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return, sizeof(rotate_return));
ret = LTTNG_OK;
break;
struct lttng_rotation_get_info_return get_info_return;
memset(&get_info_return, 0, sizeof(get_info_return));
- ret = cmd_rotate_get_info(cmd_ctx->session,
+ ret = cmd_rotate_get_info(target_session.get(),
&get_info_return,
cmd_ctx->lsm.u.get_rotation_info.rotation_id);
if (ret < 0) {
goto error;
}
- ret = setup_lttng_msg_no_cmd_header(
- cmd_ctx, &get_info_return, sizeof(get_info_return));
- if (ret < 0) {
- ret = -ret;
- goto error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return, sizeof(get_info_return));
ret = LTTNG_OK;
break;
enum lttng_rotation_schedule_type schedule_type;
uint64_t value;
- if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
+ if (target_session->kernel_session && !check_rotate_compatible()) {
DBG("Kernel tracer version does not support session rotations");
ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
goto error;
value = cmd_ctx->lsm.u.rotation_set_schedule.value;
ret = cmd_rotation_set_schedule(
- cmd_ctx->session, set_schedule, schedule_type, value);
+ target_session.get(), set_schedule, schedule_type, value);
if (ret != LTTNG_OK) {
goto error;
}
{
lttng_session_list_schedules_return schedules;
- schedules.periodic.set = !!cmd_ctx->session->rotate_timer_period;
- schedules.periodic.value = cmd_ctx->session->rotate_timer_period;
- schedules.size.set = !!cmd_ctx->session->rotate_size;
- schedules.size.value = cmd_ctx->session->rotate_size;
+ schedules.periodic.set = !!target_session->rotate_timer_period;
+ schedules.periodic.value = target_session->rotate_timer_period;
+ schedules.size.set = !!target_session->rotate_size;
+ schedules.size.value = target_session->rotate_size;
- ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules, sizeof(schedules));
- if (ret < 0) {
- ret = -ret;
- goto error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules, sizeof(schedules));
ret = LTTNG_OK;
break;
}
case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION:
{
- ret = cmd_clear_session(cmd_ctx->session, sock);
+ ret = cmd_clear_session(target_session.get(), sock);
break;
}
case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS:
size_t original_payload_size;
size_t payload_size;
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
size_t original_payload_size;
size_t payload_size;
- ret = setup_empty_lttng_msg(cmd_ctx);
- if (ret) {
- ret = LTTNG_ERR_NOMEM;
- goto setup_error;
- }
+ setup_empty_lttng_msg(cmd_ctx);
original_payload_size = cmd_ctx->reply_payload.buffer.size;
error:
if (cmd_ctx->reply_payload.buffer.size == 0) {
DBG("Missing llm header, creating one.");
- if (setup_lttng_msg_no_cmd_header(cmd_ctx, nullptr, 0) < 0) {
- goto setup_error;
- }
+ setup_lttng_msg_no_cmd_header(cmd_ctx, nullptr, 0);
}
command_ctx_set_status_code(*cmd_ctx, static_cast<lttng_error_code>(ret));
-
-setup_error:
- if (cmd_ctx->session) {
- session_unlock(cmd_ctx->session);
- session_put(cmd_ctx->session);
- cmd_ctx->session = nullptr;
- }
- if (need_tracing_session) {
- session_unlock_list();
- }
-init_setup_error:
LTTNG_ASSERT(!rcu_read_ongoing());
return ret;
}
set_thread_status(false);
}
+// Helper function to log the source_location if the exception is derived from lttng::runtime_error
+template <typename ExceptionType>
+typename std::enable_if<std::is_base_of<lttng::runtime_error, ExceptionType>::value,
+ std::string>::type
+formatted_source_location(const ExceptionType& ex)
+{
+ return fmt::format("{}", ex.source_location);
+}
+
+template <typename ExceptionType>
+typename std::enable_if<!std::is_base_of<lttng::runtime_error, ExceptionType>::value,
+ std::string>::type
+formatted_source_location(const ExceptionType&)
+{
+ return "";
+}
+
+template <class ExceptionType>
+static void log_nested_exceptions(const ExceptionType& ex, unsigned int level = 0)
+{
+ const auto location = formatted_source_location(ex);
+
+ if (level == 0) {
+ if (location.size()) {
+ WARN_FMT("Client request failed: {}, location='{}'", ex.what(), location);
+ } else {
+ WARN_FMT("Client request failed: {}", ex.what());
+ }
+ } else {
+ if (location.size()) {
+ WARN_FMT("\t{}, location='{}'", ex.what(), location);
+ } else {
+ WARN_FMT("\t{}", ex.what());
+ }
+ }
+
+ try {
+ std::rethrow_if_nested(ex);
+ } catch (const lttng::runtime_error& nested_ex) {
+ log_nested_exceptions(nested_ex, level + 1);
+ } catch (const std::exception& nested_ex) {
+ log_nested_exceptions(nested_ex, level + 1);
+ }
+}
+
/*
* This thread manage all clients request using the unix client socket for
* communication.
cmd_ctx.creds.uid = UINT32_MAX;
cmd_ctx.creds.gid = UINT32_MAX;
cmd_ctx.creds.pid = 0;
- cmd_ctx.session = nullptr;
lttng_payload_clear(&cmd_ctx.reply_payload);
cmd_ctx.lttng_msg_size = 0;
try {
ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
rcu_thread_offline();
- if (ret < 0) {
- if (sock >= 0) {
- ret = close(sock);
- if (ret) {
- PERROR("close");
- }
- }
- sock = -1;
- /*
- * TODO: Inform client somehow of the fatal error. At
- * this point, ret < 0 means that a zmalloc failed
- * (ENOMEM). Error detected but still accept
- * command, unless a socket error has been
- * detected.
- */
- continue;
- }
} catch (const std::bad_alloc& ex) {
- WARN_FMT("Failed to allocate memory while handling client request: {}",
- ex.what());
-
- /*
- * Reset the payload contents as the command may have left them in an
- * inconsistent state.
- */
- (void) setup_empty_lttng_msg(&cmd_ctx);
- command_ctx_set_status_code(cmd_ctx, LTTNG_ERR_NOMEM);
+ log_nested_exceptions(ex);
+ ret = LTTNG_ERR_NOMEM;
} catch (const lttng::ctl::error& ex) {
- WARN_FMT("Client request failed: {}", ex.what());
-
- (void) setup_empty_lttng_msg(&cmd_ctx);
- command_ctx_set_status_code(cmd_ctx, ex.code());
+ log_nested_exceptions(ex);
+ ret = ex.code();
} catch (const lttng::invalid_argument_error& ex) {
- WARN_FMT("Client request failed: {}", ex.what());
-
- (void) setup_empty_lttng_msg(&cmd_ctx);
- command_ctx_set_status_code(cmd_ctx, LTTNG_ERR_INVALID);
+ log_nested_exceptions(ex);
+ ret = LTTNG_ERR_INVALID;
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ log_nested_exceptions(ex);
+ ret = LTTNG_ERR_SESS_NOT_FOUND;
+ } catch (const lttng::runtime_error& ex) {
+ log_nested_exceptions(ex);
+ ret = LTTNG_ERR_UNK;
} catch (const std::exception& ex) {
- WARN_FMT("Client request failed: {}", ex.what());
-
- (void) setup_empty_lttng_msg(&cmd_ctx);
- command_ctx_set_status_code(cmd_ctx, LTTNG_ERR_UNK);
+ log_nested_exceptions(ex);
+ ret = LTTNG_ERR_UNK;
}
if (ret < LTTNG_OK || ret >= LTTNG_ERR_NR) {
ret = LTTNG_ERR_UNK;
}
+ if (ret != LTTNG_OK) {
+ /*
+ * Reset the payload contents as the command may have left them in an
+ * inconsistent state.
+ */
+ setup_empty_lttng_msg(&cmd_ctx);
+ }
+
+ command_ctx_set_status_code(cmd_ctx, static_cast<lttng_error_code>(ret));
+
cmd_completion_handler = cmd_pop_completion_handler();
if (cmd_completion_handler) {
enum lttng_error_code completion_code;
static struct cmd_completion_handler *current_completion_handler;
static int validate_ust_event_name(const char *);
-static int cmd_enable_event_internal(struct ltt_session *session,
+static int cmd_enable_event_internal(ltt_session::locked_ref& session,
const struct lttng_domain *domain,
char *channel_name,
struct lttng_event *event,
struct lttng_bytecode *filter,
struct lttng_event_exclusion *exclusion,
int wpipe);
-static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
+static enum lttng_error_code cmd_enable_channel_internal(ltt_session::locked_ref& session,
const struct lttng_domain *domain,
const struct lttng_channel *_attr,
int wpipe);
*
* The wpipe arguments is used as a notifier for the kernel thread.
*/
-int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
+int cmd_enable_channel(command_ctx *cmd_ctx, ltt_session::locked_ref& session, int sock, int wpipe)
{
int ret;
size_t channel_len;
goto end;
}
- ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe);
+ ret = cmd_enable_channel_internal(session, &command_domain, channel, wpipe);
end:
lttng_dynamic_buffer_reset(&channel_buffer);
return ret;
}
-static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
+static enum lttng_error_code cmd_enable_channel_internal(ltt_session::locked_ref& session,
const struct lttng_domain *domain,
const struct lttng_channel *_attr,
int wpipe)
{
enum lttng_error_code ret_code;
- struct ltt_ust_session *usess = session->ust_session;
+ struct ltt_ust_session *usess = session.get()->ust_session;
struct lttng_ht *chan_ht;
size_t len;
struct lttng_channel *attr = nullptr;
- LTTNG_ASSERT(session);
LTTNG_ASSERT(_attr);
LTTNG_ASSERT(domain);
* live timer does the same thing but sends also synchronisation
* beacons for inactive streams.
*/
- if (session->live_timer > 0) {
- attr->attr.live_timer_interval = session->live_timer;
+ if (session.get()->live_timer > 0) {
+ attr->attr.live_timer_interval = session.get()->live_timer;
attr->attr.switch_timer_interval = 0;
}
"Setting the monitor interval timer to 0 "
"(disabled) for channel '%s' of session '%s'",
attr->name,
- session->name);
+ session.get()->name);
lttng_channel_set_monitor_timer_interval(attr, 0);
}
break;
* Command LTTNG_DISABLE_EVENT processed by the client thread.
*/
int cmd_disable_event(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& locked_session,
struct lttng_event *event,
char *filter_expression,
struct lttng_bytecode *bytecode,
struct lttng_event_exclusion *exclusion)
{
int ret;
+ ltt_session& session = *locked_session.get();
const char *event_name;
- const struct ltt_session *session = cmd_ctx->session;
const char *channel_name = cmd_ctx->lsm.u.disable.channel_name;
const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
struct ltt_kernel_channel *kchan;
struct ltt_kernel_session *ksess;
- ksess = session->kernel_session;
+ ksess = session.kernel_session;
/*
* If a non-default channel has been created in the
struct ltt_ust_channel *uchan;
struct ltt_ust_session *usess;
- usess = session->ust_session;
+ usess = session.ust_session;
if (validate_ust_event_name(event_name)) {
ret = LTTNG_ERR_INVALID_EVENT_NAME;
case LTTNG_DOMAIN_PYTHON:
{
struct agent *agt;
- struct ltt_ust_session *usess = session->ust_session;
+ struct ltt_ust_session *usess = session.ust_session;
LTTNG_ASSERT(usess);
* Command LTTNG_ADD_CONTEXT processed by the client thread.
*/
int cmd_add_context(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& locked_session,
const struct lttng_event_context *event_context,
int kwpipe)
{
int ret, chan_kern_created = 0, chan_ust_created = 0;
const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
- const struct ltt_session *session = cmd_ctx->session;
+ const struct ltt_session& session = *locked_session.get();
const char *channel_name = cmd_ctx->lsm.u.context.channel_name;
/*
* some point in time before. The tracer does not allow it and would
* result in a corrupted trace.
*/
- if (cmd_ctx->session->has_been_started) {
+ if (session.has_been_started) {
ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
goto end;
}
switch (domain) {
case LTTNG_DOMAIN_KERNEL:
- LTTNG_ASSERT(session->kernel_session);
+ LTTNG_ASSERT(session.kernel_session);
- if (session->kernel_session->channel_count == 0) {
+ if (session.kernel_session->channel_count == 0) {
/* Create default channel */
- ret = channel_kernel_create(session->kernel_session, nullptr, kwpipe);
+ ret = channel_kernel_create(session.kernel_session, nullptr, kwpipe);
if (ret != LTTNG_OK) {
goto error;
}
chan_kern_created = 1;
}
/* Add kernel context to kernel tracer */
- ret = context_kernel_add(session->kernel_session, event_context, channel_name);
+ ret = context_kernel_add(session.kernel_session, event_context, channel_name);
if (ret != LTTNG_OK) {
goto error;
}
/* fall through */
case LTTNG_DOMAIN_UST:
{
- struct ltt_ust_session *usess = session->ust_session;
+ struct ltt_ust_session *usess = session.ust_session;
unsigned int chan_count;
LTTNG_ASSERT(usess);
error:
if (chan_kern_created) {
struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name(
- DEFAULT_CHANNEL_NAME, session->kernel_session);
+ DEFAULT_CHANNEL_NAME, session.kernel_session);
/* Created previously, this should NOT fail. */
LTTNG_ASSERT(kchan);
kernel_destroy_channel(kchan);
if (chan_ust_created) {
struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name(
- session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
+ session.ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
/* Created previously, this should NOT fail. */
LTTNG_ASSERT(uchan);
/* Remove from the channel list of the session. */
- trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan);
+ trace_ust_delete_channel(session.ust_session->domain_global.channels, uchan);
trace_ust_destroy_channel(uchan);
}
end:
* be hidden from clients. Such events are used in the agent implementation to
* enable the events through which all "agent" events are funeled.
*/
-static int _cmd_enable_event(struct ltt_session *session,
+static int _cmd_enable_event(ltt_session::locked_ref& locked_session,
const struct lttng_domain *domain,
char *channel_name,
struct lttng_event *event,
{
int ret = 0, channel_created = 0;
struct lttng_channel *attr = nullptr;
+ ltt_session& session = *locked_session.get();
- LTTNG_ASSERT(session);
LTTNG_ASSERT(event);
LTTNG_ASSERT(channel_name);
* session, explicitely require that -c chan_name needs
* to be provided.
*/
- if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
+ if (session.kernel_session->has_non_default_channel && channel_name[0] == '\0') {
ret = LTTNG_ERR_NEED_CHANNEL_NAME;
goto error;
}
- kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
+ kchan = trace_kernel_get_channel_by_name(channel_name, session.kernel_session);
if (kchan == nullptr) {
attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
if (attr == nullptr) {
goto error;
}
- ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
+ ret = cmd_enable_channel_internal(locked_session, domain, attr, wpipe);
if (ret != LTTNG_OK) {
goto error;
}
}
/* Get the newly created kernel channel pointer */
- kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
+ kchan = trace_kernel_get_channel_by_name(channel_name, session.kernel_session);
if (kchan == nullptr) {
/* This sould not happen... */
ret = LTTNG_ERR_FATAL;
case LTTNG_DOMAIN_UST:
{
struct ltt_ust_channel *uchan;
- struct ltt_ust_session *usess = session->ust_session;
+ struct ltt_ust_session *usess = session.ust_session;
LTTNG_ASSERT(usess);
goto error;
}
- ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
+ ret = cmd_enable_channel_internal(locked_session, domain, attr, wpipe);
if (ret != LTTNG_OK) {
goto error;
}
struct agent *agt;
struct lttng_event uevent;
struct lttng_domain tmp_dom;
- struct ltt_ust_session *usess = session->ust_session;
+ struct ltt_ust_session *usess = session.ust_session;
LTTNG_ASSERT(usess);
}
}
- ret = cmd_enable_event_internal(session,
+ ret = cmd_enable_event_internal(locked_session,
&tmp_dom,
(char *) default_chan_name,
&uevent,
* We own filter, exclusion, and filter_expression.
*/
int cmd_enable_event(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& locked_session,
struct lttng_event *event,
char *filter_expression,
struct lttng_event_exclusion *exclusion,
* - bytecode,
* - exclusion
*/
- ret = _cmd_enable_event(cmd_ctx->session,
+ ret = _cmd_enable_event(locked_session,
&command_domain,
cmd_ctx->lsm.u.enable.channel_name,
event,
* never be made visible to clients and are immune to checks such as
* reserved names.
*/
-static int cmd_enable_event_internal(struct ltt_session *session,
+static int cmd_enable_event_internal(ltt_session::locked_ref& locked_session,
const struct lttng_domain *domain,
char *channel_name,
struct lttng_event *event,
struct lttng_event_exclusion *exclusion,
int wpipe)
{
- return _cmd_enable_event(session,
+ return _cmd_enable_event(locked_session,
domain,
channel_name,
event,
struct ltt_session *new_session = nullptr;
enum lttng_session_descriptor_status descriptor_status;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
if (home_path) {
if (*home_path != '/') {
ERR("Home path provided by client is not absolute");
/* Release the global reference on error. */
session_destroy(new_session);
}
- session_unlock_list();
+
return ret_code;
}
* Using the session list, filled a lttng_session array to send back to the
* client for session listing.
*
- * The session list lock MUST be acquired before calling this function. Use
- * session_lock_list() and session_unlock_list().
+ * The session list lock MUST be acquired before calling this function.
*/
void cmd_list_lttng_sessions(struct lttng_session *sessions,
size_t session_count,
trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
switch (trigger_domain) {
case LTTNG_DOMAIN_KERNEL:
{
ret_code);
}
- goto end_unlock_session_list;
+ return ret_code;
}
break;
}
agt = agent_create(trigger_domain);
if (!agt) {
ret_code = LTTNG_ERR_NOMEM;
- goto end_unlock_session_list;
+ return ret_code;
}
agent_add(agt, the_trigger_agents_ht_by_domain);
ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
if (ret_code != LTTNG_OK) {
- goto end_unlock_session_list;
+ return ret_code;
}
break;
abort();
}
- ret_code = LTTNG_OK;
-end_unlock_session_list:
- session_unlock_list();
- return ret_code;
+ return LTTNG_OK;
}
-enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
- struct lttng_trigger *trigger,
- bool is_trigger_anonymous,
- struct notification_thread_handle *notification_thread,
- struct lttng_trigger **return_trigger)
+lttng::ctl::trigger cmd_register_trigger(const struct lttng_credentials *cmd_creds,
+ struct lttng_trigger *trigger,
+ bool is_trigger_anonymous,
+ struct notification_thread_handle *notification_thread)
{
enum lttng_error_code ret_code;
const char *trigger_name;
*/
if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
if (lttng_credentials_get_uid(cmd_creds) != 0) {
- ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
- trigger_name,
- (int) trigger_owner,
- (int) lttng_credentials_get_uid(cmd_creds));
- ret_code = LTTNG_ERR_INVALID_TRIGGER;
- goto end;
+ LTTNG_THROW_CTL(
+ fmt::format(
+ "Trigger credentials do not match the command credentials: trigger_name = `{}`, trigger_owner_uid={}, command_creds_uid={}",
+ trigger_name,
+ trigger_owner,
+ lttng_credentials_get_uid(cmd_creds)),
+ LTTNG_ERR_INVALID_TRIGGER);
}
}
*/
ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
if (ret_code != LTTNG_OK) {
- ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
- trigger_name,
- (int) trigger_owner,
- ret_code);
- goto end;
+ LTTNG_THROW_CTL(
+ fmt::format(
+ "Failed to generate bytecode of trigger: trigger_name=`{}`, trigger_owner_uid={}",
+ trigger_name,
+ trigger_owner),
+ ret_code);
}
/*
ret_code = notification_thread_command_register_trigger(
notification_thread, trigger, is_trigger_anonymous);
if (ret_code != LTTNG_OK) {
- DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
- trigger_name,
- (int) trigger_owner,
- ret_code);
- goto end;
+ LTTNG_THROW_CTL(
+ fmt::format(
+ "Failed to register trigger to notification thread: trigger_name=`{}`, trigger_owner_uid={}",
+ trigger_name,
+ trigger_owner),
+ ret_code);
}
trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
ret_code = synchronize_tracer_notifier_register(
notification_thread, trigger, cmd_creds);
if (ret_code != LTTNG_OK) {
- ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
- goto end;
+ LTTNG_THROW_CTL("Failed to register tracer notifier", ret_code);
}
}
* reference to the trigger so the caller doesn't have to care if those
* are distinct instances or not.
*/
- if (ret_code == LTTNG_OK) {
- lttng_trigger_get(trigger);
- *return_trigger = trigger;
- /* Ownership of trigger was transferred to caller. */
- trigger = nullptr;
- }
-end:
- return ret_code;
+ LTTNG_ASSERT(ret_code == LTTNG_OK);
+ lttng_trigger_get(trigger);
+ return lttng::ctl::trigger(trigger);
}
static enum lttng_error_code
LTTNG_ASSERT(lttng_condition_get_type(condition) ==
LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
switch (trigger_domain) {
case LTTNG_DOMAIN_KERNEL:
ret_code = kernel_unregister_event_notifier(trigger);
if (ret_code != LTTNG_OK) {
- goto end_unlock_session_list;
+ return ret_code;
}
break;
LTTNG_ASSERT(agt);
ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
if (ret_code != LTTNG_OK) {
- goto end_unlock_session_list;
+ return ret_code;
}
break;
abort();
}
- ret_code = LTTNG_OK;
-
-end_unlock_session_list:
- session_unlock_list();
- return ret_code;
+ return LTTNG_OK;
}
enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
#define CMD_H
#include "context.hpp"
+#include "ctl-utils.hpp"
#include "lttng-sessiond.hpp"
#include "lttng/tracker.h"
#include "session.hpp"
int cmd_disable_channel(struct ltt_session *session,
enum lttng_domain_type domain,
char *channel_name);
-int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe);
+int cmd_enable_channel(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& session,
+ int sock,
+ int wpipe);
/* Process attribute tracker commands */
enum lttng_error_code
/* Event commands */
int cmd_disable_event(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& locked_session,
struct lttng_event *event,
char *filter_expression,
struct lttng_bytecode *filter,
struct lttng_event_exclusion *exclusion);
int cmd_add_context(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& locked_session,
const struct lttng_event_context *event_context,
int kwpipe);
int cmd_set_filter(struct ltt_session *session,
struct lttng_event *event,
struct lttng_bytecode *bytecode);
int cmd_enable_event(struct command_ctx *cmd_ctx,
+ ltt_session::locked_ref& session,
struct lttng_event *event,
char *filter_expression,
struct lttng_event_exclusion *exclusion,
int cmd_regenerate_metadata(struct ltt_session *session);
int cmd_regenerate_statedump(struct ltt_session *session);
-enum lttng_error_code
+lttng::ctl::trigger
cmd_register_trigger(const struct lttng_credentials *cmd_creds,
struct lttng_trigger *trigger,
bool is_anonymous_trigger,
- struct notification_thread_handle *notification_thread_handle,
- struct lttng_trigger **return_trigger);
+ struct notification_thread_handle *notification_thread_handle);
enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
const struct lttng_trigger *trigger,
--- /dev/null
+/*
+ * Copyright (C) 2024 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ *
+ */
+
+#ifndef LTTNG_SESSIOND_CTL_UTILS_H
+#define LTTNG_SESSIOND_CTL_UTILS_H
+
+#include <common/make-unique-wrapper.hpp>
+
+#include <lttng/lttng.h>
+
+namespace lttng {
+namespace ctl {
+/*
+ * The 'session_descriptor' alias, based on unique_ptr, manages lttng_session_descriptor resources
+ * with automatic cleanup.
+ */
+using session_descriptor = std::unique_ptr<
+ lttng_session_descriptor,
+ lttng::memory::create_deleter_class<lttng_session_descriptor,
+ lttng_session_descriptor_destroy>::deleter>;
+
+/*
+ * The 'trigger' alias, based on unique_ptr, manages lttng_trigger resources
+ * with automatic cleanup.
+ */
+using trigger = std::unique_ptr<
+ lttng_trigger,
+ lttng::memory::create_deleter_class<lttng_trigger, lttng_trigger_put>::deleter>;
+
+} /* namespace ctl */
+} /* namespace lttng */
+
+#endif /* LTTNG_SESSIOND_CTL_UTILS_H */
* registration done message, no thread can see the application
* and change its state.
*/
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
lttng::urcu::read_lock_guard read_lock;
/*
ret = send_socket_to_thread(
notifiers->apps_cmd_notify_pipe_write_fd, app->notify_sock);
if (ret < 0) {
- session_unlock_list();
/*
* No notify thread, stop the UST tracing. However, this is
* not an internal error of the this thread thus setting
ret = send_socket_to_thread(notifiers->apps_cmd_pipe_write_fd,
app->sock);
if (ret < 0) {
- session_unlock_list();
/*
* No apps. thread, stop the UST tracing. However, this is
* not an internal error of the this thread thus setting
goto error;
}
- session_unlock_list();
}
} while (node != nullptr);
struct lttcomm_consumer_msg lkm;
struct consumer_output *consumer;
enum lttng_error_code status;
- struct ltt_session *session = nullptr;
struct lttng_channel_extended *channel_attr_extended;
bool is_local_trace;
size_t consumer_path_offset = 0;
lttng::urcu::read_lock_guard read_lock;
+ ltt_session::ref session;
/* Safety net */
LTTNG_ASSERT(channel);
}
health_code_update();
- session = session_find_by_id(ksession->id);
- LTTNG_ASSERT(session);
- ASSERT_LOCKED(session->lock);
+
+ try {
+ session = ltt_session::find_session(ksession->id);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ ERR_FMT("Fatal error during the creation of a kernel channel: {}, location='{}'",
+ ex.what(),
+ ex.source_location);
+ abort();
+ }
+
+ ASSERT_LOCKED(session->_lock);
ASSERT_SESSION_LIST_LOCKED();
status = notification_thread_command_add_channel(the_notification_thread_handle,
channel->published_to_notification_thread = true;
error:
- if (session) {
- session_put(session);
- }
free(pathname);
return ret;
}
*/
struct command_ctx {
unsigned int lttng_msg_size;
- struct ltt_session *session;
/* Input message */
struct lttcomm_session_msg lsm;
/* Reply content, starts with an lttcomm_lttng_msg header. */
struct ltt_session *session, *tmp;
struct ltt_session_list *session_list;
- session_list = session_get_list();
DBG("Initiating destruction of all sessions");
+ auto list_lock = lttng::sessiond::lock_session_list();
+
+ session_list = session_get_list();
if (!session_list) {
return;
}
- session_lock_list();
/* Initiate the destruction of all sessions. */
cds_list_for_each_entry_safe (session, tmp, &session_list->head, list) {
if (!session_get(session)) {
session_unlock(session);
session_put(session);
}
- session_unlock_list();
/* Wait for the destruction of all sessions to complete. */
DBG("Waiting for the destruction of all sessions to complete");
- session_list_wait_empty();
+ session_list_wait_empty(std::move(list_lock));
DBG("Destruction of all sessions completed");
}
int ret;
struct ltt_kernel_channel *channel;
struct ltt_session *session;
- const struct ltt_session_list *session_list = session_get_list();
DBG("Updating kernel poll set");
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ const struct ltt_session_list *session_list = session_get_list();
+
cds_list_for_each_entry (session, &session_list->head, list) {
if (!session_get(session)) {
continue;
if (ret < 0) {
session_unlock(session);
session_put(session);
- goto error;
+ return -1;
}
DBG("Channel fd %d added to kernel set", channel->fd);
}
session_unlock(session);
session_put(session);
}
- session_unlock_list();
return 0;
-
-error:
- session_unlock_list();
- return -1;
}
/*
struct ltt_session *session;
struct ltt_kernel_session *ksess;
struct ltt_kernel_channel *channel;
- const struct ltt_session_list *session_list = session_get_list();
DBG("Updating kernel streams for channel fd %d", fd);
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ const struct ltt_session_list *session_list = session_get_list();
+
cds_list_for_each_entry (session, &session_list->head, list) {
if (!session_get(session)) {
continue;
session_unlock(session);
session_put(session);
}
- session_unlock_list();
+
return ret;
error:
session_unlock(session);
session_put(session);
- session_unlock_list();
return ret;
}
DBG("Launching scheduled time-based rotation on session \"%s\"", session.name);
ASSERT_SESSION_LIST_LOCKED();
- ASSERT_LOCKED(session.lock);
+ ASSERT_LOCKED(session._lock);
ret = cmd_rotate_session(&session,
&rotation_return,
cds_list_del(&job->head);
}
- session_lock_list();
- const auto unlock_list =
- lttng::make_scope_exit([]() noexcept { session_unlock_list(); });
+ const auto list_lock = lttng::sessiond::lock_session_list();
/* locked_ref will unlock the session and release the ref held by the job. */
session_lock(job->session);
condition_session_name,
consumed);
- session_lock_list();
- const auto unlock_list = lttng::make_scope_exit([]() noexcept { session_unlock_list(); });
-
- ltt_session::locked_ref session{ [&condition_session_name]() {
- auto raw_session_ptr = session_find_by_name(condition_session_name);
-
- if (raw_session_ptr) {
- session_lock(raw_session_ptr);
- }
-
- return raw_session_ptr;
- }() };
- if (!session) {
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::locked_ref session;
+ try {
+ session = ltt_session::find_locked_session(condition_session_name);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
DBG_FMT("Failed to find session while handling notification: notification_type={}, session name=`{}`",
lttng_condition_type_str(condition_type),
condition_session_name);
.gid = LTTNG_OPTIONAL_INIT_VALUE(session.gid),
};
- ASSERT_LOCKED(session.lock);
+ ASSERT_LOCKED(session._lock);
auto rotate_condition = lttng::make_unique_wrapper<lttng_condition, lttng_condition_put>(
lttng_condition_session_consumed_size_create());
{
int ret;
const char *session_name;
- struct ltt_session *session;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
session_name = lttng_save_session_attr_get_session_name(attr);
if (session_name) {
- session = session_find_by_name(session_name);
- if (!session) {
+
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ ltt_session::locked_ref session;
+
+ try {
+ session = ltt_session::find_locked_session(session_name);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ WARN_FMT("Failed to save session: {} {}", ex.what(), ex.source_location);
ret = LTTNG_ERR_SESS_NOT_FOUND;
- goto end;
+ return ret;
}
- session_lock(session);
- ret = save_session(session, attr, creds);
- session_unlock(session);
- session_put(session);
+ ret = save_session(session.get(), attr, creds);
if (ret != LTTNG_OK) {
- goto end;
+ return ret;
}
} else {
struct ltt_session_list *list = session_get_list();
+ struct ltt_session *session;
cds_list_for_each_entry (session, &list->head, list) {
if (!session_get(session)) {
session_put(session);
/* Don't abort if we don't have the required permissions. */
if (ret != LTTNG_OK && ret != LTTNG_ERR_EPERM) {
- goto end;
+ return ret;
}
}
}
- ret = LTTNG_OK;
-end:
- session_unlock_list();
- return ret;
+ return LTTNG_OK;
}
* Please see session.h for more explanation and correct usage of the list.
*/
struct ltt_session_list the_session_list;
+
+/*
+ * Return a ltt_session structure ptr that matches name. If no session found,
+ * NULL is returned. This must be called with the session list lock held using
+ * session_lock_list and session_unlock_list.
+ * A reference to the session is implicitly acquired by this function.
+ */
+struct ltt_session *session_find_by_name(const char *name)
+{
+ struct ltt_session *iter;
+
+ LTTNG_ASSERT(name);
+ ASSERT_SESSION_LIST_LOCKED();
+
+ DBG2("Trying to find session by name %s", name);
+
+ cds_list_for_each_entry (iter, &the_session_list.head, list) {
+ if (!strncmp(iter->name, name, NAME_MAX) && !iter->destroyed) {
+ goto found;
+ }
+ }
+
+ return nullptr;
+found:
+ return session_get(iter) ? iter : nullptr;
+}
+
+/*
+ * Return an ltt_session that matches the id. If no session is found,
+ * NULL is returned. This must be called with rcu_read_lock and
+ * session list lock held (to guarantee the lifetime of the session).
+ */
+struct ltt_session *session_find_by_id(uint64_t id)
+{
+ struct lttng_ht_node_u64 *node;
+ struct lttng_ht_iter iter;
+ struct ltt_session *ls;
+
+ ASSERT_RCU_READ_LOCKED();
+ ASSERT_SESSION_LIST_LOCKED();
+
+ if (!ltt_sessions_ht_by_id) {
+ goto end;
+ }
+
+ lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
+ node = lttng_ht_iter_get_node_u64(&iter);
+ if (node == nullptr) {
+ goto end;
+ }
+ ls = lttng::utils::container_of(node, <t_session::node);
+
+ DBG3("Session %" PRIu64 " found by id.", id);
+ return session_get(ls) ? ls : nullptr;
+
+end:
+ DBG3("Session %" PRIu64 " NOT found by id", id);
+ return nullptr;
+}
} /* namespace */
/*
/*
* Returns once the session list is empty.
*/
-void session_list_wait_empty()
+void session_list_wait_empty(std::unique_lock<std::mutex> list_lock)
{
- std::unique_lock<std::mutex> list_lock(the_session_list.lock);
-
/* Keep waiting until the session list is empty. */
the_session_list.removal_cond.wait(list_lock,
[] { return cds_list_empty(&the_session_list.head); });
}
-/*
- * Acquire session list lock
- */
-void session_lock_list() noexcept
-{
- the_session_list.lock.lock();
-}
-
/*
* Try to acquire session list lock
*/
return the_session_list.lock.try_lock() ? 0 : 1;
}
-/*
- * Release session list lock
- */
-void session_unlock_list() noexcept
-{
- the_session_list.lock.unlock();
-}
-
/*
* Get the session's consumer destination type.
*
void session_lock(struct ltt_session *session)
{
LTTNG_ASSERT(session);
+ session->lock();
+}
- pthread_mutex_lock(&session->lock);
+void ltt_session::lock() const noexcept
+{
+ pthread_mutex_lock(&_lock);
+}
+
+void ltt_session::unlock() const noexcept
+{
+ ltt_session::_const_session_unlock(*this);
}
/*
void session_unlock(struct ltt_session *session)
{
LTTNG_ASSERT(session);
+ session->unlock();
+}
- pthread_mutex_unlock(&session->lock);
+void ltt_session::_const_session_unlock(const ltt_session &session)
+{
+ pthread_mutex_unlock(&session._lock);
}
static int _session_set_trace_chunk_no_lock_check(struct ltt_session *session,
struct lttng_trace_chunk *new_trace_chunk,
struct lttng_trace_chunk **current_trace_chunk)
{
- ASSERT_LOCKED(session->lock);
+ ASSERT_LOCKED(session->_lock);
return _session_set_trace_chunk_no_lock_check(
session, new_trace_chunk, current_trace_chunk);
}
int ret;
struct ltt_ust_session *usess;
struct ltt_kernel_session *ksess;
- struct ltt_session *session = lttng::utils::container_of(ref, <t_session::ref);
+ struct ltt_session *session = lttng::utils::container_of(ref, <t_session::ref_count);
const bool session_published = session->published;
LTTNG_ASSERT(!session->chunk_being_archived);
snapshot_destroy(&session->snapshot);
- pthread_mutex_destroy(&session->lock);
+ pthread_mutex_destroy(&session->_lock);
if (session_published) {
ASSERT_SESSION_LIST_LOCKED();
*/
bool session_get(struct ltt_session *session)
{
- return urcu_ref_get_unless_zero(&session->ref);
+ return urcu_ref_get_unless_zero(&session->ref_count);
}
/*
* may cause the removal of the session from the session_list.
*/
ASSERT_SESSION_LIST_LOCKED();
- LTTNG_ASSERT(session->ref.refcount);
- urcu_ref_put(&session->ref, session_release);
+ LTTNG_ASSERT(session->ref_count.refcount);
+ urcu_ref_put(&session->ref_count, session_release);
}
/*
return lttng_dynamic_array_add_element(&session->clear_notifiers, &element);
}
-/*
- * Return a ltt_session structure ptr that matches name. If no session found,
- * NULL is returned. This must be called with the session list lock held using
- * session_lock_list and session_unlock_list.
- * A reference to the session is implicitly acquired by this function.
- */
-struct ltt_session *session_find_by_name(const char *name)
-{
- struct ltt_session *iter;
-
- LTTNG_ASSERT(name);
- ASSERT_SESSION_LIST_LOCKED();
-
- DBG2("Trying to find session by name %s", name);
-
- cds_list_for_each_entry (iter, &the_session_list.head, list) {
- if (!strncmp(iter->name, name, NAME_MAX) && !iter->destroyed) {
- goto found;
- }
- }
-
- return nullptr;
-found:
- return session_get(iter) ? iter : nullptr;
-}
-
-/*
- * Return an ltt_session that matches the id. If no session is found,
- * NULL is returned. This must be called with rcu_read_lock and
- * session list lock held (to guarantee the lifetime of the session).
- */
-struct ltt_session *session_find_by_id(uint64_t id)
-{
- struct lttng_ht_node_u64 *node;
- struct lttng_ht_iter iter;
- struct ltt_session *ls;
-
- ASSERT_RCU_READ_LOCKED();
- ASSERT_SESSION_LIST_LOCKED();
-
- if (!ltt_sessions_ht_by_id) {
- goto end;
- }
-
- lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
- node = lttng_ht_iter_get_node_u64(&iter);
- if (node == nullptr) {
- goto end;
- }
- ls = lttng::utils::container_of(node, <t_session::node);
-
- DBG3("Session %" PRIu64 " found by id.", id);
- return session_get(ls) ? ls : nullptr;
-
-end:
- DBG3("Session %" PRIu64 " NOT found by id", id);
- return nullptr;
-}
-
/*
* Create a new session and add it to the session list.
* Session list lock must be held by the caller.
lttng_dynamic_array_init(&new_session->clear_notifiers,
sizeof(struct ltt_session_clear_notifier_element),
nullptr);
- urcu_ref_init(&new_session->ref);
- pthread_mutex_init(&new_session->lock, nullptr);
+ urcu_ref_init(&new_session->ref_count);
+ pthread_mutex_init(&new_session->_lock, nullptr);
new_session->creation_time = time(nullptr);
if (new_session->creation_time == (time_t) -1) {
int ret = 0;
ASSERT_SESSION_LIST_LOCKED();
- ASSERT_LOCKED(session.lock);
+ ASSERT_LOCKED(session._lock);
session.rotation_state = result;
if (session.rotation_pending_check_timer_enabled) {
return found;
}
-void ls::details::locked_session_release(ltt_session *session)
+void ltt_session::_locked_session_release(ltt_session *session)
{
if (!session) {
return;
session_put(session);
}
-ltt_session::locked_ref ls::find_locked_session_by_id(ltt_session::id_t id)
+void ltt_session::_locked_const_session_release(const ltt_session *session)
+{
+ if (!session) {
+ return;
+ }
+
+ ltt_session::_const_session_unlock(*session);
+ ltt_session::_const_session_put(session);
+}
+
+ltt_session::locked_ref ltt_session::find_locked_session(ltt_session::id_t id)
{
lttng::urcu::read_lock_guard rcu_lock;
auto session = session_find_by_id(id);
if (!session) {
- return nullptr;
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
}
/*
return ltt_session::locked_ref(session);
}
-ltt_session::sptr ls::find_session_by_id(ltt_session::id_t id)
+ltt_session::locked_ref ltt_session::find_locked_session(lttng::c_string_view name)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_name(name.data());
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
+ }
+
+ session_lock(session);
+ return ltt_session::locked_ref(session);
+}
+
+ltt_session::const_locked_ref ltt_session::find_locked_const_session(ltt_session::id_t id)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_id(id);
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
+ }
+
+ session_lock(session);
+ return ltt_session::const_locked_ref(session);
+}
+
+ltt_session::const_locked_ref ltt_session::find_locked_const_session(lttng::c_string_view name)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_name(name.data());
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
+ }
+
+ session_lock(session);
+ return ltt_session::const_locked_ref(session);
+}
+
+ltt_session::ref ltt_session::find_session(ltt_session::id_t id)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_id(id);
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
+ }
+
+ return ltt_session::ref(session);
+}
+
+ltt_session::ref ltt_session::find_session(lttng::c_string_view name)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_name(name.data());
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
+ }
+
+ return ltt_session::ref(session);
+}
+
+ltt_session::const_ref ltt_session::find_const_session(ltt_session::id_t id)
{
lttng::urcu::read_lock_guard rcu_lock;
auto session = session_find_by_id(id);
if (!session) {
- return nullptr;
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
+ }
+
+ return ltt_session::const_ref(session);
+}
+
+ltt_session::const_ref ltt_session::find_const_session(lttng::c_string_view name)
+{
+ lttng::urcu::read_lock_guard rcu_lock;
+ auto session = session_find_by_name(name.data());
+
+ if (!session) {
+ LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
}
- return { session, session_put };
+ return ltt_session::const_ref(session);
+}
+
+void ltt_session::_const_session_put(const ltt_session *session)
+{
+ /*
+ * The session list lock must be held as any session_put()
+ * may cause the removal of the session from the session_list.
+ */
+ ASSERT_SESSION_LIST_LOCKED();
+ LTTNG_ASSERT(session->ref_count.refcount);
+ urcu_ref_put(&session->ref_count, session_release);
+}
+
+std::unique_lock<std::mutex> ls::lock_session_list()
+{
+ return std::unique_lock<std::mutex>(the_session_list.lock);
}
#include "trace-kernel.hpp"
#include <common/dynamic-array.hpp>
+#include <common/exception.hpp>
#include <common/hashtable/hashtable.hpp>
#include <common/make-unique-wrapper.hpp>
#include <common/pthread-lock.hpp>
using ltt_session_destroy_notifier = void (*)(const struct ltt_session *, void *);
using ltt_session_clear_notifier = void (*)(const struct ltt_session *, void *);
-namespace lttng {
-namespace sessiond {
-namespace details {
-void locked_session_release(ltt_session *session);
-} /* namespace details */
-} /* namespace sessiond */
-} /* namespace lttng */
+struct ltt_session;
+struct ltt_session_list;
+
+enum lttng_error_code
+session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
+void session_lock(struct ltt_session *session);
+void session_unlock(struct ltt_session *session);
+
+bool session_get(struct ltt_session *session);
+void session_put(struct ltt_session *session);
+
+/*
+ * The session list lock covers more ground than its name implies. While
+ * it does protect against concurent mutations of the session list, it is
+ * also used as a multi-session lock when synchronizing newly-registered
+ * 'user space tracer' and 'agent' applications.
+ *
+ * In other words, it prevents tracer configurations from changing while they
+ * are being transmitted to the various applications.
+ */
+int session_trylock_list() noexcept;
+
+#define LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(session_name) \
+ throw lttng::sessiond::exceptions::session_not_found_error(session_name, \
+ LTTNG_SOURCE_LOCATION())
+#define LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id) \
+ throw lttng::sessiond::exceptions::session_not_found_error(id, LTTNG_SOURCE_LOCATION())
+
+void session_destroy(struct ltt_session *session);
+int session_add_destroy_notifier(struct ltt_session *session,
+ ltt_session_destroy_notifier notifier,
+ void *user_data);
+
+int session_add_clear_notifier(struct ltt_session *session,
+ ltt_session_clear_notifier notifier,
+ void *user_data);
+void session_notify_clear(ltt_session& session);
+
+enum consumer_dst_type session_get_consumer_destination_type(const struct ltt_session *session);
+const char *session_get_net_consumer_hostname(const struct ltt_session *session);
+void session_get_net_consumer_ports(const struct ltt_session *session,
+ uint16_t *control_port,
+ uint16_t *data_port);
+struct lttng_trace_archive_location *
+session_get_trace_archive_location(const struct ltt_session *session);
+
+struct ltt_session_list *session_get_list();
+void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
+
+bool session_access_ok(struct ltt_session *session, uid_t uid);
+
+int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result);
+
+/* Create a new trace chunk object from the session's configuration. */
+struct lttng_trace_chunk *
+session_create_new_trace_chunk(const struct ltt_session *session,
+ const struct consumer_output *consumer_output_override,
+ const char *session_base_path_override,
+ const char *chunk_name_override);
+
+/*
+ * Set `new_trace_chunk` as the session's current trace chunk. A reference
+ * to `new_trace_chunk` is acquired by the session. The chunk is created
+ * on remote peers (consumer and relay daemons).
+ *
+ * A reference to the session's current trace chunk is returned through
+ * `current_session_trace_chunk` on success.
+ */
+int session_set_trace_chunk(struct ltt_session *session,
+ struct lttng_trace_chunk *new_trace_chunk,
+ struct lttng_trace_chunk **current_session_trace_chunk);
+
+/*
+ * Close a chunk on the remote peers of a session. Has no effect on the
+ * ltt_session itself.
+ */
+int session_close_trace_chunk(struct ltt_session *session,
+ struct lttng_trace_chunk *trace_chunk,
+ enum lttng_trace_chunk_command_type close_command,
+ char *path);
+
+/* Open a packet in all channels of a given session. */
+enum lttng_error_code session_open_packets(struct ltt_session *session);
+
+bool session_output_supports_trace_chunks(const struct ltt_session *session);
+
+/*
+ * Sample the id of a session looked up via its name.
+ * Here the term "sampling" hint the caller that this return the id at a given
+ * point in time with no guarantee that the session for which the id was
+ * sampled still exist at that point.
+ *
+ * Return 0 when the session is not found,
+ * Return 1 when the session is found and set `id`.
+ */
+bool sample_session_id_by_name(const char *name, uint64_t *id);
/*
* Tracing session list
*/
struct ltt_session {
using id_t = uint64_t;
+
+private:
+ static void _locked_session_release(ltt_session *session);
+ static void _locked_const_session_release(const ltt_session *session);
+ static void _const_session_put(const ltt_session *session);
+ static void _const_session_unlock(const ltt_session &session);
+
+public:
using locked_ref =
std::unique_ptr<ltt_session,
lttng::memory::create_deleter_class<
ltt_session,
- lttng::sessiond::details::locked_session_release>::deleter>;
- using sptr = std::shared_ptr<ltt_session>;
+ ltt_session::_locked_session_release>::deleter>;
+ using ref = std::unique_ptr<
+ ltt_session,
+ lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
+ using const_locked_ref =
+ std::unique_ptr<const ltt_session,
+ lttng::memory::create_deleter_class<
+ const ltt_session,
+ ltt_session::_locked_const_session_release>::deleter>;
+ using const_ref = std::unique_ptr<
+ const ltt_session,
+ lttng::memory::create_deleter_class<const ltt_session, ltt_session::_const_session_put>::deleter>;
+
+ void lock() const noexcept;
+ void unlock() const noexcept;
+
+ /*
+ * Session list lock must be acquired by the caller.
+ *
+ * The caller must not keep the ownership of the returned locked session
+ * for longer than strictly necessary. If your intention is to acquire
+ * a reference to an ltt_session, see `find_session()`.
+ */
+ static locked_ref find_locked_session(ltt_session::id_t id);
+ static locked_ref find_locked_session(lttng::c_string_view name);
+ static const_locked_ref find_locked_const_session(ltt_session::id_t id);
+ static const_locked_ref find_locked_const_session(lttng::c_string_view name);
+
+ static ref find_session(ltt_session::id_t id);
+ static ref find_session(lttng::c_string_view name);
+ static const_ref find_const_session(ltt_session::id_t id);
+ static const_ref find_const_session(lttng::c_string_view name);
char name[NAME_MAX];
bool has_auto_generated_name;
time_t creation_time;
struct ltt_kernel_session *kernel_session;
struct ltt_ust_session *ust_session;
- struct urcu_ref ref;
+ mutable struct urcu_ref ref_count;
/*
* Protect any read/write on this session data structure. This lock must be
* acquired *before* using any public functions declared below. Use
* session_lock() and session_unlock() for that.
*/
- pthread_mutex_t lock;
+ mutable pthread_mutex_t _lock;
struct cds_list_head list;
/* session unique identifier */
id_t id;
struct lttng_dynamic_array clear_notifiers;
/* Session base path override. Set non-null. */
char *base_path;
-};
-
-enum lttng_error_code
-session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
-void session_lock(struct ltt_session *session);
-void session_unlock(struct ltt_session *session);
-
-/*
- * The session list lock covers more ground than its name implies. While
- * it does protect against concurent mutations of the session list, it is
- * also used as a multi-session lock when synchronizing newly-registered
- * 'user space tracer' and 'agent' applications.
- *
- * In other words, it prevents tracer configurations from changing while they
- * are being transmitted to the various applications.
- */
-void session_lock_list() noexcept;
-int session_trylock_list() noexcept;
-void session_unlock_list() noexcept;
-
-void session_destroy(struct ltt_session *session);
-int session_add_destroy_notifier(struct ltt_session *session,
- ltt_session_destroy_notifier notifier,
- void *user_data);
-
-int session_add_clear_notifier(struct ltt_session *session,
- ltt_session_clear_notifier notifier,
- void *user_data);
-void session_notify_clear(ltt_session& session);
-
-bool session_get(struct ltt_session *session);
-void session_put(struct ltt_session *session);
-
-enum consumer_dst_type session_get_consumer_destination_type(const struct ltt_session *session);
-const char *session_get_net_consumer_hostname(const struct ltt_session *session);
-void session_get_net_consumer_ports(const struct ltt_session *session,
- uint16_t *control_port,
- uint16_t *data_port);
-struct lttng_trace_archive_location *
-session_get_trace_archive_location(const struct ltt_session *session);
-
-struct ltt_session *session_find_by_name(const char *name);
-struct ltt_session *session_find_by_id(ltt_session::id_t id);
-
-struct ltt_session_list *session_get_list();
-void session_list_wait_empty();
-
-bool session_access_ok(struct ltt_session *session, uid_t uid);
-
-int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result);
-/* Create a new trace chunk object from the session's configuration. */
-struct lttng_trace_chunk *
-session_create_new_trace_chunk(const struct ltt_session *session,
- const struct consumer_output *consumer_output_override,
- const char *session_base_path_override,
- const char *chunk_name_override);
-
-/*
- * Set `new_trace_chunk` as the session's current trace chunk. A reference
- * to `new_trace_chunk` is acquired by the session. The chunk is created
- * on remote peers (consumer and relay daemons).
- *
- * A reference to the session's current trace chunk is returned through
- * `current_session_trace_chunk` on success.
- */
-int session_set_trace_chunk(struct ltt_session *session,
- struct lttng_trace_chunk *new_trace_chunk,
- struct lttng_trace_chunk **current_session_trace_chunk);
-
-/*
- * Close a chunk on the remote peers of a session. Has no effect on the
- * ltt_session itself.
- */
-int session_close_trace_chunk(struct ltt_session *session,
- struct lttng_trace_chunk *trace_chunk,
- enum lttng_trace_chunk_command_type close_command,
- char *path);
-
-/* Open a packet in all channels of a given session. */
-enum lttng_error_code session_open_packets(struct ltt_session *session);
-bool session_output_supports_trace_chunks(const struct ltt_session *session);
-
-/*
- * Sample the id of a session looked up via its name.
- * Here the term "sampling" hint the caller that this return the id at a given
- * point in time with no guarantee that the session for which the id was
- * sampled still exist at that point.
- *
- * Return 0 when the session is not found,
- * Return 1 when the session is found and set `id`.
- */
-bool sample_session_id_by_name(const char *name, uint64_t *id);
+};
namespace lttng {
namespace sessiond {
-/*
- * Session list lock must be acquired by the caller.
- * The caller must not keep the ownership of the returned locked session
- * for longer than strictly necessary. If your intention is to acquire
- * a reference to an ltt_session, see `find_session_by_id()`.
- */
-ltt_session::locked_ref find_locked_session_by_id(ltt_session::id_t id);
-
-ltt_session::sptr find_session_by_id(ltt_session::id_t id);
+std::unique_lock<std::mutex> lock_session_list();
+namespace exceptions {
+/**
+ * @class session_not_found_error
+ * @brief Represents a session-not-found error and provides the parameters used to query the session
+ * for use by error-reporting code.
+ */
+class session_not_found_error : public lttng::runtime_error {
+public:
+ class query_parameter {
+ public:
+ enum class query_type : std::uint8_t { BY_NAME, BY_ID };
+
+ /*
+ * Intentionally not explicit to allow construction from c-style strings,
+ * std::string, and lttng::c_string_view.
+ *
+ * NOLINTBEGIN(google-explicit-constructor)
+ */
+ query_parameter(const std::string& session_name) :
+ type(query_type::BY_NAME), parameter(session_name)
+ {
+ }
+ /* NOLINTEND(google-explicit-constructor) */
+
+ explicit query_parameter(ltt_session::id_t id_) : type(query_type::BY_ID), parameter(id_)
+ {
+ }
+
+ query_parameter(const query_parameter& other) : type(other.type)
+ {
+ if (type == query_type::BY_NAME) {
+ new (¶meter.name) std::string(other.parameter.name);
+ } else {
+ parameter.id = other.parameter.id;
+ }
+ }
+
+ ~query_parameter()
+ {
+ if (type == query_type::BY_NAME) {
+ parameter.name.~basic_string();
+ }
+ }
+
+ query_type type;
+ union parameter {
+ explicit parameter(std::string name_) : name(std::move(name_))
+ {
+ }
+
+ explicit parameter(ltt_session::id_t id_) : id(id_)
+ {
+ }
+
+ /*
+ * parameter doesn't have enough information to do this safely; it it
+ * delegated to its parent which uses placement new.
+ */
+ parameter()
+ {
+ }
+
+ parameter(const parameter&) = delete;
+ parameter(const parameter&&) = delete;
+ parameter& operator=(parameter&) = delete;
+ parameter& operator=(parameter&&) = delete;
+
+ ~parameter()
+ {
+ }
+
+ std::string name;
+ ltt_session::id_t id;
+ } parameter;
+ };
+
+ session_not_found_error(const std::string& session_name,
+ const lttng::source_location& source_location_) :
+ lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
+ source_location_),
+ query_parameter(session_name)
+ {
+ }
+
+ session_not_found_error(ltt_session::id_t session_id,
+ const lttng::source_location& source_location_) :
+ lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
+ source_location_),
+ query_parameter(session_id)
+ {
+ }
+
+ session_not_found_error(const session_not_found_error& other) = default;
+ ~session_not_found_error() noexcept override = default;
+
+ query_parameter query_parameter;
+};
+} // namespace exceptions
} /* namespace sessiond */
} /* namespace lttng */
static void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
{
uint64_t discarded = 0, lost = 0;
- struct ltt_session *session;
struct ltt_ust_channel *uchan;
if (ua_chan->attr.type != LTTNG_UST_ABI_CHAN_PER_CPU) {
}
lttng::urcu::read_lock_guard read_lock;
- session = session_find_by_id(ua_chan->session->tracing_id);
+
+ ltt_session::ref session;
+ try {
+ session = ltt_session::find_session(ua_chan->session->tracing_id);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ DBG_FMT("Failed to save per-pid lost/discarded counters: {}, location='{}'",
+ ex.what(),
+ ex.source_location);
+ }
+
if (!session || !session->ust_session) {
/*
* Not finding the session is not an error because there are
* events is done exactly once. The session is then unpublished
* from the session list, resulting in this condition.
*/
- goto end;
+ return;
}
if (ua_chan->attr.overwrite) {
ua_chan->name);
if (!uchan) {
ERR("Missing UST channel to store discarded counters");
- goto end;
+ return;
}
uchan->per_pid_closed_app_discarded += discarded;
uchan->per_pid_closed_app_lost += lost;
-
-end:
- if (session) {
- session_put(session);
- }
}
/*
* The session list lock must be held during this function to guarantee
* the existence of ua_sess.
*/
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
/* Delete ust app sessions info */
sock = app->sock;
app->sock = -1;
DBG2("UST app pid %d deleted", app->pid);
free(app);
- session_unlock_list();
}
/*
int ret;
struct buffer_reg_uid *reg_uid;
struct buffer_reg_channel *buf_reg_chan;
- struct ltt_session *session = nullptr;
+ ltt_session::ref session;
enum lttng_error_code notification_ret;
LTTNG_ASSERT(app);
goto error;
}
- session = session_find_by_id(ua_sess->tracing_id);
- LTTNG_ASSERT(session);
- ASSERT_LOCKED(session->lock);
+ /* Guaranteed to exist; will not throw. */
+ session = ltt_session::find_session(ua_sess->tracing_id);
+ ASSERT_LOCKED(session->_lock);
ASSERT_SESSION_LIST_LOCKED();
/*
}
error:
- if (session) {
- session_put(session);
- }
return ret;
}
int ret;
lsu::registry_session *registry;
enum lttng_error_code cmd_ret;
- struct ltt_session *session = nullptr;
+ ltt_session::ref session;
uint64_t chan_reg_key;
LTTNG_ASSERT(app);
goto error;
}
- session = session_find_by_id(ua_sess->tracing_id);
- LTTNG_ASSERT(session);
- ASSERT_LOCKED(session->lock);
+ /* Guaranteed to exist; will not throw. */
+ session = ltt_session::find_session(ua_sess->tracing_id);
+ ASSERT_LOCKED(session->_lock);
ASSERT_SESSION_LIST_LOCKED();
/* Create and get channel on the consumer side. */
}
}
error:
- if (session) {
- session_put(session);
- }
return ret;
}
int ret = 0;
struct ust_app_channel *metadata;
struct consumer_socket *socket;
- struct ltt_session *session = nullptr;
+ ltt_session::ref session;
LTTNG_ASSERT(ua_sess);
LTTNG_ASSERT(app);
*/
locked_registry->_metadata_key = metadata->key;
- session = session_find_by_id(ua_sess->tracing_id);
- LTTNG_ASSERT(session);
- ASSERT_LOCKED(session->lock);
+ /* Guaranteed to exist; will not throw. */
+ session = ltt_session::find_session(ua_sess->tracing_id);
+ ASSERT_LOCKED(session->_lock);
ASSERT_SESSION_LIST_LOCKED();
/*
lttng_fd_put(LTTNG_FD_APPS, 1);
delete_ust_app_channel(-1, metadata, app, locked_registry);
error:
- if (session) {
- session_put(session);
- }
return ret;
}
{
/* The caller already holds the session and session list locks. */
ASSERT_SESSION_LIST_LOCKED();
- const auto session = lttng::sessiond::find_session_by_id(_tracing_id);
-
- LTTNG_ASSERT(session);
- ASSERT_LOCKED(session->lock);
+ const auto session = ltt_session::find_session(_tracing_id);
+ ASSERT_LOCKED(session->_lock);
visitor.visit(lst::environment_field<const char *>(
"trace_name",
{
}
+lttng::allocation_failure::allocation_failure(const std::string& msg,
+std::size_t allocation_size_,
+ const lttng::source_location& location) :
+ lttng::runtime_error(msg, location), allocation_size(allocation_size_)
+{
+}
+
lttng::unsupported_error::unsupported_error(const std::string& msg,
const lttng::source_location& location) :
lttng::runtime_error(msg, location)
#define LTTNG_THROW_POSIX(msg, errno_code) \
throw lttng::posix_error(msg, errno_code, LTTNG_SOURCE_LOCATION())
#define LTTNG_THROW_ERROR(msg) throw lttng::runtime_error(msg, LTTNG_SOURCE_LOCATION())
+#define LTTNG_THROW_ALLOCATION_FAILURE_ERROR(msg, allocation_size) \
+ throw lttng::allocation_failure(msg, allocation_size, LTTNG_SOURCE_LOCATION())
#define LTTNG_THROW_UNSUPPORTED_ERROR(msg) \
throw lttng::unsupported_error(msg, LTTNG_SOURCE_LOCATION())
#define LTTNG_THROW_COMMUNICATION_ERROR(msg) \
lttng::source_location source_location;
};
+/**
+ * @class allocation_failure
+ * @brief Represents an allocation failure.
+ *
+ * Thrown when an allocation fails. Differs from bad_alloc in that it offers a message and a
+ * source location.
+ */
+class allocation_failure : public lttng::runtime_error {
+public:
+ explicit allocation_failure(const std::string& msg,
+ std::size_t allocation_size,
+ const lttng::source_location& source_location);
+
+ std::size_t allocation_size;
+};
+
/**
* @class unsupported_error
* @brief Represents an error for unsupported features.
{
struct ltt_session *iter, *tmp;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
cds_list_for_each_entry_safe (iter, tmp, &session_list->head, list) {
session_destroy(iter);
}
- session_unlock_list();
/* Session list must be 0 */
LTTNG_ASSERT(!session_list_count());
enum lttng_error_code ret_code;
struct ltt_session *session = nullptr;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
ret_code = session_create(name, geteuid(), getegid(), &session);
session_put(session);
if (ret_code == LTTNG_OK) {
ret = -1;
}
- session_unlock_list();
return ret;
}
/* Fail */
ret = -1;
}
+
return ret;
}
*/
static int two_session_same_name()
{
- int ret;
- struct ltt_session *sess;
-
- ret = create_one_session(SESSION1);
+ const auto ret = create_one_session(SESSION1);
if (ret < 0) {
/* Fail */
- ret = -1;
- goto end;
+ return -1;
}
- session_lock_list();
- sess = session_find_by_name(SESSION1);
- if (sess) {
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ try {
+ const auto session = ltt_session::find_session(SESSION1);
/* Success */
- session_put(sess);
- session_unlock_list();
- ret = 0;
- goto end_unlock;
- } else {
+ return 0;
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
/* Fail */
- ret = -1;
- goto end_unlock;
+ return -1;
}
-end_unlock:
- session_unlock_list();
-end:
- return ret;
}
static void test_session_list()
static void test_validate_session()
{
- struct ltt_session *tmp;
-
- session_lock_list();
- tmp = session_find_by_name(SESSION1);
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::ref session;
+
+ try {
+ session = ltt_session::find_session(SESSION1);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ }
- ok(tmp != nullptr, "Validating session: session found");
+ ok(session, "Validating session: session found");
- if (tmp) {
- ok(tmp->kernel_session == nullptr && strlen(tmp->name),
+ if (session) {
+ ok(session->kernel_session == nullptr && strlen(session->name),
"Validating session: basic sanity check");
} else {
skip(1, "Skipping session validation check as session was not found");
- goto end;
+ return;
}
- session_lock(tmp);
- session_unlock(tmp);
- session_put(tmp);
-end:
- session_unlock_list();
+ session->lock();
+ session->unlock();
}
static void test_destroy_session()
{
- struct ltt_session *tmp;
-
- session_lock_list();
- tmp = session_find_by_name(SESSION1);
+ /*
+ * Mind the order of the declaration of list_lock vs session:
+ * the session list lock must always be released _after_ the release of
+ * a session's reference (the destruction of a ref/locked_ref) to ensure
+ * since the reference's release may unpublish the session from the list of
+ * sessions.
+ */
+ const auto list_lock = lttng::sessiond::lock_session_list();
+ ltt_session::ref session;
+
+ try {
+ session = ltt_session::find_session(SESSION1);
+ } catch (const lttng::sessiond::exceptions::session_not_found_error& ex) {
+ }
- ok(tmp != nullptr, "Destroying session: session found");
+ ok(session, "Destroying session: session found");
- if (tmp) {
- ok(destroy_one_session(tmp) == 0, "Destroying session: %s destroyed", SESSION1);
+ if (session) {
+ ok(destroy_one_session(session.release()) == 0,
+ "Destroying session: %s destroyed",
+ SESSION1);
} else {
skip(1, "Skipping session destruction as it was not found");
}
- session_unlock_list();
}
static void test_duplicate_session()
enum lttng_error_code ret_code;
const char *expected_session_name_prefix = DEFAULT_SESSION_NAME;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
+
ret_code = session_create(nullptr, geteuid(), getegid(), &session);
ok(ret_code == LTTNG_OK, "Create session with a NULL name (auto-generate a name)");
if (!session) {
DEFAULT_SESSION_NAME);
end:
session_put(session);
- session_unlock_list();
}
static void test_large_session_number()
failed = 0;
- session_lock_list();
+ const auto list_lock = lttng::sessiond::lock_session_list();
for (i = 0; i < MAX_SESSIONS; i++) {
cds_list_for_each_entry_safe (iter, tmp, &session_list->head, list) {
LTTNG_ASSERT(session_get(iter));
}
}
}
- session_unlock_list();
ok(failed == 0 && session_list_count() == 0,
"Large sessions number: destroyed %u sessions",