mcExchange
mcExchange

Reputation: 6497

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:

{
    "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

Is my launch.json wrong?

Some additional info:

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:

Upvotes: 1

Views: 1693

Answers (2)

user1697068
user1697068

Reputation: 1

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.

Upvotes: 0

mcExchange
mcExchange

Reputation: 6497

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 (doesn't have to be the same folder as the Unity project)
  • Set breakpoint in any .cc file (that is accessed 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) enter image description here
    • above that list is an option "Attach to:" -> Select -> "Native" enter image description here
  • Launch Game inside Unity Editor

--> the game should now break when hitting the breakpoint

Upvotes: 2

Related Questions