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
a28012be
Commit
a28012be
authored
Jul 08, 2017
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add string type
parent
e20808ff
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
103 additions
and
15 deletions
+103
-15
src/ast.cpp
src/ast.cpp
+4
-0
src/ast.h
src/ast.h
+9
-0
src/bpftrace.cpp
src/bpftrace.cpp
+2
-0
src/codegen_llvm.cpp
src/codegen_llvm.cpp
+30
-6
src/codegen_llvm.h
src/codegen_llvm.h
+1
-0
src/irbuilderbpf.cpp
src/irbuilderbpf.cpp
+32
-9
src/irbuilderbpf.h
src/irbuilderbpf.h
+1
-0
src/lexer.l
src/lexer.l
+2
-0
src/mapkey.cpp
src/mapkey.cpp
+2
-0
src/parser.yy
src/parser.yy
+2
-0
src/printer.cpp
src/printer.cpp
+6
-0
src/printer.h
src/printer.h
+1
-0
src/semantic_analyser.cpp
src/semantic_analyser.cpp
+8
-0
src/semantic_analyser.h
src/semantic_analyser.h
+1
-0
src/types.cpp
src/types.cpp
+1
-0
src/types.h
src/types.h
+1
-0
No files found.
src/ast.cpp
View file @
a28012be
...
...
@@ -8,6 +8,10 @@ void Integer::accept(Visitor &v) {
v
.
visit
(
*
this
);
}
void
String
::
accept
(
Visitor
&
v
)
{
v
.
visit
(
*
this
);
}
void
Builtin
::
accept
(
Visitor
&
v
)
{
v
.
visit
(
*
this
);
}
...
...
src/ast.h
View file @
a28012be
...
...
@@ -30,6 +30,14 @@ public:
void
accept
(
Visitor
&
v
)
override
;
};
class
String
:
public
Expression
{
public:
explicit
String
(
std
::
string
str
)
:
str
(
str
)
{
}
std
::
string
str
;
void
accept
(
Visitor
&
v
)
override
;
};
class
Builtin
:
public
Expression
{
public:
explicit
Builtin
(
std
::
string
ident
)
:
ident
(
ident
)
{
}
...
...
@@ -144,6 +152,7 @@ class Visitor {
public:
virtual
~
Visitor
()
{
}
virtual
void
visit
(
Integer
&
integer
)
=
0
;
virtual
void
visit
(
String
&
string
)
=
0
;
virtual
void
visit
(
Builtin
&
builtin
)
=
0
;
virtual
void
visit
(
Call
&
call
)
=
0
;
virtual
void
visit
(
Map
&
map
)
=
0
;
...
...
src/bpftrace.cpp
View file @
a28012be
...
...
@@ -106,6 +106,8 @@ int BPFtrace::print_map(Map &map) const
std
::
cout
<<
get_stack
(
*
(
uint32_t
*
)
value
.
data
(),
false
,
8
);
else
if
(
map
.
type_
.
type
==
Type
::
ustack
)
std
::
cout
<<
get_stack
(
*
(
uint32_t
*
)
value
.
data
(),
true
,
8
);
else
if
(
map
.
type_
.
type
==
Type
::
string
)
std
::
cout
<<
value
.
data
()
<<
std
::
endl
;
else
std
::
cout
<<
*
(
int64_t
*
)
value
.
data
()
<<
std
::
endl
;
...
...
src/codegen_llvm.cpp
View file @
a28012be
...
...
@@ -17,6 +17,15 @@ void CodegenLLVM::visit(Integer &integer)
expr_
=
b_
.
getInt64
(
integer
.
n
);
}
void
CodegenLLVM
::
visit
(
String
&
string
)
{
string
.
str
.
resize
(
string
.
type
.
size
-
1
);
Constant
*
const_str
=
ConstantDataArray
::
getString
(
module_
->
getContext
(),
string
.
str
,
true
);
AllocaInst
*
buf
=
b_
.
CreateAllocaBPF
(
string
.
type
,
"str"
);
b_
.
CreateStore
(
b_
.
CreateGEP
(
const_str
,
b_
.
getInt64
(
0
)),
buf
);
expr_
=
buf
;
}
void
CodegenLLVM
::
visit
(
Builtin
&
builtin
)
{
if
(
builtin
.
ident
==
"nsecs"
)
...
...
@@ -146,10 +155,19 @@ void CodegenLLVM::visit(AssignMapStatement &assignment)
assignment
.
expr
->
accept
(
*
this
);
AllocaInst
*
val
=
b_
.
CreateAllocaBPF
(
map
.
type
,
map
.
ident
+
"_val"
);
b_
.
CreateStore
(
expr_
,
val
);
AllocaInst
*
key
=
getMapKey
(
map
);
b_
.
CreateMapUpdateElem
(
map
,
key
,
val
);
if
(
assignment
.
expr
->
type
.
type
==
Type
::
string
)
{
Value
*
val
=
expr_
;
AllocaInst
*
key
=
getMapKey
(
map
);
b_
.
CreateMapUpdateElem
(
map
,
key
,
val
);
}
else
{
AllocaInst
*
val
=
b_
.
CreateAllocaBPF
(
map
.
type
,
map
.
ident
+
"_val"
);
b_
.
CreateStore
(
expr_
,
val
);
AllocaInst
*
key
=
getMapKey
(
map
);
b_
.
CreateMapUpdateElem
(
map
,
key
,
val
);
}
}
void
CodegenLLVM
::
visit
(
AssignMapCallStatement
&
assignment
)
...
...
@@ -256,7 +274,10 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map)
for
(
Expression
*
expr
:
*
map
.
vargs
)
{
expr
->
accept
(
*
this
);
Value
*
offset_val
=
b_
.
CreateGEP
(
key
,
{
b_
.
getInt64
(
0
),
b_
.
getInt64
(
offset
)});
b_
.
CreateStore
(
expr_
,
offset_val
);
if
(
expr
->
type
.
type
==
Type
::
string
)
b_
.
CreateMemcpy
(
offset_val
,
expr_
,
b_
.
getInt64
(
expr
->
type
.
size
));
else
b_
.
CreateStore
(
expr_
,
offset_val
);
offset
+=
expr
->
type
.
size
;
}
}
...
...
@@ -283,7 +304,10 @@ AllocaInst *CodegenLLVM::getQuantizeMapKey(Map &map, Value *log2)
for
(
Expression
*
expr
:
*
map
.
vargs
)
{
expr
->
accept
(
*
this
);
Value
*
offset_val
=
b_
.
CreateGEP
(
key
,
{
b_
.
getInt64
(
0
),
b_
.
getInt64
(
offset
)});
b_
.
CreateStore
(
expr_
,
offset_val
);
if
(
expr
->
type
.
type
==
Type
::
string
)
b_
.
CreateMemcpy
(
offset_val
,
expr_
,
b_
.
getInt64
(
expr
->
type
.
size
));
else
b_
.
CreateStore
(
expr_
,
offset_val
);
offset
+=
expr
->
type
.
size
;
}
Value
*
offset_val
=
b_
.
CreateGEP
(
key
,
{
b_
.
getInt64
(
0
),
b_
.
getInt64
(
offset
)});
...
...
src/codegen_llvm.h
View file @
a28012be
...
...
@@ -24,6 +24,7 @@ public:
{
}
void
visit
(
Integer
&
integer
)
override
;
void
visit
(
String
&
string
)
override
;
void
visit
(
Builtin
&
builtin
)
override
;
void
visit
(
Call
&
call
)
override
;
void
visit
(
Map
&
map
)
override
;
...
...
src/irbuilderbpf.cpp
View file @
a28012be
...
...
@@ -23,21 +23,38 @@ IRBuilderBPF::IRBuilderBPF(LLVMContext &context,
GlobalValue
::
ExternalLinkage
,
"llvm.bpf.pseudo"
,
&
module_
);
FunctionType
*
memcpy_func_type
=
FunctionType
::
get
(
getVoidTy
(),
{
getInt8PtrTy
(),
getInt8PtrTy
(),
getInt64Ty
(),
getInt32Ty
(),
getInt1Ty
()},
false
);
Function
::
Create
(
memcpy_func_type
,
GlobalValue
::
ExternalLinkage
,
"llvm.memcpy.p0i8.p0i8.i64"
,
&
module_
);
}
AllocaInst
*
IRBuilderBPF
::
CreateAllocaBPF
(
const
SizedType
&
stype
,
const
std
::
string
&
name
)
{
llvm
::
Type
*
ty
;
switch
(
stype
.
size
)
if
(
stype
.
type
==
Type
::
string
)
{
case
8
:
ty
=
getInt64Ty
();
break
;
case
4
:
ty
=
getInt32Ty
();
break
;
default:
abort
();
ty
=
ArrayType
::
get
(
getInt8Ty
(),
stype
.
size
);
}
else
{
switch
(
stype
.
size
)
{
case
8
:
ty
=
getInt64Ty
();
break
;
case
4
:
ty
=
getInt32Ty
();
break
;
default:
abort
();
}
}
Function
*
parent
=
GetInsertBlock
()
->
getParent
();
BasicBlock
&
entry_block
=
parent
->
getEntryBlock
();
...
...
@@ -59,6 +76,12 @@ AllocaInst *IRBuilderBPF::CreateAllocaMapKey(int bytes, const std::string &name)
return
new
AllocaInst
(
ty
,
name
,
&
entry_block
.
front
());
}
void
IRBuilderBPF
::
CreateMemcpy
(
Value
*
dst
,
Value
*
src
,
Value
*
len
)
{
Function
*
memcpy_func
=
module_
.
getFunction
(
"llvm.memcpy.p0i8.p0i8.i64"
);
CreateCall
(
memcpy_func
,
{
dst
,
src
,
len
,
getInt32
(
1
),
getInt1
(
0
)},
"memcpy"
);
}
CallInst
*
IRBuilderBPF
::
CreateBpfPseudoCall
(
int
mapfd
)
{
Function
*
pseudo_func
=
module_
.
getFunction
(
"llvm.bpf.pseudo"
);
...
...
src/irbuilderbpf.h
View file @
a28012be
...
...
@@ -20,6 +20,7 @@ public:
AllocaInst
*
CreateAllocaBPF
(
const
SizedType
&
stype
,
const
std
::
string
&
name
=
""
);
AllocaInst
*
CreateAllocaMapKey
(
int
bytes
,
const
std
::
string
&
name
=
""
);
void
CreateMemcpy
(
Value
*
dst
,
Value
*
src
,
Value
*
len
);
CallInst
*
CreateBpfPseudoCall
(
int
mapfd
);
CallInst
*
CreateBpfPseudoCall
(
Map
&
map
);
LoadInst
*
CreateMapLookupElem
(
Map
&
map
,
AllocaInst
*
key
);
...
...
src/lexer.l
View file @
a28012be
...
...
@@ -23,6 +23,7 @@ hspace [ \t]
vspace [\n\r]
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9])*:
string \"(\\.|[^\\"])*\"
%%
...
...
@@ -36,6 +37,7 @@ path :(\\.|[_\-\./a-zA-Z0-9])*:
{ident} { return Parser::make_IDENT(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{string} { return Parser::make_STRING(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{int} { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
":" { return Parser::make_COLON(loc); }
...
...
src/mapkey.cpp
View file @
a28012be
...
...
@@ -69,6 +69,8 @@ std::string MapKey::argument_value(const BPFtrace &bpftrace,
return
bpftrace
.
get_stack
(
*
(
uint32_t
*
)
data
,
false
);
case
Type
:
:
ustack
:
return
bpftrace
.
get_stack
(
*
(
uint32_t
*
)
data
,
true
);
case
Type
:
:
string
:
return
std
::
string
((
char
*
)
data
);
}
abort
();
}
...
...
src/parser.yy
View file @
a28012be
...
...
@@ -69,6 +69,7 @@ void yyerror(bpftrace::Driver &driver, const char *s);
%token <std::string> IDENT "identifier"
%token <std::string> PATH "path"
%token <std::string> STRING "string"
%token <std::string> MAP "map"
%token <int> INT "integer"
...
...
@@ -127,6 +128,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); }
;
expr : INT { $$ = new ast::Integer($1); }
| STRING { $$ = new ast::String($1.substr(1, $1.size()-2)); }
| IDENT { $$ = new ast::Builtin($1); }
| map { $$ = $1; }
| "(" expr ")" { $$ = $2; }
...
...
src/printer.cpp
View file @
a28012be
...
...
@@ -10,6 +10,12 @@ void Printer::visit(Integer &integer)
out_
<<
indent
<<
"int: "
<<
integer
.
n
<<
std
::
endl
;
}
void
Printer
::
visit
(
String
&
string
)
{
std
::
string
indent
(
depth_
,
' '
);
out_
<<
indent
<<
"string: "
<<
string
.
str
<<
std
::
endl
;
}
void
Printer
::
visit
(
Builtin
&
builtin
)
{
std
::
string
indent
(
depth_
,
' '
);
...
...
src/printer.h
View file @
a28012be
...
...
@@ -11,6 +11,7 @@ public:
explicit
Printer
(
std
::
ostream
&
out
)
:
out_
(
out
)
{
}
void
visit
(
Integer
&
integer
)
override
;
void
visit
(
String
&
string
)
override
;
void
visit
(
Builtin
&
builtin
)
override
;
void
visit
(
Call
&
call
)
override
;
void
visit
(
Map
&
map
)
override
;
...
...
src/semantic_analyser.cpp
View file @
a28012be
...
...
@@ -13,6 +13,14 @@ void SemanticAnalyser::visit(Integer &integer)
integer
.
type
=
SizedType
(
Type
::
integer
,
8
);
}
void
SemanticAnalyser
::
visit
(
String
&
string
)
{
if
(
string
.
str
.
size
()
>
STRING_SIZE
-
1
)
{
err_
<<
"String is too long (over "
<<
STRING_SIZE
<<
" bytes): "
<<
string
.
str
<<
std
::
endl
;
}
string
.
type
=
SizedType
(
Type
::
string
,
STRING_SIZE
);
}
void
SemanticAnalyser
::
visit
(
Builtin
&
builtin
)
{
if
(
builtin
.
ident
==
"nsecs"
||
...
...
src/semantic_analyser.h
View file @
a28012be
...
...
@@ -18,6 +18,7 @@ public:
out_
(
out
)
{
}
void
visit
(
Integer
&
integer
)
override
;
void
visit
(
String
&
string
)
override
;
void
visit
(
Builtin
&
builtin
)
override
;
void
visit
(
Call
&
call
)
override
;
void
visit
(
Map
&
map
)
override
;
...
...
src/types.cpp
View file @
a28012be
...
...
@@ -31,6 +31,7 @@ std::string typestr(Type t)
case
Type
:
:
count
:
return
"count"
;
break
;
case
Type
:
:
stack
:
return
"stack"
;
break
;
case
Type
:
:
ustack
:
return
"ustack"
;
break
;
case
Type
:
:
string
:
return
"string"
;
break
;
default:
abort
();
}
}
...
...
src/types.h
View file @
a28012be
...
...
@@ -9,6 +9,7 @@
namespace
bpftrace
{
const
int
MAX_STACK_SIZE
=
32
;
const
int
STRING_SIZE
=
32
;
enum
class
Type
{
...
...
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