Android debugging tool

Source: Internet
Author: User
Tags time in milliseconds

1. view the current stack

1) function: InProgramJoinCodeTo view the printed current function call relationship in logcat.

2) method:
New exception ("Print trace"). printstacktrace ();

2. methodtracing

1) function: this function is used for hotspot analysis and performance optimization. It analyzes the CPU time used by each function, the number of calls, and the function call relationship.

2) method:

A) add the tracing switch to the program code.

 
  
  
  1. Import Android. OS. debug;
  2. ......
  3. Android. OS. Debug. startmethodtracing ("/data/tmp/test ");// Create the/data/tmp directory first 
  4. ......// The tracked program segment 
  5. Android. OS. Debug. stopmethodtracing ();

B) Compile and run the program. The/data/tmp/test. trace file is generated on the device.

C) copy the trace file to the PC.

 
  
  
  1. $ ADB pull/data/tmp/test. Trace ./

D) Use the android tool to analyze the trace file

 
  
  
  1. $ Android_src/out/host/linux-x86/bin/traceview test. Trace

At this time, you can see the number of calls to each function, CPU usage, and other information.

E) Use the built-in Android tool to analyze and generate a call relationship class diagram

 
  
  
  1. $ Apt-Get install graphviz # Install image-related software
  2. $ Android_src/out/host/linux-x86/bin/dmtracedump-G test.png test. Trace

Test.png

3) Note
The trace file generation conflicts with the debug version of The libdvm module. Therefore, this method is only applicable to debugging non-debug version simulators. Otherwise, an error is reported when the trace file is analyzed.

3. hprof (heap profile)

1) features:
It is used for memory analysis at the Java layer to display detailed memory usage information and identify suspicious Memory leakage objects.

2) method:

A) add the 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 ");// Create the/data/tmp directory first 
  6. }Catch(Ioexception IOE ){
  7. }

B) copy the hprof file to the PC end.

 
  
  
  1. $ ADB pull/data/tmp/input. hprof ./

C) use the hprof-Conv command to convert hprof to the standard hprof recognized by mat.

 
  
  
  1. $ Android_src/out/host/linux-x86/bin/hprof-Conv input. hprof output. hprof

D) Use the mat tool to view hprof Information

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

Use a tool to open output. hprof

3) Note: This tool can only display the Java layer, but cannot display the memory usage information of the C layer.

4. samplingprofile (used in Android 2.0)

1) Functions
Sample the currently running function every n milliseconds and output it to log.

2) Add sampling settings to the code.

  
  
  1. Import Dalvik. system. samplingprofiler
  2. ......
  3. SamplingprofileSP=Samplingprofiler. Getinstance ();
  4. Sp. Start (n); // n indicates the number of samples per second.
  5. Sp. logsnapshot (sp. snapshot ());
  6. ......
  7. Sp. Shutdown ();

It starts a thread monitoring and prints information in logcat.

5. Use the system signal to obtain the current stack status and memory information

1) Principle
The Dalvik Virtual Machine processes sigquit and SIGUSR1 signals (Dalvik/Vm/signalcatcher. c) to obtain the current stack and the current memory respectively.

2) Usage

A) $ chmod 777/data/ANR-R # Set the ANR directory permission to writable.
$ RM/data/ANR/traces.txt # Delete the previous trace information
$ PS # Find the process number
$ Kill-3 process # Send the sigquit signal to the process. At this time, the trace information is generated.
$ CAT/data/ANR/traces.txt
Function 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 the process number
$ Kill-10 process no. # Send the sigquit message to the process. hprof information is generated at this time.
$ Ls/data/MISC/*. hprof
The HPRF file is generated. For how to use this file, see section 2 (hprof)
Note: The hprof files are large and should be deleted immediately after use to avoid occupying full storage space.

6. logcat and its principles

1) Android. util. Log uses the standard Java output Syntax of println and adds the prefix I/V/d ....

2) Dalvik uses pipelines and threads to redirect stdout and stderr to management using dup2 (Vm/stdioconverter. c: dvmstdioconverterstartup), and then start a thread to read the content from the other end of the pipeline (Dalvik/Vm/stdioconverter. c: stdioconverterthreadstart (). Use the log public tool (System/CORE/liblog/logd_write.c: _ android_log_print () to output it to/dev/log /*.

3) logcat adds different parameters to view different input information under/dev/log /.

 
  
  
  1. # Logcat-B Main displays information in the master buffer zone
  2. # Logcat-B radio displays information in the wireless buffer zone
  3. # Logcat-B events: displays information in the event Buffer

7. jdwp (Java debug wire protocol) and Its Principle

1) The Virtual Machine (device) is loaded with the agent jdwp at startup to provide debugging functions. On the debugger side (PC side), connect to the device through the jdwp protocol, and send commands to get the status and control Java program execution. Jdwp communicates with reply through command.

2) in JDK, the debugging tool jdb is a debugger, and ddms also provides a debugger to connect to the device.

3) Dalvik provides two Connection Methods for jdwp: TCP mode and ADB mode. You can manually specify the port in TCP mode, and the ADB mode is automatically set to port 8700, generally, ddms debugging is implemented through ADB.

8. Monkey

1) Monkey is an android command line tool. It sends a pseudo-random user event stream to the system to perform stress testing on developing applications.

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 switched.

9. other gadgets
For details, see the tools provided in Android. OS. debug.

1) obtain the time in milliseconds for computing.
Threadcputimenanos ()

2) Calculate the memory allocation between two points

 
  
  
  1. Startalloccounting ()
  2. Stopalloccounting ()
  3. Getglobalalloccount ()
  4. Get.....

3) print the currently loaded class
Getloadedclasscount ()
Printloadedclasses () It must enable the ndebug function to enable the log function in system/CORE /.

10. Print the debug information

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.