How to configure VS Code to be able to step into a shared library (.so) that is loaded when debugging a Python script?

佐手、 提交于 2021-01-27 20:23:17

问题


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?


回答1:


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




回答2:


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).




回答3:


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.



来源:https://stackoverflow.com/questions/55098128/how-to-configure-vs-code-to-be-able-to-step-into-a-shared-library-so-that-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!