From 42405604afefb14b662f107bbf323679e81e2c74 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Tue, 26 Oct 2021 10:41:13 +0800 Subject: [PATCH] idf.py gdb: autoload prefix_map_gdbinit when exists in project_description.json --- CMakeLists.txt | 2 ++ tools/cmake/project.cmake | 1 + tools/cmake/project_description.json.in | 2 +- tools/idf_py_actions/debug_ext.py | 37 ++++++++++++++----------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30126a843b..b0b9713ad6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,6 +151,8 @@ if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0) endif() if(CONFIG_APP_REPRODUCIBLE_BUILD) + idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${BUILD_DIR}/prefix_map_gdbinit") + list(APPEND compile_options "-fdebug-prefix-map=${IDF_PATH}=/IDF") list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT") list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD") diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index eed3def77a..433a3257bc 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -111,6 +111,7 @@ function(__project_info test_components) include(${sdkconfig_cmake}) idf_build_get_property(COMPONENT_KCONFIGS KCONFIGS) idf_build_get_property(COMPONENT_KCONFIGS_PROJBUILD KCONFIG_PROJBUILDS) + idf_build_get_property(debug_prefix_map_gdbinit DEBUG_PREFIX_MAP_GDBINIT) # Write project description JSON file idf_build_get_property(build_dir BUILD_DIR) diff --git a/tools/cmake/project_description.json.in b/tools/cmake/project_description.json.in index 6b9b84167c..d14966a181 100644 --- a/tools/cmake/project_description.json.in +++ b/tools/cmake/project_description.json.in @@ -18,5 +18,5 @@ }, "build_components" : ${build_components_json}, "build_component_paths" : ${build_component_paths_json}, - "debug_prefix_map_gdbinit": "${BUILD_DIR}/prefix_map_gdbinit" + "debug_prefix_map_gdbinit": "${debug_prefix_map_gdbinit}" } diff --git a/tools/idf_py_actions/debug_ext.py b/tools/idf_py_actions/debug_ext.py index 53b94e810a..f534b03d87 100644 --- a/tools/idf_py_actions/debug_ext.py +++ b/tools/idf_py_actions/debug_ext.py @@ -7,6 +7,7 @@ import sys import threading import time from threading import Thread +from typing import Any, Dict, List from idf_py_actions.errors import FatalError from idf_py_actions.tools import ensure_build_directory @@ -83,19 +84,6 @@ def action_extensions(base_actions, project_path): print('Failed to close/kill {}'.format(target)) processes[target] = None # to indicate this has ended - def _get_commandline_options(ctx): - """ Return all the command line options up to first action """ - # This approach ignores argument parsing done Click - result = [] - - for arg in sys.argv: - if arg in ctx.command.commands_with_aliases: - break - - result.append(arg) - - return result - def create_local_gdbinit(gdbinit, elf_file): with open(gdbinit, 'w') as f: if os.name == 'nt': @@ -188,6 +176,13 @@ def action_extensions(base_actions, project_path): processes['openocd_outfile_name'] = openocd_out_name print('OpenOCD started as a background task {}'.format(process.pid)) + def get_gdb_args(gdbinit, project_desc: Dict[str, Any]) -> List[str]: + args = ['-x={}'.format(gdbinit)] + debug_prefix_gdbinit = project_desc.get('debug_prefix_map_gdbinit') + if debug_prefix_gdbinit: + args.append('-ix={}'.format(debug_prefix_gdbinit)) + return args + def gdbui(action, ctx, args, gdbgui_port, gdbinit, require_openocd): """ Asynchronous GDB-UI target @@ -198,7 +193,17 @@ def action_extensions(base_actions, project_path): if gdbinit is None: gdbinit = os.path.join(local_dir, 'gdbinit') create_local_gdbinit(gdbinit, os.path.join(args.build_dir, project_desc['app_elf'])) - args = ['gdbgui', '-g', gdb, '--gdb-args="-x={}"'.format(gdbinit)] + + # this is a workaround for gdbgui + # gdbgui is using shlex.split for the --gdb-args option. When the input is: + # - '"-x=foo -x=bar"', would return ['foo bar'] + # - '-x=foo', would return ['-x', 'foo'] and mess up the former option '--gdb-args' + # so for one item, use extra double quotes. for more items, use no extra double quotes. + gdb_args = get_gdb_args(gdbinit, project_desc) + gdb_args = '"{}"'.format(' '.join(gdb_args)) if len(gdb_args) == 1 else ' '.join(gdb_args) + args = ['gdbgui', '-g', gdb, '--gdb-args', gdb_args] + print(args) + if gdbgui_port is not None: args += ['--port', gdbgui_port] gdbgui_out_name = os.path.join(local_dir, GDBGUI_OUT_FILE) @@ -278,10 +283,10 @@ def action_extensions(base_actions, project_path): if gdbinit is None: gdbinit = os.path.join(local_dir, 'gdbinit') create_local_gdbinit(gdbinit, elf_file) - args = [gdb, '-x={}'.format(gdbinit)] + args = [gdb, *get_gdb_args(gdbinit, project_desc)] if gdb_tui is not None: args += ['-tui'] - t = Thread(target=run_gdb, args=(args, )) + t = Thread(target=run_gdb, args=(args,)) t.start() while True: try: