microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

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

Open DavidDuranPerez opened 3 years ago

DavidDuranPerez commented 3 years ago

Type: Debugger

Describe the bug

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.

WardenGnaw commented 3 years ago

In this last step, vscode should automatically jump between the two debuggers between python and c++ code, but I cannot achieve this behavior.

The issue is that the python debugger and C++ debugger are two separate debug adapters. The C++ debugger does not know if the python debugger is stopped and vice versa. They are querying the process independently and do not know what the other debugger has done to that process. The interesting question is if we can see when the debug request/responses are coming from each adapter and if VS Code can handle the multiple requests at once.

Also, this extension is primarily focused on C/C++ and not mix mode with other languages. We appreciate any help from other users who may be attempting this scenario.

DavidDuranPerez commented 3 years ago

Yes, any help is much appreciated.

Bear also in mind that, theoretically, this works in Linux (see link). Hence, there may be some specific problem related to Windows...

axelande commented 3 years ago

I can just confirm Davids findings.. Feels odd that it seems to work on other platforms than Windows. I'm trying to migrate a large project from visual studio to vscode.

axelande commented 3 years ago

@WardenGnaw Is there anything in particular you need some help with?

WardenGnaw commented 3 years ago

The help wanted is very general since this seems to be a mixed python and c++ debugging scenario issue since individually debugging via C++ is working. I am unsure how the python debugger would cause the gdb attach to not break at breakpoints.

DavidDuranPerez commented 3 years ago

@axelande, I am not familiar with this library, but I am open to help you if you decide to contribute towards fixing the issue.

roxanadangerm commented 3 years ago

@DavidDuranPerez @WardenGnaw I am happy to help to solve this issue. Is there any logs we should look at?

DavidDuranPerez commented 3 years ago

Hi @roxanadangerm glad to hear that. I don't have any log file, just the information I provided. If you tell me how to obtain the log file, I can give it to you that. Thanks.

jameszhangzq commented 11 months ago

Hi @DavidDuranPerez, I'm facing the same problem and have been looking for solutions everywhere but cannot find any, just wondering if you have solved this issue?

DavidDuranPerez commented 11 months ago

Unfortunately, I found no solution. The other alternative has been to have Linux on a hard drive and debug the code there.

On Mon, 13 Nov 2023 at 09:49, jameszhangzq @.***> wrote:

Hi @DavidDuranPerez https://github.com/DavidDuranPerez, I'm facing the same problem and have been looking for solutions everywhere but cannot find any, just wondering if you have solved this issue?

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-cpptools/issues/6974#issuecomment-1807696407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGDLPXPHXWLAVVZ4BC3PMGLYEHNLHAVCNFSM4XVKVDH2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBQG43DSNRUGA3Q . You are receiving this because you were mentioned.Message ID: @.***>

-- David Durán Pérez Aeronautical Engineer @.***

mrx23dot commented 3 months ago

my guess is start 2 debugger servers one for each language, connect individual VScode instances for each.

Or try to solve it outside vscode with gdb, then just attach vscode as a viewer.