Skip to content

Commit

Permalink
Add a StaticVector pretty-printer for LLDB
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Aug 17, 2024
1 parent 51659c8 commit 81452d8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .lldbinit
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions tools/lldbutils/README.md
Original file line number Diff line number Diff line change
@@ -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"]
```
6 changes: 6 additions & 0 deletions tools/lldbutils/lldbutils/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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'
)

0 comments on commit 81452d8

Please sign in to comment.