Android Development--android Debug Toolset "Go"

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/halzhang/archive/2010/08/11/1797097.html

1. View the current stack

1) Function: Add code to the program so that you can see the current function call relationship printed in Logcat

2) Method:
New Exception ("Print Trace"). Printstacktrace ();

2. methodtracing

1) Function: For hot spot analysis and performance optimization, analyze CPU time, number of calls, function call relationship, etc.

2) Method:

A) Add the tracking switch in the program code

   1:import Android.os.Debug;
   2: ...
   3:android.os.debug.startmethodtracing ("/data/tmp/test"); Build/data/tmp Directory First
   4: ...//The program segment being traced
   5:android.os.debug.stopmethodtracing ();

b) compile, run, generate/data/tmp/test.trace file on device side

c) Copy the trace file to the PC side
$ adb pull/data/tmp/test.trace./

d) Analyze trace files using the Android self-bring tool
$ $ANDROID _src/out/host/linux-x86/bin/traceview test.trace
At this point, you can see the number of times each function is called CPU utilization and other information

e) Analyze the build Call relationship class diagram using the Android Self-tool
$ 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 this directory Test.png

3) Note
Trace file generation conflicts with the debug version of the LIBDVM module, so this method is only suitable for debugging non-debug version emulators, otherwise it will be error when parsing trace files

3. HProf (Heap profile)

1) Function:
Memory analysis for Java-level display of detailed memory footprint information indicating suspect memory leak objects

2) Method:

A) Add a dump action to the code

   1:import Android.os.Debug;
   2:import java.io.IOException;
   3: ...
   4:try {
   5:android.os.debug.dumphprofdata ("/data/tmp/input.hprof"); Build/data/tmp Directory First
   6:} catch (IOException IoE) {
   7:}

b) Copy the Hprof file to the PC side
$ adb pull/data/tmp/input.hprof./

c) Use the command Hprof-conv to turn hprof into the standard hprof for mat recognition
$ $ANDROID _src/out/host/linux-x86/bin/hprof-conv input.hprof output.hprof

d) Use the Mat tool to see hprof information
Download Mat tool:http://www.eclipse.org/mat/downloads.php
Open output.hprof with tools

3) Note: This tool can only display Java-level, not the C-tier memory consumption information

4. Samplingprofile (used on Android 2.0 version)

1) function
Samples the currently running function every n milliseconds and outputs it to log

2) Add sampling settings to the code

   1:import Dalvik.system.SamplingProfiler
   2: ...
   3:samplingprofile sp = Samplingprofiler.getinstance ();
   4:sp.start (n); n Sets the number of samples per second
   5:sp.logsnapshot (Sp.snapshot ());
   6: ...
   7:sp.shutdown ();


It will start a thread monitor and print information in Logcat

5. Take the current stack condition and memory information in the form of a system signal

1) principle
Dalvik virtual machine to sigquit and SIGUSR1 signal Processing (DALVIK/VM/SIGNALCATCHER.C), respectively, to complete the current stack and take the current memory situation function

2) Usage

A) $ chmod 777/data/anr-r # Set the anr directory permission to writable
$ rm/data/anr/traces.txt #
Delete previous trace information
$ ps #
Find process number
$ kill-3
Process number # sends a sigquit signal to the process, at which point the trace is generated Information
$ cat/data/anr/traces.txt

Feature implementation: Traverse the 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
# # # # # # # # # # # # # # send sigquit signaling to the process, generate hprof at this time Information
$ ls/data/misc/*.hprof

Now generate the Hprf file, how to use this file, see Part Two (HProf)
Note: hprof files are very large, pay attention to delete immediately after use, so as not to occupy the full memory

6. Logcat and principle

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

2) Dalvik the use of pipelines and threading, first using DUP2 to redirect stdout and stderr to management (Vm/stdioconverter.c:dvmstdioconverterstartup), Then start a thread to read the content (Dalvik/vm/stdioconverter.c:stdioconverterthreadstart ()) from the other end of the pipeline, using the log utility (System/core/liblog/logd_ WRITE.C: __android_log_print ()) output to/dev/log/*

3) Logcat by adding different parameters to see the different input information under/dev/log/
# logcat-b Main display information in the primary buffer
# logcat-b Radio
displays information in the wireless buffer
# logcat-b
Events displays information in the event buffer

7. JDWP (Java Debug Wire protocol) and principle

1) The virtual machine (device side) loaded the agent JDWP at startup, thus having the debugging function. The debugger side (PC side) connects to the device via the JDWP protocol, sending commands to get the state and control the execution of the Java program. JDWP is communicated by command and reply (reply).

2) The Debug tool jdb in the JDK is a debugger, and the DDMS also provides a debugger to connect to the device.

3) Dalvik provides two kinds of connection methods for JDWP: TCP and ADB, TCP can manually specify the port, the ADB is automatically set to 8700 port, usually using DDMS debugging is through the ADB mode

8. Monkey

1) Monkey is a command-line tool that comes with Android. It sends a pseudo-random user event stream to the system, enabling stress testing of the application under development.

2) method
Open the setting interface on the device side
$ adb shell
# monkey-p Com.android.settings-v

You can see that the interface is constantly being switched

9. Other Gadgets
See the tools provided in Android.os.Debug for details

1) Take the nanosecond-level time to calculate the time
Threadcputimenanos ()

2) Statistics of memory allocation between two points
Startalloccounting ()
Stopalloccounting ()
Getglobalalloccount ()
Get .....

3) Print the current load class
Getloadedclasscount ()
Printloadedclasses ()
it needs to open the ndebug function to open the system/core/ Log function

10. Print Debug information
$ adb bugreport

11. Reference

1) usage of monkey in Android
Http://junjie0324.spaces.live.com/blog/cns!BAAE46DF931F8C64!204.entry

Transferred from: http://xy0811.spaces.live.com

Android Development--android Debug Toolset "Go"

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.