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