1Variable Formatting
2===================
3
4.. contents::
5   :local:
6
7LLDB has a data formatters subsystem that allows users to define custom display
8options for their variables.
9
10Usually, when you type ``frame variable`` or run some expression LLDB will
11automatically choose the way to display your results on a per-type basis, as in
12the following example:
13
14::
15
16   (lldb) frame variable
17   (uint8_t) x = 'a'
18   (intptr_t) y = 124752287
19
20Note: ``frame variable`` without additional arguments prints the list of
21variables of the current frame.
22
23However, in certain cases, you may want to associate a different style to the
24display for certain datatypes. To do so, you need to give hints to the debugger
25as to how variables should be displayed. The LLDB type command allows you to do
26just that.
27
28Using it you can change your visualization to look like this:
29
30::
31
32   (lldb) frame variable
33   (uint8_t) x = chr='a' dec=65 hex=0x41
34   (intptr_t) y = 0x76f919f
35
36In addition, some data structures can encode their data in a way that is not
37easily readable to the user, in which case a data formatter can be used to
38show the data in a human readable way. For example, without a formatter,
39printing a ``std::deque<int>`` with the elements ``{2, 3, 4, 5, 6}`` would
40result in something like:
41
42::
43
44   (lldb) frame variable a_deque
45   (std::deque<Foo, std::allocator<int> >) $0 = {
46      std::_Deque_base<Foo, std::allocator<int> > = {
47         _M_impl = {
48            _M_map = 0x000000000062ceb0
49            _M_map_size = 8
50            _M_start = {
51               _M_cur = 0x000000000062cf00
52               _M_first = 0x000000000062cf00
53               _M_last = 0x000000000062d2f4
54               _M_node = 0x000000000062cec8
55            }
56            _M_finish = {
57               _M_cur = 0x000000000062d300
58               _M_first = 0x000000000062d300
59               _M_last = 0x000000000062d6f4
60               _M_node = 0x000000000062ced0
61            }
62         }
63      }
64   }
65
66which is very hard to make sense of.
67
68Note: ``frame variable <var>`` prints out the variable ``<var>`` in the current
69frame.
70
71On the other hand, a proper formatter is able to produce the following output:
72
73::
74
75   (lldb) frame variable a_deque
76   (std::deque<Foo, std::allocator<int> >) $0 = size=5 {
77      [0] = 2
78      [1] = 3
79      [2] = 4
80      [3] = 5
81      [4] = 6
82   }
83
84which is what the user would expect from a good debugger.
85
86Note: you can also use ``v <var>`` instead of ``frame variable <var>``.
87
88It's worth mentioning that the ``size=5`` string is produced by a summary
89provider and the list of children is produced by a synthetic child provider.
90More information about these providers is available later in this document.
91
92
93There are several features related to data visualization: formats, summaries,
94filters, synthetic children.
95
96To reflect this, the type command has five subcommands:
97
98::
99
100   type format
101   type summary
102   type filter
103   type synthetic
104   type category
105
106These commands are meant to bind printing options to types. When variables are
107printed, LLDB will first check if custom printing options have been associated
108to a variable's type and, if so, use them instead of picking the default
109choices.
110
111Each of the commands (except ``type category``) has four subcommands available:
112
113- ``add``: associates a new printing option to one or more types
114- ``delete``: deletes an existing association
115- ``list``: provides a listing of all associations
116- ``clear``: deletes all associations
117
118Type Format
119-----------
120
121Type formats enable you to quickly override the default format for displaying
122primitive types (the usual basic C/C++/ObjC types: int, float, char, ...).
123
124If for some reason you want all int variables in your program to print out as
125hex, you can add a format to the int type.
126
127This is done by typing
128
129::
130
131   (lldb) type format add --format hex int
132
133at the LLDB command line.
134
135The ``--format`` (which you can shorten to -f) option accepts a :doc:`format
136name<formatting>`. Then, you provide one or more types to which you want the
137new format applied.
138
139A frequent scenario is that your program has a typedef for a numeric type that
140you know represents something that must be printed in a certain way. Again, you
141can add a format just to that typedef by using type format add with the name
142alias.
143
144But things can quickly get hierarchical. Let's say you have a situation like
145the following:
146
147::
148
149   typedef int A;
150   typedef A B;
151   typedef B C;
152   typedef C D;
153
154and you want to show all A's as hex, all C's as byte arrays and leave the
155defaults untouched for other types (albeit its contrived look, the example is
156far from unrealistic in large software systems).
157
158If you simply type
159
160::
161
162   (lldb) type format add -f hex A
163   (lldb) type format add -f uint8_t[] C
164
165values of type B will be shown as hex and values of type D as byte arrays, as in:
166
167::
168
169   (lldb) frame variable -T
170   (A) a = 0x00000001
171   (B) b = 0x00000002
172   (C) c = {0x03 0x00 0x00 0x00}
173   (D) d = {0x04 0x00 0x00 0x00}
174
175This is because by default LLDB cascades formats through typedef chains. In
176order to avoid that you can use the option -C no to prevent cascading, thus
177making the two commands required to achieve your goal:
178
179::
180
181   (lldb) type format add -C no -f hex A
182   (lldb) type format add -C no -f uint8_t[] C
183
184
185which provides the desired output:
186
187::
188
189   (lldb) frame variable -T
190   (A) a = 0x00000001
191   (B) b = 2
192   (C) c = {0x03 0x00 0x00 0x00}
193   (D) d = 4
194
195Note, that qualifiers such as const and volatile will be stripped when matching types for example:
196
197::
198
199   (lldb) frame var x y z
200   (int) x = 1
201   (const int) y = 2
202   (volatile int) z = 4
203   (lldb) type format add -f hex int
204   (lldb) frame var x y z
205   (int) x = 0x00000001
206   (const int) y = 0x00000002
207   (volatile int) z = 0x00000004
208
209Two additional options that you will want to look at are --skip-pointers (-p)
210and --skip-references (-r). These two options prevent LLDB from applying a
211format for type T to values of type T* and T& respectively.
212
213::
214
215   (lldb) type format add -f float32[] int
216   (lldb) frame variable pointer *pointer -T
217   (int *) pointer = {1.46991e-39 1.4013e-45}
218   (int) *pointer = {1.53302e-42}
219   (lldb) type format add -f float32[] int -p
220   (lldb) frame variable pointer *pointer -T
221   (int *) pointer = 0x0000000100100180
222   (int) *pointer = {1.53302e-42}
223
224While they can be applied to pointers and references, formats will make no
225attempt to dereference the pointer and extract the value before applying the
226format, which means you are effectively formatting the address stored in the
227pointer rather than the pointee value. For this reason, you may want to use the
228-p option when defining formats.
229
230If you need to delete a custom format simply type type format delete followed
231by the name of the type to which the format applies.Even if you defined the
232same format for multiple types on the same command, type format delete will
233only remove the format for the type name passed as argument.
234
235To delete ALL formats, use ``type format clear``. To see all the formats
236defined, use type format list.
237
238If all you need to do, however, is display one variable in a custom format,
239while leaving the others of the same type untouched, you can simply type:
240
241::
242
243   (lldb) frame variable counter -f hex
244
245This has the effect of displaying the value of counter as an hexadecimal
246number, and will keep showing it this way until you either pick a different
247format or till you let your program run again.
248
249Finally, this is a list of formatting options available out of which you can
250pick:
251
252+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
253| **Format name**                               | **Abbreviation** | **Description**                                                          |
254+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
255| ``default``                                   |                  | the default LLDB algorithm is used to pick a format                      |
256+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
257| ``boolean``                                   | B                | show this as a true/false boolean, using the customary rule that 0 is    |
258|                                               |                  | false and everything else is true                                        |
259+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
260| ``binary``                                    | b                | show this as a sequence of bits                                          |
261+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
262| ``bytes``                                     | y                | show the bytes one after the other                                       |
263+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
264| ``bytes with ASCII``                          | Y                | show the bytes, but try to display them as ASCII characters as well      |
265+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
266| ``character``                                 | c                | show the bytes as ASCII characters                                       |
267+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
268| ``printable character``                       | C                | show the bytes as printable ASCII characters                             |
269+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
270| ``complex float``                             | F                | interpret this value as the real and imaginary part of a complex         |
271|                                               |                  | floating-point number                                                    |
272+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
273| ``c-string``                                  | s                | show this as a 0-terminated C string                                     |
274+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
275| ``decimal``                                   | d                | show this as a signed integer number (this does not perform a cast, it   |
276|                                               |                  | simply shows the bytes as  an integer with sign)                         |
277+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
278| ``enumeration``                               | E                | show this as an enumeration, printing the                                |
279|                                               |                  | value's name if available or the integer value otherwise                 |
280+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
281| ``hex``                                       | x                | show this as in hexadecimal notation (this does                          |
282|                                               |                  | not perform a cast, it simply shows the bytes as hex)                    |
283+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
284| ``float``                                     | f                | show this as a floating-point number (this does not perform a cast, it   |
285|                                               |                  | simply interprets the bytes as an IEEE754 floating-point value)          |
286+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
287| ``octal``                                     | o                | show this in octal notation                                              |
288+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
289| ``OSType``                                    | O                | show this as a MacOS OSType                                              |
290+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
291| ``unicode16``                                 | U                | show this as UTF-16 characters                                           |
292+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
293| ``unicode32``                                 |                  | show this as UTF-32 characters                                           |
294+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
295| ``unsigned decimal``                          | u                | show this as an unsigned integer number (this does not perform a cast,   |
296|                                               |                  | it simply shows the bytes as unsigned integer)                           |
297+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
298| ``pointer``                                   | p                | show this as a native pointer (unless this is really a pointer, the      |
299|                                               |                  | resulting address will probably be invalid)                              |
300+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
301| ``char[]``                                    |                  | show this as an array of characters                                      |
302+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
303| ``int8_t[], uint8_t[]``                       |                  | show this as an array of the corresponding integer type                  |
304| ``int16_t[], uint16_t[]``                     |                  |                                                                          |
305| ``int32_t[], uint32_t[]``                     |                  |                                                                          |
306| ``int64_t[], uint64_t[]``                     |                  |                                                                          |
307| ``uint128_t[]``                               |                  |                                                                          |
308+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
309| ``float32[], float64[]``                      |                  | show this as an array of the corresponding                               |
310|                                               |                  |                       floating-point type                                |
311+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
312| ``complex integer``                           | I                | interpret this value as the real and imaginary part of a complex integer |
313|                                               |                  | number                                                                   |
314+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
315| ``character array``                           | a                | show this as a character array                                           |
316+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
317| ``address``                                   | A                | show this as an address target (symbol/file/line + offset), possibly     |
318|                                               |                  | also the string this address is pointing to                              |
319+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
320| ``hex float``                                 |                  | show this as hexadecimal floating point                                  |
321+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
322| ``instruction``                               | i                | show this as an disassembled opcode                                      |
323+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
324| ``void``                                      | v                | don't show anything                                                      |
325+-----------------------------------------------+------------------+--------------------------------------------------------------------------+
326
327Type Summary
328------------
329
330Type formats work by showing a different kind of display for the value of a
331variable. However, they only work for basic types. When you want to display a
332class or struct in a custom format, you cannot do that using formats.
333
334A different feature, type summaries, works by extracting information from
335classes, structures, ... (aggregate types) and arranging it in a user-defined
336format, as in the following example:
337
338before adding a summary...
339
340::
341
342   (lldb) frame variable -T one
343   (i_am_cool) one = {
344      (int) x = 3
345      (float) y = 3.14159
346      (char) z = 'E'
347   }
348
349after adding a summary...
350
351::
352
353   (lldb) frame variable one
354   (i_am_cool) one = int = 3, float = 3.14159, char = 69
355
356There are two ways to use type summaries: the first one is to bind a summary
357string to the type; the second is to write a Python script that returns the
358string to be used as summary. Both options are enabled by the type summary add
359command.
360
361The command to obtain the output shown in the example is:
362
363::
364
365(lldb) type summary add --summary-string "int = ${var.x}, float = ${var.y}, char = ${var.z%u}" i_am_cool
366
367Initially, we will focus on summary strings, and then describe the Python
368binding mechanism.
369
370Summary Strings
371---------------
372
373Summary strings are written using a simple control language, exemplified by the
374snippet above. A summary string contains a sequence of tokens that are
375processed by LLDB to generate the summary.
376
377Summary strings can contain plain text, control characters and special
378variables that have access to information about the current object and the
379overall program state.
380
381Plain text is any sequence of characters that doesn't contain a ``{``, ``}``, ``$``,
382or ``\`` character, which are the syntax control characters.
383
384The special variables are found in between a "${" prefix, and end with a "}"
385suffix. Variables can be a simple name or they can refer to complex objects
386that have subitems themselves. In other words, a variable looks like
387``${object}`` or ``${object.child.otherchild}``. A variable can also be
388prefixed or suffixed with other symbols meant to change the way its value is
389handled. An example is ``${*var.int_pointer[0-3]}``.
390
391Basically, the syntax is the same one described Frame and Thread Formatting
392plus additional symbols specific for summary strings. The main of them is
393${var, which is used refer to the variable that a summary is being created for.
394
395The simplest thing you can do is grab a member variable of a class or structure
396by typing its expression path. In the previous example, the expression path for
397the field float y is simply .y. Thus, to ask the summary string to display y
398you would type ${var.y}.
399
400If you have code like the following:
401
402::
403
404   struct A {
405      int x;
406      int y;
407   };
408   struct B {
409      A x;
410      A y;
411      int *z;
412   };
413
414the expression path for the y member of the x member of an object of type B
415would be .x.y and you would type ``${var.x.y}`` to display it in a summary
416string for type B.
417
418By default, a summary defined for type T, also works for types T* and T& (you
419can disable this behavior if desired). For this reason, expression paths do not
420differentiate between . and ->, and the above expression path .x.y would be
421just as good if you were displaying a B*, or even if the actual definition of B
422were:
423
424::
425
426   struct B {
427      A *x;
428      A y;
429      int *z;
430   };
431
432This is unlike the behavior of frame variable which, on the contrary, will
433enforce the distinction. As hinted above, the rationale for this choice is that
434waiving this distinction enables you to write a summary string once for type T
435and use it for both T and T* instances. As a summary string is mostly about
436extracting nested members' information, a pointer to an object is just as good
437as the object itself for the purpose.
438
439If you need to access the value of the integer pointed to by B::z, you cannot
440simply say ${var.z} because that symbol refers to the pointer z. In order to
441dereference it and get the pointed value, you should say ``${*var.z}``. The
442``${*var`` tells LLDB to get the object that the expression paths leads to, and
443then dereference it. In this example is it equivalent to ``*(bObject.z)`` in
444C/C++ syntax. Because ``.`` and ``->`` operators can both be used, there is no
445need to have dereferences in the middle of an expression path (e.g. you do not
446need to type ``${*(var.x).x}``) to read A::x as contained in ``*(B::x)``. To
447achieve that effect you can simply write ``${var.x->x}``, or even
448``${var.x.x}``. The ``*`` operator only binds to the result of the whole
449expression path, rather than piecewise, and there is no way to use parentheses
450to change that behavior.
451
452Of course, a summary string can contain more than one ${var specifier, and can
453use ``${var`` and ``${*var`` specifiers together.
454
455Formatting Summary Elements
456---------------------------
457
458An expression path can include formatting codes. Much like the type formats
459discussed previously, you can also customize the way variables are displayed in
460summary strings, regardless of the format they have applied to their types. To
461do that, you can use %format inside an expression path, as in ${var.x->x%u},
462which would display the value of x as an unsigned integer.
463
464You can also use some other special format markers, not available for formats
465themselves, but which carry a special meaning when used in this context:
466
467+------------+--------------------------------------------------------------------------+
468| **Symbol** | **Description**                                                          |
469+------------+--------------------------------------------------------------------------+
470| ``Symbol`` | ``Description``                                                          |
471+------------+--------------------------------------------------------------------------+
472| ``%S``     | Use this object's summary (the default for aggregate types)              |
473+------------+--------------------------------------------------------------------------+
474| ``%V``     | Use this object's value (the default for non-aggregate types)            |
475+------------+--------------------------------------------------------------------------+
476| ``%@``     | Use a language-runtime specific description (for C++ this does nothing,  |
477|            |                     for Objective-C it calls the NSPrintForDebugger API) |
478+------------+--------------------------------------------------------------------------+
479| ``%L``     | Use this object's location (memory address, register name, ...)          |
480+------------+--------------------------------------------------------------------------+
481| ``%#``     | Use the count of the children of this object                             |
482+------------+--------------------------------------------------------------------------+
483| ``%T``     | Use this object's datatype name                                          |
484+------------+--------------------------------------------------------------------------+
485| ``%N``     | Print the variable's basename                                            |
486+------------+--------------------------------------------------------------------------+
487| ``%>``     | Print the expression path for this item                                  |
488+------------+--------------------------------------------------------------------------+
489
490Starting with SVN r228207, you can also specify
491``${script.var:pythonFuncName}``. Previously, back to r220821, this was
492specified with a different syntax: ``${var.script:pythonFuncName}``.
493
494It is expected that the function name you use specifies a function whose
495signature is the same as a Python summary function. The return string from the
496function will be placed verbatim in the output.
497
498You cannot use element access, or formatting symbols, in combination with this
499syntax. For example the following:
500
501::
502
503   ${script.var.element[0]:myFunctionName%@}
504
505is not valid and will cause the summary to fail to evaluate.
506
507
508Element Inlining
509----------------
510
511Option --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.
512
513As an example, given a type pair:
514
515::
516
517   (lldb) frame variable --show-types a_pair
518   (pair) a_pair = {
519      (int) first = 1;
520      (int) second = 2;
521   }
522
523If one types the following commands:
524
525::
526
527   (lldb) type summary add --inline-children pair
528
529the output becomes:
530
531::
532
533   (lldb) frame variable a_pair
534   (pair) a_pair = (first=1, second=2)
535
536
537Of course, one can obtain the same effect by typing
538
539::
540
541   (lldb) type summary add pair --summary-string "(first=${var.first}, second=${var.second})"
542
543While the final result is the same, using --inline-children can often save
544time. If one does not need to see the names of the variables, but just their
545values, the option --omit-names (-O, uppercase letter o), can be combined with
546--inline-children to obtain:
547
548::
549
550   (lldb) frame variable a_pair
551   (pair) a_pair = (1, 2)
552
553which is of course the same as typing
554
555::
556
557   (lldb) type summary add pair --summary-string "(${var.first}, ${var.second})"
558
559Bitfields And Array Syntax
560--------------------------
561
562Sometimes, a basic type's value actually represents several different values
563packed together in a bitfield.
564
565With the classical view, there is no way to look at them. Hexadecimal display
566can help, but if the bits actually span nibble boundaries, the help is limited.
567
568Binary view would show it all without ambiguity, but is often too detailed and
569hard to read for real-life scenarios.
570
571To cope with the issue, LLDB supports native bitfield formatting in summary
572strings. If your expression paths leads to a so-called scalar type (the usual
573int, float, char, double, short, long, long long, double, long double and
574unsigned variants), you can ask LLDB to only grab some bits out of the value
575and display them in any format you like. If you only need one bit you can use
576the [n], just like indexing an array. To extract multiple bits, you can use a
577slice-like syntax: [n-m], e.g.
578
579::
580
581   (lldb) frame variable float_point
582   (float) float_point = -3.14159
583
584::
585
586   (lldb) type summary add --summary-string "Sign: ${var[31]%B} Exponent: ${var[30-23]%x} Mantissa: ${var[0-22]%u}" float
587   (lldb) frame variable float_point
588   (float) float_point = -3.14159 Sign: true Exponent: 0x00000080 Mantissa: 4788184
589
590In this example, LLDB shows the internal representation of a float variable by
591extracting bitfields out of a float object.
592
593When typing a range, the extremes n and m are always included, and the order of
594the indices is irrelevant.
595
596LLDB 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.
597e.g.
598
599::
600
601   (lldb) frame variable sarray
602   (Simple [3]) sarray = {
603      [0] = {
604         x = 1
605         y = 2
606         z = '\x03'
607      }
608      [1] = {
609         x = 4
610         y = 5
611         z = '\x06'
612      }
613      [2] = {
614         x = 7
615         y = 8
616         z = '\t'
617      }
618   }
619
620   (lldb) type summary add --summary-string "${var[].x}" "Simple [3]"
621
622   (lldb) frame variable sarray
623   (Simple [3]) sarray = [1,4,7]
624
625The [] 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:
626
627::
628
629   (lldb) frame variable sarray[1]
630   (Simple) sarray[1] = {
631      x = 4
632      y = 5
633      z = '\x06'
634   }
635
636You can also ask LLDB to only print a subset of the array range by using the
637same syntax used to extract bit for bitfields:
638
639::
640
641   (lldb) type summary add --summary-string "${var[1-2].x}" "Simple [3]"
642
643   (lldb) frame variable sarray
644   (Simple [3]) sarray = [4,7]
645
646If you are dealing with a pointer that you know is an array, you can use this
647syntax to display the elements contained in the pointed array instead of just
648the pointer value. However, because pointers have no notion of their size, the
649empty brackets [] operator does not work, and you must explicitly provide
650higher and lower bounds.
651
652In general, LLDB needs the square brackets ``operator []`` in order to handle
653arrays and pointers correctly, and for pointers it also needs a range. However,
654a few special cases are defined to make your life easier:
655
656you can print a 0-terminated string (C-string) using the %s format, omitting
657square brackets, as in:
658
659::
660
661   (lldb) type summary add --summary-string "${var%s}" "char *"
662
663This syntax works for char* as well as for char[] because LLDB can rely on the
664final \0 terminator to know when the string has ended.
665
666LLDB has default summary strings for char* and char[] that use this special
667case. On debugger startup, the following are defined automatically:
668
669::
670
671   (lldb) type summary add --summary-string "${var%s}" "char *"
672   (lldb) type summary add --summary-string "${var%s}" -x "char \[[0-9]+]"
673
674any of the array formats (int8_t[], float32{}, ...), and the y, Y and a formats
675work to print an array of a non-aggregate type, even if square brackets are
676omitted.
677
678::
679
680   (lldb) type summary add --summary-string "${var%int32_t[]}" "int [10]"
681
682This feature, however, is not enabled for pointers because there is no way for
683LLDB to detect the end of the pointed data.
684
685This also does not work for other formats (e.g. boolean), and you must specify
686the square brackets operator to get the expected output.
687
688Python Scripting
689----------------
690
691Most of the times, summary strings prove good enough for the job of summarizing
692the contents of a variable. However, as soon as you need to do more than
693picking some values and rearranging them for display, summary strings stop
694being an effective tool. This is because summary strings lack the power to
695actually perform any kind of computation on the value of variables.
696
697To solve this issue, you can bind some Python scripting code as a summary for
698your datatype, and that script has the ability to both extract children
699variables as the summary strings do and to perform active computation on the
700extracted values. As a small example, let's say we have a Rectangle class:
701
702::
703
704
705   class Rectangle
706   {
707   private:
708      int height;
709      int width;
710   public:
711      Rectangle() : height(3), width(5) {}
712      Rectangle(int H) : height(H), width(H*2-1) {}
713      Rectangle(int H, int W) : height(H), width(W) {}
714      int GetHeight() { return height; }
715      int GetWidth() { return width; }
716   };
717
718Summary strings are effective to reduce the screen real estate used by the
719default viewing mode, but are not effective if we want to display the area and
720perimeter of Rectangle objects
721
722To obtain this, we can simply attach a small Python script to the Rectangle
723class, as shown in this example:
724
725::
726
727   (lldb) type summary add -P Rectangle
728   Enter your Python command(s). Type 'DONE' to end.
729   def function (valobj,internal_dict,options):
730      height_val = valobj.GetChildMemberWithName('height')
731      width_val = valobj.GetChildMemberWithName('width')
732      height = height_val.GetValueAsUnsigned(0)
733      width = width_val.GetValueAsUnsigned(0)
734      area = height*width
735      perimeter = 2*(height + width)
736      return 'Area: ' + str(area) + ', Perimeter: ' + str(perimeter)
737      DONE
738   (lldb) frame variable
739   (Rectangle) r1 = Area: 20, Perimeter: 18
740   (Rectangle) r2 = Area: 72, Perimeter: 36
741   (Rectangle) r3 = Area: 16, Perimeter: 16
742
743In order to write effective summary scripts, you need to know the LLDB public
744API, which is the way Python code can access the LLDB object model. For further
745details on the API you should look at the LLDB API reference documentation.
746
747
748As a brief introduction, your script is encapsulated into a function that is
749passed two parameters: ``valobj`` and ``internal_dict``.
750
751``internal_dict`` is an internal support parameter used by LLDB and you should
752not touch it.
753
754``valobj`` is the object encapsulating the actual variable being displayed, and
755its type is `SBValue`. Out of the many possible operations on an `SBValue`, the
756basic one is retrieve the children objects it contains (essentially, the fields
757of the object wrapped by it), by calling ``GetChildMemberWithName()``, passing
758it the child's name as a string.
759
760If the variable has a value, you can ask for it, and return it as a string
761using ``GetValue()``, or as a signed/unsigned number using
762``GetValueAsSigned()``, ``GetValueAsUnsigned()``. It is also possible to
763retrieve an `SBData` object by calling ``GetData()`` and then read the object's
764contents out of the `SBData`.
765
766If you need to delve into several levels of hierarchy, as you can do with
767summary strings, you can use the method ``GetValueForExpressionPath()``,
768passing it an expression path just like those you could use for summary strings
769(one of the differences is that dereferencing a pointer does not occur by
770prefixing the path with a ``*```, but by calling the ``Dereference()`` method
771on the returned `SBValue`). If you need to access array slices, you cannot do
772that (yet) via this method call, and you must use ``GetChildAtIndex()``
773querying it for the array items one by one. Also, handling custom formats is
774something you have to deal with on your own.
775
776``options`` Python summary formatters can optionally define this
777third argument, which is an object of type ``lldb.SBTypeSummaryOptions``,
778allowing for a few customizations of the result. The decision to
779adopt or not this third argument - and the meaning of options
780thereof - is up to the individual formatter's writer.
781
782Other than interactively typing a Python script there are two other ways for
783you to input a Python script as a summary:
784
785- using the --python-script option to type summary add and typing the script
786  code as an option argument; as in:
787
788::
789
790   (lldb) type summary add --python-script "height = valobj.GetChildMemberWithName('height').GetValueAsUnsigned(0);width = valobj.GetChildMemberWithName('width').GetValueAsUnsigned(0); return 'Area: %d' % (height*width)" Rectangle
791
792
793- using the --python-function (-F) option to type summary add and giving the
794  name of a Python function with the correct prototype. Most probably, you will
795  define (or have already defined) the function in the interactive interpreter,
796  or somehow loaded it from a file, using the command script import command.
797  LLDB will emit a warning if it is unable to find the function you passed, but
798  will still register the binding.
799
800Regular Expression Typenames
801----------------------------
802
803As you noticed, in order to associate the custom summary string to the array
804types, one must give the array size as part of the typename. This can long
805become tiresome when using arrays of different sizes, Simple [3], Simple [9],
806Simple [12], ...
807
808If you use the -x option, type names are treated as regular expressions instead
809of type names. This would let you rephrase the above example for arrays of type
810Simple [3] as:
811
812::
813   (lldb) type summary add --summary-string "${var[].x}" -x "Simple \[[0-9]+\]"
814   (lldb) frame variable
815   (Simple [3]) sarray = [1,4,7]
816   (Simple [2]) sother = [3,6]
817
818The above scenario works for Simple [3] as well as for any other array of
819Simple objects.
820
821While this feature is mostly useful for arrays, you could also use regular
822expressions to catch other type sets grouped by name. However, as regular
823expression matching is slower than normal name matching, LLDB will first try to
824match by name in any way it can, and only when this fails, will it resort to
825regular expression matching.
826
827One of the ways LLDB uses this feature internally, is to match the names of STL
828container classes, regardless of the template arguments provided. The details
829for this are found at FormatManager.cpp
830
831The regular expression language used by LLDB is the POSIX extended language, as
832defined by the Single UNIX Specification, of which macOS is a compliant
833implementation.
834
835Names Summaries
836---------------
837
838For a given type, there may be different meaningful summary representations.
839However, currently, only one summary can be associated to a type at each
840moment. If you need to temporarily override the association for a variable,
841without changing the summary string for to its type, you can use named
842summaries.
843
844Named summaries work by attaching a name to a summary when creating it. Then,
845when there is a need to attach the summary to a variable, the frame variable
846command, supports a --summary option that tells LLDB to use the named summary
847given instead of the default one.
848
849::
850
851   (lldb) type summary add --summary-string "x=${var.integer}" --name NamedSummary
852   (lldb) frame variable one
853   (i_am_cool) one = int = 3, float = 3.14159, char = 69
854   (lldb) frame variable one --summary NamedSummary
855   (i_am_cool) one = x=3
856
857When defining a named summary, binding it to one or more types becomes
858optional. Even if you bind the named summary to a type, and later change the
859summary string for that type, the named summary will not be changed by that.
860You can delete named summaries by using the type summary delete command, as if
861the summary name was the datatype that the summary is applied to
862
863A summary attached to a variable using the --summary option, has the same
864semantics that a custom format attached using the -f option has: it stays
865attached till you attach a new one, or till you let your program run again.
866
867Synthetic Children
868------------------
869
870Summaries work well when one is able to navigate through an expression path. In
871order for LLDB to do so, appropriate debugging information must be available.
872
873Some types are opaque, i.e. no knowledge of their internals is provided. When
874that's the case, expression paths do not work correctly.
875
876In other cases, the internals are available to use in expression paths, but
877they do not provide a user-friendly representation of the object's value.
878
879For instance, consider an STL vector, as implemented by the GNU C++ Library:
880
881::
882
883   (lldb) frame variable numbers -T
884   (std::vector<int>) numbers = {
885      (std::_Vector_base<int, std::allocator<int> >) std::_Vector_base<int, std::allocator<int> > = {
886         (std::_Vector_base<int, std::allocator&tl;int> >::_Vector_impl) _M_impl = {
887               (int *) _M_start = 0x00000001001008a0
888               (int *) _M_finish = 0x00000001001008a8
889               (int *) _M_end_of_storage = 0x00000001001008a8
890         }
891      }
892   }
893
894Here, you can see how the type is implemented, and you can write a summary for
895that implementation but that is not going to help you infer what items are
896actually stored in the vector.
897
898What you would like to see is probably something like:
899
900::
901
902   (lldb) frame variable numbers -T
903   (std::vector<int>) numbers = {
904      (int) [0] = 1
905      (int) [1] = 12
906      (int) [2] = 123
907      (int) [3] = 1234
908   }
909
910Synthetic children are a way to get that result.
911
912The feature is based upon the idea of providing a new set of children for a
913variable that replaces the ones available by default through the debug
914information. In the example, we can use synthetic children to provide the
915vector items as children for the std::vector object.
916
917In order to create synthetic children, you need to provide a Python class that
918adheres to a given interface (the word is italicized because Python has no
919explicit notion of interface, by that word we mean a given set of methods must
920be implemented by the Python class):
921
922.. code-block:: python
923
924   class SyntheticChildrenProvider:
925      def __init__(self, valobj, internal_dict):
926         this call should initialize the Python object using valobj as the variable to provide synthetic children for
927      def num_children(self):
928         this call should return the number of children that you want your object to have
929      def get_child_index(self,name):
930         this call should return the index of the synthetic child whose name is given as argument
931      def get_child_at_index(self,index):
932         this call should return a new LLDB SBValue object representing the child at the index given as argument
933      def update(self):
934         this call should be used to update the internal state of this Python object whenever the state of the variables in LLDB changes.[1]
935         Also, this method is invoked before any other method in the interface.
936      def has_children(self):
937         this call should return True if this object might have children, and False if this object can be guaranteed not to have children.[2]
938      def get_value(self):
939         this call can return an SBValue to be presented as the value of the synthetic value under consideration.[3]
940
941As a warning, exceptions that are thrown by python formatters are caught
942silently by LLDB and should be handled appropriately by the formatter itself.
943Being more specific, in case of exceptions, LLDB might assume that the given
944object has no children or it might skip printing some children, as they are
945printed one by one.
946
947[1] This method is optional. Also, a boolean value must be returned
948(starting with SVN rev153061/LLDB-134). If ``False`` is returned, then
949whenever the process reaches a new stop, this method will be invoked again to
950generate an updated list of the children for a given variable. Otherwise, if
951``True`` is returned, then the value is cached and this method won't be called
952again, effectively freezing the state of the value in subsequent stops. Beware
953that returning ``True`` incorrectly could show misleading information to the
954user.
955
956[2] This method is optional (starting with SVN rev166495/LLDB-175). While
957implementing it in terms of num_children is acceptable, implementors are
958encouraged to look for optimized coding alternatives whenever reasonable.
959
960[3] This method is optional (starting with SVN revision 219330). The `SBValue`
961you return here will most likely be a numeric type (int, float, ...) as its
962value bytes will be used as-if they were the value of the root `SBValue` proper.
963As a shortcut for this, you can inherit from lldb.SBSyntheticValueProvider, and
964just define get_value as other methods are defaulted in the superclass as
965returning default no-children responses.
966
967If a synthetic child provider supplies a special child named
968``$$dereference$$`` then it will be used when evaluating ``operator *`` and
969``operator ->`` in the frame variable command and related SB API
970functions. It is possible to declare this synthetic child without
971including it in the range of children displayed by LLDB. For example,
972this subset of a synthetic children provider class would allow the
973synthetic value to be dereferenced without actually showing any
974synthtic children in the UI:
975
976.. code-block:: python
977
978      class SyntheticChildrenProvider:
979          [...]
980          def num_children(self):
981              return 0
982          def get_child_index(self, name):
983              if name == '$$dereference$$':
984                  return 0
985              return -1
986          def get_child_at_index(self, index):
987              if index == 0:
988                  return <valobj resulting from dereference>
989              return None
990
991
992For examples of how synthetic children are created, you are encouraged to look
993at examples/synthetic in the LLDB trunk. Please, be aware that the code in
994those files (except bitfield/) is legacy code and is not maintained. You may
995especially want to begin looking at this example to get a feel for this
996feature, as it is a very easy and well commented example.
997
998The design pattern consistently used in synthetic providers shipping with LLDB
999is to use the __init__ to store the `SBValue` instance as a part of self. The
1000update function is then used to perform the actual initialization. Once a
1001synthetic children provider is written, one must load it into LLDB before it
1002can be used. Currently, one can use the LLDB script command to type Python code
1003interactively, or use the command script import fileName command to load Python
1004code from a Python module (ordinary rules apply to importing modules this way).
1005A third option is to type the code for the provider class interactively while
1006adding it.
1007
1008For example, let's pretend we have a class Foo for which a synthetic children
1009provider class Foo_Provider is available, in a Python module contained in file
1010~/Foo_Tools.py. The following interaction sets Foo_Provider as a synthetic
1011children provider in LLDB:
1012
1013::
1014
1015   (lldb) command script import ~/Foo_Tools.py
1016   (lldb) type synthetic add Foo --python-class Foo_Tools.Foo_Provider
1017   (lldb) frame variable a_foo
1018   (Foo) a_foo = {
1019      x = 1
1020      y = "Hello world"
1021   }
1022
1023LLDB has synthetic children providers for a core subset of STL classes, both in
1024the version provided by libstdcpp and by libcxx, as well as for several
1025Foundation classes.
1026
1027Synthetic children extend summary strings by enabling a new special variable:
1028``${svar``.
1029
1030This symbol tells LLDB to refer expression paths to the synthetic children
1031instead of the real ones. For instance,
1032
1033::
1034
1035   (lldb) type summary add --expand -x "std::vector<" --summary-string "${svar%#} items"
1036   (lldb) frame variable numbers
1037   (std::vector<int>) numbers = 4 items {
1038      (int) [0] = 1
1039      (int) [1] = 12
1040      (int) [2] = 123
1041      (int) [3] = 1234
1042   }
1043
1044It's important to mention that LLDB invokes the synthetic child provider before
1045invoking the summary string provider, which allows the latter to have access to
1046the actual displayable children. This applies to both inlined summary strings
1047and python-based summary providers.
1048
1049
1050As a warning, when programmatically accessing the children or children count of
1051a variable that has a synthetic child provider, notice that LLDB hides the
1052actual raw children. For example, suppose we have a ``std::vector``, which has
1053an actual in-memory property ``__begin`` marking the beginning of its data.
1054After the synthetic child provider is executed, the ``std::vector`` variable
1055won't show ``__begin`` as child anymore, even through the SB API. It will have
1056instead the children calculated by the provider. In case the actual raw
1057children are needed, a call to ``value.GetNonSyntheticValue()`` is enough to
1058get a raw version of the value. It is import to remember this when implementing
1059summary string providers, as they run after the synthetic child provider.
1060
1061
1062In some cases, if LLDB is unable to use the real object to get a child
1063specified in an expression path, it will automatically refer to the synthetic
1064children. While in summaries it is best to always use ${svar to make your
1065intentions clearer, interactive debugging can benefit from this behavior, as
1066in:
1067
1068::
1069
1070   (lldb) frame variable numbers[0] numbers[1]
1071   (int) numbers[0] = 1
1072   (int) numbers[1] = 12
1073
1074Unlike many other visualization features, however, the access to synthetic
1075children only works when using frame variable, and is not supported in
1076expression:
1077
1078::
1079
1080   (lldb) expression numbers[0]
1081   Error [IRForTarget]: Call to a function '_ZNSt33vector<int, std::allocator<int> >ixEm' that is not present in the target
1082   error: Couldn't convert the expression to DWARF
1083
1084The reason for this is that classes might have an overloaded ``operator []``,
1085or other special provisions and the expression command chooses to ignore
1086synthetic children in the interest of equivalency with code you asked to have
1087compiled from source.
1088
1089Filters
1090-------
1091
1092Filters are a solution to the display of complex classes. At times, classes
1093have many member variables but not all of these are actually necessary for the
1094user to see.
1095
1096A filter will solve this issue by only letting the user see those member
1097variables they care about. Of course, the equivalent of a filter can be
1098implemented easily using synthetic children, but a filter lets you get the job
1099done without having to write Python code.
1100
1101For instance, if your class Foobar has member variables named A thru Z, but you
1102only need to see the ones named B, H and Q, you can define a filter:
1103
1104::
1105
1106   (lldb) type filter add Foobar --child B --child H --child Q
1107   (lldb) frame variable a_foobar
1108   (Foobar) a_foobar = {
1109      (int) B = 1
1110      (char) H = 'H'
1111      (std::string) Q = "Hello world"
1112   }
1113
1114Objective-C Dynamic Type Discovery
1115----------------------------------
1116
1117When doing Objective-C development, you may notice that some of your variables
1118come out as of type id (for instance, items extracted from NSArray). By
1119default, LLDB will not show you the real type of the object. it can actually
1120dynamically discover the type of an Objective-C variable, much like the runtime
1121itself does when invoking a selector. In order to be shown the result of that
1122discovery that, however, a special option to frame variable or expression is
1123required: ``--dynamic-type``.
1124
1125
1126``--dynamic-type`` can have one of three values:
1127
1128- ``no-dynamic-values``: the default, prevents dynamic type discovery
1129- ``no-run-target``: enables dynamic type discovery as long as running code on
1130  the target is not required
1131- ``run-target``: enables code execution on the target in order to perform
1132  dynamic type discovery
1133
1134If you specify a value of either no-run-target or run-target, LLDB will detect
1135the dynamic type of your variables and show the appropriate formatters for
1136them. As an example:
1137
1138::
1139
1140   (lldb) expr @"Hello"
1141   (NSString *) $0 = 0x00000001048000b0 @"Hello"
1142   (lldb) expr -d no-run @"Hello"
1143   (__NSCFString *) $1 = 0x00000001048000b0 @"Hello"
1144
1145Because LLDB uses a detection algorithm that does not need to invoke any
1146functions on the target process, no-run-target is enough for this to work.
1147
1148As a side note, the summary for NSString shown in the example is built right
1149into LLDB. It was initially implemented through Python (the code is still
1150available for reference at CFString.py). However, this is out of sync with the
1151current implementation of the NSString formatter (which is a C++ function
1152compiled into the LLDB core).
1153
1154Categories
1155----------
1156
1157Categories are a way to group related formatters. For instance, LLDB itself
1158groups the formatters for the libstdc++ types in a category named
1159gnu-libstdc++. Basically, categories act like containers in which to store
1160formatters for a same library or OS release.
1161
1162By default, several categories are created in LLDB:
1163
1164- default: this is the category where every formatter ends up, unless another category is specified
1165- objc: formatters for basic and common Objective-C types that do not specifically depend on macOS
1166- gnu-libstdc++: formatters for std::string, std::vector, std::list and std::map as implemented by libstdcpp
1167- libcxx: formatters for std::string, std::vector, std::list and std::map as implemented by libcxx
1168- system: truly basic types for which a formatter is required
1169- AppKit: Cocoa classes
1170- CoreFoundation: CF classes
1171- CoreGraphics: CG classes
1172- CoreServices: CS classes
1173- VectorTypes: compact display for several vector types
1174
1175If you want to use a custom category for your formatters, all the type ... add
1176provide a --category (-w) option, that names the category to add the formatter
1177to. To delete the formatter, you then have to specify the correct category.
1178
1179Categories can be in one of two states: enabled and disabled. A category is
1180initially disabled, and can be enabled using the type category enable command.
1181To disable an enabled category, the command to use is type category disable.
1182
1183The order in which categories are enabled or disabled is significant, in that
1184LLDB uses that order when looking for formatters. Therefore, when you enable a
1185category, it becomes the second one to be searched (after default, which always
1186stays on top of the list). The default categories are enabled in such a way
1187that the search order is:
1188
1189- default
1190- objc
1191- CoreFoundation
1192- AppKit
1193- CoreServices
1194- CoreGraphics
1195- gnu-libstdc++
1196- libcxx
1197- VectorTypes
1198- system
1199
1200As said, gnu-libstdc++ and libcxx contain formatters for C++ STL data types.
1201system contains formatters for char* and char[], which reflect the behavior of
1202older versions of LLDB which had built-in formatters for these types. Because
1203now these are formatters, you can even replace them with your own if so you
1204wish.
1205
1206There is no special command to create a category. When you place a formatter in
1207a category, if that category does not exist, it is automatically created. For
1208instance,
1209
1210::
1211
1212   (lldb) type summary add Foobar --summary-string "a foobar" --category newcategory
1213
1214automatically creates a (disabled) category named newcategory.
1215
1216Another way to create a new (empty) category, is to enable it, as in:
1217
1218::
1219
1220   (lldb) type category enable newcategory
1221
1222However, in this case LLDB warns you that enabling an empty category has no
1223effect. If you add formatters to the category after enabling it, they will be
1224honored. But an empty category per se does not change the way any type is
1225displayed. The reason the debugger warns you is that enabling an empty category
1226might be a typo, and you effectively wanted to enable a similarly-named but
1227not-empty category.
1228
1229Finding Formatters 101
1230----------------------
1231
1232Searching for a formatter (including formats, starting in SVN rev r192217)
1233given a variable goes through a rather intricate set of rules. Namely, what
1234happens is that LLDB starts looking in each enabled category, according to the
1235order in which they were enabled (latest enabled first). In each category, LLDB
1236does the following:
1237
1238- If there is a formatter for the type of the variable, use it
1239- If this object is a pointer, and there is a formatter for the pointee type
1240  that does not skip pointers, use it
1241- If this object is a reference, and there is a formatter for the referred type
1242  that does not skip references, use it
1243- If this object is an Objective-C class and dynamic types are enabled, look
1244  for a formatter for the dynamic type of the object. If dynamic types are
1245  disabled, or the search failed, look for a formatter for the declared type of
1246  the object
1247- If this object's type is a typedef, go through typedef hierarchy (LLDB might
1248  not be able to do this if the compiler has not emitted enough information. If
1249  the required information to traverse typedef hierarchies is missing, type
1250  cascading will not work. The clang compiler, part of the LLVM project, emits
1251  the correct debugging information for LLDB to cascade). If at any level of
1252  the hierarchy there is a valid formatter that can cascade, use it.
1253- If everything has failed, repeat the above search, looking for regular
1254  expressions instead of exact matches
1255
1256If any of those attempts returned a valid formatter to be used, that one is
1257used, and the search is terminated (without going to look in other categories).
1258If nothing was found in the current category, the next enabled category is
1259scanned according to the same algorithm. If there are no more enabled
1260categories, the search has failed.
1261
1262**Warning**: previous versions of LLDB defined cascading to mean not only going
1263through typedef chains, but also through inheritance chains. This feature has
1264been removed since it significantly degrades performance. You need to set up
1265your formatters for every type in inheritance chains to which you want the
1266formatter to apply.
1267