diff mbox

[v2,15/22] Make exceptions use std::string and be self-managing

Message ID 20190227201849.32210-16-tom@tromey.com
State New
Headers show

Commit Message

Tom Tromey Feb. 27, 2019, 8:18 p.m. UTC
This changes the exception's "message" member to be a std::string.
This allows removing the stack of exception messages, because now
exceptions will self-destruct when needed.

Note that this does increase the cost of copying an exception.  This
will be somewhat addressed in a subsequent patch.

gdb/ChangeLog
2019-02-27  Tom Tromey  <tom@tromey.com>

	* xml-support.c (gdb_xml_parser::parse): Update.
	* x86-linux-nat.c (x86_linux_nat_target::enable_btrace): Update.
	* value.c (show_convenience): Update.
	* unittests/cli-utils-selftests.c (test_number_or_range_parser)
	(test_parse_flags_qcs): Update.
	* thread.c (thr_try_catch_cmd): Update.
	* target.c (target_translate_tls_address): Update.
	* stack.c (print_frame_arg, read_frame_local, read_frame_arg)
	(info_frame_command_core, frame_apply_command_count): Update.
	* rust-exp.y (rust_lex_exception_test): Update.
	* riscv-tdep.c (riscv_print_one_register_info): Update.
	* remote.c (remote_target::enable_btrace): Update.
	* record-btrace.c (record_btrace_enable_warn): Update.
	* python/py-utils.c (gdbpy_convert_exception): Update.
	* printcmd.c (do_one_display, print_variable_and_value): Update.
	* mi/mi-main.c (mi_print_exception): Update.
	* mi/mi-interp.c (mi_cmd_interpreter_exec): Update.
	* mi/mi-cmd-stack.c (list_arg_or_local): Update.
	* linux-nat.c (linux_nat_target::attach): Update.
	* linux-fork.c (class scoped_switch_fork_info): Update.
	* infrun.c (displaced_step_prepare): Update.
	* infcall.c (call_function_by_hand_dummy): Update.
	* guile/scm-exception.c (gdbscm_scm_from_gdb_exception): Update.
	* gnu-v3-abi.c (print_one_vtable): Update.
	* frame.c (get_prev_frame_always): Update.
	* f-valprint.c (info_common_command_for_block): Update.
	* exec.c (try_open_exec_file): Update.
	* exceptions.c (print_exception, exception_print)
	(exception_fprintf, exception_print_same): Update.
	* dwarf2-frame.c (dwarf2_build_frame_info): Update.
	* dwarf-index-cache.c (index_cache::store)
	(index_cache::lookup_gdb_index): Update.
	* darwin-nat.c (maybe_cache_shell): Update.
	* cp-valprint.c (cp_print_value_fields): Update.
	* compile/compile-cplus-symbols.c (gcc_cplus_convert_symbol)
	(gcc_cplus_symbol_address): Update.
	* compile/compile-c-symbols.c (gcc_convert_symbol)
	(gcc_symbol_address, generate_c_for_for_one_variable): Update.
	* common/selftest.c: Update.
	* common/common-exceptions.h (struct gdb_exception) <message>: Now
	a std::string.
	(exception_try_scope_entry, exception_try_scope_exit): Don't
	declare.
	(struct exception_try_scope): Remove.
	(TRY): Don't use exception_try_scope.
	* common/common-exceptions.c (exception_none): Change
	initializer.
	(struct catcher) <state, exception>: Initialize inline.
	<prev>: Remove member.
	(current_catcher): Remove.
	(catchers): New global.
	(exceptions_state_mc_init): Simplify.
	(catcher_pop): Remove.
	(exceptions_state_mc, exceptions_state_mc_catch): Update.
	(try_scope_depth, exception_try_scope_entry)
	(exception_try_scope_exit): Remove.
	(throw_exception_sjlj): Update.
	(exception_messages, exception_messages_size): Remove.
	(throw_it): Simplify.
	* cli/cli-script.c (script_from_file): Update.
	* breakpoint.c (insert_bp_location, update_breakpoint_locations):
	Update.
	* ada-valprint.c (ada_val_print): Update.
	* ada-lang.c (ada_to_fixed_type_1, ada_exception_name_addr)
	(create_excep_cond_exprs): Update.

gdb/gdbserver/ChangeLog
2019-02-27  Tom Tromey  <tom@tromey.com>

	* server.c (handle_btrace_general_set, handle_qxfer_btrace)
	(handle_qxfer_btrace_conf, detach_or_kill_for_exit_cleanup)
	(captured_main, main): Update.
	* gdbreplay.c (main): Update.
---
 gdb/ChangeLog                       |  68 ++++++++++++++++
 gdb/ada-lang.c                      |   6 +-
 gdb/ada-valprint.c                  |   2 +-
 gdb/breakpoint.c                    |  14 ++--
 gdb/cli/cli-script.c                |   3 +-
 gdb/common/common-exceptions.c      | 117 +++++-----------------------
 gdb/common/common-exceptions.h      |  22 +-----
 gdb/common/selftest.c               |   2 +-
 gdb/compile/compile-c-symbols.c     |   6 +-
 gdb/compile/compile-cplus-symbols.c |   4 +-
 gdb/cp-valprint.c                   |   2 +-
 gdb/darwin-nat.c                    |   2 +-
 gdb/dwarf-index-cache.c             |   4 +-
 gdb/dwarf2-frame.c                  |   4 +-
 gdb/exceptions.c                    |  16 +---
 gdb/exec.c                          |  14 +---
 gdb/f-valprint.c                    |   3 +-
 gdb/frame.c                         |   6 +-
 gdb/gdbserver/ChangeLog             |   7 ++
 gdb/gdbserver/gdbreplay.c           |   2 +-
 gdb/gdbserver/server.c              |  13 ++--
 gdb/gnu-v3-abi.c                    |   2 +-
 gdb/guile/scm-exception.c           |   2 +-
 gdb/infcall.c                       |   2 +-
 gdb/infrun.c                        |   4 +-
 gdb/linux-fork.c                    |   2 +-
 gdb/linux-nat.c                     |   5 +-
 gdb/mi/mi-cmd-stack.c               |   8 +-
 gdb/mi/mi-interp.c                  |   2 +-
 gdb/mi/mi-main.c                    |   4 +-
 gdb/printcmd.c                      |  11 +--
 gdb/python/py-utils.c               |   2 +-
 gdb/record-btrace.c                 |   2 +-
 gdb/remote.c                        |   4 +-
 gdb/riscv-tdep.c                    |   2 +-
 gdb/rust-exp.y                      |   2 +-
 gdb/stack.c                         |  34 ++++----
 gdb/target.c                        |   4 +-
 gdb/thread.c                        |   2 +-
 gdb/unittests/cli-utils-selftests.c |   8 +-
 gdb/value.c                         |   2 +-
 gdb/x86-linux-nat.c                 |   2 +-
 gdb/xml-support.c                   |   4 +-
 43 files changed, 197 insertions(+), 230 deletions(-)

Comments

Pedro Alves April 3, 2019, 4:20 p.m. UTC | #1
On 02/27/2019 08:18 PM, Tom Tromey wrote:
> This changes the exception's "message" member to be a std::string.
> This allows removing the stack of exception messages, because now
> exceptions will self-destruct when needed.
> 
> Note that this does increase the cost of copying an exception.  This
> will be somewhat addressed in a subsequent patch.

I have a concern about this patch, something that I alluded to
at the Cauldron.

I think it'd be better if we followed what the C++ standard library
does, and make copy constructor/assignment op nothrow:

  "Each standard library class T that derives from class exception shall have
  a publicly accessible copy constructor and a publicly accessible copy
  assignment operator that do not exit with an exception." (N4659/21.8.2)

See: 
  https://stackoverflow.com/questions/21644735/should-i-declare-the-copy-constructor-of-my-exceptions-noexcept

I believe that rule exists because throwing an exception copies
the exception object, and if that copy operation throws, then the process
is terminated with std::terminate().  Copying a std::string may require
a heap allocation, which likely doesn't throw on most OSs with
over-commit enabled, but it pedantically can.

I think fixing this isn't hard, so I think we should do it from the
start -- instead of gdb_exception holding a std::string, hold a
reference counted immutable heap-allocated C string.

>  		throw_error (except.error,
>  			     _("unable to read value of %s (%s)"),
> -			     xvz_name, except.message);
> +			     xvz_name, except.message.c_str ());

Since you're touching most (all?) lines that refer to "message",
did you consider adding a "what()" method, to model what
std::exception has?

  const char *what() const noexcept;

Maybe that helps with newcomers' familiarity?

Thanks,
Pedro Alves
Tom Tromey April 3, 2019, 5:58 p.m. UTC | #2
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> I think fixing this isn't hard, so I think we should do it from the
Pedro> start -- instead of gdb_exception holding a std::string, hold a
Pedro> reference counted immutable heap-allocated C string.

I'll do this.

How about just a std::shared_ptr<std::string>?  Or would you rather
plain std::shared_ptr<char>?  I don't recall offhand if having a real
string was handy anywhere or not.

Pedro> Since you're touching most (all?) lines that refer to "message",
Pedro> did you consider adding a "what()" method, to model what
Pedro> std::exception has?

Pedro>   const char *what() const noexcept;

Pedro> Maybe that helps with newcomers' familiarity?

I didn't think of it, but I can do this.

Tom
Pedro Alves April 3, 2019, 6:28 p.m. UTC | #3
On 04/03/2019 06:58 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> I think fixing this isn't hard, so I think we should do it from the
> Pedro> start -- instead of gdb_exception holding a std::string, hold a
> Pedro> reference counted immutable heap-allocated C string.
> 
> I'll do this.
> 
> How about just a std::shared_ptr<std::string>?  

That should be fine.

Thanks,
Pedro Alves
diff mbox

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index a6fadc846e3..9c23d8e0d5f 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -9056,7 +9056,7 @@  ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
 		   optimized out).  */
 		throw_error (except.error,
 			     _("unable to read value of %s (%s)"),
-			     xvz_name, except.message);
+			     xvz_name, except.message.c_str ());
 	      }
 	    END_CATCH
 
@@ -12365,7 +12365,7 @@  ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
 
   CATCH (e, RETURN_MASK_ERROR)
     {
-      warning (_("failed to get exception name: %s"), e.message);
+      warning (_("failed to get exception name: %s"), e.message.c_str ());
       return 0;
     }
   END_CATCH
@@ -12459,7 +12459,7 @@  create_excep_cond_exprs (struct ada_catchpoint *c,
 	    {
 	      warning (_("failed to reevaluate internal exception condition "
 			 "for catchpoint %d: %s"),
-		       c->number, e.message);
+		       c->number, e.message.c_str ());
 	    }
 	  END_CATCH
 	}
diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index f2aed32cc86..72cae3b7f47 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -1213,7 +1213,7 @@  ada_val_print (struct type *type,
   CATCH (except, RETURN_MASK_ERROR)
     {
       fprintf_filtered (stream, _("<error reading variable: %s>"),
-			except.message);
+			except.message.c_str ());
     }
   END_CATCH
 }
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 9be99ff4efa..17b4d643613 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -2667,18 +2667,18 @@  insert_bp_location (struct bp_location *bl,
 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
 		{
 		  *hw_breakpoint_error = 1;
-		  *hw_bp_error_explained_already = bp_excpt.message != NULL;
+		  *hw_bp_error_explained_already = !bp_excpt.message.empty ();
                   fprintf_unfiltered (tmp_error_stream,
                                       "Cannot insert hardware breakpoint %d%s",
                                       bl->owner->number,
-				      bp_excpt.message ? ":" : ".\n");
-                  if (bp_excpt.message != NULL)
+				      !bp_excpt.message.empty () ? ":" : ".\n");
+                  if (!bp_excpt.message.empty ())
                     fprintf_unfiltered (tmp_error_stream, "%s.\n",
-					bp_excpt.message);
+					bp_excpt.message.c_str ());
 		}
 	      else
 		{
-		  if (bp_excpt.message == NULL)
+		  if (bp_excpt.message.empty ())
 		    {
 		      std::string message
 			= memory_error_message (TARGET_XFER_E_IO,
@@ -2694,7 +2694,7 @@  insert_bp_location (struct bp_location *bl,
 		      fprintf_unfiltered (tmp_error_stream,
 					  "Cannot insert breakpoint %d: %s\n",
 					  bl->owner->number,
-					  bp_excpt.message);
+					  bp_excpt.message.c_str ());
 		    }
 		}
 	      return 1;
@@ -13529,7 +13529,7 @@  update_breakpoint_locations (struct breakpoint *b,
 	    {
 	      warning (_("failed to reevaluate condition "
 			 "for breakpoint %d: %s"), 
-		       b->number, e.message);
+		       b->number, e.message.c_str ());
 	      new_loc->enabled = 0;
 	    }
 	  END_CATCH
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 85f00c75b3f..c02c7880648 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1553,7 +1553,8 @@  script_from_file (FILE *stream, const char *file)
 	 prepended.  */
       throw_error (e.error,
 		   _("%s:%d: Error in sourced command file:\n%s"),
-		   source_file_name.c_str (), source_line_number, e.message);
+		   source_file_name.c_str (), source_line_number,
+		   e.message.c_str ());
     }
   END_CATCH
 }
diff --git a/gdb/common/common-exceptions.c b/gdb/common/common-exceptions.c
index c3529c989fd..15b59f4dd94 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.  */
@@ -235,8 +192,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.  */
@@ -268,52 +225,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 = 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 6cc09eab938..d4b30104239 100644
--- a/gdb/common/common-exceptions.h
+++ b/gdb/common/common-exceptions.h
@@ -112,7 +112,7 @@  struct gdb_exception
 {
   enum return_reason reason;
   enum errors error;
-  const char *message;
+  std::string message;
 };
 
 /* Functions to drive the sjlj-based exceptions state machine.  Though
@@ -127,8 +127,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 +176,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 +195,6 @@  struct exception_try_scope
   {									\
     try									\
       {									\
-	exception_try_scope exception_try_scope_instance;		\
 	do								\
 	  {
 
diff --git a/gdb/common/selftest.c b/gdb/common/selftest.c
index fe060ff64f1..6f972e17910 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.message.c_str ());
 	}
       END_CATCH
 
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index a0f833e6f01..1a1c10e9308 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -369,7 +369,7 @@  gcc_convert_symbol (void *datum,
 
   CATCH (e, RETURN_MASK_ALL)
     {
-      context->plugin ().error (e.message);
+      context->plugin ().error (e.message.c_str ());
     }
   END_CATCH
 
@@ -432,7 +432,7 @@  gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
 
   CATCH (e, RETURN_MASK_ERROR)
     {
-      context->plugin ().error (e.message);
+      context->plugin ().error (e.message.c_str ());
     }
   END_CATCH
 
@@ -602,7 +602,7 @@  generate_c_for_for_one_variable (compile_instance *compiler,
 
   CATCH (e, RETURN_MASK_ERROR)
     {
-      compiler->insert_symbol_error (sym, e.message);
+      compiler->insert_symbol_error (sym, e.message.c_str ());
     }
   END_CATCH
 }
diff --git a/gdb/compile/compile-cplus-symbols.c b/gdb/compile/compile-cplus-symbols.c
index 0979784466e..83289e37cdd 100644
--- a/gdb/compile/compile-cplus-symbols.c
+++ b/gdb/compile/compile-cplus-symbols.c
@@ -392,7 +392,7 @@  gcc_cplus_convert_symbol (void *datum,
     {
       /* We can't allow exceptions to escape out of this callback.  Safest
 	 is to simply emit a gcc error.  */
-      instance->plugin ().error (e.message);
+      instance->plugin ().error (e.message.c_str ());
     }
   END_CATCH
 
@@ -469,7 +469,7 @@  gcc_cplus_symbol_address (void *datum, struct gcc_cp_context *gcc_context,
 
   CATCH (e, RETURN_MASK_ERROR)
     {
-      instance->plugin ().error (e.message);
+      instance->plugin ().error (e.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
index 7124da42ec9..b4478ee644c 100644
--- a/gdb/cp-valprint.c
+++ b/gdb/cp-valprint.c
@@ -325,7 +325,7 @@  cp_print_value_fields (struct type *type, struct type *real_type,
 		    {
 		      fprintf_filtered (stream,
 					_("<error reading variable: %s>"),
-					ex.message);
+					ex.message.c_str ());
 		    }
 		  END_CATCH
 
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 8c34aa8a3f2..0a75ad3e484 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1962,7 +1962,7 @@  caching a copy of your shell.  However, this failed:\n\
 If you correct the problem, gdb will automatically try again the next time\n\
 you \"run\".  To prevent these attempts, you can use:\n\
     set startup-with-shell off"),
-		   ex.message);
+		   ex.message.c_str ());
 	  return false;
 	}
       END_CATCH
diff --git a/gdb/dwarf-index-cache.c b/gdb/dwarf-index-cache.c
index 445f8b199e6..ab5b6aa9419 100644
--- a/gdb/dwarf-index-cache.c
+++ b/gdb/dwarf-index-cache.c
@@ -133,7 +133,7 @@  index_cache::store (struct dwarf2_per_objfile *dwarf2_per_objfile)
     {
       if (debug_index_cache)
 	printf_unfiltered ("index cache: couldn't store index cache for objfile "
-			 "%s: %s", objfile_name (obj), except.message);
+			   "%s: %s", objfile_name (obj), except.message.c_str ());
     }
   END_CATCH
 }
@@ -193,7 +193,7 @@  index_cache::lookup_gdb_index (const bfd_build_id *build_id,
     {
       if (debug_index_cache)
 	printf_unfiltered ("index cache: couldn't read %s: %s\n",
-			 filename.c_str (), except.message);
+			   filename.c_str (), except.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/dwarf2-frame.c b/gdb/dwarf2-frame.c
index 178ac44ecbe..21d826b012e 100644
--- a/gdb/dwarf2-frame.c
+++ b/gdb/dwarf2-frame.c
@@ -2257,7 +2257,7 @@  dwarf2_build_frame_info (struct objfile *objfile)
 	  CATCH (e, RETURN_MASK_ERROR)
 	    {
 	      warning (_("skipping .eh_frame info of %s: %s"),
-		       objfile_name (objfile), e.message);
+		       objfile_name (objfile), e.message.c_str ());
 
 	      if (fde_table.num_entries != 0)
 		{
@@ -2298,7 +2298,7 @@  dwarf2_build_frame_info (struct objfile *objfile)
       CATCH (e, RETURN_MASK_ERROR)
 	{
 	  warning (_("skipping .debug_frame info of %s: %s"),
-		   objfile_name (objfile), e.message);
+		   objfile_name (objfile), e.message.c_str ());
 
 	  if (fde_table.num_entries != 0)
 	    {
diff --git a/gdb/exceptions.c b/gdb/exceptions.c
index a29ef0cde50..3341354ad9d 100644
--- a/gdb/exceptions.c
+++ b/gdb/exceptions.c
@@ -81,7 +81,7 @@  print_exception (struct ui_file *file, struct gdb_exception e)
   const char *start;
   const char *end;
 
-  for (start = e.message; start != NULL; start = end)
+  for (start = e.message.c_str (); start != NULL; start = end)
     {
       end = strchr (start, '\n');
       if (end == NULL)
@@ -112,7 +112,7 @@  print_exception (struct ui_file *file, struct gdb_exception e)
 void
 exception_print (struct ui_file *file, struct gdb_exception e)
 {
-  if (e.reason < 0 && e.message != NULL)
+  if (e.reason < 0 && !e.message.empty ())
     {
       print_flush ();
       print_exception (file, e);
@@ -123,7 +123,7 @@  void
 exception_fprintf (struct ui_file *file, struct gdb_exception e,
 		   const char *prefix, ...)
 {
-  if (e.reason < 0 && e.message != NULL)
+  if (e.reason < 0 && !e.message.empty ())
     {
       va_list args;
 
@@ -143,15 +143,7 @@  exception_fprintf (struct ui_file *file, struct gdb_exception e,
 int
 exception_print_same (struct gdb_exception e1, struct gdb_exception e2)
 {
-  const char *msg1 = e1.message;
-  const char *msg2 = e2.message;
-
-  if (msg1 == NULL)
-    msg1 = "";
-  if (msg2 == NULL)
-    msg2 = "";
-
   return (e1.reason == e2.reason
 	  && e1.error == e2.error
-	  && strcmp (msg1, msg2) == 0);
+	  && e1.message == e2.message);
 }
diff --git a/gdb/exec.c b/gdb/exec.c
index 77bd140a8e8..706de9e32f0 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -161,7 +161,6 @@  try_open_exec_file (const char *exec_file_host, struct inferior *inf,
      Even without a symbol file, the remote-based debugging session should
      continue normally instead of ending abruptly.  Hence we catch thrown
      errors/exceptions in the following code.  */
-  std::string saved_message;
   TRY
     {
       /* We must do this step even if exec_file_host is NULL, so that
@@ -170,17 +169,10 @@  try_open_exec_file (const char *exec_file_host, struct inferior *inf,
     }
   CATCH (err, RETURN_MASK_ERROR)
     {
-      if (err.message != NULL)
-	warning ("%s", err.message);
+      if (!err.message.empty ())
+	warning ("%s", err.message.c_str ());
 
       prev_err = err;
-
-      /* Save message so it doesn't get trashed by the catch below.  */
-      if (err.message != NULL)
-	{
-	  saved_message = err.message;
-	  prev_err.message = saved_message.c_str ();
-	}
     }
   END_CATCH
 
@@ -193,7 +185,7 @@  try_open_exec_file (const char *exec_file_host, struct inferior *inf,
       CATCH (err, RETURN_MASK_ERROR)
 	{
 	  if (!exception_print_same (prev_err, err))
-	    warning ("%s", err.message);
+	    warning ("%s", err.message.c_str ());
 	}
       END_CATCH
     }
diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
index d9fe26f1d6a..d67aad20ba7 100644
--- a/gdb/f-valprint.c
+++ b/gdb/f-valprint.c
@@ -416,7 +416,8 @@  info_common_command_for_block (const struct block *block, const char *comname,
 
 	    CATCH (except, RETURN_MASK_ERROR)
 	      {
-		printf_filtered ("<error reading variable: %s>", except.message);
+		printf_filtered ("<error reading variable: %s>",
+				 except.message.c_str ());
 	      }
 	    END_CATCH
 
diff --git a/gdb/frame.c b/gdb/frame.c
index d8b5f819f1d..20721204d5e 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -2101,7 +2101,7 @@  get_prev_frame_always (struct frame_info *this_frame)
       if (ex.error == MEMORY_ERROR)
 	{
 	  this_frame->stop_reason = UNWIND_MEMORY_ERROR;
-	  if (ex.message != NULL)
+	  if (!ex.message.empty ())
 	    {
 	      char *stop_string;
 	      size_t size;
@@ -2110,9 +2110,9 @@  get_prev_frame_always (struct frame_info *this_frame)
 	         Allocate using stack local STOP_STRING then assign the
 	         pointer to the frame, this allows the STOP_STRING on the
 	         frame to be of type 'const char *'.  */
-	      size = strlen (ex.message) + 1;
+	      size = ex.message.size () + 1;
 	      stop_string = (char *) frame_obstack_zalloc (size);
-	      memcpy (stop_string, ex.message, size);
+	      memcpy (stop_string, ex.message.c_str (), size);
 	      this_frame->stop_string = stop_string;
 	    }
 	  prev_frame = NULL;
diff --git a/gdb/gdbserver/gdbreplay.c b/gdb/gdbserver/gdbreplay.c
index bda8095839c..8b192361504 100644
--- a/gdb/gdbserver/gdbreplay.c
+++ b/gdb/gdbserver/gdbreplay.c
@@ -534,7 +534,7 @@  main (int argc, char *argv[])
       if (exception.reason == RETURN_ERROR)
 	{
 	  fflush (stdout);
-	  fprintf (stderr, "%s\n", exception.message);
+	  fprintf (stderr, "%s\n", exception.message.c_str ());
 	}
 
       exit (1);
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 25c62aad830..57570f97d98 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -474,7 +474,7 @@  handle_btrace_general_set (char *own_buf)
     }
   CATCH (exception, RETURN_MASK_ERROR)
     {
-      sprintf (own_buf, "E.%s", exception.message);
+      sprintf (own_buf, "E.%s", exception.message.c_str ());
     }
   END_CATCH
 
@@ -1884,7 +1884,7 @@  handle_qxfer_btrace (const char *annex,
 	}
       CATCH (exception, RETURN_MASK_ERROR)
 	{
-	  sprintf (cs.own_buf, "E.%s", exception.message);
+	  sprintf (cs.own_buf, "E.%s", exception.message.c_str ());
 	  result = -1;
 	}
       END_CATCH
@@ -1956,7 +1956,7 @@  handle_qxfer_btrace_conf (const char *annex,
 	}
       CATCH (exception, RETURN_MASK_ERROR)
 	{
-	  sprintf (cs.own_buf, "E.%s", exception.message);
+	  sprintf (cs.own_buf, "E.%s", exception.message.c_str ());
 	  result = -1;
 	}
       END_CATCH
@@ -3559,7 +3559,8 @@  detach_or_kill_for_exit_cleanup ()
   CATCH (exception, RETURN_MASK_ALL)
     {
       fflush (stdout);
-      fprintf (stderr, "Detach or kill failed: %s\n", exception.message);
+      fprintf (stderr, "Detach or kill failed: %s\n",
+	       exception.message.c_str ());
       exit_code = 1;
     }
   END_CATCH
@@ -3933,7 +3934,7 @@  captured_main (int argc, char *argv[])
       CATCH (exception, RETURN_MASK_ERROR)
 	{
 	  fflush (stdout);
-	  fprintf (stderr, "gdbserver: %s\n", exception.message);
+	  fprintf (stderr, "gdbserver: %s\n", exception.message.c_str ());
 
 	  if (response_needed)
 	    {
@@ -3963,7 +3964,7 @@  main (int argc, char *argv[])
       if (exception.reason == RETURN_ERROR)
 	{
 	  fflush (stdout);
-	  fprintf (stderr, "%s\n", exception.message);
+	  fprintf (stderr, "%s\n", exception.message.c_str ());
 	  fprintf (stderr, "Exiting\n");
 	  exit_code = 1;
 	}
diff --git a/gdb/gnu-v3-abi.c b/gdb/gnu-v3-abi.c
index 15c7c667001..952553f43ab 100644
--- a/gdb/gnu-v3-abi.c
+++ b/gdb/gnu-v3-abi.c
@@ -912,7 +912,7 @@  print_one_vtable (struct gdbarch *gdbarch, struct value *value,
 	}
       CATCH (ex, RETURN_MASK_ERROR)
 	{
-	  printf_filtered (_("<error: %s>"), ex.message);
+	  printf_filtered (_("<error: %s>"), ex.message.c_str ());
 	  got_error = 1;
 	}
       END_CATCH
diff --git a/gdb/guile/scm-exception.c b/gdb/guile/scm-exception.c
index e37edd01787..d502ae06435 100644
--- a/gdb/guile/scm-exception.c
+++ b/gdb/guile/scm-exception.c
@@ -446,7 +446,7 @@  gdbscm_scm_from_gdb_exception (struct gdb_exception exception)
 
   return gdbscm_make_error (key, NULL, "~A",
 			    scm_list_1 (gdbscm_scm_from_c_string
-					(exception.message)),
+					(exception.message.c_str ())),
 			    SCM_BOOL_F);
 }
 
diff --git a/gdb/infcall.c b/gdb/infcall.c
index e58ba849031..45cf24f6393 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -1190,7 +1190,7 @@  An error occurred while in a function called from GDB.\n\
 Evaluation of the expression containing the function\n\
 (%s) will be abandoned.\n\
 When the function is done executing, GDB will silently stop."),
-		       e.message, name);
+		       e.message.c_str (), name);
 	case RETURN_QUIT:
 	default:
 	  throw_exception (e);
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 582c1f77008..b8e1e1fbfd8 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1790,7 +1790,7 @@  displaced_step_prepare (thread_info *thread)
 	{
 	  fprintf_unfiltered (gdb_stdlog,
 			      "infrun: disabling displaced stepping: %s\n",
-			      ex.message);
+			      ex.message.c_str ());
 	}
 
       /* Be verbose if "set displaced-stepping" is "on", silent if
@@ -1798,7 +1798,7 @@  displaced_step_prepare (thread_info *thread)
       if (can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
 	{
 	  warning (_("disabling displaced stepping: %s"),
-		   ex.message);
+		   ex.message.c_str ());
 	}
 
       /* Disable further displaced stepping attempts.  */
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index b1b390c5c6e..5a97dfa23dc 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -481,7 +481,7 @@  public:
 	CATCH (ex, RETURN_MASK_ALL)
 	  {
 	    warning (_("Couldn't restore checkpoint state in %s: %s"),
-		     target_pid_to_str (m_oldfp->ptid), ex.message);
+		     target_pid_to_str (m_oldfp->ptid), ex.message.c_str ());
 	  }
 	END_CATCH
       }
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 063afe26666..875e6f66cc1 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -1199,9 +1199,10 @@  linux_nat_target::attach (const char *args, int from_tty)
       std::string reason = linux_ptrace_attach_fail_reason (pid);
 
       if (!reason.empty ())
-	throw_error (ex.error, "warning: %s\n%s", reason.c_str (), ex.message);
+	throw_error (ex.error, "warning: %s\n%s", reason.c_str (),
+		     ex.message.c_str ());
       else
-	throw_error (ex.error, "%s", ex.message);
+	throw_error (ex.error, "%s", ex.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index 119e887e6b3..e55c835a75f 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -527,7 +527,7 @@  list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
 
   if (arg->val || arg->error)
     {
-      const char *error_message = NULL;
+      std::string error_message;
 
       if (arg->error)
 	error_message = arg->error;
@@ -544,12 +544,12 @@  list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
 	    }
 	  CATCH (except, RETURN_MASK_ERROR)
 	    {
-	      error_message = except.message;
+	      error_message = std::move (except.message);
 	    }
 	  END_CATCH
 	}
-      if (error_message != NULL)
-	stb.printf (_("<error reading variable: %s>"), error_message);
+      if (!error_message.empty ())
+	stb.printf (_("<error reading variable: %s>"), error_message.c_str ());
       uiout->field_stream ("value", stb);
     }
 }
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 3e9f36897a8..b3a8094f425 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -232,7 +232,7 @@  mi_cmd_interpreter_exec (const char *command, char **argv, int argc)
 
       if (e.reason < 0)
 	{
-	  mi_error_message = e.message;
+	  mi_error_message = std::move (e.message);
 	  break;
 	}
     }
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 46bc928d9fe..4f12218ea69 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1881,10 +1881,10 @@  mi_print_exception (const char *token, struct gdb_exception exception)
 
   fputs_unfiltered (token, mi->raw_stdout);
   fputs_unfiltered ("^error,msg=\"", mi->raw_stdout);
-  if (exception.message == NULL)
+  if (exception.message.empty ())
     fputs_unfiltered ("unknown error", mi->raw_stdout);
   else
-    fputstr_unfiltered (exception.message, '"', mi->raw_stdout);
+    fputstr_unfiltered (exception.message.c_str (), '"', mi->raw_stdout);
   fputs_unfiltered ("\"", mi->raw_stdout);
 
   switch (exception.error)
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 03763d577ab..46d242ddb11 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -1893,7 +1893,7 @@  do_one_display (struct display *d)
 	  /* Can't re-parse the expression.  Disable this display item.  */
 	  d->enabled_p = 0;
 	  warning (_("Unable to display \"%s\": %s"),
-		   d->exp_string, ex.message);
+		   d->exp_string, ex.message.c_str ());
 	  return;
 	}
       END_CATCH
@@ -1956,7 +1956,8 @@  do_one_display (struct display *d)
 	}
       CATCH (ex, RETURN_MASK_ERROR)
 	{
-	  fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
+	  fprintf_filtered (gdb_stdout, _("<error: %s>\n"),
+			    ex.message.c_str ());
 	}
       END_CATCH
     }
@@ -1990,7 +1991,7 @@  do_one_display (struct display *d)
 	}
       CATCH (ex, RETURN_MASK_ERROR)
 	{
-	  fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
+	  fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message.c_str ());
 	}
       END_CATCH
 
@@ -2195,8 +2196,8 @@  print_variable_and_value (const char *name, struct symbol *var,
     }
   CATCH (except, RETURN_MASK_ERROR)
     {
-      fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
-		       except.message);
+      fprintf_filtered (stream, "<error reading variable %s (%s)>", name,
+			except.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index a380b34afe8..e482f90a1d9 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -247,7 +247,7 @@  gdbpy_convert_exception (struct gdb_exception exception)
   else
     exc_class = gdbpy_gdb_error;
 
-  PyErr_Format (exc_class, "%s", exception.message);
+  PyErr_Format (exc_class, "%s", exception.message.c_str ());
 }
 
 /* Converts OBJ to a CORE_ADDR value.
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index dc26938f0b9..da2d246f723 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -288,7 +288,7 @@  record_btrace_enable_warn (struct thread_info *tp)
     }
   CATCH (error, RETURN_MASK_ERROR)
     {
-      warning ("%s", error.message);
+      warning ("%s", error.message.c_str ());
     }
   END_CATCH
 }
diff --git a/gdb/remote.c b/gdb/remote.c
index 5f658deefaa..eb74c23520a 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -13803,8 +13803,8 @@  remote_target::enable_btrace (ptid_t ptid, const struct btrace_config *conf)
     }
   CATCH (err, RETURN_MASK_ERROR)
     {
-      if (err.message != NULL)
-	warning ("%s", err.message);
+      if (!err.message.empty ())
+	warning ("%s", err.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/riscv-tdep.c b/gdb/riscv-tdep.c
index 8a996f32d39..d93e52c412b 100644
--- a/gdb/riscv-tdep.c
+++ b/gdb/riscv-tdep.c
@@ -660,7 +660,7 @@  riscv_print_one_register_info (struct gdbarch *gdbarch,
     {
       /* Handle failure to read a register without interrupting the entire
          'info registers' flow.  */
-      fprintf_filtered (file, "%s\n", ex.message);
+      fprintf_filtered (file, "%s\n", ex.message.c_str ());
       return;
     }
   END_CATCH
diff --git a/gdb/rust-exp.y b/gdb/rust-exp.y
index 9b9735a5d03..51a6fdd5b10 100644
--- a/gdb/rust-exp.y
+++ b/gdb/rust-exp.y
@@ -2604,7 +2604,7 @@  rust_lex_exception_test (rust_parser *parser, const char *input,
     }
   CATCH (except, RETURN_MASK_ERROR)
     {
-      SELF_CHECK (strcmp (except.message, err) == 0);
+      SELF_CHECK (except.message == err);
     }
   END_CATCH
 }
diff --git a/gdb/stack.c b/gdb/stack.c
index bce8d58f543..bce46392909 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -229,7 +229,6 @@  static void
 print_frame_arg (const struct frame_arg *arg)
 {
   struct ui_out *uiout = current_uiout;
-  const char *error_message = NULL;
 
   string_file stb;
 
@@ -264,6 +263,7 @@  print_frame_arg (const struct frame_arg *arg)
     uiout->text ("...");
   else
     {
+      std::string error_message;
       if (arg->error)
 	error_message = arg->error;
       else
@@ -299,12 +299,12 @@  print_frame_arg (const struct frame_arg *arg)
 	    }
 	  CATCH (except, RETURN_MASK_ERROR)
 	    {
-	      error_message = except.message;
+	      error_message = std::move (except.message);
 	    }
 	  END_CATCH
 	}
-      if (error_message != NULL)
-	stb.printf (_("<error reading variable: %s>"), error_message);
+      if (!error_message.empty ())
+	stb.printf (_("<error reading variable: %s>"), error_message.c_str ());
     }
 
   uiout->field_stream ("value", stb);
@@ -328,7 +328,7 @@  read_frame_local (struct symbol *sym, struct frame_info *frame,
     }
   CATCH (except, RETURN_MASK_ERROR)
     {
-      argp->error = xstrdup (except.message);
+      argp->error = xstrdup (except.message.c_str ());
     }
   END_CATCH
 }
@@ -354,8 +354,8 @@  read_frame_arg (struct symbol *sym, struct frame_info *frame,
 	}
       CATCH (except, RETURN_MASK_ERROR)
 	{
-	  val_error = (char *) alloca (strlen (except.message) + 1);
-	  strcpy (val_error, except.message);
+	  val_error = (char *) alloca (except.message.size () + 1);
+	  strcpy (val_error, except.message.c_str ());
 	}
       END_CATCH
     }
@@ -377,8 +377,8 @@  read_frame_arg (struct symbol *sym, struct frame_info *frame,
 	{
 	  if (except.error != NO_ENTRY_VALUE_ERROR)
 	    {
-	      entryval_error = (char *) alloca (strlen (except.message) + 1);
-	      strcpy (entryval_error, except.message);
+	      entryval_error = (char *) alloca (except.message.size () + 1);
+	      strcpy (entryval_error, except.message.c_str ());
 	    }
 	}
       END_CATCH
@@ -436,10 +436,11 @@  read_frame_arg (struct symbol *sym, struct frame_info *frame,
 			 fetched do not display anything.  */
 		      if (except.error == NO_ENTRY_VALUE_ERROR)
 			val_equal = 1;
-		      else if (except.message != NULL)
+		      else if (!except.message.empty ())
 			{
-			  entryval_error = (char *) alloca (strlen (except.message) + 1);
-			  strcpy (entryval_error, except.message);
+			  entryval_error
+			    = (char *) alloca (except.message.size () + 1);
+			  strcpy (entryval_error, except.message.c_str ());
 			}
 		    }
 		  END_CATCH
@@ -480,8 +481,8 @@  read_frame_arg (struct symbol *sym, struct frame_info *frame,
 	    }
 	  CATCH (except, RETURN_MASK_ERROR)
 	    {
-	      val_error = (char *) alloca (strlen (except.message) + 1);
-	      strcpy (val_error, except.message);
+	      val_error = (char *) alloca (except.message.size () + 1);
+	      strcpy (val_error, except.message.c_str ());
 	    }
 	  END_CATCH
 	}
@@ -1410,7 +1411,8 @@  info_frame_command_core (struct frame_info *fi, bool selected_frame_p)
 	      val_print_not_saved (gdb_stdout);
 	      break;
 	    default:
-	      fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
+	      fprintf_filtered (gdb_stdout, _("<error: %s>"),
+				ex.message.c_str ());
 	      break;
 	    }
 	}
@@ -2723,7 +2725,7 @@  frame_apply_command_count (const char *which_command,
 	      if (!flags.quiet)
 		print_stack_frame (fi, 1, LOCATION, 0);
 	      if (flags.cont)
-		printf_filtered ("%s\n", ex.message);
+		printf_filtered ("%s\n", ex.message.c_str ());
 	      else
 		throw_exception (ex);
 	    }
diff --git a/gdb/target.c b/gdb/target.c
index 116510e8cb8..b27ecb49e0e 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -754,12 +754,12 @@  target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
 		error (_("Cannot find thread-local storage for %s, "
 		         "shared library %s:\n%s"),
 		       target_pid_to_str (ptid),
-		       objfile_name (objfile), ex.message);
+		       objfile_name (objfile), ex.message.c_str ());
 	      else
 		error (_("Cannot find thread-local storage for %s, "
 		         "executable file %s:\n%s"),
 		       target_pid_to_str (ptid),
-		       objfile_name (objfile), ex.message);
+		       objfile_name (objfile), ex.message.c_str ());
 	      break;
 	    default:
 	      throw_exception (ex);
diff --git a/gdb/thread.c b/gdb/thread.c
index 6c232529646..5ec898838f0 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1479,7 +1479,7 @@  thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
 			     print_thread_id (thr),
 			     target_pid_to_str (inferior_ptid));
 	  if (flags.cont)
-	    printf_filtered ("%s\n", ex.message);
+	    printf_filtered ("%s\n", ex.message.c_str ());
 	  else
 	    throw_exception (ex);
 	}
diff --git a/gdb/unittests/cli-utils-selftests.c b/gdb/unittests/cli-utils-selftests.c
index dd4a7a0f0f9..1aaa6e2b00d 100644
--- a/gdb/unittests/cli-utils-selftests.c
+++ b/gdb/unittests/cli-utils-selftests.c
@@ -87,7 +87,7 @@  test_number_or_range_parser ()
       {
 	SELF_CHECK (ex.reason == RETURN_ERROR);
 	SELF_CHECK (ex.error == GENERIC_ERROR);
-	SELF_CHECK (strcmp (ex.message, "negative value") == 0);
+	SELF_CHECK (ex.message == "negative value");
 	SELF_CHECK (strcmp (minus_one.cur_tok (), "-1") == 0);
       }
     END_CATCH;
@@ -225,9 +225,9 @@  test_parse_flags_qcs ()
 	SELF_CHECK (ex.reason == RETURN_ERROR);
 	SELF_CHECK (ex.error == GENERIC_ERROR);
 	SELF_CHECK
-	  (strcmp (ex.message,
-		   "test_parse_flags_qcs.t4.cs: "
-		   "-c and -s are mutually exclusive") == 0);
+	  (ex.message
+	   == "test_parse_flags_qcs.t4.cs: "
+	   "-c and -s are mutually exclusive");
       }
     END_CATCH;
   }
diff --git a/gdb/value.c b/gdb/value.c
index 95b6a56f32e..9bbffbd9a9c 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2544,7 +2544,7 @@  show_convenience (const char *ignore, int from_tty)
 	}
       CATCH (ex, RETURN_MASK_ERROR)
 	{
-	  fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
+	  fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message.c_str ());
 	}
       END_CATCH
 
diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c
index fd15dc9b872..f1d63bf8bff 100644
--- a/gdb/x86-linux-nat.c
+++ b/gdb/x86-linux-nat.c
@@ -224,7 +224,7 @@  x86_linux_nat_target::enable_btrace (ptid_t ptid,
   CATCH (exception, RETURN_MASK_ERROR)
     {
       error (_("Could not enable branch tracing for %s: %s"),
-	     target_pid_to_str (ptid), exception.message);
+	     target_pid_to_str (ptid), exception.message.c_str ());
     }
   END_CATCH
 
diff --git a/gdb/xml-support.c b/gdb/xml-support.c
index 2b19a000471..a176a0a54fb 100644
--- a/gdb/xml-support.c
+++ b/gdb/xml-support.c
@@ -594,8 +594,8 @@  gdb_xml_parser::parse (const char *buffer)
   if (m_error.reason == RETURN_ERROR
       && m_error.error == XML_PARSE_ERROR)
     {
-      gdb_assert (m_error.message != NULL);
-      error_string = m_error.message;
+      gdb_assert (!m_error.message.empty ());
+      error_string = m_error.message.c_str ();
     }
   else if (status == XML_STATUS_ERROR)
     {