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

support all iters #2630

Merged
merged 6 commits into from
Jun 20, 2023
Merged

support all iters #2630

merged 6 commits into from
Jun 20, 2023

Conversation

chengshuyi
Copy link
Contributor

This pr mainly improves bpftrace's support for iter

  • bpftrace -l iter:* print the iter supported by the current kernel
  • Support the iter supported by the current kernel, no need to hard code

issues: #2620

Checklist
  • Language changes are updated in man/adoc/bpftrace.adoc and if needed in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@chengshuyi chengshuyi force-pushed the iter branch 3 times, most recently from d9883c6 to e28f583 Compare May 30, 2023 07:01
int main(void)
{
struct bpf_iter__task iter_task;
struct bpf_iter__task_file iter_task_file;
struct bpf_iter__task_vma iter_task_vma;

func_1(0, 0, 0);

bpf_iter_task();

Check warning

Code scanning / CodeQL

Expression has no effect

This expression has no effect (because [bpf_iter_task](1) has no external side effects).
int main(void)
{
struct bpf_iter__task iter_task;
struct bpf_iter__task_file iter_task_file;
struct bpf_iter__task_vma iter_task_vma;

func_1(0, 0, 0);

bpf_iter_task();
bpf_iter_task_file();

Check warning

Code scanning / CodeQL

Expression has no effect

This expression has no effect (because [bpf_iter_task_file](1) has no external side effects).

bpf_iter_task();
bpf_iter_task_file();
bpf_iter_task_vma();

Check warning

Code scanning / CodeQL

Expression has no effect

This expression has no effect (because [bpf_iter_task_vma](1) has no external side effects).
@chengshuyi chengshuyi force-pushed the iter branch 2 times, most recently from 917ba89 to 37f4f08 Compare May 30, 2023 08:10
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.

Woah, didn't realize it would be this easy to support all iters. Lgtm overall, just some small nits. Great PR!

src/btf.cpp Outdated Show resolved Hide resolved
src/btf.cpp Outdated Show resolved Hide resolved
src/probe_matcher.cpp Outdated Show resolved Hide resolved
src/probe_matcher.cpp Outdated Show resolved Hide resolved
@chengshuyi
Copy link
Contributor Author

chengshuyi commented Jun 5, 2023

Can we also delete

https://github.com/iovisor/bpftrace/blob/446109acdef586fb0f0b5d40196dedb946b613ed/src/bpffeature.h#L111-L122

and
https://github.com/iovisor/bpftrace/blob/446109acdef586fb0f0b5d40196dedb946b613ed/src/bpffeature.h#L57-L58

?

OK, it has been removed. In addition, a new commit support to get all iter params has been added, thanks for your review

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.

Nice!

src/btf.cpp Outdated Show resolved Hide resolved
@danobi
Copy link
Member

danobi commented Jun 16, 2023

Can you update the changelog please?

According to [1], we can get all iter types supported by the current kernel according to the 'bpf_ter__' prefix

[1]: https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html#how-to-use-bpf-iterators

Signed-off-by: Shuyi Cheng <[email protected]>
Run bpftrace -l iter:* the output is as follows:

	iter:task
	iter:task_file
	iter:task_vma

But the current kernel does not support 'iter:task_vma'. After applying this patch, run the command again, the output is as follows:

	iter:bpf_map
	iter:bpf_map_elem
	iter:bpf_prog
	iter:bpf_sk_storage_map
	iter:ipv6_route
	iter:netlink
	iter:sockmap
	iter:task
	iter:task_file
	iter:tcp
	iter:udp

Signed-off-by: Shuyi Cheng <[email protected]>
For each iter, the kernel will have a bpf_iter_$ITER function, so we can get the parameters of iter through bpf_iter_$ITER.

Run `bpftrace -l iter:*` and shows:

```
iter:task
    struct task_struct * task
iter:task_file
    struct task_struct * task
    int fd
    struct file * file
iter:task_vma
    struct task_struct * task
    struct vm_area_struct * vma
```

The output after applying this patch is as follows:

```
iter:bpf_map
    struct bpf_map * map
iter:bpf_map_elem
    struct bpf_map * map
    void * key
    void * value
iter:bpf_prog
    struct bpf_prog * prog
iter:bpf_sk_storage_map
    struct bpf_map * map
    struct sock * sk
    void * value
iter:ipv6_route
    struct fib6_info * rt
iter:netlink
    struct netlink_sock * sk
iter:sockmap
    struct bpf_map * map
    void * key
    struct sock * sk
iter:task
    struct task_struct * task
iter:task_file
    struct task_struct * task
    u32 fd
    struct file * file
iter:tcp
    struct sock_common * sk_common
    uid_t uid
iter:udp
    struct udp_sock * udp_sk
    uid_t uid
    int bucket
```

Signed-off-by: Shuyi Cheng <[email protected]>
Support all iters, this patch mainly modifies the following places:

- Delete the hard code, and get the type of iter by concatenating `bpf_iter__` and the function name
- Use has_iter to check whether the iter exists
- add iter tracing function for tests
- clean up older iter related code

Signed-off-by: Shuyi Cheng <[email protected]>
When ap.func is empty, the input parameter tokens of the wildcard_match function will be empty, and a panic will occur when tokens[0] is obtained. Therefore, iter should be similar to kfunc, and avoid ap.func being empty during the semantic analysis stage.

```
bpftrace -l kfunc:
stdin:1:1-7: ERROR: kfunc should specify a function
kfunc:
~~~~~~

bpftrace -l iter:
stdin:1:1-6: ERROR: iter should specify a iterator's name
iter:
~~~~~
```

Signed-off-by: Shuyi Cheng <[email protected]>
Signed-off-by: Shuyi Cheng <[email protected]>
@chengshuyi
Copy link
Contributor Author

Can you update the changelog please?

Done, thanks.

@viktormalik viktormalik merged commit 593f5c9 into bpftrace:master Jun 20, 2023
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.

3 participants