output_helpers.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright 2015-2021 Espressif Systems (Shanghai) CO 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. import subprocess
  15. import sys
  16. from typing import BinaryIO, Callable, Optional, Union # noqa: F401
  17. # ANSI terminal codes (if changed, regular expressions in LineMatcher need to be updated)
  18. ANSI_RED = '\033[1;31m'
  19. ANSI_YELLOW = '\033[0;33m'
  20. ANSI_NORMAL = '\033[0m'
  21. def color_print(message, color, newline='\n'): # type: (str, str, Optional[str]) -> None
  22. """ Print a message to stderr with colored highlighting """
  23. sys.stderr.write('%s%s%s%s' % (color, message, ANSI_NORMAL, newline))
  24. def normal_print(message): # type: (str) -> None
  25. sys.stderr.write(ANSI_NORMAL + message)
  26. def yellow_print(message, newline='\n'): # type: (str, Optional[str]) -> None
  27. color_print(message, ANSI_YELLOW, newline)
  28. def red_print(message, newline='\n'): # type: (str, Optional[str]) -> None
  29. color_print(message, ANSI_RED, newline)
  30. def lookup_pc_address(pc_addr, toolchain_prefix, elf_file): # type: (str, str, str) -> Optional[str]
  31. cmd = ['%saddr2line' % toolchain_prefix, '-pfiaC', '-e', elf_file, pc_addr]
  32. try:
  33. translation = subprocess.check_output(cmd, cwd='.')
  34. if b'?? ??:0' not in translation:
  35. return translation.decode()
  36. except OSError as e:
  37. red_print('%s: %s' % (' '.join(cmd), e))
  38. return None