#include "xml-tdesc.h"
#include "osabi.h"
#include "filenames.h"
+#include <unordered_map>
+#include <string>
/* Maximum sizes.
This is just to catch obviously wrong values. */
then we will create unnecessary duplicate gdbarches. See
gdbarch_list_lookup_by_info. */
-struct tdesc_xml_cache
-{
- const char *xml_document;
- struct target_desc *tdesc;
-};
-typedef struct tdesc_xml_cache tdesc_xml_cache_s;
-DEF_VEC_O(tdesc_xml_cache_s);
-
-static VEC(tdesc_xml_cache_s) *xml_cache;
+static std::unordered_map<std::string, target_desc *> xml_cache;
/* Callback data for target description parsing. */
int next_regnum;
/* The struct or union we are currently parsing, or last parsed. */
- struct tdesc_type *current_type;
+ tdesc_type_with_fields *current_type;
/* The byte size of the current struct/flags type, if specified. Zero
if not specified. Flags values must specify a size. */
struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
int ix = 0, length;
- char *name, *group, *type;
+ char *name, *group;
+ const char *type;
int bitsize, regnum, save_restore;
length = VEC_length (gdb_xml_value_s, attributes);
{
struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
char *id = (char *) xml_find_attribute (attributes, "id")->value;
- struct tdesc_type *type;
struct gdb_xml_value *attr;
- type = tdesc_create_struct (data->current_feature, id);
- data->current_type = type;
+ tdesc_type_with_fields *type_with_fields
+ = tdesc_create_struct (data->current_feature, id);
+ data->current_type = type_with_fields;
data->current_type_size = 0;
attr = xml_find_attribute (attributes, "size");
_("Struct size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- tdesc_set_struct_size (type, size);
+ tdesc_set_struct_size (type_with_fields, size);
data->current_type_size = size;
}
}
char *id = (char *) xml_find_attribute (attributes, "id")->value;
ULONGEST size = * (ULONGEST *)
xml_find_attribute (attributes, "size")->value;
- struct tdesc_type *type;
if (size > MAX_FIELD_SIZE)
{
_("Flags size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- type = tdesc_create_flags (data->current_feature, id, size);
- data->current_type = type;
+ data->current_type = tdesc_create_flags (data->current_feature, id, size);
data->current_type_size = size;
}
char *id = (char *) xml_find_attribute (attributes, "id")->value;
int size = * (ULONGEST *)
xml_find_attribute (attributes, "size")->value;
- struct tdesc_type *type;
if (size > MAX_FIELD_SIZE)
{
_("Enum size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- type = tdesc_create_enum (data->current_feature, id, size);
- data->current_type = type;
+ data->current_type = tdesc_create_enum (data->current_feature, id, size);
data->current_type_size = 0;
}
if (start != -1)
{
- struct tdesc_type *t = data->current_type;
+ tdesc_type_with_fields *t = data->current_type;
/* Older versions of gdb can't handle elided end values.
Stick with that for now, to help ensure backward compatibility.
tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
void *fetcher_baton)
{
- struct cleanup *back_to, *result_cleanup;
struct tdesc_parsing_data data;
- struct tdesc_xml_cache *cache;
- char *expanded_text;
- int ix;
/* Expand all XInclude directives. */
- expanded_text = xml_process_xincludes (_("target description"),
- document, fetcher, fetcher_baton, 0);
- if (expanded_text == NULL)
+ std::string expanded_text;
+
+ if (!xml_process_xincludes (expanded_text,
+ _("target description"),
+ document, fetcher, fetcher_baton, 0))
{
warning (_("Could not load XML target description; ignoring"));
return NULL;
}
/* Check for an exact match in the list of descriptions we have
- previously parsed. strcmp is a slightly inefficient way to
- do this; an SHA-1 checksum would work as well. */
- for (ix = 0; VEC_iterate (tdesc_xml_cache_s, xml_cache, ix, cache); ix++)
- if (strcmp (cache->xml_document, expanded_text) == 0)
- {
- xfree (expanded_text);
- return cache->tdesc;
- }
-
- back_to = make_cleanup (null_cleanup, NULL);
+ previously parsed. */
+ const auto it = xml_cache.find (expanded_text);
+ if (it != xml_cache.end ())
+ return it->second;
memset (&data, 0, sizeof (struct tdesc_parsing_data));
data.tdesc = allocate_target_description ();
- result_cleanup = make_cleanup_free_target_description (data.tdesc);
- make_cleanup (xfree, expanded_text);
+ struct cleanup *result_cleanup
+ = make_cleanup_free_target_description (data.tdesc);
if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
- tdesc_elements, expanded_text, &data) == 0)
+ tdesc_elements, expanded_text.c_str (), &data) == 0)
{
/* Parsed successfully. */
- struct tdesc_xml_cache new_cache;
-
- new_cache.xml_document = expanded_text;
- new_cache.tdesc = data.tdesc;
- VEC_safe_push (tdesc_xml_cache_s, xml_cache, &new_cache);
+ xml_cache.emplace (std::move (expanded_text), data.tdesc);
discard_cleanups (result_cleanup);
- do_cleanups (back_to);
return data.tdesc;
}
else
{
warning (_("Could not load XML target description; ignoring"));
- do_cleanups (back_to);
+ do_cleanups (result_cleanup);
return NULL;
}
}
const struct target_desc *
file_read_description_xml (const char *filename)
{
- struct target_desc *tdesc;
- char *tdesc_str;
- struct cleanup *back_to;
-
- tdesc_str = xml_fetch_content_from_file (filename, NULL);
+ gdb::unique_xmalloc_ptr<char> tdesc_str
+ = xml_fetch_content_from_file (filename, NULL);
if (tdesc_str == NULL)
{
warning (_("Could not open \"%s\""), filename);
return NULL;
}
- back_to = make_cleanup (xfree, tdesc_str);
-
- tdesc = tdesc_parse_xml (tdesc_str, xml_fetch_content_from_file,
- (void *) ldirname (filename).c_str ());
- do_cleanups (back_to);
-
- return tdesc;
+ return tdesc_parse_xml (tdesc_str.get (), xml_fetch_content_from_file,
+ (void *) ldirname (filename).c_str ());
}
/* Read a string representation of available features from the target,
is "target.xml". Other calls may be performed for the DTD or
for <xi:include>. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
fetch_available_features_from_target (const char *name, void *baton_)
{
struct target_ops *ops = (struct target_ops *) baton_;
const struct target_desc *
target_read_description_xml (struct target_ops *ops)
{
- struct target_desc *tdesc;
- char *tdesc_str;
- struct cleanup *back_to;
-
- tdesc_str = fetch_available_features_from_target ("target.xml", ops);
+ gdb::unique_xmalloc_ptr<char> tdesc_str
+ = fetch_available_features_from_target ("target.xml", ops);
if (tdesc_str == NULL)
return NULL;
- back_to = make_cleanup (xfree, tdesc_str);
- tdesc = tdesc_parse_xml (tdesc_str,
- fetch_available_features_from_target,
- ops);
- do_cleanups (back_to);
-
- return tdesc;
+ return tdesc_parse_xml (tdesc_str.get (),
+ fetch_available_features_from_target,
+ ops);
}
/* Fetches an XML target description using OPS, processing
includes, but not parsing it. Used to dump whole tdesc
as a single XML file. */
-char *
+gdb::optional<std::string>
target_fetch_description_xml (struct target_ops *ops)
{
#if !defined(HAVE_LIBEXPAT)
"disabled at compile time"));
}
- return NULL;
+ return {};
#else
- struct target_desc *tdesc;
- char *tdesc_str;
- char *expanded_text;
- struct cleanup *back_to;
-
- tdesc_str = fetch_available_features_from_target ("target.xml", ops);
+ gdb::unique_xmalloc_ptr<char>
+ tdesc_str = fetch_available_features_from_target ("target.xml", ops);
if (tdesc_str == NULL)
- return NULL;
+ return {};
- back_to = make_cleanup (xfree, tdesc_str);
- expanded_text = xml_process_xincludes (_("target description"),
- tdesc_str,
- fetch_available_features_from_target, ops, 0);
- do_cleanups (back_to);
- if (expanded_text == NULL)
+ std::string output;
+ if (!xml_process_xincludes (output,
+ _("target description"),
+ tdesc_str.get (),
+ fetch_available_features_from_target, ops, 0))
{
warning (_("Could not load XML target description; ignoring"));
- return NULL;
+ return {};
}
-
- return expanded_text;
+ return output;
#endif
}