]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add experimental option --enable-py-limited-api
authorMatthieu Longo <matthieu.longo@arm.com>
Fri, 11 Jul 2025 16:39:41 +0000 (17:39 +0100)
committerMatthieu Longo <matthieu.longo@arm.com>
Wed, 15 Oct 2025 15:23:21 +0000 (16:23 +0100)
Today, GDB links against the Python library using the unstable API. This
approach causes portability issues of the generated GDB artifact. Indeed
the built artifact is tighly coupled with the specific version of Python
that it was compiled with. Using a slighly minor version of Python can
cause unpredictable crashes at runtime due to ABI instability between
the Python versions, even minor ones.

The solution would consist in restricting the usage of Python functions
to the limited C API controlled via Py_LIMITED_API that must be defined
before the inclusion of <Python.h>.

This patch does not aim at porting the whole GDB codebase to the Python
limited C API, but rather enabling a development mode where developers
can experiment with the Python limited C API, and fix issues.
This development mode is accessible with the configure option
--enable-py-limited-api which is set by default to 'no'.

Note: the version of the Python limited API is currently set to 3.11
because of PyBuffer_FillInfo and PyBuffer_Release. This choice is not
frozen, and could be reviewed later on depending on newly discovered
issues during the migration.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
Approved-By: Tom Tromey <tom@tromey.com>
gdb/config.in
gdb/configure
gdb/configure.ac

index efc3100cb9e7c42ae9a6622d3eb356a5cca4d5d8..2a2dcb809dd32112b0209f09ca5dccc0c017ce70 100644 (file)
 /* Define if the python directory should be relocated when GDB is moved. */
 #undef PYTHON_PATH_RELOCATABLE
 
+/* Define if GDB should be built against the Python limited C API. */
+#undef Py_LIMITED_API
+
 /* Relocated directory for source files. */
 #undef RELOC_SRCDIR
 
index b7a2079d2062c11a54483d0f6f763144bc7a8730..04bb60f9f1f2e9b982472da5e57a7cee5c91a848 100755 (executable)
@@ -957,6 +957,7 @@ with_libexpat_prefix
 with_libexpat_type
 with_python
 with_python_libdir
+enable_py_limited_api
 with_guile
 enable_gdb_compile
 enable_source_highlight
@@ -1662,6 +1663,8 @@ Optional Features:
   --enable-gdbtk          enable gdbtk graphical user interface (GUI)
   --enable-profiling      enable profiling of GDB
   --enable-codesign=CERT  sign gdb with 'codesign -s CERT'
+  --enable-py-limited-api enable build against the Python limited C API,
+                          default 'no'
   --enable-gdb-compile    enable support for the compile subsystem, default
                           'yes'
   --enable-source-highlight
@@ -11888,7 +11891,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11891 "configure"
+#line 11894 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11994,7 +11997,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11997 "configure"
+#line 12000 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -28726,6 +28729,33 @@ else
 fi
 
 
+# Check whether to build GDB against Python limited C API.
+# Check whether --enable-py-limited-api was given.
+if test "${enable_py_limited_api+set}" = set; then :
+  enableval=$enable_py_limited_api;
+          case $enableval in
+            yes | no)
+              ;;
+            *)
+              as_fn_error $? "bad value $enableval for --enable-py-limited-api" "$LINENO" 5
+              ;;
+          esac
+
+else
+  enable_py_limited_api=no
+fi
+
+
+if test "$enable_py_limited_api" == yes; then
+  # The minimal Python limited API version is currently set to 3.11 for the
+  # support of PyBuffer_FillInfo and PyBuffer_Release.
+  # The choice of the minimal version for the Python limited API won't be frozen
+  # until the end of the migration.
+
+$as_echo "#define Py_LIMITED_API 0x030b0000" >>confdefs.h
+
+fi
+
 # -------------------- #
 # Check for libguile.  #
 # -------------------- #
index a88b6ebffe58aa56ad7962d101bdc5aeb1e5ce22..5e5a3a01d06117a93e0c4773d0138bccaff9295f 100644 (file)
@@ -1070,6 +1070,22 @@ AC_SUBST(PYTHON_CPPFLAGS)
 AC_SUBST(PYTHON_LIBS)
 AM_CONDITIONAL(HAVE_PYTHON, test "${have_libpython}" != no)
 
+# Check whether to build GDB against Python limited C API.
+AC_ARG_ENABLE([py-limited-api],
+             [AS_HELP_STRING([--enable-py-limited-api],
+                             [enable build against the Python limited C API, default 'no'])],
+             [GDB_CHECK_YES_NO_VAL([$enableval], [--enable-py-limited-api])],
+             [enable_py_limited_api=no])
+
+if test "$enable_py_limited_api" == yes; then
+  # The minimal Python limited API version is currently set to 3.11 for the
+  # support of PyBuffer_FillInfo and PyBuffer_Release.
+  # The choice of the minimal version for the Python limited API won't be frozen
+  # until the end of the migration.
+  AC_DEFINE(Py_LIMITED_API, 0x030b0000,
+           [Define if GDB should be built against the Python limited C API.])
+fi
+
 # -------------------- #
 # Check for libguile.  #
 # -------------------- #