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 892247b9cb67ca6529b6ca2c45e87afcbf32b66d
Author: Bowen Wang <[email protected]>
AuthorDate: Fri May 8 16:41:38 2026 +0800

    tools/coredump: support UTF-16 encoded log files
    
    
    Detect BOM header and use correct encoding when opening log files.
    Serial tools on Windows may save logs in UTF-16LE format, which
    previously caused parsing failures.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 tools/coredump.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/coredump.py b/tools/coredump.py
index 7763af24eff..31c4140df46 100755
--- a/tools/coredump.py
+++ b/tools/coredump.py
@@ -36,6 +36,16 @@ except ImportError:
     print("lzf module not found, execute `pip install python-lzf`")
 
 
+def detect_encoding(filepath):
+    with open(filepath, "rb") as f:
+        bom = f.read(4)
+    if bom[:2] == b"\xff\xfe":
+        return "utf-16-le"
+    if bom[:2] == b"\xfe\xff":
+        return "utf-16-be"
+    return "utf-8"
+
+
 def try_extract(filepath) -> io.BytesIO:
     """
     Parse log file lines, and try to extract coredump data text
@@ -46,7 +56,8 @@ def try_extract(filepath) -> io.BytesIO:
     in_coredump = False
     bas64 = False
     coredump = []
-    with open(filepath, "r", errors="ignore") as f:
+    encoding = detect_encoding(filepath)
+    with open(filepath, "r", encoding=encoding, errors="ignore") as f:
         for line in f.readlines():
             line = line.rstrip("\n\r")
             if "Start coredump" in line:

Reply via email to