PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/stack_core.py

https://github.com/douglasascosta/platform_development
Python | 206 lines | 145 code | 17 blank | 44 comment | 29 complexity | 0d973251f4e9b759f5aa078e18e2818e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """stack symbolizes native crash dumps."""
  17. import re
  18. import symbol
  19. def PrintTraceLines(trace_lines):
  20. """Print back trace."""
  21. maxlen = max(map(lambda tl: len(tl[1]), trace_lines))
  22. print
  23. print "Stack Trace:"
  24. print " RELADDR " + "FUNCTION".ljust(maxlen) + " FILE:LINE"
  25. for tl in trace_lines:
  26. (addr, symbol_with_offset, location) = tl
  27. print " %8s %s %s" % (addr, symbol_with_offset.ljust(maxlen), location)
  28. return
  29. def PrintValueLines(value_lines):
  30. """Print stack data values."""
  31. maxlen = max(map(lambda tl: len(tl[2]), value_lines))
  32. print
  33. print "Stack Data:"
  34. print " ADDR VALUE " + "FUNCTION".ljust(maxlen) + " FILE:LINE"
  35. for vl in value_lines:
  36. (addr, value, symbol_with_offset, location) = vl
  37. print " %8s %8s %s %s" % (addr, value, symbol_with_offset.ljust(maxlen), location)
  38. return
  39. UNKNOWN = "<unknown>"
  40. HEAP = "[heap]"
  41. STACK = "[stack]"
  42. def PrintOutput(trace_lines, value_lines):
  43. if trace_lines:
  44. PrintTraceLines(trace_lines)
  45. if value_lines:
  46. PrintValueLines(value_lines)
  47. def PrintDivider():
  48. print
  49. print "-----------------------------------------------------\n"
  50. def ConvertTrace(lines):
  51. """Convert strings containing native crash to a stack."""
  52. process_info_line = re.compile("(pid: [0-9]+, tid: [0-9]+.*)")
  53. signal_line = re.compile("(signal [0-9]+ \(.*\).*)")
  54. register_line = re.compile("(([ ]*[0-9a-z]{2} [0-9a-f]{8}){4})")
  55. thread_line = re.compile("(.*)(\-\-\- ){15}\-\-\-")
  56. dalvik_jni_thread_line = re.compile("(\".*\" prio=[0-9]+ tid=[0-9]+ NATIVE.*)")
  57. dalvik_native_thread_line = re.compile("(\".*\" sysTid=[0-9]+ nice=[0-9]+.*)")
  58. width = "{8}"
  59. if symbol.ARCH == "arm64" or symbol.ARCH == "x86_64":
  60. width = "{16}"
  61. # Note that both trace and value line matching allow for variable amounts of
  62. # whitespace (e.g. \t). This is because the we want to allow for the stack
  63. # tool to operate on AndroidFeedback provided system logs. AndroidFeedback
  64. # strips out double spaces that are found in tombsone files and logcat output.
  65. #
  66. # Examples of matched trace lines include lines from tombstone files like:
  67. # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so
  68. # #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so (symbol)
  69. # Or lines from AndroidFeedback crash report system logs like:
  70. # 03-25 00:51:05.520 I/DEBUG ( 65): #00 pc 001cf42e /data/data/com.my.project/lib/libmyproject.so
  71. # Please note the spacing differences.
  72. trace_line = re.compile("(.*)\#([0-9]+)[ \t]+(..)[ \t]+([0-9a-f]" + width + ")[ \t]+([^\r\n \t]*)( \((.*)\))?") # pylint: disable-msg=C6310
  73. # Examples of matched value lines include:
  74. # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so
  75. # bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so (symbol)
  76. # 03-25 00:51:05.530 I/DEBUG ( 65): bea4170c 8018e4e9 /data/data/com.my.project/lib/libmyproject.so
  77. # Again, note the spacing differences.
  78. value_line = re.compile("(.*)([0-9a-f]" + width + ")[ \t]+([0-9a-f]" + width + ")[ \t]+([^\r\n \t]*)( \((.*)\))?")
  79. # Lines from 'code around' sections of the output will be matched before
  80. # value lines because otheriwse the 'code around' sections will be confused as
  81. # value lines.
  82. #
  83. # Examples include:
  84. # 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8
  85. # 03-25 00:51:05.530 I/DEBUG ( 65): 801cf40c ffffc4cc 00b2f2c5 00b2f1c7 00c1e1a8
  86. code_line = re.compile("(.*)[ \t]*[a-f0-9]" + width +
  87. "[ \t]*[a-f0-9]" + width +
  88. "[ \t]*[a-f0-9]" + width +
  89. "[ \t]*[a-f0-9]" + width +
  90. "[ \t]*[a-f0-9]" + width +
  91. "[ \t]*[ \r\n]") # pylint: disable-msg=C6310
  92. trace_lines = []
  93. value_lines = []
  94. last_frame = -1
  95. for ln in lines:
  96. # AndroidFeedback adds zero width spaces into its crash reports. These
  97. # should be removed or the regular expresssions will fail to match.
  98. line = unicode(ln, errors='ignore')
  99. process_header = process_info_line.search(line)
  100. signal_header = signal_line.search(line)
  101. register_header = register_line.search(line)
  102. thread_header = thread_line.search(line)
  103. dalvik_jni_thread_header = dalvik_jni_thread_line.search(line)
  104. dalvik_native_thread_header = dalvik_native_thread_line.search(line)
  105. if process_header or signal_header or register_header or thread_header \
  106. or dalvik_jni_thread_header or dalvik_native_thread_header:
  107. if trace_lines or value_lines:
  108. PrintOutput(trace_lines, value_lines)
  109. PrintDivider()
  110. trace_lines = []
  111. value_lines = []
  112. last_frame = -1
  113. if process_header:
  114. print process_header.group(1)
  115. if signal_header:
  116. print signal_header.group(1)
  117. if register_header:
  118. print register_header.group(1)
  119. if thread_header:
  120. print thread_header.group(1)
  121. if dalvik_jni_thread_header:
  122. print dalvik_jni_thread_header.group(1)
  123. if dalvik_native_thread_header:
  124. print dalvik_native_thread_header.group(1)
  125. continue
  126. if trace_line.match(line):
  127. match = trace_line.match(line)
  128. (unused_0, frame, unused_1,
  129. code_addr, area, symbol_present, symbol_name) = match.groups()
  130. if frame <= last_frame and (trace_lines or value_lines):
  131. PrintOutput(trace_lines, value_lines)
  132. PrintDivider()
  133. trace_lines = []
  134. value_lines = []
  135. last_frame = frame
  136. if area == UNKNOWN or area == HEAP or area == STACK:
  137. trace_lines.append((code_addr, "", area))
  138. else:
  139. # If a calls b which further calls c and c is inlined to b, we want to
  140. # display "a -> b -> c" in the stack trace instead of just "a -> c"
  141. info = symbol.SymbolInformation(area, code_addr)
  142. nest_count = len(info) - 1
  143. for (source_symbol, source_location, object_symbol_with_offset) in info:
  144. if not source_symbol:
  145. if symbol_present:
  146. source_symbol = symbol.CallCppFilt(symbol_name)
  147. else:
  148. source_symbol = UNKNOWN
  149. if not source_location:
  150. source_location = area
  151. if nest_count > 0:
  152. nest_count = nest_count - 1
  153. trace_lines.append(("v------>", source_symbol, source_location))
  154. else:
  155. if not object_symbol_with_offset:
  156. object_symbol_with_offset = source_symbol
  157. trace_lines.append((code_addr,
  158. object_symbol_with_offset,
  159. source_location))
  160. if code_line.match(line):
  161. # Code lines should be ignored. If this were exluded the 'code around'
  162. # sections would trigger value_line matches.
  163. continue;
  164. if value_line.match(line):
  165. match = value_line.match(line)
  166. (unused_, addr, value, area, symbol_present, symbol_name) = match.groups()
  167. if area == UNKNOWN or area == HEAP or area == STACK or not area:
  168. value_lines.append((addr, value, "", area))
  169. else:
  170. info = symbol.SymbolInformation(area, value)
  171. (source_symbol, source_location, object_symbol_with_offset) = info.pop()
  172. if not source_symbol:
  173. if symbol_present:
  174. source_symbol = symbol.CallCppFilt(symbol_name)
  175. else:
  176. source_symbol = UNKNOWN
  177. if not source_location:
  178. source_location = area
  179. if not object_symbol_with_offset:
  180. object_symbol_with_offset = source_symbol
  181. value_lines.append((addr,
  182. value,
  183. object_symbol_with_offset,
  184. source_location))
  185. PrintOutput(trace_lines, value_lines)
  186. # vi: ts=2 sw=2