-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add offsetof() #2579
Add offsetof() #2579
Conversation
198d9ea
to
ba6222d
Compare
a47adff
to
5babd76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good, thanks! I'd just like to see a few more tests and highlighted a couple of nits below.
5babd76
to
817461f
Compare
817461f
to
cf0aab7
Compare
00c9afe
to
8068c20
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for getting this through!
Add the offsetof() function, the syntax follows the kernel offsetof() macro definition, and currently only supports single-member format parameters such as offsetof(struct task_struct, comm). The function returns a long integer with the output format '%ld'. This is the third PR for offsetof(), v1[0] and v2[1] was not merged, and the discussion of offsetof() can be found in [0][1]. Basic use: struct Foo { int a; long b; char c; struct { int d; }; struct { int a; } e; } struct offsetof { int bswap; int comm; int kstack; int ustack; int pid; int ctx; int arg; } BEGIN { printf("%ld\n", offsetof(struct Foo, c)); printf("%ld\n", offsetof(struct Foo, d)); printf("%ld\n", offsetof(struct offsetof, bswap)); printf("%ld\n", offsetof(struct task_struct, comm)); printf("%ld\n", offsetof(*curtask, comm)); exit(); } Get offset: 16 20 0 3240 3240 [0] v1: bpftrace#2216 [1] v2: bpftrace#2565 Signed-off-by: Rong Tao <rongtao@cestc.cn>
8068c20
to
a1aae77
Compare
@lenticularis39 Fixed the description of |
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c). Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c). Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: bpftrace#2579 [1] merged Link: bpftrace#2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Since bpftrace PR [1] support offsetof() function. As previously discussed [2], the current offsetof() does not support sub-fields, and this PR implements offsetof(struct foo, a.b.c), implement [1] unfinished features. Example: struct Foo { struct e { int a; int b; struct c { int a, b, c; } c; } e; } struct offsetof { struct { int comm; int ustack; } kstack; } BEGIN { printf("struct Foo e.b offset %ld\n", offsetof(struct Foo, e.b)); printf("struct Foo e.c offset %ld\n", offsetof(struct Foo, e.c)); printf("struct Foo e.c.b offset %ld\n", offsetof(struct Foo, e.c.b)); printf("struct offsetof kstack.ustack offset %ld\n", offsetof(struct offsetof, kstack.ustack)); exit(); } Link: #2579 [1] merged Link: #2216 [2] Signed-off-by: Rong Tao <rongtao@cestc.cn>
Add the offsetof() function, the syntax follows the kernel offsetof() macro definition, and currently only supports single-member format parameters such as offsetof(struct task_struct, comm). The function returns a long integer with the output format '%ld'.
This is the second PR for offsetof(), v1[0] and v2[1] was not merged, and the discussion of offsetof() can be found in [0][1].
Basic use:
Get offset:
[0] v1: #2216
[1] v2: #2565