2020-11-20 02:19:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding=utf-8
|
|
|
|
#
|
|
|
|
# A script which parses ESP-IDF panic handler output (registers & stack dump),
|
|
|
|
# and then acts as a GDB server over stdin/stdout, presenting the information
|
|
|
|
# from the panic handler to GDB.
|
|
|
|
# This allows for generating backtraces out of raw stack dumps on architectures
|
|
|
|
# where backtracing on the target side is not possible.
|
|
|
|
#
|
|
|
|
# Note that the "act as a GDB server" approach is somewhat a hack.
|
|
|
|
# A much nicer solution would have been to convert the panic handler output
|
|
|
|
# into a core file, and point GDB to the core file.
|
|
|
|
# However, RISC-V baremetal GDB currently lacks core dump support.
|
|
|
|
#
|
|
|
|
# The approach is inspired by Cesanta's ESP8266 GDB server:
|
|
|
|
# https://github.com/cesanta/mongoose-os/blob/27777c8977/platforms/esp8266/tools/serve_core.py
|
|
|
|
#
|
|
|
|
# Copyright 2020 Espressif Systems (Shanghai) Co. Ltd.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
2021-01-26 02:49:01 +00:00
|
|
|
import binascii
|
|
|
|
import logging
|
2020-11-20 02:19:04 +00:00
|
|
|
import struct
|
|
|
|
import sys
|
2021-01-26 02:49:01 +00:00
|
|
|
from builtins import bytes
|
2020-11-20 02:19:04 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
# Used for type annotations only. Silence linter warnings.
|
2021-01-26 02:49:01 +00:00
|
|
|
from pyparsing import (Combine, Group, Literal, OneOrMore, ParserElement, # noqa: F401 # pylint: disable=unused-import
|
|
|
|
ParseResults, Word, nums, srange)
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import typing # noqa: F401 # pylint: disable=unused-import
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# pyparsing helper
|
2021-01-26 02:49:01 +00:00
|
|
|
hexnumber = srange('[0-9a-f]')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
# List of registers to be passed to GDB, in the order GDB expects.
|
|
|
|
# The names should match those used in IDF panic handler.
|
|
|
|
# Registers not present in IDF panic handler output (like X0) will be assumed to be 0.
|
|
|
|
GDB_REGS_INFO_RISCV_ILP32 = [
|
2021-01-26 02:49:01 +00:00
|
|
|
'X0', 'RA', 'SP', 'GP',
|
|
|
|
'TP', 'T0', 'T1', 'T2',
|
|
|
|
'S0/FP', 'S1', 'A0', 'A1',
|
|
|
|
'A2', 'A3', 'A4', 'A5',
|
|
|
|
'A6', 'A7', 'S2', 'S3',
|
|
|
|
'S4', 'S5', 'S6', 'S7',
|
|
|
|
'S8', 'S9', 'S10', 'S11',
|
|
|
|
'T3', 'T4', 'T5', 'T6',
|
|
|
|
'MEPC'
|
2020-11-20 02:19:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
GDB_REGS_INFO = {
|
2021-01-26 02:49:01 +00:00
|
|
|
'esp32c3': GDB_REGS_INFO_RISCV_ILP32
|
2020-11-20 02:19:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
PanicInfo = namedtuple('PanicInfo', 'core_id regs stack_base_addr stack_data')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
|
2021-05-14 09:20:38 +00:00
|
|
|
def build_riscv_panic_output_parser(): # type: () -> typing.Any[typing.Type[ParserElement]]
|
2020-11-20 02:19:04 +00:00
|
|
|
"""Builds a parser for the panic handler output using pyparsing"""
|
|
|
|
|
|
|
|
# We don't match the first line, since "Guru Meditation" will not be printed in case of an abort:
|
|
|
|
# Guru Meditation Error: Core 0 panic'ed (Store access fault). Exception was unhandled.
|
|
|
|
|
|
|
|
# Core 0 register dump:
|
2021-01-26 02:49:01 +00:00
|
|
|
reg_dump_header = Group(Literal('Core') +
|
|
|
|
Word(nums)('core_id') +
|
|
|
|
Literal('register dump:'))('reg_dump_header')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
# MEPC : 0x4200232c RA : 0x42009694 SP : 0x3fc93a80 GP : 0x3fc8b320
|
2021-01-26 02:49:01 +00:00
|
|
|
reg_name = Word(srange('[A-Z_0-9/-]'))('name')
|
|
|
|
hexnumber_with_0x = Combine(Literal('0x') + Word(hexnumber))
|
|
|
|
reg_value = hexnumber_with_0x('value')
|
|
|
|
reg_dump_one_reg = Group(reg_name + Literal(':') + reg_value) # not named because there will be OneOrMore
|
|
|
|
reg_dump_all_regs = Group(OneOrMore(reg_dump_one_reg))('regs')
|
2020-11-20 02:19:04 +00:00
|
|
|
reg_dump = Group(reg_dump_header + reg_dump_all_regs) # not named because there will be OneOrMore
|
2021-01-26 02:49:01 +00:00
|
|
|
reg_dumps = Group(OneOrMore(reg_dump))('reg_dumps')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
# Stack memory:
|
|
|
|
# 3fc93a80: 0x00000030 0x00000021 0x3fc8aedc 0x4200232a 0xa5a5a5a5 0xa5a5a5a5 0x3fc8aedc 0x420099b0
|
2021-01-26 02:49:01 +00:00
|
|
|
stack_line = Group(Word(hexnumber)('base') + Literal(':') +
|
|
|
|
Group(OneOrMore(hexnumber_with_0x))('data'))
|
|
|
|
stack_dump = Group(Literal('Stack memory:') +
|
|
|
|
Group(OneOrMore(stack_line))('lines'))('stack_dump')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
# Parser for the complete panic output:
|
|
|
|
panic_output = reg_dumps + stack_dump
|
|
|
|
return panic_output
|
|
|
|
|
|
|
|
|
|
|
|
def get_stack_addr_and_data(res): # type: (ParseResults) -> typing.Tuple[int, bytes]
|
|
|
|
""" Extract base address and bytes from the parsed stack dump """
|
|
|
|
stack_base_addr = 0 # First reported address in the dump
|
|
|
|
base_addr = 0 # keeps track of the address for the given line of the dump
|
|
|
|
bytes_in_line = 0 # bytes of stack parsed on the previous line; used to validate the next base address
|
2021-01-24 12:15:49 +00:00
|
|
|
stack_data = bytes(b'') # accumulates all the dumped stack data
|
2020-11-20 02:19:04 +00:00
|
|
|
for line in res.stack_dump.lines:
|
|
|
|
# update and validate the base address
|
|
|
|
prev_base_addr = base_addr
|
|
|
|
base_addr = int(line.base, 16)
|
|
|
|
if stack_base_addr == 0:
|
|
|
|
stack_base_addr = base_addr
|
|
|
|
else:
|
|
|
|
assert base_addr == prev_base_addr + bytes_in_line
|
|
|
|
|
|
|
|
# convert little-endian hex words to byte representation
|
|
|
|
words = [int(w, 16) for w in line.data]
|
2021-01-24 12:15:49 +00:00
|
|
|
line_data = bytes(b''.join([struct.pack('<I', w) for w in words]))
|
2020-11-20 02:19:04 +00:00
|
|
|
bytes_in_line = len(line_data)
|
|
|
|
|
|
|
|
# accumulate in the whole stack data
|
|
|
|
stack_data += line_data
|
|
|
|
|
|
|
|
return stack_base_addr, stack_data
|
|
|
|
|
|
|
|
|
|
|
|
def parse_idf_riscv_panic_output(panic_text): # type: (str) -> PanicInfo
|
|
|
|
""" Decode panic handler output from a file """
|
|
|
|
panic_output = build_riscv_panic_output_parser()
|
|
|
|
results = panic_output.searchString(panic_text)
|
|
|
|
if len(results) != 1:
|
|
|
|
raise ValueError("Couldn't parse panic handler output")
|
|
|
|
res = results[0]
|
|
|
|
|
|
|
|
if len(res.reg_dumps) > 1:
|
2021-01-26 02:49:01 +00:00
|
|
|
raise NotImplementedError('Handling of multi-core register dumps not implemented')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
# Build a dict of register names/values
|
|
|
|
rd = res.reg_dumps[0]
|
|
|
|
core_id = int(rd.reg_dump_header.core_id)
|
|
|
|
regs = dict()
|
|
|
|
for reg in rd.regs:
|
|
|
|
reg_value = int(reg.value, 16)
|
|
|
|
regs[reg.name] = reg_value
|
|
|
|
|
|
|
|
stack_base_addr, stack_data = get_stack_addr_and_data(res)
|
|
|
|
|
|
|
|
return PanicInfo(core_id=core_id,
|
|
|
|
regs=regs,
|
|
|
|
stack_base_addr=stack_base_addr,
|
|
|
|
stack_data=stack_data)
|
|
|
|
|
|
|
|
|
|
|
|
PANIC_OUTPUT_PARSERS = {
|
2021-01-26 02:49:01 +00:00
|
|
|
'esp32c3': parse_idf_riscv_panic_output
|
2020-11-20 02:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GdbServer(object):
|
|
|
|
def __init__(self, panic_info, target, log_file=None): # type: (PanicInfo, str, str) -> None
|
|
|
|
self.panic_info = panic_info
|
|
|
|
self.in_stream = sys.stdin
|
|
|
|
self.out_stream = sys.stdout
|
|
|
|
self.reg_list = GDB_REGS_INFO[target]
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
self.logger = logging.getLogger('GdbServer')
|
2020-11-20 02:19:04 +00:00
|
|
|
if log_file:
|
2021-01-26 02:49:01 +00:00
|
|
|
handler = logging.FileHandler(log_file, 'w+')
|
2020-11-20 02:19:04 +00:00
|
|
|
self.logger.setLevel(logging.DEBUG)
|
2021-01-26 02:49:01 +00:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
2020-11-20 02:19:04 +00:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(handler)
|
|
|
|
|
|
|
|
def run(self): # type: () -> None
|
|
|
|
""" Process GDB commands from stdin until GDB tells us to quit """
|
2021-01-26 02:49:01 +00:00
|
|
|
buffer = ''
|
2020-11-20 02:19:04 +00:00
|
|
|
while True:
|
|
|
|
buffer += self.in_stream.read(1)
|
|
|
|
if len(buffer) > 3 and buffer[-3] == '#':
|
|
|
|
self._handle_command(buffer)
|
2021-01-26 02:49:01 +00:00
|
|
|
buffer = ''
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
def _handle_command(self, buffer): # type: (str) -> None
|
|
|
|
command = buffer[1:-3] # ignore checksums
|
|
|
|
# Acknowledge the command
|
2021-01-26 02:49:01 +00:00
|
|
|
self.out_stream.write('+')
|
2020-11-20 02:19:04 +00:00
|
|
|
self.out_stream.flush()
|
2021-01-26 02:49:01 +00:00
|
|
|
self.logger.debug('Got command: %s', command)
|
|
|
|
if command == '?':
|
2020-11-20 02:19:04 +00:00
|
|
|
# report sigtrap as the stop reason; the exact reason doesn't matter for backtracing
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('T05')
|
|
|
|
elif command.startswith('Hg') or command.startswith('Hc'):
|
2020-11-20 02:19:04 +00:00
|
|
|
# Select thread command
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('OK')
|
|
|
|
elif command == 'qfThreadInfo':
|
2020-11-20 02:19:04 +00:00
|
|
|
# Get list of threads.
|
|
|
|
# Only one thread for now, can be extended to show one thread for each core,
|
|
|
|
# if we dump both cores (e.g. on an interrupt watchdog)
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('m1')
|
|
|
|
elif command == 'qC':
|
2020-11-20 02:19:04 +00:00
|
|
|
# That single thread is selected.
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('QC1')
|
|
|
|
elif command == 'g':
|
2020-11-20 02:19:04 +00:00
|
|
|
# Registers read
|
|
|
|
self._respond_regs()
|
2021-01-26 02:49:01 +00:00
|
|
|
elif command.startswith('m'):
|
2020-11-20 02:19:04 +00:00
|
|
|
# Memory read
|
2021-01-26 02:49:01 +00:00
|
|
|
addr, size = [int(v, 16) for v in command[1:].split(',')]
|
2020-11-20 02:19:04 +00:00
|
|
|
self._respond_mem(addr, size)
|
2021-01-26 02:49:01 +00:00
|
|
|
elif command.startswith('vKill') or command == 'k':
|
2020-11-20 02:19:04 +00:00
|
|
|
# Quit
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('OK')
|
2020-11-20 02:19:04 +00:00
|
|
|
raise SystemExit(0)
|
|
|
|
else:
|
|
|
|
# Empty response required for any unknown command
|
2021-01-26 02:49:01 +00:00
|
|
|
self._respond('')
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
def _respond(self, data): # type: (str) -> None
|
|
|
|
# calculate checksum
|
2021-01-26 02:49:01 +00:00
|
|
|
data_bytes = bytes(data.encode('ascii')) # bytes() for Py2 compatibility
|
2020-11-20 02:19:04 +00:00
|
|
|
checksum = sum(data_bytes) & 0xff
|
|
|
|
# format and write the response
|
2021-01-26 02:49:01 +00:00
|
|
|
res = '${}#{:02x}'.format(data, checksum)
|
|
|
|
self.logger.debug('Wrote: %s', res)
|
2020-11-20 02:19:04 +00:00
|
|
|
self.out_stream.write(res)
|
|
|
|
self.out_stream.flush()
|
|
|
|
# get the result ('+' or '-')
|
|
|
|
ret = self.in_stream.read(1)
|
2021-01-26 02:49:01 +00:00
|
|
|
self.logger.debug('Response: %s', ret)
|
2020-11-20 02:19:04 +00:00
|
|
|
if ret != '+':
|
|
|
|
sys.stderr.write("GDB responded with '-' to {}".format(res))
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
def _respond_regs(self): # type: () -> None
|
2021-01-26 02:49:01 +00:00
|
|
|
response = ''
|
2020-11-20 02:19:04 +00:00
|
|
|
for reg_name in self.reg_list:
|
|
|
|
# register values are reported as hexadecimal strings
|
|
|
|
# in target byte order (i.e. LSB first for RISC-V)
|
|
|
|
reg_val = self.panic_info.regs.get(reg_name, 0)
|
2021-01-26 02:49:01 +00:00
|
|
|
reg_bytes = struct.pack('<L', reg_val)
|
|
|
|
response += binascii.hexlify(reg_bytes).decode('ascii')
|
2020-11-20 02:19:04 +00:00
|
|
|
self._respond(response)
|
|
|
|
|
|
|
|
def _respond_mem(self, start_addr, size): # type: (int, int) -> None
|
|
|
|
stack_addr_min = self.panic_info.stack_base_addr
|
|
|
|
stack_data = self.panic_info.stack_data
|
|
|
|
stack_len = len(self.panic_info.stack_data)
|
|
|
|
stack_addr_max = stack_addr_min + stack_len
|
|
|
|
|
|
|
|
# For any memory address that is not on the stack, pretend the value is 0x00.
|
|
|
|
# GDB should never ask us for program memory, it will be obtained from the ELF file.
|
2021-05-14 09:20:38 +00:00
|
|
|
def in_stack(addr): # type: (int) -> typing.Any[bool]
|
2020-11-20 02:19:04 +00:00
|
|
|
return stack_addr_min <= addr < stack_addr_max
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
result = ''
|
2020-11-20 02:19:04 +00:00
|
|
|
for addr in range(start_addr, start_addr + size):
|
|
|
|
if not in_stack(addr):
|
2021-01-26 02:49:01 +00:00
|
|
|
result += '00'
|
2020-11-20 02:19:04 +00:00
|
|
|
else:
|
2021-01-26 02:49:01 +00:00
|
|
|
result += '{:02x}'.format(stack_data[addr - stack_addr_min])
|
2020-11-20 02:19:04 +00:00
|
|
|
|
|
|
|
self._respond(result)
|
|
|
|
|
|
|
|
|
2021-05-14 09:20:38 +00:00
|
|
|
def main(): # type: () -> None
|
2020-11-20 02:19:04 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2021-01-26 02:49:01 +00:00
|
|
|
parser.add_argument('input_file', type=argparse.FileType('r'),
|
|
|
|
help='File containing the panic handler output')
|
|
|
|
parser.add_argument('--target', choices=GDB_REGS_INFO.keys(),
|
|
|
|
help='Chip to use (determines the architecture)')
|
|
|
|
parser.add_argument('--gdb-log', default=None,
|
|
|
|
help='If specified, the file for logging GDB server debug information')
|
2020-11-20 02:19:04 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
panic_info = PANIC_OUTPUT_PARSERS[args.target](args.input_file.read())
|
|
|
|
|
|
|
|
server = GdbServer(panic_info, target=args.target, log_file=args.gdb_log)
|
|
|
|
try:
|
|
|
|
server.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
if __name__ == '__main__':
|
2020-11-20 02:19:04 +00:00
|
|
|
main()
|