[Android] Android code Debugging Tools: TraceView and Dmtracedump

Source: Internet
Author: User

? Android Program Debugging Tool

The highlights of the code debugging tools that Google provides us:TraceView and dmtracedump . With these two tools, we debug the program to analyze the bug is very handy. TraceView helps us analyze program performance and dmtracedump generate function call graphs. Unfortunately, the dmtracedump provided by Google is a failed tool and cannot be plotted, this article describes the solution in detail and implements the drawing.

? generate a. trace file

The Android.os.Debug class, which is an important two method of debug.startmethodtracing () and debug.stopmethodtracing (). These two methods are used to create a. trace file, starting with debug.startmethodtracing () and ending with debug.stopmethodtracing (), During all the call procedures are saved in the. trace file, including information such as the name of the function called and the time of execution.
Add the following code to the location of the debug start code, and to the terminating position.

    1. Debug.startmethodtracing ("test");
    2. Debug.stopmethodtracing ();

Where the parameter test is the name of the trace file to be created, Test.trace. The default path is/sdcard/test.trace, or you can make your own/data/log/test, indicating that the file is in/data/log/test.trace.

? traceview

Execute in the SDK:

./traceview Test.trace

We can get

1. Start and stop times for each thread in the program to invoke the method

2. Information and efficiency analysis of function execution

? dmtracedump

The original intention of Dmtracedump was to combine the entire invocation process with the time analysis in the form of a function call graph. But Google this project has been in a state of broken, delay can not be perfected. Now dmtracdump only the-o option can be used, the function call information is listed on the terminal, and the TraceView function is similar if executed./dmtracedump–g test.png Test.trace will be stuck.

At first I thought it was test.trace file, the file terminator slightly modified, drew a shocking picture:

Later, the search on the network of cattle have provided an alternative to dmtracedump (this is the original link), is a Python script, with the help of dot to draw vector diagram. The Python script must be aware of the alignment format, and the alignment indentation is his logical structure.

  1. #!/usr/bin/env python
  2. """
  3. Turn the TraceView data into a JPG pic, showing methods call relationship
  4. """
  5. Import Sys
  6. Import OS
  7. Import struct
  8. Import re
  9. ################################################################################
  10. ######################## Global Variable #####################################
  11. ################################################################################
  12. Target_thread=1 #the thread, want, filt out and other threads
  13. #all_actions = ["Enter", "Exit", "Exception", "reserved"]
  14. All_threads = {}
  15. All_methods = {}
  16. All_records = []
  17. Parent_methods = {}
  18. Child_methods = {}
  19. Method_calls = {}
  20. ################################################################################
  21. ############################## Methods #####################################
  22. ################################################################################
  23. def add_one_thread (line):
  24. Fields = Line.split ("/t")
  25. All_threads[int (fields[0],10)]=fields
  26. def add_one_method (line):
  27. Fields = Line.split ("/t")
  28. All_methods[int (fields[0],16)]=fields
  29. def Add_one_record (one):
  30. Thread_id,=struct.unpack ("B", One[:1])
  31. if (thread_id = = Target_thread):
  32. Tmp,=struct.unpack ("L", One[1:5])
  33. Method_id= (TMP/4) * 4;
  34. method_action= tmp% 4;
  35. Time_offset,=struct.unpack ("L", One[5:])
  36. All_records.append ([thread_id, method_id, Method_action, Time_offset])
  37. def handle_one_call (parent_method_id,method_id):
  38. If not (Parent_methods.has_key (parent_method_id)):
  39. Parent_methods[parent_method_id]=1
  40. If not (Child_methods.has_key (method_id)):
  41. Child_methods[method_id]=1
  42. If Method_calls.has_key (parent_method_id):
  43. If Method_calls[parent_method_id].has_key (method_id):
  44. Method_calls[parent_method_id][method_id]+=1
  45. Else
  46. Method_calls[parent_method_id][method_id]=1
  47. Else
  48. method_calls[parent_method_id]={}
  49. Method_calls[parent_method_id][method_id]=1
  50. def gen_funcname (method_id):
  51. R1=re.compile (R ' [/{1}lt;>] ')
  52. Str1=r1.sub ("_", All_methods[method_id][1])
  53. Str2=r1.sub ("_", All_methods[method_id][2])
  54. Return str1+ "_" +str2
  55. Def gen_dot_script_file ():
  56. Myoutfile = open ("Graph.dot", "W")
  57. Myoutfile.write ("digraph vanzo {/n/n");
  58. For one in All_methods.keys ():
  59. If Parent_methods.has_key (one):
  60. Myoutfile.write (Gen_funcname (one) + "[shape=rectangle];/n")
  61. Else
  62. If Child_methods.has_key (one):
  63. Myoutfile.write (Gen_funcname (one) + "[shape=ellipse];/n")
  64. For one in Method_calls.keys ():
  65. For Method_calls[one]:
  66. Myoutfile.write (Gen_funcname (one) + ' + ' + gen_funcname (both) + ' [label= "' + str (method_calls[one][two]) + '" fonts Ize= "];/n")
  67. Myoutfile.write ("/n}/n");
  68. Myoutfile.close
  69. ################################################################################
  70. ########################## Script starts from here #############################
  71. ################################################################################
  72. If Len (SYS.ARGV) < 2:
  73. print ' No input file specified. '
  74. Sys.exit ()
  75. If not (Os.path.exists (sys.argv[1])):
  76. Print "Input File not exists"
  77. Sys.exit ()
  78. #Now Handle the text part
  79. Current_section=0
  80. For line in open (Sys.argv[1]):
  81. Line2 = Line.strip ()
  82. if (Line2.startswith ("*")):
  83. if (Line2.startswith ("*version")):
  84. Current_section=1
  85. Else
  86. if (Line2.startswith ("*threads")):
  87. current_section=2
  88. Else
  89. if (Line2.startswith ("*methods")):
  90. Current_section=3
  91. Else
  92. if (Line2.startswith ("*end")):
  93. Current_section=4
  94. Break
  95. Continue
  96. If current_section==2:
  97. Add_one_thread (line2)
  98. If current_section==3:
  99. Add_one_method (line2)
  100. #Now Handle the binary part
  101. Mybinfile = open (Sys.argv[1], "RB")
  102. AllData = Mybinfile.read ()
  103. Mybinfile.close ()
  104. Pos=alldata.find ("SLOW")
  105. Offset,=struct.unpack ("H", Alldata[pos+6:pos+8])
  106. Pos2=pos+offset #pos2 is where the record begin
  107. Numofrecords = Len (alldata)-Pos2
  108. Numofrecords = NUMOFRECORDS/9
  109. For I in Xrange (numofrecords):
  110. Add_one_record (Alldata[pos2 + i * 9:pos2 + i * 9 + 9])
  111. MY_STACK=[0]
  112. For Onerecord in All_records:
  113. THREAD_ID=ONERECORD[0];
  114. METHOD_ID=ONERECORD[1];
  115. ACTION=ONERECORD[2];
  116. TIME=ONERECORD[3];
  117. if (action==0):
  118. if (len (my_stack) > 1):
  119. PARENT_METHOD_ID=MY_STACK[-1]
  120. Handle_one_call (parent_method_id,method_id)
  121. My_stack.append (method_id)
  122. Else
  123. if (action==1):
  124. if (len (my_stack) > 1):
  125. My_stack.pop ()
  126. Gen_dot_script_file ()
  127. Os.system ("Dot-tjpg graph.dot-o output.jpg;rm-f Graph.dot");
Modify,/T becomes \t,/n to \ n. Added in the source oncreate of the calculator
    1. Debug.startmethodtracing ("Calc");
    2. LOG.V (log_tags "+++++++++++++++++++++++++test++++++++++++++++");
    3. Debug.stopmethodtracing ();
Run the script to get Calc.trace, draw the Out.jpg  
   Drawing crashes when the resulting trace file is more complex. Modify the script at the end of the dot command when the parameters, JPG format is too big to crash,-tjpg changed to-TPNG:GD, draw a large PNG.
I did an experiment with my camera and got a very large PNG, which is one of the corners:

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.