[Lldb-commits] [lldb] [lldb] Add a compiler/interpreter of LLDB data formatter bytecode to lldb/examples (PR #113398)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Fri Dec 6 09:01:39 PST 2024
================
@@ -0,0 +1,167 @@
+"""
+This is the llvm::Optional data formatter from llvm/utils/lldbDataFormatters.py
+with the implementation replaced by bytecode.
+"""
+
+from __future__ import annotations
+from formatter_bytecode import *
+import lldb
+
+
+def __lldb_init_module(debugger, internal_dict):
+ debugger.HandleCommand(
+ "type synthetic add -w llvm "
+ f"-l {__name__}.MyOptionalSynthProvider "
+ '-x "^MyOptional<.+>$"'
+ )
+ debugger.HandleCommand(
+ "type summary add -w llvm "
+ f"-e -F {__name__}.MyOptionalSummaryProvider "
+ '-x "^MyOptional<.+>$"'
+ )
+
+
+def stringify(bytecode: bytearray) -> str:
+ s = ""
+ in_hex = False
+ for b in bytecode:
+ if (b < 32 or b > 127 or chr(b) in ['"', "`", "'"]) or (
+ in_hex
+ and chr(b).lower()
+ in [
+ "a",
+ "b",
+ "c",
+ "d",
+ "e",
+ "f",
+ "0",
+ "1",
+ "2",
+ "3",
+ "4",
+ "5",
+ "6",
+ "7",
+ "8",
+ "9",
+ ]
+ ):
+ s += r"\x" + hex(b)[2:]
+ in_hex = True
+ else:
+ s += chr(b)
+ in_hex = False
+ return s
+
+
+def evaluate(assembler: str, data: list):
+ bytecode = compile(assembler)
+ trace = True
+ if trace:
+ print(
+ "Compiled to {0} bytes of bytecode:\n{1}".format(
+ len(bytecode), stringify(bytecode)
+ )
+ )
+ result = interpret(bytecode, [], data, False) # trace)
+ if trace:
+ print("--> {0}".format(result))
+ return result
+
+
+# def GetOptionalValue(valobj):
+# storage = valobj.GetChildMemberWithName("Storage")
+# if not storage:
+# storage = valobj
+#
+# failure = 2
+# hasVal = storage.GetChildMemberWithName("hasVal").GetValueAsUnsigned(failure)
+# if hasVal == failure:
+# return "<could not read MyOptional>"
+#
+# if hasVal == 0:
+# return None
+#
+# underlying_type = storage.GetType().GetTemplateArgumentType(0)
+# storage = storage.GetChildMemberWithName("value")
+# return storage.Cast(underlying_type)
----------------
JDevlieghere wrote:
I was suggesting to remove it since you have the individual bits, but maybe it's good to have the whole thing there too, to get a sense of how the full formatter works.
https://github.com/llvm/llvm-project/pull/113398
More information about the lldb-commits
mailing list