How to fully debug the framework-Layer Code for Android

Source: Internet
Author: User

1. I wrote an article about how to debug system_process for Android boot.

2 google's eclipse plug-in ADT has been able to easily debug the Android apk, but the application entered the Activity at the time of debugging.

3. If you want to know the Startup Process of the Activity, do you have to read the code + log output? This article tells you: NO!

4. I believe all of you are interested in code. Here we will release the code first.

ZygoteInit. java

Public StaticList ReadCommandOutput (String command ){

Runtime rt = Runtime.GetRuntime();

Java. lang. Processproc;

Try{

Proc implements rt.exe c (command );

If(Proc. waitFor ()! = 0 ){

Return Null;

}

Shortlist List = NewShortlist ();

InputStreamReaderir =NewInputStreamReader (proc. getInputStream ());

BufferedReaderin =NewBufferedReader (ir );

Stringline =Null;

While(Line = in. readLine ())! =Null){

List. add (line );

}

ReturnList;

}Catch(InterruptedException e ){

E. printStackTrace ();

}Catch(IOException e ){

E. printStackTrace ();

}

Return Null;

}

Public StaticString getPackageName (){

String strPid = Integer.ToString(Android. OS. Process.MyPid());

String cmd = "ps ";

List Result = ReadCommandOutput(Cmd );

If(Result =Null){

Return"";

}

For(String info: result ){

If(Info. contains (strPid )){

IntIndex = info. lastIndexOf ("");

If(Index> = 0 ){

StringsubStr = info. substring (index + 1 );

Log.I(TAG, SubStr );

ReturnSubStr;

}

}

}

Return"";

}

Public Static BooleanNeedDebug (String packageName ){

StringdebugProcess = android. OS. SystemProperties.Get("Persist. sys. debug ");

Log.I(TAG, DebugProcess );

If(DebugProcess. equals (packageName )){

Return True;

}

Return False;

}

Public Static VoidMain (Stringargv []) {

Try{

// Start profilingZygoteInitialization.

SamplingProfilerIntegration.Start();

RegisterZygoteSocket();

EventLog.WriteEvent(LOG_BOOT_PROGRESS_PRELOAD_START,

SystemClock.UptimeMillis());

Preload();

EventLog.WriteEvent(LOG_BOOT_PROGRESS_PRELOAD_END,

SystemClock.UptimeMillis());

// Finish profilingZygoteInitialization.

SamplingProfilerIntegration.WriteZygoteSnapshot();

// Do an initialGcTo cleanup after startup

Gc();

// If requested, start system server directlyfromZygote

If(Argv. length! = 2 ){

Throw NewRuntimeException (argv [0] +USAGE_STRING);

}

If(Argv [1]. equals ("start-system-server ")){

StartSystemServer();

}Else If(! Argv [1]. equals ("")){

Throw NewRuntimeException (argv [0] +USAGE_STRING);

}

Log.I(TAG, "Accepting command socket connections ");

If(ZYGOTE_FORK_MODE){

RunForkMode();

}Else{

RunSelectLoopMode();

}

CloseServerSocket();

}Catch(MethodAndArgsCaller caller ){

String packageName =GetPackageName();

If(NeedDebug(PackageName )){

Android. ddm. DdmHandleAppName.SetAppName(PackageName, UserHandle.MyUserId());

Android. OS. Debug.WaitForDebugger();

}

Caller. run ();

}Catch(RuntimeException ex ){

Log.E(TAG, "Zygote died with exception", ex );

CloseServerSocket();

ThrowEx;

}

}

5. If you are interested, continue reading!

6 readCommandOutput: used to execute the command and obtain the command output.

7 getPackageName () is used to obtain the package name of the current process.

Here, the default process name is the package name.

Obtain ps output

Then, use the pid to find the output line of the program.

Extract the output line package name of the program.

8 needDebug () is used to determine whether the current process requires debugging. The principle is as follows:

You can use setprop persist. sys. debug to set the package name.

NeedDebug gets persist. sys. debug

Compare with the package name of the process to determine whether debugging is required.

9 The subsequent actions are the same as those in Android system_process for boot debugging:

Set DDM name

Android. ddm. DdmHandleAppName. setAppName (packageName, UserHandle. myUserId ());

Wait for the debugger to connect:

Android. OS. Debug. waitForDebugger ();

10 next, recompile and burn the records. mmm... is a long process, but the result will prove that it is indeed worthwhile.

11. Create a pseudo project and change the package in Manifest to the package name to be debugged.

12. Introduce the framework-layer code to be debugged in the project.

13 next, the adb shell connects to android and runs the command line

14. Run the following command:

Setprop persist. sys. debug package name

15. set breakpoints on the code to be debugged

16 next, start the application. Note that it is to start the program directly, rather than start debugging through eclipse!

17 enjoy it!

18 (end)

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.