Bug in gdb.block_for_pc documentation?

Simon Marchi simon.marchi@polymtl.ca
Fri Dec 22 21:58:00 GMT 2017

On 2017-12-21 20:15, Roman Popov wrote:
> Hi all,
> Looks like there is a bug in docs:
> *"Function: gdb.block_for_pc (pc)*
> *Return the innermost gdb.Block containing the given pc value. If the 
> block
> cannot be found for the pc value specified, the function will return 
> None."*
> https://sourceware.org/gdb/current/onlinedocs/gdb/Blocks-In-Python.html#Blocks-In-Python
> Instead of returning None it throws runtime exception:
> gdb.block_for_pc(0)
>>> RuntimeError: Cannot locate object file for block.
> Thanks,
> Roman

Thanks for reporting.  The corresponding code is here:

There are two failure cases, as far as I can see

1) We can't find a compilation unit for the given pc -> we throw an 
2) We can find a compilation unit for the given pc, but can't find a 
corresponding block -> we return None

It's easy to trigger 1), you just call block_for_pc with a bad random 
address.  I don't know how to trigger 2).  You would have to pass a pc 
covered by a compilation unit, but not covered by a block, not sure when 
that happens.

Now we have two choices, either we adapt the documentation to match the 
reality, which easy.  Or we change the code to match the documentation, 
which is harder because we risk breaking user scripts.

I don't really like the current situation, because the exception thrown, 
RuntimeError, is very generic.  So if one uses "except RuntimeError" to 
catch the case where block_for_pc fails, they risk catching other 
things, like RecursionError, which derives from RuntimeError.  Returning 
None, as the documentation suggests, would make more sense to me...


More information about the Gdb mailing list