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
dd6bb122
Commit
dd6bb122
authored
Sep 10, 2018
by
Brendan Gregg
Committed by
GitHub
Sep 10, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #78 from iovisor/uaddr
Uaddr
parents
d2d3179a
278f840b
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
3 deletions
+81
-3
src/ast/codegen_llvm.cpp
src/ast/codegen_llvm.cpp
+8
-1
src/ast/semantic_analyser.cpp
src/ast/semantic_analyser.cpp
+30
-0
src/ast/semantic_analyser.h
src/ast/semantic_analyser.h
+1
-0
src/bpftrace.cpp
src/bpftrace.cpp
+34
-2
src/bpftrace.h
src/bpftrace.h
+3
-0
tests/codegen.cpp
tests/codegen.cpp
+5
-0
No files found.
src/ast/codegen_llvm.cpp
View file @
dd6bb122
...
...
@@ -310,6 +310,13 @@ void CodegenLLVM::visit(Call &call)
addr
=
bpftrace_
.
resolve_kname
(
name
.
c_str
());
expr_
=
b_
.
getInt64
(
addr
);
}
else
if
(
call
.
func
==
"uaddr"
)
{
uint64_t
addr
;
auto
&
name
=
static_cast
<
String
&>
(
*
call
.
vargs
->
at
(
0
)).
str
;
addr
=
bpftrace_
.
resolve_uname
(
name
.
c_str
());
expr_
=
b_
.
getInt64
(
addr
);
}
else
if
(
call
.
func
==
"join"
)
{
call
.
vargs
->
front
()
->
accept
(
*
this
);
...
...
src/ast/semantic_analyser.cpp
View file @
dd6bb122
...
...
@@ -5,6 +5,7 @@
#include "printf.h"
#include "arch/arch.h"
#include <sys/stat.h>
#include <regex>
#include "libbpf.h"
...
...
@@ -216,6 +217,17 @@ void SemanticAnalyser::visit(Call &call)
}
}
call
.
type
=
SizedType
(
Type
::
integer
,
8
);
}
else
if
(
call
.
func
==
"uaddr"
)
{
if
(
check_nargs
(
call
,
1
))
{
if
(
check_arg
(
call
,
Type
::
string
,
0
,
true
))
{
if
(
check_alpha_numeric
(
call
,
0
))
{
;
}
}
}
call
.
type
=
SizedType
(
Type
::
integer
,
8
);
}
else
if
(
call
.
func
==
"printf"
)
{
check_assignment
(
call
,
false
,
false
);
...
...
@@ -909,5 +921,23 @@ bool SemanticAnalyser::check_arg(const Call &call, Type type, int arg_num, bool
return
true
;
}
bool
SemanticAnalyser
::
check_alpha_numeric
(
const
Call
&
call
,
int
arg_num
)
{
if
(
!
call
.
vargs
)
return
false
;
auto
&
arg
=
static_cast
<
String
&>
(
*
call
.
vargs
->
at
(
0
)).
str
;
bool
is_alpha
=
std
::
regex_match
(
arg
,
std
::
regex
(
"^[a-zA-Z0-9_-]+$"
));
if
(
!
is_alpha
)
{
err_
<<
call
.
func
<<
"() expects an alpha numeric string as input"
;
err_
<<
" (
\"
"
<<
arg
<<
"
\"
provided)"
<<
std
::
endl
;
return
false
;
}
return
true
;
}
}
// namespace ast
}
// namespace bpftrace
src/ast/semantic_analyser.h
View file @
dd6bb122
...
...
@@ -55,6 +55,7 @@ private:
bool
check_nargs
(
const
Call
&
call
,
int
expected_nargs
);
bool
check_varargs
(
const
Call
&
call
,
int
min_nargs
,
int
max_nargs
);
bool
check_arg
(
const
Call
&
call
,
Type
type
,
int
arg_num
,
bool
want_literal
=
false
);
bool
check_alpha_numeric
(
const
Call
&
call
,
int
arg_num
);
Probe
*
probe_
;
std
::
map
<
std
::
string
,
SizedType
>
variable_val_
;
...
...
src/bpftrace.cpp
View file @
dd6bb122
...
...
@@ -1145,8 +1145,7 @@ uint64_t BPFtrace::resolve_kname(const char *name)
if
(
found
)
{
std
::
string
first_word
=
line
.
substr
(
0
,
line
.
find
(
" "
));
addr
=
std
::
stoull
(
first_word
,
0
,
16
);
addr
=
read_address_from_output
(
line
);
}
}
...
...
@@ -1155,6 +1154,39 @@ uint64_t BPFtrace::resolve_kname(const char *name)
return
addr
;
}
uint64_t
BPFtrace
::
resolve_uname
(
const
char
*
name
)
{
uint64_t
addr
=
0
;
// TODO: switch from objdump to library call
std
::
string
call_str
=
"objdump -tT /bin/bash | grep "
;
call_str
+=
name
;
const
char
*
call
=
call_str
.
c_str
();
auto
result
=
exec_system
(
call
);
addr
=
read_address_from_output
(
result
);
return
addr
;
}
std
::
string
BPFtrace
::
exec_system
(
const
char
*
cmd
)
{
std
::
array
<
char
,
128
>
buffer
;
std
::
string
result
;
std
::
shared_ptr
<
FILE
>
pipe
(
popen
(
cmd
,
"r"
),
pclose
);
if
(
!
pipe
)
throw
std
::
runtime_error
(
"popen() failed!"
);
while
(
!
feof
(
pipe
.
get
()))
{
if
(
fgets
(
buffer
.
data
(),
128
,
pipe
.
get
())
!=
nullptr
)
result
+=
buffer
.
data
();
}
return
result
;
}
uint64_t
BPFtrace
::
read_address_from_output
(
std
::
string
output
)
{
std
::
string
first_word
=
output
.
substr
(
0
,
output
.
find
(
" "
));
return
std
::
stoull
(
first_word
,
0
,
16
);
}
std
::
string
BPFtrace
::
resolve_usym
(
uintptr_t
addr
,
int
pid
,
bool
show_offset
)
{
struct
bcc_symbol
sym
;
...
...
src/bpftrace.h
View file @
dd6bb122
...
...
@@ -38,6 +38,7 @@ public:
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
);
uint64_t
resolve_uname
(
const
char
*
name
);
std
::
string
resolve_name
(
uint64_t
name_id
);
int
pid_
;
...
...
@@ -82,6 +83,8 @@ private:
static
uint64_t
reduce_value
(
const
std
::
vector
<
uint8_t
>
&
value
,
int
ncpus
);
static
uint64_t
min_value
(
const
std
::
vector
<
uint8_t
>
&
value
,
int
ncpus
);
static
uint64_t
max_value
(
const
std
::
vector
<
uint8_t
>
&
value
,
int
ncpus
);
static
uint64_t
read_address_from_output
(
std
::
string
output
);
static
std
::
string
exec_system
(
const
char
*
cmd
);
static
std
::
string
hist_index_label
(
int
power
);
static
std
::
string
lhist_index_label
(
int
number
);
std
::
vector
<
uint8_t
>
find_empty_key
(
IMap
&
map
,
size_t
size
)
const
;
...
...
tests/codegen.cpp
View file @
dd6bb122
...
...
@@ -1029,6 +1029,11 @@ TEST(codegen, call_kaddr)
// TODO: test kaddr()
}
TEST
(
codegen
,
call_uaddr
)
{
// TODO: test uaddr()
}
TEST
(
codegen
,
call_hist
)
{
test
(
"kprobe:f { @x = hist(pid) }"
,
...
...
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