+static void increment_extended_len(const char *filter_expression,
+ size_t *extended_len)
+{
+ *extended_len += sizeof(struct lttcomm_event_extended_header);
+
+ if (filter_expression) {
+ *extended_len += strlen(filter_expression) + 1;
+ }
+}
+
+static void append_extended_info(const char *filter_expression,
+ void **extended_at)
+{
+ struct lttcomm_event_extended_header extended_header;
+ size_t filter_len = 0;
+
+ if (filter_expression) {
+ filter_len = strlen(filter_expression) + 1;
+ }
+
+ extended_header.filter_len = filter_len;
+ memcpy(*extended_at, &extended_header, sizeof(extended_header));
+ *extended_at += sizeof(extended_header);
+ memcpy(*extended_at, filter_expression, filter_len);
+ *extended_at += filter_len;
+}
+
+/*
+ * Create a list of agent domain events.
+ *
+ * Return number of events in list on success or else a negative value.
+ */
+static int list_lttng_agent_events(struct agent *agt,
+ struct lttng_event **events, size_t *total_size)
+{
+ int i = 0, ret = 0;
+ unsigned int nb_event = 0;
+ struct agent_event *event;
+ struct lttng_event *tmp_events;
+ struct lttng_ht_iter iter;
+ size_t extended_len = 0;
+ void *extended_at;
+
+ assert(agt);
+ assert(events);
+
+ DBG3("Listing agent events");
+
+ rcu_read_lock();
+ nb_event = lttng_ht_get_count(agt->events);
+ rcu_read_unlock();
+ if (nb_event == 0) {
+ ret = nb_event;
+ *total_size = 0;
+ goto error;
+ }
+
+ /* Compute required extended infos size */
+ extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
+
+ /*
+ * This is only valid because the commands which add events are
+ * processed in the same thread as the listing.
+ */
+ rcu_read_lock();
+ cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
+ increment_extended_len(event->filter_expression, &extended_len);
+ }
+ rcu_read_unlock();
+
+ *total_size = nb_event * sizeof(*tmp_events) + extended_len;
+ tmp_events = zmalloc(*total_size);
+ if (!tmp_events) {
+ PERROR("zmalloc agent events session");
+ ret = -LTTNG_ERR_FATAL;
+ goto error;
+ }
+
+ extended_at = ((uint8_t *) tmp_events) +
+ nb_event * sizeof(struct lttng_event);
+
+ rcu_read_lock();
+ cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
+ strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
+ tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
+ tmp_events[i].enabled = event->enabled;
+ tmp_events[i].loglevel = event->loglevel_value;
+ tmp_events[i].loglevel_type = event->loglevel_type;
+ i++;
+
+ /* Append extended info */
+ append_extended_info(event->filter_expression, &extended_at);
+ }
+ rcu_read_unlock();
+
+ *events = tmp_events;
+ ret = nb_event;
+
+error:
+ assert(nb_event == i);
+ return ret;
+}
+