+ trace_count = bt_value_array_get_size(query_result);
+ if (trace_count < 0) {
+ ret = -1;
+ goto error;
+ }
+
+ for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
+ int64_t begin, end;
+ uint64_t stream_idx;
+ int64_t stream_count;
+
+ trace_info = bt_value_array_borrow_element_by_index_const(
+ query_result, trace_idx);
+ if (!trace_info || !bt_value_is_map(trace_info)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve trace from query result.");
+ goto error;
+ }
+
+ intersection_range = bt_value_map_borrow_entry_value_const(
+ trace_info, "intersection-range-ns");
+ if (!intersection_range) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
+ goto error;
+ }
+
+ intersection_begin = bt_value_map_borrow_entry_value_const(intersection_range,
+ "begin");
+ if (!intersection_begin) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
+ goto error;
+ }
+
+ intersection_end = bt_value_map_borrow_entry_value_const(intersection_range,
+ "end");
+ if (!intersection_end) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
+ goto error;
+ }
+
+ begin = bt_value_integer_get(intersection_begin);
+ end = bt_value_integer_get(intersection_end);
+
+ if (begin < 0 || end < 0 || end < begin) {
+ BT_LOGW("Invalid trace stream intersection values: "
+ "intersection-range-ns:begin=%" PRId64
+ ", intersection-range-ns:end=%" PRId64,
+ begin, end);
+ ret = -1;
+ goto error;
+ }
+
+ stream_infos = bt_value_map_borrow_entry_value_const(trace_info,
+ "streams");
+ if (!stream_infos || !bt_value_is_array(stream_infos)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
+ goto error;
+ }
+
+ stream_count = bt_value_array_get_size(stream_infos);
+ if (stream_count < 0) {
+ ret = -1;
+ goto error;
+ }
+
+ /*
+ * FIXME
+ *
+ * The first path of a stream's "paths" is currently used to
+ * associate streams/ports to a given trace intersection.
+ *
+ * This is a fragile hack as it relies on the port names
+ * being set to the various streams path.
+ *
+ * A stream name should be introduced as part of the trace-info
+ * query result.
+ */
+ for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
+ const char *stream_path;
+
+ port_id = g_new0(struct port_id, 1);
+ if (!port_id) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id structure.");
+ goto error;
+ }
+ port_id->instance_name = strdup(cfg_comp->instance_name->str);
+ if (!port_id->instance_name) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
+ goto error;
+ }
+
+ trace_range = g_new0(struct trace_range, 1);
+ if (!trace_range) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
+ goto error;
+ }
+ trace_range->intersection_range_begin_ns = begin;
+ trace_range->intersection_range_end_ns = end;
+
+ stream_info = bt_value_array_borrow_element_by_index_const(
+ stream_infos, stream_idx);
+ if (!stream_info || !bt_value_is_map(stream_info)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
+ goto error;
+ }
+
+ stream_paths = bt_value_map_borrow_entry_value_const(stream_info,
+ "paths");
+ if (!stream_paths || !bt_value_is_array(stream_paths)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
+ goto error;
+ }
+
+ stream_path_value =
+ bt_value_array_borrow_element_by_index_const(
+ stream_paths, 0);
+ if (!stream_path_value ||
+ !bt_value_is_string(stream_path_value)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
+ goto error;
+ }
+
+ stream_path = bt_value_string_get(stream_path_value);
+ port_id->port_name = strdup(stream_path);
+ if (!port_id->port_name) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
+ goto error;
+ }
+
+ BT_LOGD("Inserting stream intersection ");
+
+ g_hash_table_insert(ctx->intersections, port_id, trace_range);
+
+ port_id = NULL;
+ trace_range = NULL;
+ }
+ }
+