summaryrefslogtreecommitdiff
path: root/gdb/target-descriptions.c
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2020-06-09 23:08:54 +0100
committerAndrew Burgess <andrew.burgess@embecosm.com>2020-06-23 22:17:20 +0100
commitcaa7fd04f652c00caf5c84d486c622cb1ffaf6c9 (patch)
treeb8aa24886889a95510d4b0cb5c7caf041ea36453 /gdb/target-descriptions.c
parentfbf42f4e6d04745fe615dce1abd0190b78e368a6 (diff)
downloadbinutils-gdb-caa7fd04f652c00caf5c84d486c622cb1ffaf6c9.tar.gz
gdb: New maintenance command to print XML target description
This commit adds a new maintenance command that dumps the current target description as an XML document. This is a maintenance command as I currently only see this being useful for GDB developers, or for people debugging a new remote target. By default the command will print whatever the current target description is, whether this was delivered by the remote, loaded by the user from a file, or if it is a built in target within GDB. The command can also take an optional filename argument. In this case GDB loads a target description from the file, and then reprints it. This could be useful for testing GDB's parsing of target descriptions, or to check that GDB can successfully parse a particular XML description. It is worth noting that the XML description printed will not be an exact copy of the document fed into GDB. For example this minimal input file: <target> <feature name="abc"> <reg name="r1" bitsize="32"/> </feature> </target> Will produce this output: (gdb) maint print xml-tdesc path/to/file.xml <?xml version="1.0"?> <!DOCTYPE target SYSTEM "gdb-target.dtd"> <target> <feature name="abc"> <reg name="r1" bitsize="32" type="int" regnum="0"/> </feature> </target> Notice that GDB filled in both the 'type' and 'regnum' fields of the <reg>. I think this is actually a positive as it means we get to really understand how GDB processed the document, if GDB made some assumptions that differ to those the user expected then hopefully this will bring those issues to the users attention. To implement this I have tweaked the output produced by the print_xml_feature which is defined within the gdbsupport/ directory. The changes I have made to this class are: 1. The <architecture>...</architecture> tags are now not produced if the architecture name is NULL. 2. The <osabi>...</osabi> tags get a newline at the end. 3. And, the whole XML document is indented using white space in a nested fashion (as in the example output above). I think that these changes should be fine, the print_xml_feature class is used: 1. In gdbserver to generate an XML document to send as the target description to GDB. 2. In GDB as part of a self-check function, a target_desc is converted to XML then parsed back into a target_desc. We then check the before and after target_desc objects are the same. 3. In the new 'maint print xml-tdesc' command. In all of these use cases adding the extra white space should be fine. gdbsupport/ChangeLog: * tdesc.cc (print_xml_feature::visit_pre): Use add_line to add output content, and call indent as needed in all overloaded variants. (print_xml_feature::visit_post): Likewise. (print_xml_feature::visit): Likewise. (print_xml_feature::add_line): Two new overloaded functions. * tdesc.h (print_xml_feature::indent): New member function. (print_xml_feature::add_line): Two new overloaded member functions. (print_xml_feature::m_depth): New member variable. gdb/ChangeLog: * target-descriptions.c (tdesc_architecture_name): Protect against NULL pointer dereference. (maint_print_xml_tdesc_cmd): New function. (_initialize_target_descriptions): Register new 'maint print xml-tdesc' command and give it the filename completer. * NEWS: Mention new 'maint print xml-tdesc' command. gdb/testsuite/ChangeLog: * gdb.xml/tdesc-reload.c: New file. * gdb.xml/tdesc-reload.exp: New file. * gdb.xml/maint-xml-dump-01.xml: New file. * gdb.xml/maint-xml-dump-02.xml: New file. * gdb.xml/maint-xml-dump.exp: New file. gdb/doc/ChangeLog: * gdb.texinfo (Maintenance Commands): Document new 'maint print xml-desc' command.
Diffstat (limited to 'gdb/target-descriptions.c')
-rw-r--r--gdb/target-descriptions.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 1937e7ca4a7..1abdf51ec72 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -662,7 +662,9 @@ tdesc_architecture (const struct target_desc *target_desc)
const char *
tdesc_architecture_name (const struct target_desc *target_desc)
{
- return target_desc->arch->printable_name;
+ if (target_desc->arch != NULL)
+ return target_desc->arch->printable_name;
+ return NULL;
}
/* See gdbsupport/tdesc.h. */
@@ -1755,6 +1757,36 @@ maint_print_c_tdesc_cmd (const char *args, int from_tty)
}
}
+/* Implement the maintenance print xml-tdesc command. */
+
+static void
+maint_print_xml_tdesc_cmd (const char *args, int from_tty)
+{
+ const struct target_desc *tdesc;
+
+ if (args == NULL)
+ {
+ /* Use the global target-supplied description, not the current
+ architecture's. This lets a GDB for one architecture generate XML
+ for another architecture's description, even though the gdbarch
+ initialization code will reject the new description. */
+ tdesc = current_target_desc;
+ }
+ else
+ {
+ /* Use the target description from the XML file. */
+ tdesc = file_read_description_xml (args);
+ }
+
+ if (tdesc == NULL)
+ error (_("There is no target description to print."));
+
+ std::string buf;
+ print_xml_feature v (&buf);
+ tdesc->accept (v);
+ puts_unfiltered (buf.c_str ());
+}
+
namespace selftests {
/* A reference target description, used for testing (see record_xml_tdesc). */
@@ -1892,6 +1924,11 @@ Print the current target description as a C source file."),
&maintenanceprintlist);
set_cmd_completer (cmd, filename_completer);
+ cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
+Print the current target description as an XML file."),
+ &maintenanceprintlist);
+ set_cmd_completer (cmd, filename_completer);
+
cmd = add_cmd ("xml-descriptions", class_maintenance,
maintenance_check_xml_descriptions, _("\
Check equality of GDB target descriptions and XML created descriptions.\n\