Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bpftrace
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bpftrace
Commits
b834cd3b
Commit
b834cd3b
authored
Jul 22, 2018
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parser: Pass through includes and structs as strings
parent
4cc2e864
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
72 additions
and
77 deletions
+72
-77
src/ast/ast.cpp
src/ast/ast.cpp
+0
-4
src/ast/ast.h
src/ast/ast.h
+3
-15
src/ast/codegen_llvm.cpp
src/ast/codegen_llvm.cpp
+0
-6
src/ast/codegen_llvm.h
src/ast/codegen_llvm.h
+0
-1
src/ast/printer.cpp
src/ast/printer.cpp
+3
-11
src/ast/printer.h
src/ast/printer.h
+0
-1
src/ast/semantic_analyser.cpp
src/ast/semantic_analyser.cpp
+0
-6
src/ast/semantic_analyser.h
src/ast/semantic_analyser.h
+0
-1
src/lexer.l
src/lexer.l
+27
-12
src/parser.yy
src/parser.yy
+8
-12
tests/parser.cpp
tests/parser.cpp
+31
-8
No files found.
src/ast/ast.cpp
View file @
b834cd3b
...
@@ -68,10 +68,6 @@ void Probe::accept(Visitor &v) {
...
@@ -68,10 +68,6 @@ void Probe::accept(Visitor &v) {
v
.
visit
(
*
this
);
v
.
visit
(
*
this
);
}
}
void
Include
::
accept
(
Visitor
&
v
)
{
v
.
visit
(
*
this
);
}
void
Program
::
accept
(
Visitor
&
v
)
{
void
Program
::
accept
(
Visitor
&
v
)
{
v
.
visit
(
*
this
);
v
.
visit
(
*
this
);
}
}
...
...
src/ast/ast.h
View file @
b834cd3b
...
@@ -199,22 +199,11 @@ public:
...
@@ -199,22 +199,11 @@ public:
};
};
using
ProbeList
=
std
::
vector
<
Probe
*>
;
using
ProbeList
=
std
::
vector
<
Probe
*>
;
class
Include
:
public
Node
{
public:
explicit
Include
(
const
std
::
string
&
file
,
bool
system_header
)
:
file
(
file
),
system_header
(
system_header
)
{
}
std
::
string
file
;
bool
system_header
;
void
accept
(
Visitor
&
v
)
override
;
};
using
IncludeList
=
std
::
vector
<
Include
*>
;
class
Program
:
public
Node
{
class
Program
:
public
Node
{
public:
public:
Program
(
IncludeList
*
include
s
,
ProbeList
*
probes
)
Program
(
const
std
::
string
&
c_definition
s
,
ProbeList
*
probes
)
:
includes
(
include
s
),
probes
(
probes
)
{
}
:
c_definitions
(
c_definition
s
),
probes
(
probes
)
{
}
IncludeList
*
include
s
;
std
::
string
c_definition
s
;
ProbeList
*
probes
;
ProbeList
*
probes
;
void
accept
(
Visitor
&
v
)
override
;
void
accept
(
Visitor
&
v
)
override
;
...
@@ -239,7 +228,6 @@ public:
...
@@ -239,7 +228,6 @@ public:
virtual
void
visit
(
Predicate
&
pred
)
=
0
;
virtual
void
visit
(
Predicate
&
pred
)
=
0
;
virtual
void
visit
(
AttachPoint
&
ap
)
=
0
;
virtual
void
visit
(
AttachPoint
&
ap
)
=
0
;
virtual
void
visit
(
Probe
&
probe
)
=
0
;
virtual
void
visit
(
Probe
&
probe
)
=
0
;
virtual
void
visit
(
Include
&
include
)
=
0
;
virtual
void
visit
(
Program
&
program
)
=
0
;
virtual
void
visit
(
Program
&
program
)
=
0
;
};
};
...
...
src/ast/codegen_llvm.cpp
View file @
b834cd3b
...
@@ -410,14 +410,8 @@ void CodegenLLVM::visit(Probe &probe)
...
@@ -410,14 +410,8 @@ void CodegenLLVM::visit(Probe &probe)
b_
.
CreateRet
(
ConstantInt
::
get
(
module_
->
getContext
(),
APInt
(
64
,
0
)));
b_
.
CreateRet
(
ConstantInt
::
get
(
module_
->
getContext
(),
APInt
(
64
,
0
)));
}
}
void
CodegenLLVM
::
visit
(
Include
&
include
)
{
}
void
CodegenLLVM
::
visit
(
Program
&
program
)
void
CodegenLLVM
::
visit
(
Program
&
program
)
{
{
for
(
Include
*
include
:
*
program
.
includes
)
include
->
accept
(
*
this
);
for
(
Probe
*
probe
:
*
program
.
probes
)
for
(
Probe
*
probe
:
*
program
.
probes
)
probe
->
accept
(
*
this
);
probe
->
accept
(
*
this
);
}
}
...
...
src/ast/codegen_llvm.h
View file @
b834cd3b
...
@@ -43,7 +43,6 @@ public:
...
@@ -43,7 +43,6 @@ public:
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Include
&
include
)
override
;
void
visit
(
Program
&
program
)
override
;
void
visit
(
Program
&
program
)
override
;
AllocaInst
*
getMapKey
(
Map
&
map
);
AllocaInst
*
getMapKey
(
Map
&
map
);
AllocaInst
*
getQuantizeMapKey
(
Map
&
map
,
Value
*
log2
);
AllocaInst
*
getQuantizeMapKey
(
Map
&
map
,
Value
*
log2
);
...
...
src/ast/printer.cpp
View file @
b834cd3b
...
@@ -167,23 +167,15 @@ void Printer::visit(Probe &probe)
...
@@ -167,23 +167,15 @@ void Printer::visit(Probe &probe)
--
depth_
;
--
depth_
;
}
}
void
Printer
::
visit
(
Include
&
include
)
{
std
::
string
indent
(
depth_
,
' '
);
if
(
include
.
system_header
)
out_
<<
indent
<<
"#include <"
<<
include
.
file
<<
">"
<<
std
::
endl
;
else
out_
<<
indent
<<
"#include
\"
"
<<
include
.
file
<<
"
\"
"
<<
std
::
endl
;
}
void
Printer
::
visit
(
Program
&
program
)
void
Printer
::
visit
(
Program
&
program
)
{
{
if
(
program
.
c_definitions
.
size
()
>
0
)
out_
<<
program
.
c_definitions
<<
std
::
endl
;
std
::
string
indent
(
depth_
,
' '
);
std
::
string
indent
(
depth_
,
' '
);
out_
<<
indent
<<
"Program"
<<
std
::
endl
;
out_
<<
indent
<<
"Program"
<<
std
::
endl
;
++
depth_
;
++
depth_
;
for
(
Include
*
include
:
*
program
.
includes
)
include
->
accept
(
*
this
);
for
(
Probe
*
probe
:
*
program
.
probes
)
for
(
Probe
*
probe
:
*
program
.
probes
)
probe
->
accept
(
*
this
);
probe
->
accept
(
*
this
);
--
depth_
;
--
depth_
;
...
...
src/ast/printer.h
View file @
b834cd3b
...
@@ -26,7 +26,6 @@ public:
...
@@ -26,7 +26,6 @@ public:
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Include
&
include
)
override
;
void
visit
(
Program
&
program
)
override
;
void
visit
(
Program
&
program
)
override
;
int
depth_
=
0
;
int
depth_
=
0
;
...
...
src/ast/semantic_analyser.cpp
View file @
b834cd3b
...
@@ -479,14 +479,8 @@ void SemanticAnalyser::visit(Probe &probe)
...
@@ -479,14 +479,8 @@ void SemanticAnalyser::visit(Probe &probe)
}
}
}
}
void
SemanticAnalyser
::
visit
(
Include
&
include
)
{
}
void
SemanticAnalyser
::
visit
(
Program
&
program
)
void
SemanticAnalyser
::
visit
(
Program
&
program
)
{
{
for
(
Include
*
include
:
*
program
.
includes
)
include
->
accept
(
*
this
);
for
(
Probe
*
probe
:
*
program
.
probes
)
for
(
Probe
*
probe
:
*
program
.
probes
)
probe
->
accept
(
*
this
);
probe
->
accept
(
*
this
);
}
}
...
...
src/ast/semantic_analyser.h
View file @
b834cd3b
...
@@ -34,7 +34,6 @@ public:
...
@@ -34,7 +34,6 @@ public:
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
Predicate
&
pred
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
AttachPoint
&
ap
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Probe
&
probe
)
override
;
void
visit
(
Include
&
include
)
override
;
void
visit
(
Program
&
program
)
override
;
void
visit
(
Program
&
program
)
override
;
int
analyse
();
int
analyse
();
...
...
src/lexer.l
View file @
b834cd3b
...
@@ -9,7 +9,8 @@
...
@@ -9,7 +9,8 @@
#define yywrap(x) 1
#define yywrap(x) 1
static bpftrace::location loc;
static bpftrace::location loc;
static std::string string_buffer;
static std::string buffer;
static int open_brackets;
#define YY_USER_ACTION loc.columns(yyleng);
#define YY_USER_ACTION loc.columns(yyleng);
#define yyterminate() return bpftrace::Parser::make_END(loc)
#define yyterminate() return bpftrace::Parser::make_END(loc)
...
@@ -25,8 +26,8 @@ hspace [ \t]
...
@@ -25,8 +26,8 @@ hspace [ \t]
vspace [\n\r]
vspace [\n\r]
space {hspace}|{vspace}
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9])*:
path :(\\.|[_\-\./a-zA-Z0-9])*:
header <(\\.|[_\-\./a-zA-Z0-9])*>
%x STR
%x STR
%x STRUCT
%%
%%
...
@@ -40,9 +41,7 @@ header <(\\.|[_\-\./a-zA-Z0-9])*>
...
@@ -40,9 +41,7 @@ header <(\\.|[_\-\./a-zA-Z0-9])*>
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
return Parser::make_BUILTIN(yytext, loc); }
return Parser::make_BUILTIN(yytext, loc); }
{ident} { return Parser::make_IDENT(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{header} { return Parser::make_HEADER(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{var} { return Parser::make_VAR(yytext, loc); }
{var} { return Parser::make_VAR(yytext, loc); }
{int} { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
{int} { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
...
@@ -75,17 +74,17 @@ pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
...
@@ -75,17 +74,17 @@ pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
"^" { return Parser::make_BXOR(loc); }
"^" { return Parser::make_BXOR(loc); }
"!" { return Parser::make_LNOT(loc); }
"!" { return Parser::make_LNOT(loc); }
"~" { return Parser::make_BNOT(loc); }
"~" { return Parser::make_BNOT(loc); }
"#include" { return Parser::make_INCLUDE(loc); }
"." { return Parser::make_DOT(loc); }
"." { return Parser::make_DOT(loc); }
"->" { return Parser::make_PTR(loc); }
"->" { return Parser::make_PTR(loc); }
"#".* { return Parser::make_CPREPROC(yytext, loc); }
\" { BEGIN(STR);
string_
buffer.clear(); }
\" { BEGIN(STR); buffer.clear(); }
<STR>\" { BEGIN(INITIAL); return Parser::make_STRING(
string_
buffer, loc); }
<STR>\" { BEGIN(INITIAL); return Parser::make_STRING(buffer, loc); }
<STR>[^\\\n\"]+ {
string_
buffer += std::string(yytext); }
<STR>[^\\\n\"]+ { buffer += std::string(yytext); }
<STR>\\n {
string_
buffer += std::string("\n"); }
<STR>\\n { buffer += std::string("\n"); }
<STR>\\t {
string_
buffer += std::string("\t"); }
<STR>\\t { buffer += std::string("\t"); }
<STR>\\\" {
string_
buffer += std::string("\""); }
<STR>\\\" { buffer += std::string("\""); }
<STR>\\\\ {
string_
buffer += std::string("\\"); }
<STR>\\\\ { buffer += std::string("\\"); }
<STR>\n { driver.error(loc, std::string("unterminated string")); }
<STR>\n { driver.error(loc, std::string("unterminated string")); }
<STR><<EOF>> { driver.error(loc, std::string("unterminated string")); }
<STR><<EOF>> { driver.error(loc, std::string("unterminated string")); }
<STR>\\. { driver.error(loc, std::string("invalid escape character '") +
<STR>\\. { driver.error(loc, std::string("invalid escape character '") +
...
@@ -93,6 +92,22 @@ pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
...
@@ -93,6 +92,22 @@ pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
<STR>. { driver.error(loc, std::string("invalid character '") +
<STR>. { driver.error(loc, std::string("invalid character '") +
std::string(yytext) + std::string("'")); }
std::string(yytext) + std::string("'")); }
struct { BEGIN(STRUCT); buffer = std::string(yytext); open_brackets = 0; }
<STRUCT>"{" { BEGIN(STRUCT); buffer += std::string(yytext); open_brackets++; }
<STRUCT>"}" { buffer += std::string(yytext);
if (open_brackets == 1)
{
BEGIN(INITIAL); return Parser::make_STRUCT(buffer, loc);
}
else
{
open_brackets--;
}
}
<STRUCT>\n { buffer += std::string(yytext); }
<STRUCT>. { buffer += std::string(yytext); }
{ident} { return Parser::make_IDENT(yytext, loc); }
. { driver.error(loc, std::string("invalid character '") +
. { driver.error(loc, std::string("invalid character '") +
std::string(yytext) + std::string("'")); }
std::string(yytext) + std::string("'")); }
...
...
src/parser.yy
View file @
b834cd3b
...
@@ -65,7 +65,6 @@ void yyerror(bpftrace::Driver &driver, const char *s);
...
@@ -65,7 +65,6 @@ void yyerror(bpftrace::Driver &driver, const char *s);
BXOR "^"
BXOR "^"
LNOT "!"
LNOT "!"
BNOT "~"
BNOT "~"
INCLUDE "#include"
DOT "."
DOT "."
PTR "->"
PTR "->"
;
;
...
@@ -73,14 +72,14 @@ void yyerror(bpftrace::Driver &driver, const char *s);
...
@@ -73,14 +72,14 @@ void yyerror(bpftrace::Driver &driver, const char *s);
%token <std::string> BUILTIN "builtin"
%token <std::string> BUILTIN "builtin"
%token <std::string> IDENT "identifier"
%token <std::string> IDENT "identifier"
%token <std::string> PATH "path"
%token <std::string> PATH "path"
%token <std::string> HEADER "header"
%token <std::string> CPREPROC "preprocessor directive"
%token <std::string> STRUCT "struct"
%token <std::string> STRING "string"
%token <std::string> STRING "string"
%token <std::string> MAP "map"
%token <std::string> MAP "map"
%token <std::string> VAR "variable"
%token <std::string> VAR "variable"
%token <int> INT "integer"
%token <int> INT "integer"
%type <ast::IncludeList *> includes
%type <std::string> c_definitions
%type <ast::Include *> include
%type <ast::ProbeList *> probes
%type <ast::ProbeList *> probes
%type <ast::Probe *> probe
%type <ast::Probe *> probe
%type <ast::Predicate *> pred
%type <ast::Predicate *> pred
...
@@ -114,15 +113,12 @@ void yyerror(bpftrace::Driver &driver, const char *s);
...
@@ -114,15 +113,12 @@ void yyerror(bpftrace::Driver &driver, const char *s);
%%
%%
program :
include
s probes { driver.root_ = new ast::Program($1, $2); }
program :
c_definition
s probes { driver.root_ = new ast::Program($1, $2); }
;
;
includes : includes include { $$ = $1; $1->push_back($2); }
c_definitions : CPREPROC c_definitions { $$ = $1 + "\n" + $2; }
| { $$ = new ast::IncludeList; }
| STRUCT c_definitions { $$ = $1 + "\n" + $2; }
;
| { $$ = std::string(); }
include : INCLUDE STRING { $$ = new ast::Include($2, false); }
| INCLUDE HEADER { $$ = new ast::Include($2.substr(1, $2.size()-2), true); }
;
;
probes : probes probe { $$ = $1; $1->push_back($2); }
probes : probes probe { $$ = $1; $1->push_back($2); }
...
...
tests/parser.cpp
View file @
b834cd3b
...
@@ -326,9 +326,10 @@ TEST(Parser, short_map_name)
...
@@ -326,9 +326,10 @@ TEST(Parser, short_map_name)
TEST
(
Parser
,
include
)
TEST
(
Parser
,
include
)
{
{
test
(
"#include <stdio.h> kprobe:sys_read { @x = 1 }"
,
test
(
"#include <stdio.h>
\n
kprobe:sys_read { @x = 1 }"
,
"#include <stdio.h>
\n
"
"
\n
"
"Program
\n
"
"Program
\n
"
" #include <stdio.h>
\n
"
" kprobe:sys_read
\n
"
" kprobe:sys_read
\n
"
" =
\n
"
" =
\n
"
" map: @x
\n
"
" map: @x
\n
"
...
@@ -337,9 +338,10 @@ TEST(Parser, include)
...
@@ -337,9 +338,10 @@ TEST(Parser, include)
TEST
(
Parser
,
include_quote
)
TEST
(
Parser
,
include_quote
)
{
{
test
(
"#include
\"
stdio.h
\"
kprobe:sys_read { @x = 1 }"
,
test
(
"#include
\"
stdio.h
\"\n
kprobe:sys_read { @x = 1 }"
,
"#include
\"
stdio.h
\"\n
"
"
\n
"
"Program
\n
"
"Program
\n
"
" #include
\"
stdio.h
\"\n
"
" kprobe:sys_read
\n
"
" kprobe:sys_read
\n
"
" =
\n
"
" =
\n
"
" map: @x
\n
"
" map: @x
\n
"
...
@@ -348,11 +350,12 @@ TEST(Parser, include_quote)
...
@@ -348,11 +350,12 @@ TEST(Parser, include_quote)
TEST
(
Parser
,
include_multiple
)
TEST
(
Parser
,
include_multiple
)
{
{
test
(
"#include <stdio.h> #include
\"
blah
\"
#include <foo.h> kprobe:sys_read { @x = 1 }"
,
test
(
"#include <stdio.h>
\n
#include
\"
blah
\"\n
#include <foo.h>
\n
kprobe:sys_read { @x = 1 }"
,
"#include <stdio.h>
\n
"
"#include
\"
blah
\"\n
"
"#include <foo.h>
\n
"
"
\n
"
"Program
\n
"
"Program
\n
"
" #include <stdio.h>
\n
"
" #include
\"
blah
\"\n
"
" #include <foo.h>
\n
"
" kprobe:sys_read
\n
"
" kprobe:sys_read
\n
"
" =
\n
"
" =
\n
"
" map: @x
\n
"
" map: @x
\n
"
...
@@ -507,6 +510,26 @@ TEST(Parser, field_access_builtin)
...
@@ -507,6 +510,26 @@ TEST(Parser, field_access_builtin)
" count
\n
"
);
" count
\n
"
);
}
}
TEST
(
Parser
,
cstruct
)
{
test
(
"struct Foo { int x, y; char *str; } kprobe:sys_read { 1; }"
,
"struct Foo { int x, y; char *str; }
\n
"
"
\n
"
"Program
\n
"
" kprobe:sys_read
\n
"
" int: 1
\n
"
);
}
TEST
(
Parser
,
cstruct_nested
)
{
test
(
"struct Foo { struct { int x; } bar; } kprobe:sys_read { 1; }"
,
"struct Foo { struct { int x; } bar; }
\n
"
"
\n
"
"Program
\n
"
" kprobe:sys_read
\n
"
" int: 1
\n
"
);
}
}
// namespace parser
}
// namespace parser
}
// namespace test
}
// namespace test
}
// namespace bpftrace
}
// namespace bpftrace
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment