panic_dut.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. import logging
  4. import os
  5. import subprocess
  6. import sys
  7. from typing import Any, Dict, List, Optional, TextIO
  8. import pexpect
  9. from panic_utils import NoGdbProcessError, attach_logger, quote_string, sha256, verify_valid_gdb_subprocess
  10. from pygdbmi.gdbcontroller import GdbController
  11. from pytest_embedded_idf.app import IdfApp
  12. from pytest_embedded_idf.dut import IdfDut
  13. from pytest_embedded_idf.serial import IdfSerial
  14. class PanicTestDut(IdfDut):
  15. BOOT_CMD_ADDR = 0x9000
  16. BOOT_CMD_SIZE = 0x1000
  17. DEFAULT_EXPECT_TIMEOUT = 10
  18. COREDUMP_UART_START = '================= CORE DUMP START ================='
  19. COREDUMP_UART_END = '================= CORE DUMP END ================='
  20. app: IdfApp
  21. serial: IdfSerial
  22. def __init__(self, *args: Any, **kwargs: Any) -> None:
  23. super().__init__(*args, **kwargs)
  24. self.gdbmi: Optional[GdbController] = None
  25. # record this since pygdbmi is using logging.debug to generate some single character mess
  26. self.log_level = logging.getLogger().level
  27. # pygdbmi is using logging.debug to generate some single character mess
  28. if self.log_level <= logging.DEBUG:
  29. logging.getLogger().setLevel(logging.INFO)
  30. self.coredump_output: Optional[TextIO] = None
  31. def close(self) -> None:
  32. if self.gdbmi:
  33. logging.info('Waiting for GDB to exit')
  34. self.gdbmi.exit()
  35. super().close()
  36. def revert_log_level(self) -> None:
  37. logging.getLogger().setLevel(self.log_level)
  38. @property
  39. def is_xtensa(self) -> bool:
  40. return self.target in self.XTENSA_TARGETS
  41. def run_test_func(self, test_func_name: str) -> None:
  42. self.expect_exact('Enter test name:')
  43. self.write(test_func_name)
  44. self.expect_exact('Got test name: ' + test_func_name)
  45. def expect_none(self, pattern, **kwargs) -> None: # type: ignore
  46. """like dut.expect_all, but with an inverse logic"""
  47. if 'timeout' not in kwargs:
  48. kwargs['timeout'] = 1
  49. try:
  50. res = self.expect(pattern, **kwargs)
  51. raise AssertionError(f'Unexpected: {res.group().decode("utf8")}')
  52. except pexpect.TIMEOUT:
  53. pass
  54. def expect_backtrace(self) -> None:
  55. assert self.is_xtensa, 'Backtrace can be printed only on Xtensa'
  56. match = self.expect(r'Backtrace:( 0x[0-9a-fA-F]{8}:0x[0-9a-fA-F]{8})+(?P<corrupted> \|<-CORRUPTED)?')
  57. assert not match.group('corrupted')
  58. def expect_corrupted_backtrace(self) -> None:
  59. assert self.is_xtensa, 'Backtrace can be printed only on Xtensa'
  60. self.expect_exact('Backtrace:')
  61. self.expect_exact('CORRUPTED')
  62. def expect_stack_dump(self) -> None:
  63. assert not self.is_xtensa, 'Stack memory dump is only printed on RISC-V'
  64. self.expect_exact('Stack memory:')
  65. def expect_gme(self, reason: str) -> None:
  66. """Expect method for Guru Meditation Errors"""
  67. self.expect_exact(f"Guru Meditation Error: Core 0 panic'ed ({reason})")
  68. def expect_reg_dump(self, core: int = 0) -> None:
  69. """Expect method for the register dump"""
  70. self.expect(r'Core\s+%d register dump:' % core)
  71. def expect_cpu_reset(self) -> None:
  72. # no digital system reset for panic handling restarts (see IDF-7255)
  73. self.expect(r'.*rst:.*(RTC_SW_CPU_RST|SW_CPU_RESET|SW_CPU)')
  74. def expect_elf_sha256(self) -> None:
  75. """Expect method for ELF SHA256 line"""
  76. elf_sha256 = sha256(self.app.elf_file)
  77. elf_sha256_len = int(
  78. self.app.sdkconfig.get('CONFIG_APP_RETRIEVE_LEN_ELF_SHA', '9')
  79. )
  80. self.expect_exact('ELF file SHA256: ' + elf_sha256[0:elf_sha256_len])
  81. def _call_espcoredump(
  82. self, extra_args: List[str], coredump_file_name: str, output_file_name: str
  83. ) -> None:
  84. # no "with" here, since we need the file to be open for later inspection by the test case
  85. if not self.coredump_output:
  86. self.coredump_output = open(output_file_name, 'w')
  87. espcoredump_script = os.path.join(
  88. os.environ['IDF_PATH'], 'components', 'espcoredump', 'espcoredump.py'
  89. )
  90. espcoredump_args = [
  91. sys.executable,
  92. espcoredump_script,
  93. 'info_corefile',
  94. '--core',
  95. coredump_file_name,
  96. ]
  97. espcoredump_args += extra_args
  98. espcoredump_args.append(self.app.elf_file)
  99. logging.info('Running %s', ' '.join(espcoredump_args))
  100. logging.info('espcoredump output is written to %s', self.coredump_output.name)
  101. subprocess.check_call(espcoredump_args, stdout=self.coredump_output)
  102. self.coredump_output.flush()
  103. self.coredump_output.seek(0)
  104. def process_coredump_uart(self) -> None:
  105. """Extract the core dump from UART output of the test, run espcoredump on it"""
  106. self.expect(self.COREDUMP_UART_START)
  107. res = self.expect('(.+)' + self.COREDUMP_UART_END)
  108. coredump_base64 = res.group(1).decode('utf8')
  109. with open(os.path.join(self.logdir, 'coredump_data.b64'), 'w') as coredump_file:
  110. logging.info('Writing UART base64 core dump to %s', coredump_file.name)
  111. coredump_file.write(coredump_base64)
  112. output_file_name = os.path.join(self.logdir, 'coredump_uart_result.txt')
  113. self._call_espcoredump(
  114. ['--core-format', 'b64'], coredump_file.name, output_file_name
  115. )
  116. def process_coredump_flash(self) -> None:
  117. """Extract the core dump from flash, run espcoredump on it"""
  118. coredump_file_name = os.path.join(self.logdir, 'coredump_data.bin')
  119. logging.info('Writing flash binary core dump to %s', coredump_file_name)
  120. self.serial.dump_flash(partition='coredump', output=coredump_file_name)
  121. output_file_name = os.path.join(self.logdir, 'coredump_flash_result.txt')
  122. self._call_espcoredump(
  123. ['--core-format', 'raw'], coredump_file_name, output_file_name
  124. )
  125. def gdb_write(self, command: str) -> Any:
  126. """
  127. Wrapper to write to gdb with a longer timeout, as test runner
  128. host can be slow sometimes
  129. """
  130. assert self.gdbmi, 'This function should be called only after start_gdb'
  131. return self.gdbmi.write(command, timeout_sec=10)
  132. def start_gdb(self) -> None:
  133. """
  134. Runs GDB and connects it to the "serial" port of the DUT.
  135. After this, the DUT expect methods can no longer be used to capture output.
  136. """
  137. gdb_args = ['--nx', '--quiet', '--interpreter=mi2']
  138. if self.is_xtensa:
  139. gdb_path = 'xtensa-esp-elf-gdb-no-python' # TODO: GCC-311
  140. gdb_args = [f'--mcpu={self.target}'] + gdb_args
  141. else:
  142. gdb_path = 'riscv32-esp-elf-gdb-no-python' # TODO: GCC-311
  143. try:
  144. from pygdbmi.constants import GdbTimeoutError
  145. gdb_command = [gdb_path] + gdb_args
  146. self.gdbmi = GdbController(command=gdb_command)
  147. pygdbmi_logger = attach_logger()
  148. except ImportError:
  149. # fallback for pygdbmi<0.10.0.0.
  150. from pygdbmi.gdbcontroller import GdbTimeoutError
  151. self.gdbmi = GdbController(gdb_path=gdb_path, gdb_args=gdb_args)
  152. pygdbmi_logger = self.gdbmi.logger
  153. # pygdbmi logs to console by default, make it log to a file instead
  154. pygdbmi_log_file_name = os.path.join(self.logdir, 'pygdbmi_log.txt')
  155. pygdbmi_logger.setLevel(logging.DEBUG)
  156. while pygdbmi_logger.hasHandlers():
  157. pygdbmi_logger.removeHandler(pygdbmi_logger.handlers[0])
  158. log_handler = logging.FileHandler(pygdbmi_log_file_name)
  159. log_handler.setFormatter(
  160. logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
  161. )
  162. logging.info(f'Saving pygdbmi logs to {pygdbmi_log_file_name}')
  163. pygdbmi_logger.addHandler(log_handler)
  164. try:
  165. gdb_command = self.gdbmi.command
  166. except AttributeError:
  167. # fallback for pygdbmi < 0.10
  168. gdb_command = self.gdbmi.cmd
  169. logging.info(f'Running command: "{" ".join(quote_string(c) for c in gdb_command)}"')
  170. for _ in range(10):
  171. try:
  172. # GdbController creates a process with subprocess.Popen(). Is it really running? It is probable that
  173. # an RPI under high load will get non-responsive during creating a lot of processes.
  174. if not hasattr(self.gdbmi, 'verify_valid_gdb_subprocess'):
  175. # for pygdbmi >= 0.10.0.0
  176. verify_valid_gdb_subprocess(self.gdbmi.gdb_process)
  177. resp = self.gdbmi.get_gdb_response(
  178. timeout_sec=10
  179. ) # calls verify_valid_gdb_subprocess() internally for pygdbmi < 0.10.0.0
  180. # it will be interesting to look up this response if the next GDB command fails (times out)
  181. logging.info('GDB response: %s', resp)
  182. break # success
  183. except GdbTimeoutError:
  184. logging.warning(
  185. 'GDB internal error: cannot get response from the subprocess'
  186. )
  187. except NoGdbProcessError:
  188. logging.error('GDB internal error: process is not running')
  189. break # failure - TODO: create another GdbController
  190. except ValueError:
  191. logging.error(
  192. 'GDB internal error: select() returned an unexpected file number'
  193. )
  194. # Set up logging for GDB remote protocol
  195. gdb_remotelog_file_name = os.path.join(self.logdir, 'gdb_remote_log.txt')
  196. self.gdb_write('-gdb-set remotelogfile ' + gdb_remotelog_file_name)
  197. # Load the ELF file
  198. self.gdb_write('-file-exec-and-symbols {}'.format(self.app.elf_file))
  199. # Connect GDB to UART
  200. self.serial.close()
  201. logging.info('Connecting to GDB Stub...')
  202. self.gdb_write('-gdb-set serial baud 115200')
  203. if sys.platform == 'darwin':
  204. assert '/dev/tty.' not in self.serial.port, \
  205. '/dev/tty.* ports can\'t be used with GDB on macOS. Use with /dev/cu.* instead.'
  206. # Make sure we get the 'stopped' notification
  207. responses = self.gdb_write('-target-select remote ' + self.serial.port)
  208. stop_response = self.find_gdb_response('stopped', 'notify', responses)
  209. retries = 3
  210. while not stop_response and retries > 0:
  211. logging.info('Sending -exec-interrupt')
  212. responses = self.gdb_write('-exec-interrupt')
  213. stop_response = self.find_gdb_response('stopped', 'notify', responses)
  214. retries -= 1
  215. frame = stop_response['payload']['frame']
  216. if 'file' not in frame:
  217. frame['file'] = '?'
  218. if 'line' not in frame:
  219. frame['line'] = '?'
  220. logging.info('Stopped in {func} at {addr} ({file}:{line})'.format(**frame))
  221. # Drain remaining responses
  222. self.gdbmi.get_gdb_response(raise_error_on_timeout=False)
  223. def gdb_backtrace(self) -> Any:
  224. """
  225. Returns the list of stack frames for the current thread.
  226. Each frame is a dictionary, refer to pygdbmi docs for the format.
  227. """
  228. assert self.gdbmi
  229. responses = self.gdb_write('-stack-list-frames')
  230. return self.find_gdb_response('done', 'result', responses)['payload']['stack']
  231. @staticmethod
  232. def verify_gdb_backtrace(
  233. gdb_backtrace: List[Any], expected_functions_list: List[Any]
  234. ) -> None:
  235. """
  236. Raises an assert if the function names listed in expected_functions_list do not match the backtrace
  237. given by gdb_backtrace argument. The latter is in the same format as returned by gdb_backtrace()
  238. function.
  239. """
  240. actual_functions_list = [frame['func'] for frame in gdb_backtrace]
  241. if actual_functions_list != expected_functions_list:
  242. logging.error(f'Expected backtrace: {expected_functions_list}')
  243. logging.error(f'Actual backtrace: {actual_functions_list}')
  244. assert False, 'Got unexpected backtrace'
  245. @staticmethod
  246. def find_gdb_response(
  247. message: str, response_type: str, responses: List[Any]
  248. ) -> Any:
  249. """
  250. Helper function which extracts one response from an array of GDB responses, filtering
  251. by message and type. Returned message is a dictionary, refer to pygdbmi docs for the format.
  252. """
  253. def match_response(response: Dict[str, Any]) -> bool:
  254. return response['message'] == message and response['type'] == response_type # type: ignore
  255. filtered_responses = [r for r in responses if match_response(r)]
  256. if not filtered_responses:
  257. return None
  258. return filtered_responses[0]