Commit 19a48d57 authored by yonghong-song's avatar yonghong-song

Merge pull request #257 from iovisor/bblanco_dev

Mask function calls from influencing probe_read
parents 4d97f7fa 74adb734
...@@ -94,14 +94,17 @@ bool BMapDeclVisitor::VisitBuiltinType(const BuiltinType *T) { ...@@ -94,14 +94,17 @@ bool BMapDeclVisitor::VisitBuiltinType(const BuiltinType *T) {
return true; return true;
} }
class ProbeChecker : public clang::RecursiveASTVisitor<ProbeChecker> { class ProbeChecker : public RecursiveASTVisitor<ProbeChecker> {
public: public:
explicit ProbeChecker(Expr *arg, const set<Decl *> &ptregs) explicit ProbeChecker(Expr *arg, const set<Decl *> &ptregs)
: needs_probe_(false), ptregs_(ptregs) { : needs_probe_(false), ptregs_(ptregs) {
if (arg) if (arg)
TraverseStmt(arg); TraverseStmt(arg);
} }
bool VisitDeclRefExpr(clang::DeclRefExpr *E) { bool VisitCallExpr(CallExpr *E) {
return false;
}
bool VisitDeclRefExpr(DeclRefExpr *E) {
if (ptregs_.find(E->getDecl()) != ptregs_.end()) if (ptregs_.find(E->getDecl()) != ptregs_.end())
needs_probe_ = true; needs_probe_ = true;
return true; return true;
...@@ -113,10 +116,10 @@ class ProbeChecker : public clang::RecursiveASTVisitor<ProbeChecker> { ...@@ -113,10 +116,10 @@ class ProbeChecker : public clang::RecursiveASTVisitor<ProbeChecker> {
}; };
// Visit a piece of the AST and mark it as needing probe reads // Visit a piece of the AST and mark it as needing probe reads
class ProbeSetter : public clang::RecursiveASTVisitor<ProbeSetter> { class ProbeSetter : public RecursiveASTVisitor<ProbeSetter> {
public: public:
explicit ProbeSetter(set<Decl *> *ptregs) : ptregs_(ptregs) {} explicit ProbeSetter(set<Decl *> *ptregs) : ptregs_(ptregs) {}
bool VisitDeclRefExpr(clang::DeclRefExpr *E) { bool VisitDeclRefExpr(DeclRefExpr *E) {
ptregs_->insert(E->getDecl()); ptregs_->insert(E->getDecl());
return true; return true;
} }
...@@ -161,7 +164,7 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) { ...@@ -161,7 +164,7 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) {
return true; return true;
} }
bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) { bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) {
if (E->getOpcode() != UO_Deref) if (E->getOpcode() == UO_AddrOf)
return true; return true;
if (memb_visited_.find(E) != memb_visited_.end()) if (memb_visited_.find(E) != memb_visited_.end())
return true; return true;
...@@ -559,10 +562,10 @@ bool BTypeConsumer::HandleTopLevelDecl(DeclGroupRef Group) { ...@@ -559,10 +562,10 @@ bool BTypeConsumer::HandleTopLevelDecl(DeclGroupRef Group) {
return true; return true;
} }
ProbeConsumer::ProbeConsumer(clang::ASTContext &C, Rewriter &rewriter) ProbeConsumer::ProbeConsumer(ASTContext &C, Rewriter &rewriter)
: visitor_(rewriter) {} : visitor_(rewriter) {}
bool ProbeConsumer::HandleTopLevelDecl(clang::DeclGroupRef Group) { bool ProbeConsumer::HandleTopLevelDecl(DeclGroupRef Group) {
for (auto D : Group) { for (auto D : Group) {
if (FunctionDecl *F = dyn_cast<FunctionDecl>(D)) { if (FunctionDecl *F = dyn_cast<FunctionDecl>(D)) {
if (F->isExternallyVisible() && F->hasBody()) { if (F->isExternallyVisible() && F->hasBody()) {
......
...@@ -214,6 +214,27 @@ int kprobe__sys_open(struct pt_regs *ctx, const char *filename, ...@@ -214,6 +214,27 @@ int kprobe__sys_open(struct pt_regs *ctx, const char *filename,
}; };
""") """)
def test_task_switch(self):
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
struct key_t {
u32 prev_pid;
u32 curr_pid;
};
BPF_TABLE("hash", struct key_t, u64, stats, 1024);
int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) {
struct key_t key = {};
u64 zero = 0, *val;
key.curr_pid = bpf_get_current_pid_tgid();
key.prev_pid = prev->pid;
val = stats.lookup_or_init(&key, &zero);
(*val)++;
return 0;
}
""")
if __name__ == "__main__": if __name__ == "__main__":
main() main()
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