]> git.proxmox.com Git - rustc.git/blame - src/librustc_trans/trans/debuginfo/gdb.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc_trans / trans / debuginfo / gdb.rs
CommitLineData
d9579d0f
AL
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
13use llvm;
d9579d0f 14
b039eaaf 15use trans::common::{C_bytes, CrateContext, C_i32};
d9579d0f
AL
16use trans::declare;
17use trans::type_::Type;
d9579d0f
AL
18use session::config::NoDebugInfo;
19
20use std::ffi::CString;
21use std::ptr;
b039eaaf 22use syntax::attr;
d9579d0f
AL
23
24
25/// Inserts a side-effect free instruction sequence that makes sure that the
26/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
27pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) {
28 if needs_gdb_debug_scripts_section(ccx) {
29 let empty = CString::new("").unwrap();
30 let gdb_debug_scripts_section_global =
31 get_or_insert_gdb_debug_scripts_section_global(ccx);
32 unsafe {
b039eaaf
SL
33 // Load just the first byte as that's all that's necessary to force
34 // LLVM to keep around the reference to the global.
35 let indices = [C_i32(ccx, 0), C_i32(ccx, 0)];
36 let element =
37 llvm::LLVMBuildInBoundsGEP(ccx.raw_builder(),
38 gdb_debug_scripts_section_global,
39 indices.as_ptr(),
40 indices.len() as ::libc::c_uint,
41 empty.as_ptr());
d9579d0f
AL
42 let volative_load_instruction =
43 llvm::LLVMBuildLoad(ccx.raw_builder(),
b039eaaf 44 element,
d9579d0f
AL
45 empty.as_ptr());
46 llvm::LLVMSetVolatile(volative_load_instruction, llvm::True);
b039eaaf 47 llvm::LLVMSetAlignment(volative_load_instruction, 1);
d9579d0f
AL
48 }
49 }
50}
51
52/// Allocates the global variable responsible for the .debug_gdb_scripts binary
53/// section.
54pub fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext)
55 -> llvm::ValueRef {
b039eaaf
SL
56 let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
57 let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
d9579d0f
AL
58
59 let section_var = unsafe {
60 llvm::LLVMGetNamedGlobal(ccx.llmod(),
b039eaaf 61 c_section_var_name.as_ptr() as *const _)
d9579d0f
AL
62 };
63
64 if section_var == ptr::null_mut() {
65 let section_name = b".debug_gdb_scripts\0";
66 let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
67
68 unsafe {
69 let llvm_type = Type::array(&Type::i8(ccx),
70 section_contents.len() as u64);
71
72 let section_var = declare::define_global(ccx, section_var_name,
73 llvm_type).unwrap_or_else(||{
74 ccx.sess().bug(&format!("symbol `{}` is already defined", section_var_name))
75 });
76 llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
77 llvm::LLVMSetInitializer(section_var, C_bytes(ccx, section_contents));
78 llvm::LLVMSetGlobalConstant(section_var, llvm::True);
79 llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
80 llvm::SetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
81 // This should make sure that the whole section is not larger than
82 // the string it contains. Otherwise we get a warning from GDB.
83 llvm::LLVMSetAlignment(section_var, 1);
84 section_var
85 }
86 } else {
87 section_var
88 }
89}
90
91pub fn needs_gdb_debug_scripts_section(ccx: &CrateContext) -> bool {
92 let omit_gdb_pretty_printer_section =
93 attr::contains_name(&ccx.tcx()
94 .map
95 .krate()
96 .attrs,
97 "omit_gdb_pretty_printer_section");
98
99 !omit_gdb_pretty_printer_section &&
100 !ccx.sess().target.target.options.is_like_osx &&
101 !ccx.sess().target.target.options.is_like_windows &&
102 ccx.sess().opts.debuginfo != NoDebugInfo
103}