1 # Copyright 2017-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the gdb testsuite.
18 # Tests which verify (or not) that GDB can access in-scope variables
19 # when stopped within an OpenMP parallel region.
23 if { [test_compiler_info "clang*"] } {
24 # Clang doesn't add OpenMP information in the correct scope,
25 # so all relevant tests here will fail. See here for more info:
26 # https://github.com/llvm/llvm-project/issues/44236
27 unsupported "Clang doesn't provide required info for the test"
31 set have_nested_function_support 0
32 set opts {openmp debug}
33 if [support_nested_function_tests] {
34 lappend opts "additional_flags=-DHAVE_NESTED_FUNCTION_SUPPORT"
35 set have_nested_function_support 1
38 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} {
42 # gdb_openmp_setup may be defined to set auto-load safe-path and possibly
43 # sysroot. These settings are required for gdb to be able to find
44 # the libgomp python plugin. (sysroot only needs to be defined for
47 # This approach has both pros and cons. On the plus side, it's easy
48 # to automatically set a precise auto-load safe-path. (It's easy because
49 # the output of ldd on the binary may be examined to learn the location
52 # However, making these settings is also a drawback due to potentially
53 # overriding settings made by a board file. Therefore, this proc
54 # is optional and will only be called if it's defined.
56 if {[info procs gdb_openmp_setup] != ""} {
57 if {[gdb_openmp_setup $binfile] != ""} {
58 untested "could not set up OpenMP environment"
67 # We want to invoke setup_kfail (and in some cases setup_xfail) when
68 # GDB does not yet have support for finding the values of variables in
69 # (non-master) threads. We'll check this by looking at the output of
70 # "maint print thread-parent". If this command is undefined, then GDB
71 # does not yet have thread parent support, and it makes sense to kfail
72 # tests which won't work. It's possible for GDB to have this support,
73 # but not work. E.g. it may be the case that the plugin doesn't
74 # exist or is not found. We may eventually need to add additional
75 # constraints related to setting allow_kfail to 0. But, for the moment,
76 # this simple test should be sufficient.
79 gdb_test_multiple "maint print thread-parent" "maint print thread-parent" {
80 -re "Undefined maintenance print command.*$gdb_prompt" {
81 pass "maint print thread-parent (does not exist)"
83 -re "No parent found.*" {
84 pass "maint print thread-parent"
89 # Determine whether to xfail some of the tests based on GCC version.
91 # This may need to be tweaked somewhat. Testing shows that GCC 7.3.1
92 # needs the xfails. GCC 8.3.1 and 9.1.1 do not. The assumption made
93 # below is that all versions of gcc 8 and above won't require the
94 # XFAIL setup and that all versions of gcc 7 and below will, but it's
95 # possible that there are versions in between 7.3.1 and 8.3.1 for
96 # which this assumption is invalid.
99 if {[test_compiler_info {gcc-[0-7]-*}]} {
103 # maybe_setup_kfail will set up a kfail for gdb/22214 when COND holds in
104 # addition to considering the values of $have_older_gcc and $allow_kfail.
106 # When $have_older_gcc evaluates to true, setup_xfail will invoked
109 proc maybe_setup_kfail {cond} {
110 global have_older_gcc allow_kfail
111 if {$have_older_gcc} {
113 } elseif {[uplevel 1 [list expr $cond]] && $allow_kfail} {
114 setup_kfail "gdb/22214" *-*-*
118 with_test_prefix "single_scope" {
120 gdb_breakpoint [gdb_get_line_number "single_scope: thread_num="]
121 gdb_breakpoint [gdb_get_line_number "single_scope: s1="]
123 foreach pref {"first thread" "second thread"} {
124 with_test_prefix $pref {
125 gdb_continue_to_breakpoint "at printf"
127 if {$have_older_gcc} { setup_xfail "*-*-*" }
128 set thread_num [get_valueof "" thread_num "unknown"]
129 if {$have_older_gcc} { setup_xfail "*-*-*" }
130 gdb_test "print s1" "= -41"
131 gdb_test "print s2" "= \[12\]02"
132 if {$have_older_gcc} { setup_xfail "*-*-*" }
133 gdb_test "print s3" "= -43"
134 gdb_test "print i1" "= 11"
135 gdb_test "print i2" "= \[12]12"
136 maybe_setup_kfail {$thread_num != 0}
137 gdb_test "print i3" "= 13"
141 with_test_prefix "after parallel region" {
142 gdb_continue_to_breakpoint "at printf"
144 gdb_test "print s1" "= -41"
145 gdb_test "print s2" "= -42"
146 gdb_test "print s3" "= -43"
147 gdb_test "print i1" "= 11"
148 gdb_test "print i2" "= 12"
149 gdb_test "print i3" "= 13"
154 with_test_prefix "multi_scope" {
155 gdb_breakpoint [gdb_get_line_number "multi_scope: thread_num="]
156 gdb_breakpoint [gdb_get_line_number "multi_scope: i01="]
158 foreach pref {"first thread" "second thread"} {
159 with_test_prefix $pref {
160 gdb_continue_to_breakpoint "at printf"
162 if {$have_older_gcc} { setup_xfail "*-*-*" }
163 set thread_num [get_valueof "" thread_num "unknown"]
165 gdb_test "print i01" "= 1"
166 maybe_setup_kfail {$thread_num != 0}
167 gdb_test "print i02" "= 2"
168 gdb_test "print i11" "= 11"
169 maybe_setup_kfail {$thread_num != 0}
170 gdb_test "print i12" "= 12"
171 gdb_test "print i21" "= \[12\]21"
172 maybe_setup_kfail {$thread_num != 0}
173 gdb_test "print i22" "= 22"
174 gdb_test "print file_scope_var" "= 9876"
178 with_test_prefix "after parallel" {
179 gdb_continue_to_breakpoint "at printf"
181 gdb_test "print i01" "= 1"
182 gdb_test "print i02" "= 2"
183 gdb_test "print i11" "= 11"
184 gdb_test "print i12" "= 12"
185 gdb_test "print i21" "= -21"
186 gdb_test "print i22" "= 22"
187 gdb_test "print file_scope_var" "= 9876"
191 # Nested functions in C are a GNU extension, so only do the nested function
192 # tests if compiling with -DHAVE_NESTED_FUNCTION_SUPPORT was successful.
194 if $have_nested_function_support {
195 with_test_prefix "nested_func" {
196 gdb_breakpoint [gdb_get_line_number "nested_func: tn="]
198 foreach call_prefix {"1st call" "2nd call"} {
199 with_test_prefix $call_prefix {
200 foreach thread_prefix {"1st thread" "2nd thread"} {
201 with_test_prefix $thread_prefix {
202 gdb_continue_to_breakpoint "at printf"
204 if {$have_older_gcc} { setup_xfail "*-*-*" }
205 set thread_num [get_valueof "" "tn" "unknown"]
207 gdb_test "print file_scope_var" "= 9876"
208 if {$have_older_gcc} { setup_xfail *-*-* }
209 gdb_test "print s1" "= -42"
210 if {$call_prefix eq "1st call"} {
211 gdb_test "print i" "= 1"
213 gdb_test "print i" "= 101"
215 gdb_test "print j" "= \[12\]000"
216 maybe_setup_kfail {$thread_num != 0}
217 if {$call_prefix eq "1st call"} {
218 gdb_test "print k" "= 3"
220 gdb_test "print k" "= 103"
222 if {$call_prefix eq "1st call"} {
223 gdb_test "print p" "= 10"
225 gdb_test "print p" "= 20"
227 gdb_test "print q" "= \[12\]001"
228 maybe_setup_kfail {$thread_num != 0}
229 if {$call_prefix eq "1st call"} {
230 gdb_test "print r" "= 12"
232 gdb_test "print r" "= 22"
234 gdb_test "print x" "= 4"
235 gdb_test "print y" "= \[12\]002"
236 maybe_setup_kfail {$thread_num != 0}
237 gdb_test "print z" "= 6"
238 if {$have_older_gcc} { setup_xfail "*-*-*" }
239 gdb_test "print tn" "= \[01\]"
247 with_test_prefix "nested_parallel" {
248 gdb_breakpoint [gdb_get_line_number "nested_parallel (inner threads)"]
250 with_test_prefix "inner_threads" {
251 foreach pref {"1st stop" "2nd stop" "3rd stop" "4th stop"} {
252 with_test_prefix $pref {
253 gdb_continue_to_breakpoint "at printf"
255 # Don't need setup_xfail here due to fact that num is made
256 # made known to the inner parallel region.
257 set thread_num [get_valueof "" "num" "unknown"]
259 if {$have_older_gcc} { setup_xfail "*-*-*" }
260 set inner_thread_num [get_valueof "" "inner_num" "unknown"]
262 gdb_test "print file_scope_var" "= 9876"
263 gdb_test "print num" "= \[01\]"
264 maybe_setup_kfail {$thread_num != 0 || $inner_thread_num != 0}
265 gdb_test "print i" "= 1"
266 maybe_setup_kfail {$thread_num != 0 || $inner_thread_num != 0}
267 gdb_test "print j" "= 2"
268 if {$have_older_gcc || ($inner_thread_num != 0 && $allow_kfail)} { setup_xfail *-*-* }
269 gdb_test "print l" "= 10\[24\]"
270 if {$have_older_gcc ||( $inner_thread_num != 0 && $allow_kfail)} { setup_xfail *-*-* }
271 gdb_test "print k" "= 10\[13\]"
276 with_test_prefix "outer_threads" {
277 gdb_breakpoint [gdb_get_line_number "nested_parallel (outer threads)"]
279 with_test_prefix "outer stop" {
280 gdb_continue_to_breakpoint "at printf"
282 if {$have_older_gcc} { setup_xfail "*-*-*" }
283 # Use get_local_valueof instead of get_valueof to avoid picking up
284 # some random 'num' in a shared library.
285 set thread_num [get_local_valueof "num" "unknown"]
287 gdb_test "print file_scope_var" "= 9876"
288 if {$have_older_gcc} { setup_xfail "*-*-*" }
289 gdb_test "print num" "= \[01\]"
290 maybe_setup_kfail {$thread_num != 0}
291 gdb_test "print i" "= 1"
292 maybe_setup_kfail {$thread_num != 0}
293 gdb_test "print j" "= 2"
294 gdb_test "print l" "= 10\[24\]"
295 if {$have_older_gcc} { setup_xfail "*-*-*" }
296 gdb_test "print k" "= 10\[13\]"