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