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

refactor(base): add stacktrace to replace backtrace #16643

Merged
merged 100 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 95 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
726edbd
refactor(base): lock free backtrace capture
zhang2014 Oct 13, 2024
3d16acf
refactor(base): lock free backtrace capture
zhang2014 Oct 17, 2024
89e2841
refactor(base): lock free backtrace capture
zhang2014 Oct 18, 2024
7ecb75d
refactor(base): lock free backtrace capture
zhang2014 Oct 18, 2024
6e9089f
refactor(base): lock free backtrace capture
zhang2014 Oct 19, 2024
611f920
refactor(base): lock free backtrace capture
zhang2014 Oct 19, 2024
795a99c
refactor(base): lock free backtrace capture
zhang2014 Oct 19, 2024
a895532
refactor(base): lock free backtrace capture
zhang2014 Oct 19, 2024
bfe72d3
refactor(base): make lint for macos
zhang2014 Oct 19, 2024
b46e40e
refactor(base): make lint for macos
zhang2014 Oct 19, 2024
86fec21
refactor(base): make lint for linux
zhang2014 Oct 19, 2024
fecb3f1
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Oct 19, 2024
3393c6d
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Oct 21, 2024
fbaf436
refactor(base): lock free backtrace capture
zhang2014 Oct 21, 2024
ddc8964
refactor(base): lock free backtrace capture
zhang2014 Oct 21, 2024
8bc3081
refactor(base): add display text cache
zhang2014 Oct 21, 2024
8806ccc
refactor(base): lock free backtrace capture
zhang2014 Oct 22, 2024
184a81c
refactor(base): lock free backtrace capture
zhang2014 Oct 22, 2024
baaee98
refactor(base): lock free backtrace capture
zhang2014 Oct 22, 2024
150dcef
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
a2d9493
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
a466d66
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
5bba03e
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
81cd75d
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
3de5d85
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
4998a9c
refactor(base): lock free backtrace capture
zhang2014 Oct 23, 2024
b60dceb
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Oct 24, 2024
22a3756
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Oct 30, 2024
b6a2b6e
refactor(base): lock free backtrace capture
zhang2014 Nov 4, 2024
81c2fc6
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
3fe67bb
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
1a17add
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
b2b3f5a
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
afab37b
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
947a56d
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
78d227f
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
5a8070a
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
66ff7a5
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
d44d90a
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
191ba30
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
9e574e5
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
5646981
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
a402964
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
a243c84
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
13ec5f8
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
c84cbdc
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
308030d
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
aa5e2aa
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
7479420
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
94d635e
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
bc6d718
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
aa188dd
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
e3559ee
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
4c64e5a
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
189f74d
refactor(base): lock free backtrace capture
zhang2014 Nov 5, 2024
ec00e90
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
3a6160b
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
b302788
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
b3ba7f2
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
7a8b47c
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
59595d4
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
7390c1f
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
987d0bd
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
03ef6b3
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
aa5182b
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
19680cd
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
6993810
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
737a384
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
73ef5fa
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
b83569e
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
6844e46
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
8902c93
refactor(base): lock free backtrace capture
zhang2014 Nov 6, 2024
449ff6a
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
887e883
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
78a383a
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
4dc0d07
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
1bbec9a
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
0949e75
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
6421eb0
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
aca416a
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
d7a6e8d
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
984479d
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
d285bee
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
f6d6176
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
a100b16
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
e72964f
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
d11df3a
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
4d1080c
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
9c21564
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
2a6a534
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
ec98625
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Nov 7, 2024
cc6334e
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
2b2fef6
refactor(base): lock free backtrace capture
zhang2014 Nov 7, 2024
f6ed286
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Nov 7, 2024
141df18
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Nov 8, 2024
aca5cd8
fix(base): fix test failure
zhang2014 Nov 11, 2024
209584e
Merge branch 'refactor/optimize_backtrace' of github.com:zhang2014/da…
zhang2014 Nov 11, 2024
2b6324b
Merge branch 'main' into refactor/optimize_backtrace
zhang2014 Nov 11, 2024
e902545
fix(base): fix test failure
zhang2014 Nov 11, 2024
810ad69
Merge branch 'refactor/optimize_backtrace' of github.com:zhang2014/da…
zhang2014 Nov 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ geo-types = "0.7.13"
geohash = "0.13.0"
geos = { version = "9.0.0", features = ["static", "geo", "geo-types"] }
geozero = { version = "0.14.0", features = ["default", "with-wkb", "with-geos", "with-geojson"] }
gimli = "0.31.0"
globiter = "0.1"
goldenfile = "1.4"
h3o = "0.4.0"
Expand Down Expand Up @@ -353,6 +354,7 @@ num-bigint = "0.4.6"
num-derive = "0.3.3"
num-traits = "0.2.19"
num_cpus = "1.13.1"
object = "0.36.5"
object_store_opendal = "0.48.1"
once_cell = "1.15.0"
openai_api_rust = "0.1"
Expand Down Expand Up @@ -512,6 +514,7 @@ nom-rule = "0.4"
pratt = "0.4.0"
pretty = "0.11.3"
rspack-codespan-reporting = "0.11"
rustc-demangle = "0.1"
strsim = "0.10"
strum_macros = "0.24"
vergen = { version = "8.3.1", default-features = false, features = ["build", "cargo", "git", "gix", "rustc"] }
Expand Down Expand Up @@ -595,6 +598,7 @@ gimli = { opt-level = 3 }
miniz_oxide = { opt-level = 3 }
object = { opt-level = 3 }
rustc-demangle = { opt-level = 3 }
databend-common-exception = { opt-level = 3 }

[profile.test]
opt-level = 0
Expand Down
1 change: 1 addition & 0 deletions src/common/arrow/src/arrow/ffi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ unsafe fn read_bytes(ptr: *const u8, len: usize) -> &'static str {
}

unsafe fn metadata_from_bytes(data: *const ::std::os::raw::c_char) -> (Metadata, Extension) {
#[allow(clippy::unnecessary_cast)]
let mut data = data as *const u8; // u8 = i8
if data.is_null() {
return (Metadata::default(), None);
Expand Down
1 change: 1 addition & 0 deletions src/common/base/src/mem_allocator/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ pub mod linux {
}
// fallback to (5.13.0)
let fallback_version = 5u32 << 16 | 13u32 << 8;
#[allow(clippy::unnecessary_cast)]
let slice = unsafe { &*(&uname.release[..length] as *const _ as *const [u8]) };
let result = match std::str::from_utf8(slice) {
Ok(ver) => match semver::Version::parse(ver) {
Expand Down
7 changes: 6 additions & 1 deletion src/common/exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ databend-common-ast = { workspace = true }
anyhow = { workspace = true }
arrow-flight = { workspace = true }
arrow-schema = { workspace = true }
backtrace = { workspace = true }
backtrace = { workspace = true, features = ["std", "serialize-serde"] }
bincode = { workspace = true }
geos = { workspace = true }
geozero = { workspace = true }
gimli = { workspace = true }
http = { workspace = true }
libc = { workspace = true }
object = { workspace = true }
once_cell = { workspace = true }
opendal = { workspace = true }
parquet = { workspace = true }
paste = { workspace = true }
prost = { workspace = true }
reqwest = { workspace = true }
rustc-demangle = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sqlx = { workspace = true }
Expand Down
205 changes: 205 additions & 0 deletions src/common/exception/src/elf/dwarf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

use gimli::DebugAbbrev;
use gimli::DebugAddr;
use gimli::DebugAranges;
use gimli::DebugInfo;
use gimli::DebugInfoOffset;
use gimli::DebugLine;
use gimli::DebugLineStr;
use gimli::DebugRanges;
use gimli::DebugRngLists;
use gimli::DebugStr;
use gimli::DebugStrOffsets;
use gimli::EndianSlice;
use gimli::NativeEndian;
use gimli::RangeLists;
use gimli::Reader;
use gimli::UnitHeader;
use gimli::UnitType;
use object::CompressionFormat;
use object::Object;
use object::ObjectSection;

use crate::elf::dwarf_unit::Unit;
use crate::elf::dwarf_unit::UnitAttrs;
use crate::elf::ElfFile;

#[derive(Debug)]
pub struct CallLocation {
pub symbol: Option<String>,
pub file: Option<String>,
pub line: Option<u32>,
pub column: Option<u32>,
pub is_inlined: bool,
}

pub struct Dwarf {
#[allow(unused)]
elf: Arc<ElfFile>,
debug_str: DebugStr<EndianSlice<'static, NativeEndian>>,
debug_info: DebugInfo<EndianSlice<'static, NativeEndian>>,
debug_line: DebugLine<EndianSlice<'static, NativeEndian>>,
debug_line_str: DebugLineStr<EndianSlice<'static, NativeEndian>>,
debug_str_offsets: DebugStrOffsets<EndianSlice<'static, NativeEndian>>,
debug_aranges: DebugAranges<EndianSlice<'static, NativeEndian>>,
debug_abbrev: DebugAbbrev<EndianSlice<'static, NativeEndian>>,
debug_addr: DebugAddr<EndianSlice<'static, NativeEndian>>,
debug_range_list: RangeLists<EndianSlice<'static, NativeEndian>>,
}

static EMPTY_BYTES: &[u8] = &[];

impl Dwarf {
pub fn create(elf: Arc<ElfFile>) -> Option<Dwarf> {
fn get_debug_section(elf: &ElfFile, name: &str) -> EndianSlice<'static, NativeEndian> {
let Some(section) = elf.section_by_name(name) else {
return EndianSlice::new(EMPTY_BYTES, NativeEndian);
};

// Unsupported compress debug info
let Ok(compressed) = section.compressed_file_range() else {
return EndianSlice::new(EMPTY_BYTES, NativeEndian);
};

#[allow(clippy::missing_transmute_annotations)]
unsafe {
match compressed.format != CompressionFormat::None {
true => EndianSlice::new(EMPTY_BYTES, NativeEndian),
false => match section.data() {
Err(_) => EndianSlice::new(EMPTY_BYTES, NativeEndian),
Ok(data) => EndianSlice::new(std::mem::transmute(data), NativeEndian),
},
}
}
}

for name in [".debug_info", ".debug_abbrev", ".debug_line"] {
if get_debug_section(&elf, name).is_empty() {
return None;
}
}

Some(Dwarf {
debug_str: DebugStr::from(get_debug_section(&elf, ".debug_str")),
debug_info: DebugInfo::from(get_debug_section(&elf, ".debug_info")),
debug_line: DebugLine::from(get_debug_section(&elf, ".debug_line")),
debug_line_str: DebugLineStr::from(get_debug_section(&elf, ".debug_line_str")),
debug_str_offsets: DebugStrOffsets::from(get_debug_section(&elf, ".debug_str_offsets")),
debug_aranges: DebugAranges::from(get_debug_section(&elf, ".debug_aranges")),
debug_abbrev: DebugAbbrev::from(get_debug_section(&elf, ".debug_abbrev")),
debug_range_list: RangeLists::new(
DebugRanges::from(get_debug_section(&elf, ".debug_ranges")),
DebugRngLists::from(get_debug_section(&elf, ".debug_rnglists")),
),
debug_addr: DebugAddr::from(get_debug_section(&elf, ".debug_addr")),
elf,
})
}

fn find_debug_info_offset(&self, probe: u64) -> Option<DebugInfoOffset<usize>> {
let mut heads = self.debug_aranges.headers();
while let Some(head) = heads.next().ok()? {
let mut entries = head.entries();
while let Some(entry) = entries.next().ok()? {
if probe >= entry.address() && probe <= entry.address() + entry.length() {
return Some(head.debug_info_offset());
}
}
}

None
}

fn get_unit(
&self,
head: UnitHeader<EndianSlice<'static, NativeEndian>>,
) -> gimli::Result<Option<Unit<EndianSlice<'static, NativeEndian>>>> {
let abbrev_offset = head.debug_abbrev_offset();
let Ok(abbreviations) = self.debug_abbrev.abbreviations(abbrev_offset) else {
return Ok(None);
};

let mut cursor = head.entries(&abbreviations);
let (_idx, root) = cursor.next_dfs()?.unwrap();

let mut attrs = root.attrs();
let mut unit_attrs = UnitAttrs::create();

while let Some(attr) = attrs.next()? {
unit_attrs.set_attr(&self.debug_str, attr);
}

Ok(Some(Unit {
head,
abbreviations,
attrs: unit_attrs,
debug_str: self.debug_str,
debug_info: self.debug_info,
debug_abbrev: self.debug_abbrev,
debug_line: self.debug_line,
debug_line_str: self.debug_line_str,
debug_str_offsets: self.debug_str_offsets,
debug_addr: self.debug_addr,
range_list: self.debug_range_list,
}))
}

fn fast_find_frames(&self, probe: u64) -> gimli::Result<Option<Vec<CallLocation>>> {
if let Some(debug_info_offset) = self.find_debug_info_offset(probe) {
let head = self.debug_info.header_from_offset(debug_info_offset)?;

let type_ = head.type_();
if matches!(type_, UnitType::Compilation | UnitType::Skeleton(_)) {
if let Some(unit) = self.get_unit(head)? {
return Ok(Some(unit.find_frames(probe)?));
}
}
}

Ok(None)
}

fn slow_find_frames(&self, probe: u64) -> gimli::Result<Vec<CallLocation>> {
let mut units = self.debug_info.units();
while let Some(head) = units.next()? {
if matches!(head.type_(), UnitType::Compilation | UnitType::Skeleton(_)) {
if let Some(unit) = self.get_unit(head)? {
if unit.match_pc(probe) {
return unit.find_frames(probe);
}
}
}
}

Ok(vec![])
}

pub fn find_frames(&self, probe: u64) -> gimli::Result<Vec<CallLocation>> {
match self.fast_find_frames(probe)? {
Some(location) => Ok(location),
None => self.slow_find_frames(probe),
}
}
}

// #[cfg(target_os = "linux")]
#[derive(Copy, Clone, Debug)]
pub enum HighPc {
Addr(u64),
Offset(u64),
}
Loading
Loading