microsoft / microsoft/vscode-cpptools

Debug a Python C/C++ extension in VSCode on Windows

Open
#6,974 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

debugger help wanted question
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Type: Debugger

Describe the bug

  • OS and Version: Windows 10, 64-bit
  • VS Code Version: 1.53.2
  • C/C++ Extension Version: 1.2.0
  • Other extensions you installed (and if the issue persists after disabling them): Python
  • Vscode Github issue that redirects me here: issue
  • I wouild like to debug a Python C/C++ extension on Windows I have posted a question with full details in StackOverflow, but no suitable answer has been posted.
    In Linux, it seems to be possible (see Nadiah link). However, on Windows, after attaching a "(gdb) attach" to the active Python process, I cannot stop on any breakpoint of the C code.
    I can debug a standalone C/C++ application without problems.
    I am not sure if it is a bug or it is a feature that is not yet available in Windows. Any help is much appreciated.

To Reproduce
Please include a code sample and launch.json configuration.
Code to reproduce the behavior:

  1. Python Code
import os
import greet

pid = os.getpid()

test=2.2

greet.greet('World')

print("hi")
  1. C code
#include <Python.h>

static PyObject *
greet_name(PyObject *self, PyObject *args)
{
    const char *name;

    if (!PyArg_ParseTuple(args, "s", &name))
    {
        return NULL;
    }
    
    printf("Helllo %s!\n", name);

    Py_RETURN_NONE;
}

static PyMethodDef GreetMethods[] = {
    {"greet", greet_name, METH_VARARGS, "Greet an entity."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef greet =
{
    PyModuleDef_HEAD_INIT,
    "greet",     /* name of module */
    "",          /* module documentation, may be NULL */
    -1,          /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    GreetMethods
};

PyMODINIT_FUNC PyInit_greet(void)
{
    return PyModule_Create(&greet);
}

I compile the C code with GCC 8.1 by running

python setup.py install

  1. Setup file
import os

from setuptools import setup, Extension

os.environ["CC"] = "g++-8.1.0"

_DEBUG = True
_DEBUG_LEVEL = 0
# extra_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = ["-Wall", "-Wextra"]
if _DEBUG:
    extra_compile_args += ["-g3", "-O0", "-DDEBUG=%s" % _DEBUG_LEVEL, "-UNDEBUG"]
else:
    extra_compile_args += ["-DNDEBUG", "-O3"]

setup(
    name='greet',
    version='1.0',
    description='Python Package with Hello World C Extension',
    ext_modules=[
        Extension(
            'greet',
            sources=['greetmodule.c'],
            py_limited_api=True,
            extra_compile_args=extra_compile_args)
    ],
)

I even specify O0 option to have all debug symbols.

  1. Launch JSON file
"configurations": [
    {
        "name": "(gdb) Attach",
        "type": "cppdbg",
        "request": "attach",
        "program": "venv/Scripts/python",
        "processId": "${command:pickProcess}",
        "MIMode": "gdb",
        // "miDebuggerPath": "/path/to/gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
    }
]

Steps that I follow:

  1. Add a breakpoint in the python file, run the launch configuration “Python: Current File” and wait until the breakpoint is reached.
  2. Run the “(gdb) Attach” launch configuration, select the python interpreter whose path contains “/.vscode/”. In this case, in Windows, I am not getting prompted to enter my user password as it happens in Linux.
  3. Set breakpoints in the C++ Files
  4. The python debugger is currently stopped at a break point. Switch back from the debugger “(gdb) Attach” to the other debugger “Python: Current File” and press F5 (Continue).
    In this last step, vscode should automatically jump between the two debuggers between python and c++ code, but I cannot achieve this behavior.

I am able to debug Python and C programs alone, but not together.

Additional context
If applicable, please include logging by adding "logging": { "engineLogging": true, "trace": true, "traceResponse": true } in your launch.json
Add any other context about the problem here including log or error messages in your Debug Console or Output windows.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with greetmodule.c, setup.py, and the two launch configurations: “Python: Current File” and “(gdb) Attach”. Read the reported breakpoint sequence and compare the behavior of the Python and C/C++ debugging sessions on Windows. Done means the documented mixed Python/C debugging workflow can stop at the C breakpoint and resume across both debuggers, or the supported limitation is clearly identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, python, vscode
Domain
desktop, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.