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
918e4c82
Commit
918e4c82
authored
Feb 05, 2017
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basic attaching of kprobes
parent
e865a011
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
0 deletions
+114
-0
src/bpftrace.cpp
src/bpftrace.cpp
+86
-0
src/bpftrace.h
src/bpftrace.h
+24
-0
src/semantic_analyser.cpp
src/semantic_analyser.cpp
+4
-0
No files found.
src/bpftrace.cpp
View file @
918e4c82
#include <iostream>
#include "bpftrace.h"
#include "bpftrace.h"
#include "libbpf.h"
namespace
ebpf
{
namespace
ebpf
{
namespace
bpftrace
{
namespace
bpftrace
{
...
@@ -15,5 +18,88 @@ std::string typestr(Type t)
...
@@ -15,5 +18,88 @@ std::string typestr(Type t)
}
}
}
}
std
::
string
typestr
(
ProbeType
t
)
{
switch
(
t
)
{
case
ProbeType
:
:
kprobe
:
return
"kprobe"
;
break
;
case
ProbeType
:
:
kretprobe
:
return
"kretprobe"
;
break
;
default:
abort
();
}
}
int
BPFtrace
::
attach_probes
()
{
int
err
=
0
;
for
(
Probe
&
probe
:
probes_
)
{
if
(
probe
.
type
==
ProbeType
::
kprobe
||
probe
.
type
==
ProbeType
::
kretprobe
)
{
err
=
attach_kprobe
(
probe
);
}
else
{
abort
();
}
if
(
err
)
{
std
::
cerr
<<
"Error attaching probe: "
<<
typestr
(
probe
.
type
)
<<
":"
<<
probe
.
attach_point
<<
std
::
endl
;
return
err
;
}
}
return
0
;
}
int
BPFtrace
::
add_probe
(
ast
::
Probe
&
p
)
{
Probe
probe
;
probe
.
attach_point
=
p
.
attach_point
;
if
(
p
.
type
==
"kprobe"
)
{
probe
.
type
=
ProbeType
::
kprobe
;
}
else
if
(
p
.
type
==
"kretprobe"
)
{
probe
.
type
=
ProbeType
::
kretprobe
;
}
else
{
return
-
1
;
}
probes_
.
push_back
(
probe
);
return
0
;
}
int
BPFtrace
::
attach_kprobe
(
Probe
&
probe
)
{
std
::
string
event
;
std
::
string
event_desc
;
int
pid
=
-
1
;
int
cpu
=
0
;
int
group_fd
=
-
1
;
perf_reader_cb
cb
=
nullptr
;
void
*
cb_cookie
=
nullptr
;
switch
(
probe
.
type
)
{
case
ProbeType
:
:
kprobe
:
event
=
"p_"
+
probe
.
attach_point
;
break
;
case
ProbeType
:
:
kretprobe
:
event
=
"r_"
+
probe
.
attach_point
;
break
;
default:
abort
();
}
switch
(
probe
.
type
)
{
case
ProbeType
:
:
kprobe
:
event_desc
=
"p:kprobes/"
+
event
+
" "
+
probe
.
attach_point
;
break
;
case
ProbeType
:
:
kretprobe
:
event_desc
=
"r:kprobes/"
+
event
+
" "
+
probe
.
attach_point
;
break
;
default:
abort
();
}
bpf_attach_kprobe
(
probe
.
progfd
,
event
.
c_str
(),
event_desc
.
c_str
(),
pid
,
cpu
,
group_fd
,
cb
,
cb_cookie
);
}
}
// namespace bpftrace
}
// namespace bpftrace
}
// namespace ebpf
}
// namespace ebpf
src/bpftrace.h
View file @
918e4c82
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#include <memory>
#include <memory>
#include <vector>
#include <vector>
#include "ast.h"
#include "map.h"
#include "map.h"
namespace
ebpf
{
namespace
ebpf
{
...
@@ -17,14 +18,37 @@ enum class Type
...
@@ -17,14 +18,37 @@ enum class Type
count
,
count
,
};
};
enum
class
ProbeType
{
kprobe
,
kretprobe
,
};
std
::
string
typestr
(
Type
t
);
std
::
string
typestr
(
Type
t
);
std
::
string
typestr
(
ProbeType
t
);
class
Probe
{
public:
ProbeType
type
;
std
::
string
attach_point
;
int
progfd
;
};
class
BPFtrace
class
BPFtrace
{
{
public:
public:
int
attach_probes
();
int
add_probe
(
ast
::
Probe
&
p
);
std
::
map
<
std
::
string
,
Type
>
map_val_
;
std
::
map
<
std
::
string
,
Type
>
map_val_
;
std
::
map
<
std
::
string
,
std
::
vector
<
Type
>>
map_args_
;
std
::
map
<
std
::
string
,
std
::
vector
<
Type
>>
map_args_
;
std
::
map
<
std
::
string
,
std
::
unique_ptr
<
ebpf
::
bpftrace
::
Map
>>
maps_
;
std
::
map
<
std
::
string
,
std
::
unique_ptr
<
ebpf
::
bpftrace
::
Map
>>
maps_
;
private:
std
::
vector
<
Probe
>
probes_
;
int
attach_kprobe
(
Probe
&
probe
);
};
};
}
// namespace bpftrace
}
// namespace bpftrace
...
...
src/semantic_analyser.cpp
View file @
918e4c82
...
@@ -169,6 +169,10 @@ void SemanticAnalyser::visit(Probe &probe)
...
@@ -169,6 +169,10 @@ void SemanticAnalyser::visit(Probe &probe)
for
(
Statement
*
stmt
:
*
probe
.
stmts
)
{
for
(
Statement
*
stmt
:
*
probe
.
stmts
)
{
stmt
->
accept
(
*
this
);
stmt
->
accept
(
*
this
);
}
}
if
(
bpftrace_
.
add_probe
(
probe
))
{
err_
<<
"Invalid probe type: '"
<<
probe
.
type
<<
"'"
<<
std
::
endl
;
}
}
}
void
SemanticAnalyser
::
visit
(
Program
&
program
)
void
SemanticAnalyser
::
visit
(
Program
&
program
)
...
...
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