gdb/testsuite: Improve detection of bug gdb/24541
[external/binutils.git] / gdb / testsuite / gdb.mi / mi-catch-cpp-exceptions.exp
1 # Copyright 2019 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 # 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"]
36
37 # Restart this test, load the test binary and set a breakpoint in
38 # main.
39 proc restart_for_test {} {
40     global srcdir subdir binfile srcfile
41     global main_lineno
42
43     if {[mi_gdb_start]} {
44         continue
45     }
46
47     mi_delete_breakpoints
48     mi_gdb_reinitialize_dir $srcdir/$subdir
49     mi_gdb_load ${binfile}
50
51     mi_runto main
52
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}
57 }
58
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 } {
62     global hex
63
64     mi_send_resuming_command "exec-continue" \
65         "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"
71 }
72
73 # Issue an -exec-continue and stop at the breakpoint in main.
74 proc continue_to_breakpoint_in_main {} {
75     global main_lineno
76
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"
80 }
81
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 ""}} {
86     global decimal
87     mi_gdb_test "-catch-${type} ${extra}" \
88         "\\^done,bkpt=\{number=\"$decimal\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \
89         "Setup -catch-${type}"
90 }
91
92 # Ensure that -catch-throw will catch only throws and nothing else.
93 with_test_prefix "-catch-throw" {
94     restart_for_test
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
101 }
102
103 # Ensure that -catch-rethrow catches only rethrows and nothing else.
104 with_test_prefix "-catch-rethrow" {
105     restart_for_test
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
110 }
111
112 # Ensure that -catch-catch catches only catch points, and nothing
113 # else.
114 with_test_prefix "-catch-catch" {
115     restart_for_test
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
124 }
125
126 # Now check that all of the command with a regexp that doesn't match,
127 # don't trigger.
128 with_test_prefix "all with invalid regexp" {
129     restart_for_test
130     setup_catchpoint "throw" "-r blahblah"
131     setup_catchpoint "rethrow" "-r woofwoof"
132     setup_catchpoint "catch" "-r miowmiow"
133
134     # Would like to use 'continue_to_breakpoint_in_main' here, if
135     # there wasn't a bug that requires a use of kfail.
136
137     mi_send_resuming_command "exec-continue" \
138         "exec-continue"
139     set testname "run until breakpoint in main"
140     gdb_expect {
141         -re "could not find minimal symbol for typeinfo address.*$mi_gdb_prompt$" {
142             kfail "gdb/24541" "${testname}"
143         }
144         -re "\\*stopped,bkptno=\"$decimal\",reason=\"breakpoint-hit\",disp=\"keep\".*func=\"__cxa_throw\".*$mi_gdb_prompt$" {
145             kfail "gdb/24541" "${testname}"
146         }
147         -re "\\*stopped,reason=\"breakpoint-hit\".*func=\"main\".*line=\"${main_lineno}\".*$mi_gdb_prompt$" {
148             pass "${testname}"
149         }
150         timeout {
151             fail "${testname} (timeout)"
152         }
153     }
154 }
155
156 # Now check that all of the commands with a regexp that does match,
157 # still trigger.
158 with_test_prefix "all with valid regexp" {
159     restart_for_test
160     setup_catchpoint "throw" "-r my_ex"
161     setup_catchpoint "rethrow" "-r _except"
162     setup_catchpoint "catch" "-r my_exception"
163     continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
164     continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
165     continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
166     continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
167     continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
168     continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
169     continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
170     continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
171     continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
172     continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
173     continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
174     continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
175     continue_to_breakpoint_in_main
176 }
177
178 # Check that the temporary switch works on its own.
179 with_test_prefix "all with -t" {
180     restart_for_test
181     setup_catchpoint "throw" "-t"
182     setup_catchpoint "rethrow" "-t"
183     setup_catchpoint "catch" "-t"
184     continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
185     continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
186     continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
187     continue_to_breakpoint_in_main
188 }
189
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 }