Blame SOURCES/gdb-rhbz1197665-fix-applyframefilter-backport.patch

01917d
commit 21909fa1c6d934bfa0c7ad3ef95909db48f6f756
01917d
Author: Tom Tromey <tromey@redhat.com>
01917d
Date:   Wed Jan 22 08:10:01 2014 -0700
01917d
01917d
    fix crash in frame filters
01917d
    
01917d
    apply_frame_filter calls ensure_python_env before computing the
01917d
    gdbarch to use.  This means that python_gdbarch can be NULL while in
01917d
    Python code, and if a frame filter depends on this somehow (easy to
01917d
    do), gdb will crash.
01917d
    
01917d
    The fix is to compute the gdbarch first.
01917d
    
01917d
    Built and regtested on x86-64 Fedora 18.
01917d
    New test case included.
01917d
    
01917d
    2014-01-23  Tom Tromey  <tromey@redhat.com>
01917d
    
01917d
    	PR python/16491:
01917d
    	* python/py-framefilter.c (apply_frame_filter): Call
01917d
    	ensure_python_env after computing gdbarch.
01917d
    
01917d
    2014-01-23  Tom Tromey  <tromey@redhat.com>
01917d
    
01917d
    	PR python/16491:
01917d
    	* gdb.python/py-framefilter.py (Reverse_Function.function): Read a
01917d
    	string from an inferior frame.
01917d
    	* gdb.python/py-framefilter-mi.exp: Update.
01917d
01917d
Index: gdb-7.6.1/gdb/python/py-framefilter.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/python/py-framefilter.c
01917d
+++ gdb-7.6.1/gdb/python/py-framefilter.c
01917d
@@ -1468,18 +1468,18 @@ apply_frame_filter (struct frame_info *f
01917d
   PyObject *item;
01917d
   htab_t levels_printed;
01917d
 
01917d
-  cleanups = ensure_python_env (gdbarch, current_language);
01917d
-
01917d
   TRY_CATCH (except, RETURN_MASK_ALL)
01917d
     {
01917d
       gdbarch = get_frame_arch (frame);
01917d
     }
01917d
   if (except.reason < 0)
01917d
     {
01917d
-      gdbpy_convert_exception (except);
01917d
-      goto error;
01917d
+      /* Let gdb try to print the stack trace.  */
01917d
+      return PY_BT_NO_FILTERS;
01917d
     }
01917d
 
01917d
+  cleanups = ensure_python_env (gdbarch, current_language);
01917d
+
01917d
   iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);
01917d
 
01917d
   if (iterable == NULL)
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.python/py-framefilter-mi.exp
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.python/py-framefilter-mi.exp
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.python/py-framefilter-mi.exp
01917d
@@ -66,10 +66,10 @@ mi_continue_to_line [gdb_get_line_number
01917d
   "step to breakpoint"
01917d
 
01917d
 mi_gdb_test "-stack-list-frames" \
01917d
-    "\\^done,stack=\\\[frame={level=\"0\",addr=\"$hex\",func=\"cnuf_dne\".*},frame={level=\"1\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"2\",addr=\"$hex\",func=\"bcnuf\".*},frame={level=\"3\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"22\",addr=\"$hex\",func=\"1cnuf\".*,children=\\\[frame={level=\"23\",addr=\"$hex\",func=\"func2\".*}\\\]},frame={level=\"24\",addr=\"$hex\",func=\"3cnuf\".*},frame={level=\"27\",addr=\"$hex\",func=\"niam\".*}\\\].*" \
01917d
+    "\\^done,stack=\\\[frame={level=\"0\",addr=\"$hex\",func=\"cnuf_dne.*\".*},frame={level=\"1\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"2\",addr=\"$hex\",func=\"bcnuf\".*},frame={level=\"3\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"22\",addr=\"$hex\",func=\"1cnuf\".*,children=\\\[frame={level=\"23\",addr=\"$hex\",func=\"func2\".*}\\\]},frame={level=\"24\",addr=\"$hex\",func=\"3cnuf\".*},frame={level=\"27\",addr=\"$hex\",func=\"niam\".*}\\\].*" \
01917d
     "filtered stack listing"
01917d
 mi_gdb_test "-stack-list-frames 0 3" \
01917d
-    "\\^done,stack=\\\[frame={level=\"0\",addr=\"$hex\",func=\"cnuf_dne\".*},frame={level=\"1\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"2\",addr=\"$hex\",func=\"bcnuf\".*},frame={level=\"3\",addr=\"$hex\",func=\"acnuf\".*}\\\]" \
01917d
+    "\\^done,stack=\\\[frame={level=\"0\",addr=\"$hex\",func=\"cnuf_dne.*\".*},frame={level=\"1\",addr=\"$hex\",func=\"acnuf\".*},frame={level=\"2\",addr=\"$hex\",func=\"bcnuf\".*},frame={level=\"3\",addr=\"$hex\",func=\"acnuf\".*}\\\]" \
01917d
     "filtered stack list 0 3"
01917d
 mi_gdb_test "-stack-list-frames 22 24" \
01917d
     "\\^done,stack=\\\[frame={level=\"22\",addr=\"$hex\",func=\"1cnuf\".*,children=\\\[frame={level=\"23\",addr=\"$hex\",func=\"func2\".*}\\\]},frame={level=\"24\",addr=\"$hex\",func=\"3cnuf\".*}\\\]" \
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.python/py-framefilter.py
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.python/py-framefilter.py
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.python/py-framefilter.py
01917d
@@ -30,8 +30,11 @@ class Reverse_Function (FrameDecorator):
01917d
         fname = str (self.fobj.function())
01917d
         if (fname == None or fname == ""):
01917d
             return None
01917d
+        if fname == 'end_func':
01917d
+            extra = self.fobj.inferior_frame().read_var('str').string()
01917d
         else:
01917d
-            fname = fname[::-1]
01917d
+            extra = ''
01917d
+        fname = fname[::-1] + extra
01917d
         return fname
01917d
 
01917d
 class Dummy (FrameDecorator):
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv-gdb.py.in
01917d
===================================================================
01917d
--- /dev/null
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv-gdb.py.in
01917d
@@ -0,0 +1,48 @@
01917d
+# Copyright (C) 2013 Free Software Foundation, Inc.
01917d
+
01917d
+# This program is free software; you can redistribute it and/or modify
01917d
+# it under the terms of the GNU General Public License as published by
01917d
+# the Free Software Foundation; either version 3 of the License, or
01917d
+# (at your option) any later version.
01917d
+#
01917d
+# This program is distributed in the hope that it will be useful,
01917d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+# GNU General Public License for more details.
01917d
+#
01917d
+# You should have received a copy of the GNU General Public License
01917d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
01917d
+
01917d
+# This file is part of the GDB testsuite.  It tests Python-based
01917d
+# frame-filters.
01917d
+import gdb
01917d
+import itertools
01917d
+from gdb.FrameDecorator import FrameDecorator
01917d
+
01917d
+
01917d
+class FrameObjFile ():
01917d
+
01917d
+    def __init__ (self):
01917d
+        self.name = "Filter1"
01917d
+        self.priority = 1
01917d
+        self.enabled = False
01917d
+        gdb.current_progspace().frame_filters ["Progspace" + self.name] = self
01917d
+        gdb.current_objfile().frame_filters ["ObjectFile" + self.name] = self
01917d
+
01917d
+    def filter (self, frame_iter):
01917d
+        return frame_iter
01917d
+
01917d
+class FrameObjFile2 ():
01917d
+
01917d
+    def __init__ (self):
01917d
+        self.name = "Filter2"
01917d
+        self.priority = 100
01917d
+        self.enabled = True
01917d
+        gdb.current_progspace().frame_filters ["Progspace" + self.name] = self
01917d
+        gdb.current_objfile().frame_filters ["ObjectFile" + self.name] = self
01917d
+
01917d
+    def filter (self, frame_iter):
01917d
+        return frame_iter
01917d
+
01917d
+FrameObjFile()
01917d
+FrameObjFile2()
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv.exp
01917d
===================================================================
01917d
--- /dev/null
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv.exp
01917d
@@ -0,0 +1,76 @@
01917d
+# Copyright (C) 2013-2015 Free Software Foundation, Inc.
01917d
+
01917d
+# This program is free software; you can redistribute it and/or modify
01917d
+# it under the terms of the GNU General Public License as published by
01917d
+# the Free Software Foundation; either version 3 of the License, or
01917d
+# (at your option) any later version.
01917d
+#
01917d
+# This program is distributed in the hope that it will be useful,
01917d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+# GNU General Public License for more details.
01917d
+#
01917d
+# You should have received a copy of the GNU General Public License
01917d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
01917d
+
01917d
+# This file is part of the GDB testsuite.  It tests Python-based
01917d
+# frame-filters.
01917d
+
01917d
+# This test is specifically crafted for RH BZ 1197665.
01917d
+
01917d
+load_lib gdb-python.exp
01917d
+
01917d
+standard_testfile py-framefilter.c
01917d
+
01917d
+# We cannot use prepare_for_testing as we have to set the safe-patch
01917d
+# to check objfile and progspace printers.
01917d
+if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
01917d
+    return -1
01917d
+}
01917d
+
01917d
+# Start with a fresh gdb.
01917d
+gdb_exit
01917d
+gdb_start
01917d
+
01917d
+# Skip all tests if Python scripting is not enabled.
01917d
+if { [skip_python_tests] } { continue }
01917d
+
01917d
+# Make the -gdb.py script available to gdb, it is automagically loaded by gdb.
01917d
+# Care is taken to put it in the same directory as the binary so that
01917d
+# gdb will find it.
01917d
+set remote_obj_python_file \
01917d
+    [remote_download \
01917d
+	 host ${srcdir}/${subdir}/${testfile}-gdb.py.in \
01917d
+	 ${subdir}/${testfile}-gdb.py]
01917d
+
01917d
+gdb_reinitialize_dir $srcdir/$subdir
01917d
+gdb_test_no_output "set auto-load safe-path ${remote_obj_python_file}" \
01917d
+    "set auto-load safe-path"
01917d
+gdb_load ${binfile}
01917d
+# Verify gdb loaded the script.
01917d
+gdb_test "info auto-load python-scripts" "Yes.*/${testfile}-gdb.py.*" \
01917d
+    "Test auto-load had loaded python scripts"
01917d
+
01917d
+if ![runto_main] then {
01917d
+    perror "couldn't run to breakpoint"
01917d
+    return
01917d
+}
01917d
+gdb_test_no_output "set python print-stack full" \
01917d
+    "Set python print-stack to full"
01917d
+
01917d
+# Load global frame-filters
01917d
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
01917d
+gdb_test_no_output "python execfile ('${remote_python_file}')" \
01917d
+    "Load python file"
01917d
+
01917d
+gdb_breakpoint [gdb_get_line_number "Backtrace end breakpoint"]
01917d
+gdb_breakpoint [gdb_get_line_number "Inner test breakpoint"]
01917d
+gdb_continue_to_breakpoint "Inner test breakpoint"
01917d
+
01917d
+# Test multiple local blocks.
01917d
+gdb_test "bt full no-filters" \
01917d
+    ".*#0.*end_func.*h = 9.*f = 42.*g = 19.*bar = $hex \"Inside block x2\".*d = 15.*e = 14.*foo = $hex \"Inside block\".*str = $hex \"The End\".*st2 = $hex \"Is Near\".*b = 12.*c = 5.*" \
01917d
+    "bt full no-filters"
01917d
+gdb_test "bt full" \
01917d
+    ".*#0.*cnuf_dne.*h = 9.*f = 42.*g = 19.*bar = $hex \"Inside block x2\".*d = 15.*e = 14.*foo = $hex \"Inside block\".*str = $hex \"The End\".*st2 = $hex \"Is Near\".*b = 12.*c = 5.*" \
01917d
+    "bt full with filters"
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv.py
01917d
===================================================================
01917d
--- /dev/null
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.python/rhbz1197665-apply-frame-filter-segv.py
01917d
@@ -0,0 +1,56 @@
01917d
+# Copyright (C) 2013-2015 Free Software Foundation, Inc.
01917d
+
01917d
+# This program is free software; you can redistribute it and/or modify
01917d
+# it under the terms of the GNU General Public License as published by
01917d
+# the Free Software Foundation; either version 3 of the License, or
01917d
+# (at your option) any later version.
01917d
+#
01917d
+# This program is distributed in the hope that it will be useful,
01917d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+# GNU General Public License for more details.
01917d
+#
01917d
+# You should have received a copy of the GNU General Public License
01917d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
01917d
+
01917d
+# This file is part of the GDB testsuite.  It tests Python-based
01917d
+# frame-filters.
01917d
+
01917d
+# This test is specifically crafted for RH BZ 1197665.
01917d
+
01917d
+import gdb
01917d
+import itertools
01917d
+from gdb.FrameDecorator import FrameDecorator
01917d
+import copy
01917d
+
01917d
+class Reverse_Function (FrameDecorator):
01917d
+
01917d
+    def __init__(self, fobj):
01917d
+        super(Reverse_Function, self).__init__(fobj)
01917d
+        self.fobj = fobj
01917d
+
01917d
+    def function (self):
01917d
+        # This function call should not fail.
01917d
+        gdb.target_charset ()
01917d
+
01917d
+        fname = str (self.fobj.function())
01917d
+        if (fname == None or fname == ""):
01917d
+            return None
01917d
+        else:
01917d
+            fname = fname[::-1]
01917d
+        return fname
01917d
+
01917d
+class FrameFilter ():
01917d
+
01917d
+    def __init__ (self):
01917d
+        self.name = "Reverse"
01917d
+        self.priority = 100
01917d
+        self.enabled = True
01917d
+        gdb.frame_filters [self.name] = self
01917d
+
01917d
+    def filter (self, frame_iter):
01917d
+        frame_iter = itertools.imap (Reverse_Function,
01917d
+                                     frame_iter)
01917d
+        return frame_iter
01917d
+
01917d
+FrameFilter()