开发者

gdb python programming: how to write code that will set breakpoints to every method of a C++ class?

I would like to be able to set breakpoints to every method of a C++ class in gdb. I think the easiest way to do this is probably python, since now python has complete access to gdb. I know very little python, and with gdb on top of it, it's even harder. I am wondering if anyone kn开发者_如何学运维ows how to write a class python code that sets breakpoints to every method of a named class in gdb.


Assuming you compiled with debugging symbols, you dont even need python for this:

rbreak source.cpp:.


Edit: I just noticed you're asking for how to do this with a C++ class, not a python one. Oops. I'll leave the answer up in the hope that it will be useful to anyone debugging a python extension...


A bit of googling finds: Python code can manipulate breakpoints via the gdb.Breakpoint class..

We can find all methods of a class like this:

import inspect
class Foo(object):
    bar = 1
    def baz(self):
        print "quoz"
inspect.getmembers(Foo, inspect.ismethod)
# [('baz', <unbound method Foo.baz>)] 

Putting it together:

def stop_hammertime(klass):
    methods = inspect.getmembers(klass, inspect.ismethod)
    method_names = [klass.__name__ + m[0] for m in methods]
    return [gdb.Breakpoint(m) for m in method_names]

Note: This is untested as I don't have the gdb module installed.


You can generate (for example using python) a .gdbrc file with a line containing 'break C::foo' for every function of your class C and then start gdb.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜