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
2faf0605
Commit
2faf0605
authored
Dec 12, 2017
by
4ast
Committed by
GitHub
Dec 12, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1474 from iovisor/yhs_dev
provide padded structure for table_{key|leaf}_desc API
parents
34b3c10e
538a84e1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
16 deletions
+86
-16
src/cc/json_map_decl_visitor.cc
src/cc/json_map_decl_visitor.cc
+59
-13
tests/python/test_clang.py
tests/python/test_clang.py
+22
-0
tests/python/test_trace2.py
tests/python/test_trace2.py
+5
-3
No files found.
src/cc/json_map_decl_visitor.cc
View file @
2faf0605
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
#include <string>
#include <string>
#include <clang/AST/ASTContext.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/RecordLayout.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include "common.h"
#include "common.h"
#include "table_desc.h"
#include "table_desc.h"
...
@@ -43,6 +44,10 @@ class BMapDeclVisitor : public clang::RecursiveASTVisitor<BMapDeclVisitor> {
...
@@ -43,6 +44,10 @@ class BMapDeclVisitor : public clang::RecursiveASTVisitor<BMapDeclVisitor> {
bool
VisitEnumConstantDecl
(
clang
::
EnumConstantDecl
*
D
);
bool
VisitEnumConstantDecl
(
clang
::
EnumConstantDecl
*
D
);
bool
VisitEnumDecl
(
clang
::
EnumDecl
*
D
);
bool
VisitEnumDecl
(
clang
::
EnumDecl
*
D
);
private:
bool
shouldSkipPadding
(
const
RecordDecl
*
D
);
void
genJSONForField
(
FieldDecl
*
F
);
private:
private:
clang
::
ASTContext
&
C
;
clang
::
ASTContext
&
C
;
std
::
string
&
result_
;
std
::
string
&
result_
;
...
@@ -50,6 +55,36 @@ class BMapDeclVisitor : public clang::RecursiveASTVisitor<BMapDeclVisitor> {
...
@@ -50,6 +55,36 @@ class BMapDeclVisitor : public clang::RecursiveASTVisitor<BMapDeclVisitor> {
// Encode the struct layout as a json description
// Encode the struct layout as a json description
BMapDeclVisitor
::
BMapDeclVisitor
(
ASTContext
&
C
,
string
&
result
)
:
C
(
C
),
result_
(
result
)
{}
BMapDeclVisitor
::
BMapDeclVisitor
(
ASTContext
&
C
,
string
&
result
)
:
C
(
C
),
result_
(
result
)
{}
bool
BMapDeclVisitor
::
shouldSkipPadding
(
const
RecordDecl
*
D
)
{
if
(
D
->
isUnion
()
||
D
->
field_empty
())
return
true
;
for
(
auto
F
:
D
->
getDefinition
()
->
fields
())
{
if
(
F
->
isBitField
())
return
true
;
QualType
Ty
=
F
->
getType
();
if
(
Ty
->
isIncompleteArrayType
())
return
true
;
}
return
false
;
}
void
BMapDeclVisitor
::
genJSONForField
(
FieldDecl
*
F
)
{
if
(
F
->
isAnonymousStructOrUnion
())
{
if
(
const
RecordType
*
R
=
dyn_cast
<
RecordType
>
(
F
->
getType
()))
TraverseDecl
(
R
->
getDecl
());
result_
+=
", "
;
return
;
}
result_
+=
"["
;
TraverseDecl
(
F
);
if
(
const
ConstantArrayType
*
T
=
dyn_cast
<
ConstantArrayType
>
(
F
->
getType
()))
result_
+=
", ["
+
T
->
getSize
().
toString
(
10
,
false
)
+
"]"
;
if
(
F
->
isBitField
())
result_
+=
", "
+
to_string
(
F
->
getBitWidthValue
(
C
));
result_
+=
"], "
;
}
bool
BMapDeclVisitor
::
VisitFieldDecl
(
FieldDecl
*
D
)
{
bool
BMapDeclVisitor
::
VisitFieldDecl
(
FieldDecl
*
D
)
{
result_
+=
"
\"
"
;
result_
+=
"
\"
"
;
result_
+=
D
->
getName
();
result_
+=
D
->
getName
();
...
@@ -82,25 +117,36 @@ bool BMapDeclVisitor::TraverseRecordDecl(RecordDecl *D) {
...
@@ -82,25 +117,36 @@ bool BMapDeclVisitor::TraverseRecordDecl(RecordDecl *D) {
return
false
;
return
false
;
return
true
;
return
true
;
}
}
bool
BMapDeclVisitor
::
VisitRecordDecl
(
RecordDecl
*
D
)
{
bool
BMapDeclVisitor
::
VisitRecordDecl
(
RecordDecl
*
D
)
{
result_
+=
"[
\"
"
;
result_
+=
"[
\"
"
;
result_
+=
D
->
getName
();
result_
+=
D
->
getName
();
result_
+=
"
\"
, ["
;
result_
+=
"
\"
, ["
;
for
(
auto
F
:
D
->
getDefinition
()
->
fields
())
{
if
(
F
->
isAnonymousStructOrUnion
())
{
bool
SkipPadding
=
shouldSkipPadding
(
D
);
if
(
const
RecordType
*
R
=
dyn_cast
<
RecordType
>
(
F
->
getType
()))
if
(
SkipPadding
)
{
TraverseDecl
(
R
->
getDecl
());
for
(
auto
F
:
D
->
getDefinition
()
->
fields
())
{
result_
+=
", "
;
genJSONForField
(
F
);
continue
;
}
}
else
{
const
ASTRecordLayout
&
Layout
=
C
.
getASTRecordLayout
(
D
);
CharUnits
Offset
=
C
.
toCharUnitsFromBits
(
Layout
.
getFieldOffset
(
0
));
for
(
auto
F
:
D
->
getDefinition
()
->
fields
())
{
CharUnits
FieldSize
=
C
.
getTypeSizeInChars
(
F
->
getType
());
auto
FieldOffsetBits
=
Layout
.
getFieldOffset
(
F
->
getFieldIndex
());
CharUnits
FieldOffset
=
C
.
toCharUnitsFromBits
(
FieldOffsetBits
);
uint64_t
Padding
=
(
FieldOffset
-
Offset
).
getQuantity
();
if
(
Padding
)
{
/* Padding before this field with "char __pad_<FieldIndex>[Padding]". */
result_
+=
"[
\"
__pad_"
+
to_string
(
F
->
getFieldIndex
())
+
"
\"
,
\"
char
\"
,["
+
to_string
(
Padding
)
+
"]], "
;
}
Offset
=
FieldOffset
+
FieldSize
;
genJSONForField
(
F
);
}
}
result_
+=
"["
;
TraverseDecl
(
F
);
if
(
const
ConstantArrayType
*
T
=
dyn_cast
<
ConstantArrayType
>
(
F
->
getType
()))
result_
+=
", ["
+
T
->
getSize
().
toString
(
10
,
false
)
+
"]"
;
if
(
F
->
isBitField
())
result_
+=
", "
+
to_string
(
F
->
getBitWidthValue
(
C
));
result_
+=
"], "
;
}
}
if
(
!
D
->
getDefinition
()
->
field_empty
())
if
(
!
D
->
getDefinition
()
->
field_empty
())
result_
.
erase
(
result_
.
end
()
-
2
);
result_
.
erase
(
result_
.
end
()
-
2
);
result_
+=
"]"
;
result_
+=
"]"
;
...
...
tests/python/test_clang.py
View file @
2faf0605
...
@@ -675,6 +675,28 @@ BPF_HASH(table1, unsigned __int128, __int128);
...
@@ -675,6 +675,28 @@ BPF_HASH(table1, unsigned __int128, __int128);
struct
.
pack
(
'LL'
,
k
[
0
],
k
[
1
])),
struct
.
pack
(
'LL'
,
k
[
0
],
k
[
1
])),
"2001:db8::"
)
"2001:db8::"
)
def
test_padding_types
(
self
):
text
=
"""
struct key_t {
u32 f1_1; /* offset 0 */
struct {
char f2_1; /* offset 16 */
__int128 f2_2; /* offset 32 */
};
u8 f1_3; /* offset 48 */
unsigned __int128 f1_4; /* offset 64 */
};
struct value_t {
u8 src[4] __attribute__ ((aligned (8))); /* offset 0 */
u8 dst[4] __attribute__ ((aligned (8))); /* offset 8 */
};
BPF_HASH(table1, struct key_t, struct value_t);
"""
b
=
BPF
(
text
=
text
)
table
=
b
[
'table1'
]
self
.
assertEqual
(
ct
.
sizeof
(
table
.
Key
),
80
)
self
.
assertEqual
(
ct
.
sizeof
(
table
.
Leaf
),
12
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
()
main
()
tests/python/test_trace2.py
View file @
2faf0605
...
@@ -11,12 +11,14 @@ from unittest import main, TestCase
...
@@ -11,12 +11,14 @@ from unittest import main, TestCase
text
=
"""
text
=
"""
#include <linux/ptrace.h>
#include <linux/ptrace.h>
struct Ptr { u64 ptr; };
struct Ptr { u64 ptr; };
struct Counters {
u64
stat1; };
struct Counters {
char unused; __int128
stat1; };
BPF_HASH(stats, struct Ptr, struct Counters, 1024);
BPF_HASH(stats, struct Ptr, struct Counters, 1024);
int count_sched(struct pt_regs *ctx) {
int count_sched(struct pt_regs *ctx) {
struct Ptr key = {.ptr=PT_REGS_PARM1(ctx)};
struct Ptr key = {.ptr=PT_REGS_PARM1(ctx)};
struct Counters zleaf = {0};
struct Counters zleaf;
memset(&zleaf, 0, sizeof(zleaf));
stats.lookup_or_init(&key, &zleaf)->stat1++;
stats.lookup_or_init(&key, &zleaf)->stat1++;
return 0;
return 0;
}
}
...
@@ -32,7 +34,7 @@ class TestTracingEvent(TestCase):
...
@@ -32,7 +34,7 @@ class TestTracingEvent(TestCase):
for
i
in
range
(
0
,
100
):
for
i
in
range
(
0
,
100
):
sleep
(
0.01
)
sleep
(
0.01
)
for
key
,
leaf
in
self
.
stats
.
items
():
for
key
,
leaf
in
self
.
stats
.
items
():
print
(
"ptr %x:"
%
key
.
ptr
,
"stat1
%d"
%
leaf
.
stat1
)
print
(
"ptr %x:"
%
key
.
ptr
,
"stat1
(%d %d)"
%
(
leaf
.
stat1
[
1
],
leaf
.
stat1
[
0
])
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
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