Bug 26832 - Fix detection for embedded libpython.a with python3
Summary: Fix detection for embedded libpython.a with python3
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: unknown
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-02 15:15 UTC by Romain Geissler
Modified: 2020-12-05 13:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Romain Geissler 2020-11-02 15:15:47 UTC
Hi,

I build gdb on Linux with a statically embedded libpython.a rather than the recommanded .so way. It seems like the resulting gdb fails to load any module using dynamic link, like for example "import math".

It's clear that the "problem" is the fact of using -rdynamic vs --dynamic-list. Actually this "problem" was fixed a long time ago, and we can see in configure.ac something like:

     AC_RUN_IFELSE(
       [AC_LANG_PROGRAM(
         [#include "Python.h"],
         [int err;
          Py_Initialize ();
          err = PyRun_SimpleString ("import itertools\n");
          Py_Finalize ();
          return err == 0 ? 0 : 1;])],
       [dynamic_list=true], [], [true])

to check whether python can import something without "-rdynamic". However with python 3 (at least in my case it was python 3.9, but I could also check with another python 3.4 that it was also the case) itertools seems to be a builtin, so doesn't actually load any.so.

Do you think this "import itertools" shall be replaced by another module which we know is a real dynamic python module on both python 2 and python 3 ? I propose to replace it by "import ctypes" instead which seems to be a modules in both major python releases.

Cheers,
Romain
Comment 1 Romain Geissler 2020-11-02 23:43:05 UTC
FYI: I tested the following patch, which worked for me (I tested only a statically linked libpython, using python 3.9 and gdb 10 on Linux):

--- gdb/configure
+++ gdb/configure
@@ -16159,7 +16159,7 @@
 {
 int err;  
           Py_Initialize ();
-          err = PyRun_SimpleString ("import itertools\n");
+          err = PyRun_SimpleString ("import ctypes\n");
           Py_Finalize ();
           return err == 0 ? 0 : 1;
   ;
--- gdb/configure.ac
+++ gdb/configure.ac
@@ -1599,7 +1599,7 @@
          [#include "Python.h"],
          [int err;
           Py_Initialize ();
-          err = PyRun_SimpleString ("import itertools\n");
+          err = PyRun_SimpleString ("import ctypes\n");
           Py_Finalize ();
           return err == 0 ? 0 : 1;])],
        [dynamic_list=true], [], [true])


Cheers,
Romain
Comment 2 Simon Marchi 2020-11-03 01:12:14 UTC
Thanks.  Note that we don't do patch review on Bugzilla, so if you'd like your patch to be considered for merging, please send it to the gdb-patches mailing list following the guidelines here:

https://sourceware.org/gdb/wiki/ContributionChecklist
Comment 3 Sourceware Commits 2020-11-06 18:02:04 UTC
The master branch has been updated by Andrew Burgess <aburgess@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3fed4c0b7adeae7b2bfd9aa59818bb8f3f381031

commit 3fed4c0b7adeae7b2bfd9aa59818bb8f3f381031
Author: Romain Geissler <romain.geissler@amadeus.com>
Date:   Fri Nov 6 17:47:21 2020 +0000

    gdb: better static python detection in configure machinery
    
    In python 3, itertools is a builtin module, so whether or not the
    python you link against is a shared or a static one, importing it
    works.
    
    Change the import test to use ctypes which is a dynamic module in both
    python 2 and 3.
    
    gdb/ChangeLog:
    
            PR python/26832
            * configure: Regenerate.
            * configure.ac: Check for python modules ctypes instead of
            itertools.
Comment 4 Sourceware Commits 2020-11-06 18:03:11 UTC
The gdb-10-branch branch has been updated by Andrew Burgess <aburgess@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=60714c0fc0cc7cc873440b4b9362c11f28daf585

commit 60714c0fc0cc7cc873440b4b9362c11f28daf585
Author: Romain Geissler <romain.geissler@amadeus.com>
Date:   Fri Nov 6 17:47:21 2020 +0000

    gdb: better static python detection in configure machinery
    
    In python 3, itertools is a builtin module, so whether or not the
    python you link against is a shared or a static one, importing it
    works.
    
    Change the import test to use ctypes which is a dynamic module in both
    python 2 and 3.
    
    gdb/ChangeLog:
    
            PR python/26832
            * configure: Regenerate.
            * configure.ac: Check for python modules ctypes instead of
            itertools.
Comment 5 Romain Geissler 2020-12-05 13:00:02 UTC
Fixed in the master branch + cherry-picked in the gdb 10 branch.