From patchwork Thu Aug 17 08:48:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Hayward X-Patchwork-Id: 22179 Received: (qmail 23192 invoked by alias); 17 Aug 2017 08:49:33 -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 22729 invoked by uid 89); 17 Aug 2017 08:49:06 -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: EUR03-VE1-obe.outbound.protection.outlook.com Received: from mail-eopbgr50043.outbound.protection.outlook.com (HELO EUR03-VE1-obe.outbound.protection.outlook.com) (40.107.5.43) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 17 Aug 2017 08:48:55 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com (10.160.211.19) by AM3PR08MB0101.eurprd08.prod.outlook.com (10.160.211.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1341.17; Thu, 17 Aug 2017 08:48:30 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::b8e2:8809:e2b2:ae37]) by AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::b8e2:8809:e2b2:ae37%14]) with mapi id 15.01.1341.024; Thu, 17 Aug 2017 08:48:30 +0000 From: Alan Hayward To: "gdb-patches@sourceware.org" CC: nd Subject: [PATCH 5/7]: Regcache: Allow writable regcache Date: Thu, 17 Aug 2017 08:48:30 +0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alan.Hayward@arm.com; x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; AM3PR08MB0101; 6:nQCgGP4XiBQZRaNd7FfuR3gHjeI8tIHNzSG1Fa+7nRxLXMyJzpPE9JG+KAlIDY24VnPLGMPc0pHHSvgygsLYw+tx18h+yCOQICBbluMF9ycnV9zxnItmOz7MCq4jVMJflbL9tMuq+7rjajBsgp9ecJXwpis0KtuhmpDt4JqCDezdutaMF+GaMvnrQLpoCBmVI2gb5YvRU9CVxlXOaaCXq4PCB1t2g9Syi97ZeVZ/jCvRKBy4QwPny6LieOKN4Su6c+0I1OVIkRYRC8Ra5+nR/4w0QORYlZ20Y74OZefEr6iEDQ8zRZ1bgOxW2G7lusL9DVcjeikHZq7JsAhZUPxBnA==; 5:b+rxaDLh0rxJG2VPSZYhaVXOs8mq9w+sYaV15IoLPokznLoz2vIC6Oenb7WxqoPnh/JxY8owJYD6YhtNi933KN6Q/uihDrjxtuMcZkcPvTT7Q+HOnu0OS0JxmhNKQXUrTTUpKE+Kq/Y5QCiIjf1qEA==; 24:vmybl4ceo4ZMoDGJcH3P4lgWlvirxipLyCOxcgU8TuyMLnGnJEECNnDoA+DKqb1zBV2VEuYw5ONaZbUFe/erNqlMYsDJV+TIZwbdXLcwAjg=; 7:MCub1HdF4V/sbf0sY0Pz3cMS9jkVWHrmLWMUik02gJsZLoOw1Lc6iQP9Crg/o3uHYcaM/jF6s6/21icjcrh6gP4XjH3ggfdEysFN9YJ2PyQfyR2sDP2+50v+hGe2wkvHNinwlINqeEUIeN6709N4HUzq+ypsrQ5bSGAJJa/4zhAh8gzDD2uhHUbydXvAxIkUCD0fKZsjQYncmTs4pl0YO4BIYeL89DWHJppt4wf/g1k= x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: ab26e9ed-fe0d-408e-755d-08d4e54cbc23 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(300000502095)(300135100095)(22001)(2017030254152)(300000503095)(300135400095)(48565401081)(2017052603031)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:AM3PR08MB0101; x-ms-traffictypediagnostic: AM3PR08MB0101: nodisclaimer: True x-exchange-antispam-report-test: UriScan:(180628864354917); x-microsoft-antispam-prvs: x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(100000703101)(100105400095)(10201501046)(3002001)(6055026)(6041248)(20161123562025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123555025)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:AM3PR08MB0101; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:AM3PR08MB0101; x-forefront-prvs: 0402872DA1 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(39860400002)(199003)(377424004)(189002)(81166006)(68736007)(86362001)(3280700002)(575784001)(478600001)(189998001)(2501003)(5640700003)(99286003)(5660300001)(2906002)(6436002)(25786009)(7736002)(53936002)(3660700001)(33656002)(3846002)(305945005)(6512007)(36756003)(102836003)(101416001)(97736004)(8936002)(6116002)(66066001)(14454004)(50986999)(6916009)(2900100001)(8676002)(54356999)(6486002)(106356001)(105586002)(72206003)(6506006)(81156014)(2351001)(82746002)(83716003)(4326008)(110136004)(5250100002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM3PR08MB0101; 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) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-ID: MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Aug 2017 08:48:30.1235 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM3PR08MB0101 X-IsSubscribed: yes This patch allows a regcache to be writable. With the previous patches in place, we are now secure that only a target_regcache can write through to a target, and only a regcache will use the cooked registers. This patch adds a readonly flag to the public regcache constructor and dup(), with a default value of true. A target_regcache can never be readonly. The cooked registers are only allocated on a regcache, a target_regcache will allocate only the raw registers. However, the register status array is always fully allocated, this ensures that cooked_read/cooked_write can always check the status of the register without having to add a target_regcache check. Tested on a --enable-targets=all build with board files unix, native-gdbserver and unittest.exp. 2017-08-16 Alan Hayward * regcache.c (regcache::regcache): Add bool flags. (target_regcache::target_regcache): Likewise. (regcache::dup): Likewise. (target_regcache::dup): Likewise. (regcache::get_register_status): Split into two functions... (target_regcache::get_register_status): ...here. (regcache::cooked_read): Remove check. (regcache::cooked_read_value): Likewise. diff --git a/gdb/regcache.h b/gdb/regcache.h index 1437dac220e0364557e3c568f1c5223cec598dfd..f4408a562f84bb2d0dcd2792c646c0f98deee716 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -235,9 +235,8 @@ typedef struct cached_reg class regcache { public: - regcache (gdbarch *gdbarch, address_space *aspace_) - : regcache (gdbarch, aspace_, true) - {} + regcache (gdbarch *gdbarch, address_space *aspace_, bool readonly_p_ = true, + bool allocate_registers = true); regcache (const regcache &) = delete; void operator= (const regcache &) = delete; @@ -256,7 +255,7 @@ public: } /* Duplicate self into a new regcache. */ - virtual regcache* dup (); + virtual regcache* dup (bool readonly_p = true); /* Copy the register contents from a target_regcache to self. All cooked registers are read and cached. */ @@ -300,7 +299,7 @@ public: void raw_supply_zeroed (int regnum); - enum register_status get_register_status (int regnum) const; + virtual enum register_status get_register_status (int regnum) const; void raw_set_cached_value (int regnum, const gdb_byte *buf); @@ -337,7 +336,6 @@ public: void debug_print_register (const char *func, int regno); protected: - regcache (gdbarch *gdbarch, address_space *aspace_, bool readonly_p_); gdb_byte *register_buffer (int regnum) const; @@ -392,13 +390,14 @@ public: void restore_to (target_regcache *dst) = delete; /* Duplicate self into a new regcache. Result is not a target_regcache. */ - regcache* dup (); + regcache* dup (bool readonly_p = true); /* Overridden regcache methods. These versions will pass the read/write through to the target. */ enum register_status raw_read (int regnum, gdb_byte *buf); virtual void raw_write (int regnum, const gdb_byte *buf); void raw_update (int regnum); + enum register_status get_register_status (int regnum) const; ptid_t ptid () const { diff --git a/gdb/regcache.c b/gdb/regcache.c index 5405dba7a706910f0b6d20c77eef657e38695b34..9d04c7be904cfb17ac8fd871e9d7678df1694dfd 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -189,29 +189,32 @@ regcache_register_size (const struct regcache *regcache, int n) } regcache::regcache (gdbarch *gdbarch, address_space *aspace_, - bool readonly_p_) + bool readonly_p_, bool allocate_registers) : m_aspace (aspace_), m_readonly_p (readonly_p_) { gdb_assert (gdbarch != NULL); m_descr = regcache_descr (gdbarch); - if (m_readonly_p) + if (allocate_registers) { + /* Need extra space to store the additional cooked registers for when + the detached regcache is used to save a regcache. */ m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers); - m_register_status = XCNEWVEC (signed char, - m_descr->sizeof_cooked_register_status); - } - else - { - m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers); - m_register_status = XCNEWVEC (signed char, - m_descr->sizeof_raw_register_status); } + + /* All status' are initialised to REG_UNKNOWN. */ + m_register_status = XCNEWVEC (signed char, + m_descr->sizeof_cooked_register_status); } target_regcache::target_regcache (gdbarch *gdbarch, address_space *aspace_) - : regcache (gdbarch, aspace_, false) + : regcache (gdbarch, aspace_, false, false) { + /* Only allocate the raw registers - cooked registers are not cached. + Note that the register status is still fully allocated, to allow the + checking of the state of any register. */ + m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers); + m_ptid = minus_one_ptid; /* A target_regcache should never be readonly. */ @@ -359,9 +362,9 @@ regcache::restore_to (target_regcache *dst) /* Duplicate detached regcache to a detached regcache. */ regcache* -regcache::dup () +regcache::dup (bool readonly_p) { - regcache *new_regcache = new regcache (arch (), aspace ()); + regcache *new_regcache = new regcache (arch (), aspace (), readonly_p); memcpy (new_regcache->m_registers, m_registers, m_descr->sizeof_cooked_registers); @@ -373,9 +376,9 @@ regcache::dup () /* Duplicate a target_regcache to a detached regcache. */ regcache* -target_regcache::dup () +target_regcache::dup (bool readonly_p) { - regcache *new_regcache = new regcache (arch (), aspace ()); + regcache *new_regcache = new regcache (arch (), aspace (), readonly_p); new_regcache->save (do_cooked_read, (void *) this); return new_regcache; } @@ -391,14 +394,17 @@ enum register_status regcache::get_register_status (int regnum) const { gdb_assert (regnum >= 0); - if (m_readonly_p) - gdb_assert (regnum < m_descr->nr_cooked_registers); - else - gdb_assert (regnum < m_descr->nr_raw_registers); - + gdb_assert (regnum < m_descr->nr_cooked_registers); return (enum register_status) m_register_status[regnum]; } +enum register_status +target_regcache::get_register_status (int regnum) const +{ + gdb_assert (regnum < m_descr->nr_raw_registers); + return regcache::get_register_status (regnum); +} + void regcache_invalidate (struct regcache *regcache, int regnum) { @@ -707,8 +713,7 @@ regcache::cooked_read (int regnum, gdb_byte *buf) gdb_assert (regnum < m_descr->nr_cooked_registers); if (regnum < m_descr->nr_raw_registers) return raw_read (regnum, buf); - else if (m_readonly_p - && m_register_status[regnum] != REG_UNKNOWN) + else if (m_register_status[regnum] != REG_UNKNOWN) { /* Read-only register cache, perhaps the cooked value was cached? */ @@ -760,7 +765,7 @@ regcache::cooked_read_value (int regnum) gdb_assert (regnum < m_descr->nr_cooked_registers); if (regnum < m_descr->nr_raw_registers - || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN) + || m_register_status[regnum] != REG_UNKNOWN || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) { struct value *result;