pytest_gdb.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. import logging
  4. import os
  5. import re
  6. import subprocess
  7. import sys
  8. import pexpect
  9. import pytest
  10. from pytest_embedded_idf import IdfDut
  11. try:
  12. from idf_py_actions.debug_ext import get_openocd_arguments
  13. except ModuleNotFoundError:
  14. sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
  15. from idf_py_actions.debug_ext import get_openocd_arguments
  16. @pytest.mark.supported_targets
  17. @pytest.mark.jtag
  18. def test_idf_gdb(dut: IdfDut) -> None:
  19. # Need to wait a moment to connect via OpenOCD after the hard reset happened.
  20. # Along with this check that app runs ok
  21. dut.expect('Hello world!')
  22. # Don't need to have output from UART anymore
  23. dut.serial.stop_redirect_thread()
  24. with open(os.path.join(dut.logdir, 'ocd.txt'), 'w') as ocd_log:
  25. cmd = ['openocd', *get_openocd_arguments(dut.target).split()]
  26. openocd_scripts = os.getenv('OPENOCD_SCRIPTS')
  27. if openocd_scripts:
  28. cmd.extend(['-s', openocd_scripts])
  29. logging.info('Running %s', cmd)
  30. ocd = subprocess.Popen(cmd, stdout=ocd_log, stderr=ocd_log)
  31. try:
  32. gdb_env = os.environ.copy()
  33. gdb_env['ESP_IDF_GDB_TESTING'] = '1'
  34. with open(os.path.join(dut.logdir, 'gdb.txt'), 'w') as gdb_log, \
  35. pexpect.spawn(f'idf.py -B {dut.app.binary_path} gdb --batch',
  36. env=gdb_env,
  37. timeout=60,
  38. logfile=gdb_log,
  39. encoding='utf-8',
  40. codec_errors='ignore') as p:
  41. p.expect(re.compile(r'add symbol table from file.*bootloader.elf'))
  42. p.expect(re.compile(r'add symbol table from file.*rom.elf'))
  43. p.expect_exact('hit Temporary breakpoint 1, app_main ()')
  44. finally:
  45. ocd.terminate()
  46. ocd.kill()