+/*
+ * Machine interface
+ * Jul and ust event listing
+ */
+static int mi_list_agent_ust_events(struct lttng_event *events, int count,
+ struct lttng_domain *domain)
+{
+ int ret, i;
+ pid_t cur_pid = 0;
+ char *cmdline = NULL;
+ int pid_element_open = 0;
+
+ /* Open domains element */
+ ret = mi_lttng_domains_open(writer);
+ if (ret) {
+ goto end;
+ }
+
+ /* Write domain */
+ ret = mi_lttng_domain(writer, domain, 1);
+ if (ret) {
+ goto end;
+ }
+
+ /* Open pids element element */
+ ret = mi_lttng_pids_open(writer);
+ if (ret) {
+ goto end;
+ }
+
+ for (i = 0; i < count; i++) {
+ if (cur_pid != events[i].pid) {
+ if (pid_element_open) {
+ /* Close the previous events and pid element */
+ ret = mi_lttng_close_multi_element(writer, 2);
+ if (ret) {
+ goto end;
+ }
+ pid_element_open = 0;
+ }
+
+ cur_pid = events[i].pid;
+ cmdline = get_cmdline_by_pid(cur_pid);
+ if (!cmdline) {
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ if (!pid_element_open) {
+ /* Open and write a pid element */
+ ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
+ if (ret) {
+ goto error;
+ }
+
+ /* Open events element */
+ ret = mi_lttng_events_open(writer);
+ if (ret) {
+ goto error;
+ }
+
+ pid_element_open = 1;
+ }
+ free(cmdline);
+ }
+
+ /* Write an event */
+ ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ /* Close pids */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+
+ /* Close domain, domains */
+ ret = mi_lttng_close_multi_element(writer, 2);
+end:
+ return ret;
+error:
+ free(cmdline);
+ return ret;
+}
+
+static int list_agent_events(void)
+{
+ int i, size, ret = CMD_SUCCESS;
+ struct lttng_domain domain;
+ struct lttng_handle *handle = NULL;
+ struct lttng_event *event_list = NULL;
+ pid_t cur_pid = 0;
+ char *cmdline = NULL;
+ const char *agent_domain_str;
+
+ memset(&domain, 0, sizeof(domain));
+ if (opt_jul) {
+ domain.type = LTTNG_DOMAIN_JUL;
+ } else if (opt_log4j) {
+ domain.type = LTTNG_DOMAIN_LOG4J;
+ } else if (opt_python) {
+ domain.type = LTTNG_DOMAIN_PYTHON;
+ } else {
+ ERR("Invalid agent domain selected.");
+ ret = CMD_ERROR;
+ goto error;
+ }
+
+ agent_domain_str = get_domain_str(domain.type);
+
+ DBG("Getting %s tracing events", agent_domain_str);
+
+ handle = lttng_create_handle(NULL, &domain);
+ if (handle == NULL) {
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ size = lttng_list_tracepoints(handle, &event_list);
+ if (size < 0) {
+ ERR("Unable to list %s events: %s", agent_domain_str,
+ lttng_strerror(size));
+ ret = CMD_ERROR;
+ goto end;
+ }
+
+ if (lttng_opt_mi) {
+ /* Mi print */
+ ret = mi_list_agent_ust_events(event_list, size, &domain);
+ if (ret) {
+ ret = CMD_ERROR;
+ goto error;
+ }
+ } else {
+ /* Pretty print */
+ MSG("%s events (Logger name):\n-------------------------",
+ agent_domain_str);
+
+ if (size == 0) {
+ MSG("None");
+ }
+
+ for (i = 0; i < size; i++) {
+ if (cur_pid != event_list[i].pid) {
+ cur_pid = event_list[i].pid;
+ cmdline = get_cmdline_by_pid(cur_pid);
+ if (cmdline == NULL) {
+ ret = CMD_ERROR;
+ goto error;
+ }
+ MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
+ free(cmdline);
+ }
+ MSG("%s- %s", indent6, event_list[i].name);
+ }
+
+ MSG("");
+ }
+
+error:
+ free(event_list);
+end:
+ lttng_destroy_handle(handle);
+ return ret;
+}
+