Brifing

Android ART Debugging Fix with a Hook-Based Solution

A temporary library resolves JDWP deadlock issues, making debugging smoother.

ProAndroidDev - Medium2 min

1ART Debugging Issue

On Android 16, attaching a Java debugger (JDWP) to large apps causes process lockups and ANR errors. This stems from a deadlock between the garbage collector thread and ART's native debug layer. While the ART team acknowledges the bug, a mainline fix will take time.

2Need for a Workaround

Developers need immediate debugging capabilities without waiting for the fix. Previous workarounds either altered the environment or hindered seamless debugging in monolithic apps. A new approach aims to solve the issue directly within ART.

3JdwpSilencer Library

This library uses a native hook to suppress JDWP events inside ART, preventing the deadlock window from opening. It allows developers to debug large apps effortlessly, with the debugger attaching smoothly as if nothing happened.

4Technical Details

The deadlock triggers when CLASS_PREPARE, THREAD_END/THREAD_START, and GC pressure overlap during debug attachment. JdwpSilencer reduces pressure by silencing ThreadStart and ThreadDeath events, which are only used for debugger bookkeeping and don’t affect app logic.

5Native Hook Implementation

The solution intercepts art::RuntimeCallbacks methods, replacing them with no-op functions. Using the ShadowHook library, inline hooks are applied to libart.so symbols. This silences ThreadStart and ThreadDeath events, eliminating deadlock risks.

6Class Preparation Challenge

Initially, ClassPrepare and ClassLoad events were also silenced, but this broke breakpoints on unprepared classes. The final version only suppresses ThreadStart and ThreadDeath for a safer solution.

7Kotlin API Design

JdwpSilencer.kt offers four core functions: init(), isInitialized(), enabled, and debugLogs. This API simplifies hook setup and management. The enabled parameter allows dynamic toggling of event suppression.

8Kill Switch Feature

Some developers faced debugger hangs at the first breakpoint. The enabled parameter can be set to false to disable suppression dynamically, providing flexibility for different scenarios.

9Library Benefits

JdwpSilencer enables debugging without altering app structure. Native code modifications resolve the issue without performance overhead. Developers can continue their work seamlessly until the mainline fix arrives.

10Limitations

The solution may not work perfectly in all cases. Silencing ClassPrepare/ClassLoad events could limit some debugger features, but ThreadStart/ThreadDeath suppression suffices for most scenarios.

11Next Steps

Until the ART team releases the main fix, JdwpSilencer serves as a lifeline for developers. Its open-source nature allows community-driven improvements and adaptations.

12Conclusion

JdwpSilencer provides a practical solution for Android developers to bypass deadlock issues. Integrating this library can save time and maintain productivity while awaiting the official fix.

Original article: ProAndroidDev - Medium — Android ART Hata Ayıklama Sorununu Çözen Kancalı Çözüm