Commit cb5bc0e0 authored by Teng Qin's avatar Teng Qin Committed by yonghong-song

Misc fixes for C++ USDT class (#1764)

* Add stream debug output for C++ USDT class

This commit adds ability to output USDT class debug message to iostream

* USDT::init() as public function

It would be nice for users be able to call init() and see if the probe
exists / well-formatted before sending them to BPF instance
parent 03a0e2b0
...@@ -74,10 +74,16 @@ int main(int argc, char** argv) { ...@@ -74,10 +74,16 @@ int main(int argc, char** argv) {
} }
std::string binary_path(argv[1]); std::string binary_path(argv[1]);
bpf = new ebpf::BPF();
std::vector<ebpf::USDT> u; std::vector<ebpf::USDT> u;
u.emplace_back(binary_path, "folly", "request_context_switch_before", u.emplace_back(binary_path, "folly", "request_context_switch_before",
"on_context_switch"); "on_context_switch");
auto usdt_init_res = u[0].init();
if (usdt_init_res.code() != 0) {
std::cerr << usdt_init_res.msg() << std::endl;
return 1;
}
bpf = new ebpf::BPF();
auto init_res = bpf->init(BPF_PROGRAM, {}, u); auto init_res = bpf->init(BPF_PROGRAM, {}, u);
if (init_res.code() != 0) { if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl; std::cerr << init_res.msg() << std::endl;
...@@ -88,6 +94,8 @@ int main(int argc, char** argv) { ...@@ -88,6 +94,8 @@ int main(int argc, char** argv) {
if (attach_res.code() != 0) { if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl; std::cerr << attach_res.msg() << std::endl;
return 1; return 1;
} else {
std::cout << "Attached to USDT " << u[0];
} }
auto open_res = bpf->open_perf_buffer("events", &handle_output); auto open_res = bpf->open_perf_buffer("events", &handle_output);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <cctype> #include <cctype>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <ostream>
#include <string> #include <string>
#include "BPFTable.h" #include "BPFTable.h"
...@@ -47,8 +48,7 @@ class BPF { ...@@ -47,8 +48,7 @@ class BPF {
explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr, explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr,
bool rw_engine_enabled = true) bool rw_engine_enabled = true)
: flag_(flag), : flag_(flag), bpf_module_(new BPFModule(flag, ts, rw_engine_enabled)) {}
bpf_module_(new BPFModule(flag, ts, rw_engine_enabled)) {}
StatusTuple init(const std::string& bpf_program, StatusTuple init(const std::string& bpf_program,
const std::vector<std::string>& cflags = {}, const std::vector<std::string>& cflags = {},
const std::vector<USDT>& usdt = {}); const std::vector<USDT>& usdt = {});
...@@ -244,6 +244,8 @@ class USDT { ...@@ -244,6 +244,8 @@ class USDT {
name_(name), name_(name),
probe_func_(probe_func) {} probe_func_(probe_func) {}
StatusTuple init();
bool operator==(const USDT& other) const { bool operator==(const USDT& other) const {
return (provider_ == other.provider_) && (name_ == other.name_) && return (provider_ == other.provider_) && (name_ == other.name_) &&
(binary_path_ == other.binary_path_) && (binary_path_ == other.binary_path_) &&
...@@ -251,11 +253,16 @@ class USDT { ...@@ -251,11 +253,16 @@ class USDT {
} }
std::string print_name() const { std::string print_name() const {
return provider_ + ":" + name_ + " from " + binary_path_; return provider_ + ":" + name_ + " from " + binary_path_ + " for probe " +
"probe_func_";
}
friend std::ostream& operator<<(std::ostream& out, const USDT& usdt) {
return out << usdt.provider_ << ":" << usdt.name_ << " from "
<< usdt.binary_path_ << " for probe " << usdt.probe_func_;
} }
private: private:
StatusTuple init();
bool initialized_; bool initialized_;
std::string binary_path_; std::string binary_path_;
......
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