microsoft / microsoft/vscode-cpptools

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

Aperta
#9,033 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

debugger help wanted
Lingua principale
TypeScript
Stelle
6.2k
Fork
1.7k
Merge medio
14h 46m
PR unite (30g)
61

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci la session mista Python/C++ usando .vscode/launch.json, srcfile.cpp e test.py, quindi esegui python setup.py debug_dev da /home/MyProj/cpp. Inizia con le configurazioni (gdb) Attach e Python C++ Debug e ispeziona il custom torch::jit::script::Module al breakpoint C++. Il lavoro è completato quando il debugger riesce a mostrare i valori dell’oggetto custom-class ricevuto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp, python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.