Commit | Line | Data |
---|---|---|
30056ea0 AB |
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,reason=\"breakpoint-hit\".*func=\"main\".*line=\"${main_lineno}\".*$mi_gdb_prompt$" { | |
145 | pass "${testname}" | |
146 | } | |
147 | timeout { | |
148 | fail "${testname} (timeout)" | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | # Now check that all of the commands with a regexp that does match, | |
154 | # still trigger. | |
155 | with_test_prefix "all with valid regexp" { | |
156 | restart_for_test | |
157 | setup_catchpoint "throw" "-r my_ex" | |
158 | setup_catchpoint "rethrow" "-r _except" | |
159 | setup_catchpoint "catch" "-r my_exception" | |
160 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1" | |
161 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1" | |
162 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2" | |
163 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2" | |
164 | continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1" | |
165 | continue_to_next_exception "main" "${catch_2_lineno}" "catch 3" | |
166 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3" | |
167 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4" | |
168 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4" | |
169 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5" | |
170 | continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2" | |
171 | continue_to_next_exception "main" "${catch_2_lineno}" "catch 6" | |
172 | continue_to_breakpoint_in_main | |
173 | } | |
174 | ||
175 | # Check that the temporary switch works on its own. | |
176 | with_test_prefix "all with -t" { | |
177 | restart_for_test | |
178 | setup_catchpoint "throw" "-t" | |
179 | setup_catchpoint "rethrow" "-t" | |
180 | setup_catchpoint "catch" "-t" | |
181 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1" | |
182 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1" | |
183 | continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1" | |
184 | continue_to_breakpoint_in_main | |
185 | } | |
186 | ||
187 | # Check that the temporary switch works when used with a regexp. | |
188 | restart_for_test | |
189 | with_test_prefix "all with -t and regexp" { | |
190 | setup_catchpoint "throw" "-t -r my_ex" | |
191 | setup_catchpoint "rethrow" "-t -r _except" | |
192 | setup_catchpoint "catch" "-t -r my_exception" | |
193 | continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1" | |
194 | continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1" | |
195 | continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1" | |
196 | continue_to_breakpoint_in_main | |
197 | } |