+ CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
+ cds_list_add(&lkc->list, &session->channel_list.head);
+ session->channel_count++;
+
+ DBG("Kernel channel created (fd: %d)", lkc->fd);
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * kernel_enable_event
+ *
+ * Enable kernel event.
+ */
+int kernel_enable_event(struct ltt_kernel_session *session, char *name)
+{
+ int ret;
+ struct ltt_kernel_channel *chan;
+ struct ltt_kernel_event *event;
+ struct lttng_kernel_event *lke;
+
+ event = malloc(sizeof(struct ltt_kernel_event));
+ lke = malloc(sizeof(struct lttng_kernel_event));
+
+ if (event == NULL || lke == NULL) {
+ perror("kernel enable event malloc");
+ ret = -errno;
+ goto error;
+ }
+
+ /* Setting up a kernel event */
+ strncpy(lke->name, name, LTTNG_SYM_NAME_LEN);
+ lke->instrumentation = LTTNG_KERNEL_TRACEPOINTS;
+ event->event = lke;
+
+ cds_list_for_each_entry(chan, &session->channel_list.head, list) {
+ ret = kernctl_create_event(chan->fd, lke);
+ if (ret < 0) {
+ goto error;
+ }
+
+ event->fd = ret;
+ /* Add event to event list */
+ cds_list_add(&event->list, &chan->events_list.head);
+ DBG("Event %s enabled (fd: %d)", name, event->fd);
+ }
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * kernel_open_metadata
+ *
+ * Open metadata stream.
+ */
+int kernel_open_metadata(struct ltt_kernel_session *session)
+{
+ int ret;
+ struct ltt_kernel_metadata *lkm;
+ struct lttng_kernel_channel *conf;
+
+ lkm = malloc(sizeof(struct ltt_kernel_metadata));
+ conf = malloc(sizeof(struct lttng_kernel_channel));
+
+ if (lkm == NULL || conf == NULL) {
+ perror("kernel open metadata malloc");
+ ret = -errno;
+ goto error;
+ }
+
+ conf->overwrite = DEFAULT_KERNEL_OVERWRITE;
+ conf->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
+ conf->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
+ conf->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
+ conf->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
+
+ ret = kernctl_open_metadata(session->fd, conf);
+ if (ret < 0) {
+ goto error;
+ }
+
+ lkm->fd = ret;
+ lkm->conf = conf;
+ ret = asprintf(&lkm->pathname, "%s/metadata", DEFAULT_TRACE_OUTPUT);
+ if (ret < 0) {
+ perror("asprintf kernel metadata");
+ goto error;
+ }
+ session->metadata = lkm;
+
+ DBG("Kernel metadata opened (fd: %d)", lkm->fd);
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * kernel_start_session
+ *
+ * Start tracing session.
+ */
+int kernel_start_session(struct ltt_kernel_session *session)
+{
+ int ret;
+
+ ret = kernctl_start_session(session->fd);
+ if (ret < 0) {
+ goto error;
+ }
+
+ DBG("Kernel session started");
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * 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_create_channel_stream
+ *
+ * Create a stream for a channel.
+ *
+ * Return the number of created stream. Else, a negative value.
+ */
+int kernel_create_channel_stream(struct ltt_kernel_channel *channel)
+{
+ int ret;
+ struct ltt_kernel_stream *lks;
+
+ while ((ret = kernctl_create_stream(channel->fd)) > 0) {
+ lks = malloc(sizeof(struct ltt_kernel_stream));
+ if (lks == NULL) {
+ perror("kernel create stream malloc");
+ ret = -errno;
+ goto error;
+ }
+
+ lks->fd = ret;
+ ret = asprintf(&lks->pathname, "%s/trace_%d",
+ channel->pathname, channel->stream_count);
+ if (ret < 0) {
+ perror("asprintf kernel create stream");
+ goto error;
+ }
+ lks->state = 0;
+
+ cds_list_add(&lks->list, &channel->stream_list.head);
+ channel->stream_count++;
+ }
+
+ DBG("Kernel channel stream created (num: %d)", channel->stream_count);
+
+ return channel->stream_count;
+
+error:
+ return ret;
+}
+
+/*
+ * kernel_create_metadata_stream
+ *
+ * Create the metadata stream.
+ */
+int kernel_create_metadata_stream(struct ltt_kernel_session *session)
+{
+ int ret;
+
+ ret = kernctl_create_stream(session->metadata->fd);
+ if (ret < 0) {
+ perror("kernel create metadata stream");
+ ret = -errno;
+ goto error;
+ }