+
+ /* Set default attributes */
+ chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
+ chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
+ chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
+ chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
+ chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
+
+ /* Init metadata */
+ lkm->fd = 0;
+ lkm->conf = chan;
+ /* Set default metadata path */
+ ret = asprintf(&lkm->pathname, "%s/metadata", DEFAULT_TRACE_OUTPUT);
+ if (ret < 0) {
+ perror("asprintf kernel metadata");
+ goto error;
+ }
+
+ return lkm;
+
+error:
+ return NULL;
+}
+
+/*
+ * trace_create_kernel_stream
+ *
+ * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
+ * default.
+ *
+ * Return pointer to structure or NULL.
+ */
+struct ltt_kernel_stream *trace_create_kernel_stream(void)
+{
+ struct ltt_kernel_stream *lks;
+
+ lks = malloc(sizeof(struct ltt_kernel_stream));
+ if (lks == NULL) {
+ perror("kernel stream malloc");
+ goto error;
+ }
+
+ /* Init stream */
+ lks->fd = 0;
+ lks->pathname = NULL;
+ lks->state = 0;
+
+ return lks;
+
+error:
+ return NULL;
+}
+
+void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
+{
+ DBG("[trace] Closing stream fd %d", stream->fd);
+ /* Close kernel fd */
+ close(stream->fd);
+ free(stream->pathname);
+
+ /* Remove from stream list */
+ cds_list_del(&stream->list);
+ free(stream);
+}
+
+void trace_destroy_kernel_event(struct ltt_kernel_event *event)
+{
+ DBG("[trace] Closing event fd %d", event->fd);
+ /* Close kernel fd */
+ close(event->fd);
+ /* Free attributes */
+ free(event->event);
+
+ /* Remove from event list */
+ cds_list_del(&event->list);
+ free(event);
+}
+
+void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
+{
+ struct ltt_kernel_stream *stream;
+ struct ltt_kernel_event *event;
+
+ DBG("[trace] Closing channel fd %d", channel->fd);
+ /* Close kernel fd */
+ close(channel->fd);
+ free(channel->pathname);
+ /* Free attributes structure */
+ free(channel->channel);
+
+ /* For each stream in the channel list */
+ cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
+ trace_destroy_kernel_stream(stream);
+ }
+
+ /* For each event in the channel list */
+ cds_list_for_each_entry(event, &channel->events_list.head, list) {
+ trace_destroy_kernel_event(event);
+ }
+
+ /* Remove from channel list */
+ cds_list_del(&channel->list);
+ free(channel);
+}
+
+void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
+{
+ DBG("[trace] Closing metadata fd %d", metadata->fd);
+ /* Close kernel fd */
+ close(metadata->fd);
+ /* Free attributes */
+ free(metadata->conf);
+
+ free(metadata);
+}
+
+void trace_destroy_kernel_session(struct ltt_kernel_session *session)
+{
+ struct ltt_kernel_channel *channel;
+
+ DBG("[trace] Closing session fd %d", session->fd);
+ /* Close kernel fds */
+ close(session->fd);
+ if (session->metadata_stream_fd != 0) {
+ DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
+ close(session->metadata_stream_fd);
+ }
+
+ trace_destroy_kernel_metadata(session->metadata);
+
+ cds_list_for_each_entry(channel, &session->channel_list.head, list) {
+ trace_destroy_kernel_channel(channel);
+ }
+
+ free(session);