Blame SOURCES/gdb-dts-rhel6-python-compat.patch

6240d7
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
6240d7
From: Fedora GDB patches <invalid@email.com>
6240d7
Date: Fri, 27 Oct 2017 21:07:50 +0200
6240d7
Subject: gdb-dts-rhel6-python-compat.patch
6240d7
6240d7
;; [rhel6] DTS backward Python compatibility API (BZ 1020004, Phil Muldoon).
6240d7
;;=fedora
6240d7
6240d7
https://bugzilla.redhat.com/show_bug.cgi?id=1020004
6240d7
6240d7
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
6240d7
--- a/gdb/data-directory/Makefile.in
6240d7
+++ b/gdb/data-directory/Makefile.in
6240d7
@@ -71,6 +71,8 @@ PYTHON_FILE_LIST = \
6240d7
 	gdb/__init__.py \
6240d7
 	gdb/FrameDecorator.py \
6240d7
 	gdb/FrameIterator.py \
6240d7
+	gdb/FrameWrapper.py \
6240d7
+	gdb/backtrace.py \
6240d7
 	gdb/frames.py \
6240d7
 	gdb/printing.py \
6240d7
 	gdb/prompt.py \
6240d7
@@ -79,6 +81,7 @@ PYTHON_FILE_LIST = \
6240d7
 	gdb/xmethod.py \
6240d7
 	gdb/command/__init__.py \
6240d7
 	gdb/command/explore.py \
6240d7
+	gdb/command/backtrace.py \
6240d7
 	gdb/command/frame_filters.py \
6240d7
 	gdb/command/pretty_printers.py \
6240d7
 	gdb/command/prompt.py \
6240d7
diff --git a/gdb/python/lib/gdb/FrameWrapper.py b/gdb/python/lib/gdb/FrameWrapper.py
6240d7
new file mode 100644
6240d7
--- /dev/null
6240d7
+++ b/gdb/python/lib/gdb/FrameWrapper.py
6240d7
@@ -0,0 +1,122 @@
6240d7
+# Wrapper API for frames.
6240d7
+
6240d7
+# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
6240d7
+
6240d7
+# This program is free software; you can redistribute it and/or modify
6240d7
+# it under the terms of the GNU General Public License as published by
6240d7
+# the Free Software Foundation; either version 3 of the License, or
6240d7
+# (at your option) any later version.
6240d7
+#
6240d7
+# This program is distributed in the hope that it will be useful,
6240d7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6240d7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6240d7
+# GNU General Public License for more details.
6240d7
+#
6240d7
+# You should have received a copy of the GNU General Public License
6240d7
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6240d7
+
6240d7
+import gdb
6240d7
+
6240d7
+# FIXME: arguably all this should be on Frame somehow.
6240d7
+class FrameWrapper:
6240d7
+    def __init__ (self, frame):
6240d7
+        self.frame = frame;
6240d7
+
6240d7
+    def write_symbol (self, stream, sym, block):
6240d7
+        if len (sym.linkage_name):
6240d7
+            nsym, is_field_of_this = gdb.lookup_symbol (sym.linkage_name, block)
6240d7
+            if nsym.addr_class != gdb.SYMBOL_LOC_REGISTER:
6240d7
+                sym = nsym
6240d7
+
6240d7
+        stream.write (sym.print_name + "=")
6240d7
+        try:
6240d7
+            val = self.read_var (sym)
6240d7
+            if val != None:
6240d7
+                val = str (val)
6240d7
+        # FIXME: would be nice to have a more precise exception here.
6240d7
+        except RuntimeError as text:
6240d7
+            val = text
6240d7
+        if val == None:
6240d7
+            stream.write ("???")
6240d7
+        else:
6240d7
+            stream.write (str (val))
6240d7
+
6240d7
+    def print_frame_locals (self, stream, func):
6240d7
+
6240d7
+        try:
6240d7
+            block = self.frame.block()
6240d7
+        except RuntimeError:
6240d7
+            block = None
6240d7
+
6240d7
+        while block != None:
6240d7
+            if block.is_global or block.is_static:
6240d7
+                break
6240d7
+
6240d7
+        for sym in block:
6240d7
+            if sym.is_argument:
6240d7
+                continue;
6240d7
+
6240d7
+            self.write_symbol (stream, sym, block)
6240d7
+            stream.write ('\n')
6240d7
+
6240d7
+    def print_frame_args (self, stream, func):
6240d7
+
6240d7
+        try:
6240d7
+            block = self.frame.block()
6240d7
+        except RuntimeError:
6240d7
+            block = None
6240d7
+
6240d7
+        while block != None:
6240d7
+            if block.function != None:
6240d7
+                break
6240d7
+            block = block.superblock
6240d7
+
6240d7
+        first = True
6240d7
+        for sym in block:
6240d7
+            if not sym.is_argument:
6240d7
+                continue;
6240d7
+
6240d7
+            if not first:
6240d7
+                stream.write (", ")
6240d7
+
6240d7
+            self.write_symbol (stream, sym, block)
6240d7
+            first = False
6240d7
+
6240d7
+    # FIXME: this should probably just be a method on gdb.Frame.
6240d7
+    # But then we need stream wrappers.
6240d7
+    def describe (self, stream, full):
6240d7
+        if self.type () == gdb.DUMMY_FRAME:
6240d7
+            stream.write (" <function called from gdb>\n")
6240d7
+        elif self.type () == gdb.SIGTRAMP_FRAME:
6240d7
+            stream.write (" <signal handler called>\n")
6240d7
+        else:
6240d7
+            sal = self.find_sal ()
6240d7
+            pc = self.pc ()
6240d7
+            name = self.name ()
6240d7
+            if not name:
6240d7
+                name = "??"
6240d7
+            if pc != sal.pc or not sal.symtab:
6240d7
+                stream.write (" 0x%08x in" % pc)
6240d7
+            stream.write (" " + name + " (")
6240d7
+
6240d7
+            func = self.function ()
6240d7
+            self.print_frame_args (stream, func)
6240d7
+
6240d7
+            stream.write (")")
6240d7
+
6240d7
+            if sal.symtab and sal.symtab.filename:
6240d7
+                stream.write (" at " + sal.symtab.filename)
6240d7
+                stream.write (":" + str (sal.line))
6240d7
+
6240d7
+            if not self.name () or (not sal.symtab or not sal.symtab.filename):
6240d7
+                lib = gdb.solib_name (pc)
6240d7
+                if lib:
6240d7
+                    stream.write (" from " + lib)
6240d7
+
6240d7
+            stream.write ("\n")
6240d7
+
6240d7
+            if full:
6240d7
+                self.print_frame_locals (stream, func)
6240d7
+
6240d7
+    def __getattr__ (self, name):
6240d7
+        return getattr (self.frame, name)
6240d7
diff --git a/gdb/python/lib/gdb/backtrace.py b/gdb/python/lib/gdb/backtrace.py
6240d7
new file mode 100644
6240d7
--- /dev/null
6240d7
+++ b/gdb/python/lib/gdb/backtrace.py
6240d7
@@ -0,0 +1,42 @@
6240d7
+# Filtering backtrace.
6240d7
+
6240d7
+# Copyright (C) 2008, 2011 Free Software Foundation, Inc.
6240d7
+
6240d7
+# This program is free software; you can redistribute it and/or modify
6240d7
+# it under the terms of the GNU General Public License as published by
6240d7
+# the Free Software Foundation; either version 3 of the License, or
6240d7
+# (at your option) any later version.
6240d7
+#
6240d7
+# This program is distributed in the hope that it will be useful,
6240d7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6240d7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6240d7
+# GNU General Public License for more details.
6240d7
+#
6240d7
+# You should have received a copy of the GNU General Public License
6240d7
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6240d7
+
6240d7
+import gdb
6240d7
+import itertools
6240d7
+
6240d7
+# Our only exports.
6240d7
+__all__ = ['push_frame_filter', 'create_frame_filter']
6240d7
+
6240d7
+old_frame_filter = None
6240d7
+
6240d7
+def push_frame_filter (constructor):
6240d7
+    """Register a new backtrace filter class with the 'backtrace' command.
6240d7
+The filter will be passed an iterator as an argument.  The iterator
6240d7
+will return gdb.Frame-like objects.  The filter should in turn act as
6240d7
+an iterator returning such objects."""
6240d7
+    global old_frame_filter
6240d7
+    if old_frame_filter == None:
6240d7
+        old_frame_filter = constructor
6240d7
+    else:
6240d7
+        old_frame_filter = lambda iterator, filter = frame_filter: constructor (filter(iterator))
6240d7
+
6240d7
+def create_frame_filter (iter):
6240d7
+    global old_frame_filter
6240d7
+    if old_frame_filter is None:
6240d7
+        return iter
6240d7
+    return old_frame_filter (iter)
6240d7
+
6240d7
diff --git a/gdb/python/lib/gdb/command/backtrace.py b/gdb/python/lib/gdb/command/backtrace.py
6240d7
new file mode 100644
6240d7
--- /dev/null
6240d7
+++ b/gdb/python/lib/gdb/command/backtrace.py
6240d7
@@ -0,0 +1,106 @@
6240d7
+# New backtrace command.
6240d7
+
6240d7
+# Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
6240d7
+
6240d7
+# This program is free software; you can redistribute it and/or modify
6240d7
+# it under the terms of the GNU General Public License as published by
6240d7
+# the Free Software Foundation; either version 3 of the License, or
6240d7
+# (at your option) any later version.
6240d7
+#
6240d7
+# This program is distributed in the hope that it will be useful,
6240d7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6240d7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6240d7
+# GNU General Public License for more details.
6240d7
+#
6240d7
+# You should have received a copy of the GNU General Public License
6240d7
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6240d7
+
6240d7
+import gdb
6240d7
+import gdb.backtrace
6240d7
+import itertools
6240d7
+from gdb.FrameIterator import FrameIterator
6240d7
+from gdb.FrameWrapper import FrameWrapper
6240d7
+import sys
6240d7
+
6240d7
+class ReverseBacktraceParameter (gdb.Parameter):
6240d7
+    """The new-backtrace command can show backtraces in 'reverse' order.
6240d7
+This means that the innermost frame will be printed last.
6240d7
+Note that reverse backtraces are more expensive to compute."""
6240d7
+
6240d7
+    set_doc = "Enable or disable reverse backtraces."
6240d7
+    show_doc = "Show whether backtraces will be printed in reverse order."
6240d7
+
6240d7
+    def __init__(self):
6240d7
+        gdb.Parameter.__init__ (self, "reverse-backtrace",
6240d7
+                                gdb.COMMAND_STACK, gdb.PARAM_BOOLEAN)
6240d7
+        # Default to compatibility with gdb.
6240d7
+        self.value = False
6240d7
+
6240d7
+class FilteringBacktrace (gdb.Command):
6240d7
+    """Print backtrace of all stack frames, or innermost COUNT frames.
6240d7
+With a negative argument, print outermost -COUNT frames.
6240d7
+Use of the 'full' qualifier also prints the values of the local variables.
6240d7
+Use of the 'raw' qualifier avoids any filtering by loadable modules.
6240d7
+"""
6240d7
+
6240d7
+    def __init__ (self):
6240d7
+        # FIXME: this is not working quite well enough to replace
6240d7
+        # "backtrace" yet.
6240d7
+        gdb.Command.__init__ (self, "new-backtrace", gdb.COMMAND_STACK)
6240d7
+        self.reverse = ReverseBacktraceParameter()
6240d7
+
6240d7
+    def reverse_iter (self, iter):
6240d7
+        result = []
6240d7
+        for item in iter:
6240d7
+            result.append (item)
6240d7
+        result.reverse()
6240d7
+        return result
6240d7
+
6240d7
+    def final_n (self, iter, x):
6240d7
+        result = []
6240d7
+        for item in iter:
6240d7
+            result.append (item)
6240d7
+        return result[x:]
6240d7
+
6240d7
+    def invoke (self, arg, from_tty):
6240d7
+        i = 0
6240d7
+        count = 0
6240d7
+        filter = True
6240d7
+        full = False
6240d7
+
6240d7
+        for word in arg.split (" "):
6240d7
+            if word == '':
6240d7
+                continue
6240d7
+            elif word == 'raw':
6240d7
+                filter = False
6240d7
+            elif word == 'full':
6240d7
+                full = True
6240d7
+            else:
6240d7
+                count = int (word)
6240d7
+
6240d7
+        # FIXME: provide option to start at selected frame
6240d7
+        # However, should still number as if starting from newest
6240d7
+        newest_frame = gdb.newest_frame()
6240d7
+        iter = itertools.imap (FrameWrapper,
6240d7
+                               FrameIterator (newest_frame))
6240d7
+        if filter:
6240d7
+            iter = gdb.backtrace.create_frame_filter (iter)
6240d7
+
6240d7
+        # Now wrap in an iterator that numbers the frames.
6240d7
+        iter = itertools.izip (itertools.count (0), iter)
6240d7
+
6240d7
+        # Reverse if the user wanted that.
6240d7
+        if self.reverse.value:
6240d7
+            iter = self.reverse_iter (iter)
6240d7
+
6240d7
+        # Extract sub-range user wants.
6240d7
+        if count < 0:
6240d7
+            iter = self.final_n (iter, count)
6240d7
+        elif count > 0:
6240d7
+            iter = itertools.islice (iter, 0, count)
6240d7
+
6240d7
+        for pair in iter:
6240d7
+            sys.stdout.write ("#%-2d" % pair[0])
6240d7
+            pair[1].describe (sys.stdout, full)
6240d7
+
6240d7
+FilteringBacktrace()