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