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
e1abac20
Commit
e1abac20
authored
Mar 08, 2017
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Detach probes
parent
b6af7c84
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
20 deletions
+79
-20
src/bpftrace.cpp
src/bpftrace.cpp
+54
-8
src/bpftrace.h
src/bpftrace.h
+6
-0
src/main.cpp
src/main.cpp
+19
-12
No files found.
src/bpftrace.cpp
View file @
e1abac20
#include <iostream>
#include "bpftrace.h"
#include "libbpf.h"
namespace
ebpf
{
namespace
bpftrace
{
...
...
@@ -30,8 +29,8 @@ std::string typestr(ProbeType t)
int
BPFtrace
::
attach_probes
()
{
int
err
=
0
;
for
(
Probe
&
probe
:
probes_
)
{
int
err
=
0
;
if
(
probe
.
type
==
ProbeType
::
kprobe
||
probe
.
type
==
ProbeType
::
kretprobe
)
{
err
=
attach_kprobe
(
probe
);
...
...
@@ -44,6 +43,31 @@ int BPFtrace::attach_probes()
std
::
cerr
<<
"Error attaching probe: "
<<
typestr
(
probe
.
type
)
<<
":"
<<
probe
.
attach_point
<<
std
::
endl
;
return
err
;
}
probe
.
attached
=
true
;
}
return
0
;
}
int
BPFtrace
::
detach_probes
()
{
for
(
Probe
&
probe
:
probes_
)
{
if
(
!
probe
.
attached
)
continue
;
int
err
=
0
;
if
(
probe
.
type
==
ProbeType
::
kprobe
||
probe
.
type
==
ProbeType
::
kretprobe
)
{
err
=
bpf_detach_kprobe
(
eventname
(
probe
).
c_str
());
}
else
{
abort
();
}
if
(
err
)
{
std
::
cerr
<<
"Error detaching probe: "
<<
typestr
(
probe
.
type
)
<<
":"
<<
probe
.
attach_point
<<
std
::
endl
;
return
err
;
}
}
return
0
;
}
...
...
@@ -67,29 +91,51 @@ int BPFtrace::add_probe(ast::Probe &p)
int
BPFtrace
::
attach_kprobe
(
Probe
&
probe
)
{
std
::
string
event
;
bpf_probe_attach_type
attach_type
;
int
pid
=
-
1
;
int
cpu
=
0
;
int
group_fd
=
-
1
;
perf_reader_cb
cb
=
nullptr
;
void
*
cb_cookie
=
nullptr
;
void
*
result
=
bpf_attach_kprobe
(
probe
.
progfd
,
attachtype
(
probe
),
eventname
(
probe
).
c_str
(),
probe
.
attach_point
.
c_str
(),
pid
,
cpu
,
group_fd
,
cb
,
cb_cookie
);
if
(
result
==
nullptr
)
return
1
;
return
0
;
}
std
::
string
BPFtrace
::
eventname
(
Probe
&
probe
)
{
std
::
string
event
;
switch
(
probe
.
type
)
{
case
ProbeType
:
:
kprobe
:
event
=
"p_"
+
probe
.
attach_point
;
attach_type
=
BPF_PROBE_ENTRY
;
break
;
case
ProbeType
:
:
kretprobe
:
event
=
"r_"
+
probe
.
attach_point
;
attach_type
=
BPF_PROBE_RETURN
;
break
;
default:
abort
();
}
return
event
;
}
bpf_attach_kprobe
(
probe
.
progfd
,
attach_type
,
event
.
c_str
(),
probe
.
attach_point
.
c_str
(),
pid
,
cpu
,
group_fd
,
cb
,
cb_cookie
);
bpf_probe_attach_type
BPFtrace
::
attachtype
(
Probe
&
probe
)
{
bpf_probe_attach_type
attach_type
;
switch
(
probe
.
type
)
{
case
ProbeType
:
:
kprobe
:
attach_type
=
BPF_PROBE_ENTRY
;
break
;
case
ProbeType
:
:
kretprobe
:
attach_type
=
BPF_PROBE_RETURN
;
break
;
default:
abort
();
}
return
attach_type
;
}
}
// namespace bpftrace
...
...
src/bpftrace.h
View file @
e1abac20
...
...
@@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include "libbpf.h"
#include "ast.h"
#include "map.h"
...
...
@@ -33,12 +34,14 @@ public:
ProbeType
type
;
std
::
string
attach_point
;
int
progfd
;
bool
attached
=
false
;
};
class
BPFtrace
{
public:
int
attach_probes
();
int
detach_probes
();
int
add_probe
(
ast
::
Probe
&
p
);
std
::
map
<
std
::
string
,
Type
>
map_val_
;
...
...
@@ -49,6 +52,9 @@ private:
std
::
vector
<
Probe
>
probes_
;
int
attach_kprobe
(
Probe
&
probe
);
static
std
::
string
eventname
(
Probe
&
probe
);
static
bpf_probe_attach_type
attachtype
(
Probe
&
probe
);
};
}
// namespace bpftrace
...
...
src/main.cpp
View file @
e1abac20
...
...
@@ -17,7 +17,7 @@ void usage()
int
main
(
int
argc
,
char
*
argv
[])
{
int
result
;
int
err
;
Driver
driver
;
std
::
string
script
;
...
...
@@ -44,7 +44,7 @@ int main(int argc, char *argv[])
return
1
;
}
char
*
file_name
=
argv
[
optind
];
result
=
driver
.
parse_file
(
file_name
);
err
=
driver
.
parse_file
(
file_name
);
}
else
{
...
...
@@ -54,11 +54,11 @@ int main(int argc, char *argv[])
usage
();
return
1
;
}
result
=
driver
.
parse_str
(
script
);
err
=
driver
.
parse_str
(
script
);
}
if
(
result
)
return
result
;
if
(
err
)
return
err
;
BPFtrace
bpftrace
;
...
...
@@ -66,16 +66,23 @@ int main(int argc, char *argv[])
driver
.
root_
->
accept
(
p
);
ast
::
SemanticAnalyser
semantics
(
driver
.
root_
,
bpftrace
);
result
=
semantics
.
analyse
();
if
(
result
)
return
result
;
err
=
semantics
.
analyse
();
if
(
err
)
return
err
;
ast
::
CodegenLLVM
llvm
(
driver
.
root_
,
bpftrace
);
result
=
llvm
.
compile
();
if
(
result
)
return
result
;
err
=
llvm
.
compile
();
if
(
err
)
return
err
;
bpftrace
.
attach_probes
();
err
=
bpftrace
.
attach_probes
();
if
(
err
)
goto
cleanup
;
// TODO wait here while script is running
cleanup:
bpftrace
.
detach_probes
();
return
0
;
}
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