This article mainly explains the usage of a variety of Android debugging tools.
1. View the current stack
1) Function: Add code to the program so that you can see the printed current function call relationship in logcat.
2) Method:
new Exception(“print trace”).printStackTrace();
2. MethodTracing
1) Function: used for hot spot analysis and performance optimization, analyzing the CPU time, number of calls, function call relationships, etc. of each function.
2) Method:
a) Add a trace switch to the program code:
import ; …… (“/data/tmp/test”); // Create the /data/tmp directory first…… // The tracked program segment();
b) After compilation and running, the device side generates the /data/tmp/ file.
c) Copy the trace file to the PC side
$ adb pull /data/tmp/ ./
d) Use Android's own tools to analyze trace files
$ $ANDROID_SRC/out/host/linux-x86/bin/traceview
At this time, you can see the number of times each function is called, the CPU usage and other information.
e) Use Android's own tools to analyze and generate call relationship class diagrams
$ apt-get install graphviz # Install picture-related software
$ANDROID_SRC/out/host/linux-x86/bin/dmtracedump -g
At this time, a class diagram is generated in the directory
3) Attention
The trace file generation conflicts with the DEBUG version of the libdvm module, so this method is only suitable for debugging of non-DEBUG version simulators, otherwise an error will be reported when analyzing the trace file.
3. HProf (Heap Profile)
1) Function:
Used for memory analysis at the Java level, displaying detailed memory usage information, and pointing out suspicious memory leak objects.
2) Method:
a) Add dump action to the code:
import ; import ; …… try { (“/data/tmp/”); // Create the /data/tmp directory first} catch (IOException ioe) { }
b) Copy the hprof file to the PC side
$ adb pull /data/tmp/ ./
c) Use the command hprof-conv to convert hprof to standard hprof recognized by MAT
$ $ANDROID_SRC/out/host/linux-x86/bin/hprof-conv
d) Use MAT tool to view hprof information
Download MAT tool: /mat/
Open with tools
3) Note: This tool can only display Java-level, but cannot display memory usage information at the C-level.
4. SamplingProfile (used on Android 2.0 version)
1) Function
Sample the currently running function every N milliseconds and output it to the log.
2) Add sampling settings to the code:
import …… SamplingProfile sp = (); (n); // n is the setting number of samples per second(()); …… ();
It will enable a thread monitoring and print information in logcat.
5. Use the system signal to retrieve the current stack status and memory information
1) Principle
The dalvik virtual machine processes SIGQUIT and SIGUSR1 signals (dalvik/vm/), and completes the functions of getting the current stack and getting the current memory situation respectively.
2) Usage
a) $ chmod 777 /data/anr -R # Set anr directory permissions to write
$ rm /data/anr/ # Delete previous trace information
$ ps # Find the process number
$ kill -3 Process number # Send SIGQUIT signal to the process, and generate trace information at this time
$ cat /data/anr/
Function implementation: traverse thread list(dalvik/vm/:dvmDumpAllThreadEx()) and print the current function call relationship (dalvik/vm/interp/:dumpFrames())
b)$ chmod 777 /data/misc -R
$ ps # Find the process number
$ kill -10 Process number # Send SIGQUIT message signal to the process, and generate hprof information at this time
$ ls /data/misc/*.hprof
At this time, the hprf file is generated. How to use this file, see the second part (HProf)
Note: The hprof files are very large. Please delete them immediately after using them to avoid filling the memory.
6. logcat and principle
1) Use println's standard java to output words and sentences, and add the prefix I/V/D….
2) Dalvik uses the method of adding threads to redirect stdout and stderr to management using dup2.
(vm/:dvmstdioConverterStartup), and then start a thread to read the content from the other end of the pipeline (dalvik/vm/:stdioconverterThreadStart()), and use the LOG public tool (system/core/liblog/logd_write.c: __android_log_print()) to output it to /dev/log/*.
3) Logcat uses different parameters to view different input information under /dev/log/
# logcat -b main Display information in the main buffer
# logcat -b radio displays information in wireless buffer
# logcat -b events Display information in the event buffer
7. jdwp(java debug wire protocol) and principles
1) The virtual machine (device side) loads Agent JDWP at startup, thus having debugging functions. On the debugger side (PC side), the device is connected through the JDWP protocol, and the state is obtained and the execution of Java programs is controlled by sending commands. JDWP communicates through commands and reply.
2) Debugging tool in JDK jdb is a debugger, and DDMS also provides debuggers to connect to the device.
3) dalvik provides two connection methods for JDWP: tcp method and adb method. The tcp method can manually specify ports, and the adb method is automatically set to port 8700. Usually, using DDMS debugging is through adb method.
8. monkey
1) Monkey is a command line tool that comes with Android. It sends a pseudo-random user event stream to the system, implementing stress testing of the application being developed.
2) Method
Open the setting interface on the device side
$ adb shell
# monkey -p -v 500
At this time, you can see that the interface is constantly being switched.
9. Other gadgets
See the tools provided in details.
1) Take the time in the order of nanoseconds to calculate the time
threadCpuTimeNanos()
2) Statistics the memory allocation between two points:
startAllocCounting() stopAllocCounting() getGlobalAllocCount() get…..
3) Print the currently loaded class
getLoadedClassCount()
printLoadedClasses() It needs to turn on the NDEBUG function to turn on the Log function in system/core/
10. Print debug information
$ adb bugreport
The above is the information sorting out the Android debugging tool information. We will continue to add relevant knowledge in the future. Thank you for your support for this site!