+error:
+ return NULL;
+}
+
+/*
+ * trace_create_kernel_session
+ *
+ * Allocate and initialize a kernel session data structure.
+ *
+ * Return pointer to structure or NULL.
+ */
+struct ltt_kernel_session *trace_create_kernel_session(void)
+{
+ struct ltt_kernel_session *lks;
+
+ /* Allocate a new ltt kernel session */
+ lks = malloc(sizeof(struct ltt_kernel_session));
+ if (lks == NULL) {
+ perror("create kernel session malloc");
+ goto error;
+ }
+
+ /* Init data structure */
+ lks->fd = 0;
+ lks->metadata_stream_fd = 0;
+ lks->channel_count = 0;
+ lks->stream_count_global = 0;
+ lks->metadata = NULL;
+ CDS_INIT_LIST_HEAD(&lks->channel_list.head);
+
+ return lks;
+
+error:
+ return NULL;
+}
+
+/*
+ * trace_create_kernel_channel
+ *
+ * Allocate and initialize a kernel channel data structure.
+ *
+ * Return pointer to structure or NULL.
+ */
+struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
+{
+ int ret;
+ struct ltt_kernel_channel *lkc;
+
+ lkc = malloc(sizeof(struct ltt_kernel_channel));
+ if (lkc == NULL) {
+ perror("ltt_kernel_channel malloc");
+ goto error;
+ }
+
+ lkc->channel = malloc(sizeof(struct lttng_channel));
+ if (lkc->channel == NULL) {
+ perror("lttng_channel malloc");
+ goto error;
+ }
+ memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
+
+ lkc->fd = 0;
+ lkc->stream_count = 0;
+ lkc->enabled = 1;
+ /* Init linked list */
+ CDS_INIT_LIST_HEAD(&lkc->events_list.head);
+ CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
+ /* Set default trace output path */
+ ret = asprintf(&lkc->pathname, "%s", path);
+ if (ret < 0) {
+ perror("asprintf kernel create channel");
+ goto error;
+ }
+
+ return lkc;
+
+error:
+ return NULL;
+}
+
+/*
+ * trace_create_kernel_event
+ *
+ * Allocate and initialize a kernel event. Set name and event type.
+ *
+ * Return pointer to structure or NULL.
+ */
+struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
+{
+ struct ltt_kernel_event *lke;
+ struct lttng_kernel_event *attr;
+
+ lke = malloc(sizeof(struct ltt_kernel_event));
+ attr = malloc(sizeof(struct lttng_kernel_event));
+ if (lke == NULL || attr == NULL) {
+ perror("kernel event malloc");
+ goto error;
+ }
+
+ switch (ev->type) {
+ case LTTNG_EVENT_KPROBES:
+ attr->instrumentation = LTTNG_KERNEL_KPROBES;
+ attr->u.kprobe.addr = ev->attr.kprobe.addr;
+ attr->u.kprobe.offset = ev->attr.kprobe.offset;
+ strncpy(attr->u.kprobe.symbol_name,
+ ev->attr.kprobe.symbol_name, LTTNG_SYM_NAME_LEN);
+ break;
+ case LTTNG_EVENT_FUNCTION:
+ attr->instrumentation = LTTNG_KERNEL_FUNCTION;
+ strncpy(attr->u.ftrace.symbol_name,
+ ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
+ break;
+ case LTTNG_EVENT_TRACEPOINTS:
+ attr->instrumentation = LTTNG_KERNEL_TRACEPOINTS;
+ break;
+ default:
+ ERR("Unknown kernel instrumentation type (%d)", ev->type);
+ goto error;
+ }
+
+ /* Copy event name */
+ strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
+
+ /* Setting up a kernel event */
+ lke->fd = 0;
+ lke->event = attr;
+ lke->enabled = 1;
+
+ return lke;
+
+error:
+ return NULL;
+}
+
+/*
+ * trace_create_kernel_metadata
+ *
+ * Allocate and initialize a kernel metadata.
+ *
+ * Return pointer to structure or NULL.
+ */
+struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
+{
+ int ret;
+ struct ltt_kernel_metadata *lkm;
+ struct lttng_channel *chan;
+
+ lkm = malloc(sizeof(struct ltt_kernel_metadata));
+ chan = malloc(sizeof(struct lttng_channel));
+ if (lkm == NULL || chan == NULL) {
+ perror("kernel metadata malloc");
+ goto error;
+ }
+
+ /* 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;
+ chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+
+ /* Init metadata */
+ lkm->fd = 0;
+ lkm->conf = chan;
+ /* Set default metadata path */
+ ret = asprintf(&lkm->pathname, "%s/metadata", path);
+ if (ret < 0) {
+ perror("asprintf kernel metadata");
+ goto error;
+ }
+
+ return lkm;
+
+error: