rust/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
5.5 KiB
Rust
Raw Normal View History

2015-04-24 05:25:35 +00:00
// .debug_gdb_scripts binary section.
2015-04-24 03:25:42 +00:00
2019-02-17 18:58:58 +00:00
use crate::llvm;
2015-04-24 03:25:42 +00:00
2019-02-17 18:58:58 +00:00
use crate::builder::Builder;
use crate::common::CodegenCx;
use crate::value::Value;
use rustc_codegen_ssa::base::collect_debugger_visualizers_transitive;
use rustc_codegen_ssa::traits::*;
use rustc_hir::def_id::LOCAL_CRATE;
2020-03-29 15:19:48 +00:00
use rustc_middle::bug;
use rustc_session::config::{CrateType, DebugInfo};
2015-04-24 03:25:42 +00:00
2020-01-01 18:30:57 +00:00
use rustc_span::symbol::sym;
use rustc_span::DebuggerVisualizerType;
2015-04-24 03:25:42 +00:00
/// Inserts a side-effect free instruction sequence that makes sure that the
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
if needs_gdb_debug_scripts_section(bx) {
let gdb_debug_scripts_section =
bx.const_bitcast(get_or_insert_gdb_debug_scripts_section_global(bx), bx.type_i8p());
2016-12-17 01:48:25 +00:00
// Load just the first byte as that's all that's necessary to force
// LLVM to keep around the reference to the global.
let volative_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section);
2015-04-24 03:25:42 +00:00
unsafe {
llvm::LLVMSetAlignment(volative_load_instruction, 1);
2015-04-24 03:25:42 +00:00
}
}
}
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
/// section.
pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Value {
let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1];
2015-04-24 03:25:42 +00:00
let section_var =
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
2015-04-24 03:25:42 +00:00
section_var.unwrap_or_else(|| {
2015-04-24 03:25:42 +00:00
let section_name = b".debug_gdb_scripts\0";
let mut section_contents = Vec::new();
// Add the pretty printers for the standard library first.
section_contents.extend_from_slice(b"\x01gdb_load_rust_pretty_printers.py\0");
// Next, add the pretty printers that were specified via the `#[debugger_visualizer]` attribute.
let visualizers = collect_debugger_visualizers_transitive(
cx.tcx,
DebuggerVisualizerType::GdbPrettyPrinter,
);
let crate_name = cx.tcx.crate_name(LOCAL_CRATE);
for (index, visualizer) in visualizers.iter().enumerate() {
// The initial byte `4` instructs GDB that the following pretty printer
2022-05-24 18:22:14 +00:00
// is defined inline as opposed to in a standalone file.
section_contents.extend_from_slice(b"\x04");
2022-07-18 05:25:34 +00:00
let vis_name = format!("pretty-printer-{}-{}\n", crate_name, index);
section_contents.extend_from_slice(vis_name.as_bytes());
section_contents.extend_from_slice(&visualizer.src);
// The final byte `0` tells GDB that the pretty printer has been
// fully defined and can continue searching for additional
// pretty printers.
section_contents.extend_from_slice(b"\0");
}
2015-04-24 03:25:42 +00:00
unsafe {
let section_contents = section_contents.as_slice();
let llvm_type = cx.type_array(cx.type_i8(), section_contents.len() as u64);
2015-04-24 03:25:42 +00:00
let section_var = cx
.define_global(section_var_name, llvm_type)
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
2015-04-24 03:25:42 +00:00
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
2015-04-24 03:25:42 +00:00
// This should make sure that the whole section is not larger than
// the string it contains. Otherwise we get a warning from GDB.
llvm::LLVMSetAlignment(section_var, 1);
section_var
}
})
2015-04-24 03:25:42 +00:00
}
pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
2021-09-30 17:38:50 +00:00
let omit_gdb_pretty_printer_section =
cx.tcx.sess.contains_name(cx.tcx.hir().krate_attrs(), sym::omit_gdb_pretty_printer_section);
2015-04-24 03:25:42 +00:00
// To ensure the section `__rustc_debug_gdb_scripts_section__` will not create
// ODR violations at link time, this section will not be emitted for rlibs since
// each rlib could produce a different set of visualizers that would be embedded
// in the `.debug_gdb_scripts` section. For that reason, we make sure that the
// section is only emitted for leaf crates.
let embed_visualizers = cx.sess().crate_types().iter().any(|&crate_type| match crate_type {
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Staticlib => {
// These are crate types for which we will embed pretty printers since they
// are treated as leaf crates.
true
}
CrateType::ProcMacro => {
// We could embed pretty printers for proc macro crates too but it does not
// seem like a good default, since this is a rare use case and we don't
// want to slow down the common case.
false
}
CrateType::Rlib => {
2022-05-24 18:22:14 +00:00
// As per the above description, embedding pretty printers for rlibs could
// lead to ODR violations so we skip this crate type as well.
false
}
});
2015-04-24 03:25:42 +00:00
!omit_gdb_pretty_printer_section
&& cx.sess().opts.debuginfo != DebugInfo::None
&& cx.sess().target.emit_debug_gdb_scripts
&& embed_visualizers
2015-04-24 03:25:42 +00:00
}