]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
40c17c68e63055f779397c7b09a67e4fb6bdfaf1
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.mi / mi-catch-cpp-exceptions.exp
1 # Copyright 2019-2020 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # Test the -catch-throw, -catch-rethrow, and -catch-catch MI commands.
17
18 if { [skip_cplus_tests] } { continue }
19
20 load_lib mi-support.exp
21 set MIFLAGS "-i=mi"
22
23 standard_testfile .cc
24
25 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
26 untested "failed to compile"
27 return -1
28 }
29
30 if [mi_gdb_start] {
31 continue
32 }
33 mi_delete_breakpoints
34 mi_gdb_reinitialize_dir $srcdir/$subdir
35 mi_gdb_load ${binfile}
36 if {![mi_run_to_main]} {
37 return -1
38 }
39 set libstdcxx_probe_tests_supported [expr ![mi_skip_libstdcxx_probe_tests]]
40
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"]
47
48 # Restart this test, load the test binary and set a breakpoint in
49 # main.
50 proc restart_for_test {} {
51 global srcdir subdir binfile srcfile
52 global main_lineno
53
54 if {[mi_gdb_start]} {
55 continue
56 }
57
58 mi_delete_breakpoints
59 mi_gdb_reinitialize_dir $srcdir/$subdir
60 mi_gdb_load ${binfile}
61
62 mi_runto main
63
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}
68 }
69
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 } {
73 global hex
74
75 mi_send_resuming_command "exec-continue" \
76 "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"
82 }
83
84 # Issue an -exec-continue and stop at the breakpoint in main.
85 proc continue_to_breakpoint_in_main {} {
86 global main_lineno
87
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"
91 }
92
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 ""}} {
97 global decimal
98 mi_gdb_test "-catch-${type} ${extra}" \
99 "\\^done,bkpt=\{number=\"$decimal\",type=\"catchpoint\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \
100 "Setup -catch-${type}"
101 }
102
103 # Ensure that -catch-throw will catch only throws and nothing else.
104 with_test_prefix "-catch-throw" {
105 restart_for_test
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
112 }
113
114 # Ensure that -catch-rethrow catches only rethrows and nothing else.
115 with_test_prefix "-catch-rethrow" {
116 restart_for_test
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
121 }
122
123 # Ensure that -catch-catch catches only catch points, and nothing
124 # else.
125 with_test_prefix "-catch-catch" {
126 restart_for_test
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
135 }
136
137 if { $libstdcxx_probe_tests_supported == 1 } {
138 # Now check that all of the command with a regexp that doesn't match,
139 # don't trigger.
140 with_test_prefix "all with invalid regexp" {
141 restart_for_test
142 setup_catchpoint "throw" "-r blahblah"
143 setup_catchpoint "rethrow" "-r woofwoof"
144 setup_catchpoint "catch" "-r miowmiow"
145 continue_to_breakpoint_in_main
146 }
147 } else {
148 unsupported "all with invalid regexp"
149 }
150
151 if { $libstdcxx_probe_tests_supported == 1 } {
152 # Now check that all of the commands with a regexp that does match,
153 # still trigger.
154 with_test_prefix "all with valid regexp" {
155 restart_for_test
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
172 }
173 } else {
174 unsupported "all with valid regexp"
175 }
176
177 # Check that the temporary switch works on its own.
178 with_test_prefix "all with -t" {
179 restart_for_test
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
187 }
188
189 if { $libstdcxx_probe_tests_supported == 1 } {
190 # Check that the temporary switch works when used with a regexp.
191 restart_for_test
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
200 }
201 } else {
202 unsupported "all with -t and regexp"
203 }