aboutsummaryrefslogtreecommitdiff
path: root/gdb/common
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/common')
-rw-r--r--gdb/common/common-exceptions.c133
-rw-r--r--gdb/common/common-exceptions.h81
-rw-r--r--gdb/common/selftest.c2
3 files changed, 77 insertions, 139 deletions
diff --git a/gdb/common/common-exceptions.c b/gdb/common/common-exceptions.c
index c3529c9..b2d8e25 100644
--- a/gdb/common/common-exceptions.c
+++ b/gdb/common/common-exceptions.c
@@ -19,8 +19,9 @@
#include "common-defs.h"
#include "common-exceptions.h"
+#include <forward_list>
-const struct gdb_exception exception_none = { (enum return_reason) 0, GDB_NO_ERROR, NULL };
+const struct gdb_exception exception_none;
/* Possible catcher states. */
enum catcher_state {
@@ -42,42 +43,21 @@ enum catcher_action {
struct catcher
{
- enum catcher_state state;
+ enum catcher_state state = CATCHER_CREATED;
/* Jump buffer pointing back at the exception handler. */
jmp_buf buf;
/* Status buffer belonging to the exception handler. */
- struct gdb_exception exception;
- /* Back link. */
- struct catcher *prev;
+ struct gdb_exception exception = exception_none;
};
/* Where to go for throw_exception(). */
-static struct catcher *current_catcher;
+static std::forward_list<struct catcher> catchers;
jmp_buf *
-exceptions_state_mc_init (void)
+exceptions_state_mc_init ()
{
- struct catcher *new_catcher = XCNEW (struct catcher);
-
- /* Start with no exception. */
- new_catcher->exception = exception_none;
-
- /* Push this new catcher on the top. */
- new_catcher->prev = current_catcher;
- current_catcher = new_catcher;
- new_catcher->state = CATCHER_CREATED;
-
- return &new_catcher->buf;
-}
-
-static void
-catcher_pop (void)
-{
- struct catcher *old_catcher = current_catcher;
-
- current_catcher = old_catcher->prev;
-
- xfree (old_catcher);
+ catchers.emplace_front ();
+ return &catchers.front ().buf;
}
/* Catcher state machine. Returns non-zero if the m/c should be run
@@ -86,14 +66,14 @@ catcher_pop (void)
static int
exceptions_state_mc (enum catcher_action action)
{
- switch (current_catcher->state)
+ switch (catchers.front ().state)
{
case CATCHER_CREATED:
switch (action)
{
case CATCH_ITER:
/* Allow the code to run the catcher. */
- current_catcher->state = CATCHER_RUNNING;
+ catchers.front ().state = CATCHER_RUNNING;
return 1;
default:
internal_error (__FILE__, __LINE__, _("bad state"));
@@ -105,10 +85,10 @@ exceptions_state_mc (enum catcher_action action)
/* No error/quit has occured. */
return 0;
case CATCH_ITER_1:
- current_catcher->state = CATCHER_RUNNING_1;
+ catchers.front ().state = CATCHER_RUNNING_1;
return 1;
case CATCH_THROWING:
- current_catcher->state = CATCHER_ABORTING;
+ catchers.front ().state = CATCHER_ABORTING;
/* See also throw_exception. */
return 1;
default:
@@ -121,10 +101,10 @@ exceptions_state_mc (enum catcher_action action)
/* The did a "break" from the inner while loop. */
return 0;
case CATCH_ITER_1:
- current_catcher->state = CATCHER_RUNNING;
+ catchers.front ().state = CATCHER_RUNNING;
return 0;
case CATCH_THROWING:
- current_catcher->state = CATCHER_ABORTING;
+ catchers.front ().state = CATCHER_ABORTING;
/* See also throw_exception. */
return 1;
default:
@@ -152,8 +132,8 @@ int
exceptions_state_mc_catch (struct gdb_exception *exception,
int mask)
{
- *exception = current_catcher->exception;
- catcher_pop ();
+ *exception = std::move (catchers.front ().exception);
+ catchers.pop_front ();
if (exception->reason < 0)
{
@@ -185,29 +165,6 @@ exceptions_state_mc_action_iter_1 (void)
return exceptions_state_mc (CATCH_ITER_1);
}
-/* How many nested TRY blocks we have. See exception_messages and
- throw_it. */
-
-static int try_scope_depth;
-
-/* Called on entry to a TRY scope. */
-
-void *
-exception_try_scope_entry (void)
-{
- ++try_scope_depth;
- return nullptr;
-}
-
-/* Called on exit of a TRY scope, either normal exit or exception
- exit. */
-
-void
-exception_try_scope_exit (void *saved_state)
-{
- --try_scope_depth;
-}
-
/* Called by the default catch block. IOW, we'll get here before
jumping out to the next outermost scope an exception if a GDB
exception is not caught. */
@@ -218,14 +175,6 @@ exception_rethrow (void)
throw;
}
-/* Copy the 'gdb_exception' portion of FROM to TO. */
-
-static void
-gdb_exception_sliced_copy (struct gdb_exception *to, const struct gdb_exception *from)
-{
- *to = *from;
-}
-
/* Return EXCEPTION to the nearest containing CATCH_SJLJ block. */
void
@@ -235,8 +184,8 @@ throw_exception_sjlj (struct gdb_exception exception)
that call via setjmp's return value. Note that REASON can't be
zero, by definition in common-exceptions.h. */
exceptions_state_mc (CATCH_THROWING);
- current_catcher->exception = exception;
- longjmp (current_catcher->buf, exception.reason);
+ catchers.front ().exception = exception;
+ longjmp (catchers.front ().buf, exception.reason);
}
/* Implementation of throw_exception that uses C++ try/catch. */
@@ -246,16 +195,12 @@ throw_exception_cxx (struct gdb_exception exception)
{
if (exception.reason == RETURN_QUIT)
{
- gdb_exception_RETURN_MASK_QUIT ex;
-
- gdb_exception_sliced_copy (&ex, &exception);
+ gdb_exception_RETURN_MASK_QUIT ex (exception);
throw ex;
}
else if (exception.reason == RETURN_ERROR)
{
- gdb_exception_RETURN_MASK_ERROR ex;
-
- gdb_exception_sliced_copy (&ex, &exception);
+ gdb_exception_RETURN_MASK_ERROR ex (exception);
throw ex;
}
else
@@ -268,52 +213,16 @@ throw_exception (struct gdb_exception exception)
throw_exception_cxx (exception);
}
-/* A stack of exception messages.
- This is needed to handle nested calls to throw_it: we don't want to
- xfree space for a message before it's used.
- This can happen if we throw an exception during a cleanup:
- An outer TRY_CATCH may have an exception message it wants to print,
- but while doing cleanups further calls to throw_it are made.
-
- This is indexed by the size of the current_catcher list.
- It is a dynamically allocated array so that we don't care how deeply
- GDB nests its TRY_CATCHs. */
-static char **exception_messages;
-
-/* The number of currently allocated entries in exception_messages. */
-static int exception_messages_size;
-
static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
throw_it (enum return_reason reason, enum errors error, const char *fmt,
va_list ap)
{
struct gdb_exception e;
- char *new_message;
- int depth = try_scope_depth;
-
- gdb_assert (depth > 0);
-
- /* Note: The new message may use an old message's text. */
- new_message = xstrvprintf (fmt, ap);
-
- if (depth > exception_messages_size)
- {
- int old_size = exception_messages_size;
-
- exception_messages_size = depth + 10;
- exception_messages = XRESIZEVEC (char *, exception_messages,
- exception_messages_size);
- memset (exception_messages + old_size, 0,
- (exception_messages_size - old_size) * sizeof (char *));
- }
-
- xfree (exception_messages[depth - 1]);
- exception_messages[depth - 1] = new_message;
/* Create the exception. */
e.reason = reason;
e.error = error;
- e.message = new_message;
+ e.message.reset (new std::string (string_vprintf (fmt, ap)));
/* Throw the exception. */
throw_exception (e);
diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h
index 6cc09ea..54e7404 100644
--- a/gdb/common/common-exceptions.h
+++ b/gdb/common/common-exceptions.h
@@ -22,6 +22,7 @@
#include <setjmp.h>
#include <new>
+#include <memory>
/* Reasons for calling throw_exceptions(). NOTE: all reason values
must be different from zero. enum value 0 is reserved for internal
@@ -110,9 +111,47 @@ enum errors {
struct gdb_exception
{
+ gdb_exception ()
+ : reason ((enum return_reason) 0),
+ error (GDB_NO_ERROR)
+ {
+ }
+
+ gdb_exception (enum return_reason r, enum errors e)
+ : reason (r),
+ error (e)
+ {
+ }
+
+ /* The copy constructor exists so that we can mark it "noexcept",
+ which is a good practice for any sort of exception object. */
+ gdb_exception (const gdb_exception &other) noexcept
+ : reason (other.reason),
+ error (other.error),
+ message (other.message)
+ {
+ }
+
+ /* The assignment operator exists so that we can mark it "noexcept",
+ which is a good practice for any sort of exception object. */
+ gdb_exception &operator= (const gdb_exception &other) noexcept
+ {
+ reason = other.reason;
+ error = other.error;
+ message = other.message;
+ return *this;
+ }
+
+ /* Return the contents of the exception message, as a C string. The
+ string remains owned by the exception object. */
+ const char *what () const noexcept
+ {
+ return message->c_str ();
+ }
+
enum return_reason reason;
enum errors error;
- const char *message;
+ std::shared_ptr<std::string> message;
};
/* Functions to drive the sjlj-based exceptions state machine. Though
@@ -127,8 +166,6 @@ extern int exceptions_state_mc_catch (struct gdb_exception *, int);
/* For the C++ try/catch-based TRY/CATCH mechanism. */
-extern void *exception_try_scope_entry (void);
-extern void exception_try_scope_exit (void *saved_state);
extern void exception_rethrow (void) ATTRIBUTE_NORETURN;
/* Macro to wrap up standard try/catch behavior.
@@ -178,23 +215,6 @@ extern void exception_rethrow (void) ATTRIBUTE_NORETURN;
#define END_CATCH_SJLJ \
}
-/* Prevent error/quit during TRY from calling cleanups established
- prior to here. This pops out the scope in either case of normal
- exit or exception exit. */
-struct exception_try_scope
-{
- exception_try_scope ()
- {
- saved_state = exception_try_scope_entry ();
- }
- ~exception_try_scope ()
- {
- exception_try_scope_exit (saved_state);
- }
-
- void *saved_state;
-};
-
/* We still need to wrap TRY/CATCH in C++ so that cleanups and C++
exceptions can coexist.
@@ -214,7 +234,6 @@ struct exception_try_scope
{ \
try \
{ \
- exception_try_scope exception_try_scope_instance; \
do \
{
@@ -236,14 +255,26 @@ struct exception_try_scope
struct gdb_exception_RETURN_MASK_ALL : public gdb_exception
{
+ explicit gdb_exception_RETURN_MASK_ALL (const gdb_exception &ex) noexcept
+ : gdb_exception (ex)
+ {
+ }
};
struct gdb_exception_RETURN_MASK_ERROR : public gdb_exception_RETURN_MASK_ALL
{
+ explicit gdb_exception_RETURN_MASK_ERROR (const gdb_exception &ex) noexcept
+ : gdb_exception_RETURN_MASK_ALL (ex)
+ {
+ }
};
struct gdb_exception_RETURN_MASK_QUIT : public gdb_exception_RETURN_MASK_ALL
{
+ explicit gdb_exception_RETURN_MASK_QUIT (const gdb_exception &ex) noexcept
+ : gdb_exception_RETURN_MASK_ALL (ex)
+ {
+ }
};
/* An exception type that inherits from both std::bad_alloc and a gdb
@@ -256,12 +287,10 @@ struct gdb_quit_bad_alloc
: public gdb_exception_RETURN_MASK_QUIT,
public std::bad_alloc
{
- explicit gdb_quit_bad_alloc (gdb_exception ex)
- : std::bad_alloc ()
+ explicit gdb_quit_bad_alloc (const gdb_exception &ex) noexcept
+ : gdb_exception_RETURN_MASK_QUIT (ex),
+ std::bad_alloc ()
{
- gdb_exception *self = this;
-
- *self = ex;
}
};
diff --git a/gdb/common/selftest.c b/gdb/common/selftest.c
index fe060ff..1e73708 100644
--- a/gdb/common/selftest.c
+++ b/gdb/common/selftest.c
@@ -90,7 +90,7 @@ run_tests (const char *filter)
CATCH (ex, RETURN_MASK_ERROR)
{
++failed;
- debug_printf ("Self test failed: %s\n", ex.message);
+ debug_printf ("Self test failed: %s\n", ex.what ());
}
END_CATCH