From 52900a435de85e706f5280f4a184374dd729c58e Mon Sep 17 00:00:00 2001 From: Yaxiong Zhao Date: Mon, 13 Dec 2021 12:24:04 -0800 Subject: [PATCH] Replace StatusTuple::code() != 0 with !StatusTuple.ok() in examples/ --- examples/cpp/CGroupTest.cc | 8 ++++---- examples/cpp/CPUDistribution.cc | 6 +++--- examples/cpp/FollyRequestContextSwitch.cc | 6 +++--- examples/cpp/HelloWorld.cc | 6 +++--- examples/cpp/KFuncExample.cc | 8 ++++---- examples/cpp/KModRetExample.cc | 12 ++++++------ examples/cpp/LLCStat.cc | 6 +++--- examples/cpp/RandomRead.cc | 8 ++++---- examples/cpp/RecordMySQLQuery.cc | 6 +++--- examples/cpp/SkLocalStorageIterator.cc | 8 ++++---- examples/cpp/TCPSendStack.cc | 6 +++--- examples/cpp/TaskIterator.cc | 4 ++-- examples/cpp/pyperf/PyPerfUtil.cc | 12 ++++++------ 13 files changed, 48 insertions(+), 48 deletions(-) diff --git a/examples/cpp/CGroupTest.cc b/examples/cpp/CGroupTest.cc index bfe156ca..151b6d99 100644 --- a/examples/cpp/CGroupTest.cc +++ b/examples/cpp/CGroupTest.cc @@ -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; } @@ -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; } diff --git a/examples/cpp/CPUDistribution.cc b/examples/cpp/CPUDistribution.cc index 010339f6..5f4acd9f 100644 --- a/examples/cpp/CPUDistribution.cc +++ b/examples/cpp/CPUDistribution.cc @@ -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; } @@ -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; } diff --git a/examples/cpp/FollyRequestContextSwitch.cc b/examples/cpp/FollyRequestContextSwitch.cc index d2ff02c5..2040a0c0 100644 --- a/examples/cpp/FollyRequestContextSwitch.cc +++ b/examples/cpp/FollyRequestContextSwitch.cc @@ -93,13 +93,13 @@ 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 { @@ -107,7 +107,7 @@ int main(int argc, char** argv) { } 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; } diff --git a/examples/cpp/HelloWorld.cc b/examples/cpp/HelloWorld.cc index e229ced3..51356b34 100644 --- a/examples/cpp/HelloWorld.cc +++ b/examples/cpp/HelloWorld.cc @@ -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; } @@ -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; } @@ -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; } diff --git a/examples/cpp/KFuncExample.cc b/examples/cpp/KFuncExample.cc index fed2b2cf..9b6a2c09 100644 --- a/examples/cpp/KFuncExample.cc +++ b/examples/cpp/KFuncExample.cc @@ -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; } @@ -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; } @@ -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; } diff --git a/examples/cpp/KModRetExample.cc b/examples/cpp/KModRetExample.cc index b5c3a90d..3ebb7dc6 100644 --- a/examples/cpp/KModRetExample.cc +++ b/examples/cpp/KModRetExample.cc @@ -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; } @@ -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; @@ -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; } @@ -159,7 +159,7 @@ static int not_modify_return(ebpf::BPF &bpf) { auto count_table = bpf.get_array_table("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; @@ -173,7 +173,7 @@ 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; } @@ -181,7 +181,7 @@ int main() { uint32_t key = 0, val = getpid(); auto pid_table = bpf.get_array_table("target_pid"); res = pid_table.update_value(key, val); - if (res.code() != 0) { + if (!res.ok()) { std::cerr << res.msg() << std::endl; return 1; } diff --git a/examples/cpp/LLCStat.cc b/examples/cpp/LLCStat.cc index b351f1dd..7e7e2082 100644 --- a/examples/cpp/LLCStat.cc +++ b/examples/cpp/LLCStat.cc @@ -73,7 +73,7 @@ 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; } @@ -81,13 +81,13 @@ int main(int argc, char** argv) { 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; } diff --git a/examples/cpp/RandomRead.cc b/examples/cpp/RandomRead.cc index 4851ddec..f566a9f4 100644 --- a/examples/cpp/RandomRead.cc +++ b/examples/cpp/RandomRead.cc @@ -106,14 +106,14 @@ 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; } @@ -121,13 +121,13 @@ int main(int argc, char** argv) { 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; } diff --git a/examples/cpp/RecordMySQLQuery.cc b/examples/cpp/RecordMySQLQuery.cc index 6d49eee9..aea43b32 100644 --- a/examples/cpp/RecordMySQLQuery.cc +++ b/examples/cpp/RecordMySQLQuery.cc @@ -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; } @@ -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; } diff --git a/examples/cpp/SkLocalStorageIterator.cc b/examples/cpp/SkLocalStorageIterator.cc index fdcf1d5f..88bead93 100644 --- a/examples/cpp/SkLocalStorageIterator.cc +++ b/examples/cpp/SkLocalStorageIterator.cc @@ -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; } @@ -111,7 +111,7 @@ int main() { auto sk_table = bpf.get_sk_storage_table("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); @@ -119,7 +119,7 @@ int main() { } 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); @@ -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; } diff --git a/examples/cpp/TCPSendStack.cc b/examples/cpp/TCPSendStack.cc index f7f150d5..68a1d216 100644 --- a/examples/cpp/TCPSendStack.cc +++ b/examples/cpp/TCPSendStack.cc @@ -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; } @@ -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; } diff --git a/examples/cpp/TaskIterator.cc b/examples/cpp/TaskIterator.cc index 3815e3aa..dc30663f 100644 --- a/examples/cpp/TaskIterator.cc +++ b/examples/cpp/TaskIterator.cc @@ -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; } diff --git a/examples/cpp/pyperf/PyPerfUtil.cc b/examples/cpp/pyperf/PyPerfUtil.cc index 84af1840..312d0181 100644 --- a/examples/cpp/pyperf/PyPerfUtil.cc +++ b/examples/cpp/pyperf/PyPerfUtil.cc @@ -171,7 +171,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::init() { std::to_string(kPythonStackProgIdx)); auto initRes = bpf_.init(PYPERF_BPF_PROGRAM, cflags); - if (initRes.code() != 0) { + if (!initRes.ok()) { std::fprintf(stderr, "Failed to compiled PyPerf BPF programs: %s\n", initRes.msg().c_str()); return PyPerfResult::INIT_FAIL; @@ -180,7 +180,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::init() { int progFd = -1; auto loadRes = bpf_.load_func(kPythonStackFuncName, BPF_PROG_TYPE_PERF_EVENT, progFd); - if (loadRes.code() != 0) { + if (!loadRes.ok()) { std::fprintf(stderr, "Failed to load BPF program %s: %s\n", kPythonStackFuncName.c_str(), loadRes.msg().c_str()); return PyPerfResult::INIT_FAIL; @@ -188,7 +188,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::init() { auto progTable = bpf_.get_prog_table(kProgsTableName); auto updateRes = progTable.update_value(kPythonStackProgIdx, progFd); - if (updateRes.code() != 0) { + if (!updateRes.ok()) { std::fprintf(stderr, "Failed to set BPF program %s FD %d to program table: %s\n", kPythonStackFuncName.c_str(), progFd, updateRes.msg().c_str()); @@ -216,7 +216,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::init() { auto openRes = bpf_.open_perf_buffer( kSamplePerfBufName, &handleSampleCallback, &handleLostSamplesCallback, this, kPerfBufSizePages); - if (openRes.code() != 0) { + if (!openRes.ok()) { std::fprintf(stderr, "Unable to open Perf Buffer: %s\n", openRes.msg().c_str()); return PyPerfResult::PERF_BUF_OPEN_FAIL; @@ -245,7 +245,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::profile(int64_t sampleRate, // Attach to CPU cycles auto attachRes = bpf_.attach_perf_event(0, 0, kOnEventFuncName, sampleRate, 0); - if (attachRes.code() != 0) { + if (!attachRes.ok()) { std::fprintf(stderr, "Attach to CPU cycles event failed: %s\n", attachRes.msg().c_str()); return PyPerfResult::EVENT_ATTACH_FAIL; @@ -269,7 +269,7 @@ PyPerfUtil::PyPerfResult PyPerfUtil::profile(int64_t sampleRate, // Detach the event auto detachRes = bpf_.detach_perf_event(0, 0); - if (detachRes.code() != 0) { + if (!detachRes.ok()) { std::fprintf(stderr, "Detach CPU cycles event failed: %s\n", detachRes.msg().c_str()); return PyPerfResult::EVENT_DETACH_FAIL; -- 2.34.1