简体   繁体   中英

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 knows 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.

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