Skip to content
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

Merged
merged 1 commit into from
May 2, 2023
Merged

Add offsetof() #2579

merged 1 commit into from
May 2, 2023

Conversation

Rtoax
Copy link
Contributor

@Rtoax Rtoax commented Apr 25, 2023

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:

    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: #2216
[1] v2: #2565

@Rtoax Rtoax mentioned this pull request Apr 25, 2023
@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch 4 times, most recently from 198d9ea to ba6222d Compare April 25, 2023 06:37
@@ -272,6 +279,16 @@
{
}

Offsetof::Offsetof(SizedType record, std::string &field, location loc)

Check notice

Code scanning / CodeQL

Large object passed by value

This parameter of type [SizedType](1) is 136 bytes - consider passing a const pointer/reference instead.
@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch 2 times, most recently from a47adff to 5babd76 Compare April 25, 2023 07:57
Copy link
Contributor

@viktormalik viktormalik left a 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.

src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
tests/codegen/call_offsetof.cpp Outdated Show resolved Hide resolved
tests/mocks.cpp Outdated Show resolved Hide resolved
tests/semantic_analyser.cpp Show resolved Hide resolved
src/parser.yy Show resolved Hide resolved
@viktormalik
Copy link
Contributor

cc @lenticularis39

@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch from 5babd76 to 817461f Compare April 25, 2023 08:23
@Rtoax Rtoax requested a review from viktormalik April 25, 2023 08:42
@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch from 817461f to cf0aab7 Compare April 25, 2023 09:49
@Rtoax Rtoax requested a review from lenticularis39 April 25, 2023 09:50
src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
tests/semantic_analyser.cpp Show resolved Hide resolved
@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch 2 times, most recently from 00c9afe to 8068c20 Compare April 25, 2023 11:18
@Rtoax Rtoax requested a review from viktormalik April 25, 2023 11:18
Copy link
Contributor

@viktormalik viktormalik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Member

@danobi danobi left a 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!

docs/reference_guide.md Outdated Show resolved Hide resolved
man/adoc/bpftrace.adoc Show resolved Hide resolved
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>
@Rtoax Rtoax force-pushed the patch-44-offsetof-v4 branch from 8068c20 to a1aae77 Compare April 28, 2023 13:14
@Rtoax
Copy link
Contributor Author

Rtoax commented Apr 28, 2023

@lenticularis39 Fixed the description of offsetof() in docs/reference_guide.md and man/adoc/bpftrace.adoc, and rebase to master. please review.

@Rtoax Rtoax requested a review from lenticularis39 April 28, 2023 14:19
@viktormalik viktormalik merged commit a7c2c35 into bpftrace:master May 2, 2023
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Jan 31, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Jan 31, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 1, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 2, 2025
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>
Rtoax added a commit to Rtoax/bpftrace that referenced this pull request Feb 2, 2025
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>
jordalgo pushed a commit that referenced this pull request Feb 7, 2025
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants