1# Copyright 2018-2024 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
16load_lib "ada.exp"
17
18require allow_ada_tests gnat_runtime_has_debug_info
19
20standard_ada_testfile foo
21
22if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug additional_flags=-gnata ]] != "" } {
23  return -1
24}
25
26clean_restart ${testfile}
27
28# Some global variables used to simplify the maintenance of some of
29# the regular expressions below.
30set eol "\r\n"
31set sp "\[ \t\]*"
32
33set when "when"
34set catchpoint_constraint_error_msg \
35  "Catchpoint $decimal, exception at $hex in foo \\\(\\\).*at .*foo.adb:$decimal$eol$decimal$sp$when Constraint_Error =>"
36
37set catchpoint_program_error_msg \
38  "Catchpoint $decimal, exception at $hex in foo \\\(\\\).*at .*foo.adb:$decimal$eol$decimal$sp$when Program_Error =>"
39
40set catchpoint_storage_error_msg \
41  "Catchpoint $decimal, exception at $hex in foo \\\(\\\).*at .*foo.adb:$decimal$eol$decimal$sp$when Storage_Error =>"
42
43if {![runto_main]} {
44   return 0
45}
46
47############################################
48# 1. Try catching all exceptions handlers. #
49############################################
50
51gdb_test "catch handlers" \
52    "Catchpoint $decimal: all Ada exceptions handlers" \
53    "insert catchpoint on all Ada exceptions handlers"
54
55# Continue.  The program should stop at first exception handling.
56
57gdb_test "continue" \
58    "Continuing\.$eol$eol$catchpoint_constraint_error_msg" \
59    "continuing to first Constraint_Error exception handlers"
60
61# Resume the program's exception.
62#
63# The program will first go through a block of code which has an
64# exception handler, but since no exception is raised, we should
65# not stop there.  Instead, we expect to stop in the handler of
66# the next exception being raised.
67
68gdb_test "continue" \
69    "Continuing\.$eol$eol$catchpoint_storage_error_msg" \
70    "continuing and stopping in Storage_Error exception handlers"
71
72gdb_test_no_output "delete 2" \
73                   "delete catchpoint on all Ada exceptions handlers"
74
75##################################################
76# 2. Try catching some named exception handlers. #
77##################################################
78
79# Insert a catchpoint on Program_Error Ada exception handlers.
80
81gdb_test "catch handlers Program_Error" \
82         "Catchpoint $decimal: `Program_Error' Ada exception handlers" \
83         "insert catchpoint on Program_Error Ada exception handlers"
84
85# Continue, we should not stop at ABORT_SIGNAL but at Program_Error one.
86
87gdb_test "continue" \
88    "Continuing\.$eol$eol$catchpoint_program_error_msg" \
89    "continuing without stopping to Program_Error exception handlers"
90
91gdb_test_no_output \
92    "delete 3" \
93    "delete catchpoint on all Program_Error Ada exception handlers"
94
95# Insert a catchpoint on Storage_Error Ada exception handlers.
96
97gdb_test "catch handlers Storage_Error" \
98         "Catchpoint $decimal: `Storage_Error' Ada exception handlers" \
99         "insert catchpoint on Storage_Error Ada exception handlers"
100
101# Continue, we should stop at Storage_Error handlers.
102
103gdb_test "continue" \
104    "Continuing\.$eol$eol$catchpoint_storage_error_msg" \
105    "continuing without stopping to Storage_Error exception handlers"
106
107gdb_test_no_output \
108    "delete 4" \
109    "delete catchpoint on all Storage_Error Ada exception handlers"
110
111########################################################################
112# 3. Try catching with condition and without named exception handlers. #
113########################################################################
114
115# Insert a catchpoint on all Ada exceptions handlers with condition.
116
117gdb_test "catch handlers if Global_Var = 2" \
118         "Catchpoint $decimal: all Ada exceptions handlers" \
119         "insert catchpoint on all Ada exception handlers with condition"
120
121# Check that condition is stored and properly displayed.
122
123gdb_test "info breakpoint" "stop only if Global_Var = 2" \
124         "Check catch handlers with condition"
125
126# Continue, we should not stop at ABORT_SIGNAL but at Program_Error one.
127
128gdb_test "continue" \
129    "Continuing\.$eol$eol$catchpoint_constraint_error_msg" \
130    "continuing to second Constraint_Error exception handlers"
131
132gdb_test_no_output \
133    "delete 5" \
134    "delete catchpoint on all all Ada exceptions handlers with condition"
135
136################################################################
137# 4. Try catching with condition and named exception handlers. #
138################################################################
139
140# Insert a catchpoint on Program_Error Ada exception handlers with
141# condition.
142
143gdb_test "catch handlers Program_Error if Global_Var = 4" \
144    "Catchpoint $decimal: `Program_Error' Ada exception handlers" \
145    "insert catchpoint on Program_Error Ada exception handlers with condition"
146
147# Continue, we should not stop at first Program_Error handlers but at
148# the second one.
149
150gdb_test "continue" \
151    "Continuing\.$eol$eol$catchpoint_program_error_msg" \
152    "continuing to Program_Error exception handlers"
153
154# Continue, the program should exit properly.
155
156gdb_test "continue" \
157         "Continuing\..*$inferior_exited_re.*" \
158         "continuing to program completion"
159