From b13426caf3bbb80859eaab0a63fdad7c2a42e89c Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Tue, 8 Sep 2015 22:11:46 -0700 Subject: [PATCH] Add debug flag for printing rewritten C text * Many times it is useful to print out the C file after the BFrontendAction has run. e.g.: BPF("file.c", debug=0x4) Signed-off-by: Brenden Blanco --- src/cc/bpf_module.cc | 6 +++--- src/cc/frontends/b/loader.cc | 3 ++- src/cc/frontends/b/loader.h | 3 ++- src/cc/frontends/clang/b_frontend_action.cc | 7 ++++--- src/cc/frontends/clang/b_frontend_action.h | 6 +++--- src/cc/frontends/clang/loader.cc | 6 +++--- src/cc/frontends/clang/loader.h | 3 ++- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/cc/bpf_module.cc b/src/cc/bpf_module.cc index 6738e3a..001419e 100644 --- a/src/cc/bpf_module.cc +++ b/src/cc/bpf_module.cc @@ -295,7 +295,7 @@ unique_ptr BPFModule::finalize_rw(unique_ptr m) { // load an entire c file as a module int BPFModule::load_cfile(const string &file, bool in_memory) { - clang_loader_ = make_unique(&*ctx_); + clang_loader_ = make_unique(&*ctx_, flags_); if (clang_loader_->parse(&mod_, &tables_, file, in_memory)) return -1; return 0; @@ -307,7 +307,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory) { // Load in a pre-built list of functions into the initial Module object, then // build an ExecutionEngine. int BPFModule::load_includes(const string &tmpfile) { - clang_loader_ = make_unique(&*ctx_); + clang_loader_ = make_unique(&*ctx_, flags_); if (clang_loader_->parse(&mod_, &tables_, tmpfile, false)) return -1; return 0; @@ -652,7 +652,7 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) { if (int rc = load_includes(BCC_INSTALL_PREFIX "/share/bcc/include/bcc/helpers.h")) return rc; - b_loader_.reset(new BLoader); + b_loader_.reset(new BLoader(flags_)); if (int rc = b_loader_->parse(&*mod_, filename, proto_filename, &tables_)) return rc; if (int rc = annotate()) diff --git a/src/cc/frontends/b/loader.cc b/src/cc/frontends/b/loader.cc index 8c05193..b26e945 100644 --- a/src/cc/frontends/b/loader.cc +++ b/src/cc/frontends/b/loader.cc @@ -27,7 +27,8 @@ using std::vector; namespace ebpf { -BLoader::BLoader() { +BLoader::BLoader(unsigned flags) : flags_(flags) { + (void)flags_; } BLoader::~BLoader() { diff --git a/src/cc/frontends/b/loader.h b/src/cc/frontends/b/loader.h index 772b505..3e3570d 100644 --- a/src/cc/frontends/b/loader.h +++ b/src/cc/frontends/b/loader.h @@ -35,11 +35,12 @@ class CodegenLLVM; class BLoader { public: - BLoader(); + explicit BLoader(unsigned flags); ~BLoader(); int parse(llvm::Module *mod, const std::string &filename, const std::string &proto_filename, std::unique_ptr> *tables); private: + unsigned flags_; std::unique_ptr parser_; std::unique_ptr proto_parser_; std::unique_ptr codegen_; diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc index 803efa4..cdb51e8 100644 --- a/src/cc/frontends/clang/b_frontend_action.cc +++ b/src/cc/frontends/clang/b_frontend_action.cc @@ -442,13 +442,14 @@ bool BTypeConsumer::HandleTopLevelDecl(DeclGroupRef D) { return true; } -BFrontendAction::BFrontendAction(llvm::raw_ostream &os) - : rewriter_(new Rewriter), os_(os), tables_(new vector) { +BFrontendAction::BFrontendAction(llvm::raw_ostream &os, unsigned flags) + : os_(os), flags_(flags), rewriter_(new Rewriter), tables_(new vector) { } void BFrontendAction::EndSourceFileAction() { // uncomment to see rewritten source - //rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(llvm::errs()); + if (flags_ & 0x4) + rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(llvm::errs()); rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(os_); os_.flush(); } diff --git a/src/cc/frontends/clang/b_frontend_action.h b/src/cc/frontends/clang/b_frontend_action.h index b21e72a..aa61638 100644 --- a/src/cc/frontends/clang/b_frontend_action.h +++ b/src/cc/frontends/clang/b_frontend_action.h @@ -67,7 +67,6 @@ class BTypeVisitor : public clang::RecursiveASTVisitor { bool VisitCallExpr(clang::CallExpr *Call); bool VisitVarDecl(clang::VarDecl *Decl); bool VisitMemberExpr(clang::MemberExpr *E); - bool VisitDeclRefExpr(clang::DeclRefExpr *E); bool VisitBinaryOperator(clang::BinaryOperator *E); bool VisitImplicitCastExpr(clang::ImplicitCastExpr *E); @@ -96,7 +95,7 @@ class BFrontendAction : public clang::ASTFrontendAction { public: // Initialize with the output stream where the new source file contents // should be written. - explicit BFrontendAction(llvm::raw_ostream &os); + BFrontendAction(llvm::raw_ostream &os, unsigned flags); // Called by clang when the AST has been completed, here the output stream // will be flushed. @@ -108,8 +107,9 @@ class BFrontendAction : public clang::ASTFrontendAction { // take ownership of the table-to-fd mapping data structure std::unique_ptr> take_tables() { return move(tables_); } private: - std::unique_ptr rewriter_; llvm::raw_ostream &os_; + unsigned flags_; + std::unique_ptr rewriter_; std::unique_ptr> tables_; }; diff --git a/src/cc/frontends/clang/loader.cc b/src/cc/frontends/clang/loader.cc index c4ee097..d930faa 100644 --- a/src/cc/frontends/clang/loader.cc +++ b/src/cc/frontends/clang/loader.cc @@ -58,8 +58,8 @@ using std::vector; namespace ebpf { -ClangLoader::ClangLoader(llvm::LLVMContext *ctx) - : ctx_(ctx) +ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags) + : ctx_(ctx), flags_(flags) {} ClangLoader::~ClangLoader() {} @@ -159,7 +159,7 @@ int ClangLoader::parse(unique_ptr *mod, unique_ptr *mod, std::unique_ptr> *tables, const std::string &file, bool in_memory); private: llvm::LLVMContext *ctx_; + unsigned flags_; }; } // namespace ebpf -- 2.7.4