aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-11-22 09:30:21 -0700
committerTom Tromey <tom@tromey.com>2017-11-26 10:42:15 -0700
commit41272101db08e20f044dbf0f09b6d79a0fa6c1ac (patch)
tree0e310fc1f5c505889ac90b7c1955295286822640 /gdb/nat
parent44287fd89091a833451e74c79e46404c23fdaf9b (diff)
downloadgdb-41272101db08e20f044dbf0f09b6d79a0fa6c1ac.zip
gdb-41272101db08e20f044dbf0f09b6d79a0fa6c1ac.tar.gz
gdb-41272101db08e20f044dbf0f09b6d79a0fa6c1ac.tar.bz2
Change maybe_disable_address_space_randomization to a class
This changes maybe_disable_address_space_randomization to be an RAII class, rather than having it return a cleanup. Regression tested by the buildbot. ChangeLog 2017-11-26 Tom Tromey <tom@tromey.com> * nat/linux-personality.h (class maybe_disable_address_space_randomization): New class. (maybe_disable_address_space_randomization): Don't declare function. * nat/linux-personality.c (restore_personality) (make_disable_asr_cleanup): Remove. (maybe_disable_address_space_randomization): Now a constructor. (~maybe_disable_address_space_randomization): New destructor. * linux-nat.c (linux_nat_create_inferior): Update. gdbserver/ChangeLog 2017-11-26 Tom Tromey <tom@tromey.com> * linux-low.c (linux_create_inferior): Update.
Diffstat (limited to 'gdb/nat')
-rw-r--r--gdb/nat/linux-personality.c71
-rw-r--r--gdb/nat/linux-personality.h27
2 files changed, 44 insertions, 54 deletions
diff --git a/gdb/nat/linux-personality.c b/gdb/nat/linux-personality.c
index 21a9ca9..bcec2ce 100644
--- a/gdb/nat/linux-personality.c
+++ b/gdb/nat/linux-personality.c
@@ -27,68 +27,43 @@
# endif /* ! HAVE_DECL_ADDR_NO_RANDOMIZE */
#endif /* HAVE_PERSONALITY */
-#ifdef HAVE_PERSONALITY
-
-/* Restore address space randomization of the inferior. ARG is the
- original inferior's personality value before the address space
- randomization was disabled. */
-
-static void
-restore_personality (void *arg)
-{
- int personality_orig = (int) (uintptr_t) arg;
-
- errno = 0;
- personality (personality_orig);
- if (errno != 0)
- warning (_("Error restoring address space randomization: %s"),
- safe_strerror (errno));
-}
-#endif /* HAVE_PERSONALITY */
-
-/* Return a cleanup responsible for restoring the inferior's
- personality (and restoring the inferior's address space
- randomization) if HAVE_PERSONALITY and if PERSONALITY_SET is not
- zero; return a null cleanup if not HAVE_PERSONALITY or if
- PERSONALITY_SET is zero. */
-
-static struct cleanup *
-make_disable_asr_cleanup (int personality_set, int personality_orig)
-{
-#ifdef HAVE_PERSONALITY
- if (personality_set != 0)
- return make_cleanup (restore_personality,
- (void *) (uintptr_t) personality_orig);
-#endif /* HAVE_PERSONALITY */
-
- return make_cleanup (null_cleanup, NULL);
-}
-
/* See comment on nat/linux-personality.h. */
-struct cleanup *
+maybe_disable_address_space_randomization::
maybe_disable_address_space_randomization (int disable_randomization)
+ : m_personality_set (false),
+ m_personality_orig (0)
{
- int personality_orig = 0;
- int personality_set = 0;
-
#ifdef HAVE_PERSONALITY
if (disable_randomization)
{
errno = 0;
- personality_orig = personality (0xffffffff);
- if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
+ m_personality_orig = personality (0xffffffff);
+ if (errno == 0 && !(m_personality_orig & ADDR_NO_RANDOMIZE))
{
- personality_set = 1;
- personality (personality_orig | ADDR_NO_RANDOMIZE);
+ m_personality_set = true;
+ personality (m_personality_orig | ADDR_NO_RANDOMIZE);
}
- if (errno != 0 || (personality_set
+ if (errno != 0 || (m_personality_set
&& !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
warning (_("Error disabling address space randomization: %s"),
safe_strerror (errno));
}
#endif /* HAVE_PERSONALITY */
+}
+
+maybe_disable_address_space_randomization::
+~maybe_disable_address_space_randomization ()
+{
+#ifdef HAVE_PERSONALITY
- return make_disable_asr_cleanup (personality_set,
- personality_orig);
+ if (m_personality_set)
+ {
+ errno = 0;
+ personality (m_personality_orig);
+ if (errno != 0)
+ warning (_("Error restoring address space randomization: %s"),
+ safe_strerror (errno));
+ }
+#endif /* HAVE_PERSONALITY */
}
diff --git a/gdb/nat/linux-personality.h b/gdb/nat/linux-personality.h
index 687086e..fcea02a 100644
--- a/gdb/nat/linux-personality.h
+++ b/gdb/nat/linux-personality.h
@@ -20,12 +20,27 @@
#ifndef NAT_LINUX_PERSONALITY_H
#define NAT_LINUX_PERSONALITY_H
-/* Disable the inferior's address space randomization if
- DISABLE_RANDOMIZATION is not zero and if we have
- <sys/personality.h>. Return a cleanup which, when called, will
- re-enable the inferior's address space randomization. */
+class maybe_disable_address_space_randomization
+{
+public:
-extern struct cleanup *maybe_disable_address_space_randomization
- (int disable_randomization);
+ /* Disable the inferior's address space randomization if
+ DISABLE_RANDOMIZATION is not zero and if we have
+ <sys/personality.h>. */
+ maybe_disable_address_space_randomization (int disable_randomization);
+
+ /* On destruction, re-enable address space randomization. */
+ ~maybe_disable_address_space_randomization ();
+
+ DISABLE_COPY_AND_ASSIGN (maybe_disable_address_space_randomization);
+
+private:
+
+ /* True if the personality was set in the constructor. */
+ bool m_personality_set;
+
+ /* If m_personality_set is true, the original personality value. */
+ int m_personality_orig;
+};
#endif /* ! NAT_LINUX_PERSONALITY_H */