This document is the central repository for all information pertaining to
debug information in LLVM. It describes the actual format
that the LLVM debug information takes, which is useful for those
interested in creating front-ends or dealing directly with the information.
Further, this document provides specific examples of what debug information
for C/C++ looks like.

The idea of the LLVM debugging information is to capture how the important
pieces of the source-language's Abstract Syntax Tree map onto LLVM code.
Several design aspects have shaped the solution that appears here. The
important ones are:

Debugging information should have very little impact on the rest of the
compiler. No transformations, analyses, or code generators should need to
be modified because of debugging information.

Because LLVM is designed to support arbitrary programming languages,
LLVM-to-LLVM tools should not need to know anything about the semantics of
the source-level-language.

Source-level languages are often widely different from one another.
LLVM should not put any restrictions of the flavor of the source-language,
and the debugging information should work with any language.

With code generator support, it should be possible to use an LLVM compiler
to compile a program to native machine code and standard debugging
formats. This allows compatibility with traditional machine-code level
debuggers, like GDB or DBX.

The approach used by the LLVM implementation is to use a small set
of intrinsic functions to define a
mapping between LLVM program objects and the source-level objects. The
description of the source-level program is maintained in LLVM metadata
in an implementation-defined format
(the C/C++ front-end currently uses working draft 7 of
the DWARF 3
standard).

When a program is being debugged, a debugger interacts with the user and
turns the stored debug information into source-language specific information.
As such, a debugger must be aware of the source-language, and is thus tied to
a specific language or family of languages.

The role of debug information is to provide meta information normally
stripped away during the compilation process. This meta information provides
an LLVM user a relationship between generated code and the original program
source code.

Currently, debug information is consumed by DwarfDebug to produce dwarf
information used by the gdb debugger. Other targets could use the same
information to produce stabs or other debug forms.

It would also be reasonable to use debug information to feed profiling tools
for analysis of generated code, or, tools for reconstructing the original
source from generated code.

An extremely high priority of LLVM debugging information is to make it
interact well with optimizations and analysis. In particular, the LLVM debug
information provides the following guarantees:

LLVM debug information always provides information to accurately read
the source-level state of the program, regardless of which LLVM
optimizations have been run, and without any modification to the
optimizations themselves. However, some optimizations may impact the
ability to modify the current state of the program with a debugger, such
as setting program variables, or calling functions that have been
deleted.

As desired, LLVM optimizations can be upgraded to be aware of the LLVM
debugging information, allowing them to update the debugging information
as they perform aggressive optimizations. This means that, with effort,
the LLVM optimizers could optimize debug code just as well as non-debug
code.

LLVM debug information is automatically optimized along with the rest of
the program, using existing facilities. For example, duplicate
information is automatically merged by the linker, and unused information
is automatically removed.

Basically, the debug information allows you to compile a program with
"-O0 -g" and get full debug information, allowing you to arbitrarily
modify the program as it executes from a debugger. Compiling a program with
"-O3 -g" gives you full debug information that is always available
and accurate for reading (e.g., you get accurate stack traces despite tail
call elimination and inlining), but you might lose the ability to modify the
program and call functions where were optimized out of the program, or
inlined away completely.

LLVM test suite provides a
framework to test optimizer's handling of debugging information. It can be
run like this:

% cd llvm/projects/test-suite/MultiSource/Benchmarks # or some other level
% make TEST=dbgopt

This will test impact of debugging information on optimization passes. If
debugging information influences optimization passes then it will be reported
as a failure. See TestingGuide for more
information on LLVM test infrastructure and how to run various tests.

LLVM debugging information has been carefully designed to make it possible
for the optimizer to optimize the program and debugging information without
necessarily having to know anything about debugging information. In
particular, the use of metadata avoids duplicated debugging information from
the beginning, and the global dead code elimination pass automatically
deletes debugging information for a function if it decides to delete the
function.

To do this, most of the debugging information (descriptors for types,
variables, functions, source files, etc) is inserted by the language
front-end in the form of LLVM metadata.

Debug information is designed to be agnostic about the target debugger and
debugging information representation (e.g. DWARF/Stabs/etc). It uses a
generic pass to decode the information that represents variables, types,
functions, namespaces, etc: this allows for arbitrary source-language
semantics and type-systems to be used, as long as there is a module
written for the target debugger to interpret the information.

To provide basic functionality, the LLVM debugger does have to make some
assumptions about the source-level language being debugged, though it keeps
these to a minimum. The only common features that the LLVM debugger assumes
exist are source files,
and program objects. These abstract
objects are used by a debugger to form stack traces, show information about
local variables, etc.

This section of the documentation first describes the representation aspects
common to any source-language. The next section
describes the data layout conventions used by the C and C++ front-ends.

In consideration of the complexity and volume of debug information, LLVM
provides a specification for well formed debug descriptors.

Consumers of LLVM debug information expect the descriptors for program
objects to start in a canonical format, but the descriptors can include
additional information appended at the end that is source-language
specific. All LLVM debugging information is versioned, allowing backwards
compatibility in the case that the core structures need to change in some
way. Also, all debugging information objects start with a tag to indicate
what type of object it is. The source-language is allowed to define its own
objects, by using unreserved tag numbers. We recommend using with tags in
the range 0x1000 through 0x2000 (there is a defined enum DW_TAG_user_base =
0x1000.)

The fields of debug descriptors used internally by LLVM
are restricted to only the simple data types i32, i1,
float, double, mdstring and mdnode.

These descriptors contain a source language ID for the file (we use the DWARF
3.0 ID numbers, such as DW_LANG_C89, DW_LANG_C_plus_plus,
DW_LANG_Cobol74, etc), three strings describing the filename,
working directory of the compiler, and an identifier string for the compiler
that produced it.

Compile unit descriptors provide the root context for objects declared in a
specific compilation unit. File descriptors are defined using this context.
These descriptors are collected by a named metadata
!llvm.dbg.cu. Compile unit descriptor keeps track of subprograms,
global variables and type information.

These descriptors provide debug information about globals variables. The
provide details such as name, type and where the variable is defined. All
global variables are collected by named metadata !llvm.dbg.gv.

These descriptors provide debug information about functions, methods and
subprograms. They provide details such as name, return types and the source
location where the subprogram is defined.
All subprogram descriptors are collected by a named metadata
!llvm.dbg.sp.

These descriptors define primitive types used in the code. Example int, bool
and float. The context provides the scope of the type, which is usually the
top level. Since basic types are not usually user defined the context
and line number can be left as NULL and 0. The size, alignment and offset
are expressed in bits and can be 64 bit values. The alignment is used to
round the offset when embedded in a
composite type (example to keep float
doubles on 64 bit boundaries.) The offset is the bit offset if embedded in
a composite type.

The type encoding provides the details of the type. The values are typically
one of the following:

DW_TAG_member is used to define a member of
a composite type
or subprogram. The type of the member is
the derived
type. DW_TAG_formal_parameter is used to define a member which
is a formal argument of a subprogram.

DW_TAG_typedef is used to provide a name for the derived type.

DW_TAG_pointer_type,DW_TAG_reference_type,
DW_TAG_const_type, DW_TAG_volatile_type
and DW_TAG_restrict_type are used to qualify
the derived type.

Derived type location can be determined
from the context and line number. The size, alignment and offset are
expressed in bits and can be 64 bit values. The alignment is used to round
the offset when embedded in a composite
type (example to keep float doubles on 64 bit boundaries.) The offset is
the bit offset if embedded in a composite
type.

The vector flag indicates that an array type is a native packed vector.

The members of array types (tag = DW_TAG_array_type) or vector types
(tag = DW_TAG_vector_type) are subrange
descriptors, each representing the range of subscripts at that level of
indexing.

The members of enumeration types (tag = DW_TAG_enumeration_type) are
enumerator descriptors, each representing
the definition of enumeration value for the set. All enumeration type
descriptors are collected by named metadata !llvm.dbg.enum.

The members of structure (tag = DW_TAG_structure_type) or union (tag
= DW_TAG_union_type) types are any one of
the basic,
derived
or composite type descriptors, each
representing a field member of the structure or union.

For C++ classes (tag = DW_TAG_structure_type), member descriptors
provide information about base classes, static members and member
functions. If a member is a derived type
descriptor and has a tag of DW_TAG_inheritance, then the type
represents a base class. If the member of is
a global variable descriptor then it
represents a static member. And, if the member is
a subprogram descriptor then it represents
a member function. For static members and member
functions, getName() returns the members link or the C++ mangled
name. getDisplayName() the simplied version of the name.

The first member of subroutine (tag = DW_TAG_subroutine_type) type
elements is the return type for the subroutine. The remaining elements are
the formal arguments to the subroutine.

Composite type location can be
determined from the context and line number. The size, alignment and
offset are expressed in bits and can be 64 bit values. The alignment is used
to round the offset when embedded in
a composite type (as an example, to keep
float doubles on 64 bit boundaries.) The offset is the bit offset if embedded
in a composite type.

These descriptors are used to define ranges of array subscripts for an array
composite type. The low value defines
the lower bounds typically zero for C/C++. The high value is the upper
bounds. Values are 64 bit. High - low + 1 is the size of the array. If low
> high the array bounds are not included in generated debugging information.

An auto variable is any variable declared in the body of the function. An
argument variable is any variable that appears as a formal argument to the
function. A return variable is used to track the result of a function and
has no source correspondent.

The context is either the subprogram or block where the variable is defined.
Name the source variable name. Context and line indicate where the
variable was defined. Type descriptor defines the declared type of the
variable.

This intrinsic provides information about a local element (ex. variable.) The
first argument is metadata holding alloca for the variable. The
second argument is metadata containing description of the variable.

This intrinsic provides information when a user source variable is set to a
new value. The first argument is the new value (wrapped as metadata). The
second argument is the offset in the user source variable where the new value
is written. The third argument is metadata containing description of the
user source variable.

In many languages, the local variables in functions can have their lifetimes
or scopes limited to a subset of a function. In the C family of languages,
for example, variables are only live (readable and writable) within the
source block that they are defined in. In functional languages, values are
only readable after they have been defined. Though this is a very obvious
concept, it is non-trivial to model in LLVM, because it has no notion of
scoping in this sense, and does not want to be tied to a language's scoping
rules.

In order to handle this, the LLVM debug format uses the metadata attached to
llvm instructions to encode line number and scoping information. Consider
the following C fragment, for example:

This example illustrates a few important details about LLVM debugging
information. In particular, it shows how the llvm.dbg.declare
intrinsic and location information, which are attached to an instruction,
are applied together to allow a debugger to analyze the relationship between
statements, variable definitions, and the code used to implement the
function.

call void @llvm.dbg.declare(metadata, metadata !0), !dbg !7

The first intrinsic
%llvm.dbg.declare
encodes debugging information for the variable X. The metadata
!dbg !7 attached to the intrinsic provides scope information for the
variable X.

Here !7 is metadata providing location information. It has four
fields: line number, column number, scope, and original scope. The original
scope represents inline location if this instruction is inlined inside a
caller, and is null otherwise. In this example, scope is encoded by
!1. !1 represents a lexical block inside the scope
!2, where !2 is a
subprogram descriptor. This way the
location information attached to the intrinsics indicates that the
variable X is declared at line number 2 at a function level scope in
function foo.

Now lets take another example.

call void @llvm.dbg.declare(metadata, metadata !12), !dbg !14

The second intrinsic
%llvm.dbg.declare
encodes debugging information for variable Z. The metadata
!dbg !14 attached to the intrinsic provides scope information for
the variable Z.

The C and C++ front-ends represent information about the program in a format
that is effectively identical
to DWARF 3.0 in
terms of information content. This allows code generators to trivially
support native debuggers by generating standard dwarf information, and
contains enough information for non-dwarf targets to translate it as
needed.

This section describes the forms used to represent C and C++ programs. Other
languages could pattern themselves after this (which itself is tuned to
representing programs in the same way that DWARF 3 does), or they could
choose to provide completely different forms if they don't fit into the DWARF
model. As support for debugging information gets added to the various LLVM
source-language front-ends, the information used should be documented
here.

The following sections provide examples of various C/C++ constructs and the
debug information that would best describe those constructs.

llvm::Instruction provides easy access to metadata attached with an
instruction. One can extract line number information encoded in LLVM IR
using Instruction::getMetadata() and
DILocation::getLineNumber().