aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2021-10-23 09:59:25 +0100
committerAndrew Burgess <aburgess@redhat.com>2022-03-22 10:05:05 +0000
commit25209e2c6979c3838e14e099f0333609810db280 (patch)
treec3a6ecdc110db913c886182431031054a1461cf0 /gdb/testsuite/gdb.python
parent6c111a4ec26ba4a1343cb788c34e4bccce65bfeb (diff)
downloadgdb-25209e2c6979c3838e14e099f0333609810db280.zip
gdb-25209e2c6979c3838e14e099f0333609810db280.tar.gz
gdb-25209e2c6979c3838e14e099f0333609810db280.tar.bz2
gdb/python: add gdb.format_address function
Add a new function, gdb.format_address, which is a wrapper around GDB's print_address function. This method takes an address, and returns a string with the format: ADDRESS <SYMBOL+OFFSET> Where, ADDRESS is the original address, formatted as hexadecimal, SYMBOL is a symbol with an address lower than ADDRESS, and OFFSET is the offset from SYMBOL to ADDRESS in decimal. If there's no SYMBOL suitably close to ADDRESS then the <SYMBOL+OFFSET> part is not included. This is useful if a user wants to write a Python script that pretty-prints addresses, the user no longer needs to do manual symbol lookup, or worry about correctly formatting addresses. Additionally, there are some settings that effect how GDB picks SYMBOL, and whether the file name and line number should be included with the SYMBOL name, the gdb.format_address function ensures that the users Python script also benefits from these settings. The gdb.format_address by default selects SYMBOL from the current inferiors program space, and address is formatted using the architecture for the current inferior. However, a user can also explicitly pass a program space and architecture like this: gdb.format_address(ADDRESS, PROGRAM_SPACE, ARCHITECTURE) In order to format an address for a different inferior. Notes on the implementation: In py-arch.c I extended arch_object_to_gdbarch to add an assertion for the type of the PyObject being worked on. Prior to this commit all uses of arch_object_to_gdbarch were guaranteed to pass this function a gdb.Architecture object, but, with this commit, this might not be the case. So, with this commit I've made it a requirement that the PyObject be a gdb.Architecture, and this is checked with the assert. And in order that callers from other files can check if they have a gdb.Architecture object, I've added the new function gdbpy_is_architecture. In py-progspace.c I've added two new function, the first progspace_object_to_program_space, converts a PyObject of type gdb.Progspace to the associated program_space pointer, and gdbpy_is_progspace checks if a PyObject is a gdb.Progspace or not.
Diffstat (limited to 'gdb/testsuite/gdb.python')
-rw-r--r--gdb/testsuite/gdb.python/py-format-address.c32
-rw-r--r--gdb/testsuite/gdb.python/py-format-address.exp177
2 files changed, 209 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.python/py-format-address.c b/gdb/testsuite/gdb.python/py-format-address.c
new file mode 100644
index 0000000..6493fc4
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-format-address.c
@@ -0,0 +1,32 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2022 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* This test is compiled multiple times with FUNCTION_NAME defined to
+ different strings, this means we should (hopefully) get the same code
+ layout in memory, but with different strings for the function name. */
+
+int
+FUNCTION_NAME (void)
+{
+ return 0;
+}
+
+int
+main (void)
+{
+ return FUNCTION_NAME ();
+}
diff --git a/gdb/testsuite/gdb.python/py-format-address.exp b/gdb/testsuite/gdb.python/py-format-address.exp
new file mode 100644
index 0000000..5c80829
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-format-address.exp
@@ -0,0 +1,177 @@
+# Copyright 2022 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+load_lib gdb-python.exp
+standard_testfile
+
+foreach func_name { foo bar } {
+ if {[build_executable "build binary with ${func_name} function" \
+ "$testfile-${func_name}" $srcfile \
+ [list debug \
+ additional_flags=-DFUNCTION_NAME=${func_name}]] == -1} {
+ return -1
+ }
+}
+
+set binary_foo [standard_output_file "${testfile}-foo"]
+set binary_bar [standard_output_file "${testfile}-bar"]
+
+clean_restart $binary_foo
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+if ![runto_main] {
+ return -1
+}
+
+# Check the gdb.format_address method when using the default values
+# for the program space and architecture (these will be selected based
+# on the current inferior).
+set main_addr [get_hexadecimal_valueof "&main" "UNKNOWN"]
+set next_addr [format 0x%x [expr $main_addr + 1]]
+
+foreach_with_prefix symbol_filename { on off } {
+ gdb_test_no_output "set print symbol-filename ${symbol_filename}"
+
+ if { $symbol_filename == "on" } {
+ set filename_pattern " at \[^\r\n\]+/${srcfile}:$decimal"
+ } else {
+ set filename_pattern ""
+ }
+
+ gdb_test "python print(\"Got: \" + gdb.format_address($main_addr))" \
+ "Got: $main_addr <main${filename_pattern}>" \
+ "gdb.format_address, result should have no offset"
+
+ gdb_test "python print(\"Got: \" + gdb.format_address($next_addr))" \
+ "Got: $next_addr <main\\+1${filename_pattern}>" \
+ "gdb.format_address, result should have an offset"
+}
+
+if {![is_address_zero_readable]} {
+ gdb_test "python print(\"Got: \" + gdb.format_address(0))" \
+ "Got: 0x0" \
+ "gdb.format_address for address 0"
+}
+
+# Now check that gdb.format_address will accept the program space and
+# architecture arguments correctly.
+gdb_test_no_output "python inf = gdb.selected_inferior()"
+
+# First, pass both arguments, this should be fine.
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace, inf.architecture()))" \
+ "Got: $main_addr <main>" \
+ "gdb.format_address passing program space and architecture"
+
+# Now pass the program space and architecture as None.
+# First, pass both arguments, this should be fine.
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, None, None))" \
+ "Got: $main_addr <main>" \
+ "gdb.format_address passing program space and architecture as None"
+
+# Now forget the architecture, this should fail.
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace))" \
+ [multi_line \
+ "ValueError: The architecture and progspace arguments must both be supplied" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address passing program space only"
+
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, inf.progspace, None))" \
+ [multi_line \
+ "ValueError: The architecture and progspace arguments must both be supplied" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address passing real program space, but architecture is None"
+
+# Now skip the program space argument.
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, architecture=inf.architecture()))" \
+ [multi_line \
+ "ValueError: The architecture and progspace arguments must both be supplied" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address passing architecture only"
+
+gdb_test "python print(\"Got: \" + gdb.format_address($main_addr, None, inf.architecture()))" \
+ [multi_line \
+ "ValueError: The architecture and progspace arguments must both be supplied" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address passing real architecture, but progspace is None"
+
+# Now, before we add a second inferior, lets just check we can format
+# the address of 'foo' correctly.
+set foo_addr [get_hexadecimal_valueof "&foo" "UNKNOWN"]
+
+gdb_test "python print(\"Got: \" + gdb.format_address($foo_addr, inf.progspace, inf.architecture()))" \
+ "Got: $foo_addr <foo>" \
+ "gdb.format_address for foo, with just one inferior"
+
+# Now lets add a second inferior, using a slightly different
+# executable, select that inferior, and capture a reference to the
+# inferior in a Python object.
+gdb_test "add-inferior -exec ${binary_bar}" ".*" \
+ "add a second inferior running the bar executable"
+gdb_test "inferior 2" ".*"
+gdb_test_no_output "python inf2 = gdb.selected_inferior()"
+
+# Now we can test formatting an address from inferior 1.
+gdb_test "python print(\"Got: \" + gdb.format_address($foo_addr, inf.progspace, inf.architecture()))" \
+ "Got: $foo_addr <foo>" \
+ "gdb.format_address for foo, while inferior 2 is selected"
+
+# Grab the address of 'bar'. Hopefully this will be the same address
+# as 'foo', but if not, that's not the end of the world, the test just
+# wont be quite as tough.
+set bar_addr [get_hexadecimal_valueof "&bar" "UNKNOWN"]
+
+# Now format the address of bar using the default inferior and
+# architecture, this should display the 'bar' symbol rather than
+# 'foo'.
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr))" \
+ "Got: $foo_addr <bar>" \
+ "gdb.format_address for bar, while inferior 2 is selected"
+
+# And again, but this time, specificy the program space and
+# architecture.
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.architecture()))" \
+ "Got: $foo_addr <bar>" \
+ "gdb.format_address for bar, while inferior 2 is selected, pass progspace and architecture"
+
+# Reselect inferior 1, and then format an address from inferior 2.
+gdb_test "inferior 1" ".*"
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.architecture()))" \
+ "Got: $foo_addr <bar>" \
+ "gdb.format_address for bar, while inferior 1 is selected, pass progspace and architecture"
+
+# Try pasing incorrect object types for program space and architecture.
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.progspace, inf2.progspace))" \
+ [multi_line \
+ "TypeError: The architecture argument is not a gdb.Architecture object" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address pass wrong object type for architecture"
+
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, inf2.architecture(), inf2.architecture()))" \
+ [multi_line \
+ "TypeError: The progspace argument is not a gdb.Progspace object" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address pass wrong object type for progspace"
+
+# Now invalidate inferior 2's program space, and try using that.
+gdb_test "python pspace = inf2.progspace"
+gdb_test "python arch = inf2.architecture()"
+gdb_test "remove-inferior 2"
+gdb_test "python print(\"Got: \" + gdb.format_address($bar_addr, pspace, arch))" \
+ [multi_line \
+ "ValueError: The progspace argument is not valid" \
+ "Error while executing Python code\\."] \
+ "gdb.format_address called with an invalid program space"