@@ -5,14 +5,12 @@ use std::path::{Component, Path};
5
5
6
6
use cranelift_codegen:: binemit:: CodeOffset ;
7
7
use cranelift_codegen:: MachSrcLoc ;
8
- use gimli:: write:: {
9
- Address , AttributeValue , FileId , FileInfo , LineProgram , LineString , LineStringTable ,
10
- } ;
11
- use rustc_data_structures:: sync:: Lrc ;
8
+ use gimli:: write:: { AttributeValue , FileId , FileInfo , LineProgram , LineString , LineStringTable } ;
12
9
use rustc_span:: {
13
10
FileName , Pos , SourceFile , SourceFileAndLine , SourceFileHash , SourceFileHashAlgorithm ,
14
11
} ;
15
12
13
+ use crate :: debuginfo:: emit:: address_for_func;
16
14
use crate :: debuginfo:: FunctionDebugContext ;
17
15
use crate :: prelude:: * ;
18
16
@@ -60,72 +58,78 @@ fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
60
58
61
59
impl DebugContext {
62
60
pub ( crate ) fn get_span_loc (
61
+ & mut self ,
63
62
tcx : TyCtxt < ' _ > ,
64
63
function_span : Span ,
65
64
span : Span ,
66
- ) -> ( Lrc < SourceFile > , u64 , u64 ) {
65
+ ) -> ( FileId , u64 , u64 ) {
67
66
// Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
68
67
// In order to have a good line stepping behavior in debugger, we overwrite debug
69
68
// locations of macro expansions with that of the outermost expansion site (when the macro is
70
69
// annotated with `#[collapse_debuginfo]` or when `-Zdebug-macros` is provided).
71
70
let span = tcx. collapsed_debuginfo ( span, function_span) ;
72
71
match tcx. sess . source_map ( ) . lookup_line ( span. lo ( ) ) {
73
72
Ok ( SourceFileAndLine { sf : file, line } ) => {
73
+ let file_id = self . add_source_file ( & file) ;
74
74
let line_pos = file. lines ( ) [ line] ;
75
75
let col = file. relative_position ( span. lo ( ) ) - line_pos;
76
76
77
- ( file , u64:: try_from ( line) . unwrap ( ) + 1 , u64:: from ( col. to_u32 ( ) ) + 1 )
77
+ ( file_id , u64:: try_from ( line) . unwrap ( ) + 1 , u64:: from ( col. to_u32 ( ) ) + 1 )
78
78
}
79
- Err ( file) => ( file, 0 , 0 ) ,
79
+ Err ( file) => ( self . add_source_file ( & file) , 0 , 0 ) ,
80
80
}
81
81
}
82
82
83
83
pub ( crate ) fn add_source_file ( & mut self , source_file : & SourceFile ) -> FileId {
84
- let line_program: & mut LineProgram = & mut self . dwarf . unit . line_program ;
85
- let line_strings: & mut LineStringTable = & mut self . dwarf . line_strings ;
86
-
87
- match & source_file. name {
88
- FileName :: Real ( path) => {
89
- let ( dir_path, file_name) =
90
- split_path_dir_and_file ( if self . should_remap_filepaths {
91
- path. remapped_path_if_available ( )
92
- } else {
93
- path. local_path_if_available ( )
94
- } ) ;
95
- let dir_name = osstr_as_utf8_bytes ( dir_path. as_os_str ( ) ) ;
96
- let file_name = osstr_as_utf8_bytes ( file_name) ;
97
-
98
- let dir_id = if !dir_name. is_empty ( ) {
99
- let dir_name = LineString :: new ( dir_name, line_program. encoding ( ) , line_strings) ;
100
- line_program. add_directory ( dir_name)
101
- } else {
102
- line_program. default_directory ( )
103
- } ;
104
- let file_name = LineString :: new ( file_name, line_program. encoding ( ) , line_strings) ;
105
-
106
- let info = make_file_info ( source_file. src_hash ) ;
107
-
108
- line_program. file_has_md5 &= info. is_some ( ) ;
109
- line_program. add_file ( file_name, dir_id, info)
110
- }
111
- // FIXME give more appropriate file names
112
- filename => {
113
- let dir_id = line_program. default_directory ( ) ;
114
- let dummy_file_name = LineString :: new (
115
- filename
116
- . display ( if self . should_remap_filepaths {
117
- FileNameDisplayPreference :: Remapped
84
+ let cache_key = ( source_file. stable_id , source_file. src_hash ) ;
85
+ * self . created_files . entry ( cache_key) . or_insert_with ( || {
86
+ let line_program: & mut LineProgram = & mut self . dwarf . unit . line_program ;
87
+ let line_strings: & mut LineStringTable = & mut self . dwarf . line_strings ;
88
+
89
+ match & source_file. name {
90
+ FileName :: Real ( path) => {
91
+ let ( dir_path, file_name) =
92
+ split_path_dir_and_file ( if self . should_remap_filepaths {
93
+ path. remapped_path_if_available ( )
118
94
} else {
119
- FileNameDisplayPreference :: Local
120
- } )
121
- . to_string ( )
122
- . into_bytes ( ) ,
123
- line_program. encoding ( ) ,
124
- line_strings,
125
- ) ;
126
- line_program. add_file ( dummy_file_name, dir_id, None )
95
+ path. local_path_if_available ( )
96
+ } ) ;
97
+ let dir_name = osstr_as_utf8_bytes ( dir_path. as_os_str ( ) ) ;
98
+ let file_name = osstr_as_utf8_bytes ( file_name) ;
99
+
100
+ let dir_id = if !dir_name. is_empty ( ) {
101
+ let dir_name =
102
+ LineString :: new ( dir_name, line_program. encoding ( ) , line_strings) ;
103
+ line_program. add_directory ( dir_name)
104
+ } else {
105
+ line_program. default_directory ( )
106
+ } ;
107
+ let file_name =
108
+ LineString :: new ( file_name, line_program. encoding ( ) , line_strings) ;
109
+
110
+ let info = make_file_info ( source_file. src_hash ) ;
111
+
112
+ line_program. file_has_md5 &= info. is_some ( ) ;
113
+ line_program. add_file ( file_name, dir_id, info)
114
+ }
115
+ filename => {
116
+ let dir_id = line_program. default_directory ( ) ;
117
+ let dummy_file_name = LineString :: new (
118
+ filename
119
+ . display ( if self . should_remap_filepaths {
120
+ FileNameDisplayPreference :: Remapped
121
+ } else {
122
+ FileNameDisplayPreference :: Local
123
+ } )
124
+ . to_string ( )
125
+ . into_bytes ( ) ,
126
+ line_program. encoding ( ) ,
127
+ line_strings,
128
+ ) ;
129
+ line_program. add_file ( dummy_file_name, dir_id, None )
130
+ }
127
131
}
128
- }
132
+ } )
129
133
}
130
134
}
131
135
@@ -138,7 +142,7 @@ impl FunctionDebugContext {
138
142
pub ( super ) fn create_debug_lines (
139
143
& mut self ,
140
144
debug_context : & mut DebugContext ,
141
- symbol : usize ,
145
+ func_id : FuncId ,
142
146
context : & Context ,
143
147
) -> CodeOffset {
144
148
let create_row_for_span =
@@ -151,11 +155,7 @@ impl FunctionDebugContext {
151
155
debug_context. dwarf . unit . line_program . generate_row ( ) ;
152
156
} ;
153
157
154
- debug_context
155
- . dwarf
156
- . unit
157
- . line_program
158
- . begin_sequence ( Some ( Address :: Symbol { symbol, addend : 0 } ) ) ;
158
+ debug_context. dwarf . unit . line_program . begin_sequence ( Some ( address_for_func ( func_id) ) ) ;
159
159
160
160
let mut func_end = 0 ;
161
161
@@ -178,10 +178,7 @@ impl FunctionDebugContext {
178
178
assert_ne ! ( func_end, 0 ) ;
179
179
180
180
let entry = debug_context. dwarf . unit . get_mut ( self . entry_id ) ;
181
- entry. set (
182
- gimli:: DW_AT_low_pc ,
183
- AttributeValue :: Address ( Address :: Symbol { symbol, addend : 0 } ) ,
184
- ) ;
181
+ entry. set ( gimli:: DW_AT_low_pc , AttributeValue :: Address ( address_for_func ( func_id) ) ) ;
185
182
entry. set ( gimli:: DW_AT_high_pc , AttributeValue :: Udata ( u64:: from ( func_end) ) ) ;
186
183
187
184
func_end
0 commit comments