gdb/python: fix FrameDecorator regression on Python 2
authorAndrew Burgess <andrew.burgess@embecosm.com>
Mon, 15 Mar 2021 14:20:13 +0000 (14:20 +0000)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Tue, 16 Mar 2021 09:31:56 +0000 (09:31 +0000)
This commit:

  commit d1cab9876d72d867b2de82688f5f5a2a4b655edb
  Date:   Tue Sep 15 11:08:56 2020 -0600

      Don't use gdb_py_long_from_ulongest

Introduced a regression when GDB is compiled with Python 2.  The frame
filter API expects the gdb.FrameDecorator.function () method to return
either a string (the name of a function) or an address, which GDB then
uses to lookup a msymbol.

If the address returned from gdb.FrameDecorator.function () comes from
gdb.Frame.pc () then before the above commit we would always expect to
see a PyLong object.

After the above commit we might (on Python 2) get a PyInt object.

The GDB code does not expect to see a PyInt, and only checks for a
PyLong, we then see an error message like:

  RuntimeError: FrameDecorator.function: expecting a String, integer or None.

This commit just adds an additional call to PyInt_Check which handle
the missing case.

I had already written a test case to cover this issue before spotting
that the gdb.python/py-framefilter.exp test also triggers this
failure.  As the new test case is slightly different I have kept it
in.

The new test forces the behaviour of gdb.FrameDecorator.function
returning an address.  The reason the existing test case hits this is
due to the behaviour of the builtin gdb.FrameDecorator base class.  If
the base class behaviour ever changed then the return an address case
would only be tested by the new test case.

gdb/ChangeLog:

* python/py-framefilter.c (py_print_frame): Use PyInt_Check as
well as PyLong_Check for Python 2.

gdb/testsuite/ChangeLog:

* gdb.python/py-framefilter-addr.c: New file.
* gdb.python/py-framefilter-addr.exp: New file.
* gdb.python/py-framefilter-addr.py: New file.

gdb/ChangeLog
gdb/python/py-framefilter.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-framefilter-addr.c [new file with mode: 0644]
gdb/testsuite/gdb.python/py-framefilter-addr.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-framefilter-addr.py [new file with mode: 0644]

index 0ac6b29443e37015621812722e6b0e53a072cb50..2e4628b87c592f728b8057ff04fbfd08d467c2cb 100644 (file)
@@ -1,3 +1,8 @@
+2021-03-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * python/py-framefilter.c (py_print_frame): Use PyInt_Check as
+       well as PyLong_Check for Python 2.
+
 2021-03-15  Tom Tromey  <tromey@adacore.com>
 
        PR build/27579:
index 6dd741ab7040fc16ea74a59f6f81f1bf171fb677..18071982e201d2b357d5277ff38a55999865cde3 100644 (file)
@@ -924,7 +924,11 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
 
              function = function_to_free.get ();
            }
-         else if (PyLong_Check (py_func.get ()))
+         else if (PyLong_Check (py_func.get ())
+#if PY_MAJOR_VERSION == 2
+                  || PyInt_Check (py_func.get ())
+#endif
+                  )
            {
              CORE_ADDR addr;
              struct bound_minimal_symbol msymbol;
index 374ea39417276f07c68ad1e3a0e4442456b424a0..1e23b91d9a274bb8c20baf9277eeb8765154babe 100644 (file)
@@ -1,3 +1,9 @@
+2021-03-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * gdb.python/py-framefilter-addr.c: New file.
+       * gdb.python/py-framefilter-addr.exp: New file.
+       * gdb.python/py-framefilter-addr.py: New file.
+
 2021-03-16  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * gdb.threads/execl.exp: Remove duplicate 'info threads' test.
diff --git a/gdb/testsuite/gdb.python/py-framefilter-addr.c b/gdb/testsuite/gdb.python/py-framefilter-addr.c
new file mode 100644 (file)
index 0000000..446ec3e
--- /dev/null
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2021 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+func3 ()
+{
+  return 0;    /* Break here.  */
+}
+
+int
+func2 ()
+{
+  return func3 ();
+}
+
+int
+func1 ()
+{
+  return func2 ();
+}
+
+int
+main ()
+{
+  return func1 ();
+}
diff --git a/gdb/testsuite/gdb.python/py-framefilter-addr.exp b/gdb/testsuite/gdb.python/py-framefilter-addr.exp
new file mode 100644 (file)
index 0000000..108509c
--- /dev/null
@@ -0,0 +1,61 @@
+# Copyright (C) 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite.  It tests Python-based
+# frame-filters when the 'function ()' method on FrameDecorator
+# returns the address for the function being decorated.
+
+load_lib gdb-python.exp
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
+    return -1
+}
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+if ![runto_main] {
+   return -1
+}
+
+# Run to our test breakpoint.
+gdb_breakpoint [gdb_get_line_number "Break here"]
+gdb_continue_to_breakpoint "run to test breakpoint"
+
+gdb_test "bt" \
+    [multi_line \
+        "#0  func3 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:21" \
+        "#1  $hex in func2 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:27" \
+        "#2  $hex in func1 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:33" \
+        "#3  $hex in main \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:39" ] \
+    "backtrace without frame filters"
+
+# Make the frame filters Python script available.
+set remote_python_file \
+    [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py \
+        ${testfile}.py]
+
+# And load it into GDB.
+gdb_test_no_output "source ${remote_python_file}" "load python file"
+
+gdb_test "bt" \
+    [multi_line \
+        "#0  func3 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:21" \
+        "#1  $hex in func2 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:27" \
+        "#2  $hex in func1 \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:33" \
+        "#3  $hex in main \\(\\) at \[^\r\n\]+/py-framefilter-addr.c:39" ] \
+    "backtrace with frame filters"
diff --git a/gdb/testsuite/gdb.python/py-framefilter-addr.py b/gdb/testsuite/gdb.python/py-framefilter-addr.py
new file mode 100644 (file)
index 0000000..a27879a
--- /dev/null
@@ -0,0 +1,52 @@
+# Copyright (C) 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+import itertools
+from gdb.FrameDecorator import FrameDecorator
+import copy
+
+# A FrameDecorator that just returns gdb.Frame.pc () from 'function'.
+# We want to ensure that GDB correctly handles this case.
+class Function_Returns_Address (FrameDecorator):
+
+    def __init__(self, fobj):
+        super (Function_Returns_Address, self).__init__ (fobj)
+        self._fobj = fobj
+
+    def function (self):
+        frame = self.inferior_frame ()
+        return frame.pc ()
+
+class Frame_Filter ():
+
+    def __init__ (self):
+        self.name = "function_returns_address"
+        self.priority = 100
+        self.enabled = True
+        gdb.frame_filters [self.name] = self
+
+    def filter (self, frame_iter):
+        # Python 3.x moved the itertools.imap functionality to map(),
+        # so check if it is available.
+        if hasattr(itertools, "imap"):
+            frame_iter = itertools.imap (Function_Returns_Address,
+                                         frame_iter)
+        else:
+            frame_iter = map(Function_Returns_Address, frame_iter)
+
+        return frame_iter
+
+Frame_Filter()
This page took 0.039734 seconds and 4 git commands to generate.