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 custom signal handling probe type #3522

Merged
merged 1 commit into from
Nov 2, 2024

Conversation

amiremohamadi
Copy link
Contributor

@amiremohamadi amiremohamadi commented Oct 16, 2024

#3500

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

@amiremohamadi amiremohamadi marked this pull request as draft October 16, 2024 13:46
@amiremohamadi amiremohamadi marked this pull request as ready for review October 17, 2024 08:32
@amiremohamadi amiremohamadi changed the title [Draft] Add custom signal handling probe type Add custom signal handling probe type Oct 17, 2024
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 feature! Just a few comments below.

man/adoc/bpftrace.adoc Outdated Show resolved Hide resolved
src/ast/attachpoint_parser.cpp Outdated Show resolved Hide resolved
src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
src/bpftrace.cpp Outdated Show resolved Hide resolved
@ajor
Copy link
Member

ajor commented Oct 17, 2024

This feature is a good example of something where #3521 would be useful if we had it - it's a good feature to add but I don't think we're settled on a probe name yet.

@ajor
Copy link
Member

ajor commented Oct 17, 2024

To throw out another idea for probe naming that combines two cases we'd eventually want to support:

trigger:signal:SIGUSR1 {}
trigger:keypress:space {}

@amiremohamadi
Copy link
Contributor Author

To throw out another idea for probe naming that combines two cases we'd eventually want to support:

trigger:signal:SIGUSR1 {}
trigger:keypress:space {}

looks more clear. if you all agree, we can use this naming.

@amiremohamadi
Copy link
Contributor Author

or maybe using event instead of trigger?

event:signal:SIGUSR1 {}

@viktormalik
Copy link
Contributor

What is the purpose of using the trigger: or event: prefix? To logically group probe types which fire when the bpftrace process is a target of an event (signal, keypress)? Are we expecting more such events in future?

@ajor
Copy link
Member

ajor commented Oct 18, 2024

What is the purpose of using the trigger: or event: prefix? To logically group probe types which fire when the bpftrace process is a target of an event (signal, keypress)? Are we expecting more such events in future?

The logical grouping was the reason in my mind. I can't think of any classes of event like this right now other than signals and keypresses.

I guess another reason for grouping them behind a single probe type would be that it makes it clear that it's not tracing any signal or keypress systemwide - only ones sent to the bpftrace runtime. Another idea based on this:

bpftrace:signal:SIGUSR
bpftrace:keypress:space

@viktormalik
Copy link
Contributor

I guess another reason for grouping them behind a single probe type would be that it makes it clear that it's not tracing any signal or keypress systemwide - only ones sent to the bpftrace runtime. Another idea based on this:

bpftrace:signal:SIGUSR
bpftrace:keypress:space

I second this reason and this syntax makes it pretty clear that these are internal events received by bpftrace itself. I like it probably the best out of the proposed options. And it nicely opens way for more internal (pseudo-)events in future. We could even move BEGIN and END to the same group (at least internally).

@amiremohamadi amiremohamadi force-pushed the signal-probe branch 2 times, most recently from 7d5759b to 4905494 Compare October 18, 2024 16:30
@amiremohamadi amiremohamadi force-pushed the signal-probe branch 2 times, most recently from b7c6bf7 to e0f13fb Compare October 20, 2024 18:56
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 changes! A few more comments but I think that it's almost ready.

src/ast/passes/semantic_analyser.cpp Outdated Show resolved Hide resolved
src/bpftrace.cpp Outdated Show resolved Hide resolved
src/types.h Outdated Show resolved Hide resolved
@jordalgo
Copy link
Contributor

Nit: The commit message could use some more detail and perhaps an example.

man/adoc/bpftrace.adoc Outdated Show resolved Hide resolved
@jordalgo
Copy link
Contributor

I don't mean to hold up this PR but I got confused when I first saw this naming bpftrace:signal:SIGUSR. I read the conversation above and understand the reasoning but naming a probe after the name of the tool feels wrong. I'm still thinking about alternatives but what about something like self:signal:SIGUSR ?

@jordalgo
Copy link
Contributor

Should this also deprecate the ability to print maps when issuing a SIGUSR to bpftrace? Seems strange to have both.
https://github.com/bpftrace/bpftrace/blob/master/man/adoc/bpftrace.adoc#map-printing

@viktormalik
Copy link
Contributor

Should this also deprecate the ability to print maps when issuing a SIGUSR to bpftrace? Seems strange to have both. https://github.com/bpftrace/bpftrace/blob/master/man/adoc/bpftrace.adoc#map-printing

I think the PR removes the functionality but fails to mention it in the docs. This is probably a good candidate for the "Breaking" changelog section and a migration guide entry.

@amiremohamadi
Copy link
Contributor Author

amiremohamadi commented Oct 21, 2024

hmm. while defining a signal probe changes the default behavior, still it's not a breaking change for those who don’t use it. does it really need a migration guide entry?
@viktormalik @jordalgo

src/bpftrace.cpp Outdated Show resolved Hide resolved
@jordalgo
Copy link
Contributor

@viktormalik - Making sure you saw my comment above. What do you think about self:signal:SIGUSR instead of bpftrace:signal:SIGUSR?

@danobi
Copy link
Member

danobi commented Oct 23, 2024

Apologies in advance if this sends you on bit of a side quest, but BPF_PROG_TYPE_RAW_TRACEPOINT was added in 4.17 kernel: torvalds/linux@a0fe3e5

bpftrace's policy w.r.t. old kernels is we support up to the oldest LTS kernel still in service: https://www.kernel.org/category/releases.html

The oldest LTS kernel still in service is 4.19, so we are free to assume BPF_PROG_TYPE_RAW_TRACEPOINT is always available.

So let's delete all the trigger probe stuff. It was always a bit of a hack. It should greatly simplify some of the stuff you're doing in this PR. Also means we can delete the feature probe (has_raw_tp_special())

@amiremohamadi
Copy link
Contributor Author

@danobi just to make sure i got your point correctly, now we don't need to attach the special probes to anyting? calling bpf_prog_test_run_opts would be enough, right?

@andreasgerstmayr
Copy link
Contributor

Also cc @andreasgerstmayr who added the original "print maps on SIGUSR1" feature.

Thanks for the ping! I agree it's a breaking change, but it'll be simple to adjust existing programs.
Nice feature! 💯

@amiremohamadi amiremohamadi force-pushed the signal-probe branch 2 times, most recently from 0ae22d2 to a26f8e8 Compare October 25, 2024 19:07
@danobi
Copy link
Member

danobi commented Oct 25, 2024

@danobi just to make sure i got your point correctly, now we don't need to attach the special probes to anyting? calling bpf_prog_test_run_opts would be enough, right?

Yes, that's right. And sorry about late reply - must've missed notification

@danobi
Copy link
Member

danobi commented Oct 26, 2024

@viktormalik - Making sure you saw my comment above. What do you think about self:signal:SIGUSR instead of bpftrace:signal:SIGUSR?

Hmm, I bpftrace prefix is ok. Maybe I write too much c++/rust. In both languages you end up having a top level namespace w/ same name as project. self is probably ok too.

@jordalgo
Copy link
Contributor

Maybe I write too much c++/rust. In both languages you end up having a top level namespace w/ same name as project.

But do you find yourself writing the words "c++" or "rust" in these projects? Maybe it's just a gut reaction but I feel weird reading/writing "bpftrace" as a keyword in the tool/language itself. "self" is also shorter.

And if we decide to someday add support for embedding bpftrace scripts in other languages then the process running these scripts will not be "bpftrace" but some other process. "self" would still apply in this case.

@danobi
Copy link
Member

danobi commented Oct 28, 2024

:P sure. Sounds like self is shorter and less ambiguous for future use cases.

<joke>or we could do "this"</joke>

@amiremohamadi
Copy link
Contributor Author

thanks folks. I’d like to know @ajor and @viktormalik opinion too before doing any modifications.

@viktormalik
Copy link
Contributor

Yeah, self sounds good and probably expresses the purpose better (the "target" is the current bpftrace process). No strong opinion on my side.

Copy link
Contributor

@jordalgo jordalgo 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 for the contribution!

This does make me crave the ability for bpftrace to be more interactive and be able to handle custom commands from stdin e.g. "print_maps", "lost_event_count", "print_map_value(@A, 1)", etc.

@amiremohamadi
Copy link
Contributor Author

This does make me crave the ability for bpftrace to be more interactive and be able to handle custom commands from stdin e.g. "print_maps", "lost_event_count", "print_map_value(@A, 1)", etc.

if I’m not mistaken, we can also receive data via signals. while this approach isn’t as convenient as sending data through stdin, it can still be implemented relatively easily in the future

@jordalgo
Copy link
Contributor

jordalgo commented Nov 1, 2024

@viktormalik @danobi - One last look before I merge?

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, just one nit.

Also, we should squash the PR, even though it's not ideal as it actually does 2 things - simplify the special probes implementation and add signal probes.

src/required_resources.h Outdated Show resolved Hide resolved
src/bpfbytecode.cpp Show resolved Hide resolved
this commit implements a custom signal handling probe
which will be triggered when the specified signal is
sent to the bpftrace process. the default behavior of
printing maps on `SIGUSR1` is removed.

```
bpftrace:signal:SIGUSR1 {
    print("hello");
}
```

Also trigger functions and extra checks for the special probes
are removed as BPF_PROG_TYPE_RAW_TRACEPOINT was added in 4.17 kernel:

    torvalds/linux@a0fe3e5

The oldest LTS kernel still in service is 4.19, so we are free
to assume BPF_PROG_TYPE_RAW_TRACEPOINT is always available
@danobi
Copy link
Member

danobi commented Nov 2, 2024

Did a quick skim - didn't spot any major issues

@danobi danobi merged commit 7b72595 into bpftrace:master Nov 2, 2024
18 checks passed
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.

7 participants