|
|
ae2451 |
--- Tools/gdb/libpython.py.orig 2013-10-09 10:54:59.894701668 +0200
|
|
|
ae2451 |
+++ Tools/gdb/libpython.py 2013-10-09 11:09:30.278703290 +0200
|
|
|
ae2451 |
@@ -1194,39 +1194,113 @@
|
|
|
ae2451 |
iter_frame = iter_frame.newer()
|
|
|
ae2451 |
return index
|
|
|
ae2451 |
|
|
|
ae2451 |
+ # We divide frames into:
|
|
|
ae2451 |
+ # - "python frames":
|
|
|
ae2451 |
+ # - "bytecode frames" i.e. PyEval_EvalFrameEx
|
|
|
ae2451 |
+ # - "other python frames": things that are of interest from a python
|
|
|
ae2451 |
+ # POV, but aren't bytecode (e.g. GC, GIL)
|
|
|
ae2451 |
+ # - everything else
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ def is_python_frame(self):
|
|
|
ae2451 |
+ '''Is this a PyEval_EvalFrameEx frame, or some other important
|
|
|
ae2451 |
+ frame? (see is_other_python_frame for what "important" means in this
|
|
|
ae2451 |
+ context)'''
|
|
|
ae2451 |
+ if self.is_evalframeex():
|
|
|
ae2451 |
+ return True
|
|
|
ae2451 |
+ if self.is_other_python_frame():
|
|
|
ae2451 |
+ return True
|
|
|
ae2451 |
+ return False
|
|
|
ae2451 |
+
|
|
|
ae2451 |
def is_evalframeex(self):
|
|
|
ae2451 |
- '''Is this a PyEval_EvalFrameEx frame?'''
|
|
|
ae2451 |
- if self._gdbframe.name() == 'PyEval_EvalFrameEx':
|
|
|
ae2451 |
- '''
|
|
|
ae2451 |
- I believe we also need to filter on the inline
|
|
|
ae2451 |
- struct frame_id.inline_depth, only regarding frames with
|
|
|
ae2451 |
- an inline depth of 0 as actually being this function
|
|
|
ae2451 |
-
|
|
|
ae2451 |
- So we reject those with type gdb.INLINE_FRAME
|
|
|
ae2451 |
- '''
|
|
|
ae2451 |
- if self._gdbframe.type() == gdb.NORMAL_FRAME:
|
|
|
ae2451 |
- # We have a PyEval_EvalFrameEx frame:
|
|
|
ae2451 |
- return True
|
|
|
ae2451 |
+ if self._gdbframe.function():
|
|
|
ae2451 |
+ if self._gdbframe.function().name == 'PyEval_EvalFrameEx':
|
|
|
ae2451 |
+ '''
|
|
|
ae2451 |
+ I believe we also need to filter on the inline
|
|
|
ae2451 |
+ struct frame_id.inline_depth, only regarding frames with
|
|
|
ae2451 |
+ an inline depth of 0 as actually being this function
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ So we reject those with type gdb.INLINE_FRAME
|
|
|
ae2451 |
+ '''
|
|
|
ae2451 |
+ if self._gdbframe.type() == gdb.NORMAL_FRAME:
|
|
|
ae2451 |
+ # We have a PyEval_EvalFrameEx frame:
|
|
|
ae2451 |
+ return True
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ return False
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ def is_other_python_frame(self):
|
|
|
ae2451 |
+ '''Is this frame worth displaying in python backtraces?
|
|
|
ae2451 |
+ Examples:
|
|
|
ae2451 |
+ - waiting on the GIL
|
|
|
ae2451 |
+ - garbage-collecting
|
|
|
ae2451 |
+ - within a CFunction
|
|
|
ae2451 |
+ If it is, return a descriptive string
|
|
|
ae2451 |
+ For other frames, return False
|
|
|
ae2451 |
+ '''
|
|
|
ae2451 |
+ if self.is_waiting_for_gil():
|
|
|
ae2451 |
+ return 'Waiting for a lock (e.g. GIL)'
|
|
|
ae2451 |
+ elif self.is_gc_collect():
|
|
|
ae2451 |
+ return 'Garbage-collecting'
|
|
|
ae2451 |
+ else:
|
|
|
ae2451 |
+ # Detect invocations of PyCFunction instances:
|
|
|
ae2451 |
+ if self._gdbframe.name() == 'PyCFunction_Call':
|
|
|
ae2451 |
+ try:
|
|
|
ae2451 |
+ func = self._gdbframe.read_var('func')
|
|
|
ae2451 |
+ # Use the prettyprinter for the func:
|
|
|
ae2451 |
+ return str(func)
|
|
|
ae2451 |
+ except RuntimeError:
|
|
|
ae2451 |
+ return 'PyCFunction invocation (unable to read "func")'
|
|
|
ae2451 |
+ older = self.older()
|
|
|
ae2451 |
+ if older and older._gdbframe.name() == 'call_function':
|
|
|
ae2451 |
+ # Within that frame:
|
|
|
ae2451 |
+ # 'call_function' contains, amongst other things, a
|
|
|
ae2451 |
+ # hand-inlined copy of PyCFunction_Call.
|
|
|
ae2451 |
+ # "func" is the local containing the PyObject* of the
|
|
|
ae2451 |
+ # callable instance
|
|
|
ae2451 |
+ # Report it, but only if it's a PyCFunction (since otherwise
|
|
|
ae2451 |
+ # we'd be reporting an implementation detail of every other
|
|
|
ae2451 |
+ # function invocation)
|
|
|
ae2451 |
+ try:
|
|
|
ae2451 |
+ func = older._gdbframe.read_var('func')
|
|
|
ae2451 |
+ funcobj = PyObjectPtr.from_pyobject_ptr(func)
|
|
|
ae2451 |
+ if isinstance(funcobj, PyCFunctionObjectPtr):
|
|
|
ae2451 |
+ # Use the prettyprinter for the func:
|
|
|
ae2451 |
+ return str(func)
|
|
|
ae2451 |
+ except RuntimeError:
|
|
|
ae2451 |
+ return False
|
|
|
ae2451 |
|
|
|
ae2451 |
+ # This frame isn't worth reporting:
|
|
|
ae2451 |
return False
|
|
|
ae2451 |
|
|
|
ae2451 |
+ def is_waiting_for_gil(self):
|
|
|
ae2451 |
+ '''Is this frame waiting for a lock?'''
|
|
|
ae2451 |
+ framename = self._gdbframe.name()
|
|
|
ae2451 |
+ if framename:
|
|
|
ae2451 |
+ return 'pthread_cond_timedwait' in framename or \
|
|
|
ae2451 |
+ 'PyThread_acquire_lock' in framename
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ def is_gc_collect(self):
|
|
|
ae2451 |
+ '''Is this frame "collect" within the the garbage-collector?'''
|
|
|
ae2451 |
+ return self._gdbframe.name() == 'collect'
|
|
|
ae2451 |
+
|
|
|
ae2451 |
def get_pyop(self):
|
|
|
ae2451 |
try:
|
|
|
ae2451 |
f = self._gdbframe.read_var('f')
|
|
|
ae2451 |
- frame = PyFrameObjectPtr.from_pyobject_ptr(f)
|
|
|
ae2451 |
- if not frame.is_optimized_out():
|
|
|
ae2451 |
- return frame
|
|
|
ae2451 |
- # gdb is unable to get the "f" argument of PyEval_EvalFrameEx()
|
|
|
ae2451 |
- # because it was "optimized out". Try to get "f" from the frame
|
|
|
ae2451 |
- # of the caller, PyEval_EvalCodeEx().
|
|
|
ae2451 |
- orig_frame = frame
|
|
|
ae2451 |
- caller = self._gdbframe.older()
|
|
|
ae2451 |
- if caller:
|
|
|
ae2451 |
- f = caller.read_var('f')
|
|
|
ae2451 |
- frame = PyFrameObjectPtr.from_pyobject_ptr(f)
|
|
|
ae2451 |
- if not frame.is_optimized_out():
|
|
|
ae2451 |
- return frame
|
|
|
ae2451 |
- return orig_frame
|
|
|
ae2451 |
+ obj = PyFrameObjectPtr.from_pyobject_ptr(f)
|
|
|
ae2451 |
+ if isinstance(obj, PyFrameObjectPtr):
|
|
|
ae2451 |
+ return obj
|
|
|
ae2451 |
+ else:
|
|
|
ae2451 |
+ return None
|
|
|
ae2451 |
+ except ValueError:
|
|
|
ae2451 |
+ return None
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ def get_py_co(self):
|
|
|
ae2451 |
+ try:
|
|
|
ae2451 |
+ co = self._gdbframe.read_var('co')
|
|
|
ae2451 |
+ obj = PyCodeObjectPtr.from_pyobject_ptr(co)
|
|
|
ae2451 |
+ if isinstance(obj, PyCodeObjectPtr):
|
|
|
ae2451 |
+ return obj
|
|
|
ae2451 |
+ else:
|
|
|
ae2451 |
+ return None
|
|
|
ae2451 |
except ValueError:
|
|
|
ae2451 |
return None
|
|
|
ae2451 |
|
|
|
ae2451 |
@@ -1239,8 +1313,22 @@
|
|
|
ae2451 |
|
|
|
ae2451 |
@classmethod
|
|
|
ae2451 |
def get_selected_python_frame(cls):
|
|
|
ae2451 |
- '''Try to obtain the Frame for the python code in the selected frame,
|
|
|
ae2451 |
- or None'''
|
|
|
ae2451 |
+ '''Try to obtain the Frame for the python-related code in the selected
|
|
|
ae2451 |
+ frame, or None'''
|
|
|
ae2451 |
+ frame = cls.get_selected_frame()
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ while frame:
|
|
|
ae2451 |
+ if frame.is_python_frame():
|
|
|
ae2451 |
+ return frame
|
|
|
ae2451 |
+ frame = frame.older()
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ # Not found:
|
|
|
ae2451 |
+ return None
|
|
|
ae2451 |
+
|
|
|
ae2451 |
+ @classmethod
|
|
|
ae2451 |
+ def get_selected_bytecode_frame(cls):
|
|
|
ae2451 |
+ '''Try to obtain the Frame for the python bytecode interpreter in the
|
|
|
ae2451 |
+ selected GDB frame, or None'''
|
|
|
ae2451 |
frame = cls.get_selected_frame()
|
|
|
ae2451 |
|
|
|
ae2451 |
while frame:
|
|
|
ae2451 |
@@ -1265,7 +1353,11 @@
|
|
|
ae2451 |
else:
|
|
|
ae2451 |
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
|
|
|
ae2451 |
else:
|
|
|
ae2451 |
- sys.stdout.write('#%i\n' % self.get_index())
|
|
|
ae2451 |
+ info = self.is_other_python_frame()
|
|
|
ae2451 |
+ if info:
|
|
|
ae2451 |
+ sys.stdout.write('#%i %s\n' % (self.get_index(), info))
|
|
|
ae2451 |
+ else:
|
|
|
ae2451 |
+ sys.stdout.write('#%i\n' % self.get_index())
|
|
|
ae2451 |
|
|
|
ae2451 |
class PyList(gdb.Command):
|
|
|
ae2451 |
'''List the current Python source code, if any
|
|
|
ae2451 |
@@ -1301,7 +1393,7 @@
|
|
|
ae2451 |
if m:
|
|
|
ae2451 |
start, end = map(int, m.groups())
|
|
|
ae2451 |
|
|
|
ae2451 |
- frame = Frame.get_selected_python_frame()
|
|
|
ae2451 |
+ frame = Frame.get_selected_bytecode_frame()
|
|
|
ae2451 |
if not frame:
|
|
|
ae2451 |
print 'Unable to locate python frame'
|
|
|
ae2451 |
return
|
|
|
ae2451 |
@@ -1353,7 +1445,7 @@
|
|
|
ae2451 |
if not iter_frame:
|
|
|
ae2451 |
break
|
|
|
ae2451 |
|
|
|
ae2451 |
- if iter_frame.is_evalframeex():
|
|
|
ae2451 |
+ if iter_frame.is_python_frame():
|
|
|
ae2451 |
# Result:
|
|
|
ae2451 |
if iter_frame.select():
|
|
|
ae2451 |
iter_frame.print_summary()
|
|
|
ae2451 |
@@ -1407,7 +1499,7 @@
|
|
|
ae2451 |
def invoke(self, args, from_tty):
|
|
|
ae2451 |
frame = Frame.get_selected_python_frame()
|
|
|
ae2451 |
while frame:
|
|
|
ae2451 |
- if frame.is_evalframeex():
|
|
|
ae2451 |
+ if frame.is_python_frame():
|
|
|
ae2451 |
frame.print_summary()
|
|
|
ae2451 |
frame = frame.older()
|
|
|
ae2451 |
|