From 9877e1aa544b3433d0f982bfc82cd6db8cc927ae Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Wed, 14 Jun 2017 14:45:46 -0400 Subject: [PATCH] Fix copytrace: copy packet_context MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Jérémie Galarneau --- plugins/ctf/fs-sink/write.c | 29 ++--- plugins/libctfcopytrace/ctfcopytrace.c | 142 +++++-------------------- plugins/libctfcopytrace/ctfcopytrace.h | 19 +++- plugins/lttng-utils/copy.c | 15 +-- plugins/utils/trimmer/copy.c | 15 +-- 5 files changed, 53 insertions(+), 167 deletions(-) diff --git a/plugins/ctf/fs-sink/write.c b/plugins/ctf/fs-sink/write.c index 6413108b..51bd7208 100644 --- a/plugins/ctf/fs-sink/write.c +++ b/plugins/ctf/fs-sink/write.c @@ -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; } diff --git a/plugins/libctfcopytrace/ctfcopytrace.c b/plugins/libctfcopytrace/ctfcopytrace.c index b50783cc..f5cd071a 100644 --- a/plugins/libctfcopytrace/ctfcopytrace.c +++ b/plugins/libctfcopytrace/ctfcopytrace.c @@ -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 diff --git a/plugins/libctfcopytrace/ctfcopytrace.h b/plugins/libctfcopytrace/ctfcopytrace.h index 2ca5b789..785f8a47 100644 --- a/plugins/libctfcopytrace/ctfcopytrace.h +++ b/plugins/libctfcopytrace/ctfcopytrace.h @@ -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. diff --git a/plugins/lttng-utils/copy.c b/plugins/lttng-utils/copy.c index 2d48ac61..629d81be 100644 --- a/plugins/lttng-utils/copy.c +++ b/plugins/lttng-utils/copy.c @@ -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; diff --git a/plugins/utils/trimmer/copy.c b/plugins/utils/trimmer/copy.c index 9d0d8141..ca0970a0 100644 --- a/plugins/utils/trimmer/copy.c +++ b/plugins/utils/trimmer/copy.c @@ -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; } -- 2.34.1