Skip to content

Commit

Permalink
symbolizer: intro sled disk store
Browse files Browse the repository at this point in the history
  • Loading branch information
noneback committed Jul 26, 2024
1 parent dd53bce commit be6e4fc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ target/
**/.vscode/
.vim/
.vscode/
*.txt
tests/


# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
Expand Down
2 changes: 2 additions & 0 deletions doctor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ procfs = "0.16.0"
clap = "4.5.9"
goblin = {version = "0.8.2", features = ["elf32","elf32"]}
moka = { version = "0.12.8", features = ["future","sync"] }
sled = { version = "0.34.7", features = ["io_uring", "zstd"] }


[[bin]]
name = "doctor"
Expand Down
6 changes: 4 additions & 2 deletions doctor/src/symbolizer/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::path::PathBuf;

use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum SymbolizerError {
#[error("fail to open symbol store: {0}")]
OpenSymbolStoreFailed(#[from] std::io::Error),
OpenSymbolStoreDiskDBFailed(#[from] sled::Error),
#[error("failed to fetch elf: {0}")]
FetchElfFailed(PathBuf),
#[error("load {0}, elf failed: {1}")]
Io(PathBuf, std::io::Error),
}
58 changes: 21 additions & 37 deletions doctor/src/symbolizer/symbol_store.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use goblin::elf::Elf;
use std::cmp::Ordering;
use sled::Db;

use std::collections::BTreeSet;
use std::fmt::Display;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{BufReader, Read};
use std::path::PathBuf;

use moka::sync::Cache;

use super::error::SymbolizerError;
use moka::sync::Cache;

#[derive(PartialEq, Eq, PartialOrd, Clone, Debug)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
struct Symbol {
pub(crate) addr: u64,
pub(crate) name: Option<String>,
Expand All @@ -32,37 +32,28 @@ impl Hash for Symbol {
}
}

impl Ord for Symbol {
fn cmp(&self, other: &Self) -> Ordering {
match self.addr.cmp(&other.addr) {
Ordering::Equal => self.name.cmp(&other.name),
other => other,
}
}
}
// use rusqlite::Connection;
pub struct SymbolStore {
cache: Cache<PathBuf, BTreeSet<Symbol>>,
db: Db,
}

impl SymbolStore {
pub fn new(path: PathBuf) -> SymbolStore {
Self {
pub fn new(path: PathBuf) -> Result<SymbolStore, SymbolizerError> {
Ok(Self {
cache: Cache::new(100),
}
db: sled::open(path)?,
})
}

pub fn get_symbol(&self, dso: &PathBuf, offset: u64) -> Option<Symbol> {
let syms = self.fetch_elf(&dso).unwrap();
let syms = self.fetch_elf(dso).unwrap();
let target = Symbol {
addr: offset,
name: None,
};

match syms.range(..target).next_back() {
Some(s) => Some(s.clone()),
None => None,
}
syms.range(..target).next_back().cloned()
}

fn fetch_elf(&self, dso: &PathBuf) -> Result<BTreeSet<Symbol>, SymbolizerError> {
Expand All @@ -78,10 +69,12 @@ impl SymbolStore {
return Ok(());
}

let file = File::open(dso.clone())?;
let file = File::open(dso.clone()).map_err(|e| SymbolizerError::Io(dso.clone(), e))?;
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer)?;
reader
.read_to_end(&mut buffer)
.map_err(|e| SymbolizerError::Io(dso.clone(), e));
let elf = Elf::parse(&buffer).expect("Failed to parse ELF file");
// static syms
let mut syms = elf
Expand All @@ -92,13 +85,10 @@ impl SymbolStore {
let addr = sym.st_value;
match elf.dynstrtab.get_at(sym.st_name) {
Some(n) => Symbol {
addr: addr,
addr,
name: Some(String::from(n)),
},
None => Symbol {
addr: addr,
name: None,
},
None => Symbol { addr, name: None },
}
})
.collect::<BTreeSet<_>>();
Expand All @@ -111,13 +101,10 @@ impl SymbolStore {
let addr = sym.st_value;
match elf.dynstrtab.get_at(sym.st_name) {
Some(n) => Symbol {
addr: addr,
addr,
name: Some(String::from(n)),
},
None => Symbol {
addr: addr,
name: None,
},
None => Symbol { addr, name: None },
}
})
.collect::<BTreeSet<_>>();
Expand All @@ -135,11 +122,8 @@ mod tests {
fn test_fetch_elf() {
// 获取当前可执行文件路径
println!("start");
let ss = SymbolStore::new("./".into());
let sym = ss.get_symbol(
&"/usr/local/aegis/aegis_client/aegis_11_91/libFileQuara.so".into(),
0x65417,
);
let ss = SymbolStore::new("/home/noneback/workspace/doctor/doctor/tests".into()).unwrap();
let sym = ss.get_symbol(&"/usr/lib/libc.so.6".into(), 0x92242);

println!("sym:\n {}", sym.unwrap());
}
Expand Down
4 changes: 2 additions & 2 deletions doctor/src/symbolizer/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ struct Symbolizer {
}

impl Symbolizer {
pub fn symbolize(dso: PathBuf, offset: u64) {}
pub fn batch_symbolize(dso: PathBuf, offsets: &[u64]) {}
pub fn symbolize(_dso: PathBuf, _offset: u64) {}
pub fn batch_symbolize(_dso: PathBuf, _offsets: &[u64]) {}
}

0 comments on commit be6e4fc

Please sign in to comment.