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
32e2efa4
Commit
32e2efa4
authored
Apr 21, 2018
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add stack pointer builtin (sp)
parent
36d75e06
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
97 additions
and
21 deletions
+97
-21
README.md
README.md
+1
-0
src/arch/arch.h
src/arch/arch.h
+2
-0
src/arch/x86_64.cpp
src/arch/x86_64.cpp
+5
-0
src/ast/codegen_llvm.cpp
src/ast/codegen_llvm.cpp
+16
-21
src/ast/semantic_analyser.cpp
src/ast/semantic_analyser.cpp
+1
-0
tests/codegen.cpp
tests/codegen.cpp
+42
-0
tests/semantic_analyser.cpp
tests/semantic_analyser.cpp
+30
-0
No files found.
README.md
View file @
32e2efa4
...
...
@@ -192,6 +192,7 @@ Variables:
-
`arg0`
,
`arg1`
, ... etc. - Arguments to the function being traced
-
`retval`
- Return value from function being traced
-
`func`
- Name of the function currently being traced
-
`sp`
- Stack pointer
Functions:
-
`quantize(int n)`
- Produce a log2 histogram of values of
`n`
...
...
src/arch/arch.h
View file @
32e2efa4
...
...
@@ -5,10 +5,12 @@
namespace
bpftrace
{
namespace
arch
{
// Offsets are based off the pt_regs struct from the Linux kernel
int
max_arg
();
int
arg_offset
(
int
arg_num
);
int
ret_offset
();
int
pc_offset
();
int
sp_offset
();
std
::
string
name
();
}
// namespace arch
...
...
src/arch/x86_64.cpp
View file @
32e2efa4
...
...
@@ -34,6 +34,11 @@ int pc_offset()
return
16
;
// ip
}
int
sp_offset
()
{
return
19
;
// sp
}
std
::
string
name
()
{
return
std
::
string
(
"x86_64"
);
...
...
src/ast/codegen_llvm.cpp
View file @
32e2efa4
...
...
@@ -75,30 +75,25 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_
=
buf
;
}
else
if
(
!
builtin
.
ident
.
compare
(
0
,
3
,
"arg"
)
&&
builtin
.
ident
.
size
()
==
4
&&
builtin
.
ident
.
at
(
3
)
>=
'0'
&&
builtin
.
ident
.
at
(
3
)
<=
'9'
)
builtin
.
ident
.
at
(
3
)
>=
'0'
&&
builtin
.
ident
.
at
(
3
)
<=
'9'
||
builtin
.
ident
==
"retval"
||
builtin
.
ident
==
"func"
||
builtin
.
ident
==
"sp"
)
{
int
arg_num
=
atoi
(
builtin
.
ident
.
substr
(
3
).
c_str
());
int
offset
;
if
(
builtin
.
ident
==
"retval"
)
offset
=
arch
::
ret_offset
()
*
sizeof
(
uintptr_t
);
else
if
(
builtin
.
ident
==
"func"
)
offset
=
arch
::
pc_offset
()
*
sizeof
(
uintptr_t
);
else
if
(
builtin
.
ident
==
"sp"
)
offset
=
arch
::
sp_offset
()
*
sizeof
(
uintptr_t
);
else
// argX
{
int
arg_num
=
atoi
(
builtin
.
ident
.
substr
(
3
).
c_str
());
offset
=
arch
::
arg_offset
(
arg_num
)
*
sizeof
(
uintptr_t
);
}
AllocaInst
*
dst
=
b_
.
CreateAllocaBPF
(
builtin
.
type
,
builtin
.
ident
);
int
offset
=
arch
::
arg_offset
(
arg_num
)
*
sizeof
(
uintptr_t
);
Value
*
src
=
b_
.
CreateGEP
(
ctx_
,
b_
.
getInt64
(
offset
));
b_
.
CreateProbeRead
(
dst
,
8
,
src
);
expr_
=
b_
.
CreateLoad
(
dst
);
b_
.
CreateLifetimeEnd
(
dst
);
}
else
if
(
builtin
.
ident
==
"retval"
)
{
AllocaInst
*
dst
=
b_
.
CreateAllocaBPF
(
builtin
.
type
,
builtin
.
ident
);
int
offset
=
arch
::
ret_offset
()
*
sizeof
(
uintptr_t
);
Value
*
src
=
b_
.
CreateGEP
(
ctx_
,
b_
.
getInt64
(
offset
));
b_
.
CreateProbeRead
(
dst
,
8
,
src
);
expr_
=
b_
.
CreateLoad
(
dst
);
b_
.
CreateLifetimeEnd
(
dst
);
}
else
if
(
builtin
.
ident
==
"func"
)
{
AllocaInst
*
dst
=
b_
.
CreateAllocaBPF
(
builtin
.
type
,
builtin
.
ident
);
int
offset
=
arch
::
pc_offset
()
*
sizeof
(
uintptr_t
);
Value
*
src
=
b_
.
CreateGEP
(
ctx_
,
b_
.
getInt64
(
offset
));
b_
.
CreateProbeRead
(
dst
,
8
,
src
);
expr_
=
b_
.
CreateLoad
(
dst
);
...
...
src/ast/semantic_analyser.cpp
View file @
32e2efa4
...
...
@@ -33,6 +33,7 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin
.
ident
==
"uid"
||
builtin
.
ident
==
"gid"
||
builtin
.
ident
==
"cpu"
||
builtin
.
ident
==
"sp"
||
builtin
.
ident
==
"retval"
)
{
builtin
.
type
=
SizedType
(
Type
::
integer
,
8
);
}
...
...
tests/codegen.cpp
View file @
32e2efa4
...
...
@@ -648,6 +648,48 @@ attributes #1 = { argmemonly nounwind }
)EXPECTED"
);
}
TEST
(
codegen
,
builtin_sp
)
{
test
(
"kprobe:f { @x = sp }"
,
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
%sp = alloca i64, align 8
%1 = bitcast i64* %sp to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
%2 = getelementptr i8, i8* %0, i64 152
%probe_read = call i64 inttoptr (i64 4 to i64 (i8*, i64, i8*)*)(i64* nonnull %sp, i64 8, i8* %2)
%3 = load i64, i64* %sp, align 8
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %1)
%4 = bitcast i64* %"@x_key" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %4)
store i64 0, i64* %"@x_key", align 8
%5 = bitcast i64* %"@x_val" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %5)
store i64 %3, i64* %"@x_val", align 8
%pseudo = 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 %4)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %5)
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
,
call_quantize
)
{
test
(
"kprobe:f { @x = quantize(pid) }"
,
...
...
tests/semantic_analyser.cpp
View file @
32e2efa4
...
...
@@ -45,6 +45,36 @@ void test(const std::string &input, int expected_result=0)
test
(
bpftrace
,
driver
,
input
,
expected_result
);
}
TEST
(
semantic_analyser
,
builtin_variables
)
{
test
(
"kprobe:f { pid }"
,
0
);
test
(
"kprobe:f { tid }"
,
0
);
test
(
"kprobe:f { uid }"
,
0
);
test
(
"kprobe:f { gid }"
,
0
);
test
(
"kprobe:f { nsecs }"
,
0
);
test
(
"kprobe:f { cpu }"
,
0
);
test
(
"kprobe:f { comm }"
,
0
);
test
(
"kprobe:f { stack }"
,
0
);
test
(
"kprobe:f { ustack }"
,
0
);
test
(
"kprobe:f { arg0 }"
,
0
);
test
(
"kprobe:f { retval }"
,
0
);
test
(
"kprobe:f { func }"
,
0
);
test
(
"kprobe:f { sp }"
,
0
);
test
(
"kprobe:f { fake }"
,
1
);
}
TEST
(
semantic_analyser
,
builtin_functions
)
{
test
(
"kprobe:f { @x = quantize(123) }"
,
0
);
test
(
"kprobe:f { @x = count() }"
,
0
);
test
(
"kprobe:f { @x = delete() }"
,
0
);
test
(
"kprobe:f { str(0xffff) }"
,
0
);
test
(
"kprobe:f { printf(
\"
hello
\\
n
\"
) }"
,
0
);
test
(
"kprobe:f { sym(0xffff) }"
,
0
);
test
(
"kprobe:f { usym(0xffff) }"
,
0
);
test
(
"kprobe:f { fake() }"
,
1
);
}
TEST
(
semantic_analyser
,
probe_count
)
{
MockBPFtrace
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