Commit 8ad9ba03 authored by 4ast's avatar 4ast Committed by GitHub

Merge pull request #824 from palmtenor/exception_h

Clean up bcc_exception.h
parents 8ff670d9 259ff4e2
......@@ -61,15 +61,15 @@ 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 (std::get<0>(init_res) != 0) {
std::cerr << std::get<1>(init_res) << std::endl;
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
auto attach_res =
bpf.attach_kprobe("finish_task_switch", "task_switch_event");
if (std::get<0>(attach_res) != 0) {
std::cerr << std::get<1>(attach_res) << std::endl;
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
......@@ -88,8 +88,8 @@ int main(int argc, char** argv) {
}
auto detach_res = bpf.detach_kprobe("finish_task_switch");
if (std::get<0>(detach_res) != 0) {
std::cerr << std::get<1>(detach_res) << std::endl;
if (detach_res.code() != 0) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
......
......@@ -20,8 +20,8 @@ int on_sys_clone(void *ctx) {
int main() {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (std::get<0>(init_res) != 0) {
std::cerr << std::get<1>(init_res) << std::endl;
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
......@@ -29,8 +29,8 @@ int main() {
std::string line;
auto attach_res = bpf.attach_kprobe("sys_clone", "on_sys_clone");
if (std::get<0>(attach_res) != 0) {
std::cerr << std::get<1>(attach_res) << std::endl;
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
......@@ -39,8 +39,8 @@ int main() {
std::cout << line << std::endl;
// Detach the probe if we got at least one line.
auto detach_res = bpf.detach_kprobe("sys_clone");
if (std::get<0>(detach_res) != 0) {
std::cerr << std::get<1>(detach_res) << std::endl;
if (detach_res.code() != 0) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
break;
......
......@@ -70,21 +70,21 @@ void signal_handler(int s) {
int main(int argc, char** argv) {
bpf = new ebpf::BPF();
auto init_res = bpf->init(BPF_PROGRAM);
if (std::get<0>(init_res) != 0) {
std::cerr << std::get<1>(init_res) << std::endl;
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
auto attach_res =
bpf->attach_tracepoint("random:urandom_read", "on_urandom_read");
if (std::get<0>(attach_res) != 0) {
std::cerr << std::get<1>(attach_res) << std::endl;
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
auto open_res = bpf->open_perf_buffer("events", &handle_output);
if (std::get<0>(open_res) != 0) {
std::cerr << std::get<1>(open_res) << std::endl;
if (open_res.code() != 0) {
std::cerr << open_res.msg() << std::endl;
return 1;
}
......
......@@ -63,15 +63,15 @@ int main(int argc, char** argv) {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (std::get<0>(init_res) != 0) {
std::cerr << std::get<1>(init_res) << std::endl;
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
auto attach_res =
bpf.attach_uprobe(mysql_path, ALLOC_QUERY_FUNC, "probe_mysql_query");
if (std::get<0>(attach_res) != 0) {
std::cerr << std::get<1>(attach_res) << std::endl;
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
......@@ -94,8 +94,8 @@ int main(int argc, char** argv) {
}
auto detach_res = bpf.detach_uprobe(mysql_path, ALLOC_QUERY_FUNC);
if (std::get<0>(detach_res) != 0) {
std::cerr << std::get<1>(detach_res) << std::endl;
if (detach_res.code() != 0) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
......
......@@ -58,14 +58,14 @@ struct stack_key_t {
int main(int argc, char** argv) {
ebpf::BPF bpf;
auto init_res = bpf.init(BPF_PROGRAM);
if (std::get<0>(init_res) != 0) {
std::cerr << std::get<1>(init_res) << std::endl;
if (init_res.code() != 0) {
std::cerr << init_res.msg() << std::endl;
return 1;
}
auto attach_res = bpf.attach_kprobe("tcp_sendmsg", "on_tcp_send");
if (std::get<0>(attach_res) != 0) {
std::cerr << std::get<1>(attach_res) << std::endl;
if (attach_res.code() != 0) {
std::cerr << attach_res.msg() << std::endl;
return 1;
}
......@@ -105,8 +105,8 @@ int main(int argc, char** argv) {
}
auto detach_res = bpf.detach_kprobe("tcp_sendmsg");
if (std::get<0>(detach_res) != 0) {
std::cerr << std::get<1>(detach_res) << std::endl;
if (detach_res.code() != 0) {
std::cerr << detach_res.msg() << std::endl;
return 1;
}
......
This diff is collapsed.
......@@ -68,36 +68,36 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
StatusTuple BPFPerfBuffer::open_on_cpu(perf_reader_raw_cb cb, int cpu,
void* cb_cookie) {
if (cpu_readers_.find(cpu) != cpu_readers_.end())
return mkstatus(-1, "Perf buffer already open on CPU %d", cpu);
return StatusTuple(-1, "Perf buffer already open on CPU %d", cpu);
auto reader =
static_cast<perf_reader*>(bpf_open_perf_buffer(cb, cb_cookie, -1, cpu));
if (reader == nullptr)
return mkstatus(-1, "Unable to construct perf reader");
return StatusTuple(-1, "Unable to construct perf reader");
int reader_fd = perf_reader_fd(reader);
if (!update(&cpu, &reader_fd)) {
perf_reader_free(static_cast<void*>(reader));
return mkstatus(-1, "Unable to open perf buffer on CPU %d: %s", cpu,
strerror(errno));
return StatusTuple(-1, "Unable to open perf buffer on CPU %d: %s", cpu,
strerror(errno));
}
cpu_readers_[cpu] = static_cast<perf_reader*>(reader);
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple BPFPerfBuffer::open(perf_reader_raw_cb cb, void* cb_cookie) {
for (int i = 0; i < sysconf(_SC_NPROCESSORS_ONLN); i++)
TRY2(open_on_cpu(cb, i, cb_cookie));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple BPFPerfBuffer::close_on_cpu(int cpu) {
auto it = cpu_readers_.find(cpu);
if (it == cpu_readers_.end())
return mkstatus(0);
return StatusTuple(0);
perf_reader_free(static_cast<void*>(it->second));
if (!remove(const_cast<int*>(&(it->first))))
return mkstatus(-1, "Unable to close perf buffer on CPU %d", it->first);
return StatusTuple(-1, "Unable to close perf buffer on CPU %d", it->first);
cpu_readers_.erase(it);
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple BPFPerfBuffer::close() {
......@@ -105,15 +105,15 @@ StatusTuple BPFPerfBuffer::close() {
bool has_error = false;
for (int i = 0; i < sysconf(_SC_NPROCESSORS_ONLN); i++) {
auto res = close_on_cpu(i);
if (std::get<0>(res) != 0) {
if (res.code() != 0) {
errors += "Failed to close CPU" + std::to_string(i) + " perf buffer: ";
errors += std::get<1>(res) + "\n";
errors += res.msg() + "\n";
has_error = true;
}
}
if (has_error)
return mkstatus(-1, errors);
return mkstatus(0);
return StatusTuple(-1, errors);
return StatusTuple(0);
}
void BPFPerfBuffer::poll(int timeout) {
......@@ -126,9 +126,9 @@ void BPFPerfBuffer::poll(int timeout) {
BPFPerfBuffer::~BPFPerfBuffer() {
auto res = close();
if (std::get<0>(res) != 0)
if (res.code() != 0)
std::cerr << "Failed to close all perf buffer on destruction: "
<< std::get<1>(res) << std::endl;
<< res.msg() << std::endl;
}
} // namespace ebpf
......@@ -16,53 +16,46 @@
#pragma once
#include <exception>
#include <cstdio>
#include <string>
#include <tuple>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#undef NDEBUG
namespace ebpf {
typedef std::tuple<int, std::string> StatusTuple;
class StatusTuple {
public:
StatusTuple(int ret) : ret_(ret) {}
template <typename... Args>
StatusTuple mkstatus(int ret, const char *fmt, Args... args) {
char buf[1024];
snprintf(buf, sizeof(buf), fmt, args...);
return std::make_tuple(ret, std::string(buf));
}
StatusTuple(int ret, const char *msg) : ret_(ret), msg_(msg) {}
static inline StatusTuple mkstatus(int ret, const char *msg) {
return std::make_tuple(ret, std::string(msg));
}
StatusTuple(int ret, const std::string &msg) : ret_(ret), msg_(msg) {}
static inline StatusTuple mkstatus(
int ret, const std::string& msg
) {
return std::make_tuple(ret, msg);
}
template <typename... Args>
StatusTuple(int ret, const char *fmt, Args... args) : ret_(ret) {
char buf[2048];
snprintf(buf, sizeof(buf), fmt, args...);
msg_ = std::string(buf);
}
static inline StatusTuple mkstatus(int ret) {
return std::make_tuple(ret, std::string());
}
void append_msg(const std::string& msg) {
msg_ += msg;
}
#define TRY(CMD) \
do { \
int __status = (CMD); \
if (__status != 0) { \
return __status; \
} \
} while (0)
int code() { return ret_; }
std::string msg() { return msg_; }
private:
int ret_;
std::string msg_;
};
#define TRY2(CMD) \
do { \
#define TRY2(CMD) \
do { \
StatusTuple __stp = (CMD); \
if (std::get<0>(__stp) != 0) { \
return __stp; \
} \
if (__stp.code() != 0) { \
return __stp; \
} \
} while (0)
} // namespace ebpf
This diff is collapsed.
......@@ -20,7 +20,6 @@
#include "loader.h"
#include "table_desc.h"
using std::get;
using std::string;
using std::unique_ptr;
using std::vector;
......@@ -57,8 +56,8 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot
ebpf::cc::TypeCheck type_check(parser_->scopes_.get(), proto_parser_->scopes_.get());
auto ret = type_check.visit(parser_->root_node_);
if (get<0>(ret) != 0 || get<1>(ret).size()) {
fprintf(stderr, "Type error @line=%d: %s\n", get<0>(ret), get<1>(ret).c_str());
if (ret.code() != 0 || ret.msg().size()) {
fprintf(stderr, "Type error @line=%d: %s\n", ret.code(), ret.msg().c_str());
return -1;
}
......@@ -66,9 +65,9 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot
codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(mod, parser_->scopes_.get(), proto_parser_->scopes_.get());
ret = codegen_->visit(parser_->root_node_, **tables);
if (get<0>(ret) != 0 || get<1>(ret).size()) {
fprintf(stderr, "Codegen error @line=%d: %s\n", get<0>(ret), get<1>(ret).c_str());
return get<0>(ret);
if (ret.code() != 0 || ret.msg().size()) {
fprintf(stderr, "Codegen error @line=%d: %s\n", ret.code(), ret.msg().c_str());
return ret.code();
}
return 0;
......
......@@ -114,20 +114,18 @@ class Node {
};
template <typename... Args>
std::tuple<int, std::string> mkstatus_(Node *n, const char *fmt, Args... args) {
char buf[1024];
snprintf(buf, sizeof(buf), fmt, args...);
string out_msg(buf);
StatusTuple mkstatus_(Node *n, const char *fmt, Args... args) {
StatusTuple status = StatusTuple(n->line_ ? n->line_ : -1, fmt, args...);
if (n->line_ > 0)
out_msg += "\n" + n->text_;
return std::make_tuple(n->line_ ? n->line_ : -1, out_msg);
status.append_msg("\n" + n->text_);
return status;
}
static inline std::tuple<int, std::string> mkstatus_(Node *n, const char *msg) {
string out_msg(msg);
static inline StatusTuple mkstatus_(Node *n, const char *msg) {
StatusTuple status = StatusTuple(n->line_ ? n->line_ : -1, msg);
if (n->line_ > 0)
out_msg += "\n" + n->text_;
return std::make_tuple(n->line_ ? n->line_ : -1, out_msg);
status.append_msg("\n" + n->text_);
return status;
}
class StmtNode : public Node {
......
......@@ -38,7 +38,7 @@ StatusTuple Printer::visit_block_stmt_node(BlockStmtNode* n) {
--indent_;
}
fprintf(out_, "%*s}", indent_, "");
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_if_stmt_node(IfStmtNode* n) {
......@@ -50,7 +50,7 @@ StatusTuple Printer::visit_if_stmt_node(IfStmtNode* n) {
fprintf(out_, " else ");
TRY2(n->false_block_->accept(this));
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_onvalid_stmt_node(OnValidStmtNode* n) {
......@@ -62,7 +62,7 @@ StatusTuple Printer::visit_onvalid_stmt_node(OnValidStmtNode* n) {
fprintf(out_, " else ");
TRY2(n->else_block_->accept(this));
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_switch_stmt_node(SwitchStmtNode* n) {
......@@ -70,7 +70,7 @@ StatusTuple Printer::visit_switch_stmt_node(SwitchStmtNode* n) {
TRY2(n->cond_->accept(this));
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_case_stmt_node(CaseStmtNode* n) {
......@@ -81,7 +81,7 @@ StatusTuple Printer::visit_case_stmt_node(CaseStmtNode* n) {
fprintf(out_, "default");
}
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_ident_expr_node(IdentExprNode* n) {
......@@ -92,37 +92,37 @@ StatusTuple Printer::visit_ident_expr_node(IdentExprNode* n) {
if (n->sub_name_.size()) {
fprintf(out_, ".%s", n->sub_name_.c_str());
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_assign_expr_node(AssignExprNode* n) {
TRY2(n->lhs_->accept(this));
fprintf(out_, " = ");
TRY2(n->rhs_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_packet_expr_node(PacketExprNode* n) {
fprintf(out_, "$");
TRY2(n->id_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_integer_expr_node(IntegerExprNode* n) {
fprintf(out_, "%s:%zu", n->val_.c_str(), n->bits_);
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_string_expr_node(StringExprNode *n) {
fprintf(out_, "%s", n->val_.c_str());
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_binop_expr_node(BinopExprNode* n) {
TRY2(n->lhs_->accept(this));
fprintf(out_, "%d", n->op_);
TRY2(n->rhs_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_unop_expr_node(UnopExprNode* n) {
......@@ -135,25 +135,25 @@ StatusTuple Printer::visit_unop_expr_node(UnopExprNode* n) {
}
fprintf(out_, "%s", s);
TRY2(n->expr_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_bitop_expr_node(BitopExprNode* n) {
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_return_expr_node(ReturnExprNode* n) {
fprintf(out_, "return ");
TRY2(n->expr_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_goto_expr_node(GotoExprNode* n) {
const char* s = n->is_continue_ ? "continue " : "goto ";
fprintf(out_, "%s", s);
TRY2(n->id_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_method_call_expr_node(MethodCallExprNode* n) {
......@@ -177,19 +177,19 @@ StatusTuple Printer::visit_method_call_expr_node(MethodCallExprNode* n) {
--indent_;
fprintf(out_, "%*s}", indent_, "");
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_table_index_expr_node(TableIndexExprNode *n) {
fprintf(out_, "%s[", n->id_->c_str());
TRY2(n->index_->accept(this));
fprintf(out_, "]");
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_expr_stmt_node(ExprStmtNode* n) {
TRY2(n->expr_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_struct_variable_decl_stmt_node(StructVariableDeclStmtNode* n) {
......@@ -207,7 +207,7 @@ StatusTuple Printer::visit_struct_variable_decl_stmt_node(StructVariableDeclStmt
}
fprintf(out_, "}");
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_integer_variable_decl_stmt_node(IntegerVariableDeclStmtNode* n) {
......@@ -218,7 +218,7 @@ StatusTuple Printer::visit_integer_variable_decl_stmt_node(IntegerVariableDeclSt
fprintf(out_, "; ");
TRY2(n->init_[0]->accept(this));
}
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_struct_decl_stmt_node(StructDeclStmtNode* n) {
......@@ -233,12 +233,12 @@ StatusTuple Printer::visit_struct_decl_stmt_node(StructDeclStmtNode* n) {
}
--indent_;
fprintf(out_, "%*s}", indent_, "");
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_state_decl_stmt_node(StateDeclStmtNode* n) {
if (!n->id_) {
return mkstatus(0);
return StatusTuple(0);
}
fprintf(out_, "state ");
TRY2(n->id_->accept(this));
......@@ -249,11 +249,11 @@ StatusTuple Printer::visit_state_decl_stmt_node(StateDeclStmtNode* n) {
// TRY2(n->id2_->accept(this));
//}
//TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_parser_state_stmt_node(ParserStateStmtNode* n) {
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_match_decl_stmt_node(MatchDeclStmtNode* n) {
......@@ -268,7 +268,7 @@ StatusTuple Printer::visit_match_decl_stmt_node(MatchDeclStmtNode* n) {
}
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_miss_decl_stmt_node(MissDeclStmtNode* n) {
......@@ -283,7 +283,7 @@ StatusTuple Printer::visit_miss_decl_stmt_node(MissDeclStmtNode* n) {
}
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_failure_decl_stmt_node(FailureDeclStmtNode* n) {
......@@ -298,7 +298,7 @@ StatusTuple Printer::visit_failure_decl_stmt_node(FailureDeclStmtNode* n) {
}
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_table_decl_stmt_node(TableDeclStmtNode* n) {
......@@ -313,7 +313,7 @@ StatusTuple Printer::visit_table_decl_stmt_node(TableDeclStmtNode* n) {
fprintf(out_, "> ");
TRY2(n->id_->accept(this));
fprintf(out_, "(%zu)", n->size_);
return mkstatus(0);
return StatusTuple(0);
}
StatusTuple Printer::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
......@@ -328,7 +328,7 @@ StatusTuple Printer::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
}
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
return StatusTuple(0);
}
} // namespace cc
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment