xref: /llvm-project/llvm/docs/SourceLevelDebugging.rst (revision 01e5d4609bed5db52890cb66a91c65e9bb6a122f)
1================================
2Source Level Debugging with LLVM
3================================
4
5.. contents::
6   :local:
7
8Introduction
9============
10
11This document is the central repository for all information pertaining to debug
12information in LLVM.  It describes the :ref:`actual format that the LLVM debug
13information takes <format>`, which is useful for those interested in creating
14front-ends or dealing directly with the information.  Further, this document
15provides specific examples of what debug information for C/C++ looks like.
16
17Philosophy behind LLVM debugging information
18--------------------------------------------
19
20The idea of the LLVM debugging information is to capture how the important
21pieces of the source-language's Abstract Syntax Tree map onto LLVM code.
22Several design aspects have shaped the solution that appears here.  The
23important ones are:
24
25* Debugging information should have very little impact on the rest of the
26  compiler.  No transformations, analyses, or code generators should need to
27  be modified because of debugging information.
28
29* LLVM optimizations should interact in :ref:`well-defined and easily described
30  ways <intro_debugopt>` with the debugging information.
31
32* Because LLVM is designed to support arbitrary programming languages,
33  LLVM-to-LLVM tools should not need to know anything about the semantics of
34  the source-level-language.
35
36* Source-level languages are often **widely** different from one another.
37  LLVM should not put any restrictions of the flavor of the source-language,
38  and the debugging information should work with any language.
39
40* With code generator support, it should be possible to use an LLVM compiler
41  to compile a program to native machine code and standard debugging
42  formats.  This allows compatibility with traditional machine-code level
43  debuggers, like GDB or DBX.
44
45The approach used by the LLVM implementation is to use a small set of
46:ref:`intrinsic functions <format_common_intrinsics>` to define a mapping
47between LLVM program objects and the source-level objects.  The description of
48the source-level program is maintained in LLVM metadata in an
49:ref:`implementation-defined format <ccxx_frontend>` (the C/C++ front-end
50currently uses working draft 7 of the `DWARF 3 standard
51<http://www.eagercon.com/dwarf/dwarf3std.htm>`_).
52
53When a program is being debugged, a debugger interacts with the user and turns
54the stored debug information into source-language specific information.  As
55such, a debugger must be aware of the source-language, and is thus tied to a
56specific language or family of languages.
57
58Debug information consumers
59---------------------------
60
61The role of debug information is to provide meta information normally stripped
62away during the compilation process.  This meta information provides an LLVM
63user a relationship between generated code and the original program source
64code.
65
66Currently, there are two backend consumers of debug info: DwarfDebug and
67CodeViewDebug. DwarfDebug produces DWARF suitable for use with GDB, LLDB, and
68other DWARF-based debuggers. :ref:`CodeViewDebug <codeview>` produces CodeView,
69the Microsoft debug info format, which is usable with Microsoft debuggers such
70as Visual Studio and WinDBG. LLVM's debug information format is mostly derived
71from and inspired by DWARF, but it is feasible to translate into other target
72debug info formats such as STABS.
73
74It would also be reasonable to use debug information to feed profiling tools
75for analysis of generated code, or, tools for reconstructing the original
76source from generated code.
77
78.. _intro_debugopt:
79
80Debug information and optimizations
81-----------------------------------
82
83An extremely high priority of LLVM debugging information is to make it interact
84well with optimizations and analysis.  In particular, the LLVM debug
85information provides the following guarantees:
86
87* LLVM debug information **always provides information to accurately read
88  the source-level state of the program**, regardless of which LLVM
89  optimizations have been run. :doc:`HowToUpdateDebugInfo` specifies how debug
90  info should be updated in various kinds of code transformations to avoid
91  breaking this guarantee, and how to preserve as much useful debug info as
92  possible.  Note that some optimizations may impact the ability to modify the
93  current state of the program with a debugger, such as setting program
94  variables, or calling functions that have been deleted.
95
96* As desired, LLVM optimizations can be upgraded to be aware of debugging
97  information, allowing them to update the debugging information as they
98  perform aggressive optimizations.  This means that, with effort, the LLVM
99  optimizers could optimize debug code just as well as non-debug code.
100
101* LLVM debug information does not prevent optimizations from
102  happening (for example inlining, basic block reordering/merging/cleanup,
103  tail duplication, etc).
104
105* LLVM debug information is automatically optimized along with the rest of
106  the program, using existing facilities.  For example, duplicate
107  information is automatically merged by the linker, and unused information
108  is automatically removed.
109
110Basically, the debug information allows you to compile a program with
111"``-O0 -g``" and get full debug information, allowing you to arbitrarily modify
112the program as it executes from a debugger.  Compiling a program with
113"``-O3 -g``" gives you full debug information that is always available and
114accurate for reading (e.g., you get accurate stack traces despite tail call
115elimination and inlining), but you might lose the ability to modify the program
116and call functions which were optimized out of the program, or inlined away
117completely.
118
119The :doc:`LLVM test-suite <TestSuiteMakefileGuide>` provides a framework to
120test the optimizer's handling of debugging information.  It can be run like
121this:
122
123.. code-block:: bash
124
125  % cd llvm/projects/test-suite/MultiSource/Benchmarks  # or some other level
126  % make TEST=dbgopt
127
128This will test impact of debugging information on optimization passes.  If
129debugging information influences optimization passes then it will be reported
130as a failure.  See :doc:`TestingGuide` for more information on LLVM test
131infrastructure and how to run various tests.
132
133.. _format:
134
135Debugging information format
136============================
137
138LLVM debugging information has been carefully designed to make it possible for
139the optimizer to optimize the program and debugging information without
140necessarily having to know anything about debugging information.  In
141particular, the use of metadata avoids duplicated debugging information from
142the beginning, and the global dead code elimination pass automatically deletes
143debugging information for a function if it decides to delete the function.
144
145To do this, most of the debugging information (descriptors for types,
146variables, functions, source files, etc) is inserted by the language front-end
147in the form of LLVM metadata.
148
149Debug information is designed to be agnostic about the target debugger and
150debugging information representation (e.g. DWARF/Stabs/etc).  It uses a generic
151pass to decode the information that represents variables, types, functions,
152namespaces, etc: this allows for arbitrary source-language semantics and
153type-systems to be used, as long as there is a module written for the target
154debugger to interpret the information.
155
156To provide basic functionality, the LLVM debugger does have to make some
157assumptions about the source-level language being debugged, though it keeps
158these to a minimum.  The only common features that the LLVM debugger assumes
159exist are `source files <LangRef.html#difile>`_, and `program objects
160<LangRef.html#diglobalvariable>`_.  These abstract objects are used by a
161debugger to form stack traces, show information about local variables, etc.
162
163This section of the documentation first describes the representation aspects
164common to any source-language.  :ref:`ccxx_frontend` describes the data layout
165conventions used by the C and C++ front-ends.
166
167Debug information descriptors are `specialized metadata nodes
168<LangRef.html#specialized-metadata>`_, first-class subclasses of ``Metadata``.
169
170There are two models for defining the values of source variables at different
171states of the program and tracking these values through optimization and code
172generation: :ref:`intrinsic function calls <format_common_intrinsics>`, the
173current default, and :ref:`debug records <debug_records>`, which are a new
174non-instruction-based model
175(for an explanation of how this works and why it is desirable, see the
176`RemoveDIs <RemoveDIsDebugInfo.html>`_ document). Each module must use one or
177the other; they may never be mixed within an IR module. To enable writing debug
178records instead of intrinsic calls, use the flag
179``--write-experimental-debuginfo``.
180
181.. _format_common_intrinsics:
182
183Debugger intrinsic functions
184----------------------------
185
186LLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to
187track source local variables through optimization and code generation.
188
189``llvm.dbg.declare``
190^^^^^^^^^^^^^^^^^^^^
191
192.. code-block:: llvm
193
194  void @llvm.dbg.declare(metadata, metadata, metadata)
195
196This intrinsic provides information about a local element (e.g., variable).
197The first argument is metadata holding the address of variable, typically a
198static alloca in the function entry block.  The second argument is a
199`local variable <LangRef.html#dilocalvariable>`_ containing a description of
200the variable.  The third argument is a `complex expression
201<LangRef.html#diexpression>`_.  An `llvm.dbg.declare` intrinsic describes the
202*address* of a source variable.
203
204.. code-block:: text
205
206    %i.addr = alloca i32, align 4
207    call void @llvm.dbg.declare(metadata i32* %i.addr, metadata !1,
208                                metadata !DIExpression()), !dbg !2
209    !1 = !DILocalVariable(name: "i", ...) ; int i
210    !2 = !DILocation(...)
211    ...
212    %buffer = alloca [256 x i8], align 8
213    ; The address of i is buffer+64.
214    call void @llvm.dbg.declare(metadata [256 x i8]* %buffer, metadata !3,
215                               metadata !DIExpression(DW_OP_plus, 64)), !dbg !4
216    !3 = !DILocalVariable(name: "i", ...) ; int i
217    !4 = !DILocation(...)
218
219A frontend should generate exactly one call to ``llvm.dbg.declare`` at the point
220of declaration of a source variable. Optimization passes that fully promote the
221variable from memory to SSA values will replace this call with possibly multiple
222calls to `llvm.dbg.value`. Passes that delete stores are effectively partial
223promotion, and they will insert a mix of calls to ``llvm.dbg.value`` to track
224the source variable value when it is available. After optimization, there may be
225multiple calls to ``llvm.dbg.declare`` describing the program points where the
226variables lives in memory. All calls for the same concrete source variable must
227agree on the memory location.
228
229
230``llvm.dbg.value``
231^^^^^^^^^^^^^^^^^^
232
233.. code-block:: llvm
234
235  void @llvm.dbg.value(metadata, metadata, metadata)
236
237This intrinsic provides information when a user source variable is set to a new
238value.  The first argument is the new value (wrapped as metadata).  The second
239argument is a `local variable <LangRef.html#dilocalvariable>`_ containing a
240description of the variable.  The third argument is a `complex expression
241<LangRef.html#diexpression>`_.
242
243An `llvm.dbg.value` intrinsic describes the *value* of a source variable
244directly, not its address.  Note that the value operand of this intrinsic may
245be indirect (i.e, a pointer to the source variable), provided that interpreting
246the complex expression derives the direct value.
247
248``llvm.dbg.assign``
249^^^^^^^^^^^^^^^^^^^
250.. toctree::
251   :hidden:
252
253   AssignmentTracking
254
255.. code-block:: llvm
256
257  void @llvm.dbg.assign(Value *Value,
258                        DIExpression *ValueExpression,
259                        DILocalVariable *Variable,
260                        DIAssignID *ID,
261                        Value *Address,
262                        DIExpression *AddressExpression)
263
264This intrinsic marks the position in IR where a source assignment occurred. It
265encodes the value of the variable. It references the store, if any, that
266performs the assignment, and the destination address.
267
268The first three arguments are the same as for an ``llvm.dbg.value``. The fourth
269argument is a ``DIAssignID`` used to reference a store. The fifth is the
270destination of the store (wrapped as metadata), and the sixth is a `complex
271expression <LangRef.html#diexpression>`_ that modifies it.
272
273The formal LLVM-IR signature is:
274
275.. code-block:: llvm
276
277  void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
278
279
280See :doc:`AssignmentTracking` for more info.
281
282.. _debug_records:
283
284Debug Records
285----------------------------
286
287LLVM also has an alternative to intrinsic functions, debug records, which
288function similarly but are not instructions. The basic syntax for debug records
289is:
290
291.. code-block:: llvm
292
293    #dbg_<kind>([<arg>, ]* <DILocation>)
294  ; Using the intrinsic model, the above is equivalent to:
295  call void llvm.dbg.<kind>([metadata <arg>, ]*), !dbg <DILocation>
296
297A debug intrinsic function can be converted to a debug record with the
298following steps:
299
3001. Add an extra level of indentation.
3012. Replace everything prior to the intrinsic kind (declare/value/assign) with
302   ``#dbg_``.
3033. Remove the leading ``metadata`` from the intrinsic's arguments.
3044. Transfer the ``!dbg`` attachment to be an argument, dropping the leading
305   ``!dbg``.
306
307For each kind of intrinsic function, there is an equivalent debug record.
308
309``#dbg_declare``
310^^^^^^^^^^^^^^^^
311
312.. code-block:: llvm
313
314    #dbg_declare([Value|MDNode], DILocalVariable, DIExpression, DILocation)
315
316Equivalent to the ``llvm.dbg.declare`` intrinsic.
317
318``#dbg_value``
319^^^^^^^^^^^^^^
320
321.. code-block:: llvm
322
323    #dbg_value([Value|DIArgList|MDNode], DILocalVariable, DIExpression, DILocation)
324
325Equivalent to the ``llvm.dbg.value`` intrinsic.
326
327``#dbg_assign``
328^^^^^^^^^^^^^^^
329
330.. code-block:: llvm
331
332    #dbg_assign([Value|DIArgList|MDNode], DILocalVariable, DIExpression,
333                DIAssignID, [Value|MDNode], DIExpression, DILocation)
334
335Equivalent to the ``llvm.dbg.assign`` intrinsic.
336
337Object lifetimes and scoping
338============================
339
340In many languages, the local variables in functions can have their lifetimes or
341scopes limited to a subset of a function.  In the C family of languages, for
342example, variables are only live (readable and writable) within the source
343block that they are defined in.  In functional languages, values are only
344readable after they have been defined.  Though this is a very obvious concept,
345it is non-trivial to model in LLVM, because it has no notion of scoping in this
346sense, and does not want to be tied to a language's scoping rules.
347
348In order to handle this, the LLVM debug format uses the metadata attached to
349llvm instructions to encode line number and scoping information.  Consider the
350following C fragment, for example:
351
352.. code-block:: c
353
354  1.  void foo() {
355  2.    int X = 21;
356  3.    int Y = 22;
357  4.    {
358  5.      int Z = 23;
359  6.      Z = X;
360  7.    }
361  8.    X = Y;
362  9.  }
363
364Compiled to LLVM, this function would be represented like this:
365
366.. code-block:: text
367
368  ; Function Attrs: nounwind ssp uwtable
369  define void @foo() #0 !dbg !4 {
370  entry:
371    %X = alloca i32, align 4
372    %Y = alloca i32, align 4
373    %Z = alloca i32, align 4
374    call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
375    store i32 21, i32* %X, align 4, !dbg !14
376    call void @llvm.dbg.declare(metadata i32* %Y, metadata !15, metadata !13), !dbg !16
377    store i32 22, i32* %Y, align 4, !dbg !16
378    call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
379    store i32 23, i32* %Z, align 4, !dbg !19
380    %0 = load i32, i32* %X, align 4, !dbg !20
381    store i32 %0, i32* %Z, align 4, !dbg !21
382    %1 = load i32, i32* %Y, align 4, !dbg !22
383    store i32 %1, i32* %X, align 4, !dbg !23
384    ret void, !dbg !24
385  }
386
387  ; Function Attrs: nounwind readnone
388  declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
389
390  attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "frame-pointer"="all" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
391  attributes #1 = { nounwind readnone }
392
393  !llvm.dbg.cu = !{!0}
394  !llvm.module.flags = !{!7, !8, !9}
395  !llvm.ident = !{!10}
396
397  !0 = !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
398  !1 = !DIFile(filename: "/dev/stdin", directory: "/Users/dexonsmith/data/llvm/debug-info")
399  !2 = !{}
400  !3 = !{!4}
401  !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, retainedNodes: !2)
402  !5 = !DISubroutineType(types: !6)
403  !6 = !{null}
404  !7 = !{i32 2, !"Dwarf Version", i32 2}
405  !8 = !{i32 2, !"Debug Info Version", i32 3}
406  !9 = !{i32 1, !"PIC Level", i32 2}
407  !10 = !{!"clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)"}
408  !11 = !DILocalVariable(name: "X", scope: !4, file: !1, line: 2, type: !12)
409  !12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
410  !13 = !DIExpression()
411  !14 = !DILocation(line: 2, column: 9, scope: !4)
412  !15 = !DILocalVariable(name: "Y", scope: !4, file: !1, line: 3, type: !12)
413  !16 = !DILocation(line: 3, column: 9, scope: !4)
414  !17 = !DILocalVariable(name: "Z", scope: !18, file: !1, line: 5, type: !12)
415  !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
416  !19 = !DILocation(line: 5, column: 11, scope: !18)
417  !20 = !DILocation(line: 6, column: 11, scope: !18)
418  !21 = !DILocation(line: 6, column: 9, scope: !18)
419  !22 = !DILocation(line: 8, column: 9, scope: !4)
420  !23 = !DILocation(line: 8, column: 7, scope: !4)
421  !24 = !DILocation(line: 9, column: 3, scope: !4)
422
423
424This example illustrates a few important details about LLVM debugging
425information.  In particular, it shows how the ``llvm.dbg.declare`` intrinsic and
426location information, which are attached to an instruction, are applied
427together to allow a debugger to analyze the relationship between statements,
428variable definitions, and the code used to implement the function.
429
430.. code-block:: llvm
431
432  call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
433    ; [debug line = 2:7] [debug variable = X]
434
435The first intrinsic ``%llvm.dbg.declare`` encodes debugging information for the
436variable ``X``.  The metadata ``!dbg !14`` attached to the intrinsic provides
437scope information for the variable ``X``.
438
439.. code-block:: text
440
441  !14 = !DILocation(line: 2, column: 9, scope: !4)
442  !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
443                              isLocal: false, isDefinition: true, scopeLine: 1,
444                              isOptimized: false, retainedNodes: !2)
445
446Here ``!14`` is metadata providing `location information
447<LangRef.html#dilocation>`_.  In this example, scope is encoded by ``!4``, a
448`subprogram descriptor <LangRef.html#disubprogram>`_.  This way the location
449information attached to the intrinsics indicates that the variable ``X`` is
450declared at line number 2 at a function level scope in function ``foo``.
451
452Now lets take another example.
453
454.. code-block:: llvm
455
456  call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
457    ; [debug line = 5:9] [debug variable = Z]
458
459The third intrinsic ``%llvm.dbg.declare`` encodes debugging information for
460variable ``Z``.  The metadata ``!dbg !19`` attached to the intrinsic provides
461scope information for the variable ``Z``.
462
463.. code-block:: text
464
465  !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
466  !19 = !DILocation(line: 5, column: 11, scope: !18)
467
468Here ``!19`` indicates that ``Z`` is declared at line number 5 and column
469number 11 inside of lexical scope ``!18``.  The lexical scope itself resides
470inside of subprogram ``!4`` described above.
471
472The scope information attached with each instruction provides a straightforward
473way to find instructions covered by a scope.
474
475Object lifetime in optimized code
476=================================
477
478In the example above, every variable assignment uniquely corresponds to a
479memory store to the variable's position on the stack. However in heavily
480optimized code LLVM promotes most variables into SSA values, which can
481eventually be placed in physical registers or memory locations. To track SSA
482values through compilation, when objects are promoted to SSA values an
483``llvm.dbg.value`` intrinsic is created for each assignment, recording the
484variable's new location. Compared with the ``llvm.dbg.declare`` intrinsic:
485
486* A dbg.value terminates the effect of any preceding dbg.values for (any
487  overlapping fragments of) the specified variable.
488* The dbg.value's position in the IR defines where in the instruction stream
489  the variable's value changes.
490* Operands can be constants, indicating the variable is assigned a
491  constant value.
492
493Care must be taken to update ``llvm.dbg.value`` intrinsics when optimization
494passes alter or move instructions and blocks -- the developer could observe such
495changes reflected in the value of variables when debugging the program. For any
496execution of the optimized program, the set of variable values presented to the
497developer by the debugger should not show a state that would never have existed
498in the execution of the unoptimized program, given the same input. Doing so
499risks misleading the developer by reporting a state that does not exist,
500damaging their understanding of the optimized program and undermining their
501trust in the debugger.
502
503Sometimes perfectly preserving variable locations is not possible, often when a
504redundant calculation is optimized out. In such cases, a ``llvm.dbg.value``
505with operand ``poison`` should be used, to terminate earlier variable locations
506and let the debugger present ``optimized out`` to the developer. Withholding
507these potentially stale variable values from the developer diminishes the
508amount of available debug information, but increases the reliability of the
509remaining information.
510
511To illustrate some potential issues, consider the following example:
512
513.. code-block:: llvm
514
515  define i32 @foo(i32 %bar, i1 %cond) {
516  entry:
517    call @llvm.dbg.value(metadata i32 0, metadata !1, metadata !2)
518    br i1 %cond, label %truebr, label %falsebr
519  truebr:
520    %tval = add i32 %bar, 1
521    call @llvm.dbg.value(metadata i32 %tval, metadata !1, metadata !2)
522    %g1 = call i32 @gazonk()
523    br label %exit
524  falsebr:
525    %fval = add i32 %bar, 2
526    call @llvm.dbg.value(metadata i32 %fval, metadata !1, metadata !2)
527    %g2 = call i32 @gazonk()
528    br label %exit
529  exit:
530    %merge = phi [ %tval, %truebr ], [ %fval, %falsebr ]
531    %g = phi [ %g1, %truebr ], [ %g2, %falsebr ]
532    call @llvm.dbg.value(metadata i32 %merge, metadata !1, metadata !2)
533    call @llvm.dbg.value(metadata i32 %g, metadata !3, metadata !2)
534    %plusten = add i32 %merge, 10
535    %toret = add i32 %plusten, %g
536    call @llvm.dbg.value(metadata i32 %toret, metadata !1, metadata !2)
537    ret i32 %toret
538  }
539
540Containing two source-level variables in ``!1`` and ``!3``. The function could,
541perhaps, be optimized into the following code:
542
543.. code-block:: llvm
544
545  define i32 @foo(i32 %bar, i1 %cond) {
546  entry:
547    %g = call i32 @gazonk()
548    %addoper = select i1 %cond, i32 11, i32 12
549    %plusten = add i32 %bar, %addoper
550    %toret = add i32 %plusten, %g
551    ret i32 %toret
552  }
553
554What ``llvm.dbg.value`` intrinsics should be placed to represent the original variable
555locations in this code? Unfortunately the second, third and fourth
556dbg.values for ``!1`` in the source function have had their operands
557(%tval, %fval, %merge) optimized out. Assuming we cannot recover them, we
558might consider this placement of dbg.values:
559
560.. code-block:: llvm
561
562  define i32 @foo(i32 %bar, i1 %cond) {
563  entry:
564    call @llvm.dbg.value(metadata i32 0, metadata !1, metadata !2)
565    %g = call i32 @gazonk()
566    call @llvm.dbg.value(metadata i32 %g, metadata !3, metadata !2)
567    %addoper = select i1 %cond, i32 11, i32 12
568    %plusten = add i32 %bar, %addoper
569    %toret = add i32 %plusten, %g
570    call @llvm.dbg.value(metadata i32 %toret, metadata !1, metadata !2)
571    ret i32 %toret
572  }
573
574However, this will cause ``!3`` to have the return value of ``@gazonk()`` at
575the same time as ``!1`` has the constant value zero -- a pair of assignments
576that never occurred in the unoptimized program. To avoid this, we must terminate
577the range that ``!1`` has the constant value assignment by inserting a poison
578dbg.value before the dbg.value for ``!3``:
579
580.. code-block:: llvm
581
582  define i32 @foo(i32 %bar, i1 %cond) {
583  entry:
584    call @llvm.dbg.value(metadata i32 0, metadata !1, metadata !2)
585    %g = call i32 @gazonk()
586    call @llvm.dbg.value(metadata i32 poison, metadata !1, metadata !2)
587    call @llvm.dbg.value(metadata i32 %g, metadata !3, metadata !2)
588    %addoper = select i1 %cond, i32 11, i32 12
589    %plusten = add i32 %bar, %addoper
590    %toret = add i32 %plusten, %g
591    call @llvm.dbg.value(metadata i32 %toret, metadata !1, metadata !2)
592    ret i32 %toret
593  }
594
595There are a few other dbg.value configurations that mean it terminates
596dominating location definitions without adding a new location. The complete
597list is:
598
599* Any location operand is ``poison`` (or ``undef``).
600* Any location operand is an empty metadata tuple (``!{}``) (which cannot
601  occur in a ``!DIArgList``).
602* There are no location operands (empty ``DIArgList``) and the ``DIExpression``
603  is empty.
604
605This class of dbg.value that kills variable locations is called a "kill
606dbg.value" or "kill location", and for legacy reasons the term "undef
607dbg.value" may be used in existing code. The ``DbgVariableIntrinsic`` methods
608``isKillLocation`` and ``setKillLocation`` should be used where possible rather
609than inspecting location operands directly to check or set whether a dbg.value
610is a kill location.
611
612In general, if any dbg.value has its operand optimized out and cannot be
613recovered, then a kill dbg.value is necessary to terminate earlier variable
614locations. Additional kill dbg.values may be necessary when the debugger can
615observe re-ordering of assignments.
616
617How variable location metadata is transformed during CodeGen
618============================================================
619
620LLVM preserves debug information throughout mid-level and backend passes,
621ultimately producing a mapping between source-level information and
622instruction ranges. This
623is relatively straightforwards for line number information, as mapping
624instructions to line numbers is a simple association. For variable locations
625however the story is more complex. As each ``llvm.dbg.value`` intrinsic
626represents a source-level assignment of a value to a source variable, the
627variable location intrinsics effectively embed a small imperative program
628within the LLVM IR. By the end of CodeGen, this becomes a mapping from each
629variable to their machine locations over ranges of instructions.
630From IR to object emission, the major transformations which affect variable
631location fidelity are:
632
6331. Instruction Selection
6342. Register allocation
6353. Block layout
636
637each of which are discussed below. In addition, instruction scheduling can
638significantly change the ordering of the program, and occurs in a number of
639different passes.
640
641Some variable locations are not transformed during CodeGen. Stack locations
642specified by ``llvm.dbg.declare`` are valid and unchanging for the entire
643duration of the function, and are recorded in a simple MachineFunction table.
644Location changes in the prologue and epilogue of a function are also ignored:
645frame setup and destruction may take several instructions, require a
646disproportionate amount of debugging information in the output binary to
647describe, and should be stepped over by debuggers anyway.
648
649Variable locations in Instruction Selection and MIR
650---------------------------------------------------
651
652Instruction selection creates a MIR function from an IR function, and just as
653it transforms ``intermediate`` instructions into machine instructions, so must
654``intermediate`` variable locations become machine variable locations.
655Within IR, variable locations are always identified by a Value, but in MIR
656there can be different types of variable locations. In addition, some IR
657locations become unavailable, for example if the operation of multiple IR
658instructions are combined into one machine instruction (such as
659multiply-and-accumulate) then intermediate Values are lost. To track variable
660locations through instruction selection, they are first separated into
661locations that do not depend on code generation (constants, stack locations,
662allocated virtual registers) and those that do. For those that do, debug
663metadata is attached to SDNodes in SelectionDAGs. After instruction selection
664has occurred and a MIR function is created, if the SDNode associated with debug
665metadata is allocated a virtual register, that virtual register is used as the
666variable location. If the SDNode is folded into a machine instruction or
667otherwise transformed into a non-register, the variable location becomes
668unavailable.
669
670Locations that are unavailable are treated as if they have been optimized out:
671in IR the location would be assigned ``undef`` by a debug intrinsic, and in MIR
672the equivalent location is used.
673
674After MIR locations are assigned to each variable, machine pseudo-instructions
675corresponding to each ``llvm.dbg.value`` intrinsic are inserted. There are two
676forms of this type of instruction.
677
678The first form, ``DBG_VALUE``, appears thus:
679
680.. code-block:: text
681
682  DBG_VALUE %1, $noreg, !123, !DIExpression()
683
684And has the following operands:
685 * The first operand can record the variable location as a register,
686   a frame index, an immediate, or the base address register if the original
687   debug intrinsic referred to memory. ``$noreg`` indicates the variable
688   location is undefined, equivalent to an ``undef`` dbg.value operand.
689 * The type of the second operand indicates whether the variable location is
690   directly referred to by the DBG_VALUE, or whether it is indirect. The
691   ``$noreg`` register signifies the former, an immediate operand (0) the
692   latter.
693 * Operand 3 is the Variable field of the original debug intrinsic.
694 * Operand 4 is the Expression field of the original debug intrinsic.
695
696The second form, ``DBG_VALUE_LIST``, appears thus:
697
698.. code-block:: text
699
700  DBG_VALUE_LIST !123, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus), %1, %2
701
702And has the following operands:
703 * The first operand is the Variable field of the original debug intrinsic.
704 * The second operand is the Expression field of the original debug intrinsic.
705 * Any number of operands, from the 3rd onwards, record a sequence of variable
706   location operands, which may take any of the same values as the first
707   operand of the ``DBG_VALUE`` instruction above. These variable location
708   operands are inserted into the final DWARF Expression in positions indicated
709   by the DW_OP_LLVM_arg operator in the `DIExpression
710   <LangRef.html#diexpression>`_.
711
712The position at which the DBG_VALUEs are inserted should correspond to the
713positions of their matching ``llvm.dbg.value`` intrinsics in the IR block.  As
714with optimization, LLVM aims to preserve the order in which variable
715assignments occurred in the source program. However SelectionDAG performs some
716instruction scheduling, which can reorder assignments (discussed below).
717Function parameter locations are moved to the beginning of the function if
718they're not already, to ensure they're immediately available on function entry.
719
720To demonstrate variable locations during instruction selection, consider
721the following example:
722
723.. code-block:: llvm
724
725  define i32 @foo(i32* %addr) {
726  entry:
727    call void @llvm.dbg.value(metadata i32 0, metadata !3, metadata !DIExpression()), !dbg !5
728    br label %bb1, !dbg !5
729
730  bb1:                                              ; preds = %bb1, %entry
731    %bar.0 = phi i32 [ 0, %entry ], [ %add, %bb1 ]
732    call void @llvm.dbg.value(metadata i32 %bar.0, metadata !3, metadata !DIExpression()), !dbg !5
733    %addr1 = getelementptr i32, i32 *%addr, i32 1, !dbg !5
734    call void @llvm.dbg.value(metadata i32 *%addr1, metadata !3, metadata !DIExpression()), !dbg !5
735    %loaded1 = load i32, i32* %addr1, !dbg !5
736    %addr2 = getelementptr i32, i32 *%addr, i32 %bar.0, !dbg !5
737    call void @llvm.dbg.value(metadata i32 *%addr2, metadata !3, metadata !DIExpression()), !dbg !5
738    %loaded2 = load i32, i32* %addr2, !dbg !5
739    %add = add i32 %bar.0, 1, !dbg !5
740    call void @llvm.dbg.value(metadata i32 %add, metadata !3, metadata !DIExpression()), !dbg !5
741    %added = add i32 %loaded1, %loaded2
742    %cond = icmp ult i32 %added, %bar.0, !dbg !5
743    br i1 %cond, label %bb1, label %bb2, !dbg !5
744
745  bb2:                                              ; preds = %bb1
746    ret i32 0, !dbg !5
747  }
748
749If one compiles this IR with ``llc -o - -start-after=codegen-prepare -stop-after=expand-isel-pseudos -mtriple=x86_64--``, the following MIR is produced:
750
751.. code-block:: text
752
753  bb.0.entry:
754    successors: %bb.1(0x80000000)
755    liveins: $rdi
756
757    %2:gr64 = COPY $rdi
758    %3:gr32 = MOV32r0 implicit-def dead $eflags
759    DBG_VALUE 0, $noreg, !3, !DIExpression(), debug-location !5
760
761  bb.1.bb1:
762    successors: %bb.1(0x7c000000), %bb.2(0x04000000)
763
764    %0:gr32 = PHI %3, %bb.0, %1, %bb.1
765    DBG_VALUE %0, $noreg, !3, !DIExpression(), debug-location !5
766    DBG_VALUE %2, $noreg, !3, !DIExpression(DW_OP_plus_uconst, 4, DW_OP_stack_value), debug-location !5
767    %4:gr32 = MOV32rm %2, 1, $noreg, 4, $noreg, debug-location !5 :: (load 4 from %ir.addr1)
768    %5:gr64_nosp = MOVSX64rr32 %0, debug-location !5
769    DBG_VALUE $noreg, $noreg, !3, !DIExpression(), debug-location !5
770    %1:gr32 = INC32r %0, implicit-def dead $eflags, debug-location !5
771    DBG_VALUE %1, $noreg, !3, !DIExpression(), debug-location !5
772    %6:gr32 = ADD32rm %4, %2, 4, killed %5, 0, $noreg, implicit-def dead $eflags :: (load 4 from %ir.addr2)
773    %7:gr32 = SUB32rr %6, %0, implicit-def $eflags, debug-location !5
774    JB_1 %bb.1, implicit $eflags, debug-location !5
775    JMP_1 %bb.2, debug-location !5
776
777  bb.2.bb2:
778    %8:gr32 = MOV32r0 implicit-def dead $eflags
779    $eax = COPY %8, debug-location !5
780    RET 0, $eax, debug-location !5
781
782Observe first that there is a DBG_VALUE instruction for every ``llvm.dbg.value``
783intrinsic in the source IR, ensuring no source level assignments go missing.
784Then consider the different ways in which variable locations have been recorded:
785
786* For the first dbg.value an immediate operand is used to record a zero value.
787* The dbg.value of the PHI instruction leads to a DBG_VALUE of virtual register
788  ``%0``.
789* The first GEP has its effect folded into the first load instruction
790  (as a 4-byte offset), but the variable location is salvaged by folding
791  the GEPs effect into the DIExpression.
792* The second GEP is also folded into the corresponding load. However, it is
793  insufficiently simple to be salvaged, and is emitted as a ``$noreg``
794  DBG_VALUE, indicating that the variable takes on an undefined location.
795* The final dbg.value has its Value placed in virtual register ``%1``.
796
797Instruction Scheduling
798----------------------
799
800A number of passes can reschedule instructions, notably instruction selection
801and the pre-and-post RA machine schedulers. Instruction scheduling can
802significantly change the nature of the program -- in the (very unlikely) worst
803case the instruction sequence could be completely reversed. In such
804circumstances LLVM follows the principle applied to optimizations, that it is
805better for the debugger not to display any state than a misleading state.
806Thus, whenever instructions are advanced in order of execution, any
807corresponding DBG_VALUE is kept in its original position, and if an instruction
808is delayed then the variable is given an undefined location for the duration
809of the delay. To illustrate, consider this pseudo-MIR:
810
811.. code-block:: text
812
813  %1:gr32 = MOV32rm %0, 1, $noreg, 4, $noreg, debug-location !5 :: (load 4 from %ir.addr1)
814  DBG_VALUE %1, $noreg, !1, !2
815  %4:gr32 = ADD32rr %3, %2, implicit-def dead $eflags
816  DBG_VALUE %4, $noreg, !3, !4
817  %7:gr32 = SUB32rr %6, %5, implicit-def dead $eflags
818  DBG_VALUE %7, $noreg, !5, !6
819
820Imagine that the SUB32rr were moved forward to give us the following MIR:
821
822.. code-block:: text
823
824  %7:gr32 = SUB32rr %6, %5, implicit-def dead $eflags
825  %1:gr32 = MOV32rm %0, 1, $noreg, 4, $noreg, debug-location !5 :: (load 4 from %ir.addr1)
826  DBG_VALUE %1, $noreg, !1, !2
827  %4:gr32 = ADD32rr %3, %2, implicit-def dead $eflags
828  DBG_VALUE %4, $noreg, !3, !4
829  DBG_VALUE %7, $noreg, !5, !6
830
831In this circumstance LLVM would leave the MIR as shown above. Were we to move
832the DBG_VALUE of virtual register %7 upwards with the SUB32rr, we would re-order
833assignments and introduce a new state of the program. Whereas with the solution
834above, the debugger will see one fewer combination of variable values, because
835``!3`` and ``!5`` will change value at the same time. This is preferred over
836misrepresenting the original program.
837
838In comparison, if one sunk the MOV32rm, LLVM would produce the following:
839
840.. code-block:: text
841
842  DBG_VALUE $noreg, $noreg, !1, !2
843  %4:gr32 = ADD32rr %3, %2, implicit-def dead $eflags
844  DBG_VALUE %4, $noreg, !3, !4
845  %7:gr32 = SUB32rr %6, %5, implicit-def dead $eflags
846  DBG_VALUE %7, $noreg, !5, !6
847  %1:gr32 = MOV32rm %0, 1, $noreg, 4, $noreg, debug-location !5 :: (load 4 from %ir.addr1)
848  DBG_VALUE %1, $noreg, !1, !2
849
850Here, to avoid presenting a state in which the first assignment to ``!1``
851disappears, the DBG_VALUE at the top of the block assigns the variable the
852undefined location, until its value is available at the end of the block where
853an additional DBG_VALUE is added. Were any other DBG_VALUE for ``!1`` to occur
854in the instructions that the MOV32rm was sunk past, the DBG_VALUE for ``%1``
855would be dropped and the debugger would never observe it in the variable. This
856accurately reflects that the value is not available during the corresponding
857portion of the original program.
858
859Variable locations during Register Allocation
860---------------------------------------------
861
862To avoid debug instructions interfering with the register allocator, the
863LiveDebugVariables pass extracts variable locations from a MIR function and
864deletes the corresponding DBG_VALUE instructions. Some localized copy
865propagation is performed within blocks. After register allocation, the
866VirtRegRewriter pass re-inserts DBG_VALUE instructions in their original
867positions, translating virtual register references into their physical
868machine locations. To avoid encoding incorrect variable locations, in this
869pass any DBG_VALUE of a virtual register that is not live, is replaced by
870the undefined location. The LiveDebugVariables may insert redundant DBG_VALUEs
871because of virtual register rewriting. These will be subsequently removed by
872the RemoveRedundantDebugValues pass.
873
874LiveDebugValues expansion of variable locations
875-----------------------------------------------
876
877After all optimizations have run and shortly before emission, the
878LiveDebugValues pass runs to achieve two aims:
879
880* To propagate the location of variables through copies and register spills,
881* For every block, to record every valid variable location in that block.
882
883After this pass the DBG_VALUE instruction changes meaning: rather than
884corresponding to a source-level assignment where the variable may change value,
885it asserts the location of a variable in a block, and loses effect outside the
886block. Propagating variable locations through copies and spills is
887straightforwards: determining the variable location in every basic block
888requires the consideration of control flow. Consider the following IR, which
889presents several difficulties:
890
891.. code-block:: text
892
893  define dso_local i32 @foo(i1 %cond, i32 %input) !dbg !12 {
894  entry:
895    br i1 %cond, label %truebr, label %falsebr
896
897  bb1:
898    %value = phi i32 [ %value1, %truebr ], [ %value2, %falsebr ]
899    br label %exit, !dbg !26
900
901  truebr:
902    call void @llvm.dbg.value(metadata i32 %input, metadata !30, metadata !DIExpression()), !dbg !24
903    call void @llvm.dbg.value(metadata i32 1, metadata !23, metadata !DIExpression()), !dbg !24
904    %value1 = add i32 %input, 1
905    br label %bb1
906
907  falsebr:
908    call void @llvm.dbg.value(metadata i32 %input, metadata !30, metadata !DIExpression()), !dbg !24
909    call void @llvm.dbg.value(metadata i32 2, metadata !23, metadata !DIExpression()), !dbg !24
910    %value2 = add i32 %input, 2
911    br label %bb1
912
913  exit:
914    ret i32 %value, !dbg !30
915  }
916
917Here the difficulties are:
918
919* The control flow is roughly the opposite of basic block order
920* The value of the ``!23`` variable merges into ``%bb1``, but there is no PHI
921  node
922
923As mentioned above, the ``llvm.dbg.value`` intrinsics essentially form an
924imperative program embedded in the IR, with each intrinsic defining a variable
925location. This *could* be converted to an SSA form by mem2reg, in the same way
926that it uses use-def chains to identify control flow merges and insert phi
927nodes for IR Values. However, because debug variable locations are defined for
928every machine instruction, in effect every IR instruction uses every variable
929location, which would lead to a large number of debugging intrinsics being
930generated.
931
932Examining the example above, variable ``!30`` is assigned ``%input`` on both
933conditional paths through the function, while ``!23`` is assigned differing
934constant values on either path. Where control flow merges in ``%bb1`` we would
935want ``!30`` to keep its location (``%input``), but ``!23`` to become undefined
936as we cannot determine at runtime what value it should have in %bb1 without
937inserting a PHI node. mem2reg does not insert the PHI node to avoid changing
938codegen when debugging is enabled, and does not insert the other dbg.values
939to avoid adding very large numbers of intrinsics.
940
941Instead, LiveDebugValues determines variable locations when control
942flow merges. A dataflow analysis is used to propagate locations between blocks:
943when control flow merges, if a variable has the same location in all
944predecessors then that location is propagated into the successor. If the
945predecessor locations disagree, the location becomes undefined.
946
947Once LiveDebugValues has run, every block should have all valid variable
948locations described by DBG_VALUE instructions within the block. Very little
949effort is then required by supporting classes (such as
950DbgEntityHistoryCalculator) to build a map of each instruction to every
951valid variable location, without the need to consider control flow. From
952the example above, it is otherwise difficult to determine that the location
953of variable ``!30`` should flow "up" into block ``%bb1``, but that the location
954of variable ``!23`` should not flow "down" into the ``%exit`` block.
955
956.. _ccxx_frontend:
957
958C/C++ front-end specific debug information
959==========================================
960
961The C and C++ front-ends represent information about the program in a
962format that is effectively identical to `DWARF <http://www.dwarfstd.org/>`_
963in terms of information content.  This allows code generators to
964trivially support native debuggers by generating standard dwarf
965information, and contains enough information for non-dwarf targets to
966translate it as needed.
967
968This section describes the forms used to represent C and C++ programs.  Other
969languages could pattern themselves after this (which itself is tuned to
970representing programs in the same way that DWARF does), or they could choose
971to provide completely different forms if they don't fit into the DWARF model.
972As support for debugging information gets added to the various LLVM
973source-language front-ends, the information used should be documented here.
974
975The following sections provide examples of a few C/C++ constructs and
976the debug information that would best describe those constructs.  The
977canonical references are the ``DINode`` classes defined in
978``include/llvm/IR/DebugInfoMetadata.h`` and the implementations of the
979helper functions in ``lib/IR/DIBuilder.cpp``.
980
981C/C++ source file information
982-----------------------------
983
984``llvm::Instruction`` provides easy access to metadata attached with an
985instruction.  One can extract line number information encoded in LLVM IR using
986``Instruction::getDebugLoc()`` and ``DILocation::getLine()``.
987
988.. code-block:: c++
989
990  if (DILocation *Loc = I->getDebugLoc()) { // Here I is an LLVM instruction
991    unsigned Line = Loc->getLine();
992    StringRef File = Loc->getFilename();
993    StringRef Dir = Loc->getDirectory();
994    bool ImplicitCode = Loc->isImplicitCode();
995  }
996
997When the flag ImplicitCode is true then it means that the Instruction has been
998added by the front-end but doesn't correspond to source code written by the user. For example
999
1000.. code-block:: c++
1001
1002  if (MyBoolean) {
1003    MyObject MO;
1004    ...
1005  }
1006
1007At the end of the scope the MyObject's destructor is called but it isn't written
1008explicitly. This information is useful to avoid to have counters on brackets when
1009making code coverage.
1010
1011C/C++ global variable information
1012---------------------------------
1013
1014Given an integer global variable declared as follows:
1015
1016.. code-block:: c
1017
1018  _Alignas(8) int MyGlobal = 100;
1019
1020a C/C++ front-end would generate the following descriptors:
1021
1022.. code-block:: text
1023
1024  ;;
1025  ;; Define the global itself.
1026  ;;
1027  @MyGlobal = global i32 100, align 8, !dbg !0
1028
1029  ;;
1030  ;; List of debug info of globals
1031  ;;
1032  !llvm.dbg.cu = !{!1}
1033
1034  ;; Some unrelated metadata.
1035  !llvm.module.flags = !{!6, !7}
1036  !llvm.ident = !{!8}
1037
1038  ;; Define the global variable itself
1039  !0 = distinct !DIGlobalVariable(name: "MyGlobal", scope: !1, file: !2, line: 1, type: !5, isLocal: false, isDefinition: true, align: 64)
1040
1041  ;; Define the compile unit.
1042  !1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2,
1043                               producer: "clang version 4.0.0",
1044                               isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug,
1045                               enums: !3, globals: !4)
1046
1047  ;;
1048  ;; Define the file
1049  ;;
1050  !2 = !DIFile(filename: "/dev/stdin",
1051               directory: "/Users/dexonsmith/data/llvm/debug-info")
1052
1053  ;; An empty array.
1054  !3 = !{}
1055
1056  ;; The Array of Global Variables
1057  !4 = !{!0}
1058
1059  ;;
1060  ;; Define the type
1061  ;;
1062  !5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
1063
1064  ;; Dwarf version to output.
1065  !6 = !{i32 2, !"Dwarf Version", i32 4}
1066
1067  ;; Debug info schema version.
1068  !7 = !{i32 2, !"Debug Info Version", i32 3}
1069
1070  ;; Compiler identification
1071  !8 = !{!"clang version 4.0.0"}
1072
1073
1074The align value in DIGlobalVariable description specifies variable alignment in
1075case it was forced by C11 _Alignas(), C++11 alignas() keywords or compiler
1076attribute __attribute__((aligned ())). In other case (when this field is missing)
1077alignment is considered default. This is used when producing DWARF output
1078for DW_AT_alignment value.
1079
1080C/C++ function information
1081--------------------------
1082
1083Given a function declared as follows:
1084
1085.. code-block:: c
1086
1087  int main(int argc, char *argv[]) {
1088    return 0;
1089  }
1090
1091a C/C++ front-end would generate the following descriptors:
1092
1093.. code-block:: text
1094
1095  ;;
1096  ;; Define the anchor for subprograms.
1097  ;;
1098  !4 = !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !5,
1099                     isLocal: false, isDefinition: true, scopeLine: 1,
1100                     flags: DIFlagPrototyped, isOptimized: false,
1101                     retainedNodes: !2)
1102
1103  ;;
1104  ;; Define the subprogram itself.
1105  ;;
1106  define i32 @main(i32 %argc, i8** %argv) !dbg !4 {
1107  ...
1108  }
1109
1110C++ specific debug information
1111==============================
1112
1113C++ special member functions information
1114----------------------------------------
1115
1116DWARF v5 introduces attributes defined to enhance debugging information of C++ programs. LLVM can generate (or omit) these appropriate DWARF attributes. In C++ a special member function Ctors, Dtors, Copy/Move Ctors, assignment operators can be declared with C++11 keyword deleted. This is represented in LLVM using spFlags value DISPFlagDeleted.
1117
1118Given a class declaration with copy constructor declared as deleted:
1119
1120.. code-block:: c
1121
1122  class foo {
1123   public:
1124     foo(const foo&) = deleted;
1125  };
1126
1127A C++ frontend would generate following:
1128
1129.. code-block:: text
1130
1131  !17 = !DISubprogram(name: "foo", scope: !11, file: !1, line: 5, type: !18, scopeLine: 5, flags: DIFlagPublic | DIFlagPrototyped, spFlags: DISPFlagDeleted)
1132
1133and this will produce an additional DWARF attribute as:
1134
1135.. code-block:: text
1136
1137  DW_TAG_subprogram [7] *
1138    DW_AT_name [DW_FORM_strx1]    (indexed (00000006) string = "foo")
1139    DW_AT_decl_line [DW_FORM_data1]       (5)
1140    ...
1141    DW_AT_deleted [DW_FORM_flag_present]  (true)
1142
1143Fortran specific debug information
1144==================================
1145
1146Fortran function information
1147----------------------------
1148
1149There are a few DWARF attributes defined to support client debugging of Fortran programs.  LLVM can generate (or omit) the appropriate DWARF attributes for the prefix-specs of ELEMENTAL, PURE, IMPURE, RECURSIVE, and NON_RECURSIVE.  This is done by using the spFlags values: DISPFlagElemental, DISPFlagPure, and DISPFlagRecursive.
1150
1151.. code-block:: fortran
1152
1153  elemental function elem_func(a)
1154
1155a Fortran front-end would generate the following descriptors:
1156
1157.. code-block:: text
1158
1159  !11 = distinct !DISubprogram(name: "subroutine2", scope: !1, file: !1,
1160          line: 5, type: !8, scopeLine: 6,
1161          spFlags: DISPFlagDefinition | DISPFlagElemental, unit: !0,
1162          retainedNodes: !2)
1163
1164and this will materialize an additional DWARF attribute as:
1165
1166.. code-block:: text
1167
1168  DW_TAG_subprogram [3]
1169     DW_AT_low_pc [DW_FORM_addr]     (0x0000000000000010 ".text")
1170     DW_AT_high_pc [DW_FORM_data4]   (0x00000001)
1171     ...
1172     DW_AT_elemental [DW_FORM_flag_present]  (true)
1173
1174There are a few DWARF tags defined to represent Fortran specific constructs i.e DW_TAG_string_type for representing Fortran character(n). In LLVM this is represented as DIStringType.
1175
1176.. code-block:: fortran
1177
1178  character(len=*), intent(in) :: string
1179
1180a Fortran front-end would generate the following descriptors:
1181
1182.. code-block:: text
1183
1184  !DILocalVariable(name: "string", arg: 1, scope: !10, file: !3, line: 4, type: !15)
1185  !DIStringType(name: "character(*)!2", stringLength: !16, stringLengthExpression: !DIExpression(), size: 32)
1186
1187A fortran deferred-length character can also contain the information of raw storage of the characters in addition to the length of the string. This information is encoded in the  stringLocationExpression field. Based on this information, DW_AT_data_location attribute is emitted in a DW_TAG_string_type debug info.
1188
1189  !DIStringType(name: "character(*)!2", stringLengthExpression: !DIExpression(), stringLocationExpression: !DIExpression(DW_OP_push_object_address, DW_OP_deref), size: 32)
1190
1191and this will materialize in DWARF tags as:
1192
1193.. code-block:: text
1194
1195   DW_TAG_string_type
1196                DW_AT_name      ("character(*)!2")
1197                DW_AT_string_length     (0x00000064)
1198   0x00000064:    DW_TAG_variable
1199                  DW_AT_location      (DW_OP_fbreg +16)
1200                  DW_AT_type  (0x00000083 "integer*8")
1201                  DW_AT_data_location (DW_OP_push_object_address, DW_OP_deref)
1202                  ...
1203                  DW_AT_artificial    (true)
1204
1205A Fortran front-end may need to generate a *trampoline* function to call a
1206function defined in a different compilation unit. In this case, the front-end
1207can emit the following descriptor for the trampoline function:
1208
1209.. code-block:: text
1210
1211  !DISubprogram(name: "sub1_.t0p", linkageName: "sub1_.t0p", scope: !4, file: !4, type: !5, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !7, retainedNodes: !24, targetFuncName: "sub1_")
1212
1213The targetFuncName field is the name of the function that the trampoline
1214calls. This descriptor results in the following DWARF tag:
1215
1216.. code-block:: text
1217
1218  DW_TAG_subprogram
1219    ...
1220    DW_AT_linkage_name	("sub1_.t0p")
1221    DW_AT_name	("sub1_.t0p")
1222    DW_AT_trampoline	("sub1_")
1223
1224Debugging information format
1225============================
1226
1227Debugging Information Extension for Objective C Properties
1228----------------------------------------------------------
1229
1230Introduction
1231^^^^^^^^^^^^
1232
1233Objective C provides a simpler way to declare and define accessor methods using
1234declared properties.  The language provides features to declare a property and
1235to let compiler synthesize accessor methods.
1236
1237The debugger lets developer inspect Objective C interfaces and their instance
1238variables and class variables.  However, the debugger does not know anything
1239about the properties defined in Objective C interfaces.  The debugger consumes
1240information generated by compiler in DWARF format.  The format does not support
1241encoding of Objective C properties.  This proposal describes DWARF extensions to
1242encode Objective C properties, which the debugger can use to let developers
1243inspect Objective C properties.
1244
1245Proposal
1246^^^^^^^^
1247
1248Objective C properties exist separately from class members.  A property can be
1249defined only by "setter" and "getter" selectors, and be calculated anew on each
1250access.  Or a property can just be a direct access to some declared ivar.
1251Finally it can have an ivar "automatically synthesized" for it by the compiler,
1252in which case the property can be referred to in user code directly using the
1253standard C dereference syntax as well as through the property "dot" syntax, but
1254there is no entry in the ``@interface`` declaration corresponding to this ivar.
1255
1256To facilitate debugging, these properties we will add a new DWARF TAG into the
1257``DW_TAG_structure_type`` definition for the class to hold the description of a
1258given property, and a set of DWARF attributes that provide said description.
1259The property tag will also contain the name and declared type of the property.
1260
1261If there is a related ivar, there will also be a DWARF property attribute placed
1262in the ``DW_TAG_member`` DIE for that ivar referring back to the property TAG
1263for that property.  And in the case where the compiler synthesizes the ivar
1264directly, the compiler is expected to generate a ``DW_TAG_member`` for that
1265ivar (with the ``DW_AT_artificial`` set to 1), whose name will be the name used
1266to access this ivar directly in code, and with the property attribute pointing
1267back to the property it is backing.
1268
1269The following examples will serve as illustration for our discussion:
1270
1271.. code-block:: objc
1272
1273  @interface I1 {
1274    int n2;
1275  }
1276
1277  @property int p1;
1278  @property int p2;
1279  @end
1280
1281  @implementation I1
1282  @synthesize p1;
1283  @synthesize p2 = n2;
1284  @end
1285
1286This produces the following DWARF (this is a "pseudo dwarfdump" output):
1287
1288.. code-block:: none
1289
1290  0x00000100:  TAG_structure_type [7] *
1291                 AT_APPLE_runtime_class( 0x10 )
1292                 AT_name( "I1" )
1293                 AT_decl_file( "Objc_Property.m" )
1294                 AT_decl_line( 3 )
1295
1296  0x00000110    TAG_APPLE_property
1297                  AT_name ( "p1" )
1298                  AT_type ( {0x00000150} ( int ) )
1299
1300  0x00000120:   TAG_APPLE_property
1301                  AT_name ( "p2" )
1302                  AT_type ( {0x00000150} ( int ) )
1303
1304  0x00000130:   TAG_member [8]
1305                  AT_name( "_p1" )
1306                  AT_APPLE_property ( {0x00000110} "p1" )
1307                  AT_type( {0x00000150} ( int ) )
1308                  AT_artificial ( 0x1 )
1309
1310  0x00000140:    TAG_member [8]
1311                   AT_name( "n2" )
1312                   AT_APPLE_property ( {0x00000120} "p2" )
1313                   AT_type( {0x00000150} ( int ) )
1314
1315  0x00000150:  AT_type( ( int ) )
1316
1317Note, the current convention is that the name of the ivar for an
1318auto-synthesized property is the name of the property from which it derives
1319with an underscore prepended, as is shown in the example.  But we actually
1320don't need to know this convention, since we are given the name of the ivar
1321directly.
1322
1323Also, it is common practice in ObjC to have different property declarations in
1324the @interface and @implementation - e.g. to provide a read-only property in
1325the interface, and a read-write interface in the implementation.  In that case,
1326the compiler should emit whichever property declaration will be in force in the
1327current translation unit.
1328
1329Developers can decorate a property with attributes which are encoded using
1330``DW_AT_APPLE_property_attribute``.
1331
1332.. code-block:: objc
1333
1334  @property (readonly, nonatomic) int pr;
1335
1336.. code-block:: none
1337
1338  TAG_APPLE_property [8]
1339    AT_name( "pr" )
1340    AT_type ( {0x00000147} (int) )
1341    AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic)
1342
1343The setter and getter method names are attached to the property using
1344``DW_AT_APPLE_property_setter`` and ``DW_AT_APPLE_property_getter`` attributes.
1345
1346.. code-block:: objc
1347
1348  @interface I1
1349  @property (setter=myOwnP3Setter:) int p3;
1350  -(void)myOwnP3Setter:(int)a;
1351  @end
1352
1353  @implementation I1
1354  @synthesize p3;
1355  -(void)myOwnP3Setter:(int)a{ }
1356  @end
1357
1358The DWARF for this would be:
1359
1360.. code-block:: none
1361
1362  0x000003bd: TAG_structure_type [7] *
1363                AT_APPLE_runtime_class( 0x10 )
1364                AT_name( "I1" )
1365                AT_decl_file( "Objc_Property.m" )
1366                AT_decl_line( 3 )
1367
1368  0x000003cd      TAG_APPLE_property
1369                    AT_name ( "p3" )
1370                    AT_APPLE_property_setter ( "myOwnP3Setter:" )
1371                    AT_type( {0x00000147} ( int ) )
1372
1373  0x000003f3:     TAG_member [8]
1374                    AT_name( "_p3" )
1375                    AT_type ( {0x00000147} ( int ) )
1376                    AT_APPLE_property ( {0x000003cd} )
1377                    AT_artificial ( 0x1 )
1378
1379New DWARF Tags
1380^^^^^^^^^^^^^^
1381
1382+-----------------------+--------+
1383| TAG                   | Value  |
1384+=======================+========+
1385| DW_TAG_APPLE_property | 0x4200 |
1386+-----------------------+--------+
1387
1388New DWARF Attributes
1389^^^^^^^^^^^^^^^^^^^^
1390
1391+--------------------------------+--------+-----------+
1392| Attribute                      | Value  | Classes   |
1393+================================+========+===========+
1394| DW_AT_APPLE_property           | 0x3fed | Reference |
1395+--------------------------------+--------+-----------+
1396| DW_AT_APPLE_property_getter    | 0x3fe9 | String    |
1397+--------------------------------+--------+-----------+
1398| DW_AT_APPLE_property_setter    | 0x3fea | String    |
1399+--------------------------------+--------+-----------+
1400| DW_AT_APPLE_property_attribute | 0x3feb | Constant  |
1401+--------------------------------+--------+-----------+
1402
1403New DWARF Constants
1404^^^^^^^^^^^^^^^^^^^
1405
1406+--------------------------------------+-------+
1407| Name                                 | Value |
1408+======================================+=======+
1409| DW_APPLE_PROPERTY_readonly           | 0x01  |
1410+--------------------------------------+-------+
1411| DW_APPLE_PROPERTY_getter             | 0x02  |
1412+--------------------------------------+-------+
1413| DW_APPLE_PROPERTY_assign             | 0x04  |
1414+--------------------------------------+-------+
1415| DW_APPLE_PROPERTY_readwrite          | 0x08  |
1416+--------------------------------------+-------+
1417| DW_APPLE_PROPERTY_retain             | 0x10  |
1418+--------------------------------------+-------+
1419| DW_APPLE_PROPERTY_copy               | 0x20  |
1420+--------------------------------------+-------+
1421| DW_APPLE_PROPERTY_nonatomic          | 0x40  |
1422+--------------------------------------+-------+
1423| DW_APPLE_PROPERTY_setter             | 0x80  |
1424+--------------------------------------+-------+
1425| DW_APPLE_PROPERTY_atomic             | 0x100 |
1426+--------------------------------------+-------+
1427| DW_APPLE_PROPERTY_weak               | 0x200 |
1428+--------------------------------------+-------+
1429| DW_APPLE_PROPERTY_strong             | 0x400 |
1430+--------------------------------------+-------+
1431| DW_APPLE_PROPERTY_unsafe_unretained  | 0x800 |
1432+--------------------------------------+-------+
1433| DW_APPLE_PROPERTY_nullability        | 0x1000|
1434+--------------------------------------+-------+
1435| DW_APPLE_PROPERTY_null_resettable    | 0x2000|
1436+--------------------------------------+-------+
1437| DW_APPLE_PROPERTY_class              | 0x4000|
1438+--------------------------------------+-------+
1439
1440Name Accelerator Tables
1441-----------------------
1442
1443Introduction
1444^^^^^^^^^^^^
1445
1446The "``.debug_pubnames``" and "``.debug_pubtypes``" formats are not what a
1447debugger needs.  The "``pub``" in the section name indicates that the entries
1448in the table are publicly visible names only.  This means no static or hidden
1449functions show up in the "``.debug_pubnames``".  No static variables or private
1450class variables are in the "``.debug_pubtypes``".  Many compilers add different
1451things to these tables, so we can't rely upon the contents between gcc, icc, or
1452clang.
1453
1454The typical query given by users tends not to match up with the contents of
1455these tables.  For example, the DWARF spec states that "In the case of the name
1456of a function member or static data member of a C++ structure, class or union,
1457the name presented in the "``.debug_pubnames``" section is not the simple name
1458given by the ``DW_AT_name attribute`` of the referenced debugging information
1459entry, but rather the fully qualified name of the data or function member."
1460So the only names in these tables for complex C++ entries is a fully
1461qualified name.  Debugger users tend not to enter their search strings as
1462"``a::b::c(int,const Foo&) const``", but rather as "``c``", "``b::c``" , or
1463"``a::b::c``".  So the name entered in the name table must be demangled in
1464order to chop it up appropriately and additional names must be manually entered
1465into the table to make it effective as a name lookup table for debuggers to
1466use.
1467
1468All debuggers currently ignore the "``.debug_pubnames``" table as a result of
1469its inconsistent and useless public-only name content making it a waste of
1470space in the object file.  These tables, when they are written to disk, are not
1471sorted in any way, leaving every debugger to do its own parsing and sorting.
1472These tables also include an inlined copy of the string values in the table
1473itself making the tables much larger than they need to be on disk, especially
1474for large C++ programs.
1475
1476Can't we just fix the sections by adding all of the names we need to this
1477table? No, because that is not what the tables are defined to contain and we
1478won't know the difference between the old bad tables and the new good tables.
1479At best we could make our own renamed sections that contain all of the data we
1480need.
1481
1482These tables are also insufficient for what a debugger like LLDB needs.  LLDB
1483uses clang for its expression parsing where LLDB acts as a PCH.  LLDB is then
1484often asked to look for type "``foo``" or namespace "``bar``", or list items in
1485namespace "``baz``".  Namespaces are not included in the pubnames or pubtypes
1486tables.  Since clang asks a lot of questions when it is parsing an expression,
1487we need to be very fast when looking up names, as it happens a lot.  Having new
1488accelerator tables that are optimized for very quick lookups will benefit this
1489type of debugging experience greatly.
1490
1491We would like to generate name lookup tables that can be mapped into memory
1492from disk, and used as is, with little or no up-front parsing.  We would also
1493be able to control the exact content of these different tables so they contain
1494exactly what we need.  The Name Accelerator Tables were designed to fix these
1495issues.  In order to solve these issues we need to:
1496
1497* Have a format that can be mapped into memory from disk and used as is
1498* Lookups should be very fast
1499* Extensible table format so these tables can be made by many producers
1500* Contain all of the names needed for typical lookups out of the box
1501* Strict rules for the contents of tables
1502
1503Table size is important and the accelerator table format should allow the reuse
1504of strings from common string tables so the strings for the names are not
1505duplicated.  We also want to make sure the table is ready to be used as-is by
1506simply mapping the table into memory with minimal header parsing.
1507
1508The name lookups need to be fast and optimized for the kinds of lookups that
1509debuggers tend to do.  Optimally we would like to touch as few parts of the
1510mapped table as possible when doing a name lookup and be able to quickly find
1511the name entry we are looking for, or discover there are no matches.  In the
1512case of debuggers we optimized for lookups that fail most of the time.
1513
1514Each table that is defined should have strict rules on exactly what is in the
1515accelerator tables and documented so clients can rely on the content.
1516
1517Hash Tables
1518^^^^^^^^^^^
1519
1520Standard Hash Tables
1521""""""""""""""""""""
1522
1523Typical hash tables have a header, buckets, and each bucket points to the
1524bucket contents:
1525
1526.. code-block:: none
1527
1528  .------------.
1529  |  HEADER    |
1530  |------------|
1531  |  BUCKETS   |
1532  |------------|
1533  |  DATA      |
1534  `------------'
1535
1536The BUCKETS are an array of offsets to DATA for each hash:
1537
1538.. code-block:: none
1539
1540  .------------.
1541  | 0x00001000 | BUCKETS[0]
1542  | 0x00002000 | BUCKETS[1]
1543  | 0x00002200 | BUCKETS[2]
1544  | 0x000034f0 | BUCKETS[3]
1545  |            | ...
1546  | 0xXXXXXXXX | BUCKETS[n_buckets]
1547  '------------'
1548
1549So for ``bucket[3]`` in the example above, we have an offset into the table
15500x000034f0 which points to a chain of entries for the bucket.  Each bucket must
1551contain a next pointer, full 32 bit hash value, the string itself, and the data
1552for the current string value.
1553
1554.. code-block:: none
1555
1556              .------------.
1557  0x000034f0: | 0x00003500 | next pointer
1558              | 0x12345678 | 32 bit hash
1559              | "erase"    | string value
1560              | data[n]    | HashData for this bucket
1561              |------------|
1562  0x00003500: | 0x00003550 | next pointer
1563              | 0x29273623 | 32 bit hash
1564              | "dump"     | string value
1565              | data[n]    | HashData for this bucket
1566              |------------|
1567  0x00003550: | 0x00000000 | next pointer
1568              | 0x82638293 | 32 bit hash
1569              | "main"     | string value
1570              | data[n]    | HashData for this bucket
1571              `------------'
1572
1573The problem with this layout for debuggers is that we need to optimize for the
1574negative lookup case where the symbol we're searching for is not present.  So
1575if we were to lookup "``printf``" in the table above, we would make a 32-bit
1576hash for "``printf``", it might match ``bucket[3]``.  We would need to go to
1577the offset 0x000034f0 and start looking to see if our 32 bit hash matches.  To
1578do so, we need to read the next pointer, then read the hash, compare it, and
1579skip to the next bucket.  Each time we are skipping many bytes in memory and
1580touching new pages just to do the compare on the full 32 bit hash.  All of
1581these accesses then tell us that we didn't have a match.
1582
1583Name Hash Tables
1584""""""""""""""""
1585
1586To solve the issues mentioned above we have structured the hash tables a bit
1587differently: a header, buckets, an array of all unique 32 bit hash values,
1588followed by an array of hash value data offsets, one for each hash value, then
1589the data for all hash values:
1590
1591.. code-block:: none
1592
1593  .-------------.
1594  |  HEADER     |
1595  |-------------|
1596  |  BUCKETS    |
1597  |-------------|
1598  |  HASHES     |
1599  |-------------|
1600  |  OFFSETS    |
1601  |-------------|
1602  |  DATA       |
1603  `-------------'
1604
1605The ``BUCKETS`` in the name tables are an index into the ``HASHES`` array.  By
1606making all of the full 32 bit hash values contiguous in memory, we allow
1607ourselves to efficiently check for a match while touching as little memory as
1608possible.  Most often checking the 32 bit hash values is as far as the lookup
1609goes.  If it does match, it usually is a match with no collisions.  So for a
1610table with "``n_buckets``" buckets, and "``n_hashes``" unique 32 bit hash
1611values, we can clarify the contents of the ``BUCKETS``, ``HASHES`` and
1612``OFFSETS`` as:
1613
1614.. code-block:: none
1615
1616  .-------------------------.
1617  |  HEADER.magic           | uint32_t
1618  |  HEADER.version         | uint16_t
1619  |  HEADER.hash_function   | uint16_t
1620  |  HEADER.bucket_count    | uint32_t
1621  |  HEADER.hashes_count    | uint32_t
1622  |  HEADER.header_data_len | uint32_t
1623  |  HEADER_DATA            | HeaderData
1624  |-------------------------|
1625  |  BUCKETS                | uint32_t[n_buckets] // 32 bit hash indexes
1626  |-------------------------|
1627  |  HASHES                 | uint32_t[n_hashes] // 32 bit hash values
1628  |-------------------------|
1629  |  OFFSETS                | uint32_t[n_hashes] // 32 bit offsets to hash value data
1630  |-------------------------|
1631  |  ALL HASH DATA          |
1632  `-------------------------'
1633
1634So taking the exact same data from the standard hash example above we end up
1635with:
1636
1637.. code-block:: none
1638
1639              .------------.
1640              | HEADER     |
1641              |------------|
1642              |          0 | BUCKETS[0]
1643              |          2 | BUCKETS[1]
1644              |          5 | BUCKETS[2]
1645              |          6 | BUCKETS[3]
1646              |            | ...
1647              |        ... | BUCKETS[n_buckets]
1648              |------------|
1649              | 0x........ | HASHES[0]
1650              | 0x........ | HASHES[1]
1651              | 0x........ | HASHES[2]
1652              | 0x........ | HASHES[3]
1653              | 0x........ | HASHES[4]
1654              | 0x........ | HASHES[5]
1655              | 0x12345678 | HASHES[6]    hash for BUCKETS[3]
1656              | 0x29273623 | HASHES[7]    hash for BUCKETS[3]
1657              | 0x82638293 | HASHES[8]    hash for BUCKETS[3]
1658              | 0x........ | HASHES[9]
1659              | 0x........ | HASHES[10]
1660              | 0x........ | HASHES[11]
1661              | 0x........ | HASHES[12]
1662              | 0x........ | HASHES[13]
1663              | 0x........ | HASHES[n_hashes]
1664              |------------|
1665              | 0x........ | OFFSETS[0]
1666              | 0x........ | OFFSETS[1]
1667              | 0x........ | OFFSETS[2]
1668              | 0x........ | OFFSETS[3]
1669              | 0x........ | OFFSETS[4]
1670              | 0x........ | OFFSETS[5]
1671              | 0x000034f0 | OFFSETS[6]   offset for BUCKETS[3]
1672              | 0x00003500 | OFFSETS[7]   offset for BUCKETS[3]
1673              | 0x00003550 | OFFSETS[8]   offset for BUCKETS[3]
1674              | 0x........ | OFFSETS[9]
1675              | 0x........ | OFFSETS[10]
1676              | 0x........ | OFFSETS[11]
1677              | 0x........ | OFFSETS[12]
1678              | 0x........ | OFFSETS[13]
1679              | 0x........ | OFFSETS[n_hashes]
1680              |------------|
1681              |            |
1682              |            |
1683              |            |
1684              |            |
1685              |            |
1686              |------------|
1687  0x000034f0: | 0x00001203 | .debug_str ("erase")
1688              | 0x00000004 | A 32 bit array count - number of HashData with name "erase"
1689              | 0x........ | HashData[0]
1690              | 0x........ | HashData[1]
1691              | 0x........ | HashData[2]
1692              | 0x........ | HashData[3]
1693              | 0x00000000 | String offset into .debug_str (terminate data for hash)
1694              |------------|
1695  0x00003500: | 0x00001203 | String offset into .debug_str ("collision")
1696              | 0x00000002 | A 32 bit array count - number of HashData with name "collision"
1697              | 0x........ | HashData[0]
1698              | 0x........ | HashData[1]
1699              | 0x00001203 | String offset into .debug_str ("dump")
1700              | 0x00000003 | A 32 bit array count - number of HashData with name "dump"
1701              | 0x........ | HashData[0]
1702              | 0x........ | HashData[1]
1703              | 0x........ | HashData[2]
1704              | 0x00000000 | String offset into .debug_str (terminate data for hash)
1705              |------------|
1706  0x00003550: | 0x00001203 | String offset into .debug_str ("main")
1707              | 0x00000009 | A 32 bit array count - number of HashData with name "main"
1708              | 0x........ | HashData[0]
1709              | 0x........ | HashData[1]
1710              | 0x........ | HashData[2]
1711              | 0x........ | HashData[3]
1712              | 0x........ | HashData[4]
1713              | 0x........ | HashData[5]
1714              | 0x........ | HashData[6]
1715              | 0x........ | HashData[7]
1716              | 0x........ | HashData[8]
1717              | 0x00000000 | String offset into .debug_str (terminate data for hash)
1718              `------------'
1719
1720So we still have all of the same data, we just organize it more efficiently for
1721debugger lookup.  If we repeat the same "``printf``" lookup from above, we
1722would hash "``printf``" and find it matches ``BUCKETS[3]`` by taking the 32 bit
1723hash value and modulo it by ``n_buckets``.  ``BUCKETS[3]`` contains "6" which
1724is the index into the ``HASHES`` table.  We would then compare any consecutive
172532 bit hashes values in the ``HASHES`` array as long as the hashes would be in
1726``BUCKETS[3]``.  We do this by verifying that each subsequent hash value modulo
1727``n_buckets`` is still 3.  In the case of a failed lookup we would access the
1728memory for ``BUCKETS[3]``, and then compare a few consecutive 32 bit hashes
1729before we know that we have no match.  We don't end up marching through
1730multiple words of memory and we really keep the number of processor data cache
1731lines being accessed as small as possible.
1732
1733The string hash that is used for these lookup tables is the Daniel J.
1734Bernstein hash which is also used in the ELF ``GNU_HASH`` sections.  It is a
1735very good hash for all kinds of names in programs with very few hash
1736collisions.
1737
1738Empty buckets are designated by using an invalid hash index of ``UINT32_MAX``.
1739
1740Details
1741^^^^^^^
1742
1743These name hash tables are designed to be generic where specializations of the
1744table get to define additional data that goes into the header ("``HeaderData``"),
1745how the string value is stored ("``KeyType``") and the content of the data for each
1746hash value.
1747
1748Header Layout
1749"""""""""""""
1750
1751The header has a fixed part, and the specialized part.  The exact format of the
1752header is:
1753
1754.. code-block:: c
1755
1756  struct Header
1757  {
1758    uint32_t   magic;           // 'HASH' magic value to allow endian detection
1759    uint16_t   version;         // Version number
1760    uint16_t   hash_function;   // The hash function enumeration that was used
1761    uint32_t   bucket_count;    // The number of buckets in this hash table
1762    uint32_t   hashes_count;    // The total number of unique hash values and hash data offsets in this table
1763    uint32_t   header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment
1764                                // Specifically the length of the following HeaderData field - this does not
1765                                // include the size of the preceding fields
1766    HeaderData header_data;     // Implementation specific header data
1767  };
1768
1769The header starts with a 32 bit "``magic``" value which must be ``'HASH'``
1770encoded as an ASCII integer.  This allows the detection of the start of the
1771hash table and also allows the table's byte order to be determined so the table
1772can be correctly extracted.  The "``magic``" value is followed by a 16 bit
1773``version`` number which allows the table to be revised and modified in the
1774future.  The current version number is 1. ``hash_function`` is a ``uint16_t``
1775enumeration that specifies which hash function was used to produce this table.
1776The current values for the hash function enumerations include:
1777
1778.. code-block:: c
1779
1780  enum HashFunctionType
1781  {
1782    eHashFunctionDJB = 0u, // Daniel J Bernstein hash function
1783  };
1784
1785``bucket_count`` is a 32 bit unsigned integer that represents how many buckets
1786are in the ``BUCKETS`` array.  ``hashes_count`` is the number of unique 32 bit
1787hash values that are in the ``HASHES`` array, and is the same number of offsets
1788are contained in the ``OFFSETS`` array.  ``header_data_len`` specifies the size
1789in bytes of the ``HeaderData`` that is filled in by specialized versions of
1790this table.
1791
1792Fixed Lookup
1793""""""""""""
1794
1795The header is followed by the buckets, hashes, offsets, and hash value data.
1796
1797.. code-block:: c
1798
1799  struct FixedTable
1800  {
1801    uint32_t buckets[Header.bucket_count];  // An array of hash indexes into the "hashes[]" array below
1802    uint32_t hashes [Header.hashes_count];  // Every unique 32 bit hash for the entire table is in this table
1803    uint32_t offsets[Header.hashes_count];  // An offset that corresponds to each item in the "hashes[]" array above
1804  };
1805
1806``buckets`` is an array of 32 bit indexes into the ``hashes`` array.  The
1807``hashes`` array contains all of the 32 bit hash values for all names in the
1808hash table.  Each hash in the ``hashes`` table has an offset in the ``offsets``
1809array that points to the data for the hash value.
1810
1811This table setup makes it very easy to repurpose these tables to contain
1812different data, while keeping the lookup mechanism the same for all tables.
1813This layout also makes it possible to save the table to disk and map it in
1814later and do very efficient name lookups with little or no parsing.
1815
1816DWARF lookup tables can be implemented in a variety of ways and can store a lot
1817of information for each name.  We want to make the DWARF tables extensible and
1818able to store the data efficiently so we have used some of the DWARF features
1819that enable efficient data storage to define exactly what kind of data we store
1820for each name.
1821
1822The ``HeaderData`` contains a definition of the contents of each HashData chunk.
1823We might want to store an offset to all of the debug information entries (DIEs)
1824for each name.  To keep things extensible, we create a list of items, or
1825Atoms, that are contained in the data for each name.  First comes the type of
1826the data in each atom:
1827
1828.. code-block:: c
1829
1830  enum AtomType
1831  {
1832    eAtomTypeNULL       = 0u,
1833    eAtomTypeDIEOffset  = 1u,   // DIE offset, check form for encoding
1834    eAtomTypeCUOffset   = 2u,   // DIE offset of the compiler unit header that contains the item in question
1835    eAtomTypeTag        = 3u,   // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
1836    eAtomTypeNameFlags  = 4u,   // Flags from enum NameFlags
1837    eAtomTypeTypeFlags  = 5u,   // Flags from enum TypeFlags
1838  };
1839
1840The enumeration values and their meanings are:
1841
1842.. code-block:: none
1843
1844  eAtomTypeNULL       - a termination atom that specifies the end of the atom list
1845  eAtomTypeDIEOffset  - an offset into the .debug_info section for the DWARF DIE for this name
1846  eAtomTypeCUOffset   - an offset into the .debug_info section for the CU that contains the DIE
1847  eAtomTypeDIETag     - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is
1848  eAtomTypeNameFlags  - Flags for functions and global variables (isFunction, isInlined, isExternal...)
1849  eAtomTypeTypeFlags  - Flags for types (isCXXClass, isObjCClass, ...)
1850
1851Then we allow each atom type to define the atom type and how the data for each
1852atom type data is encoded:
1853
1854.. code-block:: c
1855
1856  struct Atom
1857  {
1858    uint16_t type;  // AtomType enum value
1859    uint16_t form;  // DWARF DW_FORM_XXX defines
1860  };
1861
1862The ``form`` type above is from the DWARF specification and defines the exact
1863encoding of the data for the Atom type.  See the DWARF specification for the
1864``DW_FORM_`` definitions.
1865
1866.. code-block:: c
1867
1868  struct HeaderData
1869  {
1870    uint32_t die_offset_base;
1871    uint32_t atom_count;
1872    Atoms    atoms[atom_count0];
1873  };
1874
1875``HeaderData`` defines the base DIE offset that should be added to any atoms
1876that are encoded using the ``DW_FORM_ref1``, ``DW_FORM_ref2``,
1877``DW_FORM_ref4``, ``DW_FORM_ref8`` or ``DW_FORM_ref_udata``.  It also defines
1878what is contained in each ``HashData`` object -- ``Atom.form`` tells us how large
1879each field will be in the ``HashData`` and the ``Atom.type`` tells us how this data
1880should be interpreted.
1881
1882For the current implementations of the "``.apple_names``" (all functions +
1883globals), the "``.apple_types``" (names of all types that are defined), and
1884the "``.apple_namespaces``" (all namespaces), we currently set the ``Atom``
1885array to be:
1886
1887.. code-block:: c
1888
1889  HeaderData.atom_count = 1;
1890  HeaderData.atoms[0].type = eAtomTypeDIEOffset;
1891  HeaderData.atoms[0].form = DW_FORM_data4;
1892
1893This defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is
1894encoded as a 32 bit value (DW_FORM_data4).  This allows a single name to have
1895multiple matching DIEs in a single file, which could come up with an inlined
1896function for instance.  Future tables could include more information about the
1897DIE such as flags indicating if the DIE is a function, method, block,
1898or inlined.
1899
1900The KeyType for the DWARF table is a 32 bit string table offset into the
1901".debug_str" table.  The ".debug_str" is the string table for the DWARF which
1902may already contain copies of all of the strings.  This helps make sure, with
1903help from the compiler, that we reuse the strings between all of the DWARF
1904sections and keeps the hash table size down.  Another benefit to having the
1905compiler generate all strings as DW_FORM_strp in the debug info, is that
1906DWARF parsing can be made much faster.
1907
1908After a lookup is made, we get an offset into the hash data.  The hash data
1909needs to be able to deal with 32 bit hash collisions, so the chunk of data
1910at the offset in the hash data consists of a triple:
1911
1912.. code-block:: c
1913
1914  uint32_t str_offset
1915  uint32_t hash_data_count
1916  HashData[hash_data_count]
1917
1918If "str_offset" is zero, then the bucket contents are done. 99.9% of the
1919hash data chunks contain a single item (no 32 bit hash collision):
1920
1921.. code-block:: none
1922
1923  .------------.
1924  | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
1925  | 0x00000004 | uint32_t HashData count
1926  | 0x........ | uint32_t HashData[0] DIE offset
1927  | 0x........ | uint32_t HashData[1] DIE offset
1928  | 0x........ | uint32_t HashData[2] DIE offset
1929  | 0x........ | uint32_t HashData[3] DIE offset
1930  | 0x00000000 | uint32_t KeyType (end of hash chain)
1931  `------------'
1932
1933If there are collisions, you will have multiple valid string offsets:
1934
1935.. code-block:: none
1936
1937  .------------.
1938  | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
1939  | 0x00000004 | uint32_t HashData count
1940  | 0x........ | uint32_t HashData[0] DIE offset
1941  | 0x........ | uint32_t HashData[1] DIE offset
1942  | 0x........ | uint32_t HashData[2] DIE offset
1943  | 0x........ | uint32_t HashData[3] DIE offset
1944  | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print")
1945  | 0x00000002 | uint32_t HashData count
1946  | 0x........ | uint32_t HashData[0] DIE offset
1947  | 0x........ | uint32_t HashData[1] DIE offset
1948  | 0x00000000 | uint32_t KeyType (end of hash chain)
1949  `------------'
1950
1951Current testing with real world C++ binaries has shown that there is around 1
195232 bit hash collision per 100,000 name entries.
1953
1954Contents
1955^^^^^^^^
1956
1957As we said, we want to strictly define exactly what is included in the
1958different tables.  For DWARF, we have 3 tables: "``.apple_names``",
1959"``.apple_types``", and "``.apple_namespaces``".
1960
1961"``.apple_names``" sections should contain an entry for each DWARF DIE whose
1962``DW_TAG`` is a ``DW_TAG_label``, ``DW_TAG_inlined_subroutine``, or
1963``DW_TAG_subprogram`` that has address attributes: ``DW_AT_low_pc``,
1964``DW_AT_high_pc``, ``DW_AT_ranges`` or ``DW_AT_entry_pc``.  It also contains
1965``DW_TAG_variable`` DIEs that have a ``DW_OP_addr`` in the location (global and
1966static variables).  All global and static variables should be included,
1967including those scoped within functions and classes.  For example using the
1968following code:
1969
1970.. code-block:: c
1971
1972  static int var = 0;
1973
1974  void f ()
1975  {
1976    static int var = 0;
1977  }
1978
1979Both of the static ``var`` variables would be included in the table.  All
1980functions should emit both their full names and their basenames.  For C or C++,
1981the full name is the mangled name (if available) which is usually in the
1982``DW_AT_MIPS_linkage_name`` attribute, and the ``DW_AT_name`` contains the
1983function basename.  If global or static variables have a mangled name in a
1984``DW_AT_MIPS_linkage_name`` attribute, this should be emitted along with the
1985simple name found in the ``DW_AT_name`` attribute.
1986
1987"``.apple_types``" sections should contain an entry for each DWARF DIE whose
1988tag is one of:
1989
1990* DW_TAG_array_type
1991* DW_TAG_class_type
1992* DW_TAG_enumeration_type
1993* DW_TAG_pointer_type
1994* DW_TAG_reference_type
1995* DW_TAG_string_type
1996* DW_TAG_structure_type
1997* DW_TAG_subroutine_type
1998* DW_TAG_typedef
1999* DW_TAG_union_type
2000* DW_TAG_ptr_to_member_type
2001* DW_TAG_set_type
2002* DW_TAG_subrange_type
2003* DW_TAG_base_type
2004* DW_TAG_const_type
2005* DW_TAG_immutable_type
2006* DW_TAG_file_type
2007* DW_TAG_namelist
2008* DW_TAG_packed_type
2009* DW_TAG_volatile_type
2010* DW_TAG_restrict_type
2011* DW_TAG_atomic_type
2012* DW_TAG_interface_type
2013* DW_TAG_unspecified_type
2014* DW_TAG_shared_type
2015
2016Only entries with a ``DW_AT_name`` attribute are included, and the entry must
2017not be a forward declaration (``DW_AT_declaration`` attribute with a non-zero
2018value).  For example, using the following code:
2019
2020.. code-block:: c
2021
2022  int main ()
2023  {
2024    int *b = 0;
2025    return *b;
2026  }
2027
2028We get a few type DIEs:
2029
2030.. code-block:: none
2031
2032  0x00000067:     TAG_base_type [5]
2033                  AT_encoding( DW_ATE_signed )
2034                  AT_name( "int" )
2035                  AT_byte_size( 0x04 )
2036
2037  0x0000006e:     TAG_pointer_type [6]
2038                  AT_type( {0x00000067} ( int ) )
2039                  AT_byte_size( 0x08 )
2040
2041The DW_TAG_pointer_type is not included because it does not have a ``DW_AT_name``.
2042
2043"``.apple_namespaces``" section should contain all ``DW_TAG_namespace`` DIEs.
2044If we run into a namespace that has no name this is an anonymous namespace, and
2045the name should be output as "``(anonymous namespace)``" (without the quotes).
2046Why?  This matches the output of the ``abi::cxa_demangle()`` that is in the
2047standard C++ library that demangles mangled names.
2048
2049
2050Language Extensions and File Format Changes
2051^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2052
2053Objective-C Extensions
2054""""""""""""""""""""""
2055
2056"``.apple_objc``" section should contain all ``DW_TAG_subprogram`` DIEs for an
2057Objective-C class.  The name used in the hash table is the name of the
2058Objective-C class itself.  If the Objective-C class has a category, then an
2059entry is made for both the class name without the category, and for the class
2060name with the category.  So if we have a DIE at offset 0x1234 with a name of
2061method "``-[NSString(my_additions) stringWithSpecialString:]``", we would add
2062an entry for "``NSString``" that points to DIE 0x1234, and an entry for
2063"``NSString(my_additions)``" that points to 0x1234.  This allows us to quickly
2064track down all Objective-C methods for an Objective-C class when doing
2065expressions.  It is needed because of the dynamic nature of Objective-C where
2066anyone can add methods to a class.  The DWARF for Objective-C methods is also
2067emitted differently from C++ classes where the methods are not usually
2068contained in the class definition, they are scattered about across one or more
2069compile units.  Categories can also be defined in different shared libraries.
2070So we need to be able to quickly find all of the methods and class functions
2071given the Objective-C class name, or quickly find all methods and class
2072functions for a class + category name.  This table does not contain any
2073selector names, it just maps Objective-C class names (or class names +
2074category) to all of the methods and class functions.  The selectors are added
2075as function basenames in the "``.debug_names``" section.
2076
2077In the "``.apple_names``" section for Objective-C functions, the full name is
2078the entire function name with the brackets ("``-[NSString
2079stringWithCString:]``") and the basename is the selector only
2080("``stringWithCString:``").
2081
2082Mach-O Changes
2083""""""""""""""
2084
2085The sections names for the apple hash tables are for non-mach-o files.  For
2086mach-o files, the sections should be contained in the ``__DWARF`` segment with
2087names as follows:
2088
2089* "``.apple_names``" -> "``__apple_names``"
2090* "``.apple_types``" -> "``__apple_types``"
2091* "``.apple_namespaces``" -> "``__apple_namespac``" (16 character limit)
2092* "``.apple_objc``" -> "``__apple_objc``"
2093
2094.. _codeview:
2095
2096CodeView Debug Info Format
2097==========================
2098
2099LLVM supports emitting CodeView, the Microsoft debug info format, and this
2100section describes the design and implementation of that support.
2101
2102Format Background
2103-----------------
2104
2105CodeView as a format is clearly oriented around C++ debugging, and in C++, the
2106majority of debug information tends to be type information. Therefore, the
2107overriding design constraint of CodeView is the separation of type information
2108from other "symbol" information so that type information can be efficiently
2109merged across translation units. Both type information and symbol information is
2110generally stored as a sequence of records, where each record begins with a
211116-bit record size and a 16-bit record kind.
2112
2113Type information is usually stored in the ``.debug$T`` section of the object
2114file.  All other debug info, such as line info, string table, symbol info, and
2115inlinee info, is stored in one or more ``.debug$S`` sections. There may only be
2116one ``.debug$T`` section per object file, since all other debug info refers to
2117it. If a PDB (enabled by the ``/Zi`` MSVC option) was used during compilation,
2118the ``.debug$T`` section will contain only an ``LF_TYPESERVER2`` record pointing
2119to the PDB. When using PDBs, symbol information appears to remain in the object
2120file ``.debug$S`` sections.
2121
2122Type records are referred to by their index, which is the number of records in
2123the stream before a given record plus ``0x1000``. Many common basic types, such
2124as the basic integral types and unqualified pointers to them, are represented
2125using type indices less than ``0x1000``. Such basic types are built in to
2126CodeView consumers and do not require type records.
2127
2128Each type record may only contain type indices that are less than its own type
2129index. This ensures that the graph of type stream references is acyclic. While
2130the source-level type graph may contain cycles through pointer types (consider a
2131linked list struct), these cycles are removed from the type stream by always
2132referring to the forward declaration record of user-defined record types. Only
2133"symbol" records in the ``.debug$S`` streams may refer to complete,
2134non-forward-declaration type records.
2135
2136Working with CodeView
2137---------------------
2138
2139These are instructions for some common tasks for developers working to improve
2140LLVM's CodeView support. Most of them revolve around using the CodeView dumper
2141embedded in ``llvm-readobj``.
2142
2143* Testing MSVC's output::
2144
2145    $ cl -c -Z7 foo.cpp # Use /Z7 to keep types in the object file
2146    $ llvm-readobj --codeview foo.obj
2147
2148* Getting LLVM IR debug info out of Clang::
2149
2150    $ clang -g -gcodeview --target=x86_64-windows-msvc foo.cpp -S -emit-llvm
2151
2152  Use this to generate LLVM IR for LLVM test cases.
2153
2154* Generate and dump CodeView from LLVM IR metadata::
2155
2156    $ llc foo.ll -filetype=obj -o foo.obj
2157    $ llvm-readobj --codeview foo.obj > foo.txt
2158
2159  Use this pattern in lit test cases and FileCheck the output of llvm-readobj
2160
2161Improving LLVM's CodeView support is a process of finding interesting type
2162records, constructing a C++ test case that makes MSVC emit those records,
2163dumping the records, understanding them, and then generating equivalent records
2164in LLVM's backend.
2165