]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/debuginfo/doc.md
fix most compiler/ doctests
[rust.git] / compiler / rustc_codegen_llvm / src / debuginfo / doc.md
1 # Debug Info Module
2
3 This module serves the purpose of generating debug symbols. We use LLVM's
4 [source level debugging](https://llvm.org/docs/SourceLevelDebugging.html)
5 features for generating the debug information. The general principle is
6 this:
7
8 Given the right metadata in the LLVM IR, the LLVM code generator is able to
9 create DWARF debug symbols for the given code. The
10 [metadata](https://llvm.org/docs/LangRef.html#metadata-type) is structured
11 much like DWARF *debugging information entries* (DIE), representing type
12 information such as datatype layout, function signatures, block layout,
13 variable location and scope information, etc. It is the purpose of this
14 module to generate correct metadata and insert it into the LLVM IR.
15
16 As the exact format of metadata trees may change between different LLVM
17 versions, we now use LLVM
18 [DIBuilder](https://llvm.org/docs/doxygen/html/classllvm_1_1DIBuilder.html)
19 to create metadata where possible. This will hopefully ease the adaption of
20 this module to future LLVM versions.
21
22 The public API of the module is a set of functions that will insert the
23 correct metadata into the LLVM IR when called with the right parameters.
24 The module is thus driven from an outside client with functions like
25 `debuginfo::create_local_var_metadata(bx: block, local: &ast::local)`.
26
27 Internally the module will try to reuse already created metadata by
28 utilizing a cache. The way to get a shared metadata node when needed is
29 thus to just call the corresponding function in this module:
30 ```ignore (illustrative)
31 let file_metadata = file_metadata(cx, file);
32 ```
33 The function will take care of probing the cache for an existing node for
34 that exact file path.
35
36 All private state used by the module is stored within either the
37 CodegenUnitDebugContext struct (owned by the CodegenCx) or the
38 FunctionDebugContext (owned by the FunctionCx).
39
40 This file consists of three conceptual sections:
41 1. The public interface of the module
42 2. Module-internal metadata creation functions
43 3. Minor utility functions
44
45
46 ## Recursive Types
47
48 Some kinds of types, such as structs and enums can be recursive. That means
49 that the type definition of some type X refers to some other type which in
50 turn (transitively) refers to X. This introduces cycles into the type
51 referral graph. A naive algorithm doing an on-demand, depth-first traversal
52 of this graph when describing types, can get trapped in an endless loop
53 when it reaches such a cycle.
54
55 For example, the following simple type for a singly-linked list...
56
57 ```
58 struct List {
59     value: i32,
60     tail: Option<Box<List>>,
61 }
62 ```
63
64 will generate the following callstack with a naive DFS algorithm:
65
66 ```ignore (illustrative)
67 describe(t = List)
68   describe(t = i32)
69   describe(t = Option<Box<List>>)
70     describe(t = Box<List>)
71       describe(t = List) // at the beginning again...
72       ...
73 ```
74
75 To break cycles like these, we use "stubs". That is, when
76 the algorithm encounters a possibly recursive type (any struct or enum), it
77 immediately creates a type description node and inserts it into the cache
78 *before* describing the members of the type. This type description is just
79 a stub (as type members are not described and added to it yet) but it
80 allows the algorithm to already refer to the type. After the stub is
81 inserted into the cache, the algorithm continues as before. If it now
82 encounters a recursive reference, it will hit the cache and does not try to
83 describe the type anew. This behavior is encapsulated in the
84 `type_map::build_type_with_children()` function.
85
86
87 ## Source Locations and Line Information
88
89 In addition to data type descriptions the debugging information must also
90 allow to map machine code locations back to source code locations in order
91 to be useful. This functionality is also handled in this module. The
92 following functions allow to control source mappings:
93
94 + `set_source_location()`
95 + `clear_source_location()`
96 + `start_emitting_source_locations()`
97
98 `set_source_location()` allows to set the current source location. All IR
99 instructions created after a call to this function will be linked to the
100 given source location, until another location is specified with
101 `set_source_location()` or the source location is cleared with
102 `clear_source_location()`. In the later case, subsequent IR instruction
103 will not be linked to any source location. As you can see, this is a
104 stateful API (mimicking the one in LLVM), so be careful with source
105 locations set by previous calls. It's probably best to not rely on any
106 specific state being present at a given point in code.
107
108 One topic that deserves some extra attention is *function prologues*. At
109 the beginning of a function's machine code there are typically a few
110 instructions for loading argument values into allocas and checking if
111 there's enough stack space for the function to execute. This *prologue* is
112 not visible in the source code and LLVM puts a special PROLOGUE END marker
113 into the line table at the first non-prologue instruction of the function.
114 In order to find out where the prologue ends, LLVM looks for the first
115 instruction in the function body that is linked to a source location. So,
116 when generating prologue instructions we have to make sure that we don't
117 emit source location information until the 'real' function body begins. For
118 this reason, source location emission is disabled by default for any new
119 function being codegened and is only activated after a call to the third
120 function from the list above, `start_emitting_source_locations()`. This
121 function should be called right before regularly starting to codegen the
122 top-level block of the given function.
123
124 There is one exception to the above rule: `llvm.dbg.declare` instruction
125 must be linked to the source location of the variable being declared. For
126 function parameters these `llvm.dbg.declare` instructions typically occur
127 in the middle of the prologue, however, they are ignored by LLVM's prologue
128 detection. The `create_argument_metadata()` and related functions take care
129 of linking the `llvm.dbg.declare` instructions to the correct source
130 locations even while source location emission is still disabled, so there
131 is no need to do anything special with source location handling here.