]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
1 | // .debug_gdb_scripts binary section. |
2 | ||
9fa01778 | 3 | use crate::llvm; |
d9579d0f | 4 | |
9fa01778 | 5 | use crate::builder::Builder; |
dfeec247 | 6 | use crate::common::CodegenCx; |
9fa01778 | 7 | use crate::value::Value; |
353b0b11 | 8 | use rustc_ast::attr; |
923072b8 | 9 | use rustc_codegen_ssa::base::collect_debugger_visualizers_transitive; |
a1dfa0c6 | 10 | use rustc_codegen_ssa::traits::*; |
923072b8 | 11 | use rustc_hir::def_id::LOCAL_CRATE; |
ba9703b0 | 12 | use rustc_middle::bug; |
923072b8 | 13 | use rustc_session::config::{CrateType, DebugInfo}; |
dfeec247 | 14 | use rustc_span::symbol::sym; |
923072b8 | 15 | use rustc_span::DebuggerVisualizerType; |
d9579d0f AL |
16 | |
17 | /// Inserts a side-effect free instruction sequence that makes sure that the | |
18 | /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker. | |
9fa01778 | 19 | pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) { |
a1dfa0c6 | 20 | if needs_gdb_debug_scripts_section(bx) { |
94222f64 XL |
21 | let gdb_debug_scripts_section = |
22 | bx.const_bitcast(get_or_insert_gdb_debug_scripts_section_global(bx), bx.type_i8p()); | |
32a655c1 SL |
23 | // Load just the first byte as that's all that's necessary to force |
24 | // LLVM to keep around the reference to the global. | |
94222f64 | 25 | let volative_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section); |
d9579d0f | 26 | unsafe { |
b039eaaf | 27 | llvm::LLVMSetAlignment(volative_load_instruction, 1); |
d9579d0f AL |
28 | } |
29 | } | |
30 | } | |
31 | ||
32 | /// Allocates the global variable responsible for the .debug_gdb_scripts binary | |
33 | /// section. | |
a2a8927a | 34 | pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Value { |
b039eaaf | 35 | let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0"; |
dfeec247 | 36 | let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1]; |
d9579d0f | 37 | |
dfeec247 XL |
38 | let section_var = |
39 | unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) }; | |
d9579d0f | 40 | |
b7449926 | 41 | section_var.unwrap_or_else(|| { |
d9579d0f | 42 | let section_name = b".debug_gdb_scripts\0"; |
923072b8 FG |
43 | let mut section_contents = Vec::new(); |
44 | ||
45 | // Add the pretty printers for the standard library first. | |
46 | section_contents.extend_from_slice(b"\x01gdb_load_rust_pretty_printers.py\0"); | |
47 | ||
48 | // Next, add the pretty printers that were specified via the `#[debugger_visualizer]` attribute. | |
49 | let visualizers = collect_debugger_visualizers_transitive( | |
50 | cx.tcx, | |
51 | DebuggerVisualizerType::GdbPrettyPrinter, | |
52 | ); | |
53 | let crate_name = cx.tcx.crate_name(LOCAL_CRATE); | |
54 | for (index, visualizer) in visualizers.iter().enumerate() { | |
55 | // The initial byte `4` instructs GDB that the following pretty printer | |
56 | // is defined inline as opposed to in a standalone file. | |
57 | section_contents.extend_from_slice(b"\x04"); | |
064997fb | 58 | let vis_name = format!("pretty-printer-{}-{}\n", crate_name, index); |
923072b8 FG |
59 | section_contents.extend_from_slice(vis_name.as_bytes()); |
60 | section_contents.extend_from_slice(&visualizer.src); | |
61 | ||
62 | // The final byte `0` tells GDB that the pretty printer has been | |
63 | // fully defined and can continue searching for additional | |
64 | // pretty printers. | |
65 | section_contents.extend_from_slice(b"\0"); | |
66 | } | |
d9579d0f AL |
67 | |
68 | unsafe { | |
923072b8 | 69 | let section_contents = section_contents.as_slice(); |
dfeec247 | 70 | let llvm_type = cx.type_array(cx.type_i8(), section_contents.len() as u64); |
d9579d0f | 71 | |
dfeec247 XL |
72 | let section_var = cx |
73 | .define_global(section_var_name, llvm_type) | |
74 | .unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name)); | |
e74abb32 | 75 | llvm::LLVMSetSection(section_var, section_name.as_ptr().cast()); |
a1dfa0c6 | 76 | llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents)); |
d9579d0f | 77 | llvm::LLVMSetGlobalConstant(section_var, llvm::True); |
ba9703b0 | 78 | llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global); |
9e0c209e | 79 | llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage); |
d9579d0f AL |
80 | // This should make sure that the whole section is not larger than |
81 | // the string it contains. Otherwise we get a warning from GDB. | |
82 | llvm::LLVMSetAlignment(section_var, 1); | |
83 | section_var | |
84 | } | |
b7449926 | 85 | }) |
d9579d0f AL |
86 | } |
87 | ||
9fa01778 | 88 | pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool { |
c295e0f8 | 89 | let omit_gdb_pretty_printer_section = |
353b0b11 | 90 | attr::contains_name(cx.tcx.hir().krate_attrs(), sym::omit_gdb_pretty_printer_section); |
d9579d0f | 91 | |
923072b8 FG |
92 | // To ensure the section `__rustc_debug_gdb_scripts_section__` will not create |
93 | // ODR violations at link time, this section will not be emitted for rlibs since | |
94 | // each rlib could produce a different set of visualizers that would be embedded | |
95 | // in the `.debug_gdb_scripts` section. For that reason, we make sure that the | |
96 | // section is only emitted for leaf crates. | |
97 | let embed_visualizers = cx.sess().crate_types().iter().any(|&crate_type| match crate_type { | |
98 | CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Staticlib => { | |
99 | // These are crate types for which we will embed pretty printers since they | |
100 | // are treated as leaf crates. | |
101 | true | |
102 | } | |
103 | CrateType::ProcMacro => { | |
104 | // We could embed pretty printers for proc macro crates too but it does not | |
105 | // seem like a good default, since this is a rare use case and we don't | |
106 | // want to slow down the common case. | |
107 | false | |
108 | } | |
109 | CrateType::Rlib => { | |
110 | // As per the above description, embedding pretty printers for rlibs could | |
111 | // lead to ODR violations so we skip this crate type as well. | |
112 | false | |
113 | } | |
114 | }); | |
115 | ||
dfeec247 XL |
116 | !omit_gdb_pretty_printer_section |
117 | && cx.sess().opts.debuginfo != DebugInfo::None | |
29967ef6 | 118 | && cx.sess().target.emit_debug_gdb_scripts |
923072b8 | 119 | && embed_visualizers |
d9579d0f | 120 | } |