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