Skip to content

Commit

Permalink
symbolizer: intro dwarf
Browse files Browse the repository at this point in the history
  • Loading branch information
noneback committed Nov 11, 2024
1 parent 819b99b commit 9ceb4ac
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
12 changes: 12 additions & 0 deletions doctor/src/profiler/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use thiserror::Error;

use super::symbolizer::error::SymbolizerError;

#[derive(Error, Debug)]
pub enum TranslateError {
#[error("data not found")]
Expand All @@ -10,4 +12,14 @@ pub enum TranslateError {
Internal,
#[error(transparent)]
Io(#[from] std::io::Error), // 使用`from`属性自动从`std::io::Error`转换
#[error("symbolize {0}")]
Symbolize(SymbolizerError),
#[error("anyhow {0}")]
AnyError(#[from] anyhow::Error),
}

impl From<SymbolizerError> for TranslateError {
fn from(err: SymbolizerError) -> Self {
TranslateError::Symbolize(err)
}
}
46 changes: 23 additions & 23 deletions doctor/src/profiler/symbolizer/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{
};

use super::{error::SymbolizerError, symbol::Symbol};
use anyhow::Ok;
use blazesym::symbolize::Symbolize;

use blazesym::symbolize::{Sym, Symbolize};
use gimli::{Dwarf, RunTimeEndian, Section, SectionId};
use goblin::{
container::Endian,
Expand All @@ -28,6 +28,7 @@ impl DwarfInfo {
pub struct ElfMetadata {
path: PathBuf,
syms: BTreeSet<Symbol>,
debug_info: BTreeSet<Symbol>, // from dwarf
pt_loads: Vec<ProgramHeader>,
}

Expand All @@ -44,7 +45,9 @@ impl ElfMetadata {
let elf = Elf::parse(&mmap).expect("Failed to parse ELF file");
let pt_loads = elf
.program_headers
.clone()
.into_iter()
.map(|h| h.clone())
.filter(|h| h.p_type == PT_LOAD)
.collect::<Vec<ProgramHeader>>();

Expand Down Expand Up @@ -88,40 +91,37 @@ impl ElfMetadata {
RunTimeEndian::Big
};

let dwarf = Dwarf::load(|id| Self::load_dwarf(id, elf, mmap, endian))
.map_err(|e| SymbolizerError::GmiliFailed(Box::new(e)))?;
let dwarf = Dwarf::load(|id| Self::load_dwarf(id, &elf, &mmap, endian))?;
let debug_info = BTreeSet::new();

Ok(Self {
path,
syms,
debug_info,
pt_loads,
})
}

fn load_dwarf<'input, Endian: gimli::Endianity>(
id: SectionId,
elf: Elf,
mmap: Mmap,
endian: RunTimeEndian,
elf: &Elf,
mmap: &'input Mmap,
endian: Endian,
) -> Result<gimli::EndianSlice<'input, Endian>, SymbolizerError> {
let loader = |id: SectionId| -> Result<_, gimli::EndianSlice<Endian>> {
let data = match elf
.section_headers
.iter()
.find(|h| elf.shdr_strtab.get_at(h.sh_name).unwrap_or("") == id.name())
{
Some(section) => {
let start = section.sh_offset as usize;
let end = start + section.sh_size as usize;
&mmap[start..end]
}
None => &[],
};
Ok(gimli::EndianSlice::new(data, endian))
let data = match elf
.section_headers
.iter()
.find(|h| elf.shdr_strtab.get_at(h.sh_name).unwrap_or("") == id.name())
{
Some(section) => {
let start = section.sh_offset as usize;
let end = start + section.sh_size as usize;
&mmap[start..end]
}
None => &[],
};

let endian_slice = EndianSlice::new(data, endian);
Ok(endian_slice)
Ok(gimli::EndianSlice::new(data, endian))
}

fn translate(&self, file_offset: u64) -> Option<u64> {
Expand Down
4 changes: 2 additions & 2 deletions doctor/src/profiler/symbolizer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub enum SymbolizerError {
TranslateVirtOffsetFailed(PathBuf, u64),
#[error("load {0}, elf failed: {1}")]
MMapIOFailed(PathBuf, std::io::Error),
#[error("gmili: {0}")]
GmiliFailed(Box<dyn std::error::Error>),
#[error("gmili load: {0}")]
GmiliFailed(#[from] gimli::Error),
}
8 changes: 4 additions & 4 deletions doctor/src/profiler/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use aya::maps::stack_trace::StackTrace;
use blazesym::symbolize::Symbolizer;

use super::{
dso::Dso, perf_record::PerfStackFrame, process::ProcessMetadata,
dso::Dso, error::TranslateError, perf_record::PerfStackFrame, process::ProcessMetadata,
symbolizer::symbolizerr::Symbolizerr,
};

Expand Down Expand Up @@ -51,8 +51,8 @@ impl Translator {
) -> Result<Vec<PerfStackFrame>, Error> {
// for frame in record.stack_frames {}
let ips = utrace.frames().iter().map(|f| f.ip).collect::<Vec<_>>();
self.translate_usyms_v2(pid, ips)
.map_err(|e: Error| anyhow!("translate_utrace pid {} -> {}", pid, e))
self.translate_usyms_v3(pid, ips)
.map_err(|e| anyhow!("translate_utrace pid {} -> {}", pid, e))
}

pub fn translate_ksyms(&mut self, ip: u64) -> Result<PerfStackFrame, Error> {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Translator {
&mut self,
pid: u32,
ips: Vec<u64>,
) -> Result<Vec<PerfStackFrame>, Error> {
) -> Result<Vec<PerfStackFrame>, TranslateError> {
let symer = Symbolizerr::new("/root/workspace/profiler/bianque/doctor/tests".into())?;
let proc = ProcessMetadata::new(pid)?;
let mut frames = Vec::new();
Expand Down

0 comments on commit 9ceb4ac

Please sign in to comment.