{
int ret = 0;
GQuark name_quark = g_quark_from_string(field_name);
+ GQuark underscore_name_quark;
struct structure_field *field;
+ GString *underscore_name = g_string_new(NULL);
+
+ if (!underscore_name) {
+ BT_LOGE_STR("Failed to allocate a GString.");
+ ret = -1;
+ goto end;
+ }
+
+ g_string_assign(underscore_name, "_");
+ g_string_append(underscore_name, field_name);
+ underscore_name_quark = g_quark_from_string(underscore_name->str);
/* Make sure structure does not contain a field of the same name */
if (g_hash_table_lookup_extended(field_name_to_index,
goto end;
}
+ if (g_hash_table_lookup_extended(field_name_to_index,
+ GUINT_TO_POINTER(underscore_name_quark), NULL, NULL)) {
+ BT_LOGW("Structure or variant field type already contains a field type with this name: "
+ "field-name=\"%s\"", underscore_name->str);
+ ret = -1;
+ goto end;
+ }
+
field = g_new0(struct structure_field, 1);
if (!field) {
BT_LOGE_STR("Failed to allocate one structure/variant field type field.");
g_hash_table_insert(field_name_to_index,
GUINT_TO_POINTER(name_quark),
GUINT_TO_POINTER(fields->len));
+ g_hash_table_insert(field_name_to_index,
+ GUINT_TO_POINTER(underscore_name_quark),
+ GUINT_TO_POINTER(fields->len));
g_ptr_array_add(fields, field);
BT_LOGV("Added structure/variant field type field: field-ft-addr=%p, "
"field-name=\"%s\"", field_type, field_name);
end:
+ g_string_free(underscore_name, TRUE);
return ret;
}
iter->u.name_quark = g_quark_try_string(name);
if (!iter->u.name_quark) {
- BT_LOGV("No such enumeration field type mapping name: "
- "ft-addr=%p, mapping-name=\"%s\"",
- type, name);
- goto error;
- }
-
- /* Advance iterator to first entry, or leave index at -1. */
- if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
- /* No entry found. */
- BT_LOGV("No such enumeration field type mapping name: "
- "ft-addr=%p, mapping-name=\"%s\"",
- type, name);
- goto error;
+ /*
+ * No results are possible, set the iterator's position at the
+ * end.
+ */
+ iter->index = iter->enumeration_type->entries->len;
}
-
return iter;
error:
bt_put(iter);
}
iter->u.signed_value = value;
-
- /* Advance iterator to first entry, or leave index at -1. */
- if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
- /* No entry found. */
- goto error;
- }
-
return iter;
error:
bt_put(iter);
bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
struct bt_ctf_field_type *type, uint64_t value)
{
- struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
-
- if (!type) {
- BT_LOGW_STR("Invalid parameter: field type is NULL.");
- goto error;
- }
+ struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
iter = bt_ctf_field_type_enumeration_find_mappings_type(
type, ITERATOR_BY_UNSIGNED_VALUE);
goto error;
}
iter->u.unsigned_value = value;
-
- /* Advance iterator to first entry, or leave index at -1. */
- if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
- /* No entry found. */
- goto error;
- }
-
return iter;
error:
bt_put(iter);
goto end;
}
+ if (iter->index == -1) {
+ BT_LOGW_STR("Invalid enumeration field type mapping iterator access: position=-1");
+ ret = -1;
+ goto end;
+ }
+
ret = bt_ctf_field_type_enumeration_get_mapping_signed(
&iter->enumeration_type->parent, iter->index,
mapping_name, range_begin, range_end);
goto end;
}
+ if (iter->index == -1) {
+ BT_LOGW_STR("Invalid enumeration field type mapping iterator access: position=-1");
+ ret = -1;
+ goto end;
+ }
+
ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(
&iter->enumeration_type->parent, iter->index,
mapping_name, range_begin, range_end);
goto end;
}
- if (bt_ctf_validate_identifier(field_name)) {
- BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
- "struct-ft-addr=%p, field-name=\"%s\"",
- type, field_name);
- ret = -1;
- goto end;
- }
-
if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
BT_LOGW("Invalid parameter: field type is not a structure field type: "
"addr=%p, ft-id=%s", type,
goto end;
}
- if (bt_ctf_validate_identifier(field_name)) {
- BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
- "variant-ft-addr=%p, field-name=\"%s\"",
- type, field_name);
- ret = -1;
- goto end;
- }
-
if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
BT_LOGW("Invalid parameter: field type is not a variant field type: "
"addr=%p, ft-id=%s", type,
}
iter = bt_ctf_field_enumeration_get_mappings(tag);
- if (!iter) {
+ ret = bt_ctf_field_type_enumeration_mapping_iterator_next(iter);
+ if (!iter || ret) {
BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: "
"enum-field-addr=%p", tag);
goto end;
return base_string;
}
+static
+void append_struct_variant_field_name(struct metadata_context *context,
+ const char *name)
+{
+ g_string_append_c(context->string, ' ');
+
+ if (bt_ctf_validate_identifier(name)) {
+ g_string_append_c(context->string, '_');
+ }
+
+ g_string_append(context->string, name);
+}
+
static
int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
struct metadata_context *context)
}
if (context->field_name->len) {
- g_string_append_printf(context->string, " %s",
+ append_struct_variant_field_name(context,
context->field_name->str);
g_string_assign(context->field_name, "");
}
}
if (context->field_name->len) {
- g_string_append_printf(context->string, " %s",
+ append_struct_variant_field_name(context,
context->field_name->str);
}
g_string_append(context->string, ";\n");
}
if (context->field_name->len) {
- g_string_append_printf(context->string, " %s;",
+ append_struct_variant_field_name(context,
context->field_name->str);
+ g_string_append_c(context->string, ';');
}
g_string_append_c(context->string, '\n');
}
if (context->field_name->len) {
- g_string_append_printf(context->string, " %s[%u]",
- context->field_name->str, array->length);
+ append_struct_variant_field_name(context,
+ context->field_name->str);
+
+ g_string_append_printf(context->string, "[%u]", array->length);
g_string_assign(context->field_name, "");
} else {
g_string_append_printf(context->string, "[%u]", array->length);
}
if (context->field_name->len) {
- g_string_append_printf(context->string, " %s[%s]",
- context->field_name->str,
+ append_struct_variant_field_name(context,
+ context->field_name->str);
+
+ g_string_append_printf(context->string, "[%s]",
sequence->length_field_name->str);
g_string_assign(context->field_name, "");
} else {