+ /* Stream class already registered to the trace */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ /*
+ * If the stream class has a clock, register this clock to this
+ * trace if not already done.
+ */
+ if (stream_class->clock) {
+ const char *clock_name =
+ bt_ctf_clock_get_name(stream_class->clock);
+ struct bt_ctf_clock *trace_clock;
+
+ assert(clock_name);
+ trace_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
+ bt_put(trace_clock);
+ if (trace_clock) {
+ if (trace_clock != stream_class->clock) {
+ /*
+ * Error: two different clocks in the
+ * trace would share the same name.
+ */
+ ret = -1;
+ goto end;
+ }
+ } else {
+ clock_to_add_to_trace = bt_get(stream_class->clock);
+ }
+ }
+
+ /*
+ * We're about to freeze both the trace and the stream class.
+ * Also, each event class contained in this stream class are
+ * already frozen.
+ *
+ * This trace, this stream class, and all its event classes
+ * should be valid at this point.
+ *
+ * Validate trace and stream class first, then each event
+ * class of this stream class can be validated individually.
+ */
+ packet_header_type =
+ bt_ctf_trace_get_packet_header_type(trace);
+ packet_context_type =
+ bt_ctf_stream_class_get_packet_context_type(stream_class);
+ event_header_type =
+ bt_ctf_stream_class_get_event_header_type(stream_class);
+ stream_event_ctx_type =
+ bt_ctf_stream_class_get_event_context_type(stream_class);
+ ret = bt_ctf_validate_class_types(trace->environment,
+ packet_header_type, packet_context_type, event_header_type,
+ stream_event_ctx_type, NULL, NULL, trace->valid,
+ stream_class->valid, 1, &trace_sc_validation_output,
+ trace_sc_validation_flags);
+ BT_PUT(packet_header_type);
+ BT_PUT(packet_context_type);
+ BT_PUT(event_header_type);
+ BT_PUT(stream_event_ctx_type);
+
+ if (ret) {
+ /*
+ * This means something went wrong during the validation
+ * process, not that the objects are invalid.
+ */
+ goto end;
+ }
+
+ if ((trace_sc_validation_output.valid_flags &
+ trace_sc_validation_flags) !=
+ trace_sc_validation_flags) {
+ /* Invalid trace/stream class */
+ ret = -1;
+ goto end;
+ }
+
+ if (event_class_count > 0) {
+ ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
+ event_class_count);
+ if (!ec_validation_outputs) {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ /* Validate each event class individually */
+ for (i = 0; i < event_class_count; i++) {
+ struct bt_ctf_event_class *event_class =
+ bt_ctf_stream_class_get_event_class(stream_class, i);
+ struct bt_ctf_field_type *event_context_type = NULL;
+ struct bt_ctf_field_type *event_payload_type = NULL;
+
+ event_context_type =
+ bt_ctf_event_class_get_context_type(event_class);
+ event_payload_type =
+ bt_ctf_event_class_get_payload_type(event_class);
+
+ /*
+ * It is important to use the field types returned by
+ * the previous trace and stream class validation here
+ * because copies could have been made.
+ */
+ ret = bt_ctf_validate_class_types(trace->environment,
+ trace_sc_validation_output.packet_header_type,
+ trace_sc_validation_output.packet_context_type,
+ trace_sc_validation_output.event_header_type,
+ trace_sc_validation_output.stream_event_ctx_type,
+ event_context_type, event_payload_type,
+ 1, 1, event_class->valid, &ec_validation_outputs[i],
+ ec_validation_flags);
+ BT_PUT(event_context_type);
+ BT_PUT(event_payload_type);
+ BT_PUT(event_class);
+
+ if (ret) {
+ goto end;
+ }
+
+ if ((ec_validation_outputs[i].valid_flags &
+ ec_validation_flags) != ec_validation_flags) {
+ /* Invalid event class */