+ if (!trace) {
+ BT_LOGW_STR("Invalid parameter: trace is NULL.");
+ ret = -1;
+ goto end;
+ }
+
+ if (trace->is_static) {
+ BT_LOGW("Invalid parameter: trace is static: "
+ "addr=%p, name=\"%s\"",
+ trace, bt_ctf_trace_get_name(trace));
+ ret = -1;
+ goto end;
+ }
+
+ if (!bt_ctf_clock_class_is_valid(clock_class)) {
+ BT_LOGW("Invalid parameter: clock class is invalid: "
+ "trace-addr=%p, trace-name=\"%s\", "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ trace, bt_ctf_trace_get_name(trace),
+ clock_class, bt_ctf_clock_class_get_name(clock_class));
+ ret = -1;
+ goto end;
+ }
+
+ /* Check for duplicate clock classes */
+ if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
+ BT_LOGW("Invalid parameter: clock class already exists in trace: "
+ "trace-addr=%p, trace-name=\"%s\", "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ trace, bt_ctf_trace_get_name(trace),
+ clock_class, bt_ctf_clock_class_get_name(clock_class));
+ ret = -1;
+ goto end;
+ }
+
+ bt_get(clock_class);
+ g_ptr_array_add(trace->clocks, clock_class);
+
+ if (trace->frozen) {
+ BT_LOGV_STR("Freezing added clock class because trace is frozen.");
+ bt_ctf_clock_class_freeze(clock_class);
+ }
+
+ BT_LOGV("Added clock class to trace: "
+ "trace-addr=%p, trace-name=\"%s\", "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ trace, bt_ctf_trace_get_name(trace),
+ clock_class, bt_ctf_clock_class_get_name(clock_class));
+
+end:
+ return ret;
+}
+
+int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
+{
+ int64_t ret = (int64_t) -1;
+
+ if (!trace) {
+ BT_LOGW_STR("Invalid parameter: trace is NULL.");
+ goto end;
+ }
+
+ ret = trace->clocks->len;
+end:
+ return ret;
+}
+
+struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
+ struct bt_ctf_trace *trace, uint64_t index)
+{
+ struct bt_ctf_clock_class *clock_class = NULL;
+
+ if (!trace) {
+ BT_LOGW_STR("Invalid parameter: trace is NULL.");
+ goto end;
+ }
+
+ if (index >= trace->clocks->len) {
+ BT_LOGW("Invalid parameter: index is out of bounds: "
+ "addr=%p, name=\"%s\", "
+ "index=%" PRIu64 ", count=%u",
+ trace, bt_ctf_trace_get_name(trace),
+ index, trace->clocks->len);
+ goto end;
+ }
+
+ clock_class = g_ptr_array_index(trace->clocks, index);
+ bt_get(clock_class);
+end:
+ return clock_class;
+}
+
+static
+bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace,
+ struct bt_ctf_field_type *packet_header_type)
+{
+ int ret;
+ bool is_valid = true;
+ struct bt_ctf_field_type *field_type = NULL;
+
+ if (!packet_header_type) {
+ /*
+ * No packet header field type: trace must have only
+ * one stream. At this point the stream class being
+ * added is not part of the trace yet, so we validate
+ * that the trace contains no stream classes yet.
+ */
+ if (trace->stream_classes->len >= 1) {
+ BT_LOGW_STR("Invalid packet header field type: "
+ "packet header field type does not exist but there's more than one stream class in the trace.");
+ goto invalid;
+ }
+
+ /* No packet header field type: valid at this point */
+ goto end;
+ }
+
+ /* Packet header field type, if it exists, must be a structure */
+ if (!bt_ctf_field_type_is_structure(packet_header_type)) {
+ BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
+ "ft-addr=%p, ft-id=%s",
+ packet_header_type,
+ bt_ctf_field_type_id_string(packet_header_type->id));
+ goto invalid;
+ }
+
+ /*
+ * If there's a `magic` field, it must be a 32-bit unsigned
+ * integer field type. Also it must be the first field of the
+ * packet header field type.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_header_type, "magic");
+ if (field_type) {
+ const char *field_name;
+
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
+ "magic-ft-addr=%p, magic-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
+ "magic-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_get_size(field_type) != 32) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
+ "magic-ft-addr=%p, magic-ft-size=%u",
+ field_type,
+ bt_ctf_field_type_integer_get_size(field_type));
+ goto invalid;
+ }
+
+ ret = bt_ctf_field_type_structure_get_field_by_index(
+ packet_header_type, &field_name, NULL, 0);
+ assert(ret == 0);
+
+ if (strcmp(field_name, "magic") != 0) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
+ "magic-ft-addr=%p, first-field-name=\"%s\"",
+ field_type, field_name);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `uuid` field, it must be an array field type of
+ * length 16 with an 8-bit unsigned integer element field type.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_header_type, "uuid");
+ if (field_type) {
+ struct bt_ctf_field_type *elem_ft;
+
+ if (!bt_ctf_field_type_is_array(field_type)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
+ "uuid-ft-addr=%p, uuid-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_array_get_length(field_type) != 16) {
+ BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
+ "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
+ field_type,
+ bt_ctf_field_type_array_get_length(field_type));
+ goto invalid;
+ }
+
+ elem_ft = bt_ctf_field_type_array_get_element_type(field_type);
+ assert(elem_ft);
+
+ if (!bt_ctf_field_type_is_integer(elem_ft)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
+ "elem-ft-addr=%p, elem-ft-id=%s",
+ elem_ft,
+ bt_ctf_field_type_id_string(elem_ft->id));
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(elem_ft)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
+ "elem-ft-addr=%p", elem_ft);
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_get_size(elem_ft) != 8) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
+ "elem-ft-addr=%p, elem-ft-size=%u",
+ elem_ft,
+ bt_ctf_field_type_integer_get_size(elem_ft));
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ bt_put(elem_ft);
+ BT_PUT(field_type);
+ }
+
+ /*
+ * The `stream_id` field must exist if there's more than one
+ * stream classes in the trace.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_header_type, "stream_id");
+
+ if (!field_type && trace->stream_classes->len >= 1) {
+ BT_LOGW_STR("Invalid packet header field type: "
+ "`stream_id` field does not exist but there's more than one stream class in the trace.");
+ goto invalid;
+ }
+
+ /*
+ * If there's a `stream_id` field, it must be an unsigned
+ * integer field type.
+ */
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
+ "stream-id-ft-addr=%p, stream-id-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
+ "stream-id-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ goto end;
+
+invalid:
+ is_valid = false;
+
+end:
+ bt_put(field_type);
+ return is_valid;
+}
+
+static
+bool packet_context_field_type_is_valid(struct bt_ctf_trace *trace,
+ struct bt_ctf_stream_class *stream_class,
+ struct bt_ctf_field_type *packet_context_type)
+{
+ bool is_valid = true;
+ struct bt_ctf_field_type *field_type = NULL;
+
+ if (!packet_context_type) {
+ /* No packet context field type: valid at this point */
+ goto end;
+ }
+
+ /* Packet context field type, if it exists, must be a structure */
+ if (!bt_ctf_field_type_is_structure(packet_context_type)) {
+ BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
+ "ft-addr=%p, ft-id=%s",
+ packet_context_type,
+ bt_ctf_field_type_id_string(packet_context_type->id));
+ goto invalid;
+ }
+
+ /*
+ * If there's a `packet_size` field, it must be an unsigned
+ * integer field type.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_context_type, "packet_size");
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
+ "packet-size-ft-addr=%p, packet-size-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
+ "packet-size-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `content_size` field, it must be an unsigned
+ * integer field type.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_context_type, "content_size");
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
+ "content-size-ft-addr=%p, content-size-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
+ "content-size-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `events_discarded` field, it must be an unsigned
+ * integer field type.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_context_type, "events_discarded");
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
+ "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
+ "events-discarded-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `timestamp_begin` field, it must be an unsigned
+ * integer field type. Also, if the trace is not a CTF writer's
+ * trace, then we cannot automatically set the mapped clock
+ * class of this field, so it must have a mapped clock class.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_context_type, "timestamp_begin");
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
+ "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
+ "timestamp-begin-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ if (!trace->is_created_by_writer) {
+ struct bt_ctf_clock_class *clock_class =
+ bt_ctf_field_type_integer_get_mapped_clock_class(
+ field_type);
+
+ bt_put(clock_class);
+ if (!clock_class) {
+ BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
+ "timestamp-begin-ft-addr=%p", field_type);
+ goto invalid;
+ }
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `timestamp_end` field, it must be an unsigned
+ * integer field type. Also, if the trace is not a CTF writer's
+ * trace, then we cannot automatically set the mapped clock
+ * class of this field, so it must have a mapped clock class.
+ */
+ field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+ packet_context_type, "timestamp_end");
+ if (field_type) {
+ if (!bt_ctf_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
+ "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
+ field_type,
+ bt_ctf_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_ctf_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
+ "timestamp-end-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ if (!trace->is_created_by_writer) {
+ struct bt_ctf_clock_class *clock_class =
+ bt_ctf_field_type_integer_get_mapped_clock_class(
+ field_type);
+
+ bt_put(clock_class);
+ if (!clock_class) {
+ BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
+ "timestamp-end-ft-addr=%p", field_type);
+ goto invalid;
+ }
+ }
+
+ BT_PUT(field_type);