diff --git a/scripts/sof-target-install.sh b/scripts/sof-target-install.sh index 0ad63c1ff9a7..42e3d3add3fe 100755 --- a/scripts/sof-target-install.sh +++ b/scripts/sof-target-install.sh @@ -10,7 +10,6 @@ do scp tools/build_tools/topology/*.tplg root@${host}:/lib/firmware/intel/sof-tplg/ scp tools/build_tools/logger/sof-logger \ build_*_*/src/arch/xtensa/sof-*.ldc \ - tools/coredumper/* \ tools/build_tools/eqctl/sof-eqctl \ tools/kmod_scripts/* \ root@${host}:~/ diff --git a/tools/README.md b/tools/README.md index c74f411f8560..a13f51e03f68 100644 --- a/tools/README.md +++ b/tools/README.md @@ -107,33 +107,38 @@ set clock value to 19.9 (MHz). $ sof-logger -l ldc_file -i trace_dump -o out_file -c 19.9 -### sof-coredump-reader +### Core dumps -Tool for processing FW stack dumps. In verbose mode it prints the stack leading -to the core dump including DSP registers and function calls. -It outputs unwrapped gdb command function call addresses to human readable -function call format either to a file or stdout. +The firmware uses the Zephyr core dump support (`CONFIG_DEBUG_COREDUMP`), so +the Zephyr tools read its core dumps. See the Zephyr documentation in +`zephyr/doc/services/debugging/coredump.rst`. +On Intel ADSP platforms the dump goes to the telemetry slot of the debug memory +window. After a firmware panic, the Linux SOF driver prints the registers and +the stack, and `/sys/kernel/debug/sof/exception` returns the whole slot. The +dump does not record its length, and the Zephyr parser rejects the rest of the +slot, so cut the dump out with `sof-coredump-trim.py`, then serve it to gdb with +the Zephyr core dump gdb server: + +```bash +cat /sys/kernel/debug/sof/exception > capture.bin +sof/tools/coredumper/sof-coredump-trim.py capture.bin coredump.bin +zephyr/scripts/coredump/coredump_gdbserver.py build-/zephyr/zephyr.elf coredump.bin ``` -Usage sof-coredump-reader.py [-h] [-a ARCH] [-c] [-l COLUMNCOUNT] [-v] - (--stdout | -o OUTFILE) [--stdin | -i INFILE] - --h show this help message and exit --a ARCH determine architecture of dump file; valid archs are: LE64bit, LE32bit --c set output to be colourful --l COLUMNCOUNT set how many colums to group the output in --v increase output verbosity ---stdin input is from stdin --i INFILE path to sys dump bin ---stdout output is to stdout --o OUTFILE output is to FILE -``` -**sof-coredump-to-gdb.sh** shows example usage of sof-coredump-reader.py -We read from dump file into sof-coredump-reader.py, -then we pipe its output to xt-gdb, which operates on given elf-file. +`sof-coredump-trim.py` uses the Zephyr core dump parser. It finds the Zephyr +tree from `--zephyr-base`, `$ZEPHYR_BASE` or the west workspace. + +Then connect the gdb of the platform toolchain to it: + +```bash +-gdb build-/zephyr/zephyr.elf -ex "target remote :1234" -ex bt +``` - $ ./sof-coredump-to-gdb.sh sof-tgl dump_file +On QEMU (`qemu_xtensa`), build with `CONFIG_DEBUG_COREDUMP=y` and +`CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y`. The dump then goes to the console, and +`zephyr/scripts/coredump/coredump_serial_log_parser.py` turns the console log +into a dump file for the gdb server. ### tests diff --git a/tools/coredumper/sof-coredump-reader.py b/tools/coredumper/sof-coredump-reader.py deleted file mode 100755 index 9c1f5cceb762..000000000000 --- a/tools/coredumper/sof-coredump-reader.py +++ /dev/null @@ -1,765 +0,0 @@ -#!/usr/bin/env python3 - -# SPDX-License-Identifier: BSD-3-Clause -# -# Copyright (c) 2019, Intel Corporation. All rights reserved. -# -# Author: Michal Jerzy Wierzbicki -# Adrian Bonislawski - -# Tool for processing FW stack dumps. -# For more detailed useage, use --help option. - -from __future__ import print_function -import argparse -import struct -import os -import sys -import itertools -import re -import shutil -import time -from ctypes import LittleEndianStructure, BigEndianStructure, c_uint32, c_char -from collections import namedtuple -from operator import attrgetter -from functools import partial - -def stderr_print(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) - -def stdout_print(*args, **kwargs): - print(*args, file=sys.stdout, **kwargs) - -try: - from sty import fg, bg, ef, rs, Rule, Render - CAN_COLOUR=True -except: - CAN_COLOUR=False - -ArchDefinition = namedtuple('ArchDefinition', ['name', 'bitness', 'endianness']) -VALID_ARCHS = {} -[VALID_ARCHS.update({archdef.name : archdef}) - for archdef in [ArchDefinition(*tup) for tup in - [ - ( 'LE32bit', - 32, - LittleEndianStructure, ), - ( 'LE64bit', - 64, - LittleEndianStructure, ), -# ( 'BE32bit', #untested, treat as not implemented -# 32, -# BigEndianStructure, ), -# ( 'BE64bit', #untested, treat as not implemented -# 64, -# BigEndianStructure, ), - ]] -] - -# Exception causes: -# CODE: [Exception cause, excvaddr loaded] -EXCCAUSE_CODE = { - 0: ["IllegalInstructionCause: Illegal instruction", False], - 1: ["SyscallCause: SYSCALL instruction", True], - 2: ["InstructionFetchErrorCause: Processor internal physical address or data error during instruction fetch", True], - 3: ["LoadStoreErrorCause: Processor internal physical address or data error during load or store", True], - 4: ["Level1InterruptCause: Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register", False], - 5: ["AllocaCause: MOVSP instruction, if caller's registers are not in the register file", False], - 6: ["IntegerDivideByZeroCause: QUOS, QUOU, REMS, or REMU divisor operand is zero", False], - 8: ["PrivilegedCause: Attempt to execute a privileged operation when CRING ? 0", False], - 9: ["LoadStoreAlignmentCause: Load or store to an unaligned address", True], - 12: ["InstrPIFDataErrorCause: PIF data error during instruction fetch", True], - 13: ["LoadStorePIFDataErrorCause: ynchronous PIF data error during LoadStore access", True], - 14: ["InstrPIFAddrErrorCause: PIF address error during instruction fetch", True], - 15: ["LoadStorePIFAddrErrorCause: Synchronous PIF address error during LoadStore access", True], - 16: ["InstTLBMissCause: Error during Instruction TLB refill", True], - 17: ["InstTLBMultiHitCause: Multiple instruction TLB entries matched", True], - 18: ["InstFetchPrivilegeCause: An instruction fetch referenced a virtual address at a ring level less than CRING", True], - 20: ["InstFetchProhibitedCause: An instruction fetch referenced a page mapped with an attribute that does not permit instruction fetch", True], - 24: ["LoadStoreTLBMissCause: Error during TLB refill for a load or store", True], - 25: ["LoadStoreTLBMultiHitCause: Multiple TLB entries matched for a load or store", True], - 26: ["LoadStorePrivilegeCause: A load or store referenced a virtual address at a ring level less than CRING", True], - 28: ["LoadProhibitedCause: A load referenced a page mapped with an attribute that does not permit loads", True], - 29: ["StoreProhibitedCause: A store referenced a page mapped with an attribute that does not permit stores", True], - 32: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 33: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 34: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 35: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 36: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 37: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 38: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False], - 39: ["CoprocessornDisabled: Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39", False] -} - -def valid_archs_print(): - archs = ''.join("{0}, ".format(x) for x in VALID_ARCHS) - archs = archs[:len(archs)-2] - return "{0}.".format(archs) - -TERM_SIZE = shutil.get_terminal_size((120, 20)) -AR_WINDOW_WIDTH = 4 -IS_COLOUR=False - -class argparse_readable_file( argparse.Action): - def raise_error(self, filepath, reason): - raise argparse.ArgumentTypeError( - "is_readable_file:{0} {1}".format( - filepath, - reason - )) - def is_readable_file(self, filepath): - if not os.path.isfile(filepath): - self.raise_error(filepath, "is not a valid path") - if os.access(filepath, os.R_OK): - return True - else: - self.raise_error(filepath, "is not a readable file") - return False - def __call__(self, parser, namespace, values, option_string=None): - filepath = values[0] - if (self.is_readable_file(filepath)): - setattr(namespace, self.dest, filepath) - -class argparse_writeable_file(argparse.Action): - def raise_error(self, filepath, reason): - raise argparse.ArgumentTypeError( - "is_writeable_file:{0} {1}".format( - filepath, - reason - )) - def is_writeable_file(self, filepath): - absdir = os.path.abspath(os.path.dirname(filepath)) - if os.path.isdir(absdir) and not os.path.exists(filepath): - return True - else: - if not os.path.isfile(filepath): - self.raise_error(filepath, "is not a valid path") - if os.access(filepath, os.W_OK): - return True - else: - self.raise_error(filepath, "is not a writeable file") - return False - def __call__(self, parser, namespace, values, option_string=None): - filepath = values[0] - if (self.is_writeable_file(filepath)): - setattr(namespace, self.dest, filepath) - else: - self.raise_error( - filepath, - "failed to determine whether file is writeable" - ) - -class argparse_architecture( argparse.Action): - def raise_error(self, value, reason): - raise argparse.ArgumentTypeError( - "architecture: {0} {1}".format( - value, - reason - )) - def is_valid_architecture(self, value): - if value in VALID_ARCHS: - return True - return False - def __call__(self, parser, namespace, values, option_string=None): - value = values[0] - if (self.is_valid_architecture(value)): - setattr(namespace, self.dest, VALID_ARCHS[value]) - else: - self.raise_error( - value, - "is invalid architecture. Valid architectures are: {0}".format(valid_archs_print()) - ) - -def parse_params(): - parser = argparse.ArgumentParser( - description="Tool for processing FW stack dumps." - +" In verbose mode it prints DSP registers, and function call" - +" addresses in stack up to that which caused core dump." - +" It then prints either to file or to stdin all gdb" - +" commands unwrapping those function call addresses to" - +" function calls in human readable format." - ) - ArgTuple = namedtuple('ArgTuple', ['name', 'optionals', 'parent']) - ArgTuple.__new__.__defaults__ = ((), {}, parser) - - # below cannot be empty once declared - outputMethod = parser.add_mutually_exclusive_group(required=True) - inputMethod = parser.add_mutually_exclusive_group() - [parent.add_argument(*name, **optionals) - for name, optionals, parent in sorted([ArgTuple(*x) for x in - [ - ( ( '-a', '--arch' , ), { - 'type' : str, - 'help' :'determine architecture of dump file; valid archs are: {0}' - .format(valid_archs_print()), - 'action' : argparse_architecture, - 'nargs' : 1, - 'default': VALID_ARCHS['LE64bit'], - },), - ( ( '-v', '--verbose' , ), { - 'help' :'increase output verbosity', - 'action':'store_true', - },), - ( ( '--stdin' , ), { - 'help' :'input is from stdin', - 'action' : 'store_true', - }, - inputMethod), - ( ( '-o', '--outfile' , ), { - 'type' : str, - 'help' :'output is to FILE', - 'action' : argparse_writeable_file, - 'nargs' : 1, - }, - outputMethod), - ( ( '--stdout' , ), { - 'help' :'output is to stdout', - 'action' :'store_true', - }, - outputMethod), - ( ( '-c', '--colour', ), { - 'help' :'set output to be colourful!', - 'action':'store_true', - },), - ( ( '-l', '--columncount', ), { - 'type' : int, - 'help' :'set how many colums to group the output in, ignored without -v', - 'action':'store', - 'nargs' : 1, - },), - ( ( '-i', '--infile' , ), { - 'type' : str, - 'help' :'path to sys dump bin', - 'action' : argparse_readable_file, - 'nargs' : 1, - }, - inputMethod), - ]], - key=lambda argtup: (argtup.parent.__hash__(), argtup.name) - ) - ] - - parsed = parser.parse_args() - - if parsed.columncount and not parsed.verbose: - stderr_print("INFO: -l option will be ignored without -v") - - return parsed - -def chunks(l, n): - return [l[i:i + n] for i in range(0, len(l), n)] - -def flaten(l): - return [item for sublist in l for item in sublist] - -def raiseIfArchNotValid(arch): - if arch not in VALID_ARCHS.values(): - raise ValueError( - "CoreDumpFactory: {0} not in valid architectures: {1}" - .format(arch, valid_archs_print()) - ) - endiannesses = [arch.endianness for arch in VALID_ARCHS.values()] - if arch.endianness not in endiannesses: - raise ValueError( - "CoreDumpFactory: {0} not in valid endiannesses: {1}" - .format(endianness, endiannesses) - ) - -def FileInfoFactory(arch, filename_length): - raiseIfArchNotValid(arch) - class FileInfo(arch.endianness): - _fields_ = [ - ("hdr", c_uint32), - ("code", c_uint32), - ("filename", filename_length * c_char), - ("line_no", c_uint32) - ] - def __str__(self): - return "{}:{:d}".format(self.filename.decode(), self.line_no) - if FileInfo is None: - raise RuntimeError( - "FileInfoFactory: failed to produce FileInfo({0})" - .format(arch.name) - ) - return FileInfo - -class Colourer(): - #TODO: Add detection of 8bit/24bit terminal - # Add 8bit/24bit colours (with flag, maybe --colour=24bit) - # Use this below as fallback only - __print = partial(stdout_print) - if CAN_COLOUR == True: - __style = { - 'o' : fg.red, - 'O' : fg.yellow, - 'y' : fg.blue, - 'Y' : fg.cyan, - 'D' : fg.white + bg.red, - } - matchings = [] - matchingsInParenth = [] - matchingsInStderr = [] - def __init__(self): - if CAN_COLOUR == True: - self.matchings = [ - ( - lambda x: self.enstyleNumHex(x.group()), - re.compile(r'\b([A-Fa-f0-9]{8})\b') - ), - ( - lambda x: '0x' + self.enstyleNumHex(x.group(2)), - re.compile(r'(0x)([A-Fa-f0-9]{1,})') - ), - ( - lambda x: self.enstyleNumBin(x.group()), - re.compile(r'\b(b[01]+)\b') - ), - ( - r'\1' + - r'\2' + - self.enstyle( fg.green , r'\3') , - re.compile(r'(\#)(ar)([0-9]+)\b') - ), - ( - self.enstyle(bg.green , r'\1') + - rs.all + '\n', - re.compile(r'(\(xt-gdb\)\ *)') - ), - ( - r'\1' + - r'\2' + - self.enstyle( fg.green , r'\3') + - r':' + - self.enstyle( fg.magenta , r'\4') , - re.compile(r'(\bat\b\ *)(.+/)?(.*):([0-9]+)') - ), - ( - lambda x:\ - 'in '+ - self.enstyle(fg.green, x.group(2))+ - self.enstyleFuncParenth(x.group(3)), - re.compile(r'(\bin\b\ *)([^\ ]+)\ *(\(.*\))') - ), - ] - self.matchingsInParenth = [ - ( - self.enstyle( fg.yellow , r'\1') + - self.enstyle( fg.magenta , r'=' ) + - r'\2', - re.compile(r'([\-_a-zA-Z0-9]+)\ *=\ *([^,]+)') - ), - ( - self.enstyle( fg.magenta , r'\1') , - re.compile(r'(\ *[\(\)]\ *)') - ), - ( - self.enstyle( fg.magenta , r', ') , - re.compile(r'(\ *,\ *)') - ), - ] - self.matchingsInStderr = [ - ( - self.enstyle(bg.yellow + fg.black , r'\1') , - re.compile(r'([Ww]arning)') - ), - ( - self.enstyle(bg.red + fg.black , r'\1') , - re.compile(r'([Ee]rror)') - ), - ( - self.enstyle(bg.magenta + fg.black , r'\1') , - re.compile(r'([Ff]atal)') - ), - ] - - def toGroup(self, txt): - return [ (label, sum(1 for _ in group)) - for label, group in itertools.groupby(txt) ] - - def leadingZero(self, txt, char): - result = "" - groups = self.toGroup(txt) - lead = 0 - if groups[0][0] == '0': - lead = min(4, groups[0][1]) - result += char.lower() * lead - result += char.upper() * (4-lead) - return result - - def findSub(self, txt, mask, sub, char): - pos = txt.find(sub) - if pos >= 0: - return mask[:pos] + char * len(sub) + mask[(len(sub)+pos):] - else: - return mask - - def enstyleFuncParenth(self, txt): - result = txt - for repl, regex in self.matchingsInParenth: - result = re.sub(regex, repl, result) - return result - - def enstyleNumBin(self, txt): - result = rs.all + bg.magenta + "b" - prev = "" - for c in txt[1:]: - if prev != c: - prev = c - result += rs.all - if c == "0": - result += fg.red - result += c - result += rs.all - return result - - def enstyleNumHex(self, txt): - if len(txt) < 8: - txt = (8-len(txt))*'0' + txt - p1 = 'o' - p2 = 'y' - if txt == "00000000": - styleMask = p1 * 8 - elif txt.lower() == "deadbeef": - styleMask = "DDDDDDDD" - else: - styleMask = "".join( - [self.leadingZero(string, style) - for string, style in [ - (txt[:4], p1), - (txt[4:], p2), - ]]) - styleMask = "".join( - [self.findSub(txt, styleMask, string, style) - for string, style in [ - ('dead', 'D'), - ]]) - - result = "" - thisstyle = '' - for iter, style in enumerate(styleMask): - if thisstyle != style: - thisstyle = style - result += rs.all + self.__style[thisstyle] - result += txt[iter] - result += rs.all - return result - - def enstyleStderr(self, txt): - if txt is None: - return '' - result = txt - for repl, regex in self.matchingsInStderr: - result = re.sub(regex, repl, result) - for repl, regex in self.matchings: - result = re.sub(regex, repl, result) - return fg.red + result + rs.all - - def enstyle(self, style, txt): - return style + txt + rs.all - - def produce_string(self, txt): - result = txt - for repl, regex in self.matchings: - result = re.sub(regex, repl, result) - return result - - def print(self, txt): - self.__print(self.produce_string(txt)) - -def CoreDumpFactory(dsp_arch): - raiseIfArchNotValid(dsp_arch) - class CoreDump(dsp_arch.endianness): - _fields_ = [(x, c_uint32) for x in - [ - # struct sof_ipc_dsp_oops_arch_hdr { - "arch", - "totalsize", - # } - # struct sof_ipc_dsp_oops_plat_hdr { - "configidhi", - "configidlo", - "numaregs", - "stackoffset", - "stackptr", - # } - "exccause", - "excvaddr", - "ps" - ] - + ["epc" + str(x) for x in range(1,7+1)] - + ["eps" + str(x) for x in range(2,7+1)] - + [ - "depc", - "intenable", - "interrupt", - "sar", - "debugcause", - "windowbase", - "windowstart", - "excsave1" # to - ] - ] + [ - ("a", dsp_arch.bitness * c_uint32) - ] - - def __init__(self, columncount): - self.dsp_arch = dsp_arch - self._fields_ - self.ar_regex = re.compile(r'ar[0-9]+') - # below: smart column count - self._longest_field = len(max([x[0] for x in self._fields_], key=len)) - if columncount is not None: - self.columncount = max (1, int(columncount[0])) - else: - self.columncount = max(1, - int(TERM_SIZE[0]/(self._longest_field + 2 + 2 * AR_WINDOW_WIDTH + 2)) - ) - self.columncount_ar = ( - self.columncount - if self.columncount <= AR_WINDOW_WIDTH else - AR_WINDOW_WIDTH * int(self.columncount/AR_WINDOW_WIDTH) - ) - - def __windowbase_shift(self, iter, direction): - return (iter + self.windowbase * AR_WINDOW_WIDTH * direction)\ - % self.dsp_arch.bitness - def windowbase_shift_left(self, iter): - return self.__windowbase_shift(iter, -1) - def windowbase_shift_right(self, iter): - return self.__windowbase_shift(iter, 1) - - def reg_from_string(self, string): - if self.ar_regex.fullmatch(string): - return self.a[self.windowbase_shift_left(int(string[2:]))] - else: - return self.__getattribute__(string) - - def __str__(self): - string = "" - string += "exccause" - return string - - def to_string(self, is_gdb): - # in case windowbase in dump has invalid value - windowbase_shift = min( - self.windowbase * AR_WINDOW_WIDTH, - self.dsp_arch.bitness - ) - # flatten + chunk enable to smartly print in N columns - string = ''.join([self.fmt(is_gdb, x) - for x in flaten( - [chunks(word, self.columncount) for word in [ - ["arch", "totalsize", "stackptr", "stackoffset"], - ["configidhi", "configidlo", "numaregs"], - ]] - ) - ]) - - string += "\n# CPU registers:\n\n" - - string += ''.join([self.fmt(is_gdb, x) - for x in flaten( - [chunks(word, self.columncount) for word in [ - ["exccause", "excvaddr", "ps"], - ["epc" + str(x) for x in range(1,7+1)], - ["eps" + str(x) for x in range(2,7+1)], - ["depc", "intenable", "interrupt", "sar", "debugcause"], - ["windowbase", "windowstart"], - ["excsave1"], - ]] + - [chunks(word, self.columncount_ar) for word in [ - ["ar" + str(x) for x in itertools.chain( - range( windowbase_shift, self.dsp_arch.bitness), - range(0, windowbase_shift), - )] - ]] - ) - ]) - - if not is_gdb: - string += "\n" - return string - - def fmt_gdb_command(self): - return "set ${}=0x{:08x}\n" - - def fmt_pretty_form(self, separator = "|"): - return separator + "{:" + str(self._longest_field) + "} {:08x} " - - def fmt_separator(self, name): - separator = "# " - return separator - - def fmt_pretty_auto(self, name): - return self.fmt_pretty_form(self.fmt_separator(name)) - - def fmt(self, is_gdb, names): - if is_gdb: - fmtr = lambda name: self.fmt_gdb_command() - else: - fmtr = lambda name: self.fmt_pretty_auto(name) - - string = "" - for name in names: - string += fmtr(name).format( - name, self.reg_from_string(name) - ) - if not is_gdb: - string += "\n" - return string - - def windowstart_process(self): - string = "" - binary = "{0:b}".format(self.windowstart) - bit_start = len(binary)-1-self.windowbase - fnc_num = 0 - header = "" - - for it, c in enumerate(binary[bit_start:]): - if c != "0": - header += str(fnc_num) - fnc_num += 1 - else: - header += " " - for it, c in enumerate(binary[:bit_start]): - if c != "0": - header += str(fnc_num) - fnc_num += 1 - else: - header += " " - header = header[self.windowbase+1:]+ header[:self.windowbase+1] - - string += "# windowbase: {:0X}\n".format(self.windowbase) - string += "# {0}\n".format(header) - string += "# windowstart: b{0}\n\n".format(binary) - string += "# reg a0 a1\n" - string += "# (return) (sptr)\n" - string += "# --- -------- -------\n" - fnc_num = 0 - for iter, digit in enumerate(binary[bit_start:]): - if (digit == '1'): - reg = "ar{0}".format(AR_WINDOW_WIDTH * (self.windowbase - iter)) - reg1 = "ar{0}".format(AR_WINDOW_WIDTH * (self.windowbase - iter) + 1) - string += "# {0:2d} ".format(++fnc_num) - string += self.fmt_pretty_auto(reg).format( - reg, self.reg_from_string(reg) - ) + " {:0x} ".format(self.reg_from_string(reg1)) + "\n" - fnc_num += 1 - for iter, digit in enumerate(binary[:bit_start]): - if (digit == '1'): - reg = "ar{0}".format(AR_WINDOW_WIDTH * (len(binary) - 1 - iter)) - reg1 = "ar{0}".format(AR_WINDOW_WIDTH * (len(binary) - 1 - iter) + 1) - string += "# {0:2d} ".format(++fnc_num) - string += self.fmt_pretty_auto(reg).format( - reg, self.reg_from_string(reg) - ) + " {:0x} ".format(self.reg_from_string(reg1)) + "\n" - fnc_num += 1 - return string - - if CoreDump is None: - raise RuntimeError( - "CoreDumpFactory: failed to produce CoreDump({0})" - .format(dsp_arch.name) - ) - return CoreDump - -class CoreDumpReader(object): - def __init__(self, args): - self.core_dump = CoreDumpFactory(args.arch)( - args.columncount - ) - self.file_info = FileInfoFactory(args.arch, 32)() - - if IS_COLOUR: - colourer = Colourer() - if args.verbose: - verbosePrint =\ - colourer.print\ - if IS_COLOUR else\ - stdout_print - else: - verbosePrint = lambda *discard_this: None - - if args.stdout: - stdoutOpen = lambda: None - stdoutPrint = print - stdoutClose = lambda: None - elif args.outfile: - #TODO: open file in stdOutOpen - stdoutDest = open(args.outfile, "w") - stdoutOpen = lambda: None - stdoutPrint = stdoutDest.write - stdoutClose = stdoutDest.close - else: - raise RuntimeError("CoreDumpReader: No output method.") - - if args.stdin: - inStream = lambda: sys.stdin.buffer - elif args.infile: - inStream = lambda: open(args.infile, "rb") - else: - raise RuntimeError("CoreDumpReader: No input method.") - - with inStream() as cd_file: - [cd_file.readinto(x) for x in [ - self.core_dump, - self.file_info - ]] - self.stack = cd_file.read() - - verbosePrint("# Core header:\n") - verbosePrint(self.core_dump.to_string(0)) - - verbosePrint(self.core_dump.windowstart_process()) - - stack_base = self.core_dump.stackptr - stack_dw_num = int(len(self.stack)/AR_WINDOW_WIDTH) - verbosePrint("# Stack dumped from {:08x} dwords num {:d}" - .format(stack_base, stack_dw_num)) - - stdoutOpen() - - # disable confirmation request for undefined breakpoint - stdoutPrint("set confirm off\n") - - # for XTOS SOF build - stdoutPrint("break _MemErrorVector\n") - # for Zephyr SOF build - stdoutPrint("break _MemoryExceptionVector_text_start\n") - - stdoutPrint("run\n") - stdoutPrint(self.core_dump.to_string(1)) - - #TODO: make this elegant - for dw, addr in [( - struct.unpack("I", self.stack[i*AR_WINDOW_WIDTH : (i+1)*AR_WINDOW_WIDTH])[0], - stack_base + i*AR_WINDOW_WIDTH - ) for i in range(0, stack_dw_num)]: - stdoutPrint("set *0x{:08x}=0x{:08x}\n" - .format(addr, dw)) - - # Exccause 63 is reserved for panics; the other causes come - # from actual exceptions - if self.core_dump.exccause != 63: - verbosePrint("\n# *EXCEPTION*\n") - verbosePrint("# exccause: " + EXCCAUSE_CODE[self.core_dump.exccause][0]) - if EXCCAUSE_CODE[self.core_dump.exccause][1]: - verbosePrint("# excvaddr: " + str(self.core_dump.excvaddr)) - stdoutPrint('p "Exception location:"\nlist *$epc1\n'); - else: - verbosePrint("# Location: " + str(self.file_info)); - - # TODO: if excsave1 is not empty, pc should be set to that value - # (exception mode, not forced panic mode) - stdoutPrint('set $pc=&arch_dump_regs_a\np "backtrace"\nbacktrace\n') - stdoutClose() - -if __name__ == "__main__": - args = parse_params() - if args.colour: - if CAN_COLOUR: - IS_COLOUR=True - else: - stderr_print("INFO: Cannot color the output: module 'sty' not found") - CoreDumpReader(args) - diff --git a/tools/coredumper/sof-coredump-to-gdb.sh b/tools/coredumper/sof-coredump-to-gdb.sh deleted file mode 100755 index d366b447181c..000000000000 --- a/tools/coredumper/sof-coredump-to-gdb.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -# SPDX-License-Identifier: BSD-3-Clause -# Copyright(c) 2018 Intel Corporation. All rights reserved. - -# Exemplatory usage of sof-coredump-reader.py ($reader_name) -# We read from dump file into sof-coredump-reader.py, then we pipe its output -# to xt-gdb, which operates on given elf-file. - -THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" -elf="${1}" -dump="${2}" - -reader_name="sof-coredump-reader.py" # in case it is changed -reader_output="$(${THIS_SCRIPT_DIR}/${reader_name} -vc -i ${dump} -l 4 \ - -o reader-output.txt)" -reader_result="$?" # if $reader_name script fails, running xt-gdb is pointless -if [[ ${reader_result} -ne 0 ]] ; then - echo "${reader_name} failed!" - exit ${reader_result} -else - (echo "${reader_output}") - echo "quit" >> reader-output.txt - xt-gdb -q "${elf}" --command=reader-output.txt |& tee gdb_log.txt \ - | grep -E '^[0-9]|#|\$' - rm -rf reader-output.txt -fi diff --git a/tools/coredumper/sof-coredump-trim.py b/tools/coredumper/sof-coredump-trim.py new file mode 100755 index 000000000000..7b71615c2538 --- /dev/null +++ b/tools/coredumper/sof-coredump-trim.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +"""Cut an Intel ADSP core dump capture down to the Zephyr core dump it holds. + +On Intel ADSP the Zephyr core dump goes to the telemetry slot of the debug +memory window, and /sys/kernel/debug/sof/exception returns the whole slot. The +dump does not record its length, so the capture ends with the rest of the slot, +which the Zephyr core dump parser rejects. This tool parses the capture with the +Zephyr parser, stops at the first byte that does not start a known section and +writes the dump up to that point, ready for coredump_gdbserver.py. + +Zephyr writes the sections of a dump in a fixed order, so a section out of that +order also ends the dump. Data after the dump that happens to form a valid +memory region is still kept, as gdb would show it as dumped memory. +""" + +import argparse +import io +import logging +import os +import sys + + +def zephyr_base(arg): + """Find the Zephyr tree: the argument, $ZEPHYR_BASE or the west workspace.""" + candidates = [arg, os.environ.get("ZEPHYR_BASE"), + os.path.join(os.path.dirname(os.path.abspath(__file__)), + "..", "..", "..", "zephyr")] + for path in candidates: + if path and os.path.isfile(os.path.join(path, "scripts", "coredump", + "coredump_parser", "log_parser.py")): + return path + sys.exit("Zephyr tree not found: use --zephyr-base or set ZEPHYR_BASE") + + +class SectionTracker(io.BytesIO): + """Remember where the parser read the last section ID. + + The parser reads each section ID as a single byte before the section + itself, so a one-byte read marks the start of a section. + """ + + section_start = 0 + + def read(self, size=-1): + if size == 1: + self.section_start = self.tell() + return super().read(size) + + +def dump_parser(log_parser): + """The Zephyr parser, stopping at a section that is out of order. + + Zephyr writes the sections of a dump in a fixed order: the architecture + block, the threads metadata, the CPU snapshots, then the memory regions, + with one architecture block and at most one threads metadata block. A + section out of that order is data after the dump that happens to look like + a section. The Zephyr parser would accept it, and a second architecture + block would replace the registers of the dump. + """ + + class InOrder(log_parser.CoredumpLogFile): + def __init__(self): + super().__init__(None) + self.last = None + + def in_order(self, rank, repeats, parse): + if self.last is not None and (rank < self.last or + (rank == self.last and not repeats)): + return False + self.last = rank + return parse() + + def parse_arch_section(self): + return self.in_order(0, False, super().parse_arch_section) + + def parse_threads_metadata_section(self): + return self.in_order(1, False, super().parse_threads_metadata_section) + + def parse_cpu_snapshot_section(self): + return self.in_order(2, True, super().parse_cpu_snapshot_section) + + def parse_memory_section(self): + return self.in_order(3, True, super().parse_memory_section) + + return InOrder() + + +def dump_length(log_parser, data): + """Length of the core dump at the start of data, or 0 if there is none.""" + logf = dump_parser(log_parser) + logf.fd = SectionTracker(data) + if logf.parse(): + end = len(data) + else: + # the dump ends where the section that failed to parse starts + end = logf.fd.section_start + if not logf.get_arch_data(): + return 0 + return end + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("capture", help="file read from /sys/kernel/debug/sof/exception") + parser.add_argument("dump", help="output core dump for coredump_gdbserver.py") + parser.add_argument("--zephyr-base", help="Zephyr tree (default: $ZEPHYR_BASE)") + args = parser.parse_args() + + sys.path.insert(0, os.path.join(zephyr_base(args.zephyr_base), "scripts", "coredump")) + from coredump_parser import log_parser # pylint: disable=import-outside-toplevel + + with open(args.capture, "rb") as f: + data = f.read() + + # the parser logs the section it cannot handle as an error, which is + # expected here: that is where the dump ends + logging.disable(logging.CRITICAL) + length = dump_length(log_parser, data) + if length: + # check that the cut dump parses on its own + logf = dump_parser(log_parser) + logf.fd = io.BytesIO(data[:length]) + if not logf.parse(): + length = 0 + logging.disable(logging.NOTSET) + + if not length: + sys.exit(f"{args.capture}: no Zephyr core dump found") + + with open(args.dump, "wb") as f: + f.write(data[:length]) + print(f"{args.dump}: {length} of {len(data)} bytes") + + +if __name__ == "__main__": + main()