X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Flttng-ctl.cpp;h=82f10bec03228f16a5267e899e8c9efe23f11872;hb=28ab034a2c3582d07d3423d2d746731f87d3969f;hp=3e39b5b0cc216ad229b91bce5add89872cba24d2;hpb=999af9c1150784f8676c6fce0764772d2314854a;p=lttng-tools.git diff --git a/src/lib/lttng-ctl/lttng-ctl.cpp b/src/lib/lttng-ctl/lttng-ctl.cpp index 3e39b5b0c..82f10bec0 100644 --- a/src/lib/lttng-ctl/lttng-ctl.cpp +++ b/src/lib/lttng-ctl/lttng-ctl.cpp @@ -11,53 +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 +#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; @@ -83,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) { @@ -117,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; } @@ -227,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; } @@ -266,7 +265,7 @@ int lttng_check_tracing_group(void) } /* 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; @@ -291,8 +290,7 @@ end: return ret; } -static enum lttng_error_code check_enough_available_memory( - uint64_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; @@ -316,8 +314,7 @@ static enum lttng_error_code check_enough_available_memory( goto end; } - num_bytes_requested_total = - num_bytes_requested_per_cpu * (uint64_t) num_cpu; + 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 @@ -395,7 +392,7 @@ error: */ static int set_session_daemon_path(void) { - int in_tgroup = 0; /* In tracing group. */ + int in_tgroup = 0; /* In tracing group. */ uid_t uid; uid = getuid(); @@ -407,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; @@ -434,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; } @@ -496,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 = NULL; if (len) { if (!user_len) { @@ -508,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; @@ -552,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; @@ -602,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; } @@ -622,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; @@ -642,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; @@ -694,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(); @@ -723,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; - handle = (lttng_handle *) zmalloc(sizeof(struct lttng_handle)); + handle = zmalloc(); if (handle == NULL) { 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; } @@ -767,8 +763,7 @@ 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; @@ -779,9 +774,8 @@ int lttng_register_consumer(struct lttng_handle *handle, } 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; @@ -789,8 +783,7 @@ 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; @@ -817,10 +810,9 @@ int lttng_start_tracing(const char *session_name) } 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; @@ -845,10 +837,9 @@ static int _lttng_stop_tracing(const char *session_name, int wait) } 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; @@ -911,13 +902,21 @@ 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) { @@ -925,74 +924,53 @@ int lttng_add_context(struct lttng_handle *handle, 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; } @@ -1005,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, NULL, 0, NULL); } /* @@ -1018,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, NULL); } /* @@ -1041,8 +1020,8 @@ static char *set_agent_filter(const char *filter, struct lttng_event *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); } @@ -1065,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"); @@ -1097,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; + size_t bytecode_len = 0; /* * We have either a filter or some exclusions, so we need to set up @@ -1135,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 != NULL || 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) { @@ -1182,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 { /* @@ -1195,131 +1160,96 @@ 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 : NULL, + &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 : NULL, + fd_count, + view.buffer.size ? view.buffer.data : NULL, + view.buffer.size, + NULL, + NULL, + 0); } -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 @@ -1328,28 +1258,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; + 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 @@ -1372,57 +1312,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 != NULL || 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) { @@ -1430,11 +1329,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. */ @@ -1443,48 +1342,94 @@ 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, + NULL, + filter_expression, + bytecode_len, + (ctx && bytecode_len) ? &ctx->bytecode->b : NULL, + &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; -mem_error: + 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 : NULL, + fd_count, + view.buffer.size ? view.buffer.data : NULL, + view.buffer.size, + NULL, + NULL, + 0); + } + +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 @@ -1493,15 +1438,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; } @@ -1511,8 +1452,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; @@ -1585,8 +1525,7 @@ 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; @@ -1607,17 +1546,14 @@ int lttng_enable_channel(struct lttng_handle *handle, * 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) { + if (in_chan->attr.num_subbuf > UINT64_MAX / in_chan->attr.subbuf_size) { /* Overflow */ ret = -LTTNG_ERR_OVERFLOW; goto end; } - 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); + 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; @@ -1632,27 +1568,24 @@ int lttng_enable_channel(struct lttng_handle *handle, /* Populate the channel extended attribute if necessary. */ if (!channel->attr.extended.ptr) { - struct lttng_channel_extended *extended = - (struct lttng_channel_extended *) zmalloc( - sizeof(*extended)); + struct lttng_channel_extended *extended = zmalloc(); if (!extended) { ret = -LTTNG_ERR_NOMEM; goto end; } - lttng_channel_set_default_extended_attr( - &handle->domain, extended); + + lttng_channel_set_default_extended_attr(&handle->domain, extended); channel->attr.extended.ptr = extended; } /* Prepare the payload */ memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_ENABLE_CHANNEL; + 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; @@ -1666,8 +1599,7 @@ int lttng_enable_channel(struct lttng_handle *handle, lsm.u.channel.length = buffer.size; - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( - &lsm, buffer.data, buffer.size, NULL); + ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, buffer.size, NULL); end: lttng_channel_destroy(channel); lttng_dynamic_buffer_reset(&buffer); @@ -1690,10 +1622,9 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name) 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; @@ -1701,8 +1632,7 @@ 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; @@ -1719,26 +1649,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 = NULL; + 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 = NULL; + size_t cmd_header_len; + unsigned int nb_events = 0; if (handle == NULL) { - return -LTTNG_ERR_INVALID; + 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, + NULL, + 0, + NULL, + 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; } /* @@ -1747,26 +1726,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 = NULL; + unsigned int nb_event_fields = 0; + struct lttng_payload reply; + + lttng_payload_init(&reply); if (handle == NULL) { - return -LTTNG_ERR_INVALID; + 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; + } } - return ret / sizeof(struct lttng_event_field); + { + 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; + } + } + + ret = nb_event_fields; + +end: + lttng_payload_reset(&reply); + return ret; } /* @@ -1778,24 +1803,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 = NULL; + struct lttcomm_session_msg lsm = {}; + struct lttcomm_list_command_header *cmd_header = NULL; + 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, + NULL, + 0, + NULL, + 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; } /* @@ -1811,12 +1880,15 @@ 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; struct lttng_buffer_view reply_view; @@ -1834,8 +1906,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; @@ -1846,8 +1917,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; @@ -1855,8 +1925,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; @@ -1865,8 +1934,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; @@ -1877,8 +1946,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; @@ -1924,12 +1992,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, NULL); break; default: ret = -LTTNG_ERR_INVALID; @@ -1973,10 +2039,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; @@ -1997,15 +2061,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); + descriptor = + lttng_session_descriptor_snapshot_network_create(name, snapshot_url, NULL); break; default: ret = -LTTNG_ERR_INVALID; @@ -2028,8 +2088,7 @@ 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; @@ -2042,10 +2101,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, NULL, 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; @@ -2125,21 +2183,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; memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_LIST_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 = NULL; - ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); + ret = lttng_ctl_ask_sessiond(&lsm, (void **) &sessions); if (ret <= 0) { goto end; } @@ -2154,14 +2212,12 @@ int lttng_list_sessions(struct lttng_session **out_sessions) 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; } @@ -2172,8 +2228,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; @@ -2194,8 +2250,7 @@ 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; @@ -2205,17 +2260,16 @@ int lttng_set_session_shm_path(const char *session_name, } 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; @@ -2232,8 +2286,7 @@ 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; @@ -2244,16 +2297,15 @@ int lttng_list_domains(const char *session_name, } 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; } @@ -2269,8 +2321,7 @@ 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, total_payload_received; struct lttcomm_session_msg lsm; @@ -2287,9 +2338,8 @@ int lttng_list_channels(struct lttng_handle *handle, } 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; @@ -2297,9 +2347,14 @@ int lttng_list_channels(struct lttng_handle *handle, COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, - (void **) &reception_buffer, (void **) &cmd_header, - &cmd_header_len); + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + NULL, + 0, + NULL, + 0, + (void **) &reception_buffer, + (void **) &cmd_header, + &cmd_header_len); if (ret < 0) { goto end; } @@ -2324,11 +2379,10 @@ int lttng_list_channels(struct lttng_handle *handle, { enum lttng_error_code ret_code; const struct lttng_buffer_view events_view = - lttng_buffer_view_init(reception_buffer, 0, - total_payload_received); + 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); + &events_view, cmd_header->count, channels); if (ret_code != LTTNG_OK) { ret = -ret_code; goto end; @@ -2349,44 +2403,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; - - /* Safety check. An handle and channel name are mandatory */ + 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) { 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; @@ -2394,275 +2437,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; - } + const struct lttcomm_list_command_header *cmd_reply_header = NULL; + const lttng_payload_view cmd_reply_header_view = + lttng_payload_view_from_payload(&reply, 0, sizeof(*cmd_reply_header)); - /* - * 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; - } - - 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; - } + 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); - comm_ext_at += ext_comm->userspace_probe_location_len; - } + 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; } @@ -2695,8 +2511,8 @@ 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. @@ -2708,8 +2524,7 @@ 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; @@ -2729,8 +2544,7 @@ 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; @@ -2742,20 +2556,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; + 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; + attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; + attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER; break; } default: @@ -2772,7 +2582,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain, } 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; @@ -2797,8 +2607,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; @@ -2824,7 +2633,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; @@ -2838,14 +2647,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; @@ -2854,15 +2663,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; @@ -2876,14 +2683,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; @@ -2909,9 +2715,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; } @@ -2956,7 +2761,8 @@ 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; @@ -2970,10 +2776,9 @@ int lttng_set_consumer_url(struct lttng_handle *handle, 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; @@ -2989,8 +2794,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, NULL); free(uris); error: @@ -3000,9 +2805,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; } @@ -3010,9 +2814,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; } @@ -3020,11 +2823,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; } @@ -3045,10 +2848,9 @@ int lttng_data_pending(const char *session_name) } 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; @@ -3088,10 +2890,9 @@ 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; @@ -3130,10 +2931,9 @@ 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; @@ -3149,13 +2949,16 @@ 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; @@ -3208,7 +3011,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) { @@ -3223,8 +3026,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; @@ -3250,11 +3052,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; @@ -3263,11 +3063,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; @@ -3303,31 +3101,33 @@ int lttng_register_trigger(struct lttng_trigger *trigger) return _lttng_register_trigger(trigger, NULL, 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; @@ -3359,19 +3159,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; } } @@ -3379,11 +3176,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; @@ -3438,8 +3234,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; @@ -3454,7 +3249,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) { @@ -3471,22 +3266,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) { @@ -3512,28 +3305,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 lttcomm_session_msg lsm = { + .cmd_type = LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS, + .session = {}, + .domain = {}, + .u = {}, + .fd_count = 0, + }; struct lttng_triggers *local_triggers = NULL; 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;