+
+/*
+ * kernel_wait_quiescent
+ *
+ * Make a kernel wait to make sure in-flight probe have completed.
+ */
+void kernel_wait_quiescent(int fd)
+{
+ int ret;
+
+ DBG("Kernel quiescent wait on %d", fd);
+
+ ret = kernctl_wait_quiescent(fd);
+ if (ret < 0) {
+ perror("wait quiescent ioctl");
+ ERR("Kernel quiescent wait failed");
+ }
+}
+
+/*
+ * kernel_metadata_flush_buffer
+ *
+ * Force flush buffer of metadata.
+ */
+int kernel_metadata_flush_buffer(int fd)
+{
+ int ret;
+
+ ret = kernctl_buffer_flush(fd);
+ if (ret < 0) {
+ ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
+ }
+
+ return 0;
+}
+
+/*
+ * kernel_flush_buffer
+ *
+ * Force flush buffer for channel.
+ */
+int kernel_flush_buffer(struct ltt_kernel_channel *channel)
+{
+ int ret;
+ struct ltt_kernel_stream *stream;
+
+ DBG("Flush buffer for channel %s", channel->channel->name);
+
+ cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
+ DBG("Flushing channel stream %d", stream->fd);
+ ret = kernctl_buffer_flush(stream->fd);
+ if (ret < 0) {
+ perror("ioctl");
+ ERR("Fail to flush buffer for stream %d (ret: %d)",
+ stream->fd, ret);
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * kernel_stop_session
+ *
+ * Stop tracing session.
+ */
+int kernel_stop_session(struct ltt_kernel_session *session)
+{
+ int ret;
+
+ ret = kernctl_stop_session(session->fd);
+ if (ret < 0) {
+ goto error;
+ }
+
+ DBG("Kernel session stopped");
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * kernel_open_channel_stream
+ *
+ * Open stream of channel, register it to the kernel tracer and add it
+ * to the stream list of the channel.
+ *
+ * Return the number of created stream. Else, a negative value.
+ */
+int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
+{
+ int ret;
+ struct ltt_kernel_stream *lks;
+
+ while ((ret = kernctl_create_stream(channel->fd)) > 0) {
+ lks = trace_create_kernel_stream();
+ if (lks == NULL) {
+ close(ret);
+ goto error;
+ }
+
+ lks->fd = ret;
+ /* Prevent fd duplication after execlp() */
+ ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl session fd");
+ }
+
+ ret = asprintf(&lks->pathname, "%s/%s_%d",
+ channel->pathname, channel->channel->name, channel->stream_count);
+ if (ret < 0) {
+ perror("asprintf kernel create stream");
+ goto error;
+ }
+
+ /* Add stream to channe stream list */
+ cds_list_add(&lks->list, &channel->stream_list.head);
+ channel->stream_count++;
+
+ DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
+ channel->stream_count, lks->fd, lks->state, lks->pathname);
+ }
+
+ return channel->stream_count;
+
+error:
+ return -1;
+}
+
+/*
+ * kernel_open_metadata_stream
+ *
+ * Open the metadata stream and set it to the kernel session.
+ */
+int kernel_open_metadata_stream(struct ltt_kernel_session *session)
+{
+ int ret;
+
+ ret = kernctl_create_stream(session->metadata->fd);
+ if (ret < 0) {
+ perror("kernel create metadata stream");
+ goto error;
+ }
+
+ DBG("Kernel metadata stream created (fd: %d)", ret);
+ session->metadata_stream_fd = ret;
+ /* Prevent fd duplication after execlp() */
+ ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl session fd");
+ }
+
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * kernel_list_events
+ *
+ * Get the event list from the kernel tracer and return that list in the CTF
+ * format.
+ */
+ssize_t kernel_list_events(int tracer_fd, char **list)
+{
+ int fd;
+ char *buf, *line = NULL;
+ size_t nb, nbmem, total = 0;
+ ssize_t size;
+ FILE *fp;
+
+ fd = kernctl_tracepoint_list(tracer_fd);
+ if (fd < 0) {
+ perror("kernel tracepoint list");
+ goto error;
+ }
+
+ fp = fdopen(fd, "r");
+ if (fp == NULL) {
+ perror("kernel tracepoint list fdopen");
+ goto error;
+ }
+
+ /*
+ * Init memory size counter
+ * See kernel-ctl.h for explanation of this value
+ */
+ nbmem = KERNEL_EVENT_LIST_SIZE;
+ buf = malloc(nbmem);
+
+ while ((size = getline(&line, &nb, fp)) != -1) {
+ if (total + size > nbmem) {
+ DBG("Reallocating event list from %zd to %zd bytes", nbmem,
+ total + size + KERNEL_EVENT_LIST_SIZE);
+ /* Adding the default size again */
+ nbmem = total + size + KERNEL_EVENT_LIST_SIZE;
+ buf = realloc(buf, nbmem);
+ if (buf == NULL) {
+ perror("realloc list events");
+ goto error;
+ }
+ }
+ memcpy(buf + total, line, size);
+ total += size;
+ }
+
+ *list = buf;
+
+ DBG("Kernel list events done");
+
+ return total;
+
+error:
+ return -1;
+}