Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed Python/Cython/C++ debugging #2900

Closed
Silmathoron opened this issue Dec 6, 2018 · 7 comments
Closed

Mixed Python/Cython/C++ debugging #2900

Silmathoron opened this issue Dec 6, 2018 · 7 comments

Comments

@Silmathoron
Copy link

Type: Debugger

  • OS and Version: Linux Mint 19
  • VS Code Version: 1.29.1
  • C/C++ Extension Version: 0.20.1
  • Other extensions you installed (and if the issue persists after disabling them): Python, Language Cython, C/C++ Advanced Lint, CMake

I am trying to debug a library containing a mixed Python/Cython API and a C++ backend linked to Python via a Cython wrap of the C++ API.
So I want to be able to set breakpoints both in Python (that's OK) and in the C++ code (not working), as well as (if possible) to check the variables values in the C++ code.

According to what I found on SO (here and here), I need to attach the GDB session to the PyDB.
I noted that another user managed to do it, but I cannot get it to work on my side.

The attachment I configured in JSON looks like this:

{ 
        "name": "(gdb) Attach",
        "type": "cppdbg",
        "request": "attach",
        "program": "${workspaceFolder}/env/bin/python",
        "processId": "${command:pickProcess}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "additionalSOLibSearchPath": "${workspaceFolder}/env/lib;${workspaceFolder}/env/lib/python3.6/site-packages/myPythonLib/"
    }

The ${workspaceFolder}/env/lib contains the shared library compiled from the C++ while ${workspaceFolder}/env/lib/python3.6/site-packages/myPythonLib contains the shared library compiled from the Cython files.

To (try and) debug, I performed the following steps:

  1. Run the Python debugger on the python script, where I have a breakpoint set after the import of the library, leading the debug session to pause.
  2. Run the GDB session and attach to the PID of a process looking like python /path/to/ptvsd_launcher.py some other stuff --port 35485 myscript.py.
  3. (optional) Pause the GDB session.
  4. Resume the Python debugger.

The GDB session seems to attach fine but breakpoints in the C++ have no effect and only Python breakpoints stop the run.

After it is attached, the GDB session always appears running and shows one python thread per core on my machine (all running).

I can also pause the GDB session before resuming the Python session (3). This makes only the 1st python thread switch to "paused on pause", which looks nice, but if I try to then resume the Python side, I think it crashes with the last stack items being

futex_wait_cancelable(unsigned int expected, unsigned int * futex_word) (/build/glibc-OTsEL5/glibc-2.27/sysdeps/unix/sysv/linux/futex-internal.h:88)
__pthread_cond_wait_common(pthread_mutex_t * mutex, pthread_cond_t * cond) (/build/glibc-OTsEL5/glibc-2.27/nptl/pthread_cond_wait.c:502)
__pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex) (/build/glibc-OTsEL5/glibc-2.27/nptl/pthread_cond_wait.c:655)
Tcl_WaitForEvent (Unknown Source:0)
Tcl_DoOneEvent (Unknown Source:0)
[Unknown/Just-In-Time compiled code] (Unknown Source:0)
_PyCFunction_FastCallDict(PyObject * kwargs, PyObject ** args, PyObject * func_obj) (methodobject.c:234)

As a final note, the debugger keeps complaining that it cannot find files like futex_interal.h, or raise.c or select.c, even though I have libc6 installed on my system (both lib and dev).

Hope this is clear enough (and that there is something obvious I'm missing)

@Silmathoron
Copy link
Author

Turns out I thought I was compiling with debug symbols but it wasn't the case... sorry for the noise!
Closing this.

@czlsws163
Copy link

@Silmathoron would you please share the steps you set up VS for Mixed Python/Cython/C++ debugging?

@Silmathoron
Copy link
Author

@czlsws163 the steps are detailed in the first post... items 1 to 4 plus links to StackOverflow for additional explanations.
Where exactly are you stuck?
The only this I was missing is that I forgot to compile with debug symbols activated, the precise method for doing this depends on how you're compiling you code

@zhiqiu
Copy link

zhiqiu commented Sep 25, 2019

@Silmathoron I try to debug python code which use c++ extension, like tensorflow. The problem is, breakpoint at cpp file does not work, would you please share more on that?

@Silmathoron
Copy link
Author

@zhiqiu where are you trying to set the breakpoint? In order to work, the breakpoint must be in an executable or library which has been compiled with debug symbols so if it's inside one of the cpp files of tensorflow, make sure that you compiled it yourself with debug symbols, I'm afraid I cannot tell you much more than that...

@zhiqiu
Copy link

zhiqiu commented Sep 30, 2019

@Silmathoron Thanks for your reply. I tried a tiny demo to mix debug python and c++, the python script imports c++ extension written with pybind11. It worked and the program execution stoped at C++ breakpoints well. I don't know why the real extension (call it X) I want to debug not work. I guess the difference between X and demo includes, (1) the file size, X is 800+M, while demo is 1M; (2) the tiny demo outputs .DSYM file which contains symbols and .so, while X outputs .so (the symbols are compiled in .so?).
Not sure whether the two difference account for the failure of debugging X.

One thing more, I tried X in clion and it worded fine and faster...

@miteshyh
Copy link

miteshyh commented Jan 6, 2020

Thanks @Silmathoron,

In case you are using containers as I did just add --cap-add sys_ptrace capbility while you give run command. Details in below post:

https://stackoverflow.com/questions/45171339/gdb-cannot-attach-to-process/45171694#45171694

@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants