debug-info: sync the copied trace on static change
[babeltrace.git] / plugins / libctfcopytrace / ctfcopytrace.c
index d2d422100ee7228f7b96e6513d208d0ec7dd1388..b8e281355b05abc44d798b8c337586719f28127e 100644 (file)
@@ -39,6 +39,7 @@
 #include "ctfcopytrace.h"
 #include "clock-fields.h"
 
+BT_HIDDEN
 struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
                struct bt_ctf_clock_class *clock_class)
 {
@@ -65,18 +66,14 @@ struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
        }
 
        description = bt_ctf_clock_class_get_description(clock_class);
-       if (!description) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
-       }
-
-       int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
-                       description);
-       if (int_ret != 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
+       if (description) {
+               int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
+                               description);
+               if (int_ret != 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto end_destroy;
+               }
        }
 
        u64_ret = bt_ctf_clock_class_get_frequency(clock_class);
@@ -134,7 +131,7 @@ struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
                goto end_destroy;
        }
 
-       int_ret = bt_ctf_clock_class_get_is_absolute(clock_class);
+       int_ret = bt_ctf_clock_class_is_absolute(clock_class);
        if (int_ret == -1) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
@@ -156,6 +153,7 @@ end:
        return writer_clock_class;
 }
 
+BT_HIDDEN
 enum bt_component_status ctf_copy_clock_classes(FILE *err,
                struct bt_ctf_trace *writer_trace,
                struct bt_ctf_stream_class *writer_stream_class,
@@ -169,7 +167,7 @@ enum bt_component_status ctf_copy_clock_classes(FILE *err,
        for (i = 0; i < clock_class_count; i++) {
                struct bt_ctf_clock_class *writer_clock_class;
                struct bt_ctf_clock_class *clock_class =
-                       bt_ctf_trace_get_clock_class(trace, i);
+                       bt_ctf_trace_get_clock_class_by_index(trace, i);
 
                if (!clock_class) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
@@ -207,12 +205,14 @@ end:
        return ret;
 }
 
+BT_HIDDEN
 struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                struct bt_ctf_event_class *event_class)
 {
        struct bt_ctf_event_class *writer_event_class = NULL;
+       struct bt_ctf_field_type *context, *payload_type;
        const char *name;
-       int count, i;
+       int count, i, ret;
 
        name = bt_ctf_event_class_get_name(event_class);
        if (!name) {
@@ -234,67 +234,70 @@ struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                struct bt_value *attr_value;
                int ret;
 
-               attr_name = bt_ctf_event_class_get_attribute_name(event_class, i);
+               attr_name = bt_ctf_event_class_get_attribute_name_by_index(
+                       event_class, i);
                if (!attr_name) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       BT_PUT(writer_event_class);
-                       goto end;
+                       goto error;
                }
-               attr_value = bt_ctf_event_class_get_attribute_value(event_class, i);
+               attr_value = bt_ctf_event_class_get_attribute_value_by_index(
+                       event_class, i);
                if (!attr_value) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       BT_PUT(writer_event_class);
-                       goto end;
+                       goto error;
                }
 
                ret = bt_ctf_event_class_set_attribute(writer_event_class,
                                attr_name, attr_value);
-               bt_put(attr_value);
+               BT_PUT(attr_value);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       BT_PUT(writer_event_class);
-                       goto end;
+                       goto error;
                }
        }
 
-       count = bt_ctf_event_class_get_field_count(event_class);
-       for (i = 0; i < count; i++) {
-               const char *field_name;
-               struct bt_ctf_field_type *field_type;
-               int ret;
-
-               ret = bt_ctf_event_class_get_field(event_class, &field_name,
-                               &field_type, i);
+       payload_type = bt_ctf_event_class_get_payload_type(event_class);
+       if (payload_type) {
+               ret = bt_ctf_event_class_set_payload_type(writer_event_class,
+                               payload_type);
                if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
-                       BT_PUT(writer_event_class);
-                       goto end;
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
                }
+               BT_PUT(payload_type);
+       }
 
-               ret = bt_ctf_event_class_add_field(writer_event_class, field_type,
-                               field_name);
+       context = bt_ctf_event_class_get_context_type(event_class);
+       if (context) {
+               ret = bt_ctf_event_class_set_context_type(
+                               writer_event_class, context);
+               BT_PUT(context);
                if (ret < 0) {
-                       fprintf(err, "[error] Cannot add field %s\n", field_name);
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
-                       bt_put(field_type);
-                       BT_PUT(writer_event_class);
-                       goto end;
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto error;
                }
-               bt_put(field_type);
        }
 
+       goto end;
+
+error:
+       BT_PUT(writer_event_class);
 end:
        return writer_event_class;
 }
 
+BT_HIDDEN
 enum bt_component_status ctf_copy_event_classes(FILE *err,
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_stream_class *writer_stream_class)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL;
        int count, i;
 
        count = bt_ctf_stream_class_get_event_class_count(stream_class);
@@ -305,35 +308,38 @@ enum bt_component_status ctf_copy_event_classes(FILE *err,
        }
 
        for (i = 0; i < count; i++) {
-               struct bt_ctf_event_class *event_class, *writer_event_class;
-               struct bt_ctf_field_type *context;
                int int_ret;
 
-               event_class = bt_ctf_stream_class_get_event_class(
+               event_class = bt_ctf_stream_class_get_event_class_by_index(
                                stream_class, i);
                if (!event_class) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       bt_put(event_class);
-                       goto end;
+                       goto error;
+               }
+               if (i < bt_ctf_stream_class_get_event_class_count(writer_stream_class)) {
+                       writer_event_class = bt_ctf_stream_class_get_event_class_by_index(
+                                       writer_stream_class, i);
+                       if (writer_event_class) {
+                               /*
+                                * If the writer_event_class already exists,
+                                * just skip it. It can be used to resync the
+                                * event_classes after a trace has become
+                                * static.
+                                */
+                               BT_PUT(writer_event_class);
+                               BT_PUT(event_class);
+                               continue;
+                       }
                }
+
                writer_event_class = ctf_copy_event_class(err, event_class);
                if (!writer_event_class) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       bt_put(event_class);
-                       goto end;
-               }
-
-               context = bt_ctf_event_class_get_context_type(event_class);
-               ret = bt_ctf_event_class_set_context_type(writer_event_class, context);
-               bt_put(context);
-               if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                                       __LINE__);
-                       goto end;
+                       goto error;
                }
 
                int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class,
@@ -343,24 +349,29 @@ enum bt_component_status ctf_copy_event_classes(FILE *err,
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       bt_put(event_class);
-                       goto end;
+                       goto error;
                }
-               bt_put(event_class);
-               bt_put(writer_event_class);
+               BT_PUT(writer_event_class);
+               BT_PUT(event_class);
        }
 
+       goto end;
+
+error:
+       bt_put(event_class);
+       bt_put(writer_event_class);
 end:
        return ret;
 }
 
+BT_HIDDEN
 struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_trace *writer_trace,
                bool override_ts64)
 {
-       struct bt_ctf_field_type *type, *new_event_header_type;
-       struct bt_ctf_stream_class *writer_stream_class;
+       struct bt_ctf_field_type *type = NULL;
+       struct bt_ctf_stream_class *writer_stream_class = NULL;
        int ret_int;
        const char *name = bt_ctf_stream_class_get_name(stream_class);
 
@@ -384,12 +395,12 @@ struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
 
        ret_int = bt_ctf_stream_class_set_packet_context_type(
                        writer_stream_class, type);
-       bt_put(type);
        if (ret_int < 0) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
                goto error;
        }
+       BT_PUT(type);
 
        type = bt_ctf_stream_class_get_event_header_type(stream_class);
        if (!type) {
@@ -399,9 +410,10 @@ struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
        }
 
        if (override_ts64) {
+               struct bt_ctf_field_type *new_event_header_type;
+
                new_event_header_type = override_header_type(err, type,
                                writer_trace);
-               bt_put(type);
                if (!new_event_header_type) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
@@ -409,7 +421,7 @@ struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
                }
                ret_int = bt_ctf_stream_class_set_event_header_type(
                                writer_stream_class, new_event_header_type);
-               bt_put(new_event_header_type);
+               BT_PUT(new_event_header_type);
                if (ret_int < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
@@ -418,42 +430,44 @@ struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
        } else {
                ret_int = bt_ctf_stream_class_set_event_header_type(
                                writer_stream_class, type);
-               bt_put(type);
                if (ret_int < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
                        goto error;
                }
        }
+       BT_PUT(type);
 
        type = bt_ctf_stream_class_get_event_context_type(stream_class);
        if (type) {
                ret_int = bt_ctf_stream_class_set_event_context_type(
                                writer_stream_class, type);
-               bt_put(type);
                if (ret_int < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
                        goto error;
                }
        }
+       BT_PUT(type);
 
        goto end;
 
 error:
        BT_PUT(writer_stream_class);
 end:
+       bt_put(type);
        return writer_stream_class;
 }
 
+BT_HIDDEN
 enum bt_component_status ctf_copy_packet_context_field(FILE *err,
                struct bt_ctf_field *field, const char *field_name,
                struct bt_ctf_field *writer_packet_context,
                struct bt_ctf_field_type *writer_packet_context_type)
 {
        enum bt_component_status ret;
-       struct bt_ctf_field *writer_field;
-       struct bt_ctf_field_type *field_type;
+       struct bt_ctf_field *writer_field = NULL;
+       struct bt_ctf_field_type *field_type = NULL;
        int int_ret;
        uint64_t value;
 
@@ -467,21 +481,15 @@ enum bt_component_status ctf_copy_packet_context_field(FILE *err,
        /*
         * Only support for integers for now.
         */
-       if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
+       if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
                fprintf(err, "[error] Unsupported packet context field type\n");
-               bt_put(field_type);
                ret = BT_COMPONENT_STATUS_ERROR;
-               goto end;
+               goto error;
        }
-       bt_put(field_type);
+       BT_PUT(field_type);
 
-       /*
-        * TODO: handle the special case of the first/last packet that might
-        * be trimmed. In these cases, the timestamp_begin/end need to be
-        * explicitely set to the first/last event timestamps.
-        */
-       writer_field = bt_ctf_field_structure_get_field(writer_packet_context,
-                       field_name);
+       writer_field = bt_ctf_field_structure_get_field_by_name(
+               writer_packet_context, field_name);
        if (!writer_field) {
                ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
@@ -489,14 +497,13 @@ enum bt_component_status ctf_copy_packet_context_field(FILE *err,
                goto end;
        }
 
-
        int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value);
        if (int_ret < 0) {
                fprintf(err, "[error] Wrong packet_context field type\n");
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
                ret = BT_COMPONENT_STATUS_ERROR;
-               goto end_put_writer_field;
+               goto end;
        }
 
        int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
@@ -504,31 +511,35 @@ enum bt_component_status ctf_copy_packet_context_field(FILE *err,
                ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_writer_field;
+               goto end;
        }
 
        ret = BT_COMPONENT_STATUS_OK;
 
-end_put_writer_field:
-       bt_put(writer_field);
+       goto end;
+
+error:
+       bt_put(field_type);
 end:
+       bt_put(writer_field);
        return ret;
 }
 
+BT_HIDDEN
 struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
                struct bt_ctf_packet *packet,
                struct bt_ctf_stream *writer_stream)
 {
        enum bt_component_status ret;
-       struct bt_ctf_field *packet_context, *writer_packet_context = NULL;
-       struct bt_ctf_field_type *struct_type, *writer_packet_context_type;
-       struct bt_ctf_stream_class *writer_stream_class;
+       struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
+       struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL;
+       struct bt_ctf_stream_class *writer_stream_class = NULL;
+       struct bt_ctf_field *field = NULL;
+       struct bt_ctf_field_type *field_type = NULL;
        int nr_fields, i;
 
        packet_context = bt_ctf_packet_get_context(packet);
        if (!packet_context) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
                goto end;
        }
 
@@ -536,99 +547,88 @@ struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
        if (!writer_stream_class) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_packet_context;
+               goto error;
        }
 
        writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type(
                        writer_stream_class);
+       BT_PUT(writer_stream_class);
        if (!writer_packet_context_type) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_writer_stream_class;
+               goto error;
        }
 
        struct_type = bt_ctf_field_get_type(packet_context);
        if (!struct_type) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_writer_packet_context_type;
+               goto error;
        }
 
        writer_packet_context = bt_ctf_field_create(writer_packet_context_type);
        if (!writer_packet_context) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_struct_type;
+               goto error;
        }
 
        nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
        for (i = 0; i < nr_fields; i++) {
-               struct bt_ctf_field *field;
-               struct bt_ctf_field_type *field_type;
                const char *field_name;
 
                field = bt_ctf_field_structure_get_field_by_index(
                                packet_context, i);
                if (!field) {
-                       BT_PUT(writer_packet_context);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end_put_struct_type;
+                       goto error;
                }
-               if (bt_ctf_field_type_structure_get_field(struct_type,
+               if (bt_ctf_field_type_structure_get_field_by_index(struct_type,
                                        &field_name, &field_type, i) < 0) {
-                       bt_put(field);
-                       BT_PUT(writer_packet_context);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end_put_struct_type;
-               }
-               if (!strncmp(field_name, "content_size", strlen("content_size")) ||
-                               !strncmp(field_name, "packet_size",
-                                       strlen("packet_size"))) {
-                       bt_put(field_type);
-                       bt_put(field);
-                       continue;
+                       goto error;
                }
 
-               if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
+               if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
                        fprintf(err, "[error] Unexpected packet context field type\n");
-                       bt_put(field);
-                       BT_PUT(writer_packet_context);
-                       goto end_put_struct_type;
+                       goto error;
                }
 
                ret = ctf_copy_packet_context_field(err, field, field_name,
                                writer_packet_context, writer_packet_context_type);
-               bt_put(field_type);
-               bt_put(field);
+               BT_PUT(field_type);
+               BT_PUT(field);
                if (ret != BT_COMPONENT_STATUS_OK) {
-                       BT_PUT(writer_packet_context);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end_put_struct_type;
+                       goto error;
                }
        }
 
-end_put_struct_type:
+       goto end;
+
+error:
+       BT_PUT(writer_packet_context);
+end:
+       bt_put(field);
+       bt_put(field_type);
        bt_put(struct_type);
-end_put_writer_packet_context_type:
        bt_put(writer_packet_context_type);
-end_put_writer_stream_class:
        bt_put(writer_stream_class);
-end_put_packet_context:
        bt_put(packet_context);
-end:
        return writer_packet_context;
 }
 
+BT_HIDDEN
 int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
                struct bt_ctf_event_class *writer_event_class,
                struct bt_ctf_event *writer_event,
                struct bt_ctf_field *event_header)
 {
-       struct bt_ctf_clock_class *clock_class, *writer_clock_class;
-       struct bt_ctf_clock_value *clock_value, *writer_clock_value;
+       struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL;
+       struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL;
 
        int ret;
        struct bt_ctf_field *writer_event_header = NULL;
@@ -642,7 +642,7 @@ int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
        }
 
        clock_value = bt_ctf_event_get_clock_value(event, clock_class);
-       bt_put(clock_class);
+       BT_PUT(clock_class);
        if (!clock_value) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
@@ -650,7 +650,7 @@ int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
        }
 
        ret = bt_ctf_clock_value_get_value(clock_value, &value);
-       bt_put(clock_value);
+       BT_PUT(clock_value);
        if (ret) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
@@ -665,15 +665,15 @@ int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
        }
 
        writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
-       bt_put(writer_clock_class);
+       BT_PUT(writer_clock_class);
        if (!writer_clock_value) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end;
+               goto error;
        }
 
        ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
-       bt_put(writer_clock_value);
+       BT_PUT(writer_clock_value);
        if (ret) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
@@ -684,12 +684,11 @@ int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
        if (!writer_event_header) {
                fprintf(err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               ret = -1;
                goto end;
        }
 
        ret = bt_ctf_event_set_header(writer_event, writer_event_header);
-       bt_put(writer_event_header);
+       BT_PUT(writer_event_header);
        if (ret < 0) {
                fprintf(err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
@@ -701,18 +700,18 @@ int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
        goto end;
 
 error:
-       BT_PUT(writer_event_header);
        ret = -1;
 end:
        return ret;
 }
 
+BT_HIDDEN
 struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
                struct bt_ctf_event_class *writer_event_class,
                bool override_ts64)
 {
-       struct bt_ctf_event *writer_event;
-       struct bt_ctf_field *field, *copy_field;
+       struct bt_ctf_event *writer_event = NULL;
+       struct bt_ctf_field *field = NULL, *copy_field = NULL;
        int ret;
 
        writer_event = bt_ctf_event_create(writer_event_class);
@@ -724,10 +723,9 @@ struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
 
        field = bt_ctf_event_get_header(event);
        if (!field) {
-               BT_PUT(writer_event);
                fprintf(err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        /*
@@ -737,147 +735,176 @@ struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
        if (override_ts64) {
                copy_field = bt_ctf_event_get_header(writer_event);
                if (!copy_field) {
-                       BT_PUT(writer_event);
-                       bt_put(field);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end;
+                       goto error;
                }
 
                ret = copy_override_field(err, event, writer_event, field,
                                copy_field);
-               bt_put(field);
                if (ret) {
-                       BT_PUT(writer_event);
-                       BT_PUT(copy_field);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end;
+                       goto error;
                }
+               BT_PUT(copy_field);
        } else {
                ret = ctf_copy_event_header(err, event, writer_event_class,
                                writer_event, field);
                if (ret) {
-                       BT_PUT(writer_event);
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
-                       goto end;
+                       goto error;
                }
        }
+       BT_PUT(field);
 
        /* Optional field, so it can fail silently. */
        field = bt_ctf_event_get_stream_event_context(event);
        copy_field = bt_ctf_field_copy(field);
-       bt_put(field);
        if (copy_field) {
                ret = bt_ctf_event_set_stream_event_context(writer_event,
                                copy_field);
-               bt_put(copy_field);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        goto error;
                }
        }
+       BT_PUT(field);
+       BT_PUT(copy_field);
 
        /* Optional field, so it can fail silently. */
        field = bt_ctf_event_get_event_context(event);
        copy_field = bt_ctf_field_copy(field);
-       bt_put(field);
        if (copy_field) {
                ret = bt_ctf_event_set_event_context(writer_event, copy_field);
-               bt_put(copy_field);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        goto error;
                }
        }
+       BT_PUT(field);
+       BT_PUT(copy_field);
 
-       field = bt_ctf_event_get_payload_field(event);
+       field = bt_ctf_event_get_event_payload(event);
        if (!field) {
-               BT_PUT(writer_event);
                fprintf(err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
        copy_field = bt_ctf_field_copy(field);
-       bt_put(field);
        if (copy_field) {
-               ret = bt_ctf_event_set_payload_field(writer_event, copy_field);
-               bt_put(copy_field);
+               ret = bt_ctf_event_set_event_payload(writer_event, copy_field);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        goto error;
                }
        }
+       BT_PUT(field);
+       BT_PUT(copy_field);
+
        goto end;
 
 error:
        BT_PUT(writer_event);
 end:
+       bt_put(field);
+       bt_put(copy_field);
        return writer_event;
 }
 
+BT_HIDDEN
 enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
                struct bt_ctf_trace *writer_trace)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        int field_count, i, int_ret;
-       struct bt_ctf_field_type *header_type;
+       struct bt_ctf_field_type *header_type = NULL;
+       enum bt_ctf_byte_order order;
+       const char *trace_name;
 
        field_count = bt_ctf_trace_get_environment_field_count(trace);
        for (i = 0; i < field_count; i++) {
                int ret_int;
                const char *name;
-               struct bt_value *value;
+               struct bt_value *value = NULL;
 
-               name = bt_ctf_trace_get_environment_field_name(trace, i);
+               name = bt_ctf_trace_get_environment_field_name_by_index(
+                       trace, i);
                if (!name) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       goto end_put_writer_trace;
+                       goto end;
                }
-               value = bt_ctf_trace_get_environment_field_value(trace, i);
+               value = bt_ctf_trace_get_environment_field_value_by_index(
+                       trace, i);
                if (!value) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       goto end_put_writer_trace;
+                       goto end;
                }
 
                ret_int = bt_ctf_trace_set_environment_field(writer_trace,
                                name, value);
-               bt_put(value);
+               BT_PUT(value);
                if (ret_int < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                        __LINE__);
                        fprintf(err, "[error] Unable to set environment field %s\n",
                                        name);
                        ret = BT_COMPONENT_STATUS_ERROR;
-                       goto end_put_writer_trace;
+                       goto end;
                }
        }
 
-       header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
-       if (!header_type) {
+       order = bt_ctf_trace_get_native_byte_order(trace);
+       if (order == BT_CTF_BYTE_ORDER_UNKNOWN) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
                ret = BT_COMPONENT_STATUS_ERROR;
-               goto end_put_writer_trace;
+               goto end;
        }
 
-       int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
-       if (int_ret < 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto end_put_header_type;
+       /*
+        * Only explicitly set the writer trace's native byte order if
+        * the original trace has a specific one. Otherwise leave what
+        * the CTF writer object chooses, which is the machine's native
+        * byte order.
+        */
+       if (order != BT_CTF_BYTE_ORDER_NONE) {
+               ret = bt_ctf_trace_set_native_byte_order(writer_trace, order);
+               if (ret) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
        }
 
-end_put_header_type:
-       bt_put(header_type);
-end_put_writer_trace:
+       header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
+       if (header_type) {
+               int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
+               BT_PUT(header_type);
+               if (int_ret < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+       }
+
+       trace_name = bt_ctf_trace_get_name(trace);
+       if (trace_name) {
+               int_ret = bt_ctf_trace_set_name(writer_trace, trace_name);
+               if (int_ret < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+       }
+
+end:
        return ret;
 }
-
This page took 0.0349699999999999 seconds and 4 git commands to generate.