benibenj / vscode-pythonCpp

A Visual Studio Code Debug Extension for debugging mixed Python and C++ code. The extension starts a Python debug session and attaches the C++ debugger to it. This extension is useful for debugging Python programs that call functions from shared libraries (.so/.dll).
Other
47 stars 7 forks source link

Can't step into c++ function in pytorch source code #2

Closed lalalazy12 closed 2 years ago

lalalazy12 commented 3 years ago

Here is my default configurations in launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python C++ Debugger",
            "type": "pythoncpp",
            "request": "launch",
            "pythonConfig": "default",
            "cppConfig": "default (gdb) Attach"
        },
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "processId": "${command:pickProcess}",
            "MIMode": "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",
            "justMyCode": false
        }
    ]
}

I do not know if the setting is correct, and I do insert breakpoints at both python file and c++ file, however it dose not work.

benibenj commented 3 years ago

Firstly, are you running your code in linux or on windows? If windows you might want to consider using "cppConfig": "default (win) Attach".

Secondly, if you want to use the python and/or C++ configurations from your launch file, you need to define them in the pythoncpp config:

{
      "name": "Python C++ Debug",
      "type": "pythoncpp",
      "request": "launch",
      "pythonLaunchName": "Python: Current File",
      "cppAttachName": "(gdb) Attach",
}

However, if you want to use the default launch config, your configuration looks fine.

Lastly, as you haven't provided much information, the only thing I can add is that you should make sure that whatever C++ function you are calling that it has been compiled with DebugInfo.

lalalazy12 commented 3 years ago

Thank you for your timely reply!

Firstly, are you running your code in linux or on windows? If windows you might want to consider using "cppConfig": "default (win) Attach".

I am using Linux.

Lastly, as you haven't provided much information, the only thing I can add is that you should make sure that whatever C++ function you are calling that it has been compiled with DebugInfo.

When I am compiling source code, I set this: $export DEBUG=1, as mentioned in Pytorch installation document, I thought it would be fine.

And for this line: "program": "enter program name, for example ${workspaceFolder}/a.out", I have another question, I am new to Pytorch, I can't find the /a.out anywhere, I am even not sure if it exists, could you help me if you are familiar with Pytorch?

From other solutions, I tried to set like this: "program": "~/miniconda3/envs/xmyenv/bin/python", since I am using conda., however it saied: program path: ~/miniconda3/envs/xmyenv/bin/python does not exist

benibenj commented 3 years ago

To get the program path, you can click on 'python' in the status bar at the bottom which will open a dropdown with all python interpreters that vscode can find. You will also see the corresponding path for your chosen interpreter which is the path you want to set the "program" attribute to. Screenshot (104)

lalalazy12 commented 3 years ago

Dose this tool work for C extension using Cpython? or just works with ctypes?

benibenj commented 3 years ago

Yes, this extension should also work for Cpython

lalalazy12 commented 2 years ago

Yes, this extension should also work for Cpython

I write a small test by using CPython, here is the c file:

#include "Python.h"

int fastfactorial(int n){
 if(n<=1)
 return 1;
 else
 return n * fastfactorial(n-1);
}

static PyObject* factorial(PyObject* self, PyObject* args){
int n;
if (!PyArg_ParseTuple(args,"i",&n))
  return NULL;
int result = fastfactorial(n);
return Py_BuildValue("i",result);
}

static PyMethodDef mainMethods[] = {
 {"factorial",factorial,METH_VARARGS,"Calculate the factorial of n"},
 {NULL,NULL,0,NULL}
};

static PyModuleDef cmathi = {
 PyModuleDef_HEAD_INIT,
 "cmathi","Factorial Calculation",
 -1,
 mainMethods
};

PyMODINIT_FUNC PyInit_cmathi(void){
 return PyModule_Create(&cmathi);
}

and here is the python file:

from cmathi import factorial
print(factorial(3))

It works well, i.e. I can compile it and the result is correct.

And when I use your extension, it is not working, I insert two breakpoints in both python call and the c function in c file, I can not step into the c file.

btw. here is the setup.py, if you want to reproduce this bug, you can compile it:

from distutils.core import setup, Extension
factorial_module = Extension('cmathi',sources = ['cmathi.c'])
setup(name = 'MathExtension',version='1.0',description = 'This is a math package',ext_modules = [factorial_module])

and this is my config file in vscode:

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python C++ Debug",
            "type": "pythoncpp",
            "request": "launch",
            "pythonLaunchName": "Python: Current File",
            "cppAttachName": "(gdb) Attach",
        },

        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/home/sparkuser/miniconda3/envs/xmyenv/bin/python",
            "processId": "${command:pickProcess}",
            "MIMode": "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",
            "justMyCode": false
        }
    ]
}
lalalazy12 commented 2 years ago

Following the compile solution B.1, It finally works, Thanks a lot.

But still not work with pytorch source code, so sad.

NiKeYiGuN commented 1 year ago

Yes, this extension should also work for Cpython

also working for cppyy or swig?