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