From 416937235b48b55387c6074adc6a0dd59a5c76f4 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 10 Dec 2018 08:03:58 -0500 Subject: [PATCH] bt_trace_class_create(): accept mandatory self component This patch makes bt_trace_class_create() accept a mandatory self component. The self component parameter is not used now, but it could be in the future to control allocation (object pooling) and for bookkeeping, as well as to have a system where a new trace class within the graph can lead to notifying other components about it so they can prepare associated data. The `ctf` plugin is changed so that it does not require a trace class to create packet indexes. This is required because the `trace-info` query requires packet indexes to exist, but there's no self component in this context to create a trace class. Signed-off-by: Philippe Proulx --- include/babeltrace/trace-ir/trace-class.h | 7 +- lib/trace-ir/trace-class.c | 3 +- plugins/ctf/common/metadata/ast.h | 1 + plugins/ctf/common/metadata/decoder.c | 3 +- plugins/ctf/common/metadata/decoder.h | 1 + .../ctf/common/metadata/visitor-generate-ir.c | 53 ++++++---- plugins/ctf/common/msg-iter/msg-iter.c | 66 ++++++++----- plugins/ctf/common/msg-iter/msg-iter.h | 18 ---- plugins/ctf/fs-src/data-stream-file.c | 51 +--------- plugins/ctf/fs-src/data-stream-file.h | 6 -- plugins/ctf/fs-src/fs.c | 47 +++++---- plugins/ctf/fs-src/fs.h | 3 +- plugins/ctf/fs-src/metadata.c | 11 ++- plugins/ctf/fs-src/metadata.h | 3 +- plugins/ctf/fs-src/query.c | 2 +- plugins/text/dmesg/dmesg.c | 6 +- tests/lib/test_bt_message_iterator.c | 97 ++++++++++--------- tests/lib/test_trace_ir_ref.c | 47 ++++++++- 18 files changed, 234 insertions(+), 191 deletions(-) diff --git a/include/babeltrace/trace-ir/trace-class.h b/include/babeltrace/trace-ir/trace-class.h index 5cbabc03..b253eb2e 100644 --- a/include/babeltrace/trace-ir/trace-class.h +++ b/include/babeltrace/trace-ir/trace-class.h @@ -27,7 +27,10 @@ * http://www.efficios.com/ctf */ -/* For bt_bool, bt_uuid, bt_trace_class, bt_stream_class, bt_field_class */ +/* + * For bt_bool, bt_uuid, bt_trace_class, bt_stream_class, + * bt_field_class, bt_self_component + */ #include /* For bt_trace_class_status */ @@ -39,7 +42,7 @@ extern "C" { #endif -extern bt_trace_class *bt_trace_class_create(void); +extern bt_trace_class *bt_trace_class_create(bt_self_component *self_comp); extern void bt_trace_class_set_assigns_automatic_stream_class_id( bt_trace_class *trace_class, bt_bool value); diff --git a/lib/trace-ir/trace-class.c b/lib/trace-ir/trace-class.c index b072bad8..78a7161b 100644 --- a/lib/trace-ir/trace-class.c +++ b/lib/trace-ir/trace-class.c @@ -96,11 +96,12 @@ void free_packet_header_field(struct bt_field_wrapper *field_wrapper, bt_field_wrapper_destroy(field_wrapper); } -struct bt_trace_class *bt_trace_class_create(void) +struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) { struct bt_trace_class *tc = NULL; int ret; + BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); BT_LOGD_STR("Creating default trace class object."); tc = g_new0(struct bt_trace_class, 1); if (!tc) { diff --git a/plugins/ctf/common/metadata/ast.h b/plugins/ctf/common/metadata/ast.h index ef40333b..b7a097c8 100644 --- a/plugins/ctf/common/metadata/ast.h +++ b/plugins/ctf/common/metadata/ast.h @@ -318,6 +318,7 @@ const char *node_type(struct ctf_node *node); BT_HIDDEN struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( + bt_self_component_source *self_comp, const struct ctf_metadata_decoder_config *config); void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor); diff --git a/plugins/ctf/common/metadata/decoder.c b/plugins/ctf/common/metadata/decoder.c index af1e1eda..b17caf1f 100644 --- a/plugins/ctf/common/metadata/decoder.c +++ b/plugins/ctf/common/metadata/decoder.c @@ -348,6 +348,7 @@ int ctf_metadata_decoder_packetized_file_stream_to_buf( BT_HIDDEN struct ctf_metadata_decoder *ctf_metadata_decoder_create( + bt_self_component_source *self_comp, const struct ctf_metadata_decoder_config *config) { struct ctf_metadata_decoder *mdec = @@ -372,7 +373,7 @@ struct ctf_metadata_decoder *ctf_metadata_decoder_create( } mdec->config = *config; - mdec->visitor = ctf_visitor_generate_ir_create(config); + mdec->visitor = ctf_visitor_generate_ir_create(self_comp, config); if (!mdec->visitor) { BT_LOGE("Failed to create a CTF IR metadata AST visitor: " "mdec-addr=%p", mdec); diff --git a/plugins/ctf/common/metadata/decoder.h b/plugins/ctf/common/metadata/decoder.h index 1edb548c..fac2f1d7 100644 --- a/plugins/ctf/common/metadata/decoder.h +++ b/plugins/ctf/common/metadata/decoder.h @@ -45,6 +45,7 @@ struct ctf_metadata_decoder_config { */ BT_HIDDEN struct ctf_metadata_decoder *ctf_metadata_decoder_create( + bt_self_component_source *self_comp, const struct ctf_metadata_decoder_config *config); /* diff --git a/plugins/ctf/common/metadata/visitor-generate-ir.c b/plugins/ctf/common/metadata/visitor-generate-ir.c index 71957e20..e009b443 100644 --- a/plugins/ctf/common/metadata/visitor-generate-ir.c +++ b/plugins/ctf/common/metadata/visitor-generate-ir.c @@ -570,7 +570,7 @@ end: * @returns New visitor context, or NULL on error */ static -struct ctx *ctx_create( +struct ctx *ctx_create(bt_self_component_source *self_comp, const struct ctf_metadata_decoder_config *decoder_config) { struct ctx *ctx = NULL; @@ -583,10 +583,13 @@ struct ctx *ctx_create( goto error; } - ctx->trace_class = bt_trace_class_create(); - if (!ctx->trace_class) { - BT_LOGE_STR("Cannot create empty trace class."); - goto error; + if (self_comp) { + ctx->trace_class = bt_trace_class_create( + bt_self_component_source_as_self_component(self_comp)); + if (!ctx->trace_class) { + BT_LOGE_STR("Cannot create empty trace class."); + goto error; + } } ctx->ctf_tc = ctf_trace_class_create(); @@ -4802,12 +4805,13 @@ end: BT_HIDDEN struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( + bt_self_component_source *self_comp, const struct ctf_metadata_decoder_config *decoder_config) { struct ctx *ctx = NULL; /* Create visitor's context */ - ctx = ctx_create(decoder_config); + ctx = ctx_create(self_comp, decoder_config); if (!ctx) { BT_LOGE_STR("Cannot create visitor's context."); goto error; @@ -4836,8 +4840,11 @@ bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class( struct ctx *ctx = (void *) visitor; BT_ASSERT(ctx); - BT_ASSERT(ctx->trace_class); - bt_trace_class_get_ref(ctx->trace_class); + + if (ctx->trace_class) { + bt_trace_class_get_ref(ctx->trace_class); + } + return ctx->trace_class; } @@ -5042,11 +5049,19 @@ int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, goto end; } - /* Update "in IR" for field classes */ - ret = ctf_trace_class_update_in_ir(ctx->ctf_tc); - if (ret) { - ret = -EINVAL; - goto end; + if (ctx->trace_class) { + /* + * Update "in IR" for field classes. + * + * If we have no IR trace class, then we'll have no way + * to create IR fields anyway, so we leave all the + * `in_ir` members false. + */ + ret = ctf_trace_class_update_in_ir(ctx->ctf_tc); + if (ret) { + ret = -EINVAL; + goto end; + } } /* Update saved value indexes */ @@ -5063,11 +5078,13 @@ int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, goto end; } - /* Copy new CTF metadata -> new IR metadata */ - ret = ctf_trace_class_translate(ctx->trace_class, ctx->ctf_tc); - if (ret) { - ret = -EINVAL; - goto end; + if (ctx->trace_class) { + /* Copy new CTF metadata -> new IR metadata */ + ret = ctf_trace_class_translate(ctx->trace_class, ctx->ctf_tc); + if (ret) { + ret = -EINVAL; + goto end; + } } end: diff --git a/plugins/ctf/common/msg-iter/msg-iter.c b/plugins/ctf/common/msg-iter/msg-iter.c index 8bed0378..7bd9192b 100644 --- a/plugins/ctf/common/msg-iter/msg-iter.c +++ b/plugins/ctf/common/msg-iter/msg-iter.c @@ -120,6 +120,9 @@ struct bt_msg_iter { bool done_filling_string; /* Trace and classes */ + /* True to set IR fields */ + bool set_ir_fields; + struct { struct ctf_trace_class *tc; struct ctf_stream_class *sc; @@ -909,7 +912,7 @@ enum bt_msg_iter_status read_packet_context_begin_state( * packet is created from a stream, and this API must be * able to return the packet header and context fields * without creating a stream - * (bt_msg_iter_borrow_packet_header_context_fields()). + * (read_packet_header_context_fields()). */ notit->packet_context_field = bt_packet_context_field_create( @@ -1946,7 +1949,11 @@ enum bt_bfcr_status bfcr_floating_point_cb(double value, "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " "fc-type=%d, fc-in-ir=%d, value=%f", notit, notit->bfcr, fc, fc->type, fc->in_ir, value); - BT_ASSERT(fc->in_ir); + + if (unlikely(!fc->in_ir)) { + goto end; + } + field = borrow_next_field(notit); BT_ASSERT(field); BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); @@ -1954,6 +1961,8 @@ enum bt_bfcr_status bfcr_floating_point_cb(double value, BT_FIELD_CLASS_TYPE_REAL); bt_field_real_set_value(field, value); stack_top(notit->stack)->index++; + +end: return status; } @@ -1970,7 +1979,10 @@ enum bt_bfcr_status bfcr_string_begin_cb( "fc-type=%d, fc-in-ir=%d", notit, notit->bfcr, fc, fc->type, fc->in_ir); - BT_ASSERT(fc->in_ir); + if (unlikely(!fc->in_ir)) { + goto end; + } + field = borrow_next_field(notit); BT_ASSERT(field); BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); @@ -1985,6 +1997,8 @@ enum bt_bfcr_status bfcr_string_begin_cb( * subsequent call to bfcr_string_end_cb(). */ stack_push(notit->stack, field); + +end: return BT_BFCR_STATUS_OK; } @@ -2002,7 +2016,11 @@ enum bt_bfcr_status bfcr_string_cb(const char *value, "fc-type=%d, fc-in-ir=%d, string-length=%zu", notit, notit->bfcr, fc, fc->type, fc->in_ir, len); - BT_ASSERT(fc->in_ir); + + if (unlikely(!fc->in_ir)) { + goto end; + } + field = stack_top(notit->stack)->base; BT_ASSERT(field); @@ -2030,13 +2048,18 @@ enum bt_bfcr_status bfcr_string_end_cb( "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " "fc-type=%d, fc-in-ir=%d", notit, notit->bfcr, fc, fc->type, fc->in_ir); - BT_ASSERT(fc->in_ir); + + if (unlikely(!fc->in_ir)) { + goto end; + } /* Pop string field */ stack_pop(notit->stack); /* Go to next field */ stack_top(notit->stack)->index++; + +end: return BT_BFCR_STATUS_OK; } @@ -2633,11 +2656,9 @@ end: return status; } -BT_HIDDEN -enum bt_msg_iter_status bt_msg_iter_borrow_packet_header_context_fields( - struct bt_msg_iter *notit, - bt_field **packet_header_field, - bt_field **packet_context_field) +static +enum bt_msg_iter_status read_packet_header_context_fields( + struct bt_msg_iter *notit) { int ret; enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; @@ -2646,7 +2667,7 @@ enum bt_msg_iter_status bt_msg_iter_borrow_packet_header_context_fields( if (notit->state == STATE_EMIT_MSG_NEW_PACKET) { /* We're already there */ - goto set_fields; + goto end; } while (true) { @@ -2672,7 +2693,7 @@ enum bt_msg_iter_status bt_msg_iter_borrow_packet_header_context_fields( * Packet header and context fields are * potentially decoded (or they don't exist). */ - goto set_fields; + goto end; case STATE_INIT: case STATE_EMIT_MSG_NEW_STREAM: case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: @@ -2694,22 +2715,13 @@ enum bt_msg_iter_status bt_msg_iter_borrow_packet_header_context_fields( } } -set_fields: +end: ret = set_current_packet_content_sizes(notit); if (ret) { status = BT_MSG_ITER_STATUS_ERROR; goto end; } - if (packet_header_field) { - *packet_header_field = notit->dscopes.trace_packet_header; - } - - if (packet_context_field) { - *packet_context_field = notit->dscopes.stream_packet_context; - } - -end: return status; } @@ -2788,8 +2800,14 @@ enum bt_msg_iter_status bt_msg_iter_get_packet_properties( struct bt_msg_iter *notit, struct bt_msg_iter_packet_properties *props) { + enum bt_msg_iter_status status; + BT_ASSERT(notit); BT_ASSERT(props); + status = read_packet_header_context_fields(notit); + if (status != BT_MSG_ITER_STATUS_OK) { + goto end; + } props->exp_packet_total_size = (uint64_t) notit->cur_exp_packet_total_size; @@ -2802,5 +2820,7 @@ enum bt_msg_iter_status bt_msg_iter_get_packet_properties( props->snapshots.packets = notit->snapshots.packets; props->snapshots.beginning_clock = notit->snapshots.beginning_clock; props->snapshots.end_clock = notit->snapshots.end_clock; - return BT_MSG_ITER_STATUS_OK; + +end: + return status; } diff --git a/plugins/ctf/common/msg-iter/msg-iter.h b/plugins/ctf/common/msg-iter/msg-iter.h index 89028bbb..269d6b28 100644 --- a/plugins/ctf/common/msg-iter/msg-iter.h +++ b/plugins/ctf/common/msg-iter/msg-iter.h @@ -290,24 +290,6 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message( bt_self_message_iterator *msg_iter, bt_message **message); -/** - * Returns the first packet header and context fields. This function - * never needs to call the `borrow_stream()` medium operation because - * it does not create packet or event objects. - * - * @param msg_iter CTF message iterator - * @param packet_header_field Packet header field (\c NULL if there's - * no packet header field) - * @param packet_context_field Packet context field (\c NULL if there's - * no packet context field) - * @returns One of #bt_msg_iter_status values - */ -BT_HIDDEN -enum bt_msg_iter_status bt_msg_iter_borrow_packet_header_context_fields( - struct bt_msg_iter *notit, - bt_field **packet_header_field, - bt_field **packet_context_field); - struct bt_msg_iter_packet_properties { uint64_t exp_packet_total_size; uint64_t exp_packet_content_size; diff --git a/plugins/ctf/fs-src/data-stream-file.c b/plugins/ctf/fs-src/data-stream-file.c index 3c4b0bfe..517bfec6 100644 --- a/plugins/ctf/fs-src/data-stream-file.c +++ b/plugins/ctf/fs-src/data-stream-file.c @@ -324,17 +324,12 @@ struct ctf_fs_ds_index *build_index_from_idx_file( BT_LOGD("Building index from .idx file of stream file %s", ds_file->file->path->str); - - ret = bt_msg_iter_borrow_packet_header_context_fields( - ds_file->msg_iter, NULL, NULL); + ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); if (ret) { - BT_LOGD_STR("Cannot borrow first packet's header and context " - "fields."); + BT_LOGD_STR("Cannot read first packet's header and context fields."); goto error; } - ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); - BT_ASSERT(ret == 0); sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id); BT_ASSERT(sc); @@ -550,8 +545,8 @@ struct ctf_fs_ds_index *build_index_from_stream_file( struct ctf_fs_ds_index_entry *entry; struct bt_msg_iter_packet_properties props; - iter_status = bt_msg_iter_borrow_packet_header_context_fields( - ds_file->msg_iter, NULL, NULL); + iter_status = bt_msg_iter_get_packet_properties( + ds_file->msg_iter, &props); if (iter_status != BT_MSG_ITER_STATUS_OK) { if (iter_status == BT_MSG_ITER_STATUS_EOF) { break; @@ -559,10 +554,6 @@ struct ctf_fs_ds_index *build_index_from_stream_file( goto error; } - ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, - &props); - BT_ASSERT(ret == 0); - current_packet_offset = bt_msg_iter_get_current_packet_offset( ds_file->msg_iter); @@ -739,40 +730,6 @@ bt_message_iterator_status ctf_fs_ds_file_next( return status; } -BT_HIDDEN -int ctf_fs_ds_file_borrow_packet_header_context_fields( - struct ctf_fs_ds_file *ds_file, - bt_field **packet_header_field, - bt_field **packet_context_field) -{ - enum bt_msg_iter_status msg_iter_status; - int ret = 0; - - BT_ASSERT(ds_file); - msg_iter_status = bt_msg_iter_borrow_packet_header_context_fields( - ds_file->msg_iter, packet_header_field, packet_context_field); - switch (msg_iter_status) { - case BT_MSG_ITER_STATUS_EOF: - case BT_MSG_ITER_STATUS_OK: - break; - case BT_MSG_ITER_STATUS_AGAIN: - abort(); - case BT_MSG_ITER_STATUS_INVAL: - case BT_MSG_ITER_STATUS_ERROR: - default: - goto error; - break; - } - - goto end; - -error: - ret = -1; - -end: - return ret; -} - BT_HIDDEN void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) { diff --git a/plugins/ctf/fs-src/data-stream-file.h b/plugins/ctf/fs-src/data-stream-file.h index 0ccba27a..05c1dc34 100644 --- a/plugins/ctf/fs-src/data-stream-file.h +++ b/plugins/ctf/fs-src/data-stream-file.h @@ -132,12 +132,6 @@ struct ctf_fs_ds_file *ctf_fs_ds_file_create( struct bt_msg_iter *msg_iter, bt_stream *stream, const char *path); -BT_HIDDEN -int ctf_fs_ds_file_borrow_packet_header_context_fields( - struct ctf_fs_ds_file *ds_file, - bt_field **packet_header_field, - bt_field **packet_context_field); - BT_HIDDEN void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *stream); diff --git a/plugins/ctf/fs-src/fs.c b/plugins/ctf/fs-src/fs.c index b6e2fb4f..daaae12e 100644 --- a/plugins/ctf/fs-src/fs.c +++ b/plugins/ctf/fs-src/fs.c @@ -640,16 +640,13 @@ int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, goto error; } - ret = ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file, - NULL, NULL); + ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); if (ret) { BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", path); goto error; } - ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); - BT_ASSERT(ret == 0); sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id); BT_ASSERT(sc); @@ -843,6 +840,10 @@ int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) ctf_fs_file_destroy(file); } + if (!ctf_fs_trace->trace) { + goto end; + } + /* * At this point, DS file groupes are created, but their * associated stream objects do not exist yet. This is because @@ -962,7 +963,8 @@ end: } BT_HIDDEN -struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, +struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component_source *self_comp, + const char *path, const char *name, struct ctf_fs_metadata_config *metadata_config) { struct ctf_fs_trace *ctf_fs_trace; @@ -995,20 +997,25 @@ struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, goto error; } - ret = ctf_fs_metadata_set_trace_class(ctf_fs_trace, metadata_config); + ret = ctf_fs_metadata_set_trace_class(self_comp, + ctf_fs_trace, metadata_config); if (ret) { goto error; } - ctf_fs_trace->trace = - bt_trace_create(ctf_fs_trace->metadata->trace_class); - if (!ctf_fs_trace->trace) { - goto error; + if (ctf_fs_trace->metadata->trace_class) { + ctf_fs_trace->trace = + bt_trace_create(ctf_fs_trace->metadata->trace_class); + if (!ctf_fs_trace->trace) { + goto error; + } } - ret = set_trace_name(ctf_fs_trace->trace, name); - if (ret) { - goto error; + if (ctf_fs_trace->trace) { + ret = set_trace_name(ctf_fs_trace->trace, name); + if (ret) { + goto error; + } } ret = create_ds_file_groups(ctf_fs_trace); @@ -1021,7 +1028,9 @@ struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, * trace needs. There won't be any more. Therefore it is safe to * make this trace static. */ - (void) bt_trace_make_static(ctf_fs_trace->trace); + if (ctf_fs_trace->trace) { + (void) bt_trace_make_static(ctf_fs_trace->trace); + } goto end; @@ -1213,7 +1222,8 @@ GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { } static -int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs, +int create_ctf_fs_traces(bt_self_component_source *self_comp, + struct ctf_fs_component *ctf_fs, const char *path_param) { struct ctf_fs_trace *ctf_fs_trace = NULL; @@ -1254,8 +1264,9 @@ int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs, GString *trace_path = tp_node->data; GString *trace_name = tn_node->data; - ctf_fs_trace = ctf_fs_trace_create(trace_path->str, - trace_name->str, &ctf_fs->metadata_config); + ctf_fs_trace = ctf_fs_trace_create(self_comp, + trace_path->str, trace_name->str, + &ctf_fs->metadata_config); if (!ctf_fs_trace) { BT_LOGE("Cannot create trace for `%s`.", trace_path->str); @@ -1366,7 +1377,7 @@ struct ctf_fs_component *ctf_fs_create( goto error; } - if (create_ctf_fs_traces(ctf_fs, path_param)) { + if (create_ctf_fs_traces(self_comp, ctf_fs, path_param)) { goto error; } diff --git a/plugins/ctf/fs-src/fs.h b/plugins/ctf/fs-src/fs.h index 3f36387f..56fe4891 100644 --- a/plugins/ctf/fs-src/fs.h +++ b/plugins/ctf/fs-src/fs.h @@ -170,7 +170,8 @@ bt_query_status ctf_fs_query( const bt_value **result); BT_HIDDEN -struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, +struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component_source *self_comp, + const char *path, const char *name, struct ctf_fs_metadata_config *config); BT_HIDDEN diff --git a/plugins/ctf/fs-src/metadata.c b/plugins/ctf/fs-src/metadata.c index 7d480d16..628033be 100644 --- a/plugins/ctf/fs-src/metadata.c +++ b/plugins/ctf/fs-src/metadata.c @@ -86,7 +86,10 @@ end: return file; } -int ctf_fs_metadata_set_trace_class(struct ctf_fs_trace *ctf_fs_trace, +BT_HIDDEN +int ctf_fs_metadata_set_trace_class( + bt_self_component_source *self_comp, + struct ctf_fs_trace *ctf_fs_trace, struct ctf_fs_metadata_config *config) { int ret = 0; @@ -103,7 +106,7 @@ int ctf_fs_metadata_set_trace_class(struct ctf_fs_trace *ctf_fs_trace, goto end; } - ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create( + ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(self_comp, config ? &decoder_config : NULL); if (!ctf_fs_trace->metadata->decoder) { BT_LOGE("Cannot create metadata decoder object"); @@ -121,7 +124,7 @@ int ctf_fs_metadata_set_trace_class(struct ctf_fs_trace *ctf_fs_trace, ctf_fs_trace->metadata->trace_class = ctf_metadata_decoder_get_ir_trace_class( ctf_fs_trace->metadata->decoder); - BT_ASSERT(ctf_fs_trace->metadata->trace_class); + BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); ctf_fs_trace->metadata->tc = ctf_metadata_decoder_borrow_ctf_trace_class( ctf_fs_trace->metadata->decoder); @@ -132,12 +135,14 @@ end: return ret; } +BT_HIDDEN int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata) { /* Nothing to initialize for the moment. */ return 0; } +BT_HIDDEN void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) { if (metadata->text) { diff --git a/plugins/ctf/fs-src/metadata.h b/plugins/ctf/fs-src/metadata.h index 6e936e40..4d0e2ecc 100644 --- a/plugins/ctf/fs-src/metadata.h +++ b/plugins/ctf/fs-src/metadata.h @@ -45,7 +45,8 @@ BT_HIDDEN void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata); BT_HIDDEN -int ctf_fs_metadata_set_trace_class(struct ctf_fs_trace *ctf_fs_trace, +int ctf_fs_metadata_set_trace_class(bt_self_component_source *self_comp, + struct ctf_fs_trace *ctf_fs_trace, struct ctf_fs_metadata_config *config); BT_HIDDEN diff --git a/plugins/ctf/fs-src/query.c b/plugins/ctf/fs-src/query.c index bccb78aa..8dcc492e 100644 --- a/plugins/ctf/fs-src/query.c +++ b/plugins/ctf/fs-src/query.c @@ -378,7 +378,7 @@ int populate_trace_info(const char *trace_path, const char *trace_name, goto end; } - trace = ctf_fs_trace_create(trace_path, trace_name, NULL); + trace = ctf_fs_trace_create(NULL, trace_path, trace_name, NULL); if (!trace) { BT_LOGE("Failed to create fs trace at \'%s\'", trace_path); ret = -1; diff --git a/plugins/text/dmesg/dmesg.c b/plugins/text/dmesg/dmesg.c index 00cac4a3..4a7876d7 100644 --- a/plugins/text/dmesg/dmesg.c +++ b/plugins/text/dmesg/dmesg.c @@ -68,6 +68,7 @@ struct dmesg_component { bt_bool no_timestamp; } params; + bt_self_component_source *self_comp; bt_trace_class *trace_class; bt_stream_class *stream_class; bt_event_class *event_class; @@ -120,7 +121,9 @@ int create_meta(struct dmesg_component *dmesg_comp, bool has_ts) bt_field_class *fc = NULL; int ret = 0; - dmesg_comp->trace_class = bt_trace_class_create(); + dmesg_comp->trace_class = bt_trace_class_create( + bt_self_component_source_as_self_component( + dmesg_comp->self_comp)); if (!dmesg_comp->trace_class) { BT_LOGE_STR("Cannot create an empty trace class object."); goto error; @@ -379,6 +382,7 @@ bt_self_component_status dmesg_init( goto error; } + dmesg_comp->self_comp = self_comp; dmesg_comp->params.path = g_string_new(NULL); if (!dmesg_comp->params.path) { BT_LOGE_STR("Failed to allocate a GString."); diff --git a/tests/lib/test_bt_message_iterator.c b/tests/lib/test_bt_message_iterator.c index 6738afdc..51a058eb 100644 --- a/tests/lib/test_bt_message_iterator.c +++ b/tests/lib/test_bt_message_iterator.c @@ -239,18 +239,15 @@ bool compare_test_events(const struct test_event *expected_events) } static -void init_static_data(void) +void init_static_data(bt_self_component_source *self_comp) { bt_trace_class *trace_class; bt_trace *trace; - /* Test events */ - test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); - BT_ASSERT(test_events); - /* Metadata, streams, and packets*/ - trace_class = bt_trace_class_create(); - BT_ASSERT(trace); + trace_class = bt_trace_class_create( + bt_self_component_source_as_self_component(self_comp)); + BT_ASSERT(trace_class); src_stream_class = bt_stream_class_create(trace_class); BT_ASSERT(src_stream_class); src_event_class = bt_event_class_create(src_stream_class); @@ -286,9 +283,6 @@ void init_static_data(void) static void fini_static_data(void) { - /* Test events */ - g_array_free(test_events, TRUE); - /* Metadata */ bt_stream_class_put_ref(src_stream_class); bt_event_class_put_ref(src_event_class); @@ -469,6 +463,7 @@ bt_self_component_status src_init( { int ret; + init_static_data(self_comp); ret = bt_self_component_source_add_output_port( self_comp, "out", NULL, NULL); BT_ASSERT(ret == 0); @@ -721,9 +716,10 @@ void create_source_sink(bt_graph *graph, } } +typedef void (*compare_func_t)(void); + static -void do_std_test(enum test test, const char *name, - const struct test_event *expected_test_events) +void do_std_test(enum test test, const char *name, compare_func_t compare_func) { const bt_component_source *src_comp; const bt_component_sink *sink_comp; @@ -760,18 +756,18 @@ void do_std_test(enum test test, const char *name, "graph finishes without any error"); /* Compare the resulting test events */ - if (expected_test_events) { - ok(compare_test_events(expected_test_events), - "the produced sequence of test events is the expected one"); + if (compare_func) { + compare_func(); } bt_component_source_put_ref(src_comp); bt_component_sink_put_ref(sink_comp); + fini_static_data(); BT_GRAPH_PUT_REF_AND_RESET(graph); } static -void test_no_auto_msgs(void) +void test_no_auto_msgs_compare(void) { const struct test_event expected_test_events[] = { { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, @@ -794,33 +790,20 @@ void test_no_auto_msgs(void) { .type = TEST_EV_TYPE_SENTINEL, }, }; + ok(compare_test_events(expected_test_events), + "the produced sequence of test events is the expected one"); +} + +static +void test_no_auto_msgs(void) +{ do_std_test(TEST_NO_AUTO_MSGS, "no automatic messages", - expected_test_events); + test_no_auto_msgs_compare); } static void test_output_port_message_iterator(void) { - const struct test_event expected_test_events[] = { - { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, - { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, - { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, - { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, - { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, - { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, }, - { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, - { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, }, - { .type = TEST_EV_TYPE_END, }, - { .type = TEST_EV_TYPE_SENTINEL, }, - }; const bt_component_source *src_comp; bt_port_output_message_iterator *msg_iter; bt_message_iterator_status iter_status = @@ -836,10 +819,9 @@ void test_output_port_message_iterator(void) create_source_sink(graph, &src_comp, NULL); /* Create message iterator on source's output port */ - upstream_port = bt_component_source_borrow_output_port_by_name_const(src_comp, - "out"); - msg_iter = bt_port_output_message_iterator_create(graph, - upstream_port); + upstream_port = bt_component_source_borrow_output_port_by_name_const( + src_comp, "out"); + msg_iter = bt_port_output_message_iterator_create(graph, upstream_port); ok(msg_iter, "bt_private_output_port_message_iterator_create() succeeds"); /* Consume the message iterator */ @@ -851,9 +833,33 @@ void test_output_port_message_iterator(void) "output port message iterator finishes without any error"); /* Compare the resulting test events */ - ok(compare_test_events(expected_test_events), - "the produced sequence of test events is the expected one"); + { + const struct test_event expected_test_events[] = { + { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, + { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, + { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, + { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, + { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, + { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, }, + { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, + { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, }, + { .type = TEST_EV_TYPE_END, }, + { .type = TEST_EV_TYPE_SENTINEL, }, + }; + + ok(compare_test_events(expected_test_events), + "the produced sequence of test events is the expected one"); + } + fini_static_data(); bt_component_source_put_ref(src_comp); BT_GRAPH_PUT_REF_AND_RESET(graph); bt_port_output_message_iterator_put_ref(msg_iter); @@ -868,9 +874,10 @@ int main(int argc, char **argv) } plan_tests(NR_TESTS); - init_static_data(); + test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); + BT_ASSERT(test_events); test_no_auto_msgs(); test_output_port_message_iterator(); - fini_static_data(); + g_array_free(test_events, TRUE); return exit_status(); } diff --git a/tests/lib/test_trace_ir_ref.c b/tests/lib/test_trace_ir_ref.c index 61c3de61..71b833b4 100644 --- a/tests/lib/test_trace_ir_ref.c +++ b/tests/lib/test_trace_ir_ref.c @@ -294,11 +294,12 @@ static void set_trace_packet_header(bt_trace_class *trace_class) bt_field_class_put_ref(packet_header_type); } -static bt_trace_class *create_tc1(void) +static bt_trace_class *create_tc1(bt_self_component_source *self_comp) { bt_trace_class *tc1 = NULL; - tc1 = bt_trace_class_create(); + tc1 = bt_trace_class_create( + bt_self_component_source_as_self_component(self_comp)); BT_ASSERT(tc1); set_trace_packet_header(tc1); create_sc1(tc1); @@ -322,7 +323,7 @@ static void init_weak_refs(bt_trace_class *tc, *ec3 = bt_stream_class_borrow_event_class_by_index(*sc2, 0); } -static void test_example_scenario(void) +static void test_example_scenario(bt_self_component_source *self_comp) { /* * Weak pointers to trace IR objects are to be used very @@ -338,7 +339,7 @@ static void test_example_scenario(void) struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 }; /* The only reference which exists at this point is on TC1. */ - tc1 = create_tc1(); + tc1 = create_tc1(self_comp); ok(tc1, "Initialize trace"); BT_ASSERT(tc1); init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1, @@ -465,6 +466,42 @@ static void test_example_scenario(void) BT_EVENT_CLASS_PUT_REF_AND_RESET(user_c.ec); } +static +bt_self_component_status src_init( + bt_self_component_source *self_comp, + const bt_value *params, void *init_method_data) +{ + test_example_scenario(self_comp); + return BT_SELF_COMPONENT_STATUS_OK; +} + +static +bt_self_message_iterator_status src_iter_next( + bt_self_message_iterator *self_iterator, + bt_message_array_const msgs, uint64_t capacity, + uint64_t *count) +{ + return BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; +} + +static void test_example_scenario_in_graph(void) +{ + bt_component_class_source *comp_cls; + bt_graph *graph; + int ret; + + comp_cls = bt_component_class_source_create("src", src_iter_next); + BT_ASSERT(comp_cls); + ret = bt_component_class_source_set_init_method(comp_cls, src_init); + BT_ASSERT(ret == 0); + graph = bt_graph_create(); + ret = bt_graph_add_source_component(graph, comp_cls, "src-comp", + NULL, NULL); + BT_ASSERT(ret == 0); + bt_graph_put_ref(graph); + bt_component_class_source_put_ref(comp_cls); +} + static void create_writer_user_full(struct writer_user *user) { gchar *trace_path; @@ -604,7 +641,7 @@ int main(int argc, char **argv) /* Initialize tap harness before any tests */ plan_tests(NR_TESTS); - test_example_scenario(); + test_example_scenario_in_graph(); test_put_order(); return exit_status(); -- 2.34.1