Search code examples
pythonlinuxtracebpfebpf

Is it feasible to use eBPF to trace code at the Python stack level?


dtrace on non-Linux platforms has long been advertised to be able to dynamically instrument node.js code to do dynamic tracing at the node level, for example to allow debugging of node programs at the level of JavaScript stack frames and variables (together with lower level tracing) from a core dump.

Has eBPF on Linux now reached that level of sophistication? I am interested in Python in particular, but would consider this question answered if it was answered for any similar dynamic language.

If not, what remains to be done to support that?


Solution

  • Yes, you can use BPF and USDT probes to trace Python scripts. You'll have to build your Python runtime with USDT probes though.

    bcc includes a few tracing scripts for Python. For example, you can use pythoncalls.sh to print the top 2 methods called:

    $ ./pythoncalls.sh -T 2 -p 26914
    Tracing calls in process 26914 (language: python)... Ctrl-C to quit.
    
    METHOD                                              # CALLS
    <stdin>.<module>                                          1
    <stdin>.fibo                                       14190928
    ^C
    

    Note that several other virtual machines support USDT probes, e.g., those of Java, Perl, PHP, Ruby, and Tcl.