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 BPFTRACE_STACK_MODE env variable #2586

Merged
merged 1 commit into from
May 9, 2023
Merged

Conversation

jordalgo
Copy link
Contributor

@jordalgo jordalgo commented May 3, 2023

Summary:
This is so users can more easily set the global output for their stacks rather than repeating the same string for every invocation of ustack or kstack

Test Plan:
Tested locally.

BPFTRACE_STACK_MODE=tomato sudo ./src/bpftrace -e 'kprobe:do_nanosleep {
printf("Jordan\n%s\n", ustack());  }'
ERROR: Env var 'BPFTRACE_STACK_MODE' did not contain a valid StackMode:
tomato
BPFTRACE_STACK_MODE=perf sudo ./src/bpftrace -e 'kprobe:do_nanosleep {
printf("Jordan\n%s\n", ustack());  }'
Attaching 1 probe...
Jordan

	7f088cae85b3 __clock_nanosleep+99
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	7f088caed253 __nanosleep+19
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	1f3418b
scribe::common::sink::QueueSink::sendLoop(scribe::common::sink::detail::BatchQueue*,
int)+2971 (/usr/local/fbprojects/scribe/bin/scribed)
	6187e12 folly::ThreadedExecutor::work(folly::Function<void
()>&)+82 (/usr/local/fbprojects/scribe/bin/scribed)
	7f088cedf2e5 execute_native_thread_routine+21
(/usr/local/fbcode/platform010-compat/lib/libstdc++.so.6.0.29)
	7f088ca979bf start_thread+559
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	7f088cb288ac __GI___clone3+44
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)

Reviewers:

Subscribers:

Tasks:

Tags:

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

@jordalgo
Copy link
Contributor Author

jordalgo commented May 3, 2023

Not sure where to add unit tests for this.

src/main.cpp Outdated Show resolved Hide resolved
@viktormalik
Copy link
Contributor

Not sure where to add unit tests for this.

I think that runtime tests (e.g. in tests/runtime/call) would be sufficient.

@@ -553,6 +554,11 @@ Default: `..`

Trailer to add to strings that were truncated. Set to empty string to disable truncation trailers.

### 9.11 `BPFTRACE_STACK_MODE`
Copy link
Member

Choose a reason for hiding this comment

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

Please also update bpftrace.adoc . (We really have to unify it one of these days)


Default: bpftrace

Output format for ustack and kstack builtins. Available modes/formats: `bpftrace`, `perf`, and `raw`.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps also mention that it's still override-able at call sites?

@jordalgo
Copy link
Contributor Author

jordalgo commented May 4, 2023

Not sure where to add unit tests for this.

I think that runtime tests (e.g. in tests/runtime/call) would be sufficient.

I don't see a way to verify the ustack or kstack output easily. The current ustack test just checks that the probe is attached. I could add something like this just to ensure bpftrace doesn't vomit but I don't know how useful it is:

NAME ustack_stack_mode_env
PROG k:do_nanosleep { printf("%s\n%s\n", ustack(), ustack(1)); exit(); }
ENV BPFTRACE_STACK_MODE=raw
EXPECT Attaching 1 probe
TIMEOUT 5
AFTER ./testprogs/syscall nanosleep  1e8

@viktormalik
Copy link
Contributor

I don't see a way to verify the ustack or kstack output easily. The current ustack test just checks that the probe is attached. I could add something like this just to ensure bpftrace doesn't vomit but I don't know how useful it is:

Could you verify that any kstack/ustack was printed in the correct format?

# bpftrace format
EXPECT ^\s+[a-zA-Z0-9_]+
# perf format
EXPECT ^\s+[0-9a-f]+ [a-zA-Z0-9_]+
# raw format
EXPECT ^\s+[0-9a-f]+$

@jordalgo
Copy link
Contributor Author

jordalgo commented May 4, 2023

Could you verify that any kstack/ustack was printed in the correct format?

# bpftrace format
EXPECT ^\s+[a-zA-Z0-9_]+
# perf format
EXPECT ^\s+[0-9a-f]+ [a-zA-Z0-9_]+
# raw format
EXPECT ^\s+[0-9a-f]+$

I worry that the output might change slightly and then the regex would fail e.g. here is the output for this:

NAME ustack_stack_mode_env
PROG k:do_nanosleep { printf("%s", ustack(1)); exit(); }
ENV BPFTRACE_STACK_MODE=perf
EXPECT ^\s+[0-9a-f]+$
TIMEOUT 5
AFTER ./testprogs/syscall nanosleep  1e8
Attaching 1 probe...\n__BPFTRACE_NOTIFY_PROBES_ATTACHED\n\n\t7f088cae85b3 __clock_nanosleep+99 (/usr/local/fbcode/platform010-compat/lib/libc.so.6)\n\n\n

@viktormalik
Copy link
Contributor

Could you verify that any kstack/ustack was printed in the correct format?

# bpftrace format
EXPECT ^\s+[a-zA-Z0-9_]+
# perf format
EXPECT ^\s+[0-9a-f]+ [a-zA-Z0-9_]+
# raw format
EXPECT ^\s+[0-9a-f]+$

I worry that the output might change slightly and then the regex would fail e.g. here is the output for this:

NAME ustack_stack_mode_env
PROG k:do_nanosleep { printf("%s", ustack(1)); exit(); }
ENV BPFTRACE_STACK_MODE=perf
EXPECT ^\s+[0-9a-f]+$
TIMEOUT 5
AFTER ./testprogs/syscall nanosleep  1e8
Attaching 1 probe...\n__BPFTRACE_NOTIFY_PROBES_ATTACHED\n\n\t7f088cae85b3 __clock_nanosleep+99 (/usr/local/fbcode/platform010-compat/lib/libc.so.6)\n\n\n

But you're using the raw regex for perf format.

@jordalgo
Copy link
Contributor Author

jordalgo commented May 4, 2023

But you're using the raw regex for perf format.

I was just using that as an example of the output. As the regexes for all would need to start with something that looked like this

^Attaching 1 probe...\\n__BPFTRACE_NOTIFY_PROBES_ATTACHED[\\n\\t]*

Feels fragile

@viktormalik
Copy link
Contributor

I don't think so, the regex should be matched against every line of the output.

@jordalgo
Copy link
Contributor Author

jordalgo commented May 4, 2023

I don't think so, the regex should be matched against every line of the output.

Well, I don't quite understand HOW it passes but it does :) E.g. why doesn't the check fail for the line "Attaching 1 probe..." against this regex ^\s+[0-9a-f]+$. Maybe it just needs to match against at least ONE line in the output?

@jordalgo jordalgo force-pushed the master branch 3 times, most recently from fe7c0bd to cf31b7d Compare May 4, 2023 17:23
@danobi
Copy link
Member

danobi commented May 7, 2023

I think the regex only has to match a single line:

https://github.com/iovisor/bpftrace/blob/8e9853fb015e62968cde853dc8c1cdca8c68fec8/tests/runtime/engine/runner.py#L251
https://docs.python.org/3/library/re.html#re.search

which would match my experience (IIRC)

So I’m not sure having perf/raw/bpftrace regex is correct.

@jordalgo
Copy link
Contributor Author

jordalgo commented May 8, 2023

So I’m not sure having perf/raw/bpftrace regex is correct.

I guess it depends how much extra noise we get outside of the single stack trace that we think might pollute the results. The tests I added seem to do the right thing and fail if we change the StackMode...

@viktormalik
Copy link
Contributor

So I’m not sure having perf/raw/bpftrace regex is correct.

I guess it depends how much extra noise we get outside of the single stack trace that we think might pollute the results. The tests I added seem to do the right thing and fail if we change the StackMode...

Agreed with @jordalgo, the added regexes are specific enough to avoid matching anything outside of the expected stack trace.

Summary:
This is so users can more easily set the global output for their stacks
rather than repeating the same string for every invocation of ustack or
kstack

Test Plan:
Tested locally.
```
BPFTRACE_STACK_MODE=tomato sudo ./src/bpftrace -e 'kprobe:do_nanosleep {
printf("Jordan\n%s\n", ustack());  }'
ERROR: Env var 'BPFTRACE_STACK_MODE' did not contain a valid StackMode:
tomato
```

```
BPFTRACE_STACK_MODE=perf sudo ./src/bpftrace -e 'kprobe:do_nanosleep {
printf("Jordan\n%s\n", ustack());  }'
Attaching 1 probe...
Jordan

	7f088cae85b3 __clock_nanosleep+99
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	7f088caed253 __nanosleep+19
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	1f3418b
scribe::common::sink::QueueSink::sendLoop(scribe::common::sink::detail::BatchQueue*,
int)+2971 (/usr/local/fbprojects/scribe/bin/scribed)
	6187e12 folly::ThreadedExecutor::work(folly::Function<void
()>&)+82 (/usr/local/fbprojects/scribe/bin/scribed)
	7f088cedf2e5 execute_native_thread_routine+21
(/usr/local/fbcode/platform010-compat/lib/libstdc++.so.6.0.29)
	7f088ca979bf start_thread+559
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
	7f088cb288ac __GI___clone3+44
(/usr/local/fbcode/platform010-compat/lib/libc.so.6)
```

Reviewers:

Subscribers:

Tasks:

Tags:
@danobi danobi merged commit 491089d into bpftrace:master May 9, 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