Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions tools/ctrace/cmake/dependencies/ConfigureOpenCSD.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ if(NOT EXISTS "${CTRACE_OPENCSD_SOURCE_DIR}/CMakeLists.txt")
)
endif()

function(ctrace_apply_opencsd_patch)
function(ctrace_apply_opencsd_patch patch_name)
find_package(Git REQUIRED QUIET)
set(patch_file "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/OpenCSD.patch")
set(patch_file "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${patch_name}")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${patch_file}")

# Reconfiguration must preserve both the patch and unrelated local edits.
Expand All @@ -34,7 +34,7 @@ function(ctrace_apply_opencsd_patch)
ERROR_VARIABLE patch_error
)
if(NOT patch_result EQUAL 0)
message(FATAL_ERROR "Failed to apply the ctrace OpenCSD patch: ${patch_error}")
message(FATAL_ERROR "Failed to apply the ctrace OpenCSD patch ${patch_name}: ${patch_error}")
endif()
endfunction()

Expand All @@ -61,7 +61,9 @@ set(BUILD_TESTING OFF CACHE BOOL "Build the testing tree." FORCE)
set(ctrace_saved_install_prefix_default "${CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT}")
set(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT FALSE)

ctrace_apply_opencsd_patch()
# Apply independently so an existing ITM-patched checkout can receive later fixes.
ctrace_apply_opencsd_patch(OpenCSD.patch)
ctrace_apply_opencsd_patch(OpenCSD-thumb-fetch.patch)
add_subdirectory(
"${CTRACE_OPENCSD_SOURCE_DIR}"
"${CMAKE_BINARY_DIR}/external/OpenCSD"
Expand Down
130 changes: 130 additions & 0 deletions tools/ctrace/cmake/dependencies/OpenCSD-thumb-fetch.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
diff --git a/decoder/source/ocsd_code_follower.cpp b/decoder/source/ocsd_code_follower.cpp
--- a/decoder/source/ocsd_code_follower.cpp
+++ b/decoder/source/ocsd_code_follower.cpp
@@ -35,5 +35,6 @@

#include "common/ocsd_code_follower.h"
+#include "i_dec/trc_idec_arminst.h"

OcsdCodeFollower::OcsdCodeFollower()
{
@@ -141,2 +142,2 @@
- uint32_t opcode; // buffer for opcode
+ uint32_t opcode = 0; // buffer for opcode, including a partial read

@@ -147,7 +148,9 @@
if(err != OCSD_OK)
return err;

- if(bytesReq == 4) // check that we got all memory requested.
+ // A complete Thumb-16 instruction needs only the first halfword.
+ if((bytesReq == 4) || ((bytesReq >= 2) && (m_instr_info.isa == ocsd_isa_thumb2) &&
+ !is_wide_thumb((uint16_t)opcode)))
{
m_instr_info.opcode = opcode;
err = m_pIDecode->first()->DecodeInstruction(&m_instr_info);
diff --git a/decoder/source/etmv4/trc_pkt_decode_etmv4i.cpp b/decoder/source/etmv4/trc_pkt_decode_etmv4i.cpp
--- a/decoder/source/etmv4/trc_pkt_decode_etmv4i.cpp
+++ b/decoder/source/etmv4/trc_pkt_decode_etmv4i.cpp
@@ -37,6 +37,7 @@
#include "opencsd/etmv4/trc_pkt_decode_etmv4i.h"

#include "common/trc_gen_elem.h"
+#include "i_dec/trc_idec_arminst.h"


#define DCD_NAME "DCD_ETMV4"
@@ -1772,13 +1773,14 @@
// walk iCount instructions
for (int i = 0; i < iCount; i++)
{
- uint32_t opcode;
+ uint32_t opcode = 0;
uint32_t bytesReq = 4;

err = accessMemory(m_instr_info.instr_addr, getCurrMemSpace(), &bytesReq, (uint8_t *)&opcode);
if (err != OCSD_OK) break;

- if (bytesReq == 4) // got data back
+ if ((bytesReq == 4) || ((bytesReq >= 2) && (m_instr_info.isa == ocsd_isa_thumb2) &&
+ !is_wide_thumb((uint16_t)opcode)))
{
m_instr_info.opcode = opcode;
err = instrDecode(&m_instr_info);
@@ -1845,7 +1847,7 @@
ocsd_err_t err = OCSD_OK;
TrcStackElemAddr *pElem = dynamic_cast<TrcStackElemAddr *>(m_P0_stack.back()); // get the address element
etmv4_addr_val_t srcAddr = pElem->getAddr();
- uint32_t opcode, bytesReq = 4;
+ uint32_t opcode = 0, bytesReq = 4;
ocsd_vaddr_t currAddr = m_instr_info.instr_addr; // get the latest decoded address.
instr_range_t out_range;
bool bSplitRangeOnN = getComponentOpMode() & ETE_OPFLG_PKTDEC_SRCADDR_N_ATOMS ? true : false;
@@ -1858,7 +1860,8 @@
return err;
}

- if (bytesReq != 4)
+ if ((bytesReq != 4) && ((bytesReq < 2) || (m_instr_info.isa != ocsd_isa_thumb2) ||
+ is_wide_thumb((uint16_t)opcode)))
{
// can't access - no bytes returned - output nacc.
err = m_out_elem.addElemType(pElem->getRootIndex(), OCSD_GEN_TRC_ELEM_ADDR_NACC);
@@ -1917,6 +1920,7 @@
while ((instr.instr_addr < out_range.en_addr) && !bMemAccErr)
{
bytesReq = 4;
+ opcode = 0;
err = accessMemory(instr.instr_addr, getCurrMemSpace(), &bytesReq, (uint8_t *)&opcode);
if (err != OCSD_OK)
{
@@ -1924,7 +1928,8 @@
return err;
}

- if (bytesReq == 4)
+ if ((bytesReq == 4) || ((bytesReq >= 2) && (instr.isa == ocsd_isa_thumb2) &&
+ !is_wide_thumb((uint16_t)opcode)))
{
instr.opcode = opcode;
err = instrDecode(&instr);
@@ -2023,10 +2028,12 @@
{
// start off by reading next opcode;
bytesReq = 4;
+ opcode = 0;
err = accessMemory(m_instr_info.instr_addr, getCurrMemSpace(),&bytesReq,(uint8_t *)&opcode);
if(err != OCSD_OK) break;

- if(bytesReq == 4) // got data back
+ if((bytesReq == 4) || ((bytesReq >= 2) && (m_instr_info.isa == ocsd_isa_thumb2) &&
+ !is_wide_thumb((uint16_t)opcode)))
{
m_instr_info.opcode = opcode;
err = instrDecode(&m_instr_info);
diff --git a/decoder/source/ptm/trc_pkt_decode_ptm.cpp b/decoder/source/ptm/trc_pkt_decode_ptm.cpp
--- a/decoder/source/ptm/trc_pkt_decode_ptm.cpp
+++ b/decoder/source/ptm/trc_pkt_decode_ptm.cpp
@@ -34,6 +34,7 @@

#include <sstream>
#include "opencsd/ptm/trc_pkt_decode_ptm.h"
+#include "i_dec/trc_idec_arminst.h"

#define DCD_NAME "DCD_PTM"

@@ -633,11 +634,13 @@
{
// start off by reading next opcode;
bytesReq = 4;
+ opcode = 0;
curr_op_address = m_instr_info.instr_addr; // save the start address for the current opcode
err = accessMemory(m_instr_info.instr_addr,mem_space,&bytesReq,(uint8_t *)&opcode);
if(err != OCSD_OK) break;

- if(bytesReq == 4) // got data back
+ if((bytesReq == 4) || ((bytesReq >= 2) && (m_instr_info.isa == ocsd_isa_thumb2) &&
+ !is_wide_thumb((uint16_t)opcode)))
{
m_instr_info.opcode = opcode;
err = instrDecode(&m_instr_info);
12 changes: 9 additions & 3 deletions tools/ctrace/docs/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ ctrace consumes the repository-wide cxxopts and yaml-cpp targets configured by d

`tools/ctrace/cmake/dependencies/ConfigureOpenCSD.cmake` builds OpenCSD's
upstream minimal static target, enables large trace-source IDs, and adjusts its
MSVC build options. It applies `tools/ctrace/cmake/dependencies/OpenCSD.patch`,
which replaces an invalid empty-vector subscript in the ITM sync search with
`std::vector::data()`. See the [OpenCSD issue notes](opencsd-issues.md).
MSVC build options. It applies two local patches independently:

- `tools/ctrace/cmake/dependencies/OpenCSD.patch` replaces an invalid empty-vector
subscript in the ITM sync search with `std::vector::data()`.
- `tools/ctrace/cmake/dependencies/OpenCSD-thumb-fetch.patch` permits complete
Thumb-16 instructions after short memory reads in the ETMv3, ETMv4 and PTM
fetch paths, while retaining inaccessible-code handling for partial instructions.

See the [OpenCSD issue notes](opencsd-issues.md).

The OpenCSD minimal static target still contains upstream decoders other than
ITM because OpenCSD 1.8.3 does not expose a supported ITM-only CMake target.
Expand Down
Loading
Loading