Commit ef6bb809 authored by Brenden Blanco's avatar Brenden Blanco

Better get_stackid rewriter logic and unittest version testing

Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent ad0e8836
...@@ -315,6 +315,8 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { ...@@ -315,6 +315,8 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
string prefix, suffix; string prefix, suffix;
string map_update_policy = "BPF_ANY"; string map_update_policy = "BPF_ANY";
string txt; string txt;
auto rewrite_start = Call->getLocStart();
auto rewrite_end = Call->getLocEnd();
if (memb_name == "lookup_or_init") { if (memb_name == "lookup_or_init") {
map_update_policy = "BPF_NOEXIST"; map_update_policy = "BPF_NOEXIST";
string name = Ref->getDecl()->getName(); string name = Ref->getDecl()->getName();
...@@ -354,8 +356,11 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { ...@@ -354,8 +356,11 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
txt += ", bpf_get_smp_processor_id(), " + args_other + ")"; txt += ", bpf_get_smp_processor_id(), " + args_other + ")";
} else if (memb_name == "get_stackid") { } else if (memb_name == "get_stackid") {
if (table_it->type == BPF_MAP_TYPE_STACK_TRACE) { if (table_it->type == BPF_MAP_TYPE_STACK_TRACE) {
string arg0 = rewriter_.getRewrittenText(SourceRange(Call->getArg(0)->getLocStart(),
Call->getArg(0)->getLocEnd()));
txt = "bpf_get_stackid("; txt = "bpf_get_stackid(";
txt += "bpf_pseudo_fd(1, " + fd + "), " + args + ")"; txt += "bpf_pseudo_fd(1, " + fd + "), " + arg0;
rewrite_end = Call->getArg(0)->getLocEnd();
} else { } else {
unsigned diag_id = C.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, unsigned diag_id = C.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
"get_stackid only available on stacktrace maps"); "get_stackid only available on stacktrace maps");
...@@ -387,12 +392,12 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { ...@@ -387,12 +392,12 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
txt = prefix + args + suffix; txt = prefix + args + suffix;
} }
if (!rewriter_.isRewritable(Call->getLocStart())) { if (!rewriter_.isRewritable(rewrite_start) || !rewriter_.isRewritable(rewrite_end)) {
C.getDiagnostics().Report(Call->getLocStart(), diag::err_expected) C.getDiagnostics().Report(Call->getLocStart(), diag::err_expected)
<< "use of map function not in a macro"; << "use of map function not in a macro";
return false; return false;
} }
rewriter_.ReplaceText(SourceRange(Call->getLocStart(), Call->getLocEnd()), txt); rewriter_.ReplaceText(SourceRange(rewrite_start, rewrite_end), txt);
return true; return true;
} }
} }
......
...@@ -3,8 +3,23 @@ ...@@ -3,8 +3,23 @@
# Licensed under the Apache License, Version 2.0 (the "License") # Licensed under the Apache License, Version 2.0 (the "License")
import bcc import bcc
import distutils.version
import os
import unittest import unittest
def kernel_version_ge(major, minor):
# True if running kernel is >= X.Y
version = distutils.version.LooseVersion(os.uname()[2]).version
if version[0] > major:
return True
if version[0] < major:
return False
if minor and version[1] < minor:
return False
return True
@unittest.skipUnless(kernel_version_ge(4,6), "requires kernel >= 4.6")
class TestStackid(unittest.TestCase): class TestStackid(unittest.TestCase):
def test_simple(self): def test_simple(self):
b = bcc.BPF(text=""" b = bcc.BPF(text="""
...@@ -14,7 +29,7 @@ BPF_STACK_TRACE(stack_traces, 10240); ...@@ -14,7 +29,7 @@ BPF_STACK_TRACE(stack_traces, 10240);
BPF_HASH(stack_entries, int, int); BPF_HASH(stack_entries, int, int);
BPF_HASH(stub); BPF_HASH(stub);
int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) { int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) {
int id = stack_traces.get_stackid(ctx, (BPF_F_REUSE_STACKID)); int id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID);
if (id < 0) if (id < 0)
return 0; return 0;
int key = 1; int key = 1;
......
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