[llvm] [llvm-profgen] Add support for ETM trace decoding (PR #191584)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 18:11:39 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
Author: gulfemsavrun
<details>
<summary>Changes</summary>
This patch introduces ETMReader to llvm-profgen,
enabling the reconstruction of execution profiles from ETM formatted trace data.
- Integrate OpenCSD (CoreSight Decoding Library) as an optional dependency via the LLVM_ENABLE_OPENCSD flag.
- Implement ETMReader, which uses hardware configuration and ELF memory mapping to decode instruction traces.
- Add the --etm command-line option to specify raw trace inputs.
The implementation targets microcontroller-class (Cortex-M) devices based on the binary's target triple.
---
Patch is 29.27 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/191584.diff
11 Files Affected:
- (modified) llvm/docs/CommandGuide/llvm-profgen.rst (+5)
- (modified) llvm/docs/ReleaseNotes.md (+2)
- (added) llvm/test/tools/llvm-profgen/Inputs/etm-opencsd.yaml (+48)
- (added) llvm/test/tools/llvm-profgen/etm-arch.test (+81)
- (added) llvm/test/tools/llvm-profgen/etm-opencsd.test (+15)
- (modified) llvm/test/tools/llvm-profgen/lit.local.cfg (+10)
- (modified) llvm/tools/llvm-profgen/CMakeLists.txt (+20)
- (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+209)
- (modified) llvm/tools/llvm-profgen/PerfReader.h (+30-1)
- (modified) llvm/tools/llvm-profgen/ProfiledBinary.h (+1)
- (modified) llvm/tools/llvm-profgen/llvm-profgen.cpp (+18-7)
``````````diff
diff --git a/llvm/docs/CommandGuide/llvm-profgen.rst b/llvm/docs/CommandGuide/llvm-profgen.rst
index e515612983894..350efef89f7e7 100644
--- a/llvm/docs/CommandGuide/llvm-profgen.rst
+++ b/llvm/docs/CommandGuide/llvm-profgen.rst
@@ -24,6 +24,11 @@ At least one of the following commands are required:
Path of perf-script trace created by Linux perf tool with `script`
command(the raw perf.data should be profiled with -b).
+.. option:: --etm=<string>
+
+ Path of the ETM trace file created by ARM CoreSight trace tools.
+ Requires the OpenCSD library to be enabled during the build.
+
.. option:: --binary=<string[,string,...]>
Path of the input profiled binary files.
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index a47c5a48d51b8..cfa9e812f9ee8 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -238,6 +238,8 @@ Changes to the Debug Info
Changes to the LLVM tools
-------------------------
+* `llvm-profgen` now supports ETM trace decoding using the OpenCSD library for Cortex-M targets.
+
* `llvm-objcopy` no longer corrupts the symbol table when `--update-section` is called for ELF files.
* `FileCheck` option `-check-prefix` now accepts a comma-separated list of
prefixes, making it an alias of the existing `-check-prefixes` option.
diff --git a/llvm/test/tools/llvm-profgen/Inputs/etm-opencsd.yaml b/llvm/test/tools/llvm-profgen/Inputs/etm-opencsd.yaml
new file mode 100644
index 0000000000000..6824b4a9c108b
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/etm-opencsd.yaml
@@ -0,0 +1,48 @@
+# This file contains binary data extracted from the OpenCSD repository sample tests
+# (specifically from decoder/tests/snapshots/armv8_1m_branches/).
+# It is used to verify ETM trace decoding in llvm-profgen.
+--- !ELF
+FileHeader:
+ Class: ELFCLASS32
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_ARM
+ Flags: [ EF_ARM_EABI_VER5 ]
+ProgramHeaders:
+ - Type: PT_LOAD
+ Flags: [ PF_X, PF_R ]
+ VAddr: 0x1E008
+ FirstSec: .text1
+ LastSec: .text1
+ - Type: PT_LOAD
+ Flags: [ PF_X, PF_R ]
+ VAddr: 0x4F9A02
+ FirstSec: .text2
+ LastSec: .text2
+Sections:
+ - Name: .text1
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1E008
+ AddressAlign: 0x4
+ Content: 2de9f04f8db08142984651eab08706922ab147ea0002b2f1ff3f40f3bf80002fcde9020100f1af8000f08b8016984ff0000a4fea88093a4600f1040b202000eb4810059704900698043008900de000bf079a0af1080a059f0bf1200b0498083a0899ba45014408916bda012a10460792d8bf012008284ff00802a7eb0a0150eab28c082951eab28001293bdb06990024089e01eb8a02169901eb8a03ffe704eb0a0707fb08f193ed002a02eb810191ed001a1a9921ee021a91ed000a44f003c011e000bf19990134644507fb01f1189f4e4407eb810191ed002aa0ee012a81ed002a0fd0dbe73546594600bfb5ec012ab1ec013a22ee032a31ee021a0ff00bc0e0e700bfbaf1000fa2d00afb08f1069a09ab199e02eb8101169acde90b180afb06f10992189a02eb81071a990baa91ed000a1799cde900760a915146dff09cf186e7dde902318b421add08fb01f0069a199e02eb8002581a169bcde90b2806fb01f20993189b03eb82071a9a09ab92ed000a179acde900760a920baadff07cf10db0bde8f08f4ef669304ef6fc12c0f27700a621c0f2770251f218f64ef669304ef20772c0f27700b221c0f2770251f20df6d4d4
+ - Name: .text2
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x4F9A02
+ AddressAlign: 0x4
+ Content: 002851d096f85c00002864d030462a46234606f5956800f0f2f996ed080a96f82c0020ee009a01286ed1b7eec80a06f594792a464846234602f067f8d8e906104a1e70f100020fdbd8e9083253ea020708d0eb1a64eb0202c91a904104da484602f079f9c8e9085496f8541096f8a40401293ad1002847d098ed000bb5ee400bf1ee10fa40d098ed041b21ee011b81ee000b98ed021b31ee400bb7eec91a80fe010bb7eec09b2fe006f5a4602a4623460af038fe002800f0a080b7eec80a4ef622214ff46d70c2f6942121228ded000b92f18dfb91e0d6ed010a30462a462346b0ee480a00f0e3f887e098ed020b9fed6b1b002801fe000bb7eec91a80fe010bb7eec00b09fe009ad6e928324ff07e5096ed1a0a0390012078ee400a8df80a00002153ea020040f2011706f1680004910591adf8087015d0eb1a64eb020283eae27382eae277b3ebe27367ebe27245f60107cff64707db1b72f10d0228bf8df80a1001e08df80a1003a902aab0ee490a00f029fa96f8e804002804bf96f8f8045feac0703fd030462a46234600f039fe98bb002706f264404121c6f8a874c6e9b077c6f8ac74c6f8b074c6f8b474c6f8b874c6f8bc74c6f85874c6f85c74a6f8607438f7ebf506f1c800faf7c3fed6f808052a46234686f85c70016b0131016306f5aa600af09efd38b142f6491140f6c440caf62f2100226ae706b0bdec048b01b0bde8f0832846214670f75efd41ec180bd8e9120170f758fd96ed120a41ec110bb7eec00a9fed1f2ba0ee021bb4ee481bf1ee10faa6d896f84400012186f8f81401280bd100204df2e111c9e90000c8f2762140f6aa60002292f1c8fa96f8450001280ed1306f40f6f931c3f249010022c8f85800012086f8040540f6ea6092f1b5fa44f6976140f65260c8f26911002292f1acfa76e700bf00bf00bf00000000000000000000000065cdcd412de9f04397b080ed1a0a20eea00a0646d0f8d0048ded090a96ed020a96ed031a00216f4620ee000a21ee011ac6e91b1038462421984691468ded0a0a8ded0b1a38f755f54ff0030e09a800bf50f8041b47f8101b0ff007c0002069468df858000ca809f0a1fb9df85800002865d09df85900f8b906f174042421204638f737f5002001210caa00bf0130134640f001e0274600bf53f80c5b47f8045b0ff007c00131032804f10c0402f10402ecd1012086f8980001204a4686f85c0030464346c6e9189800f046fda0bb002406f264404121c6f8a844c6e9b044c6f8ac44c6f8b044c6f8b444c6f8b844c6f8bc44c6f85844c6f85c44a6f8604438f7f8f406f1c800faf7d0fdd6f808054a46434686f85c40016b0131016306f5aa600af0abfc40b142f6491140f6c440caf62f21002292f106fa17b0bde8f0834ef6fb304ef6bf22c0f27700c121c0f2770275f1eaff2de9f0412ded048b96b00446d0e9181016461d468a1a70eb030223dab6eb010865eb00074ef20140c5f20b40b8eb000077f10200c4e918652ddb04f5a26032462b460af070fc002800f0a98045f6ca2140f64b10c9f283311122cde9008792f1c7f99ce004f5a26032462b460af05bfc002800f09480d4e9180342f6ab71c2f604215222029040f6f200cde90065039392f1aef983e0204600f0f6fa204620ee008a00f06dfb204632462b4620ee009a00f0e2fb20ee000a8ded108a8ded119a8ded120a4ff0030e39a107ad80ef500000202a4687ef504f91ed003f40f6ff7123fe400f35fe016fedee101a71fe4d0fa2ec037f01300ff00dc84046394670f7e5fb41ec100b9fed2e1b40f6ff7110aa20ee010b05f10c0704f16808b7eec00b8ded0a0aedee101aa16a10ee100a71fe4d0f92ed001f31ee600e0b9104a904f17402284671fe4d0f81ed001f19f732f694ed1a0a6c340021022313aa05eb81003e4690ed001a43f001e0204620ee011ab0ec013a96ed002a0c3622ee032a31ee021a0ff00bc802eb810001310437032980ed001ae2d113aa07ca88e8070016b0bdec048bbde8f08100bf00bf00bf00bf0000000001000000020000000300000095d626e80b2e113e2de9f0472ded068b9cb0b1eec0aab0ee409a15460f46b0ee608a04464ff0000a012600f10c08cdf808a08df80c60b4ee4aaaf1ee10fab0ee4a0a5ad60df11c098df820608df86c600df1080c04ae1aab404639464a468ded070a8ded1a0acde9006c01f091ffb4ee4aaaf1ee10fa8df864a043d6012004ae8df86c001aaa304639464346cdf800908ded1aaa07f0b6fb4ff0030e30469fed1d0a00bf15f8011bb0ee401a012908bf90ed001aa0ec011a0ff00dc040f6ff71edee101a18ee100a71fe4d0f96ed001f31ee600e31fe4d8f89ed001f99ed001f94ed003f00ef420d71fe4d0f84ed001f1cb0bdec068bbde8f087b0ee490a70f785fb9fe7b0ee490a70f780fbb0ee40aab4e700bf000000002de9f04381b02ded088b88b0b0ee608ab0ee409a9846154606460af0efff002848d0b0ee480a0af0ffff002842d096f8d80406f59c69199f012802bfc9e90058002086f8d80496f8e8043946189c012802bfc9e90458002086f8e80420460af0fdff00281cbfc6e92858c6e92a4796f85c00d0b330462a464346fff755fe96ed1a0a96ed1c1a96f82e10002030ee410a9fedad1a06f16807012981ee001a0690b1ee412a8ded071a8ded052a29d128ee08aa40e006f5a6602a4643460af0c7fa002800f0ec80b7eec90ab7eec81a49f2f47140f2fc408ded000bc3f27661a2228ded021b92f118f8d9e0b0ee490a0af05bfbd6ed040a30462a464346fff76cfdcde096ed091a96f82d0021ee01aa012811d1b0ee40ba06f1c800b0ee490afaf7c3fb96f8e100012803d196ed3a0a80fe0aaab0ee4b0a0af015fbd6e92810012279ee400a8df8122040f20112adf8102051ea000312d0691a68eb000081eae07180eae072b1ebe07162ebe07045f60102cff64702891a70f10d0002d300208df8120005a904aa3846b0ee4a0afff7b0fe96f8f804002848d196ed130ad6f8fc04b4ee408af1ee10fa9cbf0130c6f8fc04316d884201da002030e096f84400012186f8f81401280bd100204df2e111c9e90000c8f2762140f6aa60002291f19cff96f8450001280ed1306f40f6f931c3f249010022c9f82000012086f8040540f6ea6091f189ff48f2370140f6aa50c4f2f171002291f180ff96f8f80496f8e814002908bf5feac0703fd030462a46434600f07cfa98bb002406f264404121c6f8a844c6e9b044c6f8ac44c6f8b044c6f8b444c6f8b844c6f8bc44c6f85844c6f85c44a6f8604438f72ef206f1c800faf706fbd6f808052a46434686f85c40016b0131016306f5aa600af0e1f938b142f6491140f6c440caf62f21002222e708b0bdec088b01b0bde8f0832846414670f7a1f941ec180bd9e9040170f79bf996ed120a41ec110bb7eec00a9fed1d2ba0ee021bb4ee481bf1ee10faa6d896f84400012186f8f81401280bd100204df2e111c9e90000c8f2762140f6aa60002291f10bff96f8450001280ed1306f40f6f931c3f249010022c9f82000012086f8040540f6ea6091f1f8fe44f6976140f65260c8f26911002291f1effe76e700bf00bf0000000065cdcd413e15044690f85c007047d4d410b50446702290ed1a0a90f8040594f84510c0074ff0700018bf4ff4a060002950ea1280204490ed001a30ee410a0af0eff994ed2e1a30ee410a10bd80b590ed1a0a0af0e5f980bd90f8461090f8f80460ea010000f001007047d4d42de9f04f81b02ded028b90b090f8981000f1740890ed0e8a059009b3032406ae00274ff07e590df13c0a4ff0010b45460320424603210323c6e90077b760cdf83c90cde9005bcde9026bcdf810a023f797f5
+ - Name: .ARM.attributes
+ Type: SHT_ARM_ATTRIBUTES
+ Content: 4140000000616561626900013600000043322E30390005636F727465782D6D3300060A074D080009020E0011011204140115001703180119011A021E0622012601
+Symbols:
+ - Name: foo
+ Type: STT_FUNC
+ Section: .text1
+ Value: 0x1E008
+ Size: 436
+ - Name: bar
+ Type: STT_FUNC
+ Section: .text2
+ Value: 0x4F9A02
+ Size: 2776
diff --git a/llvm/test/tools/llvm-profgen/etm-arch.test b/llvm/test/tools/llvm-profgen/etm-arch.test
new file mode 100644
index 0000000000000..31bdef63e0205
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/etm-arch.test
@@ -0,0 +1,81 @@
+; REQUIRES: opencsd
+; RUN: split-file %s %t
+; RUN: printf '\x80' > %t.etm
+
+; Test 1: ARM binary with Cortex-M attributes should pass.
+; RUN: yaml2obj %t/arm-valid.yaml -o %t.arm.valid.bin
+; RUN: llvm-profgen --etm=%t.etm --binary=%t.arm.valid.bin --output=%t.out 2>&1 | FileCheck %s --check-prefix=ARM-VALID
+
+; Test 2: ARM binary without Cortex-M attributes should fail.
+; RUN: yaml2obj %t/arm-no-attr.yaml -o %t.arm.noattr.bin
+; RUN: not llvm-profgen --etm=%t.etm --binary=%t.arm.noattr.bin --output=%t.out 2>&1 | FileCheck %s --check-prefix=ARM-NOT-VALID
+
+; Test 3: Non-ARM binary (X86) should fail.
+; RUN: yaml2obj %t/x86.yaml -o %t.x86.bin
+; RUN: not llvm-profgen --etm=%t.etm --binary=%t.x86.bin --output=%t.out 2>&1 | FileCheck %s --check-prefix=X86
+
+; ARM-VALID-NOT: OpenCSD: Unsupported processor architecture
+
+; ARM-NOT-VALID: OpenCSD: Unsupported processor architecture. Only microcontroller-class (Cortex-M) is currently supported.
+
+; X86: OpenCSD: Unsupported processor architecture. Only microcontroller-class (Cortex-M) is currently supported.
+
+;--- arm-valid.yaml
+--- !ELF
+FileHeader:
+ Class: ELFCLASS32
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_ARM
+ProgramHeaders:
+ - Type: PT_LOAD
+ Flags: [ PF_X, PF_R ]
+ VAddr: 0x1000
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x4
+ Content: C3
+ - Name: .ARM.attributes
+ Type: SHT_ARM_ATTRIBUTES
+ Content: 4140000000616561626900013600000043322E30390005636F727465782D6D3300060A074D080009020E0011011204140115001703180119011A021E0622012601
+
+;--- arm-no-attr.yaml
+--- !ELF
+FileHeader:
+ Class: ELFCLASS32
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_ARM
+ProgramHeaders:
+ - Type: PT_LOAD
+ Flags: [ PF_X, PF_R ]
+ VAddr: 0x1000
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x4
+ Content: C3
+
+;--- x86.yaml
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_X86_64
+ProgramHeaders:
+ - Type: PT_LOAD
+ Flags: [ PF_X, PF_R ]
+ VAddr: 0x1000
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x4
+ Content: C3
diff --git a/llvm/test/tools/llvm-profgen/etm-opencsd.test b/llvm/test/tools/llvm-profgen/etm-opencsd.test
new file mode 100644
index 0000000000000..0de28a01f07c5
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/etm-opencsd.test
@@ -0,0 +1,15 @@
+; REQUIRES: opencsd
+; RUN: yaml2obj %S/Inputs/etm-opencsd.yaml -o %t.bin
+
+; Verify ETM trace decoding using OpenCSD with inlined trace data.
+; RUN: printf "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x83\x01\x9a\x4f\x00\x00\x96\x81\x9a\xfc\xfe\xfa\xf8\xfd\xfd\xd6\xfd\xfd\xc1" > %t.trace
+
+; Run the generator to produce the output file.
+; RUN: llvm-profgen --etm=%t.trace --binary=%t.bin --output=%t.prof --format=text
+; RUN: ls %t.prof
+
+; Verify error handling when no synchronization header (0x80) is found.
+; RUN: printf "\x00\x00\x00" > %t.nosync
+; RUN: not llvm-profgen --etm=%t.nosync --binary=%t.bin --output=%t2.prof 2>&1 | FileCheck %s --check-prefix=NOSYNC
+
+; NOSYNC: error: No synchronization header (0x80) found in the bitstream.
diff --git a/llvm/test/tools/llvm-profgen/lit.local.cfg b/llvm/test/tools/llvm-profgen/lit.local.cfg
index c628a80ff368d..ec7d3d52b8215 100644
--- a/llvm/test/tools/llvm-profgen/lit.local.cfg
+++ b/llvm/test/tools/llvm-profgen/lit.local.cfg
@@ -5,3 +5,13 @@ config.suffixes = [".test", ".ll", ".s", ".yaml"]
if not "X86" in config.root.targets:
config.unsupported = True
+
+llvm_profgen = lit.util.which('llvm-profgen')
+if llvm_profgen:
+ try:
+ result = subprocess.run([llvm_profgen, '--etm=/dev/null'], capture_output=True, text=True)
+ if "OpenCSD support was not found" not in result.stderr:
+ config.available_features.add('opencsd')
+ except Exception as e:
+ pass
+
diff --git a/llvm/tools/llvm-profgen/CMakeLists.txt b/llvm/tools/llvm-profgen/CMakeLists.txt
index 8d13bbb2bdb14..e42af2d8a6330 100644
--- a/llvm/tools/llvm-profgen/CMakeLists.txt
+++ b/llvm/tools/llvm-profgen/CMakeLists.txt
@@ -25,3 +25,23 @@ add_llvm_tool(llvm-profgen
ProfileGenerator.cpp
MissingFrameInferrer.cpp
)
+
+set(LLVM_ENABLE_OPENCSD "AUTO" CACHE STRING "Enable OpenCSD support in llvm-profgen")
+
+if(NOT LLVM_ENABLE_OPENCSD STREQUAL "OFF")
+ find_library(OPENCSD_LIB NAMES opencsd_c_api opencsd)
+ find_path(OPENCSD_INCLUDE NAMES opencsd/ocsd_if_types.h)
+
+ if(OPENCSD_LIB AND OPENCSD_INCLUDE)
+ target_compile_definitions(llvm-profgen PRIVATE HAVE_OPENCSD)
+ target_include_directories(llvm-profgen PRIVATE ${OPENCSD_INCLUDE})
+ target_link_libraries(llvm-profgen PRIVATE ${OPENCSD_LIB})
+ message(STATUS "llvm-profgen: OpenCSD support enabled.")
+ else()
+ if(LLVM_ENABLE_OPENCSD STREQUAL "ON")
+ message(FATAL_ERROR "OpenCSD enabled but library or headers not found.")
+ else()
+ message(STATUS "llvm-profgen: OpenCSD not found; ETM decoding support disabled.")
+ endif()
+ endif()
+endif()
diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index 1dc59321fd91f..f0631301c3a0a 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -16,6 +16,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/TargetParser/Triple.h"
#define DEBUG_TYPE "perf-reader"
@@ -354,6 +355,11 @@ PerfReaderBase::create(ProfiledBinary *Binary, PerfInputFile &PerfInput,
return PerfReader;
}
+ if (PerfInput.Format == PerfFormat::ETMFormat) {
+ PerfReader.reset(new ETMReader(Binary, PerfInput.InputFile));
+ return PerfReader;
+ }
+
// For perf data input, we need to convert them into perf script first.
// If this is a kernel perf file, there is no need for retrieving PIDs.
if (PerfInput.Format == PerfFormat::PerfData)
@@ -1369,5 +1375,208 @@ void PerfScriptReader::parsePerfTraces() {
SmallVector<CleanupInstaller, 2> PerfScriptReader::TempFileCleanups;
+void ETMReader::recordProcessedRange(uint64_t Start, uint64_t End,
+ uint64_t Count) {
+ if (!SampleCounters.empty()) {
+ auto &Counter = SampleCounters.begin()->second;
+ Counter.recordRangeCount(Start, End, Count);
+ }
+}
+
+#ifdef HAVE_OPENCSD
+ocsd_datapath_resp_t ETMReader::processTrace(
+ const void *PContext, const ocsd_trc_index_t /*IndexSOP*/,
+ const uint8_t /*TrcChanID*/, const ocsd_generic_trace_elem *Element) {
+ auto *Reader = static_cast<ETMReader *>(const_cast<void *>(PContext));
+ if (!Reader || !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;
+
+ // 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.
+ if (End > Start)
+ Reader->recordProcessedRange(Start, End - 1, 1);
+ }
+
+ return OCSD_RESP_CONT;
+}
+
+// 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.
+void ETMReader::mapELFSegments(dcd_tree_handle_t DcdTree,
+ ProfiledBinary &Binary) {
+ std::string ExePath = Binary.getPath().str();
+
+ auto BinaryOrErr = object::createBinary(Binary.getPath());
+ if (!BinaryOrErr)
+ exitWithError("Failed to open binary for ETM mapping", Binary.getPath());
+
+ object::Binary &SourceBin = *BinaryOrErr.get().getBinary();
+ auto *ElfBase = dyn_cast<object::ELFObjectFileBase>(&SourceBin);
+ if (!ElfBase)
+ return;
+
+ std::vector<ocsd_file_mem_region_t> Regions;
+ auto ProcessHeaders = [&](const auto &ElfFile) {
+ auto ProgramHeaders = ElfFile.program_headers();
+ if (!ProgramHeaders)
+ return;
+
+ for (const auto &Phdr : *ProgramHeaders) {
+ // Only map segments that are loadable and executable (code sections).
+ if (Phdr.p_type == llvm::ELF::PT_LOAD &&
+ (Phdr.p_flags & llvm::ELF::PF_X)) {
+ ocsd_file_mem_region_t Region;
+ memset(&Region, 0, sizeof(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>(ElfBase))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF64LEObjectFile>(ElfBase))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF32BEObjectFile>(ElfBase))
+ ProcessHeaders(O->getELFFile());
+ else if (auto *O = dyn_cast<object::ELF64BEObjectFile>(ElfBase))
+ ProcessHeaders(O->getELFFile());
+
+ if (!Regions.empty()) {
+ // Single transaction mapping to avoid overlap errors.
+ if (ocsd_dt_add_binfile_region_mem_acc(
+ DcdTree, Regions.data(), (uint32_t)Regions.size(),
+ OCSD_MEM_SPACE_ANY, ExePath.c_str()) != 0) {
+ exitWithError("OpenCSD: Failed to map ELF executable segments.");
+ }
+ }
+}
+
+// Configure the decoder for microcontroller-class targets based on the binary's
+// architecture.
+void ETMReader::setupDecoder(dcd_tree_handle_t DcdTree,
+ ProfiledBinary &Binary) {
+ ocsd_etmv4_cfg Config;
+ memset(&Config, 0, sizeof(Config));
+
+ const Triple &BinaryTriple = Binary.getTriple();
+ // Identify microcontroller-class hardware (Cortex-M) by checking the
+ // architecture family or the instruction ...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/191584
More information about the llvm-commits
mailing list