Commit 32e2efa4 authored by Alastair Robertson's avatar Alastair Robertson

Add stack pointer builtin (sp)

parent 36d75e06
......@@ -192,6 +192,7 @@ Variables:
- `arg0`, `arg1`, ... etc. - Arguments to the function being traced
- `retval` - Return value from function being traced
- `func` - Name of the function currently being traced
- `sp` - Stack pointer
Functions:
- `quantize(int n)` - Produce a log2 histogram of values of `n`
......
......@@ -5,10 +5,12 @@
namespace bpftrace {
namespace arch {
// Offsets are based off the pt_regs struct from the Linux kernel
int max_arg();
int arg_offset(int arg_num);
int ret_offset();
int pc_offset();
int sp_offset();
std::string name();
} // namespace arch
......
......@@ -34,6 +34,11 @@ int pc_offset()
return 16; // ip
}
int sp_offset()
{
return 19; // sp
}
std::string name()
{
return std::string("x86_64");
......
......@@ -75,30 +75,25 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_ = buf;
}
else if (!builtin.ident.compare(0, 3, "arg") && builtin.ident.size() == 4 &&
builtin.ident.at(3) >= '0' && builtin.ident.at(3) <= '9')
builtin.ident.at(3) >= '0' && builtin.ident.at(3) <= '9' ||
builtin.ident == "retval" ||
builtin.ident == "func" ||
builtin.ident == "sp")
{
int arg_num = atoi(builtin.ident.substr(3).c_str());
int offset;
if (builtin.ident == "retval")
offset = arch::ret_offset() * sizeof(uintptr_t);
else if (builtin.ident == "func")
offset = arch::pc_offset() * sizeof(uintptr_t);
else if (builtin.ident == "sp")
offset = arch::sp_offset() * sizeof(uintptr_t);
else // argX
{
int arg_num = atoi(builtin.ident.substr(3).c_str());
offset = arch::arg_offset(arg_num) * sizeof(uintptr_t);
}
AllocaInst *dst = b_.CreateAllocaBPF(builtin.type, builtin.ident);
int offset = arch::arg_offset(arg_num) * sizeof(uintptr_t);
Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset));
b_.CreateProbeRead(dst, 8, src);
expr_ = b_.CreateLoad(dst);
b_.CreateLifetimeEnd(dst);
}
else if (builtin.ident == "retval")
{
AllocaInst *dst = b_.CreateAllocaBPF(builtin.type, builtin.ident);
int offset = arch::ret_offset() * sizeof(uintptr_t);
Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset));
b_.CreateProbeRead(dst, 8, src);
expr_ = b_.CreateLoad(dst);
b_.CreateLifetimeEnd(dst);
}
else if (builtin.ident == "func")
{
AllocaInst *dst = b_.CreateAllocaBPF(builtin.type, builtin.ident);
int offset = arch::pc_offset() * sizeof(uintptr_t);
Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset));
b_.CreateProbeRead(dst, 8, src);
expr_ = b_.CreateLoad(dst);
......
......@@ -33,6 +33,7 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin.ident == "uid" ||
builtin.ident == "gid" ||
builtin.ident == "cpu" ||
builtin.ident == "sp" ||
builtin.ident == "retval") {
builtin.type = SizedType(Type::integer, 8);
}
......
......@@ -648,6 +648,48 @@ attributes #1 = { argmemonly nounwind }
)EXPECTED");
}
TEST(codegen, builtin_sp)
{
test("kprobe:f { @x = sp }",
R"EXPECTED(; Function Attrs: nounwind
declare i64 @llvm.bpf.pseudo(i64, i64) #0
; Function Attrs: argmemonly nounwind
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1
define i64 @"kprobe:f"(i8*) local_unnamed_addr section "s_kprobe:f" {
entry:
%"@x_val" = alloca i64, align 8
%"@x_key" = alloca i64, align 8
%sp = alloca i64, align 8
%1 = bitcast i64* %sp to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
%2 = getelementptr i8, i8* %0, i64 152
%probe_read = call i64 inttoptr (i64 4 to i64 (i8*, i64, i8*)*)(i64* nonnull %sp, i64 8, i8* %2)
%3 = load i64, i64* %sp, align 8
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %1)
%4 = bitcast i64* %"@x_key" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %4)
store i64 0, i64* %"@x_key", align 8
%5 = bitcast i64* %"@x_val" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %5)
store i64 %3, i64* %"@x_val", align 8
%pseudo = call i64 @llvm.bpf.pseudo(i64 1, i64 1)
%update_elem = call i64 inttoptr (i64 2 to i64 (i8*, i8*, i8*, i64)*)(i64 %pseudo, i64* nonnull %"@x_key", i64* nonnull %"@x_val", i64 0)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %4)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %5)
ret i64 0
}
; Function Attrs: argmemonly nounwind
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
attributes #0 = { nounwind }
attributes #1 = { argmemonly nounwind }
)EXPECTED");
}
TEST(codegen, call_quantize)
{
test("kprobe:f { @x = quantize(pid) }",
......
......@@ -45,6 +45,36 @@ void test(const std::string &input, int expected_result=0)
test(bpftrace, driver, input, expected_result);
}
TEST(semantic_analyser, builtin_variables)
{
test("kprobe:f { pid }", 0);
test("kprobe:f { tid }", 0);
test("kprobe:f { uid }", 0);
test("kprobe:f { gid }", 0);
test("kprobe:f { nsecs }", 0);
test("kprobe:f { cpu }", 0);
test("kprobe:f { comm }", 0);
test("kprobe:f { stack }", 0);
test("kprobe:f { ustack }", 0);
test("kprobe:f { arg0 }", 0);
test("kprobe:f { retval }", 0);
test("kprobe:f { func }", 0);
test("kprobe:f { sp }", 0);
test("kprobe:f { fake }", 1);
}
TEST(semantic_analyser, builtin_functions)
{
test("kprobe:f { @x = quantize(123) }", 0);
test("kprobe:f { @x = count() }", 0);
test("kprobe:f { @x = delete() }", 0);
test("kprobe:f { str(0xffff) }", 0);
test("kprobe:f { printf(\"hello\\n\") }", 0);
test("kprobe:f { sym(0xffff) }", 0);
test("kprobe:f { usym(0xffff) }", 0);
test("kprobe:f { fake() }", 1);
}
TEST(semantic_analyser, probe_count)
{
MockBPFtrace bpftrace;
......
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