microsoft / microsoft/vscode-cpptools

Unable to view values for custom class object in mixed mode (Python & C++) debugging

Open
#9,033 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug type: Debugger

Describe the bug

  • OS and Version: Ubuntu 18.04.5 LTS (on Remote)
  • VS Code Version: 1.65.2 (Running on Windows 10, but, connected to remote server via MS Remote - SSH extension)
  • C/C++ Extension Version: v1.8.4
  • Other extensions you installed: MS Python v2022.2.1924087327; BeniBenj Python C++ Debugger v0.2.15
  • A clear and concise description of what the bug is:

When debugging a mixed language application I am using the launch.json file as shown below to debug both Python and C++. The problem is, when the control reaches the C++ code (via gdb attach), the debugger is not able to "look inside" the object received from the Python code. All relevant libraries and header files have been included.

To Reproduce
Create the following directory structure:

|-- home
|   |-- .vscode
|   |   `-- launch.json
|   |-- MyProj
|   |   |-- cpp
|   |   |   |-- build
|   |   |   |-- include
|   |   |   |   `-- srcfile.h
|   |   |   |-- libs
|   |   |   |   `-- libtorch
|   |   |   |-- source
|   |   |   |   `-- srcfile.cpp
|   |   |   |-- setup.py
|   |   |   `-- setup.cfg
|   |   `-- __init__.py
|   `-- test.py

launch.json

{
    "logging": {
        "engineLogging": true
    },
    "configurations": [
        {
            "name": "Python C++ Debug",
            "type": "pythoncpp",
            "request": "launch",
            "pythonLaunchName": "Python: Current File",
            "cppAttachName": "(gdb) Attach",
            "stopAtEntry": false,
            "externalConsole": false,
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit"
            ]           
        },
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "args": [],
            "program": "/root/anaconda3/envs/py39env/bin/python",
            "processId": "${command:pickProcess}",
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                   "description": "Enable pretty-printing for gdb",
                   "text": "-enable-pretty-printing",
                   "ignoreFailures": true
                },
                {
                   "text": "set print elements 0"
                }
            ],
        },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "python": "/root/anaconda3/envs/py39env/bin/python",
            "program": "${workspaceFolder}/test.py",
            "console": "integratedTerminal",
            "stopOnEntry": false,
            "cwd": "${workspaceFolder}"
        }
    ]
}

srcfile.h

#ifndef _SRCFILE_H
#define _SRCFILE_H

#include <torch/script.h>
#include <torch/torch.h>
#include <torch/extension.h>
#include <pybind11/pybind11.h>

#include <iostream>
#include <memory>

int test(torch::jit::script::Module);

#endif

srcfile.cpp

#include <srcfile.h>


int test(torch::jit::script::Module mod) {
    std::cout<<"Received data"<<std::endl;
    int blah = 1;
    return blah;
}

PYBIND11_MODULE(srcfile, m) {
    m.doc() = "dummy function"; // optional module docstring
    m.def("test", &test, "dummy function");
}

setup.cfg

[build_ext]
debug = 1
[aliases]
release_install = build_ext install
debug_install = build_ext --debug install
debug_dev = build_ext --debug develop

setup.py

from setuptools import setup, Extension
from torch.utils import cpp_extension
import os

os.environ["MAX_JOBS"] = "6"

cpp_module = cpp_extension.CppExtension('srcfile',
    sources = ['src/srcfile.cpp'],
    library_dirs = ['/home/MyProj/cpp/libs/libtorch'],
    include_dirs = [
        '/home/MyProj/cpp/include',
        '/home/MyProj/cpp/libs/libtorch/include',
        '/home/MyProj/cpp/libs/libtorch/include/torch/csrc/api/include',
    ],
    extra_compile_args=['-ggdb']
)

setup( 
    name = 'srcfile',
    author = 'Anurag',
    ext_modules = [cpp_module],
    cmdclass = {'build_ext':cpp_extension.BuildExtension}
)

test.py

import torch
import torch.nn as nn
from MyProj.cpp import srcfile as j

device = torch.device('cpu')

class Dummy(nn.Module):
    def __init__(self):
        super(Dummy, self).__init__()
        self.conv = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=2, stride=1, padding=0, bias=False)
        self.flat = nn.Flatten(start_dim=1, end_dim=-1)
        self.lin = nn.Linear(in_features=81, out_features=5, bias=False)

    def forward(self, x):
        x = self.conv(x)
        x = self.flat(x)
        x = self.lin(x)

        return x    


def main():
    net = Dummy()
    #x = torch.randn(10,10,3, dtype=torch.float16)
    scr = torch.jit.script(net)
    print(j.test(scr._c))
    blah = 1


if __name__ == '__main__':
    main()

Now goto the directory: /home/MyProj/cpp and run the command: python setup.py debug_dev

After this, go to the debug panel and launch the debug configuration. Set breakpoints at the blah = 1 statement of both the languages.

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

Reproduce the mixed Python/C++ session using .vscode/launch.json, srcfile.cpp, and test.py, then run python setup.py debug_dev from /home/MyProj/cpp. Start with the (gdb) Attach and Python C++ Debug configurations and inspect the custom torch::jit::script::Module at the C++ breakpoint. Done means the debugger can show the received custom-class object's values.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
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.