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

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