This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1980e80b4339b14b7979ea1395e73adf36caf8ba
Author: wangmingrong1 <[email protected]>
AuthorDate: Mon Nov 2 16:08:34 2026 +0800

    feat(gcov): add binary pack format support
    
    Add support for parsing gcov binary pack files produced by
    gcov_binfile_dump (COVERAGE_MINI mode). The binary format stores
    gcda files as [path_len(4B LE)][path+NUL][data_size(4B LE)][data]
    sequences.
    
    - Add is_gcov_binpack_file() heuristic detection
    - Add parse_gcov_binpack() to extract individual gcda files
    - Integrate binary pack parsing into the main file dispatch logic
    
    Signed-off-by: wangmingrong1 <[email protected]>
---
 tools/gcov.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 105 insertions(+), 7 deletions(-)

diff --git a/tools/gcov.py b/tools/gcov.py
index 4c871ecde6c..1818bf04629 100755
--- a/tools/gcov.py
+++ b/tools/gcov.py
@@ -22,6 +22,7 @@ import argparse
 import os
 import re
 import shutil
+import struct
 import subprocess
 import sys
 
@@ -32,6 +33,13 @@ import sys
 PROFRAW_MAGIC_64 = b"\xff\x6c\x70\x72\x6f\x66\x72\x81"
 PROFRAW_MAGIC_32 = b"\xff\x6c\x70\x72\x6f\x66\x52\x81"
 
+# gcov binary pack magic 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
+
 
 def is_profraw_file(path):
     """Check if a file is an LLVM profraw file by magic number."""
@@ -125,6 +133,93 @@ def parse_gcda_data(path):
     return gcda_path
 
 
+def is_gcov_binpack_file(path):
+    """Check if a file is a gcov binary pack file.
+
+    Binary format: [path_len(4B LE)][path+NUL][data_size(4B LE)][data]...
+    Heuristic: first 4 bytes are a small uint32, followed by a printable path.
+    """
+
+    try:
+        with open(path, "rb") as f:
+            header = f.read(8)
+            if len(header) < 8:
+                return False
+            path_len = struct.unpack_from("<I", header, 0)[0]
+            if path_len < GCOV_BINPACK_PATH_LEN_MIN:
+                return False
+            if path_len > GCOV_BINPACK_PATH_LEN_MAX:
+                return False
+            first_char = header[4]
+            return first_char >= 0x20 and first_char < 0x7F
+    except (IOError, OSError):
+        return False
+
+
+def parse_gcov_binpack(path):
+    """Parse gcov binary pack file into individual gcda files.
+
+    Binary format: [path_len(4B LE)][path+NUL][data_size(4B LE)][data]...
+    """
+
+    with open(path, "rb") as f:
+        data = f.read()
+
+    gcda_path = path + "_covert"
+    os.makedirs(gcda_path, exist_ok=True)
+
+    offset = 0
+    count = 0
+    errors = 0
+
+    while offset < len(data):
+        if offset + 4 > len(data):
+            break
+
+        path_len = struct.unpack_from("<I", data, offset)[0]
+        offset += 4
+
+        if (
+            path_len == 0
+            or path_len > GCOV_BINPACK_PATH_LEN_MAX
+            or offset + path_len > len(data)
+        ):
+            print(f"Invalid path_len {path_len} at offset {offset - 4}")
+            errors += 1
+            break
+
+        filepath = data[offset : offset + 
path_len].rstrip(b"\x00").decode("utf-8")
+        offset += path_len
+
+        if offset + 4 > len(data):
+            print(f"Truncated at data_size for {filepath}")
+            errors += 1
+            break
+
+        data_size = struct.unpack_from("<I", data, offset)[0]
+        offset += 4
+
+        if offset + data_size > len(data):
+            print(f"Truncated data for {filepath}")
+            gcda_data = data[offset:]
+            offset = len(data)
+            errors += 1
+        else:
+            gcda_data = data[offset : offset + data_size]
+            offset += data_size
+
+        out_path = os.path.join(gcda_path, filepath.lstrip("/"))
+        os.makedirs(os.path.dirname(out_path), exist_ok=True)
+
+        with open(out_path, "wb") as out:
+            out.write(gcda_data)
+
+        count += 1
+
+    print(f"Parsed binary pack: {count} files ({errors} errors)")
+    return gcda_path
+
+
 def correct_content_path(file, shield: list, newpath):
     with open(file, "r", encoding="utf-8") as f:
         content = f.read()
@@ -634,19 +729,22 @@ def main():
         debug_file = os.path.join(result_dir, "debug.log")
         sys.stdout = open(debug_file, "w+")
 
-    # Parse hex dump files (works for both gcc and clang)
+    # Parse dump files (hex dump, binary pack, or profraw)
     gcda_dirs = []
     for i in args.gcda_dir:
         if os.path.isfile(i):
-            # Check if it's a binary profraw or a text hex dump log
-            if is_profraw_file(
-                os.path.join(root_dir, i) if not os.path.isabs(i) else i
-            ):
-                # Binary profraw file, use directly
+            abs_path = os.path.join(root_dir, i) if not os.path.isabs(i) else i
+            if is_profraw_file(abs_path):
+                # Binary profraw file (clang), use directly
                 gcda_dirs.append(os.path.abspath(i))
+            elif is_gcov_binpack_file(abs_path):
+                # Binary pack file from COVERAGE_MINI gcov_binfile_dump
+                gcda_dirs.append(parse_gcov_binpack(abs_path))
+                if args.delete:
+                    os.remove(i)
             else:
                 # Text file: try to parse as hex dump log
-                gcda_dirs.append(parse_gcda_data(os.path.join(root_dir, i)))
+                gcda_dirs.append(parse_gcda_data(abs_path))
                 if args.delete:
                     os.remove(i)
         else:

Reply via email to