Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debuginfo: embed gdb visualizers in module #1462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/debuginfo/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ impl DebugContext {
}
Ok(())
});

self.add_gdb_debugger_visualizers(product);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs to be called at

fn create_entry_fn(
and a load of the produced global needs to be inserted there too to ensure the section only gets inserted a single time in the whole program and that it doesn't get removed by the linker.

}

fn add_gdb_debugger_visualizers(&mut self, product: &mut ObjectProduct) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut section = WriterRelocate::new(self.endian);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to go through WriterRelocate here. A plain Vec is enough. There will be no relocations inside this section.

section.write(b"\x01gdb_load_rust_pretty_printers.py\0").unwrap();

for (index, visualizer) in self.gdb_visualizers.iter().enumerate() {
let vis_name = format!("\x04pretty-printer-{}-{index}\n", self.crate_name);

section.write(vis_name.as_bytes()).unwrap();
section.write(&visualizer.src).unwrap();

section.write(b"\0").unwrap();
}
product.add_gdb_visualizers_section(section.writer.take());
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod line_info;
mod object;
mod unwind;

use std::collections::BTreeSet;

use cranelift_codegen::ir::Endianness;
use cranelift_codegen::isa::TargetIsa;
use gimli::write::{
Expand All @@ -13,6 +15,8 @@ use gimli::write::{
};
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
use indexmap::IndexSet;
use rustc_codegen_ssa::base::collect_debugger_visualizers_transitive;
use rustc_middle::middle::debugger_visualizer::{DebuggerVisualizerFile, DebuggerVisualizerType};
use rustc_session::Session;

pub(crate) use self::emit::{DebugReloc, DebugRelocName};
Expand All @@ -30,6 +34,8 @@ pub(crate) struct DebugContext {
unit_range_list: RangeList,

should_remap_filepaths: bool,
gdb_visualizers: BTreeSet<DebuggerVisualizerFile>,
crate_name: String,
}

pub(crate) struct FunctionDebugContext {
Expand Down Expand Up @@ -60,6 +66,10 @@ impl DebugContext {
Endianness::Big => RunTimeEndian::Big,
};

let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
let gdb_visualizers =
collect_debugger_visualizers_transitive(tcx, DebuggerVisualizerType::GdbPrettyPrinter);

let mut dwarf = DwarfUnit::new(encoding);

let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
Expand All @@ -80,7 +90,7 @@ impl DebugContext {
let name = path.to_string_lossy().into_owned();
(name, None)
}
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
None => (crate_name.clone(), None),
};

let mut line_program = LineProgram::new(
Expand Down Expand Up @@ -112,6 +122,8 @@ impl DebugContext {
dwarf,
unit_range_list: RangeList(Vec::new()),
should_remap_filepaths,
gdb_visualizers,
crate_name
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/debuginfo/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(super) trait WriteDebugInfo {
type SectionId: Copy;

fn add_debug_section(&mut self, name: SectionId, data: Vec<u8>) -> Self::SectionId;
fn add_gdb_visualizers_section(&mut self, data: Vec<u8>) -> Self::SectionId;
fn add_debug_reloc(
&mut self,
section_map: &FxHashMap<SectionId, Self::SectionId>,
Expand All @@ -22,6 +23,18 @@ pub(super) trait WriteDebugInfo {
impl WriteDebugInfo for ObjectProduct {
type SectionId = (object::write::SectionId, object::write::SymbolId);

fn add_gdb_visualizers_section(
&mut self,
data: Vec<u8>,
) -> (object::write::SectionId, object::write::SymbolId) {
let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
let section_id =
self.object.add_section(segment, b".debug_gdb_scripts".to_vec(), SectionKind::Debug);
self.object.section_mut(section_id).set_data(data, 1);
let symbol_id = self.object.section_symbol(section_id);
(section_id, symbol_id)
}

fn add_debug_section(
&mut self,
id: SectionId,
Expand Down