From 01f2c9101b219738b226fed44d5724844ecfb059 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Wed, 18 Mar 2026 13:22:46 -0700 Subject: [PATCH] [Debugging] Add lldb data formatter for RigidArray This introduces a debugging data formatter for RigidArray using a new feature of LLDB: [Formatter Bytecode][1]. The scope of this PR is a single initial data type, with the short and long term plan being to support all types vended by Swift Collections. The bytecode for this implementation is produced using a Python -> Formatter Bytecode compiler, contained in the [`formatter_bytecode`][2] module. The workflow for authoring and shipping the formatter is: 1. Update RigidArray.py as needed 2. Compile RigidArray.py to RigidArray+Formatter.swift (again, `formatter_bytecode`) 3. Compile RigidArray+Formatter.swift as part of the BasicContainers module By including the bytecode in the binary, lldb can load RigidArray's data formatter automatically, without any friction that comes with installation, setup, config, updating, or versioning. [1]: https://lldb.llvm.org/resources/formatterbytecode.html [2]: https://github.com/llvm/llvm-project/blob/main/lldb/examples/python/formatter_bytecode.py --- .../RigidArray/RigidArray+Formatter.swift | 36 +++++++++++++++++++ Utils/Debugger/Formatters/RigidArray.py | 26 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Sources/BasicContainers/RigidArray/RigidArray+Formatter.swift create mode 100644 Utils/Debugger/Formatters/RigidArray.py diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Formatter.swift b/Sources/BasicContainers/RigidArray/RigidArray+Formatter.swift new file mode 100644 index 00000000..fb8503c5 --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Formatter.swift @@ -0,0 +1,36 @@ +#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS) +@section("__DATA_CONST,__lldbformatters") +#else +@section(".lldbformatters") +#endif +@used +let __BasicContainers___RigidArray______formatter: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) = ( + // version + 0x01, + // remaining record size + 0x5f, + // type name size + 0x22, + // type name + 0x5e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x5b, 0x2e, 0x5d, 0x52, 0x69, 0x67, 0x69, 0x64, 0x41, 0x72, 0x72, 0x61, 0x79, 0x3c, 0x2e, 0x2b, 0x3e, 0x24, + // flags + 0x00, + // sig_update + 0x06, + // program size + 0x27, + // program + 0x20, 0x00, 0x03, 0x22, 0x08, 0x5f, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x23, 0x12, 0x60, 0x23, 0x18, 0x60, 0x20, 0x00, 0x03, 0x22, 0x06, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x23, 0x12, 0x60, 0x23, 0x18, 0x60, 0x23, 0x21, 0x60, + // sig_get_num_children + 0x02, + // program size + 0x04, + // program + 0x20, 0x02, 0x03, 0x13, + // sig_get_child_at_index + 0x04, + // program size + 0x0a, + // program + 0x20, 0x01, 0x03, 0x20, 0x03, 0x03, 0x23, 0x11, 0x60, 0x13, +) diff --git a/Utils/Debugger/Formatters/RigidArray.py b/Utils/Debugger/Formatters/RigidArray.py new file mode 100644 index 00000000..fef4b4e8 --- /dev/null +++ b/Utils/Debugger/Formatters/RigidArray.py @@ -0,0 +1,26 @@ +import lldb + + +class RigidArraySynthetic: + valobj: lldb.SBValue + storage: lldb.SBValue + count: int + + def __init__(self, valobj: lldb.SBValue, _) -> None: + self.valobj = valobj + + def num_children(self) -> int: + return self.count + + def get_child_at_index(self, idx: int) -> lldb.SBValue: + return self.storage.GetChildAtIndex(idx) + + def update(self) -> None: + self.storage = self.valobj.GetChildMemberWithName( + "_storage" + ).GetSyntheticValue() + self.count = ( + self.valobj.GetChildMemberWithName("_count") + .GetSyntheticValue() + .GetValueAsUnsigned() + )