Skip to content

Commit

Permalink
Replace StatusTuple::code() != 0 with !StatusTuple.ok() in examples/
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao1012 authored and yonghong-song committed Dec 14, 2021
1 parent b2f78da commit 52900a4
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 48 deletions.
8 changes: 4 additions & 4 deletions examples/cpp/CGroupTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ int main(int argc, char** argv) {

ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto cgroup_array = bpf.get_cgroup_array("cgroup");
auto update_res = cgroup_array.update_value(0, argv[1]);
if (update_res.code() != 0) {
if (!update_res.ok()) {
std::cerr << update_res.msg() << std::endl;
return 1;
}

auto attach_res =
bpf.attach_kprobe("vfs_open", "on_vfs_open");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
Expand All @@ -76,7 +76,7 @@ int main(int argc, char** argv) {
std::cout << line << std::endl;

auto detach_res = bpf.detach_kprobe("vfs_open");
if (detach_res.code() != 0) {
if (!detach_res.ok()) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/CPUDistribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ int task_switch_event(struct pt_regs *ctx, struct task_struct *prev) {
int main(int argc, char** argv) {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto attach_res =
bpf.attach_kprobe("finish_task_switch", "task_switch_event");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
Expand All @@ -88,7 +88,7 @@ int main(int argc, char** argv) {
}

auto detach_res = bpf.detach_kprobe("finish_task_switch");
if (detach_res.code() != 0) {
if (!detach_res.ok()) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/FollyRequestContextSwitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ int main(int argc, char** argv) {
ebpf::BPF* bpf = new ebpf::BPF();

auto init_res = bpf->init(BPF_PROGRAM, {}, {u});
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto attach_res = bpf->attach_usdt_all();
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
} else {
std::cout << "Attached to USDT " << u;
}

auto open_res = bpf->open_perf_buffer("events", &handle_output);
if (open_res.code() != 0) {
if (!open_res.ok()) {
std::cerr << open_res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/HelloWorld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int on_sys_clone(void *ctx) {
int main() {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
Expand All @@ -31,7 +31,7 @@ int main() {
std::string clone_fnname = bpf.get_syscall_fnname("clone");

auto attach_res = bpf.attach_kprobe(clone_fnname, "on_sys_clone");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
Expand All @@ -43,7 +43,7 @@ int main() {
std::cout << line << std::endl;
// Detach the probe if we got at least one line.
auto detach_res = bpf.detach_kprobe(clone_fnname);
if (detach_res.code() != 0) {
if (!detach_res.ok()) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/KFuncExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ void handle_output(void *cb_cookie, void *data, int data_size) {
int main() {
ebpf::BPF bpf;
auto res = bpf.init(BPF_PROGRAM);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}

int prog_fd;
res = bpf.load_func("kfunc____x64_sys_openat", BPF_PROG_TYPE_TRACING, prog_fd);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand All @@ -97,7 +97,7 @@ int main() {
}

res = bpf.load_func("kretfunc____x64_sys_openat", BPF_PROG_TYPE_TRACING, prog_fd);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand All @@ -109,7 +109,7 @@ int main() {
}

auto open_res = bpf.open_perf_buffer("events", &handle_output);
if (open_res.code() != 0) {
if (!open_res.ok()) {
std::cerr << open_res.msg() << std::endl;
return 1;
}
Expand Down
12 changes: 6 additions & 6 deletions examples/cpp/KModRetExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static int modify_return(ebpf::BPF &bpf) {
int prog_fd;
auto res = bpf.load_func("kmod_ret____x64_sys_openat",
BPF_PROG_TYPE_TRACING, prog_fd, BPF_F_SLEEPABLE);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand All @@ -122,7 +122,7 @@ static int modify_return(ebpf::BPF &bpf) {
uint32_t key = 0;
struct fname_buf val;
res = fname_table.get_value(key, val);
if (res.code() != 0) {
if (!res.ok()) {
close(attach_fd);
std::cerr << res.msg() << std::endl;
return 1;
Expand All @@ -138,7 +138,7 @@ static int not_modify_return(ebpf::BPF &bpf) {
int prog_fd;
auto res = bpf.load_func("kmod_ret__security_file_open",
BPF_PROG_TYPE_TRACING, prog_fd);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand All @@ -159,7 +159,7 @@ static int not_modify_return(ebpf::BPF &bpf) {
auto count_table = bpf.get_array_table<uint32_t>("count");
uint32_t key = 0, val = 0;
res = count_table.get_value(key, val);
if (res.code() != 0) {
if (!res.ok()) {
close(attach_fd);
std::cerr << res.msg() << std::endl;
return 1;
Expand All @@ -173,15 +173,15 @@ static int not_modify_return(ebpf::BPF &bpf) {
int main() {
ebpf::BPF bpf;
auto res = bpf.init(BPF_PROGRAM);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}

uint32_t key = 0, val = getpid();
auto pid_table = bpf.get_array_table<uint32_t>("target_pid");
res = pid_table.update_value(key, val);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/LLCStat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ struct event_t {
int main(int argc, char** argv) {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto attach_ref_res =
bpf.attach_perf_event(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES,
"on_cache_ref", 100, 0);
if (attach_ref_res.code() != 0) {
if (!attach_ref_res.ok()) {
std::cerr << attach_ref_res.msg() << std::endl;
return 1;
}
auto attach_miss_res = bpf.attach_perf_event(
PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES, "on_cache_miss", 100, 0);
if (attach_miss_res.code() != 0) {
if (!attach_miss_res.ok()) {
std::cerr << attach_miss_res.msg() << std::endl;
return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/RandomRead.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,28 @@ int main(int argc, char** argv) {

bpf = new ebpf::BPF(0, nullptr, true, "", allow_rlimit);
auto init_res = bpf->init(BPF_PROGRAM, cflags, {});
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
if (argc == 3) {
auto cgroup_array = bpf->get_cgroup_array("cgroup");
auto update_res = cgroup_array.update_value(0, argv[2]);
if (update_res.code() != 0) {
if (!update_res.ok()) {
std::cerr << update_res.msg() << std::endl;
return 1;
}
}

auto attach_res =
bpf->attach_raw_tracepoint("urandom_read", "on_urandom_read");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}

auto open_res = bpf->open_perf_buffer("events", &handle_output);
if (open_res.code() != 0) {
if (!open_res.ok()) {
std::cerr << open_res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/RecordMySQLQuery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ int main(int argc, char** argv) {

ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto attach_res =
bpf.attach_uprobe(mysql_path, ALLOC_QUERY_FUNC, "probe_mysql_query");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
Expand All @@ -95,7 +95,7 @@ int main(int argc, char** argv) {
}

auto detach_res = bpf.detach_uprobe(mysql_path, ALLOC_QUERY_FUNC);
if (detach_res.code() != 0) {
if (!detach_res.ok()) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/SkLocalStorageIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct info_t {
int main() {
ebpf::BPF bpf;
auto res = bpf.init(BPF_PROGRAM);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand All @@ -111,15 +111,15 @@ int main() {
auto sk_table = bpf.get_sk_storage_table<unsigned long long>("sk_data_map");

res = sk_table.update_value(sockfd1, v1);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << "sk_data_map sockfd1 update failure: " << res.msg() << std::endl;
close(sockfd2);
close(sockfd1);
return 1;
}

res = sk_table.update_value(sockfd2, v2);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << "sk_data_map sockfd2 update failure: " << res.msg() << std::endl;
close(sockfd2);
close(sockfd1);
Expand All @@ -128,7 +128,7 @@ int main() {

int prog_fd;
res = bpf.load_func("bpf_iter__bpf_sk_storage_map", BPF_PROG_TYPE_TRACING, prog_fd);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/TCPSendStack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ struct stack_key_t {
int main(int argc, char** argv) {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (init_res.code() != 0) {
if (!init_res.ok()) {
std::cerr << init_res.msg() << std::endl;
return 1;
}

auto attach_res = bpf.attach_kprobe("tcp_sendmsg", "on_tcp_send");
if (attach_res.code() != 0) {
if (!attach_res.ok()) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
Expand All @@ -77,7 +77,7 @@ int main(int argc, char** argv) {
sleep(probe_time);

auto detach_res = bpf.detach_kprobe("tcp_sendmsg");
if (detach_res.code() != 0) {
if (!detach_res.ok()) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/TaskIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ struct info_t {
int main() {
ebpf::BPF bpf;
auto res = bpf.init(BPF_PROGRAM);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}

int prog_fd;
res = bpf.load_func("bpf_iter__task", BPF_PROG_TYPE_TRACING, prog_fd);
if (res.code() != 0) {
if (!res.ok()) {
std::cerr << res.msg() << std::endl;
return 1;
}
Expand Down
Loading

0 comments on commit 52900a4

Please sign in to comment.