Skip to content

Commit

Permalink
Misc fixes for C++ USDT class (iovisor#1764)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
palmtenor authored and yonghong-song committed May 16, 2018
1 parent 03a0e2b commit cb5bc0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 9 additions & 1 deletion examples/cpp/FollyRequestContextSwitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ int main(int argc, char** argv) {
}
std::string binary_path(argv[1]);

bpf = new ebpf::BPF();
std::vector<ebpf::USDT> u;
u.emplace_back(binary_path, "folly", "request_context_switch_before",
"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);
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
Expand All @@ -88,6 +94,8 @@ int main(int argc, char** argv) {
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
} else {
std::cout << "Attached to USDT " << u[0];
}

auto open_res = bpf->open_perf_buffer("events", &handle_output);
Expand Down
15 changes: 11 additions & 4 deletions src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cctype>
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>

#include "BPFTable.h"
Expand Down Expand Up @@ -47,8 +48,7 @@ class BPF {

explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr,
bool rw_engine_enabled = true)
: flag_(flag),
bpf_module_(new BPFModule(flag, ts, rw_engine_enabled)) {}
: flag_(flag), bpf_module_(new BPFModule(flag, ts, rw_engine_enabled)) {}
StatusTuple init(const std::string& bpf_program,
const std::vector<std::string>& cflags = {},
const std::vector<USDT>& usdt = {});
Expand Down Expand Up @@ -244,18 +244,25 @@ class USDT {
name_(name),
probe_func_(probe_func) {}

StatusTuple init();

bool operator==(const USDT& other) const {
return (provider_ == other.provider_) && (name_ == other.name_) &&
(binary_path_ == other.binary_path_) &&
(probe_func_ == other.probe_func_);
}

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:
StatusTuple init();
bool initialized_;

std::string binary_path_;
Expand Down

0 comments on commit cb5bc0e

Please sign in to comment.