Skip to content

Commit 5753c8d

Browse files
committed
debuginfo: extract gdb.rs
1 parent 5993ae8 commit 5753c8d

File tree

3 files changed

+101
-80
lines changed

3 files changed

+101
-80
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ pub fn create_entry_wrapper(ccx: &CrateContext,
21822182
unsafe {
21832183
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
21842184

2185-
debuginfo::insert_reference_to_gdb_debug_scripts_section_global(ccx);
2185+
debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(ccx);
21862186

21872187
let (start_fn, args) = if use_start_lang_item {
21882188
let start_def_id = match ccx.tcx().lang_items.require(StartFnLangItem) {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://!rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://!www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://!opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// .debug_gdb_scripts binary section
12+
13+
use llvm;
14+
use llvm::ValueRef;
15+
16+
use trans::common::{C_bytes, CrateContext};
17+
use trans::declare;
18+
use trans::type_::Type;
19+
use middle::ty::ClosureTyper;
20+
use session::config::NoDebugInfo;
21+
22+
use std::ffi::CString;
23+
use std::ptr;
24+
use syntax::attr;
25+
26+
27+
/// Inserts a side-effect free instruction sequence that makes sure that the
28+
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
29+
pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) {
30+
if needs_gdb_debug_scripts_section(ccx) {
31+
let empty = CString::new("").unwrap();
32+
let gdb_debug_scripts_section_global =
33+
get_or_insert_gdb_debug_scripts_section_global(ccx);
34+
unsafe {
35+
let volative_load_instruction =
36+
llvm::LLVMBuildLoad(ccx.raw_builder(),
37+
gdb_debug_scripts_section_global,
38+
empty.as_ptr());
39+
llvm::LLVMSetVolatile(volative_load_instruction, llvm::True);
40+
}
41+
}
42+
}
43+
44+
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
45+
/// section.
46+
pub fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext)
47+
-> llvm::ValueRef {
48+
let section_var_name = "__rustc_debug_gdb_scripts_section__";
49+
50+
let section_var = unsafe {
51+
llvm::LLVMGetNamedGlobal(ccx.llmod(),
52+
section_var_name.as_ptr() as *const _)
53+
};
54+
55+
if section_var == ptr::null_mut() {
56+
let section_name = b".debug_gdb_scripts\0";
57+
let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
58+
59+
unsafe {
60+
let llvm_type = Type::array(&Type::i8(ccx),
61+
section_contents.len() as u64);
62+
63+
let section_var = declare::define_global(ccx, section_var_name,
64+
llvm_type).unwrap_or_else(||{
65+
ccx.sess().bug(&format!("symbol `{}` is already defined", section_var_name))
66+
});
67+
llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
68+
llvm::LLVMSetInitializer(section_var, C_bytes(ccx, section_contents));
69+
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
70+
llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
71+
llvm::SetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
72+
// This should make sure that the whole section is not larger than
73+
// the string it contains. Otherwise we get a warning from GDB.
74+
llvm::LLVMSetAlignment(section_var, 1);
75+
section_var
76+
}
77+
} else {
78+
section_var
79+
}
80+
}
81+
82+
pub fn needs_gdb_debug_scripts_section(ccx: &CrateContext) -> bool {
83+
let omit_gdb_pretty_printer_section =
84+
attr::contains_name(&ccx.tcx()
85+
.map
86+
.krate()
87+
.attrs,
88+
"omit_gdb_pretty_printer_section");
89+
90+
!omit_gdb_pretty_printer_section &&
91+
!ccx.sess().target.target.options.is_like_osx &&
92+
!ccx.sess().target.target.options.is_like_windows &&
93+
ccx.sess().opts.debuginfo != NoDebugInfo
94+
}

src/librustc_trans/trans/debuginfo/mod.rs

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// See doc.rs for documentation.
1212
mod doc;
1313

14+
pub mod gdb;
15+
1416
use self::VariableAccess::*;
1517
use self::VariableKind::*;
1618
use self::MemberOffset::*;
@@ -25,9 +27,8 @@ use llvm::debuginfo::*;
2527
use metadata::csearch;
2628
use middle::subst::{self, Substs};
2729
use trans::{self, adt, machine, type_of};
28-
use trans::common::{self, NodeIdAndSpan, CrateContext, FunctionContext, Block, C_bytes,
30+
use trans::common::{self, NodeIdAndSpan, CrateContext, FunctionContext, Block,
2931
NormalizingClosureTyper};
30-
use trans::declare;
3132
use trans::_match::{BindingInfo, TrByCopy, TrByMove, TrByRef};
3233
use trans::monomorphize;
3334
use trans::type_::Type;
@@ -46,7 +47,7 @@ use std::ptr;
4647
use std::rc::{Rc, Weak};
4748
use syntax::util::interner::Interner;
4849
use syntax::codemap::{Span, Pos};
49-
use syntax::{ast, codemap, ast_util, ast_map, attr};
50+
use syntax::{ast, codemap, ast_util, ast_map};
5051
use syntax::parse::token::{self, special_idents};
5152

5253
const DW_LANG_RUST: c_uint = 0x9000;
@@ -552,12 +553,12 @@ pub fn finalize(cx: &CrateContext) {
552553
debug!("finalize");
553554
let _ = compile_unit_metadata(cx);
554555

555-
if needs_gdb_debug_scripts_section(cx) {
556+
if gdb::needs_gdb_debug_scripts_section(cx) {
556557
// Add a .debug_gdb_scripts section to this compile-unit. This will
557558
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
558559
// which activates the Rust pretty printers for binary this section is
559560
// contained in.
560-
get_or_insert_gdb_debug_scripts_section_global(cx);
561+
gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
561562
}
562563

563564
unsafe {
@@ -3866,77 +3867,3 @@ fn namespace_for_item(cx: &CrateContext, def_id: ast::DefId) -> Rc<NamespaceTree
38663867
}
38673868
})
38683869
}
3869-
3870-
3871-
//=-----------------------------------------------------------------------------
3872-
// .debug_gdb_scripts binary section
3873-
//=-----------------------------------------------------------------------------
3874-
3875-
/// Inserts a side-effect free instruction sequence that makes sure that the
3876-
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
3877-
pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) {
3878-
if needs_gdb_debug_scripts_section(ccx) {
3879-
let empty = CString::new("").unwrap();
3880-
let gdb_debug_scripts_section_global =
3881-
get_or_insert_gdb_debug_scripts_section_global(ccx);
3882-
unsafe {
3883-
let volative_load_instruction =
3884-
llvm::LLVMBuildLoad(ccx.raw_builder(),
3885-
gdb_debug_scripts_section_global,
3886-
empty.as_ptr());
3887-
llvm::LLVMSetVolatile(volative_load_instruction, llvm::True);
3888-
}
3889-
}
3890-
}
3891-
3892-
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
3893-
/// section.
3894-
fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext)
3895-
-> llvm::ValueRef {
3896-
let section_var_name = "__rustc_debug_gdb_scripts_section__";
3897-
3898-
let section_var = unsafe {
3899-
llvm::LLVMGetNamedGlobal(ccx.llmod(),
3900-
section_var_name.as_ptr() as *const _)
3901-
};
3902-
3903-
if section_var == ptr::null_mut() {
3904-
let section_name = b".debug_gdb_scripts\0";
3905-
let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
3906-
3907-
unsafe {
3908-
let llvm_type = Type::array(&Type::i8(ccx),
3909-
section_contents.len() as u64);
3910-
3911-
let section_var = declare::define_global(ccx, section_var_name,
3912-
llvm_type).unwrap_or_else(||{
3913-
ccx.sess().bug(&format!("symbol `{}` is already defined", section_var_name))
3914-
});
3915-
llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
3916-
llvm::LLVMSetInitializer(section_var, C_bytes(ccx, section_contents));
3917-
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
3918-
llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
3919-
llvm::SetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
3920-
// This should make sure that the whole section is not larger than
3921-
// the string it contains. Otherwise we get a warning from GDB.
3922-
llvm::LLVMSetAlignment(section_var, 1);
3923-
section_var
3924-
}
3925-
} else {
3926-
section_var
3927-
}
3928-
}
3929-
3930-
fn needs_gdb_debug_scripts_section(ccx: &CrateContext) -> bool {
3931-
let omit_gdb_pretty_printer_section =
3932-
attr::contains_name(&ccx.tcx()
3933-
.map
3934-
.krate()
3935-
.attrs,
3936-
"omit_gdb_pretty_printer_section");
3937-
3938-
!omit_gdb_pretty_printer_section &&
3939-
!ccx.sess().target.target.options.is_like_osx &&
3940-
!ccx.sess().target.target.options.is_like_windows &&
3941-
ccx.sess().opts.debuginfo != NoDebugInfo
3942-
}

0 commit comments

Comments
 (0)