Hi, debugging the iOS platform on KMP is proving t...
# multiplatform
j
Hi, debugging the iOS platform on KMP is proving to be a bit complicated. I am not sure if I am doing something wrong, but is it not possible to inspect variables or evaluate expressions using the intelliJ debugger? I can set the breakpoints fine but when it comes to seeing the snapshot of my objects I don’t get any output, just errors.
k
Evaluate expression feature is not supported yet for kotlin native debugger
j
How could you make to stop at line? My KMM debugger is always refusing to add line break.
@Konstantin Tskhovrebov Could you please share the version of Kotlin, Android Studio and Xcode you use, that you make it stop at least? 🙂 I was unfortunately not that lucky. https://kotlinlang.slack.com/archives/C3PQML5NU/p1673631195903349
k
I guess the main problem is coroutines' machinery. try to debug functions with plain kotlin code
for checking your environment there is kdoctor tool https://github.com/Kotlin/kdoctor
j
We use co-rutines in code (and as a dependency), but the code I try to debug is called directly from UI/thread. Or do you mean that my project should not use co-rutines at all?
k
I mean coroutines generate lot of internal stuff under the hood. and KN debugger cannot understand what happened yet.
but it should work fine in function without context switching or other coroutine related stuff.
j
But the call I try to debug does not use corutines, I should be fine, right? We use co-rutines only for background syncing stuff. Everything else is done synchronously / direct call / no threading on iOS-level.
k
yes. it should be fine
did you try to debug new project from wizard?
j
Kdoctor found no issues.
I have tried to debug the example project from github. And I had the same experience in debugging from Android Studio and Xcode. To be honest I haven’t seen debugging of Kotlin Multiplatform (on iOS) working on any machine for 3 years we use the Kotlin in our app. 🤷‍♂️
Here are results of debugging of KampKit example app (Android Studio & Xcode)
k
@Jan Kuchař once stopped anywhere during debugging session, could you please enter next commands in
LLDB
console:
Copy code
br list
image dump line-table
and share output?
j
Sure.
Copy code
(lldb) br list

Current breakpoints:
1: name = 'os_log_fault_default_callback', locations = 1, resolved = 1, hit count = 0

  1.1: where = libsystem_trace.dylib`os_log_fault_default_callback, address = 0x000000018006ddb0, resolved, hit count = 0 

2: names = {'objc_exception_throw', '__cxa_throw'}, locations = 2, resolved = 2, hit count = 0

  2.1: where = libobjc.A.dylib`objc_exception_throw, address = 0x000000018005110c, resolved, hit count = 0 
  2.2: where = libc++abi.dylib`__cxa_throw, address = 0x000000018023dc84, resolved, hit count = 0
k
Did you put any line breakpoints? It looks like you have only function-name and exceptional.
j
To be hones I can dump the line-table only for swift files or kotlin-built-in functions. (or I miss the way how to do that)
Did you put any line breakpoints?
Sure, I did. But they are “skipped” when placed into kotlin code. (see the attached video)
k
Do you operate Xcode or Android Studio + KMM? I'm here to take a look at KMM issues)
j
This example I have tried in Xcode. But I can use Android Studio if you wish. 🙂
This is the ouput from Android Studio, with the example application, with breakpoints placed the same way as in shared video.
Copy code
(lldb) br list
Current breakpoints:
1: regex = 'co.touchlab.kampkit.models.BreedRepository#<set-log>', locations = 0 (pending)

2: regex = 'co.touchlab.kampkit.models.BreedRepository#refreshBreeds\(', locations = 1, resolved = 1, hit count = 0
  2.1: where = shared`kfun:co.touchlab.kampkit.models.BreedRepository#refreshBreeds(){}, address = 0x0000000104066cf4, resolved, hit count = 0 

3: regex = 'co.touchlab.kampkit.models.BreedRepository#getBreeds\(', locations = 1, resolved = 1, hit count = 1
  3.1: where = shared`kfun:co.touchlab.kampkit.models.BreedRepository#getBreeds(){}kotlinx.coroutines.flow.Flow<kotlin.collections.List<co.touchlab.kampkit.db.Breed>>, address = 0x00000001040657d0, resolved, hit count = 1 

4: regex = 'co.touchlab.kampkit.models.BreedRepository#updateBreedFavorite\(', locations = 1, resolved = 1, hit count = 0
  4.1: where = shared`kfun:co.touchlab.kampkit.models.BreedRepository#updateBreedFavorite(co.touchlab.kampkit.db.Breed){}, address = 0x0000000104066e0c, resolved, hit count = 0 

5: regex = 'co.touchlab.kampkit.models.BreedRepository#isBreedListStale\(', locations = 0 (pending)

6: file = '/Users/jkuchar1/Documents/www/ivy/KaMPKit/shared/src/commonMain/kotlin/co/touchlab/kampkit/models/BreedRepository.kt', line = 33, exact_match = 0, locations = 0 (pending)

7: file = '/Users/jkuchar1/Documents/www/ivy/KaMPKit/shared/src/commonMain/kotlin/co/touchlab/kampkit/models/BreedRepository.kt', line = 34, exact_match = 0, locations = 0 (pending)

8: file = '/Users/jkuchar1/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.6.4/f83eca53d0ca9fb7b4b21980e52354d443a3e354/kotlinx-coroutines-core-1.6.4-sources.jar!/commonMain/flow/Builders.kt', line = 61, exact_match = 0, locations = 0 (pending)

9: file = '/Users/jkuchar1/Documents/www/ivy/KaMPKit/shared/src/commonMain/kotlin/co/touchlab/kampkit/models/BreedRepository.kt', line = 51, exact_match = 0, locations = 0 (pending)

10: file = '/Users/jkuchar1/Documents/www/ivy/KaMPKit/shared/src/commonMain/kotlin/co/touchlab/kampkit/models/BreedRepository.kt', line = 55, exact_match = 0, locations = 0 (pending)
There is nothing much to dump for the kotlin code…
@Kirill Shmakov [JB] If you wish I would be happy to schedule a call and try to debug this issue together. We can turn this into consultation, if you wish.
And I’m reproducing this issue on latest stable Android Studio. In the morning I have tried bleeding-edge, with even more broken results. ref: https://kotlinlang.slack.com/archives/C3PQML5NU/p1673864704384189?thread_ts=1673631195.903349&amp;cid=C3PQML5NU
k
There is nothing much to dump for the kotlin code…
Could you please dump line table for the module in which KaMPKit is being compiled? I meant binary. Should be one.
Do you have Spotlight turn on on you machine?
j
How to dump whole module?
Yes, I use Spotlight.
k
Copy code
image dump line-table /path/to/binary
You could see binary location via
Activity Monitor
(in case it is about simulator). It should be LLDBFrontend process.
j
Copy code
(lldb) image dump line-table /Users/jkuchar1/Library/Developer/CoreSimulator/Devices/D12F0F38-9CAF-4195-BC36-C1ADA63586C6/data/Containers/Bundle/Application/25B9DF61-1ECC-49CF-9B2C-88BB89DAB564/KaMPKitiOS.app/KaMPKitiOS
warning: No source filenames matched '/Users/jkuchar1/Library/Developer/CoreSimulator/Devices/D12F0F38-9CAF-4195-BC36-C1ADA63586C6/data/Containers/Bundle/Application/25B9DF61-1ECC-49CF-9B2C-88BB89DAB564/KaMPKitiOS.app/KaMPKitiOS'.
error: no source filenames matched any command arguments
So I tried to find “shared” binary, that should be output of the kotlin compilation process, and…
Copy code
(lldb) image dump line-table shared
Line table for /Users/jkuchar1/Documents/www/ivy/KaMPKit/ios/shared in `KaMPKitiOS
: :4294967295
I can see, that there are debug symbold available (dSym with DWARF file)
k
Did LLDBFrontend process pick this symbols file? If not, it would fail to locate breakpoints.
j
How could I check that?
Probably yes?
k
If you stop the running ios app now and try to launch it again, will breakpoints work? my idea is dsymbols were not generated before first launch of the app.
j
k
Ok, so I see dSYM-s for the app, but not for the binary responsible for the library. Here is how it looks in some example app.
What would be
image dump line-table KaMPKitiOS
and
image dump line-table shared
?
j
I does not see the shared debug symbols loaded, even they are available on disk.
Copy code
(lldb) image dump line-table KaMPKitiOS
warning: No source filenames matched 'KaMPKitiOS'.
error: no source filenames matched any command arguments
(lldb) image dump line-table shared
Line table for /Users/jkuchar1/Documents/www/ivy/KaMPKit/ios/shared in `KaMPKitiOS
: :4294967295
k
Here is may be more convenient way to check.
target modules list
Shows all the binaries involved in application run. If symbols are associated with binary file, they are shown right after it. Normally, symbols should be encountered by Spotlight, which then informs LLDB. If symbols are located in directory not indexed by Spotlight than it will not find them.
j
There are both “found” [0] for the app and [4] for shared
Copy code
(lldb) image dump line-table shared
Line table for /Users/jkuchar1/Documents/www/ivy/KaMPKit/ios/shared in `KaMPKitiOS
: :4294967295
(lldb) target modules list
[  0] AB6F8E0F-87E6-3B2A-B63F-DD3665FF9BEA 0x0000000104008000 /Users/jkuchar1/Documents/www/ivy/KaMPKit/build/ios/Debug-iphonesimulator/KaMPKitiOS.app/KaMPKitiOS 
      /Users/jkuchar1/Documents/www/ivy/KaMPKit/build/ios/Debug-iphonesimulator/KaMPKitiOS.app.dSYM/Contents/Resources/DWARF/KaMPKitiOS
[  1] 487CFDEB-9B07-39BF-BFB9-970B61AEA2D1 0x00000001041bc000 /usr/lib/dyld 
[  2] D763ECDD-458A-3DC0-92D4-BF4F7A54BFBB 0x00000001040e0000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim 
[  3] CA7DB440-78B7-3D9E-8BA8-D1657150F8B9 0x000000018026c000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libc++.1.dylib 
[  4] EFCFC5D6-061D-3065-BD55-037C46504842 0x000000010516c000 /Users/jkuchar1/Library/Developer/CoreSimulator/Devices/D12F0F38-9CAF-4195-BC36-C1ADA63586C6/data/Containers/Bundle/Application/858F2678-A44B-49FB-BFAC-B2E97DE21BC3/KaMPKitiOS.app/Frameworks/shared.framework/shared
But the [4] does not have the DWARF file found. Will check the Spotlight. I have some problems with it as it reindexes the whole thing from time to time.
Ok, I have found that it was in location ignored by Spotlight as developer files are usually millions of small files. Moved it out of it, waited a bit. For some reason it still does not find the DWART file for [4]
Copy code
(lldb) target modules list
[  0] F50E4B9C-84E7-38A5-AA07-40638ECAC5E0 0x0000000100700000 /Users/jkuchar1/Documents/dev-mobile/ivy/KaMPKit/build/ios/Debug-iphonesimulator/KaMPKitiOS.app/KaMPKitiOS 
      /Users/jkuchar1/Documents/dev-mobile/ivy/KaMPKit/build/ios/Debug-iphonesimulator/KaMPKitiOS.app.dSYM/Contents/Resources/DWARF/KaMPKitiOS
[  1] 487CFDEB-9B07-39BF-BFB9-970B61AEA2D1 0x0000000100a18000 /usr/lib/dyld 
[  2] D763ECDD-458A-3DC0-92D4-BF4F7A54BFBB 0x0000000100900000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim 
[  3] CA7DB440-78B7-3D9E-8BA8-D1657150F8B9 0x000000018026c000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libc++.1.dylib 
[  4] 829B57BB-E142-3A03-A1DE-8818C76B3BB3 0x00000001019c8000 /Users/jkuchar1/Library/Developer/CoreSimulator/Devices/D12F0F38-9CAF-4195-BC36-C1ADA63586C6/data/Containers/Bundle/Application/F40CD066-0136-4DA0-A600-E151B174D2AA/KaMPKitiOS.app/Frameworks/shared.framework/shared 
[  5] 98600970-6567-3882-9999-72F0A1413B46 0x000000018270a000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libsqlite3.dylib 
[  6] 4A53F446-37DF-36ED-AAF0-242BF3845FC0 0x0000000180710000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation
Normally, symbols should be encountered by Spotlight, which then informs LLDB. If symbols are located in directory not indexed by Spotlight than it will not find them
This is game changer. As I always exclude source code from indexing. But I have checked that it was not the case on this machine. To make sure I have moved the project out of the location. Clean built. Still could not debug. + tried to reindex relevant folders by Spotlight
Ohh! After dosing some other work and returning in the evening it started to work. So it was probably because of the Spotlight index NOT including the project folder. 🤦‍♂️ I would highlight this in red in documentation, as there is almost no way you can find out that it is because of the Spotlight (which seems to be completely unrelated).
image.png
It looks like highly unrealible behaviour. In KaMPKit it started working after having for few hours Android Studio off. However in my project I’m still stuck with the same problem. Look at line [4]: Therefor still in the disassembly view.
Copy code
(lldb) target modules list
[  0] 3FFB4878-0126-38AD-A6CC-E32D0FCEB720 0x0000000102918000 /Users/jkuchar1/Documents/dev-mobile/ivy/app/build/ios/Debug-iphonesimulator/Ivy <http://Assistant.app/Ivy|Assistant.app/Ivy> Assistant 
      /Users/jkuchar1/Documents/dev-mobile/ivy/app/build/ios/Debug-iphonesimulator/Ivy Assistant.app.dSYM/Contents/Resources/DWARF/Ivy Assistant
[  1] 487CFDEB-9B07-39BF-BFB9-970B61AEA2D1 0x000000010586c000 /usr/lib/dyld 
[  2] D763ECDD-458A-3DC0-92D4-BF4F7A54BFBB 0x00000001057ac000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim 
[  3] CA7DB440-78B7-3D9E-8BA8-D1657150F8B9 0x000000018026c000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libc++.1.dylib 
[  4] 2FE2D7A2-FB62-3A28-BE33-10DE18E58271 0x0000000106f84000 /Users/jkuchar1/Library/Developer/CoreSimulator/Devices/E939F22D-281E-4381-9FC0-7E9F65656051/data/Containers/Bundle/Application/255E1D84-3B4D-455C-AA99-47739078D71F/Ivy <http://Assistant.app/Frameworks/ivy.framework/ivy|Assistant.app/Frameworks/ivy.framework/ivy> 
[  5] 734A44A7-FC7C-363F-8B5F-97AF2BB7ACB7 0x00000001a1933000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libz.1.dylib 
[  6] 371A6454-EBB8-34C7-A78F-B29CFB7339CC 0x0000000105d74000 /Users/jkuchar1/Library/Developer/CoreSimulator/Devices/E939F22D-281E-4381-9FC0-7E9F65656051/data/Containers/Bundle/Application/255E1D84-3B4D-455C-AA99-47739078D71F/Ivy <http://Assistant.app/Frameworks/SmartlookAnalytics.framework/SmartlookAnalytics|Assistant.app/Frameworks/SmartlookAnalytics.framework/SmartlookAnalytics> 
[
…and also debugging of Kotlin code in Xcode started to work (in the example app). In my app for some reason lldb still does not see the dSym file.
I gave it a night to update the Spotlight index (even there were not indicator that there were something indexing) and it started to pick up the debug symbols and started to work in my project. However, this is so much unpredictable behaviours in there. Isn’t there any deterministic way how to hint the lldb from IDE to load symbols that IDE already knows about? Or shouldn’t the IDE just run
target modules list
and determine if all modules from the IDE has dSym files available? If not provide link to documentation discussing the Spotlight issue? How to update the index and how to check that my dSym files are in index available?
k
Thank you for the investigation! I will think how to support users here cc @Ilia Solovei
j
@Ilia Solovei Let me know if you need any further information or assistance. (preferably on my e-mail/mobile – contacts are on https://jankuchar.cz)
m
Do you have updates on this? @Konstantin Tskhovrebov @Ilia Solovei I think I encountered the same issue, debugging started working after some time
k
Do you use the latest kmm plugin? Because the issue was fixed some time ago
m
Should I try 1.9.0 kmm plugin? I was not able to resolve it in AS (also selecting the eap channel)
k
I mean kmm plugin in Android studio. Show your
kdoctor -v
output
m
Copy code
➤ Android Studio (AI-222.4459.24.2221.10121639)
    Location: /Users/mss/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-2/222.4459.24.2221.10121639/Android <http://Studio.app|Studio.app>
    Bundled Java: openjdk 17.0.6 2023-01-17
    Kotlin Plugin: 222-1.8.20-release-AS3739.54
    Kotlin Multiplatform Mobile Plugin: 0.6.0(222)-13
  i Note that, by default, Android Studio uses bundled JDK for Gradle tasks execution.
    Gradle JDK can be configured in Android Studio Preferences under Build, Execution, Deployment -> Build Tools -> Gradle section
Breakpoints are working, but everything was an
ObjHeader *
After some hours we were able to see the proper types in the debugger Evaluate expression was anyway not working KT-59532
k
The bug was fixed in 0.5.2
All should be fine
1362 Views