A: Java program debugging principle:

Java this kind of upper language compiles the result is bytecode, bytecode needs the JVM to interpret and execute, so the debugging Java is specific and the JVM communication problem, the general IDE for Java program debugging function is the PACKAGING of JDB, about JVM debugging system online there are many articles, such as: Juejin. Cn/post / 688739… No further elaboration

Ii. Native program debugging principle:

Native code contains CPU instructions of the corresponding platform and is directly run by CPU. Debugging of native code requires CPU support (such as INT3 soft interrupt instruction), and assistance of operating system (such as Linux ptrace system call), LLDB, GDB, IDA android_server of debugger is based on the realization of the function of the above, there are details, such as: zhuanlan.zhihu.com/p/336922639, not here

C: Class, Dex, Elf three kinds of file instructions and source code correspondence description structure:

1: class bytecode and source line number

2: Dex bytecode and source line number corresponding description structure:

3: ELF instruction and source line number correspondence description structure:

4: summary

When the class has no line number, you can only decompile and debug the class instruction

When dex has no line number, it can only decompile and debug the SMali instruction

When ELF has no line numbers, it can only disassemble debugging assembly instructions

4. Debug Android Studio

AS is essentially a Java program, and debugging AS is debugging a Java program

1: Enables the AS to start in Debug mode

After DMG installation package is installed, executable program path: / Applications/Android Studio. The app/Contents/MacOS/Studio because this is a MAC executable file, the program inside and start a Java program, and introduced into Android Studio jar path and related parameters, There is no way to pass Java parameters directly through this, but AS provides a VM configuration file whose contents are read at startup and added to Java parameters: Applications/Android Studio. The app/Contents/bin/Studio. Vmoptions plus: -agentlib: JDWP =transport=dt_socket,server=y,suspend=n,address=6006 Double-click directly/Applications/Android Studio. The app/Contents/MacOS/Studio program start the AS, can be observed that the output of the terminal:

The JVM is ready to be attached by the debugger

2: Debug configuration:

JDWP protocol implementation of the program can be used as a debugger, of course, there is no reason to make a own, with JDB command line operation is too cumbersome, manual source management is also very laborious, it is better to use a complete package of IDE, here using Idea, create a Remote JVM Debug type configuration. The configuration is shown as follows:

3: Import code

Source come from, you can go to download, even can compile a ourselves AS reference: tools.android.com/build/studi… However, most of the time, we just want to Debug, so we don’t want to bother so much. We can directly import the JAR package of AS into Idea, and use the decompilation and debugging functions of Idea to complete our goal (there are many JARS in each directory of the program package of AS, and import which one we need, AS shown below) :

Import Idea: create a Java project in Idea, create a folder casually, copy the required JAR in the past, right-click jar ->Add as Library

4: Starts debugging

The first step is to find the Class or method involved in the function to be debugged, either by searching the string in the JAR, or by trying to feel the Class breakpoint of the relevant name. In the IDEA Plugin framework, most plugins rely on Action for their entry. You can break some of the Action methods

Debug Gradle

Gradle is essentially a Java program. Debugging Gradle is debugging a Java program

1: Configure Gradle to start in Debug mode

Gradle. jvmargs= -xdebug -xrunjDWp :transport=dt_socket,server= Y,suspend= Y,address=5005

Starting Gradle, such as Assemble Task, as shown below, waits for the debugger link to resume due to suspend=y set above

2: debugs the configuration

Idea debugging is used here, and the configuration is the same AS AS debugging:

3: Import code

You can download the source code and even build Gradle yourself at github.com/gradle/grad… We can use the decompilation and Debug functions of Idea to complete our goal. The jar location of Gradle and Gradle Plugin is as follows:

Gradle program location

The lib directory is a compiled JAR. If the jar is of gradle-{version}-all type, the SRC directory contains the corresponding source code, which can be imported for debugging

Gradle plugin location

Search for the target plug-in in the folder shown below:

For example, I want to search for Android Gradle Plugin:

Import Idea: create a Java project in Idea, create a folder, copy the jar needed in the past, right-click jar ->Add as Library, as the same as as, no further details

4: Starts debugging

The first step is to find out which class or method is involved in the function you want to debug

Debug any App Java layer

Edit aOSP brush machine, the type to choose userDebug or Eng, such a system has root permission and global debugging, how to compile a lot of information online, if the middle of the problem can also refer to my writing aOSP compile pit, do not repeat

If it is a third-party App, you need to decomcompile dex for Java source code import AS debugging, if the line number is not always adjusted, indicating that the line number information is confused or removed, this time you can consider decomcompiling into SMali, using AS+ Smalidea plug-in debugging smali code, there are a lot of information online. Such as: blog.csdn.net/YJJYXM/arti… If AS is unable to set breakpoints on smALI files, see blog.csdn.net/qq_43278826…

Seven: Debug any App Native layer

Edit aOSP brush machine, the type to choose userDebug or Eng, such a system has root permission and global debugging, how to compile a lot of information online, if the middle of the problem can also refer to my writing aOSP compile pit, do not repeat

Since so in the third-party app removes debugging information and we do not have the corresponding source code, we can only disassemble debugging. I am generally used to IDA, and there are many materials on the Internet, such as: blog.csdn.net/Breeze_CAT/… IDA debug: bbs.pediy.com/thread-2654…

Debug the Java layer of Android system

Edit aOSP brush machine, the type to choose userDebug or Eng, such a system has root permission and global debugging, how to compile a lot of information online, if the middle of the problem can also refer to my writing aOSP compile pit, do not repeat

Import the compiled code in the AS (you can refer to www.jianshu.com/p/2ba5d6bd4.) Or, if necessary, replace the source code in the Android SDK with the source code for the compiled system (which I did), noting that the targetSdk version is the same as the compiled system version

Nine: Debug the Android system Native layer

Edit aOSP brush machine, the type to choose userDebug or Eng, such a system has root permission and global debugging, how to compile a lot of information online, if the middle of the problem can also refer to my writing aOSP compile pit, do not repeat

To use AS AS a debugging environment, see: weishu. Me /2017/01/14/…