X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=plugins%2Fctf%2Fcommon%2Fmetadata%2Fdecoder.c;h=38733ae38b444a0fe04cc9d33424021ef33e14dd;hb=40f4ba76dd6f9508ca51b6220eaed57632281a07;hp=8837da7769e168019a7dd22dbb2ad1db136ccbae;hpb=d6e2075358c89e188ba026a4cd7be5c938575212;p=babeltrace.git diff --git a/plugins/ctf/common/metadata/decoder.c b/plugins/ctf/common/metadata/decoder.c index 8837da77..38733ae3 100644 --- a/plugins/ctf/common/metadata/decoder.c +++ b/plugins/ctf/common/metadata/decoder.c @@ -20,10 +20,10 @@ #include #include #include -#include +#include #include #include -#include +#include #include #include @@ -33,7 +33,7 @@ #define TSDL_MAGIC 0x75d11d57 -BT_HIDDEN +extern int yydebug; struct ctf_metadata_decoder { @@ -41,6 +41,7 @@ struct ctf_metadata_decoder { uint8_t uuid[16]; bool is_uuid_set; int bo; + struct ctf_metadata_decoder_config config; }; struct packet_header { @@ -102,6 +103,11 @@ int decode_packet(struct ctf_metadata_decoder *mdec, FILE *in_fp, FILE *out_fp, int ret = 0; const long offset = ftell(in_fp); + if (offset < 0) { + BT_LOGE_ERRNO("Failed to get current metadata file position", + "."); + goto error; + } BT_LOGV("Decoding metadata packet: mdec-addr=%p, offset=%ld", mdec, offset); readlen = fread(&header, sizeof(header), 1, in_fp); @@ -206,20 +212,28 @@ int decode_packet(struct ctf_metadata_decoder *mdec, FILE *in_fp, FILE *out_fp, toread = header.content_size / CHAR_BIT - sizeof(header); for (;;) { - readlen = fread(buf, sizeof(uint8_t), - MIN(sizeof(buf) - 1, toread), in_fp); + size_t loop_read; + + loop_read = MIN(sizeof(buf) - 1, toread); + readlen = fread(buf, sizeof(uint8_t), loop_read, in_fp); if (ferror(in_fp)) { BT_LOGE("Cannot read metadata packet buffer: " - "offset=%ld, read-size=%u", - ftell(in_fp), (unsigned int) readlen); + "offset=%ld, read-size=%zu", + ftell(in_fp), loop_read); + goto error; + } + if (readlen > loop_read) { + BT_LOGE("fread returned more byte than expected: " + "read-size-asked=%zu, read-size-returned=%zu", + loop_read, readlen); goto error; } writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp); if (writelen < readlen || ferror(out_fp)) { BT_LOGE("Cannot write decoded metadata text to buffer: " - "read-offset=%ld, write-size=%u", - ftell(in_fp), (unsigned int) readlen); + "read-offset=%ld, write-size=%zu", + ftell(in_fp), readlen); goto error; } @@ -290,6 +304,13 @@ int ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( /* Close stream, which also flushes the buffer */ ret = bt_close_memstream(buf, &size, out_fp); + /* + * See fclose(3). Further access to out_fp after both success + * and error, even through another bt_close_memstream(), results + * in undefined behavior. Nullify out_fp to ensure we don't + * fclose it twice on error. + */ + out_fp = NULL; if (ret < 0) { BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p", strerror(errno), mdec); @@ -327,22 +348,33 @@ int ctf_metadata_decoder_packetized_file_stream_to_buf( BT_HIDDEN struct ctf_metadata_decoder *ctf_metadata_decoder_create( - int64_t clock_class_offset_ns, const char *name) + const struct ctf_metadata_decoder_config *config, + const char *name) { struct ctf_metadata_decoder *mdec = g_new0(struct ctf_metadata_decoder, 1); + struct ctf_metadata_decoder_config default_config = { + .clock_class_offset_s = 0, + .clock_class_offset_ns = 0, + }; + + if (!config) { + config = &default_config; + } BT_LOGD("Creating CTF metadata decoder: " + "clock-class-offset-s=%" PRId64 ", " "clock-class-offset-ns=%" PRId64 ", name=\"%s\"", - clock_class_offset_ns, name); + config->clock_class_offset_s, config->clock_class_offset_ns, + name); if (!mdec) { BT_LOGE_STR("Failed to allocate one CTF metadata decoder."); goto end; } - mdec->visitor = ctf_visitor_generate_ir_create(clock_class_offset_ns, - name); + mdec->config = *config; + mdec->visitor = ctf_visitor_generate_ir_create(config, name); if (!mdec->visitor) { BT_LOGE("Failed to create a CTF IR metadata AST visitor: " "mdec-addr=%p", mdec); @@ -351,9 +383,12 @@ struct ctf_metadata_decoder *ctf_metadata_decoder_create( goto end; } - BT_LOGD("Created CTF metadata decoder: " - "clock-class-offset-ns=%" PRId64 ", name=\"%s\", addr=%p", - clock_class_offset_ns, name, mdec); + BT_LOGD("Creating CTF metadata decoder: " + "clock-class-offset-s=%" PRId64 ", " + "clock-class-offset-ns=%" PRId64 ", " + "name=\"%s\", addr=%p", + config->clock_class_offset_s, config->clock_class_offset_ns, + name, mdec); end: return mdec; @@ -382,7 +417,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( char *buf = NULL; bool close_fp = false; - assert(mdec); + BT_ASSERT(mdec); if (ctf_metadata_decoder_is_packetized(fp, &mdec->bo)) { BT_LOGD("Metadata stream is packetized: mdec-addr=%p", mdec); @@ -416,6 +451,12 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( BT_LOGD("Metadata stream is plain text: mdec-addr=%p", mdec); + if (init_pos < 0) { + BT_LOGE_ERRNO("Failed to get current file position", "."); + status = CTF_METADATA_DECODER_STATUS_ERROR; + goto end; + } + /* Check text-only metadata header and version */ nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor); if (nr_items < 2) { @@ -454,7 +495,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( goto end; } - assert(fp); + BT_ASSERT(fp); ret = ctf_scanner_append_ast(scanner, fp); if (ret) { BT_LOGE("Cannot create the metadata AST out of the metadata text: " @@ -473,6 +514,10 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( ret = ctf_visitor_generate_ir_visit_node(mdec->visitor, &scanner->ast->root); + // TODO + ret = -1; + goto end; + switch (ret) { case 0: /* Success */ @@ -511,8 +556,15 @@ end: } BT_HIDDEN -struct bt_ctf_trace *ctf_metadata_decoder_get_trace( +struct bt_trace *ctf_metadata_decoder_get_ir_trace( + struct ctf_metadata_decoder *mdec) +{ + return ctf_visitor_generate_ir_get_ir_trace(mdec->visitor); +} + +BT_HIDDEN +struct ctf_trace_class *ctf_metadata_decoder_borrow_ctf_trace_class( struct ctf_metadata_decoder *mdec) { - return ctf_visitor_generate_ir_get_trace(mdec->visitor); + return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec->visitor); }