We know how to download and compile the source code of the Android 7.0 system.

In order to further study and study the Android system source code, today we will talk about how to debug the Android system source code, only learned how to debug the system source code, to help us read and understand the source code more efficiently.

As we know, the Android Framework code is mainly composed of Java, C++ and other code, therefore, for the system source debugging, we here will be divided into two parts

  • Java related code debugging
  • C++ Native code debugging

First, Java related code debugging

For the debugging of Java-related code, we mainly use Android Studio development tools here.

To debug the source code in Android Studio, the first step is to import the system source code into Android Studio.

####1. Compile idegen for Android source code import, Google official to provide us with a very convenient tool idegen

It is in the system source path we downloaded:

developement/tools/idegen
Copy the code

To quote README

IDEGen automatically generates Android IDE configurations for IntelliJ IDEA and Eclipse.

The Idegen tool automatically generates configuration files for Android development tools (Android Studio and Eclipse).

In this case, let’s use the Idegen tool to generate the configuration files needed to import the source code.

First open the command line tool, CD to go to the source directory,

Execute the following commands:

Initialize command tool
soruce build/envsetup.sh 
# Compile the idegen module to generate idegen.jar
mmm development/tools/idegen/
Generate configuration files for Android development tools
sudo ./development/tools/idegen/idegen.sh
Copy the code

After executing the above instructions, the following three files are generated in the source path

Android. Ipr: project-related Settings, such as compiler configuration, entry, associated libraries, etc.

Android. iml: describes modules, such as paths to modules, dependencies, etc.

Android. Iws: includes some personal workspace Settings.

####2. Import source code Next we can start importing source code.

If you are importing source code for the first time, Android Studio can be a huge memory hog, so we need to set up our VM options.

If Linux device in Android Studio bin/studio64. Add – Xms748m – Xmx748m vmoptions file,

If you’re using a Mac, add it in the Contents/ info.plist directory of the AS directory.

Due to the huge amount of Android system source code, it takes a long time to load Android Studio in one go

So, before we start importing, we can open the Android. iml file and adjust the source code to suit our needs.

Here,

represents the directory that does not need to be loaded. We can use the

tag to add the corresponding directory address according to our own needs.

Next, select File -> open to select the Android. ipr File and open it

Android Studio will then start loading the source code

Without adding or modifying the

, this takes a long time to load, and after a period of waiting, the code is loaded, as shown in the figure below:

The directory in red is excluded and will be filtered out when the scan index is loaded.

After loading the source code, we can also right-click the Module option in the Project Structure to exclude the source directory that does not need to be loaded, as shown in the following figure:

####3. Configure code dependencies to ensure correct code hop

In order to read and debug code to ensure that the code jumps correctly, we need to configure the dependencies.

< span style = “box-sizing: border-box; color: RGB (51, 51, 51); line-height: 21px; font-size: 14px! Important; word-break: inherit! Important;” Add the external and Frameworks dependencies to the source code, as shown below:

The next step is to set up the SDK and make sure to associate the corresponding SDK version with the system version

### Start debugging source code

Before debugging, the SDK of Project should be set. Under File -> Project, open Project Structure and select Project to set the SDK of the corresponding version, which is consistent with the system version:

Make sure Android Studio allows ADB debugging

Next, open the Android emulator as described in the previous article

Click the Attach Debugger to Android Process button in the Android Studio toolbar, and the Choose Process window will open. We select the process according to the code we need to debug:

Suppose we want to debug the source code for Android’s native browser. As shown in the figure, we set a breakpoint in the loadUrlFromUrlBar method in its entry file WebViewBrowserActivity.

Click on WebViewBrowser to open the app

Click Attach to Android Process button to open Choose Process, you can see the process webViewBrowser is running, select, OK

Then we enter the URL in the URL input field of the APP to jump

As you can see in the figure, the code has successfully entered the breakpoint, and we are then free to debug the Java code as we wish.


For the Framework Native code, we use the GDB tool for debugging.

### What is GDB? It is a GNU project debugging tool, its function is very powerful, can be used to debug C, C++, object-C, Pascal and other languages written projects.

For those of you who are used to visual ides, perhaps the biggest drawback is that it does not support graphics

But the instructions provided by GDB are very flexible

  • You can start whatever you want,
  • You can set breakpoints as you want,
  • You can view variables at breakpoints, code information
  • You can see the call stack the program is running on

Once you get used to it, you can fly!

Under normal circumstances, using GDB to debug Android source code need to install GBdServer attach on the Android device associated with the process we need to debug, and then use GDB instructions to connect to gDBServer for debugging

However, the official provides us with gDBClient tool, which allows us to debug GDB easily.

Start GDB debugging

Here we are based on gDBClient to carry out the actual GDB debugging demonstration:

As with Java debugging above, we still use the system’s own browser as an example.

####1. Click the startup icon to open the browser APP:

####2. Open a command line terminal, go to the system source directory (my source directory is aosp), and initialize the command tool:

Enter the source path
cd aosp
Initialize command tool
source build/envsetup.sh
# Select the version of the compiled source code, refer to the previous article
lunch
Copy the code

####3. Use the ADB command to find the PID of the process to debug

Use shell ps command to find related processes, grep search filter webView processes
adb shell ps -A | grep webview
Copy the code

As shown in the figure, 2157 is the PID of the process where the browser APP resides

####4. Use the gdbclient command tool to enable GDB to debug PID processes

# gdbClient 
      
        can enable GDB debugging corresponding PID process
      
gdbclient 2157
Copy the code

The GDB debugging command interface is displayed

####5. Run the GDB b command

In the GDB directive, we use b < code file > : line number to set breakpoints.

Here we choose frameworks/base/libs/hwui renderthread/DrawFrameTask. CPP code file drawFrame method on breakpoints, located 71 line file:

This method is mainly used to draw frames and is triggered when the interface of the browser app changes.

We enter the set breakpoint command:

b frameworks/base/libs/hwui/renderthread/DrawFrameTask.cpp:71
Copy the code

Input command shows Breakpoint at 0 x7f69e9892c11 2: after the file frameworks/base/libs/hwui renderthread/DrawFrameTask. CPP, line 71. Our breakpoint is set successfully.

####6. On the CLI, enter c to start listening

C indicates “continue”. In this case, “Continuing” is displayed

When we click on the emulator, we will trigger the interface change and enter the code breakpoint for drawing frames:

As shown, the breakpoint is entered, which means that our code has been debugged successfully.


We’re just showing you the general process here,

Debugging GDB code requires that you have some familiarity with the source code and know which process calls which file methods.

At the same time, we also need to be familiar with the various commands of GDB, here to recommend a good introduction to the article, can quickly start:

GDB 10 minute tutorial

As a side note, if you want to listen when a process starts, you can use the following command to associate the directory, get the PID, and debug it through gdbClient

adb shell gdbserver :5039 /system/bin/my_test_app
Process my_test_app created; pid = 3460
Listening on port 5039
Copy the code
gdbclient <app pid>
Copy the code

If you want to debug C++ code for the Framework using Android Studio, you can also refer to the following two articles, but I think this approach has some limitations.

How to debug the Android Native Framework

Debug Framework layer code with Android Studio


As stated at the beginning of the article, only by learning how to debug the Framework source code, can help us better learn the Android Framework, I hope this article can give you some help, if there is a better debugging method, welcome to give me a message!