pythongcclinkershared-librariesltrace

ltrace doesn't work for Python compiled with --enable-shared


When I build Python 3.12.4 with --enable-shared option:

Enable building a shared Python library: libpython (default is no).

the only output I get from ltrace is:

Py_BytesMain(2, 0x7ffd1e465518, 0x7ffd1e465530, 0x649272aa9da0)                                                                                                                = 0
+++ exited (status 0) +++

If the option is not used, ltrace works as expected.

Why is it happening? How can I troubleshoot it?

My config:


Solution

  • From man ltrace:

    -e filter
      A qualifying expression which modifies which library calls to trace.
      ...
      If no -e is given, @MAIN is assumed as a default.
    

    That is: if you don't pass -e, then only calls from the main binary are traced.

    When you build Python with --enable-shared, the (tiny) python binary only calls the Py_BytesMain (which is in libpython.so), and the rest of the execution continues in that shared library, which is filtered out due to the assumed default filter.

    To fix this, use -e '@*'.


    Here is a simple example demonstrating the problem:

    // foo.c
    #include <stdio.h>
    
    int foo()
    {
      puts("Hello");
      return 42;
    }
    
    // main.c
    extern int foo();
    int main() { return foo(); }
    
    gcc -fPIC -shared -o foo.so foo.c && gcc main.c ./foo.so
    
    ltrace ./a.out
    foo(1, 0x7ffef092d2f8, 0x7ffef092d308, 0x559054ea9dc8Hello
    )                                                                                                                                                                            = 42
    +++ exited (status 42) +++
    
    ltrace -e '@*' ./a.out
    libc.so.6->(0x7f730193b2c0, 0, 0x7ffd369fa3b8, 0x5625cd742dc8)                                                                                                                                                                    = 0
    a.out->foo(1, 0x7ffd369fa3a8, 0x7ffd369fa3b8, 0x5625cd742dc8 <unfinished ...>
    foo.so->puts("Hello" <unfinished ...>
    libc.so.6->(0x7f7301903000, 0x7ffd369fa3a8, 0x7ffd369fa3b8, 0x5625cd742dc8)                                                                                                                                                       = 5
    libc.so.6->(31, 0x7ffd369fa090, 0x7f7301799930, 0x7f73018dab20)                                                                                                                                                                   = 465
    libc.so.6->(24, 0x7ffd369fa090, 0x7f7301799950, 0x7f73019387e0)                                                                                                                                                                   = 360
    libc.so.6->(1, 0x7ffd369fa090, 0x7f73017998f0, 0x7f73019387e0)                                                                                                                                                                    = 15
    libc.so.6->(14, 0x7ffd369fa090, 0x7f7301799960, 0x7f73019387e0)                                                                                                                                                                   = 210
    libc.so.6->(12, 0x7ffd369fa090, 0x7f7301799910, 0x7f73019387e0)                                                                                                                                                                   = 180
    libc.so.6->(36, 0x7ffd369fa090, 0x7f7301799980, 0x7f73019387e0)                                                                                                                                                                   = 540
    libc.so.6->(21, 0x7ffd369fa090, 0x7f7301799990, 0x7f73019387e0)                                                                                                                                                                   = 315
    libc.so.6->(2, 0x7ffd369fa090, 0x7f73017999a0, 0x7f73019387e0)                                                                                                                                                                    = 30
    libc.so.6->(25, 0x7ffd369fa090, 0x7f73017999e0, 0x7f73019387e0)                                                                                                                                                                   = 375
    libc.so.6->(5, 0x7ffd369fa090, 0x7f7301799a00, 0x7f73019387e0)                                                                                                                                                                    = 75
    libc.so.6->(0, 0x7ffd369fa090, 0x7f7301799a10, 0x7f73019387e0)                                                                                                                                                                    = 0
    libc.so.6->(22, 0x7ffd369fa090, 0x7f7301799c40, 0x7f73019387e0)                                                                                                                                                                   = 330
    Hello
    <... puts resumed> )                                                                                                                                                                                                              = 6
    <... foo resumed> )                                                                                                                                                                                                               = 42
    +++ exited (status 42) +++