xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/non-lazy-array-index.exp (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1# Copyright 2021-2023 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# Check that GDB does not do excess accesses to the inferior memory
17# when fetching elements from an array in the C language.
18
19standard_testfile
20
21if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
22    return -1
23}
24
25if {![runto_main]} {
26    return -1
27}
28
29# Load 'global_foo' into a history variable.
30gdb_test "p global_foo" "\\{f = 1, array = \\{1, 2, 3, 4, 5\\}\\}"
31
32gdb_test_no_output "set debug target 1"
33
34# Now print an array element from within 'global_foo', but accessed
35# via the history element.  The history element should be non-lazy,
36# and so there should be no reason for GDB to fetch the array element
37# from the inferior memory, we should be able to grab the contents
38# directory from the history value.
39#
40# To check this we 'set debug target 1' (above), and then look for any
41# xfer_partial calls; there shouldn't be any.
42set saw_memory_access false
43set saw_auxv_parse false
44gdb_test_multiple "p \$.array\[1\]" "" {
45    -re "^p \\\$\\.array\\\[1\\\]\r\n" {
46	exp_continue
47    }
48    -re "^->\[^\r\n\]+xfer_partial\[^\r\n\]+\r\n" {
49	set saw_memory_access true
50	exp_continue
51    }
52    -re "^->\[^\r\n\]+auxv_parse\[^\r\n\]+\r\n" {
53	set saw_auxv_parse true
54	exp_continue
55    }
56    -re "^->\[^\r\n\]+\r\n" {
57	exp_continue
58    }
59    -re "^<-\[^\r\n\]+\r\n" {
60	exp_continue
61    }
62    -re "^\[^\$\]\[^\r\n\]+\r\n" {
63	exp_continue
64    }
65    -re "^\\\$${decimal} = 2\r\n$gdb_prompt " {
66	if { $saw_memory_access } {
67	    if { $saw_auxv_parse } {
68		# The expression parser may look up global symbols, which
69		# may require reading AUXV in order to determine the debug
70		# base for SVR4 linker namespaces.
71		xfail "$gdb_test_name"
72	    } else {
73		fail "$gdb_test_name"
74	    }
75	} else {
76	    pass "$gdb_test_name"
77	}
78    }
79}
80
81gdb_test "set debug target 0" ".*"
82
83if { ! [skip_python_tests] } {
84    gdb_test_no_output "python val = gdb.parse_and_eval('global_foo')"
85    gdb_test "python print('val = %s' % val)" "val = \\{f = 1, array = \\{1, 2, 3, 4, 5\\}\\}"
86    gdb_test "python print('val.is_lazy = %s' % val.is_lazy)" "val\\.is_lazy = False"
87
88    # Grab an element from the array within VAL.  The element should
89    # immediately be non-lazy as the value contents can be copied
90    # directly from VAL.
91    gdb_test_no_output "python elem = val\['array'\]\[1\]"
92    gdb_test "python print('elem.is_lazy = %s' % elem.is_lazy)" "elem\\.is_lazy = False"
93    gdb_test "python print('elem = %s' % elem)" "elem = 2"
94}
95