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