[llvm] [llvm-profgen] Add support for ETM trace decoding (PR #191584)
Peter Smith via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 22 08:16:34 PDT 2026
================
@@ -0,0 +1,220 @@
+//===-- ETMTraceDecoder.cpp - ETM Trace Decoder -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/ETMTraceDecoder.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/Error.h"
+
+#ifdef HAVE_OPENCSD
+#include "opencsd/c_api/opencsd_c_api.h"
+
+namespace llvm {
+
+namespace {
+class ETMDecoderImpl : public ETMDecoder {
+ dcd_tree_handle_t DcdTree = 0;
+ std::string BinaryPath;
+ Triple TargetTriple;
+
+ // Trace processing and Callback handling.
+ static ocsd_datapath_resp_t
+ processTrace(const void *PContext, const ocsd_trc_index_t /*IndexSOP*/,
+ const uint8_t /*TrcChanID*/,
+ const ocsd_generic_trace_elem *Element) {
+ auto *Decoder = static_cast<ETMDecoderImpl *>(const_cast<void *>(PContext));
+ if (!Decoder || !Element)
+ return OCSD_RESP_FATAL_SYS_ERR;
+
+ // Process instruction ranges reconstructed by the decoder.
+ if (Element->elem_type == OCSD_GEN_TRC_ELEM_INSTR_RANGE) {
+ uint64_t Start = Element->st_addr;
+ uint64_t End = Element->en_addr;
+ if (End > Start) {
+ // OpenCSD ranges are exclusive at the end [Start, End).
+ // llvm-profgen range counters expect inclusive bounds [Start, End].
+ // Adjust the exclusive end address provided by OpenCSD to include
+ // the last executed instruction within the reported range.
+ Decoder->CurrentCallback->processInstructionRange(Start, End - 1);
+ }
+ }
+ return OCSD_RESP_CONT;
+ }
+
+ Callback *CurrentCallback = nullptr;
+
+ // Iterate through the ELF program headers to collect all executable LOAD
+ // segments. These are registered as a single transaction to the OpenCSD
+ // memory manager to prevent overlap/collision errors between different
+ // memory regions.
+ Error mapELFSegments(dcd_tree_handle_t DcdTree, object::Binary &SourceBin) {
+ SmallVector<ocsd_file_mem_region_t, 4> Regions;
+ auto ProcessHeaders = [&](const auto &ElfFile) {
+ auto ProgramHeaders = ElfFile.program_headers();
+ if (!ProgramHeaders)
+ return;
+
+ for (const auto &Phdr : *ProgramHeaders) {
+ if (Phdr.p_type == llvm::ELF::PT_LOAD &&
+ (Phdr.p_flags & llvm::ELF::PF_X)) {
+ ocsd_file_mem_region_t Region{};
+ Region.start_address = (uint64_t)Phdr.p_vaddr;
+ Region.file_offset = (uint64_t)Phdr.p_offset;
+ Region.region_size = (uint64_t)Phdr.p_filesz;
+ Regions.push_back(Region);
+ }
+ }
+ };
+
+ if (auto *O = dyn_cast<object::ELF32LEObjectFile>(&SourceBin))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF64LEObjectFile>(&SourceBin))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF32BEObjectFile>(&SourceBin))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF64BEObjectFile>(&SourceBin))
+ ProcessHeaders(O->getELFFile());
+
+ if (!Regions.empty()) {
+ if (ocsd_dt_add_binfile_region_mem_acc(
+ DcdTree, Regions.data(), (uint32_t)Regions.size(),
+ OCSD_MEM_SPACE_ANY, BinaryPath.c_str()) != 0) {
+ return createStringError(
+ inconvertibleErrorCode(),
+ "OpenCSD: Failed to map ELF executable segments.");
+ }
+ }
+ return Error::success();
+ }
+
+public:
+ ETMDecoderImpl(StringRef BinaryPath) : BinaryPath(BinaryPath.str()) {}
+ ~ETMDecoderImpl() override {
+ if (DcdTree)
+ // Deallocate the decoder tree resources.
+ ocsd_destroy_dcd_tree(DcdTree);
+ }
+
+ /// Initialize the decoder by auto-detecting the target architecture and
+ /// configuring the OpenCSD decoder.
+ Error initialize() {
+ auto BinaryOrErr = object::createBinary(BinaryPath);
+ if (!BinaryOrErr)
+ return BinaryOrErr.takeError();
+
+ object::Binary &SourceBin = *BinaryOrErr.get().getBinary();
+ auto *ElfBase = dyn_cast<object::ELFObjectFileBase>(&SourceBin);
+ if (!ElfBase)
+ return createStringError(inconvertibleErrorCode(),
+ "OpenCSD: Unsupported binary format. Only ELF "
+ "is currently supported.");
+
+ TargetTriple = ElfBase->makeTriple();
+
+ DcdTree = ocsd_create_dcd_tree(OCSD_TRC_SRC_SINGLE, 0);
+ if (!DcdTree)
+ return createStringError(inconvertibleErrorCode(),
+ "Failed to create OpenCSD decoder tree.");
+
+ // Configure and initialize the instruction-level decoder.
+ ocsd_etmv4_cfg Config{};
+ // Initialize the decoder for microcontroller-class targets.
+ if (TargetTriple.isArmMClass() || TargetTriple.isThumb()) {
+ Config.arch_ver = ARCH_V8;
+ Config.core_prof = profile_CortexM;
+ } else {
+ return createStringError(
+ inconvertibleErrorCode(),
+ "OpenCSD: Unsupported processor architecture. Only "
+ "microcontroller-class (Cortex-M) is currently supported.");
+ }
+ uint8_t CSID = 0x10;
----------------
smithp35 wrote:
Thanks for updating the v6-m configuration, I've no more comments.
I've been trying to find a source for the trace id. I've seen two values mentioned as being common, 0x1 and 0x10.
The best source I've found for 0x10 is the Linux kernel https://github.com/torvalds/linux/blob/master/include/linux/coresight-pmu.h#L22
> /*
> * The legacy Trace ID system based on fixed calculation from the cpu
> * number. This has been replaced by drivers using a dynamic allocation
> * system - but need to retain the legacy algorithm for backward comparibility
> * in certain situations:-
> * a) new perf running on older systems that generate the legacy mapping
> * b) older tools that may not update at the same time as the kernel.
> */
> #define CORESIGHT_LEGACY_CPU_TRACE_ID(cpu) (0x10 + (cpu * 2))
As I understand it the CSID corresponds to the Trace ID register (TRCTRACEIDR) in ETMv4 https://developer.arm.com/documentation/100232/0002/ETM-M33-Register-Descriptions-/ETM-M33-registers/Trace-ID-Register or ETMTRACEIDR in ETMv3
As the kernel comment says, it is now set by the Trace Probe. As such it can be any non-reserved value. The ETM v4.5 spec says:
```
TRACEID, bits[6:0]
Trace ID field. Sets the trace ID value for instruction trace.
When FEAT_ETMv4p5 is not implemented, bit[0] must be 0 if data trace is enabled. If data trace is
enabled, a trace unit sets the trace ID for data trace, to TRACEID+1. When FEAT_ETMv4p5 is
implemented, this restriction is removed.
The implemented width of the field is IMPLEMENTATION DEFINED and is defined by the value of
TRCIDR5.TRACEIDSIZE. Unimplemented bits are RES0.
If an implementation supports AMBA® ATB, then:
• The width of the field is 7 bits.
• Writing a reserved trace ID value does not affect behavior of the trace unit but it might cause
UNPREDICTABLE behavior of the trace capture infrastructure.
See the AMBA® 5 ATB Protocol Specification for information about which ATID bus values
are Reserved.
```
I've found a couple of references for 0x1
* https://github.com/czietz/etm-trace-rp2350/blob/master/trace.gdb#L208
* Lauterbach manual https://www2.lauterbach.com/pdf/trace_arm_etm.pdf see `ETM.TraceID`
May imply that CSID should be a parameter (defaulting to 0x10) or the person doing the tracing will need to set their trace id to 0x10. Most IDE manuals don't look like they expose this to the user directly. As the IDE is going to configure and analyse the trace itself it can set the trace id behind the scenes. It looks like target specific .ini files are used to write to system registers so it maybe that a user will need to add a write to TRCTRACEIDR or ETMTRACEIDR depending on their system.
https://github.com/llvm/llvm-project/pull/191584
More information about the llvm-commits
mailing list