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