gdb_panic_server.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 builtins import bytes
  27. from collections import namedtuple
  28. # Used for type annotations only. Silence linter warnings.
  29. from pyparsing import (Combine, Group, Literal, OneOrMore, ParserElement, # noqa: F401 # pylint: disable=unused-import
  30. ParseResults, Word, nums, srange)
  31. try:
  32. import typing # noqa: F401 # pylint: disable=unused-import
  33. except ImportError:
  34. pass
  35. # pyparsing helper
  36. hexnumber = srange('[0-9a-f]')
  37. # List of registers to be passed to GDB, in the order GDB expects.
  38. # The names should match those used in IDF panic handler.
  39. # Registers not present in IDF panic handler output (like X0) will be assumed to be 0.
  40. GDB_REGS_INFO_RISCV_ILP32 = [
  41. 'X0', 'RA', 'SP', 'GP',
  42. 'TP', 'T0', 'T1', 'T2',
  43. 'S0/FP', 'S1', 'A0', 'A1',
  44. 'A2', 'A3', 'A4', 'A5',
  45. 'A6', 'A7', 'S2', 'S3',
  46. 'S4', 'S5', 'S6', 'S7',
  47. 'S8', 'S9', 'S10', 'S11',
  48. 'T3', 'T4', 'T5', 'T6',
  49. 'MEPC'
  50. ]
  51. GDB_REGS_INFO = {
  52. 'esp32c3': GDB_REGS_INFO_RISCV_ILP32,
  53. 'esp32c2': GDB_REGS_INFO_RISCV_ILP32,
  54. 'esp32h2': 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. }
  129. class GdbServer(object):
  130. def __init__(self, panic_info, target, log_file=None): # type: (PanicInfo, str, str) -> None
  131. self.panic_info = panic_info
  132. self.in_stream = sys.stdin
  133. self.out_stream = sys.stdout
  134. self.reg_list = GDB_REGS_INFO[target]
  135. self.logger = logging.getLogger('GdbServer')
  136. if log_file:
  137. handler = logging.FileHandler(log_file, 'w+')
  138. self.logger.setLevel(logging.DEBUG)
  139. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  140. handler.setFormatter(formatter)
  141. self.logger.addHandler(handler)
  142. def run(self): # type: () -> None
  143. """ Process GDB commands from stdin until GDB tells us to quit """
  144. buffer = ''
  145. while True:
  146. buffer += self.in_stream.read(1)
  147. if len(buffer) > 3 and buffer[-3] == '#':
  148. self._handle_command(buffer)
  149. buffer = ''
  150. def _handle_command(self, buffer): # type: (str) -> None
  151. command = buffer[1:-3] # ignore checksums
  152. # Acknowledge the command
  153. self.out_stream.write('+')
  154. self.out_stream.flush()
  155. self.logger.debug('Got command: %s', command)
  156. if command == '?':
  157. # report sigtrap as the stop reason; the exact reason doesn't matter for backtracing
  158. self._respond('T05')
  159. elif command.startswith('Hg') or command.startswith('Hc'):
  160. # Select thread command
  161. self._respond('OK')
  162. elif command == 'qfThreadInfo':
  163. # Get list of threads.
  164. # Only one thread for now, can be extended to show one thread for each core,
  165. # if we dump both cores (e.g. on an interrupt watchdog)
  166. self._respond('m1')
  167. elif command == 'qC':
  168. # That single thread is selected.
  169. self._respond('QC1')
  170. elif command == 'g':
  171. # Registers read
  172. self._respond_regs()
  173. elif command.startswith('m'):
  174. # Memory read
  175. addr, size = [int(v, 16) for v in command[1:].split(',')]
  176. self._respond_mem(addr, size)
  177. elif command.startswith('vKill') or command == 'k':
  178. # Quit
  179. self._respond('OK')
  180. raise SystemExit(0)
  181. else:
  182. # Empty response required for any unknown command
  183. self._respond('')
  184. def _respond(self, data): # type: (str) -> None
  185. # calculate checksum
  186. data_bytes = bytes(data.encode('ascii')) # bytes() for Py2 compatibility
  187. checksum = sum(data_bytes) & 0xff
  188. # format and write the response
  189. res = '${}#{:02x}'.format(data, checksum)
  190. self.logger.debug('Wrote: %s', res)
  191. self.out_stream.write(res)
  192. self.out_stream.flush()
  193. # get the result ('+' or '-')
  194. ret = self.in_stream.read(1)
  195. self.logger.debug('Response: %s', ret)
  196. if ret != '+':
  197. sys.stderr.write("GDB responded with '-' to {}".format(res))
  198. raise SystemExit(1)
  199. def _respond_regs(self): # type: () -> None
  200. response = ''
  201. for reg_name in self.reg_list:
  202. # register values are reported as hexadecimal strings
  203. # in target byte order (i.e. LSB first for RISC-V)
  204. reg_val = self.panic_info.regs.get(reg_name, 0)
  205. reg_bytes = struct.pack('<L', reg_val)
  206. response += binascii.hexlify(reg_bytes).decode('ascii')
  207. self._respond(response)
  208. def _respond_mem(self, start_addr, size): # type: (int, int) -> None
  209. stack_addr_min = self.panic_info.stack_base_addr
  210. stack_data = self.panic_info.stack_data
  211. stack_len = len(self.panic_info.stack_data)
  212. stack_addr_max = stack_addr_min + stack_len
  213. # For any memory address that is not on the stack, pretend the value is 0x00.
  214. # GDB should never ask us for program memory, it will be obtained from the ELF file.
  215. def in_stack(addr): # type: (int) -> typing.Any[bool]
  216. return stack_addr_min <= addr < stack_addr_max
  217. result = ''
  218. for addr in range(start_addr, start_addr + size):
  219. if not in_stack(addr):
  220. result += '00'
  221. else:
  222. result += '{:02x}'.format(stack_data[addr - stack_addr_min])
  223. self._respond(result)
  224. def main(): # type: () -> None
  225. parser = argparse.ArgumentParser()
  226. parser.add_argument('input_file', type=argparse.FileType('r'),
  227. help='File containing the panic handler output')
  228. parser.add_argument('--target', choices=GDB_REGS_INFO.keys(),
  229. help='Chip to use (determines the architecture)')
  230. parser.add_argument('--gdb-log', default=None,
  231. help='If specified, the file for logging GDB server debug information')
  232. args = parser.parse_args()
  233. panic_info = PANIC_OUTPUT_PARSERS[args.target](args.input_file.read())
  234. server = GdbServer(panic_info, target=args.target, log_file=args.gdb_log)
  235. try:
  236. server.run()
  237. except KeyboardInterrupt:
  238. sys.exit(0)
  239. if __name__ == '__main__':
  240. main()