-3

Question:

I am trying to debug a Python script that calls a C++ function through a .pyd module built with pybind11 (compiled using MSVC). I want to set a breakpoint in Python, and when I press Step Into (F11,step into) in VSCode, I want it to step into the C++ implementation automatically.

Code Example:

Python (debug.py)

import sys
import os


# Add debug directory to Python path
debug_path = os.path.join(os.path.dirname(__file__), 'build', 'Debug')

sys.path.insert(0, debug_path)

import myadder


result = myadder.add(5, 3)  #  BREAKPOINT here


for i in range(3):
    result = myadder.add(i, i + 1)
    print(f"add({i}, {i+1}) = {result}")

C++ (add.cpp)

// add.cpp
#include <cstdio>

int add(int a, int b) {
    printf(" C++ add() called with a=%d, b=%d\n", a, b);
    int result = a + b;  // Set C++ breakpoint here
    printf(" C++ add() returning: %d\n", result);
    return result;
}

C++ (bindings.cpp)

// bindings.cpp
#include <pybind11/pybind11.h>

int add(int a, int b);

namespace py = pybind11;

PYBIND11_MODULE(myadder, m) {
    m.def("add", &add, "A function that adds two numbers");
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(myadder)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)  


# Use Python to find pybind11 automatically
execute_process(
    COMMAND python -c "import pybind11; print(pybind11.get_cmake_dir())"
    OUTPUT_VARIABLE pybind11_DIR
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

# find  pybind11 package
find_package(pybind11 REQUIRED)

pybind11_add_module(myadder bindings.cpp add.cpp)

Build

mkdir build
cd build
cmake .. -G "Visual Studio 16 2019" -A x64
cmake --build . --config Debug

✅Build Info:

Compiler: MSVC

Built as: myadder.pyd with pybind11

Python version: 3.12

Platform: Windows 11

Debugger: cppvsdbg

VSCode Plugin: Python C++ Debugger

launch.json Config

{
    // 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": "Python C++ Debugger",
            "type": "pythoncpp",
            "request": "launch",
            "pythonLaunchName": "Python: Current File",
            "cppAttachName": "(Windows) Attach"
        },
      
        {
            "name": "(Windows) Attach",
            "type": "cppvsdbg",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "symbolSearchPath": "${workspaceFolder}/build/Debug", // .pdb 
            
        },
        // Python 
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/debug.py",
            "console": "integratedTerminal",
            "justMyCode": false,
            "stopOnEntry": true,
        }
    ]
}

Current Behavior: When I run the Python C++ debugger and set a breakpoint at myadder.add(5, 3), it stops in Python, but Step Into (F11) does not go into the C++ function.

However, if I first hit the Python breakpoint and then manually click "Windows(Attach)", then I can then hit breakpoints in C++. But this is a manual.

click Windows (Attach)

✅Desired Behavior:

I want to be able to:

Run Python C++ Debugger.

Stop at the breakpoint in Python code.

Press F11 (or clicking Step Into) can make VSCode step into the C++ implementation without clicking (Windows Attach).

Is it possible to step into C++ pybind11 code directly from Python using the cppvsdbg debugger in VSCode without manually clicking (Windows) Attach?

If so, what is the correct launch.json configuration or steps to make it work?

Any advice? Thanks.

4
  • Was this generated by AI? Commented Aug 4 at 9:55
  • @LajosArpad you mean the code?or the question description ? Commented Aug 4 at 10:35
  • On SO content generated by AI is off-topic. If you expect human responses, then make a human inquiry. Commented Aug 4 at 11:00
  • This issue exists on my demo. And some of descriptions are summarized with AI. Commented Aug 4 at 11:05

1 Answer 1

1

I searched and finally found a similar post:

Debug a Python C/C++ Pybind11 extension in VSCode [Linux]

"Upon reaching your binded code in Python, you may have to click manually in the call stack (in the debug panel on the left) to actually switch into the C++ code."

From the above description, it seems that we need to click the call stack to switch to C++ code manually. So what I encountered is normal.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.