diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2021-10-23 09:59:25 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2022-03-22 10:05:05 +0000 |
commit | 25209e2c6979c3838e14e099f0333609810db280 (patch) | |
tree | c3a6ecdc110db913c886182431031054a1461cf0 /gdb/doc | |
parent | 6c111a4ec26ba4a1343cb788c34e4bccce65bfeb (diff) | |
download | gdb-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/doc')
-rw-r--r-- | gdb/doc/python.texi | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 918418b..50b0fc8 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -615,6 +615,60 @@ currently active connection (@pxref{Connections In Python}). The connection objects are in no particular order in the returned list. @end defun +@defun gdb.format_address (@var{address} @r{[}, @var{progspace}, @var{architecture}@r{]}) +Return a string in the format @samp{@var{addr} +<@var{symbol}+@var{offset}>}, where @var{addr} is @var{address} +formatted in hexadecimal, @var{symbol} is the symbol whose address is +the nearest to @var{address} and below it in memory, and @var{offset} +is the offset from @var{symbol} to @var{address} in decimal. + +If no suitable @var{symbol} was found, then the +<@var{symbol}+@var{offset}> part is not included in the returned +string, instead the returned string will just contain the +@var{address} formatted as hexadecimal. How far @value{GDBN} looks +back for a suitable symbol can be controlled with @kbd{set print +max-symbolic-offset} (@pxref{Print Settings}). + +Additionally, the returned string can include file name and line +number information when @kbd{set print symbol-filename on} +(@pxref{Print Settings}), in this case the format of the returned +string is @samp{@var{addr} <@var{symbol}+@var{offset}> at +@var{filename}:@var{line-number}}. + + +The @var{progspace} is the gdb.Progspace in which @var{symbol} is +looked up, and @var{architecture} is used when formatting @var{addr}, +e.g.@: in order to determine the size of an address in bytes. + +If neither @var{progspace} or @var{architecture} are passed, then by +default @value{GDBN} will use the program space and architecture of +the currently selected inferior, thus, the following two calls are +equivalent: + +@smallexample +gdb.format_address(address) +gdb.format_address(address, + gdb.selected_inferior().progspace, + gdb.selected_inferior().architecture()) +@end smallexample + +It is not valid to only pass one of @var{progspace} or +@var{architecture}, either they must both be provided, or neither must +be provided (and the defaults will be used). + +This method uses the same mechanism for formatting address, symbol, +and offset information as core @value{GDBN} does in commands such as +@kbd{disassemble}. + +Here are some examples of the possible string formats: + +@smallexample +0x00001042 +0x00001042 <symbol+16> +0x00001042 <symbol+16 at file.c:123> +@end smallexample +@end defun + @node Exception Handling @subsubsection Exception Handling @cindex python exceptions |