+
+error:
+ return ret;
+}
+
+/*
+ * process_opt_session_uuid
+ *
+ * Set current session uuid to the current flow of
+ * command(s) using the already shorten uuid.
+ */
+static int process_opt_session_uuid(void)
+{
+ int ret, count, i;
+ struct lttng_session *sessions;
+
+ count = lttng_list_sessions(&sessions);
+ if (count < 0) {
+ ret = count;
+ goto error;
+ }
+
+ for (i = 0; i < count; i++) {
+ if (strncmp(sessions[i].uuid, short_uuid, 8) == 0) {
+ lttng_set_current_session_uuid(sessions[i].uuid);
+ break;
+ }
+ }
+
+ free(sessions);
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * process_opt_list_traces
+ *
+ * Get list of all traces for a specific session uuid.
+ */
+static int process_opt_list_traces(void)
+{
+ int ret, i;
+ uuid_t uuid;
+ struct lttng_trace *traces;
+
+ uuid_parse(opt_session_uuid, uuid);
+ ret = lttng_list_traces(&uuid, &traces);
+ if (ret < 0) {
+ goto error;
+ }
+
+ MSG("Userspace traces:");
+ for (i = 0; i < ret; i++) {
+ if (traces[i].type == USERSPACE) {
+ MSG("\t%d) %s (pid: %d): %s",
+ i, traces[i].name, traces[i].pid,
+ get_cmdline_by_pid(traces[i].pid));
+ } else {
+ break;
+ }
+ }
+
+ MSG("Kernel traces:");
+ for (;i < ret; i++) {
+ if (traces[i].type == KERNEL) {
+ MSG("\t%d) %s", i, traces[i].name);
+ }
+ }
+
+ free(traces);
+
+error:
+ return ret;
+}
+
+/*
+ * process_opt_create_session
+ *
+ * Create a new session using the name pass
+ * to the command line.
+ */
+static int process_opt_create_session(void)
+{
+ int ret;
+ uuid_t session_id;
+ char str_uuid[37];
+
+ ret = lttng_create_session(opt_create_session, &session_id);
+ if (ret < 0) {
+ goto error;
+ }
+
+ uuid_unparse(session_id, str_uuid);
+
+ MSG("Session created:");
+ MSG(" %s (%s)", opt_create_session, str_uuid);
+
+error:
+ return ret;
+}
+
+/*
+ * extract_short_uuid
+ *
+ * Extract shorten uuid and copy it to out.
+ * Shorten uuid format : '<name>.<short_uuid>'
+ */
+static int extract_short_uuid(char *in, char *out)
+{
+ char *tok;
+
+ tok = strchr(in, '.');
+ if (strlen(tok+1) == 8) {
+ memcpy(out, tok+1, 8);
+ out[9] = '\0';
+ return 0;
+ }
+
+ return -1;
+}
+
+/*
+ * shorten_uuid
+ *
+ * Small function to shorten the 37 bytes long uuid_t
+ * string representation to 8 characters.
+ */
+static void shorten_uuid(char *in, char *out)
+{
+ memcpy(out, in, 8);
+ out[8] = '\0';
+}
+
+/*
+ * process_opt_list_sessions
+ *
+ * Get the list of available sessions from
+ * the session daemon and print it to user.
+ */
+static int process_opt_list_sessions(void)
+{
+ int ret, count, i;
+ char tmp_short_uuid[9];
+ struct lttng_session *sess;
+
+ count = lttng_list_sessions(&sess);
+ DBG("Session count %d", count);
+ if (count < 0) {
+ ret = count;
+ goto error;
+ }
+
+ MSG("Available sessions (UUIDs):");
+ for (i = 0; i < count; i++) {
+ shorten_uuid(sess[i].uuid, tmp_short_uuid);
+ MSG(" %d) %s.%s", i+1, sess[i].name, tmp_short_uuid);
+ }
+
+ free(sess);
+ MSG("\nTo select a session, use -s, --session UUID.");
+
+ return 0;
+
+error:
+ return ret;