From 81452d870d0548fb7729931ad961b391d6b830f4 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 17 Aug 2024 08:46:11 +0100 Subject: [PATCH] Add a `StaticVector` pretty-printer for LLDB --- .lldbinit | 4 +++ tools/lldbutils/README.md | 17 ++++++++++ tools/lldbutils/lldbutils/__init__.py | 6 ++++ .../pretty_printers/utils/static_vector_pp.py | 32 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .lldbinit create mode 100644 tools/lldbutils/README.md create mode 100644 tools/lldbutils/lldbutils/__init__.py create mode 100644 tools/lldbutils/lldbutils/pretty_printers/utils/static_vector_pp.py diff --git a/.lldbinit b/.lldbinit new file mode 100644 index 00000000000..6e6a4ef79ae --- /dev/null +++ b/.lldbinit @@ -0,0 +1,4 @@ +script topsrcdir = topsrcdir if "topsrcdir" in locals() else os.getcwd() +script sys.path.append(os.path.join(topsrcdir, "tools/lldbutils")) +script import lldbutils +script lldbutils.init() diff --git a/tools/lldbutils/README.md b/tools/lldbutils/README.md new file mode 100644 index 00000000000..2aca048fc3d --- /dev/null +++ b/tools/lldbutils/README.md @@ -0,0 +1,17 @@ +# lldb debugging enhancements + +The code in this directory is imported via `.lldbinit`. + +Working directory `.lldbinit` is not loaded by default. + +You can add the following to `~/.lldbinit` to load it when launching `lldb` from the command line: + +``` +settings set target.load-cwd-lldbinit true +``` + +If you're using VS Code, you can instead add the following to your configuration: + +```json +"lldb.launch.initCommands": ["command source ${workspaceFolder}/.lldbinit"] +``` diff --git a/tools/lldbutils/lldbutils/__init__.py b/tools/lldbutils/lldbutils/__init__.py new file mode 100644 index 00000000000..b49a60da79f --- /dev/null +++ b/tools/lldbutils/lldbutils/__init__.py @@ -0,0 +1,6 @@ +import lldb + +import lldbutils.pretty_printers.utils.static_vector_pp + +def init(): + lldbutils.pretty_printers.utils.static_vector_pp.init(lldb.debugger) diff --git a/tools/lldbutils/lldbutils/pretty_printers/utils/static_vector_pp.py b/tools/lldbutils/lldbutils/pretty_printers/utils/static_vector_pp.py new file mode 100644 index 00000000000..2f48e2877f8 --- /dev/null +++ b/tools/lldbutils/lldbutils/pretty_printers/utils/static_vector_pp.py @@ -0,0 +1,32 @@ +import lldb + + +class StaticVectorSyntheticChildrenProvider: + def __init__(self, valobj: lldb.SBValue, internal_dict): + self._val: lldb.SBValue = valobj + self._size: lldb.SBValue = self._val.GetChildMemberWithName("size_") + self._element_type: lldb.SBType = self._val.GetType().GetTemplateArgumentType(0) + self._element_size = self._element_type.GetByteSize() + self._data_addr = int( + self._val.GetChildMemberWithName("data_").GetLoadAddress() + ) + + def num_children(self, max_children): + return self._size.GetValueAsUnsigned(0) + + def get_child_index(self, name): + index = int(name) + return index if index < self.num_children() else None + + def get_child_at_index(self, index): + return self._val.CreateValueFromAddress( + f"[{index}]", + self._data_addr + self._element_size * index, + self._element_type, + ) + + +def init(debugger: lldb.debugger): + debugger.HandleCommand( + 'type synthetic add -x "devilution::StaticVector<" -l lldbutils.pretty_printers.utils.static_vector_pp.StaticVectorSyntheticChildrenProvider' + )