DebugUtils.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from io import open
  16. from tiny_test_fw import Utility
  17. import debug_backend
  18. import logging
  19. import pexpect
  20. import pygdbmi.gdbcontroller
  21. class CustomProcess(object):
  22. def __init__(self, cmd, logfile, verbose=True):
  23. self.verbose = verbose
  24. self.f = open(logfile, 'w')
  25. if self.verbose:
  26. Utility.console_log('Starting {} > {}'.format(cmd, self.f.name))
  27. self.pexpect_proc = pexpect.spawn(cmd, timeout=60, logfile=self.f, encoding='utf-8')
  28. def __enter__(self):
  29. return self
  30. def close(self):
  31. self.pexpect_proc.terminate(force=True)
  32. def __exit__(self, type, value, traceback):
  33. self.close()
  34. self.f.close()
  35. class OCDBackend(object):
  36. def __init__(self, logfile_path, target, cfg_cmds=[], extra_args=[]):
  37. # TODO Use configuration file implied by the test environment (board)
  38. self.oocd = debug_backend.create_oocd(chip_name=target,
  39. oocd_exec='openocd',
  40. oocd_scripts=None,
  41. oocd_cfg_files=['board/esp32-wrover-kit-3.3v.cfg'],
  42. oocd_cfg_cmds=cfg_cmds,
  43. oocd_debug=2,
  44. oocd_args=extra_args,
  45. host='localhost',
  46. log_level=logging.DEBUG,
  47. log_stream_handler=None,
  48. log_file_handler=logging.FileHandler(logfile_path, 'w'),
  49. scope=None)
  50. self.oocd.start()
  51. def __enter__(self):
  52. return self
  53. def __exit__(self, type, value, traceback):
  54. self.oocd.stop()
  55. def cmd_exec(self, cmd):
  56. return self.oocd.cmd_exec(cmd)
  57. class GDBBackend(object):
  58. def __init__(self, logfile_path, elffile_path, target, gdbinit_path=None, working_dir=None):
  59. self.gdb = debug_backend.create_gdb(chip_name=target,
  60. gdb_path='xtensa-{}-elf-gdb'.format(target),
  61. remote_target=None,
  62. extended_remote_mode=False,
  63. gdb_log_file=logfile_path,
  64. log_level=None,
  65. log_stream_handler=None,
  66. log_file_handler=None,
  67. scope=None)
  68. if working_dir:
  69. self.gdb.console_cmd_run('directory {}'.format(working_dir))
  70. self.gdb.exec_file_set(elffile_path)
  71. if gdbinit_path:
  72. try:
  73. self.gdb.console_cmd_run('source {}'.format(gdbinit_path))
  74. except debug_backend.defs.DebuggerTargetStateTimeoutError:
  75. # The internal timeout is not enough on RPI for more time consuming operations, e.g. "load".
  76. # So lets try to apply the commands one-by-one:
  77. with open(gdbinit_path, 'r') as f:
  78. for line in f:
  79. line = line.strip()
  80. if len(line) > 0 and not line.startswith('#'):
  81. self.gdb.console_cmd_run(line)
  82. # Note that some commands cannot be applied with console_cmd_run, e.g. "commands"
  83. def __enter__(self):
  84. return self
  85. def __exit__(self, type, value, traceback):
  86. try:
  87. self.gdb.gdb_exit()
  88. except pygdbmi.gdbcontroller.NoGdbProcessError as e:
  89. # the debug backend can fail on gdb exit when it tries to read the response after issuing the exit command.
  90. Utility.console_log('Ignoring exception: {}'.format(e), 'O')
  91. except debug_backend.defs.DebuggerTargetStateTimeoutError:
  92. Utility.console_log('Ignoring timeout exception for GDB exit', 'O')