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