Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the debugging tool of Android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use Android debugging tools, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use Android debugging tools. Let's take a look.

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) methods:

New Exception (print trace) .printStackTrace ()

2. MethodTracing

1) function: for hot spot analysis and performance optimization, analyze the CPU time occupied by each function, the number of calls, function call relationship, etc.

2) methods:

A) add a tracking switch to the program code

Import android.os.Debug;... Android.os.Debug.startMethodTracing ("/ data/tmp/test"); / / pre-built / data/tmp directory. / / tracked program segment android.os.Debug.stopMethodTracing ()

B) compile, after running, the device side generates / data/tmp/test.trace file

C) copy the trace file to the PC

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

D) use android native tools to analyze trace files

$$ANDROID_SRC/out/host/linux-x86/bin/traceview test.trace

At this point, you can see information such as the number of times each function is called, CPU occupancy, and so on.

E) use android native tools to analyze and generate invocation class diagrams

$apt-get install graphviz # install picture-related software $ANDROID_SRC/out/host/linux-x86/bin/dmtracedump-g test.png test.trace

At this point, the class diagram test.png is generated under the directory.

3) pay attention

The generation of trace files conflicts with the DEBUG version of libdvm module, so this method is only suitable for debugging non-DEBUG simulators, otherwise an error will be reported when analyzing trace files.

3. HProf (Heap Profile)

1) function:

Used for memory analysis at the java level, displaying detailed memory footprint information and pointing out suspected memory leaks

2) methods:

A) add dump action to the code

Import android.os.Debug; import java.io.IOException;... Try {android.os.Debug.dumpHprofData ("/ data/tmp/input.hprof"); / / pre-built / data/tmp directory} catch (IOException ioe) {}

B) copy the hprof file to the PC

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

C) use the command hprof-conv to convert hprof to a standard hprof recognized by MAT

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

D) use the MAT tool to view hprof information

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

Open output.hprof with tools

3) Note: this tool can only display the java level, not the memory footprint of the C layer.

4. SamplingProfile (used on android 2.0)

1) function

Samples the currently running functions every N milliseconds and outputs them to log

2) add sampling settings to the code

Import dalvik.system.SamplingProfiler... SamplingProfile sp = SamplingProfiler.getInstance (); sp.start (n); / / n is the set number of samples per second sp.logSnapshot (sp.snapshot ());... Sp.shutDown ()

It starts a thread monitor and prints information in logcat

5. Fetch the current stack condition and memory information by sending system signals

1) principle

The dalvik virtual machine processes SIGQUIT and SIGUSR1 signals (dalvik/vm/SignalCatcher.c) to fetch the current stack and memory respectively.

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 the process number

$kill-3 process number # sends a SIGQUIT signal to the process, and the trace information is generated

$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 the process number

$kill-10 process number # sends a SIGQUIT signal to the process, and the hprof information is generated.

$ls / data/misc/*.hprof

The hprf file is generated at this time. For more information on how to use this file, see part 2 (HProf)

Note: hprof files are very large, please delete them immediately after use, so as not to fill up the memory.

6. Logcat and its principle

1) android.util.Log uses println's standard java output words and sentences with the prefix I/V/D. .

2) dalvik uses pipes and threads to redirect stdout and stderr to management (vm/StdioConverter.c:dvmstdioConverterStartup) using dup2, then starts a thread to read content from the other end of the pipe (dalvik/vm/StdioConverter.c:stdioconverterThreadStart ()), and uses the LOG common tool (system/core/liblog/logd_write.c: _ _ android_log_print ()) to output to / dev/log/*

3) logcat looks at different input information under / dev/log/ by adding different parameters

# logcat-b main displays information in the main 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) loads Agent JDWP at startup so that it has the debugging function. On the debugger side (PC side), the device is connected with the JDWP protocol, and the status is obtained and the execution of the Java program is controlled by sending commands. JDWP communicates through commands (command) and replies (reply).

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

3) dalvik provides two connection modes for JDWP: tcp mode and adb mode. The port of tcp mode can be specified manually, and the port of adb mode can be automatically set to port 8700. Usually, DDMS debugging is done through adb mode.

8. Monkey

1) monkey is a command line tool that comes with android. It sends a pseudo-random stream of user events to the system to carry out 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 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 for details.

1) take nanosecond time to calculate the time

ThreadCpuTimeNanos ()

2) Statistics the memory allocation between the two points

StartAllocCounting () stopAllocCounting () getGlobalAllocCount () get... ..

3) print the class that is currently load

GetLoadedClassCount ()

PrintLoadedClasses () it needs to turn on the NDEBUG function to turn on the Log function in system/core/.

10. Print debug information

This is the end of $adb bugreport's article on "how to use Android debugging tools". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Android debugging tools". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report