char payload[];
} LTTNG_PACKED;
+struct lttng_event_context_comm {
+ uint32_t type;
+ /*
+ * Depending on the type.
+ * For:
+ * - LTTNG_EVENT_CONTEXT_APP_CONTEXT.
+ *
+ * -> struct lttng_event_context_app_comm
+ *
+ * For
+ * - LTTNG_EVENT_CONTEXT_PERF_COUNTER,
+ * - LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER,
+ * - LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER.
+ *
+ * -> struct lttng_event_context_perf_counter_comm
+ *
+ * Other type -> no payload.
+ */
+ char payload[];
+} LTTNG_PACKED;
+
+struct lttng_event_context_perf_counter_comm {
+ uint32_t type;
+ uint64_t config;
+ /* Includes terminator `\0`. */
+ uint32_t name_len;
+ /*
+ * char name [name_len]
+ */
+ char payload[];
+} LTTNG_PACKED;
+
+struct lttng_event_context_app_comm {
+ /* Includes terminator `\0`. */
+ uint32_t provider_name_len;
+ /* Includes terminator `\0`. */
+ uint32_t ctx_name_len;
+ /*
+ * provider name [provider_name_len]
+ * ctx name [ctx_name_len]
+ */
+ char payload[];
+} LTTNG_PACKED;
+
struct lttng_event_extended {
/*
* exclusions and filter_expression are only set when the lttng_event
struct lttng_dynamic_buffer *buf,
int *fd_to_send);
+LTTNG_HIDDEN
+ssize_t lttng_event_context_create_from_buffer(
+ const struct lttng_buffer_view *view,
+ struct lttng_event_context **event_ctx);
+
+LTTNG_HIDDEN
+int lttng_event_context_serialize(struct lttng_event_context *context,
+ struct lttng_dynamic_buffer *buf);
+
+LTTNG_HIDDEN
+void lttng_event_context_destroy(struct lttng_event_context *context);
+
LTTNG_HIDDEN
enum lttng_error_code lttng_events_create_and_flatten_from_buffer(
const struct lttng_buffer_view *view,
switch (cmd_ctx->lsm->cmd_type) {
case LTTNG_ADD_CONTEXT:
{
- /*
- * An LTTNG_ADD_CONTEXT command might have a supplementary
- * payload if the context being added is an application context.
- */
- if (cmd_ctx->lsm->u.context.ctx.ctx ==
- LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
- char *provider_name = NULL, *context_name = NULL;
- size_t provider_name_len =
- cmd_ctx->lsm->u.context.provider_name_len;
- size_t context_name_len =
- cmd_ctx->lsm->u.context.context_name_len;
-
- if (provider_name_len == 0 || context_name_len == 0) {
- /*
- * Application provider and context names MUST
- * be provided.
- */
- ret = -LTTNG_ERR_INVALID;
- goto error;
- }
-
- provider_name = zmalloc(provider_name_len + 1);
- if (!provider_name) {
- ret = -LTTNG_ERR_NOMEM;
- goto error;
- }
- cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
- provider_name;
-
- context_name = zmalloc(context_name_len + 1);
- if (!context_name) {
- ret = -LTTNG_ERR_NOMEM;
- goto error_add_context;
- }
- cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
- context_name;
-
- ret = lttcomm_recv_unix_sock(*sock, provider_name,
- provider_name_len);
- if (ret < 0) {
- goto error_add_context;
- }
-
- ret = lttcomm_recv_unix_sock(*sock, context_name,
- context_name_len);
- if (ret < 0) {
- goto error_add_context;
- }
- }
-
- /*
- * cmd_add_context assumes ownership of the provider and context
- * names.
- */
- ret = cmd_add_context(cmd_ctx->session,
- cmd_ctx->lsm->domain.type,
- cmd_ctx->lsm->u.context.channel_name,
- ALIGNED_CONST_PTR(cmd_ctx->lsm->u.context.ctx),
- kernel_poll_pipe[1]);
-
- cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
- cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
-error_add_context:
- free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
- free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
- if (ret < 0) {
- goto error;
- }
+ ret = cmd_add_context(cmd_ctx, *sock, kernel_poll_pipe[1]);
break;
}
case LTTNG_DISABLE_CHANNEL:
/*
* Command LTTNG_ADD_CONTEXT processed by the client thread.
*/
-int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
- char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
+
+int cmd_add_context(struct command_ctx *cmd_ctx, int sock, int kwpipe)
{
+ struct lttng_event_context *ctx = NULL;
int ret, chan_kern_created = 0, chan_ust_created = 0;
- char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
+ struct lttng_dynamic_buffer buffer;
+ struct lttng_buffer_view view;
+ size_t context_len;
+ ssize_t sock_recv_len;
+ enum lttng_domain_type domain = cmd_ctx->lsm->domain.type;
+ struct ltt_session *session = cmd_ctx->session;
+ const char *channel_name = cmd_ctx->lsm->u.context.channel_name;
+
+ lttng_dynamic_buffer_init(&buffer);
+
+ context_len = (size_t) cmd_ctx->lsm->u.context.length;
+ ret = lttng_dynamic_buffer_set_size(&buffer, context_len);
+ if (ret) {
+ ret = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ sock_recv_len = lttcomm_recv_unix_sock(sock, buffer.data, context_len);
+ if (sock_recv_len < 0 || sock_recv_len != context_len) {
+ ERR("Failed to receive \"add context\" command payload");
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ view = lttng_buffer_view_from_dynamic_buffer(&buffer, 0, -1);
+
+ if (lttng_event_context_create_from_buffer(&view, &ctx) !=
+ context_len) {
+ ERR("Invalid event context payload received in \"add context\" command");
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
/*
* Don't try to add a context if the session has been started at
* some point in time before. The tracer does not allow it and would
* result in a corrupted trace.
*/
- if (session->has_been_started) {
+ if (cmd_ctx->session->has_been_started) {
ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
goto end;
}
- if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
- app_ctx_provider_name = ctx->u.app_ctx.provider_name;
- app_ctx_name = ctx->u.app_ctx.ctx_name;
- }
-
switch (domain) {
case LTTNG_DOMAIN_KERNEL:
assert(session->kernel_session);
}
ret = context_ust_add(usess, domain, ctx, channel_name);
- free(app_ctx_provider_name);
- free(app_ctx_name);
- app_ctx_name = NULL;
- app_ctx_provider_name = NULL;
if (ret != LTTNG_OK) {
goto error;
}
trace_ust_destroy_channel(uchan);
}
end:
- free(app_ctx_provider_name);
- free(app_ctx_name);
+ lttng_event_context_destroy(ctx);
return ret;
}
/* Event commands */
int cmd_disable_event(struct command_ctx *cmd_ctx, int sock);
-int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
- char *channel_name, const struct lttng_event_context *ctx, int kwpipe);
+int cmd_add_context(struct command_ctx *cmd_ctx, int sock, int kwpipe);
int cmd_set_filter(struct ltt_session *session, enum lttng_domain_type domain,
char *channel_name, struct lttng_event *event,
struct lttng_filter_bytecode *bytecode);
* Add kernel context to tracer.
*/
int context_kernel_add(struct ltt_kernel_session *ksession,
- const struct lttng_event_context *ctx, char *channel_name)
+ const struct lttng_event_context *ctx,
+ const char *channel_name)
{
int ret;
struct ltt_kernel_channel *kchan;
int context_ust_add(struct ltt_ust_session *usess,
enum lttng_domain_type domain,
const struct lttng_event_context *ctx,
- char *channel_name)
+ const char *channel_name)
{
int ret = LTTNG_OK;
struct lttng_ht_iter iter;
#include "lttng-ust-ctl.h"
int context_kernel_add(struct ltt_kernel_session *ksession,
- const struct lttng_event_context *ctx, char *channel_name);
+ const struct lttng_event_context *ctx,
+ const char *channel_name);
int context_ust_add(struct ltt_ust_session *usess,
enum lttng_domain_type domain,
const struct lttng_event_context *ctx,
- char *channel_name);
+ const char *channel_name);
#endif /* _LTT_CONTEXT_H */
return ret;
}
+static ssize_t lttng_event_context_app_populate_from_buffer(
+ const struct lttng_buffer_view *view,
+ struct lttng_event_context *event_ctx)
+{
+ ssize_t ret, offset = 0;
+ const struct lttng_event_context_app_comm *comm;
+ char *provider_name = NULL, *context_name = NULL;
+ size_t provider_name_len, context_name_len;
+ const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
+ view, offset, sizeof(*comm));
+
+ assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
+
+ if (!lttng_buffer_view_is_valid(&comm_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ comm = (typeof(comm)) comm_view.data;
+ offset += sizeof(*comm);
+
+ provider_name_len = comm->provider_name_len;
+ context_name_len = comm->ctx_name_len;
+
+ if (provider_name_len == 0 || context_name_len == 0) {
+ /*
+ * Application provider and context names MUST
+ * be provided.
+ */
+ ret = -1;
+ goto end;
+ }
+
+ {
+ const char *name;
+ const struct lttng_buffer_view provider_name_view =
+ lttng_buffer_view_from_view(view, offset,
+ provider_name_len);
+
+ if (!lttng_buffer_view_is_valid(&provider_name_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ name = provider_name_view.data;
+
+ if (!lttng_buffer_view_contains_string(&provider_name_view,
+ name, provider_name_len)) {
+ ret = -1;
+ goto end;
+ }
+
+ provider_name = lttng_strndup(name, provider_name_len);
+ if (!provider_name) {
+ ret = -1;
+ goto end;
+ }
+ offset += provider_name_len;
+ }
+
+ {
+ const char *name;
+ const struct lttng_buffer_view context_name_view =
+ lttng_buffer_view_from_view(
+ view, offset, context_name_len);
+
+ if (!lttng_buffer_view_is_valid(&context_name_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ name = context_name_view.data;
+
+ if (!lttng_buffer_view_contains_string(&context_name_view, name,
+ context_name_len)) {
+ ret = -1;
+ goto end;
+ }
+
+ context_name = lttng_strndup(name, context_name_len);
+ if (!context_name) {
+ ret = -1;
+ goto end;
+ }
+
+ offset += context_name_len;
+ }
+
+ /* Transfer ownership of the strings */
+ event_ctx->u.app_ctx.provider_name = provider_name;
+ event_ctx->u.app_ctx.ctx_name = context_name;
+ provider_name = NULL;
+ context_name = NULL;
+
+ ret = offset;
+end:
+ free(provider_name);
+ free(context_name);
+
+ return ret;
+}
+
+static ssize_t lttng_event_context_perf_counter_populate_from_buffer(
+ const struct lttng_buffer_view *view,
+ struct lttng_event_context *event_ctx)
+{
+ ssize_t ret, offset = 0;
+ const struct lttng_event_context_perf_counter_comm *comm;
+ size_t name_len;
+ const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
+ view, offset, sizeof(*comm));
+
+ assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER ||
+ event_ctx->ctx ==
+ LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER ||
+ event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER);
+
+ if (!lttng_buffer_view_is_valid(&comm_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ comm = (typeof(comm)) comm_view.data;
+ offset += sizeof(*comm);
+
+ name_len = comm->name_len;
+ {
+ const char *name;
+ const struct lttng_buffer_view provider_name_view =
+ lttng_buffer_view_from_view(
+ view, offset, name_len);
+ if (!lttng_buffer_view_is_valid(&provider_name_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ name = provider_name_view.data;
+
+ if (!lttng_buffer_view_contains_string(
+ &provider_name_view, name, name_len)) {
+ ret = -1;
+ goto end;
+ }
+
+ lttng_strncpy(event_ctx->u.perf_counter.name, name, name_len);
+ offset += name_len;
+ }
+
+ event_ctx->u.perf_counter.config = comm->config;
+ event_ctx->u.perf_counter.type = comm->type;
+
+ ret = offset;
+
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+ssize_t lttng_event_context_create_from_buffer(
+ const struct lttng_buffer_view *view,
+ struct lttng_event_context **event_ctx)
+{
+ ssize_t ret, offset = 0;
+ const struct lttng_event_context_comm *comm;
+ struct lttng_event_context *local_context = NULL;
+ struct lttng_buffer_view subtype_view;
+ struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
+ view, offset, sizeof(*comm));
+
+ assert(event_ctx);
+ assert(view);
+
+ if (!lttng_buffer_view_is_valid(&comm_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ comm = (typeof(comm)) comm_view.data;
+ offset += sizeof(*comm);
+
+ local_context = zmalloc(sizeof(*local_context));
+ if (!local_context) {
+ ret = -1;
+ goto end;
+ }
+
+ local_context->ctx = comm->type;
+
+ subtype_view = lttng_buffer_view_from_view(view, offset, -1);
+
+ switch (local_context->ctx) {
+ case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
+ ret = lttng_event_context_app_populate_from_buffer(
+ &subtype_view, local_context);
+ break;
+ case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
+ case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
+ case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
+ ret = lttng_event_context_perf_counter_populate_from_buffer(
+ &subtype_view, local_context);
+ break;
+ default:
+ /* Nothing else to deserialize. */
+ ret = 0;
+ break;
+ }
+
+ if (ret < 0) {
+ goto end;
+ }
+
+ offset += ret;
+
+ *event_ctx = local_context;
+ local_context = NULL;
+ ret = offset;
+
+end:
+ free(local_context);
+ return ret;
+}
+
+static int lttng_event_context_app_serialize(
+ struct lttng_event_context *context,
+ struct lttng_dynamic_buffer *buffer)
+{
+ int ret;
+ struct lttng_event_context_app_comm comm = { 0 };
+ size_t provider_len, ctx_len;
+ const char *provider_name;
+ const char *ctx_name;
+
+ assert(buffer);
+ assert(context);
+ assert(context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
+
+ provider_name = context->u.app_ctx.provider_name;
+ ctx_name = context->u.app_ctx.ctx_name;
+
+ if (!provider_name || !ctx_name) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ provider_len = strlen(provider_name);
+ if (provider_len == 0) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ /* Include the null terminator. */
+ comm.provider_name_len = provider_len + 1;
+
+ ctx_len = strlen(ctx_name);
+ if (ctx_len == 0) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ /* Include the null terminator. */
+ comm.ctx_name_len = ctx_len + 1;
+
+ /* Header */
+ ret = lttng_dynamic_buffer_append(buffer, &comm, sizeof(comm));
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+ ret = lttng_dynamic_buffer_append(buffer, provider_name, provider_len);
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+ ret = lttng_dynamic_buffer_append(buffer, ctx_name, ctx_len);
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+static int lttng_event_context_perf_counter_serialize(
+ struct lttng_event_perf_counter_ctx *context,
+ struct lttng_dynamic_buffer *buffer)
+{
+ int ret;
+ struct lttng_event_context_perf_counter_comm comm = { 0 };
+
+ assert(buffer);
+ assert(context);
+
+ comm.config = context->config;
+ comm.type = context->type;
+ comm.name_len = lttng_strnlen(context->name, LTTNG_SYMBOL_NAME_LEN);
+
+ if (comm.name_len == LTTNG_SYMBOL_NAME_LEN) {
+ ret = -1;
+ goto end;
+ }
+
+ /* Include the null terminator. */
+ comm.name_len += 1;
+
+ /* Header */
+ ret = lttng_dynamic_buffer_append(buffer, &comm, sizeof(comm));
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+ ret = lttng_dynamic_buffer_append(buffer, context->name, comm.name_len);
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_event_context_serialize(struct lttng_event_context *context,
+ struct lttng_dynamic_buffer *buf)
+{
+ int ret;
+ struct lttng_event_context_comm context_comm = { 0 };
+
+ assert(context);
+ assert(buf);
+
+ context_comm.type = (uint32_t) context->ctx;
+
+ /* Header */
+ ret = lttng_dynamic_buffer_append(
+ buf, &context_comm, sizeof(context_comm));
+ if (ret) {
+ goto end;
+ }
+
+ switch (context->ctx) {
+ case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
+ ret = lttng_event_context_app_serialize(context, buf);
+ break;
+ case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
+ case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
+ case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
+ ret = lttng_event_context_perf_counter_serialize(
+ &context->u.perf_counter, buf);
+ break;
+ default:
+ /* Nothing else to serialize. */
+ break;
+ }
+
+ if (ret) {
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+void lttng_event_context_destroy(struct lttng_event_context *context)
+{
+ if (!context) {
+ return;
+ }
+
+ if (context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
+ free(context->u.app_ctx.provider_name);
+ free(context->u.app_ctx.ctx_name);
+ }
+
+ free(context);
+}
static enum lttng_error_code compute_flattened_size(
struct lttng_dynamic_pointer_array *events, size_t *size)
{
/* Context */
struct {
char channel_name[LTTNG_SYMBOL_NAME_LEN];
- struct lttng_event_context ctx LTTNG_PACKED;
- uint32_t provider_name_len;
- uint32_t context_name_len;
+ uint32_t length;
} LTTNG_PACKED context;
/* Use by register_consumer */
struct {
const char *channel_name)
{
int ret;
- size_t len = 0;
- char *buf = NULL;
struct lttcomm_session_msg lsm;
+ struct lttng_dynamic_buffer buffer;
+
+ lttng_dynamic_buffer_init(&buffer);
/* Safety check. Both are mandatory. */
if (handle == NULL || ctx == NULL) {
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;
-
- if (!provider_name || !ctx_name) {
- ret = -LTTNG_ERR_INVALID;
- goto end;
- }
-
- provider_len = strlen(provider_name);
- if (provider_len == 0) {
- ret = -LTTNG_ERR_INVALID;
- goto end;
- }
- lsm.u.context.provider_name_len = provider_len;
-
- ctx_len = strlen(ctx_name);
- if (ctx_len == 0) {
- ret = -LTTNG_ERR_INVALID;
- goto end;
- }
- lsm.u.context.context_name_len = ctx_len;
-
- len = provider_len + ctx_len;
- buf = zmalloc(len);
- if (!buf) {
- ret = -LTTNG_ERR_NOMEM;
- goto end;
- }
-
- memcpy(buf, provider_name, provider_len);
- memcpy(buf + provider_len, ctx_name, ctx_len);
+ ret = lttng_event_context_serialize(ctx, &buffer);
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
}
- 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;
- }
+ lsm.u.context.length = buffer.size;
- ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
+ ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
+ &lsm, buffer.data, buffer.size, NULL);
end:
- free(buf);
+ lttng_dynamic_buffer_reset(&buffer);
return ret;
}