ctf: append error causes when returning errors
[babeltrace.git] / src / plugins / ctf / common / metadata / decoder.c
index 17b2aa4bd211be1474d4d7da288207f7cc7fc2a7..f2a140b7b9ac6088bdce7e267a3090d9c746dad6 100644 (file)
@@ -1,18 +1,11 @@
 /*
- * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
+ * SPDX-License-Identifier: MIT
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
  */
 
 #define BT_COMP_LOG_SELF_COMP (mdec->config.self_comp)
+#define BT_COMP_LOG_SELF_COMP_CLASS (mdec->config.self_comp_class)
 #define BT_LOG_OUTPUT_LEVEL (mdec->config.log_level)
 #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER"
 #include "logging/comp-logging.h"
 #include "decoder.h"
 #include "scanner.h"
 #include "logging.h"
+#include "parser-wrap.h"
+#include "decoder-packetized-file-stream-to-buf.h"
 
 #define TSDL_MAGIC     0x75d11d57
 
-extern
-int yydebug;
-
 struct ctf_metadata_decoder {
        struct ctf_scanner *scanner;
        GString *text;
@@ -63,12 +55,6 @@ struct packet_header {
        uint8_t  minor;
 } __attribute__((__packed__));
 
-BT_HIDDEN
-int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp,
-               char **buf, int byte_order, bool *is_uuid_set,
-               uint8_t *uuid, bt_logging_level log_level,
-               bt_self_component *self_comp);
-
 BT_HIDDEN
 int ctf_metadata_decoder_is_packetized(FILE *fp, bool *is_packetized,
                int *byte_order, bt_logging_level log_level,
@@ -127,16 +113,17 @@ struct ctf_metadata_decoder *ctf_metadata_decoder_create(
 
        mdec->log_cfg.log_level = config->log_level;
        mdec->log_cfg.self_comp = config->self_comp;
+       mdec->log_cfg.self_comp_class = config->self_comp_class;
        mdec->scanner = ctf_scanner_alloc();
        if (!mdec->scanner) {
-               BT_COMP_LOGE("Cannot allocate a metadata lexical scanner: "
+               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot allocate a metadata lexical scanner: "
                        "mdec-addr=%p", mdec);
                goto error;
        }
 
        mdec->text = g_string_new(NULL);
        if (!mdec->text) {
-               BT_COMP_LOGE("Failed to allocate one GString: "
+               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate one GString: "
                        "mdec-addr=%p", mdec);
                goto error;
        }
@@ -145,7 +132,7 @@ struct ctf_metadata_decoder *ctf_metadata_decoder_create(
        mdec->config = *config;
        mdec->visitor = ctf_visitor_generate_ir_create(config);
        if (!mdec->visitor) {
-               BT_COMP_LOGE("Failed to create a CTF IR metadata AST visitor: "
+               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to create a CTF IR metadata AST visitor: "
                        "mdec-addr=%p", mdec);
                goto error;
        }
@@ -210,9 +197,10 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                ret = ctf_metadata_decoder_packetized_file_stream_to_buf(fp,
                        &buf, mdec->bo, &mdec->is_uuid_set,
                        mdec->uuid, mdec->config.log_level,
-                       mdec->config.self_comp);
+                       mdec->config.self_comp,
+                       mdec->config.self_comp_class);
                if (ret) {
-                       BT_COMP_LOGE("Cannot decode packetized metadata packets to metadata text: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot decode packetized metadata packets to metadata text: "
                                "mdec-addr=%p, ret=%d", mdec, ret);
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
@@ -227,7 +215,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                fp = bt_fmemopen(buf, strlen(buf), "rb");
                close_fp = true;
                if (!fp) {
-                       BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot memory-open metadata buffer: %s: "
                                "mdec-addr=%p", strerror(errno), mdec);
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
@@ -240,7 +228,8 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec);
 
                if (init_pos < 0) {
-                       BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
+                       BT_COMP_LOGE_APPEND_CAUSE_ERRNO(BT_COMP_LOG_SELF_COMP,
+                               "Failed to get current file position", ".");
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
                }
@@ -256,7 +245,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
 
                if (!ctf_metadata_decoder_is_packet_version_valid(major,
                                minor)) {
-                       BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid metadata version found in plain text signature: "
                                "version=%u.%u, mdec-addr=%p", major, minor,
                                mdec);
                        status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION;
@@ -264,16 +253,18 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                }
 
                if (fseek(fp, init_pos, SEEK_SET)) {
-                       BT_COMP_LOGE("Cannot seek metadata file stream to initial position: %s: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot seek metadata file stream to initial position: %s: "
                                "mdec-addr=%p", strerror(errno), mdec);
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
                }
        }
 
+#if YYDEBUG
        if (BT_LOG_ON_TRACE) {
                yydebug = 1;
        }
+#endif
 
        /* Save the file's position: we'll seek back to append the plain text */
        BT_ASSERT(fp);
@@ -285,7 +276,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
        /* Append the metadata text content */
        ret = ctf_scanner_append_ast(mdec->scanner, fp);
        if (ret) {
-               BT_COMP_LOGE("Cannot create the metadata AST out of the metadata text: "
+               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create the metadata AST out of the metadata text: "
                        "mdec-addr=%p", mdec);
                status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
                goto end;
@@ -296,7 +287,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                BT_ASSERT(start_pos != -1);
                ret = fseek(fp, start_pos, SEEK_SET);
                if (ret) {
-                       BT_COMP_LOGE("Failed to seek file: ret=%d, mdec-addr=%p",
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to seek file: ret=%d, mdec-addr=%p",
                                ret, mdec);
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
@@ -304,7 +295,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
 
                ret = bt_common_append_file_content_to_g_string(mdec->text, fp);
                if (ret) {
-                       BT_COMP_LOGE("Failed to append to current plain text: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to append to current plain text: "
                                "ret=%d, mdec-addr=%p", ret, mdec);
                        status = CTF_METADATA_DECODER_STATUS_ERROR;
                        goto end;
@@ -314,7 +305,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
        ret = ctf_visitor_semantic_check(0, &mdec->scanner->ast->root,
                &mdec->log_cfg);
        if (ret) {
-               BT_COMP_LOGE("Validation of the metadata semantics failed: "
+               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Validation of the metadata semantics failed: "
                        "mdec-addr=%p", mdec);
                status = CTF_METADATA_DECODER_STATUS_ERROR;
                goto end;
@@ -333,7 +324,7 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
                        status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
                        goto end;
                default:
-                       BT_COMP_LOGE("Failed to visit AST node to create CTF IR objects: "
+                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to visit AST node to create CTF IR objects: "
                                "mdec-addr=%p, ret=%d", mdec, ret);
                        status = CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR;
                        goto end;
@@ -341,7 +332,9 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
        }
 
 end:
+#if YYDEBUG
        yydebug = 0;
+#endif
 
        if (fp && close_fp) {
                if (fclose(fp)) {
@@ -359,8 +352,8 @@ BT_HIDDEN
 bt_trace_class *ctf_metadata_decoder_get_ir_trace_class(
                struct ctf_metadata_decoder *mdec)
 {
-       BT_ASSERT(mdec);
-       BT_ASSERT(mdec->config.create_trace_class);
+       BT_ASSERT_DBG(mdec);
+       BT_ASSERT_DBG(mdec->config.create_trace_class);
        return ctf_visitor_generate_ir_get_ir_trace_class(mdec->visitor);
 }
 
@@ -368,23 +361,23 @@ BT_HIDDEN
 struct ctf_trace_class *ctf_metadata_decoder_borrow_ctf_trace_class(
                struct ctf_metadata_decoder *mdec)
 {
-       BT_ASSERT(mdec);
-       BT_ASSERT(mdec->config.create_trace_class);
+       BT_ASSERT_DBG(mdec);
+       BT_ASSERT_DBG(mdec->config.create_trace_class);
        return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec->visitor);
 }
 
 BT_HIDDEN
 const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder *mdec)
 {
-       BT_ASSERT(mdec);
-       BT_ASSERT(mdec->config.keep_plain_text);
+       BT_ASSERT_DBG(mdec);
+       BT_ASSERT_DBG(mdec->config.keep_plain_text);
        return mdec->text->str;
 }
 
 BT_HIDDEN
 int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder *mdec)
 {
-       BT_ASSERT(mdec);
+       BT_ASSERT_DBG(mdec);
        return mdec->bo;
 }
 
@@ -394,7 +387,7 @@ int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec,
 {
        int ret = 0;
 
-       BT_ASSERT(mdec);
+       BT_ASSERT_DBG(mdec);
 
        if (!mdec->is_uuid_set) {
                ret = -1;
@@ -406,3 +399,80 @@ int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec,
 end:
        return ret;
 }
+
+static
+enum ctf_metadata_decoder_status find_uuid_in_trace_decl(
+               struct ctf_metadata_decoder *mdec, struct ctf_node *trace_node,
+               bt_uuid_t uuid)
+{
+       enum ctf_metadata_decoder_status status =
+               CTF_METADATA_DECODER_STATUS_OK;
+       struct ctf_node *entry_node;
+       struct bt_list_head *decl_list = &trace_node->u.trace.declaration_list;
+       char *left = NULL;
+
+       bt_list_for_each_entry(entry_node, decl_list, siblings) {
+               if (entry_node->type == NODE_CTF_EXPRESSION) {
+                       int ret;
+
+                       left = ctf_ast_concatenate_unary_strings(
+                               &entry_node->u.ctf_expression.left);
+                       if (!left) {
+                               _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot concatenate unary strings.");
+                               status = CTF_METADATA_DECODER_STATUS_ERROR;
+                               goto end;
+                       }
+
+                       if (strcmp(left, "uuid") == 0) {
+                               ret = ctf_ast_get_unary_uuid(
+                                       &entry_node->u.ctf_expression.right,
+                                       uuid, mdec->config.log_level,
+                                       mdec->config.self_comp);
+                               if (ret) {
+                                       _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid trace's `uuid` attribute.");
+                                       status = CTF_METADATA_DECODER_STATUS_ERROR;
+                                       goto end;
+                               }
+
+                               goto end;
+                       }
+
+                       g_free(left);
+                       left = NULL;
+               }
+       }
+
+       status = CTF_METADATA_DECODER_STATUS_NONE;
+
+end:
+       g_free(left);
+       return status;
+}
+
+BT_HIDDEN
+enum ctf_metadata_decoder_status ctf_metadata_decoder_get_trace_class_uuid(
+               struct ctf_metadata_decoder *mdec, bt_uuid_t uuid)
+{
+       enum ctf_metadata_decoder_status status =
+               CTF_METADATA_DECODER_STATUS_INCOMPLETE;
+       struct ctf_node *root_node = &mdec->scanner->ast->root;
+       struct ctf_node *trace_node;
+
+       if (!root_node) {
+               status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
+               goto end;
+       }
+
+       trace_node =
+               bt_list_entry(root_node->u.root.trace.next,
+                       struct ctf_node, siblings);
+       if (!trace_node) {
+               status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
+               goto end;
+       }
+
+       status = find_uuid_in_trace_decl(mdec, trace_node, uuid);
+
+end:
+       return status;
+}
This page took 0.028944 seconds and 4 git commands to generate.