From 166fc58650170d8213cd7d43c68d9a6601336dd0 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 12 Jan 2022 18:18:08 -0500 Subject: [PATCH] Fix: liblttng-ctl comm: lttng_event is not packed MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Observed issue ============== In `lttcomm_session_msg` the lttng_event struct is marked as LTTNG_PACKED. This statement have no effect as explained in commit [2]. Solution ======== Adopt a similar pattern to the new API with a "serialize" & "create_from_buffer" approach. Most of the complexity is moved to `src/common/event.c` Known drawbacks ========= None. Reference ======== [1] https://review.lttng.org/gitweb?p=lttng-tools.git;a=commit;h=7bd95aee4660c6419a4a65429fc27754481e7e90 Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: I35d848519dacb2b119324e88f262aa95951e4ac6 --- include/lttng/event-internal.h | 84 ++ src/bin/lttng-sessiond/client.c | 363 +------ src/bin/lttng-sessiond/cmd.c | 728 +++++++------ src/bin/lttng-sessiond/cmd.h | 28 +- src/common/event.c | 1222 +++++++++++++++++++++- src/common/sessiond-comm/sessiond-comm.h | 37 +- src/common/userspace-probe.c | 41 +- src/lib/lttng-ctl/lttng-ctl.c | 602 ++++------- 8 files changed, 2004 insertions(+), 1101 deletions(-) diff --git a/include/lttng/event-internal.h b/include/lttng/event-internal.h index b3df4c9c2..e3aceaa70 100644 --- a/include/lttng/event-internal.h +++ b/include/lttng/event-internal.h @@ -15,7 +15,67 @@ #include #include +struct lttng_event_exclusion; +struct lttng_filter_bytecode; struct lttng_userspace_probe_location; +struct lttng_dynamic_buffer; +struct lttng_buffer_view; + +struct lttng_event_comm { + int8_t event_type; + int8_t loglevel_type; + int32_t loglevel; + int8_t enabled; + int32_t pid; + uint32_t flags; + + /* Payload. */ + /* Includes terminator `\0`. */ + uint32_t name_len; + uint32_t exclusion_count; + /* Includes terminator `\0`. */ + uint32_t filter_expression_len; + uint32_t bytecode_len; + + /* Type specific payload. */ + uint32_t userspace_probe_location_len; + uint32_t lttng_event_probe_attr_len; + uint32_t lttng_event_function_attr_len; + + /* + * Contain: + * - name [name_len], + * - exclusions if any + * - char filter_expression[filter_expression_len], + * - unsigned char filter_bytecode[bytecode_len], + * - userspace probe location [userspace_probe_location_len], + * - probe or ftrace based on event type. + */ + + char payload[]; +} LTTNG_PACKED; + +struct lttng_event_exclusion_comm { + /* Includes terminator `\0`. */ + uint32_t len; + char payload []; +} LTTNG_PACKED; + +struct lttng_event_probe_attr_comm { + uint64_t addr; + uint64_t offset; + /* Includes terminator `\0`. */ + uint32_t symbol_name_len; + + char payload[]; +} LTTNG_PACKED; + +struct lttng_event_function_attr_comm { + /* Includes terminator `\0`. */ + uint32_t symbol_name_len; + + char payload[]; +} LTTNG_PACKED; struct lttng_event_extended { /* @@ -36,4 +96,28 @@ struct lttng_event_extended { LTTNG_HIDDEN struct lttng_event *lttng_event_copy(const struct lttng_event *event); +LTTNG_HIDDEN +ssize_t lttng_event_create_from_buffer(const struct lttng_buffer_view *view, + struct lttng_event **event, + struct lttng_event_exclusion **exclusion, + char **filter_expression, + struct lttng_filter_bytecode **bytecode, + int sock); + +LTTNG_HIDDEN +int lttng_event_serialize(const struct lttng_event *event, + unsigned int exclusion_count, + char **exclusion_list, + char *filter_expression, + size_t bytecode_len, + void *bytecode, + struct lttng_dynamic_buffer *buf, + int *fd_to_send); + +LTTNG_HIDDEN +enum lttng_error_code lttng_events_create_and_flatten_from_buffer( + const struct lttng_buffer_view *view, + unsigned int count, + struct lttng_event **events); + #endif /* LTTNG_EVENT_INTERNAL_H */ diff --git a/src/bin/lttng-sessiond/client.c b/src/bin/lttng-sessiond/client.c index 5e95a59f0..0d56e42f5 100644 --- a/src/bin/lttng-sessiond/client.c +++ b/src/bin/lttng-sessiond/client.c @@ -534,112 +534,6 @@ static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) return i; } -static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock, - int *sock_error, struct lttng_event *event) -{ - int fd, ret; - struct lttng_userspace_probe_location *probe_location; - const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; - struct lttng_dynamic_buffer probe_location_buffer; - struct lttng_buffer_view buffer_view; - - /* - * Create a buffer to store the serialized version of the probe - * location. - */ - lttng_dynamic_buffer_init(&probe_location_buffer); - ret = lttng_dynamic_buffer_set_size(&probe_location_buffer, - cmd_ctx->lsm->u.enable.userspace_probe_location_len); - if (ret) { - ret = LTTNG_ERR_NOMEM; - goto error; - } - - /* - * Receive the probe location. - */ - ret = lttcomm_recv_unix_sock(sock, probe_location_buffer.data, - probe_location_buffer.size); - if (ret <= 0) { - DBG("Nothing recv() from client var len data... continuing"); - *sock_error = 1; - lttng_dynamic_buffer_reset(&probe_location_buffer); - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - buffer_view = lttng_buffer_view_from_dynamic_buffer( - &probe_location_buffer, 0, probe_location_buffer.size); - - /* - * Extract the probe location from the serialized version. - */ - ret = lttng_userspace_probe_location_create_from_buffer( - &buffer_view, &probe_location); - if (ret < 0) { - WARN("Failed to create a userspace probe location from the received buffer"); - lttng_dynamic_buffer_reset( &probe_location_buffer); - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - /* - * Receive the file descriptor to the target binary from the client. - */ - DBG("Receiving userspace probe target FD from client ..."); - ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); - if (ret <= 0) { - DBG("Nothing recv() from client userspace probe fd... continuing"); - *sock_error = 1; - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - /* - * Set the file descriptor received from the client through the unix - * socket in the probe location. - */ - lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); - if (!lookup) { - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - /* - * From the kernel tracer's perspective, all userspace probe event types - * are all the same: a file and an offset. - */ - switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { - case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: - ret = lttng_userspace_probe_location_function_set_binary_fd( - probe_location, fd); - break; - case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: - ret = lttng_userspace_probe_location_tracepoint_set_binary_fd( - probe_location, fd); - break; - default: - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - if (ret) { - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - /* Attach the probe location to the event. */ - ret = lttng_event_set_userspace_probe_location(event, probe_location); - if (ret) { - ret = LTTNG_ERR_PROBE_LOCATION_INVAL; - goto error; - } - - lttng_dynamic_buffer_reset(&probe_location_buffer); -error: - return ret; -} - /* * Version of setup_lttng_msg() without command header. */ @@ -1165,36 +1059,7 @@ error_add_context: } case LTTNG_DISABLE_EVENT: { - - /* - * FIXME: handle filter; for now we just receive the filter's - * bytecode along with the filter expression which are sent by - * liblttng-ctl and discard them. - * - * This fixes an issue where the client may block while sending - * the filter payload and encounter an error because the session - * daemon closes the socket without ever handling this data. - */ - size_t count = cmd_ctx->lsm->u.disable.expression_len + - cmd_ctx->lsm->u.disable.bytecode_len; - - if (count) { - char data[LTTNG_FILTER_MAX_LEN]; - - DBG("Discarding disable event command payload of size %zu", count); - while (count) { - ret = lttcomm_recv_unix_sock(*sock, data, - count > sizeof(data) ? sizeof(data) : count); - if (ret < 0) { - goto error; - } - - count -= (size_t) ret; - } - } - ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, - cmd_ctx->lsm->u.disable.channel_name, - ALIGNED_CONST_PTR(cmd_ctx->lsm->u.disable.event)); + ret = cmd_disable_event(cmd_ctx, *sock); break; } case LTTNG_ENABLE_CHANNEL: @@ -1389,162 +1254,32 @@ error_add_context: } case LTTNG_ENABLE_EVENT: { - struct lttng_event *ev = NULL; - struct lttng_event_exclusion *exclusion = NULL; - struct lttng_filter_bytecode *bytecode = NULL; - char *filter_expression = NULL; - - /* Handle exclusion events and receive it from the client. */ - if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { - size_t count = cmd_ctx->lsm->u.enable.exclusion_count; - - exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + - (count * LTTNG_SYMBOL_NAME_LEN)); - if (!exclusion) { - ret = LTTNG_ERR_EXCLUSION_NOMEM; - goto error; - } - - DBG("Receiving var len exclusion event list from client ..."); - exclusion->count = count; - ret = lttcomm_recv_unix_sock(*sock, exclusion->names, - count * LTTNG_SYMBOL_NAME_LEN); - if (ret <= 0) { - DBG("Nothing recv() from client var len data... continuing"); - *sock_error = 1; - free(exclusion); - ret = LTTNG_ERR_EXCLUSION_INVAL; - goto error; - } - } - - /* Get filter expression from client. */ - if (cmd_ctx->lsm->u.enable.expression_len > 0) { - size_t expression_len = - cmd_ctx->lsm->u.enable.expression_len; - - if (expression_len > LTTNG_FILTER_MAX_LEN) { - ret = LTTNG_ERR_FILTER_INVAL; - free(exclusion); - goto error; - } - - filter_expression = zmalloc(expression_len); - if (!filter_expression) { - free(exclusion); - ret = LTTNG_ERR_FILTER_NOMEM; - goto error; - } - - /* Receive var. len. data */ - DBG("Receiving var len filter's expression from client ..."); - ret = lttcomm_recv_unix_sock(*sock, filter_expression, - expression_len); - if (ret <= 0) { - DBG("Nothing recv() from client var len data... continuing"); - *sock_error = 1; - free(filter_expression); - free(exclusion); - ret = LTTNG_ERR_FILTER_INVAL; - goto error; - } - } - - /* Handle filter and get bytecode from client. */ - if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { - size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; - - if (bytecode_len > LTTNG_FILTER_MAX_LEN) { - ret = LTTNG_ERR_FILTER_INVAL; - free(filter_expression); - free(exclusion); - goto error; - } - - bytecode = zmalloc(bytecode_len); - if (!bytecode) { - free(filter_expression); - free(exclusion); - ret = LTTNG_ERR_FILTER_NOMEM; - goto error; - } - - /* Receive var. len. data */ - DBG("Receiving var len filter's bytecode from client ..."); - ret = lttcomm_recv_unix_sock(*sock, bytecode, bytecode_len); - if (ret <= 0) { - DBG("Nothing recv() from client var len data... continuing"); - *sock_error = 1; - free(filter_expression); - free(bytecode); - free(exclusion); - ret = LTTNG_ERR_FILTER_INVAL; - goto error; - } - - if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { - free(filter_expression); - free(bytecode); - free(exclusion); - ret = LTTNG_ERR_FILTER_INVAL; - goto error; - } - } - - ev = lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx->lsm->u.enable.event)); - if (!ev) { - DBG("Failed to copy event: %s", - cmd_ctx->lsm->u.enable.event.name); - free(filter_expression); - free(bytecode); - free(exclusion); - ret = LTTNG_ERR_NOMEM; - goto error; - } - - - if (cmd_ctx->lsm->u.enable.userspace_probe_location_len > 0) { - /* Expect a userspace probe description. */ - ret = receive_userspace_probe(cmd_ctx, *sock, sock_error, ev); - if (ret) { - free(filter_expression); - free(bytecode); - free(exclusion); - lttng_event_destroy(ev); - goto error; - } - } - - ret = cmd_enable_event(cmd_ctx->session, - ALIGNED_CONST_PTR(cmd_ctx->lsm->domain), - cmd_ctx->lsm->u.enable.channel_name, - ev, - filter_expression, bytecode, exclusion, - kernel_poll_pipe[1]); - lttng_event_destroy(ev); + ret = cmd_enable_event(cmd_ctx, *sock, kernel_poll_pipe[1]); break; } case LTTNG_LIST_TRACEPOINTS: { - struct lttng_event *events; - ssize_t nb_events; + enum lttng_error_code ret_code; + unsigned int nb_events; + struct lttcomm_list_command_header cmd_header = { 0 }; session_lock_list(); - nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); + ret_code = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, + &payload, &nb_events); session_unlock_list(); - if (nb_events < 0) { - /* Return value is a negative lttng_error_code. */ - ret = -nb_events; + if (ret_code != LTTNG_OK) { + ret = (int) ret_code; goto error; } - /* - * Setup lttng message with payload size set to the event list size in - * bytes and then copy list into the llm payload. - */ - ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, - sizeof(struct lttng_event) * nb_events); - free(events); + if (nb_events > UINT32_MAX) { + ret = LTTNG_ERR_OVERFLOW; + goto error; + } + + cmd_header.count = (uint32_t) nb_events; + ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size, + &cmd_header, sizeof(cmd_header)); if (ret < 0) { goto setup_error; @@ -1585,23 +1320,24 @@ error_add_context: } case LTTNG_LIST_SYSCALLS: { - struct lttng_event *events; - ssize_t nb_events; + enum lttng_error_code ret_code; + uint32_t nb_syscalls; + struct lttcomm_list_command_header cmd_header = { 0 }; - nb_events = cmd_list_syscalls(&events); - if (nb_events < 0) { - /* Return value is a negative lttng_error_code. */ - ret = -nb_events; + ret_code = cmd_list_syscalls(&payload, &nb_syscalls); + if (ret_code != LTTNG_OK) { + ret = (int) ret_code; goto error; } - /* - * Setup lttng message with payload size set to the event list size in - * bytes and then copy list into the llm payload. - */ - ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, - sizeof(struct lttng_event) * nb_events); - free(events); + if (nb_syscalls > UINT32_MAX) { + ret = LTTNG_ERR_OVERFLOW; + goto error; + } + + cmd_header.count = (uint32_t) nb_syscalls; + ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size, + &cmd_header, sizeof(cmd_header)); if (ret < 0) { goto setup_error; @@ -1729,27 +1465,28 @@ error_add_context: } case LTTNG_LIST_EVENTS: { - ssize_t nb_event; - struct lttng_event *events = NULL; - struct lttcomm_event_command_header cmd_header; - size_t total_size; - - memset(&cmd_header, 0, sizeof(cmd_header)); - /* Extended infos are included at the end of events */ - nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, - cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name, - &events, &total_size); - - if (nb_event < 0) { - /* Return value is a negative lttng_error_code. */ - ret = -nb_event; + enum lttng_error_code ret_code; + unsigned int nb_events; + struct lttcomm_list_command_header cmd_header = { 0 }; + + ret_code = cmd_list_events(cmd_ctx->lsm->domain.type, + cmd_ctx->session, + cmd_ctx->lsm->u.list.channel_name, &payload, + &nb_events); + + if (ret_code != LTTNG_OK) { + ret = (int) ret_code; goto error; } - cmd_header.nb_events = nb_event; - ret = setup_lttng_msg(cmd_ctx, events, total_size, - &cmd_header, sizeof(cmd_header)); - free(events); + if (nb_events > UINT32_MAX) { + ret = LTTNG_ERR_OVERFLOW; + goto error; + } + + cmd_header.count = (uint32_t) nb_events; + ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size, + &cmd_header, sizeof(cmd_header)); if (ret < 0) { goto setup_error; diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 01dc3bb74..efa46cc28 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -17,28 +17,28 @@ #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 "channel.h" #include "consumer.h" @@ -285,203 +285,99 @@ end: return ret; } -static int increment_extended_len(const char *filter_expression, - struct lttng_event_exclusion *exclusion, - const struct lttng_userspace_probe_location *probe_location, - size_t *extended_len) -{ - int ret = 0; - - *extended_len += sizeof(struct lttcomm_event_extended_header); - - if (filter_expression) { - *extended_len += strlen(filter_expression) + 1; - } - - if (exclusion) { - *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN; - } - - if (probe_location) { - ret = lttng_userspace_probe_location_serialize(probe_location, - NULL, NULL); - if (ret < 0) { - goto end; - } - *extended_len += ret; - } - ret = 0; -end: - return ret; -} - -static int append_extended_info(const char *filter_expression, - struct lttng_event_exclusion *exclusion, - struct lttng_userspace_probe_location *probe_location, - void **extended_at) -{ - int ret = 0; - size_t filter_len = 0; - size_t nb_exclusions = 0; - size_t userspace_probe_location_len = 0; - struct lttng_dynamic_buffer location_buffer; - struct lttcomm_event_extended_header extended_header; - - if (filter_expression) { - filter_len = strlen(filter_expression) + 1; - } - - if (exclusion) { - nb_exclusions = exclusion->count; - } - - if (probe_location) { - lttng_dynamic_buffer_init(&location_buffer); - ret = lttng_userspace_probe_location_serialize(probe_location, - &location_buffer, NULL); - if (ret < 0) { - ret = -1; - goto end; - } - userspace_probe_location_len = location_buffer.size; - } - - /* Set header fields */ - extended_header.filter_len = filter_len; - extended_header.nb_exclusions = nb_exclusions; - extended_header.userspace_probe_location_len = userspace_probe_location_len; - - /* Copy header */ - memcpy(*extended_at, &extended_header, sizeof(extended_header)); - *extended_at += sizeof(extended_header); - - /* Copy filter string */ - if (filter_expression) { - memcpy(*extended_at, filter_expression, filter_len); - *extended_at += filter_len; - } - - /* Copy exclusion names */ - if (exclusion) { - size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN; - - memcpy(*extended_at, &exclusion->names, len); - *extended_at += len; - } - - if (probe_location) { - memcpy(*extended_at, location_buffer.data, location_buffer.size); - *extended_at += location_buffer.size; - lttng_dynamic_buffer_reset(&location_buffer); - } - ret = 0; -end: - return ret; -} - /* * Create a list of agent domain events. * * Return number of events in list on success or else a negative value. */ -static int list_lttng_agent_events(struct agent *agt, - struct lttng_event **events, size_t *total_size) +static enum lttng_error_code list_lttng_agent_events( + struct agent *agt, struct lttng_dynamic_buffer *buffer, + unsigned int *nb_events) { - int i = 0, ret = 0; - unsigned int nb_event = 0; + enum lttng_error_code ret_code; + int ret = 0; + unsigned int local_nb_events = 0; struct agent_event *event; - struct lttng_event *tmp_events = NULL; struct lttng_ht_iter iter; - size_t extended_len = 0; - void *extended_at; + unsigned long tmp; assert(agt); - assert(events); + assert(buffer); DBG3("Listing agent events"); rcu_read_lock(); - nb_event = lttng_ht_get_count(agt->events); - if (nb_event == 0) { - ret = nb_event; - *total_size = 0; + tmp = lttng_ht_get_count(agt->events); + if (tmp == 0) { + /* Early exit. */ + goto end; + } + + if (tmp > UINT_MAX) { + ret_code = LTTNG_ERR_OVERFLOW; goto error; } - /* Compute required extended infos size */ - extended_len = nb_event * sizeof(struct lttcomm_event_extended_header); + local_nb_events = (unsigned int) tmp; - /* - * This is only valid because the commands which add events are - * processed in the same thread as the listing. - */ cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { - ret = increment_extended_len(event->filter_expression, NULL, NULL, - &extended_len); - if (ret) { - DBG("Error computing the length of extended info message"); - ret = -LTTNG_ERR_FATAL; + struct lttng_event *tmp = lttng_event_create(); + + if (!tmp) { + ret_code = LTTNG_ERR_NOMEM; goto error; } - } - *total_size = nb_event * sizeof(*tmp_events) + extended_len; - tmp_events = zmalloc(*total_size); - if (!tmp_events) { - PERROR("zmalloc agent events session"); - ret = -LTTNG_ERR_FATAL; - goto error; - } - - extended_at = ((uint8_t *) tmp_events) + - nb_event * sizeof(struct lttng_event); - - cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { - strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); - tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; - tmp_events[i].enabled = event->enabled; - tmp_events[i].loglevel = event->loglevel_value; - tmp_events[i].loglevel_type = event->loglevel_type; - i++; + if(lttng_strncpy(tmp->name, event->name, sizeof(tmp->name))) { + lttng_event_destroy(tmp); + ret_code = LTTNG_ERR_FATAL; + goto error; + } + + tmp->name[sizeof(tmp->name) - 1] = '\0'; + tmp->enabled = event->enabled; + tmp->loglevel = event->loglevel_value; + tmp->loglevel_type = event->loglevel_type; - /* Append extended info */ - ret = append_extended_info(event->filter_expression, NULL, NULL, - &extended_at); + ret = lttng_event_serialize(tmp, 0, NULL, + event->filter_expression, 0, NULL, buffer, + NULL); + lttng_event_destroy(tmp); if (ret) { - DBG("Error appending extended info message"); - ret = -LTTNG_ERR_FATAL; + ret_code = LTTNG_ERR_FATAL; goto error; } } - *events = tmp_events; - ret = nb_event; - assert(nb_event == i); - end: - rcu_read_unlock(); - return ret; + ret_code = LTTNG_OK; + *nb_events = local_nb_events; error: - free(tmp_events); - goto end; + rcu_read_unlock(); + return ret_code; } /* * Create a list of ust global domain events. */ -static int list_lttng_ust_global_events(char *channel_name, +static enum lttng_error_code list_lttng_ust_global_events(char *channel_name, struct ltt_ust_domain_global *ust_global, - struct lttng_event **events, size_t *total_size) + struct lttng_dynamic_buffer *buffer, + unsigned int *nb_events) { - int i = 0, ret = 0; - unsigned int nb_event = 0; + enum lttng_error_code ret_code; + int ret; struct lttng_ht_iter iter; struct lttng_ht_node_str *node; struct ltt_ust_channel *uchan; struct ltt_ust_event *uevent; - struct lttng_event *tmp; - size_t extended_len = 0; - void *extended_at; + unsigned long tmp; + unsigned int local_nb_events = 0; + char **exclusion_list = NULL; + uint32_t i; + + assert(buffer); + assert(nb_events); DBG("Listing UST global events for channel %s", channel_name); @@ -490,197 +386,199 @@ static int list_lttng_ust_global_events(char *channel_name, lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); node = lttng_ht_iter_get_node_str(&iter); if (node == NULL) { - ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; + ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; goto end; } uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); - nb_event = lttng_ht_get_count(uchan->events); - if (nb_event == 0) { - ret = nb_event; - *total_size = 0; + tmp = lttng_ht_get_count(uchan->events); + if (tmp == 0) { + /* Early exit. */ + ret_code = LTTNG_OK; goto end; } - DBG3("Listing UST global %d events", nb_event); + if (tmp > UINT_MAX) { + ret_code = LTTNG_ERR_OVERFLOW; + goto error; + } + + local_nb_events = (unsigned int) tmp; + + DBG3("Listing UST global %d events", *nb_events); - /* Compute required extended infos size */ cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { + struct lttng_event *tmp = NULL; + if (uevent->internal) { - nb_event--; + /* This event should remain hidden from clients */ + local_nb_events--; continue; } - ret = increment_extended_len(uevent->filter_expression, - uevent->exclusion, NULL, &extended_len); - if (ret) { - DBG("Error computing the length of extended info message"); - ret = -LTTNG_ERR_FATAL; - goto end; + /* Prepare exclusion list. */ + if (uevent->exclusion && uevent->exclusion->count > 0) { + exclusion_list = zmalloc(sizeof(char *) * uevent->exclusion->count); + if (!exclusion_list) { + goto end; + } + for (i = 0; i < uevent->exclusion->count; i++) { + exclusion_list[i] = (char *) &(uevent->exclusion->names[i]); + } } - } - if (nb_event == 0) { - /* All events are internal, skip. */ - ret = 0; - *total_size = 0; - goto end; - } - - *total_size = nb_event * sizeof(struct lttng_event) + extended_len; - tmp = zmalloc(*total_size); - if (tmp == NULL) { - ret = -LTTNG_ERR_FATAL; - goto end; - } - extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event); + tmp = lttng_event_create(); + if (!tmp) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } - cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { - if (uevent->internal) { - /* This event should remain hidden from clients */ - continue; + if (lttng_strncpy(tmp->name, uevent->attr.name, + LTTNG_SYMBOL_NAME_LEN)) { + ret_code = LTTNG_ERR_FATAL; + lttng_event_destroy(tmp); + goto end; } - strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); - tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; - tmp[i].enabled = uevent->enabled; + + tmp->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; + tmp->enabled = uevent->enabled; switch (uevent->attr.instrumentation) { case LTTNG_UST_TRACEPOINT: - tmp[i].type = LTTNG_EVENT_TRACEPOINT; + tmp->type = LTTNG_EVENT_TRACEPOINT; break; case LTTNG_UST_PROBE: - tmp[i].type = LTTNG_EVENT_PROBE; + tmp->type = LTTNG_EVENT_PROBE; break; case LTTNG_UST_FUNCTION: - tmp[i].type = LTTNG_EVENT_FUNCTION; + tmp->type = LTTNG_EVENT_FUNCTION; break; } - tmp[i].loglevel = uevent->attr.loglevel; + tmp->loglevel = uevent->attr.loglevel; switch (uevent->attr.loglevel_type) { case LTTNG_UST_LOGLEVEL_ALL: - tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; + tmp->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; break; case LTTNG_UST_LOGLEVEL_RANGE: - tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; + tmp->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; break; case LTTNG_UST_LOGLEVEL_SINGLE: - tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; + tmp->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; break; } if (uevent->filter) { - tmp[i].filter = 1; + tmp->filter = 1; } if (uevent->exclusion) { - tmp[i].exclusion = 1; + tmp->exclusion = 1; } - i++; - /* Append extended info */ - ret = append_extended_info(uevent->filter_expression, - uevent->exclusion, NULL, &extended_at); + /* + * We do not care about the filter bytecode and the fd from the + * userspace_probe_location. + */ + ret = lttng_event_serialize(tmp, + exclusion_list ? uevent->exclusion->count : 0, + exclusion_list, uevent->filter_expression, 0, + NULL, buffer, NULL); + lttng_event_destroy(tmp); + free(exclusion_list); + exclusion_list = NULL; if (ret) { - DBG("Error appending extended info message"); - ret = -LTTNG_ERR_FATAL; - goto end; + ret_code = LTTNG_ERR_FATAL; + goto error; } } - ret = nb_event; - *events = tmp; end: + /* nb_events is already set at this point. */ + ret_code = LTTNG_OK; + *nb_events = local_nb_events; +error: + free(exclusion_list); rcu_read_unlock(); - return ret; + return ret_code; } /* * Fill lttng_event array of all kernel events in the channel. */ -static int list_lttng_kernel_events(char *channel_name, +static enum lttng_error_code list_lttng_kernel_events(char *channel_name, struct ltt_kernel_session *kernel_session, - struct lttng_event **events, size_t *total_size) + struct lttng_dynamic_buffer *payload, + unsigned int *nb_events) { - int i = 0, ret; - unsigned int nb_event; + enum lttng_error_code ret_code; + int ret; struct ltt_kernel_event *event; struct ltt_kernel_channel *kchan; - size_t extended_len = 0; - void *extended_at; + + assert(payload); kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); if (kchan == NULL) { - ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; - goto error; + ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; + goto end; } - nb_event = kchan->event_count; + *nb_events = kchan->event_count; DBG("Listing events for channel %s", kchan->channel->name); - if (nb_event == 0) { - *total_size = 0; - *events = NULL; + if (*nb_events == 0) { + ret_code = LTTNG_OK; goto end; } - /* Compute required extended infos size */ - cds_list_for_each_entry(event, &kchan->events_list.head, list) { - ret = increment_extended_len(event->filter_expression, NULL, - event->userspace_probe_location, - &extended_len); - if (ret) { - DBG("Error computing the length of extended info message"); - ret = -LTTNG_ERR_FATAL; - goto error; + /* Kernel channels */ + cds_list_for_each_entry(event, &kchan->events_list.head , list) { + struct lttng_event *tmp = lttng_event_create(); + + if (!tmp) { + ret_code = LTTNG_ERR_NOMEM; + goto end; } - } - *total_size = nb_event * sizeof(struct lttng_event) + extended_len; - *events = zmalloc(*total_size); - if (*events == NULL) { - ret = -LTTNG_ERR_FATAL; - goto error; - } + if (lttng_strncpy(tmp->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) { + lttng_event_destroy(tmp); + ret_code = LTTNG_ERR_FATAL; + goto end; - extended_at = ((void *) *events) + - nb_event * sizeof(struct lttng_event); + } - /* Kernel channels */ - cds_list_for_each_entry(event, &kchan->events_list.head , list) { - strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); - (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; - (*events)[i].enabled = event->enabled; - (*events)[i].filter = - (unsigned char) !!event->filter_expression; + tmp->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; + tmp->enabled = event->enabled; + tmp->filter = (unsigned char) !!event->filter_expression; switch (event->event->instrumentation) { case LTTNG_KERNEL_TRACEPOINT: - (*events)[i].type = LTTNG_EVENT_TRACEPOINT; + tmp->type = LTTNG_EVENT_TRACEPOINT; break; case LTTNG_KERNEL_KRETPROBE: - (*events)[i].type = LTTNG_EVENT_FUNCTION; - memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, + tmp->type = LTTNG_EVENT_FUNCTION; + memcpy(&tmp->attr.probe, &event->event->u.kprobe, sizeof(struct lttng_kernel_kprobe)); break; case LTTNG_KERNEL_KPROBE: - (*events)[i].type = LTTNG_EVENT_PROBE; - memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, + tmp->type = LTTNG_EVENT_PROBE; + memcpy(&tmp->attr.probe, &event->event->u.kprobe, sizeof(struct lttng_kernel_kprobe)); break; case LTTNG_KERNEL_UPROBE: - (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE; + tmp->type = LTTNG_EVENT_USERSPACE_PROBE; break; case LTTNG_KERNEL_FUNCTION: - (*events)[i].type = LTTNG_EVENT_FUNCTION; - memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, + tmp->type = LTTNG_EVENT_FUNCTION; + memcpy(&(tmp->attr.ftrace), &event->event->u.ftrace, sizeof(struct lttng_kernel_function)); break; case LTTNG_KERNEL_NOOP: - (*events)[i].type = LTTNG_EVENT_NOOP; + tmp->type = LTTNG_EVENT_NOOP; break; case LTTNG_KERNEL_SYSCALL: - (*events)[i].type = LTTNG_EVENT_SYSCALL; + tmp->type = LTTNG_EVENT_SYSCALL; break; case LTTNG_KERNEL_ALL: /* fall-through. */ @@ -688,23 +586,40 @@ static int list_lttng_kernel_events(char *channel_name, assert(0); break; } - i++; - /* Append extended info */ - ret = append_extended_info(event->filter_expression, NULL, - event->userspace_probe_location, &extended_at); + if (event->userspace_probe_location) { + struct lttng_userspace_probe_location *location_copy = + lttng_userspace_probe_location_copy( + event->userspace_probe_location); + if (!location_copy) { + lttng_event_destroy(tmp); + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + ret = lttng_event_set_userspace_probe_location( + tmp, location_copy); + if (ret) { + lttng_event_destroy(tmp); + lttng_userspace_probe_location_destroy( + location_copy); + ret_code = LTTNG_ERR_INVALID; + goto end; + } + } + + ret = lttng_event_serialize(tmp, 0, NULL, + event->filter_expression, 0, NULL, payload, + NULL); + lttng_event_destroy(tmp); if (ret) { - DBG("Error appending extended info message"); - ret = -LTTNG_ERR_FATAL; - goto error; + ret_code = LTTNG_ERR_FATAL; + goto end; } } + ret_code = LTTNG_OK; end: - return nb_event; - -error: - return ret; + return ret_code; } /* @@ -1765,15 +1680,65 @@ end: /* * Command LTTNG_DISABLE_EVENT processed by the client thread. */ -int cmd_disable_event(struct ltt_session *session, - enum lttng_domain_type domain, const char *channel_name, - const struct lttng_event *event) +int cmd_disable_event(struct command_ctx *cmd_ctx, int sock) { int ret; + size_t event_len; const char *event_name; + ssize_t sock_recv_len; + struct lttng_event *event = NULL; + struct lttng_buffer_view view; + struct lttng_dynamic_buffer event_buffer; + struct lttng_event_exclusion *exclusion = NULL; + struct lttng_filter_bytecode *bytecode = NULL; + char *filter_expression = NULL; + unsigned int exclusion_count; + const struct ltt_session *session = cmd_ctx->session; + const char *channel_name = cmd_ctx->lsm->u.disable.channel_name; + enum lttng_domain_type domain = cmd_ctx->lsm->domain.type; + + lttng_dynamic_buffer_init(&event_buffer); + event_len = (size_t) cmd_ctx->lsm->u.disable.length; + ret = lttng_dynamic_buffer_set_size(&event_buffer, event_len); + if (ret) { + ret = LTTNG_ERR_NOMEM; + goto error; + } + + sock_recv_len = lttcomm_recv_unix_sock( + sock, event_buffer.data, event_len); + if (sock_recv_len < 0 || sock_recv_len != event_len) { + ERR("Failed to receive \"disable event\" command payload"); + ret = LTTNG_ERR_INVALID; + goto error; + } + + view = lttng_buffer_view_from_dynamic_buffer(&event_buffer, 0, -1); + + if (lttng_event_create_from_buffer(&view, &event, + &exclusion, &filter_expression, + &bytecode, sock) != event_len) { + ERR("Invalid event payload received in \"disable event\" command"); + ret = LTTNG_ERR_INVALID; + goto error; + } DBG("Disable event command for event \'%s\'", event->name); + /* + * Filter and exclusions are simply not handled by the + * disable event command at this time. + * + * FIXME + */ + (void) filter_expression; + (void) exclusion; + (void) exclusion_count; + + /* Ignore the presence of filter or exclusion for the event */ + event->filter = 0; + event->exclusion = 0; + event_name = event->name; /* Error out on unhandled search criteria */ @@ -1936,6 +1901,12 @@ int cmd_disable_event(struct ltt_session *session, error_unlock: rcu_read_unlock(); error: + free(exclusion); + free(bytecode); + free(filter_expression); + lttng_event_destroy(event); + lttng_dynamic_buffer_reset(&event_buffer); + return ret; } @@ -2513,16 +2484,63 @@ error: * Command LTTNG_ENABLE_EVENT processed by the client thread. * We own filter, exclusion, and filter_expression. */ -int cmd_enable_event(struct ltt_session *session, - const struct lttng_domain *domain, - char *channel_name, struct lttng_event *event, - char *filter_expression, - struct lttng_filter_bytecode *filter, - struct lttng_event_exclusion *exclusion, - int wpipe) +int cmd_enable_event(struct command_ctx *cmd_ctx, int sock, int wpipe) { - return _cmd_enable_event(session, domain, channel_name, event, - filter_expression, filter, exclusion, wpipe, false); + int ret; + size_t event_len; + ssize_t sock_recv_len; + struct lttng_event *event = NULL; + struct lttng_buffer_view view; + struct lttng_dynamic_buffer event_buffer; + struct lttng_event_exclusion *exclusion = NULL; + struct lttng_filter_bytecode *bytecode = NULL; + char *filter_expression = NULL; + + lttng_dynamic_buffer_init(&event_buffer); + event_len = (size_t) cmd_ctx->lsm->u.enable.length; + ret = lttng_dynamic_buffer_set_size(&event_buffer, event_len); + if (ret) { + ret = LTTNG_ERR_NOMEM; + goto end; + } + + sock_recv_len = lttcomm_recv_unix_sock( + sock, event_buffer.data, event_len); + if (sock_recv_len < 0 || sock_recv_len != event_len) { + ERR("Failed to receive \"enable event\" command payload"); + ret = LTTNG_ERR_INVALID; + goto end; + } + + view = lttng_buffer_view_from_dynamic_buffer(&event_buffer, 0, -1); + + if (lttng_event_create_from_buffer(&view, &event, + &exclusion, &filter_expression, + &bytecode, sock) != event_len) { + ERR("Invalid event payload received in \"enable event\" command"); + ret = LTTNG_ERR_INVALID; + goto end; + } + + /* + * The ownership of the following parameters is transferred to + * _cmd_enable_event: + * + * - filter_expression, + * - bytecode, + * - exclusion + */ + ret = _cmd_enable_event(cmd_ctx->session, + ALIGNED_CONST_PTR(cmd_ctx->lsm->domain), + cmd_ctx->lsm->u.enable.channel_name, event, + filter_expression, bytecode, exclusion, wpipe, false); + filter_expression = NULL; + bytecode = NULL; + exclusion = NULL; +end: + lttng_event_destroy(event); + lttng_dynamic_buffer_reset(&event_buffer); + return ret; } /* @@ -2545,46 +2563,66 @@ static int cmd_enable_event_internal(struct ltt_session *session, /* * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. */ -ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, - struct lttng_event **events) +enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain, + struct lttng_dynamic_buffer *buffer, unsigned int *nb_tracepoints) { + enum lttng_error_code ret_code; int ret; - ssize_t nb_events = 0; + ssize_t i, nb_events = 0; + struct lttng_event *events = NULL; + + assert(buffer); + assert(nb_tracepoints); switch (domain) { case LTTNG_DOMAIN_KERNEL: - nb_events = kernel_list_events(events); + nb_events = kernel_list_events(&events); if (nb_events < 0) { - ret = LTTNG_ERR_KERN_LIST_FAIL; + ret_code = LTTNG_ERR_KERN_LIST_FAIL; goto error; } break; case LTTNG_DOMAIN_UST: - nb_events = ust_app_list_events(events); + nb_events = ust_app_list_events(&events); if (nb_events < 0) { - ret = LTTNG_ERR_UST_LIST_FAIL; + ret_code = LTTNG_ERR_UST_LIST_FAIL; goto error; } break; case LTTNG_DOMAIN_LOG4J: case LTTNG_DOMAIN_JUL: case LTTNG_DOMAIN_PYTHON: - nb_events = agent_list_events(events, domain); + nb_events = agent_list_events(&events, domain); if (nb_events < 0) { - ret = LTTNG_ERR_UST_LIST_FAIL; + ret_code = LTTNG_ERR_UST_LIST_FAIL; goto error; } break; default: - ret = LTTNG_ERR_UND; + ret_code = LTTNG_ERR_UND; goto error; } - return nb_events; + for (i = 0; i < nb_events; i++) { + ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL, + buffer, NULL); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto error; + } + } + + if (nb_events > UINT_MAX) { + ret_code = LTTNG_ERR_OVERFLOW; + goto error; + } + + *nb_tracepoints = (unsigned int) nb_events; + ret_code = LTTNG_OK; error: - /* Return negative value to differentiate return code */ - return -ret; + free(events); + return ret_code; } /* @@ -2617,9 +2655,42 @@ error: return -ret; } -ssize_t cmd_list_syscalls(struct lttng_event **events) +enum lttng_error_code cmd_list_syscalls( + struct lttng_dynamic_buffer *buffer, unsigned int *nb_syscalls) { - return syscall_table_list(events); + enum lttng_error_code ret_code; + ssize_t nb_events, i; + int ret; + struct lttng_event *events = NULL; + + assert(buffer); + assert(nb_syscalls); + + nb_events = syscall_table_list(&events); + if (nb_events < 0) { + ret_code = (enum lttng_error_code) -nb_events; + goto end; + } + + for (i = 0; i < nb_events; i++) { + ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL, + buffer, NULL); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + } + + if (nb_events > UINT_MAX) { + ret_code = LTTNG_ERR_OVERFLOW; + goto end; + } + + *nb_syscalls = (uint32_t) nb_events; + ret_code = LTTNG_OK; +end: + free(events); + return ret_code; } /* @@ -3727,27 +3798,29 @@ end: /* * Command LTTNG_LIST_EVENTS processed by the client thread. */ -ssize_t cmd_list_events(enum lttng_domain_type domain, - struct ltt_session *session, char *channel_name, - struct lttng_event **events, size_t *total_size) +enum lttng_error_code cmd_list_events(enum lttng_domain_type domain, + struct ltt_session *session, + char *channel_name, + struct lttng_dynamic_buffer *payload, + unsigned int *nb_events) { - int ret = 0; - ssize_t nb_event = 0; + enum lttng_error_code ret_code = LTTNG_OK; + + assert(nb_events); switch (domain) { case LTTNG_DOMAIN_KERNEL: if (session->kernel_session != NULL) { - nb_event = list_lttng_kernel_events(channel_name, - session->kernel_session, events, - total_size); + ret_code = list_lttng_kernel_events(channel_name, + session->kernel_session, payload, nb_events); } break; case LTTNG_DOMAIN_UST: { if (session->ust_session != NULL) { - nb_event = list_lttng_ust_global_events(channel_name, - &session->ust_session->domain_global, events, - total_size); + ret_code = list_lttng_ust_global_events(channel_name, + &session->ust_session->domain_global, + payload, nb_events); } break; } @@ -3762,9 +3835,8 @@ ssize_t cmd_list_events(enum lttng_domain_type domain, cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, agt, node.node) { if (agt->domain == domain) { - nb_event = list_lttng_agent_events( - agt, events, - total_size); + ret_code = list_lttng_agent_events( + agt, payload, nb_events); break; } } @@ -3772,15 +3844,11 @@ ssize_t cmd_list_events(enum lttng_domain_type domain, } break; default: - ret = LTTNG_ERR_UND; - goto error; + ret_code = LTTNG_ERR_UND; + break; } - return nb_event; - -error: - /* Return negative value to differentiate return code */ - return -ret; + return ret_code; } /* diff --git a/src/bin/lttng-sessiond/cmd.h b/src/bin/lttng-sessiond/cmd.h index 65e3846c8..e2db45418 100644 --- a/src/bin/lttng-sessiond/cmd.h +++ b/src/bin/lttng-sessiond/cmd.h @@ -77,21 +77,13 @@ enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set( struct lttng_process_attr_values **values); /* Event commands */ -int cmd_disable_event(struct ltt_session *session, - enum lttng_domain_type domain, - const char *channel_name, - const struct lttng_event *event); +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_set_filter(struct ltt_session *session, enum lttng_domain_type domain, char *channel_name, struct lttng_event *event, struct lttng_filter_bytecode *bytecode); -int cmd_enable_event(struct ltt_session *session, const struct lttng_domain *domain, - char *channel_name, struct lttng_event *event, - char *filter_expression, - struct lttng_filter_bytecode *filter, - struct lttng_event_exclusion *exclusion, - int wpipe); +int cmd_enable_event(struct command_ctx *cmd_ctx, int sock, int wpipe); /* Trace session action commands */ int cmd_start_trace(struct ltt_session *session); @@ -108,9 +100,11 @@ int cmd_setup_relayd(struct ltt_session *session); /* Listing commands */ ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains); -ssize_t cmd_list_events(enum lttng_domain_type domain, - struct ltt_session *session, char *channel_name, - struct lttng_event **events, size_t *total_size); +enum lttng_error_code cmd_list_events(enum lttng_domain_type domain, + struct ltt_session *session, + char *channel_name, + struct lttng_dynamic_buffer *payload, + unsigned int *nb_events); enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain, struct ltt_session *session, struct lttng_dynamic_buffer *buffer, @@ -121,11 +115,13 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions, size_t session_count, uid_t uid, gid_t gid); ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, struct lttng_event_field **fields); -ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, - struct lttng_event **events); +enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain, + struct lttng_dynamic_buffer *buffer, + unsigned int *nb_tracepoints); ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, struct lttng_snapshot_output **outputs); -ssize_t cmd_list_syscalls(struct lttng_event **events); +enum lttng_error_code cmd_list_syscalls( + struct lttng_dynamic_buffer *buffer, unsigned int *nb_syscalls); int cmd_data_pending(struct ltt_session *session); diff --git a/src/common/event.c b/src/common/event.c index e4bda2505..967f36bb5 100644 --- a/src/common/event.c +++ b/src/common/event.c @@ -5,8 +5,37 @@ * */ -#include +#include "common/compat/string.h" +#include "common/macros.h" +#include "lttng/lttng-error.h" +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include + +struct event_list_element { + struct lttng_event *event; + struct lttng_event_exclusion *exclusions; + char *filter_expression; +}; + +static void event_list_destructor(void *ptr) +{ + struct event_list_element *element = (struct event_list_element *) ptr; + + free(element->filter_expression); + free(element->exclusions); + lttng_event_destroy(element->event); + free(element); +} LTTNG_HIDDEN struct lttng_event *lttng_event_copy(const struct lttng_event *event) @@ -41,3 +70,1194 @@ error: new_event = NULL; goto end; } + +static int lttng_event_probe_attr_serialize( + const struct lttng_event_probe_attr *probe, + struct lttng_dynamic_buffer *buf) +{ + int ret; + size_t symbol_name_len; + struct lttng_event_probe_attr_comm comm = { 0 }; + + symbol_name_len = lttng_strnlen(probe->symbol_name, LTTNG_SYMBOL_NAME_LEN); + if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) { + /* Not null-termintated. */ + ret = -1; + goto end; + } + + /* Include the null terminator. */ + symbol_name_len += 1; + + comm.symbol_name_len = (uint32_t) symbol_name_len; + comm.addr = probe->addr; + comm.offset = probe->addr; + + ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm)); + if (ret < 0) { + ret = -1; + goto end; + } + + ret = lttng_dynamic_buffer_append(buf, probe->symbol_name, symbol_name_len); + +end: + return ret; +} + +static int lttng_event_function_attr_serialize( + const struct lttng_event_function_attr *function, + struct lttng_dynamic_buffer *buf) +{ + int ret; + size_t symbol_name_len; + struct lttng_event_function_attr_comm comm = { 0 }; + + symbol_name_len = lttng_strnlen(function->symbol_name, LTTNG_SYMBOL_NAME_LEN); + if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) { + /* Not null-termintated. */ + ret = -1; + goto end; + } + + /* Include the null terminator. */ + symbol_name_len += 1; + + comm.symbol_name_len = (uint32_t) symbol_name_len; + + ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm)); + if (ret < 0) { + ret = -1; + goto end; + } + + ret = lttng_dynamic_buffer_append(buf, function->symbol_name, symbol_name_len); +end: + return ret; +} + +static ssize_t lttng_event_probe_attr_create_from_buffer( + const struct lttng_buffer_view *view, + struct lttng_event_probe_attr **probe_attr) +{ + ssize_t ret, offset = 0; + const struct lttng_event_probe_attr_comm *comm; + struct lttng_event_probe_attr *local_attr = NULL; + const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view( + view, offset, sizeof(*comm)); + + if (!lttng_buffer_view_is_valid(&comm_view)) { + ret = -1; + goto end; + } + + comm = (typeof(comm)) view->data; + offset += sizeof(*comm); + + local_attr = zmalloc(sizeof(*local_attr)); + if (local_attr == NULL) { + ret = -1; + goto end; + } + + local_attr->addr = comm->addr; + local_attr->offset = comm->offset; + + { + const char *name; + const struct lttng_buffer_view name_view = + lttng_buffer_view_from_view(view, offset, + comm->symbol_name_len); + if (!lttng_buffer_view_is_valid(&name_view)) { + ret = -1; + goto end; + } + + name = name_view.data; + + if (!lttng_buffer_view_contains_string( + &name_view, name, comm->symbol_name_len)) { + ret = -1; + goto end; + } + + ret = lttng_strncpy(local_attr->symbol_name, name, + LTTNG_SYMBOL_NAME_LEN); + if (ret) { + ret = -1; + goto end; + } + offset += comm->symbol_name_len; + } + + *probe_attr = local_attr; + local_attr = NULL; + ret = offset; +end: + return ret; +} + +static ssize_t lttng_event_function_attr_create_from_buffer( + const struct lttng_buffer_view *view, + struct lttng_event_function_attr **function_attr) +{ + ssize_t ret, offset = 0; + const struct lttng_event_function_attr_comm *comm; + struct lttng_event_function_attr *local_attr = NULL; + const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view( + view, offset, sizeof(*comm)); + + if (!lttng_buffer_view_is_valid(&comm_view)) { + ret = -1; + goto end; + } + + comm = (typeof(comm)) view->data; + offset += sizeof(*comm); + + local_attr = zmalloc(sizeof(*local_attr)); + if (local_attr == NULL) { + ret = -1; + goto end; + } + + { + const char *name; + const struct lttng_buffer_view name_view = + lttng_buffer_view_from_view(view, offset, + comm->symbol_name_len); + if (!lttng_buffer_view_is_valid(&name_view)) { + ret = -1; + goto end; + } + + name = name_view.data; + + if (!lttng_buffer_view_contains_string( + &name_view, name, comm->symbol_name_len)) { + ret = -1; + goto end; + } + + ret = lttng_strncpy(local_attr->symbol_name, name, + LTTNG_SYMBOL_NAME_LEN); + if (ret) { + ret = -1; + goto end; + } + offset += comm->symbol_name_len; + } + + *function_attr = local_attr; + local_attr = NULL; + ret = offset; +end: + return ret; +} + +static ssize_t lttng_event_exclusions_create_from_buffer(const struct lttng_buffer_view *view, + uint32_t count, struct lttng_event_exclusion **exclusions) +{ + ssize_t ret, offset = 0; + size_t size = (count * LTTNG_SYMBOL_NAME_LEN); + uint32_t i; + const struct lttng_event_exclusion_comm *comm; + struct lttng_event_exclusion *local_exclusions; + + + local_exclusions = zmalloc(sizeof(struct lttng_event_exclusion) + size); + if (!local_exclusions) { + ret = -1; + goto end; + } + local_exclusions->count = count; + + for (i = 0; i < count; i++) { + const char *string; + struct lttng_buffer_view string_view; + const struct lttng_buffer_view comm_view = + lttng_buffer_view_from_view( + view, offset, sizeof(*comm)); + + if (!lttng_buffer_view_is_valid(&comm_view)) { + ret = -1; + goto end; + } + + comm = (typeof(comm)) comm_view.data; + offset += sizeof(*comm); + + string_view = lttng_buffer_view_from_view( + view, offset, comm->len); + + if (!lttng_buffer_view_is_valid(&string_view)) { + ret = -1; + goto end; + } + + string = string_view.data; + + if (!lttng_buffer_view_contains_string( + &string_view, string, comm->len)) { + ret = -1; + goto end; + } + + ret = lttng_strncpy( + local_exclusions->names[i], + string, comm->len); + if (ret) { + ret = -1; + goto end; + } + offset += comm->len; + } + + *exclusions = local_exclusions; + local_exclusions = NULL; + ret = offset; +end: + free(local_exclusions); + return ret; +} + + +LTTNG_HIDDEN +ssize_t lttng_event_create_from_buffer(const struct lttng_buffer_view *view, + struct lttng_event **event, + struct lttng_event_exclusion **exclusion, + char **filter_expression, + struct lttng_filter_bytecode **bytecode, + int sock) +{ + ssize_t ret, offset = 0; + struct lttng_event *local_event = NULL; + struct lttng_event_exclusion *local_exclusions = NULL; + struct lttng_filter_bytecode *local_bytecode = NULL; + char *local_filter_expression = NULL; + const struct lttng_event_comm *event_comm; + struct lttng_event_function_attr *local_function_attr = NULL; + struct lttng_event_probe_attr *local_probe_attr = NULL; + struct lttng_userspace_probe_location *local_userspace_probe_location = + NULL; + int received_fd = -1; + + /* + * Only event is obligatory, the other output argument are optional and + * depends on what the caller is interested in. + */ + assert(event); + assert(view); + + { + const struct lttng_buffer_view comm_view = + lttng_buffer_view_from_view(view, offset, + sizeof(*event_comm)); + + if (!lttng_buffer_view_is_valid(&comm_view)) { + ret = -1; + goto end; + } + + /* lttng_event_comm header */ + event_comm = (typeof(event_comm)) comm_view.data; + offset += sizeof(*event_comm); + } + + local_event = lttng_event_create(); + if (local_event == NULL) { + ret = -1; + goto end; + } + + local_event->type = event_comm->event_type; + local_event->loglevel_type = event_comm->loglevel_type; + local_event->loglevel = event_comm->loglevel; + local_event->enabled = event_comm->enabled; + local_event->pid = event_comm->pid; + local_event->flags = event_comm->flags; + + { + const char *name; + const struct lttng_buffer_view name_view = + lttng_buffer_view_from_view(view, offset, + event_comm->name_len); + if (!lttng_buffer_view_is_valid(&name_view)) { + ret = -1; + goto end; + } + + name = name_view.data; + + if (!lttng_buffer_view_contains_string( + &name_view, name, event_comm->name_len)) { + ret = -1; + goto end; + } + + ret = lttng_strncpy( + local_event->name, name, LTTNG_SYMBOL_NAME_LEN); + if (ret) { + ret = -1; + goto end; + } + offset += event_comm->name_len; + } + + /* Exclusions */ + if (event_comm->exclusion_count == 0) { + goto deserialize_filter_expression; + } + + { + + const struct lttng_buffer_view exclusions_view = + lttng_buffer_view_from_view( + view, offset, -1); + + if (!lttng_buffer_view_is_valid(&exclusions_view)) { + ret = -1; + goto end; + } + + ret = lttng_event_exclusions_create_from_buffer(&exclusions_view, + event_comm->exclusion_count, &local_exclusions); + if (ret < 0) { + ret = -1; + goto end; + } + offset += ret; + + local_event->exclusion = 1; + } + +deserialize_filter_expression: + + if (event_comm->filter_expression_len == 0) { + if (event_comm->bytecode_len != 0) { + /* + * This is an invalid event payload. + * + * Filter expression without bytecode is possible but + * not the other way around. + * */ + ret = -1; + goto end; + } + goto deserialize_event_type_payload; + } + + { + const char *filter_expression_buffer; + const struct lttng_buffer_view filter_expression_view = + lttng_buffer_view_from_view(view, offset, + event_comm->filter_expression_len); + + if (!lttng_buffer_view_is_valid(&filter_expression_view)) { + ret = -1; + goto end; + } + + filter_expression_buffer = filter_expression_view.data; + + if (!lttng_buffer_view_contains_string(&filter_expression_view, + filter_expression_buffer, + event_comm->filter_expression_len)) { + ret = -1; + goto end; + } + + local_filter_expression = lttng_strndup( + filter_expression_buffer, + event_comm->filter_expression_len); + if (!local_filter_expression) { + ret = -1; + goto end; + } + + local_event->filter = 1; + + offset += event_comm->filter_expression_len; + } + + if (event_comm->bytecode_len == 0) { + /* + * Filter expression can be present but without bytecode + * when dealing with event listing. + */ + goto deserialize_event_type_payload; + } + + /* Bytecode */ + { + const struct lttng_buffer_view bytecode_view = + lttng_buffer_view_from_view(view, offset, + event_comm->bytecode_len); + + if (!lttng_buffer_view_is_valid(&bytecode_view)) { + ret = -1; + goto end; + } + + local_bytecode = zmalloc(event_comm->bytecode_len); + if (!local_bytecode) { + ret = -1; + goto end; + } + + memcpy(local_bytecode, bytecode_view.data, + event_comm->bytecode_len); + if ((local_bytecode->len + sizeof(*local_bytecode)) != + event_comm->bytecode_len) { + ret = -1; + goto end; + } + + offset += event_comm->bytecode_len; + } + +deserialize_event_type_payload: + /* Event type specific payload */ + switch (local_event->type) { + case LTTNG_EVENT_FUNCTION: + /* Fallthrough */ + case LTTNG_EVENT_PROBE: + { + const struct lttng_buffer_view probe_attr_view = + lttng_buffer_view_from_view(view, offset, + event_comm->lttng_event_probe_attr_len); + + if (event_comm->lttng_event_probe_attr_len == 0) { + ret = -1; + goto end; + } + + if (!lttng_buffer_view_is_valid(&probe_attr_view)) { + ret = -1; + goto end; + } + + ret = lttng_event_probe_attr_create_from_buffer( + &probe_attr_view, &local_probe_attr); + if (ret < 0 || ret != event_comm->lttng_event_probe_attr_len) { + ret = -1; + goto end; + } + + /* Copy to the local event. */ + memcpy(&local_event->attr.probe, local_probe_attr, + sizeof(local_event->attr.probe)); + + offset += ret; + break; + } + case LTTNG_EVENT_FUNCTION_ENTRY: + { + const struct lttng_buffer_view function_attr_view = + lttng_buffer_view_from_view(view, offset, + event_comm->lttng_event_function_attr_len); + + if (event_comm->lttng_event_function_attr_len == 0) { + ret = -1; + goto end; + } + + if (!lttng_buffer_view_is_valid(&function_attr_view)) { + ret = -1; + goto end; + } + + ret = lttng_event_function_attr_create_from_buffer( + &function_attr_view, &local_function_attr); + if (ret < 0 || ret != event_comm->lttng_event_function_attr_len) { + ret = -1; + goto end; + } + + /* Copy to the local event. */ + memcpy(&local_event->attr.ftrace, local_function_attr, + sizeof(local_event->attr.ftrace)); + + offset += ret; + + break; + } + case LTTNG_EVENT_USERSPACE_PROBE: + { + const struct lttng_buffer_view userspace_probe_location_view = + lttng_buffer_view_from_view(view, offset, + event_comm->userspace_probe_location_len); + const struct lttng_userspace_probe_location_lookup_method + *lookup = NULL; + + if (event_comm->userspace_probe_location_len == 0) { + ret = -1; + goto end; + } + + if (!lttng_buffer_view_is_valid( + &userspace_probe_location_view)) { + ret = -1; + goto end; + } + + ret = lttng_userspace_probe_location_create_from_buffer( + &userspace_probe_location_view, + &local_userspace_probe_location); + if (ret < 0) { + WARN("Failed to create a userspace probe location from the received buffer"); + ret = -1; + goto end; + } + + if (ret != event_comm->userspace_probe_location_len) { + WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32 ", payload length = %lu", event_comm->userspace_probe_location_len, ret); + ret = -1; + goto end; + } + + if (sock < 0) { + /* + * The userspace FD is simply not sent and we do not + * care about it. This happens on listing. + */ + goto attach_userspace_probe_to_event; + } + + /* + * Receive the file descriptor to the target binary from the + * client. + */ + DBG("Receiving userspace probe target FD from client ..."); + ret = lttcomm_recv_fds_unix_sock(sock, &received_fd, 1); + if (ret <= 0) { + DBG("Nothing recv() from client userspace probe fd... continuing"); + ret = -1; + goto end; + } + /* + * Set the file descriptor received from the client through the + * unix socket in the probe location. + */ + lookup = lttng_userspace_probe_location_get_lookup_method( + local_userspace_probe_location); + if (!lookup) { + ret = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + + /* + * From the kernel tracer's perspective, all userspace probe + * event types are all the same: a file and an offset. + */ + switch (lttng_userspace_probe_location_lookup_method_get_type( + lookup)) { + case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: + ret = lttng_userspace_probe_location_function_set_binary_fd( + local_userspace_probe_location, + received_fd); + break; + case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: + ret = lttng_userspace_probe_location_tracepoint_set_binary_fd( + local_userspace_probe_location, + received_fd); + break; + default: + ret = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + + if (ret) { + ret = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + + /* Fd transfered to the object. */ + received_fd = -1; + +attach_userspace_probe_to_event: + + /* Attach the probe location to the event. */ + ret = lttng_event_set_userspace_probe_location( + local_event, local_userspace_probe_location); + if (ret) { + ret = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + + /* + * Userspace probe location object ownership transfered to the + * event object + */ + local_userspace_probe_location = NULL; + offset += event_comm->userspace_probe_location_len; + break; + } + case LTTNG_EVENT_TRACEPOINT: + /* Fallthrough */ + case LTTNG_EVENT_ALL: + /* Fallthrough */ + case LTTNG_EVENT_SYSCALL: + /* Fallthrough */ + case LTTNG_EVENT_NOOP: + /* Nothing to do here */ + break; + default: + ret = LTTNG_ERR_UND; + goto end; + break; + } + + /* Transfer ownership to the caller. */ + *event = local_event; + local_event = NULL; + + if (bytecode) { + *bytecode = local_bytecode; + local_bytecode = NULL; + } + + if (exclusion) { + *exclusion = local_exclusions; + local_exclusions = NULL; + } + + if (filter_expression) { + *filter_expression = local_filter_expression; + local_filter_expression = NULL; + } + + ret = offset; +end: + lttng_event_destroy(local_event); + lttng_userspace_probe_location_destroy(local_userspace_probe_location); + free(local_filter_expression); + free(local_exclusions); + free(local_bytecode); + free(local_function_attr); + free(local_probe_attr); + if (received_fd > -1) { + if (close(received_fd)) { + PERROR("Failed to close received fd"); + }; + } + return ret; +} + +LTTNG_HIDDEN +int lttng_event_serialize(const struct lttng_event *event, + unsigned int exclusion_count, + char **exclusion_list, + char *filter_expression, + size_t bytecode_len, + void *bytecode, + struct lttng_dynamic_buffer *buf, + int *fd_to_send) +{ + int ret; + unsigned int i; + size_t header_offset, size_before_payload; + size_t name_len; + struct lttng_event_comm event_comm = { 0 }; + struct lttng_event_comm *header; + + assert(event); + assert(buf); + + /* Save the header location for later in-place header update. */ + header_offset = buf->size; + + name_len = lttng_strnlen(event->name, LTTNG_SYMBOL_NAME_LEN); + if (name_len == LTTNG_SYMBOL_NAME_LEN) { + /* Event name is not NULL-terminated. */ + ret = -1; + goto end; + } + + /* Add null termination. */ + name_len += 1; + + if (exclusion_count > UINT32_MAX) { + /* Possible overflow. */ + ret = -1; + goto end; + } + + if (bytecode_len > UINT32_MAX) { + /* Possible overflow. */ + ret = -1; + goto end; + } + + event_comm.name_len = (uint32_t) name_len; + event_comm.event_type = (int8_t) event->type; + event_comm.loglevel_type = (int8_t) event->loglevel_type; + event_comm.loglevel = (int32_t) event->loglevel; + event_comm.enabled = (int8_t) event->enabled; + event_comm.pid = (int32_t) event->pid; + event_comm.exclusion_count = (uint32_t) exclusion_count; + event_comm.bytecode_len = (uint32_t) bytecode_len; + event_comm.flags = (int32_t) event->flags; + + if (filter_expression) { + event_comm.filter_expression_len = + strlen(filter_expression) + 1; + } + + /* Header */ + ret = lttng_dynamic_buffer_append(buf, &event_comm, sizeof(event_comm)); + if (ret) { + goto end; + } + + /* Event name */ + ret = lttng_dynamic_buffer_append(buf, event->name, name_len); + if (ret) { + goto end; + } + + /* Exclusions */ + for (i = 0; i < exclusion_count; i++) { + size_t exclusion_len; + struct lttng_event_exclusion_comm exclusion_comm = { 0 }; + + assert(exclusion_list); + + exclusion_len = lttng_strnlen( + *(exclusion_list + i), LTTNG_SYMBOL_NAME_LEN); + if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) { + /* Exclusion is not NULL-terminated. */ + ret = -1; + goto end; + } + + /* Include null terminator '\0'. */ + exclusion_len += 1; + + exclusion_comm.len = exclusion_len; + + ret = lttng_dynamic_buffer_append(buf, &exclusion_comm, sizeof(exclusion_comm)); + if (ret) { + goto end; + } + + ret = lttng_dynamic_buffer_append(buf, *(exclusion_list + i), + exclusion_len); + if (ret) { + goto end; + } + } + + /* Filter expression and its bytecode */ + if (filter_expression) { + ret = lttng_dynamic_buffer_append(buf, filter_expression, + event_comm.filter_expression_len); + if (ret) { + goto end; + } + + /* + * Bytecode can be absent when we serialize to the client + * for listing. + */ + if (bytecode) { + ret = lttng_dynamic_buffer_append( + buf, bytecode, bytecode_len); + if (ret) { + goto end; + } + } + } + + size_before_payload = buf->size; + + /* Event type specific payload */ + switch (event->type) { + case LTTNG_EVENT_FUNCTION: + /* Fallthrough */ + case LTTNG_EVENT_PROBE: + ret = lttng_event_probe_attr_serialize(&event->attr.probe, buf); + if (ret) { + ret = -1; + goto end; + } + + header = (struct lttng_event_comm *) ((char *) buf->data + + header_offset); + header->lttng_event_probe_attr_len = + buf->size - size_before_payload; + + break; + case LTTNG_EVENT_FUNCTION_ENTRY: + ret = lttng_event_function_attr_serialize( + &event->attr.ftrace, buf); + if (ret) { + ret = -1; + goto end; + } + + /* Update the lttng_event_function_attr len. */ + header = (struct lttng_event_comm *) ((char *) buf->data + + header_offset); + header->lttng_event_function_attr_len = + buf->size - size_before_payload; + + break; + case LTTNG_EVENT_USERSPACE_PROBE: + { + struct lttng_event_extended *ev_ext = + (struct lttng_event_extended *) + event->extended.ptr; + assert(event->extended.ptr); + assert(ev_ext->probe_location); + + size_before_payload = buf->size; + if (ev_ext->probe_location) { + /* + * lttng_userspace_probe_location_serialize returns the + * number of bytes that were appended to the buffer. + */ + ret = lttng_userspace_probe_location_serialize( + ev_ext->probe_location, buf, + fd_to_send); + if (ret < 0) { + goto end; + } + ret = 0; + + /* Update the userspace probe location len. */ + header = (struct lttng_event_comm *) ((char *) buf->data + + header_offset); + header->userspace_probe_location_len = + buf->size - size_before_payload; + } + break; + } + case LTTNG_EVENT_TRACEPOINT: + /* Fallthrough */ + case LTTNG_EVENT_ALL: + /* Fallthrough */ + default: + /* Nothing to do here */ + break; + } + +end: + return ret; +} + +static enum lttng_error_code compute_flattened_size( + struct lttng_dynamic_pointer_array *events, size_t *size) +{ + enum lttng_error_code ret_code; + int ret = 0; + size_t storage_req, event_count, i; + + assert(size); + assert(events); + + event_count = lttng_dynamic_pointer_array_get_count(events); + + /* The basic struct lttng_event */ + storage_req = event_count * sizeof(struct lttng_event); + + for (i = 0; i < event_count; i++) { + int probe_storage_req = 0; + const struct event_list_element *element = + lttng_dynamic_pointer_array_get_pointer( + events, i); + const struct lttng_userspace_probe_location *location = NULL; + + location = lttng_event_get_userspace_probe_location( + element->event); + if (location) { + ret = lttng_userspace_probe_location_flatten( + location, NULL); + if (ret < 0) { + ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + + probe_storage_req = ret; + } + + /* The struct·lttng_event_extended */ + storage_req += event_count * + sizeof(struct lttng_event_extended); + + if (element->filter_expression) { + storage_req += strlen(element->filter_expression) + 1; + } + + if (element->exclusions) { + storage_req += element->exclusions->count * + LTTNG_SYMBOL_NAME_LEN; + } + + /* Padding to ensure the flat probe is aligned. */ + storage_req = ALIGN_TO(storage_req, sizeof(uint64_t)); + storage_req += probe_storage_req; + } + + *size = storage_req; + ret_code = LTTNG_OK; + +end: + return ret_code; +} + +/* + * Flatten a list of struct lttng_event. + * + * The buffer that is returned to the API client 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, + * - filter_expression + * - exclusions + * - padding to align to 64-bits + * - flattened version of userspace_probe_location + */ +static enum lttng_error_code flatten_lttng_events( + struct lttng_dynamic_pointer_array *events, + struct lttng_event **flattened_events) +{ + enum lttng_error_code ret_code; + int ret, i; + size_t storage_req; + struct lttng_dynamic_buffer local_flattened_events; + int nb_events; + + assert(events); + assert(flattened_events); + + lttng_dynamic_buffer_init(&local_flattened_events); + nb_events = lttng_dynamic_pointer_array_get_count(events); + + ret_code = compute_flattened_size(events, &storage_req); + if (ret_code != LTTNG_OK) { + goto end; + } + + /* + * We must ensure that "local_flattened_events" is never resized so as + * to preserve the validity of the flattened objects. + */ + ret = lttng_dynamic_buffer_set_capacity( + &local_flattened_events, storage_req); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + + /* Start by laying the struct lttng_event */ + for (i = 0; i < nb_events; i++) { + struct event_list_element *element = + lttng_dynamic_pointer_array_get_pointer( + events, i); + + if (!element) { + ret_code = LTTNG_ERR_FATAL; + goto end; + } + ret = lttng_dynamic_buffer_append(&local_flattened_events, + element->event, sizeof(struct lttng_event)); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + } + + for (i = 0; i < nb_events; i++) { + struct event_list_element *element = lttng_dynamic_pointer_array_get_pointer(events, i); + struct lttng_event *event = (struct lttng_event *) + (local_flattened_events.data + (sizeof(struct lttng_event) * i)); + struct lttng_event_extended *event_extended = + (struct lttng_event_extended *) + (local_flattened_events.data + local_flattened_events.size); + const struct lttng_userspace_probe_location *location = NULL; + + assert(element); + + /* Insert struct lttng_event_extended. */ + ret = lttng_dynamic_buffer_set_size(&local_flattened_events, + local_flattened_events.size + + sizeof(*event_extended)); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + event->extended.ptr = event_extended; + + /* Insert filter expression. */ + if (element->filter_expression) { + size_t len = strlen(element->filter_expression) + 1; + + event_extended->filter_expression = + local_flattened_events.data + + local_flattened_events.size; + ret = lttng_dynamic_buffer_append( + &local_flattened_events, + element->filter_expression, len); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + } + + /* Insert exclusions. */ + if (element->exclusions) { + event_extended->exclusions.count = + element->exclusions->count; + event_extended->exclusions.strings = + local_flattened_events.data + + local_flattened_events.size; + + ret = lttng_dynamic_buffer_append( + &local_flattened_events, + element->exclusions->names, + element->exclusions->count * + LTTNG_SYMBOL_NAME_LEN); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + } + + /* Insert padding to align to 64-bits. */ + ret = lttng_dynamic_buffer_set_size(&local_flattened_events, + ALIGN_TO(local_flattened_events.size, + sizeof(uint64_t))); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + + location = lttng_event_get_userspace_probe_location( + element->event); + if (location) { + event_extended->probe_location = (struct lttng_userspace_probe_location *) + (local_flattened_events.data + local_flattened_events.size); + ret = lttng_userspace_probe_location_flatten( + location, &local_flattened_events); + if (ret < 0) { + ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; + goto end; + } + } + } + + /* Don't reset local_flattened_events buffer as we return its content. */ + *flattened_events = (struct lttng_event *) local_flattened_events.data; + lttng_dynamic_buffer_init(&local_flattened_events); + ret_code = LTTNG_OK; +end: + lttng_dynamic_buffer_reset(&local_flattened_events); + return ret_code; +} + +static enum lttng_error_code event_list_create_from_buffer( + const struct lttng_buffer_view *view, + unsigned int count, + struct lttng_dynamic_pointer_array *event_list) +{ + enum lttng_error_code ret_code; + int ret; + unsigned int i; + int offset = 0; + + assert(view); + assert(event_list); + + for (i = 0; i < count; i++) { + ssize_t event_size; + const struct lttng_buffer_view event_view = + lttng_buffer_view_from_view(view, offset, -1); + struct event_list_element *element = zmalloc(sizeof(*element)); + + if (!element) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + + /* + * Lifetime and management of the object is now bound to the + * array. + */ + ret = lttng_dynamic_pointer_array_add_pointer( + event_list, element); + if (ret) { + event_list_destructor(element); + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + + /* + * Bytecode is not transmitted on listing in any case we do not + * care about it. + */ + event_size = lttng_event_create_from_buffer(&event_view, + &element->event, + &element->exclusions, + &element->filter_expression, NULL, -1); + if (event_size < 0) { + ret_code = LTTNG_ERR_INVALID; + goto end; + } + + offset += event_size; + } + + if (view->size != offset) { + ret_code = LTTNG_ERR_INVALID; + goto end; + } + + ret_code = LTTNG_OK; + +end: + return ret_code; +} + +LTTNG_HIDDEN +enum lttng_error_code lttng_events_create_and_flatten_from_buffer( + const struct lttng_buffer_view *view, + unsigned int count, + struct lttng_event **events) +{ + enum lttng_error_code ret = LTTNG_OK; + struct lttng_dynamic_pointer_array local_events; + + lttng_dynamic_pointer_array_init(&local_events, event_list_destructor); + + /* Deserialize the events */ + { + const struct lttng_buffer_view events_view = + lttng_buffer_view_from_view(view, 0, -1); + + ret = event_list_create_from_buffer( + &events_view, count, &local_events); + if (ret != LTTNG_OK) { + goto end; + } + } + + ret = flatten_lttng_events(&local_events, events); + if (ret != LTTNG_OK) { + goto end; + } + +end: + lttng_dynamic_pointer_array_reset(&local_events); + return ret; +} diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 0d3e374b3..2db5ba469 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -278,36 +278,11 @@ struct lttcomm_session_msg { /* Event data */ struct { char channel_name[LTTNG_SYMBOL_NAME_LEN]; - struct lttng_event event LTTNG_PACKED; - /* Length of following filter expression. */ - uint32_t expression_len; - /* Length of following bytecode for filter. */ - uint32_t bytecode_len; - /* Exclusion count (fixed-size strings). */ - uint32_t exclusion_count; - /* Userspace probe location size. */ - uint32_t userspace_probe_location_len; - /* - * After this structure, the following variable-length - * items are transmitted: - * - char exclusion_names[LTTNG_SYMBOL_NAME_LEN][exclusion_count] - * - char filter_expression[expression_len] - * - unsigned char filter_bytecode[bytecode_len] - */ + uint32_t length; } LTTNG_PACKED enable; struct { char channel_name[LTTNG_SYMBOL_NAME_LEN]; - struct lttng_event event LTTNG_PACKED; - /* Length of following filter expression. */ - uint32_t expression_len; - /* Length of following bytecode for filter. */ - uint32_t bytecode_len; - /* - * After this structure, the following variable-length - * items are transmitted: - * - unsigned char filter_expression[expression_len] - * - unsigned char filter_bytecode[bytecode_len] - */ + uint32_t length; } LTTNG_PACKED disable; /* Create channel */ struct { @@ -443,14 +418,6 @@ struct lttng_event_exclusion { #define LTTNG_EVENT_EXCLUSION_NAME_AT(_exclusion, _i) \ (&(_exclusion)->names[_i][0]) -/* - * Event command header. - */ -struct lttcomm_event_command_header { - /* Number of events */ - uint32_t nb_events; -} LTTNG_PACKED; - /* * Listing command header. */ diff --git a/src/common/userspace-probe.c b/src/common/userspace-probe.c index a15e5b7e4..e85c3c56f 100644 --- a/src/common/userspace-probe.c +++ b/src/common/userspace-probe.c @@ -413,17 +413,20 @@ lttng_userspace_probe_location_function_copy( goto error; } - /* Duplicate the binary fd */ + /* + * Duplicate the binary fd if possible. The binary fd can be -1 on + * listing + */ fd = lttng_userspace_probe_location_function_get_binary_fd(location); - if (fd == -1) { - ERR("Error getting file descriptor to binary"); - goto error; - } - - new_fd = dup(fd); - if (new_fd == -1) { - PERROR("Error duplicating file descriptor to binary"); - goto error; + if (fd > -1) { + new_fd = dup(fd); + if (new_fd == -1) { + PERROR("Error duplicating file descriptor to binary"); + goto error; + } + } else { + /* The original fd is not set. */ + new_fd = -1; } /* @@ -509,15 +512,15 @@ lttng_userspace_probe_location_tracepoint_copy( /* Duplicate the binary fd */ fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(location); - if (fd == -1) { - ERR("Error getting file descriptor to binary"); - goto error; - } - - new_fd = dup(fd); - if (new_fd == -1) { - PERROR("Error duplicating file descriptor to binary"); - goto error; + if (fd > -1) { + new_fd = dup(fd); + if (new_fd == -1) { + PERROR("Error duplicating file descriptor to binary"); + goto error; + } + } else { + /* The original fd is not set. */ + new_fd = -1; } /* diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 622a2bd04..6b745d020 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -20,10 +20,13 @@ #include #include +#include #include #include #include +#include #include +#include #include #include #include @@ -982,14 +985,14 @@ error: * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched. */ static int generate_filter(char *filter_expression, - struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp) + size_t *bytecode_len, + struct filter_parser_ctx **ctxp) { int ret; struct filter_parser_ctx *ctx = NULL; FILE *fmem = NULL; assert(filter_expression); - assert(lsm); assert(ctxp); /* @@ -1078,9 +1081,8 @@ static int generate_filter(char *filter_expression, dbg_printf("Size of bytecode generated: %u bytes.\n", bytecode_get_len(&ctx->bytecode->b)); - 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 = sizeof(ctx->bytecode->b) + + bytecode_get_len(&ctx->bytecode->b); /* No need to keep the memory stream. */ if (fclose(fmem) != 0) { @@ -1116,10 +1118,11 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, { struct lttcomm_session_msg lsm; struct lttng_dynamic_buffer send_buffer; - int ret = 0, i, fd_to_send = -1; + int ret = 0, fd_to_send = -1; bool send_fd = false; 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 @@ -1149,36 +1152,12 @@ 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)); 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 @@ -1188,7 +1167,7 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, 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) { @@ -1196,7 +1175,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 { /* @@ -1209,96 +1188,74 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, } } - ret = generate_filter(filter_expression, &lsm, &ctx); + if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) == + LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; + } + + ret = generate_filter(filter_expression, &bytecode_len, &ctx); if (ret) { - goto filter_error; + goto error; + } + + if (bytecode_len > LTTNG_FILTER_MAX_LEN) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto error; } } - ret = lttng_dynamic_buffer_set_capacity(&send_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 != 0) ? &ctx->bytecode->b : NULL, + &send_buffer, &fd_to_send); if (ret) { - ret = -LTTNG_ERR_EXCLUSION_NOMEM; - goto mem_error; + goto error; } - /* Put exclusion names first in the data. */ - for (i = 0; i < exclusion_count; i++) { - size_t exclusion_len; + if (fd_to_send >= 0) { + send_fd = true; + } - 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; - } + /* Prepare the command header */ + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_ENABLE_EVENT; - ret = lttng_dynamic_buffer_append(&send_buffer, - *(exclusion_list + i), - LTTNG_SYMBOL_NAME_LEN); - 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(&send_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(&send_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, &send_buffer, - &fd_to_send); - if (ret < 0) { - goto mem_error; - } + /* Domain */ + COPY_DOMAIN_PACKED(lsm.domain, handle->domain); - send_fd = fd_to_send >= 0; - /* - * 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) send_buffer.size; + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, send_fd ? &fd_to_send : NULL, send_fd ? 1 : 0, send_buffer.size ? send_buffer.data : NULL, send_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 @@ -1307,16 +1264,8 @@ 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_dynamic_buffer_reset(&send_buffer); - return ret; -ask_sessiond: - ret = lttng_ctl_ask_sessiond(&lsm, NULL); + lttng_dynamic_buffer_reset(&send_buffer); return ret; } @@ -1325,10 +1274,13 @@ int lttng_disable_event_ext(struct lttng_handle *handle, const char *original_filter_expression) { struct lttcomm_session_msg lsm; - char *varlen_data; + struct lttng_dynamic_buffer buf; int ret = 0; unsigned int free_filter_expression = 0; struct filter_parser_ctx *ctx = NULL; + size_t bytecode_len = 0; + int fd_to_send = -1; + bool send_fd = false; /* * Cast as non-const since we may replace the filter expression * by a dynamically allocated string. Otherwise, the original @@ -1336,6 +1288,8 @@ int lttng_disable_event_ext(struct lttng_handle *handle, */ char *filter_expression = (char *) original_filter_expression; + lttng_dynamic_buffer_init(&buf); + if (handle == NULL || ev == NULL) { ret = -LTTNG_ERR_INVALID; goto error; @@ -1351,47 +1305,6 @@ 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 @@ -1401,7 +1314,7 @@ int lttng_disable_event_ext(struct lttng_handle *handle, 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) { @@ -1409,7 +1322,7 @@ int lttng_disable_event_ext(struct lttng_handle *handle, * No JUL and no filter, just skip * everything below. */ - goto ask_sessiond; + goto serialize; } } else { /* @@ -1422,44 +1335,63 @@ int lttng_disable_event_ext(struct lttng_handle *handle, } } - ret = generate_filter(filter_expression, &lsm, &ctx); + ret = generate_filter(filter_expression, &bytecode_len, &ctx); if (ret) { - goto filter_error; + ret = -1; + goto error; } } - varlen_data = 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 != 0) ? &ctx->bytecode->b : NULL, + &buf, &fd_to_send); + if (ret) { + ret = -1; + goto error; + } + + if (fd_to_send >= 0) { + send_fd = true; } - /* Add filter expression. */ - if (lsm.u.disable.expression_len != 0) { - memcpy(varlen_data, - filter_expression, - lsm.u.disable.expression_len); + /* Prepare command header */ + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_DISABLE_EVENT; + + /* 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) buf.size; -mem_error: + ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, + send_fd ? &fd_to_send : NULL, send_fd ? 1 : 0, + buf.size ? buf.data : NULL, buf.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 @@ -1468,15 +1400,8 @@ 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_dynamic_buffer_reset(&buf); return ret; } @@ -1696,23 +1621,62 @@ end: int lttng_list_tracepoints(struct lttng_handle *handle, struct lttng_event **events) { - int ret; + 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; + 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; + + { + const struct lttng_buffer_view events_view = + lttng_buffer_view_init(reception_buffer, 0, + total_payload_received); + + ret_code = lttng_events_create_and_flatten_from_buffer( + &events_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; } /* @@ -1752,11 +1716,17 @@ int lttng_list_tracepoint_fields(struct lttng_handle *handle, */ int lttng_list_syscalls(struct lttng_event **events) { - int ret; + 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)); @@ -1764,12 +1734,45 @@ int lttng_list_syscalls(struct lttng_event **events) /* 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; + } + + 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); - return ret / sizeof(struct lttng_event); + ret_code = lttng_events_create_and_flatten_from_buffer( + &events_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; } /* @@ -2321,15 +2324,17 @@ end: int lttng_list_events(struct lttng_handle *handle, const char *channel_name, struct lttng_event **events) { + enum lttng_error_code ret_code; int ret; struct lttcomm_session_msg lsm; - struct lttcomm_event_command_header *cmd_header = NULL; + struct lttcomm_list_command_header *cmd_header = NULL; size_t cmd_header_len; - uint32_t nb_events, i; - void *comm_ext_at; + unsigned int nb_events; char *reception_buffer = NULL; - struct lttng_dynamic_buffer listing; - size_t storage_req; + struct lttng_dynamic_buffer tmp_buffer; + ssize_t total_payload_received; + + lttng_dynamic_buffer_init(&tmp_buffer); /* Safety check. An handle and channel name are mandatory */ if (handle == NULL || channel_name == NULL) { @@ -2361,210 +2366,33 @@ int lttng_list_events(struct lttng_handle *handle, goto end; } + total_payload_received = ret; + if (!cmd_header) { ret = -LTTNG_ERR_UNK; goto end; } - /* Set number of events and free command header */ - nb_events = cmd_header->nb_events; - if (nb_events > INT_MAX) { + if (cmd_header->count > INT_MAX) { ret = -LTTNG_ERR_OVERFLOW; goto end; } - free(cmd_header); - cmd_header = NULL; - /* - * 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 - */ - comm_ext_at = reception_buffer + - (nb_events * sizeof(struct lttng_event)); - storage_req = nb_events * sizeof(struct lttng_event); - - for (i = 0; i < nb_events; i++) { - 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_buffer_view probe_location_view; - - probe_location_view = lttng_buffer_view_init( - comm_ext_at, 0, - ext_comm->userspace_probe_location_len); - - /* - * 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_buffer( - &probe_location_view, &probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto end; - } + nb_events = (unsigned int) cmd_header->count; - 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 = ALIGN_TO(storage_req, sizeof(uint64_t)); - storage_req += probe_storage_req; - } - - 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; - } - - ret = lttng_dynamic_buffer_append(&listing, reception_buffer, - nb_events * sizeof(struct lttng_event)); - if (ret) { - ret = -LTTNG_ERR_NOMEM; - goto free_dynamic_buffer; - } - - comm_ext_at = reception_buffer + - (nb_events * sizeof(struct lttng_event)); - for (i = 0; i < nb_events; i++) { - struct lttng_event *event = (struct lttng_event *) - (listing.data + (sizeof(struct lttng_event) * i)); - struct lttcomm_event_extended_header *ext_comm = - (struct lttcomm_event_extended_header *) comm_ext_at; - struct lttng_event_extended *event_extended = - (struct lttng_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, - ALIGN_TO(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_buffer_view probe_location_view; - - probe_location_view = lttng_buffer_view_init( - comm_ext_at, 0, - ext_comm->userspace_probe_location_len); - - ret = lttng_userspace_probe_location_create_from_buffer( - &probe_location_view, &probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto free_dynamic_buffer; - } - - event_extended->probe_location = (struct lttng_userspace_probe_location *) - (listing.data + listing.size); - ret = lttng_userspace_probe_location_flatten( - probe_location, &listing); - lttng_userspace_probe_location_destroy(probe_location); - if (ret < 0) { - ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; - goto free_dynamic_buffer; - } - - comm_ext_at += ext_comm->userspace_probe_location_len; + { + const struct lttng_buffer_view events_view = + lttng_buffer_view_init(reception_buffer, 0, + total_payload_received); + ret_code = lttng_events_create_and_flatten_from_buffer( + &events_view, nb_events, events); + if (ret_code != LTTNG_OK) { + ret = -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: free(cmd_header); free(reception_buffer); -- 2.34.1