+ lttng_ctl_copy_lttng_domain(&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;
+ lttng_ctl_copy_lttng_domain(&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);
+}
+
+/*
+ * Create a brand new session using name and url for destination.
+ *
+ * Returns LTTNG_OK on success or a negative error code.
+ */
+int lttng_create_session(const char *name, const char *url)
+{
+ int ret;
+ ssize_t size;
+ struct lttcomm_session_msg lsm;
+ struct lttng_uri *uris = NULL;
+
+ if (name == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+
+ lsm.cmd_type = LTTNG_CREATE_SESSION;
+ lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
+
+ /* There should never be a data URL */
+ size = uri_parse_str_urls(url, NULL, &uris);
+ if (size < 0) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ lsm.u.uri.size = size;
+
+ ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
+ sizeof(struct lttng_uri) * size, NULL);
+
+ free(uris);
+ return ret;
+}
+
+/*
+ * Destroy session using name.
+ * Returns size of returned session payload data or a negative error code.
+ */
+static
+int _lttng_destroy_session(const char *session_name)
+{
+ struct lttcomm_session_msg lsm;
+
+ if (session_name == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_DESTROY_SESSION;
+
+ lttng_ctl_copy_string(lsm.session.name, session_name,
+ sizeof(lsm.session.name));
+
+ return lttng_ctl_ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * Stop the session and wait for the data before destroying it
+ */
+int lttng_destroy_session(const char *session_name)
+{
+ int ret;
+
+ /*
+ * Stop the tracing and wait for the data.
+ */
+ ret = _lttng_stop_tracing(session_name, 1);
+ if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
+ goto end;
+ }
+
+ ret = _lttng_destroy_session(session_name);
+end:
+ return ret;
+}
+
+/*
+ * Destroy the session without waiting for the data.
+ */
+int lttng_destroy_session_no_wait(const char *session_name)
+{
+ int ret;
+
+ /*
+ * Stop the tracing without waiting for the data.
+ * The session might already have been stopped, so just
+ * skip this error.
+ */
+ ret = _lttng_stop_tracing(session_name, 0);
+ if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
+ goto end;
+ }
+
+ ret = _lttng_destroy_session(session_name);
+end:
+ return ret;
+}
+
+/*
+ * 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 **sessions)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_SESSIONS;
+ ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return ret / sizeof(struct lttng_session);
+}
+
+int lttng_set_session_shm_path(const char *session_name,
+ const char *shm_path)
+{
+ 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;
+
+ lttng_ctl_copy_string(lsm.session.name, session_name,
+ sizeof(lsm.session.name));
+ lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
+ sizeof(lsm.u.set_shm_path.shm_path));
+
+ return lttng_ctl_ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * 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) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_DOMAINS;
+
+ lttng_ctl_copy_string(lsm.session.name, session_name,
+ sizeof(lsm.session.name));
+
+ ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return ret / sizeof(struct lttng_domain);
+}
+
+/*
+ * 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;
+ lttng_ctl_copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+
+ lttng_ctl_copy_lttng_domain(&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;
+ struct lttcomm_event_command_header *cmd_header = NULL;
+ size_t cmd_header_len;
+ uint32_t nb_events, i;
+ void *comm_ext_at;
+ void *listing_at;
+ char *reception_buffer = NULL;
+ char *listing = NULL;
+ size_t storage_req;
+
+ /* Safety check. An handle and channel name are mandatory */
+ if (handle == NULL || channel_name == NULL) {
+ return -LTTNG_ERR_INVALID;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_LIST_EVENTS;
+ lttng_ctl_copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
+ sizeof(lsm.u.list.channel_name));
+ lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
+
+ ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
+ (void **) &reception_buffer, (void **) &cmd_header,
+ &cmd_header_len);
+ if (ret < 0) {
+ goto end;
+ }
+
+ /* Set number of events and free command header */
+ nb_events = cmd_header->nb_events;
+ if (nb_events > INT_MAX) {
+ ret = -EOVERFLOW;
+ 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 by simply calling free() on the
+ * returned buffer. This is needed in order to maintain API
+ * compatibility.
+ *
+ * A first pass is performed to figure the size of the buffer that
+ * must be returned. 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;
+
+ comm_ext_at += sizeof(*ext_comm);
+ comm_ext_at += ext_comm->filter_len;
+ comm_ext_at +=
+ ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
+
+ storage_req += sizeof(struct lttng_event_extended);
+ /* TODO: missing size of flat userspace probe. */
+ storage_req += ext_comm->filter_len;
+ storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
+ storage_req += storage_req % 8;
+ }
+
+ listing = zmalloc(storage_req);
+ if (!listing) {
+ goto end;
+ }
+ memcpy(listing, reception_buffer,
+ nb_events * sizeof(struct lttng_event));
+
+ comm_ext_at = reception_buffer +
+ (nb_events * sizeof(struct lttng_event));
+ listing_at = listing +
+ (nb_events * sizeof(struct lttng_event));
+ for (i = 0; i < nb_events; i++) {
+ struct lttng_event *event = (struct lttng_event *)
+ (listing + (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_at;
+
+ event->extended.ptr = event_extended;
+ listing_at += sizeof(*event_extended);
+ comm_ext_at += sizeof(*ext_comm);
+
+ /* Copy filter expression. */
+ memcpy(listing_at, comm_ext_at, ext_comm->filter_len);
+ event_extended->filter_expression = listing_at;
+ comm_ext_at += ext_comm->filter_len;
+ listing_at += ext_comm->filter_len;
+
+ /* Copy exclusions. */
+ event_extended->exclusions.count = ext_comm->nb_exclusions;
+ event_extended->exclusions.strings = listing_at;
+ memcpy(listing_at, comm_ext_at,
+ ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN);
+ listing_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
+ comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
+
+ listing_at += ((uintptr_t) listing_at) % 8;
+ }
+
+ *events = (struct lttng_event *) listing;
+ listing = NULL;
+ ret = (int) nb_events;
+end:
+ free(cmd_header);
+ free(reception_buffer);
+ free(listing);
+ 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;