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