How Android fully debugs the framework layer code

Source: Internet
Author: User

1 wrote an article before: "Android implementation boot Debug System_process"

2 Google Eclipse Plugin ADT has been able to easily debug Android apk, but when debugging the application has entered the activity.

3 If we want to know the activation process of activity, only look at the code + look at the log output? This article can tell you: no!

4 I believe you are more interested in the code, here first put out the code

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 =rt.exec (command);
  6. if (proc.waitfor ()! = 0) {
  7. return null;
  8. }
  9. Linkedlist<string>list = new linkedlist<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 a 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, keep looking down!

6 Readcommandoutput: Used to execute commands and get output from commands

7 Getpackagename () has a package name to get the current process

The default process name here is the package name

Get the output of PS

The output line where the program is located is then found through the PID.

Extract the name of the output line package where the program is located

8 Needdebug () is used to determine if the current process needs to be debugged, the principle is this:

The user sets the package name by SetProp the Persist.sys.debug package name

Needdebug Get Persist.sys.debug

Compare with the package name of this process to determine if you want to debug

9 The next action will be the same as the "Android implementation boot Debug system_process":

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, mmm ..., a long process, but the results will prove to be worthwhile.

11 Next, create a pseudo-project, modify the package name in the manifest to be debugged

12 then introduce the framework layer code that needs to be debugged in the project

13 Next, the ADB shell connects to Android, making the command line

14 Execute Command:

SetProp Persist.sys.debug Package Name

15 setting breakpoints on code that needs to be debugged

16 Next, start the application, and note that you are launching the program directly, rather than starting debugging through eclipse!

Enjoy it!

18 (end)

How Android fully debugs the framework layer code

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.