+/*
+ * Machine interface
+ * Jul and ust event listing
+ */
+static int mi_list_jul_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 */
+ 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);
+ 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;
+}
+