简体   繁体   中英

How can I set breakpoints by regular expression in GDB via Python

I would like to script some behaviour in GDB using Python: given a regular expression describing a set of functions, instantiate a subclass of gdb.Breakpoint (eg. MyBreakpoint ) for each function matched.

There is no equivalent of rbreak in GDB's Python module. I had thought of doing this:

gdb.execute('rbreak {:s}'.format(regexp))
breakpoints = gdb.breakpoints()

# Extract breakpoint strings, delete existing breakpoints, and
# recreate them using my subclass.
for bp in breakpoints:
    loc = bp.location
    bp.delete()
    MyBreakpoint(loc)

...however this suffers from the problem that there might already be some user defined breakpoints, and this would destroy them.

My next idea was to iterate over all possible functions to break on, and do the matching using Python's re module. However, there doesn't seem to be any way to list functions available for breaking from within Python.

My question is: could either of these approaches be salvaged so that they will work reliably and not clobber state set by a user in an interactive session; or is there some other way to achieve this? Or will I have to compromise on "not clobbering user state?"

Since rbreak creates new breakpoint objects, even if the breakpoints are for the same locations as pre-existing breakpoints, you can run gdb.breakpoints() before and after the execution of rbreak to see which breakpoints were added.

obreakpoints = gdb.breakpoints();
gdb.execute('rbreak {:s}'.format(regexp))
breakpoints = set(gdb.breakpoints()).difference(set(obreakpoints))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM