gdb/mi: New commands to catch C++ exceptions

Message ID 20190509000500.20536-1-andrew.burgess@embecosm.com
State New, archived
Headers

Commit Message

Andrew Burgess May 9, 2019, 12:05 a.m. UTC
  Adds some MI commands to catch C++ exceptions.  These are
-catch-throw, -catch-rethrow, and -catch-catch, these all correspond
to the CLI commands 'catch throw', 'catch rethrow', and 'catch catch'.

Each MI command takes two optional arguments, '-t' has the effect of
calling 'tcatch' instead of 'catch', for example:

   -catch-throw -t

Is the same as:

   (gdb) tcatch throw

There is also a '-r REGEXP' argument that can supply a regexp to match
against the exception type, so:

   -catch-catch -r PATTERN

Is the same as:

   (gdb) catch catch PATTERN

gdb/ChangeLog:

	* NEWS: Mention new MI commands.
	* break-catch-throw.c (enum exception_event_kind): Move to
	breakpoint.h.
	(catch_exception_command_1): Rename to...
	(catch_exception_event): ...this, make non-static, update header
	command, and change some parameter types.
	(catch_catch_command): Update for changes to
	catch_exception_command_1.
	(catch_throw_command): Likewise.
	(catch_rethrow_command): Likewise.
	* breakpoint.c (enum exception_event_kind): Delete.
	* breakpoint.h (enum exception_event_kind): Moved here from
	break-catch-throw.c.
	(catch_exception_event): Declare.
	* mi/mi-cmd-catch.c (mi_cmd_catch_exception_event): New function.
	(mi_cmd_catch_throw): New function.
	(mi_cmd_catch_rethrow): New function.
	(mi_cmd_catch_catch): New function.
	* mi/mi-cmds.c (mi_cmds): Add 'catch-throw', 'catch-rethrow', and
	'catch-catch' entries.
	* mi/mi-cmds.h (mi_cmd_catch_throw): Declare.
	(mi_cmd_catch_rethrow): Declare.
	(mi_cmd_catch_catch): Declare.

gdb/doc/ChangeLog:

	* gdb.texinfo (GDB/MI Catchpoint Commands): Add menu entry to new
	node.
	(C++ Exception GDB/MI Catchpoint Commands): New node to describe
	new MI commands.

gdb/testsuite/ChangeLog:

	* gdb.mi/mi-catch-cpp-exceptions.cc: New file.
	* gdb.mi/mi-catch-cpp-exceptions.exp: New file.
	* lib/mi-support.exp (mi_expect_stop): Handle 'exception-caught'
	as a stop reason.
---
 gdb/ChangeLog                                    |  26 +++
 gdb/NEWS                                         |   4 +
 gdb/break-catch-throw.c                          |  30 ++--
 gdb/breakpoint.c                                 |   8 -
 gdb/breakpoint.h                                 |  19 +++
 gdb/doc/ChangeLog                                |   7 +
 gdb/doc/gdb.texinfo                              | 125 ++++++++++++++
 gdb/mi/mi-cmd-catch.c                            |  70 ++++++++
 gdb/mi/mi-cmds.c                                 |   6 +
 gdb/mi/mi-cmds.h                                 |   3 +
 gdb/testsuite/ChangeLog                          |   7 +
 gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc  |  73 ++++++++
 gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp | 202 +++++++++++++++++++++++
 gdb/testsuite/lib/mi-support.exp                 |  11 +-
 14 files changed, 560 insertions(+), 31 deletions(-)
 create mode 100644 gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc
 create mode 100644 gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
  

Comments

Tom Tromey May 9, 2019, 9:50 p.m. UTC | #1
>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

Andrew> Adds some MI commands to catch C++ exceptions.  These are
Andrew> -catch-throw, -catch-rethrow, and -catch-catch, these all correspond
Andrew> to the CLI commands 'catch throw', 'catch rethrow', and 'catch catch'.

Thanks for the patch.

I read through this and it all seems good to me (of course Eli will have
to approve the docs).

I had one tiny nit.

Andrew> +void catch_exception_event (enum exception_event_kind ex_event,
Andrew> +			    const char *regex, bool tempflag, int from_tty);

The style in breakpoint.h seems to be to use "extern void ...".

Tom
  
Eli Zaretskii May 10, 2019, 7:01 a.m. UTC | #2
> From: Andrew Burgess <andrew.burgess@embecosm.com>
> Cc: Andrew Burgess <andrew.burgess@embecosm.com>
> Date: Thu,  9 May 2019 01:05:00 +0100
> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 288615b8cd3..e350a18f1f2 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -27,6 +27,10 @@
>       'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
>       'static_members', 'max_elements', 'repeat_threshold', and 'format'.
>  
> +* MI changes
> +
> +  ** New command -catch-throw, -catch-rethrow, and -catch-catch.
            ^^^^^^^
That should be "commands", right?

> +@node C++ Exception GDB/MI Catchpoint Commands
> +@subsection C++ Exception @sc{gdb/mi} Catchpoints

@node cannot have markup, but @subsection can.  Our convention is to
use C@t{++} for "C++", as that makes it look prettier in print.
Please use this markup in @subsection and elsewhere in the text.

> +@smallexample
> + -catch-throw [ -t ] [ -r @var{regexp}]
> +@end smallexample
> +
> +Catch the throwing of a C@t{++} exception.

"Catch the throwing" is ambiguous, because the meaning of "catch" here
is unclear.  The section of the manual that describes the
corresponding CLI commands uses a more clear description: "Stop when
@var{event} occurs."  So I suggest to use the same:

  Stop when the debuggee throws a C@t{++} exception.

>                                               If @var{regexp} is given
> +then only exceptions whose type matches the regular expression will be
> +caught.

A comma is missing after "given".

> +If @samp{-t} is given then the catchpoint is enabled only for one

Likewise.

> +stop, the catchpoint is automatically deleted after the first event is
> +caught.

Again, please don't use "caught", but instead "after stopping once for
the event."

> +The possible optional parameters for this command are:
> +
> +@table @samp
> +@item -r @var{regexp}
> +Use @var{regexp} to match against the exception type.
> +@item -t
> +Create a temporary catchpoint.
> +@end table

This part sounds like a repetition to me.

> +@subsubheading @value{GDBN} Command
> +
> +The corresponding @value{GDBN} commands are @samp{catch throw}
> +and @samp{tcatch throw}.

Please add a cross-reference to where these CLI commands are
described.

> +@subsubheading Example
> +
> +@smallexample
> +-catch-throw -r exception_type
> +^done,bkptno="1"
> +(gdb)
> +@end smallexample

IMO, this example is not useful, because it doesn't show the effect of
the used features.  How about adding the output when the program is
stopped because it throws a matching exception?  Then you'd be able to
show that the type is matched as a regexp, not as a literal string.

> +@smallexample
> + -catch-rethrow [ -t ] [ -r @var{regexp}]
> +@end smallexample
> +
> +Catch the rethrowing of a C@t{++} exception.  If @var{regexp} is given
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"Stop when a C@t{++} exception is re-thrown."

A comma is missing after "given".

> +If @samp{-t} is given then the catchpoint is enabled only for one

Likewise.

> +The possible optional parameters for this command are:
> +
> +@table @samp
> +@item -r @var{regexp}
> +Use @var{regexp} to match against the exception type.
> +@item -t
> +Create a temporary catchpoint.
> +@end table

Repetition again.

> +@subsubheading @value{GDBN} Command
> +
> +The corresponding @value{GDBN} commands are @samp{catch rethrow}
> +and @samp{tcatch rethrow}.

Again, please add a cross-reference.

> +@smallexample
> +-catch-rethrow -r exception_type
> +^done,bkptno="1"
> +(gdb)
> +@end smallexample

Same comment as for the previous example.

> +@smallexample
> + -catch-catch [ -t ] [ -r @var{regexp}]
> +@end smallexample
> +
> +Catch the catching of a C@t{++} exception.  If @var{regexp} is given

Same comments as above.

Btw, as the text you use to describe all 3 commands is extremely
similar, maybe you should describe them in a single subheading,
instead of copying the same text 3 times?

Thanks.
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 288615b8cd3..e350a18f1f2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -27,6 +27,10 @@ 
      'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
      'static_members', 'max_elements', 'repeat_threshold', and 'format'.
 
+* MI changes
+
+  ** New command -catch-throw, -catch-rethrow, and -catch-catch.
+
 * New commands
 
 set may-call-functions [on|off]
diff --git a/gdb/break-catch-throw.c b/gdb/break-catch-throw.c
index a221cb31518..bb9507ee45b 100644
--- a/gdb/break-catch-throw.c
+++ b/gdb/break-catch-throw.c
@@ -37,14 +37,6 @@ 
 #include "cp-support.h"
 #include "location.h"
 
-/* Enums for exception-handling support.  */
-enum exception_event_kind
-{
-  EX_EVENT_THROW,
-  EX_EVENT_RETHROW,
-  EX_EVENT_CATCH
-};
-
 /* Each spot where we may place an exception-related catchpoint has
    two names: the SDT probe point and the function name.  This
    structure holds both.  */
@@ -420,13 +412,11 @@  extract_exception_regexp (const char **string)
   return std::string ();
 }
 
-/* Deal with "catch catch", "catch throw", and "catch rethrow"
-   commands.  */
+/* See breakpoint.h.  */
 
-static void
-catch_exception_command_1 (enum exception_event_kind ex_event,
-			   const char *arg,
-			   int tempflag, int from_tty)
+void
+catch_exception_event (enum exception_event_kind ex_event,
+		       const char *arg, bool tempflag, int from_tty)
 {
   const char *cond_string = NULL;
 
@@ -456,9 +446,9 @@  static void
 catch_catch_command (const char *arg, int from_tty,
 		     struct cmd_list_element *command)
 {
-  int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
+  bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
 
-  catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
+  catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
 }
 
 /* Implementation of "catch throw" command.  */
@@ -467,9 +457,9 @@  static void
 catch_throw_command (const char *arg, int from_tty,
 		     struct cmd_list_element *command)
 {
-  int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
+  bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
 
-  catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
+  catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
 }
 
 /* Implementation of "catch rethrow" command.  */
@@ -478,9 +468,9 @@  static void
 catch_rethrow_command (const char *arg, int from_tty,
 		       struct cmd_list_element *command)
 {
-  int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
+  bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
 
-  catch_exception_command_1 (EX_EVENT_RETHROW, arg, tempflag, from_tty);
+  catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
 }
 
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index f6d2f36d0a4..0d678587aa4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -85,14 +85,6 @@ 
 #include "common/array-view.h"
 #include "common/gdb_optional.h"
 
-/* Enums for exception-handling support.  */
-enum exception_event_kind
-{
-  EX_EVENT_THROW,
-  EX_EVENT_RETHROW,
-  EX_EVENT_CATCH
-};
-
 /* Prototypes for local functions.  */
 
 static void map_breakpoint_numbers (const char *,
diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h
index a91e3e334cf..7c2c3899b48 100644
--- a/gdb/breakpoint.h
+++ b/gdb/breakpoint.h
@@ -42,6 +42,16 @@  struct linespec_result;
 struct linespec_sals;
 struct inferior;
 
+/* Enum for exception-handling support in 'catch throw', 'catch rethrow',
+   'catch catch' and the MI equivalent.  */
+
+enum exception_event_kind
+{
+  EX_EVENT_THROW,
+  EX_EVENT_RETHROW,
+  EX_EVENT_CATCH
+};
+
 /* Why are we removing the breakpoint from the target?  */
 
 enum remove_bp_reason
@@ -1670,4 +1680,13 @@  extern void print_breakpoint (breakpoint *bp);
 /* Command element for the 'commands' command.  */
 extern cmd_list_element *commands_cmd_element;
 
+/* Deal with "catch catch", "catch throw", and "catch rethrow" commands and
+   the MI equivalents.  Sets up to catch events of type EX_EVENT.  When
+   TEMPFLAG is true only the next matching event is caught after which the
+   catch-point is deleted.  If REGEX is not NULL then only exceptions whose
+   type name matches REGEX will trigger the event.  */
+
+void catch_exception_event (enum exception_event_kind ex_event,
+			    const char *regex, bool tempflag, int from_tty);
+
 #endif /* !defined (BREAKPOINT_H) */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index b7f3b271d1f..16a775c226e 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -29406,6 +29406,7 @@ 
 @menu
 * Shared Library GDB/MI Catchpoint Commands::
 * Ada Exception GDB/MI Catchpoint Commands::
+* C++ Exception GDB/MI Catchpoint Commands::
 @end menu
 
 @node Shared Library GDB/MI Catchpoint Commands
@@ -29605,6 +29606,130 @@ 
 (gdb)
 @end smallexample
 
+@node C++ Exception GDB/MI Catchpoint Commands
+@subsection C++ Exception @sc{gdb/mi} Catchpoints
+
+The following @sc{gdb/mi} commands can be used to create catchpoints
+that stop the execution when C++ exceptions are being throw, rethrown,
+or caught..
+
+@subheading The @code{-catch-throw} Command
+@findex -catch-throw
+
+@subsubheading Synopsis
+
+@smallexample
+ -catch-throw [ -t ] [ -r @var{regexp}]
+@end smallexample
+
+Catch the throwing of a C@t{++} exception.  If @var{regexp} is given
+then only exceptions whose type matches the regular expression will be
+caught.
+
+If @samp{-t} is given then the catchpoint is enabled only for one
+stop, the catchpoint is automatically deleted after the first event is
+caught.
+
+The possible optional parameters for this command are:
+
+@table @samp
+@item -r @var{regexp}
+Use @var{regexp} to match against the exception type.
+@item -t
+Create a temporary catchpoint.
+@end table
+
+@subsubheading @value{GDBN} Command
+
+The corresponding @value{GDBN} commands are @samp{catch throw}
+and @samp{tcatch throw}.
+
+@subsubheading Example
+
+@smallexample
+-catch-throw -r exception_type
+^done,bkptno="1"
+(gdb)
+@end smallexample
+
+@subheading The @code{-catch-rethrow} Command
+@findex -catch-rethrow
+
+@subsubheading Synopsis
+
+@smallexample
+ -catch-rethrow [ -t ] [ -r @var{regexp}]
+@end smallexample
+
+Catch the rethrowing of a C@t{++} exception.  If @var{regexp} is given
+then only exceptions whose type matches the regular expression will be
+caught.
+
+If @samp{-t} is given then the catchpoint is enabled only for one
+stop, the catchpoint is automatically deleted after the first event is
+caught.
+
+The possible optional parameters for this command are:
+
+@table @samp
+@item -r @var{regexp}
+Use @var{regexp} to match against the exception type.
+@item -t
+Create a temporary catchpoint.
+@end table
+
+@subsubheading @value{GDBN} Command
+
+The corresponding @value{GDBN} commands are @samp{catch rethrow}
+and @samp{tcatch rethrow}.
+
+@subsubheading Example
+
+@smallexample
+-catch-rethrow -r exception_type
+^done,bkptno="1"
+(gdb)
+@end smallexample
+
+@subheading The @code{-catch-catch} Command
+@findex -catch-catch
+
+@subsubheading Synopsis
+
+@smallexample
+ -catch-catch [ -t ] [ -r @var{regexp}]
+@end smallexample
+
+Catch the catching of a C@t{++} exception.  If @var{regexp} is given
+then only exceptions whose type matches the regular expression will be
+caught.
+
+If @samp{-t} is given then the catchpoint is enabled only for one
+stop, the catchpoint is automatically deleted after the first event is
+caught.
+
+The possible optional parameters for this command are:
+
+@table @samp
+@item -r @var{regexp}
+Use @var{regexp} to match against the exception type.
+@item -t
+Create a temporary catchpoint.
+@end table
+
+@subsubheading @value{GDBN} Command
+
+The corresponding @value{GDBN} commands are @samp{catch catch}
+and @samp{tcatch catch}.
+
+@subsubheading Example
+
+@smallexample
+-catch-catch -r exception_type
+^done,bkptno="1"
+(gdb)
+@end smallexample
+
 @c %%%%%%%%%%%%%%%%%%%%%%%%%%%% SECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 @node GDB/MI Program Context
 @section @sc{gdb/mi}  Program Context
diff --git a/gdb/mi/mi-cmd-catch.c b/gdb/mi/mi-cmd-catch.c
index 87929c48ca4..282ce67c738 100644
--- a/gdb/mi/mi-cmd-catch.c
+++ b/gdb/mi/mi-cmd-catch.c
@@ -288,3 +288,73 @@  mi_cmd_catch_unload (const char *cmd, char *argv[], int argc)
   mi_catch_load_unload (0, argv, argc);
 }
 
+/* Core handler for -catch-throw, -catch-rethrow, and -catch-catch
+   commands.  The argument handling for all of these is identical, we just
+   pass KIND through to GDB's core to select the correct event type.  */
+
+static void
+mi_cmd_catch_exception_event (enum exception_event_kind kind,
+			      const char *cmd, char *argv[], int argc)
+{
+  char *regex = NULL;
+  bool temp = false;
+  int oind = 0;
+  char *oarg;
+  enum opt
+    {
+      OPT_TEMP,
+      OPT_REGEX,
+    };
+  static const struct mi_opt opts[] =
+    {
+      { "t", OPT_TEMP, 0 },
+      { "r", OPT_REGEX, 1 },
+      { 0, 0, 0 }
+    };
+
+  for (;;)
+    {
+      int opt = mi_getopt (cmd, argc, argv, opts,
+                           &oind, &oarg);
+
+      if (opt < 0)
+        break;
+
+      switch ((enum opt) opt)
+        {
+        case OPT_TEMP:
+          temp = true;
+          break;
+        case OPT_REGEX:
+	  regex = oarg;
+          break;
+        }
+    }
+
+  catch_exception_event (kind, regex, temp, 0 /* from_tty */);
+}
+
+/* Handler for -catch-throw.  */
+
+void
+mi_cmd_catch_throw (const char *cmd, char *argv[], int argc)
+{
+  mi_cmd_catch_exception_event (EX_EVENT_THROW, cmd, argv, argc);
+}
+
+/* Handler for -catch-rethrow.  */
+
+void
+mi_cmd_catch_rethrow (const char *cmd, char *argv[], int argc)
+{
+  mi_cmd_catch_exception_event (EX_EVENT_RETHROW, cmd, argv, argc);
+}
+
+/* Handler for -catch-catch.  */
+
+void
+mi_cmd_catch_catch (const char *cmd, char *argv[], int argc)
+{
+  mi_cmd_catch_exception_event (EX_EVENT_CATCH, cmd, argv, argc);
+}
+
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index fe30ac2e822..cd2ffa5fc37 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -75,6 +75,12 @@  static struct mi_cmd mi_cmds[] =
                    &mi_suppress_notification.breakpoint),
   DEF_MI_CMD_MI_1 ("catch-unload", mi_cmd_catch_unload,
                    &mi_suppress_notification.breakpoint),
+  DEF_MI_CMD_MI_1 ("catch-throw", mi_cmd_catch_throw,
+                   &mi_suppress_notification.breakpoint),
+  DEF_MI_CMD_MI_1 ("catch-rethrow", mi_cmd_catch_rethrow,
+                   &mi_suppress_notification.breakpoint),
+  DEF_MI_CMD_MI_1 ("catch-catch", mi_cmd_catch_catch,
+                   &mi_suppress_notification.breakpoint),
   DEF_MI_CMD_MI ("data-disassemble", mi_cmd_disassemble),
   DEF_MI_CMD_MI ("data-evaluate-expression", mi_cmd_data_evaluate_expression),
   DEF_MI_CMD_MI ("data-list-changed-registers",
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 7b22ce78134..5a8e99bd33c 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -44,6 +44,9 @@  extern mi_cmd_argv_ftype mi_cmd_catch_exception;
 extern mi_cmd_argv_ftype mi_cmd_catch_handlers;
 extern mi_cmd_argv_ftype mi_cmd_catch_load;
 extern mi_cmd_argv_ftype mi_cmd_catch_unload;
+extern mi_cmd_argv_ftype mi_cmd_catch_throw;
+extern mi_cmd_argv_ftype mi_cmd_catch_rethrow;
+extern mi_cmd_argv_ftype mi_cmd_catch_catch;
 extern mi_cmd_argv_ftype mi_cmd_disassemble;
 extern mi_cmd_argv_ftype mi_cmd_data_evaluate_expression;
 extern mi_cmd_argv_ftype mi_cmd_data_list_register_names;
diff --git a/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc
new file mode 100644
index 00000000000..cacda4653a9
--- /dev/null
+++ b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc
@@ -0,0 +1,73 @@ 
+/* Copyright 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+class my_exception
+{
+private:
+  int m_value;
+
+public:
+  my_exception (int v)
+    : m_value (v)
+  {
+    /* Nothing.  */
+  }
+};
+
+void
+bar ()
+{
+  my_exception ex (4);
+  throw ex;	/* Throw 1.  */
+}
+
+void
+foo ()
+{
+  for (int i = 0; i < 2; ++i)
+    {
+      try
+	{
+	  bar ();
+	}
+      catch (const my_exception &ex)	/* Catch 1.  */
+	{
+	  if (i == 1)
+	    throw;	/* Throw 2.  */
+	}
+    }
+}
+
+int
+main ()
+{
+  for (int i = 0; i < 2; ++i)
+    {
+      try
+	{
+	  foo ();
+	}
+      catch (const my_exception &ex)	/* Catch 2.  */
+	{
+	  if (i == 1)
+	    return 1;	/* Stop here.  */
+	}
+    }
+
+  return 0;
+}
+
diff --git a/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
new file mode 100644
index 00000000000..160dd570616
--- /dev/null
+++ b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
@@ -0,0 +1,202 @@ 
+# Copyright 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test the -catch-throw, -catch-rethrow, and -catch-catch MI commands.
+
+if { [skip_cplus_tests] } { continue }
+
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+standard_testfile .cc
+
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
+    untested "failed to compile"
+    return -1
+}
+
+# Grab some line numbers we'll need.
+set catch_1_lineno [gdb_get_line_number "Catch 1"]
+set catch_2_lineno [gdb_get_line_number "Catch 2"]
+set throw_1_lineno [gdb_get_line_number "Throw 1"]
+set throw_2_lineno [gdb_get_line_number "Throw 2"]
+set main_lineno [gdb_get_line_number "Stop here"]
+
+# Restart this test, load the test binary and set a breakpoint in
+# main.
+proc restart_for_test {} {
+    global srcdir subdir binfile srcfile
+    global main_lineno
+
+    if {[mi_gdb_start]} {
+	continue
+    }
+
+    mi_delete_breakpoints
+    mi_gdb_reinitialize_dir $srcdir/$subdir
+    mi_gdb_load ${binfile}
+
+    mi_runto main
+
+    mi_create_breakpoint \
+	"$srcfile:${main_lineno}" "break before exiting program" \
+	-disp keep -func "main.*" \
+	-file ".*mi-catch-cpp-exceptions.cc" -line ${main_lineno}
+}
+
+# Issue an -exec-continue then wait for GDB to catch a C++ exception
+# event in FUNC on LINE.  Use TESTNAME to make tests unique.
+proc continue_to_next_exception { func line testname } {
+    global hex
+
+    mi_send_resuming_command "exec-continue" \
+	"exec-continue"
+    mi_expect_stop "exception-caught" ".*" ".*" ".*" ".*" \
+	{} "run until an exception is caught: $testname"
+    mi_gdb_test "-stack-list-frames 1 1" \
+	"\\^done,stack=\\\[frame=\{level=\"1\",addr=\"$hex\",func=\"${func}\",.*,line=\"${line}\".*\}\\\]" \
+	"check previous frame: $testname"
+}
+
+# Issue an -exec-continue and stop at the breakpoint in main.
+proc continue_to_breakpoint_in_main {} {
+    global main_lineno
+
+    mi_send_resuming_command "exec-continue" "exec-continue to main"
+    mi_expect_stop "breakpoint-hit" "main" ".*" ".*" "${main_lineno}" \
+	{.* disp="keep"} "run until breakpoint in main"
+}
+
+# Ensure that -catch-throw will catch only throws and nothing else.
+with_test_prefix "-catch-throw" {
+    restart_for_test
+    mi_gdb_test "-catch-throw" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
+    continue_to_breakpoint_in_main
+}
+
+# Ensure that -catch-rethrow catches only rethrows and nothing else.
+with_test_prefix "-catch-rethrow" {
+    restart_for_test
+    mi_gdb_test "-catch-rethrow" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
+    continue_to_breakpoint_in_main
+}
+
+# Ensure that -catch-catch catches only catch points, and nothing
+# else.
+with_test_prefix "-catch-catch" {
+    restart_for_test
+    mi_gdb_test "-catch-catch" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
+    continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
+    continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
+    continue_to_breakpoint_in_main
+}
+
+# Now check that all of the command with a regexp that doesn't match,
+# don't trigger.
+with_test_prefix "all with invalid regexp" {
+    restart_for_test
+    mi_gdb_test "-catch-throw -r blahblah" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-rethrow -r woofwoof" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-catch -r miowmiow" \
+	"^done,bkptno=\"$decimal\""
+
+    # Would like to use 'continue_to_breakpoint_in_main' here, if
+    # there wasn't a bug that requires a use of kfail.
+
+    mi_send_resuming_command "exec-continue" \
+	"exec-continue"
+    set testname "run until breakpoint in main"
+    gdb_expect {
+	-re "could not find minimal symbol for typeinfo address.*$mi_gdb_prompt$" {
+	    kfail "gdb/24541" "${testname}"
+	}
+	-re "\\*stopped,reason=\"breakpoint-hit\".*func=\"main\".*line=\"${main_lineno}\".*$mi_gdb_prompt$" {
+	    pass "${testname}"
+	}
+	timeout {
+	    fail "${testname} (timeout)"
+	}
+    }
+}
+
+# Now check that all of the commands with a regexp that does match,
+# still trigger.
+with_test_prefix "all with valid regexp" {
+    restart_for_test
+    mi_gdb_test "-catch-throw -r my_ex" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-rethrow -r _except" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-catch -r my_exception" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
+    continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
+    continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
+    continue_to_breakpoint_in_main
+}
+
+# Check that the temporary switch works on its own.
+with_test_prefix "all with -t" {
+    restart_for_test
+    mi_gdb_test "-catch-throw -t" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-rethrow -t" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-catch -t" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
+    continue_to_breakpoint_in_main
+}
+
+# Check that the temporary switch works when used with a regexp.
+restart_for_test
+with_test_prefix "all with -t and regexp" {
+    mi_gdb_test "-catch-throw -t -r my_ex" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-rethrow -t -r _except" \
+	"^done,bkptno=\"$decimal\""
+    mi_gdb_test "-catch-catch -t -r my_exception" \
+	"^done,bkptno=\"$decimal\""
+    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
+    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
+    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
+    continue_to_breakpoint_in_main
+}
diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
index a58c4f6e119..8c2c7c84eda 100644
--- a/gdb/testsuite/lib/mi-support.exp
+++ b/gdb/testsuite/lib/mi-support.exp
@@ -1221,10 +1221,15 @@  proc mi_expect_stop { reason func args file line extra test } {
     set args "\\\[$args\\\]"
 
     set bn ""
+    set ebn ""
     if { $reason == "breakpoint-hit" } {
 	set bn {bkptno="[0-9]+",}
     } elseif { $reason == "solib-event" } {
 	set bn ".*"
+    } elseif { $reason == "exception-caught" } {
+	set ebn {bkptno="[0-9]+",}
+	set bn ".*"
+	set reason "breakpoint-hit"
     }
 
     set r ""
@@ -1235,9 +1240,9 @@  proc mi_expect_stop { reason func args file line extra test } {
 
     set a $after_reason
 
-    verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
+    verbose -log "mi_expect_stop: expecting: \\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
     gdb_expect {
-	-re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
+	-re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
 	    pass "$test"
 	    if {[array names expect_out "2,string"] != ""} {
 		return $expect_out(2,string)
@@ -1245,7 +1250,7 @@  proc mi_expect_stop { reason func args file line extra test } {
 	    # No debug info available but $file does match.
 	    return 0
 	}
-	-re "\\*stopped,${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\",arch=\"$any\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
+	-re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\",arch=\"$any\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
 	    verbose -log "got $expect_out(buffer)"
 	    fail "$test (stopped at wrong place)"
 	    return -1