__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #
  2. # Copyright 2021 Espressif Systems (Shanghai) CO., LTD
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. __version__ = '0.4-dev'
  17. import abc
  18. import os
  19. from abc import abstractmethod
  20. from importlib import import_module
  21. from future.utils import with_metaclass
  22. try:
  23. from typing import Optional, Tuple
  24. except ImportError:
  25. pass
  26. IDF_PATH = os.path.normpath(os.getenv('IDF_PATH', '.'))
  27. XTENSA_TARGETS = ['esp32', 'esp32s2', 'esp32s3']
  28. RISCV_TARGETS = ['esp32c3']
  29. SUPPORTED_TARGETS = XTENSA_TARGETS + RISCV_TARGETS
  30. class ESPCoreDumpError(RuntimeError):
  31. pass
  32. class ESPCoreDumpLoaderError(ESPCoreDumpError):
  33. pass
  34. class BaseArchMethodsMixin(with_metaclass(abc.ABCMeta)): # type: ignore
  35. @staticmethod
  36. @abstractmethod
  37. def get_registers_from_stack(data, grows_down):
  38. # type: (bytes, bool) -> Tuple[list[int], Optional[dict[int, int]]]
  39. """
  40. Parse stack data, growing up stacks are not supported for now.
  41. :param data: stack data
  42. :param grows_down: stack grow direction
  43. :return: return tuple (regs, exception_regs)
  44. """
  45. pass
  46. @staticmethod
  47. @abstractmethod
  48. def build_prstatus_data(tcb_addr, task_regs): # type: (int, list[int]) -> str
  49. """
  50. Build PrStatus note section
  51. :param tcb_addr: tcb addr
  52. :param task_regs: registers
  53. :return: str
  54. """
  55. pass
  56. class BaseTargetMethods(with_metaclass(abc.ABCMeta, BaseArchMethodsMixin)): # type: ignore
  57. UNKNOWN = 'unknown'
  58. TARGET = UNKNOWN
  59. COREDUMP_FAKE_STACK_START = 0x20000000
  60. COREDUMP_FAKE_STACK_LIMIT = 0x30000000
  61. COREDUMP_MAX_TASK_STACK_SIZE = 64 * 1024
  62. def __init__(self): # type: () -> None
  63. if self.TARGET == self.UNKNOWN:
  64. raise ValueError('Please use the derived child-class with valid TARGET')
  65. self._set_attr_from_soc_header()
  66. def _set_attr_from_soc_header(self): # type: () -> None
  67. module = import_module('corefile.soc_headers.{}'.format(self.TARGET))
  68. for k, v in module.__dict__.items():
  69. if k.startswith('SOC_'):
  70. setattr(self, k, v)
  71. def _esp_ptr_in_dram(self, addr): # type: (int) -> bool
  72. return self.SOC_DRAM_LOW <= addr < self.SOC_DRAM_HIGH # type: ignore
  73. def _esp_ptr_in_iram(self, addr): # type: (int) -> bool
  74. return self.SOC_IRAM_LOW <= addr < self.SOC_IRAM_HIGH # type: ignore
  75. def _esp_ptr_in_rtc_slow(self, addr): # type: (int) -> bool
  76. return self.SOC_RTC_DATA_LOW <= addr < self.SOC_RTC_DATA_HIGH # type: ignore
  77. def _esp_ptr_in_rtc_dram_fast(self, addr): # type: (int) -> bool
  78. return self.SOC_RTC_DRAM_LOW <= addr < self.SOC_RTC_DRAM_HIGH # type: ignore
  79. def tcb_is_sane(self, tcb_addr, tcb_size): # type: (int, int) -> bool
  80. for func in [self._esp_ptr_in_dram,
  81. self._esp_ptr_in_iram,
  82. self._esp_ptr_in_rtc_slow,
  83. self._esp_ptr_in_rtc_dram_fast]:
  84. res = func(tcb_addr) and func(tcb_addr + tcb_size - 1)
  85. if res:
  86. return True
  87. return False
  88. def _esp_stack_ptr_in_dram(self, addr): # type: (int) -> bool
  89. return not (addr < self.SOC_DRAM_LOW + 0x10
  90. or addr > self.SOC_DRAM_HIGH - 0x10
  91. or (addr & 0xF) != 0)
  92. def stack_is_sane(self, stack_start, stack_end): # type: (int, int) -> bool
  93. return (self._esp_stack_ptr_in_dram(stack_start)
  94. and self._esp_ptr_in_dram(stack_end)
  95. and stack_start < stack_end
  96. and (stack_end - stack_start) < self.COREDUMP_MAX_TASK_STACK_SIZE)
  97. def addr_is_fake(self, addr): # type: (int) -> bool
  98. return (self.COREDUMP_FAKE_STACK_START <= addr < self.COREDUMP_FAKE_STACK_LIMIT
  99. or addr > 2 ** 31 - 1)