Search Unity

Question How can I debug my Android native C++ library for Unity3D?

Discussion in 'Android' started by garrilla, Jul 12, 2019.

  1. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    I have created a JUCE based audio library in C++ (originally as a windows DLL)

    I have successfully built it into an AAR library with Android Studio

    I can successfully run it in a Unity3D build for Android.

    I can get the debugger messages back to LogCat

    However, I really need to step through some part of the native C++ code with a managed debugger. ( I know how use a debugger for the c# for unity on android)

    I've searched for days but cannot find an answer.

    Unity3D 2018.3.11f1

    Android Studio 3.4.1 Build #AI-183.6156.11.34.5522156, built on May 1, 2019 JRE: 1.8.0_152-release-1343-b01 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0
     
    steril and MarcSpraragen like this.
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    You use Android Studio's Native Debugger for such cases, from Unity choose Export Project in BuildPlayerWindow, and import that project in Android Studio.

    Set debugger type to Native in Android Studio, in project settings set the symbol path, if those were separated from library files. And you're good to go.
     
    steril and garrilla like this.
  3. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    Thanks.

    I know how to set the symbols but can you elaborate on "if those were separated from library files" I'm not sure what you're suggesting
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    I am talking about stripping symbols from your native library, and were they stripped to separate file, sorry for not being clear
     
  5. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    OK, thanks again. I'm with you now. They're not stripped.

    However, I do have another question if you have the patience for it.

    When my exported app comes into Android Studio, the native code is the .aar so I'm a little confused how I should set breakpoints?

    Should I bring my library into the exported app? or the exported app into the library? etc.
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    Assuming the library was built on same machine, you drag & drop C/C++ files in your Android Studio project and place breakpoints there.

    P.S AAR is simply a package (a zip file), the native library sits in aar itself, which has .so extension. You can actually unzip AAR using 7zip or othere compression program.
     
  7. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    OK, I built the AAR. (I know it a zip too - but a good tip!)

    So I just need to copy the source files into the exported project into the /libs folder? sorry if this seems obvious, just a complete android nube except for vanilla builds.
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    You don't need to copy them, you simply need to open them in Android Studio, either drag & drop, or File->Open, after that you can start placing breakpoints
     
  9. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    I really appreciate your replies and I'm so sorry for seeming dumber...


    so I have two projects open - my Unity-export app & my c++native library

    what am dragging and where am I dropping it?

    e.g drag my source folder from the library to the libs folder of the export app, etc
     
  10. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    No worries.

    You drag & drop C++ files to Android Studio session which currently debugs your application, you don't need to add those files to project.


    AS.jpg
     
    garrilla likes this.
  11. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    OK, I get you now!

    I've done that, but it doesn't hit the breakpoint I set - although I know from the logcat that it must have past that point (a Debug.Log() command from unity to the native code prints successfully)

    EDIT: my error, I was setting the wrong path for the symbols.

    It works now.

    Thanks so much for your help @Tomas1856
     
    Last edited: Jul 15, 2019
    Tomas1856 likes this.
  12. eanders-ms

    eanders-ms

    Joined:
    Jun 6, 2018
    Posts:
    1
    @garrilla amazing you got this working! Could you share what the path to your symbols was? I seem to have the same setup as you describe. I want to attach the native debugger to my native DLL in my Unity project running on Android. I'm unsure where to point it to for symbols.
     
  13. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    1) go into 'edit configurations' of your lib (this is is the drop down menu next to the build-hammer)

    2) under 'debugger' add a Symbol Directory - then select 'armeabi-v7a' by navigating to

    [my root path to my lib]\Builds\Android\lib\build\intermediates\cmake\debug_\obj\armeabi-v7a

    hope that helps
     
    Last edited: Jan 14, 2020
    astrokoo3 likes this.
  14. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    your post give me courage :)

    i'm almost give up on similar issue

    that i want to break il2cpp files on android studio

    i read this post https://forum.unity.com/threads/debugging-android-il2cpp-builds.493296/#post-3210398

    but its not work for me

    i try breakpoint but lldb didn't match the source code


    and try to command `image list`
    and there no il2cpp.so

    my extra setting is below

    rename libil2cpp.so.debug to libil2cpp.so and replace to

    \AndroidStudio\appname\src\main\jniLibs\armeabi-v7a\libil2cpp.so

    so symbolic file be involved

    and specify symbol Directories in Edit Configuration to above directory

    how do i debug il2cpp file on lldb of android studio?


    @JoshPeterson @Tomas1856
     
    Last edited: Jan 18, 2021
  15. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    why did you do this? src\main\jniLibs\armeabi-v7a\ should contain executable code only, don't copy symbol file on top of it. You need to rename ibil2cpp.so.debug to ibil2cpp.so but keep where it is, and tell Android Studio where to look for it.

    P.S This might helpful, but in 2020.1 Export Project allows you to easier to debug il2cpp, https://unity3d.com/unity/whats-new/2020.1.0


    Code (CSharp):
    1. Android: When exporting a gradle proejct, il2cpp generated native files will be also copied to gradle project, the il2cpp library will be compiled/linked via gradle. Previously il2cpp library was being precompiled/prelinked by Unity when exporting project. This improvement gives you an ability to both debug and profile il2cpp files when needed, it also increases your control over the build pipeline. Note: If you don't export a project, il2cpp library will be linked by Unity as it was before.
    You'll still need to set symbol path after project export.
     
    astrokoo3 likes this.
  16. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    it's great news

    but we are in live service using 2018.4.10f version for 1 year

    so i'm afraid to update to 2010 version

    at this time. i didn't touch the directories. and specify symbol directories outside android studio where ilil2cpp.so.debug file exist
    and change il2cpp.so.debug to il2cpp.so

    run this setup has same result

    'image list' not showing libil2cpp.so and libmain.so, libunity.so

    lookup some symbol is nothing to print @Tomas1856
     
  17. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    Just in case, did you set Debugger Type to Native, and did you set symbol path under Native Debugger scope?

    Also while paused, you can do

    target symbols add <path_to_symbol> to force symbol load
     
  18. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    upload_2021-1-19_9-22-58.png

    yes. the path inside symbol directories is where libil2cpp.so live and libil2cpp.so is copied from libil2cpp.so.debug



    `target symbols add <path_to_symbol> to force symbol load`

    upload_2021-1-19_9-27-19.png

    add symbols forced has error

    2 settings not work. thanks

    @Tomas1856
     
    Last edited: Jan 19, 2021
  19. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    i create simple project for 2020.1.0 to test debugging on android with new feature

    and it's seem to have same issue

    upload_2021-1-19_18-39-46.png

    find some function name that generated by il2cpp and copy then paste to lldb

    i test it on x86 android device (windows environment)

    Sorry if I bothered you. But please help me as I have to solve this problem. thanks

    @Tomas1856
     

    Attached Files:

  20. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    Can you clarify? Unity doesn't support x86, it only supports ARMv7 and ARM64. So is this a typo what you wrote? (Edit: sorry, forgot that 2018 still let's you select x86)

    Also just in case, you're testing on physical device, not an emulator? Because lldb won't work on emulator with Unity apps.
     
    Last edited: Jan 19, 2021
  21. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    I can confirm it works on my end, using 2018.4.26f1, I'll try to recap what I did
    • Create a new project
    • Switch to Android
    • Select Il2CPP
    • Select ARMv7 architecture only
    • In Build Settings Window
    • Select Create Symbols.zip
    • Export Project On
    • Build
    • Extract Export-0.1-v1.symbols.zip (for you name will be different depending on your export folder name)
    • Rename Export-0.1-v1.symbols\armeabi-v7a\libil2cpp.so.debug -> Export-0.1-v1.symbols\armeabi-v7a\libil2cpp.so
    • Rename Export-0.1-v1.symbols\armeabi-v7a\libunity.dbg.so -> Export-0.1-v1.symbols\armeabi-v7a\libunity.so
    • Open exported project with Android Studio
    • In Debugger Settings
    • Select Debugger Type Native
    • In LLDP Post Attach Commands add (the following signals are used by managed debugger):
    • process handle SIGXCPU -n true -p true -s false
    • process handle SIGPWR -n true -p true -s false
    • In Symbol Directories, add path to symbols
    • F:\UnityProjects\DebuggingTest\Export-0.1-v1.symbols
    • That's it, start debugging, in my case I launched it on Google Pixel
    • Break the program
    • In LLDB console, image list for me prints
    Code (CSharp):
    1. ...
    2. [269] 1C260E50-B4D2-7174-74FD-DE90EC382C3B            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\1C260E50-B4D2-7174-74FD-DE90EC382C3B\libnpt.so
    3. [270] D6F377B0-0390-417E-2AF1-53783B6079D1            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\D6F377B0-0390-417E-2AF1-53783B6079D1\libdt_fd_forward.so
    4. [271] E7A78851-1436-15DA-AA72-EE2F6DD6B816-41D0803D            F:\UnityProjects\DebuggingTest\Export\DebuggingTest\src\main\jniLibs\armeabi-v7a\libmain.so
    5. [272] 2678F5C1-70A3-9509-BA3E-5FA4C6B22E9D-B00C98A6            F:\UnityProjects\DebuggingTest\Export-0.1-v1.symbols\armeabi-v7a\libunity.so
    6. [273] 7D0ECA9F-D87C-9DC2-9668-90460E55E953            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\7D0ECA9F-D87C-9DC2-9668-90460E55E953\gralloc.msm8998.so
    7. [274] C4CF0795-E1DC-05BF-880C-5E7E265B4ADB            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\C4CF0795-E1DC-05BF-880C-5E7E265B4ADB\libqdMetaData.so
    8. [275] 81A2B1CD-77CE-4691-0D5D-4436B81DE0FA            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\81A2B1CD-77CE-4691-0D5D-4436B81DE0FA\libdrmutils.so
    9. [276] 51BAA9A5-B3BF-8831-E120-2071E98868C8            C:\Users\tomas\.lldb\module_cache\remote-android\.cache\51BAA9A5-B3BF-8831-E120-2071E98868C8\libdrm.so
    10. [277] 37E4B49F-120B-25C5-C8D3-AEDBE001246B-F0B8C490            F:\UnityProjects\DebuggingTest\Export-0.1-v1.symbols\armeabi-v7a\libil2cpp.so
    so symbol file was correctly loaded. The breakpoints (at least the symbolic ones also work fine)

    Hopefully that hints you what might be wrong.
     
    astrokoo3 likes this.
  22. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    upload_2021-1-19_19-54-57.png


    i'm using LDPlayer(https://en.ldplayer.net/) that run on windows. (ie. running device of Virtual Device highlight)

    is it emulator?

    (our serivce is mobile mmorpg. lots of player are playing game on LDPlayer for long period connected)


    i didn't select x86 for building apk

    upload_2021-1-19_20-2-45.png

    but i read this https://commonsware.com/blog/2013/11/21/libhoudini-what-it-means-for-developers.html

    so i think arm binary can run on x86


    upload_2021-1-19_19-58-54.png

    can't i debug that environment?

    if i change device to real mobile device, then i can break some function?

    thanks

    @Tomas1856
     

    Attached Files:

  23. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    great thank a lot your concern

    i will do step by step
     
  24. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
    libhoudini is an emulation library, so probably that's the reason the debugger doesn't work, but this is beyond my knowledge, only google guys can answer that.

    Also, note, Unity doesn't support emulations, if it runs - it runs, but if there's a crash on emulator, such bug would be declined by Unity QA.
     
    astrokoo3 likes this.
  25. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    23
    Thanks for clarification


    yes. that's the reason. i do it same thing as you do and it's not like your result

    @Tomas1856
     
  26. manassehclifford

    manassehclifford

    Joined:
    Sep 20, 2021
    Posts:
    8
    Hi I made unity native audio plugin with the juce library. it works well in mac and windows. I want to export it for android. this thread seems to contain a lot of awesome information. I have learnt that i have to build the plugin as a shared library inorder to use it in an android application. could anyone provide information on how i can build a shared library for android from my juce plugin project?
     
  27. kyoenid

    kyoenid

    Joined:
    Jun 20, 2022
    Posts:
    1
    I do those steps by using unity 2022.3.22f1, and android studio 2023. set symbol directories to android\unityLibrary\symbols but I can't breakpoints c++ file, and use image list cmd in lldb,there are not show any libunity or libmain, libil2cpp. If I target add symbols by manual, they just say there is no module.
    any suggestion???