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