From patchwork Thu Mar 1 11:41:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Hayward X-Patchwork-Id: 26133 Received: (qmail 51706 invoked by alias); 1 Mar 2018 11:41:12 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 51677 invoked by uid 89); 1 Mar 2018 11:41:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LOTSOFHASH, 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-ve1eur01on0072.outbound.protection.outlook.com (HELO EUR01-VE1-obe.outbound.protection.outlook.com) (104.47.1.72) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 01 Mar 2018 11:41:08 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com (10.160.211.19) by AM3PR08MB0705.eurprd08.prod.outlook.com (10.163.189.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.527.15; Thu, 1 Mar 2018 11:41:04 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::fc60:4b4d:7de8:f8b7]) by AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::fc60:4b4d:7de8:f8b7%16]) with mapi id 15.20.0527.022; Thu, 1 Mar 2018 11:41:05 +0000 From: Alan Hayward To: "gdb-patches@sourceware.org" CC: nd Subject: [PATCH v3 6/8] Create xml from target descriptions Date: Thu, 1 Mar 2018 11:41:04 +0000 Message-ID: References: <757A8B89-2EF0-46BD-BAA6-6E668538B17F@arm.com> In-Reply-To: <757A8B89-2EF0-46BD-BAA6-6E668538B17F@arm.com> x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; AM3PR08MB0705; 7:3Ph5tbw2BMKd/eiNZf2xuavhbUIsf3UCNvxCpbyfnpEmUnlvG1FgOcnqH1eIRIsIMxWl/CYCCrrduWMFhluvPnJG6Oa/AOT5JNRzRGpP1ArCkIt7yIQxnGWR6oka4PrWAilN21UqhqPIR5byAW1HQespXNYpqVjNo3VeyWnkLHualEAhOP5Uz5N1CQpMLvqay5/fOiEi62t5opt30pLS9oiykAKSBmG5PLv3chuu5Wr03ifQ9iHtD4Ezek53FZ/V x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 1a782641-6f27-40da-dd4a-08d57f695106 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(4534165)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(3008032)(2017052603307)(7153060)(7193020); SRVR:AM3PR08MB0705; x-ms-traffictypediagnostic: AM3PR08MB0705: nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917)(131327999870524); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040501)(2401047)(8121501046)(5005006)(93006095)(93001095)(3231220)(944501227)(52105095)(10201501046)(3002001)(6055026)(6041288)(20161123560045)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(6072148)(201708071742011); SRVR:AM3PR08MB0705; BCL:0; PCL:0; RULEID:; SRVR:AM3PR08MB0705; x-forefront-prvs: 05986C03E0 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(39860400002)(39380400002)(396003)(366004)(346002)(376002)(377424004)(199004)(189003)(81156014)(6346003)(6512007)(102836004)(6506007)(8936002)(8676002)(68736007)(99286004)(4743002)(59450400001)(53936002)(186003)(26005)(97736004)(3660700001)(25786009)(14454004)(105586002)(72206003)(2351001)(106356001)(82746002)(316002)(81166006)(5660300001)(478600001)(76176011)(6486002)(4326008)(36756003)(66066001)(305945005)(7736002)(83716003)(2950100002)(6916009)(575784001)(86362001)(33656002)(2501003)(6436002)(2906002)(6116002)(5250100002)(3846002)(2900100001)(3280700002)(5640700003); DIR:OUT; SFP:1101; SCL:1; SRVR:AM3PR08MB0705; H:AM3PR08MB0101.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; received-spf: None (protection.outlook.com: arm.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alan.Hayward@arm.com; x-microsoft-antispam-message-info: 9slaJsbiZl9a/OtaqjlVzms6pw4o24hag0JMpMPCYBEx9V6tE3w9w8MCzfLibvBg8JrlBc4Kq97lIqChFsDdzldIqiHOgBeLRpkN3haR6s4wQ2Nf487GIDycE1zIf/tholBNeL+CHxL3ZZVX4MLR0/0mEJDM2pJqoXv9x8tnXeE= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-ID: <789F761E65A783428A74771CFF266AB2@eurprd08.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1a782641-6f27-40da-dd4a-08d57f695106 X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Mar 2018 11:41:04.9084 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM3PR08MB0705 X-IsSubscribed: yes This patch adds a print_xml_feature visitor class which turns a C target description into xml. Both gdb and gdbserver can do this. An accept function is added to gdbserver tdesc to allow it to use vistor classes. Tests are added to maintenance_check_xml_descriptions which takes each pair of tested descriptions, turns them both into xml, then back again, and confirms the descriptions still match. Alan. 2018-03-01 Alan Hayward gdb/ * common/tdesc.c (print_xml_feature::visit_post): Add xml parsing. (print_xml_feature::visit_pre): Likewise. (print_xml_feature::visit_post): Likewise. (print_xml_feature::visit): Likewise. (print_xml_feature::visit): Likewise. (print_xml_feature::visit): Likewise. (print_xml_feature::visit): Likewise. * common/tdesc.h (print_xml_feature): Add new class. * regformats/regdat.sh: obtain xml. * target-descriptions.c (struct target_desc): Add xmltarget. (print_xml_feature::visit_pre): Add xml vistor. (tdesc_get_features_xml): Add function to get xml. (maintenance_check_xml_descriptions): Test xml generation. * xml-tdesc.c (target_read_description_xml_string): Add function. * xml-tdesc.h (target_read_description_xml_string): Add declaration. gdbserver/ * tdesc.c (void target_desc::accept): Fill in function. (tdesc_get_features_xml): Remove old xml creation. (print_xml_feature::visit_pre): Add xml vistor. index c0d2a10b0f7ba4e7b836e3b163d229f1608b02d8..1c5ddc572cf76cb24176fd638dde417687d202b0 100644 --- a/gdb/common/tdesc.h +++ b/gdb/common/tdesc.h @@ -372,4 +372,33 @@ 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 (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 () + {} + + 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/common/tdesc.c b/gdb/common/tdesc.c index 115e7cd77942b86dedced946773d1d71950e2bb3..ce5b89045c48c65ac4c6c48f6646f8d5c6ce9d8b 100644 --- a/gdb/common/tdesc.c +++ b/gdb/common/tdesc.c @@ -288,4 +288,158 @@ tdesc_add_enum_value (tdesc_type_with_fields *type, int value, type->fields.emplace_back (name, tdesc_predefined_type (TDESC_TYPE_INT32), value, -1); -} \ No newline at end of file +} + +void print_xml_feature::visit_post (const target_desc *e) +{ + *m_buffer += "\n"; +} + +void print_xml_feature::visit_pre (const tdesc_feature *e) +{ + *m_buffer += "name; + *m_buffer += "\">\n"; +} + +void print_xml_feature::visit_post (const tdesc_feature *e) +{ + *m_buffer += "\n"; +} + +void print_xml_feature::visit (const tdesc_type_builtin *type) +{ + error (_("xml output is not supported type \"%s\"."), type->name.c_str ()); +} + +void print_xml_feature::visit (const tdesc_type_vector *type) +{ + *m_buffer += "name; + *m_buffer += "\" type=\""; + *m_buffer += type->element_type->name; + *m_buffer += "\" count=\""; + *m_buffer += std::to_string (type->count); + *m_buffer += "\"/>\n"; +} + +void print_xml_feature::visit (const tdesc_type_with_fields *type) +{ + struct tdesc_type_field *f; + const static char *types[] = { "struct", "union", "flags", "enum" }; + + gdb_assert (type->kind >= TDESC_TYPE_STRUCT && type->kind <= TDESC_TYPE_ENUM); + *m_buffer += "<"; + *m_buffer += types[type->kind - TDESC_TYPE_STRUCT]; + + switch (type->kind) + { + case TDESC_TYPE_STRUCT: + case TDESC_TYPE_FLAGS: + *m_buffer += " id=\""; + *m_buffer += type->name; + if (type->size > 0) + { + *m_buffer += "\" size=\""; + *m_buffer += std::to_string (type->size); + } + *m_buffer += "\">\n"; + + for (const tdesc_type_field &f : type->fields) + { + *m_buffer += " name; + } + else + { + *m_buffer += "\" start=\""; + *m_buffer += std::to_string (f.start); + *m_buffer += "\" end=\""; + *m_buffer += std::to_string (f.end); + } + + *m_buffer += "\"/>\n"; + } + break; + + case TDESC_TYPE_ENUM: + *m_buffer += " id=\""; + *m_buffer += type->name; + *m_buffer += "\">\n"; + + for (const tdesc_type_field &f : type->fields) + { + *m_buffer += " \n"; + } + break; + + case TDESC_TYPE_UNION: + *m_buffer += " id=\""; + *m_buffer += type->name; + *m_buffer += "\">\n"; + + for (const tdesc_type_field &f : type->fields) + { + *m_buffer += " name; + *m_buffer += "\"/>\n"; + } + break; + + default: + error (_("xml output is not supported type \"%s\"."), + type->name.c_str ()); + } + + *m_buffer += "kind - TDESC_TYPE_STRUCT]; + *m_buffer += ">\n"; +} + +void print_xml_feature::visit (const tdesc_reg *reg) +{ + *m_buffer += "name; + *m_buffer += "\" bitsize=\""; + *m_buffer += std::to_string (reg->bitsize); + *m_buffer += "\" type=\""; + *m_buffer += reg->type; + *m_buffer += "\" regnum=\""; + *m_buffer += std::to_string (reg->target_regnum); + if (reg->group.length () > 0) + { + *m_buffer += "\" group=\""; + *m_buffer += reg->group; + } + *m_buffer += "\"/>\n"; +} + +void print_xml_feature::visit_pre (const target_desc *e) +{ +#ifndef IN_PROCESS_AGENT + *m_buffer += "\n"; + *m_buffer += "\n"; + *m_buffer += "\n"; + *m_buffer += ""; + *m_buffer += tdesc_architecture_name (e); + *m_buffer += "\n"; + + const char *osabi = tdesc_osabi_name (e); + if (osabi != nullptr) + { + *m_buffer += ""; + *m_buffer += osabi; + *m_buffer += "\n"; + } +#endif +} diff --git a/gdb/gdbserver/tdesc.h b/gdb/gdbserver/tdesc.h index 8534069d616b97dc82df04939a4ac935aa4e1cad..1c0c4b15f3cf2a7c4afd190963c1ca1ac77e1b59 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. */ @@ -73,6 +73,8 @@ public: return !(*this == other); } #endif + + void accept (tdesc_element_visitor &v) const; }; /* Copy target description SRC to DEST. */ diff --git a/gdb/gdbserver/tdesc.c b/gdb/gdbserver/tdesc.c index e11344762a3a4114ed9aa459d7d739bd96a90ae5..0d9609bd4c3446b7a15df569923d22fc10105b74 100644 --- a/gdb/gdbserver/tdesc.c +++ b/gdb/gdbserver/tdesc.c @@ -59,6 +59,18 @@ bool target_desc::operator== (const target_desc &other) 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) { @@ -158,8 +170,7 @@ set_tdesc_osabi (struct target_desc *target_desc, const char *name) target_desc->osabi = 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) @@ -171,31 +182,9 @@ tdesc_get_features_xml (target_desc *tdesc) if (tdesc->xmltarget == NULL) { - std::string buffer ("@"); - - buffer += ""; - buffer += ""; - buffer += ""; - buffer += tdesc_architecture_name (tdesc); - buffer += ""; - - const char *osabi = tdesc_osabi_name (tdesc); - if (osabi != nullptr) - { - buffer += ""; - buffer += osabi; - buffer += ""; - } - - for (const tdesc_feature_up &feature : tdesc->features) - { - buffer += "name; - buffer += "\"/>"; - } - - buffer += ""; - + std::string buffer ("@"); + print_xml_feature v (&buffer); + tdesc->accept (v); tdesc->xmltarget = xstrdup (buffer.c_str ()); } diff --git a/gdb/regformats/regdat.sh b/gdb/regformats/regdat.sh index 8c6e191596350fb4e983f8736985d9832f41e2d3..e6e06bdab0bdecc579686f3525e9f93555e0dd83 100755 --- a/gdb/regformats/regdat.sh +++ b/gdb/regformats/regdat.sh @@ -180,7 +180,6 @@ echo cat <expedite_regs = expedite_regs_${name}; - result->xmltarget = xmltarget_${name}; #endif init_target_desc (result); diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c index da2c1ce34531c1b23281c42f2dacbc85444ef544..93e73571177c980b4cd1975256c89e8ffb9fcdd6 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; + char *xmltarget = nullptr; + void accept (tdesc_element_visitor &v) const override { v.visit_pre (this); @@ -1667,6 +1669,21 @@ private: int m_next_regnum = 0; }; +/* See common/tdesc.h. */ + +const char * +tdesc_get_features_xml (target_desc *tdesc) +{ + if (tdesc->xmltarget == nullptr) + { + std::string buffer ("@"); + print_xml_feature v (&buffer); + tdesc->accept (v); + tdesc->xmltarget = xstrdup (buffer.c_str ()); + } + return tdesc->xmltarget; +} + static void maint_print_c_tdesc_cmd (const char *args, int from_tty) { @@ -1760,7 +1777,36 @@ maintenance_check_xml_descriptions (const char *dir, int from_tty) = file_read_description_xml (tdesc_xml.data ()); if (tdesc == NULL || *tdesc != *e.second) - failed++; + { + printf_filtered ( _("Descriptions for %s do not match\n"), e.first); + failed++; + continue; + } + + /* Convert both descriptions to xml, and then back again. Confirm all + descriptions are identical. */ + + const char *xml = tdesc_get_features_xml ((target_desc *) tdesc); + const char *xml2 = tdesc_get_features_xml ((target_desc *) e.second); + gdb_assert (*xml == '@'); + gdb_assert (*xml2 == '@'); + const target_desc *t_trans = target_read_description_xml_string (xml+1); + const target_desc *t_trans2 = target_read_description_xml_string (xml2+1); + + if (t_trans == NULL || t_trans2 == NULL) + { + printf_filtered ( + _("Could not convert descriptions for %s back to xml (%p %p)\n"), + e.first, t_trans, t_trans2); + failed++; + } + else if (*tdesc != *t_trans || *tdesc != *t_trans2) + { + printf_filtered + (_("Translated descriptions for %s do not match (%d %d)\n"), + e.first, *tdesc == *t_trans, *tdesc == *t_trans2); + failed++; + } } printf_filtered (_("Tested %lu XML files, %d failed\n"), (long) selftests::xml_tdesc.size (), failed); diff --git a/gdb/xml-tdesc.h b/gdb/xml-tdesc.h index 8f0679707ad0f1e04d803f955f7fb98b4cc0c8c8..fee60e86dd10e1543b935c3cebce505d4dc828e2 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); +/* Take an xml string, parse it, and return the parsed description. Does not + handle a string containing includes. */ + +const struct target_desc *target_read_description_xml_string (const char *); + #endif /* XML_TDESC_H */ diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c index 9190d5f3c64ffdc6d7987d651527f597d695c5a6..f793f07c96847a3d61188fff1c5d63952cc37565 100644 --- a/gdb/xml-tdesc.c +++ b/gdb/xml-tdesc.c @@ -752,3 +752,12 @@ target_fetch_description_xml (struct target_ops *ops) return output; #endif } + +/* Take an xml string, parse it, and return the parsed description. Does not + handle a string containing includes. */ + +const struct target_desc * +target_read_description_xml_string (const char *xml_str) +{ + return tdesc_parse_xml (xml_str, nullptr, nullptr); +}