diff --git a/src/bpfbytecode.cpp b/src/bpfbytecode.cpp index 86be181e..4e878c85 100644 --- a/src/bpfbytecode.cpp +++ b/src/bpfbytecode.cpp @@ -11,7 +11,7 @@ namespace bpftrace { -BpfBytecode::BpfBytecode(const void *elf, size_t elf_size, Config &config) +BpfBytecode::BpfBytecode(const void *elf, size_t elf_size, const Config &config) : log_size_(config.get(ConfigKeyInt::log_size)) { int log_level = 0; @@ -131,15 +131,15 @@ void maybe_throw_helper_verifier_error(std::string_view log, // so we try to find it. If it's not there, it's very likely that the log has // been trimmed due to insufficient log limit. This function checks if that // happened. -bool is_log_trimmed(const std::string &log) +bool is_log_trimmed(std::string_view log) { - std::vector tokens = { "processed", "insns" }; + static const std::vector tokens = { "processed", "insns" }; return !wildcard_match(log, tokens, true, true); } } // namespace void BpfBytecode::load_progs(const RequiredResources &resources, - BTF &btf, + const BTF &btf, BPFfeature &feature, const Config &config) { @@ -150,7 +150,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources, int res = bpf_object__load(bpf_object_.get()); // If requested, print the entire verifier logs, even if loading succeeded. - for (auto &[name, prog] : programs_) { + for (const auto &[name, prog] : programs_) { if (bt_debug.find(DebugStage::Verifier) != bt_debug.end()) { std::cout << "BPF verifier log for " << name << ":\n"; std::cout << "--------------------------------------\n"; @@ -164,7 +164,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources, // If loading of bpf_object failed, we try to give user some hints of what // could've gone wrong. std::ostringstream err; - for (auto &[name, prog] : programs_) { + for (const auto &[name, prog] : programs_) { if (res == 0 || prog.fd() >= 0) continue; @@ -172,7 +172,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources, // caused the failure. It can mean that libbpf didn't even try to load it // b/c some other program failed to load. So, we only log program load // failures when the verifier log is non-empty. - std::string log(prog.log_buf()); + std::string_view log(prog.log_buf()); if (!log.empty()) { // This should be the only error that may occur here and does not imply // a bpftrace bug so throw immediately with a proper error message. @@ -203,7 +203,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources, if (err.str().empty()) { // The problem does not seem to be in program loading. It may be something // else (e.g. maps failing to load) but we're not able to figure out what - // it is so advice user to check libbf output which should contain more + // it is so advise user to check libbf output which should contain more // information. LOG(ERROR, err) << "Unknown BPF object load failure. Try using the \"-d libbpf\" " @@ -215,7 +215,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources, } void BpfBytecode::prepare_progs(const std::vector &probes, - BTF &btf, + const BTF &btf, BPFfeature &feature, const Config &config) { @@ -230,7 +230,7 @@ void BpfBytecode::prepare_progs(const std::vector &probes, bool BpfBytecode::all_progs_loaded() { - for (auto &prog : programs_) { + for (const auto &prog : programs_) { if (prog.second.fd() < 0) return false; } diff --git a/src/bpfbytecode.h b/src/bpfbytecode.h index 8867ce59..80733354 100644 --- a/src/bpfbytecode.h +++ b/src/bpfbytecode.h @@ -23,7 +23,7 @@ class BpfBytecode { BpfBytecode() { } - BpfBytecode(const void *elf, size_t elf_size, Config &config); + BpfBytecode(const void *elf, size_t elf_size, const Config &config); BpfBytecode(const BpfBytecode &) = delete; BpfBytecode &operator=(const BpfBytecode &) = delete; @@ -31,7 +31,7 @@ class BpfBytecode { BpfBytecode &operator=(BpfBytecode &&) = default; void load_progs(const RequiredResources &resources, - BTF &btf, + const BTF &btf, BPFfeature &feature, const Config &config); @@ -50,7 +50,7 @@ class BpfBytecode { private: void prepare_progs(const std::vector &probes, - BTF &btf, + const BTF &btf, BPFfeature &feature, const Config &config); bool all_progs_loaded(); diff --git a/src/bpfprogram.cpp b/src/bpfprogram.cpp index e3751d0f..9e8e9741 100644 --- a/src/bpfprogram.cpp +++ b/src/bpfprogram.cpp @@ -57,17 +57,18 @@ void BpfProgram::set_expected_attach_type(const Probe &probe, } void BpfProgram::set_attach_target(const Probe &probe, - BTF &btf, + const BTF &btf, const Config &config) { if (probe.type != ProbeType::kfunc && probe.type != ProbeType::kretfunc && probe.type != ProbeType::iter) return; - std::string mod = probe.path; - std::string fun = probe.attach_point; + const std::string &mod = probe.path; + const std::string &fun = probe.attach_point; - std::string btf_fun = probe.type == ProbeType::iter ? "bpf_iter_" + fun : fun; + const std::string &btf_fun = probe.type == ProbeType::iter ? "bpf_iter_" + fun + : fun; if (btf.get_btf_id(btf_fun, mod) < 0) { std::string msg = "No BTF found for " + mod + ":" + fun; if (probe.orig_name != probe.name && @@ -82,7 +83,7 @@ void BpfProgram::set_attach_target(const Probe &probe, } } - auto attach_target = !mod.empty() ? mod + ":" + fun : fun; + const std::string &attach_target = !mod.empty() ? mod + ":" + fun : fun; bpf_program__set_attach_target(bpf_prog_, 0, attach_target.c_str()); } diff --git a/src/bpfprogram.h b/src/bpfprogram.h index 3139c69a..274462b1 100644 --- a/src/bpfprogram.h +++ b/src/bpfprogram.h @@ -20,7 +20,9 @@ class BpfProgram { void set_prog_type(const Probe &probe, BPFfeature &feature); void set_expected_attach_type(const Probe &probe, BPFfeature &feature); - void set_attach_target(const Probe &probe, BTF &btf, const Config &config); + void set_attach_target(const Probe &probe, + const BTF &btf, + const Config &config); void set_no_autoattach(); int fd() const; diff --git a/src/utils.cpp b/src/utils.cpp index 0b1cb61a..89001cb1 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -429,8 +429,8 @@ std::string erase_prefix(std::string &str) return prefix; } -bool wildcard_match(const std::string &str, - std::vector &tokens, +bool wildcard_match(std::string_view str, + const std::vector &tokens, bool start_wildcard, bool end_wildcard) { @@ -440,7 +440,7 @@ bool wildcard_match(const std::string &str, if (str.find(tokens[0], next) != next) return false; - for (std::string token : tokens) { + for (const std::string &token : tokens) { size_t found = str.find(token, next); if (found == std::string::npos) return false; diff --git a/src/utils.h b/src/utils.h index 2c1f56cc..33d0b6f5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -182,8 +182,8 @@ std::vector split_string(const std::string &str, char delimiter, bool remove_empty = false); std::string erase_prefix(std::string &str); -bool wildcard_match(const std::string &str, - std::vector &tokens, +bool wildcard_match(std::string_view str, + const std::vector &tokens, bool start_wildcard, bool end_wildcard); std::vector get_wildcard_tokens(const std::string &input, diff --git a/tests/bpfbytecode.cpp b/tests/bpfbytecode.cpp index aade7a0b..c669e03c 100644 --- a/tests/bpfbytecode.cpp +++ b/tests/bpfbytecode.cpp @@ -10,7 +10,7 @@ namespace bpftrace { namespace test { namespace bpfbytecode { -BpfBytecode codegen(const std::string &input) +BpfBytecode codegen(std::string_view input) { auto bpftrace = get_mock_bpftrace(); @@ -35,8 +35,9 @@ TEST(bpfbytecode, create_programs) auto &program = bytecode.getProgramForProbe(foo); - EXPECT_EQ(std::string(bpf_program__name(program.bpf_prog())), "kprobe_foo_1"); - EXPECT_EQ(std::string(bpf_program__section_name(program.bpf_prog())), + EXPECT_EQ(std::string_view{ bpf_program__name(program.bpf_prog()) }, + "kprobe_foo_1"); + EXPECT_EQ(std::string_view{ bpf_program__section_name(program.bpf_prog()) }, "s_kprobe_foo_1"); }