+static int list_channels(const char *channel_name)
+{
+ int count, i, ret = CMD_SUCCESS;
+ unsigned int chan_found = 0;
+ struct lttng_channel *channels = NULL;
+
+ DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
+
+ count = lttng_list_channels(handle, &channels);
+ if (count < 0) {
+ switch (-count) {
+ case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
+ if (lttng_opt_mi) {
+ /* When printing mi this is not an error
+ * but an empty channels element */
+ count = 0;
+ } else {
+ ret = CMD_SUCCESS;
+ WARN("No kernel channel");
+ goto error_channels;
+ }
+ break;
+ default:
+ /* We had a real error */
+ ret = CMD_ERROR;
+ ERR("%s", lttng_strerror(count));
+ goto error_channels;
+ break;
+ }
+ }
+
+ if (lttng_opt_mi) {
+ /* Mi print */
+ ret = mi_list_channels(channels, count, channel_name);
+ if (ret) {
+ ret = CMD_ERROR;
+ goto error;
+ }
+ } else {
+ /* Pretty print */
+ if (count) {
+ MSG("Channels:\n-------------");
+ }
+
+ for (i = 0; i < count; i++) {
+ if (channel_name != NULL) {
+ if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
+ chan_found = 1;
+ } else {
+ continue;
+ }
+ }
+ print_channel(&channels[i]);
+
+ /* Listing events per channel */
+ ret = list_events(channels[i].name);
+ if (ret) {
+ goto error;
+ }
+
+ if (chan_found) {
+ break;
+ }
+ }
+
+ if (!chan_found && channel_name != NULL) {
+ ret = CMD_ERROR;
+ ERR("Channel %s not found", channel_name);
+ goto error;
+ }
+ }
+error:
+ free(channels);
+
+error_channels:
+ return ret;
+}
+
+static const char *get_tracker_str(enum lttng_tracker_type tracker_type)
+{
+ switch (tracker_type) {
+ case LTTNG_TRACKER_PID:
+ return "PID";
+ case LTTNG_TRACKER_VPID:
+ return "VPID";
+ case LTTNG_TRACKER_UID:
+ return "UID";
+ case LTTNG_TRACKER_VUID:
+ return "VUID";
+ case LTTNG_TRACKER_GID:
+ return "GID";
+ case LTTNG_TRACKER_VGID:
+ return "VGID";
+ }
+ return NULL;
+}
+
+/*
+ * List tracker ID(s) of session and domain.
+ */
+static int list_tracker_ids(enum lttng_tracker_type tracker_type)
+{
+ int ret = 0;
+ int enabled = 1;
+ struct lttng_tracker_id *ids = NULL;
+ size_t nr_ids, i;
+
+ ret = lttng_list_tracker_ids(handle, tracker_type, &ids, &nr_ids);
+ if (ret) {
+ return ret;
+ }
+ if (nr_ids == 1 && ids[0].type == LTTNG_ID_ALL) {
+ enabled = 0;
+ }
+ if (enabled) {
+ _MSG("%s tracker: [", get_tracker_str(tracker_type));
+
+ /* Mi tracker_id element */
+ if (writer) {
+ /* Open tracker_id and targets elements */
+ ret = mi_lttng_id_tracker_open(writer, tracker_type);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ for (i = 0; i < nr_ids; i++) {
+ struct lttng_tracker_id *id = &ids[i];
+
+ if (i) {
+ _MSG(",");
+ }
+ switch (id->type) {
+ case LTTNG_ID_ALL:
+ _MSG(" *");
+ break;
+ case LTTNG_ID_VALUE:
+ _MSG(" %d", ids[i].value);
+ break;
+ case LTTNG_ID_STRING:
+ _MSG(" %s", ids[i].string);
+ break;
+ case LTTNG_ID_UNKNOWN:
+ return CMD_ERROR;
+ }
+
+ /* Mi */
+ if (writer) {
+ ret = mi_lttng_id_target(
+ writer, tracker_type, id, 0);
+ if (ret) {
+ goto end;
+ }
+ }
+ }
+ _MSG(" ]\n\n");
+
+ /* Mi close tracker_id and targets */
+ if (writer) {
+ ret = mi_lttng_close_multi_element(writer, 2);
+ if (ret) {
+ goto end;
+ }
+ }
+ }
+end:
+ for (i = 0; i < nr_ids; i++) {
+ free(ids[i].string);
+ }
+ free(ids);
+ return ret;
+}
+
+/*
+ * List all trackers of a domain
+ */
+static int list_trackers(const struct lttng_domain *domain)
+{
+ int ret;
+
+ /* Trackers listing */
+ if (lttng_opt_mi) {
+ ret = mi_lttng_trackers_open(writer);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_KERNEL:
+ /* pid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_PID);
+ if (ret) {
+ goto end;
+ }
+ /* vpid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VPID);
+ if (ret) {
+ goto end;
+ }
+ /* uid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_UID);
+ if (ret) {
+ goto end;
+ }
+ /* vuid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VUID);
+ if (ret) {
+ goto end;
+ }
+ /* gid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_GID);
+ if (ret) {
+ goto end;
+ }
+ /* vgid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VGID);
+ if (ret) {
+ goto end;
+ }
+ break;
+ case LTTNG_DOMAIN_UST:
+ /* vpid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VPID);
+ if (ret) {
+ goto end;
+ }
+ /* vuid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VUID);
+ if (ret) {
+ goto end;
+ }
+ /* vgid tracker */
+ ret = list_tracker_ids(LTTNG_TRACKER_VGID);
+ if (ret) {
+ goto end;
+ }
+ break;
+ default:
+ break;
+ }
+ if (lttng_opt_mi) {
+ /* Close trackers element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+ }
+
+end:
+ return ret;
+}
+
+static enum cmd_error_code print_periodic_rotation_schedule(
+ const struct lttng_rotation_schedule *schedule)
+{
+ enum cmd_error_code ret;
+ enum lttng_rotation_status status;
+ uint64_t value;
+
+ status = lttng_rotation_schedule_periodic_get_period(schedule,
+ &value);
+ if (status != LTTNG_ROTATION_STATUS_OK) {
+ ERR("Failed to retrieve period parameter from periodic rotation schedule.");
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT);
+ ret = CMD_SUCCESS;
+end:
+ return ret;
+}
+
+static enum cmd_error_code print_size_threshold_rotation_schedule(
+ const struct lttng_rotation_schedule *schedule)
+{
+ enum cmd_error_code ret;
+ enum lttng_rotation_status status;
+ uint64_t value;
+
+ status = lttng_rotation_schedule_size_threshold_get_threshold(schedule,
+ &value);
+ if (status != LTTNG_ROTATION_STATUS_OK) {
+ ERR("Failed to retrieve size parameter from size-based rotation schedule.");
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ MSG(" size threshold: %" PRIu64" bytes", value);
+ ret = CMD_SUCCESS;
+end:
+ return ret;
+}
+
+static enum cmd_error_code print_rotation_schedule(
+ const struct lttng_rotation_schedule *schedule)
+{
+ enum cmd_error_code ret;
+
+ switch (lttng_rotation_schedule_get_type(schedule)) {
+ case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
+ ret = print_size_threshold_rotation_schedule(schedule);
+ break;
+ case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
+ ret = print_periodic_rotation_schedule(schedule);
+ break;
+ default:
+ ret = CMD_ERROR;
+ }
+ return ret;
+}
+
+/*
+ * List the automatic rotation settings.
+ */
+static enum cmd_error_code list_rotate_settings(const char *session_name)