Skip to content

Commit 7d99fd1

Browse files
authored
Upload: NUVOTON: Change GDB load command to flash post-build processed image (#341)
* Upload: PyOCD: Halt device after UPLOAD_LAUNCH_COMMANDS For pyocd debug, it appears the device under debug must be halted after UPLOAD_LAUNCH_COMMANDS, or debugger will become abnormal. * Convert output to Intel HEX format unconditionally in post-build This converts output to Intel HEX format unconditionally but to BIN format just on demand for the following reasons: 1. Most flash programming tools support Intel HEX format, e.g. GDB load command. 2. Output can have large holes in addresses which BIN format cannot handle and can generate very large file. * Upload: Support adjustment of debug commands for debug launch This gives chance to adjust GDB commands MBED_UPLOAD_LAUNCH_COMMANDS or MBED_UPLOAD_RESTART_COMMANDS for debug launch by implementing mbed_adjust_upload_debug_commands cmake function. * Upload: Change GDB load command to flash post-build processed image With this adjustment, GDB load command changes to "load <app>.hex" from just "load": 1. "Load" will load <app>.elf and is inappropriate for targets like bootloader or TF-M enabled which need to post-build process images. 2. "load <app>.bin" is not considered because GDB load command doesn't support binary format. * Upload: Generate GDB command line gdbinit for multiple application This enables generating GDB command line gdbinit file for single project multiple application scenario. The gdbinit file name would be <app>.gdbinit for distinct among multiple applications.
1 parent b2d11fc commit 7d99fd1

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

tools/cmake/UploadMethodManager.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
# Change GDB load command to flash post-build processed image in debug launch.
5+
# With this adjustment, GDB load command changes to "load <app>.hex" from
6+
# just "load":
7+
# 1. "Load" will load <app>.elf and is inappropriate for targets like
8+
# bootloader or TF-M enabled which need to post-build process images.
9+
# 2. "load <app>.bin" is not considered because GDB load command
10+
# doesn't support binary format.
11+
#
12+
# NOTE: Place at the very start so that it can override by the below loaded
13+
# upload method if need be.
14+
function(mbed_adjust_upload_debug_commands target)
15+
# MBED_UPLOAD_LAUNCH_COMMANDS defined?
16+
if(NOT DEFINED MBED_UPLOAD_LAUNCH_COMMANDS)
17+
return()
18+
endif()
19+
20+
# GDB load command in MBED_UPLOAD_LAUNCH_COMMANDS?
21+
list(FIND MBED_UPLOAD_LAUNCH_COMMANDS "load" LOAD_INDEX)
22+
if(${LOAD_INDEX} LESS "0")
23+
return()
24+
endif()
25+
26+
# <app>.hex for debug launch load
27+
set(HEX_FILE ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${target}>.hex)
28+
29+
# "load" -> "load <app>.hex"
30+
#
31+
# GDB load command doesn't support binary format. Ignore OUTPUT_EXT
32+
# and fix to Intel Hex format.
33+
#
34+
# NOTE: The <app>.hex file name needs to be quoted (\\\") to pass along
35+
# to gdb correctly.
36+
list(TRANSFORM MBED_UPLOAD_LAUNCH_COMMANDS APPEND " \\\"${HEX_FILE}\\\"" AT ${LOAD_INDEX})
37+
38+
# Update MBED_UPLOAD_LAUNCH_COMMANDS in cache
39+
set(MBED_UPLOAD_LAUNCH_COMMANDS ${MBED_UPLOAD_LAUNCH_COMMANDS} CACHE INTERNAL "" FORCE)
40+
endfunction()
41+
442
# ----------------------------------------------
543
# Common upload method options
644

tools/cmake/mbed_ide_debug_cfg_generator.cmake

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,6 @@ elseif(MBED_UPLOAD_SUPPORTS_DEBUG)
241241

242242
function(mbed_generate_ide_debug_configuration CMAKE_TARGET)
243243

244-
# add debug target
245-
if(MBED_UPLOAD_SUPPORTS_DEBUG AND MBED_GDB_FOUND)
246-
add_custom_target(debug-${target}
247-
COMMENT "Starting GDB to debug ${target}..."
248-
COMMAND ${MBED_GDB}
249-
--command=${CMAKE_BINARY_DIR}/mbed-cmake.gdbinit
250-
$<TARGET_FILE:${target}>
251-
USES_TERMINAL)
252-
endif()
253-
254-
endfunction(mbed_generate_ide_debug_configuration)
255-
256-
function(mbed_finalize_ide_debug_configurations)
257-
258244
# create init file for GDB client
259245
if(MBED_UPLOAD_WANTS_EXTENDED_REMOTE)
260246
set(UPLOAD_GDB_REMOTE_KEYWORD "extended-remote")
@@ -264,13 +250,27 @@ elseif(MBED_UPLOAD_SUPPORTS_DEBUG)
264250

265251
list(JOIN MBED_UPLOAD_LAUNCH_COMMANDS "\n" MBED_UPLOAD_LAUNCH_COMMANDS_FOR_GDBINIT)
266252

267-
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/mbed-cmake.gdbinit CONTENT
253+
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${CMAKE_TARGET}>.gdbinit CONTENT
268254
"# connect to GDB server
269255
target ${UPLOAD_GDB_REMOTE_KEYWORD} 127.0.0.1:${MBED_GDB_PORT}
270256
${MBED_UPLOAD_LAUNCH_COMMANDS_FOR_GDBINIT}
271257
c"
272258
)
273259

260+
# add debug target
261+
if(MBED_UPLOAD_SUPPORTS_DEBUG AND MBED_GDB_FOUND)
262+
add_custom_target(debug-${target}
263+
COMMENT "Starting GDB to debug ${target}..."
264+
COMMAND ${MBED_GDB}
265+
--command=${CMAKE_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${CMAKE_TARGET}>.gdbinit
266+
$<TARGET_FILE:${target}>
267+
USES_TERMINAL)
268+
endif()
269+
270+
endfunction(mbed_generate_ide_debug_configuration)
271+
272+
function(mbed_finalize_ide_debug_configurations)
273+
274274
# Create target to start the GDB server
275275
add_custom_target(gdbserver
276276
COMMENT "Starting ${UPLOAD_METHOD} GDB server"

tools/cmake/mbed_target_functions.cmake

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ function(mbed_generate_bin_hex target)
1313

1414
set(artifact_name $<TARGET_FILE_BASE_NAME:${target}>)
1515

16+
# Convert to BIN format just on demand because the resultant output
17+
# can have large holes in addresses which BIN format cannot handle and
18+
# can generate very large file.
19+
#
1620
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
1721
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
1822
list(APPEND CMAKE_POST_BUILD_COMMAND
1923
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.bin
2024
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.bin"
2125
)
2226
endif()
23-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
24-
list(APPEND CMAKE_POST_BUILD_COMMAND
25-
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex
26-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex"
27-
)
28-
endif()
27+
# Convert to Intel HEX format unconditionally which most flash programming
28+
# tools can support. For example, GDB load command supports Intel HEX format
29+
# but no BIN format.
30+
list(APPEND CMAKE_POST_BUILD_COMMAND
31+
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex
32+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex"
33+
)
2934

3035
add_custom_command(
3136
TARGET
@@ -174,6 +179,12 @@ function(mbed_set_post_build target)
174179
mbed_generate_map_file(${target})
175180
endif()
176181

182+
# Give chance to adjust MBED_UPLOAD_LAUNCH_COMMANDS or MBED_UPLOAD_RESTART_COMMANDS
183+
# for debug launch
184+
if(COMMAND mbed_adjust_upload_debug_commands)
185+
mbed_adjust_upload_debug_commands(${target})
186+
endif()
187+
177188
mbed_generate_upload_target(${target})
178189
mbed_generate_ide_debug_configuration(${target})
179190
endfunction()

tools/cmake/upload_methods/UploadMethodPYOCD.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ set(UPLOAD_LAUNCH_COMMANDS
5353
"monitor reset halt"
5454
"load"
5555
"tbreak main"
56+
57+
# It appears the device under debug must be halted after UPLOAD_LAUNCH_COMMANDS,
58+
# or debugger will become abnormal.
59+
"monitor reset halt"
5660
)
5761
set(UPLOAD_RESTART_COMMANDS
5862
"monitor reset halt"

0 commit comments

Comments
 (0)