Commit e6b0ebf6 authored by williangaspar's avatar williangaspar
parent 9196c222
......@@ -302,6 +302,13 @@ void CodegenLLVM::visit(Call &call)
call.vargs->front()->accept(*this);
b_.CreateProbeReadStr(buf, call.type.size, expr_);
expr_ = buf;
}
else if (call.func == "kaddr")
{
uint64_t addr;
auto &name = static_cast<String&>(*call.vargs->at(0)).str;
addr = bpftrace_.resolve_kname(name.c_str());
expr_ = b_.getInt64(addr);
}
else if (call.func == "join")
{
......
......@@ -209,6 +209,14 @@ void SemanticAnalyser::visit(Call &call)
call.type = SizedType(Type::integer, 8);
}
else if (call.func == "kaddr") {
if (check_nargs(call, 1)) {
if (check_arg(call, Type::string, 0, true)) {
;
}
}
call.type = SizedType(Type::integer, 8);
}
else if (call.func == "printf") {
check_assignment(call, false, false);
if (check_varargs(call, 1, 7)) {
......
......@@ -1117,6 +1117,41 @@ std::string BPFtrace::resolve_sym(uintptr_t addr, bool show_offset)
return symbol.str();
}
uint64_t BPFtrace::resolve_kname(const char *name)
{
uint64_t addr = 0;
std::string file_name = "/proc/kallsyms";
std::ifstream file(file_name);
if (file.fail())
{
std::cerr << strerror(errno) << ": " << file_name << std::endl;
return addr;
}
std::string line;
std::string search = "\\b( ";
search += name;
std::regex e (search + ")");
std::smatch match;
while (std::getline(file, line) && addr == 0)
{
auto found = std::regex_search (line, match, e);
if (found)
{
std::string first_word = line.substr(0, line.find(" "));
addr = std::stoull(first_word, 0, 16);
}
}
file.close();
return addr;
}
std::string BPFtrace::resolve_usym(uintptr_t addr, int pid, bool show_offset)
{
struct bcc_symbol sym;
......
......@@ -37,6 +37,7 @@ public:
std::string get_stack(uint64_t stackidpid, bool ustack, int indent=0);
std::string resolve_sym(uintptr_t addr, bool show_offset=false);
std::string resolve_usym(uintptr_t addr, int pid, bool show_offset=false);
uint64_t resolve_kname(const char *name);
std::string resolve_name(uint64_t name_id);
int pid_;
......
......@@ -1024,6 +1024,23 @@ attributes #1 = { argmemonly nounwind }
)EXPECTED");
}
TEST(codegen, call_kaddr)
{
test("kprobe:f { @x = kaddr(\"avenrun\") }",
R"EXPECTED(; Function Attrs: nounwind
declare i64 @llvm.bpf.pseudo(i64, i64) #0
BDG run bpftrace_test, and copy-n-paste the '+' diff output here until bpftrace_test passes this test
; 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_hist)
{
test("kprobe:f { @x = hist(pid) }",
......
......@@ -299,6 +299,17 @@ TEST(Parser, call_unknown_function)
" call: myfunc\n");
}
TEST(Parser, call_kaddr)
{
test("kprobe:f { @ = kaddr(\"avenrun\") }",
"Program\n"
" kprobe:f\n"
" =\n"
" map: @\n"
" call: kaddr\n"
" string: avenrun\n");
}
TEST(Parser, multiple_probes)
{
test("kprobe:sys_open { 1; } kretprobe:sys_open { 2; }",
......
......@@ -253,6 +253,14 @@ TEST(semantic_analyser, call_usym)
test("kprobe:f { usym(\"hello\"); }", 10);
}
TEST(semantic_analyser, call_kaddr)
{
test("kprobe:f { kaddr(\"avenrun\"); }", 0);
test("kprobe:f { @x = kaddr(\"avenrun\"); }", 0);
test("kprobe:f { kaddr(); }", 1);
test("kprobe:f { kaddr(123); }", 1);
}
TEST(semantic_analyser, call_reg)
{
test("kprobe:f { reg(\"ip\"); }", 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