Skip to content

Commit

Permalink
Upload: NUVOTON: Change GDB load command to flash post-build processe…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
ccli8 authored Oct 15, 2024
1 parent b2d11fc commit 7d99fd1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
38 changes: 38 additions & 0 deletions tools/cmake/UploadMethodManager.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# Change GDB load command to flash post-build processed image in debug launch.
# 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.
#
# NOTE: Place at the very start so that it can override by the below loaded
# upload method if need be.
function(mbed_adjust_upload_debug_commands target)
# MBED_UPLOAD_LAUNCH_COMMANDS defined?
if(NOT DEFINED MBED_UPLOAD_LAUNCH_COMMANDS)
return()
endif()

# GDB load command in MBED_UPLOAD_LAUNCH_COMMANDS?
list(FIND MBED_UPLOAD_LAUNCH_COMMANDS "load" LOAD_INDEX)
if(${LOAD_INDEX} LESS "0")
return()
endif()

# <app>.hex for debug launch load
set(HEX_FILE ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${target}>.hex)

# "load" -> "load <app>.hex"
#
# GDB load command doesn't support binary format. Ignore OUTPUT_EXT
# and fix to Intel Hex format.
#
# NOTE: The <app>.hex file name needs to be quoted (\\\") to pass along
# to gdb correctly.
list(TRANSFORM MBED_UPLOAD_LAUNCH_COMMANDS APPEND " \\\"${HEX_FILE}\\\"" AT ${LOAD_INDEX})

# Update MBED_UPLOAD_LAUNCH_COMMANDS in cache
set(MBED_UPLOAD_LAUNCH_COMMANDS ${MBED_UPLOAD_LAUNCH_COMMANDS} CACHE INTERNAL "" FORCE)
endfunction()

# ----------------------------------------------
# Common upload method options

Expand Down
30 changes: 15 additions & 15 deletions tools/cmake/mbed_ide_debug_cfg_generator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,6 @@ elseif(MBED_UPLOAD_SUPPORTS_DEBUG)

function(mbed_generate_ide_debug_configuration CMAKE_TARGET)

# add debug target
if(MBED_UPLOAD_SUPPORTS_DEBUG AND MBED_GDB_FOUND)
add_custom_target(debug-${target}
COMMENT "Starting GDB to debug ${target}..."
COMMAND ${MBED_GDB}
--command=${CMAKE_BINARY_DIR}/mbed-cmake.gdbinit
$<TARGET_FILE:${target}>
USES_TERMINAL)
endif()

endfunction(mbed_generate_ide_debug_configuration)

function(mbed_finalize_ide_debug_configurations)

# create init file for GDB client
if(MBED_UPLOAD_WANTS_EXTENDED_REMOTE)
set(UPLOAD_GDB_REMOTE_KEYWORD "extended-remote")
Expand All @@ -264,13 +250,27 @@ elseif(MBED_UPLOAD_SUPPORTS_DEBUG)

list(JOIN MBED_UPLOAD_LAUNCH_COMMANDS "\n" MBED_UPLOAD_LAUNCH_COMMANDS_FOR_GDBINIT)

file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/mbed-cmake.gdbinit CONTENT
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${CMAKE_TARGET}>.gdbinit CONTENT
"# connect to GDB server
target ${UPLOAD_GDB_REMOTE_KEYWORD} 127.0.0.1:${MBED_GDB_PORT}
${MBED_UPLOAD_LAUNCH_COMMANDS_FOR_GDBINIT}
c"
)

# add debug target
if(MBED_UPLOAD_SUPPORTS_DEBUG AND MBED_GDB_FOUND)
add_custom_target(debug-${target}
COMMENT "Starting GDB to debug ${target}..."
COMMAND ${MBED_GDB}
--command=${CMAKE_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:${CMAKE_TARGET}>.gdbinit
$<TARGET_FILE:${target}>
USES_TERMINAL)
endif()

endfunction(mbed_generate_ide_debug_configuration)

function(mbed_finalize_ide_debug_configurations)

# Create target to start the GDB server
add_custom_target(gdbserver
COMMENT "Starting ${UPLOAD_METHOD} GDB server"
Expand Down
23 changes: 17 additions & 6 deletions tools/cmake/mbed_target_functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ function(mbed_generate_bin_hex target)

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

# Convert to BIN format just on demand because the resultant output
# can have large holes in addresses which BIN format cannot handle and
# can generate very large file.
#
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.bin
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.bin"
)
endif()
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex"
)
endif()
# Convert to Intel HEX format unconditionally which most flash programming
# tools can support. For example, GDB load command supports Intel HEX format
# but no BIN format.
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${artifact_name}.hex"
)

add_custom_command(
TARGET
Expand Down Expand Up @@ -174,6 +179,12 @@ function(mbed_set_post_build target)
mbed_generate_map_file(${target})
endif()

# Give chance to adjust MBED_UPLOAD_LAUNCH_COMMANDS or MBED_UPLOAD_RESTART_COMMANDS
# for debug launch
if(COMMAND mbed_adjust_upload_debug_commands)
mbed_adjust_upload_debug_commands(${target})
endif()

mbed_generate_upload_target(${target})
mbed_generate_ide_debug_configuration(${target})
endfunction()
Expand Down
4 changes: 4 additions & 0 deletions tools/cmake/upload_methods/UploadMethodPYOCD.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ set(UPLOAD_LAUNCH_COMMANDS
"monitor reset halt"
"load"
"tbreak main"

# It appears the device under debug must be halted after UPLOAD_LAUNCH_COMMANDS,
# or debugger will become abnormal.
"monitor reset halt"
)
set(UPLOAD_RESTART_COMMANDS
"monitor reset halt"
Expand Down

0 comments on commit 7d99fd1

Please sign in to comment.