diff options
author | Yonghong Song <yonghong.song@linux.dev> | 2024-03-17 12:28:47 -0700 |
---|---|---|
committer | yonghong-song <ys114321@gmail.com> | 2024-03-17 13:20:15 -0700 |
commit | 57ca51bdd9c86870a43b9f76e062b3aede226ca4 (patch) | |
tree | 3c3fb647e6f9d19f2a54a13ff055cee440f52bc7 | |
parent | c00d69ceb5dbbf1dedd2e30c5e16ba5ed23b50be (diff) | |
download | bcc-57ca51bdd9c86870a43b9f76e062b3aede226ca4.tar.gz |
Fix btf_type_tag issue with llvm 15
With llvm15, the tool 'execsnoop.py' failed the compilation with
the following error messages:
...
/virtual/main.c:103:157: error: expected ')'
data.ppid = ({ typeof(pid_t) _val; __builtin_memset(&_val, 0, sizeof(_val));
bpf_probe_read(&_val, sizeof(_val), (void *)&({ typeof(struct task_struct btf_type_tag(rcu)*) _val;
__builtin_memset(&_val, 0, sizeof(_val));
bpf_probe_read(&_val, sizeof(_val), (void *)&task->real_parent); _val; })->tgid); _val; });
...
The main reason is incorrect type string
btf_type_tag(rcu)
btf_type_tag is introduced in llvm15 is an attribute. The correct type representation
should be
__attribute__((btf_type_tag("rcu")))
The bug is fixed in llvm16 with [1]. Unfortunately the patch cannot be backported to llvm15 since
at that time llvm15 has been freezed.
This patch manually fixed this issue for bcc.
[1] https://github.com/llvm/llvm-project/commit/82b4446f25d4b4a0b22f5d4c067c6a920045338a
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
-rw-r--r-- | src/cc/frontends/clang/b_frontend_action.cc | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc index aa6d4023..6c53711b 100644 --- a/src/cc/frontends/clang/b_frontend_action.cc +++ b/src/cc/frontends/clang/b_frontend_action.cc @@ -520,6 +520,27 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) { } return true; } + +static std::string FixBTFTypeTag(std::string TypeStr) +{ +#if LLVM_VERSION_MAJOR == 15 + std::map<std::string, std::string> TypePair = + {{"btf_type_tag(user)", "__attribute__((btf_type_tag(\"user\")))"}, + {"btf_type_tag(rcu)", "__attribute__((btf_type_tag(\"rcu\")))"}, + {"btf_type_tag(percpu)", "__attribute__((btf_type_tag(\"percpu\")))"}}; + + for (auto T: TypePair) { + size_t index; + index = TypeStr.find(T.first, 0); + if (index != std::string::npos) { + TypeStr.replace(index, T.first.size(), T.second); + return TypeStr; + } + } +#endif + return TypeStr; +} + bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) { if (E->getOpcode() == UO_AddrOf) { addrof_stmt_ = E; @@ -598,7 +619,7 @@ bool ProbeVisitor::VisitMemberExpr(MemberExpr *E) { string rhs = rewriter_.getRewrittenText(expansionRange(SourceRange(rhs_start, GET_ENDLOC(E)))); string base_type = base->getType()->getPointeeType().getAsString(); string pre, post; - pre = "({ typeof(" + E->getType().getAsString() + ") _val; __builtin_memset(&_val, 0, sizeof(_val));"; + pre = "({ typeof(" + FixBTFTypeTag(E->getType().getAsString()) + ") _val; __builtin_memset(&_val, 0, sizeof(_val));"; if (cannot_fall_back_safely) pre += " bpf_probe_read_kernel(&_val, sizeof(_val), (void *)&"; else @@ -652,7 +673,7 @@ bool ProbeVisitor::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { if (rewriter_.getRewrittenText(lbracket_range).size() == 0) return true; - pre = "({ typeof(" + E->getType().getAsString() + ") _val; __builtin_memset(&_val, 0, sizeof(_val));"; + pre = "({ typeof(" + FixBTFTypeTag(E->getType().getAsString()) + ") _val; __builtin_memset(&_val, 0, sizeof(_val));"; if (cannot_fall_back_safely) pre += " bpf_probe_read_kernel(&_val, sizeof(_val), (void *)(("; else |