Add logging to validation and serialization failure paths
[babeltrace.git] / formats / ctf / ir / fields.c
index 58f69fd43cbd5e674412e05c7a4f11afe706834f..04fba5cf22492d0d0534668cf325d04edec07740 100644 (file)
@@ -788,6 +788,27 @@ end:
        return current_field;
 }
 
+struct bt_ctf_field *bt_ctf_field_variant_get_tag(
+               struct bt_ctf_field *variant_field)
+{
+       struct bt_ctf_field *tag = NULL;
+       struct bt_ctf_field_variant *variant;
+
+       if (!variant_field ||
+                       bt_ctf_field_type_get_type_id(variant_field->type) !=
+                       BT_CTF_TYPE_ID_VARIANT) {
+               goto end;
+       }
+
+       variant = container_of(variant_field, struct bt_ctf_field_variant,
+                       parent);
+       if (variant->tag) {
+               tag = bt_get(variant->tag);
+       }
+end:
+       return tag;
+}
+
 struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        struct bt_ctf_field *field)
 {
@@ -994,7 +1015,7 @@ int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
        }
 
        size = integer_type->declaration.len;
-       max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
+       max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1;
        if (value > max_value) {
                ret = -1;
                goto end;
@@ -1571,6 +1592,15 @@ int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
        for (i = 0; i < structure->fields->len; i++) {
                ret = bt_ctf_field_validate(structure->fields->pdata[i]);
                if (ret) {
+                       const char *name;
+                       struct bt_ctf_field_type *field_type =
+                                       bt_ctf_field_get_type(field);
+
+                       (void) bt_ctf_field_type_structure_get_field(field_type,
+                                       &name, NULL, i);
+                       fprintf(stderr, "Field %s failed validation\n",
+                                       name ? name : "NULL");
+                       bt_put(field_type);
                        goto end;
                }
        }
@@ -1611,6 +1641,7 @@ int bt_ctf_field_array_validate(struct bt_ctf_field *field)
        for (i = 0; i < array->elements->len; i++) {
                ret = bt_ctf_field_validate(array->elements->pdata[i]);
                if (ret) {
+                       fprintf(stderr, "Failed to validate sequence field #%zu\n", i);
                        goto end;
                }
        }
@@ -1634,6 +1665,7 @@ int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
        for (i = 0; i < sequence->elements->len; i++) {
                ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
                if (ret) {
+                       fprintf(stderr, "Failed to validate sequence field #%zu\n", i);
                        goto end;
                }
        }
@@ -1909,6 +1941,15 @@ int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
 
                ret = bt_ctf_field_serialize(field, pos);
                if (ret) {
+                       const char *name;
+                       struct bt_ctf_field_type *field_type =
+                                       bt_ctf_field_get_type(field);
+
+                       (void) bt_ctf_field_type_structure_get_field(field_type,
+                                       &name, NULL, i);
+                       fprintf(stderr, "Field %s failed to serialize\n",
+                                       name ? name : "NULL");
+                       bt_put(field_type);
                        break;
                }
        }
@@ -1939,6 +1980,7 @@ int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
                ret = bt_ctf_field_serialize(
                        g_ptr_array_index(array->elements, i), pos);
                if (ret) {
+                       fprintf(stderr, "Failed to serialize array element #%zu\n", i);
                        goto end;
                }
        }
@@ -1959,6 +2001,7 @@ int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
                ret = bt_ctf_field_serialize(
                        g_ptr_array_index(sequence->elements, i), pos);
                if (ret) {
+                       fprintf(stderr, "Failed to serialize sequence element #%zu\n", i);
                        goto end;
                }
        }
@@ -2339,12 +2382,19 @@ void bt_ctf_field_sequence_freeze(struct bt_ctf_field *field)
 BT_HIDDEN
 void bt_ctf_field_freeze(struct bt_ctf_field *field)
 {
+       enum bt_ctf_type_id type_id;
+
        if (!field) {
                goto end;
        }
 
-       field_freeze_funcs[bt_ctf_field_get_type_id(field)](field);
+       type_id = bt_ctf_field_get_type_id(field);
+       if (type_id <= BT_CTF_TYPE_ID_UNKNOWN ||
+                       type_id >= BT_CTF_NR_TYPE_IDS) {
+               goto end;
+       }
 
+       field_freeze_funcs[type_id](field);
 end:
        return;
 }
This page took 0.025314 seconds and 4 git commands to generate.