Skip to content
Snippets Groups Projects
  • Andrew Burgess's avatar
    25209e2c
    gdb/python: add gdb.format_address function · 25209e2c
    Andrew Burgess authored
    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.
    25209e2c
    History
    gdb/python: add gdb.format_address function
    Andrew Burgess authored
    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.