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>
This commit is contained in:
Matthieu Longo
2025-07-11 17:39:41 +01:00
parent 5643188363
commit abe6b29caf
3 changed files with 51 additions and 2 deletions

View File

@@ -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

34
gdb/configure vendored
View File

@@ -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. #
# -------------------- #

View 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. #
# -------------------- #