ir: stream.c: do not truncate file at each packet flush
[babeltrace.git] / lib / ctf-ir / packet.c
index 6236e00bdb5e7cbc4837feed6f76b4d88151532c..9ec77deac18fbec382496ee2600dcbe4d4b358ee 100644 (file)
@@ -24,6 +24,9 @@
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "PACKET"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/ctf-ir/fields-internal.h>
 #include <babeltrace/ctf-ir/packet.h>
 #include <babeltrace/ctf-ir/packet-internal.h>
@@ -35,6 +38,7 @@
 #include <babeltrace/ctf-ir/trace-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/ref.h>
+#include <inttypes.h>
 
 struct bt_ctf_stream *bt_ctf_packet_get_stream(struct bt_ctf_packet *packet)
 {
@@ -56,25 +60,45 @@ int bt_ctf_packet_set_header(struct bt_ctf_packet *packet,
        struct bt_ctf_field_type *header_field_type = NULL;
        struct bt_ctf_field_type *expected_header_field_type = NULL;
 
-       if (!packet || packet->frozen) {
+       if (!packet) {
+               BT_LOGW_STR("Invalid parameter: packet is NULL.");
                ret = -1;
                goto end;
        }
 
-       if (!header) {
-               goto skip_validation;
+       if (packet->frozen) {
+               BT_LOGW("Invalid parameter: packet is frozen: addr=%p",
+                       packet);
+               ret = -1;
+               goto end;
        }
 
        stream_class = bt_ctf_stream_get_class(packet->stream);
        assert(stream_class);
        trace = bt_ctf_stream_class_get_trace(stream_class);
        assert(trace);
+       expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace);
+
+       if (!header) {
+               if (expected_header_field_type) {
+                       BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
+                               "packet-addr=%p, packet-header-ft-addr=%p",
+                               packet, expected_header_field_type);
+                       ret = -1;
+                       goto end;
+               }
+
+               goto skip_validation;
+       }
+
        header_field_type = bt_ctf_field_get_type(header);
        assert(header_field_type);
-       expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace);
 
        if (bt_ctf_field_type_compare(header_field_type,
                        expected_header_field_type)) {
+               BT_LOGW("Invalid parameter: packet header's field type is different from the trace's packet header field type: "
+                       "packet-addr=%p, packet-header-addr=%p",
+                       packet, header);
                ret = -1;
                goto end;
        }
@@ -82,6 +106,8 @@ int bt_ctf_packet_set_header(struct bt_ctf_packet *packet,
 skip_validation:
        bt_put(packet->header);
        packet->header = bt_get(header);
+       BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p",
+               packet, header);
 
 end:
        BT_PUT(trace);
@@ -106,24 +132,44 @@ int bt_ctf_packet_set_context(struct bt_ctf_packet *packet,
        struct bt_ctf_field_type *context_field_type = NULL;
        struct bt_ctf_field_type *expected_context_field_type = NULL;
 
-       if (!packet || packet->frozen) {
+       if (!packet) {
+               BT_LOGW_STR("Invalid parameter: packet is NULL.");
                ret = -1;
                goto end;
        }
 
-       if (!context) {
-               goto skip_validation;
+       if (packet->frozen) {
+               BT_LOGW("Invalid parameter: packet is frozen: addr=%p",
+                       packet);
+               ret = -1;
+               goto end;
        }
 
        stream_class = bt_ctf_stream_get_class(packet->stream);
        assert(stream_class);
-       context_field_type = bt_ctf_field_get_type(context);
-       assert(context_field_type);
        expected_context_field_type =
                bt_ctf_stream_class_get_packet_context_type(stream_class);
 
+       if (!context) {
+               if (expected_context_field_type) {
+                       BT_LOGW("Invalid parameter: setting no packet context but packet context field type is not NULL: "
+                               "packet-addr=%p, packet-context-ft-addr=%p",
+                               packet, expected_context_field_type);
+                       ret = -1;
+                       goto end;
+               }
+
+               goto skip_validation;
+       }
+
+       context_field_type = bt_ctf_field_get_type(context);
+       assert(context_field_type);
+
        if (bt_ctf_field_type_compare(context_field_type,
                        expected_context_field_type)) {
+               BT_LOGW("Invalid parameter: packet context's field type is different from the stream class's packet context field type: "
+                       "packet-addr=%p, packet-context-addr=%p",
+                       packet, context);
                ret = -1;
                goto end;
        }
@@ -131,23 +177,27 @@ int bt_ctf_packet_set_context(struct bt_ctf_packet *packet,
 skip_validation:
        bt_put(packet->context);
        packet->context = bt_get(context);
+       BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p",
+               packet, context);
 
 end:
        BT_PUT(stream_class);
        BT_PUT(context_field_type);
        BT_PUT(expected_context_field_type);
-
        return ret;
 }
 
 BT_HIDDEN
 void bt_ctf_packet_freeze(struct bt_ctf_packet *packet)
 {
-       if (!packet) {
+       if (!packet || packet->frozen) {
                return;
        }
 
+       BT_LOGD("Freezing packet: addr=%p", packet);
+       BT_LOGD_STR("Freezing packet's header field.");
        bt_ctf_field_freeze(packet->header);
+       BT_LOGD_STR("Freezing packet's context field.");
        bt_ctf_field_freeze(packet->context);
        packet->frozen = 1;
 }
@@ -158,8 +208,12 @@ void bt_ctf_packet_destroy(struct bt_object *obj)
        struct bt_ctf_packet *packet;
 
        packet = container_of(obj, struct bt_ctf_packet, base);
+       BT_LOGD("Destroying packet: addr=%p", packet);
+       BT_LOGD_STR("Putting packet's header field.");
        bt_put(packet->header);
+       BT_LOGD_STR("Putting packet's context field.");
        bt_put(packet->context);
+       BT_LOGD_STR("Putting packet's stream.");
        bt_put(packet->stream);
        g_free(packet);
 }
@@ -171,7 +225,21 @@ struct bt_ctf_packet *bt_ctf_packet_create(
        struct bt_ctf_stream_class *stream_class = NULL;
        struct bt_ctf_trace *trace = NULL;
 
-       if (!stream || stream->pos.fd >= 0) {
+       if (!stream) {
+               BT_LOGW_STR("Invalid parameter: stream is NULL.");
+               goto end;
+       }
+
+       BT_LOGD("Creating packet object: stream-addr=%p, "
+               "stream-name=\"%s\", stream-class-addr=%p, "
+               "stream-class-name=\"%s\", stream-class-id=%" PRId64,
+               stream, bt_ctf_stream_get_name(stream),
+               stream->stream_class,
+               bt_ctf_stream_class_get_name(stream->stream_class),
+               bt_ctf_stream_class_get_id(stream->stream_class));
+
+       if (stream->pos.fd >= 0) {
+               BT_LOGW_STR("Invalid parameter: stream is a CTF writer stream.");
                goto end;
        }
 
@@ -181,24 +249,38 @@ struct bt_ctf_packet *bt_ctf_packet_create(
        assert(trace);
        packet = g_new0(struct bt_ctf_packet, 1);
        if (!packet) {
+               BT_LOGE_STR("Failed to allocate one packet object.");
                goto end;
        }
 
        bt_object_init(packet, bt_ctf_packet_destroy);
        packet->stream = bt_get(stream);
-       packet->header = bt_ctf_field_create(trace->packet_header_type);
-       if (!packet->header && trace->packet_header_type) {
-               BT_PUT(packet);
-               goto end;
+
+       if (trace->packet_header_type) {
+               BT_LOGD("Creating initial packet header field: ft-addr=%p",
+                       trace->packet_header_type);
+               packet->header = bt_ctf_field_create(trace->packet_header_type);
+               if (!packet->header) {
+                       BT_LOGE_STR("Cannot create initial packet header field object.");
+                       BT_PUT(packet);
+                       goto end;
+               }
        }
 
-       packet->context = bt_ctf_field_create(
-               stream->stream_class->packet_context_type);
-       if (!packet->context && stream->stream_class->packet_context_type) {
-               BT_PUT(packet);
-               goto end;
+       if (stream->stream_class->packet_context_type) {
+               BT_LOGD("Creating initial packet context field: ft-addr=%p",
+                       stream->stream_class->packet_context_type);
+               packet->context = bt_ctf_field_create(
+                       stream->stream_class->packet_context_type);
+               if (!packet->context) {
+                       BT_LOGE_STR("Cannot create initial packet header field object.");
+                       BT_PUT(packet);
+                       goto end;
+               }
        }
 
+       BT_LOGD("Created packet object: addr=%p", packet);
+
 end:
        BT_PUT(trace);
        BT_PUT(stream_class);
This page took 0.028861 seconds and 4 git commands to generate.