From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18953 invoked by alias); 18 Apr 2018 21:26:49 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 18935 invoked by uid 89); 18 Apr 2018 21:26:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.0 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: EUR01-VE1-obe.outbound.protection.outlook.com Received: from mail-ve1eur01on0049.outbound.protection.outlook.com (HELO EUR01-VE1-obe.outbound.protection.outlook.com) (104.47.1.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Apr 2018 21:26:45 +0000 Received: from DB6PR0802MB2133.eurprd08.prod.outlook.com (10.172.226.148) by DB6PR0802MB2501.eurprd08.prod.outlook.com (10.172.251.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.696.13; Wed, 18 Apr 2018 21:26:41 +0000 Received: from DB6PR0802MB2133.eurprd08.prod.outlook.com ([fe80::a1b5:86be:9e75:2105]) by DB6PR0802MB2133.eurprd08.prod.outlook.com ([fe80::a1b5:86be:9e75:2105%14]) with mapi id 15.20.0696.013; Wed, 18 Apr 2018 21:26:41 +0000 From: Alan Hayward To: Simon Marchi CC: "gdb-patches@sourceware.org" , nd Subject: Re: [PATCH v5 6/8] Create xml from target descriptions Date: Wed, 18 Apr 2018 21:26:00 -0000 Message-ID: <85E2DDC7-231F-448A-96F3-C68D6199D050@arm.com> References: <20180410143337.71768-1-alan.hayward@arm.com> <20180410143337.71768-7-alan.hayward@arm.com> <4cde060f-100c-2293-68a3-dee0ffff707a@simark.ca> In-Reply-To: <4cde060f-100c-2293-68a3-dee0ffff707a@simark.ca> authentication-results: simark.ca; dkim=none (message not signed) header.d=none;simark.ca; dmarc=none action=none header.from=arm.com; x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DB6PR0802MB2501;7:NccngFoNkQaL6D55cQr0ZQC383FrF4U8IsGmr80OhIgWvI/w6UbsE2ilA3sQx2295iSkIMoR61NhU4WPnT1o4wY805qcrxnX71OEzPR2WgBjpcYKIxjiMqE+Uw9G6RdhdFE3Zg0qfflaY3Zsb7pmsSpsUpdvQKY6UDnOgsArL8mF6Rr+3xuMbqAEG3oQQYUZn/To8s2UuLXWv9gp8xXse665KmGHbHVckQNGWjbQBfD4koF9gFTX3aXjrRwJDe39 x-ms-exchange-antispam-srfa-diagnostics: SOS; x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020);SRVR:DB6PR0802MB2501; x-ms-traffictypediagnostic: DB6PR0802MB2501: nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(131327999870524); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3231232)(944501368)(52105095)(3002001)(10201501046)(93006095)(93001095)(6055026)(6041310)(20161123558120)(20161123562045)(20161123560045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:DB6PR0802MB2501;BCL:0;PCL:0;RULEID:;SRVR:DB6PR0802MB2501; x-forefront-prvs: 06469BCC91 x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(396003)(39860400002)(366004)(376002)(39380400002)(8676002)(36756003)(5660300001)(53936002)(6246003)(6512007)(53946003)(66066001)(229853002)(81166006)(50226002)(82746002)(8936002)(14454004)(26005)(83716003)(316002)(54906003)(102836004)(5250100002)(4326008)(305945005)(86362001)(53546011)(6506007)(3846002)(186003)(99286004)(6116002)(33656002)(76176011)(476003)(446003)(11346002)(2616005)(2900100001)(6486002)(25786009)(7736002)(6916009)(478600001)(72206003)(2906002)(6436002)(579004)(559001);DIR:OUT;SFP:1101;SCL:1;SRVR:DB6PR0802MB2501;H:DB6PR0802MB2133.eurprd08.prod.outlook.com;FPR:;SPF:None;LANG:en;MLV:sfv; x-microsoft-antispam-message-info: Efv/c8cTJHQjUm4MAHh1mCkVJLuXRQHkMjGUcEX/rd6W4Gl6q4nDwNmqAp1x/E/Z4lQ94o5UTRHQRqPZUJS9pG0u0Lvfi4666deMZ939rZJfXVcp9NjQyfFjAkQbZtC6MTURvbKVX7Yw2up+qvnwFvcniI8XYH8MFuzGxwYPnfcQJZntnqTHxRMUFp9V3yLv spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-ID: <39CBC5066000A7449C2A35D840F5EA17@eurprd08.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: f09cf017-406b-493c-5b36-08d5a57313e5 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-Network-Message-Id: f09cf017-406b-493c-5b36-08d5a57313e5 X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Apr 2018 21:26:41.4694 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0802MB2501 X-IsSubscribed: yes X-SW-Source: 2018-04/txt/msg00377.txt.bz2 > On 18 Apr 2018, at 03:43, Simon Marchi wrote: >=20 > Hi Alan, >=20 > The patch looks good to me. I noted a few formatting/minor comments. Thanks! Updated as requested and pushed. New version included at the bottom for ref= erence. >=20 >> diff --git a/gdb/common/tdesc.c b/gdb/common/tdesc.c >> index b9e9ddb3fa..b4bd7bfb00 100644 >> --- a/gdb/common/tdesc.c >> +++ b/gdb/common/tdesc.c >> @@ -290,3 +290,111 @@ tdesc_add_enum_value (tdesc_type_with_fields *type= , int value, >> tdesc_predefined_type (TDESC_TYPE_INT32), >> value, -1); >> } >> + >> +void print_xml_feature::visit_pre (const tdesc_feature *e) >> +{ >> + string_appendf (*m_buffer, "\n", e->name.c_str= ()); >> +} >> + >> +void print_xml_feature::visit_post (const tdesc_feature *e) >> +{ >> + string_appendf (*m_buffer, "\n"); >> +} >> + >> +void print_xml_feature::visit (const tdesc_type_builtin *t) >> +{ >> + error (_("xml output is not supported type \"%s\"."), t->name.c_str (= )); >=20 > Just a nit, but it seems to be missing something like a "for" between "su= pported" and "type". >=20 >> +} >> + >> +void print_xml_feature::visit (const tdesc_type_vector *t) >> +{ >> + string_appendf (*m_buffer, "\n", >> + t->name.c_str (), t->element_type->name.c_str (), t->count); >> +} >> + >> +void print_xml_feature::visit (const tdesc_type_with_fields *t) >> +{ >> + struct tdesc_type_field *f; >> + const static char *types[] =3D { "struct", "union", "flags", "enum" }; >> + >> + gdb_assert (t->kind >=3D TDESC_TYPE_STRUCT && t->kind <=3D TDESC_TYPE= _ENUM); >> + >> + string_appendf (*m_buffer, >> + "<%s id=3D\"%s\"", types[t->kind - TDESC_TYPE_STRUCT], >> + t->name.c_str ()); >> + >> + switch (t->kind) >> + { >> + case TDESC_TYPE_STRUCT: >> + case TDESC_TYPE_FLAGS: >> + if (t->size > 0) >> + string_appendf (*m_buffer, " size=3D\"%d\"", t->size); >> + string_appendf (*m_buffer, ">\n"); >> + >> + for (const tdesc_type_field &f : t->fields) >> + { >> + string_appendf (*m_buffer, " > + if (f.start =3D=3D -1) >> + string_appendf (*m_buffer, "type=3D\"%s\"/>\n", >> + f.type->name.c_str ()); >> + else >> + string_appendf (*m_buffer, "start=3D\"%d\" end=3D\"%d\"/>\n", f.st= art, >> + f.end); >> + } >> + break; >> + >> + case TDESC_TYPE_ENUM: >> + string_appendf (*m_buffer, ">\n"); >> + for (const tdesc_type_field &f : t->fields) >> + string_appendf (*m_buffer, " \n", >> + f.name.c_str (), f.start); >> + break; >> + >> + case TDESC_TYPE_UNION: >> + string_appendf (*m_buffer, ">\n"); >> + for (const tdesc_type_field &f : t->fields) >> + string_appendf (*m_buffer, " \n", >> + f.name.c_str (), f.type->name.c_str ()); >> + break; >> + >> + default: >> + error (_("xml output is not supported type \"%s\"."), t->name.c_s= tr ()); >=20 > "supported for type" here too. >=20 >> + } >> + >> + string_appendf (*m_buffer, "\n", types[t->kind - TDESC_TYPE_STRU= CT]); >> +} >> + >> +void print_xml_feature::visit (const tdesc_reg *r) >> +{ >> + string_appendf (*m_buffer, >> + "> + r->name.c_str (), r->bitsize, r->type.c_str (), >> + r->target_regnum); >> + >> + if (r->group.length () > 0) >> + string_appendf (*m_buffer, " group=3D\"%s\"", r->group.c_str ()); >> + >> + if (r->save_restore =3D=3D 0) >> + string_appendf (*m_buffer, " save-restore=3D\"no\""); >> + >> + string_appendf (*m_buffer, "/>\n"); >> +} >> + >> +void print_xml_feature::visit_pre (const target_desc *e) >> +{ >> +#ifndef IN_PROCESS_AGENT >> + string_appendf (*m_buffer, "\n"); >> + string_appendf (*m_buffer, "\n"); >> + string_appendf (*m_buffer, "\n%s= \n", >> + tdesc_architecture_name (e)); >> + >> + const char *osabi =3D tdesc_osabi_name (e); >> + if (osabi !=3D nullptr) >> + string_appendf (*m_buffer, "%s", osabi); >> +#endif >> +} >> + >> +void print_xml_feature::visit_post (const target_desc *e) >> +{ >> + string_appendf (*m_buffer, "\n"); >> +} >> diff --git a/gdb/common/tdesc.h b/gdb/common/tdesc.h >> index 311341da0d..00eaada072 100644 >> --- a/gdb/common/tdesc.h >> +++ b/gdb/common/tdesc.h >> @@ -381,4 +381,33 @@ void tdesc_create_reg (struct tdesc_feature *featur= e, const char *name, >> int regnum, int save_restore, const char *group, >> int bitsize, const char *type); >>=20 >> +/* Return the tdesc in string XML format. */ >> + >> +const char *tdesc_get_features_xml (const target_desc *tdesc); >> + >> +/* Print target description as xml. */ >> + >> +class print_xml_feature : public tdesc_element_visitor >> +{ >> +public: >> + print_xml_feature (std::string *buffer_) >> + : m_buffer (buffer_) >> + {} >> + >> + ~print_xml_feature () >> + {} >=20 > You can remove the explicit destructor. >=20 >> diff --git a/gdb/gdbserver/tdesc.h b/gdb/gdbserver/tdesc.h >> index 197fb59127..2152b17947 100644 >> --- a/gdb/gdbserver/tdesc.h >> +++ b/gdb/gdbserver/tdesc.h >> @@ -27,7 +27,7 @@ >> /* A target description. Inherit from tdesc_feature so that target_desc >> can be used as tdesc_feature. */ >>=20 >> -struct target_desc >> +struct target_desc : tdesc_element >> { >> /* A vector of elements of register definitions that >> describe the inferior's register set. */ >> @@ -51,7 +51,7 @@ struct target_desc >>=20 >> It can be NULL, then, its content is got from the following three >> fields features, arch, and osabi in tdesc_get_features_xml. */ >> - const char *xmltarget =3D NULL; >> + mutable const char *xmltarget =3D NULL; >=20 > I think the comment above might need a bit of updating. >=20 >>=20 >> /* The value of element in the XML, replying GDB. */ >> const char *arch =3D NULL; >> @@ -73,6 +73,8 @@ public: >> return !(*this =3D=3D other); >> } >> #endif >> + >> + void accept (tdesc_element_visitor &v) const; >=20 > override >=20 >> }; >>=20 >> /* Copy target description SRC to DEST. */ >> @@ -89,8 +91,4 @@ void init_target_desc (struct target_desc *tdesc); >>=20 >> const struct target_desc *current_target_desc (void); >>=20 >> -#ifndef IN_PROCESS_AGENT >> -const char *tdesc_get_features_xml (struct target_desc *tdesc); >> -#endif >> - >> #endif /* TDESC_H */ >> diff --git a/gdb/regformats/regdat.sh b/gdb/regformats/regdat.sh >> index 18108d77eb..8f546fe276 100755 >> --- a/gdb/regformats/regdat.sh >> +++ b/gdb/regformats/regdat.sh >> @@ -163,7 +163,9 @@ done >>=20 >> echo >> echo "static const char *expedite_regs_${name}[] =3D { \"`echo ${expedit= e} | sed 's/,/", "/g'`\", 0 };" >> -if test "${xmltarget}" =3D x; then >> +if test "${feature}" !=3D x; then >> + echo "static const char *xmltarget_${name} =3D 0;" >> +elif test "${xmltarget}" =3D x; then >> if test "${xmlarch}" =3D x && test "${xmlosabi}" =3D x; then >> echo "static const char *xmltarget_${name} =3D 0;" >> else >> diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c >> index da2c1ce345..b96ef961a4 100644 >> --- a/gdb/target-descriptions.c >> +++ b/gdb/target-descriptions.c >> @@ -333,6 +333,8 @@ struct target_desc : tdesc_element >> /* The features associated with this target. */ >> std::vector features; >>=20 >> + mutable char *xmltarget =3D nullptr; >=20 > Can you add a comment for this member? Just to mention that this > is used for caching/memoization of the generated xml. >=20 >> + >> void accept (tdesc_element_visitor &v) const override >> { >> v.visit_pre (this); >> @@ -1667,6 +1669,21 @@ private: >> int m_next_regnum =3D 0; >> }; >>=20 >> +/* See common/tdesc.h. */ >> + >> +const char * >> +tdesc_get_features_xml (const target_desc *tdesc) >> +{ >> + if (tdesc->xmltarget =3D=3D nullptr) >> + { >> + std::string buffer ("@"); >> + print_xml_feature v (&buffer); >> + tdesc->accept (v); >> + tdesc->xmltarget =3D xstrdup (buffer.c_str ()); >> + } >> + return tdesc->xmltarget; >> +} >> + >> static void >> maint_print_c_tdesc_cmd (const char *args, int from_tty) >> { >> @@ -1739,6 +1756,39 @@ record_xml_tdesc (const char *xml_file, const str= uct target_desc *tdesc) >>=20 >> } >>=20 >> +/* Test the convesion process of a target description to/from xml: Take= a target >> + description TDESC, convert to xml, back to a description, and confir= m the new >> + tdesc is identical to the original. */ >> +static bool >> +maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const ch= ar *name) >> +{ >> + const char *xml =3D tdesc_get_features_xml (tdesc); >> + >> + if (xml =3D=3D nullptr && *xml !=3D '@') >=20 > This condition is wrong. You probably meant >=20 > if (xml =3D=3D nullptr || *xml !=3D '@') >=20 > ? >=20 >> + { >> + printf_filtered (_("Could not convert description for %s to xml.\= n"), >> + name); >> + return false; >> + } >> + >> + const target_desc *tdesc_trans =3D string_read_description_xml (xml += 1); >> + >> + if (tdesc_trans =3D=3D nullptr) >> + { >> + printf_filtered (_("Could not convert description for %s from xml= .\n"), >> + name); >> + return false; >> + } >> + else if (*tdesc !=3D *tdesc_trans) >> + { >> + printf_filtered (_("Converted description for %s does not match.\= n"), >> + name); >> + return false; >> + } >> + return true; >> +} >> + >> + >> /* Check that the target descriptions created dynamically by >> architecture-specific code equal the descriptions created from XML fi= les >> found in the specified directory DIR. */ >> @@ -1760,6 +1810,12 @@ maintenance_check_xml_descriptions (const char *d= ir, int from_tty) >> =3D file_read_description_xml (tdesc_xml.data ()); >>=20 >> if (tdesc =3D=3D NULL || *tdesc !=3D *e.second) >> + { >> + printf_filtered ( _("Descriptions for %s do not match.\n"), e.first); >> + failed++; >> + } >> + else if (!maintenance_check_tdesc_xml_convert (tdesc, e.first) >> + || !maintenance_check_tdesc_xml_convert (e.second, e.first)) >> failed++; >> } >> printf_filtered (_("Tested %lu XML files, %d failed\n"), >> diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c >> index 9190d5f3c6..5c6ee4c8cd 100644 >> --- a/gdb/xml-tdesc.c >> +++ b/gdb/xml-tdesc.c >> @@ -752,3 +752,14 @@ target_fetch_description_xml (struct target_ops *op= s) >> return output; >> #endif >> } >> + >> +/* See xml-tdesc.h. */ >> + >> +const struct target_desc * >> +string_read_description_xml (const char *xml_str) >> +{ >> + return tdesc_parse_xml (xml_str, [] (const char *href, void *baton) { >> + error (_("xincludes are unsupported with this method")); >> + return gdb::unique_xmalloc_ptr (); >> + }, nullptr); >=20 > You'll need to change this to >=20 > return gdb::optional (); >=20 > because of a recent change upstream. Also, this indentation would follow= a bit > more what we do: >=20 > return tdesc_parse_xml (xml_str, [] (const char *href, void *baton) > { > error (_("xincludes are unsupported with this method")); > return gdb::optional (); > }, nullptr); >=20 > The idea is to format the lambda the same way we would format an if or fo= r body. >=20 >> +} >> \ No newline at end of file >> diff --git a/gdb/xml-tdesc.h b/gdb/xml-tdesc.h >> index 8f0679707a..e39d5580bb 100644 >> --- a/gdb/xml-tdesc.h >> +++ b/gdb/xml-tdesc.h >> @@ -44,5 +44,10 @@ const struct target_desc *target_read_description_xml= (struct target_ops *); >> otherwise. */ >> gdb::optional target_fetch_description_xml (target_ops *ops= ); >>=20 >> +/* Take an xml string, parse it, and return the parsed description. Do= es not >> + handle a string containing includes. */ >> + >> +const struct target_desc *string_read_description_xml (const char *); >=20 > Can you give a name to the parameter? "xml" would be fine. >=20 > Thanks, >=20 > Simon diff --git a/gdb/common/tdesc.c b/gdb/common/tdesc.c index b9e9ddb3fa..68584a7f94 100644 --- a/gdb/common/tdesc.c +++ b/gdb/common/tdesc.c @@ -290,3 +290,112 @@ tdesc_add_enum_value (tdesc_type_with_fields *type, i= nt value, tdesc_predefined_type (TDESC_TYPE_INT32), value, -1); } + +void print_xml_feature::visit_pre (const tdesc_feature *e) +{ + string_appendf (*m_buffer, "\n", e->name.c_str ()= ); +} + +void print_xml_feature::visit_post (const tdesc_feature *e) +{ + string_appendf (*m_buffer, "\n"); +} + +void print_xml_feature::visit (const tdesc_type_builtin *t) +{ + error (_("xml output is not supported for type \"%s\"."), t->name.c_str = ()); +} + +void print_xml_feature::visit (const tdesc_type_vector *t) +{ + string_appendf (*m_buffer, "\n", + t->name.c_str (), t->element_type->name.c_str (), t->count); +} + +void print_xml_feature::visit (const tdesc_type_with_fields *t) +{ + struct tdesc_type_field *f; + const static char *types[] =3D { "struct", "union", "flags", "enum" }; + + gdb_assert (t->kind >=3D TDESC_TYPE_STRUCT && t->kind <=3D TDESC_TYPE_EN= UM); + + string_appendf (*m_buffer, + "<%s id=3D\"%s\"", types[t->kind - TDESC_TYPE_STRUCT], + t->name.c_str ()); + + switch (t->kind) + { + case TDESC_TYPE_STRUCT: + case TDESC_TYPE_FLAGS: + if (t->size > 0) + string_appendf (*m_buffer, " size=3D\"%d\"", t->size); + string_appendf (*m_buffer, ">\n"); + + for (const tdesc_type_field &f : t->fields) + { + string_appendf (*m_buffer, " \n", + f.type->name.c_str ()); + else + string_appendf (*m_buffer, "start=3D\"%d\" end=3D\"%d\"/>\n", f.start, + f.end); + } + break; + + case TDESC_TYPE_ENUM: + string_appendf (*m_buffer, ">\n"); + for (const tdesc_type_field &f : t->fields) + string_appendf (*m_buffer, " \n", + f.name.c_str (), f.start); + break; + + case TDESC_TYPE_UNION: + string_appendf (*m_buffer, ">\n"); + for (const tdesc_type_field &f : t->fields) + string_appendf (*m_buffer, " \n", + f.name.c_str (), f.type->name.c_str ()); + break; + + default: + error (_("xml output is not supported for type \"%s\"."), + t->name.c_str ()); + } + + string_appendf (*m_buffer, "\n", types[t->kind - TDESC_TYPE_STRUCT]= ); +} + +void print_xml_feature::visit (const tdesc_reg *r) +{ + string_appendf (*m_buffer, + "name.c_str (), r->bitsize, r->type.c_str (), + r->target_regnum); + + if (r->group.length () > 0) + string_appendf (*m_buffer, " group=3D\"%s\"", r->group.c_str ()); + + if (r->save_restore =3D=3D 0) + string_appendf (*m_buffer, " save-restore=3D\"no\""); + + string_appendf (*m_buffer, "/>\n"); +} + +void print_xml_feature::visit_pre (const target_desc *e) +{ +#ifndef IN_PROCESS_AGENT + string_appendf (*m_buffer, "\n"); + string_appendf (*m_buffer, "\= n"); + string_appendf (*m_buffer, "\n%s\n", + tdesc_architecture_name (e)); + + const char *osabi =3D tdesc_osabi_name (e); + if (osabi !=3D nullptr) + string_appendf (*m_buffer, "%s", osabi); +#endif +} + +void print_xml_feature::visit_post (const target_desc *e) +{ + string_appendf (*m_buffer, "\n"); +} diff --git a/gdb/common/tdesc.h b/gdb/common/tdesc.h index 8b826ec436..6868bf47d9 100644 --- a/gdb/common/tdesc.h +++ b/gdb/common/tdesc.h @@ -380,4 +380,30 @@ void tdesc_create_reg (struct tdesc_feature *feature, = const char *name, int regnum, int save_restore, const char *group, int bitsize, const char *type); +/* Return the tdesc in string XML format. */ + +const char *tdesc_get_features_xml (const target_desc *tdesc); + +/* Print target description as xml. */ + +class print_xml_feature : public tdesc_element_visitor +{ +public: + print_xml_feature (std::string *buffer_) + : m_buffer (buffer_) + {} + + void visit_pre (const target_desc *e) override; + void visit_post (const target_desc *e) override; + void visit_pre (const tdesc_feature *e) override; + void visit_post (const tdesc_feature *e) override; + void visit (const tdesc_type_builtin *type) override; + void visit (const tdesc_type_vector *type) override; + void visit (const tdesc_type_with_fields *type) override; + void visit (const tdesc_reg *reg) override; + +private: + std::string *m_buffer; +}; + #endif /* ARCH_TDESC_H */ diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index 64c72bdd58..5027df5e10 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -940,7 +940,7 @@ get_features_xml (const char *annex) if (strcmp (annex, "target.xml") =3D=3D 0) { - const char *ret =3D tdesc_get_features_xml ((target_desc*) desc); + const char *ret =3D tdesc_get_features_xml (desc); if (*ret =3D=3D '@') return ret + 1; diff --git a/gdb/gdbserver/tdesc.c b/gdb/gdbserver/tdesc.c index 7603a90a59..126589f3e3 100644 --- a/gdb/gdbserver/tdesc.c +++ b/gdb/gdbserver/tdesc.c @@ -47,6 +47,18 @@ bool target_desc::operator=3D=3D (const target_desc &oth= er) const #endif +void target_desc::accept (tdesc_element_visitor &v) const +{ +#ifndef IN_PROCESS_AGENT + v.visit_pre (this); + + for (const tdesc_feature_up &feature : features) + feature->accept (v); + + v.visit_post (this); +#endif +} + void init_target_desc (struct target_desc *tdesc) { @@ -138,11 +150,10 @@ set_tdesc_osabi (struct target_desc *target_desc, con= st char *name) target_desc->osabi =3D xstrdup (name); } -/* Return a string which is of XML format, including XML target - description to be sent to GDB. */ +/* See common/tdesc.h. */ const char * -tdesc_get_features_xml (target_desc *tdesc) +tdesc_get_features_xml (const target_desc *tdesc) { /* Either .xmltarget or .features is not NULL. */ gdb_assert (tdesc->xmltarget !=3D NULL @@ -151,31 +162,9 @@ tdesc_get_features_xml (target_desc *tdesc) if (tdesc->xmltarget =3D=3D NULL) { - std::string buffer ("@"); - - buffer +=3D ""; - buffer +=3D ""; - buffer +=3D ""; - buffer +=3D tdesc_architecture_name (tdesc); - buffer +=3D ""; - - const char *osabi =3D tdesc_osabi_name (tdesc); - if (osabi !=3D nullptr) - { - buffer +=3D ""; - buffer +=3D osabi; - buffer +=3D ""; - } - - for (const tdesc_feature_up &feature : tdesc->features) - { - buffer +=3D "name; - buffer +=3D "\"/>"; - } - - buffer +=3D ""; - + std::string buffer ("@"); + print_xml_feature v (&buffer); + tdesc->accept (v); tdesc->xmltarget =3D xstrdup (buffer.c_str ()); } diff --git a/gdb/gdbserver/tdesc.h b/gdb/gdbserver/tdesc.h index 197fb59127..61a3e4ecdd 100644 --- a/gdb/gdbserver/tdesc.h +++ b/gdb/gdbserver/tdesc.h @@ -27,7 +27,7 @@ /* A target description. Inherit from tdesc_feature so that target_desc can be used as tdesc_feature. */ -struct target_desc +struct target_desc : tdesc_element { /* A vector of elements of register definitions that describe the inferior's register set. */ @@ -49,9 +49,9 @@ struct target_desc verbatim XML code (prefixed with a '@') or else the name of the actual XML file to be used in place of "target.xml". - It can be NULL, then, its content is got from the following three - fields features, arch, and osabi in tdesc_get_features_xml. */ - const char *xmltarget =3D NULL; + If NULL then its content will be generated by parsing the target + description into xml. */ + mutable const char *xmltarget =3D NULL; /* The value of element in the XML, replying GDB. */ const char *arch =3D NULL; @@ -73,6 +73,8 @@ public: return !(*this =3D=3D other); } #endif + + void accept (tdesc_element_visitor &v) const override; }; /* Copy target description SRC to DEST. */ @@ -89,8 +91,4 @@ void init_target_desc (struct target_desc *tdesc); const struct target_desc *current_target_desc (void); -#ifndef IN_PROCESS_AGENT -const char *tdesc_get_features_xml (struct target_desc *tdesc); -#endif - #endif /* TDESC_H */ diff --git a/gdb/regformats/regdat.sh b/gdb/regformats/regdat.sh index 18108d77eb..8f546fe276 100755 --- a/gdb/regformats/regdat.sh +++ b/gdb/regformats/regdat.sh @@ -163,7 +163,9 @@ done echo echo "static const char *expedite_regs_${name}[] =3D { \"`echo ${expedite}= | sed 's/,/", "/g'`\", 0 };" -if test "${xmltarget}" =3D x; then +if test "${feature}" !=3D x; then + echo "static const char *xmltarget_${name} =3D 0;" +elif test "${xmltarget}" =3D x; then if test "${xmlarch}" =3D x && test "${xmlosabi}" =3D x; then echo "static const char *xmltarget_${name} =3D 0;" else diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c index da2c1ce345..c0f85112b1 100644 --- a/gdb/target-descriptions.c +++ b/gdb/target-descriptions.c @@ -333,6 +333,9 @@ struct target_desc : tdesc_element /* The features associated with this target. */ std::vector features; + /* Used to cache the generated xml version of the target description. */ + mutable char *xmltarget =3D nullptr; + void accept (tdesc_element_visitor &v) const override { v.visit_pre (this); @@ -1667,6 +1670,21 @@ private: int m_next_regnum =3D 0; }; +/* See common/tdesc.h. */ + +const char * +tdesc_get_features_xml (const target_desc *tdesc) +{ + if (tdesc->xmltarget =3D=3D nullptr) + { + std::string buffer ("@"); + print_xml_feature v (&buffer); + tdesc->accept (v); + tdesc->xmltarget =3D xstrdup (buffer.c_str ()); + } + return tdesc->xmltarget; +} + static void maint_print_c_tdesc_cmd (const char *args, int from_tty) { @@ -1739,6 +1757,39 @@ record_xml_tdesc (const char *xml_file, const struct= target_desc *tdesc) } +/* Test the convesion process of a target description to/from xml: Take a = target + description TDESC, convert to xml, back to a description, and confirm t= he new + tdesc is identical to the original. */ +static bool +maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char = *name) +{ + const char *xml =3D tdesc_get_features_xml (tdesc); + + if (xml =3D=3D nullptr || *xml !=3D '@') + { + printf_filtered (_("Could not convert description for %s to xml.\n"), + name); + return false; + } + + const target_desc *tdesc_trans =3D string_read_description_xml (xml + 1); + + if (tdesc_trans =3D=3D nullptr) + { + printf_filtered (_("Could not convert description for %s from xml.\n= "), + name); + return false; + } + else if (*tdesc !=3D *tdesc_trans) + { + printf_filtered (_("Converted description for %s does not match.\n"), + name); + return false; + } + return true; +} + + /* Check that the target descriptions created dynamically by architecture-specific code equal the descriptions created from XML files found in the specified directory DIR. */ @@ -1760,6 +1811,12 @@ maintenance_check_xml_descriptions (const char *dir,= int from_tty) =3D file_read_description_xml (tdesc_xml.data ()); if (tdesc =3D=3D NULL || *tdesc !=3D *e.second) + { + printf_filtered ( _("Descriptions for %s do not match.\n"), e.first); + failed++; + } + else if (!maintenance_check_tdesc_xml_convert (tdesc, e.first) + || !maintenance_check_tdesc_xml_convert (e.second, e.first)) failed++; } printf_filtered (_("Tested %lu XML files, %d failed\n"), diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c index 1c3409d53b..87c6be428f 100644 --- a/gdb/xml-tdesc.c +++ b/gdb/xml-tdesc.c @@ -752,3 +752,15 @@ target_fetch_description_xml (struct target_ops *ops) return output; #endif } + +/* See xml-tdesc.h. */ + +const struct target_desc * +string_read_description_xml (const char *xml) +{ + return tdesc_parse_xml (xml, [] (const char *href, void *baton) + { + error (_("xincludes are unsupported with this method")); + return gdb::optional (); + }, nullptr); +} diff --git a/gdb/xml-tdesc.h b/gdb/xml-tdesc.h index 8f0679707a..1e3caba745 100644 --- a/gdb/xml-tdesc.h +++ b/gdb/xml-tdesc.h @@ -44,5 +44,10 @@ const struct target_desc *target_read_description_xml (s= truct target_ops *); otherwise. */ gdb::optional target_fetch_description_xml (target_ops *ops); +/* Take an xml string, parse it, and return the parsed description. Does = not + handle a string containing includes. */ + +const struct target_desc *string_read_description_xml (const char *xml); + #endif /* XML_TDESC_H */ -- 2.14.3 (Apple Git-98)