2021-01-19 00:39:04 -05:00
|
|
|
#
|
2021-04-08 23:39:37 -04:00
|
|
|
# Copyright 2021 Espressif Systems (Shanghai) CO., LTD
|
2021-01-19 00:39:04 -05:00
|
|
|
#
|
|
|
|
# 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 base64
|
|
|
|
import binascii
|
|
|
|
import hashlib
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from construct import AlignedStruct, Bytes, GreedyRange, Int32ul, Padding, Struct, abs_, this
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
from . import ESPCoreDumpLoaderError
|
2021-01-19 00:39:04 -05:00
|
|
|
from .elf import (TASK_STATUS_CORRECT, TASK_STATUS_TCB_CORRUPTED, ElfFile, ElfSegment, ESPCoreDumpElfFile,
|
|
|
|
EspTaskStatus, NoteSection)
|
2021-04-08 23:39:37 -04:00
|
|
|
from .riscv import Esp32c3Methods
|
2021-09-06 14:31:37 -04:00
|
|
|
from .xtensa import Esp32Methods, Esp32S2Methods, Esp32S3Methods
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
try:
|
|
|
|
from typing import Optional, Tuple
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
IDF_PATH = os.getenv('IDF_PATH', '')
|
2021-01-19 00:39:04 -05:00
|
|
|
PARTTOOL_PY = os.path.join(IDF_PATH, 'components', 'partition_table', 'parttool.py')
|
|
|
|
ESPTOOL_PY = os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool', 'esptool.py')
|
|
|
|
|
|
|
|
# Following structs are based on source code
|
|
|
|
# components/espcoredump/include_core_dump/esp_core_dump_priv.h
|
|
|
|
|
|
|
|
EspCoreDumpV1Header = Struct(
|
|
|
|
'tot_len' / Int32ul,
|
|
|
|
'ver' / Int32ul,
|
|
|
|
'task_num' / Int32ul,
|
|
|
|
'tcbsz' / Int32ul,
|
|
|
|
)
|
|
|
|
|
|
|
|
EspCoreDumpV2Header = Struct(
|
|
|
|
'tot_len' / Int32ul,
|
|
|
|
'ver' / Int32ul,
|
|
|
|
'task_num' / Int32ul,
|
|
|
|
'tcbsz' / Int32ul,
|
|
|
|
'segs_num' / Int32ul,
|
|
|
|
)
|
|
|
|
|
|
|
|
CRC = Int32ul
|
|
|
|
SHA256 = Bytes(32)
|
|
|
|
|
|
|
|
TaskHeader = Struct(
|
|
|
|
'tcb_addr' / Int32ul,
|
|
|
|
'stack_top' / Int32ul,
|
|
|
|
'stack_end' / Int32ul,
|
|
|
|
)
|
|
|
|
|
|
|
|
MemSegmentHeader = Struct(
|
|
|
|
'mem_start' / Int32ul,
|
|
|
|
'mem_sz' / Int32ul,
|
|
|
|
'data' / Bytes(this.mem_sz),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EspCoreDumpVersion(object):
|
2021-09-13 09:16:45 -04:00
|
|
|
"""Core dump version class, it contains all version-dependent params
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
2021-09-13 09:16:45 -04:00
|
|
|
# Chip IDs should be in sync with components/esp_hw_support/include/esp_chip_info.h
|
2021-01-19 00:39:04 -05:00
|
|
|
ESP32 = 0
|
|
|
|
ESP32S2 = 2
|
2021-09-06 14:31:37 -04:00
|
|
|
ESP32S3 = 9
|
|
|
|
XTENSA_CHIPS = [ESP32, ESP32S2, ESP32S3]
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
ESP32C3 = 5
|
|
|
|
RISCV_CHIPS = [ESP32C3]
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
COREDUMP_SUPPORTED_TARGETS = XTENSA_CHIPS + RISCV_CHIPS
|
|
|
|
|
2023-04-14 07:09:57 -04:00
|
|
|
def __init__(self, version=None): # type: (Optional[int]) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""Constructor for core dump version
|
|
|
|
"""
|
|
|
|
super(EspCoreDumpVersion, self).__init__()
|
|
|
|
if version is None:
|
|
|
|
self.version = 0
|
|
|
|
else:
|
|
|
|
self.set_version(version)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-08 23:39:37 -04:00
|
|
|
def make_dump_ver(major, minor): # type: (int, int) -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
return ((major & 0xFF) << 8) | ((minor & 0xFF) << 0)
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def set_version(self, version): # type: (int) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
self.version = version
|
|
|
|
|
|
|
|
@property
|
2021-04-08 23:39:37 -04:00
|
|
|
def chip_ver(self): # type: () -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
return (self.version & 0xFFFF0000) >> 16
|
|
|
|
|
|
|
|
@property
|
2021-04-08 23:39:37 -04:00
|
|
|
def dump_ver(self): # type: () -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
return self.version & 0x0000FFFF
|
|
|
|
|
|
|
|
@property
|
2021-04-08 23:39:37 -04:00
|
|
|
def major(self): # type: () -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
return (self.version & 0x0000FF00) >> 8
|
|
|
|
|
|
|
|
@property
|
2021-04-08 23:39:37 -04:00
|
|
|
def minor(self): # type: () -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
return self.version & 0x000000FF
|
|
|
|
|
|
|
|
|
|
|
|
class EspCoreDumpLoader(EspCoreDumpVersion):
|
|
|
|
# "legacy" stands for core dumps v0.1 (before IDF v4.1)
|
|
|
|
BIN_V1 = EspCoreDumpVersion.make_dump_ver(0, 1)
|
|
|
|
BIN_V2 = EspCoreDumpVersion.make_dump_ver(0, 2)
|
|
|
|
ELF_CRC32 = EspCoreDumpVersion.make_dump_ver(1, 0)
|
|
|
|
ELF_SHA256 = EspCoreDumpVersion.make_dump_ver(1, 1)
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def __init__(self): # type: () -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
super(EspCoreDumpLoader, self).__init__()
|
2021-04-08 23:39:37 -04:00
|
|
|
self.core_src_file = None # type: Optional[str]
|
2021-01-19 00:39:04 -05:00
|
|
|
self.core_src_struct = None
|
|
|
|
self.core_src = None
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
self.core_elf_file = None # type: Optional[str]
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
self.header = None
|
|
|
|
self.header_struct = EspCoreDumpV1Header
|
|
|
|
self.checksum_struct = CRC
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
# target classes will be assigned in ``_reload_coredump``
|
|
|
|
self.target_methods = Esp32Methods()
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
self.temp_files = [] # type: list[str]
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _create_temp_file(self): # type: () -> str
|
2021-01-19 00:39:04 -05:00
|
|
|
t = tempfile.NamedTemporaryFile('wb', delete=False)
|
2021-04-08 23:39:37 -04:00
|
|
|
# Here we close this at first to make sure the read/write is wrapped in context manager
|
|
|
|
# Otherwise the result will be wrong if you read while open in another session
|
|
|
|
t.close()
|
|
|
|
self.temp_files.append(t.name)
|
|
|
|
return t.name
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _load_core_src(self): # type: () -> str
|
|
|
|
"""
|
|
|
|
Write core elf into ``self.core_src``,
|
|
|
|
Return the target str by reading core elf
|
|
|
|
"""
|
|
|
|
with open(self.core_src_file, 'rb') as fr: # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
coredump_bytes = fr.read()
|
|
|
|
|
|
|
|
_header = EspCoreDumpV1Header.parse(coredump_bytes) # first we use V1 format to get version
|
|
|
|
self.set_version(_header.ver)
|
|
|
|
if self.dump_ver == self.ELF_CRC32:
|
|
|
|
self.checksum_struct = CRC
|
|
|
|
self.header_struct = EspCoreDumpV2Header
|
|
|
|
elif self.dump_ver == self.ELF_SHA256:
|
|
|
|
self.checksum_struct = SHA256
|
|
|
|
self.header_struct = EspCoreDumpV2Header
|
|
|
|
elif self.dump_ver == self.BIN_V1:
|
|
|
|
self.checksum_struct = CRC
|
|
|
|
self.header_struct = EspCoreDumpV1Header
|
|
|
|
elif self.dump_ver == self.BIN_V2:
|
|
|
|
self.checksum_struct = CRC
|
|
|
|
self.header_struct = EspCoreDumpV2Header
|
|
|
|
else:
|
|
|
|
raise ESPCoreDumpLoaderError('Core dump version "0x%x" is not supported!' % self.dump_ver)
|
|
|
|
|
|
|
|
self.core_src_struct = Struct(
|
|
|
|
'header' / self.header_struct,
|
|
|
|
'data' / Bytes(this.header.tot_len - self.header_struct.sizeof() - self.checksum_struct.sizeof()),
|
|
|
|
'checksum' / self.checksum_struct,
|
|
|
|
)
|
2021-04-08 23:39:37 -04:00
|
|
|
self.core_src = self.core_src_struct.parse(coredump_bytes) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
# Reload header if header struct changes after parsing
|
|
|
|
if self.header_struct != EspCoreDumpV1Header:
|
|
|
|
self.header = EspCoreDumpV2Header.parse(coredump_bytes)
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
if self.chip_ver in self.COREDUMP_SUPPORTED_TARGETS:
|
2021-01-19 00:39:04 -05:00
|
|
|
if self.chip_ver == self.ESP32:
|
2021-04-08 23:39:37 -04:00
|
|
|
self.target_methods = Esp32Methods() # type: ignore
|
|
|
|
elif self.chip_ver == self.ESP32S2:
|
|
|
|
self.target_methods = Esp32S2Methods() # type: ignore
|
|
|
|
elif self.chip_ver == self.ESP32C3:
|
|
|
|
self.target_methods = Esp32c3Methods() # type: ignore
|
2021-09-06 14:31:37 -04:00
|
|
|
elif self.chip_ver == self.ESP32S3:
|
|
|
|
self.target_methods = Esp32S3Methods() # type: ignore
|
2021-04-08 23:39:37 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2021-01-19 00:39:04 -05:00
|
|
|
else:
|
|
|
|
raise ESPCoreDumpLoaderError('Core dump chip "0x%x" is not supported!' % self.chip_ver)
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
return self.target_methods.TARGET # type: ignore
|
|
|
|
|
|
|
|
def _validate_dump_file(self): # type: () -> None
|
|
|
|
if self.chip_ver not in self.COREDUMP_SUPPORTED_TARGETS:
|
2021-01-19 00:39:04 -05:00
|
|
|
raise ESPCoreDumpLoaderError('Invalid core dump chip version: "{}", should be <= "0x{:X}"'
|
|
|
|
.format(self.chip_ver, self.ESP32S2))
|
|
|
|
|
|
|
|
if self.checksum_struct == CRC:
|
|
|
|
self._crc_validate()
|
|
|
|
elif self.checksum_struct == SHA256:
|
|
|
|
self._sha256_validate()
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _crc_validate(self): # type: () -> None
|
|
|
|
data_crc = binascii.crc32(
|
|
|
|
EspCoreDumpV2Header.build(self.core_src.header) + self.core_src.data) & 0xffffffff # type: ignore
|
|
|
|
if data_crc != self.core_src.checksum: # type: ignore
|
|
|
|
raise ESPCoreDumpLoaderError(
|
|
|
|
'Invalid core dump CRC %x, should be %x' % (data_crc, self.core_src.crc)) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _sha256_validate(self): # type: () -> None
|
|
|
|
data_sha256 = hashlib.sha256(
|
|
|
|
EspCoreDumpV2Header.build(self.core_src.header) + self.core_src.data) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
data_sha256_str = data_sha256.hexdigest()
|
2021-04-08 23:39:37 -04:00
|
|
|
sha256_str = binascii.hexlify(self.core_src.checksum).decode('ascii') # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
if data_sha256_str != sha256_str:
|
|
|
|
raise ESPCoreDumpLoaderError('Invalid core dump SHA256 "{}", should be "{}"'
|
|
|
|
.format(data_sha256_str, sha256_str))
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def create_corefile(self, exe_name=None, e_machine=ESPCoreDumpElfFile.EM_XTENSA):
|
|
|
|
# type: (Optional[str], int) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Creates core dump ELF file
|
|
|
|
"""
|
|
|
|
self._validate_dump_file()
|
|
|
|
self.core_elf_file = self._create_temp_file()
|
|
|
|
|
|
|
|
if self.dump_ver in [self.ELF_CRC32,
|
|
|
|
self.ELF_SHA256]:
|
2021-04-08 23:39:37 -04:00
|
|
|
self._extract_elf_corefile(exe_name, e_machine)
|
2021-01-19 00:39:04 -05:00
|
|
|
elif self.dump_ver in [self.BIN_V1,
|
|
|
|
self.BIN_V2]:
|
2021-04-08 23:39:37 -04:00
|
|
|
self._extract_bin_corefile(e_machine)
|
2021-01-19 00:39:04 -05:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-04-14 07:09:57 -04:00
|
|
|
def _extract_elf_corefile(self, exe_name=None, e_machine=ESPCoreDumpElfFile.EM_XTENSA): # type: (Optional[str], int) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Reads the ELF formatted core dump image and parse it
|
|
|
|
"""
|
2021-04-08 23:39:37 -04:00
|
|
|
with open(self.core_elf_file, 'wb') as fw: # type: ignore
|
|
|
|
fw.write(self.core_src.data) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
core_elf = ESPCoreDumpElfFile(self.core_elf_file, e_machine=e_machine) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
# Read note segments from core file which are belong to tasks (TCB or stack)
|
|
|
|
for seg in core_elf.note_segments:
|
|
|
|
for note_sec in seg.note_secs:
|
|
|
|
# Check for version info note
|
2023-04-12 04:18:30 -04:00
|
|
|
if note_sec.name == b'ESP_CORE_DUMP_INFO' \
|
2021-01-19 00:39:04 -05:00
|
|
|
and note_sec.type == ESPCoreDumpElfFile.PT_INFO \
|
|
|
|
and exe_name:
|
|
|
|
exe_elf = ElfFile(exe_name)
|
|
|
|
app_sha256 = binascii.hexlify(exe_elf.sha256)
|
|
|
|
coredump_sha256_struct = Struct(
|
|
|
|
'ver' / Int32ul,
|
|
|
|
'sha256' / Bytes(64) # SHA256 as hex string
|
|
|
|
)
|
|
|
|
coredump_sha256 = coredump_sha256_struct.parse(note_sec.desc[:coredump_sha256_struct.sizeof()])
|
2023-04-12 21:44:01 -04:00
|
|
|
|
|
|
|
logging.debug('App SHA256: {!r}'.format(app_sha256))
|
|
|
|
logging.debug('Core dump SHA256: {!r}'.format(coredump_sha256))
|
|
|
|
|
|
|
|
# Actual coredump SHA may be shorter than a full SHA256 hash
|
|
|
|
# with NUL byte padding, according to the app's APP_RETRIEVE_LEN_ELF_SHA
|
|
|
|
# length
|
|
|
|
core_sha_trimmed = coredump_sha256.sha256.rstrip(b'\x00').decode()
|
|
|
|
app_sha_trimmed = app_sha256[:len(core_sha_trimmed)].decode()
|
|
|
|
|
|
|
|
if core_sha_trimmed != app_sha_trimmed:
|
2021-01-19 00:39:04 -05:00
|
|
|
raise ESPCoreDumpLoaderError(
|
2023-04-12 21:44:01 -04:00
|
|
|
'Invalid application image for coredump: coredump SHA256({}) != app SHA256({}).'
|
|
|
|
.format(core_sha_trimmed, app_sha_trimmed))
|
2021-01-19 00:39:04 -05:00
|
|
|
if coredump_sha256.ver != self.version:
|
|
|
|
raise ESPCoreDumpLoaderError(
|
|
|
|
'Invalid application image for coredump: coredump SHA256 version({}) != app SHA256 version({}).'
|
|
|
|
.format(coredump_sha256.ver, self.version))
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-08 23:39:37 -04:00
|
|
|
def _get_aligned_size(size, align_with=4): # type: (int, int) -> int
|
2021-01-19 00:39:04 -05:00
|
|
|
if size % align_with:
|
|
|
|
return align_with * (size // align_with + 1)
|
|
|
|
return size
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-08 23:39:37 -04:00
|
|
|
def _build_note_section(name, sec_type, desc): # type: (str, int, str) -> bytes
|
|
|
|
b_name = bytearray(name, encoding='ascii') + b'\0'
|
|
|
|
return NoteSection.build({ # type: ignore
|
|
|
|
'namesz': len(b_name),
|
2021-01-19 00:39:04 -05:00
|
|
|
'descsz': len(desc),
|
|
|
|
'type': sec_type,
|
2021-04-08 23:39:37 -04:00
|
|
|
'name': b_name,
|
2021-01-19 00:39:04 -05:00
|
|
|
'desc': desc,
|
|
|
|
})
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _extract_bin_corefile(self, e_machine=ESPCoreDumpElfFile.EM_XTENSA): # type: (int) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Creates core dump ELF file
|
|
|
|
"""
|
|
|
|
coredump_data_struct = Struct(
|
|
|
|
'tasks' / GreedyRange(
|
|
|
|
AlignedStruct(
|
|
|
|
4,
|
|
|
|
'task_header' / TaskHeader,
|
2021-04-08 23:39:37 -04:00
|
|
|
'tcb' / Bytes(self.header.tcbsz), # type: ignore
|
|
|
|
'stack' / Bytes(abs_(this.task_header.stack_top - this.task_header.stack_end)), # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
)
|
|
|
|
),
|
2021-04-08 23:39:37 -04:00
|
|
|
'mem_seg_headers' / MemSegmentHeader[self.core_src.header.segs_num] # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
)
|
2021-04-08 23:39:37 -04:00
|
|
|
core_elf = ESPCoreDumpElfFile(e_machine=e_machine)
|
2021-01-19 00:39:04 -05:00
|
|
|
notes = b''
|
|
|
|
core_dump_info_notes = b''
|
|
|
|
task_info_notes = b''
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
coredump_data = coredump_data_struct.parse(self.core_src.data) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
for i, task in enumerate(coredump_data.tasks):
|
|
|
|
stack_len_aligned = self._get_aligned_size(abs(task.task_header.stack_top - task.task_header.stack_end))
|
|
|
|
task_status_kwargs = {
|
|
|
|
'task_index': i,
|
|
|
|
'task_flags': TASK_STATUS_CORRECT,
|
|
|
|
'task_tcb_addr': task.task_header.tcb_addr,
|
|
|
|
'task_stack_start': min(task.task_header.stack_top, task.task_header.stack_end),
|
2021-04-08 23:39:37 -04:00
|
|
|
'task_stack_end': max(task.task_header.stack_top, task.task_header.stack_end),
|
2021-01-19 00:39:04 -05:00
|
|
|
'task_stack_len': stack_len_aligned,
|
|
|
|
'task_name': Padding(16).build({}) # currently we don't have task_name, keep it as padding
|
|
|
|
}
|
|
|
|
|
|
|
|
# Write TCB
|
|
|
|
try:
|
2021-04-08 23:39:37 -04:00
|
|
|
if self.target_methods.tcb_is_sane(task.task_header.tcb_addr, self.header.tcbsz): # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
core_elf.add_segment(task.task_header.tcb_addr,
|
|
|
|
task.tcb,
|
|
|
|
ElfFile.PT_LOAD,
|
|
|
|
ElfSegment.PF_R | ElfSegment.PF_W)
|
2021-04-08 23:39:37 -04:00
|
|
|
elif task.task_header.tcb_addr and self.target_methods.addr_is_fake(task.task_header.tcb_addr):
|
2021-01-19 00:39:04 -05:00
|
|
|
task_status_kwargs['task_flags'] |= TASK_STATUS_TCB_CORRUPTED
|
|
|
|
except ESPCoreDumpLoaderError as e:
|
|
|
|
logging.warning('Skip TCB {} bytes @ 0x{:x}. (Reason: {})'
|
2021-04-08 23:39:37 -04:00
|
|
|
.format(self.header.tcbsz, task.task_header.tcb_addr, e)) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
# Write stack
|
|
|
|
try:
|
2021-04-08 23:39:37 -04:00
|
|
|
if self.target_methods.stack_is_sane(task_status_kwargs['task_stack_start'],
|
|
|
|
task_status_kwargs['task_stack_end']):
|
2021-01-19 00:39:04 -05:00
|
|
|
core_elf.add_segment(task_status_kwargs['task_stack_start'],
|
|
|
|
task.stack,
|
|
|
|
ElfFile.PT_LOAD,
|
|
|
|
ElfSegment.PF_R | ElfSegment.PF_W)
|
2021-04-08 23:39:37 -04:00
|
|
|
elif (task_status_kwargs['task_stack_start']
|
|
|
|
and self.target_methods.addr_is_fake(task_status_kwargs['task_stack_start'])):
|
2021-01-19 00:39:04 -05:00
|
|
|
task_status_kwargs['task_flags'] |= TASK_STATUS_TCB_CORRUPTED
|
|
|
|
core_elf.add_segment(task_status_kwargs['task_stack_start'],
|
|
|
|
task.stack,
|
|
|
|
ElfFile.PT_LOAD,
|
|
|
|
ElfSegment.PF_R | ElfSegment.PF_W)
|
|
|
|
except ESPCoreDumpLoaderError as e:
|
|
|
|
logging.warning('Skip task\'s ({:x}) stack {} bytes @ 0x{:x}. (Reason: {})'
|
|
|
|
.format(task_status_kwargs['tcb_addr'],
|
|
|
|
task_status_kwargs['stack_len_aligned'],
|
|
|
|
task_status_kwargs['stack_base'],
|
|
|
|
e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
logging.debug('Stack start_end: 0x{:x} @ 0x{:x}'
|
|
|
|
.format(task.task_header.stack_top, task.task_header.stack_end))
|
2021-04-08 23:39:37 -04:00
|
|
|
task_regs, extra_regs = self.target_methods.get_registers_from_stack(
|
2021-01-19 00:39:04 -05:00
|
|
|
task.stack,
|
|
|
|
task.task_header.stack_end > task.task_header.stack_top
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
raise ESPCoreDumpLoaderError(str(e))
|
|
|
|
|
|
|
|
task_info_notes += self._build_note_section('TASK_INFO',
|
|
|
|
ESPCoreDumpElfFile.PT_TASK_INFO,
|
|
|
|
EspTaskStatus.build(task_status_kwargs))
|
|
|
|
notes += self._build_note_section('CORE',
|
|
|
|
ElfFile.PT_LOAD,
|
2021-04-08 23:39:37 -04:00
|
|
|
self.target_methods.build_prstatus_data(task.task_header.tcb_addr,
|
|
|
|
task_regs))
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
if len(core_dump_info_notes) == 0: # the first task is the crashed task
|
2021-01-19 00:39:04 -05:00
|
|
|
core_dump_info_notes += self._build_note_section('ESP_CORE_DUMP_INFO',
|
|
|
|
ESPCoreDumpElfFile.PT_INFO,
|
2021-04-08 23:39:37 -04:00
|
|
|
Int32ul.build(self.header.ver)) # type: ignore
|
|
|
|
_regs = [task.task_header.tcb_addr]
|
|
|
|
|
|
|
|
# For xtensa, we need to put the exception registers into the extra info as well
|
|
|
|
if e_machine == ESPCoreDumpElfFile.EM_XTENSA and extra_regs:
|
|
|
|
for reg_id in extra_regs:
|
|
|
|
_regs.extend([reg_id, extra_regs[reg_id]])
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
core_dump_info_notes += self._build_note_section(
|
|
|
|
'EXTRA_INFO',
|
|
|
|
ESPCoreDumpElfFile.PT_EXTRA_INFO,
|
2021-04-08 23:39:37 -04:00
|
|
|
Int32ul[len(_regs)].build(_regs)
|
2021-01-19 00:39:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if self.dump_ver == self.BIN_V2:
|
|
|
|
for header in coredump_data.mem_seg_headers:
|
|
|
|
logging.debug('Read memory segment {} bytes @ 0x{:x}'.format(header.mem_sz, header.mem_start))
|
|
|
|
core_elf.add_segment(header.mem_start, header.data, ElfFile.PT_LOAD, ElfSegment.PF_R | ElfSegment.PF_W)
|
|
|
|
|
|
|
|
# add notes
|
|
|
|
try:
|
|
|
|
core_elf.add_segment(0, notes, ElfFile.PT_NOTE, 0)
|
|
|
|
except ESPCoreDumpLoaderError as e:
|
|
|
|
logging.warning('Skip NOTES segment {:d} bytes @ 0x{:x}. (Reason: {})'.format(len(notes), 0, e))
|
|
|
|
# add core dump info notes
|
|
|
|
try:
|
|
|
|
core_elf.add_segment(0, core_dump_info_notes, ElfFile.PT_NOTE, 0)
|
|
|
|
except ESPCoreDumpLoaderError as e:
|
|
|
|
logging.warning('Skip core dump info NOTES segment {:d} bytes @ 0x{:x}. (Reason: {})'
|
|
|
|
.format(len(core_dump_info_notes), 0, e))
|
|
|
|
try:
|
|
|
|
core_elf.add_segment(0, task_info_notes, ElfFile.PT_NOTE, 0)
|
|
|
|
except ESPCoreDumpLoaderError as e:
|
|
|
|
logging.warning('Skip failed tasks info NOTES segment {:d} bytes @ 0x{:x}. (Reason: {})'
|
|
|
|
.format(len(task_info_notes), 0, e))
|
|
|
|
# dump core ELF
|
|
|
|
core_elf.e_type = ElfFile.ET_CORE
|
2021-04-08 23:39:37 -04:00
|
|
|
core_elf.dump(self.core_elf_file) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
class ESPCoreDumpFlashLoader(EspCoreDumpLoader):
|
|
|
|
ESP_COREDUMP_PART_TABLE_OFF = 0x8000
|
|
|
|
|
2023-11-01 05:27:36 -04:00
|
|
|
def __init__(self, offset, target=None, port=None, baud=None, part_table_offset=None):
|
|
|
|
# type: (int, Optional[str], Optional[str], Optional[int], Optional[int]) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
super(ESPCoreDumpFlashLoader, self).__init__()
|
|
|
|
self.port = port
|
|
|
|
self.baud = baud
|
2023-11-01 05:27:36 -04:00
|
|
|
self.part_table_offset = part_table_offset
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
self._get_core_src(offset, target)
|
|
|
|
self.target = self._load_core_src()
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _get_core_src(self, off, target=None): # type: (int, Optional[str]) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Loads core dump from flash using parttool or elftool (if offset is set)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if off:
|
|
|
|
logging.info('Invoke esptool to read image.')
|
2021-04-08 23:39:37 -04:00
|
|
|
self._invoke_esptool(off=off, target=target)
|
2021-01-19 00:39:04 -05:00
|
|
|
else:
|
|
|
|
logging.info('Invoke parttool to read image.')
|
|
|
|
self._invoke_parttool()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if e.output:
|
|
|
|
logging.info(e.output)
|
|
|
|
logging.error('Error during the subprocess execution')
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _invoke_esptool(self, off=None, target=None): # type: (Optional[int], Optional[str]) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Loads core dump from flash using elftool
|
|
|
|
"""
|
2021-04-08 23:39:37 -04:00
|
|
|
if target is None:
|
|
|
|
target = 'auto'
|
|
|
|
tool_args = [sys.executable, ESPTOOL_PY, '-c', target]
|
2021-01-19 00:39:04 -05:00
|
|
|
if self.port:
|
|
|
|
tool_args.extend(['-p', self.port])
|
|
|
|
if self.baud:
|
|
|
|
tool_args.extend(['-b', str(self.baud)])
|
|
|
|
|
|
|
|
self.core_src_file = self._create_temp_file()
|
|
|
|
try:
|
|
|
|
(part_offset, part_size) = self._get_core_dump_partition_info()
|
|
|
|
if not off:
|
|
|
|
off = part_offset # set default offset if not specified
|
|
|
|
logging.warning('The core dump image offset is not specified. Use partition offset: %d.', part_offset)
|
|
|
|
if part_offset != off:
|
|
|
|
logging.warning('Predefined image offset: %d does not match core dump partition offset: %d', off,
|
|
|
|
part_offset)
|
|
|
|
|
|
|
|
# Here we use V1 format to locate the size
|
|
|
|
tool_args.extend(['read_flash', str(off), str(EspCoreDumpV1Header.sizeof())])
|
2021-04-08 23:39:37 -04:00
|
|
|
tool_args.append(self.core_src_file) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
|
|
|
|
# read core dump length
|
|
|
|
et_out = subprocess.check_output(tool_args)
|
|
|
|
if et_out:
|
|
|
|
logging.info(et_out.decode('utf-8'))
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
header = EspCoreDumpV1Header.parse(open(self.core_src_file, 'rb').read()) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
if not header or not 0 < header.tot_len <= part_size:
|
|
|
|
logging.error('Incorrect size of core dump image: {}, use partition size instead: {}'
|
|
|
|
.format(header.tot_len, part_size))
|
|
|
|
coredump_len = part_size
|
|
|
|
else:
|
|
|
|
coredump_len = header.tot_len
|
|
|
|
# set actual size of core dump image and read it from flash
|
|
|
|
tool_args[-2] = str(coredump_len)
|
|
|
|
et_out = subprocess.check_output(tool_args)
|
|
|
|
if et_out:
|
|
|
|
logging.info(et_out.decode('utf-8'))
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
logging.error('esptool script execution failed with err %d', e.returncode)
|
|
|
|
logging.debug("Command ran: '%s'", e.cmd)
|
|
|
|
logging.debug('Command out:')
|
|
|
|
logging.debug(e.output)
|
|
|
|
raise e
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _invoke_parttool(self): # type: () -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Loads core dump from flash using parttool
|
|
|
|
"""
|
|
|
|
tool_args = [sys.executable, PARTTOOL_PY]
|
|
|
|
if self.port:
|
|
|
|
tool_args.extend(['--port', self.port])
|
2023-11-01 05:27:36 -04:00
|
|
|
if self.part_table_offset:
|
|
|
|
tool_args.extend(['--partition-table-offset', str(self.part_table_offset)])
|
2021-01-19 00:39:04 -05:00
|
|
|
tool_args.extend(['read_partition', '--partition-type', 'data', '--partition-subtype', 'coredump', '--output'])
|
|
|
|
|
|
|
|
self.core_src_file = self._create_temp_file()
|
|
|
|
try:
|
2021-04-08 23:39:37 -04:00
|
|
|
tool_args.append(self.core_src_file) # type: ignore
|
2021-01-19 00:39:04 -05:00
|
|
|
# read core dump partition
|
2023-11-01 05:27:36 -04:00
|
|
|
print(f"Running {' '.join(tool_args)}")
|
2021-01-19 00:39:04 -05:00
|
|
|
et_out = subprocess.check_output(tool_args)
|
|
|
|
if et_out:
|
|
|
|
logging.info(et_out.decode('utf-8'))
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
logging.error('parttool script execution failed with err %d', e.returncode)
|
|
|
|
logging.debug("Command ran: '%s'", e.cmd)
|
|
|
|
logging.debug('Command out:')
|
|
|
|
logging.debug(e.output)
|
|
|
|
raise e
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _get_core_dump_partition_info(self, part_off=None): # type: (Optional[int]) -> Tuple[int, int]
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Get core dump partition info using parttool
|
|
|
|
"""
|
|
|
|
logging.info('Retrieving core dump partition offset and size...')
|
2023-11-01 05:27:36 -04:00
|
|
|
part_off = part_off or self.part_table_offset or self.ESP_COREDUMP_PART_TABLE_OFF
|
2021-01-19 00:39:04 -05:00
|
|
|
try:
|
|
|
|
tool_args = [sys.executable, PARTTOOL_PY, '-q', '--partition-table-offset', str(part_off)]
|
|
|
|
if self.port:
|
|
|
|
tool_args.extend(['--port', self.port])
|
|
|
|
invoke_args = tool_args + ['get_partition_info', '--partition-type', 'data',
|
|
|
|
'--partition-subtype', 'coredump',
|
|
|
|
'--info', 'offset', 'size']
|
|
|
|
res = subprocess.check_output(invoke_args).strip()
|
|
|
|
(offset_str, size_str) = res.rsplit(b'\n')[-1].split(b' ')
|
|
|
|
size = int(size_str, 16)
|
|
|
|
offset = int(offset_str, 16)
|
|
|
|
logging.info('Core dump partition offset=%d, size=%d', offset, size)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
logging.error('parttool get partition info failed with err %d', e.returncode)
|
|
|
|
logging.debug("Command ran: '%s'", e.cmd)
|
|
|
|
logging.debug('Command out:')
|
|
|
|
logging.debug(e.output)
|
|
|
|
logging.error('Check if the coredump partition exists in partition table.')
|
|
|
|
raise e
|
|
|
|
return offset, size
|
|
|
|
|
|
|
|
|
|
|
|
class ESPCoreDumpFileLoader(EspCoreDumpLoader):
|
2021-04-08 23:39:37 -04:00
|
|
|
def __init__(self, path, is_b64=False): # type: (str, bool) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
super(ESPCoreDumpFileLoader, self).__init__()
|
|
|
|
self.is_b64 = is_b64
|
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
self._get_core_src(path)
|
|
|
|
self.target = self._load_core_src()
|
2021-01-19 00:39:04 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
def _get_core_src(self, path): # type: (str) -> None
|
2021-01-19 00:39:04 -05:00
|
|
|
"""
|
|
|
|
Loads core dump from (raw binary or base64-encoded) file
|
|
|
|
"""
|
|
|
|
logging.debug('Load core dump from "%s", %s format', path, 'b64' if self.is_b64 else 'raw')
|
|
|
|
if not self.is_b64:
|
2021-04-08 23:39:37 -04:00
|
|
|
self.core_src_file = path
|
2021-01-19 00:39:04 -05:00
|
|
|
else:
|
|
|
|
self.core_src_file = self._create_temp_file()
|
2021-04-08 23:39:37 -04:00
|
|
|
with open(self.core_src_file, 'wb') as fw:
|
|
|
|
with open(path, 'rb') as fb64:
|
|
|
|
while True:
|
|
|
|
line = fb64.readline()
|
|
|
|
if len(line) == 0:
|
|
|
|
break
|
|
|
|
data = base64.standard_b64decode(line.rstrip(b'\r\n'))
|
|
|
|
fw.write(data) # type: ignore
|