X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Flttng-ctl.cpp;h=15a699e24f3fd63837edbc6b11dccfa3ac5b1a10;hb=c1896e2d809e0d32d2d9b8a840755de6d5d70eaf;hp=63841df403f249d5aeb681ce81a0ec671b7d4242;hpb=8346beb68a50caf0b63edaca526250c0068f66d3;p=lttng-tools.git diff --git a/src/lib/lttng-ctl/lttng-ctl.cpp b/src/lib/lttng-ctl/lttng-ctl.cpp index 63841df40..15a699e24 100644 --- a/src/lib/lttng-ctl/lttng-ctl.cpp +++ b/src/lib/lttng-ctl/lttng-ctl.cpp @@ -3,7 +3,7 @@ * * Linux Trace Toolkit Control Library * - * Copyright (C) 2011 David Goulet + * Copyright (C) 2011 EfficiOS Inc. * Copyright (C) 2016 Jérémie Galarneau * * SPDX-License-Identifier: LGPL-2.1-only @@ -11,52 +11,54 @@ */ #define _LGPL_SOURCE -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "lttng-ctl-helper.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include #include #include -#include -#include -#include +#include +#include +#include #include #include -#include -#include -#include -#include +#include +#include +#include +#include -#include "lttng-ctl-helper.h" -#include -#include -#include +#include +#include +#include +#include +#include +#include -#define COPY_DOMAIN_PACKED(dst, src) \ -do { \ - struct lttng_domain _tmp_domain; \ - \ - lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \ - dst = _tmp_domain; \ -} while (0) +#define COPY_DOMAIN_PACKED(dst, src) \ + do { \ + struct lttng_domain _tmp_domain; \ + \ + lttng_ctl_copy_lttng_domain(&_tmp_domain, &(src)); \ + (dst) = _tmp_domain; \ + } while (0) /* Socket to session daemon for communication */ static int sessiond_socket = -1; @@ -82,8 +84,7 @@ LTTNG_EXPORT int lttng_opt_mi; * * If domain is unknown, default domain will be the kernel. */ -void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, - struct lttng_domain *src) +void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src) { if (src && dst) { switch (src->type) { @@ -116,11 +117,12 @@ static int send_session_msg(struct lttcomm_session_msg *lsm) goto end; } - DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str((lttcomm_sessiond_command) lsm->cmd_type), - lsm->cmd_type); + DBG("LSM cmd type: '%s' (%d)", + lttcomm_sessiond_command_str((lttcomm_sessiond_command) lsm->cmd_type), + lsm->cmd_type); - ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, - sizeof(struct lttcomm_session_msg)); + ret = lttcomm_send_creds_unix_sock( + sessiond_socket, lsm, sizeof(struct lttcomm_session_msg)); if (ret < 0) { ret = -LTTNG_ERR_FATAL; } @@ -226,15 +228,13 @@ static int recv_payload_sessiond(struct lttng_payload *payload, size_t len) int ret; const size_t original_payload_size = payload->buffer.size; - ret = lttng_dynamic_buffer_set_size( - &payload->buffer, payload->buffer.size + len); + ret = lttng_dynamic_buffer_set_size(&payload->buffer, payload->buffer.size + len); if (ret) { ret = -LTTNG_ERR_NOMEM; goto end; } - ret = recv_data_sessiond( - payload->buffer.data + original_payload_size, len); + ret = recv_data_sessiond(payload->buffer.data + original_payload_size, len); end: return ret; } @@ -244,7 +244,7 @@ end: * * If yes return 1, else return -1. */ -int lttng_check_tracing_group(void) +int lttng_check_tracing_group() { gid_t *grp_list, tracing_gid; int grp_list_size, grp_id, i; @@ -258,14 +258,14 @@ int lttng_check_tracing_group(void) } /* Get number of supplementary group IDs */ - grp_list_size = getgroups(0, NULL); + grp_list_size = getgroups(0, nullptr); if (grp_list_size < 0) { PERROR("getgroups"); goto end; } /* Alloc group list of the right size */ - grp_list = (gid_t *) zmalloc(grp_list_size * sizeof(gid_t)); + grp_list = calloc(grp_list_size); if (!grp_list) { PERROR("malloc"); goto end; @@ -290,12 +290,13 @@ end: return ret; } -static int check_enough_available_memory(size_t num_bytes_requested_per_cpu) +static enum lttng_error_code check_enough_available_memory(uint64_t num_bytes_requested_per_cpu) { int ret; + enum lttng_error_code ret_code; long num_cpu; - size_t best_mem_info; - size_t num_bytes_requested_total; + uint64_t best_mem_info; + uint64_t num_bytes_requested_total; /* * Get the number of CPU currently online to compute the amount of @@ -303,10 +304,17 @@ static int check_enough_available_memory(size_t num_bytes_requested_per_cpu) */ num_cpu = sysconf(_SC_NPROCESSORS_ONLN); if (num_cpu == -1) { - goto error; + ret_code = LTTNG_ERR_FATAL; + goto end; } - num_bytes_requested_total = num_bytes_requested_per_cpu * num_cpu; + if (num_bytes_requested_per_cpu > UINT64_MAX / (uint64_t) num_cpu) { + /* Overflow */ + ret_code = LTTNG_ERR_OVERFLOW; + goto end; + } + + num_bytes_requested_total = num_bytes_requested_per_cpu * (uint64_t) num_cpu; /* * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most @@ -328,10 +336,18 @@ static int check_enough_available_memory(size_t num_bytes_requested_per_cpu) goto success; } -error: - return -1; + /* No valid source of information. */ + ret_code = LTTNG_ERR_NOMEM; + goto end; + success: - return best_mem_info >= num_bytes_requested_total; + if (best_mem_info >= num_bytes_requested_total) { + ret_code = LTTNG_OK; + } else { + ret_code = LTTNG_ERR_NOMEM; + } +end: + return ret_code; } /* @@ -374,9 +390,9 @@ error: * Returns 0 on success, negative value on failure (the sessiond socket path * is somehow too long or ENOMEM). */ -static int set_session_daemon_path(void) +static int set_session_daemon_path() { - int in_tgroup = 0; /* In tracing group. */ + int in_tgroup = 0; /* In tracing group. */ uid_t uid; uid = getuid(); @@ -388,8 +404,8 @@ static int set_session_daemon_path(void) if ((uid == 0) || in_tgroup == 1) { const int ret = lttng_strncpy(sessiond_sock_path, - DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, - sizeof(sessiond_sock_path)); + DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, + sizeof(sessiond_sock_path)); if (ret) { goto error; @@ -415,8 +431,10 @@ static int set_session_daemon_path(void) * With GNU C >= 2.1, snprintf returns the required size * (excluding closing null) */ - ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), - DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); + ret = snprintf(sessiond_sock_path, + sizeof(sessiond_sock_path), + DEFAULT_HOME_CLIENT_UNIX_SOCK, + utils_get_home_dir()); if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { goto error; } @@ -433,7 +451,7 @@ error: * * On success, return the socket's file descriptor. On error, return -1. */ -int connect_sessiond(void) +int connect_sessiond() { int ret; @@ -454,7 +472,7 @@ error: return -1; } -static void reset_global_sessiond_connection_state(void) +static void reset_global_sessiond_connection_state() { sessiond_socket = -1; connected = 0; @@ -465,7 +483,7 @@ static void reset_global_sessiond_connection_state(void) * * On success, return 0. On error, return -1. */ -static int disconnect_sessiond(void) +static int disconnect_sessiond() { int ret = 0; @@ -477,11 +495,10 @@ static int disconnect_sessiond(void) return ret; } -static int recv_sessiond_optional_data(size_t len, void **user_buf, - size_t *user_len) +static int recv_sessiond_optional_data(size_t len, void **user_buf, size_t *user_len) { int ret = 0; - void *buf = NULL; + char *buf = nullptr; if (len) { if (!user_len) { @@ -489,7 +506,7 @@ static int recv_sessiond_optional_data(size_t len, void **user_buf, goto end; } - buf = zmalloc(len); + buf = zmalloc(len); if (!buf) { ret = -ENOMEM; goto end; @@ -507,7 +524,7 @@ static int recv_sessiond_optional_data(size_t len, void **user_buf, /* Move ownership of command header buffer to user. */ *user_buf = buf; - buf = NULL; + buf = nullptr; *user_len = len; } else { /* No command header. */ @@ -516,7 +533,7 @@ static int recv_sessiond_optional_data(size_t len, void **user_buf, } if (user_buf) { - *user_buf = NULL; + *user_buf = nullptr; } } @@ -533,9 +550,13 @@ end: * Return size of data (only payload, not header) or a negative error code. */ int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, - const int *fds, size_t nb_fd, const void *vardata, - size_t vardata_len, void **user_payload_buf, - void **user_cmd_header_buf, size_t *user_cmd_header_len) + const int *fds, + size_t nb_fd, + const void *vardata, + size_t vardata_len, + void **user_payload_buf, + void **user_cmd_header_buf, + size_t *user_cmd_header_len) { int ret; size_t payload_len; @@ -583,15 +604,14 @@ int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, } /* Get command header from data transmission */ - ret = recv_sessiond_optional_data(llm.cmd_header_size, - user_cmd_header_buf, user_cmd_header_len); + ret = recv_sessiond_optional_data( + llm.cmd_header_size, user_cmd_header_buf, user_cmd_header_len); if (ret < 0) { goto end; } /* Get payload from data transmission */ - ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, - &payload_len); + ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, &payload_len); if (ret < 0) { goto end; } @@ -603,8 +623,7 @@ end: return ret; } -int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message, - struct lttng_payload *reply) +int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message, struct lttng_payload *reply) { int ret; struct lttcomm_lttng_msg llm; @@ -623,16 +642,15 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message, } /* Send command to session daemon */ - ret = lttcomm_send_creds_unix_sock(sessiond_socket, message->buffer.data, - message->buffer.size); + ret = lttcomm_send_creds_unix_sock( + sessiond_socket, message->buffer.data, message->buffer.size); if (ret < 0) { ret = -LTTNG_ERR_FATAL; goto end; } if (fd_count > 0) { - ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket, - message); + ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket, message); if (ret < 0) { ret = -LTTNG_ERR_FATAL; goto end; @@ -675,18 +693,17 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message, } if (llm.fd_count > 0) { - ret = lttcomm_recv_payload_fds_unix_sock( - sessiond_socket, llm.fd_count, reply); + ret = lttcomm_recv_payload_fds_unix_sock(sessiond_socket, llm.fd_count, reply); if (ret < 0) { goto end; } } /* Don't return the llm header to the caller. */ - memmove(reply->buffer.data, reply->buffer.data + sizeof(llm), - reply->buffer.size - sizeof(llm)); - ret = lttng_dynamic_buffer_set_size( - &reply->buffer, reply->buffer.size - sizeof(llm)); + memmove(reply->buffer.data, + reply->buffer.data + sizeof(llm), + reply->buffer.size - sizeof(llm)); + ret = lttng_dynamic_buffer_set_size(&reply->buffer, reply->buffer.size - sizeof(llm)); if (ret) { /* Can't happen as size is reduced. */ abort(); @@ -704,21 +721,19 @@ end: * * The returned pointer will be NULL in case of malloc() error. */ -struct lttng_handle *lttng_create_handle(const char *session_name, - struct lttng_domain *domain) +struct lttng_handle *lttng_create_handle(const char *session_name, struct lttng_domain *domain) { int ret; - struct lttng_handle *handle = NULL; + struct lttng_handle *handle = nullptr; - handle = (lttng_handle *) zmalloc(sizeof(struct lttng_handle)); - if (handle == NULL) { + handle = zmalloc(); + if (handle == nullptr) { PERROR("malloc handle"); goto end; } /* Copy session name */ - ret = lttng_strncpy(handle->session_name, session_name ? : "", - sizeof(handle->session_name)); + ret = lttng_strncpy(handle->session_name, session_name ?: "", sizeof(handle->session_name)); if (ret) { goto error; } @@ -732,7 +747,7 @@ end: return handle; error: free(handle); - return NULL; + return nullptr; } /* @@ -748,21 +763,19 @@ void lttng_destroy_handle(struct lttng_handle *handle) * * Returns size of returned session payload data or a negative error code. */ -int lttng_register_consumer(struct lttng_handle *handle, - const char *socket_path) +int lttng_register_consumer(struct lttng_handle *handle, const char *socket_path) { int ret; struct lttcomm_session_msg lsm; - if (handle == NULL || socket_path == NULL) { + if (handle == nullptr || socket_path == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_REGISTER_CONSUMER; - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER; + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; @@ -770,14 +783,13 @@ int lttng_register_consumer(struct lttng_handle *handle, COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_strncpy(lsm.u.reg.path, socket_path, - sizeof(lsm.u.reg.path)); + ret = lttng_strncpy(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); end: return ret; } @@ -792,22 +804,21 @@ int lttng_start_tracing(const char *session_name) int ret; struct lttcomm_session_msg lsm; - if (session_name == NULL) { + if (session_name == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_START_TRACE; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_START_TRACE; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); end: return ret; } @@ -820,22 +831,21 @@ static int _lttng_stop_tracing(const char *session_name, int wait) int ret, data_ret; struct lttcomm_session_msg lsm; - if (session_name == NULL) { + if (session_name == nullptr) { ret = -LTTNG_ERR_INVALID; goto error; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_STOP_TRACE; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_STOP_TRACE; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto error; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { goto error; } @@ -892,88 +902,75 @@ int lttng_stop_tracing_no_wait(const char *session_name) * Returns the size of the returned payload data or a negative error code. */ int lttng_add_context(struct lttng_handle *handle, - struct lttng_event_context *ctx, const char *event_name, - const char *channel_name) + struct lttng_event_context *ctx, + const char *event_name __attribute__((unused)), + const char *channel_name) { int ret; - size_t len = 0; - char *buf = NULL; - struct lttcomm_session_msg lsm; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; + struct lttng_payload payload; + + lttng_payload_init(&payload); /* Safety check. Both are mandatory. */ - if (handle == NULL || ctx == NULL) { + if (handle == nullptr || ctx == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_ADD_CONTEXT; + ret = lttng_dynamic_buffer_set_size(&payload.buffer, sizeof(lsm)); + if (ret) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } /* If no channel name, send empty string. */ - ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "", - sizeof(lsm.u.context.channel_name)); + ret = lttng_strncpy( + lsm.u.context.channel_name, channel_name ?: "", sizeof(lsm.u.context.channel_name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { - size_t provider_len, ctx_len; - const char *provider_name = ctx->u.app_ctx.provider_name; - const char *ctx_name = ctx->u.app_ctx.ctx_name; + ret = lttng_event_context_serialize(ctx, &payload); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto end; + } - if (!provider_name || !ctx_name) { - ret = -LTTNG_ERR_INVALID; - goto end; - } + lsm.u.context.length = payload.buffer.size - sizeof(lsm); - provider_len = strlen(provider_name); - if (provider_len == 0) { - ret = -LTTNG_ERR_INVALID; - goto end; - } - lsm.u.context.provider_name_len = provider_len; + /* Update message header. */ + memcpy(payload.buffer.data, &lsm, sizeof(lsm)); - ctx_len = strlen(ctx_name); - if (ctx_len == 0) { - ret = -LTTNG_ERR_INVALID; - goto end; - } - lsm.u.context.context_name_len = ctx_len; + { + struct lttng_payload reply; + struct lttng_payload_view payload_view = + lttng_payload_view_from_payload(&payload, 0, -1); - len = provider_len + ctx_len; - buf = (char *) zmalloc(len); - if (!buf) { - ret = -LTTNG_ERR_NOMEM; + lttng_payload_init(&reply); + ret = lttng_ctl_ask_sessiond_payload(&payload_view, &reply); + lttng_payload_reset(&reply); + if (ret) { goto end; } - - memcpy(buf, provider_name, provider_len); - memcpy(buf + provider_len, ctx_name, ctx_len); - } - memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); - - if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { - /* - * Don't leak application addresses to the sessiond. - * This is only necessary when ctx is for an app ctx otherwise - * the values inside the union (type & config) are overwritten. - */ - lsm.u.context.ctx.u.app_ctx.provider_name = NULL; - lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; } - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); end: - free(buf); + lttng_payload_reset(&payload); return ret; } @@ -986,10 +983,10 @@ end: * Returns size of returned session payload data or a negative error code. */ int lttng_enable_event(struct lttng_handle *handle, - struct lttng_event *ev, const char *channel_name) + struct lttng_event *ev, + const char *channel_name) { - return lttng_enable_event_with_exclusions(handle, ev, channel_name, - NULL, 0, NULL); + return lttng_enable_event_with_exclusions(handle, ev, channel_name, nullptr, 0, nullptr); } /* @@ -999,11 +996,12 @@ int lttng_enable_event(struct lttng_handle *handle, * Return size of returned session payload data if OK. */ int lttng_enable_event_with_filter(struct lttng_handle *handle, - struct lttng_event *event, const char *channel_name, - const char *filter_expression) + struct lttng_event *event, + const char *channel_name, + const char *filter_expression) { - return lttng_enable_event_with_exclusions(handle, event, channel_name, - filter_expression, 0, NULL); + return lttng_enable_event_with_exclusions( + handle, event, channel_name, filter_expression, 0, nullptr); } /* @@ -1015,15 +1013,15 @@ int lttng_enable_event_with_filter(struct lttng_handle *handle, static char *set_agent_filter(const char *filter, struct lttng_event *ev) { int err; - char *agent_filter = NULL; + char *agent_filter = nullptr; LTTNG_ASSERT(ev); /* Don't add filter for the '*' event. */ if (strcmp(ev->name, "*") != 0) { if (filter) { - err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, - ev->name); + err = asprintf( + &agent_filter, "(%s) && (logger_name == \"%s\")", filter, ev->name); } else { err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); } @@ -1046,16 +1044,17 @@ static char *set_agent_filter(const char *filter, struct lttng_event *ev) if (filter || agent_filter) { char *new_filter; - err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", - agent_filter ? agent_filter : filter, op, - ev->loglevel); + err = asprintf(&new_filter, + "(%s) && (int_loglevel %s %d)", + agent_filter ? agent_filter : filter, + op, + ev->loglevel); if (agent_filter) { free(agent_filter); } agent_filter = new_filter; } else { - err = asprintf(&agent_filter, "int_loglevel %s %d", op, - ev->loglevel); + err = asprintf(&agent_filter, "int_loglevel %s %d", op, ev->loglevel); } if (err < 0) { PERROR("asprintf"); @@ -1066,7 +1065,7 @@ static char *set_agent_filter(const char *filter, struct lttng_event *ev) return agent_filter; error: free(agent_filter); - return NULL; + return nullptr; } /* @@ -1078,15 +1077,24 @@ error: * Returns size of returned session payload data or a negative error code. */ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, - struct lttng_event *ev, const char *channel_name, - const char *original_filter_expression, - int exclusion_count, char **exclusion_list) + struct lttng_event *ev, + const char *channel_name, + const char *original_filter_expression, + int exclusion_count, + char **exclusion_list) { - struct lttcomm_session_msg lsm; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; struct lttng_payload payload; - int ret = 0, i; + int ret = 0; unsigned int free_filter_expression = 0; - struct filter_parser_ctx *ctx = NULL; + struct filter_parser_ctx *ctx = nullptr; + size_t bytecode_len = 0; /* * We have either a filter or some exclusions, so we need to set up @@ -1101,7 +1109,7 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, */ char *filter_expression = (char *) original_filter_expression; - if (handle == NULL || ev == NULL) { + if (handle == nullptr || ev == nullptr) { ret = -LTTNG_ERR_INVALID; goto error; } @@ -1116,46 +1124,22 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, goto error; } - memset(&lsm, 0, sizeof(lsm)); - - /* If no channel name, send empty string. */ - ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "", - sizeof(lsm.u.enable.channel_name)); - if (ret) { - ret = -LTTNG_ERR_INVALID; - goto error; - } - - lsm.cmd_type = LTTNG_ENABLE_EVENT; if (ev->name[0] == '\0') { /* Enable all events. */ ret = lttng_strncpy(ev->name, "*", sizeof(ev->name)); LTTNG_ASSERT(ret == 0); } - COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); - - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); - if (ret) { - ret = -LTTNG_ERR_INVALID; - goto error; - } - - lsm.u.enable.exclusion_count = exclusion_count; - lsm.u.enable.bytecode_len = 0; - /* Parse filter expression. */ - if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL - || handle->domain.type == LTTNG_DOMAIN_LOG4J - || handle->domain.type == LTTNG_DOMAIN_PYTHON) { + if (filter_expression != nullptr || handle->domain.type == LTTNG_DOMAIN_JUL || + handle->domain.type == LTTNG_DOMAIN_LOG4J || + handle->domain.type == LTTNG_DOMAIN_PYTHON) { if (handle->domain.type == LTTNG_DOMAIN_JUL || - handle->domain.type == LTTNG_DOMAIN_LOG4J || - handle->domain.type == LTTNG_DOMAIN_PYTHON) { + handle->domain.type == LTTNG_DOMAIN_LOG4J || + handle->domain.type == LTTNG_DOMAIN_PYTHON) { char *agent_filter; - /* Setup JUL filter if needed. */ + /* Setup agent filter if needed. */ agent_filter = set_agent_filter(filter_expression, ev); if (!agent_filter) { if (!filter_expression) { @@ -1163,7 +1147,7 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, * No JUL and no filter, just skip * everything below. */ - goto ask_sessiond; + goto serialize; } } else { /* @@ -1176,131 +1160,97 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, } } + if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) == LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; + } + ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); if (ret) { - goto filter_error; + goto error; } - lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) - + bytecode_get_len(&ctx->bytecode->b); - lsm.u.enable.expression_len = strlen(filter_expression) + 1; + bytecode_len = bytecode_get_len(&ctx->bytecode->b) + sizeof(ctx->bytecode->b); + if (bytecode_len > LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; + } } - ret = lttng_dynamic_buffer_set_capacity(&payload.buffer, - lsm.u.enable.bytecode_len + - lsm.u.enable.expression_len + - LTTNG_SYMBOL_NAME_LEN * - exclusion_count); +serialize: + ret = lttng_event_serialize(ev, + exclusion_count, + exclusion_list, + filter_expression, + bytecode_len, + (ctx && bytecode_len) ? &ctx->bytecode->b : nullptr, + &payload); if (ret) { - ret = -LTTNG_ERR_EXCLUSION_NOMEM; - goto mem_error; + ret = -LTTNG_ERR_INVALID; + goto error; } - /* Put exclusion names first in the data. */ - for (i = 0; i < exclusion_count; i++) { - size_t exclusion_len; - - exclusion_len = lttng_strnlen(exclusion_list[i], - LTTNG_SYMBOL_NAME_LEN); - if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) { - /* Exclusion is not NULL-terminated. */ - ret = -LTTNG_ERR_INVALID; - goto mem_error; - } - - ret = lttng_dynamic_buffer_append(&payload.buffer, - exclusion_list[i], exclusion_len); - if (ret) { - goto mem_error; - } - - /* - * Padding the rest of the entry with zeros. Every exclusion - * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer. - */ - ret = lttng_dynamic_buffer_set_size(&payload.buffer, - LTTNG_SYMBOL_NAME_LEN * (i + 1)); - if (ret) { - goto mem_error; - } + /* If no channel name, send empty string. */ + ret = lttng_strncpy( + lsm.u.enable.channel_name, channel_name ?: "", sizeof(lsm.u.enable.channel_name)); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto error; } - /* Add filter expression next. */ - if (filter_expression) { - ret = lttng_dynamic_buffer_append(&payload.buffer, - filter_expression, lsm.u.enable.expression_len); - if (ret) { - goto mem_error; - } - } - /* Add filter bytecode next. */ - if (ctx && lsm.u.enable.bytecode_len != 0) { - ret = lttng_dynamic_buffer_append(&payload.buffer, - &ctx->bytecode->b, lsm.u.enable.bytecode_len); - if (ret) { - goto mem_error; - } - } - if (ev->extended.ptr) { - struct lttng_event_extended *ev_ext = - (struct lttng_event_extended *) ev->extended.ptr; - - if (ev_ext->probe_location) { - /* - * lttng_userspace_probe_location_serialize returns the - * number of bytes that was appended to the buffer. - */ - ret = lttng_userspace_probe_location_serialize( - ev_ext->probe_location, &payload); - if (ret < 0) { - goto mem_error; - } + /* Domain */ + COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - /* - * Set the size of the userspace probe location element - * of the buffer so that the receiving side knows where - * to split it. - */ - lsm.u.enable.userspace_probe_location_len = ret; - } + /* Session name */ + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto error; } + /* Length of the serialized event. */ + lsm.u.enable.length = (uint32_t) payload.buffer.size; + { - struct lttng_payload_view view = lttng_payload_view_from_payload( - &payload, 0, -1); + struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); int fd_count = lttng_payload_view_get_fd_handle_count(&view); int fd_to_send; if (fd_count < 0) { - goto mem_error; + goto error; } LTTNG_ASSERT(fd_count == 0 || fd_count == 1); if (fd_count == 1) { - struct fd_handle *h = - lttng_payload_view_pop_fd_handle(&view); + struct fd_handle *h = lttng_payload_view_pop_fd_handle(&view); if (!h) { - goto mem_error; + goto error; } fd_to_send = fd_handle_get_fd(h); fd_handle_put(h); } + lsm.fd_count = fd_count; + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, - fd_count ? &fd_to_send : NULL, fd_count, - view.buffer.size ? view.buffer.data : NULL, - view.buffer.size, NULL, NULL, 0); + fd_count ? &fd_to_send : nullptr, + fd_count, + view.buffer.size ? view.buffer.data : + nullptr, + view.buffer.size, + nullptr, + nullptr, + nullptr); } -mem_error: +error: if (filter_expression && ctx) { filter_bytecode_free(ctx); filter_ir_free(ctx); filter_parser_ctx_free(ctx); } -filter_error: if (free_filter_expression) { /* * The filter expression has been replaced and must be freed as @@ -1309,28 +1259,38 @@ filter_error: */ free(filter_expression); } -error: /* * Return directly to the caller and don't ask the sessiond since * something went wrong in the parsing of data above. */ lttng_payload_reset(&payload); return ret; - -ask_sessiond: - ret = lttng_ctl_ask_sessiond(&lsm, NULL); - return ret; } int lttng_disable_event_ext(struct lttng_handle *handle, - struct lttng_event *ev, const char *channel_name, - const char *original_filter_expression) + struct lttng_event *ev, + const char *channel_name, + const char *original_filter_expression) { - struct lttcomm_session_msg lsm; - char *varlen_data; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; + struct lttng_payload payload; int ret = 0; unsigned int free_filter_expression = 0; - struct filter_parser_ctx *ctx = NULL; + struct filter_parser_ctx *ctx = nullptr; + size_t bytecode_len = 0; + + /* + * We have either a filter or some exclusions, so we need to set up + * a variable-length payload from where to send the data. + */ + lttng_payload_init(&payload); + /* * Cast as non-const since we may replace the filter expression * by a dynamically allocated string. Otherwise, the original @@ -1338,7 +1298,7 @@ int lttng_disable_event_ext(struct lttng_handle *handle, */ char *filter_expression = (char *) original_filter_expression; - if (handle == NULL || ev == NULL) { + if (handle == nullptr || ev == nullptr) { ret = -LTTNG_ERR_INVALID; goto error; } @@ -1353,57 +1313,16 @@ int lttng_disable_event_ext(struct lttng_handle *handle, goto error; } - memset(&lsm, 0, sizeof(lsm)); - - /* If no channel name, send empty string. */ - ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "", - sizeof(lsm.u.disable.channel_name)); - if (ret) { - ret = -LTTNG_ERR_INVALID; - goto error; - } - - lsm.cmd_type = LTTNG_DISABLE_EVENT; - - COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event)); - - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); - if (ret) { - ret = -LTTNG_ERR_INVALID; - goto error; - } - - lsm.u.disable.bytecode_len = 0; - - /* - * For the JUL domain, a filter is enforced except for the - * disable all event. This is done to avoid having the event in - * all sessions thus filtering by logger name. - */ - if (filter_expression == NULL && - (handle->domain.type != LTTNG_DOMAIN_JUL && - handle->domain.type != LTTNG_DOMAIN_LOG4J && - handle->domain.type != LTTNG_DOMAIN_PYTHON)) { - goto ask_sessiond; - } - - /* - * We have a filter, so we need to set up a variable-length - * memory block from where to send the data. - */ - - /* Parse filter expression */ - if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL - || handle->domain.type == LTTNG_DOMAIN_LOG4J - || handle->domain.type == LTTNG_DOMAIN_PYTHON) { + /* Parse filter expression. */ + if (filter_expression != nullptr || handle->domain.type == LTTNG_DOMAIN_JUL || + handle->domain.type == LTTNG_DOMAIN_LOG4J || + handle->domain.type == LTTNG_DOMAIN_PYTHON) { if (handle->domain.type == LTTNG_DOMAIN_JUL || - handle->domain.type == LTTNG_DOMAIN_LOG4J || - handle->domain.type == LTTNG_DOMAIN_PYTHON) { + handle->domain.type == LTTNG_DOMAIN_LOG4J || + handle->domain.type == LTTNG_DOMAIN_PYTHON) { char *agent_filter; - /* Setup JUL filter if needed. */ + /* Setup agent filter if needed. */ agent_filter = set_agent_filter(filter_expression, ev); if (!agent_filter) { if (!filter_expression) { @@ -1411,11 +1330,11 @@ int lttng_disable_event_ext(struct lttng_handle *handle, * No JUL and no filter, just skip * everything below. */ - goto ask_sessiond; + goto serialize; } } else { /* - * With a JUL filter, the original filter has + * With an agent filter, the original filter has * been added to it thus replace the filter * expression. */ @@ -1424,48 +1343,95 @@ int lttng_disable_event_ext(struct lttng_handle *handle, } } + if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) == LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; + } + ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); if (ret) { - goto filter_error; + goto error; } - lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) - + bytecode_get_len(&ctx->bytecode->b); - lsm.u.enable.expression_len = strlen(filter_expression) + 1; + bytecode_len = bytecode_get_len(&ctx->bytecode->b) + sizeof(ctx->bytecode->b); + if (bytecode_len > LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; + } } - varlen_data = (char *) zmalloc(lsm.u.disable.bytecode_len - + lsm.u.disable.expression_len); - if (!varlen_data) { - ret = -LTTNG_ERR_EXCLUSION_NOMEM; - goto mem_error; +serialize: + ret = lttng_event_serialize(ev, + 0, + nullptr, + filter_expression, + bytecode_len, + (ctx && bytecode_len) ? &ctx->bytecode->b : nullptr, + &payload); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto error; } - /* Add filter expression. */ - if (lsm.u.disable.expression_len != 0) { - memcpy(varlen_data, - filter_expression, - lsm.u.disable.expression_len); + /* If no channel name, send empty string. */ + ret = lttng_strncpy( + lsm.u.disable.channel_name, channel_name ?: "", sizeof(lsm.u.disable.channel_name)); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto error; } - /* Add filter bytecode next. */ - if (ctx && lsm.u.disable.bytecode_len != 0) { - memcpy(varlen_data - + lsm.u.disable.expression_len, - &ctx->bytecode->b, - lsm.u.disable.bytecode_len); + + /* Domain */ + COPY_DOMAIN_PACKED(lsm.domain, handle->domain); + + /* Session name */ + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); + if (ret) { + ret = -LTTNG_ERR_INVALID; + goto error; } - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, - lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL); - free(varlen_data); + /* Length of the serialized event. */ + lsm.u.disable.length = (uint32_t) payload.buffer.size; + + { + struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); + int fd_count = lttng_payload_view_get_fd_handle_count(&view); + int fd_to_send; + + if (fd_count < 0) { + goto error; + } + + LTTNG_ASSERT(fd_count == 0 || fd_count == 1); + if (fd_count == 1) { + struct fd_handle *h = lttng_payload_view_pop_fd_handle(&view); + + if (!h) { + goto error; + } + + fd_to_send = fd_handle_get_fd(h); + fd_handle_put(h); + } + + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + fd_count ? &fd_to_send : nullptr, + fd_count, + view.buffer.size ? view.buffer.data : + nullptr, + view.buffer.size, + nullptr, + nullptr, + nullptr); + } -mem_error: +error: if (filter_expression && ctx) { filter_bytecode_free(ctx); filter_ir_free(ctx); filter_parser_ctx_free(ctx); } -filter_error: if (free_filter_expression) { /* * The filter expression has been replaced and must be freed as @@ -1474,15 +1440,11 @@ filter_error: */ free(filter_expression); } -error: /* * Return directly to the caller and don't ask the sessiond since * something went wrong in the parsing of data above. */ - return ret; - -ask_sessiond: - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + lttng_payload_reset(&payload); return ret; } @@ -1492,8 +1454,7 @@ ask_sessiond: * If no channel name is specified, the default 'channel0' is used. * Returns size of returned session payload data or a negative error code. */ -int lttng_disable_event(struct lttng_handle *handle, const char *name, - const char *channel_name) +int lttng_disable_event(struct lttng_handle *handle, const char *name, const char *channel_name) { int ret; struct lttng_event ev; @@ -1507,18 +1468,17 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name, goto end; } - ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL); + ret = lttng_disable_event_ext(handle, &ev, channel_name, nullptr); end: return ret; } struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) { - struct lttng_channel *channel = NULL; - struct lttng_channel_extended *extended = NULL; + struct lttng_channel *channel = nullptr; if (!domain) { - goto error; + goto end; } /* Validate domain. */ @@ -1529,36 +1489,26 @@ struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) case LTTNG_BUFFER_PER_PID: break; default: - goto error; + goto end; } break; case LTTNG_DOMAIN_KERNEL: if (domain->buf_type != LTTNG_BUFFER_GLOBAL) { - goto error; + goto end; } break; default: - goto error; + goto end; } - channel = (lttng_channel *) zmalloc(sizeof(*channel)); + channel = lttng_channel_create_internal(); if (!channel) { - goto error; - } - - extended = (lttng_channel_extended *) zmalloc(sizeof(*extended)); - if (!extended) { - goto error; + goto end; } - channel->attr.extended.ptr = extended; - lttng_channel_set_default_attr(domain, &channel->attr); +end: return channel; -error: - free(channel); - free(extended); - return NULL; } void lttng_channel_destroy(struct lttng_channel *channel) @@ -1577,69 +1527,84 @@ void lttng_channel_destroy(struct lttng_channel *channel) * Enable channel per domain * Returns size of returned session payload data or a negative error code. */ -int lttng_enable_channel(struct lttng_handle *handle, - struct lttng_channel *in_chan) +int lttng_enable_channel(struct lttng_handle *handle, struct lttng_channel *in_chan) { + enum lttng_error_code ret_code; int ret; + struct lttng_dynamic_buffer buffer; struct lttcomm_session_msg lsm; - size_t total_buffer_size_needed_per_cpu = 0; + uint64_t total_buffer_size_needed_per_cpu = 0; + struct lttng_channel *channel = nullptr; + + lttng_dynamic_buffer_init(&buffer); /* NULL arguments are forbidden. No default values. */ - if (handle == NULL || in_chan == NULL) { - return -LTTNG_ERR_INVALID; + if (handle == nullptr || in_chan == nullptr) { + ret = -LTTNG_ERR_INVALID; + goto end; } - memset(&lsm, 0, sizeof(lsm)); - memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan)); - lsm.u.channel.chan.attr.extended.ptr = NULL; + /* + * Verify that the amount of memory required to create the requested + * buffer is available on the system at the moment. + */ + if (in_chan->attr.num_subbuf > UINT64_MAX / in_chan->attr.subbuf_size) { + /* Overflow */ + ret = -LTTNG_ERR_OVERFLOW; + goto end; + } - if (!in_chan->attr.extended.ptr) { - struct lttng_channel *channel; - struct lttng_channel_extended *extended; + total_buffer_size_needed_per_cpu = in_chan->attr.num_subbuf * in_chan->attr.subbuf_size; + ret_code = check_enough_available_memory(total_buffer_size_needed_per_cpu); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } - channel = lttng_channel_create(&handle->domain); - if (!channel) { - return -LTTNG_ERR_NOMEM; - } + /* Copy the channel for easier manipulation. */ + channel = lttng_channel_copy(in_chan); + if (!channel) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } - /* - * Create a new channel in order to use default extended - * attribute values. - */ - extended = (struct lttng_channel_extended *) - channel->attr.extended.ptr; - memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); - lttng_channel_destroy(channel); - } else { - struct lttng_channel_extended *extended; + /* Populate the channel extended attribute if necessary. */ + if (!channel->attr.extended.ptr) { + struct lttng_channel_extended *extended = zmalloc(); - extended = (struct lttng_channel_extended *) - in_chan->attr.extended.ptr; - memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); - } + if (!extended) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } - /* - * Verify that the amount of memory required to create the requested - * buffer is available on the system at the moment. - */ - total_buffer_size_needed_per_cpu = lsm.u.channel.chan.attr.num_subbuf * - lsm.u.channel.chan.attr.subbuf_size; - if (!check_enough_available_memory(total_buffer_size_needed_per_cpu)) { - return -LTTNG_ERR_NOMEM; + lttng_channel_set_default_extended_attr(&handle->domain, extended); + channel->attr.extended.ptr = extended; } - lsm.cmd_type = LTTNG_ENABLE_CHANNEL; + /* Prepare the payload */ + memset(&lsm, 0, sizeof(lsm)); + + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL; COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_channel_serialize(channel, &buffer); + if (ret) { + ret = -LTTNG_ERR_FATAL; + goto end; + } + + lsm.u.channel.length = buffer.size; + + ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, buffer.size, nullptr); end: + lttng_channel_destroy(channel); + lttng_dynamic_buffer_reset(&buffer); return ret; } @@ -1653,16 +1618,15 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name) struct lttcomm_session_msg lsm; /* Safety check. Both are mandatory. */ - if (handle == NULL || name == NULL) { + if (handle == nullptr || name == nullptr) { return -LTTNG_ERR_INVALID; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_DISABLE_CHANNEL; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL; - ret = lttng_strncpy(lsm.u.disable.channel_name, name, - sizeof(lsm.u.disable.channel_name)); + ret = lttng_strncpy(lsm.u.disable.channel_name, name, sizeof(lsm.u.disable.channel_name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; @@ -1670,14 +1634,13 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name) COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); end: return ret; } @@ -1688,26 +1651,75 @@ end: * Returns the number of lttng_event entries in events; * on error, returns a negative value. */ -int lttng_list_tracepoints(struct lttng_handle *handle, - struct lttng_event **events) +int lttng_list_tracepoints(struct lttng_handle *handle, struct lttng_event **events) { - int ret; - struct lttcomm_session_msg lsm; + enum lttng_error_code ret_code; + int ret, total_payload_received; + char *reception_buffer = nullptr; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; + struct lttcomm_list_command_header *cmd_header = nullptr; + size_t cmd_header_len; + unsigned int nb_events = 0; - if (handle == NULL) { - return -LTTNG_ERR_INVALID; + if (handle == nullptr) { + ret = -LTTNG_ERR_INVALID; + goto end; } - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + nullptr, + 0, + nullptr, + 0, + (void **) &reception_buffer, + (void **) &cmd_header, + &cmd_header_len); if (ret < 0) { - return ret; + goto end; + } + + total_payload_received = ret; + + if (!cmd_header) { + ret = -LTTNG_ERR_UNK; + goto end; + } + + if (cmd_header->count > INT_MAX) { + ret = -LTTNG_ERR_OVERFLOW; + goto end; } - return ret / sizeof(struct lttng_event); + nb_events = (unsigned int) cmd_header->count; + + { + struct lttng_buffer_view events_view = + lttng_buffer_view_init(reception_buffer, 0, total_payload_received); + struct lttng_payload_view events_payload_view = + lttng_payload_view_from_buffer_view(&events_view, 0, -1); + + ret_code = lttng_events_create_and_flatten_from_payload( + &events_payload_view, nb_events, events); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } + } + + ret = (int) nb_events; + +end: + free(cmd_header); + free(reception_buffer); + return ret; } /* @@ -1716,26 +1728,72 @@ int lttng_list_tracepoints(struct lttng_handle *handle, * Returns the number of lttng_event_field entries in events; * on error, returns a negative value. */ -int lttng_list_tracepoint_fields(struct lttng_handle *handle, - struct lttng_event_field **fields) +int lttng_list_tracepoint_fields(struct lttng_handle *handle, struct lttng_event_field **fields) { + enum lttng_error_code ret_code; int ret; struct lttcomm_session_msg lsm; + const struct lttcomm_list_command_header *cmd_header = nullptr; + unsigned int nb_event_fields = 0; + struct lttng_payload reply; - if (handle == NULL) { - return -LTTNG_ERR_INVALID; + lttng_payload_init(&reply); + + if (handle == nullptr) { + ret = -LTTNG_ERR_INVALID; + goto end; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS; COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); - if (ret < 0) { - return ret; + { + lttng_payload_view message_view = + lttng_payload_view_init_from_buffer((const char *) &lsm, 0, sizeof(lsm)); + + ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); + if (ret < 0) { + goto end; + } + } + + { + const lttng_buffer_view cmd_header_view = lttng_buffer_view_from_dynamic_buffer( + &reply.buffer, 0, sizeof(*cmd_header)); + + if (!lttng_buffer_view_is_valid(&cmd_header_view)) { + ret = -LTTNG_ERR_INVALID_PROTOCOL; + goto end; + } + + cmd_header = (struct lttcomm_list_command_header *) cmd_header_view.data; + } + + if (cmd_header->count > INT_MAX) { + ret = -LTTNG_ERR_OVERFLOW; + goto end; + } + + nb_event_fields = cmd_header->count; + + { + lttng_payload_view reply_view = + lttng_payload_view_from_payload(&reply, sizeof(*cmd_header), -1); + + ret_code = lttng_event_fields_create_and_flatten_from_payload( + &reply_view, nb_event_fields, fields); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } } - return ret / sizeof(struct lttng_event_field); + ret = nb_event_fields; + +end: + lttng_payload_reset(&reply); + return ret; } /* @@ -1747,24 +1805,68 @@ int lttng_list_tracepoint_fields(struct lttng_handle *handle, */ int lttng_list_syscalls(struct lttng_event **events) { - int ret; - struct lttcomm_session_msg lsm; + enum lttng_error_code ret_code; + int ret, total_payload_received; + char *reception_buffer = nullptr; + struct lttcomm_session_msg lsm = {}; + struct lttcomm_list_command_header *cmd_header = nullptr; + size_t cmd_header_len; + uint32_t nb_events = 0; if (!events) { - return -LTTNG_ERR_INVALID; + ret = -LTTNG_ERR_INVALID; + goto end; } - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_SYSCALLS; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS; /* Force kernel domain for system calls. */ lsm.domain.type = LTTNG_DOMAIN_KERNEL; - ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + nullptr, + 0, + nullptr, + 0, + (void **) &reception_buffer, + (void **) &cmd_header, + &cmd_header_len); if (ret < 0) { - return ret; + goto end; + } + total_payload_received = ret; + + if (!cmd_header) { + ret = -LTTNG_ERR_UNK; + goto end; } - return ret / sizeof(struct lttng_event); + if (cmd_header->count > INT_MAX) { + ret = -LTTNG_ERR_OVERFLOW; + goto end; + } + + nb_events = (unsigned int) cmd_header->count; + + { + const struct lttng_buffer_view events_view = + lttng_buffer_view_init(reception_buffer, 0, total_payload_received); + struct lttng_payload_view events_payload_view = + lttng_payload_view_from_buffer_view(&events_view, 0, -1); + + ret_code = lttng_events_create_and_flatten_from_payload( + &events_payload_view, nb_events, events); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } + } + + ret = (int) nb_events; + +end: + free(reception_buffer); + free(cmd_header); + return ret; } /* @@ -1780,21 +1882,24 @@ const char *lttng_strerror(int code) return error_get_str(code); } -enum lttng_error_code lttng_create_session_ext( - struct lttng_session_descriptor *session_descriptor) +enum lttng_error_code lttng_create_session_ext(struct lttng_session_descriptor *session_descriptor) { enum lttng_error_code ret_code; struct lttcomm_session_msg lsm = { - .cmd_type = LTTNG_CREATE_SESSION_EXT, + .cmd_type = LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, }; - void *reply = NULL; + void *reply = nullptr; struct lttng_buffer_view reply_view; int reply_ret; bool sessiond_must_generate_ouput; struct lttng_dynamic_buffer payload; int ret; size_t descriptor_size; - struct lttng_session_descriptor *descriptor_reply = NULL; + struct lttng_session_descriptor *descriptor_reply = nullptr; lttng_dynamic_buffer_init(&payload); if (!session_descriptor) { @@ -1803,8 +1908,7 @@ enum lttng_error_code lttng_create_session_ext( } sessiond_must_generate_ouput = - !lttng_session_descriptor_is_output_destination_initialized( - session_descriptor); + !lttng_session_descriptor_is_output_destination_initialized(session_descriptor); if (sessiond_must_generate_ouput) { const char *home_dir = utils_get_home_dir(); size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0; @@ -1815,8 +1919,7 @@ enum lttng_error_code lttng_create_session_ext( } lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; - ret = lttng_dynamic_buffer_append(&payload, home_dir, - home_dir_len); + ret = lttng_dynamic_buffer_append(&payload, home_dir, home_dir_len); if (ret) { ret_code = LTTNG_ERR_NOMEM; goto end; @@ -1824,8 +1927,7 @@ enum lttng_error_code lttng_create_session_ext( } descriptor_size = payload.size; - ret = lttng_session_descriptor_serialize(session_descriptor, - &payload); + ret = lttng_session_descriptor_serialize(session_descriptor, &payload); if (ret) { ret_code = LTTNG_ERR_INVALID; goto end; @@ -1834,8 +1936,8 @@ enum lttng_error_code lttng_create_session_ext( lsm.u.create_session.session_descriptor_size = descriptor_size; /* Command returns a session descriptor on success. */ - reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, - payload.size, &reply); + reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( + &lsm, payload.data, payload.size, &reply); if (reply_ret < 0) { ret_code = (lttng_error_code) -reply_ret; goto end; @@ -1846,8 +1948,7 @@ enum lttng_error_code lttng_create_session_ext( } reply_view = lttng_buffer_view_init((const char *) reply, 0, reply_ret); - ret = lttng_session_descriptor_create_from_buffer(&reply_view, - &descriptor_reply); + ret = lttng_session_descriptor_create_from_buffer(&reply_view, &descriptor_reply); if (ret < 0) { ret_code = LTTNG_ERR_FATAL; goto end; @@ -1870,8 +1971,8 @@ int lttng_create_session(const char *name, const char *url) { int ret; ssize_t size; - struct lttng_uri *uris = NULL; - struct lttng_session_descriptor *descriptor = NULL; + struct lttng_uri *uris = nullptr; + struct lttng_session_descriptor *descriptor = nullptr; enum lttng_error_code ret_code; if (!name) { @@ -1879,7 +1980,7 @@ int lttng_create_session(const char *name, const char *url) goto end; } - size = uri_parse_str_urls(url, NULL, &uris); + size = uri_parse_str_urls(url, nullptr, &uris); if (size < 0) { ret = -LTTNG_ERR_INVALID; goto end; @@ -1893,12 +1994,10 @@ int lttng_create_session(const char *name, const char *url) ret = -LTTNG_ERR_INVALID; goto end; } - descriptor = lttng_session_descriptor_local_create(name, - uris[0].dst.path); + descriptor = lttng_session_descriptor_local_create(name, uris[0].dst.path); break; case 2: - descriptor = lttng_session_descriptor_network_create(name, url, - NULL); + descriptor = lttng_session_descriptor_network_create(name, url, nullptr); break; default: ret = -LTTNG_ERR_INVALID; @@ -1926,15 +2025,15 @@ int lttng_create_session_snapshot(const char *name, const char *snapshot_url) int ret; enum lttng_error_code ret_code; ssize_t size; - struct lttng_uri *uris = NULL; - struct lttng_session_descriptor *descriptor = NULL; + struct lttng_uri *uris = nullptr; + struct lttng_session_descriptor *descriptor = nullptr; if (!name) { ret = -LTTNG_ERR_INVALID; goto end; } - size = uri_parse_str_urls(snapshot_url, NULL, &uris); + size = uri_parse_str_urls(snapshot_url, nullptr, &uris); if (size < 0) { ret = -LTTNG_ERR_INVALID; goto end; @@ -1942,10 +2041,8 @@ int lttng_create_session_snapshot(const char *name, const char *snapshot_url) /* * If the user does not specify a custom subdir, use the session name. */ - if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && - strlen(uris[0].subdir) == 0) { - ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", - name); + if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) { + ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name); if (ret < 0) { PERROR("Failed to set session name as network destination sub-directory"); ret = -LTTNG_ERR_FATAL; @@ -1966,15 +2063,11 @@ int lttng_create_session_snapshot(const char *name, const char *snapshot_url) ret = -LTTNG_ERR_INVALID; goto end; } - descriptor = lttng_session_descriptor_snapshot_local_create( - name, - uris[0].dst.path); + descriptor = lttng_session_descriptor_snapshot_local_create(name, uris[0].dst.path); break; case 2: descriptor = lttng_session_descriptor_snapshot_network_create( - name, - snapshot_url, - NULL); + name, snapshot_url, nullptr); break; default: ret = -LTTNG_ERR_INVALID; @@ -1997,12 +2090,11 @@ end: * * Return 0 on success else a negative LTTng error code. */ -int lttng_create_session_live(const char *name, const char *url, - unsigned int timer_interval) +int lttng_create_session_live(const char *name, const char *url, unsigned int timer_interval) { int ret; enum lttng_error_code ret_code; - struct lttng_session_descriptor *descriptor = NULL; + struct lttng_session_descriptor *descriptor = nullptr; if (!name) { ret = -LTTNG_ERR_INVALID; @@ -2011,10 +2103,9 @@ int lttng_create_session_live(const char *name, const char *url, if (url) { descriptor = lttng_session_descriptor_live_network_create( - name, url, NULL, timer_interval); + name, url, nullptr, timer_interval); } else { - descriptor = lttng_session_descriptor_live_create( - name, timer_interval); + descriptor = lttng_session_descriptor_live_create(name, timer_interval); } if (!descriptor) { ret = -LTTNG_ERR_INVALID; @@ -2037,7 +2128,7 @@ int lttng_destroy_session(const char *session_name) int ret; enum lttng_error_code ret_code; enum lttng_destruction_handle_status status; - struct lttng_destruction_handle *handle = NULL; + struct lttng_destruction_handle *handle = nullptr; /* * Stop the tracing and wait for the data to be @@ -2080,7 +2171,7 @@ int lttng_destroy_session_no_wait(const char *session_name) { enum lttng_error_code ret_code; - ret_code = lttng_destroy_session_ext(session_name, NULL); + ret_code = lttng_destroy_session_ext(session_name, nullptr); return ret_code == LTTNG_OK ? 0 : -ret_code; } @@ -2094,15 +2185,21 @@ int lttng_list_sessions(struct lttng_session **out_sessions) { int ret; struct lttcomm_session_msg lsm; - const size_t session_size = sizeof(struct lttng_session) + - sizeof(struct lttng_session_extended); + const size_t session_size = + sizeof(struct lttng_session) + sizeof(struct lttng_session_extended); size_t session_count, i; struct lttng_session_extended *sessions_extended_begin; - struct lttng_session *sessions = NULL; + struct lttng_session *sessions = nullptr; memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_SESSIONS; - ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS; + /* + * Initialize out_sessions to NULL so it is initialized when + * lttng_list_sessions returns 0, thus allowing *out_sessions to + * be subsequently freed. + */ + *out_sessions = nullptr; + ret = lttng_ctl_ask_sessiond(&lsm, (void **) &sessions); if (ret <= 0) { goto end; } @@ -2114,18 +2211,15 @@ int lttng_list_sessions(struct lttng_session **out_sessions) if (ret % session_size) { ret = -LTTNG_ERR_UNK; free(sessions); - *out_sessions = NULL; goto end; } session_count = (size_t) ret / session_size; - sessions_extended_begin = (struct lttng_session_extended *) - (&sessions[session_count]); + sessions_extended_begin = (struct lttng_session_extended *) (&sessions[session_count]); /* Set extended session info pointers. */ for (i = 0; i < session_count; i++) { struct lttng_session *session = &sessions[i]; - struct lttng_session_extended *extended = - &(sessions_extended_begin[i]); + struct lttng_session_extended *extended = &(sessions_extended_begin[i]); session->extended.ptr = extended; } @@ -2136,8 +2230,8 @@ end: return ret; } -enum lttng_error_code lttng_session_get_creation_time( - const struct lttng_session *session, uint64_t *creation_time) +enum lttng_error_code lttng_session_get_creation_time(const struct lttng_session *session, + uint64_t *creation_time) { enum lttng_error_code ret = LTTNG_OK; struct lttng_session_extended *extended; @@ -2158,34 +2252,32 @@ end: return ret; } -int lttng_set_session_shm_path(const char *session_name, - const char *shm_path) +int lttng_set_session_shm_path(const char *session_name, const char *shm_path) { int ret; struct lttcomm_session_msg lsm; - if (session_name == NULL) { + if (session_name == nullptr) { return -LTTNG_ERR_INVALID; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "", - sizeof(lsm.u.set_shm_path.shm_path)); + ret = lttng_strncpy( + lsm.u.set_shm_path.shm_path, shm_path ?: "", sizeof(lsm.u.set_shm_path.shm_path)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); end: return ret; } @@ -2196,28 +2288,26 @@ end: * Returns the number of lttng_domain entries in domains; * on error, returns a negative value. */ -int lttng_list_domains(const char *session_name, - struct lttng_domain **domains) +int lttng_list_domains(const char *session_name, struct lttng_domain **domains) { int ret; struct lttcomm_session_msg lsm; - if (session_name == NULL) { + if (session_name == nullptr) { ret = -LTTNG_ERR_INVALID; goto error; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_DOMAINS; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto error; } - ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); + ret = lttng_ctl_ask_sessiond(&lsm, (void **) domains); if (ret < 0) { goto error; } @@ -2233,25 +2323,25 @@ error: * Returns the number of lttng_channel entries in channels; * on error, returns a negative value. */ -int lttng_list_channels(struct lttng_handle *handle, - struct lttng_channel **channels) +int lttng_list_channels(struct lttng_handle *handle, struct lttng_channel **channels) { - int ret; - size_t channel_count, i; - const size_t channel_size = sizeof(struct lttng_channel) + - sizeof(struct lttng_channel_extended); + int ret, total_payload_received; struct lttcomm_session_msg lsm; - char *extended_at; + char *reception_buffer = nullptr; + size_t cmd_header_len = 0; + struct lttcomm_list_command_header *cmd_header = nullptr; + struct lttng_dynamic_buffer tmp_buffer; + + lttng_dynamic_buffer_init(&tmp_buffer); - if (handle == NULL) { + if (handle == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_CHANNELS; - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS; + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; @@ -2259,31 +2349,52 @@ int lttng_list_channels(struct lttng_handle *handle, COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + nullptr, + 0, + nullptr, + 0, + (void **) &reception_buffer, + (void **) &cmd_header, + &cmd_header_len); if (ret < 0) { goto end; } - if (ret % channel_size) { - ret = -LTTNG_ERR_UNK; - free(*channels); - *channels = NULL; + total_payload_received = ret; + + if (cmd_header_len != sizeof(*cmd_header)) { + ret = -LTTNG_ERR_FATAL; goto end; } - channel_count = (size_t) ret / channel_size; - /* Set extended info pointers */ - extended_at = ((char *) *channels) + - channel_count * sizeof(struct lttng_channel); - for (i = 0; i < channel_count; i++) { - struct lttng_channel *chan = &(*channels)[i]; + if (!cmd_header) { + ret = LTTNG_ERR_UNK; + goto end; + } - chan->attr.extended.ptr = extended_at; - extended_at += sizeof(struct lttng_channel_extended); + if (cmd_header->count > INT_MAX) { + ret = -LTTNG_ERR_OVERFLOW; + goto end; + } + + { + enum lttng_error_code ret_code; + const struct lttng_buffer_view events_view = + lttng_buffer_view_init(reception_buffer, 0, total_payload_received); + + ret_code = lttng_channels_create_and_flatten_from_buffer( + &events_view, cmd_header->count, channels); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } } - ret = (int) channel_count; + ret = (int) cmd_header->count; end: + free(cmd_header); + free(reception_buffer); return ret; } @@ -2294,44 +2405,33 @@ end: * on error, returns a negative value. */ int lttng_list_events(struct lttng_handle *handle, - const char *channel_name, struct lttng_event **events) + const char *channel_name, + struct lttng_event **events) { int ret; struct lttcomm_session_msg lsm = {}; - const struct lttcomm_event_command_header *cmd_header = NULL; - uint32_t nb_events, i; - const char *comm_ext_at; - struct lttng_dynamic_buffer listing; - size_t storage_req; - struct lttng_payload payload; - struct lttng_payload payload_copy; + struct lttng_payload reply; struct lttng_payload_view lsm_view = - lttng_payload_view_init_from_buffer( - (const char *) &lsm, 0, sizeof(lsm)); - struct lttng_buffer_view cmd_header_view; - struct lttng_buffer_view cmd_payload_view; - struct lttng_buffer_view flat_events_view; - struct lttng_buffer_view ext_view; + lttng_payload_view_init_from_buffer((const char *) &lsm, 0, sizeof(lsm)); + unsigned int nb_events = 0; + + lttng_payload_init(&reply); - /* Safety check. An handle and channel name are mandatory */ - if (handle == NULL || channel_name == NULL) { + /* Safety check. An handle and channel name are mandatory. */ + if (handle == nullptr || channel_name == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } - lttng_payload_init(&payload); - lttng_payload_init(&payload_copy); - - lsm.cmd_type = LTTNG_LIST_EVENTS; - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + /* Initialize command parameters. */ + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS; + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_strncpy(lsm.u.list.channel_name, channel_name, - sizeof(lsm.u.list.channel_name)); + ret = lttng_strncpy(lsm.u.list.channel_name, channel_name, sizeof(lsm.u.list.channel_name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; @@ -2339,275 +2439,48 @@ int lttng_list_events(struct lttng_handle *handle, COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &payload); + /* Execute command against the session daemon. */ + ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply); if (ret < 0) { goto end; } - /* - * A copy of the payload is performed since it will be - * consumed twice. Consuming the same payload twice is invalid since - * it will cause any received file descriptor to become "shared" - * between different instances of the resulting objects. - */ - ret = lttng_payload_copy(&payload, &payload_copy); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto end; - } - - cmd_header_view = lttng_buffer_view_from_dynamic_buffer( - &payload.buffer, 0, sizeof(*cmd_header)); - if (!lttng_buffer_view_is_valid(&cmd_header_view)) { - ret = -LTTNG_ERR_INVALID_PROTOCOL; - goto end; - } - - cmd_header = (typeof(cmd_header)) cmd_header_view.data; - - /* Set number of events and free command header */ - nb_events = cmd_header->nb_events; - if (nb_events > INT_MAX) { - ret = -LTTNG_ERR_OVERFLOW; - goto end; - } - - cmd_payload_view = lttng_buffer_view_from_dynamic_buffer( - &payload.buffer, sizeof(*cmd_header), -1); - - /* - * The buffer that is returned must contain a "flat" version of - * the events that are returned. In other words, all pointers - * within an lttng_event must point to a location within the returned - * buffer so that the user may free everything by simply calling free() - * on the returned buffer. This is needed in order to maintain API - * compatibility. - * - * A first pass is performed to compute the size of the buffer that - * must be allocated. A second pass is then performed to setup - * the returned events so that their members always point within the - * buffer. - * - * The layout of the returned buffer is as follows: - * - struct lttng_event[nb_events], - * - nb_events times the following: - * - struct lttng_event_extended, - * - flattened version of userspace_probe_location - * - filter_expression - * - exclusions - * - padding to align to 64-bits - */ - ext_view = lttng_buffer_view_from_view(&cmd_payload_view, - nb_events * sizeof(struct lttng_event), -1); - comm_ext_at = ext_view.data; - storage_req = nb_events * sizeof(struct lttng_event); { - struct lttng_payload_view payload_view = - lttng_payload_view_from_payload(&payload, 0, -1); - - for (i = 0; i < nb_events; i++) { - const struct lttcomm_event_extended_header *ext_comm = - (struct lttcomm_event_extended_header *) - comm_ext_at; - int probe_storage_req = 0; - - comm_ext_at += sizeof(*ext_comm); - comm_ext_at += ext_comm->filter_len; - comm_ext_at += ext_comm->nb_exclusions * - LTTNG_SYMBOL_NAME_LEN; - - if (ext_comm->userspace_probe_location_len) { - struct lttng_userspace_probe_location - *probe_location = NULL; - struct lttng_payload_view probe_location_view = lttng_payload_view_from_view( - &payload_view, - (const char *) comm_ext_at - - payload_view.buffer.data, - ext_comm->userspace_probe_location_len); - - if (!lttng_payload_view_is_valid(&probe_location_view)) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto end; - } - - /* - * Create a temporary userspace probe location - * to determine the size needed by a "flattened" - * version of that same probe location. - */ - ret = lttng_userspace_probe_location_create_from_payload( - &probe_location_view, - &probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto end; - } - - ret = lttng_userspace_probe_location_flatten( - probe_location, NULL); - lttng_userspace_probe_location_destroy( - probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto end; - } - - probe_storage_req = ret; - comm_ext_at += ext_comm->userspace_probe_location_len; - } + const struct lttcomm_list_command_header *cmd_reply_header = nullptr; + const lttng_payload_view cmd_reply_header_view = + lttng_payload_view_from_payload(&reply, 0, sizeof(*cmd_reply_header)); - storage_req += sizeof(struct lttng_event_extended); - storage_req += ext_comm->filter_len; - storage_req += ext_comm->nb_exclusions * - LTTNG_SYMBOL_NAME_LEN; - /* Padding to ensure the flat probe is aligned. */ - storage_req = lttng_align_ceil(storage_req, sizeof(uint64_t)); - storage_req += probe_storage_req; + if (!lttng_payload_view_is_valid(&cmd_reply_header_view)) { + ret = -LTTNG_ERR_INVALID_PROTOCOL; + goto end; } - } - lttng_dynamic_buffer_init(&listing); - /* - * We must ensure that "listing" is never resized so as to preserve - * the validity of the flattened objects. - */ - ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto end; - } + cmd_reply_header = (const struct lttcomm_list_command_header *) + cmd_reply_header_view.buffer.data; + if (cmd_reply_header->count > INT_MAX) { + ret = -LTTNG_ERR_OVERFLOW; + goto end; + } - cmd_payload_view = lttng_buffer_view_from_dynamic_buffer( - &payload_copy.buffer, sizeof(*cmd_header), -1); - flat_events_view = lttng_buffer_view_from_view(&cmd_payload_view, 0, - nb_events * sizeof(struct lttng_event)); - ret = lttng_dynamic_buffer_append_view(&listing, &flat_events_view); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; + nb_events = (unsigned int) cmd_reply_header->count; } - ext_view = lttng_buffer_view_from_view(&cmd_payload_view, - nb_events * sizeof(struct lttng_event), -1); - comm_ext_at = ext_view.data; - { - struct lttng_payload_view payload_copy_view = - lttng_payload_view_from_payload( - &payload_copy, 0, -1); - - for (i = 0; i < nb_events; i++) { - struct lttng_event *event = (typeof(event))( - listing.data + - (sizeof(struct lttng_event) * i)); - const struct lttcomm_event_extended_header *ext_comm = - (typeof(ext_comm)) comm_ext_at; - struct lttng_event_extended *event_extended = - (typeof(event_extended))(listing.data + - listing.size); - - /* Insert struct lttng_event_extended. */ - ret = lttng_dynamic_buffer_set_size(&listing, - listing.size + sizeof(*event_extended)); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; - } - event->extended.ptr = event_extended; - - comm_ext_at += sizeof(*ext_comm); - - /* Insert filter expression. */ - if (ext_comm->filter_len) { - event_extended->filter_expression = - listing.data + listing.size; - ret = lttng_dynamic_buffer_append(&listing, - comm_ext_at, - ext_comm->filter_len); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; - } - comm_ext_at += ext_comm->filter_len; - } - - /* Insert exclusions. */ - if (ext_comm->nb_exclusions) { - event_extended->exclusions.count = - ext_comm->nb_exclusions; - event_extended->exclusions.strings = - listing.data + listing.size; - - ret = lttng_dynamic_buffer_append(&listing, - comm_ext_at, - ext_comm->nb_exclusions * - LTTNG_SYMBOL_NAME_LEN); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; - } - comm_ext_at += ext_comm->nb_exclusions * - LTTNG_SYMBOL_NAME_LEN; - } - - /* Insert padding to align to 64-bits. */ - ret = lttng_dynamic_buffer_set_size(&listing, - lttng_align_ceil(listing.size, - sizeof(uint64_t))); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; - } - - /* Insert flattened userspace probe location. */ - if (ext_comm->userspace_probe_location_len) { - struct lttng_userspace_probe_location - *probe_location = NULL; - struct lttng_payload_view probe_location_view = lttng_payload_view_from_view( - &payload_copy_view, - (const char *) comm_ext_at - - payload_copy_view.buffer.data, - ext_comm->userspace_probe_location_len); - - if (!lttng_payload_view_is_valid(&probe_location_view)) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto free_dynamic_buffer; - } - - ret = lttng_userspace_probe_location_create_from_payload( - &probe_location_view, - &probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto free_dynamic_buffer; - } - - event_extended->probe_location = (struct lttng_userspace_probe_location - *) (listing.data + - listing.size); - ret = lttng_userspace_probe_location_flatten( - probe_location, &listing); - lttng_userspace_probe_location_destroy( - probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto free_dynamic_buffer; - } - - comm_ext_at += ext_comm->userspace_probe_location_len; - } + enum lttng_error_code ret_code; + lttng_payload_view cmd_reply_payload = lttng_payload_view_from_payload( + &reply, sizeof(struct lttcomm_list_command_header), -1); + + ret_code = lttng_events_create_and_flatten_from_payload( + &cmd_reply_payload, nb_events, events); + if (ret_code != LTTNG_OK) { + ret = -((int) ret_code); + goto end; } } - /* Don't reset listing buffer as we return its content. */ - *events = (struct lttng_event *) listing.data; - lttng_dynamic_buffer_init(&listing); ret = (int) nb_events; -free_dynamic_buffer: - lttng_dynamic_buffer_reset(&listing); end: - lttng_payload_reset(&payload); - lttng_payload_reset(&payload_copy); + lttng_payload_reset(&reply); return ret; } @@ -2621,7 +2494,7 @@ int lttng_set_tracing_group(const char *name) int ret = 0; char *new_group; - if (name == NULL) { + if (name == nullptr) { ret = -LTTNG_ERR_INVALID; goto end; } @@ -2634,14 +2507,14 @@ int lttng_set_tracing_group(const char *name) free(tracing_group); tracing_group = new_group; - new_group = NULL; + new_group = nullptr; end: return ret; } -int lttng_calibrate(struct lttng_handle *handle, - struct lttng_calibrate *calibrate) +int lttng_calibrate(struct lttng_handle *handle __attribute__((unused)), + struct lttng_calibrate *calibrate __attribute__((unused))) { /* * This command was removed in LTTng 2.9. @@ -2653,16 +2526,16 @@ int lttng_calibrate(struct lttng_handle *handle, * Set default channel attributes. * If either or both of the arguments are null, attr content is zeroe'd. */ -void lttng_channel_set_default_attr(struct lttng_domain *domain, - struct lttng_channel_attr *attr) +void lttng_channel_set_default_attr(struct lttng_domain *domain, struct lttng_channel_attr *attr) { struct lttng_channel_extended *extended; /* Safety check */ - if (attr == NULL || domain == NULL) { + if (attr == nullptr || domain == nullptr) { return; } + /* Save the pointer for later use */ extended = (struct lttng_channel_extended *) attr->extended.ptr; memset(attr, 0, sizeof(struct lttng_channel_attr)); @@ -2673,18 +2546,11 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain, switch (domain->type) { case LTTNG_DOMAIN_KERNEL: - attr->switch_timer_interval = - DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; + attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; attr->subbuf_size = default_get_kernel_channel_subbuf_size(); attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; - if (extended) { - extended->monitor_timer_interval = - DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER; - extended->blocking_timeout = - DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT; - } break; case LTTNG_DOMAIN_UST: switch (domain->buf_type) { @@ -2692,32 +2558,16 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain, attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; - attr->switch_timer_interval = - DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; - attr->read_timer_interval = - DEFAULT_UST_UID_CHANNEL_READ_TIMER; - if (extended) { - extended->monitor_timer_interval = - DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER; - extended->blocking_timeout = - DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT; - } + attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; + attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER; break; case LTTNG_BUFFER_PER_PID: default: attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; - attr->switch_timer_interval = - DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; - attr->read_timer_interval = - DEFAULT_UST_PID_CHANNEL_READ_TIMER; - if (extended) { - extended->monitor_timer_interval = - DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER; - extended->blocking_timeout = - DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT; - } + attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; + attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER; break; } default: @@ -2725,11 +2575,16 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain, break; } + if (extended) { + lttng_channel_set_default_extended_attr(domain, extended); + } + + /* Reassign the extended pointer. */ attr->extended.ptr = extended; } int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, - uint64_t *discarded_events) + uint64_t *discarded_events) { int ret = 0; struct lttng_channel_extended *chan_ext; @@ -2754,8 +2609,7 @@ end: return ret; } -int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, - uint64_t *lost_packets) +int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, uint64_t *lost_packets) { int ret = 0; struct lttng_channel_extended *chan_ext; @@ -2781,7 +2635,7 @@ end: } int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, - uint64_t *monitor_timer_interval) + uint64_t *monitor_timer_interval) { int ret = 0; @@ -2795,14 +2649,14 @@ int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, goto end; } - *monitor_timer_interval = ((struct lttng_channel_extended *) - chan->attr.extended.ptr)->monitor_timer_interval; + *monitor_timer_interval = + ((struct lttng_channel_extended *) chan->attr.extended.ptr)->monitor_timer_interval; end: return ret; } int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, - uint64_t monitor_timer_interval) + uint64_t monitor_timer_interval) { int ret = 0; @@ -2811,15 +2665,13 @@ int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, goto end; } - ((struct lttng_channel_extended *) - chan->attr.extended.ptr)->monitor_timer_interval = - monitor_timer_interval; + ((struct lttng_channel_extended *) chan->attr.extended.ptr)->monitor_timer_interval = + monitor_timer_interval; end: return ret; } -int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, - int64_t *blocking_timeout) +int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, int64_t *blocking_timeout) { int ret = 0; @@ -2833,14 +2685,13 @@ int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, goto end; } - *blocking_timeout = ((struct lttng_channel_extended *) - chan->attr.extended.ptr)->blocking_timeout; + *blocking_timeout = + ((struct lttng_channel_extended *) chan->attr.extended.ptr)->blocking_timeout; end: return ret; } -int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, - int64_t blocking_timeout) +int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, int64_t blocking_timeout) { int ret = 0; int64_t msec_timeout; @@ -2866,9 +2717,8 @@ int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, goto end; } - ((struct lttng_channel_extended *) - chan->attr.extended.ptr)->blocking_timeout = - blocking_timeout; + ((struct lttng_channel_extended *) chan->attr.extended.ptr)->blocking_timeout = + blocking_timeout; end: return ret; } @@ -2913,24 +2763,24 @@ int lttng_session_daemon_alive(void) * Return 0 on success, else a negative value. */ int lttng_set_consumer_url(struct lttng_handle *handle, - const char *control_url, const char *data_url) + const char *control_url, + const char *data_url) { int ret; ssize_t size; struct lttcomm_session_msg lsm; - struct lttng_uri *uris = NULL; + struct lttng_uri *uris = nullptr; - if (handle == NULL || (control_url == NULL && data_url == NULL)) { + if (handle == nullptr || (control_url == nullptr && data_url == nullptr)) { ret = -LTTNG_ERR_INVALID; goto error; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_SET_CONSUMER_URI; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI; - ret = lttng_strncpy(lsm.session.name, handle->session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, handle->session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto error; @@ -2946,8 +2796,8 @@ int lttng_set_consumer_url(struct lttng_handle *handle, lsm.u.uri.size = size; - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, - sizeof(struct lttng_uri) * size, NULL); + ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( + &lsm, uris, sizeof(struct lttng_uri) * size, nullptr); free(uris); error: @@ -2957,9 +2807,8 @@ error: /* * [OBSOLETE] */ -extern "C" -LTTNG_EXPORT int lttng_enable_consumer(struct lttng_handle *handle); -int lttng_enable_consumer(struct lttng_handle *handle) +extern "C" LTTNG_EXPORT int lttng_enable_consumer(struct lttng_handle *handle); +int lttng_enable_consumer(struct lttng_handle *handle __attribute__((unused))) { return -ENOSYS; } @@ -2967,9 +2816,8 @@ int lttng_enable_consumer(struct lttng_handle *handle) /* * [OBSOLETE] */ -extern "C" -LTTNG_EXPORT int lttng_disable_consumer(struct lttng_handle *handle); -int lttng_disable_consumer(struct lttng_handle *handle) +extern "C" LTTNG_EXPORT int lttng_disable_consumer(struct lttng_handle *handle); +int lttng_disable_consumer(struct lttng_handle *handle __attribute__((unused))) { return -ENOSYS; } @@ -2977,11 +2825,11 @@ int lttng_disable_consumer(struct lttng_handle *handle) /* * [OBSOLETE] */ -extern "C" -LTTNG_EXPORT int _lttng_create_session_ext(const char *name, const char *url, - const char *datetime); -int _lttng_create_session_ext(const char *name, const char *url, - const char *datetime) +extern "C" LTTNG_EXPORT int +_lttng_create_session_ext(const char *name, const char *url, const char *datetime); +int _lttng_create_session_ext(const char *name __attribute__((unused)), + const char *url __attribute__((unused)), + const char *datetime __attribute__((unused))) { return -ENOSYS; } @@ -2995,17 +2843,16 @@ int lttng_data_pending(const char *session_name) { int ret; struct lttcomm_session_msg lsm; - uint8_t *pending = NULL; + uint8_t *pending = nullptr; - if (session_name == NULL) { + if (session_name == nullptr) { return -LTTNG_ERR_INVALID; } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_DATA_PENDING; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_DATA_PENDING; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; @@ -3045,16 +2892,15 @@ int lttng_regenerate_metadata(const char *session_name) } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_REGENERATE_METADATA; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); if (ret < 0) { goto end; } @@ -3087,16 +2933,15 @@ int lttng_regenerate_statedump(const char *session_name) } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP; - ret = lttng_strncpy(lsm.session.name, session_name, - sizeof(lsm.session.name)); + ret = lttng_strncpy(lsm.session.name, session_name, sizeof(lsm.session.name)); if (ret) { ret = -LTTNG_ERR_INVALID; goto end; } - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + ret = lttng_ctl_ask_sessiond(&lsm, nullptr); if (ret < 0) { goto end; } @@ -3106,25 +2951,28 @@ end: return ret; } -static -int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, - bool generate_name) +static int +_lttng_register_trigger(struct lttng_trigger *trigger, const char *name, bool generate_name) { int ret; struct lttcomm_session_msg lsm = { - .cmd_type = LTTNG_REGISTER_TRIGGER, + .cmd_type = LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, }; lsm.u.trigger.is_trigger_anonymous = !name && !generate_name; struct lttcomm_session_msg *message_lsm; struct lttng_payload message; struct lttng_payload reply; - struct lttng_trigger *reply_trigger = NULL; + struct lttng_trigger *reply_trigger = nullptr; enum lttng_domain_type domain_type; const struct lttng_credentials user_creds = { .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), .gid = LTTNG_OPTIONAL_INIT_UNSET, }; - const char *unused_trigger_name = NULL; + const char *unused_trigger_name = nullptr; enum lttng_trigger_status trigger_status; lttng_payload_init(&message); @@ -3165,7 +3013,7 @@ int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, * "safety" checks. */ const struct lttng_credentials *trigger_creds = - lttng_trigger_get_credentials(trigger); + lttng_trigger_get_credentials(trigger); if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { if (lttng_credentials_get_uid(&user_creds) != 0) { @@ -3180,8 +3028,7 @@ int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, goto end_unset_name; } - domain_type = lttng_trigger_get_underlying_domain_type_restriction( - trigger); + domain_type = lttng_trigger_get_underlying_domain_type_restriction(trigger); lsm.domain.type = domain_type; @@ -3207,11 +3054,9 @@ int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, { struct lttng_payload_view message_view = - lttng_payload_view_from_payload( - &message, 0, -1); + lttng_payload_view_from_payload(&message, 0, -1); - message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( - &message_view); + message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(&message_view); ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); if (ret < 0) { goto end_unset_name; @@ -3220,11 +3065,9 @@ int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, { struct lttng_payload_view reply_view = - lttng_payload_view_from_payload( - &reply, 0, reply.buffer.size); + lttng_payload_view_from_payload(&reply, 0, reply.buffer.size); - ret = lttng_trigger_create_from_payload( - &reply_view, &reply_trigger); + ret = lttng_trigger_create_from_payload(&reply_view, &reply_trigger); if (ret < 0) { ret = -LTTNG_ERR_INVALID_PROTOCOL; goto end_unset_name; @@ -3243,7 +3086,7 @@ int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, goto end; end_unset_name: - trigger_status = lttng_trigger_set_name(trigger, NULL); + trigger_status = lttng_trigger_set_name(trigger, nullptr); if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { ret = -LTTNG_ERR_UNK; } @@ -3257,34 +3100,36 @@ end: int lttng_register_trigger(struct lttng_trigger *trigger) { /* Register an anonymous trigger. */ - return _lttng_register_trigger(trigger, NULL, false); + return _lttng_register_trigger(trigger, nullptr, false); } -enum lttng_error_code lttng_register_trigger_with_name( - struct lttng_trigger *trigger, const char *name) +enum lttng_error_code lttng_register_trigger_with_name(struct lttng_trigger *trigger, + const char *name) { const int ret = _lttng_register_trigger(trigger, name, false); - return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; + return ret == 0 ? LTTNG_OK : (enum lttng_error_code) - ret; } -enum lttng_error_code lttng_register_trigger_with_automatic_name( - struct lttng_trigger *trigger) +enum lttng_error_code lttng_register_trigger_with_automatic_name(struct lttng_trigger *trigger) { - const int ret = _lttng_register_trigger(trigger, nullptr, true); + const int ret = _lttng_register_trigger(trigger, nullptr, true); - return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; + return ret == 0 ? LTTNG_OK : (enum lttng_error_code) - ret; } -enum lttng_error_code lttng_error_query_execute( - const struct lttng_error_query *query, - const struct lttng_endpoint *endpoint, - struct lttng_error_query_results **results) +enum lttng_error_code lttng_error_query_execute(const struct lttng_error_query *query, + const struct lttng_endpoint *endpoint, + struct lttng_error_query_results **results) { int ret; enum lttng_error_code ret_code; struct lttcomm_session_msg lsm = { - .cmd_type = LTTNG_EXECUTE_ERROR_QUERY, + .cmd_type = LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, }; struct lttng_payload message; struct lttng_payload reply; @@ -3316,19 +3161,16 @@ enum lttng_error_code lttng_error_query_execute( } message_lsm = (struct lttcomm_session_msg *) message.buffer.data; - message_lsm->u.error_query.length = - (uint32_t) message.buffer.size - sizeof(lsm); + message_lsm->u.error_query.length = (uint32_t) message.buffer.size - sizeof(lsm); { struct lttng_payload_view message_view = - lttng_payload_view_from_payload( - &message, 0, -1); + lttng_payload_view_from_payload(&message, 0, -1); - message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( - &message_view); + message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(&message_view); ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); if (ret < 0) { - ret_code =(lttng_error_code) -ret; + ret_code = (lttng_error_code) -ret; goto end; } } @@ -3336,11 +3178,10 @@ enum lttng_error_code lttng_error_query_execute( { ssize_t reply_create_ret; struct lttng_payload_view reply_view = - lttng_payload_view_from_payload( - &reply, 0, reply.buffer.size); + lttng_payload_view_from_payload(&reply, 0, reply.buffer.size); - reply_create_ret = lttng_error_query_results_create_from_payload( - &reply_view, results); + reply_create_ret = + lttng_error_query_results_create_from_payload(&reply_view, results); if (reply_create_ret < 0) { ret_code = LTTNG_ERR_INVALID_PROTOCOL; goto end; @@ -3361,7 +3202,7 @@ int lttng_unregister_trigger(const struct lttng_trigger *trigger) struct lttcomm_session_msg *message_lsm; struct lttng_payload message; struct lttng_payload reply; - struct lttng_trigger *copy = NULL; + struct lttng_trigger *copy = nullptr; const struct lttng_credentials user_creds = { .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), .gid = LTTNG_OPTIONAL_INIT_UNSET, @@ -3395,8 +3236,7 @@ int lttng_unregister_trigger(const struct lttng_trigger *trigger) * credentials passed on the socket. These check are all * "safety" checks. */ - const struct lttng_credentials *trigger_creds = - lttng_trigger_get_credentials(copy); + const struct lttng_credentials *trigger_creds = lttng_trigger_get_credentials(copy); if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { if (lttng_credentials_get_uid(&user_creds) != 0) { ret = -LTTNG_ERR_EPERM; @@ -3411,7 +3251,7 @@ int lttng_unregister_trigger(const struct lttng_trigger *trigger) } memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; + lsm.cmd_type = LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER; ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); if (ret) { @@ -3428,22 +3268,20 @@ int lttng_unregister_trigger(const struct lttng_trigger *trigger) /* * This is needed to populate the trigger object size for the command * header and number of fds sent. - */ + */ message_lsm = (struct lttcomm_session_msg *) message.buffer.data; message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); { struct lttng_payload_view message_view = - lttng_payload_view_from_payload( - &message, 0, -1); + lttng_payload_view_from_payload(&message, 0, -1); /* * Update the message header with the number of fd that will be * sent. */ - message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( - &message_view); + message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(&message_view); ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); if (ret < 0) { @@ -3469,28 +3307,31 @@ enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers) { int ret; enum lttng_error_code ret_code = LTTNG_OK; - struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS }; - struct lttng_triggers *local_triggers = NULL; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; + struct lttng_triggers *local_triggers = nullptr; struct lttng_payload reply; struct lttng_payload_view lsm_view = - lttng_payload_view_init_from_buffer( - (const char *) &lsm, 0, sizeof(lsm)); + lttng_payload_view_init_from_buffer((const char *) &lsm, 0, sizeof(lsm)); lttng_payload_init(&reply); ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply); if (ret < 0) { - ret_code = (enum lttng_error_code) -ret; + ret_code = (enum lttng_error_code) - ret; goto end; } { struct lttng_payload_view reply_view = - lttng_payload_view_from_payload( - &reply, 0, reply.buffer.size); + lttng_payload_view_from_payload(&reply, 0, reply.buffer.size); - ret = lttng_triggers_create_from_payload( - &reply_view, &local_triggers); + ret = lttng_triggers_create_from_payload(&reply_view, &local_triggers); if (ret < 0) { ret_code = LTTNG_ERR_FATAL; goto end; @@ -3498,7 +3339,7 @@ enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers) } *triggers = local_triggers; - local_triggers = NULL; + local_triggers = nullptr; end: lttng_payload_reset(&reply); lttng_triggers_destroy(local_triggers); @@ -3508,7 +3349,7 @@ end: /* * lib constructor. */ -static void __attribute__((constructor)) init(void) +static void __attribute__((constructor)) init() { /* Set default session group */ lttng_set_tracing_group(DEFAULT_TRACING_GROUP); @@ -3517,7 +3358,7 @@ static void __attribute__((constructor)) init(void) /* * lib destructor. */ -static void __attribute__((destructor)) lttng_ctl_exit(void) +static void __attribute__((destructor)) lttng_ctl_exit() { free(tracing_group); }