Skip to content

Commit

Permalink
Cleanups: Use "const" and reduce string copies
Browse files Browse the repository at this point in the history
  • Loading branch information
ajor authored and viktormalik committed Jul 8, 2024
1 parent af32e6b commit fe9c5b4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
20 changes: 10 additions & 10 deletions src/bpfbytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::string> tokens = { "processed", "insns" };
static const std::vector<std::string> 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)
{
Expand All @@ -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";
Expand All @@ -164,15 164,15 @@ 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;

// Unfortunately, a negative fd does not mean that this specific program
// 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.
Expand Down Expand Up @@ -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\" "
Expand All @@ -215,7 215,7 @@ void BpfBytecode::load_progs(const RequiredResources &resources,
}

void BpfBytecode::prepare_progs(const std::vector<Probe> &probes,
BTF &btf,
const BTF &btf,
BPFfeature &feature,
const Config &config)
{
Expand All @@ -230,7 230,7 @@ void BpfBytecode::prepare_progs(const std::vector<Probe> &probes,

bool BpfBytecode::all_progs_loaded()
{
for (auto &prog : programs_) {
for (const auto &prog : programs_) {
if (prog.second.fd() < 0)
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/bpfbytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 23,15 @@ 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;
BpfBytecode(BpfBytecode &&) = default;
BpfBytecode &operator=(BpfBytecode &&) = default;

void load_progs(const RequiredResources &resources,
BTF &btf,
const BTF &btf,
BPFfeature &feature,
const Config &config);

Expand All @@ -50,7 50,7 @@ class BpfBytecode {

private:
void prepare_progs(const std::vector<Probe> &probes,
BTF &btf,
const BTF &btf,
BPFfeature &feature,
const Config &config);
bool all_progs_loaded();
Expand Down
11 changes: 6 additions & 5 deletions src/bpfprogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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());
}

Expand Down
4 changes: 3 additions & 1 deletion src/bpfprogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 429,8 @@ std::string erase_prefix(std::string &str)
return prefix;
}

bool wildcard_match(const std::string &str,
std::vector<std::string> &tokens,
bool wildcard_match(std::string_view str,
const std::vector<std::string> &tokens,
bool start_wildcard,
bool end_wildcard)
{
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 182,8 @@ std::vector<std::string> 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<std::string> &tokens,
bool wildcard_match(std::string_view str,
const std::vector<std::string> &tokens,
bool start_wildcard,
bool end_wildcard);
std::vector<std::string> get_wildcard_tokens(const std::string &input,
Expand Down
7 changes: 4 additions & 3 deletions tests/bpfbytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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");
}

Expand Down

0 comments on commit fe9c5b4

Please sign in to comment.