Skip to content

Commit 154e158

Browse files
authored
Merge pull request #1609 from rust-lang/line_table_only_debuginfo
Only generate line tables with -Cdebuginfo=line-table-only
2 parents fd6e372 + 8b0e675 commit 154e158

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

src/debuginfo/line_info.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Component, Path};
55

66
use cranelift_codegen::MachSrcLoc;
77
use cranelift_codegen::binemit::CodeOffset;
8-
use gimli::write::{AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable};
8+
use gimli::write::{FileId, FileInfo, LineProgram, LineString, LineStringTable};
99
use rustc_span::{FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHashAlgorithm, hygiene};
1010

1111
use crate::debuginfo::FunctionDebugContext;
@@ -117,8 +117,7 @@ impl DebugContext {
117117
}
118118
filename => {
119119
// For anonymous sources, create an empty directory instead of using the default
120-
let empty_dir = LineString::new(b"", line_program.encoding(), line_strings);
121-
let dir_id = line_program.add_directory(empty_dir);
120+
let dir_id = line_program.default_directory();
122121

123122
let dummy_file_name = LineString::new(
124123
filename.prefer_remapped_unconditionally().to_string().into_bytes(),
@@ -176,10 +175,6 @@ impl FunctionDebugContext {
176175

177176
assert_ne!(func_end, 0);
178177

179-
let entry = debug_context.dwarf.unit.get_mut(self.entry_id);
180-
entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
181-
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end)));
182-
183178
func_end
184179
}
185180
}

src/debuginfo/mod.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ pub(crate) struct DebugContext {
4242
created_files: FxHashMap<(StableSourceFileId, SourceFileHash), FileId>,
4343
stack_pointer_register: Register,
4444
namespace_map: DefIdMap<UnitEntryId>,
45-
array_size_type: UnitEntryId,
45+
array_size_type: Option<UnitEntryId>,
4646

4747
filename_display_preference: FileNameDisplayPreference,
4848
embed_source: bool,
4949
}
5050

5151
pub(crate) struct FunctionDebugContext {
52-
entry_id: UnitEntryId,
52+
entry_id: Option<UnitEntryId>,
5353
function_source_loc: (FileId, u64, u64),
5454
source_loc_set: IndexSet<(FileId, u64, u64)>,
5555
}
@@ -154,18 +154,23 @@ impl DebugContext {
154154
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
155155
}
156156

157-
let array_size_type = dwarf.unit.add(dwarf.unit.root(), gimli::DW_TAG_base_type);
158-
let array_size_type_entry = dwarf.unit.get_mut(array_size_type);
159-
array_size_type_entry.set(
160-
gimli::DW_AT_name,
161-
AttributeValue::StringRef(dwarf.strings.add("__ARRAY_SIZE_TYPE__")),
162-
);
163-
array_size_type_entry
164-
.set(gimli::DW_AT_encoding, AttributeValue::Encoding(gimli::DW_ATE_unsigned));
165-
array_size_type_entry.set(
166-
gimli::DW_AT_byte_size,
167-
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
168-
);
157+
let array_size_type = if tcx.sess.opts.debuginfo == DebugInfo::LineTablesOnly {
158+
None
159+
} else {
160+
let array_size_type = dwarf.unit.add(dwarf.unit.root(), gimli::DW_TAG_base_type);
161+
let array_size_type_entry = dwarf.unit.get_mut(array_size_type);
162+
array_size_type_entry.set(
163+
gimli::DW_AT_name,
164+
AttributeValue::StringRef(dwarf.strings.add("__ARRAY_SIZE_TYPE__")),
165+
);
166+
array_size_type_entry
167+
.set(gimli::DW_AT_encoding, AttributeValue::Encoding(gimli::DW_ATE_unsigned));
168+
array_size_type_entry.set(
169+
gimli::DW_AT_byte_size,
170+
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
171+
);
172+
Some(array_size_type)
173+
};
169174

170175
Some(DebugContext {
171176
endian,
@@ -217,6 +222,14 @@ impl DebugContext {
217222
) -> FunctionDebugContext {
218223
let (file_id, line, column) = self.get_span_loc(tcx, function_span, function_span);
219224

225+
if tcx.sess.opts.debuginfo == DebugInfo::LineTablesOnly {
226+
return FunctionDebugContext {
227+
entry_id: None,
228+
function_source_loc: (file_id, line, column),
229+
source_loc_set: IndexSet::new(),
230+
};
231+
}
232+
220233
let scope = self.item_namespace(tcx, tcx.parent(instance.def_id()));
221234

222235
let mut name = String::new();
@@ -274,7 +287,7 @@ impl DebugContext {
274287
}
275288

276289
FunctionDebugContext {
277-
entry_id,
290+
entry_id: Some(entry_id),
278291
function_source_loc: (file_id, line, column),
279292
source_loc_set: IndexSet::new(),
280293
}
@@ -288,6 +301,10 @@ impl DebugContext {
288301
def_id: DefId,
289302
data_id: DataId,
290303
) {
304+
if tcx.sess.opts.debuginfo == DebugInfo::LineTablesOnly {
305+
return;
306+
}
307+
291308
let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else { bug!() };
292309
if nested {
293310
return;
@@ -353,10 +370,12 @@ impl FunctionDebugContext {
353370
.0
354371
.push(Range::StartLength { begin: address_for_func(func_id), length: u64::from(end) });
355372

356-
let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
357-
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
358-
func_entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
359-
// Using Udata for DW_AT_high_pc requires at least DWARF4
360-
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
373+
if let Some(entry_id) = self.entry_id {
374+
let entry = debug_context.dwarf.unit.get_mut(entry_id);
375+
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
376+
entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
377+
// Using Udata for DW_AT_high_pc requires at least DWARF4
378+
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
379+
}
361380
}
362381
}

src/debuginfo/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ impl DebugContext {
109109

110110
let subrange_id = self.dwarf.unit.add(array_type_id, gimli::DW_TAG_subrange_type);
111111
let subrange_entry = self.dwarf.unit.get_mut(subrange_id);
112-
subrange_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type));
112+
subrange_entry
113+
.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type.unwrap()));
113114
subrange_entry.set(gimli::DW_AT_lower_bound, AttributeValue::Udata(0));
114115
subrange_entry.set(gimli::DW_AT_count, AttributeValue::Udata(len));
115116

0 commit comments

Comments
 (0)