Skip to content

Instantly share code, notes, and snippets.

@rhizoome
Created June 8, 2017 18:58
Show Gist options
  • Save rhizoome/2456ed7a83a1f547947ea43798ae339a to your computer and use it in GitHub Desktop.
Save rhizoome/2456ed7a83a1f547947ea43798ae339a to your computer and use it in GitHub Desktop.
py-break
class PyNameEquals(gdb.Function):
def __init__(self):
gdb.Function.__init__ (self,
"pyname_equals")
def _get_pycurframe_attr(self, attr):
frame = Frame(gdb.selected_frame())
if frame.is_evalframeex():
pyframe = frame.get_pyop()
if pyframe is None:
warnings.warn("Use a Python debug build, Python breakpoints "
"won't work otherwise.")
return None
return getattr(pyframe, attr).proxyval(set())
return None
def invoke(self, funcname):
attr = self._get_pycurframe_attr('co_name')
return attr is not None and attr == funcname.string()
PyNameEquals()
class PyModEquals(PyNameEquals):
def __init__(self):
gdb.Function.__init__ (self,
"pymod_equals")
def invoke(self, modname):
attr = self._get_pycurframe_attr('co_filename')
if attr is not None:
filename, ext = os.path.splitext(os.path.basename(attr))
return filename == modname.string()
return False
PyModEquals()
class PyBreak(gdb.Command):
"""
Set a Python breakpoint. Examples:
Break on any function or method named 'func' in module 'modname'
py-break modname.func
Break on any function or method named 'func'
py-break func
"""
def __init__(self):
gdb.Command.__init__ (self,
"py-break",
gdb.COMMAND_RUNNING,
gdb.COMPLETE_NONE)
def invoke(self, funcname, from_tty):
if '.' in funcname:
modname, dot, funcname = funcname.rpartition('.')
cond = '$pyname_equals("%s") && $pymod_equals("%s")' % (funcname,
modname)
else:
cond = '$pyname_equals("%s")' % funcname
gdb.execute('break PyEval_EvalFrameEx if ' + cond)
PyBreak()
@rhizoome
Copy link
Author

rhizoome commented Jun 8, 2017

Add to libpython.py and install python3-dbg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment