Search code examples
androidandroid-studiodebuggingandroid-emulatorandroid-source

How to run debugger across processes and layers in AOSP?


I have an Android app and a local build of AOSP with custom changes to the Framework and HAL layers. I am familiar with debugging a single Android app. However, I am interested in debugging an API call from the Android app through the Framework Layer up to the native HAL layer. I'm also interested in debugging this using the emulator with my custom AOSP changes.

What is the best way to debug API calls starting from the Android app through various layers in the AOSP stack? Is it possible to do this via CLI? Which tools are recommended or is Android Studio sufficient? I assume this would probably cross several different processes/services:

  • Android App
  • Framework Layer
  • HAL Layer
  • etc.

I am interested in doing this to understand the entire flow starting from my Android app's API calls but I am also interested in doing this to simply get a general understanding of the call sequence in AOSP from the app -> framework -> native HAL layers. I figure this might be helpful way to familiarize with the OS in general.


Solution

  • Debugging AOSP is definitely possible assuming you have made an eng or userdebug build of AOSP already. If you are wanting to follow what happens in multiple processes that is also possible but will require running multiple debuggers simultaneously and you will need to figure out which process to debug which is a bit beyond the scope of this answer, hopefully you've got a rough idea already of which processes are involved.

    Use the following commands to get an idea of all processes running:

    adb root
    adb shell ps -A
    

    Debugging Java/Kotlin vs native code require totally different steps.

    Debugging Java/Kotlin AOSP code

    Surprisingly Google seems to have missed documentation for this.

    Create an IntelliJ project from existing sources and choose the root directory where AOSP is cloned but cancel the source code scan so that no source is found. In project structure choose a regular JDK of the same level as is supported the version of AOSP you are working on--lately that's probably Java 11--but create a copy in IntelliJ of the JDK with all the bundled JARs removed from the classpath by clicking the red minus sign, give this JDK a name such as "1.8 (No Libraries)". This is because AOSP has its own java standard library in source code form already. Annoyingly these days a warning will appear over the editor saying the JDK is invalid but it works anyway so ignore it.

    Create a single new module named "android" which is not in a subdirectory but which simply sits in the root of AOSP directory. Intellij will create a subdirectory named "src" at the root of your project which you can delete.

    To improve Intellij performance, in Modules section for the project under Sources tab mark the following directories as "Excluded" (red) icon, note that some directories may not be present depending on the version of android:

    • .repo
    • out
    • prebuilts
    • ndk
    • developers
    • development
    • toolchain
    • external

    In the Modules sections for the project under the Sources tab click the "Sources" (blue) icon to add these (may vary by platform). You do not absolutely need to add every java source directory in AOSP, just the ones you want to work on and debug. You can also right click the folder in the Project view and select "Mark Directory As... Sources Root".

    Some of the most useful java source directories to add are:

    • frameworks/base/core/java
    • frameworks/base/graphics/java
    • frameworks/base/services/java
    • frameworks/base/services/core/java
    • libcore/luni/src/main/java
    • libcore/ojluni/src/main/java
    • libcore/dalvik/src/main/java

    To debug Java/Kotlin apps in IntelliJ you need to use the Android monitor tool to establish a Java remote debugging connection. If you have installed Android Studio already you can find this tool at Android/Sdk/tools/monitor. Launch Android monitor like so from the command line. As of writing May 2023 you still need to have Java 1.8 on your path to run this tool:

    monitor &> /dev/null &
    

    Highlight the desired AOSP app process to debug (note the most critical process in AOSP is called system_server so that might be what you are looking for) and you will see the 8700 port is added to it. In IntelliJ do the following to setup a remote debugging session:

    • Go to Run > Edit Configurations...
    • Click Remote and then press the + button
    • Change the name to Remote8700 and change the port to 8700
    • Click Apply and Close button

    From now on you just do Run > Debug... > Remote8700 and Intellij will connect to the process selected in monitor. You can actually debug multiple processes simultaneously by setting up multiple sessions to the Java debugging ports shown in the Android monitor tool

    Debugging Native Code

    AOSP source site has documented this part pretty well here: https://source.android.com/docs/core/tests/debug/gdb

    Tools included with AOSP let you debug via command line with gdb/lldb and now VS code.