Android Debugging tool Usage details _android

Source: Internet
Author: User
Tags chmod

This article is mainly for you to explain the use of a variety of Android debugging tools.

1. View the current stack

1 function: Add code in the program so that you can see the current function call relationship printed out in Logcat.

2) Method:

New Exception ("Print Trace"). Printstacktrace ();

2. methodtracing

1 function: For hot spot analysis and performance optimization, analyze each function CPU time, call times, function call relationship, etc.

2) Method:

A The tracking switch is added to the program code:

Import Android.os.Debug;  
..... Android.os.Debug.startMethodTracing ("/data/tmp/test"); First build the/data/tmp directory  
...//The tracked program segment  
android.os.Debug.stopMethodTracing (); 

b) Compile, run, the device side to generate/data/tmp/test.trace files.

c to copy the trace file to the PC side

$ adb pull/data/tmp/test.trace./

d to analyze the trace file using the Android self-tape tool

$ $ANDROID _src/out/host/linux-x86/bin/traceview Test.trace

At this point you can see information such as the CPU occupancy rate of each function being invoked.

E Use the Android Self-band tool to analyze the build call Relationship class diagram

$ apt-get Install Graphviz # install picture related software

$ANDROID _src/out/host/linux-x86/bin/dmtracedump-g test.png Test.trace

The class diagram is generated in the directory at this time test.png

3) Note

Trace file generation conflicts with the debug version of the LIBDVM module, so this method applies only to debugging a non-debug version simulator, otherwise an error occurs when parsing the trace file.

3. HProf (Heap profile)

1) Function:

A Java-level memory analysis that displays detailed memory footprint information indicating the suspect memory leak object.

2) Method:

A) Add a dump action in your code:

Import Android.os.Debug;  
Import java.io.IOException; 
..... try {  
android.os.Debug.dumpHprofData ("/data/tmp/input.hprof");///First build/data/tmp directory  
} catch (IOException IoE) {  
} 

b) Copy the Hprof file to the PC side

$ adb pull/data/tmp/input.hprof./

(c) Use of command Hprof-conv to convert Hprof to mat recognized standard hprof

$ $ANDROID _src/out/host/linux-x86/bin/hprof-conv input.hprof output.hprof

D Use the mat tool to look at hprof information

Download Mat tool: http://www.eclipse.org/mat/downloads.php

Open output.hprof with tools

3 Note: This tool can only display the Java level, but not the C-tier memory footprint information.

4. Samplingprofile (use on Android 2.0 version)

1) function

The currently running function is sampled every n milliseconds and exported to the log.

2 Add the sampling setting in the code:

Import Dalvik.system.SamplingProfiler ...  
Samplingprofile sp = Samplingprofiler.getinstance ();  
Sp.start (n); N to set the sampling times per second  
Sp.logsnapshot (Sp.snapshot ());  
..... Sp.shutdown (); 

It will start a thread monitor and print the information in the Logcat.

5. Use the system signal to take the current stack situation and memory information

1) principle

The Dalvik virtual machine processes the Sigquit and SIGUSR1 signals (DALVIK/VM/SIGNALCATCHER.C), completes the current stack and takes the current memory.

2) Usage

A) $ chmod 777/data/anr-r # set ANR directory permissions to writable

$ rm/data/anr/traces.txt # Delete previous trace information

$ ps # Find process number

$ kill-3 Process number # Send Sigquit signal to the process, generating trace information at this time

$ cat/data/anr/traces.txt

Function implementation: Traverse Thread list (Dalvik/vm/thread.c:dvmdumpallthreadex ()) and print the current function call relationship (Dalvik/vm/interp/stack.c:dumpframes ())

b) $ chmod 777/data/misc-r

$ ps # Find process number

$ kill-10 Process Number # Send Sigquit signaling to the process, generating hprof information at this time

$ ls/data/misc/*.hprof

The Hprf file is generated at this time, and how to use this file is shown in Part II (HPROF)

Note: hprof files are very large, pay attention to remove immediately after use, so as not to fill up the storage.

6. Logcat and principle

1) Android.util.Log use println standard Java output words and prefix i/v/d ....

2 Dalvik the use of Pipeline plus threading method, first use dup2 to redirect stdout and stderr to management

(Vm/stdioconverter.c:dvmstdioconverterstartup), and then start a thread to read the content from the other end of the pipe (dalvik/vm/stdioconverter.c: Stdioconverterthreadstart ()), use Log Public tool (SYSTEM/CORE/LIBLOG/LOGD_WRITE.C: __android_log_print ()) to output to/dev/log/*.

3) Logcat to look at the different input information under/dev/log/by adding different parameters

# Logcat-b main shows the information in the primary buffer

# Logcat-b Radio Display the information in the wireless buffer

# Logcat-b Events Displays the information in the event buffer

7. JDWP (Java Debug Wire Protocol) and principles

1 The virtual machine (device side) loaded the agent JDWP at startup, which has the debugging function. The debugger-side (PC-side) connects to the device through the JDWP protocol, obtains the status by sending commands, and controls the execution of the Java program. JDWP are communicated through commands (command) and reply (reply).

2 The Debug tool Jdb is a debugger in JDK, DDMS also provides the debugger to connect with the device.

3) Dalvik provides JDWP with two ways to connect: TCP and ADB, TCP can manually specify the port, the ADB is automatically set to 8700 ports, usually using DDMS debugging is through the ADB way.

8. Monkey

1 Monkey is an Android-led command-line tool. It sends a pseudo-random stream of user events to the system to perform a stress test on the application under development.

2) method

Open the setting interface on the device side

$ adb shell

# monkey-p Com.android.settings-v 500

At this point you can see that the interface is constantly being switched.

9. Other Gadgets

See the tools provided in Android.os.Debug.

1 to take the time of the nanosecond to calculate the time

Threadcputimenanos ()

2) Statistics of the memory allocation between two points:

Startalloccounting ()  
stopalloccounting () getglobalalloccount () Get  
..... 

3 Print the current load 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

Above is on the Android debugging tools of data collation, follow-up to continue to supplement the relevant knowledge, thank you for your support to this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.