W-M-R commented on code in PR #21344:
URL: https://github.com/apache/nuttx/pull/21344#discussion_r4477516653


##########
tools/gcov.py:
##########
@@ -18,33 +18,352 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+"""Code coverage report generator for NuttX.
+
+Supports both GCC gcov and Clang source-based coverage. The toolchain is
+inferred from the ELF binary's `.comment` section, and the cross-compile
+prefix (if any) is inferred from the ELF machine type, so users no longer
+have to specify `-t`/`--toolchain`.
+
+Both toolchains converge on the lcov tracefile format: gcc captures it
+with `lcov -c`, clang exports it with `llvm-cov export -format=lcov`, and
+`genhtml` renders both, so reports look the same regardless of the
+toolchain and tracefiles can be merged with `lcov --add-tracefile`.
+
+Usage example:
+
+    gcov.py -e build/nuttx -s build -a coverage_data.bin -o report_dir
+
+The required tools (gcov/lcov/genhtml for GCC; llvm-profdata/llvm-cov/
+llvm-readelf/ld.lld for Clang) are resolved from PATH; the cross-compile
+gcov is matched by arch (e.g. aarch64-none-elf-gcov for EM_AARCH64).
+
+Use `-b OLD=NEW` (repeatable) to rewrite source paths recorded by the
+compiler at build time to local paths, mirroring `-fdebug-prefix-map`:
+
+    gcov.py -b /old/build/path=/new/local/path ...
+
+When only the local root is known, pass just NEW and the build-time root
+is inferred from the paths recorded in the coverage data:
+
+    gcov.py -b /new/local/path ...
+"""
+
 import argparse
+import concurrent.futures
+import json
 import os
 import re
 import shutil
+import struct
 import subprocess
 import sys
+from dataclasses import dataclass, field
+from typing import List, Optional, Tuple
+
+# =============================================================================
+# 1. CONSTANTS
+# =============================================================================
+
+# LLVM raw profile magic bytes (first 8 bytes of .profraw).
+#
+# The magic is a uint64_t written by the runtime in native byte order
+# (high byte 0xff, low byte 0x81; "lprofr"/"lprofR" in the middle), so
+# big-endian platforms produce \xff..\x81 and little-endian platforms
+# (ARM aarch64, x86_64, etc.) produce \x81..\xff. Both forms must be
+# accepted, mirroring LLVM's RawInstrProfReader::hasFormat which compares
+# the on-disk magic against both the native and byte-swapped values.
+#
+# 64-bit BE: \xff lprofr \x81   /   64-bit LE: \x81 rforpl \xff
+# 32-bit BE: \xff lprofR \x81   /   32-bit LE: \x81 Rforpl \xff
+PROFRAW_MAGIC_64 = b"\xff\x6c\x70\x72\x6f\x66\x72\x81"
+PROFRAW_MAGIC_32 = b"\xff\x6c\x70\x72\x6f\x66\x52\x81"
+PROFRAW_MAGICS = (
+    PROFRAW_MAGIC_64,
+    PROFRAW_MAGIC_64[::-1],
+    PROFRAW_MAGIC_32,
+    PROFRAW_MAGIC_32[::-1],
+)
+
+# gcov binary pack heuristic:
+# First 4 bytes are path_len (LE uint32), typically 30~200 for gcda paths.
+# Followed by a path string starting with a printable char (not \x00 or \xff).
+GCOV_BINPACK_PATH_LEN_MIN = 10
+GCOV_BINPACK_PATH_LEN_MAX = 4096
+
+# Suffix appended to a -a input file to name the directory holding the
+# coverage data extracted from it (hex dump / binpack -> individual files).
+CONVERT_SUFFIX = "_convert"
+
+# ELF e_machine values we care about for cross-prefix inference.
+EM_386 = 3
+EM_X86_64 = 62
+EM_ARM = 40
+EM_AARCH64 = 183
+EM_RISCV = 243
+EM_XTENSA = 94
+EM_TRICORE = 44
+
+# Candidate cross-compile gcov binary names per architecture, in priority
+# order. The first one found on PATH is used.
+GCC_CROSS_PREFIXES = {
+    EM_AARCH64: ["aarch64-none-elf-gcov", "aarch64-linux-gnu-gcov"],
+    EM_ARM: ["arm-none-eabi-gcov", "arm-linux-gnueabi-gcov"],
+    EM_RISCV: ["riscv64-unknown-elf-gcov", "riscv32-unknown-elf-gcov"],

Review Comment:
   Other architectures have not been tested before, and will be integrated 
after testing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to