From 78caecf8f30dbdbfcb6e0fda25edc72b3e4d04a5 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Sat, 3 Aug 2024 21:04:19 -0700 Subject: [PATCH 1/5] Special case DUMMY_SP to emit line 0/column 0 locations on DWARF platforms. Line 0 has a special meaning in DWARF. From the version 5 spec: The compiler may emit the value 0 in cases where an instruction cannot be attributed to any source line. DUMMY_SP spans cannot be attributed to any line. However, because rustc internally stores line numbers starting at zero, lookup_debug_loc() adjusts every line number by one. Special casing DUMMY_SP to actually emit line 0 ensures rustc communicates to the debugger that there's no meaningful source code for this instruction, rather than telling the debugger to jump to line 1 randomly. --- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index b23e05182ca1b..3706d31e66e17 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -20,7 +20,7 @@ use rustc_session::config::{self, DebugInfo}; use rustc_session::Session; use rustc_span::symbol::Symbol; use rustc_span::{ - BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, + BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, DUMMY_SP, }; use rustc_target::abi::Size; use smallvec::SmallVec; @@ -570,7 +570,12 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { inlined_at: Option<&'ll DILocation>, span: Span, ) -> &'ll DILocation { - let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo()); + let (line, col) = if span == DUMMY_SP && !self.sess().target.is_like_msvc { + (0, 0) + } else { + let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo()); + (line, col) + }; unsafe { llvm::LLVMRustDIBuilderCreateDebugLocation(line, col, scope, inlined_at) } } From e5878555387e4ced52b3dd6c5f6a04a5f47eeed7 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Sun, 4 Aug 2024 05:26:50 -0700 Subject: [PATCH 2/5] Use Span::is_dummy(). --- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 3706d31e66e17..57b8fb2fe71b8 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -570,7 +570,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { inlined_at: Option<&'ll DILocation>, span: Span, ) -> &'ll DILocation { - let (line, col) = if span == DUMMY_SP && !self.sess().target.is_like_msvc { + let (line, col) = if span.is_dummy() && !self.sess().target.is_like_msvc { (0, 0) } else { let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo()); From 5dc4a1969c6ea5ba0b1d11f9d43045ed5a2be5cc Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Sun, 4 Aug 2024 06:09:55 -0700 Subject: [PATCH 3/5] Fix warning. --- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 57b8fb2fe71b8..66dd653bb2166 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -20,7 +20,7 @@ use rustc_session::config::{self, DebugInfo}; use rustc_session::Session; use rustc_span::symbol::Symbol; use rustc_span::{ - BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, DUMMY_SP, + BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, }; use rustc_target::abi::Size; use smallvec::SmallVec; From 3c735a00f70c8c3df737eb18494e9eb9c6196971 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Mon, 19 Aug 2024 17:10:37 -0700 Subject: [PATCH 4/5] Add a test. --- tests/debuginfo/dummy_span.rs | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/debuginfo/dummy_span.rs diff --git a/tests/debuginfo/dummy_span.rs b/tests/debuginfo/dummy_span.rs new file mode 100644 index 0000000000000..d02eead470fa1 --- /dev/null +++ b/tests/debuginfo/dummy_span.rs @@ -0,0 +1,45 @@ +//@ min-lldb-version: 310 + +//@ compile-flags:-g + +// === GDB TESTS =================================================================================== + +// gdb-command:run 7 + +// gdb-command:next +// gdb-command:next +// gdb-check:[...]#loc1[...] +// gdb-command:next +// gdb-check:[...]#loc2[...] + +// === LLDB TESTS ================================================================================== + +// lldb-command:run 7 + +// lldb-command:next +// lldb-command:next +// lldb-command:frame select +// lldb-check:[...]#loc1[...] +// lldb-command:next +// lldb-command:frame select +// lldb-check:[...]#loc2[...] + +use std::env; +use std::num::ParseIntError; + +fn main() -> Result<(), ParseIntError> { + let args = env::args(); + let number_str = args.skip(1).next().unwrap(); + let number = number_str.parse::()?; + zzz(); // #break + if number % 7 == 0 { + // This generates code with a dummy span for + // some reason. If that ever changes this + // test will not test what it wants to test. + return Ok(()); // #loc1 + } + println!("{}", number); + Ok(()) +} // #loc2 + +fn zzz() { () } From 4e9725cd2f5327f65f881c59d6bcbae46308318e Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Mon, 19 Aug 2024 17:13:30 -0700 Subject: [PATCH 5/5] Add a comment. --- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 66dd653bb2166..3fbaebe28aaec 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -570,6 +570,11 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { inlined_at: Option<&'ll DILocation>, span: Span, ) -> &'ll DILocation { + // When emitting debugging information, DWARF (i.e. everything but MSVC) + // treats line 0 as a magic value meaning that the code could not be + // attributed to any line in the source. That's also exactly what dummy + // spans are. Make that equivalence here, rather than passing dummy spans + // to lookup_debug_loc, which will return line 1 for them. let (line, col) = if span.is_dummy() && !self.sess().target.is_like_msvc { (0, 0) } else {