Commit 9196c222 authored by Brendan Gregg's avatar Brendan Gregg Committed by GitHub

Merge pull request #46 from iovisor/ctx

add ctx builtin
parents be8b6d59 f989bca1
......@@ -116,6 +116,11 @@ void CodegenLLVM::visit(Builtin &builtin)
name_id++;
expr_ = b_.getInt64(builtin.name_id);
}
else if (builtin.ident == "ctx")
{
// undocumented builtin: for debugging
expr_ = ctx_;
}
else
{
abort();
......
......@@ -34,6 +34,7 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin.ident == "cpu" ||
builtin.ident == "curtask" ||
builtin.ident == "rand" ||
builtin.ident == "ctx" ||
builtin.ident == "retval") {
builtin.type = SizedType(Type::integer, 8);
}
......
......@@ -46,7 +46,7 @@ path :(\\.|[_\-\./a-zA-Z0-9])*:
<COMMENT>"EOF" driver.error(loc, std::string("end of file during comment"));
<COMMENT>.|"\n" ;
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func|name|curtask|rand {
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func|name|curtask|rand|ctx {
return Parser::make_BUILTIN(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
......
......@@ -597,6 +597,42 @@ attributes #1 = { argmemonly nounwind }
)EXPECTED");
}
TEST(codegen, builtin_ctx)
{
test("kprobe:f { @x = ctx }",
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
%1 = bitcast i64* %"@x_key" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
store i64 0, i64* %"@x_key", align 8
%2 = zext i8* %0 to i64
%3 = bitcast i64* %"@x_val" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %3)
store i64 %2, i64* %"@x_val", align 8
%pseudo = tail 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 %1)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %3)
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, builtin_comm)
{
test("kprobe:f { @x = comm }",
......
......@@ -31,6 +31,7 @@ TEST(Parser, builtin_variables)
test("kprobe:f { cpu }", "Program\n kprobe:f\n builtin: cpu\n");
test("kprobe:f { curtask }", "Program\n kprobe:f\n builtin: curtask\n");
test("kprobe:f { rand }", "Program\n kprobe:f\n builtin: rand\n");
test("kprobe:f { ctx }", "Program\n kprobe:f\n builtin: ctx\n");
test("kprobe:f { comm }", "Program\n kprobe:f\n builtin: comm\n");
test("kprobe:f { stack }", "Program\n kprobe:f\n builtin: stack\n");
test("kprobe:f { ustack }", "Program\n kprobe:f\n builtin: ustack\n");
......
......@@ -59,6 +59,7 @@ TEST(semantic_analyser, builtin_variables)
test("kprobe:f { cpu }", 0);
test("kprobe:f { curtask }", 0);
test("kprobe:f { rand }", 0);
test("kprobe:f { ctx }", 0);
test("kprobe:f { comm }", 0);
test("kprobe:f { stack }", 0);
test("kprobe:f { ustack }", 0);
......
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