Skip to content

Commit b13426c

Browse files
author
Brenden Blanco
committed
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 <bblanco@plumgrid.com>
1 parent 841a494 commit b13426c

7 files changed

Lines changed: 19 additions & 15 deletions

File tree

‎src/cc/bpf_module.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
295295

296296
// load an entire c file as a module
297297
int BPFModule::load_cfile(const string &file, bool in_memory) {
298-
clang_loader_ = make_unique<ClangLoader>(&*ctx_);
298+
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
299299
if (clang_loader_->parse(&mod_, &tables_, file, in_memory))
300300
return -1;
301301
return 0;
@@ -307,7 +307,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory) {
307307
// Load in a pre-built list of functions into the initial Module object, then
308308
// build an ExecutionEngine.
309309
int BPFModule::load_includes(const string &tmpfile) {
310-
clang_loader_ = make_unique<ClangLoader>(&*ctx_);
310+
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
311311
if (clang_loader_->parse(&mod_, &tables_, tmpfile, false))
312312
return -1;
313313
return 0;
@@ -652,7 +652,7 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) {
652652
if (int rc = load_includes(BCC_INSTALL_PREFIX "/share/bcc/include/bcc/helpers.h"))
653653
return rc;
654654

655-
b_loader_.reset(new BLoader);
655+
b_loader_.reset(new BLoader(flags_));
656656
if (int rc = b_loader_->parse(&*mod_, filename, proto_filename, &tables_))
657657
return rc;
658658
if (int rc = annotate())

‎src/cc/frontends/b/loader.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ using std::vector;
2727

2828
namespace ebpf {
2929

30-
BLoader::BLoader() {
30+
BLoader::BLoader(unsigned flags) : flags_(flags) {
31+
(void)flags_;
3132
}
3233

3334
BLoader::~BLoader() {

‎src/cc/frontends/b/loader.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ class CodegenLLVM;
3535

3636
class BLoader {
3737
public:
38-
BLoader();
38+
explicit BLoader(unsigned flags);
3939
~BLoader();
4040
int parse(llvm::Module *mod, const std::string &filename, const std::string &proto_filename,
4141
std::unique_ptr<std::vector<TableDesc>> *tables);
4242
private:
43+
unsigned flags_;
4344
std::unique_ptr<cc::Parser> parser_;
4445
std::unique_ptr<cc::Parser> proto_parser_;
4546
std::unique_ptr<cc::CodegenLLVM> codegen_;

‎src/cc/frontends/clang/b_frontend_action.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,14 @@ bool BTypeConsumer::HandleTopLevelDecl(DeclGroupRef D) {
442442
return true;
443443
}
444444

445-
BFrontendAction::BFrontendAction(llvm::raw_ostream &os)
446-
: rewriter_(new Rewriter), os_(os), tables_(new vector<TableDesc>) {
445+
BFrontendAction::BFrontendAction(llvm::raw_ostream &os, unsigned flags)
446+
: os_(os), flags_(flags), rewriter_(new Rewriter), tables_(new vector<TableDesc>) {
447447
}
448448

449449
void BFrontendAction::EndSourceFileAction() {
450450
// uncomment to see rewritten source
451-
//rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(llvm::errs());
451+
if (flags_ & 0x4)
452+
rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(llvm::errs());
452453
rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(os_);
453454
os_.flush();
454455
}

‎src/cc/frontends/clang/b_frontend_action.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class BTypeVisitor : public clang::RecursiveASTVisitor<BTypeVisitor> {
6767
bool VisitCallExpr(clang::CallExpr *Call);
6868
bool VisitVarDecl(clang::VarDecl *Decl);
6969
bool VisitMemberExpr(clang::MemberExpr *E);
70-
bool VisitDeclRefExpr(clang::DeclRefExpr *E);
7170
bool VisitBinaryOperator(clang::BinaryOperator *E);
7271
bool VisitImplicitCastExpr(clang::ImplicitCastExpr *E);
7372

@@ -96,7 +95,7 @@ class BFrontendAction : public clang::ASTFrontendAction {
9695
public:
9796
// Initialize with the output stream where the new source file contents
9897
// should be written.
99-
explicit BFrontendAction(llvm::raw_ostream &os);
98+
BFrontendAction(llvm::raw_ostream &os, unsigned flags);
10099

101100
// Called by clang when the AST has been completed, here the output stream
102101
// will be flushed.
@@ -108,8 +107,9 @@ class BFrontendAction : public clang::ASTFrontendAction {
108107
// take ownership of the table-to-fd mapping data structure
109108
std::unique_ptr<std::vector<TableDesc>> take_tables() { return move(tables_); }
110109
private:
111-
std::unique_ptr<clang::Rewriter> rewriter_;
112110
llvm::raw_ostream &os_;
111+
unsigned flags_;
112+
std::unique_ptr<clang::Rewriter> rewriter_;
113113
std::unique_ptr<std::vector<TableDesc>> tables_;
114114
};
115115

‎src/cc/frontends/clang/loader.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ using std::vector;
5858

5959
namespace ebpf {
6060

61-
ClangLoader::ClangLoader(llvm::LLVMContext *ctx)
62-
: ctx_(ctx)
61+
ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags)
62+
: ctx_(ctx), flags_(flags)
6363
{}
6464

6565
ClangLoader::~ClangLoader() {}
@@ -159,7 +159,7 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, unique_ptr<vector<TableDes
159159
// capture the rewritten c file
160160
string out_str;
161161
llvm::raw_string_ostream os(out_str);
162-
BFrontendAction bact(os);
162+
BFrontendAction bact(os, flags_);
163163
if (!compiler1.ExecuteAction(bact))
164164
return -1;
165165
// this contains the open FDs

‎src/cc/frontends/clang/loader.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ class CodegenLLVM;
3636

3737
class ClangLoader {
3838
public:
39-
explicit ClangLoader(llvm::LLVMContext *ctx);
39+
explicit ClangLoader(llvm::LLVMContext *ctx, unsigned flags);
4040
~ClangLoader();
4141
int parse(std::unique_ptr<llvm::Module> *mod, std::unique_ptr<std::vector<TableDesc>> *tables,
4242
const std::string &file, bool in_memory);
4343
private:
4444
llvm::LLVMContext *ctx_;
45+
unsigned flags_;
4546
};
4647

4748
} // namespace ebpf

0 commit comments

Comments
 (0)