简体   繁体   中英

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. 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?

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)
  • compile DLL using debug symbols
  • in VS Code: create a launch.json like this
{
    "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.

Is my launch.json wrong?

Some additional info:

  • I'm using bazel to compile my c++ library project via command line (not sure if relevant?)
  • 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. Not sure if anything similar would be needed here as well?

Moving forward
Meanwhile I've refactored my launch.json a bit. 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++. So I changed it to "type":"cppdbg" . After installing gdb via Msys2, I'm referencing the path to that gdb in the launch.json. This is an updated version of my 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. So there seems to be some traffic between VS Code and Unity at least.
One new problem:

  • with second version of launch.json, C++ breakpoints are grey with the info message "Attempting to bind the breakpoint...."

While I couldn't figure out a solution for VS Code, I found a solution for

  • Microsoft Visual Studio Community 2022 RC (64-bit), installed with
    • Desktop development with C++
    • Universal Windows Platform development
    • note: this is not the standalone VS shipped with Unity (which is VS 2019)

the solution goes roughly like this

  • compile C++ DLL library with debug flags
    • 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
  • Open VS -> File open -> Select folder that contains C++ source code
  • Set breakpoint in .cc file (that is launched from Unity)
  • Launch Unity
  • In VS (top menu bar) click on Debug -> Attach to Process
    • in process list search for Unity.exe (there should only be one entry) 在此处输入图像描述
    • above that list is an option "Attach to:" -> Select -> "Native" 在此处输入图像描述
  • Launch Game inside Unity Editor

--> 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.

Follow steps:

  1. Open the project's Property Pages dialog box.
  2. Click the C/C++ folder.
  3. Click the General property page.
  4. Modify the Debug Information Format property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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