Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to debug a native C++ plugin ?

Discussion in 'Linux' started by Quentin-G, Aug 31, 2016.

  1. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Hi everyone,

    I've been struggling quite a lot recently due to my incapacity to easily debug a native C++ plugin. I found a few solutions for windows, but none for Linux. Debugging by manually logging info into a file was okay for a time, but it makes it sometimes really tedious to find some very simple problems. And the very little information that I get from the Unity Log File (if I get any) when Unity crashes is most of the time useless.
    Ideally, I would like to use gdb to be able to get all the debug info that I need, but it seems a bit more tricky than I thought. I tried to launch the Unity Editor with gdb without success and I'm pretty much out of ideas.

    Did anyone managed to do something similar ?

    Thanks a lot in advance ;).
     
  2. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
  3. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Hi tswalk,

    Thanks a lot for the tip. I'll try it this afternoon and let you know. This might be interesting for a few people.

    Thanks again,
    Quentrin
     
  4. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Okay, so I gave it a try, but it is still not really helping. Basically I can debug my C# code (Using the Unity debugger), but I don't have any info about what's going on in the plugin. So when it crashes I can't get the stack (a simple "backtrace" command with gdb).
    As it was not working with the Unity debugger I tried to create a configuration with cppdbg (which uses gdb).
    Here is the configuration I added :

    {
    "name": "C++ Attach",
    "type": "cppdbg",
    "request": "attach",
    "program": "/PathToMyUnityProject/Assets/MyPlugin.so",
    "processId": "${command.pickProcess}",
    "linux": {
    "MIMode": "gdb"
    }
    }

    When I attach the process to Unity I get the following message:
    "Source /build/glibc-ryFjv0/glibc-2.21/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S is not available."

    And if I try to resume after it paused I get
    "Source /build/glibc-ryFjv0/glibc-2.21/sysdeps/unix/syscall-template.S is not available." and so on

    Am I doing something wrong ?
    I was not sure about the "program" property so I also tried to give the Path to the unity Editor (which I select anyway when attaching the process) and even the path to gdb.

    I'm not really used to vscode yet but it feels like the debugging the plugin is possible. Does anyone has a clue of how to do it ?

    Thanks a lot,
    Quentin.
     
  5. Tak

    Tak

    Unity Technologies

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    If you've built your native plugin with debugging symbols (-g argument for gcc), you should be able to run Unity in gdb and get full debugging support for your plugin. Unity itself is build with minimal symbol info embedded, so you should also at least get stack frame symbols for native code in Unity. Finally, for resolving symbols from JITed code, mono_pmip is your friend.
     
    senkal_ likes this.
  6. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Hello Tak,

    Thanks a lot for your answer. I finally managed to launch Unity with gdb by first doing the command
    "handle SIGXCPU SIG33 SIG35 SIGPWR nostop noprint"

    Sadly I still can't get details with the backtrace command. I usually get this sort of info :
    0x00007fffe2fa3792 in ?? ()
    0x00007fffe2fa4002 in ?? ()
    ...
    But if I try the mono_pmip command, it only prints
    $1 = 0
    ...
    $2 = 0

    Am I missing something ?
    Thanks a lot again for your help.
     
  7. Tak

    Tak

    Unity Technologies

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    That means mono has failed to look up a symbol associated with that frame - not much to do at that point.
    If you set up the mono_backtrace macro listed with the mono_pmip documentation, you can inspect the whole backtrace at once.
     
  8. clementgotouchvr

    clementgotouchvr

    Joined:
    Nov 27, 2018
    Posts:
    1
    Hi everyone, I would like to know if there is any chance that it is possible now to debug step by step in native C++ library with Unity Free version and Visual Community?
    I was able to do that in 2016 with Unity Pro.
    Thanks
     
  9. FrozenKiwi

    FrozenKiwi

    Joined:
    Mar 7, 2019
    Posts:
    13
    Since nobody else answered and this was high in my google search: yeah, it's easy - just VS -> "Attach to Process", select "unity.exe", and that's all you need.

    To troubleshoot: in VS use the "Modules" pane to verify that your dll is loaded and has symbols. I also set code type to "Native" in the attach dialog.
     
    Edy and cLick1338 like this.
  10. rohan-zatun

    rohan-zatun

    Joined:
    Jan 13, 2021
    Posts:
    14
    Someone posted a Visual Studio Solution, here's a Rider one.

    Run > Attach To Process

    And then attach to the Unity process. It looks something like this:
    %pid% %unitypath% -projectPath %projectpath% -riderPath %riderpath% -debugCodeOptimizations.

    Do not use Attach To Unity Process as it attaches to Unity's managed debugger, which won't be useful at all.

    Something else I learned the hard way: only exported functions will have breakpoints associated with them.
     
    jpvanoosten likes this.