DebugUtils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 pexpect
  18. class CustomProcess(object):
  19. def __init__(self, cmd, logfile, verbose=True):
  20. self.verbose = verbose
  21. self.f = open(logfile, 'w')
  22. if self.verbose:
  23. Utility.console_log('Starting {} > {}'.format(cmd, self.f.name))
  24. self.pexpect_proc = pexpect.spawn(cmd, timeout=60, logfile=self.f, encoding='utf-8')
  25. def __enter__(self):
  26. return self
  27. def close(self):
  28. self.pexpect_proc.terminate(force=True)
  29. def __exit__(self, type, value, traceback):
  30. self.close()
  31. self.f.close()
  32. class OCDProcess(CustomProcess):
  33. def __init__(self, logfile_path, extra_args='', verbose=True):
  34. # TODO Use configuration file implied by the test environment (board)
  35. cmd = 'openocd {} -f board/esp32-wrover-kit-3.3v.cfg'.format(extra_args)
  36. super(OCDProcess, self).__init__(cmd, logfile_path, verbose)
  37. patterns = ['Info : Listening on port 3333 for gdb connections']
  38. try:
  39. while True:
  40. i = self.pexpect_proc.expect_exact(patterns, timeout=30)
  41. # TIMEOUT or EOF exceptions will be thrown upon other errors
  42. if i == 0:
  43. if self.verbose:
  44. Utility.console_log('openocd is listening for gdb connections')
  45. break # success
  46. except Exception:
  47. if self.verbose:
  48. Utility.console_log('openocd initialization has failed', 'R')
  49. raise
  50. def close(self):
  51. try:
  52. self.pexpect_proc.sendcontrol('c')
  53. self.pexpect_proc.expect_exact('shutdown command invoked')
  54. except Exception:
  55. if self.verbose:
  56. Utility.console_log('openocd needs to be killed', 'O')
  57. super(OCDProcess, self).close()
  58. class GDBProcess(CustomProcess):
  59. def __init__(self, logfile_path, elffile_path, target, extra_args='', verbose=True):
  60. cmd = 'xtensa-{}-elf-gdb {} {}'.format(target, extra_args, elffile_path)
  61. super(GDBProcess, self).__init__(cmd, logfile_path, verbose)
  62. def close(self):
  63. try:
  64. self.pexpect_proc.sendline('q')
  65. self.pexpect_proc.expect_exact('Quit anyway? (y or n)')
  66. self.pexpect_proc.sendline('y')
  67. self.pexpect_proc.expect_exact('Ending remote debugging.')
  68. except Exception:
  69. if self.verbose:
  70. Utility.console_log('gdb needs to be killed', 'O')
  71. super(GDBProcess, self).close()
  72. class TelnetProcess(CustomProcess):
  73. def __init__(self, logfile_path, host='localhost', port=4444, verbose=True):
  74. cmd = 'telnet {} {}'.format(host, port)
  75. super(TelnetProcess, self).__init__(cmd, logfile_path, verbose)
  76. def close(self):
  77. try:
  78. self.pexpect_proc.sendline('exit')
  79. self.pexpect_proc.expect_exact('Connection closed by foreign host.')
  80. except Exception:
  81. if self.verbose:
  82. Utility.console_log('telnet needs to be killed', 'O')
  83. super(TelnetProcess, self).close()