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