xref: /llvm-project/lldb/docs/use/variable.rst (revision 6c36bdb6eab1a3de3bce24ee0285c7745b17e407)
1edb874b2SJonas DevlieghereVariable Formatting
2edb874b2SJonas Devlieghere===================
3edb874b2SJonas Devlieghere
4edb874b2SJonas DevlieghereLLDB has a data formatters subsystem that allows users to define custom display
5edb874b2SJonas Devlieghereoptions for their variables.
6edb874b2SJonas Devlieghere
769c8e64bSWalter ErquinigoUsually, when you type ``frame variable`` or run some expression LLDB will
8edb874b2SJonas Devlieghereautomatically choose the way to display your results on a per-type basis, as in
9edb874b2SJonas Devliegherethe following example:
10edb874b2SJonas Devlieghere
11edb874b2SJonas Devlieghere::
12edb874b2SJonas Devlieghere
13edb874b2SJonas Devlieghere   (lldb) frame variable
14edb874b2SJonas Devlieghere   (uint8_t) x = 'a'
15edb874b2SJonas Devlieghere   (intptr_t) y = 124752287
16edb874b2SJonas Devlieghere
1769c8e64bSWalter ErquinigoNote: ``frame variable`` without additional arguments prints the list of
1869c8e64bSWalter Erquinigovariables of the current frame.
1969c8e64bSWalter Erquinigo
2069c8e64bSWalter ErquinigoHowever, in certain cases, you may want to associate a different style to the
2169c8e64bSWalter Erquinigodisplay for certain datatypes. To do so, you need to give hints to the debugger
22edb874b2SJonas Devlieghereas to how variables should be displayed. The LLDB type command allows you to do
23edb874b2SJonas Devliegherejust that.
24edb874b2SJonas Devlieghere
25edb874b2SJonas DevlieghereUsing it you can change your visualization to look like this:
26edb874b2SJonas Devlieghere
27edb874b2SJonas Devlieghere::
28edb874b2SJonas Devlieghere
29edb874b2SJonas Devlieghere   (lldb) frame variable
30edb874b2SJonas Devlieghere   (uint8_t) x = chr='a' dec=65 hex=0x41
31edb874b2SJonas Devlieghere   (intptr_t) y = 0x76f919f
32edb874b2SJonas Devlieghere
3369c8e64bSWalter ErquinigoIn addition, some data structures can encode their data in a way that is not
3469c8e64bSWalter Erquinigoeasily readable to the user, in which case a data formatter can be used to
3569c8e64bSWalter Erquinigoshow the data in a human readable way. For example, without a formatter,
3669c8e64bSWalter Erquinigoprinting a ``std::deque<int>`` with the elements ``{2, 3, 4, 5, 6}`` would
3769c8e64bSWalter Erquinigoresult in something like:
3869c8e64bSWalter Erquinigo
3969c8e64bSWalter Erquinigo::
4069c8e64bSWalter Erquinigo
4169c8e64bSWalter Erquinigo   (lldb) frame variable a_deque
4269c8e64bSWalter Erquinigo   (std::deque<Foo, std::allocator<int> >) $0 = {
4369c8e64bSWalter Erquinigo      std::_Deque_base<Foo, std::allocator<int> > = {
4469c8e64bSWalter Erquinigo         _M_impl = {
4569c8e64bSWalter Erquinigo            _M_map = 0x000000000062ceb0
4669c8e64bSWalter Erquinigo            _M_map_size = 8
4769c8e64bSWalter Erquinigo            _M_start = {
4869c8e64bSWalter Erquinigo               _M_cur = 0x000000000062cf00
4969c8e64bSWalter Erquinigo               _M_first = 0x000000000062cf00
5069c8e64bSWalter Erquinigo               _M_last = 0x000000000062d2f4
5169c8e64bSWalter Erquinigo               _M_node = 0x000000000062cec8
5269c8e64bSWalter Erquinigo            }
5369c8e64bSWalter Erquinigo            _M_finish = {
5469c8e64bSWalter Erquinigo               _M_cur = 0x000000000062d300
5569c8e64bSWalter Erquinigo               _M_first = 0x000000000062d300
5669c8e64bSWalter Erquinigo               _M_last = 0x000000000062d6f4
5769c8e64bSWalter Erquinigo               _M_node = 0x000000000062ced0
5869c8e64bSWalter Erquinigo            }
5969c8e64bSWalter Erquinigo         }
6069c8e64bSWalter Erquinigo      }
6169c8e64bSWalter Erquinigo   }
6269c8e64bSWalter Erquinigo
6369c8e64bSWalter Erquinigowhich is very hard to make sense of.
6469c8e64bSWalter Erquinigo
6569c8e64bSWalter ErquinigoNote: ``frame variable <var>`` prints out the variable ``<var>`` in the current
6669c8e64bSWalter Erquinigoframe.
6769c8e64bSWalter Erquinigo
6869c8e64bSWalter ErquinigoOn the other hand, a proper formatter is able to produce the following output:
6969c8e64bSWalter Erquinigo
7069c8e64bSWalter Erquinigo::
7169c8e64bSWalter Erquinigo
7269c8e64bSWalter Erquinigo   (lldb) frame variable a_deque
7369c8e64bSWalter Erquinigo   (std::deque<Foo, std::allocator<int> >) $0 = size=5 {
7469c8e64bSWalter Erquinigo      [0] = 2
7569c8e64bSWalter Erquinigo      [1] = 3
7669c8e64bSWalter Erquinigo      [2] = 4
7769c8e64bSWalter Erquinigo      [3] = 5
7869c8e64bSWalter Erquinigo      [4] = 6
7969c8e64bSWalter Erquinigo   }
8069c8e64bSWalter Erquinigo
8169c8e64bSWalter Erquinigowhich is what the user would expect from a good debugger.
8269c8e64bSWalter Erquinigo
8369c8e64bSWalter ErquinigoNote: you can also use ``v <var>`` instead of ``frame variable <var>``.
8469c8e64bSWalter Erquinigo
8569c8e64bSWalter ErquinigoIt's worth mentioning that the ``size=5`` string is produced by a summary
8669c8e64bSWalter Erquinigoprovider and the list of children is produced by a synthetic child provider.
8769c8e64bSWalter ErquinigoMore information about these providers is available later in this document.
8869c8e64bSWalter Erquinigo
8969c8e64bSWalter Erquinigo
90edb874b2SJonas DevlieghereThere are several features related to data visualization: formats, summaries,
91edb874b2SJonas Devliegherefilters, synthetic children.
92edb874b2SJonas Devlieghere
93edb874b2SJonas DevlieghereTo reflect this, the type command has five subcommands:
94edb874b2SJonas Devlieghere
95edb874b2SJonas Devlieghere::
96edb874b2SJonas Devlieghere
97edb874b2SJonas Devlieghere   type format
98edb874b2SJonas Devlieghere   type summary
99edb874b2SJonas Devlieghere   type filter
100edb874b2SJonas Devlieghere   type synthetic
101edb874b2SJonas Devlieghere   type category
102edb874b2SJonas Devlieghere
103edb874b2SJonas DevlieghereThese commands are meant to bind printing options to types. When variables are
104edb874b2SJonas Devlieghereprinted, LLDB will first check if custom printing options have been associated
105edb874b2SJonas Devlieghereto a variable's type and, if so, use them instead of picking the default
106edb874b2SJonas Devliegherechoices.
107edb874b2SJonas Devlieghere
108edb874b2SJonas DevlieghereEach of the commands (except ``type category``) has four subcommands available:
109edb874b2SJonas Devlieghere
110edb874b2SJonas Devlieghere- ``add``: associates a new printing option to one or more types
111edb874b2SJonas Devlieghere- ``delete``: deletes an existing association
112edb874b2SJonas Devlieghere- ``list``: provides a listing of all associations
113edb874b2SJonas Devlieghere- ``clear``: deletes all associations
114edb874b2SJonas Devlieghere
115edb874b2SJonas DevlieghereType Format
116edb874b2SJonas Devlieghere-----------
117edb874b2SJonas Devlieghere
118edb874b2SJonas DevlieghereType formats enable you to quickly override the default format for displaying
119edb874b2SJonas Devlieghereprimitive types (the usual basic C/C++/ObjC types: int, float, char, ...).
120edb874b2SJonas Devlieghere
121edb874b2SJonas DevlieghereIf for some reason you want all int variables in your program to print out as
122edb874b2SJonas Devliegherehex, you can add a format to the int type.
123edb874b2SJonas Devlieghere
124edb874b2SJonas DevlieghereThis is done by typing
125edb874b2SJonas Devlieghere
126edb874b2SJonas Devlieghere::
127edb874b2SJonas Devlieghere
128edb874b2SJonas Devlieghere   (lldb) type format add --format hex int
129edb874b2SJonas Devlieghere
130edb874b2SJonas Devlieghereat the LLDB command line.
131edb874b2SJonas Devlieghere
1329ce82a10SAuthor: Eddie PhillipsThe ``--format`` (which you can shorten to -f) option accepts a `format
1339ce82a10SAuthor: Eddie Phillipsname`_. Then, you provide one or more types to which you want the
134edb874b2SJonas Devliegherenew format applied.
135edb874b2SJonas Devlieghere
136edb874b2SJonas DevlieghereA frequent scenario is that your program has a typedef for a numeric type that
137edb874b2SJonas Devlieghereyou know represents something that must be printed in a certain way. Again, you
138edb874b2SJonas Devliegherecan add a format just to that typedef by using type format add with the name
139edb874b2SJonas Devliegherealias.
140edb874b2SJonas Devlieghere
141edb874b2SJonas DevlieghereBut things can quickly get hierarchical. Let's say you have a situation like
142edb874b2SJonas Devliegherethe following:
143edb874b2SJonas Devlieghere
144edb874b2SJonas Devlieghere::
145edb874b2SJonas Devlieghere
146edb874b2SJonas Devlieghere   typedef int A;
147edb874b2SJonas Devlieghere   typedef A B;
148edb874b2SJonas Devlieghere   typedef B C;
149edb874b2SJonas Devlieghere   typedef C D;
150edb874b2SJonas Devlieghere
151edb874b2SJonas Devlieghereand you want to show all A's as hex, all C's as byte arrays and leave the
152edb874b2SJonas Devliegheredefaults untouched for other types (albeit its contrived look, the example is
153edb874b2SJonas Devliegherefar from unrealistic in large software systems).
154edb874b2SJonas Devlieghere
155edb874b2SJonas DevlieghereIf you simply type
156edb874b2SJonas Devlieghere
157edb874b2SJonas Devlieghere::
158edb874b2SJonas Devlieghere
159edb874b2SJonas Devlieghere   (lldb) type format add -f hex A
160edb874b2SJonas Devlieghere   (lldb) type format add -f uint8_t[] C
161edb874b2SJonas Devlieghere
162edb874b2SJonas Devliegherevalues of type B will be shown as hex and values of type D as byte arrays, as in:
163edb874b2SJonas Devlieghere
164edb874b2SJonas Devlieghere::
165edb874b2SJonas Devlieghere
166edb874b2SJonas Devlieghere   (lldb) frame variable -T
167edb874b2SJonas Devlieghere   (A) a = 0x00000001
168edb874b2SJonas Devlieghere   (B) b = 0x00000002
169edb874b2SJonas Devlieghere   (C) c = {0x03 0x00 0x00 0x00}
170edb874b2SJonas Devlieghere   (D) d = {0x04 0x00 0x00 0x00}
171edb874b2SJonas Devlieghere
172edb874b2SJonas DevlieghereThis is because by default LLDB cascades formats through typedef chains. In
173edb874b2SJonas Devlieghereorder to avoid that you can use the option -C no to prevent cascading, thus
174edb874b2SJonas Devliegheremaking the two commands required to achieve your goal:
175edb874b2SJonas Devlieghere
176edb874b2SJonas Devlieghere::
177edb874b2SJonas Devlieghere
178edb874b2SJonas Devlieghere   (lldb) type format add -C no -f hex A
179edb874b2SJonas Devlieghere   (lldb) type format add -C no -f uint8_t[] C
180edb874b2SJonas Devlieghere
181edb874b2SJonas Devlieghere
182edb874b2SJonas Devliegherewhich provides the desired output:
183edb874b2SJonas Devlieghere
184edb874b2SJonas Devlieghere::
185edb874b2SJonas Devlieghere
186edb874b2SJonas Devlieghere   (lldb) frame variable -T
187edb874b2SJonas Devlieghere   (A) a = 0x00000001
188edb874b2SJonas Devlieghere   (B) b = 2
189edb874b2SJonas Devlieghere   (C) c = {0x03 0x00 0x00 0x00}
190edb874b2SJonas Devlieghere   (D) d = 4
191edb874b2SJonas Devlieghere
19279ac5bbbSShafik YaghmourNote, that qualifiers such as const and volatile will be stripped when matching types for example:
19379ac5bbbSShafik Yaghmour
19479ac5bbbSShafik Yaghmour::
19579ac5bbbSShafik Yaghmour
19679ac5bbbSShafik Yaghmour   (lldb) frame var x y z
19779ac5bbbSShafik Yaghmour   (int) x = 1
19879ac5bbbSShafik Yaghmour   (const int) y = 2
19979ac5bbbSShafik Yaghmour   (volatile int) z = 4
20079ac5bbbSShafik Yaghmour   (lldb) type format add -f hex int
20179ac5bbbSShafik Yaghmour   (lldb) frame var x y z
20279ac5bbbSShafik Yaghmour   (int) x = 0x00000001
20379ac5bbbSShafik Yaghmour   (const int) y = 0x00000002
20479ac5bbbSShafik Yaghmour   (volatile int) z = 0x00000004
20579ac5bbbSShafik Yaghmour
206edb874b2SJonas DevlieghereTwo additional options that you will want to look at are --skip-pointers (-p)
207edb874b2SJonas Devlieghereand --skip-references (-r). These two options prevent LLDB from applying a
208edb874b2SJonas Devlieghereformat for type T to values of type T* and T& respectively.
209edb874b2SJonas Devlieghere
210edb874b2SJonas Devlieghere::
211edb874b2SJonas Devlieghere
212edb874b2SJonas Devlieghere   (lldb) type format add -f float32[] int
213edb874b2SJonas Devlieghere   (lldb) frame variable pointer *pointer -T
214edb874b2SJonas Devlieghere   (int *) pointer = {1.46991e-39 1.4013e-45}
215edb874b2SJonas Devlieghere   (int) *pointer = {1.53302e-42}
216edb874b2SJonas Devlieghere   (lldb) type format add -f float32[] int -p
217edb874b2SJonas Devlieghere   (lldb) frame variable pointer *pointer -T
218edb874b2SJonas Devlieghere   (int *) pointer = 0x0000000100100180
219edb874b2SJonas Devlieghere   (int) *pointer = {1.53302e-42}
220edb874b2SJonas Devlieghere
221edb874b2SJonas DevlieghereWhile they can be applied to pointers and references, formats will make no
222edb874b2SJonas Devlieghereattempt to dereference the pointer and extract the value before applying the
223edb874b2SJonas Devlieghereformat, which means you are effectively formatting the address stored in the
224edb874b2SJonas Devliegherepointer rather than the pointee value. For this reason, you may want to use the
225edb874b2SJonas Devlieghere-p option when defining formats.
226edb874b2SJonas Devlieghere
227edb874b2SJonas DevlieghereIf you need to delete a custom format simply type type format delete followed
228edb874b2SJonas Devlieghereby the name of the type to which the format applies.Even if you defined the
229edb874b2SJonas Devliegheresame format for multiple types on the same command, type format delete will
230edb874b2SJonas Devlieghereonly remove the format for the type name passed as argument.
231edb874b2SJonas Devlieghere
232edb874b2SJonas DevlieghereTo delete ALL formats, use ``type format clear``. To see all the formats
233edb874b2SJonas Devliegheredefined, use type format list.
234edb874b2SJonas Devlieghere
235edb874b2SJonas DevlieghereIf all you need to do, however, is display one variable in a custom format,
236edb874b2SJonas Devliegherewhile leaving the others of the same type untouched, you can simply type:
237edb874b2SJonas Devlieghere
238edb874b2SJonas Devlieghere::
239edb874b2SJonas Devlieghere
240edb874b2SJonas Devlieghere   (lldb) frame variable counter -f hex
241edb874b2SJonas Devlieghere
242edb874b2SJonas DevlieghereThis has the effect of displaying the value of counter as an hexadecimal
243edb874b2SJonas Devliegherenumber, and will keep showing it this way until you either pick a different
244edb874b2SJonas Devlieghereformat or till you let your program run again.
245edb874b2SJonas Devlieghere
246edb874b2SJonas DevlieghereFinally, this is a list of formatting options available out of which you can
247edb874b2SJonas Devliegherepick:
248edb874b2SJonas Devlieghere
2499ce82a10SAuthor: Eddie Phillips.. _`format name`:
2509ce82a10SAuthor: Eddie Phillips
251edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
252edb874b2SJonas Devlieghere| **Format name**                               | **Abbreviation** | **Description**                                                          |
253edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
254edb874b2SJonas Devlieghere| ``default``                                   |                  | the default LLDB algorithm is used to pick a format                      |
255edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
256edb874b2SJonas Devlieghere| ``boolean``                                   | B                | show this as a true/false boolean, using the customary rule that 0 is    |
257edb874b2SJonas Devlieghere|                                               |                  | false and everything else is true                                        |
258edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
259edb874b2SJonas Devlieghere| ``binary``                                    | b                | show this as a sequence of bits                                          |
260edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
261edb874b2SJonas Devlieghere| ``bytes``                                     | y                | show the bytes one after the other                                       |
262edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
263edb874b2SJonas Devlieghere| ``bytes with ASCII``                          | Y                | show the bytes, but try to display them as ASCII characters as well      |
264edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
265edb874b2SJonas Devlieghere| ``character``                                 | c                | show the bytes as ASCII characters                                       |
266edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
267edb874b2SJonas Devlieghere| ``printable character``                       | C                | show the bytes as printable ASCII characters                             |
268edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
269edb874b2SJonas Devlieghere| ``complex float``                             | F                | interpret this value as the real and imaginary part of a complex         |
270edb874b2SJonas Devlieghere|                                               |                  | floating-point number                                                    |
271edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
272edb874b2SJonas Devlieghere| ``c-string``                                  | s                | show this as a 0-terminated C string                                     |
273edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
274c2cd84bcSJonas Devlieghere| ``decimal``                                   | d                | show this as a signed integer number (this does not perform a cast, it   |
275edb874b2SJonas Devlieghere|                                               |                  | simply shows the bytes as  an integer with sign)                         |
276edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
277edb874b2SJonas Devlieghere| ``enumeration``                               | E                | show this as an enumeration, printing the                                |
278edb874b2SJonas Devlieghere|                                               |                  | value's name if available or the integer value otherwise                 |
279edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
280edb874b2SJonas Devlieghere| ``hex``                                       | x                | show this as in hexadecimal notation (this does                          |
281edb874b2SJonas Devlieghere|                                               |                  | not perform a cast, it simply shows the bytes as hex)                    |
282edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
283edb874b2SJonas Devlieghere| ``float``                                     | f                | show this as a floating-point number (this does not perform a cast, it   |
284edb874b2SJonas Devlieghere|                                               |                  | simply interprets the bytes as an IEEE754 floating-point value)          |
285edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
286edb874b2SJonas Devlieghere| ``octal``                                     | o                | show this in octal notation                                              |
287edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
288edb874b2SJonas Devlieghere| ``OSType``                                    | O                | show this as a MacOS OSType                                              |
289edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
290edb874b2SJonas Devlieghere| ``unicode16``                                 | U                | show this as UTF-16 characters                                           |
291edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
292edb874b2SJonas Devlieghere| ``unicode32``                                 |                  | show this as UTF-32 characters                                           |
293edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
294edb874b2SJonas Devlieghere| ``unsigned decimal``                          | u                | show this as an unsigned integer number (this does not perform a cast,   |
295edb874b2SJonas Devlieghere|                                               |                  | it simply shows the bytes as unsigned integer)                           |
296edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
297edb874b2SJonas Devlieghere| ``pointer``                                   | p                | show this as a native pointer (unless this is really a pointer, the      |
298edb874b2SJonas Devlieghere|                                               |                  | resulting address will probably be invalid)                              |
299edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
300edb874b2SJonas Devlieghere| ``char[]``                                    |                  | show this as an array of characters                                      |
301edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
302edb874b2SJonas Devlieghere| ``int8_t[], uint8_t[]``                       |                  | show this as an array of the corresponding integer type                  |
303edb874b2SJonas Devlieghere| ``int16_t[], uint16_t[]``                     |                  |                                                                          |
304edb874b2SJonas Devlieghere| ``int32_t[], uint32_t[]``                     |                  |                                                                          |
305edb874b2SJonas Devlieghere| ``int64_t[], uint64_t[]``                     |                  |                                                                          |
306edb874b2SJonas Devlieghere| ``uint128_t[]``                               |                  |                                                                          |
307edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
308edb874b2SJonas Devlieghere| ``float32[], float64[]``                      |                  | show this as an array of the corresponding                               |
309edb874b2SJonas Devlieghere|                                               |                  |                       floating-point type                                |
310edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
311edb874b2SJonas Devlieghere| ``complex integer``                           | I                | interpret this value as the real and imaginary part of a complex integer |
312edb874b2SJonas Devlieghere|                                               |                  | number                                                                   |
313edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
314edb874b2SJonas Devlieghere| ``character array``                           | a                | show this as a character array                                           |
315edb874b2SJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
316c2cd84bcSJonas Devlieghere| ``address``                                   | A                | show this as an address target (symbol/file/line + offset), possibly     |
317c2cd84bcSJonas Devlieghere|                                               |                  | also the string this address is pointing to                              |
318c2cd84bcSJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
319c2cd84bcSJonas Devlieghere| ``hex float``                                 |                  | show this as hexadecimal floating point                                  |
320c2cd84bcSJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
321c2cd84bcSJonas Devlieghere| ``instruction``                               | i                | show this as an disassembled opcode                                      |
322c2cd84bcSJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
323c2cd84bcSJonas Devlieghere| ``void``                                      | v                | don't show anything                                                      |
324c2cd84bcSJonas Devlieghere+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
325edb874b2SJonas Devlieghere
326edb874b2SJonas DevlieghereType Summary
327edb874b2SJonas Devlieghere------------
328edb874b2SJonas Devlieghere
329edb874b2SJonas DevlieghereType formats work by showing a different kind of display for the value of a
330edb874b2SJonas Devliegherevariable. However, they only work for basic types. When you want to display a
331edb874b2SJonas Devlieghereclass or struct in a custom format, you cannot do that using formats.
332edb874b2SJonas Devlieghere
333edb874b2SJonas DevlieghereA different feature, type summaries, works by extracting information from
334edb874b2SJonas Devlieghereclasses, structures, ... (aggregate types) and arranging it in a user-defined
335edb874b2SJonas Devlieghereformat, as in the following example:
336edb874b2SJonas Devlieghere
337edb874b2SJonas Devliegherebefore adding a summary...
338edb874b2SJonas Devlieghere
339edb874b2SJonas Devlieghere::
340edb874b2SJonas Devlieghere
341edb874b2SJonas Devlieghere   (lldb) frame variable -T one
342edb874b2SJonas Devlieghere   (i_am_cool) one = {
343edb874b2SJonas Devlieghere      (int) x = 3
344edb874b2SJonas Devlieghere      (float) y = 3.14159
345edb874b2SJonas Devlieghere      (char) z = 'E'
346edb874b2SJonas Devlieghere   }
347edb874b2SJonas Devlieghere
348edb874b2SJonas Devlieghereafter adding a summary...
349edb874b2SJonas Devlieghere
350edb874b2SJonas Devlieghere::
351edb874b2SJonas Devlieghere
352edb874b2SJonas Devlieghere   (lldb) frame variable one
353edb874b2SJonas Devlieghere   (i_am_cool) one = int = 3, float = 3.14159, char = 69
354edb874b2SJonas Devlieghere
355edb874b2SJonas DevlieghereThere are two ways to use type summaries: the first one is to bind a summary
356edb874b2SJonas Devliegherestring to the type; the second is to write a Python script that returns the
357edb874b2SJonas Devliegherestring to be used as summary. Both options are enabled by the type summary add
358edb874b2SJonas Devliegherecommand.
359edb874b2SJonas Devlieghere
360edb874b2SJonas DevlieghereThe command to obtain the output shown in the example is:
361edb874b2SJonas Devlieghere
362edb874b2SJonas Devlieghere::
363edb874b2SJonas Devlieghere
364edb874b2SJonas Devlieghere(lldb) type summary add --summary-string "int = ${var.x}, float = ${var.y}, char = ${var.z%u}" i_am_cool
365edb874b2SJonas Devlieghere
366edb874b2SJonas DevlieghereInitially, we will focus on summary strings, and then describe the Python
367edb874b2SJonas Devliegherebinding mechanism.
368edb874b2SJonas Devlieghere
369edb874b2SJonas DevlieghereSummary Strings
370edb874b2SJonas Devlieghere---------------
371edb874b2SJonas Devlieghere
372edb874b2SJonas DevlieghereSummary strings are written using a simple control language, exemplified by the
373edb874b2SJonas Devliegheresnippet above. A summary string contains a sequence of tokens that are
374edb874b2SJonas Devlieghereprocessed by LLDB to generate the summary.
375edb874b2SJonas Devlieghere
376edb874b2SJonas DevlieghereSummary strings can contain plain text, control characters and special
377edb874b2SJonas Devliegherevariables that have access to information about the current object and the
378edb874b2SJonas Devlieghereoverall program state.
379edb874b2SJonas Devlieghere
380edb874b2SJonas DevliegherePlain text is any sequence of characters that doesn't contain a ``{``, ``}``, ``$``,
381edb874b2SJonas Devlieghereor ``\`` character, which are the syntax control characters.
382edb874b2SJonas Devlieghere
383edb874b2SJonas DevlieghereThe special variables are found in between a "${" prefix, and end with a "}"
384edb874b2SJonas Devliegheresuffix. Variables can be a simple name or they can refer to complex objects
385edb874b2SJonas Devliegherethat have subitems themselves. In other words, a variable looks like
386edb874b2SJonas Devlieghere``${object}`` or ``${object.child.otherchild}``. A variable can also be
387edb874b2SJonas Devlieghereprefixed or suffixed with other symbols meant to change the way its value is
388edb874b2SJonas Devliegherehandled. An example is ``${*var.int_pointer[0-3]}``.
389edb874b2SJonas Devlieghere
390edb874b2SJonas DevlieghereBasically, the syntax is the same one described Frame and Thread Formatting
391edb874b2SJonas Devlieghereplus additional symbols specific for summary strings. The main of them is
392edb874b2SJonas Devlieghere${var, which is used refer to the variable that a summary is being created for.
393edb874b2SJonas Devlieghere
394edb874b2SJonas DevlieghereThe simplest thing you can do is grab a member variable of a class or structure
395edb874b2SJonas Devlieghereby typing its expression path. In the previous example, the expression path for
396edb874b2SJonas Devliegherethe field float y is simply .y. Thus, to ask the summary string to display y
397edb874b2SJonas Devlieghereyou would type ${var.y}.
398edb874b2SJonas Devlieghere
399edb874b2SJonas DevlieghereIf you have code like the following:
400edb874b2SJonas Devlieghere
401edb874b2SJonas Devlieghere::
402edb874b2SJonas Devlieghere
403edb874b2SJonas Devlieghere   struct A {
404edb874b2SJonas Devlieghere      int x;
405edb874b2SJonas Devlieghere      int y;
406edb874b2SJonas Devlieghere   };
407edb874b2SJonas Devlieghere   struct B {
408edb874b2SJonas Devlieghere      A x;
409edb874b2SJonas Devlieghere      A y;
410edb874b2SJonas Devlieghere      int *z;
411edb874b2SJonas Devlieghere   };
412edb874b2SJonas Devlieghere
413edb874b2SJonas Devliegherethe expression path for the y member of the x member of an object of type B
414edb874b2SJonas Devliegherewould be .x.y and you would type ``${var.x.y}`` to display it in a summary
415edb874b2SJonas Devliegherestring for type B.
416edb874b2SJonas Devlieghere
417edb874b2SJonas DevlieghereBy default, a summary defined for type T, also works for types T* and T& (you
418edb874b2SJonas Devliegherecan disable this behavior if desired). For this reason, expression paths do not
419edb874b2SJonas Devliegheredifferentiate between . and ->, and the above expression path .x.y would be
420edb874b2SJonas Devliegherejust as good if you were displaying a B*, or even if the actual definition of B
421edb874b2SJonas Devliegherewere:
422edb874b2SJonas Devlieghere
423edb874b2SJonas Devlieghere::
424edb874b2SJonas Devlieghere
425edb874b2SJonas Devlieghere   struct B {
426edb874b2SJonas Devlieghere      A *x;
427edb874b2SJonas Devlieghere      A y;
428edb874b2SJonas Devlieghere      int *z;
429edb874b2SJonas Devlieghere   };
430edb874b2SJonas Devlieghere
431edb874b2SJonas DevlieghereThis is unlike the behavior of frame variable which, on the contrary, will
432edb874b2SJonas Devlieghereenforce the distinction. As hinted above, the rationale for this choice is that
433edb874b2SJonas Devliegherewaiving this distinction enables you to write a summary string once for type T
434edb874b2SJonas Devlieghereand use it for both T and T* instances. As a summary string is mostly about
435edb874b2SJonas Devlieghereextracting nested members' information, a pointer to an object is just as good
436edb874b2SJonas Devlieghereas the object itself for the purpose.
437edb874b2SJonas Devlieghere
438edb874b2SJonas DevlieghereIf you need to access the value of the integer pointed to by B::z, you cannot
439edb874b2SJonas Devliegheresimply say ${var.z} because that symbol refers to the pointer z. In order to
440edb874b2SJonas Devliegheredereference it and get the pointed value, you should say ``${*var.z}``. The
441edb874b2SJonas Devlieghere``${*var`` tells LLDB to get the object that the expression paths leads to, and
442edb874b2SJonas Devliegherethen dereference it. In this example is it equivalent to ``*(bObject.z)`` in
443a93aa534SJonas DevlieghereC/C++ syntax. Because ``.`` and ``->`` operators can both be used, there is no
444a93aa534SJonas Devlieghereneed to have dereferences in the middle of an expression path (e.g. you do not
445a93aa534SJonas Devlieghereneed to type ``${*(var.x).x}``) to read A::x as contained in ``*(B::x)``. To
446a93aa534SJonas Devlieghereachieve that effect you can simply write ``${var.x->x}``, or even
447a93aa534SJonas Devlieghere``${var.x.x}``. The ``*`` operator only binds to the result of the whole
448a93aa534SJonas Devlieghereexpression path, rather than piecewise, and there is no way to use parentheses
449a93aa534SJonas Devlieghereto change that behavior.
450edb874b2SJonas Devlieghere
451edb874b2SJonas DevlieghereOf course, a summary string can contain more than one ${var specifier, and can
452edb874b2SJonas Devlieghereuse ``${var`` and ``${*var`` specifiers together.
453edb874b2SJonas Devlieghere
454edb874b2SJonas DevlieghereFormatting Summary Elements
455edb874b2SJonas Devlieghere---------------------------
456edb874b2SJonas Devlieghere
457edb874b2SJonas DevlieghereAn expression path can include formatting codes. Much like the type formats
458edb874b2SJonas Devliegherediscussed previously, you can also customize the way variables are displayed in
459edb874b2SJonas Devliegheresummary strings, regardless of the format they have applied to their types. To
460edb874b2SJonas Devliegheredo that, you can use %format inside an expression path, as in ${var.x->x%u},
461edb874b2SJonas Devliegherewhich would display the value of x as an unsigned integer.
462edb874b2SJonas Devlieghere
4638530b1c4SDave LeeAdditionally, custom output can be achieved by using an LLVM format string,
4648530b1c4SDave Leecommencing with the ``:`` marker. To illustrate, compare ``${var.byte%x}`` and
4658530b1c4SDave Lee``${var.byte:x-}``. The former uses lldb's builtin hex formatting (``x``),
4668530b1c4SDave Leewhich unconditionally inserts a ``0x`` prefix, and also zero pads the value to
4678530b1c4SDave Leematch the size of the type. The latter uses ``llvm::formatv`` formatting
4688530b1c4SDave Lee(``:x-``), and will print only the hex value, with no ``0x`` prefix, and no
4698530b1c4SDave Leepadding. This raw control is useful when composing multiple pieces into a
4708530b1c4SDave Leelarger whole.
4718530b1c4SDave Lee
472edb874b2SJonas DevlieghereYou can also use some other special format markers, not available for formats
473edb874b2SJonas Devliegherethemselves, but which carry a special meaning when used in this context:
474edb874b2SJonas Devlieghere
475edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
476edb874b2SJonas Devlieghere| **Symbol** | **Description**                                                          |
477edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
478edb874b2SJonas Devlieghere| ``Symbol`` | ``Description``                                                          |
479edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
480edb874b2SJonas Devlieghere| ``%S``     | Use this object's summary (the default for aggregate types)              |
481edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
482edb874b2SJonas Devlieghere| ``%V``     | Use this object's value (the default for non-aggregate types)            |
483edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
484edb874b2SJonas Devlieghere| ``%@``     | Use a language-runtime specific description (for C++ this does nothing,  |
485edb874b2SJonas Devlieghere|            |                     for Objective-C it calls the NSPrintForDebugger API) |
486edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
487edb874b2SJonas Devlieghere| ``%L``     | Use this object's location (memory address, register name, ...)          |
488edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
489edb874b2SJonas Devlieghere| ``%#``     | Use the count of the children of this object                             |
490edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
491edb874b2SJonas Devlieghere| ``%T``     | Use this object's datatype name                                          |
492edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
493edb874b2SJonas Devlieghere| ``%N``     | Print the variable's basename                                            |
494edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
495edb874b2SJonas Devlieghere| ``%>``     | Print the expression path for this item                                  |
496edb874b2SJonas Devlieghere+------------+--------------------------------------------------------------------------+
497edb874b2SJonas Devlieghere
498a6f1d046SDave LeeSince lldb 3.7.0, you can also specify ``${script.var:pythonFuncName}``.
499edb874b2SJonas Devlieghere
500edb874b2SJonas DevlieghereIt is expected that the function name you use specifies a function whose
501edb874b2SJonas Devliegheresignature is the same as a Python summary function. The return string from the
502edb874b2SJonas Devliegherefunction will be placed verbatim in the output.
503edb874b2SJonas Devlieghere
504edb874b2SJonas DevlieghereYou cannot use element access, or formatting symbols, in combination with this
505edb874b2SJonas Devliegheresyntax. For example the following:
506edb874b2SJonas Devlieghere
507edb874b2SJonas Devlieghere::
508edb874b2SJonas Devlieghere
509edb874b2SJonas Devlieghere   ${script.var.element[0]:myFunctionName%@}
510edb874b2SJonas Devlieghere
511edb874b2SJonas Devlieghereis not valid and will cause the summary to fail to evaluate.
512edb874b2SJonas Devlieghere
513edb874b2SJonas Devlieghere
514edb874b2SJonas DevlieghereElement Inlining
515edb874b2SJonas Devlieghere----------------
516edb874b2SJonas Devlieghere
517edb874b2SJonas DevlieghereOption --inline-children (-c) to type summary add tells LLDB not to look for a summary string, but instead to just print a listing of all the object's children on one line.
518edb874b2SJonas Devlieghere
519edb874b2SJonas DevlieghereAs an example, given a type pair:
520edb874b2SJonas Devlieghere
521edb874b2SJonas Devlieghere::
522edb874b2SJonas Devlieghere
523edb874b2SJonas Devlieghere   (lldb) frame variable --show-types a_pair
524edb874b2SJonas Devlieghere   (pair) a_pair = {
525edb874b2SJonas Devlieghere      (int) first = 1;
526edb874b2SJonas Devlieghere      (int) second = 2;
527edb874b2SJonas Devlieghere   }
528edb874b2SJonas Devlieghere
529edb874b2SJonas DevlieghereIf one types the following commands:
530edb874b2SJonas Devlieghere
531edb874b2SJonas Devlieghere::
532edb874b2SJonas Devlieghere
533edb874b2SJonas Devlieghere   (lldb) type summary add --inline-children pair
534edb874b2SJonas Devlieghere
535edb874b2SJonas Devliegherethe output becomes:
536edb874b2SJonas Devlieghere
537edb874b2SJonas Devlieghere::
538edb874b2SJonas Devlieghere
539edb874b2SJonas Devlieghere   (lldb) frame variable a_pair
540edb874b2SJonas Devlieghere   (pair) a_pair = (first=1, second=2)
541edb874b2SJonas Devlieghere
542edb874b2SJonas Devlieghere
543edb874b2SJonas DevlieghereOf course, one can obtain the same effect by typing
544edb874b2SJonas Devlieghere
545edb874b2SJonas Devlieghere::
546edb874b2SJonas Devlieghere
547edb874b2SJonas Devlieghere   (lldb) type summary add pair --summary-string "(first=${var.first}, second=${var.second})"
548edb874b2SJonas Devlieghere
549edb874b2SJonas DevlieghereWhile the final result is the same, using --inline-children can often save
550edb874b2SJonas Devliegheretime. If one does not need to see the names of the variables, but just their
551edb874b2SJonas Devliegherevalues, the option --omit-names (-O, uppercase letter o), can be combined with
552edb874b2SJonas Devlieghere--inline-children to obtain:
553edb874b2SJonas Devlieghere
554edb874b2SJonas Devlieghere::
555edb874b2SJonas Devlieghere
556edb874b2SJonas Devlieghere   (lldb) frame variable a_pair
557edb874b2SJonas Devlieghere   (pair) a_pair = (1, 2)
558edb874b2SJonas Devlieghere
559edb874b2SJonas Devliegherewhich is of course the same as typing
560edb874b2SJonas Devlieghere
561edb874b2SJonas Devlieghere::
562edb874b2SJonas Devlieghere
563edb874b2SJonas Devlieghere   (lldb) type summary add pair --summary-string "(${var.first}, ${var.second})"
564edb874b2SJonas Devlieghere
565edb874b2SJonas DevlieghereBitfields And Array Syntax
566edb874b2SJonas Devlieghere--------------------------
567edb874b2SJonas Devlieghere
568edb874b2SJonas DevlieghereSometimes, a basic type's value actually represents several different values
569edb874b2SJonas Devliegherepacked together in a bitfield.
570edb874b2SJonas Devlieghere
571edb874b2SJonas DevlieghereWith the classical view, there is no way to look at them. Hexadecimal display
572edb874b2SJonas Devliegherecan help, but if the bits actually span nibble boundaries, the help is limited.
573edb874b2SJonas Devlieghere
574edb874b2SJonas DevlieghereBinary view would show it all without ambiguity, but is often too detailed and
575edb874b2SJonas Devliegherehard to read for real-life scenarios.
576edb874b2SJonas Devlieghere
577edb874b2SJonas DevlieghereTo cope with the issue, LLDB supports native bitfield formatting in summary
578edb874b2SJonas Devliegherestrings. If your expression paths leads to a so-called scalar type (the usual
579edb874b2SJonas Devlieghereint, float, char, double, short, long, long long, double, long double and
580edb874b2SJonas Devlieghereunsigned variants), you can ask LLDB to only grab some bits out of the value
581edb874b2SJonas Devlieghereand display them in any format you like. If you only need one bit you can use
582edb874b2SJonas Devliegherethe [n], just like indexing an array. To extract multiple bits, you can use a
583edb874b2SJonas Devlieghereslice-like syntax: [n-m], e.g.
584edb874b2SJonas Devlieghere
585edb874b2SJonas Devlieghere::
586edb874b2SJonas Devlieghere
587edb874b2SJonas Devlieghere   (lldb) frame variable float_point
588edb874b2SJonas Devlieghere   (float) float_point = -3.14159
589edb874b2SJonas Devlieghere
590edb874b2SJonas Devlieghere::
591edb874b2SJonas Devlieghere
592edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "Sign: ${var[31]%B} Exponent: ${var[30-23]%x} Mantissa: ${var[0-22]%u}" float
593edb874b2SJonas Devlieghere   (lldb) frame variable float_point
594edb874b2SJonas Devlieghere   (float) float_point = -3.14159 Sign: true Exponent: 0x00000080 Mantissa: 4788184
595edb874b2SJonas Devlieghere
596edb874b2SJonas DevlieghereIn this example, LLDB shows the internal representation of a float variable by
597edb874b2SJonas Devlieghereextracting bitfields out of a float object.
598edb874b2SJonas Devlieghere
599edb874b2SJonas DevlieghereWhen typing a range, the extremes n and m are always included, and the order of
600edb874b2SJonas Devliegherethe indices is irrelevant.
601edb874b2SJonas Devlieghere
602edb874b2SJonas DevlieghereLLDB also allows to use a similar syntax to display array members inside a summary string. For instance, you may want to display all arrays of a given type using a more compact notation than the default, and then just delve into individual array members that prove interesting to your debugging task. You can tell LLDB to format arrays in special ways, possibly independent of the way the array members' datatype is formatted.
603edb874b2SJonas Devliegheree.g.
604edb874b2SJonas Devlieghere
605edb874b2SJonas Devlieghere::
606edb874b2SJonas Devlieghere
607edb874b2SJonas Devlieghere   (lldb) frame variable sarray
608edb874b2SJonas Devlieghere   (Simple [3]) sarray = {
609edb874b2SJonas Devlieghere      [0] = {
610edb874b2SJonas Devlieghere         x = 1
611edb874b2SJonas Devlieghere         y = 2
612edb874b2SJonas Devlieghere         z = '\x03'
613edb874b2SJonas Devlieghere      }
614edb874b2SJonas Devlieghere      [1] = {
615edb874b2SJonas Devlieghere         x = 4
616edb874b2SJonas Devlieghere         y = 5
617edb874b2SJonas Devlieghere         z = '\x06'
618edb874b2SJonas Devlieghere      }
619edb874b2SJonas Devlieghere      [2] = {
620edb874b2SJonas Devlieghere         x = 7
621edb874b2SJonas Devlieghere         y = 8
622edb874b2SJonas Devlieghere         z = '\t'
623edb874b2SJonas Devlieghere      }
624edb874b2SJonas Devlieghere   }
625edb874b2SJonas Devlieghere
626edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var[].x}" "Simple [3]"
627edb874b2SJonas Devlieghere
628edb874b2SJonas Devlieghere   (lldb) frame variable sarray
629edb874b2SJonas Devlieghere   (Simple [3]) sarray = [1,4,7]
630edb874b2SJonas Devlieghere
631edb874b2SJonas DevlieghereThe [] symbol amounts to: if var is an array and I know its size, apply this summary string to every element of the array. Here, we are asking LLDB to display .x for every element of the array, and in fact this is what happens. If you find some of those integers anomalous, you can then inspect that one item in greater detail, without the array format getting in the way:
632edb874b2SJonas Devlieghere
633edb874b2SJonas Devlieghere::
634edb874b2SJonas Devlieghere
635edb874b2SJonas Devlieghere   (lldb) frame variable sarray[1]
636edb874b2SJonas Devlieghere   (Simple) sarray[1] = {
637edb874b2SJonas Devlieghere      x = 4
638edb874b2SJonas Devlieghere      y = 5
639edb874b2SJonas Devlieghere      z = '\x06'
640edb874b2SJonas Devlieghere   }
641edb874b2SJonas Devlieghere
642edb874b2SJonas DevlieghereYou can also ask LLDB to only print a subset of the array range by using the
643edb874b2SJonas Devliegheresame syntax used to extract bit for bitfields:
644edb874b2SJonas Devlieghere
645edb874b2SJonas Devlieghere::
646edb874b2SJonas Devlieghere
647edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var[1-2].x}" "Simple [3]"
648edb874b2SJonas Devlieghere
649edb874b2SJonas Devlieghere   (lldb) frame variable sarray
650edb874b2SJonas Devlieghere   (Simple [3]) sarray = [4,7]
651edb874b2SJonas Devlieghere
652edb874b2SJonas DevlieghereIf you are dealing with a pointer that you know is an array, you can use this
653edb874b2SJonas Devliegheresyntax to display the elements contained in the pointed array instead of just
654edb874b2SJonas Devliegherethe pointer value. However, because pointers have no notion of their size, the
655edb874b2SJonas Devlieghereempty brackets [] operator does not work, and you must explicitly provide
656edb874b2SJonas Devliegherehigher and lower bounds.
657edb874b2SJonas Devlieghere
658a93aa534SJonas DevlieghereIn general, LLDB needs the square brackets ``operator []`` in order to handle
659edb874b2SJonas Devliegherearrays and pointers correctly, and for pointers it also needs a range. However,
660edb874b2SJonas Devliegherea few special cases are defined to make your life easier:
661edb874b2SJonas Devlieghere
662edb874b2SJonas Devlieghereyou can print a 0-terminated string (C-string) using the %s format, omitting
663edb874b2SJonas Devliegheresquare brackets, as in:
664edb874b2SJonas Devlieghere
665edb874b2SJonas Devlieghere::
666edb874b2SJonas Devlieghere
667edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var%s}" "char *"
668edb874b2SJonas Devlieghere
669edb874b2SJonas DevlieghereThis syntax works for char* as well as for char[] because LLDB can rely on the
670edb874b2SJonas Devliegherefinal \0 terminator to know when the string has ended.
671edb874b2SJonas Devlieghere
672edb874b2SJonas DevlieghereLLDB has default summary strings for char* and char[] that use this special
673edb874b2SJonas Devliegherecase. On debugger startup, the following are defined automatically:
674edb874b2SJonas Devlieghere
675edb874b2SJonas Devlieghere::
676edb874b2SJonas Devlieghere
677edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var%s}" "char *"
678edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var%s}" -x "char \[[0-9]+]"
679edb874b2SJonas Devlieghere
680edb874b2SJonas Devlieghereany of the array formats (int8_t[], float32{}, ...), and the y, Y and a formats
681edb874b2SJonas Devliegherework to print an array of a non-aggregate type, even if square brackets are
682edb874b2SJonas Devlieghereomitted.
683edb874b2SJonas Devlieghere
684edb874b2SJonas Devlieghere::
685edb874b2SJonas Devlieghere
686edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var%int32_t[]}" "int [10]"
687edb874b2SJonas Devlieghere
688edb874b2SJonas DevlieghereThis feature, however, is not enabled for pointers because there is no way for
689edb874b2SJonas DevlieghereLLDB to detect the end of the pointed data.
690edb874b2SJonas Devlieghere
691edb874b2SJonas DevlieghereThis also does not work for other formats (e.g. boolean), and you must specify
692edb874b2SJonas Devliegherethe square brackets operator to get the expected output.
693edb874b2SJonas Devlieghere
694edb874b2SJonas DevliegherePython Scripting
695edb874b2SJonas Devlieghere----------------
696edb874b2SJonas Devlieghere
697edb874b2SJonas DevlieghereMost of the times, summary strings prove good enough for the job of summarizing
698edb874b2SJonas Devliegherethe contents of a variable. However, as soon as you need to do more than
699edb874b2SJonas Devliegherepicking some values and rearranging them for display, summary strings stop
700edb874b2SJonas Devliegherebeing an effective tool. This is because summary strings lack the power to
701edb874b2SJonas Devlieghereactually perform any kind of computation on the value of variables.
702edb874b2SJonas Devlieghere
703edb874b2SJonas DevlieghereTo solve this issue, you can bind some Python scripting code as a summary for
704edb874b2SJonas Devlieghereyour datatype, and that script has the ability to both extract children
705edb874b2SJonas Devliegherevariables as the summary strings do and to perform active computation on the
706edb874b2SJonas Devlieghereextracted values. As a small example, let's say we have a Rectangle class:
707edb874b2SJonas Devlieghere
708edb874b2SJonas Devlieghere::
709edb874b2SJonas Devlieghere
710edb874b2SJonas Devlieghere
711edb874b2SJonas Devlieghere   class Rectangle
712edb874b2SJonas Devlieghere   {
713edb874b2SJonas Devlieghere   private:
714edb874b2SJonas Devlieghere      int height;
715edb874b2SJonas Devlieghere      int width;
716edb874b2SJonas Devlieghere   public:
717edb874b2SJonas Devlieghere      Rectangle() : height(3), width(5) {}
718edb874b2SJonas Devlieghere      Rectangle(int H) : height(H), width(H*2-1) {}
719edb874b2SJonas Devlieghere      Rectangle(int H, int W) : height(H), width(W) {}
720edb874b2SJonas Devlieghere      int GetHeight() { return height; }
721edb874b2SJonas Devlieghere      int GetWidth() { return width; }
722edb874b2SJonas Devlieghere   };
723edb874b2SJonas Devlieghere
724edb874b2SJonas DevlieghereSummary strings are effective to reduce the screen real estate used by the
725edb874b2SJonas Devliegheredefault viewing mode, but are not effective if we want to display the area and
726edb874b2SJonas Devlieghereperimeter of Rectangle objects
727edb874b2SJonas Devlieghere
728edb874b2SJonas DevlieghereTo obtain this, we can simply attach a small Python script to the Rectangle
729edb874b2SJonas Devlieghereclass, as shown in this example:
730edb874b2SJonas Devlieghere
731edb874b2SJonas Devlieghere::
732edb874b2SJonas Devlieghere
733edb874b2SJonas Devlieghere   (lldb) type summary add -P Rectangle
734edb874b2SJonas Devlieghere   Enter your Python command(s). Type 'DONE' to end.
7351287977bSJason Molenda   def function (valobj,internal_dict,options):
736edb874b2SJonas Devlieghere      height_val = valobj.GetChildMemberWithName('height')
737edb874b2SJonas Devlieghere      width_val = valobj.GetChildMemberWithName('width')
738edb874b2SJonas Devlieghere      height = height_val.GetValueAsUnsigned(0)
739edb874b2SJonas Devlieghere      width = width_val.GetValueAsUnsigned(0)
740edb874b2SJonas Devlieghere      area = height*width
741edb874b2SJonas Devlieghere      perimeter = 2*(height + width)
742edb874b2SJonas Devlieghere      return 'Area: ' + str(area) + ', Perimeter: ' + str(perimeter)
743edb874b2SJonas Devlieghere      DONE
744edb874b2SJonas Devlieghere   (lldb) frame variable
745edb874b2SJonas Devlieghere   (Rectangle) r1 = Area: 20, Perimeter: 18
746edb874b2SJonas Devlieghere   (Rectangle) r2 = Area: 72, Perimeter: 36
747edb874b2SJonas Devlieghere   (Rectangle) r3 = Area: 16, Perimeter: 16
748edb874b2SJonas Devlieghere
749edb874b2SJonas DevlieghereIn order to write effective summary scripts, you need to know the LLDB public
750edb874b2SJonas DevlieghereAPI, which is the way Python code can access the LLDB object model. For further
751edb874b2SJonas Devliegheredetails on the API you should look at the LLDB API reference documentation.
752edb874b2SJonas Devlieghere
753edb874b2SJonas Devlieghere
754edb874b2SJonas DevlieghereAs a brief introduction, your script is encapsulated into a function that is
755edb874b2SJonas Devliegherepassed two parameters: ``valobj`` and ``internal_dict``.
756edb874b2SJonas Devlieghere
757edb874b2SJonas Devlieghere``internal_dict`` is an internal support parameter used by LLDB and you should
758edb874b2SJonas Devliegherenot touch it.
759edb874b2SJonas Devlieghere
760edb874b2SJonas Devlieghere``valobj`` is the object encapsulating the actual variable being displayed, and
761a58aceffSRaphael Isemannits type is `SBValue`. Out of the many possible operations on an `SBValue`, the
762edb874b2SJonas Devliegherebasic one is retrieve the children objects it contains (essentially, the fields
763edb874b2SJonas Devlieghereof the object wrapped by it), by calling ``GetChildMemberWithName()``, passing
764edb874b2SJonas Devlieghereit the child's name as a string.
765edb874b2SJonas Devlieghere
766edb874b2SJonas DevlieghereIf the variable has a value, you can ask for it, and return it as a string
767edb874b2SJonas Devlieghereusing ``GetValue()``, or as a signed/unsigned number using
768edb874b2SJonas Devlieghere``GetValueAsSigned()``, ``GetValueAsUnsigned()``. It is also possible to
769a58aceffSRaphael Isemannretrieve an `SBData` object by calling ``GetData()`` and then read the object's
770a58aceffSRaphael Isemanncontents out of the `SBData`.
771edb874b2SJonas Devlieghere
772edb874b2SJonas DevlieghereIf you need to delve into several levels of hierarchy, as you can do with
773edb874b2SJonas Devliegheresummary strings, you can use the method ``GetValueForExpressionPath()``,
774edb874b2SJonas Devliegherepassing it an expression path just like those you could use for summary strings
775edb874b2SJonas Devlieghere(one of the differences is that dereferencing a pointer does not occur by
776edb874b2SJonas Devlieghereprefixing the path with a ``*```, but by calling the ``Dereference()`` method
777a58aceffSRaphael Isemannon the returned `SBValue`). If you need to access array slices, you cannot do
778edb874b2SJonas Devliegherethat (yet) via this method call, and you must use ``GetChildAtIndex()``
779edb874b2SJonas Devliegherequerying it for the array items one by one. Also, handling custom formats is
780edb874b2SJonas Devliegheresomething you have to deal with on your own.
781edb874b2SJonas Devlieghere
7821287977bSJason Molenda``options`` Python summary formatters can optionally define this
7831287977bSJason Molendathird argument, which is an object of type ``lldb.SBTypeSummaryOptions``,
7841287977bSJason Molendaallowing for a few customizations of the result. The decision to
78514d68630SJason Molendaadopt or not this third argument - and the meaning of options
78614d68630SJason Molendathereof - is up to the individual formatter's writer.
7871287977bSJason Molenda
788edb874b2SJonas DevlieghereOther than interactively typing a Python script there are two other ways for
789edb874b2SJonas Devlieghereyou to input a Python script as a summary:
790edb874b2SJonas Devlieghere
791edb874b2SJonas Devlieghere- using the --python-script option to type summary add and typing the script
792edb874b2SJonas Devlieghere  code as an option argument; as in:
793edb874b2SJonas Devlieghere
794edb874b2SJonas Devlieghere::
795edb874b2SJonas Devlieghere
796edb874b2SJonas Devlieghere   (lldb) type summary add --python-script "height = valobj.GetChildMemberWithName('height').GetValueAsUnsigned(0);width = valobj.GetChildMemberWithName('width').GetValueAsUnsigned(0); return 'Area: %d' % (height*width)" Rectangle
797edb874b2SJonas Devlieghere
798edb874b2SJonas Devlieghere
799edb874b2SJonas Devlieghere- using the --python-function (-F) option to type summary add and giving the
800edb874b2SJonas Devlieghere  name of a Python function with the correct prototype. Most probably, you will
801edb874b2SJonas Devlieghere  define (or have already defined) the function in the interactive interpreter,
802edb874b2SJonas Devlieghere  or somehow loaded it from a file, using the command script import command.
803edb874b2SJonas Devlieghere  LLDB will emit a warning if it is unable to find the function you passed, but
804edb874b2SJonas Devlieghere  will still register the binding.
805edb874b2SJonas Devlieghere
806edb874b2SJonas DevlieghereRegular Expression Typenames
807edb874b2SJonas Devlieghere----------------------------
808edb874b2SJonas Devlieghere
809edb874b2SJonas DevlieghereAs you noticed, in order to associate the custom summary string to the array
810edb874b2SJonas Devliegheretypes, one must give the array size as part of the typename. This can long
811edb874b2SJonas Devliegherebecome tiresome when using arrays of different sizes, Simple [3], Simple [9],
812edb874b2SJonas DevlieghereSimple [12], ...
813edb874b2SJonas Devlieghere
814edb874b2SJonas DevlieghereIf you use the -x option, type names are treated as regular expressions instead
815edb874b2SJonas Devlieghereof type names. This would let you rephrase the above example for arrays of type
816edb874b2SJonas DevlieghereSimple [3] as:
817edb874b2SJonas Devlieghere
818edb874b2SJonas Devlieghere::
819d36757b5SAdrian Prantl
820edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "${var[].x}" -x "Simple \[[0-9]+\]"
821edb874b2SJonas Devlieghere   (lldb) frame variable
822edb874b2SJonas Devlieghere   (Simple [3]) sarray = [1,4,7]
823edb874b2SJonas Devlieghere   (Simple [2]) sother = [3,6]
824edb874b2SJonas Devlieghere
825edb874b2SJonas DevlieghereThe above scenario works for Simple [3] as well as for any other array of
826edb874b2SJonas DevlieghereSimple objects.
827edb874b2SJonas Devlieghere
828edb874b2SJonas DevlieghereWhile this feature is mostly useful for arrays, you could also use regular
829edb874b2SJonas Devlieghereexpressions to catch other type sets grouped by name. However, as regular
830edb874b2SJonas Devlieghereexpression matching is slower than normal name matching, LLDB will first try to
831edb874b2SJonas Devliegherematch by name in any way it can, and only when this fails, will it resort to
832edb874b2SJonas Devlieghereregular expression matching.
833edb874b2SJonas Devlieghere
834edb874b2SJonas DevlieghereOne of the ways LLDB uses this feature internally, is to match the names of STL
835edb874b2SJonas Devliegherecontainer classes, regardless of the template arguments provided. The details
836edb874b2SJonas Devliegherefor this are found at FormatManager.cpp
837edb874b2SJonas Devlieghere
838edb874b2SJonas DevlieghereThe regular expression language used by LLDB is the POSIX extended language, as
83919ae9d01SAdrian Prantldefined by the Single UNIX Specification, of which macOS is a compliant
840edb874b2SJonas Devlieghereimplementation.
841edb874b2SJonas Devlieghere
842edb874b2SJonas DevlieghereNames Summaries
843edb874b2SJonas Devlieghere---------------
844edb874b2SJonas Devlieghere
845edb874b2SJonas DevlieghereFor a given type, there may be different meaningful summary representations.
846edb874b2SJonas DevlieghereHowever, currently, only one summary can be associated to a type at each
847edb874b2SJonas Devliegheremoment. If you need to temporarily override the association for a variable,
848edb874b2SJonas Devliegherewithout changing the summary string for to its type, you can use named
849edb874b2SJonas Devliegheresummaries.
850edb874b2SJonas Devlieghere
851edb874b2SJonas DevlieghereNamed summaries work by attaching a name to a summary when creating it. Then,
852edb874b2SJonas Devliegherewhen there is a need to attach the summary to a variable, the frame variable
853edb874b2SJonas Devliegherecommand, supports a --summary option that tells LLDB to use the named summary
854edb874b2SJonas Devliegheregiven instead of the default one.
855edb874b2SJonas Devlieghere
856edb874b2SJonas Devlieghere::
85742d65c57SJonas Devlieghere
858edb874b2SJonas Devlieghere   (lldb) type summary add --summary-string "x=${var.integer}" --name NamedSummary
859edb874b2SJonas Devlieghere   (lldb) frame variable one
860edb874b2SJonas Devlieghere   (i_am_cool) one = int = 3, float = 3.14159, char = 69
861edb874b2SJonas Devlieghere   (lldb) frame variable one --summary NamedSummary
862edb874b2SJonas Devlieghere   (i_am_cool) one = x=3
863edb874b2SJonas Devlieghere
864edb874b2SJonas DevlieghereWhen defining a named summary, binding it to one or more types becomes
865edb874b2SJonas Devlieghereoptional. Even if you bind the named summary to a type, and later change the
866edb874b2SJonas Devliegheresummary string for that type, the named summary will not be changed by that.
867edb874b2SJonas DevlieghereYou can delete named summaries by using the type summary delete command, as if
868edb874b2SJonas Devliegherethe summary name was the datatype that the summary is applied to
869edb874b2SJonas Devlieghere
870edb874b2SJonas DevlieghereA summary attached to a variable using the --summary option, has the same
871edb874b2SJonas Devliegheresemantics that a custom format attached using the -f option has: it stays
872edb874b2SJonas Devlieghereattached till you attach a new one, or till you let your program run again.
873edb874b2SJonas Devlieghere
874edb874b2SJonas DevlieghereSynthetic Children
875edb874b2SJonas Devlieghere------------------
876edb874b2SJonas Devlieghere
877edb874b2SJonas DevlieghereSummaries work well when one is able to navigate through an expression path. In
878edb874b2SJonas Devlieghereorder for LLDB to do so, appropriate debugging information must be available.
879edb874b2SJonas Devlieghere
880edb874b2SJonas DevlieghereSome types are opaque, i.e. no knowledge of their internals is provided. When
881edb874b2SJonas Devliegherethat's the case, expression paths do not work correctly.
882edb874b2SJonas Devlieghere
883edb874b2SJonas DevlieghereIn other cases, the internals are available to use in expression paths, but
884edb874b2SJonas Devliegherethey do not provide a user-friendly representation of the object's value.
885edb874b2SJonas Devlieghere
886edb874b2SJonas DevlieghereFor instance, consider an STL vector, as implemented by the GNU C++ Library:
887edb874b2SJonas Devlieghere
888edb874b2SJonas Devlieghere::
889edb874b2SJonas Devlieghere
890edb874b2SJonas Devlieghere   (lldb) frame variable numbers -T
891edb874b2SJonas Devlieghere   (std::vector<int>) numbers = {
892edb874b2SJonas Devlieghere      (std::_Vector_base<int, std::allocator<int> >) std::_Vector_base<int, std::allocator<int> > = {
893edb874b2SJonas Devlieghere         (std::_Vector_base<int, std::allocator&tl;int> >::_Vector_impl) _M_impl = {
894edb874b2SJonas Devlieghere               (int *) _M_start = 0x00000001001008a0
895edb874b2SJonas Devlieghere               (int *) _M_finish = 0x00000001001008a8
896edb874b2SJonas Devlieghere               (int *) _M_end_of_storage = 0x00000001001008a8
897edb874b2SJonas Devlieghere         }
898edb874b2SJonas Devlieghere      }
899edb874b2SJonas Devlieghere   }
900edb874b2SJonas Devlieghere
901edb874b2SJonas DevlieghereHere, you can see how the type is implemented, and you can write a summary for
902edb874b2SJonas Devliegherethat implementation but that is not going to help you infer what items are
903edb874b2SJonas Devlieghereactually stored in the vector.
904edb874b2SJonas Devlieghere
905edb874b2SJonas DevlieghereWhat you would like to see is probably something like:
906edb874b2SJonas Devlieghere
907edb874b2SJonas Devlieghere::
908edb874b2SJonas Devlieghere
909edb874b2SJonas Devlieghere   (lldb) frame variable numbers -T
910edb874b2SJonas Devlieghere   (std::vector<int>) numbers = {
911edb874b2SJonas Devlieghere      (int) [0] = 1
912edb874b2SJonas Devlieghere      (int) [1] = 12
913edb874b2SJonas Devlieghere      (int) [2] = 123
914edb874b2SJonas Devlieghere      (int) [3] = 1234
915edb874b2SJonas Devlieghere   }
916edb874b2SJonas Devlieghere
917edb874b2SJonas DevlieghereSynthetic children are a way to get that result.
918edb874b2SJonas Devlieghere
919edb874b2SJonas DevlieghereThe feature is based upon the idea of providing a new set of children for a
920edb874b2SJonas Devliegherevariable that replaces the ones available by default through the debug
921edb874b2SJonas Devlieghereinformation. In the example, we can use synthetic children to provide the
922edb874b2SJonas Devliegherevector items as children for the std::vector object.
923edb874b2SJonas Devlieghere
924edb874b2SJonas DevlieghereIn order to create synthetic children, you need to provide a Python class that
925edb874b2SJonas Devlieghereadheres to a given interface (the word is italicized because Python has no
926edb874b2SJonas Devlieghereexplicit notion of interface, by that word we mean a given set of methods must
927edb874b2SJonas Devliegherebe implemented by the Python class):
928edb874b2SJonas Devlieghere
9290478eadfSFred Riss.. code-block:: python
930edb874b2SJonas Devlieghere
931edb874b2SJonas Devlieghere   class SyntheticChildrenProvider:
932edb874b2SJonas Devlieghere      def __init__(self, valobj, internal_dict):
933*6c36bdb6SPavel Labath         this call should initialize the Python object using valobj as the
934*6c36bdb6SPavel Labath         variable to provide synthetic children for
935*6c36bdb6SPavel Labath      def num_children(self, max_children):
936*6c36bdb6SPavel Labath         this call should return the number of children that you want your
937*6c36bdb6SPavel Labath         object to have[1]
938edb874b2SJonas Devlieghere      def get_child_index(self,name):
939*6c36bdb6SPavel Labath         this call should return the index of the synthetic child whose name is
940*6c36bdb6SPavel Labath         given as argument
941edb874b2SJonas Devlieghere      def get_child_at_index(self,index):
942*6c36bdb6SPavel Labath         this call should return a new LLDB SBValue object representing the
943*6c36bdb6SPavel Labath         child at the index given as argument
944edb874b2SJonas Devlieghere      def update(self):
945*6c36bdb6SPavel Labath         this call should be used to update the internal state of this Python
946*6c36bdb6SPavel Labath         object whenever the state of the variables in LLDB changes.[2]
94769c8e64bSWalter Erquinigo         Also, this method is invoked before any other method in the interface.
948edb874b2SJonas Devlieghere      def has_children(self):
949*6c36bdb6SPavel Labath         this call should return True if this object might have children, and
950*6c36bdb6SPavel Labath         False if this object can be guaranteed not to have children.[3]
951edb874b2SJonas Devlieghere      def get_value(self):
952*6c36bdb6SPavel Labath         this call can return an SBValue to be presented as the value of the
953*6c36bdb6SPavel Labath         synthetic value under consideration.[4]
954edb874b2SJonas Devlieghere
95569c8e64bSWalter ErquinigoAs a warning, exceptions that are thrown by python formatters are caught
95669c8e64bSWalter Erquinigosilently by LLDB and should be handled appropriately by the formatter itself.
95769c8e64bSWalter ErquinigoBeing more specific, in case of exceptions, LLDB might assume that the given
95869c8e64bSWalter Erquinigoobject has no children or it might skip printing some children, as they are
95969c8e64bSWalter Erquinigoprinted one by one.
96069c8e64bSWalter Erquinigo
961*6c36bdb6SPavel Labath[1] The `max_children` argument is optional (since lldb 3.8.0) and indicates the
962*6c36bdb6SPavel Labathmaximum number of children that lldb is interested in (at this moment). If the
963*6c36bdb6SPavel Labathcomputation of the number of children is expensive (for example, requires
964*6c36bdb6SPavel Labathtravesing a linked list to determine its size) your implementation may return
965*6c36bdb6SPavel Labath`max_children` rather than the actual number. If the computation is cheap (e.g., the
966*6c36bdb6SPavel Labathnumber is stored as a field of the object), then you can always return the true
967*6c36bdb6SPavel Labathnumber of children (that is, ignore the `max_children` argument).
968*6c36bdb6SPavel Labath
969*6c36bdb6SPavel Labath[2] This method is optional. Also, a boolean value must be returned (since lldb
970a6f1d046SDave Lee3.1.0). If ``False`` is returned, then whenever the process reaches a new stop,
971a6f1d046SDave Leethis method will be invoked again to generate an updated list of the children
972a6f1d046SDave Leefor a given variable. Otherwise, if ``True`` is returned, then the value is
973a6f1d046SDave Leecached and this method won't be called again, effectively freezing the state of
974a6f1d046SDave Leethe value in subsequent stops. Beware that returning ``True`` incorrectly could
975a6f1d046SDave Leeshow misleading information to the user.
976edb874b2SJonas Devlieghere
977*6c36bdb6SPavel Labath[3] This method is optional (since lldb 3.2.0). While implementing it in terms
978a6f1d046SDave Leeof num_children is acceptable, implementors are encouraged to look for
979a6f1d046SDave Leeoptimized coding alternatives whenever reasonable.
980edb874b2SJonas Devlieghere
981*6c36bdb6SPavel Labath[4] This method is optional (since lldb 3.5.2). The `SBValue` you return here
982a6f1d046SDave Leewill most likely be a numeric type (int, float, ...) as its value bytes will be
983a6f1d046SDave Leeused as-if they were the value of the root `SBValue` proper.  As a shortcut for
984a6f1d046SDave Leethis, you can inherit from lldb.SBSyntheticValueProvider, and just define
985a6f1d046SDave Leeget_value as other methods are defaulted in the superclass as returning default
986a6f1d046SDave Leeno-children responses.
987edb874b2SJonas Devlieghere
988a93aa534SJonas DevlieghereIf a synthetic child provider supplies a special child named
989a93aa534SJonas Devlieghere``$$dereference$$`` then it will be used when evaluating ``operator *`` and
9900478eadfSFred Riss``operator ->`` in the frame variable command and related SB API
9910478eadfSFred Rissfunctions. It is possible to declare this synthetic child without
9920478eadfSFred Rissincluding it in the range of children displayed by LLDB. For example,
9930478eadfSFred Rissthis subset of a synthetic children provider class would allow the
9940478eadfSFred Risssynthetic value to be dereferenced without actually showing any
99523e26cb9SKazu Hiratasynthetic children in the UI:
9960478eadfSFred Riss
9970478eadfSFred Riss.. code-block:: python
9980478eadfSFred Riss
9990478eadfSFred Riss      class SyntheticChildrenProvider:
10000478eadfSFred Riss          [...]
10010478eadfSFred Riss          def num_children(self):
10020478eadfSFred Riss              return 0
10030478eadfSFred Riss          def get_child_index(self, name):
10040478eadfSFred Riss              if name == '$$dereference$$':
10050478eadfSFred Riss                  return 0
10060478eadfSFred Riss              return -1
10070478eadfSFred Riss          def get_child_at_index(self, index):
10080478eadfSFred Riss              if index == 0:
10090478eadfSFred Riss                  return <valobj resulting from dereference>
10100478eadfSFred Riss              return None
10110478eadfSFred Riss
1012edb874b2SJonas Devlieghere
1013edb874b2SJonas DevlieghereFor examples of how synthetic children are created, you are encouraged to look
1014edb874b2SJonas Devlieghereat examples/synthetic in the LLDB trunk. Please, be aware that the code in
1015edb874b2SJonas Devliegherethose files (except bitfield/) is legacy code and is not maintained. You may
1016edb874b2SJonas Devlieghereespecially want to begin looking at this example to get a feel for this
1017edb874b2SJonas Devliegherefeature, as it is a very easy and well commented example.
1018edb874b2SJonas Devlieghere
1019edb874b2SJonas DevlieghereThe design pattern consistently used in synthetic providers shipping with LLDB
1020a58aceffSRaphael Isemannis to use the __init__ to store the `SBValue` instance as a part of self. The
1021edb874b2SJonas Devlieghereupdate function is then used to perform the actual initialization. Once a
1022edb874b2SJonas Devliegheresynthetic children provider is written, one must load it into LLDB before it
1023edb874b2SJonas Devliegherecan be used. Currently, one can use the LLDB script command to type Python code
1024edb874b2SJonas Devlieghereinteractively, or use the command script import fileName command to load Python
1025edb874b2SJonas Devliegherecode from a Python module (ordinary rules apply to importing modules this way).
1026edb874b2SJonas DevlieghereA third option is to type the code for the provider class interactively while
1027edb874b2SJonas Devlieghereadding it.
1028edb874b2SJonas Devlieghere
1029edb874b2SJonas DevlieghereFor example, let's pretend we have a class Foo for which a synthetic children
1030edb874b2SJonas Devlieghereprovider class Foo_Provider is available, in a Python module contained in file
1031edb874b2SJonas Devlieghere~/Foo_Tools.py. The following interaction sets Foo_Provider as a synthetic
1032edb874b2SJonas Devliegherechildren provider in LLDB:
1033edb874b2SJonas Devlieghere
1034edb874b2SJonas Devlieghere::
1035edb874b2SJonas Devlieghere
1036edb874b2SJonas Devlieghere   (lldb) command script import ~/Foo_Tools.py
1037edb874b2SJonas Devlieghere   (lldb) type synthetic add Foo --python-class Foo_Tools.Foo_Provider
1038edb874b2SJonas Devlieghere   (lldb) frame variable a_foo
1039edb874b2SJonas Devlieghere   (Foo) a_foo = {
1040edb874b2SJonas Devlieghere      x = 1
1041edb874b2SJonas Devlieghere      y = "Hello world"
1042edb874b2SJonas Devlieghere   }
1043edb874b2SJonas Devlieghere
1044edb874b2SJonas DevlieghereLLDB has synthetic children providers for a core subset of STL classes, both in
1045edb874b2SJonas Devliegherethe version provided by libstdcpp and by libcxx, as well as for several
1046edb874b2SJonas DevlieghereFoundation classes.
1047edb874b2SJonas Devlieghere
1048edb874b2SJonas DevlieghereSynthetic children extend summary strings by enabling a new special variable:
1049edb874b2SJonas Devlieghere``${svar``.
1050edb874b2SJonas Devlieghere
1051edb874b2SJonas DevlieghereThis symbol tells LLDB to refer expression paths to the synthetic children
1052edb874b2SJonas Devlieghereinstead of the real ones. For instance,
1053edb874b2SJonas Devlieghere
1054edb874b2SJonas Devlieghere::
1055edb874b2SJonas Devlieghere
1056edb874b2SJonas Devlieghere   (lldb) type summary add --expand -x "std::vector<" --summary-string "${svar%#} items"
1057edb874b2SJonas Devlieghere   (lldb) frame variable numbers
1058edb874b2SJonas Devlieghere   (std::vector<int>) numbers = 4 items {
1059edb874b2SJonas Devlieghere      (int) [0] = 1
1060edb874b2SJonas Devlieghere      (int) [1] = 12
1061edb874b2SJonas Devlieghere      (int) [2] = 123
1062edb874b2SJonas Devlieghere      (int) [3] = 1234
1063edb874b2SJonas Devlieghere   }
1064edb874b2SJonas Devlieghere
106569c8e64bSWalter ErquinigoIt's important to mention that LLDB invokes the synthetic child provider before
106669c8e64bSWalter Erquinigoinvoking the summary string provider, which allows the latter to have access to
106769c8e64bSWalter Erquinigothe actual displayable children. This applies to both inlined summary strings
106869c8e64bSWalter Erquinigoand python-based summary providers.
106969c8e64bSWalter Erquinigo
107069c8e64bSWalter Erquinigo
107169c8e64bSWalter ErquinigoAs a warning, when programmatically accessing the children or children count of
107269c8e64bSWalter Erquinigoa variable that has a synthetic child provider, notice that LLDB hides the
107369c8e64bSWalter Erquinigoactual raw children. For example, suppose we have a ``std::vector``, which has
107469c8e64bSWalter Erquinigoan actual in-memory property ``__begin`` marking the beginning of its data.
107569c8e64bSWalter ErquinigoAfter the synthetic child provider is executed, the ``std::vector`` variable
107669c8e64bSWalter Erquinigowon't show ``__begin`` as child anymore, even through the SB API. It will have
107769c8e64bSWalter Erquinigoinstead the children calculated by the provider. In case the actual raw
107869c8e64bSWalter Erquinigochildren are needed, a call to ``value.GetNonSyntheticValue()`` is enough to
107969c8e64bSWalter Erquinigoget a raw version of the value. It is import to remember this when implementing
108069c8e64bSWalter Erquinigosummary string providers, as they run after the synthetic child provider.
108169c8e64bSWalter Erquinigo
108269c8e64bSWalter Erquinigo
1083edb874b2SJonas DevlieghereIn some cases, if LLDB is unable to use the real object to get a child
1084edb874b2SJonas Devliegherespecified in an expression path, it will automatically refer to the synthetic
1085edb874b2SJonas Devliegherechildren. While in summaries it is best to always use ${svar to make your
1086edb874b2SJonas Devlieghereintentions clearer, interactive debugging can benefit from this behavior, as
1087edb874b2SJonas Devliegherein:
1088edb874b2SJonas Devlieghere
1089edb874b2SJonas Devlieghere::
1090edb874b2SJonas Devlieghere
1091edb874b2SJonas Devlieghere   (lldb) frame variable numbers[0] numbers[1]
1092edb874b2SJonas Devlieghere   (int) numbers[0] = 1
1093edb874b2SJonas Devlieghere   (int) numbers[1] = 12
1094edb874b2SJonas Devlieghere
1095edb874b2SJonas DevlieghereUnlike many other visualization features, however, the access to synthetic
1096edb874b2SJonas Devliegherechildren only works when using frame variable, and is not supported in
1097edb874b2SJonas Devlieghereexpression:
1098edb874b2SJonas Devlieghere
1099edb874b2SJonas Devlieghere::
1100edb874b2SJonas Devlieghere
1101edb874b2SJonas Devlieghere   (lldb) expression numbers[0]
1102edb874b2SJonas Devlieghere   Error [IRForTarget]: Call to a function '_ZNSt33vector<int, std::allocator<int> >ixEm' that is not present in the target
1103edb874b2SJonas Devlieghere   error: Couldn't convert the expression to DWARF
1104edb874b2SJonas Devlieghere
1105a93aa534SJonas DevlieghereThe reason for this is that classes might have an overloaded ``operator []``,
1106a93aa534SJonas Devlieghereor other special provisions and the expression command chooses to ignore
1107a93aa534SJonas Devliegheresynthetic children in the interest of equivalency with code you asked to have
1108a93aa534SJonas Devliegherecompiled from source.
1109edb874b2SJonas Devlieghere
1110edb874b2SJonas DevlieghereFilters
1111edb874b2SJonas Devlieghere-------
1112edb874b2SJonas Devlieghere
1113edb874b2SJonas DevlieghereFilters are a solution to the display of complex classes. At times, classes
1114edb874b2SJonas Devliegherehave many member variables but not all of these are actually necessary for the
1115edb874b2SJonas Devlieghereuser to see.
1116edb874b2SJonas Devlieghere
1117edb874b2SJonas DevlieghereA filter will solve this issue by only letting the user see those member
111843e451f9SPedro Gonnetvariables they care about. Of course, the equivalent of a filter can be
1119edb874b2SJonas Devlieghereimplemented easily using synthetic children, but a filter lets you get the job
1120edb874b2SJonas Devliegheredone without having to write Python code.
1121edb874b2SJonas Devlieghere
1122edb874b2SJonas DevlieghereFor instance, if your class Foobar has member variables named A thru Z, but you
1123edb874b2SJonas Devlieghereonly need to see the ones named B, H and Q, you can define a filter:
1124edb874b2SJonas Devlieghere
1125edb874b2SJonas Devlieghere::
1126edb874b2SJonas Devlieghere
1127edb874b2SJonas Devlieghere   (lldb) type filter add Foobar --child B --child H --child Q
1128edb874b2SJonas Devlieghere   (lldb) frame variable a_foobar
1129edb874b2SJonas Devlieghere   (Foobar) a_foobar = {
1130edb874b2SJonas Devlieghere      (int) B = 1
1131edb874b2SJonas Devlieghere      (char) H = 'H'
1132edb874b2SJonas Devlieghere      (std::string) Q = "Hello world"
1133edb874b2SJonas Devlieghere   }
1134edb874b2SJonas Devlieghere
1135868186cfSJorge Gorbe MoyaCallback-based type matching
1136868186cfSJorge Gorbe Moya----------------------------
1137868186cfSJorge Gorbe Moya
1138868186cfSJorge Gorbe MoyaEven though regular expression matching works well for the vast majority of data
1139868186cfSJorge Gorbe Moyaformatters (you normally know the name of the type you're writing a formatter
1140868186cfSJorge Gorbe Moyafor), there are some cases where it's useful to look at the type before deciding
1141868186cfSJorge Gorbe Moyawhat formatter to apply.
1142868186cfSJorge Gorbe Moya
1143868186cfSJorge Gorbe MoyaAs an example scenario, imagine we have a code generator that produces some
1144868186cfSJorge Gorbe Moyaclasses that inherit from a common ``GeneratedObject`` class, and we have a
1145868186cfSJorge Gorbe Moyasummary function and a synthetic child provider that work for all
1146868186cfSJorge Gorbe Moya``GeneratedObject`` instances (they all follow the same pattern). However, there
1147868186cfSJorge Gorbe Moyais no common pattern in the name of these classes, so we can't register the
1148868186cfSJorge Gorbe Moyaformatter neither by name nor by regular expression.
1149868186cfSJorge Gorbe Moya
1150868186cfSJorge Gorbe MoyaIn that case, you can write a recognizer function like this:
1151868186cfSJorge Gorbe Moya
1152868186cfSJorge Gorbe Moya::
1153868186cfSJorge Gorbe Moya
1154868186cfSJorge Gorbe Moya   def is_generated_object(sbtype, internal_dict):
1155868186cfSJorge Gorbe Moya     for base in sbtype.get_bases_array():
1156868186cfSJorge Gorbe Moya       if base.GetName() == "GeneratedObject"
1157868186cfSJorge Gorbe Moya         return True
1158868186cfSJorge Gorbe Moya     return False
1159868186cfSJorge Gorbe Moya
1160868186cfSJorge Gorbe MoyaAnd pass this function to ``type summary add`` and ``type synthetic add`` using
1161868186cfSJorge Gorbe Moyathe flag ``--recognizer-function``.
1162868186cfSJorge Gorbe Moya
1163868186cfSJorge Gorbe Moya::
1164868186cfSJorge Gorbe Moya
1165868186cfSJorge Gorbe Moya   (lldb) type summary add --expand --python-function my_summary_function --recognizer-function is_generated_object
1166868186cfSJorge Gorbe Moya   (lldb) type synthetic add --python-class my_child_provider --recognizer-function is_generated_object
1167868186cfSJorge Gorbe Moya
1168edb874b2SJonas DevlieghereObjective-C Dynamic Type Discovery
1169edb874b2SJonas Devlieghere----------------------------------
1170edb874b2SJonas Devlieghere
1171edb874b2SJonas DevlieghereWhen doing Objective-C development, you may notice that some of your variables
1172edb874b2SJonas Devliegherecome out as of type id (for instance, items extracted from NSArray). By
1173edb874b2SJonas Devliegheredefault, LLDB will not show you the real type of the object. it can actually
1174edb874b2SJonas Devliegheredynamically discover the type of an Objective-C variable, much like the runtime
1175edb874b2SJonas Devlieghereitself does when invoking a selector. In order to be shown the result of that
1176edb874b2SJonas Devliegherediscovery that, however, a special option to frame variable or expression is
1177edb874b2SJonas Devlieghererequired: ``--dynamic-type``.
1178edb874b2SJonas Devlieghere
1179edb874b2SJonas Devlieghere
1180edb874b2SJonas Devlieghere``--dynamic-type`` can have one of three values:
1181edb874b2SJonas Devlieghere
1182edb874b2SJonas Devlieghere- ``no-dynamic-values``: the default, prevents dynamic type discovery
1183edb874b2SJonas Devlieghere- ``no-run-target``: enables dynamic type discovery as long as running code on
1184edb874b2SJonas Devlieghere  the target is not required
1185edb874b2SJonas Devlieghere- ``run-target``: enables code execution on the target in order to perform
1186edb874b2SJonas Devlieghere  dynamic type discovery
1187edb874b2SJonas Devlieghere
1188edb874b2SJonas DevlieghereIf you specify a value of either no-run-target or run-target, LLDB will detect
1189edb874b2SJonas Devliegherethe dynamic type of your variables and show the appropriate formatters for
1190edb874b2SJonas Devliegherethem. As an example:
1191edb874b2SJonas Devlieghere
1192edb874b2SJonas Devlieghere::
1193edb874b2SJonas Devlieghere
1194edb874b2SJonas Devlieghere   (lldb) expr @"Hello"
1195edb874b2SJonas Devlieghere   (NSString *) $0 = 0x00000001048000b0 @"Hello"
1196edb874b2SJonas Devlieghere   (lldb) expr -d no-run @"Hello"
1197edb874b2SJonas Devlieghere   (__NSCFString *) $1 = 0x00000001048000b0 @"Hello"
1198edb874b2SJonas Devlieghere
1199edb874b2SJonas DevlieghereBecause LLDB uses a detection algorithm that does not need to invoke any
1200edb874b2SJonas Devliegherefunctions on the target process, no-run-target is enough for this to work.
1201edb874b2SJonas Devlieghere
1202edb874b2SJonas DevlieghereAs a side note, the summary for NSString shown in the example is built right
1203edb874b2SJonas Devlieghereinto LLDB. It was initially implemented through Python (the code is still
1204edb874b2SJonas Devlieghereavailable for reference at CFString.py). However, this is out of sync with the
1205edb874b2SJonas Devliegherecurrent implementation of the NSString formatter (which is a C++ function
1206edb874b2SJonas Devliegherecompiled into the LLDB core).
1207edb874b2SJonas Devlieghere
1208edb874b2SJonas DevlieghereCategories
1209edb874b2SJonas Devlieghere----------
1210edb874b2SJonas Devlieghere
1211edb874b2SJonas DevlieghereCategories are a way to group related formatters. For instance, LLDB itself
1212edb874b2SJonas Devliegheregroups the formatters for the libstdc++ types in a category named
1213edb874b2SJonas Devliegheregnu-libstdc++. Basically, categories act like containers in which to store
1214edb874b2SJonas Devlieghereformatters for a same library or OS release.
1215edb874b2SJonas Devlieghere
1216edb874b2SJonas DevlieghereBy default, several categories are created in LLDB:
1217edb874b2SJonas Devlieghere
1218edb874b2SJonas Devlieghere- default: this is the category where every formatter ends up, unless another category is specified
121919ae9d01SAdrian Prantl- objc: formatters for basic and common Objective-C types that do not specifically depend on macOS
1220edb874b2SJonas Devlieghere- gnu-libstdc++: formatters for std::string, std::vector, std::list and std::map as implemented by libstdcpp
1221edb874b2SJonas Devlieghere- libcxx: formatters for std::string, std::vector, std::list and std::map as implemented by libcxx
1222edb874b2SJonas Devlieghere- system: truly basic types for which a formatter is required
1223edb874b2SJonas Devlieghere- AppKit: Cocoa classes
1224edb874b2SJonas Devlieghere- CoreFoundation: CF classes
1225edb874b2SJonas Devlieghere- CoreGraphics: CG classes
1226edb874b2SJonas Devlieghere- CoreServices: CS classes
1227edb874b2SJonas Devlieghere- VectorTypes: compact display for several vector types
1228edb874b2SJonas Devlieghere
1229edb874b2SJonas DevlieghereIf you want to use a custom category for your formatters, all the type ... add
1230edb874b2SJonas Devlieghereprovide a --category (-w) option, that names the category to add the formatter
1231edb874b2SJonas Devlieghereto. To delete the formatter, you then have to specify the correct category.
1232edb874b2SJonas Devlieghere
1233edb874b2SJonas DevlieghereCategories can be in one of two states: enabled and disabled. A category is
1234edb874b2SJonas Devlieghereinitially disabled, and can be enabled using the type category enable command.
1235edb874b2SJonas DevlieghereTo disable an enabled category, the command to use is type category disable.
1236edb874b2SJonas Devlieghere
1237edb874b2SJonas DevlieghereThe order in which categories are enabled or disabled is significant, in that
1238edb874b2SJonas DevlieghereLLDB uses that order when looking for formatters. Therefore, when you enable a
1239edb874b2SJonas Devliegherecategory, it becomes the second one to be searched (after default, which always
1240edb874b2SJonas Devliegherestays on top of the list). The default categories are enabled in such a way
1241edb874b2SJonas Devliegherethat the search order is:
1242edb874b2SJonas Devlieghere
1243edb874b2SJonas Devlieghere- default
1244edb874b2SJonas Devlieghere- objc
1245edb874b2SJonas Devlieghere- CoreFoundation
1246edb874b2SJonas Devlieghere- AppKit
1247edb874b2SJonas Devlieghere- CoreServices
1248edb874b2SJonas Devlieghere- CoreGraphics
1249edb874b2SJonas Devlieghere- gnu-libstdc++
1250edb874b2SJonas Devlieghere- libcxx
1251edb874b2SJonas Devlieghere- VectorTypes
1252edb874b2SJonas Devlieghere- system
1253edb874b2SJonas Devlieghere
1254edb874b2SJonas DevlieghereAs said, gnu-libstdc++ and libcxx contain formatters for C++ STL data types.
1255edb874b2SJonas Devliegheresystem contains formatters for char* and char[], which reflect the behavior of
1256edb874b2SJonas Devlieghereolder versions of LLDB which had built-in formatters for these types. Because
1257edb874b2SJonas Devliegherenow these are formatters, you can even replace them with your own if so you
1258edb874b2SJonas Devliegherewish.
1259edb874b2SJonas Devlieghere
1260edb874b2SJonas DevlieghereThere is no special command to create a category. When you place a formatter in
1261edb874b2SJonas Devliegherea category, if that category does not exist, it is automatically created. For
1262edb874b2SJonas Devlieghereinstance,
1263edb874b2SJonas Devlieghere
1264edb874b2SJonas Devlieghere::
1265edb874b2SJonas Devlieghere
1266edb874b2SJonas Devlieghere   (lldb) type summary add Foobar --summary-string "a foobar" --category newcategory
1267edb874b2SJonas Devlieghere
1268edb874b2SJonas Devlieghereautomatically creates a (disabled) category named newcategory.
1269edb874b2SJonas Devlieghere
1270edb874b2SJonas DevlieghereAnother way to create a new (empty) category, is to enable it, as in:
1271edb874b2SJonas Devlieghere
1272edb874b2SJonas Devlieghere::
1273edb874b2SJonas Devlieghere
1274edb874b2SJonas Devlieghere   (lldb) type category enable newcategory
1275edb874b2SJonas Devlieghere
1276edb874b2SJonas DevlieghereHowever, in this case LLDB warns you that enabling an empty category has no
1277edb874b2SJonas Devlieghereeffect. If you add formatters to the category after enabling it, they will be
1278edb874b2SJonas Devliegherehonored. But an empty category per se does not change the way any type is
1279edb874b2SJonas Devliegheredisplayed. The reason the debugger warns you is that enabling an empty category
1280edb874b2SJonas Devliegheremight be a typo, and you effectively wanted to enable a similarly-named but
1281edb874b2SJonas Devliegherenot-empty category.
1282edb874b2SJonas Devlieghere
1283edb874b2SJonas DevlieghereFinding Formatters 101
1284edb874b2SJonas Devlieghere----------------------
1285edb874b2SJonas Devlieghere
1286a6f1d046SDave LeeSearching for a formatter (including formats, since lldb 3.4.0) given a
1287a6f1d046SDave Leevariable goes through a rather intricate set of rules. Namely, what happens is
1288a6f1d046SDave Leethat LLDB starts looking in each enabled category, according to the order in
1289a6f1d046SDave Leewhich they were enabled (latest enabled first). In each category, LLDB does the
1290a6f1d046SDave Leefollowing:
1291edb874b2SJonas Devlieghere
1292edb874b2SJonas Devlieghere- If there is a formatter for the type of the variable, use it
1293edb874b2SJonas Devlieghere- If this object is a pointer, and there is a formatter for the pointee type
1294edb874b2SJonas Devlieghere  that does not skip pointers, use it
1295edb874b2SJonas Devlieghere- If this object is a reference, and there is a formatter for the referred type
1296edb874b2SJonas Devlieghere  that does not skip references, use it
1297edb874b2SJonas Devlieghere- If this object is an Objective-C class and dynamic types are enabled, look
1298edb874b2SJonas Devlieghere  for a formatter for the dynamic type of the object. If dynamic types are
1299edb874b2SJonas Devlieghere  disabled, or the search failed, look for a formatter for the declared type of
1300edb874b2SJonas Devlieghere  the object
1301edb874b2SJonas Devlieghere- If this object's type is a typedef, go through typedef hierarchy (LLDB might
1302edb874b2SJonas Devlieghere  not be able to do this if the compiler has not emitted enough information. If
1303edb874b2SJonas Devlieghere  the required information to traverse typedef hierarchies is missing, type
1304edb874b2SJonas Devlieghere  cascading will not work. The clang compiler, part of the LLVM project, emits
1305edb874b2SJonas Devlieghere  the correct debugging information for LLDB to cascade). If at any level of
1306edb874b2SJonas Devlieghere  the hierarchy there is a valid formatter that can cascade, use it.
1307edb874b2SJonas Devlieghere- If everything has failed, repeat the above search, looking for regular
1308edb874b2SJonas Devlieghere  expressions instead of exact matches
1309edb874b2SJonas Devlieghere
1310edb874b2SJonas DevlieghereIf any of those attempts returned a valid formatter to be used, that one is
1311edb874b2SJonas Devlieghereused, and the search is terminated (without going to look in other categories).
1312edb874b2SJonas DevlieghereIf nothing was found in the current category, the next enabled category is
1313edb874b2SJonas Devliegherescanned according to the same algorithm. If there are no more enabled
1314edb874b2SJonas Devliegherecategories, the search has failed.
1315edb874b2SJonas Devlieghere
1316edb874b2SJonas Devlieghere**Warning**: previous versions of LLDB defined cascading to mean not only going
1317edb874b2SJonas Devliegherethrough typedef chains, but also through inheritance chains. This feature has
1318edb874b2SJonas Devliegherebeen removed since it significantly degrades performance. You need to set up
1319edb874b2SJonas Devlieghereyour formatters for every type in inheritance chains to which you want the
1320edb874b2SJonas Devlieghereformatter to apply.
1321