From 5143209e8744d242431229972d9affa32ba3cc1a Mon Sep 17 00:00:00 2001 From: Matheus Marchini Date: Fri, 12 Apr 2019 16:27:51 -0700 Subject: [PATCH 1/3] [clang_parser] pass BPFtrace as arg instead of StructMap --- src/clang_parser.cpp | 6 +-- src/clang_parser.h | 3 +- src/main.cpp | 2 +- tests/clang_parser.cpp | 71 +++++++++++++++++++++++------------ tests/codegen/call_kstack.cpp | 4 +- tests/codegen/call_ustack.cpp | 4 +- tests/codegen/common.h | 2 +- tests/codegen/general.cpp | 2 +- tests/probe.cpp | 2 +- tests/semantic_analyser.cpp | 2 +- 10 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp index b1db8ff..4bb8f87 100644 --- a/src/clang_parser.cpp +++ b/src/clang_parser.cpp @@ -172,7 +172,7 @@ static std::tuple get_kernel_dirs(const struct utsname return std::make_tuple(ksrc, kobj); } -void ClangParser::parse(ast::Program *program, StructMap &structs) +void ClangParser::parse(ast::Program *program, BPFtrace &bpftrace) { auto input = program->c_definitions; if (input.size() == 0) @@ -259,7 +259,6 @@ void ClangParser::parse(ast::Program *program, StructMap &structs) cursor, [](CXCursor c, CXCursor parent, CXClientData client_data) { - auto &structs = *static_cast(client_data); if (clang_getCursorKind(parent) != CXCursor_StructDecl && clang_getCursorKind(parent) != CXCursor_UnionDecl) @@ -267,6 +266,7 @@ void ClangParser::parse(ast::Program *program, StructMap &structs) if (clang_getCursorKind(c) == CXCursor_FieldDecl) { + auto &structs = static_cast(client_data)->structs_; auto struct_name = get_parent_struct_name(c); auto ident = get_clang_string(clang_getCursorSpelling(c)); auto offset = clang_Cursor_getOffsetOfField(c) / 8; @@ -290,7 +290,7 @@ void ClangParser::parse(ast::Program *program, StructMap &structs) return CXChildVisit_Recurse; }, - &structs); + &bpftrace); clang_disposeTranslationUnit(translation_unit); clang_disposeIndex(index); diff --git a/src/clang_parser.h b/src/clang_parser.h index d2ada5d..4289796 100644 --- a/src/clang_parser.h +++ b/src/clang_parser.h @@ -1,6 +1,7 @@ #pragma once #include "struct.h" +#include "bpftrace.h" namespace bpftrace { @@ -11,7 +12,7 @@ using StructMap = std::map; class ClangParser { public: - void parse(ast::Program *program, StructMap &structs); + void parse(ast::Program *program, BPFtrace &bpftrace); }; } // namespace bpftrace diff --git a/src/main.cpp b/src/main.cpp index ec3882d..f6659bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -272,7 +272,7 @@ int main(int argc, char *argv[]) } ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); err = semantics.analyse(); diff --git a/tests/clang_parser.cpp b/tests/clang_parser.cpp index f12a5e4..0c1ca31 100644 --- a/tests/clang_parser.cpp +++ b/tests/clang_parser.cpp @@ -1,25 +1,28 @@ #include "gtest/gtest.h" #include "clang_parser.h" #include "driver.h" +#include "bpftrace.h" namespace bpftrace { namespace test { namespace clang_parser { -void parse(const std::string &input, StructMap &structs) +void parse(const std::string &input, BPFtrace &bpftrace) { auto extended_input = input + "kprobe:sys_read { 1 }"; Driver driver; ASSERT_EQ(driver.parse_str(extended_input), 0); ClangParser clang; - clang.parse(driver.root_, structs); + clang.parse(driver.root_, bpftrace); } TEST(clang_parser, integers) { - StructMap structs; - parse("struct Foo { int x; int y, z; }", structs); + BPFtrace bpftrace; + parse("struct Foo { int x; int y, z; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 1U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -45,8 +48,10 @@ TEST(clang_parser, integers) TEST(clang_parser, c_union) { - StructMap structs; - parse("union Foo { char c; short s; int i; long l; }", structs); + BPFtrace bpftrace; + parse("union Foo { char c; short s; int i; long l; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 1U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -77,8 +82,10 @@ TEST(clang_parser, c_union) TEST(clang_parser, integer_ptr) { - StructMap structs; - parse("struct Foo { int *x; }", structs); + BPFtrace bpftrace; + parse("struct Foo { int *x; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 1U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -96,8 +103,10 @@ TEST(clang_parser, integer_ptr) TEST(clang_parser, string_ptr) { - StructMap structs; - parse("struct Foo { char *str; }", structs); + BPFtrace bpftrace; + parse("struct Foo { char *str; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 1U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -115,8 +124,10 @@ TEST(clang_parser, string_ptr) TEST(clang_parser, string_array) { - StructMap structs; - parse("struct Foo { char str[32]; }", structs); + BPFtrace bpftrace; + parse("struct Foo { char str[32]; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 1U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -132,8 +143,10 @@ TEST(clang_parser, string_array) TEST(clang_parser, nested_struct_named) { - StructMap structs; - parse("struct Bar { int x; } struct Foo { struct Bar bar; }", structs); + BPFtrace bpftrace; + parse("struct Bar { int x; } struct Foo { struct Bar bar; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 2U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -151,8 +164,10 @@ TEST(clang_parser, nested_struct_named) TEST(clang_parser, nested_struct_ptr_named) { - StructMap structs; - parse("struct Bar { int x; } struct Foo { struct Bar *bar; }", structs); + BPFtrace bpftrace; + parse("struct Bar { int x; } struct Foo { struct Bar *bar; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 2U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -172,8 +187,10 @@ TEST(clang_parser, nested_struct_ptr_named) TEST(clang_parser, nested_struct_anon) { - StructMap structs; - parse("struct Foo { struct { int x; } bar; }", structs); + BPFtrace bpftrace; + parse("struct Foo { struct { int x; } bar; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.size(), 2U); ASSERT_EQ(structs.count("Foo"), 1U); @@ -190,8 +207,10 @@ TEST(clang_parser, nested_struct_anon) TEST(clang_parser, nested_struct_indirect_fields) { - StructMap structs; - parse("struct Foo { struct { int x; int y;}; int a; struct { int z; }; }", structs); + BPFtrace bpftrace; + parse("struct Foo { struct { int x; int y;}; int a; struct { int z; }; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs["Foo"].fields.size(), 4U); EXPECT_EQ(structs["Foo"].fields["x"].offset, 0); @@ -206,8 +225,10 @@ TEST(clang_parser, nested_struct_indirect_fields) TEST(clang_parser, nested_struct_anon_union_struct) { - StructMap structs; - parse("struct Foo { union { long long _xy; struct { int x; int y;}; }; int a; struct { int z; }; }", structs); + BPFtrace bpftrace; + parse("struct Foo { union { long long _xy; struct { int x; int y;}; }; int a; struct { int z; }; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs["Foo"].fields.size(), 5U); EXPECT_EQ(structs["Foo"].fields["_xy"].offset, 0); @@ -225,8 +246,10 @@ TEST(clang_parser, nested_struct_anon_union_struct) TEST(clang_parser, builtin_headers) { // size_t is definied in stddef.h - StructMap structs; - parse("#include \nstruct Foo { size_t x, y, z; }", structs); + BPFtrace bpftrace; + parse("#include \nstruct Foo { size_t x, y, z; }", bpftrace); + + StructMap &structs = bpftrace.structs_; ASSERT_EQ(structs.count("Foo"), 1U); diff --git a/tests/codegen/call_kstack.cpp b/tests/codegen/call_kstack.cpp index a184af2..e64d498 100644 --- a/tests/codegen/call_kstack.cpp +++ b/tests/codegen/call_kstack.cpp @@ -68,7 +68,7 @@ TEST(codegen, call_kstack_mapids) ASSERT_EQ(driver.parse_str("kprobe:f { @x = kstack(5); @y = kstack(6); @z = kstack(6) }"), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); @@ -96,7 +96,7 @@ TEST(codegen, call_kstack_modes_mapids) ASSERT_EQ(driver.parse_str("kprobe:f { @x = kstack(perf); @y = kstack(bpftrace); @z = kstack() }"), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); diff --git a/tests/codegen/call_ustack.cpp b/tests/codegen/call_ustack.cpp index 8e80558..1941d36 100644 --- a/tests/codegen/call_ustack.cpp +++ b/tests/codegen/call_ustack.cpp @@ -74,7 +74,7 @@ TEST(codegen, call_ustack_mapids) ASSERT_EQ(driver.parse_str("kprobe:f { @x = ustack(5); @y = ustack(6); @z = ustack(6) }"), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); @@ -102,7 +102,7 @@ TEST(codegen, call_ustack_modes_mapids) ASSERT_EQ(driver.parse_str("kprobe:f { @x = ustack(perf); @y = ustack(bpftrace); @z = ustack() }"), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); diff --git a/tests/codegen/common.h b/tests/codegen/common.h index 32f8bc8..bdf733a 100644 --- a/tests/codegen/common.h +++ b/tests/codegen/common.h @@ -30,7 +30,7 @@ static void test(const std::string &input, const std::string expected_output) ASSERT_EQ(driver.parse_str(input), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); diff --git a/tests/codegen/general.cpp b/tests/codegen/general.cpp index e7e7439..e67ae10 100644 --- a/tests/codegen/general.cpp +++ b/tests/codegen/general.cpp @@ -45,7 +45,7 @@ TEST(codegen, printf_offsets) // TODO (mmarchini): also test printf with a string argument ASSERT_EQ(driver.parse_str("struct Foo { char c; int i; } kprobe:f { $foo = (Foo*)0; printf(\"%c %u\\n\", $foo->c, $foo->i) }"), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); ASSERT_EQ(semantics.create_maps(true), 0); diff --git a/tests/probe.cpp b/tests/probe.cpp index e030830..cb9b765 100644 --- a/tests/probe.cpp +++ b/tests/probe.cpp @@ -28,7 +28,7 @@ void gen_bytecode(const std::string &input, std::stringstream &out) ASSERT_EQ(driver.parse_str(input), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); ast::SemanticAnalyser semantics(driver.root_, bpftrace); ASSERT_EQ(semantics.analyse(), 0); diff --git a/tests/semantic_analyser.cpp b/tests/semantic_analyser.cpp index 2067ed9..4e2485b 100644 --- a/tests/semantic_analyser.cpp +++ b/tests/semantic_analyser.cpp @@ -16,7 +16,7 @@ void test(BPFtrace &bpftrace, Driver &driver, const std::string &input, int expe ASSERT_EQ(driver.parse_str(input), 0); ClangParser clang; - clang.parse(driver.root_, bpftrace.structs_); + clang.parse(driver.root_, bpftrace); std::stringstream out; ast::SemanticAnalyser semantics(driver.root_, bpftrace, out); -- 2.20.1