1================================ 2Source Level Debugging with LLVM 3================================ 4 5.. contents:: 6 :local: 7 8Introduction 9============ 10 11This document is the central repository for all information pertaining to debug 12information in LLVM. It describes the :ref:`actual format that the LLVM debug 13information takes <format>`, which is useful for those interested in creating 14front-ends or dealing directly with the information. Further, this document 15provides specific examples of what debug information for C/C++ looks like. 16 17Philosophy behind LLVM debugging information 18-------------------------------------------- 19 20The idea of the LLVM debugging information is to capture how the important 21pieces of the source-language's Abstract Syntax Tree map onto LLVM code. 22Several design aspects have shaped the solution that appears here. The 23important ones are: 24 25* Debugging information should have very little impact on the rest of the 26 compiler. No transformations, analyses, or code generators should need to 27 be modified because of debugging information. 28 29* LLVM optimizations should interact in :ref:`well-defined and easily described 30 ways <intro_debugopt>` with the debugging information. 31 32* Because LLVM is designed to support arbitrary programming languages, 33 LLVM-to-LLVM tools should not need to know anything about the semantics of 34 the source-level-language. 35 36* Source-level languages are often **widely** different from one another. 37 LLVM should not put any restrictions of the flavor of the source-language, 38 and the debugging information should work with any language. 39 40* With code generator support, it should be possible to use an LLVM compiler 41 to compile a program to native machine code and standard debugging 42 formats. This allows compatibility with traditional machine-code level 43 debuggers, like GDB or DBX. 44 45The approach used by the LLVM implementation is to use a small set of 46:ref:`intrinsic functions <format_common_intrinsics>` to define a mapping 47between LLVM program objects and the source-level objects. The description of 48the source-level program is maintained in LLVM metadata in an 49:ref:`implementation-defined format <ccxx_frontend>` (the C/C++ front-end 50currently uses working draft 7 of the `DWARF 3 standard 51<http://www.eagercon.com/dwarf/dwarf3std.htm>`_). 52 53When a program is being debugged, a debugger interacts with the user and turns 54the stored debug information into source-language specific information. As 55such, a debugger must be aware of the source-language, and is thus tied to a 56specific language or family of languages. 57 58Debug information consumers 59--------------------------- 60 61The role of debug information is to provide meta information normally stripped 62away during the compilation process. This meta information provides an LLVM 63user a relationship between generated code and the original program source 64code. 65 66Currently, debug information is consumed by DwarfDebug to produce dwarf 67information used by the gdb debugger. Other targets could use the same 68information to produce stabs or other debug forms. 69 70It would also be reasonable to use debug information to feed profiling tools 71for analysis of generated code, or, tools for reconstructing the original 72source from generated code. 73 74TODO - expound a bit more. 75 76.. _intro_debugopt: 77 78Debugging optimized code 79------------------------ 80 81An extremely high priority of LLVM debugging information is to make it interact 82well with optimizations and analysis. In particular, the LLVM debug 83information provides the following guarantees: 84 85* LLVM debug information **always provides information to accurately read 86 the source-level state of the program**, regardless of which LLVM 87 optimizations have been run, and without any modification to the 88 optimizations themselves. However, some optimizations may impact the 89 ability to modify the current state of the program with a debugger, such 90 as setting program variables, or calling functions that have been 91 deleted. 92 93* As desired, LLVM optimizations can be upgraded to be aware of the LLVM 94 debugging information, allowing them to update the debugging information 95 as they perform aggressive optimizations. This means that, with effort, 96 the LLVM optimizers could optimize debug code just as well as non-debug 97 code. 98 99* LLVM debug information does not prevent optimizations from 100 happening (for example inlining, basic block reordering/merging/cleanup, 101 tail duplication, etc). 102 103* LLVM debug information is automatically optimized along with the rest of 104 the program, using existing facilities. For example, duplicate 105 information is automatically merged by the linker, and unused information 106 is automatically removed. 107 108Basically, the debug information allows you to compile a program with 109"``-O0 -g``" and get full debug information, allowing you to arbitrarily modify 110the program as it executes from a debugger. Compiling a program with 111"``-O3 -g``" gives you full debug information that is always available and 112accurate for reading (e.g., you get accurate stack traces despite tail call 113elimination and inlining), but you might lose the ability to modify the program 114and call functions where were optimized out of the program, or inlined away 115completely. 116 117:ref:`LLVM test suite <test-suite-quickstart>` provides a framework to test 118optimizer's handling of debugging information. It can be run like this: 119 120.. code-block:: bash 121 122 % cd llvm/projects/test-suite/MultiSource/Benchmarks # or some other level 123 % make TEST=dbgopt 124 125This will test impact of debugging information on optimization passes. If 126debugging information influences optimization passes then it will be reported 127as a failure. See :doc:`TestingGuide` for more information on LLVM test 128infrastructure and how to run various tests. 129 130.. _format: 131 132Debugging information format 133============================ 134 135LLVM debugging information has been carefully designed to make it possible for 136the optimizer to optimize the program and debugging information without 137necessarily having to know anything about debugging information. In 138particular, the use of metadata avoids duplicated debugging information from 139the beginning, and the global dead code elimination pass automatically deletes 140debugging information for a function if it decides to delete the function. 141 142To do this, most of the debugging information (descriptors for types, 143variables, functions, source files, etc) is inserted by the language front-end 144in the form of LLVM metadata. 145 146Debug information is designed to be agnostic about the target debugger and 147debugging information representation (e.g. DWARF/Stabs/etc). It uses a generic 148pass to decode the information that represents variables, types, functions, 149namespaces, etc: this allows for arbitrary source-language semantics and 150type-systems to be used, as long as there is a module written for the target 151debugger to interpret the information. 152 153To provide basic functionality, the LLVM debugger does have to make some 154assumptions about the source-level language being debugged, though it keeps 155these to a minimum. The only common features that the LLVM debugger assumes 156exist are :ref:`source files <format_files>`, and :ref:`program objects 157<format_global_variables>`. These abstract objects are used by a debugger to 158form stack traces, show information about local variables, etc. 159 160This section of the documentation first describes the representation aspects 161common to any source-language. :ref:`ccxx_frontend` describes the data layout 162conventions used by the C and C++ front-ends. 163 164Debug information descriptors 165----------------------------- 166 167In consideration of the complexity and volume of debug information, LLVM 168provides a specification for well formed debug descriptors. 169 170Consumers of LLVM debug information expect the descriptors for program objects 171to start in a canonical format, but the descriptors can include additional 172information appended at the end that is source-language specific. All debugging 173information objects start with a tag to indicate what type of object it is. 174The source-language is allowed to define its own objects, by using unreserved 175tag numbers. We recommend using with tags in the range 0x1000 through 0x2000 176(there is a defined ``enum DW_TAG_user_base = 0x1000``.) 177 178The fields of debug descriptors used internally by LLVM are restricted to only 179the simple data types ``i32``, ``i1``, ``float``, ``double``, ``mdstring`` and 180``mdnode``. 181 182.. code-block:: llvm 183 184 !1 = metadata !{ 185 i32, ;; A tag 186 ... 187 } 188 189<a name="LLVMDebugVersion">The first field of a descriptor is always an 190``i32`` containing a tag value identifying the content of the descriptor. 191The remaining fields are specific to the descriptor. The values of tags are 192loosely bound to the tag values of DWARF information entries. However, that 193does not restrict the use of the information supplied to DWARF targets. 194 195The details of the various descriptors follow. 196 197Compile unit descriptors 198^^^^^^^^^^^^^^^^^^^^^^^^ 199 200.. code-block:: llvm 201 202 !0 = metadata !{ 203 i32, ;; Tag = 17 (DW_TAG_compile_unit) 204 metadata, ;; Source directory (including trailing slash) & file pair 205 i32, ;; DWARF language identifier (ex. DW_LANG_C89) 206 metadata ;; Producer (ex. "4.0.1 LLVM (LLVM research group)") 207 i1, ;; True if this is optimized. 208 metadata, ;; Flags 209 i32 ;; Runtime version 210 metadata ;; List of enums types 211 metadata ;; List of retained types 212 metadata ;; List of subprograms 213 metadata ;; List of global variables 214 metadata ;; List of imported entities 215 metadata ;; Split debug filename 216 i32 ;; Debug info emission kind (1 = Full Debug Info, 2 = Line Tables Only) 217 } 218 219These descriptors contain a source language ID for the file (we use the DWARF 2203.0 ID numbers, such as ``DW_LANG_C89``, ``DW_LANG_C_plus_plus``, 221``DW_LANG_Cobol74``, etc), a reference to a metadata node containing a pair of 222strings for the source file name and the working directory, as well as an 223identifier string for the compiler that produced it. 224 225Compile unit descriptors provide the root context for objects declared in a 226specific compilation unit. File descriptors are defined using this context. 227These descriptors are collected by a named metadata ``!llvm.dbg.cu``. They 228keep track of subprograms, global variables, type information, and imported 229entities (declarations and namespaces). 230 231.. _format_files: 232 233File descriptors 234^^^^^^^^^^^^^^^^ 235 236.. code-block:: llvm 237 238 !0 = metadata !{ 239 i32, ;; Tag = 41 (DW_TAG_file_type) 240 metadata, ;; Source directory (including trailing slash) & file pair 241 } 242 243These descriptors contain information for a file. Global variables and top 244level functions would be defined using this context. File descriptors also 245provide context for source line correspondence. 246 247Each input file is encoded as a separate file descriptor in LLVM debugging 248information output. 249 250.. _format_global_variables: 251 252Global variable descriptors 253^^^^^^^^^^^^^^^^^^^^^^^^^^^ 254 255.. code-block:: llvm 256 257 !1 = metadata !{ 258 i32, ;; Tag = 52 (DW_TAG_variable) 259 i32, ;; Unused field. 260 metadata, ;; Reference to context descriptor 261 metadata, ;; Name 262 metadata, ;; Display name (fully qualified C++ name) 263 metadata, ;; MIPS linkage name (for C++) 264 metadata, ;; Reference to file where defined 265 i32, ;; Line number where defined 266 metadata, ;; Reference to type descriptor 267 i1, ;; True if the global is local to compile unit (static) 268 i1, ;; True if the global is defined in the compile unit (not extern) 269 {}*, ;; Reference to the global variable 270 metadata, ;; The static member declaration, if any 271 } 272 273These descriptors provide debug information about global variables. They 274provide details such as name, type and where the variable is defined. All 275global variables are collected inside the named metadata ``!llvm.dbg.cu``. 276 277.. _format_subprograms: 278 279Subprogram descriptors 280^^^^^^^^^^^^^^^^^^^^^^ 281 282.. code-block:: llvm 283 284 !2 = metadata !{ 285 i32, ;; Tag = 46 (DW_TAG_subprogram) 286 metadata, ;; Source directory (including trailing slash) & file pair 287 metadata, ;; Reference to context descriptor 288 metadata, ;; Name 289 metadata, ;; Display name (fully qualified C++ name) 290 metadata, ;; MIPS linkage name (for C++) 291 i32, ;; Line number where defined 292 metadata, ;; Reference to type descriptor 293 i1, ;; True if the global is local to compile unit (static) 294 i1, ;; True if the global is defined in the compile unit (not extern) 295 i32, ;; Virtuality, e.g. dwarf::DW_VIRTUALITY__virtual 296 i32, ;; Index into a virtual function 297 metadata, ;; indicates which base type contains the vtable pointer for the 298 ;; derived class 299 i32, ;; Flags - Artificial, Private, Protected, Explicit, Prototyped. 300 i1, ;; isOptimized 301 {}*, ;; Reference to the LLVM function 302 metadata, ;; Lists function template parameters 303 metadata, ;; Function declaration descriptor 304 metadata, ;; List of function variables 305 i32 ;; Line number where the scope of the subprogram begins 306 } 307 308These descriptors provide debug information about functions, methods and 309subprograms. They provide details such as name, return types and the source 310location where the subprogram is defined. 311 312Block descriptors 313^^^^^^^^^^^^^^^^^ 314 315.. code-block:: llvm 316 317 !3 = metadata !{ 318 i32, ;; Tag = 11 (DW_TAG_lexical_block) 319 metadata, ;; Source directory (including trailing slash) & file pair 320 metadata, ;; Reference to context descriptor 321 i32, ;; Line number 322 i32, ;; Column number 323 i32 ;; Unique ID to identify blocks from a template function 324 } 325 326This descriptor provides debug information about nested blocks within a 327subprogram. The line number and column numbers are used to dinstinguish two 328lexical blocks at same depth. 329 330.. code-block:: llvm 331 332 !3 = metadata !{ 333 i32, ;; Tag = 11 (DW_TAG_lexical_block) 334 metadata, ;; Source directory (including trailing slash) & file pair 335 metadata ;; Reference to the scope we're annotating with a file change 336 i32, ;; DWARF path discriminator value 337 } 338 339This descriptor provides a wrapper around a lexical scope to handle file 340changes in the middle of a lexical block. 341 342.. _format_basic_type: 343 344Basic type descriptors 345^^^^^^^^^^^^^^^^^^^^^^ 346 347.. code-block:: llvm 348 349 !4 = metadata !{ 350 i32, ;; Tag = 36 (DW_TAG_base_type) 351 metadata, ;; Source directory (including trailing slash) & file pair (may be null) 352 metadata, ;; Reference to context 353 metadata, ;; Name (may be "" for anonymous types) 354 i32, ;; Line number where defined (may be 0) 355 i64, ;; Size in bits 356 i64, ;; Alignment in bits 357 i64, ;; Offset in bits 358 i32, ;; Flags 359 i32 ;; DWARF type encoding 360 } 361 362These descriptors define primitive types used in the code. Example ``int``, 363``bool`` and ``float``. The context provides the scope of the type, which is 364usually the top level. Since basic types are not usually user defined the 365context and line number can be left as NULL and 0. The size, alignment and 366offset are expressed in bits and can be 64 bit values. The alignment is used 367to round the offset when embedded in a :ref:`composite type 368<format_composite_type>` (example to keep float doubles on 64 bit boundaries). 369The offset is the bit offset if embedded in a :ref:`composite type 370<format_composite_type>`. 371 372The type encoding provides the details of the type. The values are typically 373one of the following: 374 375.. code-block:: llvm 376 377 DW_ATE_address = 1 378 DW_ATE_boolean = 2 379 DW_ATE_float = 4 380 DW_ATE_signed = 5 381 DW_ATE_signed_char = 6 382 DW_ATE_unsigned = 7 383 DW_ATE_unsigned_char = 8 384 385.. _format_derived_type: 386 387Derived type descriptors 388^^^^^^^^^^^^^^^^^^^^^^^^ 389 390.. code-block:: llvm 391 392 !5 = metadata !{ 393 i32, ;; Tag (see below) 394 metadata, ;; Source directory (including trailing slash) & file pair (may be null) 395 metadata, ;; Reference to context 396 metadata, ;; Name (may be "" for anonymous types) 397 i32, ;; Line number where defined (may be 0) 398 i64, ;; Size in bits 399 i64, ;; Alignment in bits 400 i64, ;; Offset in bits 401 i32, ;; Flags to encode attributes, e.g. private 402 metadata, ;; Reference to type derived from 403 metadata, ;; (optional) Name of the Objective C property associated with 404 ;; Objective-C an ivar, or the type of which this 405 ;; pointer-to-member is pointing to members of. 406 metadata, ;; (optional) Name of the Objective C property getter selector. 407 metadata, ;; (optional) Name of the Objective C property setter selector. 408 i32 ;; (optional) Objective C property attributes. 409 } 410 411These descriptors are used to define types derived from other types. The value 412of the tag varies depending on the meaning. The following are possible tag 413values: 414 415.. code-block:: llvm 416 417 DW_TAG_formal_parameter = 5 418 DW_TAG_member = 13 419 DW_TAG_pointer_type = 15 420 DW_TAG_reference_type = 16 421 DW_TAG_typedef = 22 422 DW_TAG_ptr_to_member_type = 31 423 DW_TAG_const_type = 38 424 DW_TAG_volatile_type = 53 425 DW_TAG_restrict_type = 55 426 427``DW_TAG_member`` is used to define a member of a :ref:`composite type 428<format_composite_type>` or :ref:`subprogram <format_subprograms>`. The type 429of the member is the :ref:`derived type <format_derived_type>`. 430``DW_TAG_formal_parameter`` is used to define a member which is a formal 431argument of a subprogram. 432 433``DW_TAG_typedef`` is used to provide a name for the derived type. 434 435``DW_TAG_pointer_type``, ``DW_TAG_reference_type``, ``DW_TAG_const_type``, 436``DW_TAG_volatile_type`` and ``DW_TAG_restrict_type`` are used to qualify the 437:ref:`derived type <format_derived_type>`. 438 439:ref:`Derived type <format_derived_type>` location can be determined from the 440context and line number. The size, alignment and offset are expressed in bits 441and can be 64 bit values. The alignment is used to round the offset when 442embedded in a :ref:`composite type <format_composite_type>` (example to keep 443float doubles on 64 bit boundaries.) The offset is the bit offset if embedded 444in a :ref:`composite type <format_composite_type>`. 445 446Note that the ``void *`` type is expressed as a type derived from NULL. 447 448.. _format_composite_type: 449 450Composite type descriptors 451^^^^^^^^^^^^^^^^^^^^^^^^^^ 452 453.. code-block:: llvm 454 455 !6 = metadata !{ 456 i32, ;; Tag (see below) 457 metadata, ;; Source directory (including trailing slash) & file pair (may be null) 458 metadata, ;; Reference to context 459 metadata, ;; Name (may be "" for anonymous types) 460 i32, ;; Line number where defined (may be 0) 461 i64, ;; Size in bits 462 i64, ;; Alignment in bits 463 i64, ;; Offset in bits 464 i32, ;; Flags 465 metadata, ;; Reference to type derived from 466 metadata, ;; Reference to array of member descriptors 467 i32, ;; Runtime languages 468 metadata, ;; Base type containing the vtable pointer for this type 469 metadata, ;; Template parameters 470 metadata ;; A unique identifier for type uniquing purpose (may be null) 471 } 472 473These descriptors are used to define types that are composed of 0 or more 474elements. The value of the tag varies depending on the meaning. The following 475are possible tag values: 476 477.. code-block:: llvm 478 479 DW_TAG_array_type = 1 480 DW_TAG_enumeration_type = 4 481 DW_TAG_structure_type = 19 482 DW_TAG_union_type = 23 483 DW_TAG_subroutine_type = 21 484 DW_TAG_inheritance = 28 485 486The vector flag indicates that an array type is a native packed vector. 487 488The members of array types (tag = ``DW_TAG_array_type``) are 489:ref:`subrange descriptors <format_subrange>`, each 490representing the range of subscripts at that level of indexing. 491 492The members of enumeration types (tag = ``DW_TAG_enumeration_type``) are 493:ref:`enumerator descriptors <format_enumerator>`, each representing the 494definition of enumeration value for the set. All enumeration type descriptors 495are collected inside the named metadata ``!llvm.dbg.cu``. 496 497The members of structure (tag = ``DW_TAG_structure_type``) or union (tag = 498``DW_TAG_union_type``) types are any one of the :ref:`basic 499<format_basic_type>`, :ref:`derived <format_derived_type>` or :ref:`composite 500<format_composite_type>` type descriptors, each representing a field member of 501the structure or union. 502 503For C++ classes (tag = ``DW_TAG_structure_type``), member descriptors provide 504information about base classes, static members and member functions. If a 505member is a :ref:`derived type descriptor <format_derived_type>` and has a tag 506of ``DW_TAG_inheritance``, then the type represents a base class. If the member 507of is a :ref:`global variable descriptor <format_global_variables>` then it 508represents a static member. And, if the member is a :ref:`subprogram 509descriptor <format_subprograms>` then it represents a member function. For 510static members and member functions, ``getName()`` returns the members link or 511the C++ mangled name. ``getDisplayName()`` the simplied version of the name. 512 513The first member of subroutine (tag = ``DW_TAG_subroutine_type``) type elements 514is the return type for the subroutine. The remaining elements are the formal 515arguments to the subroutine. 516 517:ref:`Composite type <format_composite_type>` location can be determined from 518the context and line number. The size, alignment and offset are expressed in 519bits and can be 64 bit values. The alignment is used to round the offset when 520embedded in a :ref:`composite type <format_composite_type>` (as an example, to 521keep float doubles on 64 bit boundaries). The offset is the bit offset if 522embedded in a :ref:`composite type <format_composite_type>`. 523 524.. _format_subrange: 525 526Subrange descriptors 527^^^^^^^^^^^^^^^^^^^^ 528 529.. code-block:: llvm 530 531 !42 = metadata !{ 532 i32, ;; Tag = 33 (DW_TAG_subrange_type) 533 i64, ;; Low value 534 i64 ;; High value 535 } 536 537These descriptors are used to define ranges of array subscripts for an array 538:ref:`composite type <format_composite_type>`. The low value defines the lower 539bounds typically zero for C/C++. The high value is the upper bounds. Values 540are 64 bit. ``High - Low + 1`` is the size of the array. If ``Low > High`` 541the array bounds are not included in generated debugging information. 542 543.. _format_enumerator: 544 545Enumerator descriptors 546^^^^^^^^^^^^^^^^^^^^^^ 547 548.. code-block:: llvm 549 550 !6 = metadata !{ 551 i32, ;; Tag = 40 (DW_TAG_enumerator) 552 metadata, ;; Name 553 i64 ;; Value 554 } 555 556These descriptors are used to define members of an enumeration :ref:`composite 557type <format_composite_type>`, it associates the name to the value. 558 559Local variables 560^^^^^^^^^^^^^^^ 561 562.. code-block:: llvm 563 564 !7 = metadata !{ 565 i32, ;; Tag (see below) 566 metadata, ;; Context 567 metadata, ;; Name 568 metadata, ;; Reference to file where defined 569 i32, ;; 24 bit - Line number where defined 570 ;; 8 bit - Argument number. 1 indicates 1st argument. 571 metadata, ;; Reference to the type descriptor 572 i32, ;; flags 573 metadata ;; (optional) Reference to inline location 574 metadata ;; (optional) Reference to a complex expression. 575 } 576 577These descriptors are used to define variables local to a sub program. The 578value of the tag depends on the usage of the variable: 579 580.. code-block:: llvm 581 582 DW_TAG_auto_variable = 256 583 DW_TAG_arg_variable = 257 584 585An auto variable is any variable declared in the body of the function. An 586argument variable is any variable that appears as a formal argument to the 587function. 588 589The context is either the subprogram or block where the variable is defined. 590Name the source variable name. Context and line indicate where the variable 591was defined. Type descriptor defines the declared type of the variable. 592 593Complex Expressions 594^^^^^^^^^^^^^^^^^^^ 595.. code-block:: llvm 596 597 !8 = metadata !{ 598 i32, ;; DW_TAG_expression 599 ... 600 } 601 602Complex expressions describe variable storage locations in terms of 603prefix-notated DWARF expressions. Currently the only supported 604operators are ``DW_OP_plus``, ``DW_OP_deref``, and ``DW_OP_piece``. 605 606The ``DW_OP_piece`` operator is used for (typically larger aggregate) 607variables that are fragmented across several locations. It takes two 608i32 arguments, an offset and a size in bytes to describe which piece 609of the variable is at this location. 610 611 612.. _format_common_intrinsics: 613 614Debugger intrinsic functions 615^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 616 617LLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to 618provide debug information at various points in generated code. 619 620``llvm.dbg.declare`` 621^^^^^^^^^^^^^^^^^^^^ 622 623.. code-block:: llvm 624 625 void %llvm.dbg.declare(metadata, metadata) 626 627This intrinsic provides information about a local element (e.g., variable). 628The first argument is metadata holding the alloca for the variable. The second 629argument is metadata containing a description of the variable. 630 631``llvm.dbg.value`` 632^^^^^^^^^^^^^^^^^^ 633 634.. code-block:: llvm 635 636 void %llvm.dbg.value(metadata, i64, metadata) 637 638This intrinsic provides information when a user source variable is set to a new 639value. The first argument is the new value (wrapped as metadata). The second 640argument is the offset in the user source variable where the new value is 641written. The third argument is metadata containing a description of the user 642source variable. 643 644Object lifetimes and scoping 645============================ 646 647In many languages, the local variables in functions can have their lifetimes or 648scopes limited to a subset of a function. In the C family of languages, for 649example, variables are only live (readable and writable) within the source 650block that they are defined in. In functional languages, values are only 651readable after they have been defined. Though this is a very obvious concept, 652it is non-trivial to model in LLVM, because it has no notion of scoping in this 653sense, and does not want to be tied to a language's scoping rules. 654 655In order to handle this, the LLVM debug format uses the metadata attached to 656llvm instructions to encode line number and scoping information. Consider the 657following C fragment, for example: 658 659.. code-block:: c 660 661 1. void foo() { 662 2. int X = 21; 663 3. int Y = 22; 664 4. { 665 5. int Z = 23; 666 6. Z = X; 667 7. } 668 8. X = Y; 669 9. } 670 671Compiled to LLVM, this function would be represented like this: 672 673.. code-block:: llvm 674 675 define void @foo() #0 { 676 entry: 677 %X = alloca i32, align 4 678 %Y = alloca i32, align 4 679 %Z = alloca i32, align 4 680 call void @llvm.dbg.declare(metadata !{i32* %X}, metadata !10), !dbg !12 681 ; [debug line = 2:7] [debug variable = X] 682 store i32 21, i32* %X, align 4, !dbg !12 683 call void @llvm.dbg.declare(metadata !{i32* %Y}, metadata !13), !dbg !14 684 ; [debug line = 3:7] [debug variable = Y] 685 store i32 22, i32* %Y, align 4, !dbg !14 686 call void @llvm.dbg.declare(metadata !{i32* %Z}, metadata !15), !dbg !17 687 ; [debug line = 5:9] [debug variable = Z] 688 store i32 23, i32* %Z, align 4, !dbg !17 689 %0 = load i32* %X, align 4, !dbg !18 690 [debug line = 6:5] 691 store i32 %0, i32* %Z, align 4, !dbg !18 692 %1 = load i32* %Y, align 4, !dbg !19 693 [debug line = 8:3] 694 store i32 %1, i32* %X, align 4, !dbg !19 695 ret void, !dbg !20 696 } 697 698 ; Function Attrs: nounwind readnone 699 declare void @llvm.dbg.declare(metadata, metadata) #1 700 701 attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" 702 "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" 703 "no-infs-fp-math"="false" "no-nans-fp-math"="false" 704 "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" 705 "use-soft-float"="false" } 706 attributes #1 = { nounwind readnone } 707 708 !llvm.dbg.cu = !{!0} 709 !llvm.module.flags = !{!8} 710 !llvm.ident = !{!9} 711 712 !0 = metadata !{i32 786449, metadata !1, i32 12, 713 metadata !"clang version 3.4 (trunk 193128) (llvm/trunk 193139)", 714 i1 false, metadata !"", i32 0, metadata !2, metadata !2, metadata !3, 715 metadata !2, metadata !2, metadata !""} ; [ DW_TAG_compile_unit ] \ 716 [/private/tmp/foo.c] \ 717 [DW_LANG_C99] 718 !1 = metadata !{metadata !"t.c", metadata !"/private/tmp"} 719 !2 = metadata !{i32 0} 720 !3 = metadata !{metadata !4} 721 !4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"foo", 722 metadata !"foo", metadata !"", i32 1, metadata !6, 723 i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false, 724 void ()* @foo, null, null, metadata !2, i32 1} 725 ; [ DW_TAG_subprogram ] [line 1] [def] [foo] 726 !5 = metadata !{i32 786473, metadata !1} ; [ DW_TAG_file_type ] \ 727 [/private/tmp/t.c] 728 !6 = metadata !{i32 786453, i32 0, null, metadata !"", i32 0, i64 0, i64 0, 729 i64 0, i32 0, null, metadata !7, i32 0, null, null, null} 730 ; [ DW_TAG_subroutine_type ] \ 731 [line 0, size 0, align 0, offset 0] [from ] 732 !7 = metadata !{null} 733 !8 = metadata !{i32 2, metadata !"Dwarf Version", i32 2} 734 !9 = metadata !{metadata !"clang version 3.4 (trunk 193128) (llvm/trunk 193139)"} 735 !10 = metadata !{i32 786688, metadata !4, metadata !"X", metadata !5, i32 2, 736 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [X] \ 737 [line 2] 738 !11 = metadata !{i32 786468, null, null, metadata !"int", i32 0, i64 32, 739 i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] [int] \ 740 [line 0, size 32, align 32, offset 0, enc DW_ATE_signed] 741 !12 = metadata !{i32 2, i32 0, metadata !4, null} 742 !13 = metadata !{i32 786688, metadata !4, metadata !"Y", metadata !5, i32 3, 743 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [Y] \ 744 [line 3] 745 !14 = metadata !{i32 3, i32 0, metadata !4, null} 746 !15 = metadata !{i32 786688, metadata !16, metadata !"Z", metadata !5, i32 5, 747 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [Z] \ 748 [line 5] 749 !16 = metadata !{i32 786443, metadata !1, metadata !4, i32 4, i32 0, i32 0} \ 750 ; [ DW_TAG_lexical_block ] [/private/tmp/t.c] 751 !17 = metadata !{i32 5, i32 0, metadata !16, null} 752 !18 = metadata !{i32 6, i32 0, metadata !16, null} 753 !19 = metadata !{i32 8, i32 0, metadata !4, null} ; [ DW_TAG_imported_declaration ] 754 !20 = metadata !{i32 9, i32 0, metadata !4, null} 755 756This example illustrates a few important details about LLVM debugging 757information. In particular, it shows how the ``llvm.dbg.declare`` intrinsic and 758location information, which are attached to an instruction, are applied 759together to allow a debugger to analyze the relationship between statements, 760variable definitions, and the code used to implement the function. 761 762.. code-block:: llvm 763 764 call void @llvm.dbg.declare(metadata !{i32* %X}, metadata !10), !dbg !12 765 ; [debug line = 2:7] [debug variable = X] 766 767The first intrinsic ``%llvm.dbg.declare`` encodes debugging information for the 768variable ``X``. The metadata ``!dbg !12`` attached to the intrinsic provides 769scope information for the variable ``X``. 770 771.. code-block:: llvm 772 773 !12 = metadata !{i32 2, i32 0, metadata !4, null} 774 !4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"foo", 775 metadata !"foo", metadata !"", i32 1, metadata !6, 776 i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false, 777 void ()* @foo, null, null, metadata !2, i32 1} 778 ; [ DW_TAG_subprogram ] [line 1] [def] [foo] 779 780Here ``!12`` is metadata providing location information. It has four fields: 781line number, column number, scope, and original scope. The original scope 782represents inline location if this instruction is inlined inside a caller, and 783is null otherwise. In this example, scope is encoded by ``!4``, a 784:ref:`subprogram descriptor <format_subprograms>`. This way the location 785information attached to the intrinsics indicates that the variable ``X`` is 786declared at line number 2 at a function level scope in function ``foo``. 787 788Now lets take another example. 789 790.. code-block:: llvm 791 792 call void @llvm.dbg.declare(metadata !{i32* %Z}, metadata !15), !dbg !17 793 ; [debug line = 5:9] [debug variable = Z] 794 795The third intrinsic ``%llvm.dbg.declare`` encodes debugging information for 796variable ``Z``. The metadata ``!dbg !17`` attached to the intrinsic provides 797scope information for the variable ``Z``. 798 799.. code-block:: llvm 800 801 !16 = metadata !{i32 786443, metadata !1, metadata !4, i32 4, i32 0, i32 0} \ 802 ; [ DW_TAG_lexical_block ] [/private/tmp/t.c] 803 !17 = metadata !{i32 5, i32 0, metadata !16, null} 804 805Here ``!15`` indicates that ``Z`` is declared at line number 5 and 806column number 0 inside of lexical scope ``!16``. The lexical scope itself 807resides inside of subprogram ``!4`` described above. 808 809The scope information attached with each instruction provides a straightforward 810way to find instructions covered by a scope. 811 812.. _ccxx_frontend: 813 814C/C++ front-end specific debug information 815========================================== 816 817The C and C++ front-ends represent information about the program in a format 818that is effectively identical to `DWARF 3.0 819<http://www.eagercon.com/dwarf/dwarf3std.htm>`_ in terms of information 820content. This allows code generators to trivially support native debuggers by 821generating standard dwarf information, and contains enough information for 822non-dwarf targets to translate it as needed. 823 824This section describes the forms used to represent C and C++ programs. Other 825languages could pattern themselves after this (which itself is tuned to 826representing programs in the same way that DWARF 3 does), or they could choose 827to provide completely different forms if they don't fit into the DWARF model. 828As support for debugging information gets added to the various LLVM 829source-language front-ends, the information used should be documented here. 830 831The following sections provide examples of various C/C++ constructs and the 832debug information that would best describe those constructs. 833 834C/C++ source file information 835----------------------------- 836 837Given the source files ``MySource.cpp`` and ``MyHeader.h`` located in the 838directory ``/Users/mine/sources``, the following code: 839 840.. code-block:: c 841 842 #include "MyHeader.h" 843 844 int main(int argc, char *argv[]) { 845 return 0; 846 } 847 848a C/C++ front-end would generate the following descriptors: 849 850.. code-block:: llvm 851 852 ... 853 ;; 854 ;; Define the compile unit for the main source file "/Users/mine/sources/MySource.cpp". 855 ;; 856 !0 = metadata !{ 857 i32 786449, ;; Tag 858 metadata !1, ;; File/directory name 859 i32 4, ;; Language Id 860 metadata !"clang version 3.4 ", 861 i1 false, ;; Optimized compile unit 862 metadata !"", ;; Compiler flags 863 i32 0, ;; Runtime version 864 metadata !2, ;; Enumeration types 865 metadata !2, ;; Retained types 866 metadata !3, ;; Subprograms 867 metadata !2, ;; Global variables 868 metadata !2, ;; Imported entities (declarations and namespaces) 869 metadata !"" ;; Split debug filename 870 1, ;; Full debug info 871 } 872 873 ;; 874 ;; Define the file for the file "/Users/mine/sources/MySource.cpp". 875 ;; 876 !1 = metadata !{ 877 metadata !"MySource.cpp", 878 metadata !"/Users/mine/sources" 879 } 880 !5 = metadata !{ 881 i32 786473, ;; Tag 882 metadata !1 883 } 884 885 ;; 886 ;; Define the file for the file "/Users/mine/sources/Myheader.h" 887 ;; 888 !14 = metadata !{ 889 i32 786473, ;; Tag 890 metadata !15 891 } 892 !15 = metadata !{ 893 metadata !"./MyHeader.h", 894 metadata !"/Users/mine/sources", 895 } 896 897 ... 898 899``llvm::Instruction`` provides easy access to metadata attached with an 900instruction. One can extract line number information encoded in LLVM IR using 901``Instruction::getMetadata()`` and ``DILocation::getLineNumber()``. 902 903.. code-block:: c++ 904 905 if (MDNode *N = I->getMetadata("dbg")) { // Here I is an LLVM instruction 906 DILocation Loc(N); // DILocation is in DebugInfo.h 907 unsigned Line = Loc.getLineNumber(); 908 StringRef File = Loc.getFilename(); 909 StringRef Dir = Loc.getDirectory(); 910 } 911 912C/C++ global variable information 913--------------------------------- 914 915Given an integer global variable declared as follows: 916 917.. code-block:: c 918 919 int MyGlobal = 100; 920 921a C/C++ front-end would generate the following descriptors: 922 923.. code-block:: llvm 924 925 ;; 926 ;; Define the global itself. 927 ;; 928 %MyGlobal = global int 100 929 ... 930 ;; 931 ;; List of debug info of globals 932 ;; 933 !llvm.dbg.cu = !{!0} 934 935 ;; Define the compile unit. 936 !0 = metadata !{ 937 i32 786449, ;; Tag 938 i32 0, ;; Context 939 i32 4, ;; Language 940 metadata !"foo.cpp", ;; File 941 metadata !"/Volumes/Data/tmp", ;; Directory 942 metadata !"clang version 3.1 ", ;; Producer 943 i1 true, ;; Deprecated field 944 i1 false, ;; "isOptimized"? 945 metadata !"", ;; Flags 946 i32 0, ;; Runtime Version 947 metadata !1, ;; Enum Types 948 metadata !1, ;; Retained Types 949 metadata !1, ;; Subprograms 950 metadata !3, ;; Global Variables 951 metadata !1, ;; Imported entities 952 "", ;; Split debug filename 953 1, ;; Full debug info 954 } ; [ DW_TAG_compile_unit ] 955 956 ;; The Array of Global Variables 957 !3 = metadata !{ 958 metadata !4 959 } 960 961 ;; 962 ;; Define the global variable itself. 963 ;; 964 !4 = metadata !{ 965 i32 786484, ;; Tag 966 i32 0, ;; Unused 967 null, ;; Unused 968 metadata !"MyGlobal", ;; Name 969 metadata !"MyGlobal", ;; Display Name 970 metadata !"", ;; Linkage Name 971 metadata !6, ;; File 972 i32 1, ;; Line 973 metadata !7, ;; Type 974 i32 0, ;; IsLocalToUnit 975 i32 1, ;; IsDefinition 976 i32* @MyGlobal, ;; LLVM-IR Value 977 null ;; Static member declaration 978 } ; [ DW_TAG_variable ] 979 980 ;; 981 ;; Define the file 982 ;; 983 !5 = metadata !{ 984 metadata !"foo.cpp", ;; File 985 metadata !"/Volumes/Data/tmp", ;; Directory 986 } 987 !6 = metadata !{ 988 i32 786473, ;; Tag 989 metadata !5 ;; Unused 990 } ; [ DW_TAG_file_type ] 991 992 ;; 993 ;; Define the type 994 ;; 995 !7 = metadata !{ 996 i32 786468, ;; Tag 997 null, ;; Unused 998 null, ;; Unused 999 metadata !"int", ;; Name 1000 i32 0, ;; Line 1001 i64 32, ;; Size in Bits 1002 i64 32, ;; Align in Bits 1003 i64 0, ;; Offset 1004 i32 0, ;; Flags 1005 i32 5 ;; Encoding 1006 } ; [ DW_TAG_base_type ] 1007 1008C/C++ function information 1009-------------------------- 1010 1011Given a function declared as follows: 1012 1013.. code-block:: c 1014 1015 int main(int argc, char *argv[]) { 1016 return 0; 1017 } 1018 1019a C/C++ front-end would generate the following descriptors: 1020 1021.. code-block:: llvm 1022 1023 ;; 1024 ;; Define the anchor for subprograms. 1025 ;; 1026 !6 = metadata !{ 1027 i32 786484, ;; Tag 1028 metadata !1, ;; File 1029 metadata !1, ;; Context 1030 metadata !"main", ;; Name 1031 metadata !"main", ;; Display name 1032 metadata !"main", ;; Linkage name 1033 i32 1, ;; Line number 1034 metadata !4, ;; Type 1035 i1 false, ;; Is local 1036 i1 true, ;; Is definition 1037 i32 0, ;; Virtuality attribute, e.g. pure virtual function 1038 i32 0, ;; Index into virtual table for C++ methods 1039 i32 0, ;; Type that holds virtual table. 1040 i32 0, ;; Flags 1041 i1 false, ;; True if this function is optimized 1042 Function *, ;; Pointer to llvm::Function 1043 null, ;; Function template parameters 1044 null, ;; List of function variables (emitted when optimizing) 1045 1 ;; Line number of the opening '{' of the function 1046 } 1047 ;; 1048 ;; Define the subprogram itself. 1049 ;; 1050 define i32 @main(i32 %argc, i8** %argv) { 1051 ... 1052 } 1053 1054C/C++ basic types 1055----------------- 1056 1057The following are the basic type descriptors for C/C++ core types: 1058 1059bool 1060^^^^ 1061 1062.. code-block:: llvm 1063 1064 !2 = metadata !{ 1065 i32 786468, ;; Tag 1066 null, ;; File 1067 null, ;; Context 1068 metadata !"bool", ;; Name 1069 i32 0, ;; Line number 1070 i64 8, ;; Size in Bits 1071 i64 8, ;; Align in Bits 1072 i64 0, ;; Offset in Bits 1073 i32 0, ;; Flags 1074 i32 2 ;; Encoding 1075 } 1076 1077char 1078^^^^ 1079 1080.. code-block:: llvm 1081 1082 !2 = metadata !{ 1083 i32 786468, ;; Tag 1084 null, ;; File 1085 null, ;; Context 1086 metadata !"char", ;; Name 1087 i32 0, ;; Line number 1088 i64 8, ;; Size in Bits 1089 i64 8, ;; Align in Bits 1090 i64 0, ;; Offset in Bits 1091 i32 0, ;; Flags 1092 i32 6 ;; Encoding 1093 } 1094 1095unsigned char 1096^^^^^^^^^^^^^ 1097 1098.. code-block:: llvm 1099 1100 !2 = metadata !{ 1101 i32 786468, ;; Tag 1102 null, ;; File 1103 null, ;; Context 1104 metadata !"unsigned char", 1105 i32 0, ;; Line number 1106 i64 8, ;; Size in Bits 1107 i64 8, ;; Align in Bits 1108 i64 0, ;; Offset in Bits 1109 i32 0, ;; Flags 1110 i32 8 ;; Encoding 1111 } 1112 1113short 1114^^^^^ 1115 1116.. code-block:: llvm 1117 1118 !2 = metadata !{ 1119 i32 786468, ;; Tag 1120 null, ;; File 1121 null, ;; Context 1122 metadata !"short int", 1123 i32 0, ;; Line number 1124 i64 16, ;; Size in Bits 1125 i64 16, ;; Align in Bits 1126 i64 0, ;; Offset in Bits 1127 i32 0, ;; Flags 1128 i32 5 ;; Encoding 1129 } 1130 1131unsigned short 1132^^^^^^^^^^^^^^ 1133 1134.. code-block:: llvm 1135 1136 !2 = metadata !{ 1137 i32 786468, ;; Tag 1138 null, ;; File 1139 null, ;; Context 1140 metadata !"short unsigned int", 1141 i32 0, ;; Line number 1142 i64 16, ;; Size in Bits 1143 i64 16, ;; Align in Bits 1144 i64 0, ;; Offset in Bits 1145 i32 0, ;; Flags 1146 i32 7 ;; Encoding 1147 } 1148 1149int 1150^^^ 1151 1152.. code-block:: llvm 1153 1154 !2 = metadata !{ 1155 i32 786468, ;; Tag 1156 null, ;; File 1157 null, ;; Context 1158 metadata !"int", ;; Name 1159 i32 0, ;; Line number 1160 i64 32, ;; Size in Bits 1161 i64 32, ;; Align in Bits 1162 i64 0, ;; Offset in Bits 1163 i32 0, ;; Flags 1164 i32 5 ;; Encoding 1165 } 1166 1167unsigned int 1168^^^^^^^^^^^^ 1169 1170.. code-block:: llvm 1171 1172 !2 = metadata !{ 1173 i32 786468, ;; Tag 1174 null, ;; File 1175 null, ;; Context 1176 metadata !"unsigned int", 1177 i32 0, ;; Line number 1178 i64 32, ;; Size in Bits 1179 i64 32, ;; Align in Bits 1180 i64 0, ;; Offset in Bits 1181 i32 0, ;; Flags 1182 i32 7 ;; Encoding 1183 } 1184 1185long long 1186^^^^^^^^^ 1187 1188.. code-block:: llvm 1189 1190 !2 = metadata !{ 1191 i32 786468, ;; Tag 1192 null, ;; File 1193 null, ;; Context 1194 metadata !"long long int", 1195 i32 0, ;; Line number 1196 i64 64, ;; Size in Bits 1197 i64 64, ;; Align in Bits 1198 i64 0, ;; Offset in Bits 1199 i32 0, ;; Flags 1200 i32 5 ;; Encoding 1201 } 1202 1203unsigned long long 1204^^^^^^^^^^^^^^^^^^ 1205 1206.. code-block:: llvm 1207 1208 !2 = metadata !{ 1209 i32 786468, ;; Tag 1210 null, ;; File 1211 null, ;; Context 1212 metadata !"long long unsigned int", 1213 i32 0, ;; Line number 1214 i64 64, ;; Size in Bits 1215 i64 64, ;; Align in Bits 1216 i64 0, ;; Offset in Bits 1217 i32 0, ;; Flags 1218 i32 7 ;; Encoding 1219 } 1220 1221float 1222^^^^^ 1223 1224.. code-block:: llvm 1225 1226 !2 = metadata !{ 1227 i32 786468, ;; Tag 1228 null, ;; File 1229 null, ;; Context 1230 metadata !"float", 1231 i32 0, ;; Line number 1232 i64 32, ;; Size in Bits 1233 i64 32, ;; Align in Bits 1234 i64 0, ;; Offset in Bits 1235 i32 0, ;; Flags 1236 i32 4 ;; Encoding 1237 } 1238 1239double 1240^^^^^^ 1241 1242.. code-block:: llvm 1243 1244 !2 = metadata !{ 1245 i32 786468, ;; Tag 1246 null, ;; File 1247 null, ;; Context 1248 metadata !"double",;; Name 1249 i32 0, ;; Line number 1250 i64 64, ;; Size in Bits 1251 i64 64, ;; Align in Bits 1252 i64 0, ;; Offset in Bits 1253 i32 0, ;; Flags 1254 i32 4 ;; Encoding 1255 } 1256 1257C/C++ derived types 1258------------------- 1259 1260Given the following as an example of C/C++ derived type: 1261 1262.. code-block:: c 1263 1264 typedef const int *IntPtr; 1265 1266a C/C++ front-end would generate the following descriptors: 1267 1268.. code-block:: llvm 1269 1270 ;; 1271 ;; Define the typedef "IntPtr". 1272 ;; 1273 !2 = metadata !{ 1274 i32 786454, ;; Tag 1275 metadata !3, ;; File 1276 metadata !1, ;; Context 1277 metadata !"IntPtr", ;; Name 1278 i32 0, ;; Line number 1279 i64 0, ;; Size in bits 1280 i64 0, ;; Align in bits 1281 i64 0, ;; Offset in bits 1282 i32 0, ;; Flags 1283 metadata !4 ;; Derived From type 1284 } 1285 ;; 1286 ;; Define the pointer type. 1287 ;; 1288 !4 = metadata !{ 1289 i32 786447, ;; Tag 1290 null, ;; File 1291 null, ;; Context 1292 metadata !"", ;; Name 1293 i32 0, ;; Line number 1294 i64 64, ;; Size in bits 1295 i64 64, ;; Align in bits 1296 i64 0, ;; Offset in bits 1297 i32 0, ;; Flags 1298 metadata !5 ;; Derived From type 1299 } 1300 ;; 1301 ;; Define the const type. 1302 ;; 1303 !5 = metadata !{ 1304 i32 786470, ;; Tag 1305 null, ;; File 1306 null, ;; Context 1307 metadata !"", ;; Name 1308 i32 0, ;; Line number 1309 i64 0, ;; Size in bits 1310 i64 0, ;; Align in bits 1311 i64 0, ;; Offset in bits 1312 i32 0, ;; Flags 1313 metadata !6 ;; Derived From type 1314 } 1315 ;; 1316 ;; Define the int type. 1317 ;; 1318 !6 = metadata !{ 1319 i32 786468, ;; Tag 1320 null, ;; File 1321 null, ;; Context 1322 metadata !"int", ;; Name 1323 i32 0, ;; Line number 1324 i64 32, ;; Size in bits 1325 i64 32, ;; Align in bits 1326 i64 0, ;; Offset in bits 1327 i32 0, ;; Flags 1328 i32 5 ;; Encoding 1329 } 1330 1331C/C++ struct/union types 1332------------------------ 1333 1334Given the following as an example of C/C++ struct type: 1335 1336.. code-block:: c 1337 1338 struct Color { 1339 unsigned Red; 1340 unsigned Green; 1341 unsigned Blue; 1342 }; 1343 1344a C/C++ front-end would generate the following descriptors: 1345 1346.. code-block:: llvm 1347 1348 ;; 1349 ;; Define basic type for unsigned int. 1350 ;; 1351 !5 = metadata !{ 1352 i32 786468, ;; Tag 1353 null, ;; File 1354 null, ;; Context 1355 metadata !"unsigned int", 1356 i32 0, ;; Line number 1357 i64 32, ;; Size in Bits 1358 i64 32, ;; Align in Bits 1359 i64 0, ;; Offset in Bits 1360 i32 0, ;; Flags 1361 i32 7 ;; Encoding 1362 } 1363 ;; 1364 ;; Define composite type for struct Color. 1365 ;; 1366 !2 = metadata !{ 1367 i32 786451, ;; Tag 1368 metadata !1, ;; Compile unit 1369 null, ;; Context 1370 metadata !"Color", ;; Name 1371 i32 1, ;; Line number 1372 i64 96, ;; Size in bits 1373 i64 32, ;; Align in bits 1374 i64 0, ;; Offset in bits 1375 i32 0, ;; Flags 1376 null, ;; Derived From 1377 metadata !3, ;; Elements 1378 i32 0, ;; Runtime Language 1379 null, ;; Base type containing the vtable pointer for this type 1380 null ;; Template parameters 1381 } 1382 1383 ;; 1384 ;; Define the Red field. 1385 ;; 1386 !4 = metadata !{ 1387 i32 786445, ;; Tag 1388 metadata !1, ;; File 1389 metadata !1, ;; Context 1390 metadata !"Red", ;; Name 1391 i32 2, ;; Line number 1392 i64 32, ;; Size in bits 1393 i64 32, ;; Align in bits 1394 i64 0, ;; Offset in bits 1395 i32 0, ;; Flags 1396 metadata !5 ;; Derived From type 1397 } 1398 1399 ;; 1400 ;; Define the Green field. 1401 ;; 1402 !6 = metadata !{ 1403 i32 786445, ;; Tag 1404 metadata !1, ;; File 1405 metadata !1, ;; Context 1406 metadata !"Green", ;; Name 1407 i32 3, ;; Line number 1408 i64 32, ;; Size in bits 1409 i64 32, ;; Align in bits 1410 i64 32, ;; Offset in bits 1411 i32 0, ;; Flags 1412 metadata !5 ;; Derived From type 1413 } 1414 1415 ;; 1416 ;; Define the Blue field. 1417 ;; 1418 !7 = metadata !{ 1419 i32 786445, ;; Tag 1420 metadata !1, ;; File 1421 metadata !1, ;; Context 1422 metadata !"Blue", ;; Name 1423 i32 4, ;; Line number 1424 i64 32, ;; Size in bits 1425 i64 32, ;; Align in bits 1426 i64 64, ;; Offset in bits 1427 i32 0, ;; Flags 1428 metadata !5 ;; Derived From type 1429 } 1430 1431 ;; 1432 ;; Define the array of fields used by the composite type Color. 1433 ;; 1434 !3 = metadata !{metadata !4, metadata !6, metadata !7} 1435 1436C/C++ enumeration types 1437----------------------- 1438 1439Given the following as an example of C/C++ enumeration type: 1440 1441.. code-block:: c 1442 1443 enum Trees { 1444 Spruce = 100, 1445 Oak = 200, 1446 Maple = 300 1447 }; 1448 1449a C/C++ front-end would generate the following descriptors: 1450 1451.. code-block:: llvm 1452 1453 ;; 1454 ;; Define composite type for enum Trees 1455 ;; 1456 !2 = metadata !{ 1457 i32 786436, ;; Tag 1458 metadata !1, ;; File 1459 metadata !1, ;; Context 1460 metadata !"Trees", ;; Name 1461 i32 1, ;; Line number 1462 i64 32, ;; Size in bits 1463 i64 32, ;; Align in bits 1464 i64 0, ;; Offset in bits 1465 i32 0, ;; Flags 1466 null, ;; Derived From type 1467 metadata !3, ;; Elements 1468 i32 0 ;; Runtime language 1469 } 1470 1471 ;; 1472 ;; Define the array of enumerators used by composite type Trees. 1473 ;; 1474 !3 = metadata !{metadata !4, metadata !5, metadata !6} 1475 1476 ;; 1477 ;; Define Spruce enumerator. 1478 ;; 1479 !4 = metadata !{i32 786472, metadata !"Spruce", i64 100} 1480 1481 ;; 1482 ;; Define Oak enumerator. 1483 ;; 1484 !5 = metadata !{i32 786472, metadata !"Oak", i64 200} 1485 1486 ;; 1487 ;; Define Maple enumerator. 1488 ;; 1489 !6 = metadata !{i32 786472, metadata !"Maple", i64 300} 1490 1491Debugging information format 1492============================ 1493 1494Debugging Information Extension for Objective C Properties 1495---------------------------------------------------------- 1496 1497Introduction 1498^^^^^^^^^^^^ 1499 1500Objective C provides a simpler way to declare and define accessor methods using 1501declared properties. The language provides features to declare a property and 1502to let compiler synthesize accessor methods. 1503 1504The debugger lets developer inspect Objective C interfaces and their instance 1505variables and class variables. However, the debugger does not know anything 1506about the properties defined in Objective C interfaces. The debugger consumes 1507information generated by compiler in DWARF format. The format does not support 1508encoding of Objective C properties. This proposal describes DWARF extensions to 1509encode Objective C properties, which the debugger can use to let developers 1510inspect Objective C properties. 1511 1512Proposal 1513^^^^^^^^ 1514 1515Objective C properties exist separately from class members. A property can be 1516defined only by "setter" and "getter" selectors, and be calculated anew on each 1517access. Or a property can just be a direct access to some declared ivar. 1518Finally it can have an ivar "automatically synthesized" for it by the compiler, 1519in which case the property can be referred to in user code directly using the 1520standard C dereference syntax as well as through the property "dot" syntax, but 1521there is no entry in the ``@interface`` declaration corresponding to this ivar. 1522 1523To facilitate debugging, these properties we will add a new DWARF TAG into the 1524``DW_TAG_structure_type`` definition for the class to hold the description of a 1525given property, and a set of DWARF attributes that provide said description. 1526The property tag will also contain the name and declared type of the property. 1527 1528If there is a related ivar, there will also be a DWARF property attribute placed 1529in the ``DW_TAG_member`` DIE for that ivar referring back to the property TAG 1530for that property. And in the case where the compiler synthesizes the ivar 1531directly, the compiler is expected to generate a ``DW_TAG_member`` for that 1532ivar (with the ``DW_AT_artificial`` set to 1), whose name will be the name used 1533to access this ivar directly in code, and with the property attribute pointing 1534back to the property it is backing. 1535 1536The following examples will serve as illustration for our discussion: 1537 1538.. code-block:: objc 1539 1540 @interface I1 { 1541 int n2; 1542 } 1543 1544 @property int p1; 1545 @property int p2; 1546 @end 1547 1548 @implementation I1 1549 @synthesize p1; 1550 @synthesize p2 = n2; 1551 @end 1552 1553This produces the following DWARF (this is a "pseudo dwarfdump" output): 1554 1555.. code-block:: none 1556 1557 0x00000100: TAG_structure_type [7] * 1558 AT_APPLE_runtime_class( 0x10 ) 1559 AT_name( "I1" ) 1560 AT_decl_file( "Objc_Property.m" ) 1561 AT_decl_line( 3 ) 1562 1563 0x00000110 TAG_APPLE_property 1564 AT_name ( "p1" ) 1565 AT_type ( {0x00000150} ( int ) ) 1566 1567 0x00000120: TAG_APPLE_property 1568 AT_name ( "p2" ) 1569 AT_type ( {0x00000150} ( int ) ) 1570 1571 0x00000130: TAG_member [8] 1572 AT_name( "_p1" ) 1573 AT_APPLE_property ( {0x00000110} "p1" ) 1574 AT_type( {0x00000150} ( int ) ) 1575 AT_artificial ( 0x1 ) 1576 1577 0x00000140: TAG_member [8] 1578 AT_name( "n2" ) 1579 AT_APPLE_property ( {0x00000120} "p2" ) 1580 AT_type( {0x00000150} ( int ) ) 1581 1582 0x00000150: AT_type( ( int ) ) 1583 1584Note, the current convention is that the name of the ivar for an 1585auto-synthesized property is the name of the property from which it derives 1586with an underscore prepended, as is shown in the example. But we actually 1587don't need to know this convention, since we are given the name of the ivar 1588directly. 1589 1590Also, it is common practice in ObjC to have different property declarations in 1591the @interface and @implementation - e.g. to provide a read-only property in 1592the interface,and a read-write interface in the implementation. In that case, 1593the compiler should emit whichever property declaration will be in force in the 1594current translation unit. 1595 1596Developers can decorate a property with attributes which are encoded using 1597``DW_AT_APPLE_property_attribute``. 1598 1599.. code-block:: objc 1600 1601 @property (readonly, nonatomic) int pr; 1602 1603.. code-block:: none 1604 1605 TAG_APPLE_property [8] 1606 AT_name( "pr" ) 1607 AT_type ( {0x00000147} (int) ) 1608 AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic) 1609 1610The setter and getter method names are attached to the property using 1611``DW_AT_APPLE_property_setter`` and ``DW_AT_APPLE_property_getter`` attributes. 1612 1613.. code-block:: objc 1614 1615 @interface I1 1616 @property (setter=myOwnP3Setter:) int p3; 1617 -(void)myOwnP3Setter:(int)a; 1618 @end 1619 1620 @implementation I1 1621 @synthesize p3; 1622 -(void)myOwnP3Setter:(int)a{ } 1623 @end 1624 1625The DWARF for this would be: 1626 1627.. code-block:: none 1628 1629 0x000003bd: TAG_structure_type [7] * 1630 AT_APPLE_runtime_class( 0x10 ) 1631 AT_name( "I1" ) 1632 AT_decl_file( "Objc_Property.m" ) 1633 AT_decl_line( 3 ) 1634 1635 0x000003cd TAG_APPLE_property 1636 AT_name ( "p3" ) 1637 AT_APPLE_property_setter ( "myOwnP3Setter:" ) 1638 AT_type( {0x00000147} ( int ) ) 1639 1640 0x000003f3: TAG_member [8] 1641 AT_name( "_p3" ) 1642 AT_type ( {0x00000147} ( int ) ) 1643 AT_APPLE_property ( {0x000003cd} ) 1644 AT_artificial ( 0x1 ) 1645 1646New DWARF Tags 1647^^^^^^^^^^^^^^ 1648 1649+-----------------------+--------+ 1650| TAG | Value | 1651+=======================+========+ 1652| DW_TAG_APPLE_property | 0x4200 | 1653+-----------------------+--------+ 1654 1655New DWARF Attributes 1656^^^^^^^^^^^^^^^^^^^^ 1657 1658+--------------------------------+--------+-----------+ 1659| Attribute | Value | Classes | 1660+================================+========+===========+ 1661| DW_AT_APPLE_property | 0x3fed | Reference | 1662+--------------------------------+--------+-----------+ 1663| DW_AT_APPLE_property_getter | 0x3fe9 | String | 1664+--------------------------------+--------+-----------+ 1665| DW_AT_APPLE_property_setter | 0x3fea | String | 1666+--------------------------------+--------+-----------+ 1667| DW_AT_APPLE_property_attribute | 0x3feb | Constant | 1668+--------------------------------+--------+-----------+ 1669 1670New DWARF Constants 1671^^^^^^^^^^^^^^^^^^^ 1672 1673+--------------------------------+-------+ 1674| Name | Value | 1675+================================+=======+ 1676| DW_AT_APPLE_PROPERTY_readonly | 0x1 | 1677+--------------------------------+-------+ 1678| DW_AT_APPLE_PROPERTY_readwrite | 0x2 | 1679+--------------------------------+-------+ 1680| DW_AT_APPLE_PROPERTY_assign | 0x4 | 1681+--------------------------------+-------+ 1682| DW_AT_APPLE_PROPERTY_retain | 0x8 | 1683+--------------------------------+-------+ 1684| DW_AT_APPLE_PROPERTY_copy | 0x10 | 1685+--------------------------------+-------+ 1686| DW_AT_APPLE_PROPERTY_nonatomic | 0x20 | 1687+--------------------------------+-------+ 1688 1689Name Accelerator Tables 1690----------------------- 1691 1692Introduction 1693^^^^^^^^^^^^ 1694 1695The "``.debug_pubnames``" and "``.debug_pubtypes``" formats are not what a 1696debugger needs. The "``pub``" in the section name indicates that the entries 1697in the table are publicly visible names only. This means no static or hidden 1698functions show up in the "``.debug_pubnames``". No static variables or private 1699class variables are in the "``.debug_pubtypes``". Many compilers add different 1700things to these tables, so we can't rely upon the contents between gcc, icc, or 1701clang. 1702 1703The typical query given by users tends not to match up with the contents of 1704these tables. For example, the DWARF spec states that "In the case of the name 1705of a function member or static data member of a C++ structure, class or union, 1706the name presented in the "``.debug_pubnames``" section is not the simple name 1707given by the ``DW_AT_name attribute`` of the referenced debugging information 1708entry, but rather the fully qualified name of the data or function member." 1709So the only names in these tables for complex C++ entries is a fully 1710qualified name. Debugger users tend not to enter their search strings as 1711"``a::b::c(int,const Foo&) const``", but rather as "``c``", "``b::c``" , or 1712"``a::b::c``". So the name entered in the name table must be demangled in 1713order to chop it up appropriately and additional names must be manually entered 1714into the table to make it effective as a name lookup table for debuggers to 1715se. 1716 1717All debuggers currently ignore the "``.debug_pubnames``" table as a result of 1718its inconsistent and useless public-only name content making it a waste of 1719space in the object file. These tables, when they are written to disk, are not 1720sorted in any way, leaving every debugger to do its own parsing and sorting. 1721These tables also include an inlined copy of the string values in the table 1722itself making the tables much larger than they need to be on disk, especially 1723for large C++ programs. 1724 1725Can't we just fix the sections by adding all of the names we need to this 1726table? No, because that is not what the tables are defined to contain and we 1727won't know the difference between the old bad tables and the new good tables. 1728At best we could make our own renamed sections that contain all of the data we 1729need. 1730 1731These tables are also insufficient for what a debugger like LLDB needs. LLDB 1732uses clang for its expression parsing where LLDB acts as a PCH. LLDB is then 1733often asked to look for type "``foo``" or namespace "``bar``", or list items in 1734namespace "``baz``". Namespaces are not included in the pubnames or pubtypes 1735tables. Since clang asks a lot of questions when it is parsing an expression, 1736we need to be very fast when looking up names, as it happens a lot. Having new 1737accelerator tables that are optimized for very quick lookups will benefit this 1738type of debugging experience greatly. 1739 1740We would like to generate name lookup tables that can be mapped into memory 1741from disk, and used as is, with little or no up-front parsing. We would also 1742be able to control the exact content of these different tables so they contain 1743exactly what we need. The Name Accelerator Tables were designed to fix these 1744issues. In order to solve these issues we need to: 1745 1746* Have a format that can be mapped into memory from disk and used as is 1747* Lookups should be very fast 1748* Extensible table format so these tables can be made by many producers 1749* Contain all of the names needed for typical lookups out of the box 1750* Strict rules for the contents of tables 1751 1752Table size is important and the accelerator table format should allow the reuse 1753of strings from common string tables so the strings for the names are not 1754duplicated. We also want to make sure the table is ready to be used as-is by 1755simply mapping the table into memory with minimal header parsing. 1756 1757The name lookups need to be fast and optimized for the kinds of lookups that 1758debuggers tend to do. Optimally we would like to touch as few parts of the 1759mapped table as possible when doing a name lookup and be able to quickly find 1760the name entry we are looking for, or discover there are no matches. In the 1761case of debuggers we optimized for lookups that fail most of the time. 1762 1763Each table that is defined should have strict rules on exactly what is in the 1764accelerator tables and documented so clients can rely on the content. 1765 1766Hash Tables 1767^^^^^^^^^^^ 1768 1769Standard Hash Tables 1770"""""""""""""""""""" 1771 1772Typical hash tables have a header, buckets, and each bucket points to the 1773bucket contents: 1774 1775.. code-block:: none 1776 1777 .------------. 1778 | HEADER | 1779 |------------| 1780 | BUCKETS | 1781 |------------| 1782 | DATA | 1783 `------------' 1784 1785The BUCKETS are an array of offsets to DATA for each hash: 1786 1787.. code-block:: none 1788 1789 .------------. 1790 | 0x00001000 | BUCKETS[0] 1791 | 0x00002000 | BUCKETS[1] 1792 | 0x00002200 | BUCKETS[2] 1793 | 0x000034f0 | BUCKETS[3] 1794 | | ... 1795 | 0xXXXXXXXX | BUCKETS[n_buckets] 1796 '------------' 1797 1798So for ``bucket[3]`` in the example above, we have an offset into the table 17990x000034f0 which points to a chain of entries for the bucket. Each bucket must 1800contain a next pointer, full 32 bit hash value, the string itself, and the data 1801for the current string value. 1802 1803.. code-block:: none 1804 1805 .------------. 1806 0x000034f0: | 0x00003500 | next pointer 1807 | 0x12345678 | 32 bit hash 1808 | "erase" | string value 1809 | data[n] | HashData for this bucket 1810 |------------| 1811 0x00003500: | 0x00003550 | next pointer 1812 | 0x29273623 | 32 bit hash 1813 | "dump" | string value 1814 | data[n] | HashData for this bucket 1815 |------------| 1816 0x00003550: | 0x00000000 | next pointer 1817 | 0x82638293 | 32 bit hash 1818 | "main" | string value 1819 | data[n] | HashData for this bucket 1820 `------------' 1821 1822The problem with this layout for debuggers is that we need to optimize for the 1823negative lookup case where the symbol we're searching for is not present. So 1824if we were to lookup "``printf``" in the table above, we would make a 32 hash 1825for "``printf``", it might match ``bucket[3]``. We would need to go to the 1826offset 0x000034f0 and start looking to see if our 32 bit hash matches. To do 1827so, we need to read the next pointer, then read the hash, compare it, and skip 1828to the next bucket. Each time we are skipping many bytes in memory and 1829touching new cache pages just to do the compare on the full 32 bit hash. All 1830of these accesses then tell us that we didn't have a match. 1831 1832Name Hash Tables 1833"""""""""""""""" 1834 1835To solve the issues mentioned above we have structured the hash tables a bit 1836differently: a header, buckets, an array of all unique 32 bit hash values, 1837followed by an array of hash value data offsets, one for each hash value, then 1838the data for all hash values: 1839 1840.. code-block:: none 1841 1842 .-------------. 1843 | HEADER | 1844 |-------------| 1845 | BUCKETS | 1846 |-------------| 1847 | HASHES | 1848 |-------------| 1849 | OFFSETS | 1850 |-------------| 1851 | DATA | 1852 `-------------' 1853 1854The ``BUCKETS`` in the name tables are an index into the ``HASHES`` array. By 1855making all of the full 32 bit hash values contiguous in memory, we allow 1856ourselves to efficiently check for a match while touching as little memory as 1857possible. Most often checking the 32 bit hash values is as far as the lookup 1858goes. If it does match, it usually is a match with no collisions. So for a 1859table with "``n_buckets``" buckets, and "``n_hashes``" unique 32 bit hash 1860values, we can clarify the contents of the ``BUCKETS``, ``HASHES`` and 1861``OFFSETS`` as: 1862 1863.. code-block:: none 1864 1865 .-------------------------. 1866 | HEADER.magic | uint32_t 1867 | HEADER.version | uint16_t 1868 | HEADER.hash_function | uint16_t 1869 | HEADER.bucket_count | uint32_t 1870 | HEADER.hashes_count | uint32_t 1871 | HEADER.header_data_len | uint32_t 1872 | HEADER_DATA | HeaderData 1873 |-------------------------| 1874 | BUCKETS | uint32_t[n_buckets] // 32 bit hash indexes 1875 |-------------------------| 1876 | HASHES | uint32_t[n_hashes] // 32 bit hash values 1877 |-------------------------| 1878 | OFFSETS | uint32_t[n_hashes] // 32 bit offsets to hash value data 1879 |-------------------------| 1880 | ALL HASH DATA | 1881 `-------------------------' 1882 1883So taking the exact same data from the standard hash example above we end up 1884with: 1885 1886.. code-block:: none 1887 1888 .------------. 1889 | HEADER | 1890 |------------| 1891 | 0 | BUCKETS[0] 1892 | 2 | BUCKETS[1] 1893 | 5 | BUCKETS[2] 1894 | 6 | BUCKETS[3] 1895 | | ... 1896 | ... | BUCKETS[n_buckets] 1897 |------------| 1898 | 0x........ | HASHES[0] 1899 | 0x........ | HASHES[1] 1900 | 0x........ | HASHES[2] 1901 | 0x........ | HASHES[3] 1902 | 0x........ | HASHES[4] 1903 | 0x........ | HASHES[5] 1904 | 0x12345678 | HASHES[6] hash for BUCKETS[3] 1905 | 0x29273623 | HASHES[7] hash for BUCKETS[3] 1906 | 0x82638293 | HASHES[8] hash for BUCKETS[3] 1907 | 0x........ | HASHES[9] 1908 | 0x........ | HASHES[10] 1909 | 0x........ | HASHES[11] 1910 | 0x........ | HASHES[12] 1911 | 0x........ | HASHES[13] 1912 | 0x........ | HASHES[n_hashes] 1913 |------------| 1914 | 0x........ | OFFSETS[0] 1915 | 0x........ | OFFSETS[1] 1916 | 0x........ | OFFSETS[2] 1917 | 0x........ | OFFSETS[3] 1918 | 0x........ | OFFSETS[4] 1919 | 0x........ | OFFSETS[5] 1920 | 0x000034f0 | OFFSETS[6] offset for BUCKETS[3] 1921 | 0x00003500 | OFFSETS[7] offset for BUCKETS[3] 1922 | 0x00003550 | OFFSETS[8] offset for BUCKETS[3] 1923 | 0x........ | OFFSETS[9] 1924 | 0x........ | OFFSETS[10] 1925 | 0x........ | OFFSETS[11] 1926 | 0x........ | OFFSETS[12] 1927 | 0x........ | OFFSETS[13] 1928 | 0x........ | OFFSETS[n_hashes] 1929 |------------| 1930 | | 1931 | | 1932 | | 1933 | | 1934 | | 1935 |------------| 1936 0x000034f0: | 0x00001203 | .debug_str ("erase") 1937 | 0x00000004 | A 32 bit array count - number of HashData with name "erase" 1938 | 0x........ | HashData[0] 1939 | 0x........ | HashData[1] 1940 | 0x........ | HashData[2] 1941 | 0x........ | HashData[3] 1942 | 0x00000000 | String offset into .debug_str (terminate data for hash) 1943 |------------| 1944 0x00003500: | 0x00001203 | String offset into .debug_str ("collision") 1945 | 0x00000002 | A 32 bit array count - number of HashData with name "collision" 1946 | 0x........ | HashData[0] 1947 | 0x........ | HashData[1] 1948 | 0x00001203 | String offset into .debug_str ("dump") 1949 | 0x00000003 | A 32 bit array count - number of HashData with name "dump" 1950 | 0x........ | HashData[0] 1951 | 0x........ | HashData[1] 1952 | 0x........ | HashData[2] 1953 | 0x00000000 | String offset into .debug_str (terminate data for hash) 1954 |------------| 1955 0x00003550: | 0x00001203 | String offset into .debug_str ("main") 1956 | 0x00000009 | A 32 bit array count - number of HashData with name "main" 1957 | 0x........ | HashData[0] 1958 | 0x........ | HashData[1] 1959 | 0x........ | HashData[2] 1960 | 0x........ | HashData[3] 1961 | 0x........ | HashData[4] 1962 | 0x........ | HashData[5] 1963 | 0x........ | HashData[6] 1964 | 0x........ | HashData[7] 1965 | 0x........ | HashData[8] 1966 | 0x00000000 | String offset into .debug_str (terminate data for hash) 1967 `------------' 1968 1969So we still have all of the same data, we just organize it more efficiently for 1970debugger lookup. If we repeat the same "``printf``" lookup from above, we 1971would hash "``printf``" and find it matches ``BUCKETS[3]`` by taking the 32 bit 1972hash value and modulo it by ``n_buckets``. ``BUCKETS[3]`` contains "6" which 1973is the index into the ``HASHES`` table. We would then compare any consecutive 197432 bit hashes values in the ``HASHES`` array as long as the hashes would be in 1975``BUCKETS[3]``. We do this by verifying that each subsequent hash value modulo 1976``n_buckets`` is still 3. In the case of a failed lookup we would access the 1977memory for ``BUCKETS[3]``, and then compare a few consecutive 32 bit hashes 1978before we know that we have no match. We don't end up marching through 1979multiple words of memory and we really keep the number of processor data cache 1980lines being accessed as small as possible. 1981 1982The string hash that is used for these lookup tables is the Daniel J. 1983Bernstein hash which is also used in the ELF ``GNU_HASH`` sections. It is a 1984very good hash for all kinds of names in programs with very few hash 1985collisions. 1986 1987Empty buckets are designated by using an invalid hash index of ``UINT32_MAX``. 1988 1989Details 1990^^^^^^^ 1991 1992These name hash tables are designed to be generic where specializations of the 1993table get to define additional data that goes into the header ("``HeaderData``"), 1994how the string value is stored ("``KeyType``") and the content of the data for each 1995hash value. 1996 1997Header Layout 1998""""""""""""" 1999 2000The header has a fixed part, and the specialized part. The exact format of the 2001header is: 2002 2003.. code-block:: c 2004 2005 struct Header 2006 { 2007 uint32_t magic; // 'HASH' magic value to allow endian detection 2008 uint16_t version; // Version number 2009 uint16_t hash_function; // The hash function enumeration that was used 2010 uint32_t bucket_count; // The number of buckets in this hash table 2011 uint32_t hashes_count; // The total number of unique hash values and hash data offsets in this table 2012 uint32_t header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment 2013 // Specifically the length of the following HeaderData field - this does not 2014 // include the size of the preceding fields 2015 HeaderData header_data; // Implementation specific header data 2016 }; 2017 2018The header starts with a 32 bit "``magic``" value which must be ``'HASH'`` 2019encoded as an ASCII integer. This allows the detection of the start of the 2020hash table and also allows the table's byte order to be determined so the table 2021can be correctly extracted. The "``magic``" value is followed by a 16 bit 2022``version`` number which allows the table to be revised and modified in the 2023future. The current version number is 1. ``hash_function`` is a ``uint16_t`` 2024enumeration that specifies which hash function was used to produce this table. 2025The current values for the hash function enumerations include: 2026 2027.. code-block:: c 2028 2029 enum HashFunctionType 2030 { 2031 eHashFunctionDJB = 0u, // Daniel J Bernstein hash function 2032 }; 2033 2034``bucket_count`` is a 32 bit unsigned integer that represents how many buckets 2035are in the ``BUCKETS`` array. ``hashes_count`` is the number of unique 32 bit 2036hash values that are in the ``HASHES`` array, and is the same number of offsets 2037are contained in the ``OFFSETS`` array. ``header_data_len`` specifies the size 2038in bytes of the ``HeaderData`` that is filled in by specialized versions of 2039this table. 2040 2041Fixed Lookup 2042"""""""""""" 2043 2044The header is followed by the buckets, hashes, offsets, and hash value data. 2045 2046.. code-block:: c 2047 2048 struct FixedTable 2049 { 2050 uint32_t buckets[Header.bucket_count]; // An array of hash indexes into the "hashes[]" array below 2051 uint32_t hashes [Header.hashes_count]; // Every unique 32 bit hash for the entire table is in this table 2052 uint32_t offsets[Header.hashes_count]; // An offset that corresponds to each item in the "hashes[]" array above 2053 }; 2054 2055``buckets`` is an array of 32 bit indexes into the ``hashes`` array. The 2056``hashes`` array contains all of the 32 bit hash values for all names in the 2057hash table. Each hash in the ``hashes`` table has an offset in the ``offsets`` 2058array that points to the data for the hash value. 2059 2060This table setup makes it very easy to repurpose these tables to contain 2061different data, while keeping the lookup mechanism the same for all tables. 2062This layout also makes it possible to save the table to disk and map it in 2063later and do very efficient name lookups with little or no parsing. 2064 2065DWARF lookup tables can be implemented in a variety of ways and can store a lot 2066of information for each name. We want to make the DWARF tables extensible and 2067able to store the data efficiently so we have used some of the DWARF features 2068that enable efficient data storage to define exactly what kind of data we store 2069for each name. 2070 2071The ``HeaderData`` contains a definition of the contents of each HashData chunk. 2072We might want to store an offset to all of the debug information entries (DIEs) 2073for each name. To keep things extensible, we create a list of items, or 2074Atoms, that are contained in the data for each name. First comes the type of 2075the data in each atom: 2076 2077.. code-block:: c 2078 2079 enum AtomType 2080 { 2081 eAtomTypeNULL = 0u, 2082 eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding 2083 eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that contains the item in question 2084 eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2 2085 eAtomTypeNameFlags = 4u, // Flags from enum NameFlags 2086 eAtomTypeTypeFlags = 5u, // Flags from enum TypeFlags 2087 }; 2088 2089The enumeration values and their meanings are: 2090 2091.. code-block:: none 2092 2093 eAtomTypeNULL - a termination atom that specifies the end of the atom list 2094 eAtomTypeDIEOffset - an offset into the .debug_info section for the DWARF DIE for this name 2095 eAtomTypeCUOffset - an offset into the .debug_info section for the CU that contains the DIE 2096 eAtomTypeDIETag - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is 2097 eAtomTypeNameFlags - Flags for functions and global variables (isFunction, isInlined, isExternal...) 2098 eAtomTypeTypeFlags - Flags for types (isCXXClass, isObjCClass, ...) 2099 2100Then we allow each atom type to define the atom type and how the data for each 2101atom type data is encoded: 2102 2103.. code-block:: c 2104 2105 struct Atom 2106 { 2107 uint16_t type; // AtomType enum value 2108 uint16_t form; // DWARF DW_FORM_XXX defines 2109 }; 2110 2111The ``form`` type above is from the DWARF specification and defines the exact 2112encoding of the data for the Atom type. See the DWARF specification for the 2113``DW_FORM_`` definitions. 2114 2115.. code-block:: c 2116 2117 struct HeaderData 2118 { 2119 uint32_t die_offset_base; 2120 uint32_t atom_count; 2121 Atoms atoms[atom_count0]; 2122 }; 2123 2124``HeaderData`` defines the base DIE offset that should be added to any atoms 2125that are encoded using the ``DW_FORM_ref1``, ``DW_FORM_ref2``, 2126``DW_FORM_ref4``, ``DW_FORM_ref8`` or ``DW_FORM_ref_udata``. It also defines 2127what is contained in each ``HashData`` object -- ``Atom.form`` tells us how large 2128each field will be in the ``HashData`` and the ``Atom.type`` tells us how this data 2129should be interpreted. 2130 2131For the current implementations of the "``.apple_names``" (all functions + 2132globals), the "``.apple_types``" (names of all types that are defined), and 2133the "``.apple_namespaces``" (all namespaces), we currently set the ``Atom`` 2134array to be: 2135 2136.. code-block:: c 2137 2138 HeaderData.atom_count = 1; 2139 HeaderData.atoms[0].type = eAtomTypeDIEOffset; 2140 HeaderData.atoms[0].form = DW_FORM_data4; 2141 2142This defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is 2143encoded as a 32 bit value (DW_FORM_data4). This allows a single name to have 2144multiple matching DIEs in a single file, which could come up with an inlined 2145function for instance. Future tables could include more information about the 2146DIE such as flags indicating if the DIE is a function, method, block, 2147or inlined. 2148 2149The KeyType for the DWARF table is a 32 bit string table offset into the 2150".debug_str" table. The ".debug_str" is the string table for the DWARF which 2151may already contain copies of all of the strings. This helps make sure, with 2152help from the compiler, that we reuse the strings between all of the DWARF 2153sections and keeps the hash table size down. Another benefit to having the 2154compiler generate all strings as DW_FORM_strp in the debug info, is that 2155DWARF parsing can be made much faster. 2156 2157After a lookup is made, we get an offset into the hash data. The hash data 2158needs to be able to deal with 32 bit hash collisions, so the chunk of data 2159at the offset in the hash data consists of a triple: 2160 2161.. code-block:: c 2162 2163 uint32_t str_offset 2164 uint32_t hash_data_count 2165 HashData[hash_data_count] 2166 2167If "str_offset" is zero, then the bucket contents are done. 99.9% of the 2168hash data chunks contain a single item (no 32 bit hash collision): 2169 2170.. code-block:: none 2171 2172 .------------. 2173 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main") 2174 | 0x00000004 | uint32_t HashData count 2175 | 0x........ | uint32_t HashData[0] DIE offset 2176 | 0x........ | uint32_t HashData[1] DIE offset 2177 | 0x........ | uint32_t HashData[2] DIE offset 2178 | 0x........ | uint32_t HashData[3] DIE offset 2179 | 0x00000000 | uint32_t KeyType (end of hash chain) 2180 `------------' 2181 2182If there are collisions, you will have multiple valid string offsets: 2183 2184.. code-block:: none 2185 2186 .------------. 2187 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main") 2188 | 0x00000004 | uint32_t HashData count 2189 | 0x........ | uint32_t HashData[0] DIE offset 2190 | 0x........ | uint32_t HashData[1] DIE offset 2191 | 0x........ | uint32_t HashData[2] DIE offset 2192 | 0x........ | uint32_t HashData[3] DIE offset 2193 | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print") 2194 | 0x00000002 | uint32_t HashData count 2195 | 0x........ | uint32_t HashData[0] DIE offset 2196 | 0x........ | uint32_t HashData[1] DIE offset 2197 | 0x00000000 | uint32_t KeyType (end of hash chain) 2198 `------------' 2199 2200Current testing with real world C++ binaries has shown that there is around 1 220132 bit hash collision per 100,000 name entries. 2202 2203Contents 2204^^^^^^^^ 2205 2206As we said, we want to strictly define exactly what is included in the 2207different tables. For DWARF, we have 3 tables: "``.apple_names``", 2208"``.apple_types``", and "``.apple_namespaces``". 2209 2210"``.apple_names``" sections should contain an entry for each DWARF DIE whose 2211``DW_TAG`` is a ``DW_TAG_label``, ``DW_TAG_inlined_subroutine``, or 2212``DW_TAG_subprogram`` that has address attributes: ``DW_AT_low_pc``, 2213``DW_AT_high_pc``, ``DW_AT_ranges`` or ``DW_AT_entry_pc``. It also contains 2214``DW_TAG_variable`` DIEs that have a ``DW_OP_addr`` in the location (global and 2215static variables). All global and static variables should be included, 2216including those scoped within functions and classes. For example using the 2217following code: 2218 2219.. code-block:: c 2220 2221 static int var = 0; 2222 2223 void f () 2224 { 2225 static int var = 0; 2226 } 2227 2228Both of the static ``var`` variables would be included in the table. All 2229functions should emit both their full names and their basenames. For C or C++, 2230the full name is the mangled name (if available) which is usually in the 2231``DW_AT_MIPS_linkage_name`` attribute, and the ``DW_AT_name`` contains the 2232function basename. If global or static variables have a mangled name in a 2233``DW_AT_MIPS_linkage_name`` attribute, this should be emitted along with the 2234simple name found in the ``DW_AT_name`` attribute. 2235 2236"``.apple_types``" sections should contain an entry for each DWARF DIE whose 2237tag is one of: 2238 2239* DW_TAG_array_type 2240* DW_TAG_class_type 2241* DW_TAG_enumeration_type 2242* DW_TAG_pointer_type 2243* DW_TAG_reference_type 2244* DW_TAG_string_type 2245* DW_TAG_structure_type 2246* DW_TAG_subroutine_type 2247* DW_TAG_typedef 2248* DW_TAG_union_type 2249* DW_TAG_ptr_to_member_type 2250* DW_TAG_set_type 2251* DW_TAG_subrange_type 2252* DW_TAG_base_type 2253* DW_TAG_const_type 2254* DW_TAG_constant 2255* DW_TAG_file_type 2256* DW_TAG_namelist 2257* DW_TAG_packed_type 2258* DW_TAG_volatile_type 2259* DW_TAG_restrict_type 2260* DW_TAG_interface_type 2261* DW_TAG_unspecified_type 2262* DW_TAG_shared_type 2263 2264Only entries with a ``DW_AT_name`` attribute are included, and the entry must 2265not be a forward declaration (``DW_AT_declaration`` attribute with a non-zero 2266value). For example, using the following code: 2267 2268.. code-block:: c 2269 2270 int main () 2271 { 2272 int *b = 0; 2273 return *b; 2274 } 2275 2276We get a few type DIEs: 2277 2278.. code-block:: none 2279 2280 0x00000067: TAG_base_type [5] 2281 AT_encoding( DW_ATE_signed ) 2282 AT_name( "int" ) 2283 AT_byte_size( 0x04 ) 2284 2285 0x0000006e: TAG_pointer_type [6] 2286 AT_type( {0x00000067} ( int ) ) 2287 AT_byte_size( 0x08 ) 2288 2289The DW_TAG_pointer_type is not included because it does not have a ``DW_AT_name``. 2290 2291"``.apple_namespaces``" section should contain all ``DW_TAG_namespace`` DIEs. 2292If we run into a namespace that has no name this is an anonymous namespace, and 2293the name should be output as "``(anonymous namespace)``" (without the quotes). 2294Why? This matches the output of the ``abi::cxa_demangle()`` that is in the 2295standard C++ library that demangles mangled names. 2296 2297 2298Language Extensions and File Format Changes 2299^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2300 2301Objective-C Extensions 2302"""""""""""""""""""""" 2303 2304"``.apple_objc``" section should contain all ``DW_TAG_subprogram`` DIEs for an 2305Objective-C class. The name used in the hash table is the name of the 2306Objective-C class itself. If the Objective-C class has a category, then an 2307entry is made for both the class name without the category, and for the class 2308name with the category. So if we have a DIE at offset 0x1234 with a name of 2309method "``-[NSString(my_additions) stringWithSpecialString:]``", we would add 2310an entry for "``NSString``" that points to DIE 0x1234, and an entry for 2311"``NSString(my_additions)``" that points to 0x1234. This allows us to quickly 2312track down all Objective-C methods for an Objective-C class when doing 2313expressions. It is needed because of the dynamic nature of Objective-C where 2314anyone can add methods to a class. The DWARF for Objective-C methods is also 2315emitted differently from C++ classes where the methods are not usually 2316contained in the class definition, they are scattered about across one or more 2317compile units. Categories can also be defined in different shared libraries. 2318So we need to be able to quickly find all of the methods and class functions 2319given the Objective-C class name, or quickly find all methods and class 2320functions for a class + category name. This table does not contain any 2321selector names, it just maps Objective-C class names (or class names + 2322category) to all of the methods and class functions. The selectors are added 2323as function basenames in the "``.debug_names``" section. 2324 2325In the "``.apple_names``" section for Objective-C functions, the full name is 2326the entire function name with the brackets ("``-[NSString 2327stringWithCString:]``") and the basename is the selector only 2328("``stringWithCString:``"). 2329 2330Mach-O Changes 2331"""""""""""""" 2332 2333The sections names for the apple hash tables are for non-mach-o files. For 2334mach-o files, the sections should be contained in the ``__DWARF`` segment with 2335names as follows: 2336 2337* "``.apple_names``" -> "``__apple_names``" 2338* "``.apple_types``" -> "``__apple_types``" 2339* "``.apple_namespaces``" -> "``__apple_namespac``" (16 character limit) 2340* "``.apple_objc``" -> "``__apple_objc``" 2341 2342