From e521e87e8514b9d3497208b70bcd067f132c58ed Mon Sep 17 00:00:00 2001 From: Yao Qi Date: Wed, 24 May 2017 22:15:23 +0100 Subject: [PATCH] Move current_regcache to regcache::current_regcache This patches moves global variable current_regcache to a class regcache static variable (protected) so that the unit test I add in the following patch can access it (by means of extending class regcache in unit test). gdb: 2017-05-24 Yao Qi * regcache.c (current_regcache): Change it to regcache::current_regcache. (regcache_observer_target_changed): Update. (regcache_thread_ptid_changed): Make it a regcache static method. (regcache_thread_ptid_changed): Update. (class regcache_access): New. (current_regcache_test): Update. (_initialize_regcache): Update. * regcache.h: Include forward_list. (regcache): Declare regcache_thread_ptid_changed and declare registers_changed_ptid as friend. --- gdb/ChangeLog | 15 +++++++++++++++ gdb/regcache.c | 51 +++++++++++++++++++++++++++----------------------- gdb/regcache.h | 10 +++++++++- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ea75f0795ea..fc09819596b 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,18 @@ +2017-05-24 Yao Qi + + * regcache.c (current_regcache): Change it to + regcache::current_regcache. + (regcache_observer_target_changed): Update. + (regcache_thread_ptid_changed): Make it a regcache static + method. + (regcache_thread_ptid_changed): Update. + (class regcache_access): New. + (current_regcache_test): Update. + (_initialize_regcache): Update. + * regcache.h: Include forward_list. + (regcache): Declare regcache_thread_ptid_changed and declare + registers_changed_ptid as friend. + 2017-05-24 Yao Qi * i387-tdep.c (i387_register_to_value): Use register_size diff --git a/gdb/regcache.c b/gdb/regcache.c index 7250763203d..7638eea52f2 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -472,20 +472,19 @@ regcache::invalidate (int regnum) recording if the register values have been changed (eg. by the user). Therefore all registers must be written back to the target when appropriate. */ - -static std::forward_list current_regcache; +std::forward_list regcache::current_regcache; struct regcache * get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, struct address_space *aspace) { - for (const auto ®cache : current_regcache) + for (const auto ®cache : regcache::current_regcache) if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch) return regcache; regcache *new_regcache = new regcache (gdbarch, aspace, false); - current_regcache.push_front (new_regcache); + regcache::current_regcache.push_front (new_regcache); new_regcache->set_ptid (ptid); return new_regcache; @@ -548,10 +547,10 @@ regcache_observer_target_changed (struct target_ops *target) /* Update global variables old ptids to hold NEW_PTID if they were holding OLD_PTID. */ -static void -regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) +void +regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) { - for (auto ®cache : current_regcache) + for (auto ®cache : regcache::current_regcache) { if (ptid_equal (regcache->ptid (), old_ptid)) regcache->set_ptid (new_ptid); @@ -572,15 +571,15 @@ regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) void registers_changed_ptid (ptid_t ptid) { - for (auto oit = current_regcache.before_begin (), + for (auto oit = regcache::current_regcache.before_begin (), it = std::next (oit); - it != current_regcache.end (); + it != regcache::current_regcache.end (); ) { if (ptid_match ((*it)->ptid (), ptid)) { delete *it; - it = current_regcache.erase_after (oit); + it = regcache::current_regcache.erase_after (oit); } else oit = it++; @@ -1685,19 +1684,25 @@ maintenance_print_remote_registers (char *args, int from_tty) namespace selftests { -/* Return the number of elements in current_regcache. */ - -static size_t -current_regcache_size () +class regcache_access : public regcache { - return std::distance (current_regcache.begin (), current_regcache.end ()); -} +public: + + /* Return the number of elements in current_regcache. */ + + static size_t + current_regcache_size () + { + return std::distance (regcache::current_regcache.begin (), + regcache::current_regcache.end ()); + } +}; static void current_regcache_test (void) { /* It is empty at the start. */ - SELF_CHECK (current_regcache_size () == 0); + SELF_CHECK (regcache_access::current_regcache_size () == 0); ptid_t ptid1 (1), ptid2 (2), ptid3 (3); @@ -1709,7 +1714,7 @@ current_regcache_test (void) SELF_CHECK (regcache != NULL); SELF_CHECK (regcache->ptid () == ptid1); - SELF_CHECK (current_regcache_size () == 1); + SELF_CHECK (regcache_access::current_regcache_size () == 1); /* Get regcache from ptid2, a new regcache is added to current_regcache. */ @@ -1718,7 +1723,7 @@ current_regcache_test (void) NULL); SELF_CHECK (regcache != NULL); SELF_CHECK (regcache->ptid () == ptid2); - SELF_CHECK (current_regcache_size () == 2); + SELF_CHECK (regcache_access::current_regcache_size () == 2); /* Get regcache from ptid3, a new regcache is added to current_regcache. */ @@ -1727,7 +1732,7 @@ current_regcache_test (void) NULL); SELF_CHECK (regcache != NULL); SELF_CHECK (regcache->ptid () == ptid3); - SELF_CHECK (current_regcache_size () == 3); + SELF_CHECK (regcache_access::current_regcache_size () == 3); /* Get regcache from ptid2 again, nothing is added to current_regcache. */ @@ -1736,12 +1741,12 @@ current_regcache_test (void) NULL); SELF_CHECK (regcache != NULL); SELF_CHECK (regcache->ptid () == ptid2); - SELF_CHECK (current_regcache_size () == 3); + SELF_CHECK (regcache_access::current_regcache_size () == 3); /* Mark ptid2 is changed, so regcache of ptid2 should be removed from current_regcache. */ registers_changed_ptid (ptid2); - SELF_CHECK (current_regcache_size () == 2); + SELF_CHECK (regcache_access::current_regcache_size () == 2); } } // namespace selftests @@ -1756,7 +1761,7 @@ _initialize_regcache (void) = gdbarch_data_register_post_init (init_regcache_descr); observer_attach_target_changed (regcache_observer_target_changed); - observer_attach_thread_ptid_changed (regcache_thread_ptid_changed); + observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed); add_com ("flushregs", class_maintenance, reg_flush_command, _("Force gdb to flush its register cache (maintainer command)")); diff --git a/gdb/regcache.h b/gdb/regcache.h index da00abd9f90..fdc47ba35fb 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -21,6 +21,7 @@ #define REGCACHE_H #include "common-regcache.h" +#include struct regcache; struct regset; @@ -336,9 +337,13 @@ public: debug. */ void debug_print_register (const char *func, int regno); -private: + static void regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid); +protected: regcache (gdbarch *gdbarch, address_space *aspace_, bool readonly_p_); + static std::forward_list current_regcache; + +private: gdb_byte *register_buffer (int regnum) const; void restore (struct regcache *src); @@ -382,6 +387,9 @@ private: get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, struct address_space *aspace); + friend void + registers_changed_ptid (ptid_t ptid); + friend void regcache_cpy (struct regcache *dst, struct regcache *src); }; -- 2.30.2