traceparse.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. # This script decodes Xtensa CPU trace dumps. It allows tracing the program
  5. # execution at instruction level.
  6. #
  7. # Some trivia about the Xtensa CPU trace (TRAX):
  8. # TRAX format mostly follows the IEEE-ISTO 5001-2003 (Nexus) standard.
  9. # The following Nexus Program Trace messages are implemented by TRAX:
  10. # - Indirect Branch Message
  11. # - Syncronization Message
  12. # - Indirect Branch with Synchronization Message
  13. # - Correlation Message
  14. # TRAX outputs compressed traces with 2 MSEO bits (LSB) and 6 MDO bits (MSB),
  15. # packed into a byte. MSEO bits are used to split the stream into packets and messages,
  16. # and MDO bits carry the actual data of the messages. Each message may contain multiple packets.
  17. #
  18. # This script can be used standalone, or loaded into GDB.
  19. # When used standalone, it dumps the list of trace messages to stdout.
  20. # When used from GDB, it also invokes GDB command to dump the list of assembly
  21. # instructions corresponding to each of the messages.
  22. #
  23. # Standalone usage:
  24. # traceparse.py <dump_file>
  25. #
  26. # Usage from GDB:
  27. # xtensa-esp32-elf-gdb -n --batch program.elf -x gdbinit
  28. # with the following gdbinit script:
  29. # set pagination off
  30. # set confirm off
  31. # add-symbol-file rom.elf <address of ROM .text section>
  32. # source traceparse.py
  33. # python parse_and_dump("/path/to/dump_file")
  34. #
  35. # Loading the ROM code is optional; if not loaded, disassembly for ROM sections of code
  36. # will be missing.
  37. #
  38. ###
  39. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  40. #
  41. # Licensed under the Apache License, Version 2.0 (the "License");
  42. # you may not use this file except in compliance with the License.
  43. # You may obtain a copy of the License at
  44. #
  45. # http://www.apache.org/licenses/LICENSE-2.0
  46. #
  47. # Unless required by applicable law or agreed to in writing, software
  48. # distributed under the License is distributed on an "AS IS" BASIS,
  49. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50. # See the License for the specific language governing permissions and
  51. # limitations under the License.
  52. from __future__ import print_function
  53. import sys
  54. # Check if loaded into GDB
  55. try:
  56. assert gdb.__name__ == 'gdb' # type: ignore
  57. WITH_GDB = True
  58. except NameError:
  59. WITH_GDB = False
  60. # MSEO bit masks:
  61. MSEO_PKTEND = 1 << 0 # bit 0: indicates the last byte of a packet
  62. MSEO_MSGEND = 1 << 1 # bit 1: indicates the last byte of the message
  63. # Message types. The type is stored in the first 6 MDO bits or the first packet.
  64. TVAL_INDBR = 4 # Indirect branch
  65. TVAL_INDBRSYNC = 12 # Indirect branch w/ synchronisation
  66. TVAL_SYNC = 9 # Synchronisation msg
  67. TVAL_CORR = 33 # Correlation message
  68. class TraxPacket(object):
  69. def __init__(self, data, truncated=False):
  70. self.data = data
  71. self.size_bytes = len(data)
  72. self.truncated = truncated
  73. def get_bits(self, start, count=0):
  74. """
  75. Extract data bits from the packet
  76. :param start: offset, in bits, of the part to be extracted
  77. :param count: number of bits to extract; if omitted or zero,
  78. extracts until the end of the packet
  79. :return: integer containing the extracted bits
  80. """
  81. start_byte = start // 6
  82. if count <= 0:
  83. # all remaining bits
  84. count = len(self.data) * 6 - start
  85. bits_remaining = count
  86. result = 0
  87. shift = 0
  88. for i, b in enumerate(self.data[start_byte:]):
  89. # which bit in the byte is the starting bit
  90. if i == 0:
  91. # at start_byte: take the offset into account
  92. start_bit = 2 + (start % 6)
  93. else:
  94. # every other byte: start after MSEO bits
  95. start_bit = 2
  96. # how many bits do we need to copy from this byte
  97. cnt_bits = min(bits_remaining, 8 - start_bit)
  98. mask = (2 ** cnt_bits) - 1
  99. # take this many bits after the start_bit
  100. bits = (b >> start_bit) & mask
  101. # add these bits to the result
  102. result |= bits << shift
  103. # update the remaining bit count
  104. shift += cnt_bits
  105. bits_remaining -= cnt_bits
  106. if bits_remaining == 0:
  107. break
  108. return result
  109. def __str__(self):
  110. return '%d byte packet%s' % (self.size_bytes, ' (truncated)' if self.truncated else '')
  111. class TraxMessage(object):
  112. def __init__(self, packets, truncated=False):
  113. """
  114. Create and parse a TRAX message from packets
  115. :param packets: list of TraxPacket objects, must not be empty
  116. :param truncated: whether the message was truncated in the stream
  117. """
  118. assert len(packets) > 0
  119. self.packets = packets
  120. self.truncated = truncated
  121. if truncated:
  122. self.msg_type = None
  123. else:
  124. self.msg_type = self._get_type()
  125. # Start and end of the instruction range corresponding to this message
  126. self.pc_start = 0 # inclusive
  127. self.pc_end = 0 # not inclusive
  128. self.pc_target = 0 # PC of the next range
  129. self.is_exception = False # whether the message indicates an exception
  130. self.is_correlation = False # whether this is a correlation message
  131. # message-specific fields
  132. self.icnt = 0
  133. self.uaddr = 0
  134. self.dcont = 0
  135. # decode the fields
  136. if not truncated:
  137. self._decode()
  138. def _get_type(self):
  139. """
  140. :return: Message type, one of TVAL_XXX values
  141. """
  142. return self.packets[0].get_bits(0, 6)
  143. def _decode(self):
  144. """ Parse the packets and fill in the message-specific fields """
  145. if self.msg_type == TVAL_INDBR:
  146. self.icnt = self.packets[0].get_bits(7, -1)
  147. self.btype = self.packets[0].get_bits(6, 1)
  148. self.uaddr = self.packets[1].get_bits(0)
  149. self.is_exception = self.btype > 0
  150. elif self.msg_type == TVAL_INDBRSYNC:
  151. self.icnt = self.packets[0].get_bits(8, -1)
  152. self.btype = self.packets[0].get_bits(7, 1)
  153. self.pc_target = self.packets[1].get_bits(0)
  154. self.dcont = self.packets[0].get_bits(6, 1)
  155. self.is_exception = self.btype > 0
  156. elif self.msg_type == TVAL_SYNC:
  157. self.icnt = self.packets[0].get_bits(7, -1)
  158. self.dcont = self.packets[0].get_bits(6, 1)
  159. self.pc_target = self.packets[1].get_bits(0)
  160. elif self.msg_type == TVAL_CORR:
  161. self.icnt = self.packets[0].get_bits(12, -1)
  162. self.is_correlation = True
  163. else:
  164. raise NotImplementedError('Unknown message type (%d)' % self.msg_type)
  165. def process_forward(self, cur_pc):
  166. """
  167. Given the target PC known from the previous message, determine
  168. the PC range corresponding to the current message.
  169. :param cur_pc: previous known PC
  170. :return: target PC after the current message
  171. """
  172. assert not self.truncated
  173. next_pc = cur_pc
  174. if self.msg_type == TVAL_INDBR:
  175. next_pc = cur_pc ^ self.uaddr
  176. self.pc_target = next_pc
  177. self.pc_start = cur_pc
  178. self.pc_end = self.pc_start + self.icnt + 1
  179. if self.msg_type == TVAL_INDBRSYNC:
  180. next_pc = self.pc_target
  181. self.pc_start = cur_pc
  182. self.pc_end = cur_pc + self.icnt + 1
  183. if self.msg_type == TVAL_SYNC:
  184. next_pc = self.pc_target
  185. self.pc_start = next_pc - self.icnt
  186. self.pc_end = next_pc + 1
  187. if self.msg_type == TVAL_CORR:
  188. pass
  189. return next_pc
  190. def process_backward(self, cur_pc):
  191. """
  192. Given the address of the PC known from the _next_ message, determine
  193. the PC range corresponding to the current message.
  194. :param cur_pc: next known PC
  195. :return: target PC of the _previous_ message
  196. """
  197. assert not self.truncated
  198. # Backward pass is only used to resolve addresses of messages
  199. # up to the first SYNC/INDBRSYNC message.
  200. # SYNC/INDBRSYNC messages are only handled in the forward pass.
  201. assert self.msg_type != TVAL_INDBRSYNC
  202. assert self.msg_type != TVAL_SYNC
  203. prev_pc = cur_pc
  204. self.pc_target = cur_pc
  205. if self.msg_type == TVAL_INDBR:
  206. prev_pc ^= self.uaddr
  207. self.pc_start = prev_pc
  208. self.pc_end = prev_pc + self.icnt + 1
  209. if self.msg_type == TVAL_CORR:
  210. pass
  211. return prev_pc
  212. def __str__(self):
  213. desc = 'Unknown (%d)' % self.msg_type
  214. extra = ''
  215. if self.truncated:
  216. desc = 'Truncated'
  217. if self.msg_type == TVAL_INDBR:
  218. desc = 'Indirect branch'
  219. extra = ', icnt=%d, uaddr=0x%x, exc=%d' % (self.icnt, self.uaddr, self.is_exception)
  220. if self.msg_type == TVAL_INDBRSYNC:
  221. desc = 'Indirect branch w/sync'
  222. extra = ', icnt=%d, dcont=%d, exc=%d' % (self.icnt, self.dcont, self.is_exception)
  223. if self.msg_type == TVAL_SYNC:
  224. desc = 'Synchronization'
  225. extra = ', icnt=%d, dcont=%d' % (self.icnt, self.dcont)
  226. if self.msg_type == TVAL_CORR:
  227. desc = 'Correlation'
  228. extra = ', icnt=%d' % self.icnt
  229. return '%s message, %d packets, PC range 0x%08x - 0x%08x, target PC 0x%08x' % (
  230. desc, len(self.packets), self.pc_start, self.pc_end, self.pc_target) + extra
  231. def load_messages(data):
  232. """
  233. Decodes TRAX data and resolves PC ranges.
  234. :param data: input data, bytes
  235. :return: list of TraxMessage objects
  236. """
  237. messages = []
  238. packets = []
  239. packet_start = 0
  240. msg_cnt = 0
  241. pkt_cnt = 0
  242. # Iterate over the input data, splitting bytes into packets and messages
  243. for i, b in enumerate(data):
  244. if (b & MSEO_MSGEND) and not (b & MSEO_PKTEND):
  245. raise AssertionError('Invalid MSEO bits in b=0x%x. Not a TRAX dump?' % b)
  246. if b & MSEO_PKTEND:
  247. pkt_cnt += 1
  248. packets.append(TraxPacket(data[packet_start:i + 1], packet_start == 0))
  249. packet_start = i + 1
  250. if b & MSEO_MSGEND:
  251. msg_cnt += 1
  252. try:
  253. messages.append(TraxMessage(packets, len(messages) == 0))
  254. except NotImplementedError as e:
  255. sys.stderr.write('Failed to parse message #%03d (at %d bytes): %s\n' % (msg_cnt, i, str(e)))
  256. packets = []
  257. # Resolve PC ranges of messages.
  258. # Forward pass: skip messages until a message with known PC,
  259. # i.e. a SYNC/INDBRSYNC message. Process all messages following it.
  260. pc = 0
  261. first_sync_index = -1
  262. for i, m in enumerate(messages):
  263. if pc == 0 and m.pc_target == 0:
  264. continue
  265. if first_sync_index < 0:
  266. first_sync_index = i
  267. pc = m.process_forward(pc)
  268. # Now process the skipped messages in the reverse direction,
  269. # starting from the first message with known PC.
  270. pc = messages[first_sync_index].pc_start
  271. for m in reversed(messages[0:first_sync_index]):
  272. if m.truncated:
  273. break
  274. pc = m.process_backward(pc)
  275. return messages
  276. def parse_and_dump(filename, disassemble=WITH_GDB):
  277. """
  278. Decode TRAX data from a file, print out the messages.
  279. :param filename: file to load the dump from
  280. :param disassemble: if True, print disassembly of PC ranges
  281. """
  282. with open(filename, 'rb') as f:
  283. data = f.read()
  284. messages = load_messages(data)
  285. sys.stderr.write('Loaded %d messages in %d bytes\n' % (len(messages), len(data)))
  286. for i, m in enumerate(messages):
  287. if m.truncated:
  288. continue
  289. print('%04d: %s' % (i, str(m)))
  290. if m.is_exception:
  291. print('*** Exception occurred ***')
  292. if disassemble and WITH_GDB:
  293. try:
  294. gdb.execute('disassemble 0x%08x, 0x%08x' % (m.pc_start, m.pc_end)) # noqa: F821
  295. except gdb.MemoryError: # noqa: F821
  296. print('Failed to disassemble from 0x%08x to 0x%08x' % (m.pc_start, m.pc_end))
  297. def main():
  298. if len(sys.argv) < 2:
  299. sys.stderr.write('Usage: %s <dump_file>\n')
  300. raise SystemExit(1)
  301. parse_and_dump(sys.argv[1])
  302. if __name__ == '__main__' and not WITH_GDB:
  303. main()