+ ret = lttng_strncpy(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ret = lttng_ctl_ask_sessiond(&lsm, NULL);
+end:
+ return ret;
+}
+
+/*
+ * Lists all available tracepoints of domain.
+ * Sets the contents of the events array.
+ * Returns the number of lttng_event entries in events;
+ * on error, returns a negative value.
+ */
+int lttng_list_tracepoints(struct lttng_handle *handle,
+ struct lttng_event **events)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ if (handle == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ 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);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return ret / sizeof(struct lttng_event);
+}
+
+/*
+ * Lists all available tracepoint fields of domain.
+ * Sets the contents of the event field array.
+ * Returns the number of lttng_event_field entries in events;
+ * on error, returns a negative value.
+ */
+int lttng_list_tracepoint_fields(struct lttng_handle *handle,
+ struct lttng_event_field **fields)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ if (handle == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
+ COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
+
+ ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return ret / sizeof(struct lttng_event_field);
+}
+
+/*
+ * Lists all available kernel system calls. Allocates and sets the contents of
+ * the events array.
+ *
+ * Returns the number of lttng_event entries in events; on error, returns a
+ * negative value.
+ */
+int lttng_list_syscalls(struct lttng_event **events)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ if (!events) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_SYSCALLS;
+ /* Force kernel domain for system calls. */
+ lsm.domain.type = LTTNG_DOMAIN_KERNEL;
+
+ ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return ret / sizeof(struct lttng_event);
+}
+
+/*
+ * Returns a human readable string describing
+ * the error code (a negative value).
+ */
+const char *lttng_strerror(int code)
+{
+ return error_get_str(code);
+}
+
+enum lttng_error_code lttng_create_session_ext(
+ struct lttng_session_descriptor *session_descriptor)
+{
+ enum lttng_error_code ret_code;
+ struct lttcomm_session_msg lsm = {
+ .cmd_type = LTTNG_CREATE_SESSION_EXT,
+ };
+ void *reply = NULL;
+ struct lttng_buffer_view reply_view;
+ int reply_ret;
+ bool sessiond_must_generate_ouput;
+ struct lttng_dynamic_buffer payload;
+ int ret;
+ size_t descriptor_size;
+ struct lttng_session_descriptor *descriptor_reply = NULL;
+
+ lttng_dynamic_buffer_init(&payload);
+ if (!session_descriptor) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ sessiond_must_generate_ouput =
+ !lttng_session_descriptor_is_output_destination_initialized(
+ session_descriptor);
+ if (sessiond_must_generate_ouput) {
+ const char *home_dir = utils_get_home_dir();
+ size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0;
+
+ if (!home_dir || home_dir_len > LTTNG_PATH_MAX) {
+ ret_code = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len;
+ ret = lttng_dynamic_buffer_append(&payload, home_dir,
+ home_dir_len);
+ if (ret) {
+ ret_code = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+ }
+
+ descriptor_size = payload.size;
+ ret = lttng_session_descriptor_serialize(session_descriptor,
+ &payload);
+ if (ret) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+ descriptor_size = payload.size - descriptor_size;
+ lsm.u.create_session.session_descriptor_size = descriptor_size;
+
+ /* Command returns a session descriptor on success. */
+ reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data,
+ payload.size, &reply);
+ if (reply_ret < 0) {
+ ret_code = -reply_ret;
+ goto end;
+ } else if (reply_ret == 0) {
+ /* Socket unexpectedly closed by the session daemon. */
+ ret_code = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ reply_view = lttng_buffer_view_init(reply, 0, reply_ret);
+ ret = lttng_session_descriptor_create_from_buffer(&reply_view,
+ &descriptor_reply);
+ if (ret < 0) {
+ ret_code = LTTNG_ERR_FATAL;
+ goto end;
+ }
+ ret_code = LTTNG_OK;
+ lttng_session_descriptor_assign(session_descriptor, descriptor_reply);
+end:
+ free(reply);
+ lttng_dynamic_buffer_reset(&payload);
+ lttng_session_descriptor_destroy(descriptor_reply);
+ return ret_code;
+}
+
+/*
+ * Create a new session using name and url for destination.
+ *
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_create_session(const char *name, const char *url)
+{
+ int ret;
+ ssize_t size;
+ struct lttng_uri *uris = NULL;
+ struct lttng_session_descriptor *descriptor = NULL;
+ enum lttng_error_code ret_code;
+
+ if (!name) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ size = uri_parse_str_urls(url, NULL, &uris);
+ if (size < 0) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ switch (size) {
+ case 0:
+ descriptor = lttng_session_descriptor_create(name);
+ break;
+ case 1:
+ if (uris[0].dtype != LTTNG_DST_PATH) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ descriptor = lttng_session_descriptor_local_create(name,
+ uris[0].dst.path);
+ break;
+ case 2:
+ descriptor = lttng_session_descriptor_network_create(name, url,
+ NULL);
+ break;
+ default:
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ if (!descriptor) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ ret_code = lttng_create_session_ext(descriptor);
+ ret = ret_code == LTTNG_OK ? 0 : -ret_code;
+end:
+ lttng_session_descriptor_destroy(descriptor);
+ free(uris);
+ return ret;
+}
+
+/*
+ * Create a session exclusively used for snapshot.
+ *
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
+{
+ int ret;
+ enum lttng_error_code ret_code;
+ ssize_t size;
+ struct lttng_uri *uris = NULL;
+ struct lttng_session_descriptor *descriptor = NULL;
+
+ if (!name) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ size = uri_parse_str_urls(snapshot_url, NULL, &uris);
+ if (size < 0) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ /*
+ * If the user does not specify a custom subdir, use the session name.
+ */
+ if (size > 0 && uris[0].dtype != LTTNG_DST_PATH &&
+ strlen(uris[0].subdir) == 0) {
+ ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s",
+ name);
+ if (ret < 0) {
+ PERROR("Failed to set session name as network destination sub-directory");
+ ret = -LTTNG_ERR_FATAL;
+ goto end;
+ } else if (ret >= sizeof(uris[0].subdir)) {
+ /* Truncated output. */
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ }
+
+ switch (size) {
+ case 0:
+ descriptor = lttng_session_descriptor_snapshot_create(name);
+ break;
+ case 1:
+ if (uris[0].dtype != LTTNG_DST_PATH) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ descriptor = lttng_session_descriptor_snapshot_local_create(
+ name,
+ uris[0].dst.path);
+ break;
+ case 2:
+ descriptor = lttng_session_descriptor_snapshot_network_create(
+ name,
+ snapshot_url,
+ NULL);
+ break;
+ default:
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ if (!descriptor) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ ret_code = lttng_create_session_ext(descriptor);
+ ret = ret_code == LTTNG_OK ? 0 : -ret_code;
+end:
+ lttng_session_descriptor_destroy(descriptor);
+ free(uris);
+ return ret;
+}
+
+/*
+ * Create a session exclusively used for live.
+ *
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_create_session_live(const char *name, const char *url,
+ unsigned int timer_interval)
+{
+ int ret;
+ enum lttng_error_code ret_code;
+ struct lttng_session_descriptor *descriptor = NULL;
+
+ if (!name) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (url) {
+ descriptor = lttng_session_descriptor_live_network_create(
+ name, url, NULL, timer_interval);
+ } else {
+ descriptor = lttng_session_descriptor_live_create(
+ name, timer_interval);
+ }
+ if (!descriptor) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+ ret_code = lttng_create_session_ext(descriptor);
+ ret = ret_code == LTTNG_OK ? 0 : -ret_code;
+end:
+ lttng_session_descriptor_destroy(descriptor);
+ return ret;
+}
+
+/*
+ * Stop the session and wait for the data before destroying it
+ *
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_destroy_session(const char *session_name)
+{
+ int ret;
+ enum lttng_error_code ret_code;
+ enum lttng_destruction_handle_status status;
+ struct lttng_destruction_handle *handle = NULL;
+
+ /*
+ * Stop the tracing and wait for the data to be
+ * consumed.
+ */
+ ret = _lttng_stop_tracing(session_name, 1);
+ if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
+ goto end;
+ }
+
+ ret_code = lttng_destroy_session_ext(session_name, &handle);
+ if (ret_code != LTTNG_OK) {
+ ret = (int) -ret_code;
+ goto end;
+ }
+ assert(handle);
+
+ /* Block until the completion of the destruction of the session. */
+ status = lttng_destruction_handle_wait_for_completion(handle, -1);
+ if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) {
+ ret = -LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ status = lttng_destruction_handle_get_result(handle, &ret_code);
+ if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
+ ret = -LTTNG_ERR_UNK;
+ goto end;
+ }
+ ret = ret_code == LTTNG_OK ? 0 : -ret_code;
+end:
+ lttng_destruction_handle_destroy(handle);
+ return ret;
+}
+
+/*
+ * Destroy the session without waiting for the data.
+ */
+int lttng_destroy_session_no_wait(const char *session_name)
+{
+ enum lttng_error_code ret_code;
+
+ ret_code = lttng_destroy_session_ext(session_name, NULL);
+ return ret_code == LTTNG_OK ? 0 : -ret_code;
+}
+
+/*
+ * Ask the session daemon for all available sessions.
+ * Sets the contents of the sessions array.
+ * Returns the number of lttng_session entries in sessions;
+ * on error, returns a negative value.
+ */
+int lttng_list_sessions(struct lttng_session **out_sessions)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+ const size_t session_size = sizeof(struct lttng_session) +
+ sizeof(struct lttng_session_extended);
+ size_t session_count, i;
+ struct lttng_session_extended *sessions_extended_begin;
+ struct lttng_session *sessions = NULL;
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_SESSIONS;
+ ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions);
+ if (ret <= 0) {
+ goto end;
+ }
+ if (!sessions) {
+ ret = -LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ if (ret % session_size) {
+ ret = -LTTNG_ERR_UNK;
+ free(sessions);
+ *out_sessions = NULL;
+ goto end;
+ }
+ session_count = (size_t) ret / session_size;
+ sessions_extended_begin = (struct lttng_session_extended *)
+ (&sessions[session_count]);
+
+ /* Set extended session info pointers. */
+ for (i = 0; i < session_count; i++) {
+ struct lttng_session *session = &sessions[i];
+ struct lttng_session_extended *extended =
+ &(sessions_extended_begin[i]);
+
+ session->extended.ptr = extended;
+ }
+
+ ret = (int) session_count;
+ *out_sessions = sessions;
+end:
+ return ret;
+}
+
+enum lttng_error_code lttng_session_get_creation_time(
+ const struct lttng_session *session, uint64_t *creation_time)
+{
+ enum lttng_error_code ret = LTTNG_OK;
+ struct lttng_session_extended *extended;
+
+ if (!session || !creation_time || !session->extended.ptr) {
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ extended = session->extended.ptr;
+ if (!extended->creation_time.is_set) {
+ /* Not created on the session daemon yet. */
+ ret = LTTNG_ERR_SESSION_NOT_EXIST;
+ goto end;
+ }
+ *creation_time = extended->creation_time.value;
+end:
+ return ret;
+}
+
+int lttng_set_session_shm_path(const char *session_name,
+ const char *shm_path)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ if (session_name == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
+
+ ret = lttng_strncpy(lsm.session.name, session_name,
+ sizeof(lsm.session.name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "",
+ sizeof(lsm.u.set_shm_path.shm_path));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ret = lttng_ctl_ask_sessiond(&lsm, NULL);
+end:
+ return ret;
+}
+
+/*
+ * Ask the session daemon for all available domains of a session.
+ * Sets the contents of the domains array.
+ * Returns the number of lttng_domain entries in domains;
+ * on error, returns a negative value.
+ */
+int lttng_list_domains(const char *session_name,
+ struct lttng_domain **domains)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ if (session_name == NULL) {
+ ret = -LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_DOMAINS;
+
+ ret = lttng_strncpy(lsm.session.name, session_name,
+ sizeof(lsm.session.name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
+ if (ret < 0) {
+ goto error;
+ }
+
+ return ret / sizeof(struct lttng_domain);
+error:
+ return ret;
+}
+
+/*
+ * Ask the session daemon for all available channels of a session.
+ * Sets the contents of the channels array.
+ * Returns the number of lttng_channel entries in channels;
+ * on error, returns a negative value.
+ */
+int lttng_list_channels(struct lttng_handle *handle,
+ struct lttng_channel **channels)
+{
+ int ret;
+ size_t channel_count, i;
+ const size_t channel_size = sizeof(struct lttng_channel) +
+ sizeof(struct lttng_channel_extended);
+ struct lttcomm_session_msg lsm;
+ void *extended_at;
+
+ if (handle == NULL) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_CHANNELS;
+ ret = lttng_strncpy(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
+
+ ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
+ if (ret < 0) {
+ goto end;
+ }
+
+ if (ret % channel_size) {
+ ret = -LTTNG_ERR_UNK;
+ free(*channels);
+ *channels = NULL;
+ goto end;
+ }
+ channel_count = (size_t) ret / channel_size;
+
+ /* Set extended info pointers */
+ extended_at = ((void *) *channels) +
+ channel_count * sizeof(struct lttng_channel);
+ for (i = 0; i < channel_count; i++) {
+ struct lttng_channel *chan = &(*channels)[i];
+
+ chan->attr.extended.ptr = extended_at;
+ extended_at += sizeof(struct lttng_channel_extended);
+ }
+
+ ret = (int) channel_count;
+end:
+ return ret;
+}
+
+/*
+ * Ask the session daemon for all available events of a session channel.
+ * Sets the contents of the events array.
+ * Returns the number of lttng_event entries in events;
+ * on error, returns a negative value.
+ */
+int lttng_list_events(struct lttng_handle *handle,
+ const char *channel_name, struct lttng_event **events)
+{
+ int ret;
+ struct lttcomm_session_msg lsm = {};
+ const struct lttcomm_event_command_header *cmd_header = NULL;
+ uint32_t nb_events, i;
+ const void *comm_ext_at;
+ struct lttng_dynamic_buffer listing;
+ size_t storage_req;
+ struct lttng_payload payload;
+ struct lttng_payload payload_copy;
+ struct lttng_payload_view lsm_view =
+ lttng_payload_view_init_from_buffer(
+ (const char *) &lsm, 0, sizeof(lsm));
+ struct lttng_buffer_view cmd_header_view;
+ struct lttng_buffer_view cmd_payload_view;
+ struct lttng_buffer_view flat_events_view;
+ struct lttng_buffer_view ext_view;
+
+ /* Safety check. An handle and channel name are mandatory */
+ if (handle == NULL || channel_name == NULL) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ lttng_payload_init(&payload);
+ lttng_payload_init(&payload_copy);
+
+ lsm.cmd_type = LTTNG_LIST_EVENTS;
+ ret = lttng_strncpy(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ret = lttng_strncpy(lsm.u.list.channel_name, channel_name,
+ sizeof(lsm.u.list.channel_name));
+ if (ret) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
+
+ ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &payload);
+ if (ret < 0) {
+ goto end;
+ }
+
+ /*
+ * A copy of the payload is performed since it will be
+ * consumed twice. Consuming the same payload twice is invalid since
+ * it will cause any received file descriptor to become "shared"
+ * between different instances of the resulting objects.
+ */
+ ret = lttng_payload_copy(&payload, &payload_copy);
+ if (ret) {
+ ret = -LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ cmd_header_view = lttng_buffer_view_from_dynamic_buffer(
+ &payload.buffer, 0, sizeof(*cmd_header));
+ if (!lttng_buffer_view_is_valid(&cmd_header_view)) {
+ ret = -LTTNG_ERR_INVALID_PROTOCOL;
+ goto end;
+ }
+
+ cmd_header = (typeof(cmd_header)) cmd_header_view.data;
+
+ /* Set number of events and free command header */
+ nb_events = cmd_header->nb_events;
+ if (nb_events > INT_MAX) {
+ ret = -LTTNG_ERR_OVERFLOW;
+ goto end;
+ }
+
+ cmd_payload_view = lttng_buffer_view_from_dynamic_buffer(
+ &payload.buffer, sizeof(*cmd_header), -1);
+
+ /*
+ * The buffer that is returned must contain a "flat" version of
+ * the events that are returned. In other words, all pointers
+ * within an lttng_event must point to a location within the returned
+ * buffer so that the user may free everything by simply calling free()
+ * on the returned buffer. This is needed in order to maintain API
+ * compatibility.
+ *
+ * A first pass is performed to compute the size of the buffer that
+ * must be allocated. A second pass is then performed to setup
+ * the returned events so that their members always point within the
+ * buffer.
+ *
+ * The layout of the returned buffer is as follows:
+ * - struct lttng_event[nb_events],
+ * - nb_events times the following:
+ * - struct lttng_event_extended,
+ * - flattened version of userspace_probe_location
+ * - filter_expression
+ * - exclusions
+ * - padding to align to 64-bits
+ */
+ ext_view = lttng_buffer_view_from_view(&cmd_payload_view,
+ nb_events * sizeof(struct lttng_event), -1);
+ comm_ext_at = ext_view.data;
+ storage_req = nb_events * sizeof(struct lttng_event);
+ {
+ struct lttng_payload_view payload_view =
+ lttng_payload_view_from_payload(&payload, 0, -1);
+
+ for (i = 0; i < nb_events; i++) {
+ const struct lttcomm_event_extended_header *ext_comm =
+ (struct lttcomm_event_extended_header *)
+ comm_ext_at;
+ int probe_storage_req = 0;
+
+ comm_ext_at += sizeof(*ext_comm);
+ comm_ext_at += ext_comm->filter_len;
+ comm_ext_at += ext_comm->nb_exclusions *
+ LTTNG_SYMBOL_NAME_LEN;
+
+ if (ext_comm->userspace_probe_location_len) {
+ struct lttng_userspace_probe_location
+ *probe_location = NULL;
+ struct lttng_payload_view probe_location_view = lttng_payload_view_from_view(
+ &payload_view,
+ (const char *) comm_ext_at -
+ payload_view.buffer.data,
+ ext_comm->userspace_probe_location_len);
+
+ if (!lttng_payload_view_is_valid(&probe_location_view)) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto end;
+ }
+
+ /*
+ * Create a temporary userspace probe location
+ * to determine the size needed by a "flattened"
+ * version of that same probe location.
+ */
+ ret = lttng_userspace_probe_location_create_from_payload(
+ &probe_location_view,
+ &probe_location);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto end;
+ }
+
+ ret = lttng_userspace_probe_location_flatten(
+ probe_location, NULL);
+ lttng_userspace_probe_location_destroy(
+ probe_location);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto end;
+ }
+
+ probe_storage_req = ret;
+ comm_ext_at += ext_comm->userspace_probe_location_len;
+ }
+
+ 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;
+ }
+
+ cmd_payload_view = lttng_buffer_view_from_dynamic_buffer(
+ &payload_copy.buffer, sizeof(*cmd_header), -1);
+ flat_events_view = lttng_buffer_view_from_view(&cmd_payload_view, 0,
+ nb_events * sizeof(struct lttng_event));
+ ret = lttng_dynamic_buffer_append_view(&listing, &flat_events_view);
+ if (ret) {
+ ret = -LTTNG_ERR_NOMEM;
+ goto free_dynamic_buffer;
+ }
+
+ ext_view = lttng_buffer_view_from_view(&cmd_payload_view,
+ nb_events * sizeof(struct lttng_event), -1);
+ comm_ext_at = ext_view.data;
+
+ {
+ struct lttng_payload_view payload_copy_view =
+ lttng_payload_view_from_payload(
+ &payload_copy, 0, -1);
+
+ for (i = 0; i < nb_events; i++) {
+ struct lttng_event *event = (typeof(event))(
+ listing.data +
+ (sizeof(struct lttng_event) * i));
+ const struct lttcomm_event_extended_header *ext_comm =
+ (typeof(ext_comm)) comm_ext_at;
+ struct lttng_event_extended *event_extended =
+ (typeof(event_extended))(listing.data +
+ listing.size);
+
+ /* Insert struct lttng_event_extended. */
+ ret = lttng_dynamic_buffer_set_size(&listing,
+ listing.size + sizeof(*event_extended));
+ if (ret) {
+ ret = -LTTNG_ERR_NOMEM;
+ goto free_dynamic_buffer;
+ }
+ event->extended.ptr = event_extended;
+
+ comm_ext_at += sizeof(*ext_comm);
+
+ /* Insert filter expression. */
+ if (ext_comm->filter_len) {
+ event_extended->filter_expression =
+ listing.data + listing.size;
+ ret = lttng_dynamic_buffer_append(&listing,
+ comm_ext_at,
+ ext_comm->filter_len);
+ if (ret) {
+ ret = -LTTNG_ERR_NOMEM;
+ goto free_dynamic_buffer;
+ }
+ comm_ext_at += ext_comm->filter_len;
+ }
+
+ /* Insert exclusions. */
+ if (ext_comm->nb_exclusions) {
+ event_extended->exclusions.count =
+ ext_comm->nb_exclusions;
+ event_extended->exclusions.strings =
+ listing.data + listing.size;
+
+ ret = lttng_dynamic_buffer_append(&listing,
+ comm_ext_at,
+ ext_comm->nb_exclusions *
+ LTTNG_SYMBOL_NAME_LEN);
+ if (ret) {
+ ret = -LTTNG_ERR_NOMEM;
+ goto free_dynamic_buffer;
+ }
+ comm_ext_at += ext_comm->nb_exclusions *
+ LTTNG_SYMBOL_NAME_LEN;
+ }
+
+ /* Insert padding to align to 64-bits. */
+ ret = lttng_dynamic_buffer_set_size(&listing,
+ 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_payload_view probe_location_view = lttng_payload_view_from_view(
+ &payload_copy_view,
+ (const char *) comm_ext_at -
+ payload_copy_view.buffer.data,
+ ext_comm->userspace_probe_location_len);
+
+ if (!lttng_payload_view_is_valid(&probe_location_view)) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto free_dynamic_buffer;
+ }
+
+ ret = lttng_userspace_probe_location_create_from_payload(
+ &probe_location_view,
+ &probe_location);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto free_dynamic_buffer;
+ }
+
+ event_extended->probe_location = (struct lttng_userspace_probe_location
+ *) (listing.data +
+ listing.size);
+ ret = lttng_userspace_probe_location_flatten(
+ probe_location, &listing);
+ lttng_userspace_probe_location_destroy(
+ probe_location);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
+ goto free_dynamic_buffer;
+ }
+
+ comm_ext_at += ext_comm->userspace_probe_location_len;
+ }
+ }
+ }
+
+ /* Don't reset listing buffer as we return its content. */
+ *events = (struct lttng_event *) listing.data;
+ lttng_dynamic_buffer_init(&listing);
+ ret = (int) nb_events;
+free_dynamic_buffer:
+ lttng_dynamic_buffer_reset(&listing);
+end:
+ lttng_payload_reset(&payload);
+ lttng_payload_reset(&payload_copy);
+ return ret;
+}
+
+/*
+ * Sets the tracing_group variable with name.
+ * This function allocates memory pointed to by tracing_group.
+ * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
+ */
+int lttng_set_tracing_group(const char *name)
+{
+ if (name == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ if (asprintf(&tracing_group, "%s", name) < 0) {
+ return -LTTNG_ERR_FATAL;
+ }
+
+ return 0;
+}
+
+int lttng_calibrate(struct lttng_handle *handle,
+ struct lttng_calibrate *calibrate)
+{
+ /*
+ * This command was removed in LTTng 2.9.
+ */
+ return -LTTNG_ERR_UND;
+}
+
+/*
+ * Set default channel attributes.
+ * If either or both of the arguments are null, attr content is zeroe'd.
+ */
+void lttng_channel_set_default_attr(struct lttng_domain *domain,
+ struct lttng_channel_attr *attr)
+{
+ struct lttng_channel_extended *extended;
+
+ /* Safety check */
+ if (attr == NULL || domain == NULL) {
+ return;
+ }
+
+ extended = (struct lttng_channel_extended *) attr->extended.ptr;
+ memset(attr, 0, sizeof(struct lttng_channel_attr));
+
+ /* Same for all domains. */
+ attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
+ attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
+ attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_KERNEL:
+ attr->switch_timer_interval =
+ DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
+ attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
+ attr->subbuf_size = default_get_kernel_channel_subbuf_size();
+ attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
+ attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+ if (extended) {
+ extended->monitor_timer_interval =
+ DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
+ extended->blocking_timeout =
+ DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
+ }
+ break;
+ case LTTNG_DOMAIN_UST:
+ switch (domain->buf_type) {
+ case LTTNG_BUFFER_PER_UID:
+ attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
+ attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
+ attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
+ attr->switch_timer_interval =
+ DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
+ attr->read_timer_interval =
+ DEFAULT_UST_UID_CHANNEL_READ_TIMER;
+ if (extended) {
+ extended->monitor_timer_interval =
+ DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
+ extended->blocking_timeout =
+ DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
+ }
+ break;
+ case LTTNG_BUFFER_PER_PID:
+ default:
+ attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
+ attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
+ attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
+ attr->switch_timer_interval =
+ DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
+ attr->read_timer_interval =
+ DEFAULT_UST_PID_CHANNEL_READ_TIMER;
+ if (extended) {
+ extended->monitor_timer_interval =
+ DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
+ extended->blocking_timeout =
+ DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
+ }
+ break;
+ }
+ default:
+ /* Default behavior: leave set to 0. */
+ break;
+ }
+
+ attr->extended.ptr = extended;
+}
+
+int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
+ uint64_t *discarded_events)
+{
+ int ret = 0;
+ struct lttng_channel_extended *chan_ext;
+
+ if (!channel || !discarded_events) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ chan_ext = channel->attr.extended.ptr;
+ if (!chan_ext) {
+ /*
+ * This can happen since the lttng_channel structure is
+ * used for other tasks where this pointer is never set.
+ */
+ *discarded_events = 0;
+ goto end;
+ }
+
+ *discarded_events = chan_ext->discarded_events;
+end:
+ return ret;
+}
+
+int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
+ uint64_t *lost_packets)
+{
+ int ret = 0;
+ struct lttng_channel_extended *chan_ext;
+
+ if (!channel || !lost_packets) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ chan_ext = channel->attr.extended.ptr;
+ if (!chan_ext) {
+ /*
+ * This can happen since the lttng_channel structure is
+ * used for other tasks where this pointer is never set.
+ */
+ *lost_packets = 0;
+ goto end;
+ }
+
+ *lost_packets = chan_ext->lost_packets;
+end:
+ return ret;
+}
+
+int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
+ uint64_t *monitor_timer_interval)
+{
+ int ret = 0;
+
+ if (!chan || !monitor_timer_interval) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (!chan->attr.extended.ptr) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ *monitor_timer_interval = ((struct lttng_channel_extended *)
+ chan->attr.extended.ptr)->monitor_timer_interval;
+end:
+ return ret;
+}
+
+int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
+ uint64_t monitor_timer_interval)
+{
+ int ret = 0;
+
+ if (!chan || !chan->attr.extended.ptr) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ((struct lttng_channel_extended *)
+ chan->attr.extended.ptr)->monitor_timer_interval =
+ monitor_timer_interval;
+end:
+ return ret;
+}
+
+int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
+ int64_t *blocking_timeout)
+{
+ int ret = 0;
+
+ if (!chan || !blocking_timeout) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (!chan->attr.extended.ptr) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ *blocking_timeout = ((struct lttng_channel_extended *)
+ chan->attr.extended.ptr)->blocking_timeout;
+end:
+ return ret;
+}
+
+int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
+ int64_t blocking_timeout)
+{
+ int ret = 0;
+ int64_t msec_timeout;
+
+ if (!chan || !chan->attr.extended.ptr) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (blocking_timeout < 0 && blocking_timeout != -1) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ /*
+ * LTTng-ust's use of poll() to implement this timeout mechanism forces
+ * us to accept a narrower range of values (msecs expressed as a signed
+ * 32-bit integer).
+ */
+ msec_timeout = blocking_timeout / 1000;
+ if (msec_timeout != (int32_t) msec_timeout) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ((struct lttng_channel_extended *)
+ chan->attr.extended.ptr)->blocking_timeout =
+ blocking_timeout;
+end:
+ return ret;
+}
+
+/*
+ * Check if session daemon is alive.
+ *
+ * Return 1 if alive or 0 if not.
+ * On error returns a negative value.
+ */
+int lttng_session_daemon_alive(void)
+{
+ int ret;
+
+ ret = set_session_daemon_path();
+ if (ret < 0) {
+ /* Error. */
+ return ret;
+ }