Skip to content

Commit

Permalink
Add 'get_helper_error_msg' utility
Browse files Browse the repository at this point in the history
This is for additional helpful messaging for specific errors e.g.
for when the number of bpf map entries is exceeded.

Follow up to: #2809
  • Loading branch information
Jordan Rome committed Jan 7, 2024
1 parent c1d338d commit 610b164
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
10 changes: 1 addition & 9 deletions src/bpftrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
#include "triggers.h"
#include "utils.h"

namespace libbpf {
#define __BPF_NAME_FN(x) #x
const char *bpf_func_name[] = { __BPF_FUNC_MAPPER(__BPF_NAME_FN) };
#undef __BPF_NAME_FN
} // namespace libbpf

namespace bpftrace {

DebugLevel bt_debug = DebugLevel::kNone;
Expand Down Expand Up @@ -541,9 +535,7 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
auto error_id = helpererror->error_id;
auto return_value = helpererror->return_value;
auto &info = bpftrace->resources.helper_error_info[error_id];
bpftrace->out_->helper_error(libbpf::bpf_func_name[info.func_id],
return_value,
info.loc);
bpftrace->out_->helper_error(info.func_id, return_value, info.loc);
return;
}
else if (printf_id == asyncactionint(AsyncAction::watchpoint_attach))
Expand Down
30 changes: 25 additions & 5 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
#include "utils.h"
#include <async_event_types.h>

#include <bpf/libbpf.h>

namespace libbpf {
#define __BPF_NAME_FN(x) #x
const char *bpf_func_name[] = { __BPF_FUNC_MAPPER(__BPF_NAME_FN) };
#undef __BPF_NAME_FN
} // namespace libbpf

namespace bpftrace {

namespace {
Expand Down Expand Up @@ -147,6 +155,16 @@ void Output::lhist_prepare(const std::vector<uint64_t> &values, int min, int max
}
}

std::string Output::get_helper_error_msg(int func_id, int retcode) const
{
std::string msg = strerror(-retcode);
if (func_id == libbpf::BPF_FUNC_map_update_elem && retcode == -E2BIG)
{
msg.append(" (trying increasing MAP_KEYS_MAX)");
}
return msg;
}

std::string Output::value_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
Expand Down Expand Up @@ -565,14 +583,14 @@ void TextOutput::attached_probes(uint64_t num_probes) const
out_ << "Attaching " << num_probes << " probes..." << std::endl;
}

void TextOutput::helper_error(const std::string &helper,
void TextOutput::helper_error(int func_id,
int retcode,
const location &loc) const
{
std::stringstream msg;
msg << "Failed to " << helper << ": ";
msg << "Failed to " << libbpf::bpf_func_name[func_id] << ": ";
if (retcode < 0)
msg << strerror(-retcode) << " (" << retcode << ")";
msg << get_helper_error_msg(func_id, retcode) << " (" << retcode << ")";
else
msg << retcode;
LOG(WARNING, loc, out_) << msg.str();
Expand Down Expand Up @@ -855,11 +873,13 @@ void JsonOutput::attached_probes(uint64_t num_probes) const
message(MessageType::attached_probes, "probes", num_probes);
}

void JsonOutput::helper_error(const std::string &helper,
void JsonOutput::helper_error(int func_id,
int retcode,
const location &loc) const
{
out_ << "{\"type\": \"helper_error\", \"helper\": \"" << helper
out_ << "{\"type\": \"helper_error\", \"helper\": \""
<< libbpf::bpf_func_name[func_id] << "\", \"msg\": \""
<< get_helper_error_msg(func_id, retcode)
<< "\", \"retcode\": " << retcode << ", \"line\": " << loc.begin.line
<< ", \"col\": " << loc.begin.column << "}" << std::endl;
}
Expand Down
7 changes: 4 additions & 3 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Output
virtual void message(MessageType type, const std::string& msg, bool nl = true) const = 0;
virtual void lost_events(uint64_t lost) const = 0;
virtual void attached_probes(uint64_t num_probes) const = 0;
virtual void helper_error(const std::string &helper,
virtual void helper_error(int func_id,
int retcode,
const location &loc) const = 0;

Expand All @@ -86,6 +86,7 @@ class Output
std::ostream &err_;
void hist_prepare(const std::vector<uint64_t> &values, int &min_index, int &max_index, int &max_value) const;
void lhist_prepare(const std::vector<uint64_t> &values, int min, int max, int step, int &max_index, int &max_value, int &buckets, int &start_value, int &end_value) const;
std::string get_helper_error_msg(int func_id, int retcode) const;
// Convert a log2 histogram into string
virtual std::string hist_to_str(const std::vector<uint64_t> &values,
uint32_t div,
Expand Down Expand Up @@ -194,7 +195,7 @@ class TextOutput : public Output {
void message(MessageType type, const std::string& msg, bool nl = true) const override;
void lost_events(uint64_t lost) const override;
void attached_probes(uint64_t num_probes) const override;
void helper_error(const std::string &helper,
void helper_error(int func_id,
int retcode,
const location &loc) const override;

Expand Down Expand Up @@ -249,7 +250,7 @@ class JsonOutput : public Output {
void message(MessageType type, const std::string& field, uint64_t value) const;
void lost_events(uint64_t lost) const override;
void attached_probes(uint64_t num_probes) const override;
void helper_error(const std::string &helper,
void helper_error(int func_id,
int retcode,
const location &loc) const override;

Expand Down

0 comments on commit 610b164

Please sign in to comment.