assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[babeltrace.git] / lib / ctf-ir / resolve.c
index 2ab457ab8f3dfdba226a7f98f786f67554349fc0..c4bf310d0369bf05309fdc60cc60bbbfee5ab40f 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-ir/event.h>
+#define BT_LOG_TAG "RESOLVE"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/resolve-internal.h>
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/field-path.h>
 #include <babeltrace/ctf-ir/field-path-internal.h>
-#include <babeltrace/ctf-ir/event-internal.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/values.h>
 #include <babeltrace/types.h>
+#include <babeltrace/assert-internal.h>
 #include <limits.h>
+#include <inttypes.h>
+#include <stdlib.h>
 #include <glib.h>
 
-#define _printf_error(fmt, args...) \
-       printf_verbose("[resolving] " fmt, ## args)
-
 typedef GPtrArray type_stack;
 
 /*
@@ -57,7 +58,7 @@ typedef GPtrArray type_stack;
  * `type` is owned by the stack frame.
  */
 struct type_stack_frame {
-       struct bt_ctf_field_type *type;
+       struct bt_field_type *type;
        int index;
 };
 
@@ -76,34 +77,34 @@ struct type_stack_frame {
  */
 struct resolve_context {
        struct bt_value *environment;
-       struct bt_ctf_field_type *scopes[6];
+       struct bt_field_type *scopes[6];
 
        /* Root scope being visited */
-       enum bt_ctf_scope root_scope;
+       enum bt_scope root_scope;
        type_stack *type_stack;
-       struct bt_ctf_field_type *cur_field_type;
+       struct bt_field_type *cur_field_type;
 };
 
 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
 static const char * const absolute_path_prefixes[] = {
-       [BT_CTF_SCOPE_ENV]                      = "env.",
-       [BT_CTF_SCOPE_TRACE_PACKET_HEADER]      = "trace.packet.header.",
-       [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT]    = "stream.packet.context.",
-       [BT_CTF_SCOPE_STREAM_EVENT_HEADER]      = "stream.event.header.",
-       [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT]     = "stream.event.context.",
-       [BT_CTF_SCOPE_EVENT_CONTEXT]            = "event.context.",
-       [BT_CTF_SCOPE_EVENT_FIELDS]             = "event.fields.",
+       [BT_SCOPE_ENV]                          = "env.",
+       [BT_SCOPE_TRACE_PACKET_HEADER]          = "trace.packet.header.",
+       [BT_SCOPE_STREAM_PACKET_CONTEXT]        = "stream.packet.context.",
+       [BT_SCOPE_STREAM_EVENT_HEADER]          = "stream.event.header.",
+       [BT_SCOPE_STREAM_EVENT_CONTEXT]         = "stream.event.context.",
+       [BT_SCOPE_EVENT_CONTEXT]                = "event.context.",
+       [BT_SCOPE_EVENT_FIELDS]                 = "event.fields.",
 };
 
 /* Number of path tokens used for the absolute prefixes */
 static const int absolute_path_prefix_ptoken_counts[] = {
-       [BT_CTF_SCOPE_ENV]                      = 1,
-       [BT_CTF_SCOPE_TRACE_PACKET_HEADER]      = 3,
-       [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT]    = 3,
-       [BT_CTF_SCOPE_STREAM_EVENT_HEADER]      = 3,
-       [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT]     = 3,
-       [BT_CTF_SCOPE_EVENT_CONTEXT]            = 2,
-       [BT_CTF_SCOPE_EVENT_FIELDS]             = 2,
+       [BT_SCOPE_ENV]                  = 1,
+       [BT_SCOPE_TRACE_PACKET_HEADER]  = 3,
+       [BT_SCOPE_STREAM_PACKET_CONTEXT]        = 3,
+       [BT_SCOPE_STREAM_EVENT_HEADER]  = 3,
+       [BT_SCOPE_STREAM_EVENT_CONTEXT] = 3,
+       [BT_SCOPE_EVENT_CONTEXT]                = 2,
+       [BT_SCOPE_EVENT_FIELDS]         = 2,
 };
 
 /*
@@ -144,22 +145,26 @@ void type_stack_destroy(type_stack *stack)
  * `type` is owned by the caller (stack frame gets a new reference).
  */
 static
-int type_stack_push(type_stack *stack, struct bt_ctf_field_type *type)
+int type_stack_push(type_stack *stack, struct bt_field_type *type)
 {
        int ret = 0;
        struct type_stack_frame *frame = NULL;
 
        if (!stack || !type) {
+               BT_LOGW("Invalid parameter: stack or type is NULL.");
                ret = -1;
                goto end;
        }
 
        frame = g_new0(struct type_stack_frame, 1);
        if (!frame) {
+               BT_LOGE_STR("Failed to allocate one field type stack frame.");
                ret = -1;
                goto end;
        }
 
+       BT_LOGV("Pushing field type on context's stack: "
+               "ft-addr=%p, stack-size-before=%u", type, stack->len);
        frame->type = bt_get(type);
        g_ptr_array_add(stack, frame);
 
@@ -236,6 +241,8 @@ void type_stack_pop(type_stack *stack)
                 * This will call the frame's destructor and free it, as
                 * well as put its contained field type.
                 */
+               BT_LOGV("Popping context's stack: stack-size-before=%u",
+                       stack->len);
                g_ptr_array_set_size(stack, stack->len - 1);
        }
 }
@@ -246,13 +253,13 @@ void type_stack_pop(type_stack *stack)
  * Return value is owned by `ctx` on success.
  */
 static
-struct bt_ctf_field_type *get_type_from_ctx(struct resolve_context *ctx,
-               enum bt_ctf_scope scope)
+struct bt_field_type *get_type_from_ctx(struct resolve_context *ctx,
+               enum bt_scope scope)
 {
-       assert(scope >= BT_CTF_SCOPE_TRACE_PACKET_HEADER &&
-               scope <= BT_CTF_SCOPE_EVENT_FIELDS);
+       BT_ASSERT(scope >= BT_SCOPE_TRACE_PACKET_HEADER &&
+               scope <= BT_SCOPE_EVENT_FIELDS);
 
-       return ctx->scopes[scope - BT_CTF_SCOPE_TRACE_PACKET_HEADER];
+       return ctx->scopes[scope - BT_SCOPE_TRACE_PACKET_HEADER];
 }
 
 /*
@@ -260,14 +267,14 @@ struct bt_ctf_field_type *get_type_from_ctx(struct resolve_context *ctx,
  * CTF_NODE_UNKNOWN if the path is found to be relative.
  */
 static
-enum bt_ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr)
+enum bt_scope get_root_scope_from_absolute_pathstr(const char *pathstr)
 {
-       enum bt_ctf_scope scope;
-       enum bt_ctf_scope ret = BT_CTF_SCOPE_UNKNOWN;
+       enum bt_scope scope;
+       enum bt_scope ret = BT_SCOPE_UNKNOWN;
        const size_t prefixes_count = sizeof(absolute_path_prefixes) /
                sizeof(*absolute_path_prefixes);
 
-       for (scope = BT_CTF_SCOPE_ENV; scope < BT_CTF_SCOPE_ENV +
+       for (scope = BT_SCOPE_ENV; scope < BT_SCOPE_ENV +
                        prefixes_count; scope++) {
                /*
                 * Chech if path string starts with a known absolute
@@ -278,11 +285,18 @@ enum bt_ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr)
                if (strncmp(pathstr, absolute_path_prefixes[scope],
                                strlen(absolute_path_prefixes[scope]))) {
                        /* Prefix does not match: try the next one */
+                       BT_LOGV("Prefix does not match: trying the next one: "
+                               "path=\"%s\", path-prefix=\"%s\", scope=%s",
+                               pathstr, absolute_path_prefixes[scope],
+                               bt_common_scope_string(scope));
                        continue;
                }
 
                /* Found it! */
                ret = scope;
+               BT_LOGV("Found root scope from absolute path: "
+                       "path=\"%s\", scope=%s", pathstr,
+                       bt_common_scope_string(scope));
                goto end;
        }
 
@@ -344,8 +358,8 @@ GList *pathstr_to_ptokens(const char *pathstr)
 
                        if (at == last) {
                                /* Error: empty token */
-                               _printf_error("Empty token in path string at position %d\n",
-                                       (int) (at - pathstr));
+                               BT_LOGW("Empty path token: path=\"%s\", pos=%u",
+                                       pathstr, (int) (at - pathstr));
                                goto error;
                        }
 
@@ -380,8 +394,8 @@ error:
  * caller.
  */
 static
-int ptokens_to_field_path(GList *ptokens, struct bt_ctf_field_path *field_path,
-               struct bt_ctf_field_type *type, int src_index)
+int ptokens_to_field_path(GList *ptokens, struct bt_field_path *field_path,
+               struct bt_field_type *type, int src_index)
 {
        int ret = 0;
        GList *cur_ptoken = ptokens;
@@ -393,30 +407,35 @@ int ptokens_to_field_path(GList *ptokens, struct bt_ctf_field_path *field_path,
        /* Locate target */
        while (cur_ptoken) {
                int child_index;
-               struct bt_ctf_field_type *child_type;
+               struct bt_field_type *child_type;
                const char *field_name = ptoken_get_string(cur_ptoken);
-               enum bt_ctf_field_type_id type_id =
-                       bt_ctf_field_type_get_type_id(type);
+               enum bt_field_type_id type_id =
+                       bt_field_type_get_type_id(type);
+
+               BT_LOGV("Current path token: token=\"%s\"", field_name);
 
                /* Find to which index corresponds the current path token */
-               if (type_id == CTF_TYPE_ARRAY || type_id == CTF_TYPE_SEQUENCE) {
+               if (type_id == BT_FIELD_TYPE_ID_ARRAY ||
+                               type_id == BT_FIELD_TYPE_ID_SEQUENCE) {
                        child_index = -1;
                } else {
-                       child_index = bt_ctf_field_type_get_field_index(type,
+                       child_index = bt_field_type_get_field_index(type,
                                field_name);
                        if (child_index < 0) {
                                /*
                                 * Error: field name does not exist or
                                 * wrong current type.
                                 */
-                               _printf_error("Cannot get index of field type named \"%s\"\n",
-                                       field_name);
+                               BT_LOGW("Cannot get index of field type: "
+                                       "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
+                                       field_name, src_index, child_index, first_level_done);
                                ret = -1;
                                goto end;
                        } else if (child_index > src_index &&
                                        !first_level_done) {
-                               _printf_error("Child type is located after source index (%d)\n",
-                                       src_index);
+                               BT_LOGW("Child field type is located after source field type: "
+                                       "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
+                                       field_name, src_index, child_index, first_level_done);
                                ret = -1;
                                goto end;
                        }
@@ -430,16 +449,18 @@ int ptokens_to_field_path(GList *ptokens, struct bt_ctf_field_path *field_path,
                g_array_append_val(field_path->indexes, child_index);
 
                /* Get child field type */
-               child_type = bt_ctf_field_type_get_field_at_index(type,
+               child_type = bt_field_type_borrow_field_at_index(type,
                        child_index);
                if (!child_type) {
-                       _printf_error("Cannot get child type at index %d (field \"%s\")\n",
-                               child_index, field_name);
+                       BT_LOGW("Cannot get child field type: "
+                               "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
+                               field_name, src_index, child_index, first_level_done);
                        ret = -1;
                        goto end;
                }
 
                /* Move child type to current type */
+               bt_get(child_type);
                BT_MOVE(type, child_type);
        }
 
@@ -457,12 +478,12 @@ end:
  */
 static
 int absolute_ptokens_to_field_path(GList *ptokens,
-               struct bt_ctf_field_path *field_path,
+               struct bt_field_path *field_path,
                struct resolve_context *ctx)
 {
        int ret = 0;
        GList *cur_ptoken;
-       struct bt_ctf_field_type *type;
+       struct bt_field_type *type;
 
        /* Skip absolute path tokens */
        cur_ptoken = g_list_nth(ptokens,
@@ -472,8 +493,9 @@ int absolute_ptokens_to_field_path(GList *ptokens,
        type = get_type_from_ctx(ctx, field_path->root);
        if (!type) {
                /* Error: root type is not available */
-               _printf_error("Root type with scope type %d is not available\n",
-                       field_path->root);
+               BT_LOGW("Root field type is not available: "
+                       "root-scope=%s",
+                       bt_common_scope_string(field_path->root));
                ret = -1;
                goto end;
        }
@@ -494,15 +516,15 @@ end:
  */
 static
 int relative_ptokens_to_field_path(GList *ptokens,
-               struct bt_ctf_field_path *field_path,
+               struct bt_field_path *field_path,
                struct resolve_context *ctx)
 {
        int ret = 0;
        int parent_pos_in_stack;
-       struct bt_ctf_field_path *tail_field_path = bt_ctf_field_path_create();
+       struct bt_field_path *tail_field_path = bt_field_path_create();
 
        if (!tail_field_path) {
-               _printf_error("Cannot create field path\n");
+               BT_LOGE_STR("Cannot create empty field path.");
                ret = -1;
                goto end;
        }
@@ -510,18 +532,23 @@ int relative_ptokens_to_field_path(GList *ptokens,
        parent_pos_in_stack = type_stack_size(ctx->type_stack) - 1;
 
        while (parent_pos_in_stack >= 0) {
-               struct bt_ctf_field_type *parent_type =
+               struct bt_field_type *parent_type =
                        type_stack_at(ctx->type_stack,
                                parent_pos_in_stack)->type;
                int cur_index = type_stack_at(ctx->type_stack,
                        parent_pos_in_stack)->index;
 
+               BT_LOGV("Locating target field type from current parent field type: "
+                       "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
+                       parent_pos_in_stack, parent_type, cur_index);
+
                /* Locate target from current parent type */
                ret = ptokens_to_field_path(ptokens, tail_field_path,
                        parent_type, cur_index);
                if (ret) {
                        /* Not found... yet */
-                       bt_ctf_field_path_clear(tail_field_path);
+                       BT_LOGV_STR("Not found at this point.");
+                       bt_field_path_clear(tail_field_path);
                } else {
                        /* Found: stitch tail field path to head field path */
                        int i = 0;
@@ -529,7 +556,7 @@ int relative_ptokens_to_field_path(GList *ptokens,
                                tail_field_path->indexes->len;
 
                        while (BT_TRUE) {
-                               struct bt_ctf_field_type *cur_type =
+                               struct bt_field_type *cur_type =
                                        type_stack_at(ctx->type_stack, i)->type;
                                int index = type_stack_at(
                                        ctx->type_stack, i)->index;
@@ -561,10 +588,12 @@ int relative_ptokens_to_field_path(GList *ptokens,
                /* Not found: look in previous scopes */
                field_path->root--;
 
-               while (field_path->root >= BT_CTF_SCOPE_TRACE_PACKET_HEADER) {
-                       struct bt_ctf_field_type *root_type;
-                       bt_ctf_field_path_clear(field_path);
+               while (field_path->root >= BT_SCOPE_TRACE_PACKET_HEADER) {
+                       struct bt_field_type *root_type;
+                       bt_field_path_clear(field_path);
 
+                       BT_LOGV("Looking into potential root scope: scope=%s",
+                               bt_common_scope_string(field_path->root));
                        root_type = get_type_from_ctx(ctx, field_path->root);
                        if (!root_type) {
                                field_path->root--;
@@ -576,11 +605,13 @@ int relative_ptokens_to_field_path(GList *ptokens,
                                root_type, INT_MAX);
                        if (ret) {
                                /* Not found yet */
+                               BT_LOGV_STR("Not found in this scope.");
                                field_path->root--;
                                continue;
                        }
 
                        /* Found */
+                       BT_LOGV_STR("Found in this scope.");
                        break;
                }
        }
@@ -597,18 +628,18 @@ end:
  * Return value is owned by the caller on success.
  */
 static
-struct bt_ctf_field_path *pathstr_to_field_path(const char *pathstr,
+struct bt_field_path *pathstr_to_field_path(const char *pathstr,
                struct resolve_context *ctx)
 {
        int ret;
-       enum bt_ctf_scope root_scope;
+       enum bt_scope root_scope;
        GList *ptokens = NULL;
-       struct bt_ctf_field_path *field_path = NULL;
+       struct bt_field_path *field_path = NULL;
 
        /* Create field path */
-       field_path = bt_ctf_field_path_create();
+       field_path = bt_field_path_create();
        if (!field_path) {
-               _printf_error("Cannot create field path\n");
+               BT_LOGE_STR("Cannot create empty field path.");
                ret = -1;
                goto end;
        }
@@ -616,8 +647,8 @@ struct bt_ctf_field_path *pathstr_to_field_path(const char *pathstr,
        /* Convert path string to path tokens */
        ptokens = pathstr_to_ptokens(pathstr);
        if (!ptokens) {
-               _printf_error("Cannot convert path string \"%s\" to path tokens\n",
-                       pathstr);
+               BT_LOGW("Cannot convert path string to path tokens: "
+                       "path=\"%s\"", pathstr);
                ret = -1;
                goto end;
        }
@@ -625,40 +656,58 @@ struct bt_ctf_field_path *pathstr_to_field_path(const char *pathstr,
        /* Absolute or relative path? */
        root_scope = get_root_scope_from_absolute_pathstr(pathstr);
 
-       if (root_scope == BT_CTF_SCOPE_UNKNOWN) {
+       if (root_scope == BT_SCOPE_UNKNOWN) {
                /* Relative path: start with current root scope */
                field_path->root = ctx->root_scope;
+               BT_LOGV("Detected relative path: starting with current root scope: "
+                       "scope=%s", bt_common_scope_string(field_path->root));
                ret = relative_ptokens_to_field_path(ptokens, field_path, ctx);
                if (ret) {
-                       _printf_error("Cannot get relative field path of path string \"%s\"\n",
-                               pathstr);
-                       _printf_error("  Starting at root scope %d, finished at root scope %d\n",
-                               ctx->root_scope, field_path->root);
+                       BT_LOGW("Cannot get relative field path of path string: "
+                               "path=\"%s\", start-scope=%s, end-scope=%s",
+                               pathstr, bt_common_scope_string(ctx->root_scope),
+                               bt_common_scope_string(field_path->root));
                        goto end;
                }
-       } else if (root_scope == BT_CTF_SCOPE_ENV) {
-               _printf_error("Sequence field types referring the trace environment are not supported as of this version\n");
+       } else if (root_scope == BT_SCOPE_ENV) {
+               BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
+                       "path=\"%s\"", pathstr);
                ret = -1;
                goto end;
        } else {
                /* Absolute path: use found root scope */
                field_path->root = root_scope;
+               BT_LOGV("Detected absolute path: using root scope: "
+                       "scope=%s", bt_common_scope_string(field_path->root));
                ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx);
                if (ret) {
-                       _printf_error("Cannot get absolute field path of path string \"%s\"\n",
-                               pathstr);
-                       _printf_error("  Looking in root scope %d\n", root_scope);
+                       BT_LOGW("Cannot get absolute field path of path string: "
+                               "path=\"%s\", root-scope=%s",
+                               pathstr, bt_common_scope_string(root_scope));
                        goto end;
                }
        }
 
+       if (ret == 0) {
+               GString *field_path_pretty =
+                       bt_field_path_string(field_path);
+               const char *field_path_pretty_str =
+                       field_path_pretty ? field_path_pretty->str : NULL;
+
+               BT_LOGV("Found field path: path=\"%s\", field-path=\"%s\"",
+                       pathstr, field_path_pretty_str);
+
+               if (field_path_pretty) {
+                       g_string_free(field_path_pretty, TRUE);
+               }
+       }
+
 end:
        if (ret) {
                BT_PUT(field_path);
        }
 
        ptokens_destroy(ptokens);
-
        return field_path;
 }
 
@@ -669,39 +718,40 @@ end:
  * Return value is owned by the caller on success.
  */
 static
-struct bt_ctf_field_type *field_path_to_field_type(
-               struct bt_ctf_field_path *field_path,
+struct bt_field_type *field_path_to_field_type(
+               struct bt_field_path *field_path,
                struct resolve_context *ctx)
 {
        int i;
-       struct bt_ctf_field_type *type;
+       struct bt_field_type *type;
 
        /* Start with root type */
        type = get_type_from_ctx(ctx, field_path->root);
        bt_get(type);
        if (!type) {
                /* Error: root type is not available */
-               _printf_error("Root type with scope type %d is not available\n",
-                       field_path->root);
+               BT_LOGW("Root field type is not available: root-scope=%s",
+                       bt_common_scope_string(field_path->root));
                goto error;
        }
 
        /* Locate target */
        for (i = 0; i < field_path->indexes->len; i++) {
-               struct bt_ctf_field_type *child_type;
+               struct bt_field_type *child_type;
                int child_index =
                        g_array_index(field_path->indexes, int, i);
 
                /* Get child field type */
-               child_type = bt_ctf_field_type_get_field_at_index(type,
+               child_type = bt_field_type_borrow_field_at_index(type,
                        child_index);
                if (!child_type) {
-                       _printf_error("Cannot get field type field at index %d\n",
-                               child_index);
+                       BT_LOGW("Cannot get field type: "
+                               "parent-ft-addr=%p, index=%d", type, i);
                        goto error;
                }
 
                /* Move child type to current type */
+               bt_get(child_type);
                BT_MOVE(type, child_type);
        }
 
@@ -718,15 +768,15 @@ error:
  * Return value is owned by the caller on success.
  */
 static
-struct bt_ctf_field_path *get_ctx_stack_field_path(struct resolve_context *ctx)
+struct bt_field_path *get_ctx_stack_field_path(struct resolve_context *ctx)
 {
        int i;
-       struct bt_ctf_field_path *field_path;
+       struct bt_field_path *field_path;
 
        /* Create field path */
-       field_path = bt_ctf_field_path_create();
+       field_path = bt_field_path_create();
        if (!field_path) {
-               _printf_error("Cannot create field path\n");
+               BT_LOGE_STR("Cannot create empty field path.");
                goto error;
        }
 
@@ -752,16 +802,39 @@ error:
  *
  * `field_path1` and `field_path2` are owned by the caller.
  */
-int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1,
-               struct bt_ctf_field_path *field_path2)
+int get_field_paths_lca_index(struct bt_field_path *field_path1,
+               struct bt_field_path *field_path2)
 {
        int lca_index = 0;
        int field_path1_len, field_path2_len;
 
+       if (BT_LOG_ON_VERBOSE) {
+               GString *field_path1_pretty =
+                       bt_field_path_string(field_path1);
+               GString *field_path2_pretty =
+                       bt_field_path_string(field_path2);
+               const char *field_path1_pretty_str =
+                       field_path1_pretty ? field_path1_pretty->str : NULL;
+               const char *field_path2_pretty_str =
+                       field_path2_pretty ? field_path2_pretty->str : NULL;
+
+               BT_LOGV("Finding lowest common ancestor (LCA) between two field paths: "
+                       "field-path-1=\"%s\", field-path-2=\"%s\"",
+                       field_path1_pretty_str, field_path2_pretty_str);
+
+               if (field_path1_pretty) {
+                       g_string_free(field_path1_pretty, TRUE);
+               }
+
+               if (field_path2_pretty) {
+                       g_string_free(field_path2_pretty, TRUE);
+               }
+       }
+
        /*
         * Start from both roots and find the first mismatch.
         */
-       assert(field_path1->root == field_path2->root);
+       BT_ASSERT(field_path1->root == field_path2->root);
        field_path1_len = field_path1->indexes->len;
        field_path2_len = field_path2->indexes->len;
 
@@ -775,7 +848,10 @@ int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1,
                         * This is invalid because the target cannot be
                         * an ancestor of the source.
                         */
-                       _printf_error("In source and target: one is an ancestor of the other\n");
+                       BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
+                               "lca-index=%d, field-path-1-len=%d, "
+                               "field-path-2-len=%d",
+                               lca_index, field_path1_len, field_path2_len);
                        lca_index = -1;
                        break;
                }
@@ -793,6 +869,7 @@ int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1,
                lca_index++;
        }
 
+       BT_LOGV("Found LCA: lca-index=%d", lca_index);
        return lca_index;
 }
 
@@ -802,21 +879,21 @@ int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1,
  * `target_field_path` and `target_type` are owned by the caller.
  */
 static
-int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
-               struct bt_ctf_field_type *target_type,
+int validate_target_field_path(struct bt_field_path *target_field_path,
+               struct bt_field_type *target_type,
                struct resolve_context *ctx)
 {
        int ret = 0;
-       struct bt_ctf_field_path *ctx_field_path;
+       struct bt_field_path *ctx_field_path;
        int target_field_path_len = target_field_path->indexes->len;
        int lca_index;
-       int ctx_cur_field_type_id;
-       int target_type_id;
+       enum bt_field_type_id ctx_cur_field_type_id;
+       enum bt_field_type_id target_type_id;
 
        /* Get context field path */
        ctx_field_path = get_ctx_stack_field_path(ctx);
        if (!ctx_field_path) {
-               _printf_error("Cannot get source field path\n");
+               BT_LOGW_STR("Cannot get field path from context's stack.");
                ret = -1;
                goto end;
        }
@@ -825,7 +902,7 @@ int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
         * Make sure the target is not a root.
         */
        if (target_field_path_len == 0) {
-               _printf_error("Target field path's length is 0 (targeting the root)\n");
+               BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
                ret = -1;
                goto end;
        }
@@ -835,7 +912,10 @@ int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
         * after the context field path's root.
         */
        if (target_field_path->root > ctx_field_path->root) {
-               _printf_error("Target is located after source\n");
+               BT_LOGW("Target field type is located after source field type: "
+                       "target-root=%s, source-root=%s",
+                       bt_common_scope_string(target_field_path->root),
+                       bt_common_scope_string(ctx_field_path->root));
                ret = -1;
                goto end;
        }
@@ -850,7 +930,7 @@ int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
                lca_index = get_field_paths_lca_index(target_field_path,
                        ctx_field_path);
                if (lca_index < 0) {
-                       _printf_error("Cannot get least common ancestor\n");
+                       BT_LOGW_STR("Cannot get least common ancestor.");
                        ret = -1;
                        goto end;
                }
@@ -865,8 +945,9 @@ int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
                        int, lca_index);
 
                if (target_index >= ctx_index) {
-                       _printf_error("Target index (%d) is greater or equal to source index (%d) in LCA\n",
-                               target_index, ctx_index);
+                       BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
+                               "lca-index=%d, target-index=%d, source-index=%d",
+                               lca_index, target_index, ctx_index);
                        ret = -1;
                        goto end;
                }
@@ -875,26 +956,34 @@ int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
        /*
         * Make sure the target type has the right type and properties.
         */
-       ctx_cur_field_type_id = bt_ctf_field_type_get_type_id(
+       ctx_cur_field_type_id = bt_field_type_get_type_id(
                ctx->cur_field_type);
-       target_type_id = bt_ctf_field_type_get_type_id(target_type);
-
-       if (ctx_cur_field_type_id == CTF_TYPE_VARIANT) {
-               if (target_type_id != CTF_TYPE_ENUM) {
-                       _printf_error("Variant type's tag field type is not an enumeration\n");
+       target_type_id = bt_field_type_get_type_id(target_type);
+
+       switch (ctx_cur_field_type_id) {
+       case BT_FIELD_TYPE_ID_VARIANT:
+               if (target_type_id != BT_FIELD_TYPE_ID_ENUM) {
+                       BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
+                               "tag-ft-addr=%p, tag-ft-id=%s",
+                               target_type,
+                               bt_common_field_type_id_string(target_type_id));
                        ret = -1;
                        goto end;
                }
-       } else if (ctx_cur_field_type_id == CTF_TYPE_SEQUENCE) {
-               if (target_type_id != CTF_TYPE_INTEGER ||
-                               bt_ctf_field_type_integer_get_signed(
-                                       target_type)) {
-                       _printf_error("Sequence type's length field type is not an unsigned integer\n");
+               break;
+       case BT_FIELD_TYPE_ID_SEQUENCE:
+               if (target_type_id != BT_FIELD_TYPE_ID_INTEGER ||
+                               bt_field_type_integer_is_signed(target_type)) {
+                       BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
+                               "length-ft-addr=%p, length-ft-id=%s",
+                               target_type,
+                               bt_common_field_type_id_string(target_type_id));
                        ret = -1;
                        goto end;
                }
-       } else {
-               assert(BT_FALSE);
+               break;
+       default:
+               abort();
        }
 
 end:
@@ -908,27 +997,34 @@ end:
  * `type` is owned by the caller.
  */
 static
-int resolve_sequence_or_variant_type(struct bt_ctf_field_type *type,
+int resolve_sequence_or_variant_type(struct bt_field_type *type,
                struct resolve_context *ctx)
 {
        int ret = 0;
        const char *pathstr;
-       int type_id = bt_ctf_field_type_get_type_id(type);
-       struct bt_ctf_field_path *target_field_path = NULL;
-       struct bt_ctf_field_type *target_type = NULL;
+       enum bt_field_type_id type_id = bt_field_type_get_type_id(type);
+       struct bt_field_path *target_field_path = NULL;
+       struct bt_field_type *target_type = NULL;
+       GString *target_field_path_pretty = NULL;
+       const char *target_field_path_pretty_str;
+
 
        /* Get path string */
        switch (type_id) {
-       case CTF_TYPE_SEQUENCE:
+       case BT_FIELD_TYPE_ID_SEQUENCE:
                pathstr =
-                       bt_ctf_field_type_sequence_get_length_field_name(type);
+                       bt_field_type_sequence_get_length_field_name(type);
                break;
-       case CTF_TYPE_VARIANT:
+       case BT_FIELD_TYPE_ID_VARIANT:
                pathstr =
-                       bt_ctf_field_type_variant_get_tag_name(type);
+                       bt_field_type_variant_get_tag_name(type);
                break;
        default:
-               assert(BT_FALSE);
+               abort();
+       }
+
+       if (!pathstr) {
+               BT_LOGW_STR("Cannot get path string.");
                ret = -1;
                goto end;
        }
@@ -936,55 +1032,77 @@ int resolve_sequence_or_variant_type(struct bt_ctf_field_type *type,
        /* Get target field path out of path string */
        target_field_path = pathstr_to_field_path(pathstr, ctx);
        if (!target_field_path) {
-               _printf_error("Cannot get target field path for path string \"%s\"\n",
-                       pathstr);
+               BT_LOGW("Cannot get target field path for path string: "
+                       "path=\"%s\"", pathstr);
                ret = -1;
                goto end;
        }
 
+       target_field_path_pretty = bt_field_path_string(target_field_path);
+       target_field_path_pretty_str =
+               target_field_path_pretty ? target_field_path_pretty->str : NULL;
+
        /* Get target field type */
        target_type = field_path_to_field_type(target_field_path, ctx);
        if (!target_type) {
-               _printf_error("Cannot get target field type for path string \"%s\"\n",
-                       pathstr);
+               BT_LOGW("Cannot get target field type for path string: "
+                       "path=\"%s\", target-field-path=\"%s\"",
+                       pathstr, target_field_path_pretty_str);
                ret = -1;
                goto end;
        }
 
        ret = validate_target_field_path(target_field_path, target_type, ctx);
        if (ret) {
-               _printf_error("Invalid target field path for path string \"%s\"\n",
-                       pathstr);
+               BT_LOGW("Invalid target field path for path string: "
+                       "path=\"%s\", target-field-path=\"%s\"",
+                       pathstr, target_field_path_pretty_str);
                goto end;
        }
 
        /* Set target field path and target field type */
-       if (type_id == CTF_TYPE_SEQUENCE) {
-               ret = bt_ctf_field_type_sequence_set_length_field_path(
+       switch (type_id) {
+       case BT_FIELD_TYPE_ID_SEQUENCE:
+               ret = bt_field_type_sequence_set_length_field_path(
                        type, target_field_path);
                if (ret) {
-                       _printf_error("Cannot set sequence field type's length field path\n");
+                       BT_LOGW("Cannot set sequence field type's length field path: "
+                               "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
+                               ret, type, pathstr,
+                               target_field_path_pretty_str);
                        goto end;
                }
-       } else if (type_id == CTF_TYPE_VARIANT) {
-               ret = bt_ctf_field_type_variant_set_tag_field_path(
+               break;
+       case BT_FIELD_TYPE_ID_VARIANT:
+               ret = bt_field_type_variant_set_tag_field_path(
                        type, target_field_path);
                if (ret) {
-                       _printf_error("Cannot set variant field type's tag field path\n");
+                       BT_LOGW("Cannot set varaint field type's tag field path: "
+                               "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
+                               ret, type, pathstr,
+                               target_field_path_pretty_str);
                        goto end;
                }
 
-               ret = bt_ctf_field_type_variant_set_tag_field_type(
+               ret = bt_field_type_variant_set_tag_field_type(
                        type, target_type);
                if (ret) {
-                       _printf_error("Cannot set variant field type's tag field type\n");
+                       BT_LOGW("Cannot set varaint field type's tag field type: "
+                               "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
+                               ret, type, pathstr,
+                               target_field_path_pretty_str);
                        goto end;
                }
-       } else {
-               assert(BT_FALSE);
+               break;
+       default:
+               abort();
        }
 
 end:
+       if (target_field_path_pretty) {
+               g_string_free(target_field_path_pretty, TRUE);
+       }
+
        BT_PUT(target_field_path);
        BT_PUT(target_type);
        return ret;
@@ -996,26 +1114,27 @@ end:
  * `type` is owned by the caller.
  */
 static
-int resolve_type(struct bt_ctf_field_type *type, struct resolve_context *ctx)
+int resolve_type(struct bt_field_type *type, struct resolve_context *ctx)
 {
        int ret = 0;
-       int type_id;
+       enum bt_field_type_id type_id;
 
        if (!type) {
                /* Type is not available; still valid */
                goto end;
        }
 
-       type_id = bt_ctf_field_type_get_type_id(type);
+       type_id = bt_field_type_get_type_id(type);
        ctx->cur_field_type = type;
 
        /* Resolve sequence/variant field type */
        switch (type_id) {
-       case CTF_TYPE_SEQUENCE:
-       case CTF_TYPE_VARIANT:
+       case BT_FIELD_TYPE_ID_SEQUENCE:
+       case BT_FIELD_TYPE_ID_VARIANT:
                ret = resolve_sequence_or_variant_type(type, ctx);
                if (ret) {
-                       _printf_error("Cannot resolve sequence or variant field type's length/tag\n");
+                       BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
+                               "ret=%d, ft-addr=%p", ret, type);
                        goto end;
                }
                break;
@@ -1025,50 +1144,56 @@ int resolve_type(struct bt_ctf_field_type *type, struct resolve_context *ctx)
 
        /* Recurse into compound types */
        switch (type_id) {
-       case CTF_TYPE_STRUCT:
-       case CTF_TYPE_VARIANT:
-       case CTF_TYPE_SEQUENCE:
-       case CTF_TYPE_ARRAY:
+       case BT_FIELD_TYPE_ID_STRUCT:
+       case BT_FIELD_TYPE_ID_VARIANT:
+       case BT_FIELD_TYPE_ID_SEQUENCE:
+       case BT_FIELD_TYPE_ID_ARRAY:
        {
-               int field_count, f_index;
+               int64_t field_count, f_index;
 
                ret = type_stack_push(ctx->type_stack, type);
                if (ret) {
-                       _printf_error("Cannot push field type on type stack\n");
-                       _printf_error("  Stack size: %zu\n",
-                               type_stack_size(ctx->type_stack));
+                       BT_LOGW("Cannot push field type on context's stack: "
+                               "ft-addr=%p", type);
                        goto end;
                }
 
-               field_count = bt_ctf_field_type_get_field_count(type);
+               field_count = bt_field_type_get_field_count(type);
                if (field_count < 0) {
-                       _printf_error("Cannot get field type field count\n");
+                       BT_LOGW("Cannot get field type's field count: "
+                               "ret=%" PRId64 ", ft-addr=%p",
+                               field_count, type);
                        ret = field_count;
                        goto end;
                }
 
                for (f_index = 0; f_index < field_count; f_index++) {
-                       struct bt_ctf_field_type *child_type =
-                               bt_ctf_field_type_get_field_at_index(type,
+                       struct bt_field_type *child_type =
+                               bt_field_type_borrow_field_at_index(type,
                                        f_index);
 
                        if (!child_type) {
-                               _printf_error("Cannot get field type field at index %d/%d\n",
-                                       f_index, field_count);
+                               BT_LOGW("Cannot get field type's child field: "
+                                       "ft-addr=%p, index=%" PRId64 ", "
+                                       "count=%" PRId64, type, f_index,
+                                       field_count);
                                ret = -1;
                                goto end;
                        }
 
-                       if (type_id == CTF_TYPE_ARRAY ||
-                                       type_id == CTF_TYPE_SEQUENCE) {
+                       if (type_id == BT_FIELD_TYPE_ID_ARRAY||
+                                       type_id == BT_FIELD_TYPE_ID_SEQUENCE) {
                                type_stack_peek(ctx->type_stack)->index = -1;
                        } else {
                                type_stack_peek(ctx->type_stack)->index =
                                        f_index;
                        }
 
+                       BT_LOGV("Resolving field type's child field type: "
+                               "parent-ft-addr=%p, child-ft-addr=%p, "
+                               "index=%" PRId64 ", count=%" PRId64,
+                               type, child_type, f_index, field_count);
                        ret = resolve_type(child_type, ctx);
-                       BT_PUT(child_type);
                        if (ret) {
                                goto end;
                        }
@@ -1089,28 +1214,28 @@ end:
  * Resolves the root field type corresponding to the scope `root_scope`.
  */
 static
-int resolve_root_type(enum bt_ctf_scope root_scope, struct resolve_context *ctx)
+int resolve_root_type(enum bt_scope root_scope, struct resolve_context *ctx)
 {
        int ret;
 
-       assert(type_stack_size(ctx->type_stack) == 0);
+       BT_ASSERT(type_stack_size(ctx->type_stack) == 0);
        ctx->root_scope = root_scope;
        ret = resolve_type(get_type_from_ctx(ctx, root_scope), ctx);
-       ctx->root_scope = BT_CTF_SCOPE_UNKNOWN;
+       ctx->root_scope = BT_SCOPE_UNKNOWN;
 
        return ret;
 }
 
 BT_HIDDEN
-int bt_ctf_resolve_types(
+int bt_resolve_types(
                struct bt_value *environment,
-               struct bt_ctf_field_type *packet_header_type,
-               struct bt_ctf_field_type *packet_context_type,
-               struct bt_ctf_field_type *event_header_type,
-               struct bt_ctf_field_type *stream_event_ctx_type,
-               struct bt_ctf_field_type *event_context_type,
-               struct bt_ctf_field_type *event_payload_type,
-               enum bt_ctf_resolve_flag flags)
+               struct bt_field_type *packet_header_type,
+               struct bt_field_type *packet_context_type,
+               struct bt_field_type *event_header_type,
+               struct bt_field_type *stream_event_ctx_type,
+               struct bt_field_type *event_context_type,
+               struct bt_field_type *event_payload_type,
+               enum bt_resolve_flag flags)
 {
        int ret = 0;
        struct resolve_context ctx = {
@@ -1123,71 +1248,89 @@ int bt_ctf_resolve_types(
                        event_context_type,
                        event_payload_type,
                },
-               .root_scope = BT_CTF_SCOPE_UNKNOWN,
+               .root_scope = BT_SCOPE_UNKNOWN,
        };
 
+       BT_LOGV("Resolving field types: "
+               "packet-header-ft-addr=%p, "
+               "packet-context-ft-addr=%p, "
+               "event-header-ft-addr=%p, "
+               "stream-event-context-ft-addr=%p, "
+               "event-context-ft-addr=%p, "
+               "event-payload-ft-addr=%p",
+               packet_header_type, packet_context_type, event_header_type,
+               stream_event_ctx_type, event_context_type, event_payload_type);
+
        /* Initialize type stack */
        ctx.type_stack = type_stack_create();
        if (!ctx.type_stack) {
-               printf_error("Cannot create type stack\n");
+               BT_LOGE_STR("Cannot create field type stack.");
                ret = -1;
                goto end;
        }
 
        /* Resolve packet header type */
-       if (flags & BT_CTF_RESOLVE_FLAG_PACKET_HEADER) {
-               ret = resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER, &ctx);
+       if (flags & BT_RESOLVE_FLAG_PACKET_HEADER) {
+               ret = resolve_root_type(BT_SCOPE_TRACE_PACKET_HEADER, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve trace packet header type\n");
+                       BT_LOGW("Cannot resolve trace packet header field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
        /* Resolve packet context type */
-       if (flags & BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT) {
-               ret = resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT, &ctx);
+       if (flags & BT_RESOLVE_FLAG_PACKET_CONTEXT) {
+               ret = resolve_root_type(BT_SCOPE_STREAM_PACKET_CONTEXT, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve stream packet context type\n");
+                       BT_LOGW("Cannot resolve stream packet context field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
        /* Resolve event header type */
-       if (flags & BT_CTF_RESOLVE_FLAG_EVENT_HEADER) {
-               ret = resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER, &ctx);
+       if (flags & BT_RESOLVE_FLAG_EVENT_HEADER) {
+               ret = resolve_root_type(BT_SCOPE_STREAM_EVENT_HEADER, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve stream event header type\n");
+                       BT_LOGW("Cannot resolve stream event header field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
        /* Resolve stream event context type */
-       if (flags & BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX) {
-               ret = resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT, &ctx);
+       if (flags & BT_RESOLVE_FLAG_STREAM_EVENT_CTX) {
+               ret = resolve_root_type(BT_SCOPE_STREAM_EVENT_CONTEXT, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve stream event context type\n");
+                       BT_LOGW("Cannot resolve stream event context field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
        /* Resolve event context type */
-       if (flags & BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT) {
-               ret = resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT, &ctx);
+       if (flags & BT_RESOLVE_FLAG_EVENT_CONTEXT) {
+               ret = resolve_root_type(BT_SCOPE_EVENT_CONTEXT, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve event context type\n");
+                       BT_LOGW("Cannot resolve event context field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
        /* Resolve event payload type */
-       if (flags & BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD) {
-               ret = resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS, &ctx);
+       if (flags & BT_RESOLVE_FLAG_EVENT_PAYLOAD) {
+               ret = resolve_root_type(BT_SCOPE_EVENT_FIELDS, &ctx);
                if (ret) {
-                       _printf_error("Cannot resolve event payload type\n");
+                       BT_LOGW("Cannot resolve event payload field type: "
+                               "ret=%d", ret);
                        goto end;
                }
        }
 
+       BT_LOGV_STR("Resolved field types.");
+
 end:
        type_stack_destroy(ctx.type_stack);
 
This page took 0.040608 seconds and 4 git commands to generate.