1 ================================
2 Source Level Debugging with LLVM
3 ================================
11 This document is the central repository for all information pertaining to debug
12 information in LLVM. It describes the :ref:`actual format that the LLVM debug
13 information takes <format>`, which is useful for those interested in creating
14 front-ends or dealing directly with the information. Further, this document
15 provides specific examples of what debug information for C/C++ looks like.
17 Philosophy behind LLVM debugging information
18 --------------------------------------------
20 The idea of the LLVM debugging information is to capture how the important
21 pieces of the source-language's Abstract Syntax Tree map onto LLVM code.
22 Several design aspects have shaped the solution that appears here. The
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.
29 * LLVM optimizations should interact in :ref:`well-defined and easily described
30 ways <intro_debugopt>` with the debugging information.
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.
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.
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.
45 The approach used by the LLVM implementation is to use a small set of
46 :ref:`intrinsic functions <format_common_intrinsics>` to define a mapping
47 between LLVM program objects and the source-level objects. The description of
48 the source-level program is maintained in LLVM metadata in an
49 :ref:`implementation-defined format <ccxx_frontend>` (the C/C++ front-end
50 currently uses working draft 7 of the `DWARF 3 standard
51 <http://www.eagercon.com/dwarf/dwarf3std.htm>`_).
53 When a program is being debugged, a debugger interacts with the user and turns
54 the stored debug information into source-language specific information. As
55 such, a debugger must be aware of the source-language, and is thus tied to a
56 specific language or family of languages.
58 Debug information consumers
59 ---------------------------
61 The role of debug information is to provide meta information normally stripped
62 away during the compilation process. This meta information provides an LLVM
63 user a relationship between generated code and the original program source
66 Currently, debug information is consumed by DwarfDebug to produce dwarf
67 information used by the gdb debugger. Other targets could use the same
68 information to produce stabs or other debug forms.
70 It would also be reasonable to use debug information to feed profiling tools
71 for analysis of generated code, or, tools for reconstructing the original
72 source from generated code.
74 TODO - expound a bit more.
78 Debugging optimized code
79 ------------------------
81 An extremely high priority of LLVM debugging information is to make it interact
82 well with optimizations and analysis. In particular, the LLVM debug
83 information provides the following guarantees:
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
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
99 * LLVM debug information does not prevent optimizations from
100 happening (for example inlining, basic block reordering/merging/cleanup,
101 tail duplication, etc).
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.
108 Basically, the debug information allows you to compile a program with
109 "``-O0 -g``" and get full debug information, allowing you to arbitrarily modify
110 the program as it executes from a debugger. Compiling a program with
111 "``-O3 -g``" gives you full debug information that is always available and
112 accurate for reading (e.g., you get accurate stack traces despite tail call
113 elimination and inlining), but you might lose the ability to modify the program
114 and call functions where were optimized out of the program, or inlined away
117 :ref:`LLVM test suite <test-suite-quickstart>` provides a framework to test
118 optimizer's handling of debugging information. It can be run like this:
122 % cd llvm/projects/test-suite/MultiSource/Benchmarks # or some other level
125 This will test impact of debugging information on optimization passes. If
126 debugging information influences optimization passes then it will be reported
127 as a failure. See :doc:`TestingGuide` for more information on LLVM test
128 infrastructure and how to run various tests.
132 Debugging information format
133 ============================
135 LLVM debugging information has been carefully designed to make it possible for
136 the optimizer to optimize the program and debugging information without
137 necessarily having to know anything about debugging information. In
138 particular, the use of metadata avoids duplicated debugging information from
139 the beginning, and the global dead code elimination pass automatically deletes
140 debugging information for a function if it decides to delete the function.
142 To do this, most of the debugging information (descriptors for types,
143 variables, functions, source files, etc) is inserted by the language front-end
144 in the form of LLVM metadata.
146 Debug information is designed to be agnostic about the target debugger and
147 debugging information representation (e.g. DWARF/Stabs/etc). It uses a generic
148 pass to decode the information that represents variables, types, functions,
149 namespaces, etc: this allows for arbitrary source-language semantics and
150 type-systems to be used, as long as there is a module written for the target
151 debugger to interpret the information.
153 To provide basic functionality, the LLVM debugger does have to make some
154 assumptions about the source-level language being debugged, though it keeps
155 these to a minimum. The only common features that the LLVM debugger assumes
156 exist are :ref:`source files <format_files>`, and :ref:`program objects
157 <format_global_variables>`. These abstract objects are used by a debugger to
158 form stack traces, show information about local variables, etc.
160 This section of the documentation first describes the representation aspects
161 common to any source-language. :ref:`ccxx_frontend` describes the data layout
162 conventions used by the C and C++ front-ends.
164 Debug information descriptors
165 -----------------------------
167 In consideration of the complexity and volume of debug information, LLVM
168 provides a specification for well formed debug descriptors.
170 Consumers of LLVM debug information expect the descriptors for program objects
171 to start in a canonical format, but the descriptors can include additional
172 information appended at the end that is source-language specific. All debugging
173 information objects start with a tag to indicate what type of object it is.
174 The source-language is allowed to define its own objects, by using unreserved
175 tag numbers. We recommend using with tags in the range 0x1000 through 0x2000
176 (there is a defined ``enum DW_TAG_user_base = 0x1000``.)
178 The fields of debug descriptors used internally by LLVM are restricted to only
179 the simple data types ``i32``, ``i1``, ``float``, ``double``, ``mdstring`` and
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.
191 The remaining fields are specific to the descriptor. The values of tags are
192 loosely bound to the tag values of DWARF information entries. However, that
193 does not restrict the use of the information supplied to DWARF targets.
195 The details of the various descriptors follow.
197 Compile unit descriptors
198 ^^^^^^^^^^^^^^^^^^^^^^^^
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.
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
218 These descriptors contain a source language ID for the file (we use the DWARF
219 3.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
221 strings for the source file name and the working directory, as well as an
222 identifier string for the compiler that produced it.
224 Compile unit descriptors provide the root context for objects declared in a
225 specific compilation unit. File descriptors are defined using this context.
226 These descriptors are collected by a named metadata ``!llvm.dbg.cu``. They
227 keep track of subprograms, global variables, type information, and imported
228 entities (declarations and namespaces).
238 i32, ;; Tag = 41 (DW_TAG_file_type)
239 metadata, ;; Source directory (including trailing slash) & file pair
242 These descriptors contain information for a file. Global variables and top
243 level functions would be defined using this context. File descriptors also
244 provide context for source line correspondence.
246 Each input file is encoded as a separate file descriptor in LLVM debugging
249 .. _format_global_variables:
251 Global variable descriptors
252 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
257 i32, ;; Tag = 52 (DW_TAG_variable)
258 i32, ;; Unused field.
259 metadata, ;; Reference to context descriptor
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
272 These descriptors provide debug information about globals variables. They
273 provide details such as name, type and where the variable is defined. All
274 global variables are collected inside the named metadata ``!llvm.dbg.cu``.
276 .. _format_subprograms:
278 Subprogram descriptors
279 ^^^^^^^^^^^^^^^^^^^^^^
284 i32, ;; Tag = 46 (DW_TAG_subprogram)
285 metadata, ;; Source directory (including trailing slash) & file pair
286 metadata, ;; Reference to context descriptor
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
298 i32, ;; Flags - Artificial, Private, Protected, Explicit, Prototyped.
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
307 These descriptors provide debug information about functions, methods and
308 subprograms. They provide details such as name, return types and the source
309 location where the subprogram is defined.
317 i32, ;; Tag = 11 (DW_TAG_lexical_block)
318 metadata,;; Source directory (including trailing slash) & file pair
319 metadata,;; Reference to context descriptor
321 i32, ;; Column number
322 i32 ;; Unique ID to identify blocks from a template function
325 This descriptor provides debug information about nested blocks within a
326 subprogram. The line number and column numbers are used to dinstinguish two
327 lexical blocks at same depth.
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
337 This descriptor provides a wrapper around a lexical scope to handle file
338 changes in the middle of a lexical block.
340 .. _format_basic_type:
342 Basic type descriptors
343 ^^^^^^^^^^^^^^^^^^^^^^
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)
354 i64, ;; Alignment in bits
355 i64, ;; Offset in bits
357 i32 ;; DWARF type encoding
360 These descriptors define primitive types used in the code. Example ``int``,
361 ``bool`` and ``float``. The context provides the scope of the type, which is
362 usually the top level. Since basic types are not usually user defined the
363 context and line number can be left as NULL and 0. The size, alignment and
364 offset are expressed in bits and can be 64 bit values. The alignment is used
365 to round the offset when embedded in a :ref:`composite type
366 <format_composite_type>` (example to keep float doubles on 64 bit boundaries).
367 The offset is the bit offset if embedded in a :ref:`composite type
368 <format_composite_type>`.
370 The type encoding provides the details of the type. The values are typically
371 one of the following:
379 DW_ATE_signed_char = 6
381 DW_ATE_unsigned_char = 8
383 .. _format_derived_type:
385 Derived type descriptors
386 ^^^^^^^^^^^^^^^^^^^^^^^^
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)
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.
409 These descriptors are used to define types derived from other types. The value
410 of the tag varies depending on the meaning. The following are possible tag
415 DW_TAG_formal_parameter = 5
417 DW_TAG_pointer_type = 15
418 DW_TAG_reference_type = 16
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
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
427 of 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
429 argument of a subprogram.
431 ``DW_TAG_typedef`` is used to provide a name for the derived type.
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>`.
437 :ref:`Derived type <format_derived_type>` location can be determined from the
438 context and line number. The size, alignment and offset are expressed in bits
439 and can be 64 bit values. The alignment is used to round the offset when
440 embedded in a :ref:`composite type <format_composite_type>` (example to keep
441 float doubles on 64 bit boundaries.) The offset is the bit offset if embedded
442 in a :ref:`composite type <format_composite_type>`.
444 Note that the ``void *`` type is expressed as a type derived from NULL.
446 .. _format_composite_type:
448 Composite type descriptors
449 ^^^^^^^^^^^^^^^^^^^^^^^^^^
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)
460 i64, ;; Alignment in bits
461 i64, ;; Offset in bits
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 metadata ;; A unique identifier for type uniquing purpose (may be null)
471 These descriptors are used to define types that are composed of 0 or more
472 elements. The value of the tag varies depending on the meaning. The following
473 are possible tag values:
477 DW_TAG_array_type = 1
478 DW_TAG_enumeration_type = 4
479 DW_TAG_structure_type = 19
480 DW_TAG_union_type = 23
481 DW_TAG_subroutine_type = 21
482 DW_TAG_inheritance = 28
484 The vector flag indicates that an array type is a native packed vector.
486 The members of array types (tag = ``DW_TAG_array_type``) are
487 :ref:`subrange descriptors <format_subrange>`, each
488 representing the range of subscripts at that level of indexing.
490 The members of enumeration types (tag = ``DW_TAG_enumeration_type``) are
491 :ref:`enumerator descriptors <format_enumerator>`, each representing the
492 definition of enumeration value for the set. All enumeration type descriptors
493 are collected inside the named metadata ``!llvm.dbg.cu``.
495 The members of structure (tag = ``DW_TAG_structure_type``) or union (tag =
496 ``DW_TAG_union_type``) types are any one of the :ref:`basic
497 <format_basic_type>`, :ref:`derived <format_derived_type>` or :ref:`composite
498 <format_composite_type>` type descriptors, each representing a field member of
499 the structure or union.
501 For C++ classes (tag = ``DW_TAG_structure_type``), member descriptors provide
502 information about base classes, static members and member functions. If a
503 member is a :ref:`derived type descriptor <format_derived_type>` and has a tag
504 of ``DW_TAG_inheritance``, then the type represents a base class. If the member
505 of is a :ref:`global variable descriptor <format_global_variables>` then it
506 represents a static member. And, if the member is a :ref:`subprogram
507 descriptor <format_subprograms>` then it represents a member function. For
508 static members and member functions, ``getName()`` returns the members link or
509 the C++ mangled name. ``getDisplayName()`` the simplied version of the name.
511 The first member of subroutine (tag = ``DW_TAG_subroutine_type``) type elements
512 is the return type for the subroutine. The remaining elements are the formal
513 arguments to the subroutine.
515 :ref:`Composite type <format_composite_type>` location can be determined from
516 the context and line number. The size, alignment and offset are expressed in
517 bits and can be 64 bit values. The alignment is used to round the offset when
518 embedded in a :ref:`composite type <format_composite_type>` (as an example, to
519 keep float doubles on 64 bit boundaries). The offset is the bit offset if
520 embedded in a :ref:`composite type <format_composite_type>`.
530 i32, ;; Tag = 33 (DW_TAG_subrange_type)
535 These descriptors are used to define ranges of array subscripts for an array
536 :ref:`composite type <format_composite_type>`. The low value defines the lower
537 bounds typically zero for C/C++. The high value is the upper bounds. Values
538 are 64 bit. ``High - Low + 1`` is the size of the array. If ``Low > High``
539 the array bounds are not included in generated debugging information.
541 .. _format_enumerator:
543 Enumerator descriptors
544 ^^^^^^^^^^^^^^^^^^^^^^
549 i32, ;; Tag = 40 (DW_TAG_enumerator)
554 These descriptors are used to define members of an enumeration :ref:`composite
555 type <format_composite_type>`, it associates the name to the value.
563 i32, ;; Tag (see below)
566 metadata, ;; Reference to file where defined
567 i32, ;; 24 bit - Line number where defined
568 ;; 8 bit - Argument number. 1 indicates 1st argument.
569 metadata, ;; Type descriptor
571 metadata ;; (optional) Reference to inline location
574 These descriptors are used to define variables local to a sub program. The
575 value of the tag depends on the usage of the variable:
579 DW_TAG_auto_variable = 256
580 DW_TAG_arg_variable = 257
582 An auto variable is any variable declared in the body of the function. An
583 argument variable is any variable that appears as a formal argument to the
586 The context is either the subprogram or block where the variable is defined.
587 Name the source variable name. Context and line indicate where the variable
588 was defined. Type descriptor defines the declared type of the variable.
590 .. _format_common_intrinsics:
592 Debugger intrinsic functions
593 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
595 LLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to
596 provide debug information at various points in generated code.
603 void %llvm.dbg.declare(metadata, metadata)
605 This intrinsic provides information about a local element (e.g., variable).
606 The first argument is metadata holding the alloca for the variable. The second
607 argument is metadata containing a description of the variable.
614 void %llvm.dbg.value(metadata, i64, metadata)
616 This intrinsic provides information when a user source variable is set to a new
617 value. The first argument is the new value (wrapped as metadata). The second
618 argument is the offset in the user source variable where the new value is
619 written. The third argument is metadata containing a description of the user
622 Object lifetimes and scoping
623 ============================
625 In many languages, the local variables in functions can have their lifetimes or
626 scopes limited to a subset of a function. In the C family of languages, for
627 example, variables are only live (readable and writable) within the source
628 block that they are defined in. In functional languages, values are only
629 readable after they have been defined. Though this is a very obvious concept,
630 it is non-trivial to model in LLVM, because it has no notion of scoping in this
631 sense, and does not want to be tied to a language's scoping rules.
633 In order to handle this, the LLVM debug format uses the metadata attached to
634 llvm instructions to encode line number and scoping information. Consider the
635 following C fragment, for example:
649 Compiled to LLVM, this function would be represented like this:
653 define void @foo() #0 {
655 %X = alloca i32, align 4
656 %Y = alloca i32, align 4
657 %Z = alloca i32, align 4
658 call void @llvm.dbg.declare(metadata !{i32* %X}, metadata !10), !dbg !12
659 ; [debug line = 2:7] [debug variable = X]
660 store i32 21, i32* %X, align 4, !dbg !12
661 call void @llvm.dbg.declare(metadata !{i32* %Y}, metadata !13), !dbg !14
662 ; [debug line = 3:7] [debug variable = Y]
663 store i32 22, i32* %Y, align 4, !dbg !14
664 call void @llvm.dbg.declare(metadata !{i32* %Z}, metadata !15), !dbg !17
665 ; [debug line = 5:9] [debug variable = Z]
666 store i32 23, i32* %Z, align 4, !dbg !17
667 %0 = load i32* %X, align 4, !dbg !18
669 store i32 %0, i32* %Z, align 4, !dbg !18
670 %1 = load i32* %Y, align 4, !dbg !19
672 store i32 %1, i32* %X, align 4, !dbg !19
676 ; Function Attrs: nounwind readnone
677 declare void @llvm.dbg.declare(metadata, metadata) #1
679 attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false"
680 "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
681 "no-infs-fp-math"="false" "no-nans-fp-math"="false"
682 "stack-protector-buffer-size"="8" "unsafe-fp-math"="false"
683 "use-soft-float"="false" }
684 attributes #1 = { nounwind readnone }
687 !llvm.module.flags = !{!8}
690 !0 = metadata !{i32 786449, metadata !1, i32 12,
691 metadata !"clang version 3.4 (trunk 193128) (llvm/trunk 193139)",
692 i1 false, metadata !"", i32 0, metadata !2, metadata !2, metadata !3,
693 metadata !2, metadata !2, metadata !""} ; [ DW_TAG_compile_unit ] \
694 [/private/tmp/foo.c] \
696 !1 = metadata !{metadata !"t.c", metadata !"/private/tmp"}
697 !2 = metadata !{i32 0}
698 !3 = metadata !{metadata !4}
699 !4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"foo",
700 metadata !"foo", metadata !"", i32 1, metadata !6,
701 i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false,
702 void ()* @foo, null, null, metadata !2, i32 1}
703 ; [ DW_TAG_subprogram ] [line 1] [def] [foo]
704 !5 = metadata !{i32 786473, metadata !1} ; [ DW_TAG_file_type ] \
706 !6 = metadata !{i32 786453, i32 0, null, metadata !"", i32 0, i64 0, i64 0,
707 i64 0, i32 0, null, metadata !7, i32 0, null, null, null}
708 ; [ DW_TAG_subroutine_type ] \
709 [line 0, size 0, align 0, offset 0] [from ]
710 !7 = metadata !{null}
711 !8 = metadata !{i32 2, metadata !"Dwarf Version", i32 2}
712 !9 = metadata !{metadata !"clang version 3.4 (trunk 193128) (llvm/trunk 193139)"}
713 !10 = metadata !{i32 786688, metadata !4, metadata !"X", metadata !5, i32 2,
714 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [X] \
716 !11 = metadata !{i32 786468, null, null, metadata !"int", i32 0, i64 32,
717 i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] [int] \
718 [line 0, size 32, align 32, offset 0, enc DW_ATE_signed]
719 !12 = metadata !{i32 2, i32 0, metadata !4, null}
720 !13 = metadata !{i32 786688, metadata !4, metadata !"Y", metadata !5, i32 3,
721 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [Y] \
723 !14 = metadata !{i32 3, i32 0, metadata !4, null}
724 !15 = metadata !{i32 786688, metadata !16, metadata !"Z", metadata !5, i32 5,
725 metadata !11, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [Z] \
727 !16 = metadata !{i32 786443, metadata !1, metadata !4, i32 4, i32 0, i32 0} \
728 ; [ DW_TAG_lexical_block ] [/private/tmp/t.c]
729 !17 = metadata !{i32 5, i32 0, metadata !16, null}
730 !18 = metadata !{i32 6, i32 0, metadata !16, null}
731 !19 = metadata !{i32 8, i32 0, metadata !4, null} ; [ DW_TAG_imported_declaration ]
732 !20 = metadata !{i32 9, i32 0, metadata !4, null}
734 This example illustrates a few important details about LLVM debugging
735 information. In particular, it shows how the ``llvm.dbg.declare`` intrinsic and
736 location information, which are attached to an instruction, are applied
737 together to allow a debugger to analyze the relationship between statements,
738 variable definitions, and the code used to implement the function.
742 call void @llvm.dbg.declare(metadata !{i32* %X}, metadata !10), !dbg !12
743 ; [debug line = 2:7] [debug variable = X]
745 The first intrinsic ``%llvm.dbg.declare`` encodes debugging information for the
746 variable ``X``. The metadata ``!dbg !12`` attached to the intrinsic provides
747 scope information for the variable ``X``.
751 !12 = metadata !{i32 2, i32 0, metadata !4, null}
752 !4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"foo",
753 metadata !"foo", metadata !"", i32 1, metadata !6,
754 i1 false, i1 true, i32 0, i32 0, null, i32 0, i1 false,
755 void ()* @foo, null, null, metadata !2, i32 1}
756 ; [ DW_TAG_subprogram ] [line 1] [def] [foo]
758 Here ``!12`` is metadata providing location information. It has four fields:
759 line number, column number, scope, and original scope. The original scope
760 represents inline location if this instruction is inlined inside a caller, and
761 is null otherwise. In this example, scope is encoded by ``!4``, a
762 :ref:`subprogram descriptor <format_subprograms>`. This way the location
763 information attached to the intrinsics indicates that the variable ``X`` is
764 declared at line number 2 at a function level scope in function ``foo``.
766 Now lets take another example.
770 call void @llvm.dbg.declare(metadata !{i32* %Z}, metadata !15), !dbg !17
771 ; [debug line = 5:9] [debug variable = Z]
773 The third intrinsic ``%llvm.dbg.declare`` encodes debugging information for
774 variable ``Z``. The metadata ``!dbg !17`` attached to the intrinsic provides
775 scope information for the variable ``Z``.
779 !16 = metadata !{i32 786443, metadata !1, metadata !4, i32 4, i32 0, i32 0}
780 ; [ DW_TAG_lexical_block ] [/private/tmp/t.c]
781 !17 = metadata !{i32 5, i32 0, metadata !16, null}
783 Here ``!15`` indicates that ``Z`` is declared at line number 5 and
784 column number 0 inside of lexical scope ``!16``. The lexical scope itself
785 resides inside of subprogram ``!4`` described above.
787 The scope information attached with each instruction provides a straightforward
788 way to find instructions covered by a scope.
792 C/C++ front-end specific debug information
793 ==========================================
795 The C and C++ front-ends represent information about the program in a format
796 that is effectively identical to `DWARF 3.0
797 <http://www.eagercon.com/dwarf/dwarf3std.htm>`_ in terms of information
798 content. This allows code generators to trivially support native debuggers by
799 generating standard dwarf information, and contains enough information for
800 non-dwarf targets to translate it as needed.
802 This section describes the forms used to represent C and C++ programs. Other
803 languages could pattern themselves after this (which itself is tuned to
804 representing programs in the same way that DWARF 3 does), or they could choose
805 to provide completely different forms if they don't fit into the DWARF model.
806 As support for debugging information gets added to the various LLVM
807 source-language front-ends, the information used should be documented here.
809 The following sections provide examples of various C/C++ constructs and the
810 debug information that would best describe those constructs.
812 C/C++ source file information
813 -----------------------------
815 Given the source files ``MySource.cpp`` and ``MyHeader.h`` located in the
816 directory ``/Users/mine/sources``, the following code:
820 #include "MyHeader.h"
822 int main(int argc, char *argv[]) {
826 a C/C++ front-end would generate the following descriptors:
832 ;; Define the compile unit for the main source file "/Users/mine/sources/MySource.cpp".
836 metadata !1, ;; File/directory name
837 i32 4, ;; Language Id
838 metadata !"clang version 3.4 ",
839 i1 false, ;; Optimized compile unit
840 metadata !"", ;; Compiler flags
841 i32 0, ;; Runtime version
842 metadata !2, ;; Enumeration types
843 metadata !2, ;; Retained types
844 metadata !3, ;; Subprograms
845 metadata !2, ;; Global variables
846 metadata !2, ;; Imported entities (declarations and namespaces)
847 metadata !"" ;; Split debug filename
851 ;; Define the file for the file "/Users/mine/sources/MySource.cpp".
854 metadata !"MySource.cpp",
855 metadata !"/Users/mine/sources"
863 ;; Define the file for the file "/Users/mine/sources/Myheader.h"
870 metadata !"./MyHeader.h",
871 metadata !"/Users/mine/sources",
876 ``llvm::Instruction`` provides easy access to metadata attached with an
877 instruction. One can extract line number information encoded in LLVM IR using
878 ``Instruction::getMetadata()`` and ``DILocation::getLineNumber()``.
882 if (MDNode *N = I->getMetadata("dbg")) { // Here I is an LLVM instruction
883 DILocation Loc(N); // DILocation is in DebugInfo.h
884 unsigned Line = Loc.getLineNumber();
885 StringRef File = Loc.getFilename();
886 StringRef Dir = Loc.getDirectory();
889 C/C++ global variable information
890 ---------------------------------
892 Given an integer global variable declared as follows:
898 a C/C++ front-end would generate the following descriptors:
903 ;; Define the global itself.
905 %MyGlobal = global int 100
908 ;; List of debug info of globals
912 ;; Define the compile unit.
917 metadata !"foo.cpp", ;; File
918 metadata !"/Volumes/Data/tmp", ;; Directory
919 metadata !"clang version 3.1 ", ;; Producer
920 i1 true, ;; Deprecated field
921 i1 false, ;; "isOptimized"?
922 metadata !"", ;; Flags
923 i32 0, ;; Runtime Version
924 metadata !1, ;; Enum Types
925 metadata !1, ;; Retained Types
926 metadata !1, ;; Subprograms
927 metadata !3, ;; Global Variables
928 metadata !1, ;; Imported entities
929 "", ;; Split debug filename
930 } ; [ DW_TAG_compile_unit ]
932 ;; The Array of Global Variables
938 ;; Define the global variable itself.
944 metadata !"MyGlobal", ;; Name
945 metadata !"MyGlobal", ;; Display Name
946 metadata !"", ;; Linkage Name
950 i32 0, ;; IsLocalToUnit
951 i32 1, ;; IsDefinition
952 i32* @MyGlobal, ;; LLVM-IR Value
953 null ;; Static member declaration
954 } ; [ DW_TAG_variable ]
960 metadata !"foo.cpp", ;; File
961 metadata !"/Volumes/Data/tmp", ;; Directory
965 metadata !5 ;; Unused
966 } ; [ DW_TAG_file_type ]
975 metadata !"int", ;; Name
977 i64 32, ;; Size in Bits
978 i64 32, ;; Align in Bits
982 } ; [ DW_TAG_base_type ]
984 C/C++ function information
985 --------------------------
987 Given a function declared as follows:
991 int main(int argc, char *argv[]) {
995 a C/C++ front-end would generate the following descriptors:
1000 ;; Define the anchor for subprograms.
1004 metadata !1, ;; File
1005 metadata !1, ;; Context
1006 metadata !"main", ;; Name
1007 metadata !"main", ;; Display name
1008 metadata !"main", ;; Linkage name
1009 i32 1, ;; Line number
1010 metadata !4, ;; Type
1011 i1 false, ;; Is local
1012 i1 true, ;; Is definition
1013 i32 0, ;; Virtuality attribute, e.g. pure virtual function
1014 i32 0, ;; Index into virtual table for C++ methods
1015 i32 0, ;; Type that holds virtual table.
1017 i1 false, ;; True if this function is optimized
1018 Function *, ;; Pointer to llvm::Function
1019 null, ;; Function template parameters
1020 null, ;; List of function variables (emitted when optimizing)
1021 1 ;; Line number of the opening '{' of the function
1024 ;; Define the subprogram itself.
1026 define i32 @main(i32 %argc, i8** %argv) {
1033 The following are the basic type descriptors for C/C++ core types:
1038 .. code-block:: llvm
1044 metadata !"bool", ;; Name
1045 i32 0, ;; Line number
1046 i64 8, ;; Size in Bits
1047 i64 8, ;; Align in Bits
1048 i64 0, ;; Offset in Bits
1056 .. code-block:: llvm
1062 metadata !"char", ;; Name
1063 i32 0, ;; Line number
1064 i64 8, ;; Size in Bits
1065 i64 8, ;; Align in Bits
1066 i64 0, ;; Offset in Bits
1074 .. code-block:: llvm
1080 metadata !"unsigned char",
1081 i32 0, ;; Line number
1082 i64 8, ;; Size in Bits
1083 i64 8, ;; Align in Bits
1084 i64 0, ;; Offset in Bits
1092 .. code-block:: llvm
1098 metadata !"short int",
1099 i32 0, ;; Line number
1100 i64 16, ;; Size in Bits
1101 i64 16, ;; Align in Bits
1102 i64 0, ;; Offset in Bits
1110 .. code-block:: llvm
1116 metadata !"short unsigned int",
1117 i32 0, ;; Line number
1118 i64 16, ;; Size in Bits
1119 i64 16, ;; Align in Bits
1120 i64 0, ;; Offset in Bits
1128 .. code-block:: llvm
1134 metadata !"int", ;; Name
1135 i32 0, ;; Line number
1136 i64 32, ;; Size in Bits
1137 i64 32, ;; Align in Bits
1138 i64 0, ;; Offset in Bits
1146 .. code-block:: llvm
1152 metadata !"unsigned int",
1153 i32 0, ;; Line number
1154 i64 32, ;; Size in Bits
1155 i64 32, ;; Align in Bits
1156 i64 0, ;; Offset in Bits
1164 .. code-block:: llvm
1170 metadata !"long long int",
1171 i32 0, ;; Line number
1172 i64 64, ;; Size in Bits
1173 i64 64, ;; Align in Bits
1174 i64 0, ;; Offset in Bits
1182 .. code-block:: llvm
1188 metadata !"long long unsigned int",
1189 i32 0, ;; Line number
1190 i64 64, ;; Size in Bits
1191 i64 64, ;; Align in Bits
1192 i64 0, ;; Offset in Bits
1200 .. code-block:: llvm
1207 i32 0, ;; Line number
1208 i64 32, ;; Size in Bits
1209 i64 32, ;; Align in Bits
1210 i64 0, ;; Offset in Bits
1218 .. code-block:: llvm
1224 metadata !"double",;; Name
1225 i32 0, ;; Line number
1226 i64 64, ;; Size in Bits
1227 i64 64, ;; Align in Bits
1228 i64 0, ;; Offset in Bits
1236 Given the following as an example of C/C++ derived type:
1240 typedef const int *IntPtr;
1242 a C/C++ front-end would generate the following descriptors:
1244 .. code-block:: llvm
1247 ;; Define the typedef "IntPtr".
1251 metadata !3, ;; File
1252 metadata !1, ;; Context
1253 metadata !"IntPtr", ;; Name
1254 i32 0, ;; Line number
1255 i64 0, ;; Size in bits
1256 i64 0, ;; Align in bits
1257 i64 0, ;; Offset in bits
1259 metadata !4 ;; Derived From type
1262 ;; Define the pointer type.
1268 metadata !"", ;; Name
1269 i32 0, ;; Line number
1270 i64 64, ;; Size in bits
1271 i64 64, ;; Align in bits
1272 i64 0, ;; Offset in bits
1274 metadata !5 ;; Derived From type
1277 ;; Define the const type.
1283 metadata !"", ;; Name
1284 i32 0, ;; Line number
1285 i64 0, ;; Size in bits
1286 i64 0, ;; Align in bits
1287 i64 0, ;; Offset in bits
1289 metadata !6 ;; Derived From type
1292 ;; Define the int type.
1298 metadata !"int", ;; Name
1299 i32 0, ;; Line number
1300 i64 32, ;; Size in bits
1301 i64 32, ;; Align in bits
1302 i64 0, ;; Offset in bits
1307 C/C++ struct/union types
1308 ------------------------
1310 Given the following as an example of C/C++ struct type:
1320 a C/C++ front-end would generate the following descriptors:
1322 .. code-block:: llvm
1325 ;; Define basic type for unsigned int.
1331 metadata !"unsigned int",
1332 i32 0, ;; Line number
1333 i64 32, ;; Size in Bits
1334 i64 32, ;; Align in Bits
1335 i64 0, ;; Offset in Bits
1340 ;; Define composite type for struct Color.
1344 metadata !1, ;; Compile unit
1346 metadata !"Color", ;; Name
1347 i32 1, ;; Line number
1348 i64 96, ;; Size in bits
1349 i64 32, ;; Align in bits
1350 i64 0, ;; Offset in bits
1352 null, ;; Derived From
1353 metadata !3, ;; Elements
1354 i32 0, ;; Runtime Language
1355 null, ;; Base type containing the vtable pointer for this type
1356 null ;; Template parameters
1360 ;; Define the Red field.
1364 metadata !1, ;; File
1365 metadata !1, ;; Context
1366 metadata !"Red", ;; Name
1367 i32 2, ;; Line number
1368 i64 32, ;; Size in bits
1369 i64 32, ;; Align in bits
1370 i64 0, ;; Offset in bits
1372 metadata !5 ;; Derived From type
1376 ;; Define the Green field.
1380 metadata !1, ;; File
1381 metadata !1, ;; Context
1382 metadata !"Green", ;; Name
1383 i32 3, ;; Line number
1384 i64 32, ;; Size in bits
1385 i64 32, ;; Align in bits
1386 i64 32, ;; Offset in bits
1388 metadata !5 ;; Derived From type
1392 ;; Define the Blue field.
1396 metadata !1, ;; File
1397 metadata !1, ;; Context
1398 metadata !"Blue", ;; Name
1399 i32 4, ;; Line number
1400 i64 32, ;; Size in bits
1401 i64 32, ;; Align in bits
1402 i64 64, ;; Offset in bits
1404 metadata !5 ;; Derived From type
1408 ;; Define the array of fields used by the composite type Color.
1410 !3 = metadata !{metadata !4, metadata !6, metadata !7}
1412 C/C++ enumeration types
1413 -----------------------
1415 Given the following as an example of C/C++ enumeration type:
1425 a C/C++ front-end would generate the following descriptors:
1427 .. code-block:: llvm
1430 ;; Define composite type for enum Trees
1434 metadata !1, ;; File
1435 metadata !1, ;; Context
1436 metadata !"Trees", ;; Name
1437 i32 1, ;; Line number
1438 i64 32, ;; Size in bits
1439 i64 32, ;; Align in bits
1440 i64 0, ;; Offset in bits
1442 null, ;; Derived From type
1443 metadata !3, ;; Elements
1444 i32 0 ;; Runtime language
1448 ;; Define the array of enumerators used by composite type Trees.
1450 !3 = metadata !{metadata !4, metadata !5, metadata !6}
1453 ;; Define Spruce enumerator.
1455 !4 = metadata !{i32 786472, metadata !"Spruce", i64 100}
1458 ;; Define Oak enumerator.
1460 !5 = metadata !{i32 786472, metadata !"Oak", i64 200}
1463 ;; Define Maple enumerator.
1465 !6 = metadata !{i32 786472, metadata !"Maple", i64 300}
1467 Debugging information format
1468 ============================
1470 Debugging Information Extension for Objective C Properties
1471 ----------------------------------------------------------
1476 Objective C provides a simpler way to declare and define accessor methods using
1477 declared properties. The language provides features to declare a property and
1478 to let compiler synthesize accessor methods.
1480 The debugger lets developer inspect Objective C interfaces and their instance
1481 variables and class variables. However, the debugger does not know anything
1482 about the properties defined in Objective C interfaces. The debugger consumes
1483 information generated by compiler in DWARF format. The format does not support
1484 encoding of Objective C properties. This proposal describes DWARF extensions to
1485 encode Objective C properties, which the debugger can use to let developers
1486 inspect Objective C properties.
1491 Objective C properties exist separately from class members. A property can be
1492 defined only by "setter" and "getter" selectors, and be calculated anew on each
1493 access. Or a property can just be a direct access to some declared ivar.
1494 Finally it can have an ivar "automatically synthesized" for it by the compiler,
1495 in which case the property can be referred to in user code directly using the
1496 standard C dereference syntax as well as through the property "dot" syntax, but
1497 there is no entry in the ``@interface`` declaration corresponding to this ivar.
1499 To facilitate debugging, these properties we will add a new DWARF TAG into the
1500 ``DW_TAG_structure_type`` definition for the class to hold the description of a
1501 given property, and a set of DWARF attributes that provide said description.
1502 The property tag will also contain the name and declared type of the property.
1504 If there is a related ivar, there will also be a DWARF property attribute placed
1505 in the ``DW_TAG_member`` DIE for that ivar referring back to the property TAG
1506 for that property. And in the case where the compiler synthesizes the ivar
1507 directly, the compiler is expected to generate a ``DW_TAG_member`` for that
1508 ivar (with the ``DW_AT_artificial`` set to 1), whose name will be the name used
1509 to access this ivar directly in code, and with the property attribute pointing
1510 back to the property it is backing.
1512 The following examples will serve as illustration for our discussion:
1514 .. code-block:: objc
1526 @synthesize p2 = n2;
1529 This produces the following DWARF (this is a "pseudo dwarfdump" output):
1531 .. code-block:: none
1533 0x00000100: TAG_structure_type [7] *
1534 AT_APPLE_runtime_class( 0x10 )
1536 AT_decl_file( "Objc_Property.m" )
1539 0x00000110 TAG_APPLE_property
1541 AT_type ( {0x00000150} ( int ) )
1543 0x00000120: TAG_APPLE_property
1545 AT_type ( {0x00000150} ( int ) )
1547 0x00000130: TAG_member [8]
1549 AT_APPLE_property ( {0x00000110} "p1" )
1550 AT_type( {0x00000150} ( int ) )
1551 AT_artificial ( 0x1 )
1553 0x00000140: TAG_member [8]
1555 AT_APPLE_property ( {0x00000120} "p2" )
1556 AT_type( {0x00000150} ( int ) )
1558 0x00000150: AT_type( ( int ) )
1560 Note, the current convention is that the name of the ivar for an
1561 auto-synthesized property is the name of the property from which it derives
1562 with an underscore prepended, as is shown in the example. But we actually
1563 don't need to know this convention, since we are given the name of the ivar
1566 Also, it is common practice in ObjC to have different property declarations in
1567 the @interface and @implementation - e.g. to provide a read-only property in
1568 the interface,and a read-write interface in the implementation. In that case,
1569 the compiler should emit whichever property declaration will be in force in the
1570 current translation unit.
1572 Developers can decorate a property with attributes which are encoded using
1573 ``DW_AT_APPLE_property_attribute``.
1575 .. code-block:: objc
1577 @property (readonly, nonatomic) int pr;
1579 .. code-block:: none
1581 TAG_APPLE_property [8]
1583 AT_type ( {0x00000147} (int) )
1584 AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic)
1586 The setter and getter method names are attached to the property using
1587 ``DW_AT_APPLE_property_setter`` and ``DW_AT_APPLE_property_getter`` attributes.
1589 .. code-block:: objc
1592 @property (setter=myOwnP3Setter:) int p3;
1593 -(void)myOwnP3Setter:(int)a;
1598 -(void)myOwnP3Setter:(int)a{ }
1601 The DWARF for this would be:
1603 .. code-block:: none
1605 0x000003bd: TAG_structure_type [7] *
1606 AT_APPLE_runtime_class( 0x10 )
1608 AT_decl_file( "Objc_Property.m" )
1611 0x000003cd TAG_APPLE_property
1613 AT_APPLE_property_setter ( "myOwnP3Setter:" )
1614 AT_type( {0x00000147} ( int ) )
1616 0x000003f3: TAG_member [8]
1618 AT_type ( {0x00000147} ( int ) )
1619 AT_APPLE_property ( {0x000003cd} )
1620 AT_artificial ( 0x1 )
1625 +-----------------------+--------+
1627 +=======================+========+
1628 | DW_TAG_APPLE_property | 0x4200 |
1629 +-----------------------+--------+
1631 New DWARF Attributes
1632 ^^^^^^^^^^^^^^^^^^^^
1634 +--------------------------------+--------+-----------+
1635 | Attribute | Value | Classes |
1636 +================================+========+===========+
1637 | DW_AT_APPLE_property | 0x3fed | Reference |
1638 +--------------------------------+--------+-----------+
1639 | DW_AT_APPLE_property_getter | 0x3fe9 | String |
1640 +--------------------------------+--------+-----------+
1641 | DW_AT_APPLE_property_setter | 0x3fea | String |
1642 +--------------------------------+--------+-----------+
1643 | DW_AT_APPLE_property_attribute | 0x3feb | Constant |
1644 +--------------------------------+--------+-----------+
1649 +--------------------------------+-------+
1651 +================================+=======+
1652 | DW_AT_APPLE_PROPERTY_readonly | 0x1 |
1653 +--------------------------------+-------+
1654 | DW_AT_APPLE_PROPERTY_readwrite | 0x2 |
1655 +--------------------------------+-------+
1656 | DW_AT_APPLE_PROPERTY_assign | 0x4 |
1657 +--------------------------------+-------+
1658 | DW_AT_APPLE_PROPERTY_retain | 0x8 |
1659 +--------------------------------+-------+
1660 | DW_AT_APPLE_PROPERTY_copy | 0x10 |
1661 +--------------------------------+-------+
1662 | DW_AT_APPLE_PROPERTY_nonatomic | 0x20 |
1663 +--------------------------------+-------+
1665 Name Accelerator Tables
1666 -----------------------
1671 The "``.debug_pubnames``" and "``.debug_pubtypes``" formats are not what a
1672 debugger needs. The "``pub``" in the section name indicates that the entries
1673 in the table are publicly visible names only. This means no static or hidden
1674 functions show up in the "``.debug_pubnames``". No static variables or private
1675 class variables are in the "``.debug_pubtypes``". Many compilers add different
1676 things to these tables, so we can't rely upon the contents between gcc, icc, or
1679 The typical query given by users tends not to match up with the contents of
1680 these tables. For example, the DWARF spec states that "In the case of the name
1681 of a function member or static data member of a C++ structure, class or union,
1682 the name presented in the "``.debug_pubnames``" section is not the simple name
1683 given by the ``DW_AT_name attribute`` of the referenced debugging information
1684 entry, but rather the fully qualified name of the data or function member."
1685 So the only names in these tables for complex C++ entries is a fully
1686 qualified name. Debugger users tend not to enter their search strings as
1687 "``a::b::c(int,const Foo&) const``", but rather as "``c``", "``b::c``" , or
1688 "``a::b::c``". So the name entered in the name table must be demangled in
1689 order to chop it up appropriately and additional names must be manually entered
1690 into the table to make it effective as a name lookup table for debuggers to
1693 All debuggers currently ignore the "``.debug_pubnames``" table as a result of
1694 its inconsistent and useless public-only name content making it a waste of
1695 space in the object file. These tables, when they are written to disk, are not
1696 sorted in any way, leaving every debugger to do its own parsing and sorting.
1697 These tables also include an inlined copy of the string values in the table
1698 itself making the tables much larger than they need to be on disk, especially
1699 for large C++ programs.
1701 Can't we just fix the sections by adding all of the names we need to this
1702 table? No, because that is not what the tables are defined to contain and we
1703 won't know the difference between the old bad tables and the new good tables.
1704 At best we could make our own renamed sections that contain all of the data we
1707 These tables are also insufficient for what a debugger like LLDB needs. LLDB
1708 uses clang for its expression parsing where LLDB acts as a PCH. LLDB is then
1709 often asked to look for type "``foo``" or namespace "``bar``", or list items in
1710 namespace "``baz``". Namespaces are not included in the pubnames or pubtypes
1711 tables. Since clang asks a lot of questions when it is parsing an expression,
1712 we need to be very fast when looking up names, as it happens a lot. Having new
1713 accelerator tables that are optimized for very quick lookups will benefit this
1714 type of debugging experience greatly.
1716 We would like to generate name lookup tables that can be mapped into memory
1717 from disk, and used as is, with little or no up-front parsing. We would also
1718 be able to control the exact content of these different tables so they contain
1719 exactly what we need. The Name Accelerator Tables were designed to fix these
1720 issues. In order to solve these issues we need to:
1722 * Have a format that can be mapped into memory from disk and used as is
1723 * Lookups should be very fast
1724 * Extensible table format so these tables can be made by many producers
1725 * Contain all of the names needed for typical lookups out of the box
1726 * Strict rules for the contents of tables
1728 Table size is important and the accelerator table format should allow the reuse
1729 of strings from common string tables so the strings for the names are not
1730 duplicated. We also want to make sure the table is ready to be used as-is by
1731 simply mapping the table into memory with minimal header parsing.
1733 The name lookups need to be fast and optimized for the kinds of lookups that
1734 debuggers tend to do. Optimally we would like to touch as few parts of the
1735 mapped table as possible when doing a name lookup and be able to quickly find
1736 the name entry we are looking for, or discover there are no matches. In the
1737 case of debuggers we optimized for lookups that fail most of the time.
1739 Each table that is defined should have strict rules on exactly what is in the
1740 accelerator tables and documented so clients can rely on the content.
1745 Standard Hash Tables
1746 """"""""""""""""""""
1748 Typical hash tables have a header, buckets, and each bucket points to the
1751 .. code-block:: none
1761 The BUCKETS are an array of offsets to DATA for each hash:
1763 .. code-block:: none
1766 | 0x00001000 | BUCKETS[0]
1767 | 0x00002000 | BUCKETS[1]
1768 | 0x00002200 | BUCKETS[2]
1769 | 0x000034f0 | BUCKETS[3]
1771 | 0xXXXXXXXX | BUCKETS[n_buckets]
1774 So for ``bucket[3]`` in the example above, we have an offset into the table
1775 0x000034f0 which points to a chain of entries for the bucket. Each bucket must
1776 contain a next pointer, full 32 bit hash value, the string itself, and the data
1777 for the current string value.
1779 .. code-block:: none
1782 0x000034f0: | 0x00003500 | next pointer
1783 | 0x12345678 | 32 bit hash
1784 | "erase" | string value
1785 | data[n] | HashData for this bucket
1787 0x00003500: | 0x00003550 | next pointer
1788 | 0x29273623 | 32 bit hash
1789 | "dump" | string value
1790 | data[n] | HashData for this bucket
1792 0x00003550: | 0x00000000 | next pointer
1793 | 0x82638293 | 32 bit hash
1794 | "main" | string value
1795 | data[n] | HashData for this bucket
1798 The problem with this layout for debuggers is that we need to optimize for the
1799 negative lookup case where the symbol we're searching for is not present. So
1800 if we were to lookup "``printf``" in the table above, we would make a 32 hash
1801 for "``printf``", it might match ``bucket[3]``. We would need to go to the
1802 offset 0x000034f0 and start looking to see if our 32 bit hash matches. To do
1803 so, we need to read the next pointer, then read the hash, compare it, and skip
1804 to the next bucket. Each time we are skipping many bytes in memory and
1805 touching new cache pages just to do the compare on the full 32 bit hash. All
1806 of these accesses then tell us that we didn't have a match.
1811 To solve the issues mentioned above we have structured the hash tables a bit
1812 differently: a header, buckets, an array of all unique 32 bit hash values,
1813 followed by an array of hash value data offsets, one for each hash value, then
1814 the data for all hash values:
1816 .. code-block:: none
1830 The ``BUCKETS`` in the name tables are an index into the ``HASHES`` array. By
1831 making all of the full 32 bit hash values contiguous in memory, we allow
1832 ourselves to efficiently check for a match while touching as little memory as
1833 possible. Most often checking the 32 bit hash values is as far as the lookup
1834 goes. If it does match, it usually is a match with no collisions. So for a
1835 table with "``n_buckets``" buckets, and "``n_hashes``" unique 32 bit hash
1836 values, we can clarify the contents of the ``BUCKETS``, ``HASHES`` and
1839 .. code-block:: none
1841 .-------------------------.
1842 | HEADER.magic | uint32_t
1843 | HEADER.version | uint16_t
1844 | HEADER.hash_function | uint16_t
1845 | HEADER.bucket_count | uint32_t
1846 | HEADER.hashes_count | uint32_t
1847 | HEADER.header_data_len | uint32_t
1848 | HEADER_DATA | HeaderData
1849 |-------------------------|
1850 | BUCKETS | uint32_t[n_buckets] // 32 bit hash indexes
1851 |-------------------------|
1852 | HASHES | uint32_t[n_hashes] // 32 bit hash values
1853 |-------------------------|
1854 | OFFSETS | uint32_t[n_hashes] // 32 bit offsets to hash value data
1855 |-------------------------|
1857 `-------------------------'
1859 So taking the exact same data from the standard hash example above we end up
1862 .. code-block:: none
1872 | ... | BUCKETS[n_buckets]
1874 | 0x........ | HASHES[0]
1875 | 0x........ | HASHES[1]
1876 | 0x........ | HASHES[2]
1877 | 0x........ | HASHES[3]
1878 | 0x........ | HASHES[4]
1879 | 0x........ | HASHES[5]
1880 | 0x12345678 | HASHES[6] hash for BUCKETS[3]
1881 | 0x29273623 | HASHES[7] hash for BUCKETS[3]
1882 | 0x82638293 | HASHES[8] hash for BUCKETS[3]
1883 | 0x........ | HASHES[9]
1884 | 0x........ | HASHES[10]
1885 | 0x........ | HASHES[11]
1886 | 0x........ | HASHES[12]
1887 | 0x........ | HASHES[13]
1888 | 0x........ | HASHES[n_hashes]
1890 | 0x........ | OFFSETS[0]
1891 | 0x........ | OFFSETS[1]
1892 | 0x........ | OFFSETS[2]
1893 | 0x........ | OFFSETS[3]
1894 | 0x........ | OFFSETS[4]
1895 | 0x........ | OFFSETS[5]
1896 | 0x000034f0 | OFFSETS[6] offset for BUCKETS[3]
1897 | 0x00003500 | OFFSETS[7] offset for BUCKETS[3]
1898 | 0x00003550 | OFFSETS[8] offset for BUCKETS[3]
1899 | 0x........ | OFFSETS[9]
1900 | 0x........ | OFFSETS[10]
1901 | 0x........ | OFFSETS[11]
1902 | 0x........ | OFFSETS[12]
1903 | 0x........ | OFFSETS[13]
1904 | 0x........ | OFFSETS[n_hashes]
1912 0x000034f0: | 0x00001203 | .debug_str ("erase")
1913 | 0x00000004 | A 32 bit array count - number of HashData with name "erase"
1914 | 0x........ | HashData[0]
1915 | 0x........ | HashData[1]
1916 | 0x........ | HashData[2]
1917 | 0x........ | HashData[3]
1918 | 0x00000000 | String offset into .debug_str (terminate data for hash)
1920 0x00003500: | 0x00001203 | String offset into .debug_str ("collision")
1921 | 0x00000002 | A 32 bit array count - number of HashData with name "collision"
1922 | 0x........ | HashData[0]
1923 | 0x........ | HashData[1]
1924 | 0x00001203 | String offset into .debug_str ("dump")
1925 | 0x00000003 | A 32 bit array count - number of HashData with name "dump"
1926 | 0x........ | HashData[0]
1927 | 0x........ | HashData[1]
1928 | 0x........ | HashData[2]
1929 | 0x00000000 | String offset into .debug_str (terminate data for hash)
1931 0x00003550: | 0x00001203 | String offset into .debug_str ("main")
1932 | 0x00000009 | A 32 bit array count - number of HashData with name "main"
1933 | 0x........ | HashData[0]
1934 | 0x........ | HashData[1]
1935 | 0x........ | HashData[2]
1936 | 0x........ | HashData[3]
1937 | 0x........ | HashData[4]
1938 | 0x........ | HashData[5]
1939 | 0x........ | HashData[6]
1940 | 0x........ | HashData[7]
1941 | 0x........ | HashData[8]
1942 | 0x00000000 | String offset into .debug_str (terminate data for hash)
1945 So we still have all of the same data, we just organize it more efficiently for
1946 debugger lookup. If we repeat the same "``printf``" lookup from above, we
1947 would hash "``printf``" and find it matches ``BUCKETS[3]`` by taking the 32 bit
1948 hash value and modulo it by ``n_buckets``. ``BUCKETS[3]`` contains "6" which
1949 is the index into the ``HASHES`` table. We would then compare any consecutive
1950 32 bit hashes values in the ``HASHES`` array as long as the hashes would be in
1951 ``BUCKETS[3]``. We do this by verifying that each subsequent hash value modulo
1952 ``n_buckets`` is still 3. In the case of a failed lookup we would access the
1953 memory for ``BUCKETS[3]``, and then compare a few consecutive 32 bit hashes
1954 before we know that we have no match. We don't end up marching through
1955 multiple words of memory and we really keep the number of processor data cache
1956 lines being accessed as small as possible.
1958 The string hash that is used for these lookup tables is the Daniel J.
1959 Bernstein hash which is also used in the ELF ``GNU_HASH`` sections. It is a
1960 very good hash for all kinds of names in programs with very few hash
1963 Empty buckets are designated by using an invalid hash index of ``UINT32_MAX``.
1968 These name hash tables are designed to be generic where specializations of the
1969 table get to define additional data that goes into the header ("``HeaderData``"),
1970 how the string value is stored ("``KeyType``") and the content of the data for each
1976 The header has a fixed part, and the specialized part. The exact format of the
1983 uint32_t magic; // 'HASH' magic value to allow endian detection
1984 uint16_t version; // Version number
1985 uint16_t hash_function; // The hash function enumeration that was used
1986 uint32_t bucket_count; // The number of buckets in this hash table
1987 uint32_t hashes_count; // The total number of unique hash values and hash data offsets in this table
1988 uint32_t header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment
1989 // Specifically the length of the following HeaderData field - this does not
1990 // include the size of the preceding fields
1991 HeaderData header_data; // Implementation specific header data
1994 The header starts with a 32 bit "``magic``" value which must be ``'HASH'``
1995 encoded as an ASCII integer. This allows the detection of the start of the
1996 hash table and also allows the table's byte order to be determined so the table
1997 can be correctly extracted. The "``magic``" value is followed by a 16 bit
1998 ``version`` number which allows the table to be revised and modified in the
1999 future. The current version number is 1. ``hash_function`` is a ``uint16_t``
2000 enumeration that specifies which hash function was used to produce this table.
2001 The current values for the hash function enumerations include:
2005 enum HashFunctionType
2007 eHashFunctionDJB = 0u, // Daniel J Bernstein hash function
2010 ``bucket_count`` is a 32 bit unsigned integer that represents how many buckets
2011 are in the ``BUCKETS`` array. ``hashes_count`` is the number of unique 32 bit
2012 hash values that are in the ``HASHES`` array, and is the same number of offsets
2013 are contained in the ``OFFSETS`` array. ``header_data_len`` specifies the size
2014 in bytes of the ``HeaderData`` that is filled in by specialized versions of
2020 The header is followed by the buckets, hashes, offsets, and hash value data.
2026 uint32_t buckets[Header.bucket_count]; // An array of hash indexes into the "hashes[]" array below
2027 uint32_t hashes [Header.hashes_count]; // Every unique 32 bit hash for the entire table is in this table
2028 uint32_t offsets[Header.hashes_count]; // An offset that corresponds to each item in the "hashes[]" array above
2031 ``buckets`` is an array of 32 bit indexes into the ``hashes`` array. The
2032 ``hashes`` array contains all of the 32 bit hash values for all names in the
2033 hash table. Each hash in the ``hashes`` table has an offset in the ``offsets``
2034 array that points to the data for the hash value.
2036 This table setup makes it very easy to repurpose these tables to contain
2037 different data, while keeping the lookup mechanism the same for all tables.
2038 This layout also makes it possible to save the table to disk and map it in
2039 later and do very efficient name lookups with little or no parsing.
2041 DWARF lookup tables can be implemented in a variety of ways and can store a lot
2042 of information for each name. We want to make the DWARF tables extensible and
2043 able to store the data efficiently so we have used some of the DWARF features
2044 that enable efficient data storage to define exactly what kind of data we store
2047 The ``HeaderData`` contains a definition of the contents of each HashData chunk.
2048 We might want to store an offset to all of the debug information entries (DIEs)
2049 for each name. To keep things extensible, we create a list of items, or
2050 Atoms, that are contained in the data for each name. First comes the type of
2051 the data in each atom:
2058 eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
2059 eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that contains the item in question
2060 eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
2061 eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
2062 eAtomTypeTypeFlags = 5u, // Flags from enum TypeFlags
2065 The enumeration values and their meanings are:
2067 .. code-block:: none
2069 eAtomTypeNULL - a termination atom that specifies the end of the atom list
2070 eAtomTypeDIEOffset - an offset into the .debug_info section for the DWARF DIE for this name
2071 eAtomTypeCUOffset - an offset into the .debug_info section for the CU that contains the DIE
2072 eAtomTypeDIETag - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is
2073 eAtomTypeNameFlags - Flags for functions and global variables (isFunction, isInlined, isExternal...)
2074 eAtomTypeTypeFlags - Flags for types (isCXXClass, isObjCClass, ...)
2076 Then we allow each atom type to define the atom type and how the data for each
2077 atom type data is encoded:
2083 uint16_t type; // AtomType enum value
2084 uint16_t form; // DWARF DW_FORM_XXX defines
2087 The ``form`` type above is from the DWARF specification and defines the exact
2088 encoding of the data for the Atom type. See the DWARF specification for the
2089 ``DW_FORM_`` definitions.
2095 uint32_t die_offset_base;
2096 uint32_t atom_count;
2097 Atoms atoms[atom_count0];
2100 ``HeaderData`` defines the base DIE offset that should be added to any atoms
2101 that are encoded using the ``DW_FORM_ref1``, ``DW_FORM_ref2``,
2102 ``DW_FORM_ref4``, ``DW_FORM_ref8`` or ``DW_FORM_ref_udata``. It also defines
2103 what is contained in each ``HashData`` object -- ``Atom.form`` tells us how large
2104 each field will be in the ``HashData`` and the ``Atom.type`` tells us how this data
2105 should be interpreted.
2107 For the current implementations of the "``.apple_names``" (all functions +
2108 globals), the "``.apple_types``" (names of all types that are defined), and
2109 the "``.apple_namespaces``" (all namespaces), we currently set the ``Atom``
2114 HeaderData.atom_count = 1;
2115 HeaderData.atoms[0].type = eAtomTypeDIEOffset;
2116 HeaderData.atoms[0].form = DW_FORM_data4;
2118 This defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is
2119 encoded as a 32 bit value (DW_FORM_data4). This allows a single name to have
2120 multiple matching DIEs in a single file, which could come up with an inlined
2121 function for instance. Future tables could include more information about the
2122 DIE such as flags indicating if the DIE is a function, method, block,
2125 The KeyType for the DWARF table is a 32 bit string table offset into the
2126 ".debug_str" table. The ".debug_str" is the string table for the DWARF which
2127 may already contain copies of all of the strings. This helps make sure, with
2128 help from the compiler, that we reuse the strings between all of the DWARF
2129 sections and keeps the hash table size down. Another benefit to having the
2130 compiler generate all strings as DW_FORM_strp in the debug info, is that
2131 DWARF parsing can be made much faster.
2133 After a lookup is made, we get an offset into the hash data. The hash data
2134 needs to be able to deal with 32 bit hash collisions, so the chunk of data
2135 at the offset in the hash data consists of a triple:
2140 uint32_t hash_data_count
2141 HashData[hash_data_count]
2143 If "str_offset" is zero, then the bucket contents are done. 99.9% of the
2144 hash data chunks contain a single item (no 32 bit hash collision):
2146 .. code-block:: none
2149 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
2150 | 0x00000004 | uint32_t HashData count
2151 | 0x........ | uint32_t HashData[0] DIE offset
2152 | 0x........ | uint32_t HashData[1] DIE offset
2153 | 0x........ | uint32_t HashData[2] DIE offset
2154 | 0x........ | uint32_t HashData[3] DIE offset
2155 | 0x00000000 | uint32_t KeyType (end of hash chain)
2158 If there are collisions, you will have multiple valid string offsets:
2160 .. code-block:: none
2163 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
2164 | 0x00000004 | uint32_t HashData count
2165 | 0x........ | uint32_t HashData[0] DIE offset
2166 | 0x........ | uint32_t HashData[1] DIE offset
2167 | 0x........ | uint32_t HashData[2] DIE offset
2168 | 0x........ | uint32_t HashData[3] DIE offset
2169 | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print")
2170 | 0x00000002 | uint32_t HashData count
2171 | 0x........ | uint32_t HashData[0] DIE offset
2172 | 0x........ | uint32_t HashData[1] DIE offset
2173 | 0x00000000 | uint32_t KeyType (end of hash chain)
2176 Current testing with real world C++ binaries has shown that there is around 1
2177 32 bit hash collision per 100,000 name entries.
2182 As we said, we want to strictly define exactly what is included in the
2183 different tables. For DWARF, we have 3 tables: "``.apple_names``",
2184 "``.apple_types``", and "``.apple_namespaces``".
2186 "``.apple_names``" sections should contain an entry for each DWARF DIE whose
2187 ``DW_TAG`` is a ``DW_TAG_label``, ``DW_TAG_inlined_subroutine``, or
2188 ``DW_TAG_subprogram`` that has address attributes: ``DW_AT_low_pc``,
2189 ``DW_AT_high_pc``, ``DW_AT_ranges`` or ``DW_AT_entry_pc``. It also contains
2190 ``DW_TAG_variable`` DIEs that have a ``DW_OP_addr`` in the location (global and
2191 static variables). All global and static variables should be included,
2192 including those scoped within functions and classes. For example using the
2204 Both of the static ``var`` variables would be included in the table. All
2205 functions should emit both their full names and their basenames. For C or C++,
2206 the full name is the mangled name (if available) which is usually in the
2207 ``DW_AT_MIPS_linkage_name`` attribute, and the ``DW_AT_name`` contains the
2208 function basename. If global or static variables have a mangled name in a
2209 ``DW_AT_MIPS_linkage_name`` attribute, this should be emitted along with the
2210 simple name found in the ``DW_AT_name`` attribute.
2212 "``.apple_types``" sections should contain an entry for each DWARF DIE whose
2217 * DW_TAG_enumeration_type
2218 * DW_TAG_pointer_type
2219 * DW_TAG_reference_type
2220 * DW_TAG_string_type
2221 * DW_TAG_structure_type
2222 * DW_TAG_subroutine_type
2225 * DW_TAG_ptr_to_member_type
2227 * DW_TAG_subrange_type
2233 * DW_TAG_packed_type
2234 * DW_TAG_volatile_type
2235 * DW_TAG_restrict_type
2236 * DW_TAG_interface_type
2237 * DW_TAG_unspecified_type
2238 * DW_TAG_shared_type
2240 Only entries with a ``DW_AT_name`` attribute are included, and the entry must
2241 not be a forward declaration (``DW_AT_declaration`` attribute with a non-zero
2242 value). For example, using the following code:
2252 We get a few type DIEs:
2254 .. code-block:: none
2256 0x00000067: TAG_base_type [5]
2257 AT_encoding( DW_ATE_signed )
2259 AT_byte_size( 0x04 )
2261 0x0000006e: TAG_pointer_type [6]
2262 AT_type( {0x00000067} ( int ) )
2263 AT_byte_size( 0x08 )
2265 The DW_TAG_pointer_type is not included because it does not have a ``DW_AT_name``.
2267 "``.apple_namespaces``" section should contain all ``DW_TAG_namespace`` DIEs.
2268 If we run into a namespace that has no name this is an anonymous namespace, and
2269 the name should be output as "``(anonymous namespace)``" (without the quotes).
2270 Why? This matches the output of the ``abi::cxa_demangle()`` that is in the
2271 standard C++ library that demangles mangled names.
2274 Language Extensions and File Format Changes
2275 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2277 Objective-C Extensions
2278 """"""""""""""""""""""
2280 "``.apple_objc``" section should contain all ``DW_TAG_subprogram`` DIEs for an
2281 Objective-C class. The name used in the hash table is the name of the
2282 Objective-C class itself. If the Objective-C class has a category, then an
2283 entry is made for both the class name without the category, and for the class
2284 name with the category. So if we have a DIE at offset 0x1234 with a name of
2285 method "``-[NSString(my_additions) stringWithSpecialString:]``", we would add
2286 an entry for "``NSString``" that points to DIE 0x1234, and an entry for
2287 "``NSString(my_additions)``" that points to 0x1234. This allows us to quickly
2288 track down all Objective-C methods for an Objective-C class when doing
2289 expressions. It is needed because of the dynamic nature of Objective-C where
2290 anyone can add methods to a class. The DWARF for Objective-C methods is also
2291 emitted differently from C++ classes where the methods are not usually
2292 contained in the class definition, they are scattered about across one or more
2293 compile units. Categories can also be defined in different shared libraries.
2294 So we need to be able to quickly find all of the methods and class functions
2295 given the Objective-C class name, or quickly find all methods and class
2296 functions for a class + category name. This table does not contain any
2297 selector names, it just maps Objective-C class names (or class names +
2298 category) to all of the methods and class functions. The selectors are added
2299 as function basenames in the "``.debug_names``" section.
2301 In the "``.apple_names``" section for Objective-C functions, the full name is
2302 the entire function name with the brackets ("``-[NSString
2303 stringWithCString:]``") and the basename is the selector only
2304 ("``stringWithCString:``").
2309 The sections names for the apple hash tables are for non mach-o files. For
2310 mach-o files, the sections should be contained in the ``__DWARF`` segment with
2313 * "``.apple_names``" -> "``__apple_names``"
2314 * "``.apple_types``" -> "``__apple_types``"
2315 * "``.apple_namespaces``" -> "``__apple_namespac``" (16 character limit)
2316 * "``.apple_objc``" -> "``__apple_objc``"