public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdbsupport: add debug assertions in gdb::optional::get
@ 2021-07-29 18:42 Simon Marchi
  2021-08-02 17:19 ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Marchi @ 2021-07-29 18:42 UTC (permalink / raw)
  To: gdb-patches

The libstdc++ version of optional contains some runtime checks enabled
when _GLIBCXX_DEBUG is defined.  I think it would be useful if our
version contained similar checks.

Add checks in the two `get` methods, also conditional on _GLIBCXX_DEBUG.
I think it's simpler to use that macro rather than introducing a new
GDB-specific one, as I think that if somebody is interested in enabling
these runtime checks, they'll also be interested in enabling the
libstdc++ runtime checks (and vice-versa).

I implemented these checks using gdb_assert.  Note that gdb_assert
throws (after querying the user), and we are in noexcept methods.  That
means that std::terminate / abort will immediately be called.  I think
this is ok, since if those were "real" _GLIBCXX_DEBUG checks, abort
would be called straight away.

If I add a dummy failure, it looks like so:

    $ ./gdb -q -nx --data-directory=data-directory
    /home/simark/src/binutils-gdb/gdb/../gdbsupport/gdb_optional.h:206: internal-error: T& gdb::optional<T>::get() [with T = int]: Assertion `this->has_value ()' failed.
    A problem internal to GDB has been detected,
    further debugging may prove unreliable.
    Quit this debugging session? (y or n) n
    [1]    658767 abort (core dumped)  ./gdb -q -nx --data-directory=data-directory

Change-Id: Iadfdcd131425bd2ca6a2de30d7b22e9b3cc67793
---
 gdbsupport/gdb_optional.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/gdbsupport/gdb_optional.h b/gdbsupport/gdb_optional.h
index e79ba2c52e61..745b2ba74886 100644
--- a/gdbsupport/gdb_optional.h
+++ b/gdbsupport/gdb_optional.h
@@ -200,8 +200,20 @@ class optional
   }
 
   /* The get operations have m_instantiated as a precondition.  */
-  T &get () noexcept { return m_item; }
-  constexpr const T &get () const noexcept { return m_item; }
+  T &get () noexcept
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (this->has_value ());
+#endif
+    return m_item;
+  }
+  constexpr const T &get () const noexcept
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (this->has_value ());
+#endif
+    return m_item;
+  }
 
   /* The object.  */
   union
-- 
2.32.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-08-04 19:40 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 18:42 [PATCH] gdbsupport: add debug assertions in gdb::optional::get Simon Marchi
2021-08-02 17:19 ` Tom Tromey
2021-08-03 12:56   ` Simon Marchi
2021-08-03 13:14     ` Simon Marchi
2021-08-03 15:31       ` [PATCH 1/3] gdb: fix typo in complaint in dwarf2/macro.c Simon Marchi
2021-08-03 15:31         ` [PATCH 2/3] gdb: avoid dereferencing empty str_offsets_base optional in dwarf_decode_macros Simon Marchi
2021-08-04 16:03           ` Tom Tromey
2021-08-03 15:31         ` [PATCH 3/3] gdb/testsuite: fix gdb.base/info-macros.exp with clang Simon Marchi
2021-08-04 16:15           ` Tom Tromey
2021-08-04 19:39             ` Simon Marchi
2021-08-04 15:52         ` [PATCH 1/3] gdb: fix typo in complaint in dwarf2/macro.c Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).