[binutils-gdb] gdb: add experimental option --enable-py-limited-api
Matthieu Longo
mlongo@sourceware.org
Wed Oct 15 15:24:49 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=abe6b29caf476f9760ce4b7bedf982e4ca762649
commit abe6b29caf476f9760ce4b7bedf982e4ca762649
Author: Matthieu Longo <matthieu.longo@arm.com>
Date: Fri Jul 11 17:39:41 2025 +0100
gdb: add experimental option --enable-py-limited-api
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>
Diff:
---
gdb/config.in | 3 +++
gdb/configure | 34 ++++++++++++++++++++++++++++++++--
gdb/configure.ac | 16 ++++++++++++++++
3 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/gdb/config.in b/gdb/config.in
index efc3100cb9e..2a2dcb809dd 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -710,6 +710,9 @@
/* 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
diff --git a/gdb/configure b/gdb/configure
index b7a2079d206..04bb60f9f1f 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -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. #
# -------------------- #
diff --git a/gdb/configure.ac b/gdb/configure.ac
index a88b6ebffe5..5e5a3a01d06 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -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. #
# -------------------- #
More information about the Gdb-cvs
mailing list