+error: /* fall through */
+ return ret;
+}
+
+/*
+ * set_opt_session_info
+ *
+ * Setup session_name, current_uuid, short_str_uuid and
+ * long_str_uuid using the command line options.
+ */
+static void set_opt_session_info(void)
+{
+ int count, i, short_len;
+ char *tok;
+ struct lttng_session *sessions;
+
+ if (opt_session_uuid != NULL) {
+ short_len = sizeof(short_str_uuid) - 1;
+ /* Shorten uuid */
+ tok = strchr(opt_session_uuid, '.');
+ if (strlen(tok + 1) == short_len) {
+ memcpy(short_str_uuid, tok + 1, short_len);
+ short_str_uuid[short_len] = '\0';
+ }
+
+ /* Get long real uuid_t from session daemon */
+ count = lttng_list_sessions(&sessions);
+ for (i = 0; i < count; i++) {
+ uuid_unparse(sessions[i].uuid, long_str_uuid);
+ if (strncmp(long_str_uuid, short_str_uuid, 8) == 0) {
+ uuid_copy(current_uuid, sessions[i].uuid);
+ break;
+ }
+ }
+ }
+
+ if (opt_session_name != NULL) {
+ session_name = strndup(opt_session_name, NAME_MAX);
+ }
+}
+
+/*
+ * shorten_uuid
+ *
+ * Small function to shorten the 37 bytes long uuid_t
+ * string representation to 8 characters.
+ */
+static void shorten_uuid(char *long_u, char *short_u)
+{
+ memcpy(short_u, long_u, 8);
+ short_u[UUID_SHORT_STR_LEN - 1] = '\0';
+}
+
+/*
+ * set_session_uuid
+ *
+ * Set current session uuid to the current flow of
+ * command(s) using the already shorten uuid or
+ * current full uuid.
+ */
+static int set_session_uuid(void)
+{
+ int ret, count, i;
+ char str_uuid[37];
+ struct lttng_session *sessions;
+
+ if (!uuid_is_null(current_uuid)) {
+ lttng_set_current_session_uuid(¤t_uuid);
+ goto end;
+ }
+
+ count = lttng_list_sessions(&sessions);
+ if (count < 0) {
+ ret = count;
+ goto error;
+ }
+
+ for (i = 0; i < count; i++) {
+ uuid_unparse(sessions[i].uuid, str_uuid);
+ if (strncmp(str_uuid, short_str_uuid, 8) == 0) {
+ lttng_set_current_session_uuid(&sessions[i].uuid);
+ break;
+ }
+ }
+
+ free(sessions);
+
+end:
+ 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;
+ struct lttng_trace *traces;
+
+ ret = lttng_list_traces(¤t_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];
+ char name[NAME_MAX];
+ time_t rawtime;
+ struct tm *timeinfo;
+
+ /* Auto session creation */
+ if (opt_create_session == 0) {
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+ strftime(name, sizeof(name), "%Y%m%d-%H%M%S", timeinfo);
+ session_name = strndup(name, sizeof(name));
+ }
+
+ ret = lttng_create_session(session_name, &session_id);
+ if (ret < 0) {
+ goto error;
+ }
+
+ uuid_unparse(session_id, str_uuid);
+ uuid_copy(current_uuid, session_id);
+ shorten_uuid(str_uuid, short_str_uuid);
+
+ MSG("Session UUID created: %s.%s", session_name, short_str_uuid);
+
+error: