简体   繁体   English

如何通过Python在GDB中通过正则表达式设置断点

[英]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. 我想使用Python在GDB中编写一些行为脚本:给定一个描述一组函数的正则表达式,为匹配的每个函数实例化gdb.Breakpoint (例如MyBreakpoint )的子类。

There is no equivalent of rbreak in GDB's Python module. GDB的Python模块中没有等效的rbreak 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. 我的下一个想法是遍历所有可能使用的函数,并使用Python的re模块进行匹配。 However, there doesn't seem to be any way to list functions available for breaking from within Python. 但是,似乎没有任何方法可以列出可用于在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. 由于rbreak创造了新的断点对象,即使断点是作为预先存在断点相同的位置,你可以运行gdb.breakpoints()之前和执行之后rbreak看到,加入该断点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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