Commit ea5023a4 authored by Brenden Blanco's avatar Brenden Blanco

Add pointer dereference support to probe_read

Allows things like:
```c
int kprobe__foo(struct pt_regs *ctx, u64 *ptr) {
  bpf_trace_printk("%lx\n", *ptr);
```
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent 4037b4df
...@@ -158,6 +158,23 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) { ...@@ -158,6 +158,23 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) {
} }
return true; return true;
} }
bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) {
if (E->getOpcode() != UO_Deref)
return true;
if (memb_visited_.find(E) != memb_visited_.end())
return true;
if (!ProbeChecker(E, ptregs_).needs_probe())
return true;
memb_visited_.insert(E);
Expr *sub = E->getSubExpr();
string rhs = rewriter_.getRewrittenText(SourceRange(sub->getLocStart(), sub->getLocEnd()));
string text;
text = "({ typeof(" + E->getType().getAsString() + ") _val; memset(&_val, 0, sizeof(_val));";
text += " bpf_probe_read(&_val, sizeof(_val), (u64)";
text += rhs + "); _val; })";
rewriter_.ReplaceText(SourceRange(E->getLocStart(), E->getLocEnd()), text);
return true;
}
bool ProbeVisitor::VisitMemberExpr(MemberExpr *E) { bool ProbeVisitor::VisitMemberExpr(MemberExpr *E) {
if (memb_visited_.find(E) != memb_visited_.end()) return true; if (memb_visited_.find(E) != memb_visited_.end()) return true;
......
...@@ -85,6 +85,7 @@ class ProbeVisitor : public clang::RecursiveASTVisitor<ProbeVisitor> { ...@@ -85,6 +85,7 @@ class ProbeVisitor : public clang::RecursiveASTVisitor<ProbeVisitor> {
bool VisitVarDecl(clang::VarDecl *Decl); bool VisitVarDecl(clang::VarDecl *Decl);
bool VisitCallExpr(clang::CallExpr *Call); bool VisitCallExpr(clang::CallExpr *Call);
bool VisitBinaryOperator(clang::BinaryOperator *E); bool VisitBinaryOperator(clang::BinaryOperator *E);
bool VisitUnaryOperator(clang::UnaryOperator *E);
bool VisitMemberExpr(clang::MemberExpr *E); bool VisitMemberExpr(clang::MemberExpr *E);
void set_ptreg(clang::Decl *D) { ptregs_.insert(D); } void set_ptreg(clang::Decl *D) { ptregs_.insert(D); }
private: private:
......
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