Commit d3b9d6a1 authored by Teng Qin's avatar Teng Qin

Clean C++ interface arguments for tracing events

parent fd244056
...@@ -161,7 +161,6 @@ StatusTuple BPF::detach_all() { ...@@ -161,7 +161,6 @@ StatusTuple BPF::detach_all() {
StatusTuple BPF::attach_kprobe(const std::string& kernel_func, StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
const std::string& probe_func, const std::string& probe_func,
bpf_probe_attach_type attach_type, bpf_probe_attach_type attach_type,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void* cb_cookie) { perf_reader_cb cb, void* cb_cookie) {
std::string probe_event = get_kprobe_event(kernel_func, attach_type); std::string probe_event = get_kprobe_event(kernel_func, attach_type);
if (kprobes_.find(probe_event) != kprobes_.end()) if (kprobes_.find(probe_event) != kprobes_.end())
...@@ -170,9 +169,8 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func, ...@@ -170,9 +169,8 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
int probe_fd; int probe_fd;
TRY2(load_func(probe_func, BPF_PROG_TYPE_KPROBE, probe_fd)); TRY2(load_func(probe_func, BPF_PROG_TYPE_KPROBE, probe_fd));
void* res = void* res = bpf_attach_kprobe(probe_fd, attach_type, probe_event.c_str(),
bpf_attach_kprobe(probe_fd, attach_type, probe_event.c_str(), kernel_func.c_str(), kernel_func.c_str(), cb, cb_cookie);
cb, cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -192,8 +190,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, ...@@ -192,8 +190,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
const std::string& symbol, const std::string& symbol,
const std::string& probe_func, const std::string& probe_func,
uint64_t symbol_addr, uint64_t symbol_addr,
bpf_probe_attach_type attach_type, bpf_probe_attach_type attach_type, pid_t pid,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void* cb_cookie) { perf_reader_cb cb, void* cb_cookie) {
std::string module; std::string module;
uint64_t offset; uint64_t offset;
...@@ -207,8 +204,8 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, ...@@ -207,8 +204,8 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
TRY2(load_func(probe_func, BPF_PROG_TYPE_KPROBE, probe_fd)); TRY2(load_func(probe_func, BPF_PROG_TYPE_KPROBE, probe_fd));
void* res = void* res =
bpf_attach_uprobe(probe_fd, attach_type, probe_event.c_str(), binary_path.c_str(), bpf_attach_uprobe(probe_fd, attach_type, probe_event.c_str(),
offset, pid, cb, cb_cookie); binary_path.c_str(), offset, pid, cb, cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -226,8 +223,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, ...@@ -226,8 +223,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
return StatusTuple(0); return StatusTuple(0);
} }
StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid, int cpu, StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid) {
int group_fd) {
for (const auto& u : usdt_) for (const auto& u : usdt_)
if (u == usdt) { if (u == usdt) {
bool failed = false; bool failed = false;
...@@ -259,7 +255,6 @@ StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid, int cpu, ...@@ -259,7 +255,6 @@ StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid, int cpu,
StatusTuple BPF::attach_tracepoint(const std::string& tracepoint, StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
const std::string& probe_func, const std::string& probe_func,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void* cb_cookie) { perf_reader_cb cb, void* cb_cookie) {
if (tracepoints_.find(tracepoint) != tracepoints_.end()) if (tracepoints_.find(tracepoint) != tracepoints_.end())
return StatusTuple(-1, "Tracepoint %s already attached", return StatusTuple(-1, "Tracepoint %s already attached",
...@@ -274,9 +269,8 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint, ...@@ -274,9 +269,8 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
int probe_fd; int probe_fd;
TRY2(load_func(probe_func, BPF_PROG_TYPE_TRACEPOINT, probe_fd)); TRY2(load_func(probe_func, BPF_PROG_TYPE_TRACEPOINT, probe_fd));
void* res = void* res = bpf_attach_tracepoint(probe_fd, tp_category.c_str(),
bpf_attach_tracepoint(probe_fd, tp_category.c_str(), tp_name.c_str(), cb, tp_name.c_str(), cb, cb_cookie);
cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -309,7 +303,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config, ...@@ -309,7 +303,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
cpus.push_back(cpu); cpus.push_back(cpu);
else else
cpus = get_online_cpus(); cpus = get_online_cpus();
for (int i: cpus) { for (int i : cpus) {
int fd = bpf_attach_perf_event(probe_fd, ev_type, ev_config, sample_period, int fd = bpf_attach_perf_event(probe_fd, ev_type, ev_config, sample_period,
sample_freq, pid, i, group_fd); sample_freq, pid, i, group_fd);
if (fd < 0) { if (fd < 0) {
...@@ -347,8 +341,7 @@ StatusTuple BPF::detach_kprobe(const std::string& kernel_func, ...@@ -347,8 +341,7 @@ StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
StatusTuple BPF::detach_uprobe(const std::string& binary_path, StatusTuple BPF::detach_uprobe(const std::string& binary_path,
const std::string& symbol, uint64_t symbol_addr, const std::string& symbol, uint64_t symbol_addr,
bpf_probe_attach_type attach_type, bpf_probe_attach_type attach_type, pid_t pid) {
pid_t pid) {
std::string module; std::string module;
uint64_t offset; uint64_t offset;
TRY2(check_binary_symbol(binary_path, symbol, symbol_addr, module, offset)); TRY2(check_binary_symbol(binary_path, symbol, symbol_addr, module, offset));
...@@ -399,21 +392,19 @@ StatusTuple BPF::detach_tracepoint(const std::string& tracepoint) { ...@@ -399,21 +392,19 @@ StatusTuple BPF::detach_tracepoint(const std::string& tracepoint) {
StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) { StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) {
auto it = perf_events_.find(std::make_pair(ev_type, ev_config)); auto it = perf_events_.find(std::make_pair(ev_type, ev_config));
if (it == perf_events_.end()) if (it == perf_events_.end())
return StatusTuple(-1, "Perf Event type %d config %d not attached", return StatusTuple(-1, "Perf Event type %d config %d not attached", ev_type,
ev_type, ev_config); ev_config);
TRY2(detach_perf_event_all_cpu(it->second)); TRY2(detach_perf_event_all_cpu(it->second));
perf_events_.erase(it); perf_events_.erase(it);
return StatusTuple(0); return StatusTuple(0);
} }
StatusTuple BPF::open_perf_event(const std::string& name, StatusTuple BPF::open_perf_event(const std::string& name, uint32_t type,
uint32_t type,
uint64_t config) { uint64_t config) {
if (perf_event_arrays_.find(name) == perf_event_arrays_.end()) { if (perf_event_arrays_.find(name) == perf_event_arrays_.end()) {
TableStorage::iterator it; TableStorage::iterator it;
if (!bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it)) if (!bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
return StatusTuple(-1, return StatusTuple(-1, "open_perf_event: unable to find table_storage %s",
"open_perf_event: unable to find table_storage %s",
name.c_str()); name.c_str());
perf_event_arrays_[name] = new BPFPerfEventArray(it->second); perf_event_arrays_[name] = new BPFPerfEventArray(it->second);
} }
...@@ -434,8 +425,7 @@ StatusTuple BPF::close_perf_event(const std::string& name) { ...@@ -434,8 +425,7 @@ StatusTuple BPF::close_perf_event(const std::string& name) {
StatusTuple BPF::open_perf_buffer(const std::string& name, StatusTuple BPF::open_perf_buffer(const std::string& name,
perf_reader_raw_cb cb, perf_reader_raw_cb cb,
perf_reader_lost_cb lost_cb, perf_reader_lost_cb lost_cb, void* cb_cookie,
void* cb_cookie,
int page_cnt) { int page_cnt) {
if (perf_buffers_.find(name) == perf_buffers_.end()) { if (perf_buffers_.find(name) == perf_buffers_.end()) {
TableStorage::iterator it; TableStorage::iterator it;
...@@ -467,8 +457,8 @@ void BPF::poll_perf_buffer(const std::string& name, int timeout) { ...@@ -467,8 +457,8 @@ void BPF::poll_perf_buffer(const std::string& name, int timeout) {
it->second->poll(timeout); it->second->poll(timeout);
} }
StatusTuple BPF::load_func(const std::string& func_name, StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type,
bpf_prog_type type, int& fd) { int& fd) {
if (funcs_.find(func_name) != funcs_.end()) { if (funcs_.find(func_name) != funcs_.end()) {
fd = funcs_[func_name]; fd = funcs_[func_name];
return StatusTuple(0); return StatusTuple(0);
...@@ -487,17 +477,15 @@ StatusTuple BPF::load_func(const std::string& func_name, ...@@ -487,17 +477,15 @@ StatusTuple BPF::load_func(const std::string& func_name,
log_level = 1; log_level = 1;
fd = bpf_prog_load(type, func_name.c_str(), fd = bpf_prog_load(type, func_name.c_str(),
reinterpret_cast<struct bpf_insn*>(func_start), reinterpret_cast<struct bpf_insn*>(func_start), func_size,
func_size, bpf_module_->license(), bpf_module_->license(), bpf_module_->kern_version(),
bpf_module_->kern_version(),
log_level, nullptr, 0); log_level, nullptr, 0);
if (fd < 0) if (fd < 0)
return StatusTuple(-1, "Failed to load %s: %d", func_name.c_str(), fd); return StatusTuple(-1, "Failed to load %s: %d", func_name.c_str(), fd);
bpf_module_->annotate_prog_tag(func_name, fd, bpf_module_->annotate_prog_tag(
reinterpret_cast<struct bpf_insn*>(func_start), func_name, fd, reinterpret_cast<struct bpf_insn*>(func_start), func_size);
func_size);
funcs_[func_name] = fd; funcs_[func_name] = fd;
return StatusTuple(0); return StatusTuple(0);
} }
...@@ -518,8 +506,8 @@ StatusTuple BPF::unload_func(const std::string& func_name) { ...@@ -518,8 +506,8 @@ StatusTuple BPF::unload_func(const std::string& func_name) {
StatusTuple BPF::check_binary_symbol(const std::string& binary_path, StatusTuple BPF::check_binary_symbol(const std::string& binary_path,
const std::string& symbol, const std::string& symbol,
uint64_t symbol_addr, uint64_t symbol_addr,
std::string &module_res, std::string& module_res,
uint64_t &offset_res) { uint64_t& offset_res) {
bcc_symbol output; bcc_symbol output;
int res = bcc_resolve_symname(binary_path.c_str(), symbol.c_str(), int res = bcc_resolve_symname(binary_path.c_str(), symbol.c_str(),
symbol_addr, -1, nullptr, &output); symbol_addr, -1, nullptr, &output);
...@@ -552,8 +540,7 @@ BPFProgTable BPF::get_prog_table(const std::string& name) { ...@@ -552,8 +540,7 @@ BPFProgTable BPF::get_prog_table(const std::string& name) {
return BPFProgTable({}); return BPFProgTable({});
} }
BPFStackTable BPF::get_stack_table(const std::string& name, BPFStackTable BPF::get_stack_table(const std::string& name, bool use_debug_file,
bool use_debug_file,
bool check_debug_file_crc) { bool check_debug_file_crc) {
TableStorage::iterator it; TableStorage::iterator it;
if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it)) if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
......
...@@ -42,7 +42,7 @@ struct open_probe_t { ...@@ -42,7 +42,7 @@ struct open_probe_t {
class USDT; class USDT;
class BPF { class BPF {
public: public:
static const int BPF_MAX_STACK_DEPTH = 127; static const int BPF_MAX_STACK_DEPTH = 127;
explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr) explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr)
...@@ -54,33 +54,31 @@ public: ...@@ -54,33 +54,31 @@ public:
~BPF(); ~BPF();
StatusTuple detach_all(); StatusTuple detach_all();
StatusTuple attach_kprobe( StatusTuple attach_kprobe(const std::string& kernel_func,
const std::string& kernel_func, const std::string& probe_func, const std::string& probe_func,
bpf_probe_attach_type = BPF_PROBE_ENTRY, bpf_probe_attach_type = BPF_PROBE_ENTRY,
pid_t pid = -1, int cpu = 0, int group_fd = -1, perf_reader_cb cb = nullptr,
perf_reader_cb cb = nullptr, void* cb_cookie = nullptr); void* cb_cookie = nullptr);
StatusTuple detach_kprobe( StatusTuple detach_kprobe(
const std::string& kernel_func, const std::string& kernel_func,
bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY); bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY);
StatusTuple attach_uprobe( StatusTuple attach_uprobe(const std::string& binary_path,
const std::string& binary_path, const std::string& symbol, const std::string& symbol,
const std::string& probe_func, uint64_t symbol_addr = 0, const std::string& probe_func,
bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY, uint64_t symbol_addr = 0,
pid_t pid = -1, int cpu = 0, int group_fd = -1, bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY,
perf_reader_cb cb = nullptr, void* cb_cookie = nullptr); pid_t pid = -1, perf_reader_cb cb = nullptr,
StatusTuple detach_uprobe( void* cb_cookie = nullptr);
const std::string& binary_path, const std::string& symbol, StatusTuple detach_uprobe(const std::string& binary_path,
uint64_t symbol_addr = 0, const std::string& symbol, uint64_t symbol_addr = 0,
bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY, bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY,
pid_t pid = -1); pid_t pid = -1);
StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1, int cpu = 0, StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1);
int group_fd = -1);
StatusTuple detach_usdt(const USDT& usdt); StatusTuple detach_usdt(const USDT& usdt);
StatusTuple attach_tracepoint(const std::string& tracepoint, StatusTuple attach_tracepoint(const std::string& tracepoint,
const std::string& probe_func, const std::string& probe_func,
pid_t pid = -1, int cpu = 0, int group_fd = -1,
perf_reader_cb cb = nullptr, perf_reader_cb cb = nullptr,
void* cb_cookie = nullptr); void* cb_cookie = nullptr);
StatusTuple detach_tracepoint(const std::string& tracepoint); StatusTuple detach_tracepoint(const std::string& tracepoint);
...@@ -121,14 +119,12 @@ public: ...@@ -121,14 +119,12 @@ public:
bool use_debug_file = true, bool use_debug_file = true,
bool check_debug_file_crc = true); bool check_debug_file_crc = true);
StatusTuple open_perf_event(const std::string& name, StatusTuple open_perf_event(const std::string& name, uint32_t type,
uint32_t type,
uint64_t config); uint64_t config);
StatusTuple close_perf_event(const std::string& name); StatusTuple close_perf_event(const std::string& name);
StatusTuple open_perf_buffer(const std::string& name, StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb,
perf_reader_raw_cb cb,
perf_reader_lost_cb lost_cb = nullptr, perf_reader_lost_cb lost_cb = nullptr,
void* cb_cookie = nullptr, void* cb_cookie = nullptr,
int page_cnt = DEFAULT_PERF_BUFFER_PAGE_CNT); int page_cnt = DEFAULT_PERF_BUFFER_PAGE_CNT);
...@@ -139,7 +135,7 @@ public: ...@@ -139,7 +135,7 @@ public:
int& fd); int& fd);
StatusTuple unload_func(const std::string& func_name); StatusTuple unload_func(const std::string& func_name);
private: private:
std::string get_kprobe_event(const std::string& kernel_func, std::string get_kprobe_event(const std::string& kernel_func,
bpf_probe_attach_type type); bpf_probe_attach_type type);
std::string get_uprobe_event(const std::string& binary_path, uint64_t offset, std::string get_uprobe_event(const std::string& binary_path, uint64_t offset,
...@@ -181,9 +177,8 @@ private: ...@@ -181,9 +177,8 @@ private:
StatusTuple check_binary_symbol(const std::string& binary_path, StatusTuple check_binary_symbol(const std::string& binary_path,
const std::string& symbol, const std::string& symbol,
uint64_t symbol_addr, uint64_t symbol_addr, std::string& module_res,
std::string &module_res, uint64_t& offset_res);
uint64_t &offset_res);
int flag_; int flag_;
...@@ -202,7 +197,7 @@ private: ...@@ -202,7 +197,7 @@ private:
}; };
class USDT { class USDT {
public: public:
USDT(const std::string& binary_path, const std::string& provider, USDT(const std::string& binary_path, const std::string& provider,
const std::string& name, const std::string& probe_func) const std::string& name, const std::string& probe_func)
: initialized_(false), : initialized_(false),
...@@ -221,7 +216,7 @@ public: ...@@ -221,7 +216,7 @@ public:
return provider_ + ":" + name_ + " from " + binary_path_; return provider_ + ":" + name_ + " from " + binary_path_;
} }
private: private:
StatusTuple init(); StatusTuple init();
bool initialized_; bool initialized_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment