简体   繁体   English

Visual Studio 代码:附加到 Unity 进程以调试基于 C++ 的 dll

[英]Visual Studio Code: Attach to Unity process for debugging C++ based dll

I have a Unity project that uses some C++ code via a DLL compiled in a separate project.我有一个 Unity 项目,它通过在单独项目中编译的 DLL 使用一些 C++ 代码。 Can I attach the visual studio code debugger to my Unity project such that I can debug the DLL's source code using break points?我可以将 Visual Studio 代码调试器附加到我的 Unity 项目,以便我可以使用断点调试 DLL 的源代码吗?

Here are some things I tried so far:以下是我到目前为止尝试过的一些事情:

  • in Unity: Press "Pause", then press "Start" to immediately pause the game after starting it (in order to get time to attach vs code)在 Unity 中:按“暂停”,然后按“开始”在启动游戏后立即暂停游戏(以便有时间附加 vs 代码)
  • compile DLL using debug symbols使用调试符号编译 DLL
  • in VS Code: create a launch.json like this在VS Code中:像这样创建一个launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to process",
            "type":"clr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }]
}

--> this should allow me to pick the process I want to connect to interactively --> 这应该允许我选择我想以交互方式连接的进程

  • VS Code: click on "Attach to process" -> search for my project name -> returns a process based on my/path/to/Unity.exe --> attaching seems to work, but when I "unpause" my Unity game it never hits a break point. VS Code:单击“附加到进程”-> 搜索我的项目名称-> 根据my/path/to/Unity.exe返回一个进程-> 附加似乎有效,但是当我“取消暂停”我的 Unity 游戏时它永远不会达到断点。

Is my launch.json wrong?我的launch.json错了吗?

Some additional info:一些附加信息:

  • I'm using bazel to compile my c++ library project via command line (not sure if relevant?)我正在使用 bazel 通过命令行编译我的 c++ 库项目(不确定是否相关?)
  • Usually when debugging C++ in VS code my launch.json has an entry sourceMap which directs the debugger to the root of my source files.通常在 VS 代码中调试 C++ 时,我的 launch.json 有一个条目sourceMap ,它将调试器定向到我的源文件的根目录。 Not sure if anything similar would be needed here as well?不确定这里是否也需要类似的东西?

Moving forward向前进
Meanwhile I've refactored my launch.json a bit.同时我重构了我的launch.json。 Thanks to a comment I assume "type": "clr" stand for Common Language Runtime which seems to be for debugging scripting languages but not C/C++.感谢评论,我假设"type": "clr"代表公共语言运行时,它似乎用于调试脚本语言而不是 C/C++。 So I changed it to "type":"cppdbg" .所以我把它改成了"type":"cppdbg" After installing gdb via Msys2, I'm referencing the path to that gdb in the launch.json.通过 Msys2 安装 gdb 后,我在 launch.json 中引用了 gdb 的路径。 This is an updated version of my launch.json:这是我的launch.json的更新版本:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to process",
            "type":"cppdbg",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "program": "${workspaceRoot}/Packages/com.github.homuler.mediapipe/Runtime/Plugins/mediapipe_c.dll",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
        }]
}

Spoiler: It's still not working, but inside VS Code debug console / terminal I see some output when I start the game in Unity editor.剧透:它仍然无法正常工作,但在 VS Code 调试控制台/终端中,当我在 Unity 编辑器中启动游戏时,我看到了一些 output。 So there seems to be some traffic between VS Code and Unity at least.因此,至少 VS Code 和 Unity 之间似乎存在一些流量。
One new problem:一个新问题:

  • with second version of launch.json, C++ breakpoints are grey with the info message "Attempting to bind the breakpoint...."使用第二版启动。json、C++ 断点为灰色,并显示信息消息“正在尝试绑定断点......”

While I couldn't figure out a solution for VS Code, I found a solution for虽然我无法找到 VS Code 的解决方案,但我找到了解决方案

  • Microsoft Visual Studio Community 2022 RC (64-bit), installed with Microsoft Visual Studio Community 2022 RC(64 位),随
    • Desktop development with C++使用 C++ 进行桌面开发
    • Universal Windows Platform development通用 Windows 平台开发
    • note: this is not the standalone VS shipped with Unity (which is VS 2019)注意:这不是 Unity 随附的独立 VS(即 VS 2019)

the solution goes roughly like this解决方案大致是这样的

  • compile C++ DLL library with debug flags使用调试标志编译 C++ DLL 库
    • since in my case the compilation was done using bazel, I cannot even say which compiler was used, but it doesn't seem to matter因为在我的情况下,编译是使用 bazel 完成的,我什至不能说使用了哪个编译器,但这似乎并不重要
  • Open VS -> File open -> Select folder that contains C++ source code打开 VS -> 文件打开 -> 选择包含 C++ 源代码的文件夹
  • Set breakpoint in .cc file (that is launched from Unity)在 .cc 文件中设置断点(从 Unity 启动)
  • Launch Unity启动 Unity
  • In VS (top menu bar) click on Debug -> Attach to Process在 VS(顶部菜单栏)中单击 Debug -> Attach to Process
    • in process list search for Unity.exe (there should only be one entry)在进程列表中搜索 Unity.exe(应该只有一个条目) 在此处输入图像描述
    • above that list is an option "Attach to:" -> Select -> "Native"该列表上方是一个选项“附加到:”->选择->“本机” 在此处输入图像描述
  • Launch Game inside Unity Editor在 Unity 编辑器中启动游戏

--> the game should now break when hitting the breakpoint --> 游戏现在应该在到达断点时中断

The C++ DLL library Project in VS be compiled debug first, also need add the /Zi parameter. VS中的C++ DLL库项目先编译调试,还需要添加/Zi参数。

Follow steps:遵循以下步骤:

  1. Open the project's Property Pages dialog box.打开项目的“属性页”对话框。
  2. Click the C/C++ folder.单击 C/C++ 文件夹。
  3. Click the General property page.单击常规属性页。
  4. Modify the Debug Information Format property.修改调试信息格式属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM