aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
blob: 1f119ec3a878f688f2afa7f95d6c90e31d24c807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright 2019-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test the -catch-throw, -catch-rethrow, and -catch-catch MI commands.

require allow_cplus_tests

load_lib mi-support.exp
set MIFLAGS "-i=mi"

standard_testfile .cc

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
    untested "failed to compile"
    return -1
}

mi_clean_restart $binfile
if {[mi_runto_main] < 0} {
    return -1
}
set libstdcxx_probe_tests_supported [expr ![mi_skip_libstdcxx_probe_tests]]

# Grab some line numbers we'll need.
set catch_1_lineno [gdb_get_line_number "Catch 1"]
set catch_2_lineno [gdb_get_line_number "Catch 2"]
set throw_1_lineno [gdb_get_line_number "Throw 1"]
set throw_2_lineno [gdb_get_line_number "Throw 2"]
set main_lineno [gdb_get_line_number "Stop here"]

# Restart this test, load the test binary and set a breakpoint in
# main.
proc restart_for_test {} {
    global srcdir subdir binfile srcfile
    global main_lineno

    if {[mi_clean_restart $binfile]} {
	return
    }

    mi_runto_main

    mi_create_breakpoint \
	"$srcfile:${main_lineno}" "break before exiting program" \
	-disp keep -func "main.*" \
	-file ".*mi-catch-cpp-exceptions.cc" -line ${main_lineno}
}

# Issue an -exec-continue then wait for GDB to catch a C++ exception
# event in FUNC on LINE.  Use TESTNAME to make tests unique.
proc continue_to_next_exception { func line testname } {
    global hex

    mi_send_resuming_command "exec-continue" \
	"exec-continue"
    mi_expect_stop "exception-caught" ".*" ".*" ".*" ".*" \
	{} "run until an exception is caught: $testname"
    mi_gdb_test "-stack-list-frames 1 1" \
	"\\^done,stack=\\\[frame=\{level=\"1\",addr=\"$hex\",(addr_flags=\"PAC\",)?func=\"${func}\",.*,line=\"${line}\".*\}\\\]" \
	"check previous frame: $testname"
}

# Issue an -exec-continue and stop at the breakpoint in main.
proc continue_to_breakpoint_in_main {} {
    global main_lineno

    mi_send_resuming_command "exec-continue" "exec-continue to main"
    mi_expect_stop "breakpoint-hit" "main" ".*" ".*" "${main_lineno}" \
	{.* disp="keep"} "run until breakpoint in main"
}

# TYPE is one of throw, rethrow, or catch.  This proc creates a catch
# point using -catch-TYPE.  The optional string EXTRA is any extra
# arguments to pass when setting up the catchpoint.
proc setup_catchpoint {type {extra ""}} {
    global decimal
    mi_gdb_test "-catch-${type} ${extra}" \
	"\\^done,bkpt=\{number=\"$decimal\",type=\"catchpoint\".*what=\"exception ${type}\",catch-type=\"${type}\".*\}" \
	"Setup -catch-${type}"
}

# Ensure that -catch-throw will catch only throws and nothing else.
with_test_prefix "-catch-throw" {
    restart_for_test
    setup_catchpoint "throw"
    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
    continue_to_breakpoint_in_main
}

# Ensure that -catch-rethrow catches only rethrows and nothing else.
with_test_prefix "-catch-rethrow" {
    restart_for_test
    setup_catchpoint "rethrow"
    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
    continue_to_breakpoint_in_main
}

# Ensure that -catch-catch catches only catch points, and nothing
# else.
with_test_prefix "-catch-catch" {
    restart_for_test
    setup_catchpoint "catch"
    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
    continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
    continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
    continue_to_breakpoint_in_main
}

if { $libstdcxx_probe_tests_supported  == 1 } {
    # Now check that all of the command with a regexp that doesn't match,
    # don't trigger.
    with_test_prefix "all with invalid regexp" {
	restart_for_test
	setup_catchpoint "throw" "-r blahblah"
	setup_catchpoint "rethrow" "-r woofwoof"
	setup_catchpoint "catch" "-r miowmiow"
	continue_to_breakpoint_in_main
    }
} else {
    unsupported "all with invalid regexp"
}

if { $libstdcxx_probe_tests_supported  == 1 } {
    # Now check that all of the commands with a regexp that does match,
    # still trigger.
    with_test_prefix "all with valid regexp" {
	restart_for_test
	setup_catchpoint "throw" "-r my_ex"
	setup_catchpoint "rethrow" "-r _except"
	setup_catchpoint "catch" "-r my_exception"
	continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
	continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
	continue_to_next_exception "bar" "${throw_1_lineno}" "throw 2"
	continue_to_next_exception "foo" "${catch_1_lineno}" "catch 2"
	continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
	continue_to_next_exception "main" "${catch_2_lineno}" "catch 3"
	continue_to_next_exception "bar" "${throw_1_lineno}" "throw 3"
	continue_to_next_exception "foo" "${catch_1_lineno}" "catch 4"
	continue_to_next_exception "bar" "${throw_1_lineno}" "throw 4"
	continue_to_next_exception "foo" "${catch_1_lineno}" "catch 5"
	continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 2"
	continue_to_next_exception "main" "${catch_2_lineno}" "catch 6"
	continue_to_breakpoint_in_main
    }
} else {
    unsupported "all with valid regexp"
}

# Check that the temporary switch works on its own.
with_test_prefix "all with -t" {
    restart_for_test
    setup_catchpoint "throw" "-t"
    setup_catchpoint "rethrow" "-t"
    setup_catchpoint "catch" "-t"
    continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
    continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
    continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
    continue_to_breakpoint_in_main
}

if { $libstdcxx_probe_tests_supported  == 1 } {
    # Check that the temporary switch works when used with a regexp.
    with_test_prefix "all with -t and regexp" {
	restart_for_test
	setup_catchpoint "throw" "-t -r my_ex"
	setup_catchpoint "rethrow" "-t -r _except"
	setup_catchpoint "catch" "-t -r my_exception"
	continue_to_next_exception "bar" "${throw_1_lineno}" "throw 1"
	continue_to_next_exception "foo" "${catch_1_lineno}" "catch 1"
	continue_to_next_exception "foo" "${throw_2_lineno}" "rethrow 1"
	continue_to_breakpoint_in_main
    }
} else {
    unsupported "all with -t and regexp"
}