Rebase to bpfrace 0.16.0 and LLVM 15

Resolves: rhbz#2121920
Resolves: rhbz#2118995
Resolves: rhbz#1975148
Resolves: rhbz#2088577
Resolves: rhbz#2128208
Resolves: rhbz#2073675
Resolves: rhbz#2073770
Resolves: rhbz#2075076

The new release and few added patches fix bugs in the shipped tools.
Also some RHEL-specific patches can be removed.

bpftrace 0.16.0 requires the cereal serialization library which is not
packaged into RHEL9. To overcome this problem, we download it manually,
which is ok to do as the library is only necessary during build.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
This commit is contained in:
Viktor Malik 2022-12-16 16:47:59 +01:00
parent 297bf8d34c
commit 96b95cdfd3
No known key found for this signature in database
GPG Key ID: AF7A2E1F6EE74FB3
16 changed files with 735 additions and 1603 deletions

2
.gitignore vendored
View File

@ -10,3 +10,5 @@
/bpftrace-0.11.0.tar.gz
/bpftrace-0.12.1.tar.gz
/bpftrace-0.13.1.tar.gz
/bpftrace-0.16.0.tar.gz
/cereal-1.3.2.tar.gz

View File

@ -1,834 +0,0 @@
From 291cb44a513049e94b8de80f58fa7f08d3491aea Mon Sep 17 00:00:00 2001
From: Viktor Malik <viktor.malik@gmail.com>
Date: Mon, 10 Jan 2022 16:41:05 +0100
Subject: [PATCH 8/8] Fix LLVM 13 warnings
Since LLVM 13, CreateGEP and CreateLoad require explicit types.
---
src/ast/codegen_llvm.cpp | 265 ++++++++++++++++++++++++---------------
src/ast/irbuilderbpf.cpp | 56 +++++----
2 files changed, 200 insertions(+), 121 deletions(-)
diff --git a/src/ast/codegen_llvm.cpp b/src/ast/codegen_llvm.cpp
index d30327a2..0bd23276 100644
--- a/src/ast/codegen_llvm.cpp
+++ b/src/ast/codegen_llvm.cpp
@@ -224,9 +224,10 @@ void CodegenLLVM::visit(Builtin &builtin)
// LLVM optimization is possible to transform `(uint64*)ctx` into
// `(uint8*)ctx`, but sometimes this causes invalid context access.
// Mark every context acess to supporess any LLVM optimization.
- expr_ = b_.CreateLoad(b_.getInt64Ty(),
- b_.CreateGEP(ctx, b_.getInt64(offset)),
- builtin.ident);
+ expr_ = b_.CreateLoad(
+ b_.getInt64Ty(),
+ b_.CreateGEP(b_.getInt64Ty(), ctx, b_.getInt64(offset)),
+ builtin.ident);
// LLVM 7.0 <= does not have CreateLoad(*Ty, *Ptr, isVolatile, Name),
// so call setVolatile() manually
dyn_cast<LoadInst>(expr_)->setVolatile(true);
@@ -249,16 +250,17 @@ void CodegenLLVM::visit(Builtin &builtin)
int arg_num = atoi(builtin.ident.substr(4).c_str());
Value *ctx = b_.CreatePointerCast(ctx_, b_.getInt64Ty()->getPointerTo());
- Value *sp = b_.CreateLoad(b_.getInt64Ty(),
- b_.CreateGEP(ctx, b_.getInt64(sp_offset)),
- "reg_sp");
+ Value *sp = b_.CreateLoad(
+ b_.getInt64Ty(),
+ b_.CreateGEP(b_.getInt64Ty(), ctx, b_.getInt64(sp_offset)),
+ "reg_sp");
dyn_cast<LoadInst>(sp)->setVolatile(true);
AllocaInst *dst = b_.CreateAllocaBPF(builtin.type, builtin.ident);
Value *src = b_.CreateAdd(sp,
b_.getInt64((arg_num + arch::arg_stack_offset()) *
sizeof(uintptr_t)));
b_.CreateProbeRead(ctx_, dst, 8, src, builtin.type.GetAS(), builtin.loc);
- expr_ = b_.CreateLoad(dst);
+ expr_ = b_.CreateLoad(b_.GetType(builtin.type), dst);
b_.CreateLifetimeEnd(dst);
}
else if (builtin.ident == "probe")
@@ -526,8 +528,12 @@ void CodegenLLVM::visit(Call &call)
b_.CREATE_MEMSET(buf, b_.getInt8(0), bpftrace_.strlen_, 1);
auto arg0 = call.vargs->front();
auto scoped_del = accept(call.vargs->front());
- b_.CreateProbeReadStr(
- ctx_, buf, b_.CreateLoad(strlen), expr_, arg0->type.GetAS(), call.loc);
+ b_.CreateProbeReadStr(ctx_,
+ buf,
+ b_.CreateLoad(b_.getInt64Ty(), strlen),
+ expr_,
+ arg0->type.GetAS(),
+ call.loc);
b_.CreateLifetimeEnd(strlen);
expr_ = buf;
@@ -569,12 +575,14 @@ void CodegenLLVM::visit(Call &call)
false);
AllocaInst *buf = b_.CreateAllocaBPF(buf_struct, "buffer");
- Value *buf_len_offset = b_.CreateGEP(buf,
+ Value *buf_len_offset = b_.CreateGEP(buf_struct,
+ buf,
{ b_.getInt32(0), b_.getInt32(0) });
length = b_.CreateIntCast(length, buf_struct->getElementType(0), false);
b_.CreateStore(length, buf_len_offset);
- Value *buf_data_offset = b_.CreateGEP(buf,
+ Value *buf_data_offset = b_.CreateGEP(buf_struct,
+ buf,
{ b_.getInt32(0), b_.getInt32(1) });
b_.CREATE_MEMSET(buf_data_offset,
b_.GetIntSameSize(0, elements.at(0)),
@@ -664,14 +672,14 @@ void CodegenLLVM::visit(Call &call)
b_.SetInsertPoint(notzero);
b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::join)), perfdata);
b_.CreateStore(b_.getInt64(join_id_),
- b_.CreateGEP(perfdata, b_.getInt64(8)));
+ b_.CreateGEP(b_.getInt8Ty(), perfdata, b_.getInt64(8)));
join_id_++;
AllocaInst *arr = b_.CreateAllocaBPF(b_.getInt64Ty(), call.func + "_r0");
b_.CreateProbeRead(ctx_, arr, 8, expr_, addrspace, call.loc);
b_.CreateProbeReadStr(ctx_,
b_.CreateAdd(perfdata, b_.getInt64(8 + 8)),
bpftrace_.join_argsize_,
- b_.CreateLoad(arr),
+ b_.CreateLoad(b_.getInt64Ty(), arr),
addrspace,
call.loc);
@@ -679,14 +687,18 @@ void CodegenLLVM::visit(Call &call)
{
// argi
b_.CreateStore(b_.CreateAdd(expr_, b_.getInt64(8 * i)), first);
- b_.CreateProbeRead(
- ctx_, second, 8, b_.CreateLoad(first), addrspace, call.loc);
+ b_.CreateProbeRead(ctx_,
+ second,
+ 8,
+ b_.CreateLoad(b_.getInt64Ty(), first),
+ addrspace,
+ call.loc);
b_.CreateProbeReadStr(
ctx_,
b_.CreateAdd(perfdata,
b_.getInt64(8 + 8 + i * bpftrace_.join_argsize_)),
bpftrace_.join_argsize_,
- b_.CreateLoad(second),
+ b_.CreateLoad(b_.getInt64Ty(), second),
addrspace,
call.loc);
}
@@ -729,7 +741,9 @@ void CodegenLLVM::visit(Call &call)
AllocaInst *buf = b_.CreateAllocaBPF(inet_struct, "inet");
- Value *af_offset = b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) });
+ Value *af_offset = b_.CreateGEP(inet_struct,
+ buf,
+ { b_.getInt64(0), b_.getInt32(0) });
Value *af_type;
auto inet = call.vargs->at(0);
@@ -752,7 +766,9 @@ void CodegenLLVM::visit(Call &call)
}
b_.CreateStore(af_type, af_offset);
- Value *inet_offset = b_.CreateGEP(buf, {b_.getInt32(0), b_.getInt32(1)});
+ Value *inet_offset = b_.CreateGEP(inet_struct,
+ buf,
+ { b_.getInt32(0), b_.getInt32(1) });
b_.CREATE_MEMSET(inet_offset, b_.getInt8(0), 16, 1);
auto scoped_del = accept(inet);
@@ -785,9 +801,10 @@ void CodegenLLVM::visit(Call &call)
}
Value *ctx = b_.CreatePointerCast(ctx_, b_.getInt64Ty()->getPointerTo());
- expr_ = b_.CreateLoad(b_.getInt64Ty(),
- b_.CreateGEP(ctx, b_.getInt64(offset)),
- call.func + "_" + reg_name);
+ expr_ = b_.CreateLoad(
+ b_.getInt64Ty(),
+ b_.CreateGEP(b_.getInt64Ty(), ctx, b_.getInt64(offset)),
+ call.func + "_" + reg_name);
dyn_cast<LoadInst>(expr_)->setVolatile(true);
}
else if (call.func == "printf")
@@ -812,8 +829,10 @@ void CodegenLLVM::visit(Call &call)
auto scoped_del = accept(&arg);
// and store it to data area
- Value *offset = b_.CreateGEP(
- data, { b_.getInt64(0), b_.getInt64((i - 1) * ptr_size) });
+ Value *offset = b_.CreateGEP(b_.GetType(data_type),
+ data,
+ { b_.getInt64(0),
+ b_.getInt64((i - 1) * ptr_size) });
b_.CreateStore(expr_, offset);
// keep the expression alive, so it's still there
@@ -901,7 +920,9 @@ void CodegenLLVM::visit(Call &call)
AllocaInst *buf = b_.CreateAllocaBPF(event_struct,
call.func + "_" + map.ident);
- auto aa_ptr = b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) });
+ auto aa_ptr = b_.CreateGEP(event_struct,
+ buf,
+ { b_.getInt64(0), b_.getInt32(0) });
if (call.func == "clear")
b_.CreateStore(b_.GetIntSameSize(asyncactionint(AsyncAction::clear),
elements.at(0)),
@@ -912,7 +933,9 @@ void CodegenLLVM::visit(Call &call)
aa_ptr);
auto id = bpftrace_.maps[map.ident].value()->id;
- auto *ident_ptr = b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) });
+ auto *ident_ptr = b_.CreateGEP(event_struct,
+ buf,
+ { b_.getInt64(0), b_.getInt32(1) });
b_.CreateStore(b_.GetIntSameSize(id, elements.at(1)), ident_ptr);
b_.CreatePerfEventOutput(ctx_, buf, getStructSize(event_struct));
@@ -928,12 +951,13 @@ void CodegenLLVM::visit(Call &call)
AllocaInst *buf = b_.CreateAllocaBPF(time_struct, call.func + "_t");
- b_.CreateStore(b_.GetIntSameSize(asyncactionint(AsyncAction::time),
- elements.at(0)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.GetIntSameSize(asyncactionint(AsyncAction::time), elements.at(0)),
+ b_.CreateGEP(time_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
- b_.CreateStore(b_.GetIntSameSize(time_id_, elements.at(1)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) }));
+ b_.CreateStore(
+ b_.GetIntSameSize(time_id_, elements.at(1)),
+ b_.CreateGEP(time_struct, buf, { b_.getInt64(0), b_.getInt32(1) }));
time_id_++;
b_.CreatePerfEventOutput(ctx_, buf, getStructSize(time_struct));
@@ -948,13 +972,15 @@ void CodegenLLVM::visit(Call &call)
true);
AllocaInst *buf = b_.CreateAllocaBPF(strftime_struct, call.func + "_args");
- b_.CreateStore(b_.GetIntSameSize(strftime_id_, elements.at(0)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.GetIntSameSize(strftime_id_, elements.at(0)),
+ b_.CreateGEP(strftime_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
strftime_id_++;
Expression *arg = call.vargs->at(1);
auto scoped_del = accept(arg);
- b_.CreateStore(expr_,
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) }));
+ b_.CreateStore(
+ expr_,
+ b_.CreateGEP(strftime_struct, buf, { b_.getInt64(0), b_.getInt32(1) }));
expr_ = buf;
}
else if (call.func == "kstack" || call.func == "ustack")
@@ -1068,11 +1094,12 @@ void CodegenLLVM::visit(Call &call)
AllocaInst *buf = b_.CreateAllocaBPF(unwatch_struct, "unwatch");
size_t struct_size = datalayout().getTypeAllocSize(unwatch_struct);
- b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::watchpoint_detach)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.getInt64(asyncactionint(AsyncAction::watchpoint_detach)),
+ b_.CreateGEP(unwatch_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
b_.CreateStore(
b_.CreateIntCast(expr_, b_.getInt64Ty(), false /* unsigned */),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) }));
+ b_.CreateGEP(unwatch_struct, buf, { b_.getInt64(0), b_.getInt32(1) }));
b_.CreatePerfEventOutput(ctx_, buf, struct_size);
b_.CreateLifetimeEnd(buf);
expr_ = nullptr;
@@ -1102,7 +1129,8 @@ void CodegenLLVM::visit(Variable &var)
}
else
{
- expr_ = b_.CreateLoad(variables_[var.ident]);
+ auto *var_alloca = variables_[var.ident];
+ expr_ = b_.CreateLoad(var_alloca->getType()->getElementType(), var_alloca);
}
}
@@ -1379,13 +1407,15 @@ void CodegenLLVM::visit(Unop &unop)
if (unop.is_post_op)
expr_ = oldval;
else
- expr_ = b_.CreateLoad(newval);
+ expr_ = b_.CreateLoad(b_.GetType(map.type), newval);
b_.CreateLifetimeEnd(newval);
}
else if (unop.expr->is_variable)
{
Variable &var = static_cast<Variable&>(*unop.expr);
- Value *oldval = b_.CreateLoad(variables_[var.ident]);
+ Value *oldval = b_.CreateLoad(
+ variables_[var.ident]->getType()->getElementType(),
+ variables_[var.ident]);
Value *newval;
if (is_increment)
newval = b_.CreateAdd(oldval, b_.getInt64(1));
@@ -1411,9 +1441,10 @@ void CodegenLLVM::visit(Unop &unop)
: type.GetSize();
auto as = type.GetAS();
- AllocaInst *dst = b_.CreateAllocaBPF(SizedType(type.type, size), "deref");
+ auto dst_type = SizedType(type.type, size);
+ AllocaInst *dst = b_.CreateAllocaBPF(dst_type, "deref");
b_.CreateProbeRead(ctx_, dst, size, expr_, as, unop.loc);
- expr_ = b_.CreateIntCast(b_.CreateLoad(dst),
+ expr_ = b_.CreateIntCast(b_.CreateLoad(b_.GetType(dst_type), dst),
b_.getInt64Ty(),
type.IsSigned());
b_.CreateLifetimeEnd(dst);
@@ -1434,7 +1465,7 @@ void CodegenLLVM::visit(Unop &unop)
int size = unop.type.IsIntegerTy() ? et->GetIntBitWidth() / 8 : 8;
AllocaInst *dst = b_.CreateAllocaBPF(*et, "deref");
b_.CreateProbeRead(ctx_, dst, size, expr_, type.GetAS(), unop.loc);
- expr_ = b_.CreateIntCast(b_.CreateLoad(dst),
+ expr_ = b_.CreateIntCast(b_.CreateLoad(b_.GetType(*et), dst),
b_.getInt64Ty(),
unop.type.IsSigned());
b_.CreateLifetimeEnd(dst);
@@ -1492,7 +1523,7 @@ void CodegenLLVM::visit(Ternary &ternary)
b_.CreateBr(done);
b_.SetInsertPoint(done);
- expr_ = b_.CreateLoad(result);
+ expr_ = b_.CreateLoad(b_.GetType(ternary.type), result);
}
else if (ternary.type.IsStringTy())
{
@@ -1549,7 +1580,8 @@ void CodegenLLVM::visit(FieldAccess &acc)
}
else if (type.IsTupleTy())
{
- Value *src = b_.CreateGEP(expr_,
+ Value *src = b_.CreateGEP(b_.GetType(type),
+ expr_,
{ b_.getInt32(0), b_.getInt32(acc.index) });
SizedType &elem_type = type.GetFields()[acc.index].type;
@@ -1596,10 +1628,12 @@ void CodegenLLVM::visit(FieldAccess &acc)
if (field.is_bitfield)
{
Value *raw;
+ auto field_type = b_.GetType(field.type);
if (type.IsCtxAccess())
- raw = b_.CreateLoad(
- b_.CreateIntToPtr(src, b_.GetType(field.type)->getPointerTo()),
- true);
+ raw = b_.CreateLoad(field_type,
+ b_.CreateIntToPtr(src,
+ field_type->getPointerTo()),
+ true);
else
{
AllocaInst *dst = b_.CreateAllocaBPF(field.type,
@@ -1610,7 +1644,7 @@ void CodegenLLVM::visit(FieldAccess &acc)
b_.CREATE_MEMSET(dst, b_.getInt8(0), field.type.GetSize(), 1);
b_.CreateProbeRead(
ctx_, dst, field.bitfield.read_bytes, src, type.GetAS(), acc.loc);
- raw = b_.CreateLoad(dst);
+ raw = b_.CreateLoad(field_type, dst);
b_.CreateLifetimeEnd(dst);
}
size_t rshiftbits;
@@ -1638,7 +1672,8 @@ void CodegenLLVM::visit(FieldAccess &acc)
// offset which we add to the start of the tracepoint struct.
expr_ = b_.CreateLoad(
b_.getInt32Ty(),
- b_.CreateGEP(b_.CreatePointerCast(ctx_,
+ b_.CreateGEP(b_.getInt32Ty(),
+ b_.CreatePointerCast(ctx_,
b_.getInt32Ty()->getPointerTo()),
b_.getInt64(field.offset / 4)));
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false);
@@ -1757,7 +1792,9 @@ void CodegenLLVM::visit(Tuple &tuple)
Expression *elem = tuple.elems->at(i);
auto scoped_del = accept(elem);
- Value *dst = b_.CreateGEP(buf, { b_.getInt32(0), b_.getInt32(i) });
+ Value *dst = b_.CreateGEP(tuple_ty,
+ buf,
+ { b_.getInt32(0), b_.getInt32(i) });
if (onStack(elem->type))
b_.CREATE_MEMCPY(dst, expr_, elem->type.GetSize(), 1);
@@ -2357,6 +2394,7 @@ std::tuple<Value *, CodegenLLVM::ScopedExprDeleter> CodegenLLVM::getMapKey(
size += expr->type.GetSize();
}
key = b_.CreateAllocaBPF(size, map.ident + "_key");
+ auto *key_type = ArrayType::get(b_.getInt8Ty(), size);
int offset = 0;
// Construct a map key in the stack
@@ -2364,7 +2402,8 @@ std::tuple<Value *, CodegenLLVM::ScopedExprDeleter> CodegenLLVM::getMapKey(
{
auto scoped_del = accept(expr);
Value *offset_val = b_.CreateGEP(
- key, { b_.getInt64(0), b_.getInt64(offset) });
+ key_type, key,
+ { b_.getInt64(0), b_.getInt64(offset) });
if (onStack(expr->type))
b_.CREATE_MEMCPY(offset_val, expr_, expr->type.GetSize(), 1);
@@ -2417,18 +2456,19 @@ AllocaInst *CodegenLLVM::getHistMapKey(Map &map, Value *log2)
size += expr->type.GetSize();
}
key = b_.CreateAllocaBPF(size, map.ident + "_key");
+ auto *key_type = ArrayType::get(b_.getInt8Ty(), size);
int offset = 0;
for (Expression *expr : *map.vargs) {
auto scoped_del = accept(expr);
- Value *offset_val = b_.CreateGEP(key, {b_.getInt64(0), b_.getInt64(offset)});
+ Value *offset_val = b_.CreateGEP(key_type, key, {b_.getInt64(0), b_.getInt64(offset)});
if (shouldBeOnStackAlready(expr->type))
b_.CREATE_MEMCPY(offset_val, expr_, expr->type.GetSize(), 1);
else
b_.CreateStore(expr_, offset_val);
offset += expr->type.GetSize();
}
- Value *offset_val = b_.CreateGEP(key, {b_.getInt64(0), b_.getInt64(offset)});
+ Value *offset_val = b_.CreateGEP(key_type, key, {b_.getInt64(0), b_.getInt64(offset)});
b_.CreateStore(log2, offset_val);
}
else
@@ -2475,7 +2515,7 @@ Value *CodegenLLVM::createLogicalAnd(Binop &binop)
b_.CreateBr(merge_block);
b_.SetInsertPoint(merge_block);
- return b_.CreateLoad(result);
+ return b_.CreateLoad(b_.getInt64Ty(), result);
}
Value *CodegenLLVM::createLogicalOr(Binop &binop)
@@ -2514,7 +2554,7 @@ Value *CodegenLLVM::createLogicalOr(Binop &binop)
b_.CreateBr(merge_block);
b_.SetInsertPoint(merge_block);
- return b_.CreateLoad(result);
+ return b_.CreateLoad(b_.getInt64Ty(), result);
}
Function *CodegenLLVM::createLog2Function()
@@ -2558,34 +2598,37 @@ Function *CodegenLLVM::createLog2Function()
// test for less than zero
BasicBlock *is_less_than_zero = BasicBlock::Create(module_->getContext(), "hist.is_less_than_zero", log2_func);
BasicBlock *is_not_less_than_zero = BasicBlock::Create(module_->getContext(), "hist.is_not_less_than_zero", log2_func);
- b_.CreateCondBr(b_.CreateICmpSLT(b_.CreateLoad(n_alloc), b_.getInt64(0)),
+ b_.CreateCondBr(b_.CreateICmpSLT(b_.CreateLoad(b_.getInt64Ty(), n_alloc),
+ b_.getInt64(0)),
is_less_than_zero,
is_not_less_than_zero);
b_.SetInsertPoint(is_less_than_zero);
- createRet(b_.CreateLoad(result));
+ createRet(b_.CreateLoad(b_.getInt64Ty(), result));
b_.SetInsertPoint(is_not_less_than_zero);
// test for equal to zero
BasicBlock *is_zero = BasicBlock::Create(module_->getContext(), "hist.is_zero", log2_func);
BasicBlock *is_not_zero = BasicBlock::Create(module_->getContext(), "hist.is_not_zero", log2_func);
- b_.CreateCondBr(b_.CreateICmpEQ(b_.CreateLoad(n_alloc), b_.getInt64(0)),
+ b_.CreateCondBr(b_.CreateICmpEQ(b_.CreateLoad(b_.getInt64Ty(), n_alloc),
+ b_.getInt64(0)),
is_zero,
is_not_zero);
b_.SetInsertPoint(is_zero);
b_.CreateStore(b_.getInt64(1), result);
- createRet(b_.CreateLoad(result));
+ createRet(b_.CreateLoad(b_.getInt64Ty(), result));
b_.SetInsertPoint(is_not_zero);
// power-of-2 index, offset by +2
b_.CreateStore(b_.getInt64(2), result);
for (int i = 4; i >= 0; i--)
{
- Value *n = b_.CreateLoad(n_alloc);
+ Value *n = b_.CreateLoad(b_.getInt64Ty(), n_alloc);
Value *shift = b_.CreateShl(b_.CreateIntCast(b_.CreateICmpSGE(b_.CreateIntCast(n, b_.getInt64Ty(), false), b_.getInt64(1 << (1<<i))), b_.getInt64Ty(), false), i);
b_.CreateStore(b_.CreateLShr(n, shift), n_alloc);
- b_.CreateStore(b_.CreateAdd(b_.CreateLoad(result), shift), result);
+ b_.CreateStore(b_.CreateAdd(b_.CreateLoad(b_.getInt64Ty(), result), shift),
+ result);
}
- createRet(b_.CreateLoad(result));
+ createRet(b_.CreateLoad(b_.getInt64Ty(), result));
b_.restoreIP(ip);
return module_->getFunction("log2");
}
@@ -2635,8 +2678,8 @@ Function *CodegenLLVM::createLinearFunction()
// algorithm
{
- Value *min = b_.CreateLoad(min_alloc);
- Value *val = b_.CreateLoad(value_alloc);
+ Value *min = b_.CreateLoad(b_.getInt64Ty(), min_alloc);
+ Value *val = b_.CreateLoad(b_.getInt64Ty(), value_alloc);
cmp = b_.CreateICmpSLT(val, min);
}
BasicBlock *lt_min = BasicBlock::Create(module_->getContext(), "lhist.lt_min", linear_func);
@@ -2648,8 +2691,8 @@ Function *CodegenLLVM::createLinearFunction()
b_.SetInsertPoint(ge_min);
{
- Value *max = b_.CreateLoad(max_alloc);
- Value *val = b_.CreateLoad(value_alloc);
+ Value *max = b_.CreateLoad(b_.getInt64Ty(), max_alloc);
+ Value *val = b_.CreateLoad(b_.getInt64Ty(), value_alloc);
cmp = b_.CreateICmpSGT(val, max);
}
BasicBlock *le_max = BasicBlock::Create(module_->getContext(), "lhist.le_max", linear_func);
@@ -2658,22 +2701,22 @@ Function *CodegenLLVM::createLinearFunction()
b_.SetInsertPoint(gt_max);
{
- Value *step = b_.CreateLoad(step_alloc);
- Value *min = b_.CreateLoad(min_alloc);
- Value *max = b_.CreateLoad(max_alloc);
+ Value *step = b_.CreateLoad(b_.getInt64Ty(), step_alloc);
+ Value *min = b_.CreateLoad(b_.getInt64Ty(), min_alloc);
+ Value *max = b_.CreateLoad(b_.getInt64Ty(), max_alloc);
Value *div = b_.CreateUDiv(b_.CreateSub(max, min), step);
b_.CreateStore(b_.CreateAdd(div, b_.getInt64(1)), result_alloc);
- createRet(b_.CreateLoad(result_alloc));
+ createRet(b_.CreateLoad(b_.getInt64Ty(), result_alloc));
}
b_.SetInsertPoint(le_max);
{
- Value *step = b_.CreateLoad(step_alloc);
- Value *min = b_.CreateLoad(min_alloc);
- Value *val = b_.CreateLoad(value_alloc);
+ Value *step = b_.CreateLoad(b_.getInt64Ty(), step_alloc);
+ Value *min = b_.CreateLoad(b_.getInt64Ty(), min_alloc);
+ Value *val = b_.CreateLoad(b_.getInt64Ty(), value_alloc);
Value *div3 = b_.CreateUDiv(b_.CreateSub(val, min), step);
b_.CreateStore(b_.CreateAdd(div3, b_.getInt64(1)), result_alloc);
- createRet(b_.CreateLoad(result_alloc));
+ createRet(b_.CreateLoad(b_.getInt64Ty(), result_alloc));
}
b_.restoreIP(ip);
@@ -2711,14 +2754,18 @@ void CodegenLLVM::createFormatStringCall(Call &call, int &id, CallArgs &call_arg
// as the struct is not packed we need to memset it.
b_.CREATE_MEMSET(fmt_args, b_.getInt8(0), struct_size, 1);
- Value *id_offset = b_.CreateGEP(fmt_args, {b_.getInt32(0), b_.getInt32(0)});
+ Value *id_offset = b_.CreateGEP(fmt_struct,
+ fmt_args,
+ { b_.getInt32(0), b_.getInt32(0) });
b_.CreateStore(b_.getInt64(id + asyncactionint(async_action)), id_offset);
for (size_t i=1; i<call.vargs->size(); i++)
{
Expression &arg = *call.vargs->at(i);
auto scoped_del = accept(&arg);
- Value *offset = b_.CreateGEP(fmt_args, {b_.getInt32(0), b_.getInt32(i)});
+ Value *offset = b_.CreateGEP(fmt_struct,
+ fmt_args,
+ { b_.getInt32(0), b_.getInt32(i) });
if (needMemcpy(arg.type))
b_.CREATE_MEMCPY(offset, expr_, arg.type.GetSize(), 1);
else if (arg.type.IsIntegerTy() && arg.type.GetSize() < 8)
@@ -2758,10 +2805,12 @@ void CodegenLLVM::generateWatchpointSetupProbe(
// Pull out function argument
Value *ctx = func->arg_begin();
int offset = arch::arg_offset(arg_num);
- Value *addr = b_.CreateLoad(
- b_.getInt64Ty(),
- b_.CreateGEP(ctx, b_.getInt64(offset * sizeof(uintptr_t))),
- "arg" + std::to_string(arg_num));
+ Value *arg = b_.CreateGEP(b_.getInt8Ty(),
+ ctx,
+ b_.getInt64(offset * sizeof(uintptr_t)));
+ Value *addr = b_.CreateLoad(b_.getInt64Ty(),
+ arg,
+ "arg" + std::to_string(arg_num));
// Tell userspace to setup the real watchpoint
auto elements = AsyncEvent::Watchpoint().asLLVMType(b_);
@@ -2772,12 +2821,16 @@ void CodegenLLVM::generateWatchpointSetupProbe(
size_t struct_size = datalayout().getTypeAllocSize(watchpoint_struct);
// Fill in perf event struct
- b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::watchpoint_attach)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
- b_.CreateStore(b_.getInt64(watchpoint_id_),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) }));
+ b_.CreateStore(
+ b_.getInt64(asyncactionint(AsyncAction::watchpoint_attach)),
+ b_.CreateGEP(watchpoint_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.getInt64(watchpoint_id_),
+ b_.CreateGEP(watchpoint_struct, buf, { b_.getInt64(0), b_.getInt32(1) }));
watchpoint_id_++;
- b_.CreateStore(addr, b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(2) }));
+ b_.CreateStore(
+ addr,
+ b_.CreateGEP(watchpoint_struct, buf, { b_.getInt64(0), b_.getInt32(2) }));
b_.CreatePerfEventOutput(ctx, buf, struct_size);
b_.CreateLifetimeEnd(buf);
@@ -2796,11 +2849,14 @@ void CodegenLLVM::createPrintMapCall(Call &call)
call.func + "_" + map.ident);
// store asyncactionid:
- b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::print)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.getInt64(asyncactionint(AsyncAction::print)),
+ b_.CreateGEP(print_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
auto id = bpftrace_.maps[map.ident].value()->id;
- auto *ident_ptr = b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) });
+ auto *ident_ptr = b_.CreateGEP(print_struct,
+ buf,
+ { b_.getInt64(0), b_.getInt32(1) });
b_.CreateStore(b_.GetIntSameSize(id, elements.at(1)), ident_ptr);
// top, div
@@ -2812,14 +2868,16 @@ void CodegenLLVM::createPrintMapCall(Call &call)
auto scoped_del = accept(call.vargs->at(arg_idx));
b_.CreateStore(b_.CreateIntCast(expr_, elements.at(arg_idx), false),
- b_.CreateGEP(buf,
+ b_.CreateGEP(print_struct,
+ buf,
{ b_.getInt64(0), b_.getInt32(arg_idx + 1) }));
}
for (; arg_idx < 3; arg_idx++)
{
b_.CreateStore(b_.GetIntSameSize(0, elements.at(arg_idx)),
- b_.CreateGEP(buf,
+ b_.CreateGEP(print_struct,
+ buf,
{ b_.getInt64(0), b_.getInt32(arg_idx + 1) }));
}
@@ -2844,15 +2902,19 @@ void CodegenLLVM::createPrintNonMapCall(Call &call, int &id)
size_t struct_size = datalayout().getTypeAllocSize(print_struct);
// Store asyncactionid:
- b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::print_non_map)),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(0) }));
+ b_.CreateStore(
+ b_.getInt64(asyncactionint(AsyncAction::print_non_map)),
+ b_.CreateGEP(print_struct, buf, { b_.getInt64(0), b_.getInt32(0) }));
// Store print id
- b_.CreateStore(b_.getInt64(id),
- b_.CreateGEP(buf, { b_.getInt64(0), b_.getInt32(1) }));
+ b_.CreateStore(
+ b_.getInt64(id),
+ b_.CreateGEP(print_struct, buf, { b_.getInt64(0), b_.getInt32(1) }));
// Store content
- Value *content_offset = b_.CreateGEP(buf, { b_.getInt32(0), b_.getInt32(2) });
+ Value *content_offset = b_.CreateGEP(print_struct,
+ buf,
+ { b_.getInt32(0), b_.getInt32(2) });
b_.CREATE_MEMSET(content_offset, b_.getInt8(0), arg.type.GetSize(), 1);
if (needMemcpy(arg.type))
{
@@ -3023,7 +3085,9 @@ void CodegenLLVM::readDatastructElemFromStack(Value *src_data,
src_data = b_.CreateIntToPtr(src_data,
b_.GetType(data_type)->getPointerTo());
- Value *src = b_.CreateGEP(src_data, { b_.getInt32(0), index });
+ Value *src = b_.CreateGEP(b_.GetType(data_type),
+ src_data,
+ { b_.getInt32(0), index });
// It may happen that the result pointer type is not correct, in such case
// do a typecast
@@ -3034,7 +3098,7 @@ void CodegenLLVM::readDatastructElemFromStack(Value *src_data,
if (elem_type.IsIntegerTy() || elem_type.IsPtrTy())
{
// Load the correct type from src
- expr_ = b_.CreateLoad(src, true);
+ expr_ = b_.CreateLoad(b_.GetType(elem_type), src, true);
}
else
{
@@ -3101,7 +3165,8 @@ void CodegenLLVM::probereadDatastructElem(Value *src_data,
// Read data onto stack
if (data_type.IsCtxAccess())
{
- expr_ = b_.CreateLoad(b_.CreateIntToPtr(src, dst_type->getPointerTo()),
+ expr_ = b_.CreateLoad(dst_type,
+ b_.CreateIntToPtr(src, dst_type->getPointerTo()),
true);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), elem_type.IsSigned());
@@ -3132,7 +3197,7 @@ void CodegenLLVM::probereadDatastructElem(Value *src_data,
AllocaInst *dst = b_.CreateAllocaBPF(elem_type, temp_name);
b_.CreateProbeRead(
ctx_, dst, elem_type.GetSize(), src, data_type.GetAS(), loc);
- expr_ = b_.CreateIntCast(b_.CreateLoad(dst),
+ expr_ = b_.CreateIntCast(b_.CreateLoad(b_.GetType(elem_type), dst),
b_.getInt64Ty(),
elem_type.IsSigned());
b_.CreateLifetimeEnd(dst);
diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
index ab1464d3..59771e91 100644
--- a/src/ast/irbuilderbpf.cpp
+++ b/src/ast/irbuilderbpf.cpp
@@ -94,8 +94,8 @@ AllocaInst *IRBuilderBPF::CreateUSym(llvm::Value *val)
Value *pid = CreateLShr(CreateGetPidTgid(), 32);
// The extra 0 here ensures the type of addr_offset will be int64
- Value *addr_offset = CreateGEP(buf, { getInt64(0), getInt32(0) });
- Value *pid_offset = CreateGEP(buf, { getInt64(0), getInt32(1) });
+ Value *addr_offset = CreateGEP(usym_t, buf, { getInt64(0), getInt32(0) });
+ Value *pid_offset = CreateGEP(usym_t, buf, { getInt64(0), getInt32(1) });
CreateStore(val, addr_offset);
CreateStore(pid, pid_offset);
@@ -401,7 +401,8 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
if (needMemcpy(type))
return value;
- Value *ret = CreateLoad(value);
+ // value is a pointer to i64
+ Value *ret = CreateLoad(getInt64Ty(), value);
CreateLifetimeEnd(value);
return ret;
}
@@ -621,7 +622,10 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
// bpftrace's args are internally represented as 64 bit integers. However,
// the underlying argument (of the target program) may be less than 64
// bits. So we must be careful to zero out unused bits.
- Value* reg = CreateGEP(ctx, getInt64(offset * sizeof(uintptr_t)), "load_register");
+ Value *reg = CreateGEP(getInt8Ty(),
+ ctx,
+ getInt64(offset * sizeof(uintptr_t)),
+ "load_register");
AllocaInst *dst = CreateAllocaBPF(builtin.type, builtin.ident);
Value *index_offset = nullptr;
if (argument->valid & BCC_USDT_ARGUMENT_INDEX_REGISTER_NAME)
@@ -632,7 +636,8 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
LOG(FATAL) << "offset for register " << argument->index_register_name
<< " not known";
}
- index_offset = CreateGEP(ctx,
+ index_offset = CreateGEP(getInt8Ty(),
+ ctx,
getInt64(ioffset * sizeof(uintptr_t)),
"load_register");
index_offset = CreateLoad(getInt64Ty(), index_offset);
@@ -754,7 +759,7 @@ Value *IRBuilderBPF::CreateStrncmp(Value *ctx __attribute__((unused)),
"strcmp.loop",
parent);
- auto *ptr = CreateGEP(val, { getInt32(0), getInt32(i) });
+ auto *ptr = CreateGEP(valp->getElementType(), val, { getInt32(0), getInt32(i) });
Value *l = CreateLoad(getInt8Ty(), ptr);
Value *r = getInt8(c_str[i]);
Value *cmp = CreateICmpNE(l, r, "strcmp.cmp");
@@ -767,7 +772,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *ctx __attribute__((unused)),
CreateBr(str_ne);
SetInsertPoint(str_ne);
- Value *result = CreateLoad(store);
+ // store is a pointer to bool (i1 *)
+ Value *result = CreateLoad(getInt1Ty(), store);
CreateLifetimeEnd(store);
result = CreateIntCast(result, getInt64Ty(), false);
return result;
@@ -817,9 +823,11 @@ Value *IRBuilderBPF::CreateStrncmp(Value *ctx,
}
*/
+ auto *val1p = dyn_cast<PointerType>(val1->getType());
+ auto *val2p = dyn_cast<PointerType>(val2->getType());
#ifndef NDEBUG
- PointerType *val1p = cast<PointerType>(val1->getType());
- PointerType *val2p = cast<PointerType>(val2->getType());
+ assert(val1p);
+ assert(val2p);
assert(val1p->getElementType()->isArrayTy() &&
val1p->getElementType()->getArrayElementType() == getInt8Ty());
@@ -852,11 +860,11 @@ Value *IRBuilderBPF::CreateStrncmp(Value *ctx,
"strcmp.loop_null_cmp",
parent);
- auto *ptr1 = CreateGEP(val1, { getInt32(0), getInt32(i) });
+ auto *ptr1 = CreateGEP(val1p->getElementType(), val1, { getInt32(0), getInt32(i) });
CreateProbeRead(ctx, val_l, 1, ptr1, as1, loc);
Value *l = CreateLoad(getInt8Ty(), val_l);
- auto *ptr2 = CreateGEP(val2, { getInt32(0), getInt32(i) });
+ auto *ptr2 = CreateGEP(val2p->getElementType(), val2, { getInt32(0), getInt32(i) });
CreateProbeRead(ctx, val_r, 1, ptr2, as2, loc);
Value *r = CreateLoad(getInt8Ty(), val_r);
@@ -878,7 +886,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *ctx,
CreateBr(str_ne);
SetInsertPoint(str_ne);
- Value *result = CreateLoad(store);
+ // store is a pointer to bool (i1 *)
+ Value *result = CreateLoad(getInt1Ty(), store);
CreateLifetimeEnd(store);
CreateLifetimeEnd(val_l);
CreateLifetimeEnd(val_r);
@@ -1107,7 +1116,7 @@ Value *IRBuilderBPF::CreatKFuncArg(Value *ctx,
{
ctx = CreatePointerCast(ctx, getInt64Ty()->getPointerTo());
Value *expr = CreateLoad(getInt64Ty(),
- CreateGEP(ctx, getInt64(type.kfarg_idx)),
+ CreateGEP(getInt64Ty(), ctx, getInt64(type.kfarg_idx)),
name);
// LLVM 7.0 <= does not have CreateLoad(*Ty, *Ptr, isVolatile, Name),
@@ -1161,12 +1170,15 @@ void IRBuilderBPF::CreateHelperError(Value *ctx,
elements,
true);
AllocaInst *buf = CreateAllocaBPF(helper_error_struct, "helper_error_t");
- CreateStore(GetIntSameSize(asyncactionint(AsyncAction::helper_error),
- elements.at(0)),
- CreateGEP(buf, { getInt64(0), getInt32(0) }));
- CreateStore(GetIntSameSize(error_id, elements.at(1)),
- CreateGEP(buf, { getInt64(0), getInt32(1) }));
- CreateStore(return_value, CreateGEP(buf, { getInt64(0), getInt32(2) }));
+ CreateStore(
+ GetIntSameSize(asyncactionint(AsyncAction::helper_error), elements.at(0)),
+ CreateGEP(helper_error_struct, buf, { getInt64(0), getInt32(0) }));
+ CreateStore(
+ GetIntSameSize(error_id, elements.at(1)),
+ CreateGEP(helper_error_struct, buf, { getInt64(0), getInt32(1) }));
+ CreateStore(
+ return_value,
+ CreateGEP(helper_error_struct, buf, { getInt64(0), getInt32(2) }));
auto &layout = module_.getDataLayout();
auto struct_size = layout.getTypeAllocSize(helper_error_struct);
@@ -1256,11 +1268,13 @@ void IRBuilderBPF::CreateSeqPrintf(Value *ctx,
ctx = CreatePointerCast(ctx, getInt8Ty()->getPointerTo());
Value *meta = CreateLoad(getInt64Ty()->getPointerTo(),
- CreateGEP(ctx, getInt64(0)),
+ CreateGEP(getInt8Ty(), ctx, getInt64(0)),
"meta");
dyn_cast<LoadInst>(meta)->setVolatile(true);
- Value *seq = CreateLoad(getInt64Ty(), CreateGEP(meta, getInt64(0)), "seq");
+ Value *seq = CreateLoad(getInt64Ty(),
+ CreateGEP(getInt64Ty(), meta, getInt64(0)),
+ "seq");
CallInst *call = createCall(seq_printf_func,
{ seq, fmt, fmt_size, data, data_len },
--
2.35.3

View File

@ -1,42 +0,0 @@
From aee721b9b5ed72c287b302df20ea0fffc3cd72bd Mon Sep 17 00:00:00 2001
From: Yucong Sun <sunyucong@gmail.com>
Date: Thu, 21 Oct 2021 14:43:38 -0700
Subject: [PATCH 7/7] Fix compile under llvm14 (trunk)
---
CMakeLists.txt | 2 +-
src/bpforc.h | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3beeb027..3cfae6cb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -159,7 +159,7 @@ else()
find_package(LLVM REQUIRED)
endif()
- if((${LLVM_VERSION_MAJOR} VERSION_LESS 6) OR (${LLVM_VERSION_MAJOR} VERSION_GREATER 12))
+ if((${LLVM_VERSION_MAJOR} VERSION_LESS 6) OR (${LLVM_VERSION_MAJOR} VERSION_GREATER 14))
message(SEND_ERROR "Unsupported LLVM version found: ${LLVM_INCLUDE_DIRS}")
message(SEND_ERROR "Specify an LLVM major version using LLVM_REQUESTED_VERSION=<major version>")
endif()
diff --git a/src/bpforc.h b/src/bpforc.h
index cc6a1c97..a0e5c9b8 100644
--- a/src/bpforc.h
+++ b/src/bpforc.h
@@ -14,7 +14,11 @@
#include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Support/Error.h>
+#if LLVM_VERSION_MAJOR < 14
#include <llvm/Support/TargetRegistry.h>
+#else
+#include <llvm/MC/TargetRegistry.h>
+#endif
#include <llvm/Target/TargetMachine.h>
#ifdef LLVM_ORC_V2
--
2.35.3

View File

@ -1,39 +0,0 @@
From d26accaba6f911cf82be7e9791bf6440213c5627 Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Wed, 23 Mar 2022 09:47:25 +0100
Subject: [PATCH 4/6] Fix libbtf 0.6.0 build
Libbtf 0.6.0 introduced a new version of btf_dump__new(). The new
version is btf_dump__new_v0_6_0() while the old version was renamed
btf_dump__new_deprecated(). btf_dump__new() is now overloaded,
unfortunately the macro doesn't work on cpp, at least with LLVM 12.
Let's call btf_dump__new_deprecated() explicitely when it's defined.
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
src/btf.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/src/btf.cpp b/src/btf.cpp
index a5a5e2c0..fccf0a00 100644
--- a/src/btf.cpp
+++ b/src/btf.cpp
@@ -24,6 +24,15 @@
#pragma GCC diagnostic pop
#include <bpf/libbpf.h>
+/*
+ * Since libbtf 0.6, btf_dump__new() has been overloaded and now can design
+ * either btf_dump__new_v0_6_0() or btf_dump__new_deprecated(), which is the
+ * same as btf_dump__new() for libbtf < 0.6, and the one we still use.
+ */
+#if LIBBPF_MAJOR_VERSION == 0 && LIBBPF_MINOR_VERSION >= 6
+#define btf_dump__new(a1, a2, a3, a4) btf_dump__new_deprecated(a1, a2, a3, a4)
+#endif
+
#include "bpftrace.h"
namespace bpftrace {
--
2.35.3

View File

@ -1,28 +0,0 @@
From 0585b27f6070c0d4016ae9b0d6e1a28261f2e2d2 Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Fri, 15 Oct 2021 14:26:28 +0200
Subject: [PATCH 2/6] Fix mdflush
Since kernel commit 309dca309fc ("block: store a block_device pointer
in struct bio") struct bio points again to a block_device and not to a
gendisk directly.
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
tools/mdflush.bt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/mdflush.bt b/tools/mdflush.bt
index e767b81d..541abba1 100755
--- a/tools/mdflush.bt
+++ b/tools/mdflush.bt
@@ -26,5 +26,5 @@ kprobe:md_flush_request
{
time("%H:%M:%S ");
printf("%-6d %-16s %s\n", pid, comm,
- ((struct bio *)arg1)->bi_disk->disk_name);
+ ((struct bio *)arg1)->bi_bdev->bd_disk->disk_name);
}
--
2.35.3

View File

@ -1,58 +0,0 @@
From 4d7c2c148f7fb788d74e6cd3b6d65c07a815b0c1 Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Tue, 11 Jun 2019 16:41:59 +0200
Subject: [PATCH 1/6] RHEL 9 fixes
Fixes the following RHEL 8 specific issues:
- library path in gethostlatency and threadsnoop
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
tools/gethostlatency.bt | 12 ++++++------
tools/threadsnoop.bt | 2 +-
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/tools/gethostlatency.bt b/tools/gethostlatency.bt
index 9f4ec31e..dd389c6f 100755
--- a/tools/gethostlatency.bt
+++ b/tools/gethostlatency.bt
@@ -26,17 +26,17 @@ BEGIN
"HOST");
}
-uprobe:/lib/x86_64-linux-gnu/libc.so.6:getaddrinfo,
-uprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname,
-uprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname2
+uprobe:/lib64/libc.so.6:getaddrinfo,
+uprobe:/lib64/libc.so.6:gethostbyname,
+uprobe:/lib64/libc.so.6:gethostbyname2
{
@start[tid] = nsecs;
@name[tid] = arg0;
}
-uretprobe:/lib/x86_64-linux-gnu/libc.so.6:getaddrinfo,
-uretprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname,
-uretprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname2
+uretprobe:/lib64/libc.so.6:getaddrinfo,
+uretprobe:/lib64/libc.so.6:gethostbyname,
+uretprobe:/lib64/libc.so.6:gethostbyname2
/@start[tid]/
{
$latms = (nsecs - @start[tid]) / 1e6;
diff --git a/tools/threadsnoop.bt b/tools/threadsnoop.bt
index 3824bc6d..ab52bc48 100755
--- a/tools/threadsnoop.bt
+++ b/tools/threadsnoop.bt
@@ -18,7 +18,7 @@ BEGIN
printf("%-10s %-6s %-16s %s\n", "TIME(ms)", "PID", "COMM", "FUNC");
}
-uprobe:/lib/x86_64-linux-gnu/libpthread.so.0:pthread_create
+uprobe:/usr/lib64/libpthread.so.0:pthread_create
{
printf("%-10u %-6d %-16s %s\n", elapsed / 1e6, pid, comm,
usym(arg2));
--
2.35.3

View File

@ -1,78 +0,0 @@
From ff22437ef4310a2ab37d732bdd1496a926315691 Mon Sep 17 00:00:00 2001
From: Viktor Malik <viktor.malik@gmail.com>
Date: Mon, 17 Jan 2022 11:15:26 +0100
Subject: [PATCH 5/6] Update bio* tools to work on kernel 5.16+
Kernel 5.16 contains commit:
https://github.com/torvalds/linux/commit/be6bfe36db1795babe9d92178a47b2e02193cb0f
which renamed some of the functions that the bio* tools attach to.
---
tools/biolatency.bt | 6 ++++--
tools/biosnoop.bt | 6 ++++--
tools/biostacks.bt | 3 ++-
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/tools/biolatency.bt b/tools/biolatency.bt
index 4ea910b4..d5af1f29 100755
--- a/tools/biolatency.bt
+++ b/tools/biolatency.bt
@@ -16,12 +16,14 @@ BEGIN
printf("Tracing block device I/O... Hit Ctrl-C to end.\n");
}
-kprobe:blk_account_io_start
+kprobe:blk_account_io_start,
+kprobe:__blk_account_io_start
{
@start[arg0] = nsecs;
}
-kprobe:blk_account_io_done
+kprobe:blk_account_io_done,
+kprobe:__blk_account_io_done
/@start[arg0]/
{
@usecs = hist((nsecs - @start[arg0]) / 1000);
diff --git a/tools/biosnoop.bt b/tools/biosnoop.bt
index 38ffeb52..aa88f4ba 100755
--- a/tools/biosnoop.bt
+++ b/tools/biosnoop.bt
@@ -16,7 +16,8 @@ BEGIN
printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
}
-kprobe:blk_account_io_start
+kprobe:blk_account_io_start,
+kprobe:__blk_account_io_start
{
@start[arg0] = nsecs;
@iopid[arg0] = pid;
@@ -24,7 +25,8 @@ kprobe:blk_account_io_start
@disk[arg0] = ((struct request *)arg0)->rq_disk->disk_name;
}
-kprobe:blk_account_io_done
+kprobe:blk_account_io_done,
+kprobe:__blk_account_io_done
/@start[arg0] != 0 && @iopid[arg0] != 0 && @iocomm[arg0] != ""/
{
diff --git a/tools/biostacks.bt b/tools/biostacks.bt
index 58201cdf..1bc9f819 100755
--- a/tools/biostacks.bt
+++ b/tools/biostacks.bt
@@ -18,7 +18,8 @@ BEGIN
printf("Tracing block I/O with init stacks. Hit Ctrl-C to end.\n");
}
-kprobe:blk_account_io_start
+kprobe:blk_account_io_start,
+kprobe:__blk_account_io_start
{
@reqstack[arg0] = kstack;
@reqts[arg0] = nsecs;
--
2.35.3

View File

@ -1,98 +0,0 @@
From bcb8903067ce7a45b67ad0d5cabf83154b56d5ab Mon Sep 17 00:00:00 2001
From: Viktor Malik <viktor.malik@gmail.com>
Date: Mon, 9 May 2022 07:58:46 +0200
Subject: [PATCH 10/10] biosnoop.bt: handle Linux 5.17 block layer update
The kernel upstream commit:
https://github.com/torvalds/linux/commit/f3fa33acca9f0058157214800f68b10d8e71ab7a
has removed the `rq_disk` field from `struct request`. Instead,
`->q->disk` should be used, so this is reflected in biosnoop.bt.
The old version of the tool (suitable for kernel <= 5.16) is backed up
in tools/old and used in the CI.
---
tools/biosnoop.bt | 2 +-
tools/old/biosnoop.bt | 56 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 1 deletion(-)
create mode 100755 tools/old/biosnoop.bt
diff --git a/tools/biosnoop.bt b/tools/biosnoop.bt
index 33ad75de..c00ef428 100755
--- a/tools/biosnoop.bt
+++ b/tools/biosnoop.bt
@@ -25,7 +25,7 @@ kprobe:__blk_account_io_start
@start[arg0] = nsecs;
@iopid[arg0] = pid;
@iocomm[arg0] = comm;
- @disk[arg0] = ((struct request *)arg0)->rq_disk->disk_name;
+ @disk[arg0] = ((struct request *)arg0)->q->disk->disk_name;
}
kprobe:blk_account_io_done,
diff --git a/tools/old/biosnoop.bt b/tools/old/biosnoop.bt
new file mode 100755
index 00000000..1a99643a
--- /dev/null
+++ b/tools/old/biosnoop.bt
@@ -0,0 +1,56 @@
+#!/usr/bin/env bpftrace
+/*
+ * biosnoop.bt Block I/O tracing tool, showing per I/O latency.
+ * For Linux, uses bpftrace, eBPF.
+ *
+ * TODO: switch to block tracepoints. Add offset and size columns.
+ *
+ * This is a bpftrace version of the bcc tool of the same name.
+ *
+ * For Linux <= 5.16.
+ *
+ * 15-Nov-2017 Brendan Gregg Created this.
+ */
+
+#ifndef BPFTRACE_HAVE_BTF
+#include <linux/blkdev.h>
+#include <linux/blk-mq.h>
+#endif
+
+BEGIN
+{
+ printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
+}
+
+kprobe:blk_account_io_start,
+kprobe:__blk_account_io_start
+{
+ @start[arg0] = nsecs;
+ @iopid[arg0] = pid;
+ @iocomm[arg0] = comm;
+ @disk[arg0] = ((struct request *)arg0)->rq_disk->disk_name;
+}
+
+kprobe:blk_account_io_done,
+kprobe:__blk_account_io_done
+/@start[arg0] != 0 && @iopid[arg0] != 0 && @iocomm[arg0] != ""/
+
+{
+ $now = nsecs;
+ printf("%-12u %-7s %-16s %-6d %7d\n",
+ elapsed / 1e6, @disk[arg0], @iocomm[arg0], @iopid[arg0],
+ ($now - @start[arg0]) / 1e6);
+
+ delete(@start[arg0]);
+ delete(@iopid[arg0]);
+ delete(@iocomm[arg0]);
+ delete(@disk[arg0]);
+}
+
+END
+{
+ clear(@start);
+ clear(@iopid);
+ clear(@iocomm);
+ clear(@disk);
+}
--
2.35.3

View File

@ -1,129 +0,0 @@
From f562f8fae4bdb8ac51fc815b725bfaada2371b31 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sat, 4 Sep 2021 17:28:17 -0700
Subject: [PATCH 3/6] orc: Fix build with clang >= 13
Fixes errors like
src/ast/bpforc/bpforcv2.cpp:3:9: error: constructor for 'bpftrace::BpfOrc' must explicitly initialize the member 'ES' which does not have a default constructor
BpfOrc::BpfOrc(TargetMachine *TM, DataLayout DL)
^
Fixes https://github.com/iovisor/bpftrace/issues/1963
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
src/bpforc.h | 23 ++++++++++++++++++++++-
src/bpforcv2.cpp | 23 +++++++++++++++--------
2 files changed, 37 insertions(+), 9 deletions(-)
diff --git a/src/bpforc.h b/src/bpforc.h
index c333a651..cc6a1c97 100644
--- a/src/bpforc.h
+++ b/src/bpforc.h
@@ -20,6 +20,9 @@
#ifdef LLVM_ORC_V2
#include <llvm/ExecutionEngine/Orc/Core.h>
#include <llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h>
+#if LLVM_VERSION_MAJOR >= 13
+#include <llvm/ExecutionEngine/Orc/ExecutorProcessControl.h>
+#endif
#endif
#include <optional>
@@ -71,8 +74,12 @@ class BpfOrc
std::unique_ptr<TargetMachine> TM;
DataLayout DL;
#if LLVM_VERSION_MAJOR >= 7
+#ifdef LLVM_ORC_V2
+ std::unique_ptr<ExecutionSession> ES;
+#else // LLVM_ORC_V1
ExecutionSession ES;
#endif
+#endif
#if LLVM_VERSION_MAJOR >= 7 && LLVM_VERSION_MAJOR < 12
std::shared_ptr<SymbolResolver> Resolver;
#endif
@@ -97,7 +104,21 @@ class BpfOrc
#endif
public:
+#if LLVM_VERSION_MAJOR >= 13
+ ~BpfOrc()
+ {
+ if (auto Err = ES->endSession())
+ ES->reportError(std::move(Err));
+ }
+#endif
+#ifdef LLVM_ORC_V2
+ BpfOrc(TargetMachine *TM,
+ DataLayout DL,
+ std::unique_ptr<ExecutionSession> ES);
+#else
BpfOrc(TargetMachine *TM, DataLayout DL);
+#endif
+
void compile(std::unique_ptr<Module> M);
/* Helper for creating a orc object, responsible for creating internal objects
@@ -130,7 +151,7 @@ class BpfOrc
#ifdef LLVM_ORC_V2
Expected<JITEvaluatedSymbol> lookup(StringRef Name)
{
- return ES.lookup({ &MainJD }, Mangle(Name.str()));
+ return ES->lookup({ &MainJD }, Mangle(Name.str()));
}
#endif
};
diff --git a/src/bpforcv2.cpp b/src/bpforcv2.cpp
index 9876625b..3e6684e4 100644
--- a/src/bpforcv2.cpp
+++ b/src/bpforcv2.cpp
@@ -1,21 +1,23 @@
// Included by bpforc.cpp
-BpfOrc::BpfOrc(TargetMachine *TM, DataLayout DL)
+BpfOrc::BpfOrc(TargetMachine *TM,
+ DataLayout DL,
+ std::unique_ptr<ExecutionSession> ES)
: TM(std::move(TM)),
DL(std::move(DL)),
- ObjectLayer(ES,
+ ES(std::move(ES)),
+ ObjectLayer(*(this->ES),
[this]() {
return std::make_unique<MemoryManager>(sections_);
}),
- CompileLayer(ES,
+ CompileLayer(*this->ES,
ObjectLayer,
std::make_unique<SimpleCompiler>(*this->TM)),
- Mangle(ES, this->DL),
+ Mangle(*this->ES, this->DL),
CTX(std::make_unique<LLVMContext>()),
- MainJD(cantFail(ES.createJITDylib("<main>")))
+ MainJD(cantFail(this->ES->createJITDylib("<main>")))
{
}
-
LLVMContext &BpfOrc::getContext()
{
return *CTX.getContext();
@@ -34,8 +36,13 @@ std::unique_ptr<BpfOrc> BpfOrc::Create()
// return unique_ptrs
auto DL = cantFail(JTMB.getDefaultDataLayoutForTarget());
auto TM = cantFail(JTMB.createTargetMachine());
-
- return std::make_unique<BpfOrc>(TM.release(), std::move(DL));
+#if LLVM_VERSION_MAJOR >= 13
+ auto EPC = SelfExecutorProcessControl::Create();
+ auto ES = std::make_unique<ExecutionSession>(std::move(*EPC));
+#else
+ auto ES = std::make_unique<ExecutionSession>();
+#endif
+ return std::make_unique<BpfOrc>(TM.release(), std::move(DL), std::move(ES));
}
void BpfOrc::compile(std::unique_ptr<Module> M)
--
2.35.3

View File

@ -1,285 +0,0 @@
From 9c9ac55573ac33abbbbad256eda08dafba18bf9f Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Thu, 29 Apr 2021 12:03:28 +0200
Subject: [PATCH 9/9] tools: Make tools rely on BTF instead of header files.
Many distribution already ship BTF and this is more robust that
parsing header files.
Fixes #1820
---
docs/reference_guide.md | 3 ++-
src/clang_parser.cpp | 4 ++++
tools/biosnoop.bt | 5 ++++-
tools/dcsnoop.bt | 2 ++
tools/mdflush.bt | 2 ++
tools/naptime.bt | 2 ++
tools/oomkill.bt | 2 ++
tools/runqlen.bt | 6 ++++--
tools/tcpaccept.bt | 4 ++++
tools/tcpconnect.bt | 4 ++++
tools/tcpdrop.bt | 4 ++++
tools/tcplife.bt | 4 ++++
tools/tcpretrans.bt | 4 ++++
tools/tcpsynbl.bt | 2 ++
14 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/docs/reference_guide.md b/docs/reference_guide.md
index 69a8ed22..11a685de 100644
--- a/docs/reference_guide.md
+++ b/docs/reference_guide.md
@@ -3455,7 +3455,8 @@ Attaching 1 probe...
# BTF Support
If kernel has BTF, kernel types are automatically available and there is no need to include additional headers
-to use them.
+to use them. To allow users to detect this situation in scripts, the preprocessor macro `BPFTRACE_HAVE_BTF`
+is defined if BTF is detected. See tools/ for examples of its usage.
Requirements for using BTF:
diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp
index 20269a71..7fb39949 100644
--- a/src/clang_parser.cpp
+++ b/src/clang_parser.cpp
@@ -747,6 +747,9 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
// Since we're omitting <linux/types.h> there's no reason to
// add the wokarounds for it
args.push_back("-D__CLANG_WORKAROUNDS_H");
+ // Let script know we have BTF -- this is useful for prewritten tools to
+ // conditionally include headers if BTF isn't available.
+ args.push_back("-DBPFTRACE_HAVE_BTF");
if (handler.parse_file("definitions.h", input, args, input_files, false) &&
handler.has_redefinition_error())
@@ -779,6 +782,7 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
// taken from BTF. We cannot use BTF in such a case.
args.pop_back();
args.pop_back();
+ args.pop_back();
input_files.back() = get_empty_btf_generated_header();
}
diff --git a/tools/biosnoop.bt b/tools/biosnoop.bt
index aa88f4ba..33ad75de 100755
--- a/tools/biosnoop.bt
+++ b/tools/biosnoop.bt
@@ -1,5 +1,4 @@
#!/usr/bin/env bpftrace
-#include <linux/blkdev.h>
/*
* biosnoop.bt Block I/O tracing tool, showing per I/O latency.
* For Linux, uses bpftrace, eBPF.
@@ -11,6 +10,10 @@
* 15-Nov-2017 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
+#include <linux/blkdev.h>
+#endif
+
BEGIN
{
printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
diff --git a/tools/dcsnoop.bt b/tools/dcsnoop.bt
index 183f0fb5..e85ab1aa 100755
--- a/tools/dcsnoop.bt
+++ b/tools/dcsnoop.bt
@@ -15,6 +15,7 @@
* 08-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/fs.h>
#include <linux/sched.h>
@@ -24,6 +25,7 @@ struct nameidata {
struct qstr last;
// [...]
};
+#endif
BEGIN
{
diff --git a/tools/mdflush.bt b/tools/mdflush.bt
index 541abba1..1db547f6 100755
--- a/tools/mdflush.bt
+++ b/tools/mdflush.bt
@@ -13,8 +13,10 @@
* 08-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/genhd.h>
#include <linux/bio.h>
+#endif
BEGIN
{
diff --git a/tools/naptime.bt b/tools/naptime.bt
index eb96b677..a84652a3 100755
--- a/tools/naptime.bt
+++ b/tools/naptime.bt
@@ -13,8 +13,10 @@
* 16-Feb-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/time.h>
#include <linux/sched.h>
+#endif
BEGIN
{
diff --git a/tools/oomkill.bt b/tools/oomkill.bt
index 6126682d..1c9b16a3 100755
--- a/tools/oomkill.bt
+++ b/tools/oomkill.bt
@@ -20,7 +20,9 @@
* 07-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/oom.h>
+#endif
BEGIN
{
diff --git a/tools/runqlen.bt b/tools/runqlen.bt
index 02d82d74..1be42adc 100755
--- a/tools/runqlen.bt
+++ b/tools/runqlen.bt
@@ -11,17 +11,19 @@
* 07-Oct-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/sched.h>
// Until BTF is available, we'll need to declare some of this struct manually,
// since it isn't available to be #included. This will need maintenance to match
// your kernel version. It is from kernel/sched/sched.h:
-struct cfs_rq_partial {
+struct cfs_rq {
struct load_weight load;
unsigned long runnable_weight;
unsigned int nr_running;
unsigned int h_nr_running;
};
+#endif
BEGIN
{
@@ -31,7 +33,7 @@ BEGIN
profile:hz:99
{
$task = (struct task_struct *)curtask;
- $my_q = (struct cfs_rq_partial *)$task->se.cfs_rq;
+ $my_q = (struct cfs_rq *)$task->se.cfs_rq;
$len = $my_q->nr_running;
$len = $len > 0 ? $len - 1 : 0; // subtract currently running task
@runqlen = lhist($len, 0, 100, 1);
diff --git a/tools/tcpaccept.bt b/tools/tcpaccept.bt
index b40a041b..2f4dfe1f 100755
--- a/tools/tcpaccept.bt
+++ b/tools/tcpaccept.bt
@@ -16,8 +16,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpconnect.bt b/tools/tcpconnect.bt
index 4ae30d65..05c3ca0e 100755
--- a/tools/tcpconnect.bt
+++ b/tools/tcpconnect.bt
@@ -19,8 +19,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpdrop.bt b/tools/tcpdrop.bt
index 79a86b08..2de3e507 100755
--- a/tools/tcpdrop.bt
+++ b/tools/tcpdrop.bt
@@ -17,8 +17,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcplife.bt b/tools/tcplife.bt
index 9c0d8814..a9a054bf 100755
--- a/tools/tcplife.bt
+++ b/tools/tcplife.bt
@@ -13,10 +13,14 @@
* 17-Apr-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <net/tcp_states.h>
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/tcp.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpretrans.bt b/tools/tcpretrans.bt
index 8b9500c0..777d78fa 100755
--- a/tools/tcpretrans.bt
+++ b/tools/tcpretrans.bt
@@ -17,8 +17,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpsynbl.bt b/tools/tcpsynbl.bt
index 4b3c99c3..0570f29c 100755
--- a/tools/tcpsynbl.bt
+++ b/tools/tcpsynbl.bt
@@ -13,7 +13,9 @@
* 19-Apr-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <net/sock.h>
+#endif
BEGIN
{
--
2.35.3

View File

@ -0,0 +1,457 @@
From 7afe3ced2b91d940a8d72755043ac2468687f1ee Mon Sep 17 00:00:00 2001
From: Viktor Malik <viktor.malik@gmail.com>
Date: Mon, 10 Oct 2022 14:26:38 +0200
Subject: [PATCH] IR builder: get rid of getPointerElementType calls
Usage of Value::getPointerElementType is deprecated and will be dropped
in LLVM 16 [1].
There are several places where we use this method:
- function (value) calls - the called function type is usually
available, so just pass it to createCall, the only exception is
CreateProbeReadStr which must have been refactored
- getting the type of alloca instruction - there is a dedicated
AllocaInst::getAllocatedType method that can be used instead
- strncmp - pass sizes of the strings to CreateStrncmp to be able to get
the correct string type (which is array of uint8)
[1] https://llvm.org/docs/OpaquePointers.html
---
src/ast/irbuilderbpf.cpp | 143 ++++++++++++--------------------
src/ast/irbuilderbpf.h | 23 +++--
src/ast/passes/codegen_llvm.cpp | 30 +++++--
3 files changed, 86 insertions(+), 110 deletions(-)
diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
index d49883f7..4036b2df 100644
--- a/src/ast/irbuilderbpf.cpp
+++ b/src/ast/irbuilderbpf.cpp
@@ -288,17 +288,16 @@ CallInst *IRBuilderBPF::CreateHelperCall(libbpf::bpf_func_id func_id,
Constant *helper_func = ConstantExpr::getCast(Instruction::IntToPtr,
getInt64(func_id),
helper_ptr_type);
- return createCall(helper_func, args, Name);
+ return createCall(helper_type, helper_func, args, Name);
}
-CallInst *IRBuilderBPF::createCall(Value *callee,
+CallInst *IRBuilderBPF::createCall(FunctionType *callee_type,
+ Value *callee,
ArrayRef<Value *> args,
const Twine &Name)
{
#if LLVM_VERSION_MAJOR >= 11
- auto *calleePtrType = cast<PointerType>(callee->getType());
- auto *calleeType = cast<FunctionType>(calleePtrType->getPointerElementType());
- return CreateCall(calleeType, callee, args, Name);
+ return CreateCall(callee_type, callee, args, Name);
#else
return CreateCall(callee, args, Name);
#endif
@@ -307,7 +306,7 @@ CallInst *IRBuilderBPF::createCall(Value *callee,
CallInst *IRBuilderBPF::CreateBpfPseudoCallId(int mapid)
{
Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
- return createCall(pseudo_func,
+ return CreateCall(pseudo_func,
{ getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapid) },
"pseudo");
}
@@ -346,7 +345,8 @@ CallInst *IRBuilderBPF::createMapLookup(int mapid, Value *key)
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_map_lookup_elem),
lookup_func_ptr_type);
- return createCall(lookup_func, { map_ptr, key }, "lookup_elem");
+ return createCall(
+ lookup_func_type, lookup_func, { map_ptr, key }, "lookup_elem");
}
CallInst *IRBuilderBPF::CreateGetJoinMap(Value *ctx, const location &loc)
@@ -397,8 +397,7 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
CREATE_MEMCPY(value, call, type.GetSize(), 1);
else
{
- assert(value->getType()->isPointerTy() &&
- (value->getType()->getPointerElementType() == getInt64Ty()));
+ assert(value->getAllocatedType() == getInt64Ty());
// createMapLookup returns an u8*
auto *cast = CreatePointerCast(call, value->getType(), "cast");
CreateStore(CreateLoad(getInt64Ty(), cast), value);
@@ -448,7 +447,8 @@ void IRBuilderBPF::CreateMapUpdateElem(Value *ctx,
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_map_update_elem),
update_func_ptr_type);
- CallInst *call = createCall(update_func,
+ CallInst *call = createCall(update_func_type,
+ update_func,
{ map_ptr, key, val, flags },
"update_elem");
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_update_elem, loc);
@@ -472,7 +472,8 @@ void IRBuilderBPF::CreateMapDeleteElem(Value *ctx,
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_map_delete_elem),
delete_func_ptr_type);
- CallInst *call = createCall(delete_func, { map_ptr, key }, "delete_elem");
+ CallInst *call = createCall(
+ delete_func_type, delete_func, { map_ptr, key }, "delete_elem");
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_delete_elem, loc);
}
@@ -508,72 +509,53 @@ void IRBuilderBPF::CreateProbeRead(Value *ctx,
Constant *proberead_func = ConstantExpr::getCast(Instruction::IntToPtr,
getInt64(read_fn),
proberead_func_ptr_type);
- CallInst *call = createCall(proberead_func,
+ CallInst *call = createCall(proberead_func_type,
+ proberead_func,
{ dst, size, src },
probeReadHelperName(read_fn));
CreateHelperErrorCond(ctx, call, read_fn, loc);
}
-Constant *IRBuilderBPF::createProbeReadStrFn(llvm::Type *dst,
- llvm::Type *src,
- AddrSpace as)
-{
- assert(src && (src->isIntegerTy() || src->isPointerTy()));
- // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
- FunctionType *probereadstr_func_type = FunctionType::get(
- getInt64Ty(), { dst, getInt32Ty(), src }, false);
- PointerType *probereadstr_func_ptr_type = PointerType::get(
- probereadstr_func_type, 0);
- return ConstantExpr::getCast(Instruction::IntToPtr,
- getInt64(selectProbeReadHelper(as, true)),
- probereadstr_func_ptr_type);
-}
-
CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
- AllocaInst *dst,
+ Value *dst,
size_t size,
Value *src,
AddrSpace as,
const location &loc)
{
- assert(ctx && ctx->getType() == getInt8PtrTy());
return CreateProbeReadStr(ctx, dst, getInt32(size), src, as, loc);
}
CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
Value *dst,
- size_t size,
- Value *src,
- AddrSpace as,
- const location &loc)
-{
- assert(ctx && ctx->getType() == getInt8PtrTy());
- Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
- auto read_fn = selectProbeReadHelper(as, true);
- CallInst *call = createCall(fn,
- { dst, getInt32(size), src },
- probeReadHelperName(read_fn));
- CreateHelperErrorCond(ctx, call, read_fn, loc);
- return call;
-}
-
-CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
- AllocaInst *dst,
llvm::Value *size,
Value *src,
AddrSpace as,
const location &loc)
{
assert(ctx && ctx->getType() == getInt8PtrTy());
- assert(dst && dst->getAllocatedType()->isArrayTy() &&
- dst->getAllocatedType()->getArrayElementType() == getInt8Ty());
assert(size && size->getType()->isIntegerTy());
+ if (auto *dst_alloca = dyn_cast<AllocaInst>(dst))
+ {
+ assert(dst_alloca->getAllocatedType()->isArrayTy() &&
+ dst_alloca->getAllocatedType()->getArrayElementType() ==
+ getInt8Ty());
+ }
- auto *size_i32 = CreateIntCast(size, getInt32Ty(), false);
+ auto *size_i32 = size;
+ if (size_i32->getType()->getScalarSizeInBits() != 32)
+ size_i32 = CreateIntCast(size_i32, getInt32Ty(), false);
- Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
auto read_fn = selectProbeReadHelper(as, true);
- CallInst *call = createCall(fn,
+ // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
+ FunctionType *probereadstr_func_type = FunctionType::get(
+ getInt64Ty(), { dst->getType(), getInt32Ty(), src->getType() }, false);
+ PointerType *probereadstr_func_ptr_type = PointerType::get(
+ probereadstr_func_type, 0);
+ Constant *probereadstr_callee = ConstantExpr::getCast(
+ Instruction::IntToPtr, getInt64(read_fn), probereadstr_func_ptr_type);
+ CallInst *call = createCall(probereadstr_func_type,
+ probereadstr_callee,
{ dst, size_i32, src },
probeReadHelperName(read_fn));
CreateHelperErrorCond(ctx, call, read_fn, loc);
@@ -732,8 +714,10 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
return result;
}
-Value *IRBuilderBPF::CreateStrncmp(Value *val1,
- Value *val2,
+Value *IRBuilderBPF::CreateStrncmp(Value *str1,
+ uint64_t str1_size,
+ Value *str2,
+ uint64_t str2_size,
uint64_t n,
bool inverse)
{
@@ -762,40 +746,21 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
// Check if the compared strings are literals.
// If so, we can avoid storing the literal in memory.
std::optional<std::string> literal1;
- if (auto constString1 = dyn_cast<ConstantDataArray>(val1))
+ if (auto constString1 = dyn_cast<ConstantDataArray>(str1))
literal1 = constString1->getAsString();
- else if (isa<ConstantAggregateZero>(val1))
+ else if (isa<ConstantAggregateZero>(str1))
literal1 = "";
else
literal1 = std::nullopt;
std::optional<std::string> literal2;
- if (auto constString2 = dyn_cast<ConstantDataArray>(val2))
+ if (auto constString2 = dyn_cast<ConstantDataArray>(str2))
literal2 = constString2->getAsString();
- else if (isa<ConstantAggregateZero>(val2))
+ else if (isa<ConstantAggregateZero>(str2))
literal2 = "";
else
literal2 = std::nullopt;
- auto *val1p = dyn_cast<PointerType>(val1->getType());
- auto *val2p = dyn_cast<PointerType>(val2->getType());
-#ifndef NDEBUG
- if (!literal1)
- {
- assert(val1p);
- assert(val1p->getPointerElementType()->isArrayTy() &&
- val1p->getPointerElementType()->getArrayElementType() ==
- getInt8Ty());
- }
- if (!literal2)
- {
- assert(val2p);
- assert(val2p->getPointerElementType()->isArrayTy() &&
- val2p->getPointerElementType()->getArrayElementType() ==
- getInt8Ty());
- }
-#endif
-
Function *parent = GetInsertBlock()->getParent();
AllocaInst *store = CreateAllocaBPF(getInt1Ty(), "strcmp.result");
BasicBlock *str_ne = BasicBlock::Create(module_.getContext(),
@@ -822,8 +787,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
l = getInt8(literal1->c_str()[i]);
else
{
- auto *ptr_l = CreateGEP(val1p->getPointerElementType(),
- val1,
+ auto *ptr_l = CreateGEP(ArrayType::get(getInt8Ty(), str1_size),
+ str1,
{ getInt32(0), getInt32(i) });
l = CreateLoad(getInt8Ty(), ptr_l);
}
@@ -833,8 +798,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
r = getInt8(literal2->c_str()[i]);
else
{
- auto *ptr_r = CreateGEP(val2p->getPointerElementType(),
- val2,
+ auto *ptr_r = CreateGEP(ArrayType::get(getInt8Ty(), str2_size),
+ str2,
{ getInt32(0), getInt32(i) });
r = CreateLoad(getInt8Ty(), ptr_r);
}
@@ -994,11 +959,9 @@ void IRBuilderBPF::CreateGetCurrentComm(Value *ctx,
size_t size,
const location &loc)
{
- assert(buf->getType()->getPointerElementType()->isArrayTy() &&
- buf->getType()->getPointerElementType()->getArrayNumElements() >=
- size &&
- buf->getType()->getPointerElementType()->getArrayElementType() ==
- getInt8Ty());
+ assert(buf->getAllocatedType()->isArrayTy() &&
+ buf->getAllocatedType()->getArrayNumElements() >= size &&
+ buf->getAllocatedType()->getArrayElementType() == getInt8Ty());
// long bpf_get_current_comm(char *buf, int size_of_buf)
// Return: 0 on success or negative error
@@ -1077,7 +1040,7 @@ void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_send_signal),
signal_func_ptr_type);
- CallInst *call = createCall(signal_func, { sig }, "signal");
+ CallInst *call = createCall(signal_func_type, signal_func, { sig }, "signal");
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_send_signal, loc);
}
@@ -1091,7 +1054,7 @@ void IRBuilderBPF::CreateOverrideReturn(Value *ctx, Value *rc)
Constant *override_func = ConstantExpr::getCast(Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_override_return),
override_func_ptr_type);
- createCall(override_func, { ctx, rc }, "override");
+ createCall(override_func_type, override_func, { ctx, rc }, "override");
}
CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
@@ -1126,7 +1089,8 @@ CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_skb_output),
skb_output_func_ptr_type);
- CallInst *call = createCall(skb_output_func,
+ CallInst *call = createCall(skb_output_func_type,
+ skb_output_func,
{ skb, map_ptr, flags, data, size_val },
"skb_output");
return call;
@@ -1320,7 +1284,8 @@ void IRBuilderBPF::CreateSeqPrintf(Value *ctx,
CreateGEP(getInt64Ty(), meta, getInt64(0)),
"seq");
- CallInst *call = createCall(seq_printf_func,
+ CallInst *call = createCall(seq_printf_func_type,
+ seq_printf_func,
{ seq, fmt, fmt_size, data, data_len },
"seq_printf");
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_seq_printf, loc);
diff --git a/src/ast/irbuilderbpf.h b/src/ast/irbuilderbpf.h
index e124911b..c9ffb545 100644
--- a/src/ast/irbuilderbpf.h
+++ b/src/ast/irbuilderbpf.h
@@ -90,17 +90,11 @@ public:
AddrSpace as,
const location &loc);
CallInst *CreateProbeReadStr(Value *ctx,
- AllocaInst *dst,
+ Value *dst,
llvm::Value *size,
Value *src,
AddrSpace as,
const location &loc);
- CallInst *CreateProbeReadStr(Value *ctx,
- AllocaInst *dst,
- size_t size,
- Value *src,
- AddrSpace as,
- const location &loc);
CallInst *CreateProbeReadStr(Value *ctx,
Value *dst,
size_t size,
@@ -115,7 +109,12 @@ public:
pid_t pid,
AddrSpace as,
const location &loc);
- Value *CreateStrncmp(Value *val1, Value *val2, uint64_t n, bool inverse);
+ Value *CreateStrncmp(Value *str1,
+ uint64_t str1_size,
+ Value *str2,
+ uint64_t str2_size,
+ uint64_t n,
+ bool inverse);
CallInst *CreateGetNs(bool boot_time, const location &loc);
CallInst *CreateGetPidTgid(const location &loc);
CallInst *CreateGetCurrentCgroupId(const location &loc);
@@ -131,7 +130,10 @@ public:
ArrayRef<Value *> args,
const Twine &Name,
const location *loc = nullptr);
- CallInst *createCall(Value *callee, ArrayRef<Value *> args, const Twine &Name);
+ CallInst *createCall(FunctionType *callee_type,
+ Value *callee,
+ ArrayRef<Value *> args,
+ const Twine &Name);
void CreateGetCurrentComm(Value *ctx, AllocaInst *buf, size_t size, const location& loc);
void CreatePerfEventOutput(Value *ctx,
Value *data,
@@ -185,9 +187,6 @@ private:
AddrSpace as,
const location &loc);
CallInst *createMapLookup(int mapid, Value *key);
- Constant *createProbeReadStrFn(llvm::Type *dst,
- llvm::Type *src,
- AddrSpace as);
libbpf::bpf_func_id selectProbeReadHelper(AddrSpace as, bool str);
std::map<std::string, StructType *> structs_;
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
index a818ca0b..2b888087 100644
--- a/src/ast/passes/codegen_llvm.cpp
+++ b/src/ast/passes/codegen_llvm.cpp
@@ -1133,8 +1133,12 @@ void CodegenLLVM::visit(Call &call)
auto left_string = getString(left_arg);
auto right_string = getString(right_arg);
- expr_ = b_.CreateStrncmp(
- left_string.first, right_string.first, size, false);
+ expr_ = b_.CreateStrncmp(left_string.first,
+ left_string.second,
+ right_string.first,
+ right_string.second,
+ size,
+ false);
}
else if (call.func == "override")
{
@@ -1269,8 +1273,7 @@ void CodegenLLVM::visit(Variable &var)
else
{
auto *var_alloca = variables_[var.ident];
- expr_ = b_.CreateLoad(var_alloca->getType()->getPointerElementType(),
- var_alloca);
+ expr_ = b_.CreateLoad(var_alloca->getAllocatedType(), var_alloca);
}
}
@@ -1310,7 +1313,12 @@ void CodegenLLVM::binop_string(Binop &binop)
auto right_string = getString(binop.right);
size_t len = std::min(left_string.second, right_string.second);
- expr_ = b_.CreateStrncmp(left_string.first, right_string.first, len, inverse);
+ expr_ = b_.CreateStrncmp(left_string.first,
+ left_string.second,
+ right_string.first,
+ right_string.second,
+ len,
+ inverse);
}
void CodegenLLVM::binop_buf(Binop &binop)
@@ -1334,7 +1342,12 @@ void CodegenLLVM::binop_buf(Binop &binop)
size_t len = std::min(binop.left->type.GetSize(),
binop.right->type.GetSize());
- expr_ = b_.CreateStrncmp(left_string, right_string, len, inverse);
+ expr_ = b_.CreateStrncmp(left_string,
+ binop.left->type.GetSize(),
+ right_string,
+ binop.right->type.GetSize(),
+ len,
+ inverse);
}
void CodegenLLVM::binop_int(Binop &binop)
@@ -3528,9 +3541,8 @@ void CodegenLLVM::createIncDec(Unop &unop)
else if (unop.expr->is_variable)
{
Variable &var = static_cast<Variable &>(*unop.expr);
- Value *oldval = b_.CreateLoad(
- variables_[var.ident]->getType()->getPointerElementType(),
- variables_[var.ident]);
+ Value *oldval = b_.CreateLoad(variables_[var.ident]->getAllocatedType(),
+ variables_[var.ident]);
Value *newval;
if (is_increment)
newval = b_.CreateAdd(oldval, b_.GetIntSameSize(step, oldval));
--
2.38.1

View File

@ -0,0 +1,74 @@
From 7ebf8a0a6f257770ad21bcea70d56a2671528238 Mon Sep 17 00:00:00 2001
From: Viktor Malik <viktor.malik@gmail.com>
Date: Fri, 16 Dec 2022 13:31:46 +0100
Subject: [PATCH 2/2] RHEL9: remove not existing attachpoints from tools
tools/bio* attempt to attach each probe to multiple kprobes to cover all
possible systems. Remove probes which do not exist in RHEL9 to remove
unnecessary warnings.
---
tools/biolatency.bt | 2 --
tools/biosnoop.bt | 2 --
tools/biostacks.bt | 2 --
3 files changed, 6 deletions(-)
diff --git a/tools/biolatency.bt b/tools/biolatency.bt
index d5af1f29..8fb0490d 100755
--- a/tools/biolatency.bt
+++ b/tools/biolatency.bt
@@ -16,13 +16,11 @@ BEGIN
printf("Tracing block device I/O... Hit Ctrl-C to end.\n");
}
-kprobe:blk_account_io_start,
kprobe:__blk_account_io_start
{
@start[arg0] = nsecs;
}
-kprobe:blk_account_io_done,
kprobe:__blk_account_io_done
/@start[arg0]/
{
diff --git a/tools/biosnoop.bt b/tools/biosnoop.bt
index 65e302fc..c26aa3be 100755
--- a/tools/biosnoop.bt
+++ b/tools/biosnoop.bt
@@ -20,7 +20,6 @@ BEGIN
printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
}
-kprobe:blk_account_io_start,
kprobe:__blk_account_io_start
{
@start[arg0] = nsecs;
@@ -29,7 +28,6 @@ kprobe:__blk_account_io_start
@disk[arg0] = ((struct request *)arg0)->q->disk->disk_name;
}
-kprobe:blk_account_io_done,
kprobe:__blk_account_io_done
/@start[arg0] != 0 && @iopid[arg0] != 0 && @iocomm[arg0] != ""/
diff --git a/tools/biostacks.bt b/tools/biostacks.bt
index 1bc9f819..0d848ada 100755
--- a/tools/biostacks.bt
+++ b/tools/biostacks.bt
@@ -18,14 +18,12 @@ BEGIN
printf("Tracing block I/O with init stacks. Hit Ctrl-C to end.\n");
}
-kprobe:blk_account_io_start,
kprobe:__blk_account_io_start
{
@reqstack[arg0] = kstack;
@reqts[arg0] = nsecs;
}
-kprobe:blk_start_request,
kprobe:blk_mq_start_request
/@reqts[arg0]/
{
--
2.38.1

View File

@ -0,0 +1,174 @@
From 652562eef0d53649cf29c256bc20abdffdd195ab Mon Sep 17 00:00:00 2001
From: Rong Tao <rongtao@cestc.cn>
Date: Sat, 1 Oct 2022 16:15:27 +0800
Subject: [PATCH 1/2] tcpdrop: Fix: ERROR: Error attaching probe:
'kprobe:tcp_drop'
kernel commit 8fbf195798b5('tcp_drop() is no longer needed.') remove
the kprobe:tcp_drop, bcc commit 16eab39171eb('Add
tracepoint:skb:kfree_skb if no tcp_drop() kprobe.') already fix this
problem.
CI old kernel is too old and not support the 'reason' field, move the
old tools/tcpdrop.bt into tools/old/tcpdrop.bt and set the CI to use
it.
Since 5.17 support trace_kfree_skb(skb, ..., reason) 'reason' field.
Since 5.19 remove tcp_drop() function.
ERROR log:
$ sudo ./tcpdrop.bt
./tcpdrop.bt:49-51: WARNING: tcp_drop is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
Attaching 3 probes...
cannot attach kprobe, probe entry may not exist
ERROR: Error attaching probe: 'kprobe:tcp_drop'
Link: https://github.com/iovisor/bpftrace/pull/2379
Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
tools/old/tcpdrop.bt | 85 ++++++++++++++++++++++++++++++++++++++++++++
tools/tcpdrop.bt | 22 ++++++------
2 files changed, 97 insertions(+), 10 deletions(-)
create mode 100755 tools/old/tcpdrop.bt
diff --git a/tools/old/tcpdrop.bt b/tools/old/tcpdrop.bt
new file mode 100755
index 00000000..685a5f6a
--- /dev/null
+++ b/tools/old/tcpdrop.bt
@@ -0,0 +1,85 @@
+#!/usr/bin/env bpftrace
+/*
+ * tcpdrop.bt Trace TCP kernel-dropped packets/segments.
+ * For Linux, uses bpftrace and eBPF.
+ *
+ * USAGE: tcpdrop.bt
+ *
+ * This is a bpftrace version of the bcc tool of the same name.
+ * It is limited to ipv4 addresses, and cannot show tcp flags.
+ *
+ * This provides information such as packet details, socket state, and kernel
+ * stack trace for packets/segments that were dropped via tcp_drop().
+
+ * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
+ * on newer kernels and not replaced by anything else, therefore
+ * the script will stop working
+ *
+ * For Linux <= 5.18.
+ *
+ * Copyright (c) 2018 Dale Hamel.
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ *
+ * 23-Nov-2018 Dale Hamel created this.
+ */
+
+#ifndef BPFTRACE_HAVE_BTF
+#include <linux/socket.h>
+#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
+
+BEGIN
+{
+ printf("Tracing tcp drops. Hit Ctrl-C to end.\n");
+ printf("%-8s %-8s %-16s %-21s %-21s %-8s\n", "TIME", "PID", "COMM", "SADDR:SPORT", "DADDR:DPORT", "STATE");
+
+ // See https://github.com/torvalds/linux/blob/master/include/net/tcp_states.h
+ @tcp_states[1] = "ESTABLISHED";
+ @tcp_states[2] = "SYN_SENT";
+ @tcp_states[3] = "SYN_RECV";
+ @tcp_states[4] = "FIN_WAIT1";
+ @tcp_states[5] = "FIN_WAIT2";
+ @tcp_states[6] = "TIME_WAIT";
+ @tcp_states[7] = "CLOSE";
+ @tcp_states[8] = "CLOSE_WAIT";
+ @tcp_states[9] = "LAST_ACK";
+ @tcp_states[10] = "LISTEN";
+ @tcp_states[11] = "CLOSING";
+ @tcp_states[12] = "NEW_SYN_RECV";
+}
+
+kprobe:tcp_drop
+{
+ $sk = ((struct sock *) arg0);
+ $inet_family = $sk->__sk_common.skc_family;
+
+ if ($inet_family == AF_INET || $inet_family == AF_INET6) {
+ if ($inet_family == AF_INET) {
+ $daddr = ntop($sk->__sk_common.skc_daddr);
+ $saddr = ntop($sk->__sk_common.skc_rcv_saddr);
+ } else {
+ $daddr = ntop($sk->__sk_common.skc_v6_daddr.in6_u.u6_addr8);
+ $saddr = ntop($sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr8);
+ }
+ $lport = $sk->__sk_common.skc_num;
+ $dport = $sk->__sk_common.skc_dport;
+
+ // Destination port is big endian, it must be flipped
+ $dport = bswap($dport);
+
+ $state = $sk->__sk_common.skc_state;
+ $statestr = @tcp_states[$state];
+
+ time("%H:%M:%S ");
+ printf("%-8d %-16s ", pid, comm);
+ printf("%39s:%-6d %39s:%-6d %-10s\n", $saddr, $lport, $daddr, $dport, $statestr);
+ printf("%s\n", kstack);
+ }
+}
+
+END
+{
+ clear(@tcp_states);
+}
diff --git a/tools/tcpdrop.bt b/tools/tcpdrop.bt
index 3450a533..bb31107f 100755
--- a/tools/tcpdrop.bt
+++ b/tools/tcpdrop.bt
@@ -9,16 +9,15 @@
* It is limited to ipv4 addresses, and cannot show tcp flags.
*
* This provides information such as packet details, socket state, and kernel
- * stack trace for packets/segments that were dropped via tcp_drop().
-
- * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
- * on newer kernels and not replaced by anything else, therefore
- * the script will stop working
-
+ * stack trace for packets/segments that were dropped via kfree_skb.
+ *
+ * For Linux 5.17+ (see tools/old for script for lower versions).
+ *
* Copyright (c) 2018 Dale Hamel.
* Licensed under the Apache License, Version 2.0 (the "License")
-
+ *
* 23-Nov-2018 Dale Hamel created this.
+ * 01-Oct-2022 Rong Tao use tracepoint:skb:kfree_skb
*/
#ifndef BPFTRACE_HAVE_BTF
@@ -48,12 +47,15 @@ BEGIN
@tcp_states[12] = "NEW_SYN_RECV";
}
-kprobe:tcp_drop
+tracepoint:skb:kfree_skb
{
- $sk = ((struct sock *) arg0);
+ $reason = args->reason;
+ $skb = (struct sk_buff *)args->skbaddr;
+ $sk = ((struct sock *) $skb->sk);
$inet_family = $sk->__sk_common.skc_family;
- if ($inet_family == AF_INET || $inet_family == AF_INET6) {
+ if ($reason > SKB_DROP_REASON_NOT_SPECIFIED &&
+ ($inet_family == AF_INET || $inet_family == AF_INET6)) {
if ($inet_family == AF_INET) {
$daddr = ntop($sk->__sk_common.skc_daddr);
$saddr = ntop($sk->__sk_common.skc_rcv_saddr);
--
2.38.1

View File

@ -1,20 +1,21 @@
Name: bpftrace
Version: 0.13.1
Version: 0.16.0
Release: 1%{?dist}
Summary: High-level tracing language for Linux eBPF
License: ASL 2.0
%define cereal_version 1.3.2
URL: https://github.com/iovisor/bpftrace
Source0: %{url}/archive/v%{version}/%{name}-%{version}.tar.gz
Patch0: %{name}-%{version}-RHEL-9-fixes.patch
Patch1: %{name}-%{version}-Fix-mdflush.patch
Patch2: %{name}-%{version}-orc-Fix-build-with-clang-13.patch
Patch3: %{name}-%{version}-Update-bio-tools-to-work-on-kernel-5.16.patch
Patch4: %{name}-%{version}-tools-Make-tools-rely-on-BTF-instead-of-header-files.patch
Patch5: %{name}-%{version}-Fix-libbtf-0.6.0-build.patch
Patch6: %{name}-%{version}-Fix-compile-under-llvm14-trunk.patch
Patch7: %{name}-%{version}-Fix-LLVM-13-warnings.patch
Patch8: %{name}-%{version}-biosnoop.bt-handle-Linux-5.17-block-layer-update.patch
# Cereal is a header-only serialization library which is not packaged into
# RHEL9, so we download it manually. This is ok to do as it is only necessary
# for build.
Source1: https://github.com/USCiLab/cereal/archive/v%{cereal_version}/cereal-%{cereal_version}.tar.gz
Patch0: %{name}-%{version}-IR-builder-get-rid-of-getPointerElementType-calls.patch
Patch1: %{name}-%{version}-tcpdrop-Fix-ERROR-Error-attaching-probe-kprobe-tcp_d.patch
Patch2: %{name}-%{version}-RHEL9-remove-not-existing-attachpoints-from-tools.patch
Patch10: %{name}-%{version}-RHEL-aarch64-fixes-statsnoop-and-opensnoop.patch
@ -47,7 +48,7 @@ and predecessor tracers such as DTrace and SystemTap
%prep
%autosetup -N
%autosetup -N -a 1
%autopatch -p1 -M 9
%ifarch aarch64
@ -55,6 +56,9 @@ and predecessor tracers such as DTrace and SystemTap
%endif
%build
# Set CPATH so that CMake finds the cereal headers
CPATH=$PWD/cereal-%{cereal_version}/include:$CPATH
export CPATH
%cmake . \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_TESTING:BOOL=OFF \
@ -84,11 +88,22 @@ find %{buildroot}%{_datadir}/%{name}/tools -type f -exec \
%dir %{_datadir}/%{name}/tools
%dir %{_datadir}/%{name}/tools/doc
%{_bindir}/%{name}
%{_bindir}/%{name}-aotrt
%{_mandir}/man8/*
%attr(0755,-,-) %{_datadir}/%{name}/tools/*.bt
%{_datadir}/%{name}/tools/doc/*.txt
# Do not include old versions of tools, they do not work on RHEL 9
%exclude %{_datadir}/%{name}/tools/old
%changelog
* Fri Dec 16 2022 Viktor Malik <vmalik@redhat.com> - 0.16.0-1
- Rebase on bpftrace 0.16.0 (rhbz#2121920)
- Rebuild for LLVM 15 (rhbz#2118995)
- Download the cereal library (not packaged into RHEL9)
- Fixed several tools (rhbz#1975148, rhbz#2088577, rhbz#2128208, rhbz#2073675,
rhbz#2073770)
- Resolve conflicts between bpftrace and bcc manpages (rhbz#2075076)
* Mon May 16 2022 Jerome Marchand <jmarchan@redhat.com> - 0.13.1-1
- Rebase to bpftrace 0.13.1
- Rebuild for LLVM14

View File

@ -1 +1,2 @@
SHA512 (bpftrace-0.13.1.tar.gz) = fa810067023ddd6cbf2fd4c7c46138dd756eb9b3e37d00d049017f201f71c6a69ac2eb261619eb7e83356dc43fa1a0ed4d78b40f2ff36d76caa15e5c754ab224
SHA512 (bpftrace-0.16.0.tar.gz) = 52ca4fea4e2f8d2cbf0f9f1bc69af0ee3408201f019006dd2e838b9458cfc01761eba3df24c39e05cf93220d85d0cecc69bb44ec72f9f44cec0eb94479bff734
SHA512 (cereal-1.3.2.tar.gz) = 98d306d6292789129675f1c5c5aedcb90cfcc1029c4482893a8f9b23f3c9755e5ed4762d7a528f215345cae6392e87cd8d89467115b6f031b41c8673d6b4b109