I’ve been trying to get at the file path, line and column information of a break point via the python API but have been struggling.
given the following method :
def do_thing_with_bp_in_file_on_line(file_path, line_no):
… do something with breakpoints on line_no in file at file_path
what would be the best way to find the break points that match the file_path and line number so I can act on them?
I’ve tried iterating through them and checking the file path and line number of one of a break points SBBreakpointLocation and drilling down the file spec and line entry information to compare with the parameters, but this seems long winded and has a bunch of assumptions baked into it.
Is there a more canonical way of doing this?
thanks in advance,
A breakpoint doesn’t necessarily have a file & line number. Remember that breakpoints in lldb really are “search kernels” - called “Resolvers" and one breakpoint’s resolver can match any number of locations in code. As an extreme instance “break set -r .*” will match many many functions… So the breakpoint itself doesn’t have a meaningful file and line number.
Only breakpoint locations have a single address, and thus a well-determined file & line number.
Moreover, even for a breakpoint that uses a file & line number based resolver, the resolved location’s file & line may not match the ones specified in the breakpoint. Unless you set —exact when setting the breakpoint, we move breakpoints from lines that contain no code to the nearest one that does contain code.
So a breakpoint’s notion of file and line if it even exists is not helpful in the task “find all the breakpoints that will stop when code from this given file & line are executed. Only the breakpoint locations know that.
As Jim said, we have many different kinds of breakpoints and breakpoints can have N SBBreakpointLocations and that number can change as your process runs and more shared libraries are loaded.
If you set a source file and line breakpoint to begin with, then iterating over the locations is the best way to see what you ended up with.
bp = target.BreakpointCreateByLocation("/tmp/main.cpp", 12)
for bp_loc in bp:
addr = bp_loc.GetAddress()
line_entry = addr.GetLineEntry()
file_spec = line_entry.GetFileSpec()
line = line_entry.GetLine()
column = line_entry.GetColumn()
If you just want the breakpoint source file, line and column as a string value, just run str() on the line_entry:
bp = lldb.target.BreakpointCreateByLocation("main.cpp", 6)
for l in bp:
It will print everything out for you into a single string. Note the print() statement above calls str() on "l.GetAddress().GetLineEntry()" for you.