DebugUtils.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import unicode_literals
  15. import logging
  16. from io import open
  17. import pexpect
  18. import pygdbmi.gdbcontroller
  19. from tiny_test_fw import Utility
  20. try:
  21. import debug_backend
  22. except ImportError:
  23. # Exception is ignored so the package is not required for those who don't use debug utils.
  24. err_msg = 'Please install py_debug_backend for debug utils to work properly!'
  25. class debug_backend(object):
  26. @staticmethod
  27. def create_oocd(*args, **kwargs):
  28. raise RuntimeError(err_msg)
  29. @staticmethod
  30. def create_gdb(*args, **kwargs):
  31. raise RuntimeError(err_msg)
  32. class CustomProcess(object):
  33. def __init__(self, cmd, logfile, verbose=True):
  34. self.verbose = verbose
  35. self.f = open(logfile, 'w')
  36. if self.verbose:
  37. Utility.console_log('Starting {} > {}'.format(cmd, self.f.name))
  38. self.pexpect_proc = pexpect.spawn(cmd, timeout=60, logfile=self.f, encoding='utf-8', codec_errors='ignore')
  39. def __enter__(self):
  40. return self
  41. def close(self):
  42. self.pexpect_proc.terminate(force=True)
  43. def __exit__(self, type, value, traceback):
  44. self.close()
  45. self.f.close()
  46. class OCDBackend(object):
  47. def __init__(self, logfile_path, target, cfg_cmds=[], extra_args=[]):
  48. # TODO Use configuration file implied by the test environment (board)
  49. self.oocd = debug_backend.create_oocd(chip_name=target,
  50. oocd_exec='openocd',
  51. oocd_scripts=None,
  52. oocd_cfg_files=['board/esp32-wrover-kit-3.3v.cfg'],
  53. oocd_cfg_cmds=cfg_cmds,
  54. oocd_debug=2,
  55. oocd_args=extra_args,
  56. host='localhost',
  57. log_level=logging.DEBUG,
  58. log_stream_handler=None,
  59. log_file_handler=logging.FileHandler(logfile_path, 'w'),
  60. scope=None)
  61. self.oocd.start()
  62. def __enter__(self):
  63. return self
  64. def __exit__(self, type, value, traceback):
  65. self.oocd.stop()
  66. def cmd_exec(self, cmd):
  67. return self.oocd.cmd_exec(cmd)
  68. def apptrace_start(self, trace_args):
  69. self.oocd.apptrace_start(trace_args)
  70. def apptrace_stop(self):
  71. self.oocd.apptrace_stop()
  72. def apptrace_wait_stop(self, tmo=10):
  73. self.oocd.apptrace_wait_stop(tmo=tmo)
  74. class GDBBackend(object):
  75. def __init__(self, logfile_path, elffile_path, target, gdbinit_path=None, working_dir=None):
  76. self.gdb = debug_backend.create_gdb(chip_name=target,
  77. gdb_path='xtensa-{}-elf-gdb'.format(target),
  78. remote_target=None,
  79. extended_remote_mode=False,
  80. gdb_log_file=logfile_path,
  81. log_level=None,
  82. log_stream_handler=None,
  83. log_file_handler=None,
  84. scope=None)
  85. if working_dir:
  86. self.gdb.console_cmd_run('directory {}'.format(working_dir))
  87. self.gdb.exec_file_set(elffile_path)
  88. if gdbinit_path:
  89. try:
  90. self.gdb.console_cmd_run('source {}'.format(gdbinit_path))
  91. except debug_backend.defs.DebuggerTargetStateTimeoutError:
  92. # The internal timeout is not enough on RPI for more time consuming operations, e.g. "load".
  93. # So lets try to apply the commands one-by-one:
  94. with open(gdbinit_path, 'r') as f:
  95. for line in f:
  96. line = line.strip()
  97. if len(line) > 0 and not line.startswith('#'):
  98. self.gdb.console_cmd_run(line)
  99. # Note that some commands cannot be applied with console_cmd_run, e.g. "commands"
  100. def __enter__(self):
  101. return self
  102. def __exit__(self, type, value, traceback):
  103. try:
  104. self.gdb.gdb_exit()
  105. except pygdbmi.gdbcontroller.NoGdbProcessError as e:
  106. # the debug backend can fail on gdb exit when it tries to read the response after issuing the exit command.
  107. Utility.console_log('Ignoring exception: {}'.format(e), 'O')
  108. except debug_backend.defs.DebuggerTargetStateTimeoutError:
  109. Utility.console_log('Ignoring timeout exception for GDB exit', 'O')