]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-format-address.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-format-address.exp
CommitLineData
1d506c26 1# Copyright 2022-2024 Free Software Foundation, Inc.
25209e2c
AB
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 gdb-python.exp
d82e5429 17require allow_python_tests
25209e2c
AB
18standard_testfile
19
20foreach func_name { foo bar } {
21 if {[build_executable "build binary with ${func_name} function" \
22 "$testfile-${func_name}" $srcfile \
23 [list debug \
4daa9f29 24 nopie \
25209e2c
AB
25 additional_flags=-DFUNCTION_NAME=${func_name}]] == -1} {
26 return -1
27 }
28}
29
30set binary_foo [standard_output_file "${testfile}-foo"]
31set binary_bar [standard_output_file "${testfile}-bar"]
32
33clean_restart $binary_foo
34
25209e2c
AB
35if ![runto_main] {
36 return -1
37}
38
39# Check the gdb.format_address method when using the default values
40# for the program space and architecture (these will be selected based
41# on the current inferior).
42set main_addr [get_hexadecimal_valueof "&main" "UNKNOWN"]
43set next_addr [format 0x%x [expr $main_addr + 1]]
44
45foreach_with_prefix symbol_filename { on off } {
46 gdb_test_no_output "set print symbol-filename ${symbol_filename}"
47
48 if { $symbol_filename == "on" } {
49 set filename_pattern " at \[^\r\n\]+/${srcfile}:$decimal"
50 } else {
51 set filename_pattern ""
52 }
53
54 gdb_test "python print(\"Got: \" + gdb.format_address($main_addr))" \
55 "Got: $main_addr <main${filename_pattern}>" \
56 "gdb.format_address, result should have no offset"
57
58 gdb_test "python print(\"Got: \" + gdb.format_address($next_addr))" \
59 "Got: $next_addr <main\\+1${filename_pattern}>" \
60 "gdb.format_address, result should have an offset"
61}
62
63if {![is_address_zero_readable]} {
64 gdb_test "python print(\"Got: \" + gdb.format_address(0))" \
65 "Got: 0x0" \
66 "gdb.format_address for address 0"
67}
68
69# Now check that gdb.format_address will accept the program space and
70# architecture arguments correctly.
71gdb_test_no_output "python inf = gdb.selected_inferior()"
72
73# First, pass both arguments, this should be fine.
74gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace, inf.architecture()))" \
75 "Got: $main_addr <main>" \
76 "gdb.format_address passing program space and architecture"
77
78# Now pass the program space and architecture as None.
79# First, pass both arguments, this should be fine.
80gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, None, None))" \
81 "Got: $main_addr <main>" \
82 "gdb.format_address passing program space and architecture as None"
83
84# Now forget the architecture, this should fail.
85gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace))" \
86 [multi_line \
87 "ValueError: The architecture and progspace arguments must both be supplied" \
88 "Error while executing Python code\\."] \
89 "gdb.format_address passing program space only"
90
91gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace, None))" \
92 [multi_line \
93 "ValueError: The architecture and progspace arguments must both be supplied" \
94 "Error while executing Python code\\."] \
95 "gdb.format_address passing real program space, but architecture is None"
96
97# Now skip the program space argument.
98gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, architecture=inf.architecture()))" \
99 [multi_line \
100 "ValueError: The architecture and progspace arguments must both be supplied" \
101 "Error while executing Python code\\."] \
102 "gdb.format_address passing architecture only"
103
104gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, None, inf.architecture()))" \
105 [multi_line \
106 "ValueError: The architecture and progspace arguments must both be supplied" \
107 "Error while executing Python code\\."] \
108 "gdb.format_address passing real architecture, but progspace is None"
109
110# Now, before we add a second inferior, lets just check we can format
111# the address of 'foo' correctly.
112set foo_addr [get_hexadecimal_valueof "&foo" "UNKNOWN"]
113
114gdb_test "python print(\"Got: \" + gdb.format_address($foo_addr, inf.progspace, inf.architecture()))" \
115 "Got: $foo_addr <foo>" \
116 "gdb.format_address for foo, with just one inferior"
117
118# Now lets add a second inferior, using a slightly different
119# executable, select that inferior, and capture a reference to the
120# inferior in a Python object.
121gdb_test "add-inferior -exec ${binary_bar}" ".*" \
122 "add a second inferior running the bar executable"
123gdb_test "inferior 2" ".*"
124gdb_test_no_output "python inf2 = gdb.selected_inferior()"
125
126# Now we can test formatting an address from inferior 1.
127gdb_test "python print(\"Got: \" + gdb.format_address($foo_addr, inf.progspace, inf.architecture()))" \
128 "Got: $foo_addr <foo>" \
129 "gdb.format_address for foo, while inferior 2 is selected"
130
131# Grab the address of 'bar'. Hopefully this will be the same address
132# as 'foo', but if not, that's not the end of the world, the test just
133# wont be quite as tough.
134set bar_addr [get_hexadecimal_valueof "&bar" "UNKNOWN"]
135
136# Now format the address of bar using the default inferior and
137# architecture, this should display the 'bar' symbol rather than
138# 'foo'.
139gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr))" \
4daa9f29 140 "Got: $bar_addr <bar>" \
25209e2c
AB
141 "gdb.format_address for bar, while inferior 2 is selected"
142
143# And again, but this time, specificy the program space and
144# architecture.
145gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.architecture()))" \
4daa9f29 146 "Got: $bar_addr <bar>" \
25209e2c
AB
147 "gdb.format_address for bar, while inferior 2 is selected, pass progspace and architecture"
148
149# Reselect inferior 1, and then format an address from inferior 2.
150gdb_test "inferior 1" ".*"
151gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.architecture()))" \
4daa9f29 152 "Got: $bar_addr <bar>" \
25209e2c
AB
153 "gdb.format_address for bar, while inferior 1 is selected, pass progspace and architecture"
154
155# Try pasing incorrect object types for program space and architecture.
156gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.progspace))" \
157 [multi_line \
158 "TypeError: The architecture argument is not a gdb.Architecture object" \
159 "Error while executing Python code\\."] \
160 "gdb.format_address pass wrong object type for architecture"
161
162gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.architecture(), inf2.architecture()))" \
163 [multi_line \
164 "TypeError: The progspace argument is not a gdb.Progspace object" \
165 "Error while executing Python code\\."] \
166 "gdb.format_address pass wrong object type for progspace"
167
168# Now invalidate inferior 2's program space, and try using that.
169gdb_test "python pspace = inf2.progspace"
170gdb_test "python arch = inf2.architecture()"
171gdb_test "remove-inferior 2"
172gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, pspace, arch))" \
173 [multi_line \
174 "ValueError: The progspace argument is not valid" \
175 "Error while executing Python code\\."] \
176 "gdb.format_address called with an invalid program space"