1 # Copyright 2019 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Test the -catch-throw, -catch-rethrow, and -catch-catch MI commands.
18 if { [skip_cplus_tests] } { continue }
20 load_lib mi-support.exp
25 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
26 untested "failed to compile"
30 # Grab some line numbers we'll need.
31 set catch_1_lineno [gdb_get_line_number "Catch 1"]
32 set catch_2_lineno [gdb_get_line_number "Catch 2"]
33 set throw_1_lineno [gdb_get_line_number "Throw 1"]
34 set throw_2_lineno [gdb_get_line_number "Throw 2"]
35 set main_lineno [gdb_get_line_number "Stop here"]
37 # Restart this test, load the test binary and set a breakpoint in
39 proc restart_for_test {} {
40 global srcdir subdir binfile srcfile
48 mi_gdb_reinitialize_dir $srcdir/$subdir
49 mi_gdb_load ${binfile}
53 mi_create_breakpoint \
54 "$srcfile:${main_lineno}" "break before exiting program" \
55 -disp keep -func "main.*" \
56 -file ".*mi-catch-cpp-exceptions.cc" -line ${main_lineno}
59 # Issue an -exec-continue then wait for GDB to catch a C++ exception
60 # event in FUNC on LINE. Use TESTNAME to make tests unique.
61 proc continue_to_next_exception { func line testname } {
64 mi_send_resuming_command "exec-continue" \
66 mi_expect_stop "exception-caught" ".*" ".*" ".*" ".*" \
67 {} "run until an exception is caught: $testname"
68 mi_gdb_test "-stack-list-frames 1 1" \
69 "\\^done,stack=\\\[frame=\{level=\"1\",addr=\"$hex\",func=\"${func}\",.*,line=\"${line}\".*\}\\\]" \
70 "check previous frame: $testname"
73 # Issue an -exec-continue and stop at the breakpoint in main.
74 proc continue_to_breakpoint_in_main {} {
77 mi_send_resuming_command "exec-continue" "exec-continue to main"
78 mi_expect_stop "breakpoint-hit" "main" ".*" ".*" "${main_lineno}" \
79 {.* disp="keep"} "run until breakpoint in main"
82 # TYPE is one of throw, rethrow, or catch. This proc creates a catch
83 # point using -catch-TYPE. The optional string EXTRA is any extra
84 # arguments to pass when setting up the catchpoint.
85 proc setup_catchpoint {type {extra ""}} {
87 mi_gdb_test "-catch-${type} ${extra}" \
88 "\\^done,bkpt=\{number=\"$decimal\",type=\"catchpoint\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \
89 "Setup -catch-${type}"
92 # Ensure that -catch-throw will catch only throws and nothing else.
93 with_test_prefix "-catch-throw" {
95 setup_catchpoint "throw"
96 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
97 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
98 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
99 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
100 continue_to_breakpoint_in_main
103 # Ensure that -catch-rethrow catches only rethrows and nothing else.
104 with_test_prefix "-catch-rethrow" {
106 setup_catchpoint "rethrow"
107 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
108 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
109 continue_to_breakpoint_in_main
112 # Ensure that -catch-catch catches only catch points, and nothing
114 with_test_prefix "-catch-catch" {
116 setup_catchpoint "catch"
117 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
118 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
119 continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
120 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
121 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
122 continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
123 continue_to_breakpoint_in_main
126 # Now check that all of the command with a regexp that doesn't match,
128 with_test_prefix "all with invalid regexp" {
130 setup_catchpoint "throw" "-r blahblah"
131 setup_catchpoint "rethrow" "-r woofwoof"
132 setup_catchpoint "catch" "-r miowmiow"
133 continue_to_breakpoint_in_main
136 # Now check that all of the commands with a regexp that does match,
138 with_test_prefix "all with valid regexp" {
140 setup_catchpoint "throw" "-r my_ex"
141 setup_catchpoint "rethrow" "-r _except"
142 setup_catchpoint "catch" "-r my_exception"
143 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
144 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
145 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
146 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
147 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
148 continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
149 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
150 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
151 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
152 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
153 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
154 continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
155 continue_to_breakpoint_in_main
158 # Check that the temporary switch works on its own.
159 with_test_prefix "all with -t" {
161 setup_catchpoint "throw" "-t"
162 setup_catchpoint "rethrow" "-t"
163 setup_catchpoint "catch" "-t"
164 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
165 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
166 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
167 continue_to_breakpoint_in_main
170 # Check that the temporary switch works when used with a regexp.
172 with_test_prefix "all with -t and regexp" {
173 setup_catchpoint "throw" "-t -r my_ex"
174 setup_catchpoint "rethrow" "-t -r _except"
175 setup_catchpoint "catch" "-t -r my_exception"
176 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
177 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
178 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
179 continue_to_breakpoint_in_main