Skip to content

Commit 69c8e64

Browse files
author
Walter Erquinigo
committed
[formatters] Improve documentation
This adds some important remarks to the data formatter documentation. Differential Revision: https://reviews.llvm.org/D115974
1 parent d13da5f commit 69c8e64

File tree

1 file changed

+96
-9
lines changed

1 file changed

+96
-9
lines changed

lldb/docs/use/variable.rst

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Variable Formatting
77
LLDB has a data formatters subsystem that allows users to define custom display
88
options for their variables.
99

10-
Usually, when you type frame variable or run some expression LLDB will
10+
Usually, when you type ``frame variable`` or run some expression LLDB will
1111
automatically choose the way to display your results on a per-type basis, as in
1212
the following example:
1313

@@ -17,7 +17,11 @@ the following example:
1717
(uint8_t) x = 'a'
1818
(intptr_t) y = 124752287
1919

20-
However, in certain cases, you may want to associate a different style to the display for certain datatypes. To do so, you need to give hints to the debugger
20+
Note: ``frame variable`` without additional arguments prints the list of
21+
variables of the current frame.
22+
23+
However, in certain cases, you may want to associate a different style to the
24+
display for certain datatypes. To do so, you need to give hints to the debugger
2125
as to how variables should be displayed. The LLDB type command allows you to do
2226
just that.
2327

@@ -29,6 +33,63 @@ Using it you can change your visualization to look like this:
2933
(uint8_t) x = chr='a' dec=65 hex=0x41
3034
(intptr_t) y = 0x76f919f
3135

36+
In addition, some data structures can encode their data in a way that is not
37+
easily readable to the user, in which case a data formatter can be used to
38+
show the data in a human readable way. For example, without a formatter,
39+
printing a ``std::deque<int>`` with the elements ``{2, 3, 4, 5, 6}`` would
40+
result in something like:
41+
42+
::
43+
44+
(lldb) frame variable a_deque
45+
(std::deque<Foo, std::allocator<int> >) $0 = {
46+
std::_Deque_base<Foo, std::allocator<int> > = {
47+
_M_impl = {
48+
_M_map = 0x000000000062ceb0
49+
_M_map_size = 8
50+
_M_start = {
51+
_M_cur = 0x000000000062cf00
52+
_M_first = 0x000000000062cf00
53+
_M_last = 0x000000000062d2f4
54+
_M_node = 0x000000000062cec8
55+
}
56+
_M_finish = {
57+
_M_cur = 0x000000000062d300
58+
_M_first = 0x000000000062d300
59+
_M_last = 0x000000000062d6f4
60+
_M_node = 0x000000000062ced0
61+
}
62+
}
63+
}
64+
}
65+
66+
which is very hard to make sense of.
67+
68+
Note: ``frame variable <var>`` prints out the variable ``<var>`` in the current
69+
frame.
70+
71+
On the other hand, a proper formatter is able to produce the following output:
72+
73+
::
74+
75+
(lldb) frame variable a_deque
76+
(std::deque<Foo, std::allocator<int> >) $0 = size=5 {
77+
[0] = 2
78+
[1] = 3
79+
[2] = 4
80+
[3] = 5
81+
[4] = 6
82+
}
83+
84+
which is what the user would expect from a good debugger.
85+
86+
Note: you can also use ``v <var>`` instead of ``frame variable <var>``.
87+
88+
It's worth mentioning that the ``size=5`` string is produced by a summary
89+
provider and the list of children is produced by a synthetic child provider.
90+
More information about these providers is available later in this document.
91+
92+
3293
There are several features related to data visualization: formats, summaries,
3394
filters, synthetic children.
3495

@@ -871,18 +932,26 @@ be implemented by the Python class):
871932
this call should return a new LLDB SBValue object representing the child at the index given as argument
872933
def update(self):
873934
this call should be used to update the internal state of this Python object whenever the state of the variables in LLDB changes.[1]
935+
Also, this method is invoked before any other method in the interface.
874936
def has_children(self):
875937
this call should return True if this object might have children, and False if this object can be guaranteed not to have children.[2]
876938
def get_value(self):
877939
this call can return an SBValue to be presented as the value of the synthetic value under consideration.[3]
878940
879-
[1] This method is optional. Also, it may optionally choose to return a value
880-
(starting with SVN rev153061/LLDB-134). If it returns a value, and that value
881-
is True, LLDB will be allowed to cache the children and the children count it
882-
previously obtained, and will not return to the provider class to ask. If
883-
nothing, None, or anything other than True is returned, LLDB will discard the
884-
cached information and ask. Regardless, whenever necessary LLDB will call
885-
update.
941+
As a warning, exceptions that are thrown by python formatters are caught
942+
silently by LLDB and should be handled appropriately by the formatter itself.
943+
Being more specific, in case of exceptions, LLDB might assume that the given
944+
object has no children or it might skip printing some children, as they are
945+
printed one by one.
946+
947+
[1] This method is optional. Also, a boolean value must be returned
948+
(starting with SVN rev153061/LLDB-134). If ``False`` is returned, then
949+
whenever the process reaches a new stop, this method will be invoked again to
950+
generate an updated list of the children for a given variable. Otherwise, if
951+
``True`` is returned, then the value is cached and this method won't be called
952+
again, effectively freezing the state of the value in subsequent stops. Beware
953+
that returning ``True`` incorrectly could show misleading information to the
954+
user.
886955

887956
[2] This method is optional (starting with SVN rev166495/LLDB-175). While
888957
implementing it in terms of num_children is acceptable, implementors are
@@ -972,6 +1041,24 @@ instead of the real ones. For instance,
9721041
(int) [3] = 1234
9731042
}
9741043

1044+
It's important to mention that LLDB invokes the synthetic child provider before
1045+
invoking the summary string provider, which allows the latter to have access to
1046+
the actual displayable children. This applies to both inlined summary strings
1047+
and python-based summary providers.
1048+
1049+
1050+
As a warning, when programmatically accessing the children or children count of
1051+
a variable that has a synthetic child provider, notice that LLDB hides the
1052+
actual raw children. For example, suppose we have a ``std::vector``, which has
1053+
an actual in-memory property ``__begin`` marking the beginning of its data.
1054+
After the synthetic child provider is executed, the ``std::vector`` variable
1055+
won't show ``__begin`` as child anymore, even through the SB API. It will have
1056+
instead the children calculated by the provider. In case the actual raw
1057+
children are needed, a call to ``value.GetNonSyntheticValue()`` is enough to
1058+
get a raw version of the value. It is import to remember this when implementing
1059+
summary string providers, as they run after the synthetic child provider.
1060+
1061+
9751062
In some cases, if LLDB is unable to use the real object to get a child
9761063
specified in an expression path, it will automatically refer to the synthetic
9771064
children. While in summaries it is best to always use ${svar to make your

0 commit comments

Comments
 (0)