Fix copytrace: copy packet_context
authorJulien Desfossez <jdesfossez@efficios.com>
Wed, 14 Jun 2017 18:45:46 +0000 (14:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 21 Jun 2017 18:28:14 +0000 (14:28 -0400)
Since we are not filtering the fields in the packet_context anymore, we
can now use the deep copy function bt_ctf_field_copy for the
packet_context.

Refactor the code to work with that and create a function to set the
packet_context on a packet or on a stream.

Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
plugins/ctf/fs-sink/write.c
plugins/libctfcopytrace/ctfcopytrace.c
plugins/libctfcopytrace/ctfcopytrace.h
plugins/lttng-utils/copy.c
plugins/utils/trimmer/copy.c

index 6413108b1e1569e97a232bd8de5ff1df3d763cfd..51bd7208a9cb768aa4db8cdd4b50f0f4f167c625 100644 (file)
@@ -648,8 +648,6 @@ enum bt_component_status writer_new_packet(
                struct bt_ctf_packet *packet)
 {
        struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
-       struct bt_ctf_field *writer_packet_context = NULL;
-       struct bt_ctf_field *packet_context = NULL;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        int int_ret;
 
@@ -668,27 +666,14 @@ enum bt_component_status writer_new_packet(
        }
        BT_PUT(stream);
 
-       packet_context = bt_ctf_packet_get_context(packet);
-       if (packet_context) {
-               writer_packet_context = ctf_copy_packet_context(
-                               writer_component->err, packet, writer_stream);
-               if (!writer_packet_context) {
-                       fprintf(writer_component->err, "[error] %s in %s:%d\n",
-                                       __func__, __FILE__, __LINE__);
-                       goto error;
-               }
-               BT_PUT(packet_context);
-
-               int_ret = bt_ctf_stream_set_packet_context(writer_stream,
-                               writer_packet_context);
-               if (int_ret < 0) {
-                       fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
+       int_ret = ctf_stream_copy_packet_context(
+                       writer_component->err, packet, writer_stream);
+       if (int_ret < 0) {
+               fprintf(writer_component->err, "[error] %s in %s:%d\n",
+                               __func__, __FILE__, __LINE__);
+               goto error;
        }
        BT_PUT(writer_stream);
-       BT_PUT(writer_packet_context);
 
        goto end;
 
@@ -696,8 +681,6 @@ error:
        ret = BT_COMPONENT_STATUS_ERROR;
 end:
        bt_put(writer_stream);
-       bt_put(packet_context);
-       bt_put(writer_packet_context);
        bt_put(stream);
        return ret;
 }
index b50783ccae81f4cff05871f637d84530de388320..f5cd071a142527b558032f9878cc79aa98088a28 100644 (file)
@@ -467,165 +467,77 @@ end:
 }
 
 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)
+int ctf_stream_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 *writer_field = NULL;
-       struct bt_ctf_field_type *field_type = NULL;
-       int int_ret;
-       uint64_t value;
-
-       field_type = bt_ctf_field_get_type(field);
-       if (!field_type) {
-               ret = BT_COMPONENT_STATUS_ERROR;
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end;
-       }
-       /*
-        * Only support for integers for now.
-        */
-       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");
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto error;
-       }
-       BT_PUT(field_type);
+       struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
+       int ret = 0;
 
-       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__,
-                               __LINE__);
+       packet_context = bt_ctf_packet_get_context(packet);
+       if (!packet_context) {
                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");
+       writer_packet_context = bt_ctf_field_copy(packet_context);
+       if (!writer_packet_context) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto end;
+               goto error;
        }
 
-       int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
-       if (int_ret < 0) {
-               ret = BT_COMPONENT_STATUS_ERROR;
+       ret = bt_ctf_stream_set_packet_context(writer_stream,
+                       writer_packet_context);
+       if (ret) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end;
+               goto error;
        }
 
-       ret = BT_COMPONENT_STATUS_OK;
-
        goto end;
 
 error:
-       bt_put(field_type);
+       ret = -1;
 end:
-       bt_put(writer_field);
+       bt_put(packet_context);
+       bt_put(writer_packet_context);
        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)
+int ctf_packet_copy_context(FILE *err, struct bt_ctf_packet *packet,
+               struct bt_ctf_stream *writer_stream,
+               struct bt_ctf_packet *writer_packet)
 {
-       enum bt_component_status ret;
        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;
+       int ret = 0;
 
        packet_context = bt_ctf_packet_get_context(packet);
        if (!packet_context) {
                goto end;
        }
 
-       writer_stream_class = bt_ctf_stream_get_class(writer_stream);
-       if (!writer_stream_class) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               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 error;
-       }
-
-       struct_type = bt_ctf_field_get_type(packet_context);
-       if (!struct_type) {
+       writer_packet_context = bt_ctf_field_copy(packet_context);
+       if (!writer_packet_context) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
                goto error;
        }
 
-       writer_packet_context = bt_ctf_field_create(writer_packet_context_type);
-       if (!writer_packet_context) {
+       ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
+       if (ret) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
                goto error;
        }
 
-       nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
-       for (i = 0; i < nr_fields; i++) {
-               const char *field_name;
-
-               field = bt_ctf_field_structure_get_field_by_index(
-                               packet_context, i);
-               if (!field) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
-               if (bt_ctf_field_type_structure_get_field_by_index(struct_type,
-                                       &field_name, &field_type, i) < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
-
-               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");
-                       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);
-               if (ret != BT_COMPONENT_STATUS_OK) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
-       }
-
        goto end;
 
 error:
-       BT_PUT(writer_packet_context);
+       ret = -1;
 end:
-       bt_put(field);
-       bt_put(field_type);
-       bt_put(struct_type);
-       bt_put(writer_packet_context_type);
-       bt_put(writer_stream_class);
+       bt_put(writer_packet_context);
        bt_put(packet_context);
-       return writer_packet_context;
+       return ret;
 }
 
 BT_HIDDEN
index 2ca5b789e810d954aa5ab2a8af8e5fd95692ce3f..785f8a4711b98459f7b0f248594c6bb81ea7933d 100644 (file)
@@ -104,16 +104,25 @@ enum bt_component_status ctf_copy_packet_context_field(FILE *err,
 
 /*
  * Copy all the field values of the packet context from the packet passed in
- * parameter and set it to the current packet in the writer stream.
+ * parameter and set it to the writer_stream.
  *
- * Returns BT_COMPONENT_STATUS_OK on success, and BT_COMPONENT_STATUS_ERROR on
- * error.
+ * Returns 0 on success or -1 on error.
  */
 BT_HIDDEN
-struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
-               struct bt_ctf_packet *packet,
+int ctf_stream_copy_packet_context(FILE *err, struct bt_ctf_packet *packet,
                struct bt_ctf_stream *writer_stream);
 
+/*
+ * Copy all the field values of the packet context from the packet passed in
+ * parameter and set it to the writer_packet.
+ *
+ * Returns 0 on success or -1 on error.
+ */
+BT_HIDDEN
+int ctf_packet_copy_context(FILE *err, struct bt_ctf_packet *packet,
+               struct bt_ctf_stream *writer_stream,
+               struct bt_ctf_packet *writer_packet);
+
 /*
  * Create and return a copy of the event passed in parameter. The caller has to
  * append it to the writer_stream.
index 2d48ac61b86e37dadbddf892ea5c4daba097ea4c..629d81be4f0d6156bf9b93766b5be77de34905c9 100644 (file)
@@ -1364,7 +1364,6 @@ struct bt_ctf_packet *debug_info_new_packet(
                struct bt_ctf_packet *packet)
 {
        struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
-       struct bt_ctf_field *writer_packet_context = NULL;
        struct bt_ctf_packet *writer_packet = NULL;
        struct bt_ctf_field *packet_context = NULL;
        struct debug_info_trace *di_trace;
@@ -1411,9 +1410,9 @@ struct bt_ctf_packet *debug_info_new_packet(
 
        packet_context = bt_ctf_packet_get_context(packet);
        if (packet_context) {
-               writer_packet_context = ctf_copy_packet_context(debug_it->err,
-                               packet, writer_stream);
-               if (!writer_packet_context) {
+               int_ret = ctf_packet_copy_context(debug_it->err,
+                               packet, writer_stream, writer_packet);
+               if (int_ret < 0) {
                        fprintf(debug_it->err, "[error] %s in %s:%d\n",
                                        __func__, __FILE__, __LINE__);
                        goto error;
@@ -1421,13 +1420,6 @@ struct bt_ctf_packet *debug_info_new_packet(
                BT_PUT(packet_context);
        }
 
-       int_ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
-       if (int_ret) {
-               fprintf(debug_it->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               goto error;
-       }
-
        bt_get(writer_packet);
        goto end;
 
@@ -1435,7 +1427,6 @@ error:
 
 end:
        bt_put(packet_context);
-       bt_put(writer_packet_context);
        bt_put(writer_stream);
        bt_put(stream);
        return writer_packet;
index 9d0d814171e5b6054d0a31098a241e4c854a8cc9..ca0970a06d029b3f1e6887569cc3f89a0629fbe7 100644 (file)
@@ -164,7 +164,6 @@ struct bt_ctf_packet *trimmer_new_packet(
                struct bt_ctf_packet *packet)
 {
        struct bt_ctf_stream *stream = NULL;
-       struct bt_ctf_field *writer_packet_context = NULL;
        struct bt_ctf_packet *writer_packet = NULL;
        int int_ret;
 
@@ -193,16 +192,9 @@ struct bt_ctf_packet *trimmer_new_packet(
        }
        bt_get(writer_packet);
 
-       writer_packet_context = ctf_copy_packet_context(trim_it->err, packet,
-                       stream);
-       if (!writer_packet_context) {
-               fprintf(trim_it->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               goto error;
-       }
-
-       int_ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
-       if (int_ret) {
+       int_ret = ctf_packet_copy_context(trim_it->err, packet,
+                       stream, writer_packet);
+       if (int_ret < 0) {
                fprintf(trim_it->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
                goto error;
@@ -213,7 +205,6 @@ struct bt_ctf_packet *trimmer_new_packet(
 error:
        BT_PUT(writer_packet);
 end:
-       bt_put(writer_packet_context);
        bt_put(stream);
        return writer_packet;
 }
This page took 0.031271 seconds and 4 git commands to generate.