How does Android fully debug the framework-layer code?

Source: Internet
Author: User

How does Android fully debug the framework-layer code?

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

 

 

[Java]View plaincopy
  1. Public static List <String> readCommandOutput (String command ){
  2. Runtime rt = Runtime. getRuntime ();
  3. Java. lang. Processproc;
  4. Try {
  5. Proc implements rt.exe c (command );
  6. If (proc. waitFor ()! = 0 ){
  7. Return null;
  8. }
  9. Shortlist <String> list = new shortlist <String> ();
  10. InputStreamReader ir = new InputStreamReader (proc. getInputStream ());
  11. BufferedReader in = new BufferedReader (ir );
  12. String line = null;
  13. While (line = in. readLine ())! = Null ){
  14. List. add (line );
  15. }
  16. Return list;
  17. } Catch (InterruptedException e ){
  18. E. printStackTrace ();
  19. } Catch (IOException e ){
  20. E. printStackTrace ();
  21. }
  22. Return null;
  23. }
  24. Public static String getPackageName (){
  25. String strPid = Integer. toString (android. OS. Process. myPid ());
  26. String cmd = "ps ";
  27. List <String> result = readCommandOutput (cmd );
  28. If (result = null ){
  29. Return "";
  30. }
  31. For (String info: result ){
  32. If (info. contains (strPid )){
  33. Int index = info. lastIndexOf ("");
  34. If (index> = 0 ){
  35. StringsubStr = info. substring (index + 1 );
  36. Log. I (TAG, subStr );
  37. Return subStr;
  38. }
  39. }
  40. }
  41. Return "";
  42. }
  43. Public static boolean needDebug (String packageName ){
  44. String debugProcess = android. OS. SystemProperties. get ("persist. sys. debug ");
  45. Log. I (TAG, debugProcess );
  46. If (debugProcess. equals (packageName )){
  47. Return true;
  48. }
  49. Return false;
  50. }
  51. Public static void main (Stringargv []) {
  52. Try {
  53. // Start profiling the zygote initialization.
  54. SamplingProfilerIntegration. start ();
  55. RegisterZygoteSocket ();
  56. EventLog. writeEvent (LOG_BOOT_PROGRESS_PRELOAD_START,
  57. SystemClock. uptimeMillis ());
  58. Preload ();
  59. EventLog. writeEvent (LOG_BOOT_PROGRESS_PRELOAD_END,
  60. SystemClock. uptimeMillis ());
  61. // Finish profiling the zygote initialization.
  62. SamplingProfilerIntegration. writeZygoteSnapshot ();
  63. // Do an initial gc to cleanup after startup
  64. Gc ();
  65. // If requested, start system server directlyfrom Zygote
  66. If (argv. length! = 2 ){
  67. Throw new RuntimeException (argv [0] + USAGE_STRING );
  68. }
  69. If (argv [1]. equals ("start-system-server ")){
  70. StartSystemServer ();
  71. } Else if (! Argv [1]. equals ("")){
  72. Throw new RuntimeException (argv [0] + USAGE_STRING );
  73. }
  74. Log. I (TAG, "Accepting command socket connections ");
  75. If (ZYGOTE_FORK_MODE ){
  76. RunForkMode ();
  77. } Else {
  78. RunSelectLoopMode ();
  79. }
  80. CloseServerSocket ();
  81. } Catch (MethodAndArgsCaller caller ){
  82. String packageName = getPackageName ();
  83. If (needDebug (packageName )){
  84. Android. ddm. DdmHandleAppName. setAppName (packageName, UserHandle. myUserId ());
  85. Android. OS. Debug. waitForDebugger ();
  86. }
  87. Caller. run ();
  88. } Catch (RuntimeException ex ){
  89. Log. e (TAG, "Zygote died with exception", ex );
  90. CloseServerSocket ();
  91. Throw ex;
  92. }
  93. }



 

 

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 the name of the App in DDM:

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)

 


In android, how can I view the changed effect after the framework layer code is changed?

1. the following method applies to the real machine: Download the android source code, compile the modified framwork code, generate framework. jar, push it to the system/framework directory, and restart the machine! OK

2. The following method is suitable for simulators:
(1) decompress the package with unyaffs. In the sdk directory you downloaded, replace system. img with the framework. jar and compress it into a new system. img. Then, start the simulator and OK.

(2): You can also directly compile the source code and generate system. img to replace system. img in the simulator.

How to debug official android code

If it refers to the Framework layer (such as controls and system services), you can download it from SDK Manager and re-associate it with Eclipse. The specific association method is to press Ctrl: Button and TextView. On the new page that appears, click Change Attached Source in the upper-left corner, click External Folder, find the sdk directory, and click the sources directory, select the system version folder you want to download and click OK.
If you want to see the underlying android source code, you need the Linux system environment. I have never practiced this. The specific process is not very clear. You should check Baidu's "android source code, the link will not be given, so it will not be blocked again ......

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.