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