1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::helpers::to_cstring;
use crate::types::MutPointer;
use crate::BccError;

use bcc_sys::bccapi::*;
use libc::free;

use core::sync::atomic::{AtomicPtr, Ordering};
use std::ffi::CStr;
use std::mem;
use std::os::raw::{c_char, c_void};
use std::ptr;

pub fn resolve_symbol_path(
    module: &str,
    symname: &str,
    addr: u64,
    pid: pid_t,
) -> Result<(String, u64), BccError> {
    let pid: pid_t = match pid {
        -1 => 0,
        x => x,
    };

    resolve_symname(module, symname, addr, pid)
}

pub fn resolve_symname(
    module: &str,
    symname: &str,
    addr: u64,
    pid: pid_t,
) -> Result<(String, u64), BccError> {
    let mut symbol = unsafe { mem::zeroed::<bcc_symbol>() };
    let cmodule = to_cstring(module, "module")?;
    let csymname = to_cstring(symname, "symname")?;

    let res = unsafe {
        bcc_resolve_symname(
            cmodule.as_ptr(),
            csymname.as_ptr(),
            addr,
            pid,
            ptr::null_mut(),
            &mut symbol as *mut bcc_symbol,
        )
    };
    if res < 0 {
        Err(BccError::UnknownSymbol {
            name: symname.to_string(),
            module: module.to_string(),
        })
    } else {
        let module = unsafe {
            CStr::from_ptr(symbol.module as *mut c_char)
                .to_str()?
                .to_string()
        };
        // symbol.module was allocated somewhere inside `bcc_resolve_symname`
        // so we need to free it manually
        unsafe { free(symbol.module as MutPointer) };
        Ok((module, symbol.offset))
    }
}

#[derive(Debug)]
pub struct SymbolCache {
    cache: AtomicPtr<c_void>,
}

impl SymbolCache {
    pub fn new(pid: pid_t) -> SymbolCache {
        SymbolCache {
            cache: unsafe { AtomicPtr::new(bcc_symcache_new(pid, ptr::null_mut())) },
        }
    }

    pub fn resolve_name(&self, module: &str, name: &str) -> Result<u64, BccError> {
        let cmodule = to_cstring(module, "module")?;
        let cname = to_cstring(name, "name")?;
        let mut addr: u64 = 0;

        let res = unsafe {
            bcc_symcache_resolve_name(
                self.cache.load(Ordering::SeqCst),
                cmodule.as_ptr(),
                cname.as_ptr(),
                &mut addr as *mut u64,
            )
        };
        if res < 0 {
            Err(BccError::UnknownSymbol {
                name: name.to_string(),
                module: module.to_string(),
            })
        } else {
            Ok(addr)
        }
    }
}