aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-07-29 14:42:04 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-08-03 08:50:56 -0400
commit4d7188abfdf2c8303fdcad300cd7c06c4b4c30c0 (patch)
tree878b6c898cec28f601a4904d32881d3a3ce9f1da /gdbsupport
parent748aa9b65364d72b0f9a36139e292eaf3bc86aa4 (diff)
downloadgdb-4d7188abfdf2c8303fdcad300cd7c06c4b4c30c0.zip
gdb-4d7188abfdf2c8303fdcad300cd7c06c4b4c30c0.tar.gz
gdb-4d7188abfdf2c8303fdcad300cd7c06c4b4c30c0.tar.bz2
gdbsupport: add debug assertions in gdb::optional::get
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
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/gdb_optional.h16
1 files changed, 14 insertions, 2 deletions
diff --git a/gdbsupport/gdb_optional.h b/gdbsupport/gdb_optional.h
index e79ba2c..745b2ba 100644
--- a/gdbsupport/gdb_optional.h
+++ b/gdbsupport/gdb_optional.h
@@ -200,8 +200,20 @@ private:
}
/* 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