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 get_build_dir(args, die_if_none=True): 45 # Get the build directory for the given argument list and environment. 46 47 guess = config.get('build', 'guess-dir', fallback='never') 48 guess = guess == 'runners' 49 dir = find_build_dir(None, guess) 50 51 if dir and is_zephyr_build(dir): 52 return dir 53 elif die_if_none: 54 msg = 'could not find build directory and ' 55 if dir: 56 msg = msg + 'neither {} nor {} are zephyr build directories.' 57 else: 58 msg = msg + ('{} is not a build directory and the default build ' 59 'directory cannot be determined. ') 60 log.die(msg.format(os.getcwd(), dir)) 61 else: 62 return None 63 64 65def color_print(message, color, newline='\n'): # type: (str, str, Optional[str]) -> None 66 """ Print a message to stderr with colored highlighting """ 67 sys.stderr.write('%s%s%s%s' % (color, message, ANSI_NORMAL, newline)) 68 69 70def normal_print(message): # type: (str) -> None 71 sys.stderr.write(ANSI_NORMAL + message) 72 73 74def yellow_print(message, newline='\n'): # type: (str, Optional[str]) -> None 75 color_print(message, ANSI_YELLOW, newline) 76 77 78def red_print(message, newline='\n'): # type: (str, Optional[str]) -> None 79 color_print(message, ANSI_RED, newline) 80 81 82def lookup_pc_address(pc_addr, toolchain_prefix, elf_file): # type: (str, str, str) -> Optional[str] 83 # cmd = ['%saddr2line' % toolchain_prefix, '-pfiaC', '-e', elf_file, pc_addr] 84 85 # build dir differs when sysbuild is used 86 build_dir = get_build_dir(None) 87 domain = load_domains(build_dir).get_default_domain() 88 if domain.name == 'app': 89 cache = CMakeCache.from_build_dir(build_dir) 90 else: 91 cache = CMakeCache.from_build_dir(Path(build_dir) / domain.name) 92 93 # Zephyr: set toolchain from environment path 94 toolchain_path = cache['CMAKE_ADDR2LINE'] 95 cmd = [toolchain_path, '-pfiaC', '-e', elf_file, pc_addr] 96 97 try: 98 translation = subprocess.check_output(cmd, cwd='.') 99 if b'?? ??:0' not in translation: 100 return translation.decode() 101 except OSError as e: 102 red_print('%s: %s' % (' '.join(cmd), e)) 103 return None 104