Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
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
bcc
Commits
1a6ee5ac
Commit
1a6ee5ac
authored
Jan 06, 2016
by
4ast
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #308 from iovisor/cflags
Add ability to set custom cflags when loading programs
parents
43861954
93588503
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
40 additions
and
23 deletions
+40
-23
src/cc/bpf_common.cc
src/cc/bpf_common.cc
+4
-4
src/cc/bpf_common.h
src/cc/bpf_common.h
+2
-2
src/cc/bpf_module.cc
src/cc/bpf_module.cc
+7
-7
src/cc/bpf_module.h
src/cc/bpf_module.h
+3
-3
src/cc/frontends/clang/loader.cc
src/cc/frontends/clang/loader.cc
+5
-1
src/cc/frontends/clang/loader.h
src/cc/frontends/clang/loader.h
+1
-1
src/python/bcc/__init__.py
src/python/bcc/__init__.py
+10
-5
tests/cc/test_clang.py
tests/cc/test_clang.py
+8
-0
No files found.
src/cc/bpf_common.cc
View file @
1a6ee5ac
...
...
@@ -26,18 +26,18 @@ void * bpf_module_create_b(const char *filename, const char *proto_filename, uns
return
mod
;
}
void
*
bpf_module_create_c
(
const
char
*
filename
,
unsigned
flags
)
{
void
*
bpf_module_create_c
(
const
char
*
filename
,
unsigned
flags
,
const
char
*
cflags
[],
int
ncflags
)
{
auto
mod
=
new
ebpf
::
BPFModule
(
flags
);
if
(
mod
->
load_c
(
filename
)
!=
0
)
{
if
(
mod
->
load_c
(
filename
,
cflags
,
ncflags
)
!=
0
)
{
delete
mod
;
return
nullptr
;
}
return
mod
;
}
void
*
bpf_module_create_c_from_string
(
const
char
*
text
,
unsigned
flags
)
{
void
*
bpf_module_create_c_from_string
(
const
char
*
text
,
unsigned
flags
,
const
char
*
cflags
[],
int
ncflags
)
{
auto
mod
=
new
ebpf
::
BPFModule
(
flags
);
if
(
mod
->
load_string
(
text
)
!=
0
)
{
if
(
mod
->
load_string
(
text
,
cflags
,
ncflags
)
!=
0
)
{
delete
mod
;
return
nullptr
;
}
...
...
src/cc/bpf_common.h
View file @
1a6ee5ac
...
...
@@ -25,8 +25,8 @@ extern "C" {
#endif
void
*
bpf_module_create_b
(
const
char
*
filename
,
const
char
*
proto_filename
,
unsigned
flags
);
void
*
bpf_module_create_c
(
const
char
*
filename
,
unsigned
flags
);
void
*
bpf_module_create_c_from_string
(
const
char
*
text
,
unsigned
flags
);
void
*
bpf_module_create_c
(
const
char
*
filename
,
unsigned
flags
,
const
char
*
cflags
[],
int
ncflags
);
void
*
bpf_module_create_c_from_string
(
const
char
*
text
,
unsigned
flags
,
const
char
*
cflags
[],
int
ncflags
);
void
bpf_module_destroy
(
void
*
program
);
char
*
bpf_module_license
(
void
*
program
);
unsigned
bpf_module_kern_version
(
void
*
program
);
...
...
src/cc/bpf_module.cc
View file @
1a6ee5ac
...
...
@@ -299,9 +299,9 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
}
// load an entire c file as a module
int
BPFModule
::
load_cfile
(
const
string
&
file
,
bool
in_memory
)
{
int
BPFModule
::
load_cfile
(
const
string
&
file
,
bool
in_memory
,
const
char
*
cflags
[],
int
ncflags
)
{
clang_loader_
=
make_unique
<
ClangLoader
>
(
&*
ctx_
,
flags_
);
if
(
clang_loader_
->
parse
(
&
mod_
,
&
tables_
,
file
,
in_memory
))
if
(
clang_loader_
->
parse
(
&
mod_
,
&
tables_
,
file
,
in_memory
,
cflags
,
ncflags
))
return
-
1
;
return
0
;
}
...
...
@@ -313,7 +313,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory) {
// build an ExecutionEngine.
int
BPFModule
::
load_includes
(
const
string
&
tmpfile
)
{
clang_loader_
=
make_unique
<
ClangLoader
>
(
&*
ctx_
,
flags_
);
if
(
clang_loader_
->
parse
(
&
mod_
,
&
tables_
,
tmpfile
,
false
))
if
(
clang_loader_
->
parse
(
&
mod_
,
&
tables_
,
tmpfile
,
false
,
nullptr
,
0
))
return
-
1
;
return
0
;
}
...
...
@@ -668,7 +668,7 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) {
}
// load a C file
int
BPFModule
::
load_c
(
const
string
&
filename
)
{
int
BPFModule
::
load_c
(
const
string
&
filename
,
const
char
*
cflags
[],
int
ncflags
)
{
if
(
!
sections_
.
empty
())
{
fprintf
(
stderr
,
"Program already initialized
\n
"
);
return
-
1
;
...
...
@@ -677,7 +677,7 @@ int BPFModule::load_c(const string &filename) {
fprintf
(
stderr
,
"Invalid filename
\n
"
);
return
-
1
;
}
if
(
int
rc
=
load_cfile
(
filename
,
false
))
if
(
int
rc
=
load_cfile
(
filename
,
false
,
cflags
,
ncflags
))
return
rc
;
if
(
int
rc
=
annotate
())
return
rc
;
...
...
@@ -687,12 +687,12 @@ int BPFModule::load_c(const string &filename) {
}
// load a C text string
int
BPFModule
::
load_string
(
const
string
&
text
)
{
int
BPFModule
::
load_string
(
const
string
&
text
,
const
char
*
cflags
[],
int
ncflags
)
{
if
(
!
sections_
.
empty
())
{
fprintf
(
stderr
,
"Program already initialized
\n
"
);
return
-
1
;
}
if
(
int
rc
=
load_cfile
(
text
,
true
))
if
(
int
rc
=
load_cfile
(
text
,
true
,
cflags
,
ncflags
))
return
rc
;
if
(
int
rc
=
annotate
())
return
rc
;
...
...
src/cc/bpf_module.h
View file @
1a6ee5ac
...
...
@@ -48,15 +48,15 @@ class BPFModule {
void
dump_ir
(
llvm
::
Module
&
mod
);
int
load_file_module
(
std
::
unique_ptr
<
llvm
::
Module
>
*
mod
,
const
std
::
string
&
file
,
bool
in_memory
);
int
load_includes
(
const
std
::
string
&
tmpfile
);
int
load_cfile
(
const
std
::
string
&
file
,
bool
in_memory
);
int
load_cfile
(
const
std
::
string
&
file
,
bool
in_memory
,
const
char
*
cflags
[],
int
ncflags
);
int
kbuild_flags
(
const
char
*
uname_release
,
std
::
vector
<
std
::
string
>
*
cflags
);
int
run_pass_manager
(
llvm
::
Module
&
mod
);
public:
BPFModule
(
unsigned
flags
);
~
BPFModule
();
int
load_b
(
const
std
::
string
&
filename
,
const
std
::
string
&
proto_filename
);
int
load_c
(
const
std
::
string
&
filename
);
int
load_string
(
const
std
::
string
&
text
);
int
load_c
(
const
std
::
string
&
filename
,
const
char
*
cflags
[],
int
ncflags
);
int
load_string
(
const
std
::
string
&
text
,
const
char
*
cflags
[],
int
ncflags
);
size_t
num_functions
()
const
;
uint8_t
*
function_start
(
size_t
id
)
const
;
uint8_t
*
function_start
(
const
std
::
string
&
name
)
const
;
...
...
src/cc/frontends/clang/loader.cc
View file @
1a6ee5ac
...
...
@@ -65,7 +65,7 @@ ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags)
ClangLoader
::~
ClangLoader
()
{}
int
ClangLoader
::
parse
(
unique_ptr
<
llvm
::
Module
>
*
mod
,
unique_ptr
<
vector
<
TableDesc
>>
*
tables
,
const
string
&
file
,
bool
in_memory
)
{
const
string
&
file
,
bool
in_memory
,
const
char
*
cflags
[],
int
ncflags
)
{
using
namespace
clang
;
struct
utsname
un
;
...
...
@@ -103,6 +103,10 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, unique_ptr<vector<TableDes
kflags
.
push_back
(
BCC_INSTALL_PREFIX
"/share/bcc/include"
);
for
(
auto
it
=
kflags
.
begin
();
it
!=
kflags
.
end
();
++
it
)
flags_cstr
.
push_back
(
it
->
c_str
());
if
(
cflags
)
{
for
(
auto
i
=
0
;
i
<
ncflags
;
++
i
)
flags_cstr
.
push_back
(
cflags
[
i
]);
}
// set up the error reporting class
IntrusiveRefCntPtr
<
DiagnosticOptions
>
diag_opts
(
new
DiagnosticOptions
());
...
...
src/cc/frontends/clang/loader.h
View file @
1a6ee5ac
...
...
@@ -39,7 +39,7 @@ class ClangLoader {
explicit
ClangLoader
(
llvm
::
LLVMContext
*
ctx
,
unsigned
flags
);
~
ClangLoader
();
int
parse
(
std
::
unique_ptr
<
llvm
::
Module
>
*
mod
,
std
::
unique_ptr
<
std
::
vector
<
TableDesc
>>
*
tables
,
const
std
::
string
&
file
,
bool
in_memory
);
const
std
::
string
&
file
,
bool
in_memory
,
const
char
*
cflags
[],
int
ncflags
);
private:
llvm
::
LLVMContext
*
ctx_
;
unsigned
flags_
;
...
...
src/python/bcc/__init__.py
View file @
1a6ee5ac
...
...
@@ -30,9 +30,11 @@ lib = ct.CDLL("libbcc.so")
lib
.
bpf_module_create_b
.
restype
=
ct
.
c_void_p
lib
.
bpf_module_create_b
.
argtypes
=
[
ct
.
c_char_p
,
ct
.
c_char_p
,
ct
.
c_uint
]
lib
.
bpf_module_create_c
.
restype
=
ct
.
c_void_p
lib
.
bpf_module_create_c
.
argtypes
=
[
ct
.
c_char_p
,
ct
.
c_uint
]
lib
.
bpf_module_create_c
.
argtypes
=
[
ct
.
c_char_p
,
ct
.
c_uint
,
ct
.
POINTER
(
ct
.
c_char_p
),
ct
.
c_int
]
lib
.
bpf_module_create_c_from_string
.
restype
=
ct
.
c_void_p
lib
.
bpf_module_create_c_from_string
.
argtypes
=
[
ct
.
c_char_p
,
ct
.
c_uint
]
lib
.
bpf_module_create_c_from_string
.
argtypes
=
[
ct
.
c_char_p
,
ct
.
c_uint
,
ct
.
POINTER
(
ct
.
c_char_p
),
ct
.
c_int
]
lib
.
bpf_module_destroy
.
restype
=
None
lib
.
bpf_module_destroy
.
argtypes
=
[
ct
.
c_void_p
]
lib
.
bpf_module_license
.
restype
=
ct
.
c_char_p
...
...
@@ -395,7 +397,7 @@ class BPF(object):
raise
Exception
(
"Could not find file %s"
%
filename
)
return
filename
def
__init__
(
self
,
src_file
=
""
,
hdr_file
=
""
,
text
=
None
,
cb
=
None
,
debug
=
0
):
def
__init__
(
self
,
src_file
=
""
,
hdr_file
=
""
,
text
=
None
,
cb
=
None
,
debug
=
0
,
cflags
=
[]
):
"""Create a a new BPF module with the given source code.
Note:
...
...
@@ -416,8 +418,11 @@ class BPF(object):
self
.
debug
=
debug
self
.
funcs
=
{}
self
.
tables
=
{}
cflags_array
=
(
ct
.
c_char_p
*
len
(
cflags
))()
for
i
,
s
in
enumerate
(
cflags
):
cflags_array
[
i
]
=
s
.
encode
(
"ascii"
)
if
text
:
self
.
module
=
lib
.
bpf_module_create_c_from_string
(
text
.
encode
(
"ascii"
),
self
.
debug
)
self
.
module
=
lib
.
bpf_module_create_c_from_string
(
text
.
encode
(
"ascii"
),
self
.
debug
,
cflags_array
,
len
(
cflags_array
))
else
:
src_file
=
BPF
.
_find_file
(
src_file
)
hdr_file
=
BPF
.
_find_file
(
hdr_file
)
...
...
@@ -426,7 +431,7 @@ class BPF(object):
hdr_file
.
encode
(
"ascii"
),
self
.
debug
)
else
:
self
.
module
=
lib
.
bpf_module_create_c
(
src_file
.
encode
(
"ascii"
),
self
.
debug
)
self
.
debug
,
cflags_array
,
len
(
cflags_array
)
)
if
self
.
module
==
None
:
raise
Exception
(
"Failed to compile BPF module %s"
%
src_file
)
...
...
tests/cc/test_clang.py
View file @
1a6ee5ac
...
...
@@ -287,5 +287,13 @@ BPF_TABLE("array", int, union emptyu, t3, 1);
import
ctypes
self
.
assertEqual
(
ctypes
.
sizeof
(
b
[
"t3"
].
Leaf
),
8
)
def
test_cflags
(
self
):
text
=
"""
#ifndef MYFLAG
#error "MYFLAG not set as expected"
#endif
"""
b
=
BPF
(
text
=
text
,
cflags
=
[
"-DMYFLAG"
])
if
__name__
==
"__main__"
:
main
()
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