- int ret = 0;
- int64_t *event_id = NULL;
- struct bt_ctf_trace *trace = NULL;
- struct bt_ctf_stream_class *old_stream_class = NULL;
- struct bt_ctf_validation_output validation_output = { 0 };
- struct bt_ctf_field_type *packet_header_type = NULL;
- struct bt_ctf_field_type *packet_context_type = NULL;
- struct bt_ctf_field_type *event_header_type = NULL;
- struct bt_ctf_field_type *stream_event_ctx_type = NULL;
- struct bt_ctf_field_type *event_context_type = NULL;
- struct bt_ctf_field_type *event_payload_type = NULL;
- const enum bt_ctf_validation_flag validation_flags =
- BT_CTF_VALIDATION_FLAG_EVENT;
-
- BT_LOGD("Adding event class to stream class: "
- "stream-class-addr=%p, stream-class-name=\"%s\", "
- "stream-class-id=%" PRId64 ", event-class-addr=%p, "
- "event-class-name=\"%s\", event-class-id=%" PRId64,
- stream_class, bt_ctf_stream_class_get_name(stream_class),
- bt_ctf_stream_class_get_id(stream_class),
- event_class,
- bt_ctf_event_class_get_name(event_class),
- bt_ctf_event_class_get_id(event_class));
-
- if (!stream_class || !event_class) {
- BT_LOGW("Invalid parameter: stream class or event class is NULL: "
- "stream-class-addr=%p, event-class-addr=%p",
- stream_class, event_class);
- ret = -1;
- goto end;
- }
-
- trace = bt_ctf_stream_class_get_trace(stream_class);
- if (trace && trace->is_static) {
- ret = -1;
- goto end;
- }
-
- event_id = g_new(int64_t, 1);
- if (!event_id) {
- BT_LOGE_STR("Failed to allocate one int64_t.");
- ret = -1;
- goto end;
- }
-
- /* Check for duplicate event classes */
- struct search_query query = { .value = event_class, .found = 0 };
- g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
- &query);
- if (query.found) {
- BT_LOGW_STR("Another event class part of this stream class has the same ID.");
- ret = -1;
- goto end;
- }
-
- old_stream_class = bt_ctf_event_class_get_stream_class(event_class);
- if (old_stream_class) {
- /* Event class is already associated to a stream class. */
- BT_LOGW("Event class is already part of another stream class: "
- "event-class-stream-class-addr=%p, "
- "event-class-stream-class-name=\"%s\", "
- "event-class-stream-class-id=%" PRId64,
- old_stream_class,
- bt_ctf_stream_class_get_name(old_stream_class),
- bt_ctf_stream_class_get_id(old_stream_class));
- ret = -1;
- goto end;
- }
-
- if (trace) {
- /*
- * If the stream class is associated with a trace, then
- * both those objects are frozen. Also, this event class
- * is about to be frozen.
- *
- * Therefore the event class must be validated here.
- * The trace and stream class should be valid at this
- * point.
- */
- assert(trace->valid);
- assert(stream_class->valid);
- 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);
- event_context_type =
- bt_ctf_event_class_get_context_type(event_class);
- event_payload_type =
- bt_ctf_event_class_get_payload_type(event_class);
- ret = bt_ctf_validate_class_types(
- trace->environment, packet_header_type,
- packet_context_type, event_header_type,
- stream_event_ctx_type, event_context_type,
- event_payload_type, trace->valid,
- stream_class->valid, event_class->valid,
- &validation_output, validation_flags);
- BT_PUT(packet_header_type);
- BT_PUT(packet_context_type);
- BT_PUT(event_header_type);
- BT_PUT(stream_event_ctx_type);
- BT_PUT(event_context_type);
- BT_PUT(event_payload_type);
-
- if (ret) {
- /*
- * This means something went wrong during the
- * validation process, not that the objects are
- * invalid.
- */
- BT_LOGE("Failed to validate event class: ret=%d", ret);
- goto end;
- }
-
- if ((validation_output.valid_flags & validation_flags) !=
- validation_flags) {
- /* Invalid event class */
- BT_LOGW("Invalid trace, stream class, or event class: "
- "valid-flags=0x%x",
- validation_output.valid_flags);
- ret = -1;
- goto end;
- }
- }
-
- /* Only set an event ID if none was explicitly set before */
- *event_id = bt_ctf_event_class_get_id(event_class);
- if (*event_id < 0) {
- BT_LOGV("Event class has no ID: automatically setting it: "
- "id=%" PRId64, stream_class->next_event_id);
-
- if (bt_ctf_event_class_set_id(event_class,
- stream_class->next_event_id)) {
- BT_LOGE("Cannot set event class's ID: id=%" PRId64,
- stream_class->next_event_id);
- ret = -1;
- goto end;
- }
- stream_class->next_event_id++;
- *event_id = stream_class->next_event_id;
- }
-
- ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);