Search code examples
pythonc++lldbpybind11

How can I debug C++ pybind11 module with lldb?


I tried to follow the instructions here, but I'm getting the unable to resolve breakpoint to any actual location warning. Here is what I do exactly. I first compile the code with the -g flag:

c++ -g -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` fractal.cpp -o fractal`python3-config --extension-suffix`

Then, I launch python from the command line, which is set to run the system python 3.8.5. I check the pid with

ps aux | grep -i python

In another terminal window, I launch lldb and type

attach --pid 77352

The rest of it goes this way:

(lldb) continue
Process 77352 resuming
(lldb) breakpoint set -f fractal.cpp -l 66
Breakpoint 1: no locations (pending).
WARNING:  Unable to resolve breakpoint to any actual locations.

What am I missing? Since pybind11 doesn't print errors in the C++ code until the end of the calling python script, I can't just print stuff out to the screen to see where the code breaks down. I would like to be able to go through the code line by line.

I'm on macOS 10.15.5, if it makes any difference. I also double checked that I'm selecting a valid line in fractal.cpp.


Solution

  • All you need to do now is to import your module now.

    While it's not imported, neither python nor lldb can "know" about existance of your sorce file (fractal.cpp). After you import it, the lldb will immediately react with:

    1 location added to breakpoint 1
    

    And then once you call the function with breakpont in it:

    Process 8189 stopped
    * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
        frame #0: 0x00000001011af3fa mypylib.cpython-38-darwin.so`add(i=1, j=2) at main.cpp:6:12
       3    namespace py = pybind11;
       4
       5    int add(int i, int j) {
    -> 6        return i + j;
       7    }
       8
       9    PYBIND11_MODULE(mypylib, m) {
    Target 0: (Python) stopped.