gdb_panic_server.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. 'esp32c6': GDB_REGS_INFO_RISCV_ILP32
  55. }
  56. PanicInfo = namedtuple('PanicInfo', 'core_id regs stack_base_addr stack_data')
  57. def build_riscv_panic_output_parser(): # type: () -> typing.Any[typing.Type[ParserElement]]
  58. """Builds a parser for the panic handler output using pyparsing"""
  59. # We don't match the first line, since "Guru Meditation" will not be printed in case of an abort:
  60. # Guru Meditation Error: Core 0 panic'ed (Store access fault). Exception was unhandled.
  61. # Core 0 register dump:
  62. reg_dump_header = Group(Literal('Core') +
  63. Word(nums)('core_id') +
  64. Literal('register dump:'))('reg_dump_header')
  65. # MEPC : 0x4200232c RA : 0x42009694 SP : 0x3fc93a80 GP : 0x3fc8b320
  66. reg_name = Word(srange('[A-Z_0-9/-]'))('name')
  67. hexnumber_with_0x = Combine(Literal('0x') + Word(hexnumber))
  68. reg_value = hexnumber_with_0x('value')
  69. reg_dump_one_reg = Group(reg_name + Literal(':') + reg_value) # not named because there will be OneOrMore
  70. reg_dump_all_regs = Group(OneOrMore(reg_dump_one_reg))('regs')
  71. reg_dump = Group(reg_dump_header + reg_dump_all_regs) # not named because there will be OneOrMore
  72. reg_dumps = Group(OneOrMore(reg_dump))('reg_dumps')
  73. # Stack memory:
  74. # 3fc93a80: 0x00000030 0x00000021 0x3fc8aedc 0x4200232a 0xa5a5a5a5 0xa5a5a5a5 0x3fc8aedc 0x420099b0
  75. stack_line = Group(Word(hexnumber)('base') + Literal(':') +
  76. Group(OneOrMore(hexnumber_with_0x))('data'))
  77. stack_dump = Group(Literal('Stack memory:') +
  78. Group(OneOrMore(stack_line))('lines'))('stack_dump')
  79. # Parser for the complete panic output:
  80. panic_output = reg_dumps + stack_dump
  81. return panic_output
  82. def get_stack_addr_and_data(res): # type: (ParseResults) -> typing.Tuple[int, bytes]
  83. """ Extract base address and bytes from the parsed stack dump """
  84. stack_base_addr = 0 # First reported address in the dump
  85. base_addr = 0 # keeps track of the address for the given line of the dump
  86. bytes_in_line = 0 # bytes of stack parsed on the previous line; used to validate the next base address
  87. stack_data = bytes(b'') # accumulates all the dumped stack data
  88. for line in res.stack_dump.lines:
  89. # update and validate the base address
  90. prev_base_addr = base_addr
  91. base_addr = int(line.base, 16)
  92. if stack_base_addr == 0:
  93. stack_base_addr = base_addr
  94. else:
  95. assert base_addr == prev_base_addr + bytes_in_line
  96. # convert little-endian hex words to byte representation
  97. words = [int(w, 16) for w in line.data]
  98. line_data = bytes(b''.join([struct.pack('<I', w) for w in words]))
  99. bytes_in_line = len(line_data)
  100. # accumulate in the whole stack data
  101. stack_data += line_data
  102. return stack_base_addr, stack_data
  103. def parse_idf_riscv_panic_output(panic_text): # type: (str) -> PanicInfo
  104. """ Decode panic handler output from a file """
  105. panic_output = build_riscv_panic_output_parser()
  106. results = panic_output.searchString(panic_text)
  107. if len(results) != 1:
  108. raise ValueError("Couldn't parse panic handler output")
  109. res = results[0]
  110. if len(res.reg_dumps) > 1:
  111. raise NotImplementedError('Handling of multi-core register dumps not implemented')
  112. # Build a dict of register names/values
  113. rd = res.reg_dumps[0]
  114. core_id = int(rd.reg_dump_header.core_id)
  115. regs = dict()
  116. for reg in rd.regs:
  117. reg_value = int(reg.value, 16)
  118. regs[reg.name] = reg_value
  119. stack_base_addr, stack_data = get_stack_addr_and_data(res)
  120. return PanicInfo(core_id=core_id,
  121. regs=regs,
  122. stack_base_addr=stack_base_addr,
  123. stack_data=stack_data)
  124. PANIC_OUTPUT_PARSERS = {
  125. 'esp32c3': parse_idf_riscv_panic_output,
  126. 'esp32c2': parse_idf_riscv_panic_output,
  127. 'esp32h2': parse_idf_riscv_panic_output,
  128. 'esp32c6': parse_idf_riscv_panic_output
  129. }
  130. class GdbServer(object):
  131. def __init__(self, panic_info, target, log_file=None): # type: (PanicInfo, str, str) -> None
  132. self.panic_info = panic_info
  133. self.in_stream = sys.stdin
  134. self.out_stream = sys.stdout
  135. self.reg_list = GDB_REGS_INFO[target]
  136. self.logger = logging.getLogger('GdbServer')
  137. if log_file:
  138. handler = logging.FileHandler(log_file, 'w+')
  139. self.logger.setLevel(logging.DEBUG)
  140. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  141. handler.setFormatter(formatter)
  142. self.logger.addHandler(handler)
  143. def run(self): # type: () -> None
  144. """ Process GDB commands from stdin until GDB tells us to quit """
  145. buffer = ''
  146. while True:
  147. buffer += self.in_stream.read(1)
  148. if len(buffer) > 3 and buffer[-3] == '#':
  149. self._handle_command(buffer)
  150. buffer = ''
  151. def _handle_command(self, buffer): # type: (str) -> None
  152. command = buffer[1:-3] # ignore checksums
  153. # Acknowledge the command
  154. self.out_stream.write('+')
  155. self.out_stream.flush()
  156. self.logger.debug('Got command: %s', command)
  157. if command == '?':
  158. # report sigtrap as the stop reason; the exact reason doesn't matter for backtracing
  159. self._respond('T05')
  160. elif command.startswith('Hg') or command.startswith('Hc'):
  161. # Select thread command
  162. self._respond('OK')
  163. elif command == 'qfThreadInfo':
  164. # Get list of threads.
  165. # Only one thread for now, can be extended to show one thread for each core,
  166. # if we dump both cores (e.g. on an interrupt watchdog)
  167. self._respond('m1')
  168. elif command == 'qC':
  169. # That single thread is selected.
  170. self._respond('QC1')
  171. elif command == 'g':
  172. # Registers read
  173. self._respond_regs()
  174. elif command.startswith('m'):
  175. # Memory read
  176. addr, size = [int(v, 16) for v in command[1:].split(',')]
  177. self._respond_mem(addr, size)
  178. elif command.startswith('vKill') or command == 'k':
  179. # Quit
  180. self._respond('OK')
  181. raise SystemExit(0)
  182. else:
  183. # Empty response required for any unknown command
  184. self._respond('')
  185. def _respond(self, data): # type: (str) -> None
  186. # calculate checksum
  187. data_bytes = bytes(data.encode('ascii')) # bytes() for Py2 compatibility
  188. checksum = sum(data_bytes) & 0xff
  189. # format and write the response
  190. res = '${}#{:02x}'.format(data, checksum)
  191. self.logger.debug('Wrote: %s', res)
  192. self.out_stream.write(res)
  193. self.out_stream.flush()
  194. # get the result ('+' or '-')
  195. ret = self.in_stream.read(1)
  196. self.logger.debug('Response: %s', ret)
  197. if ret != '+':
  198. sys.stderr.write("GDB responded with '-' to {}".format(res))
  199. raise SystemExit(1)
  200. def _respond_regs(self): # type: () -> None
  201. response = ''
  202. for reg_name in self.reg_list:
  203. # register values are reported as hexadecimal strings
  204. # in target byte order (i.e. LSB first for RISC-V)
  205. reg_val = self.panic_info.regs.get(reg_name, 0)
  206. reg_bytes = struct.pack('<L', reg_val)
  207. response += binascii.hexlify(reg_bytes).decode('ascii')
  208. self._respond(response)
  209. def _respond_mem(self, start_addr, size): # type: (int, int) -> None
  210. stack_addr_min = self.panic_info.stack_base_addr
  211. stack_data = self.panic_info.stack_data
  212. stack_len = len(self.panic_info.stack_data)
  213. stack_addr_max = stack_addr_min + stack_len
  214. # For any memory address that is not on the stack, pretend the value is 0x00.
  215. # GDB should never ask us for program memory, it will be obtained from the ELF file.
  216. def in_stack(addr): # type: (int) -> typing.Any[bool]
  217. return stack_addr_min <= addr < stack_addr_max
  218. result = ''
  219. for addr in range(start_addr, start_addr + size):
  220. if not in_stack(addr):
  221. result += '00'
  222. else:
  223. result += '{:02x}'.format(stack_data[addr - stack_addr_min])
  224. self._respond(result)
  225. def main(): # type: () -> None
  226. parser = argparse.ArgumentParser()
  227. parser.add_argument('input_file', type=argparse.FileType('r'),
  228. help='File containing the panic handler output')
  229. parser.add_argument('--target', choices=GDB_REGS_INFO.keys(),
  230. help='Chip to use (determines the architecture)')
  231. parser.add_argument('--gdb-log', default=None,
  232. help='If specified, the file for logging GDB server debug information')
  233. args = parser.parse_args()
  234. panic_info = PANIC_OUTPUT_PARSERS[args.target](args.input_file.read())
  235. server = GdbServer(panic_info, target=args.target, log_file=args.gdb_log)
  236. try:
  237. server.run()
  238. except KeyboardInterrupt:
  239. sys.exit(0)
  240. if __name__ == '__main__':
  241. main()