1 # Copyright 2019-2020 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"
34 mi_gdb_reinitialize_dir $srcdir/$subdir
35 mi_gdb_load ${binfile}
36 if {![mi_run_to_main]} {
39 set libstdcxx_probe_tests_supported [expr ![mi_skip_libstdcxx_probe_tests]]
41 # Grab some line numbers we'll need.
42 set catch_1_lineno [gdb_get_line_number "Catch 1"]
43 set catch_2_lineno [gdb_get_line_number "Catch 2"]
44 set throw_1_lineno [gdb_get_line_number "Throw 1"]
45 set throw_2_lineno [gdb_get_line_number "Throw 2"]
46 set main_lineno [gdb_get_line_number "Stop here"]
48 # Restart this test, load the test binary and set a breakpoint in
50 proc restart_for_test {} {
51 global srcdir subdir binfile srcfile
59 mi_gdb_reinitialize_dir $srcdir/$subdir
60 mi_gdb_load ${binfile}
64 mi_create_breakpoint \
65 "$srcfile:${main_lineno}" "break before exiting program" \
66 -disp keep -func "main.*" \
67 -file ".*mi-catch-cpp-exceptions.cc" -line ${main_lineno}
70 # Issue an -exec-continue then wait for GDB to catch a C++ exception
71 # event in FUNC on LINE. Use TESTNAME to make tests unique.
72 proc continue_to_next_exception { func line testname } {
75 mi_send_resuming_command "exec-continue" \
77 mi_expect_stop "exception-caught" ".*" ".*" ".*" ".*" \
78 {} "run until an exception is caught: $testname"
79 mi_gdb_test "-stack-list-frames 1 1" \
80 "\\^done,stack=\\\[frame=\{level=\"1\",addr=\"$hex\",func=\"${func}\",.*,line=\"${line}\".*\}\\\]" \
81 "check previous frame: $testname"
84 # Issue an -exec-continue and stop at the breakpoint in main.
85 proc continue_to_breakpoint_in_main {} {
88 mi_send_resuming_command "exec-continue" "exec-continue to main"
89 mi_expect_stop "breakpoint-hit" "main" ".*" ".*" "${main_lineno}" \
90 {.* disp="keep"} "run until breakpoint in main"
93 # TYPE is one of throw, rethrow, or catch. This proc creates a catch
94 # point using -catch-TYPE. The optional string EXTRA is any extra
95 # arguments to pass when setting up the catchpoint.
96 proc setup_catchpoint {type {extra ""}} {
98 mi_gdb_test "-catch-${type} ${extra}" \
99 "\\^done,bkpt=\{number=\"$decimal\",type=\"catchpoint\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \
100 "Setup -catch-${type}"
103 # Ensure that -catch-throw will catch only throws and nothing else.
104 with_test_prefix "-catch-throw" {
106 setup_catchpoint "throw"
107 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
108 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
109 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
110 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
111 continue_to_breakpoint_in_main
114 # Ensure that -catch-rethrow catches only rethrows and nothing else.
115 with_test_prefix "-catch-rethrow" {
117 setup_catchpoint "rethrow"
118 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
119 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
120 continue_to_breakpoint_in_main
123 # Ensure that -catch-catch catches only catch points, and nothing
125 with_test_prefix "-catch-catch" {
127 setup_catchpoint "catch"
128 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
129 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
130 continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
131 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
132 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
133 continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
134 continue_to_breakpoint_in_main
137 if { $libstdcxx_probe_tests_supported == 1 } {
138 # Now check that all of the command with a regexp that doesn't match,
140 with_test_prefix "all with invalid regexp" {
142 setup_catchpoint "throw" "-r blahblah"
143 setup_catchpoint "rethrow" "-r woofwoof"
144 setup_catchpoint "catch" "-r miowmiow"
145 continue_to_breakpoint_in_main
148 unsupported "all with invalid regexp"
151 if { $libstdcxx_probe_tests_supported == 1 } {
152 # Now check that all of the commands with a regexp that does match,
154 with_test_prefix "all with valid regexp" {
156 setup_catchpoint "throw" "-r my_ex"
157 setup_catchpoint "rethrow" "-r _except"
158 setup_catchpoint "catch" "-r my_exception"
159 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
160 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
161 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
162 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
163 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
164 continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
165 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
166 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
167 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
168 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
169 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
170 continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
171 continue_to_breakpoint_in_main
174 unsupported "all with valid regexp"
177 # Check that the temporary switch works on its own.
178 with_test_prefix "all with -t" {
180 setup_catchpoint "throw" "-t"
181 setup_catchpoint "rethrow" "-t"
182 setup_catchpoint "catch" "-t"
183 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
184 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
185 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
186 continue_to_breakpoint_in_main
189 if { $libstdcxx_probe_tests_supported == 1 } {
190 # Check that the temporary switch works when used with a regexp.
192 with_test_prefix "all with -t and regexp" {
193 setup_catchpoint "throw" "-t -r my_ex"
194 setup_catchpoint "rethrow" "-t -r _except"
195 setup_catchpoint "catch" "-t -r my_exception"
196 continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
197 continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
198 continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
199 continue_to_breakpoint_in_main
202 unsupported "all with -t and regexp"