diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-05-08 19:01:36 +0100 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-06-15 23:22:22 +0100 |
commit | 30056ea04ae3ecd828e2a06e12e6f174ae6659c9 (patch) | |
tree | 0241b9853191c5363b2e5089b81ff95413df17c6 /gdb/testsuite/gdb.mi | |
parent | ec8e2b6d3051f0b4b2a8eee9917898e95046c62f (diff) | |
download | gdb-30056ea04ae3ecd828e2a06e12e6f174ae6659c9.zip gdb-30056ea04ae3ecd828e2a06e12e6f174ae6659c9.tar.gz gdb-30056ea04ae3ecd828e2a06e12e6f174ae6659c9.tar.bz2 |
gdb/mi: New commands to catch C++ exceptions
Adds some MI commands to catch C++ exceptions. The new commands 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:
(gdb)
-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:
(gdb)
-catch-catch -r PATTERN
Is the same as:
(gdb) catch catch PATTERN
The change in print_mention_exception_catchpoint might seem a little
strange; changing the output from using ui_out::field_int and
ui_out::text to using ui_out::message.
The print_mention_exception_catchpoint is used as the 'print_mention'
method for the exception catchpoint breakpoint object. Most of the
other 'print_mention' methods (see breakpoint.c) use either
printf_filtered, of ui_out::message. Using field_int was causing an
unexpected field to be added to the MI output. Here's the output
without the change in print_mention_exception_catchpoint:
(gdb)
-catch-throw
^done,bkptno="1",bkpt={number="1",type="breakpoint",disp="keep",
enabled="y",addr="0x00000000004006c0",
what="exception throw",catch-type="throw",
thread-groups=["i1"],times="0"}
Notice the breakpoint number appears in both the 'bkptno' field, and
the 'number' field within the 'bkpt' tuple. Here's the output with
the change in print_mention_exception_catchpoint:
(gdb)
-catch-throw
^done,bkpt={number="1",type="breakpoint",disp="keep",
enabled="y",addr="0x00000000004006c0",
what="exception throw",catch-type="throw",
thread-groups=["i1"],times="0"}
gdb/ChangeLog:
* NEWS: Mention new MI commands.
* break-catch-throw.c (enum exception_event_kind): Move to
breakpoint.h.
(print_mention_exception_catchpoint): Output text as a single
message.
(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.
Diffstat (limited to 'gdb/testsuite/gdb.mi')
-rw-r--r-- | gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.cc | 73 | ||||
-rw-r--r-- | gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp | 197 |
2 files changed, 270 insertions, 0 deletions
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 0000000..cacda46 --- /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 0000000..b5dfbe6 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp @@ -0,0 +1,197 @@ +# 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" +} + +# TYPE is one of throw, rethrow, or catch. This proc creates a catch +# point using -catch-TYPE. The optional string EXTRA is any extra +# arguments to pass when setting up the catchpoint. +proc setup_catchpoint {type {extra ""}} { + global decimal + mi_gdb_test "-catch-${type} ${extra}" \ + "\\^done,bkpt=\{number=\"$decimal\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \ + "Setup -catch-${type}" +} + +# Ensure that -catch-throw will catch only throws and nothing else. +with_test_prefix "-catch-throw" { + restart_for_test + setup_catchpoint "throw" + 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 + setup_catchpoint "rethrow" + 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 + setup_catchpoint "catch" + 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 + setup_catchpoint "throw" "-r blahblah" + setup_catchpoint "rethrow" "-r woofwoof" + setup_catchpoint "catch" "-r miowmiow" + + # 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 + setup_catchpoint "throw" "-r my_ex" + setup_catchpoint "rethrow" "-r _except" + setup_catchpoint "catch" "-r my_exception" + 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 + setup_catchpoint "throw" "-t" + setup_catchpoint "rethrow" "-t" + setup_catchpoint "catch" "-t" + 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" { + setup_catchpoint "throw" "-t -r my_ex" + setup_catchpoint "rethrow" "-t -r _except" + setup_catchpoint "catch" "-t -r my_exception" + 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 +} |