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 15import os 16import subprocess 17import sys 18from typing import BinaryIO, Callable, Optional, Union # noqa: F401 19 20from west.commands import WestCommand 21from west.configuration import config 22from west import log 23 24# This relies on this file being in hal_espressif/tools/idf_monitor_base/output_helpers.py 25# If you move this file, you'll break it, so be careful. 26from pathlib import Path 27THIS_ZEPHYR = Path(__file__).parents[5] / 'zephyr' 28ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR)) 29 30sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "west_commands")) 31 32from build_helpers import load_domains 33from build_helpers import is_zephyr_build, find_build_dir # noqa: E402 34from runners.core import BuildConfiguration # noqa: E402 35from zcmake import CMakeCache 36 37 38# ANSI terminal codes (if changed, regular expressions in LineMatcher need to be updated) 39ANSI_RED = '\033[1;31m' 40ANSI_YELLOW = '\033[0;33m' 41ANSI_NORMAL = '\033[0m' 42 43 44def color_print(message, color, newline='\n'): # type: (str, str, Optional[str]) -> None 45 """ Print a message to stderr with colored highlighting """ 46 sys.stderr.write('%s%s%s%s' % (color, message, ANSI_NORMAL, newline)) 47 48 49def normal_print(message): # type: (str) -> None 50 sys.stderr.write(ANSI_NORMAL + message) 51 52 53def yellow_print(message, newline='\n'): # type: (str, Optional[str]) -> None 54 color_print(message, ANSI_YELLOW, newline) 55 56 57def red_print(message, newline='\n'): # type: (str, Optional[str]) -> None 58 color_print(message, ANSI_RED, newline) 59 60 61def lookup_pc_address(pc_addr, toolchain_prefix, elf_file): # type: (str, str, str) -> Optional[str] 62 # cmd = ['%saddr2line' % toolchain_prefix, '-pfiaC', '-e', elf_file, pc_addr] 63 64 build_dir = find_build_dir(None, True) 65 domain = load_domains(build_dir).get_default_domain() 66 67 # build dir differs when sysbuild is used 68 if domain.build_dir: 69 build_dir = domain.build_dir 70 71 cache = CMakeCache.from_build_dir(build_dir) 72 73 # Zephyr: set toolchain from environment path 74 toolchain_path = cache['CMAKE_ADDR2LINE'] 75 cmd = [toolchain_path, '-pfiaC', '-e', elf_file, pc_addr] 76 77 try: 78 translation = subprocess.check_output(cmd, cwd='.') 79 if b'?? ??:0' not in translation: 80 return translation.decode() 81 except OSError as e: 82 red_print('%s: %s' % (' '.join(cmd), e)) 83 return None 84