1 # Copyright
2022 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 load_lib gdb
-python.exp
19 foreach func_name
{ foo bar
} {
20 if {[build_executable
"build binary with ${func_name} function" \
21 "$testfile-${func_name}" $srcfile \
24 additional_flags
=-DFUNCTION_NAME
=$
{func_name
}]] == -1} {
29 set binary_foo
[standard_output_file
"${testfile}-foo"]
30 set binary_bar
[standard_output_file
"${testfile}-bar"]
32 clean_restart $binary_foo
34 # Skip all tests
if Python scripting is not enabled.
35 if { [skip_python_tests
] } { continue }
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]]
47 foreach_with_prefix symbol_filename
{ on off } {
48 gdb_test_no_output
"set print symbol-filename ${symbol_filename}"
50 if { $symbol_filename
== "on" } {
51 set filename_pattern
" at \[^\r\n\]+/${srcfile}:$decimal"
53 set filename_pattern
""
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"
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"
65 if {![is_address_zero_readable
]} {
66 gdb_test
"python print(\"Got: \" + gdb.format_address(0))" \
68 "gdb.format_address for address 0"
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()"
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"
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"
86 # Now forget the architecture
, this should fail.
87 gdb_test
"python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace))" \
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"
93 gdb_test
"python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace, None))" \
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"
99 # Now skip the
program space
argument.
100 gdb_test
"python print(\"Got: \" + gdb.format_address($main_addr, architecture=inf.architecture()))" \
102 "ValueError: The architecture and progspace arguments must both be supplied" \
103 "Error while executing Python code\\."] \
104 "gdb.format_address passing architecture only"
106 gdb_test
"python print(\"Got: \" + gdb.format_address($main_addr, None, inf.architecture()))" \
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"
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"]
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"
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()"
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"
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"]
138 # Now format the address of bar using the default inferior and
139 # architecture, this should display the 'bar
' symbol rather than
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"
145 # And again, but this time, specificy the program space and
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"
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"
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))" \
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"
164 gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.architecture(), inf2.architecture()))" \
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"
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))" \
176 "ValueError: The progspace argument is not valid" \
177 "Error while executing Python code\\."] \
178 "gdb.format_address called with an invalid program space"