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