5

Using gdb from the command line I'm able to break when the shared library is loaded. How can I get the same behavior in VS Code knowing that I have the source code of the shared library?

karel
  • 5,489
  • 46
  • 45
  • 50
Salah
  • 63
  • 1
  • 5
  • Is this actually related to Python or just C? If you mean C extension modules for Python then there is no way to cross the Python/C barrier in the debugger (you need Visual Studio for that). – Brett Cannon Mar 11 '19 at 22:42
  • I can't use Visual Studio because I am working in a Linux environment. The work flow is as follows. I have my python script which uses a certain python library. I am modifying the code of a shared library written in C++. This share library is loaded by the python library that I call from my python script. I am looking for a way to break when my shared library is loaded to be able to debug graphically in VS Code. – Salah Mar 12 '19 at 09:07

3 Answers3

6

For me it works somehow.

Here's my setup:

  • Ubuntu 18.04, debugging a C++ shared library I load from Python3 (more specifically - via Cython, but IIRC it worked equally well when loading a .so through ctypes, also I remember it working when debugging a pure C lib in a similar setup)
  • in VSCode I have a project that compiles into that .so
  • there I put a bunch of breakpoints
  • I created a launch configuration (text below)
  • also, I've compiled the .so with debugging information

here's my launch.json (for the most part, it's boilerplate, I only filled in the "program" and "args" parts and set up PYTHONPATH environment var).

note: it seems to be important to have "stopAtEntry:false" (which it is by default), otherwise VSCode tries to locate an entry .c file or something.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch 1123",
        "type": "cppdbg",
        "request": "launch",
        "program": "/home/sergey/anaconda3/bin/python",
        "args": [
            "/storage/projects/cython-vst-loader/cython_vst_loader/test_load_plugin.py"
        ],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [
            {
                "name": "PYTHONPATH",
                "value": "/storage/projects/cython-vst-loader"
            }
        ],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]

anyway, in this setup I see my VSCode showing the execution stopping on my breakpoints

enter image description here

Sergey Grechin
  • 876
  • 11
  • 14
  • Works, but only if I remove the setupCommands block. ` "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }` – MegaNightdude Aug 07 '20 at 04:15
  • An also, I don't need the environment block at all. – MegaNightdude Aug 07 '20 at 04:28
  • I have a question. What if I want to use cuda-gdb, instead of gdb for debugging not only c++, but also cuda c++? Is there a similar way to do this? – Ziqi Fan Feb 05 '22 at 21:51
0

Unfortunately there isn't a way to flow from Python code into C code for debugging purposes (only Visual Studio has that ability to my knowledge).

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
0

Thank you @user1312695, your method works for me!

I was able to step into pybullet.c now! So let me take this as an example. https://github.com/bulletphysics/bullet3

I want to install the debuggable version pybullet package into a conda environment and use VScode to start my debugging, here is what I did:

(0) create a new conda environment called debug_pybullet.

(1) modify cmake/FindPythonLibs.cmake

FindPythonLibs.cmake can't recognize conda environments, so after it found the wrong _PYTHON_EXECUTABLE, I need to manually set the path at around line 143:

set(_PYTHON_EXECUTABLE /home/MyName/anaconda3/envs/debug_pybullet/bin/python)

(2) modify CMakeLists.txt

I need to manually add the definitions which are included in file build_cmake_pybullet_double.sh, except the definition of CMAKE_BUILD_TYPE=Release (I prefer to let VSCode control this definition).

also I manually set PYTHON_SITE_PACKAGES at around line 93:

set(PYTHON_SITE_PACKAGES /home/MyName/anaconda3/envs/debug_pybullet/lib/python3.6/site-packages)

(3) Create launch.json in VScode.

Here is my version of launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/MyName/anaconda3/envs/debug_pybullet/bin/python",
            "args": [
                "/home/MyName/<path_to_python_file>/main.py"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
        }
    ]
}

Since I manually set the path, I don't need the environments and setupCommands here.

(4) In VScode, build all.

(5) Install the debuggable package:

$ source activate pybullet_debug
(pybullet_debug)$ pip install -e .

(6) Set the breakpoints in pybullet.c.

(7) Press F5, Run the python, and here we are!

A screenshot attached.

Star
  • 23
  • 5