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
02bf6819
Commit
02bf6819
authored
Aug 11, 2015
by
4ast
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #125 from iovisor/bblanco_dev
Add printf for key/leaf and fix trace_printk bug
parents
3381c792
a328d238
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
212 additions
and
40 deletions
+212
-40
src/cc/bpf_common.cc
src/cc/bpf_common.cc
+11
-0
src/cc/bpf_common.h
src/cc/bpf_common.h
+4
-0
src/cc/bpf_module.cc
src/cc/bpf_module.cc
+169
-30
src/cc/bpf_module.h
src/cc/bpf_module.h
+6
-2
src/cc/frontends/clang/b_frontend_action.cc
src/cc/frontends/clang/b_frontend_action.cc
+8
-8
src/cc/table_desc.h
src/cc/table_desc.h
+2
-0
tests/cc/test_clang.py
tests/cc/test_clang.py
+12
-0
No files found.
src/cc/bpf_common.cc
View file @
02bf6819
...
...
@@ -182,4 +182,15 @@ int bpf_table_update_id(void *program, size_t id, const char *key, const char *l
return
mod
->
table_update
(
id
,
key
,
leaf
);
}
int
bpf_table_key_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_key_printf
(
id
,
buf
,
buflen
,
key
);
}
int
bpf_table_leaf_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_key_printf
(
id
,
buf
,
buflen
,
leaf
);
}
}
src/cc/bpf_common.h
View file @
02bf6819
...
...
@@ -48,6 +48,10 @@ size_t bpf_table_key_size(void *program, const char *table_name);
size_t
bpf_table_key_size_id
(
void
*
program
,
size_t
id
);
size_t
bpf_table_leaf_size
(
void
*
program
,
const
char
*
table_name
);
size_t
bpf_table_leaf_size_id
(
void
*
program
,
size_t
id
);
int
bpf_table_key_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
);
int
bpf_table_leaf_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
);
//int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key);
//int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf);
int
bpf_table_update
(
void
*
program
,
const
char
*
table_name
,
const
char
*
key
,
const
char
*
leaf
);
int
bpf_table_update_id
(
void
*
program
,
size_t
id
,
const
char
*
key
,
const
char
*
leaf
);
...
...
src/cc/bpf_module.cc
View file @
02bf6819
This diff is collapsed.
Click to expand it.
src/cc/bpf_module.h
View file @
02bf6819
...
...
@@ -42,8 +42,9 @@ class BPFModule {
int
parse
(
llvm
::
Module
*
mod
);
int
finalize
();
int
annotate
();
std
::
unique_ptr
<
llvm
::
ExecutionEngine
>
finalize_r
eader
(
std
::
unique_ptr
<
llvm
::
Module
>
mod
);
std
::
unique_ptr
<
llvm
::
ExecutionEngine
>
finalize_r
w
(
std
::
unique_ptr
<
llvm
::
Module
>
mod
);
llvm
::
Function
*
make_reader
(
llvm
::
Module
*
mod
,
llvm
::
Type
*
type
);
llvm
::
Function
*
make_writer
(
llvm
::
Module
*
mod
,
llvm
::
Type
*
type
);
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
);
...
...
@@ -70,10 +71,12 @@ class BPFModule {
const
char
*
table_key_desc
(
const
std
::
string
&
name
)
const
;
size_t
table_key_size
(
size_t
id
)
const
;
size_t
table_key_size
(
const
std
::
string
&
name
)
const
;
int
table_key_printf
(
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
);
const
char
*
table_leaf_desc
(
size_t
id
)
const
;
const
char
*
table_leaf_desc
(
const
std
::
string
&
name
)
const
;
size_t
table_leaf_size
(
size_t
id
)
const
;
size_t
table_leaf_size
(
const
std
::
string
&
name
)
const
;
int
table_leaf_printf
(
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
);
int
table_update
(
size_t
id
,
const
char
*
key
,
const
char
*
leaf
);
int
table_update
(
const
std
::
string
&
name
,
const
char
*
key
,
const
char
*
leaf
);
char
*
license
()
const
;
...
...
@@ -84,7 +87,7 @@ class BPFModule {
std
::
string
proto_filename_
;
std
::
unique_ptr
<
llvm
::
LLVMContext
>
ctx_
;
std
::
unique_ptr
<
llvm
::
ExecutionEngine
>
engine_
;
std
::
unique_ptr
<
llvm
::
ExecutionEngine
>
r
eader
_engine_
;
std
::
unique_ptr
<
llvm
::
ExecutionEngine
>
r
w
_engine_
;
std
::
unique_ptr
<
llvm
::
Module
>
mod_
;
std
::
unique_ptr
<
BLoader
>
b_loader_
;
std
::
unique_ptr
<
ClangLoader
>
clang_loader_
;
...
...
@@ -93,6 +96,7 @@ class BPFModule {
std
::
map
<
std
::
string
,
size_t
>
table_names_
;
std
::
vector
<
std
::
string
>
function_names_
;
std
::
map
<
llvm
::
Type
*
,
llvm
::
Function
*>
readers_
;
std
::
map
<
llvm
::
Type
*
,
llvm
::
Function
*>
writers_
;
};
}
// namespace ebpf
src/cc/frontends/clang/b_frontend_action.cc
View file @
02bf6819
...
...
@@ -227,24 +227,24 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
if
(
Decl
->
getName
()
==
"incr_cksum_l3"
)
{
text
=
"bpf_l3_csum_replace_("
+
fn_args_
[
0
]
->
getName
().
str
()
+
", (u64)"
;
text
+=
args
[
0
]
+
", "
+
args
[
1
]
+
", "
+
args
[
2
]
+
", sizeof("
+
args
[
2
]
+
"))"
;
rewriter_
.
ReplaceText
(
SourceRange
(
Call
->
getLocStart
(),
Call
->
getLocEnd
()),
text
);
}
else
if
(
Decl
->
getName
()
==
"incr_cksum_l4"
)
{
text
=
"bpf_l4_csum_replace_("
+
fn_args_
[
0
]
->
getName
().
str
()
+
", (u64)"
;
text
+=
args
[
0
]
+
", "
+
args
[
1
]
+
", "
+
args
[
2
];
text
+=
", (("
+
args
[
3
]
+
" & 0x1) << 4) | sizeof("
+
args
[
2
]
+
"))"
;
rewriter_
.
ReplaceText
(
SourceRange
(
Call
->
getLocStart
(),
Call
->
getLocEnd
()),
text
);
}
else
if
(
Decl
->
getName
()
==
"bpf_trace_printk"
)
{
// #define bpf_trace_printk(fmt, args...)
// ({ char _fmt[] = fmt; bpf_trace_printk_(_fmt, sizeof(_fmt), args...); })
text
=
"({ char _fmt[] = "
+
args
[
0
]
+
"; bpf_trace_printk_(_fmt, sizeof(_fmt)"
;
if
(
args
.
size
()
>
1
)
text
+=
", "
;
for
(
auto
arg
=
args
.
begin
()
+
1
;
arg
!=
args
.
end
();
++
arg
)
{
text
+=
*
arg
;
if
(
arg
+
1
!=
args
.
end
())
text
+=
", "
;
}
if
(
args
.
size
()
<=
1
)
{
text
+=
"); })"
;
}
rewriter_
.
ReplaceText
(
SourceRange
(
Call
->
getLocStart
(),
Call
->
getLocEnd
()),
text
);
}
else
{
rewriter_
.
ReplaceText
(
SourceRange
(
Call
->
getLocStart
(),
Call
->
getArg
(
0
)
->
getLocEnd
()),
text
);
rewriter_
.
InsertTextAfter
(
Call
->
getLocEnd
(),
"); }"
);
}
}
}
}
}
...
...
src/cc/table_desc.h
View file @
02bf6819
...
...
@@ -33,6 +33,8 @@ struct TableDesc {
std
::
string
leaf_desc
;
llvm
::
Function
*
key_reader
;
llvm
::
Function
*
leaf_reader
;
llvm
::
Function
*
key_writer
;
llvm
::
Function
*
leaf_writer
;
};
}
// namespace ebpf
tests/cc/test_clang.py
View file @
02bf6819
...
...
@@ -82,6 +82,18 @@ int do_request(struct pt_regs *ctx, struct request *req) {
return 0;
}
"""
b
=
BPF
(
text
=
text
,
debug
=
0
)
fn
=
b
.
load_func
(
"do_request"
,
BPF
.
KPROBE
)
def
test_blk_start_request
(
self
):
text
=
"""
#include <linux/blkdev.h>
#include <uapi/linux/ptrace.h>
int do_request(struct pt_regs *ctx, int req) {
bpf_trace_printk("req ptr: 0x%x
\
\
n", req);
return 0;
}
"""
b
=
BPF
(
text
=
text
,
debug
=
0
)
fn
=
b
.
load_func
(
"do_request"
,
BPF
.
KPROBE
)
...
...
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