gdb_panic_server.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. # A script which parses ESP-IDF panic handler output (registers & stack dump),
  5. # and then acts as a GDB server over stdin/stdout, presenting the information
  6. # from the panic handler to GDB.
  7. # This allows for generating backtraces out of raw stack dumps on architectures
  8. # where backtracing on the target side is not possible.
  9. #
  10. # Note that the "act as a GDB server" approach is somewhat a hack.
  11. # A much nicer solution would have been to convert the panic handler output
  12. # into a core file, and point GDB to the core file.
  13. # However, RISC-V baremetal GDB currently lacks core dump support.
  14. #
  15. # The approach is inspired by Cesanta's ESP8266 GDB server:
  16. # https://github.com/cesanta/mongoose-os/blob/27777c8977/platforms/esp8266/tools/serve_core.py
  17. #
  18. # SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  19. # SPDX-License-Identifier: Apache-2.0
  20. #
  21. import argparse
  22. import binascii
  23. import logging
  24. import struct
  25. import sys
  26. from collections import namedtuple
  27. # Used for type annotations only. Silence linter warnings.
  28. from pyparsing import (Combine, Group, Literal, OneOrMore, ParserElement, # noqa: F401 # pylint: disable=unused-import
  29. ParseResults, Word, nums, srange)
  30. try:
  31. import typing # noqa: F401 # pylint: disable=unused-import
  32. except ImportError:
  33. pass
  34. # pyparsing helper
  35. hexnumber = srange('[0-9a-f]')
  36. # List of registers to be passed to GDB, in the order GDB expects.
  37. # The names should match those used in IDF panic handler.
  38. # Registers not present in IDF panic handler output (like X0) will be assumed to be 0.
  39. GDB_REGS_INFO_RISCV_ILP32 = [
  40. 'X0', 'RA', 'SP', 'GP',
  41. 'TP', 'T0', 'T1', 'T2',
  42. 'S0/FP', 'S1', 'A0', 'A1',
  43. 'A2', 'A3', 'A4', 'A5',
  44. 'A6', 'A7', 'S2', 'S3',
  45. 'S4', 'S5', 'S6', 'S7',
  46. 'S8', 'S9', 'S10', 'S11',
  47. 'T3', 'T4', 'T5', 'T6',
  48. 'MEPC'
  49. ]
  50. GDB_REGS_INFO = {
  51. 'esp32c3': GDB_REGS_INFO_RISCV_ILP32,
  52. 'esp32c2': GDB_REGS_INFO_RISCV_ILP32,
  53. 'esp32h2': GDB_REGS_INFO_RISCV_ILP32
  54. }
  55. PanicInfo = namedtuple('PanicInfo', 'core_id regs stack_base_addr stack_data')
  56. def build_riscv_panic_output_parser(): # type: () -> typing.Any[typing.Type[ParserElement]]
  57. """Builds a parser for the panic handler output using pyparsing"""
  58. # We don't match the first line, since "Guru Meditation" will not be printed in case of an abort:
  59. # Guru Meditation Error: Core 0 panic'ed (Store access fault). Exception was unhandled.
  60. # Core 0 register dump:
  61. reg_dump_header = Group(Literal('Core') +
  62. Word(nums)('core_id') +
  63. Literal('register dump:'))('reg_dump_header')
  64. # MEPC : 0x4200232c RA : 0x42009694 SP : 0x3fc93a80 GP : 0x3fc8b320
  65. reg_name = Word(srange('[A-Z_0-9/-]'))('name')
  66. hexnumber_with_0x = Combine(Literal('0x') + Word(hexnumber))
  67. reg_value = hexnumber_with_0x('value')
  68. reg_dump_one_reg = Group(reg_name + Literal(':') + reg_value) # not named because there will be OneOrMore
  69. reg_dump_all_regs = Group(OneOrMore(reg_dump_one_reg))('regs')
  70. reg_dump = Group(reg_dump_header + reg_dump_all_regs) # not named because there will be OneOrMore
  71. reg_dumps = Group(OneOrMore(reg_dump))('reg_dumps')
  72. # Stack memory:
  73. # 3fc93a80: 0x00000030 0x00000021 0x3fc8aedc 0x4200232a 0xa5a5a5a5 0xa5a5a5a5 0x3fc8aedc 0x420099b0
  74. stack_line = Group(Word(hexnumber)('base') + Literal(':') +
  75. Group(OneOrMore(hexnumber_with_0x))('data'))
  76. stack_dump = Group(Literal('Stack memory:') +
  77. Group(OneOrMore(stack_line))('lines'))('stack_dump')
  78. # Parser for the complete panic output:
  79. panic_output = reg_dumps + stack_dump
  80. return panic_output
  81. def get_stack_addr_and_data(res): # type: (ParseResults) -> typing.Tuple[int, bytes]
  82. """ Extract base address and bytes from the parsed stack dump """
  83. stack_base_addr = 0 # First reported address in the dump
  84. base_addr = 0 # keeps track of the address for the given line of the dump
  85. bytes_in_line = 0 # bytes of stack parsed on the previous line; used to validate the next base address
  86. stack_data = bytes(b'') # accumulates all the dumped stack data
  87. for line in res.stack_dump.lines:
  88. # update and validate the base address
  89. prev_base_addr = base_addr
  90. base_addr = int(line.base, 16)
  91. if stack_base_addr == 0:
  92. stack_base_addr = base_addr
  93. else:
  94. assert base_addr == prev_base_addr + bytes_in_line
  95. # convert little-endian hex words to byte representation
  96. words = [int(w, 16) for w in line.data]
  97. line_data = bytes(b''.join([struct.pack('<I', w) for w in words]))
  98. bytes_in_line = len(line_data)
  99. # accumulate in the whole stack data
  100. stack_data += line_data
  101. return stack_base_addr, stack_data
  102. def parse_idf_riscv_panic_output(panic_text): # type: (str) -> PanicInfo
  103. """ Decode panic handler output from a file """
  104. panic_output = build_riscv_panic_output_parser()
  105. results = panic_output.searchString(panic_text)
  106. if len(results) != 1:
  107. raise ValueError("Couldn't parse panic handler output")
  108. res = results[0]
  109. if len(res.reg_dumps) > 1:
  110. raise NotImplementedError('Handling of multi-core register dumps not implemented')
  111. # Build a dict of register names/values
  112. rd = res.reg_dumps[0]
  113. core_id = int(rd.reg_dump_header.core_id)
  114. regs = dict()
  115. for reg in rd.regs:
  116. reg_value = int(reg.value, 16)
  117. regs[reg.name] = reg_value
  118. stack_base_addr, stack_data = get_stack_addr_and_data(res)
  119. return PanicInfo(core_id=core_id,
  120. regs=regs,
  121. stack_base_addr=stack_base_addr,
  122. stack_data=stack_data)
  123. PANIC_OUTPUT_PARSERS = {
  124. 'esp32c3': parse_idf_riscv_panic_output,
  125. 'esp32c2': parse_idf_riscv_panic_output,
  126. 'esp32h2': parse_idf_riscv_panic_output
  127. }
  128. class GdbServer(object):
  129. def __init__(self, panic_info, target, log_file=None): # type: (PanicInfo, str, str) -> None
  130. self.panic_info = panic_info
  131. self.in_stream = sys.stdin
  132. self.out_stream = sys.stdout
  133. self.reg_list = GDB_REGS_INFO[target]
  134. self.logger = logging.getLogger('GdbServer')
  135. if log_file:
  136. handler = logging.FileHandler(log_file, 'w+')
  137. self.logger.setLevel(logging.DEBUG)
  138. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  139. handler.setFormatter(formatter)
  140. self.logger.addHandler(handler)
  141. def run(self): # type: () -> None
  142. """ Process GDB commands from stdin until GDB tells us to quit """
  143. buffer = ''
  144. while True:
  145. buffer += self.in_stream.read(1)
  146. if len(buffer) > 3 and buffer[-3] == '#':
  147. self._handle_command(buffer)
  148. buffer = ''
  149. def _handle_command(self, buffer): # type: (str) -> None
  150. command = buffer[1:-3] # ignore checksums
  151. # Acknowledge the command
  152. self.out_stream.write('+')
  153. self.out_stream.flush()
  154. self.logger.debug('Got command: %s', command)
  155. if command == '?':
  156. # report sigtrap as the stop reason; the exact reason doesn't matter for backtracing
  157. self._respond('T05')
  158. elif command.startswith('Hg') or command.startswith('Hc'):
  159. # Select thread command
  160. self._respond('OK')
  161. elif command == 'qfThreadInfo':
  162. # Get list of threads.
  163. # Only one thread for now, can be extended to show one thread for each core,
  164. # if we dump both cores (e.g. on an interrupt watchdog)
  165. self._respond('m1')
  166. elif command == 'qC':
  167. # That single thread is selected.
  168. self._respond('QC1')
  169. elif command == 'g':
  170. # Registers read
  171. self._respond_regs()
  172. elif command.startswith('m'):
  173. # Memory read
  174. addr, size = [int(v, 16) for v in command[1:].split(',')]
  175. self._respond_mem(addr, size)
  176. elif command.startswith('vKill') or command == 'k':
  177. # Quit
  178. self._respond('OK')
  179. raise SystemExit(0)
  180. else:
  181. # Empty response required for any unknown command
  182. self._respond('')
  183. def _respond(self, data): # type: (str) -> None
  184. # calculate checksum
  185. data_bytes = bytes(data.encode('ascii')) # bytes() for Py2 compatibility
  186. checksum = sum(data_bytes) & 0xff
  187. # format and write the response
  188. res = '${}#{:02x}'.format(data, checksum)
  189. self.logger.debug('Wrote: %s', res)
  190. self.out_stream.write(res)
  191. self.out_stream.flush()
  192. # get the result ('+' or '-')
  193. ret = self.in_stream.read(1)
  194. self.logger.debug('Response: %s', ret)
  195. if ret != '+':
  196. sys.stderr.write("GDB responded with '-' to {}".format(res))
  197. raise SystemExit(1)
  198. def _respond_regs(self): # type: () -> None
  199. response = ''
  200. for reg_name in self.reg_list:
  201. # register values are reported as hexadecimal strings
  202. # in target byte order (i.e. LSB first for RISC-V)
  203. reg_val = self.panic_info.regs.get(reg_name, 0)
  204. reg_bytes = struct.pack('<L', reg_val)
  205. response += binascii.hexlify(reg_bytes).decode('ascii')
  206. self._respond(response)
  207. def _respond_mem(self, start_addr, size): # type: (int, int) -> None
  208. stack_addr_min = self.panic_info.stack_base_addr
  209. stack_data = self.panic_info.stack_data
  210. stack_len = len(self.panic_info.stack_data)
  211. stack_addr_max = stack_addr_min + stack_len
  212. # For any memory address that is not on the stack, pretend the value is 0x00.
  213. # GDB should never ask us for program memory, it will be obtained from the ELF file.
  214. def in_stack(addr): # type: (int) -> typing.Any[bool]
  215. return stack_addr_min <= addr < stack_addr_max
  216. result = ''
  217. for addr in range(start_addr, start_addr + size):
  218. if not in_stack(addr):
  219. result += '00'
  220. else:
  221. result += '{:02x}'.format(stack_data[addr - stack_addr_min])
  222. self._respond(result)
  223. def main(): # type: () -> None
  224. parser = argparse.ArgumentParser()
  225. parser.add_argument('input_file', type=argparse.FileType('r'),
  226. help='File containing the panic handler output')
  227. parser.add_argument('--target', choices=GDB_REGS_INFO.keys(),
  228. help='Chip to use (determines the architecture)')
  229. parser.add_argument('--gdb-log', default=None,
  230. help='If specified, the file for logging GDB server debug information')
  231. args = parser.parse_args()
  232. panic_info = PANIC_OUTPUT_PARSERS[args.target](args.input_file.read())
  233. server = GdbServer(panic_info, target=args.target, log_file=args.gdb_log)
  234. try:
  235. server.run()
  236. except KeyboardInterrupt:
  237. sys.exit(0)
  238. if __name__ == '__main__':
  239. main()