+ return ret;
+}
+
+/*
+ * Machine interface
+ * List all availables session
+ */
+static int mi_list_sessions(struct lttng_session *sessions, int count)
+{
+ int ret, i;
+
+ /* Opening sessions element */
+ ret = mi_lttng_sessions_open(writer);
+ if (ret) {
+ goto end;
+ }
+
+ /* Listing sessions */
+ for (i = 0; i < count; i++) {
+ ret = mi_lttng_session(writer, &sessions[i], 0);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ /* Closing sessions element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+/*
+ * List available tracing session. List only basic information.
+ *
+ * If session_name is NULL, all sessions are listed.
+ */
+static int list_sessions(const char *session_name)
+{
+ int ret = CMD_SUCCESS;
+ int count, i;
+ unsigned int session_found = 0;
+ struct lttng_session *sessions;
+
+ count = lttng_list_sessions(&sessions);
+ DBG("Session count %d", count);
+ if (count < 0) {
+ ret = CMD_ERROR;
+ ERR("%s", lttng_strerror(count));
+ goto end;
+ }
+
+ if (lttng_opt_mi) {
+ /* Mi */
+ if (session_name == NULL) {
+ /* List all session */
+ ret = mi_list_sessions(sessions, count);
+ } else {
+ /* Note : this return an open session element */
+ ret = mi_list_session(session_name, sessions, count);
+ }
+ if (ret) {
+ ret = CMD_ERROR;
+ goto error;
+ }
+ } else {
+ /* Pretty print */
+ if (count == 0) {
+ MSG("Currently no available tracing session");
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ if (session_name == NULL) {
+ MSG("Available tracing sessions:");
+ }
+
+
+ for (i = 0; i < count; i++) {
+ if (session_name != NULL) {
+ if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
+ session_found = 1;
+ MSG("Tracing session %s: [%s%s]", session_name,
+ active_string(sessions[i].enabled),
+ snapshot_string(sessions[i].snapshot_mode));
+ MSG("%sTrace path: %s\n", indent4, sessions[i].path);
+ break;
+ }
+ } else {
+ MSG(" %d) %s (%s) [%s%s]", i + 1,
+ sessions[i].name, sessions[i].path,
+ active_string(sessions[i].enabled),
+ snapshot_string(sessions[i].snapshot_mode));
+ MSG("%sTrace path: %s", indent4, sessions[i].path);
+ MSG("%sLive timer interval (usec): %u\n", indent4,
+ sessions[i].live_timer_interval);
+ break;
+ }
+ }
+
+ if (!session_found && session_name != NULL) {
+ ERR("Session '%s' not found", session_name);
+ ret = CMD_ERROR;
+ goto error;
+ }
+
+ if (session_name == NULL) {
+ MSG("\nUse lttng list <session_name> for more details");
+ }
+ }