Skip to content

Commit fe7c0bd

Browse files
committed
Add BPFTRACE_STACK_MODE env variable
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:
1 parent 8e9853f commit fe7c0bd

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

docs/reference_guide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ ENVIRONMENT:
176176
BPFTRACE_CACHE_USER_SYMBOLS [default: auto] enable user symbol cache
177177
BPFTRACE_VMLINUX [default: none] vmlinux path used for kernel symbol resolution
178178
BPFTRACE_BTF [default: none] BTF file
179+
BPFTRACE_STACK_MODE [default: bpftrace] Output format for ustack and kstack builtins
179180
180181
EXAMPLES:
181182
bpftrace -l '*sleep*'
@@ -553,6 +554,12 @@ Default: `..`
553554

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

557+
### 9.11 `BPFTRACE_STACK_MODE`
558+
559+
Default: bpftrace
560+
561+
Output format for ustack and kstack builtins. Available modes/formats: `bpftrace`, `perf`, and `raw`.
562+
This can be overwritten at the call site.
556563

557564
## 10. Clang Environment Variables
558565

man/adoc/bpftrace.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ Default: `..`
258258

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

261+
=== BPFTRACE_STACK_MODE
262+
263+
Default: bpftrace
264+
265+
Output format for ustack and kstack builtins. Available modes/formats: `bpftrace`, `perf`, and `raw`.
266+
This can be overwritten at the call site.
267+
261268
////
262269
!
263270
!

src/ast/passes/semantic_analyser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,12 @@ void SemanticAnalyser::check_stack_call(Call &call, bool kernel)
14171417
}
14181418

14191419
StackType stack_type;
1420+
1421+
if (bpftrace_.stack_mode_.has_value())
1422+
{
1423+
stack_type.mode = *bpftrace_.stack_mode_;
1424+
}
1425+
14201426
if (call.vargs)
14211427
{
14221428
switch (call.vargs->size())

src/bpftrace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class BPFtrace
195195
bool usdt_file_activation_ = false;
196196
int helper_check_level_ = 0;
197197
uint64_t ast_max_nodes_ = 0; // Maximum AST nodes allowed for fuzzing
198+
std::optional<StackMode> stack_mode_;
198199
std::optional<struct timespec> boottime_;
199200

200201
static void sort_by_key(

src/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ void usage()
128128
std::cerr << " BPFTRACE_VMLINUX [default: none] vmlinux path used for kernel symbol resolution" << std::endl;
129129
std::cerr << " BPFTRACE_BTF [default: none] BTF file" << std::endl;
130130
std::cerr << " BPFTRACE_STR_TRUNC_TRAILER [default: '..'] string truncation trailer" << std::endl;
131+
std::cerr << " BPFTRACE_STACK_MODE [default: bpftrace] Output format for ustack and kstack builtins" << std::endl;
131132
std::cerr << std::endl;
132133
std::cerr << "EXAMPLES:" << std::endl;
133134
std::cerr << "bpftrace -l '*sleep*'" << std::endl;
@@ -334,6 +335,21 @@ static std::optional<struct timespec> get_boottime()
334335
if (!get_bool_env_var("BPFTRACE_VERIFY_LLVM_IR", verify_llvm_ir))
335336
return false;
336337

338+
if (const char* stack_mode = std::getenv("BPFTRACE_STACK_MODE"))
339+
{
340+
auto found = STACK_MODE_MAP.find(stack_mode);
341+
if (found != STACK_MODE_MAP.end())
342+
{
343+
bpftrace.stack_mode_ = found->second;
344+
}
345+
else
346+
{
347+
LOG(ERROR) << "Env var 'BPFTRACE_STACK_MODE' did not contain a valid "
348+
"StackMode: "
349+
<< stack_mode;
350+
}
351+
}
352+
337353
return true;
338354
}
339355

src/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ enum class StackMode
6969
raw,
7070
};
7171

72+
const std::map<std::string, StackMode> STACK_MODE_MAP = {
73+
{ "bpftrace", StackMode::bpftrace },
74+
{ "perf", StackMode::perf },
75+
{ "raw", StackMode::raw },
76+
};
77+
7278
struct StackType
7379
{
7480
size_t limit = DEFAULT_STACK_SIZE;

tests/runtime/call

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,40 @@ EXPECT Attaching 1 probe
207207
TIMEOUT 5
208208
AFTER ./testprogs/syscall nanosleep 1e8
209209

210-
211210
NAME ustack
212211
PROG k:do_nanosleep { printf("%s\n%s\n", ustack(), ustack(1)); exit(); }
213212
EXPECT Attaching 1 probe
214213
TIMEOUT 5
215214
AFTER ./testprogs/syscall nanosleep 1e8
216215

216+
NAME ustack_stack_mode_env_bpftrace
217+
PROG k:do_nanosleep { printf("%s", ustack(1)); exit(); }
218+
ENV BPFTRACE_STACK_MODE=bpftrace
219+
EXPECT ^\s+[a-zA-Z0-9_]+\+[0-9]+
220+
TIMEOUT 5
221+
AFTER ./testprogs/syscall nanosleep 1e8
222+
223+
NAME ustack_stack_mode_env_perf
224+
PROG k:do_nanosleep { printf("%s", ustack(1)); exit(); }
225+
ENV BPFTRACE_STACK_MODE=perf
226+
EXPECT ^\s+[0-9a-f]+ [a-zA-Z0-9_]+\+[0-9]+
227+
TIMEOUT 5
228+
AFTER ./testprogs/syscall nanosleep 1e8
229+
230+
NAME ustack_stack_mode_env_raw
231+
PROG k:do_nanosleep { printf("%s", ustack(1)); exit(); }
232+
ENV BPFTRACE_STACK_MODE=raw
233+
EXPECT ^\s+[0-9a-f]+$
234+
TIMEOUT 5
235+
AFTER ./testprogs/syscall nanosleep 1e8
236+
237+
NAME ustack_stack_mode_env_override
238+
PROG k:do_nanosleep { printf("%s", ustack(raw, 1)); exit(); }
239+
ENV BPFTRACE_STACK_MODE=perf
240+
EXPECT ^\s+[0-9a-f]+$
241+
TIMEOUT 5
242+
AFTER ./testprogs/syscall nanosleep 1e8
243+
217244
NAME cat
218245
PROG i:ms:1 { cat("/proc/uptime"); exit();}
219246
EXPECT [0-9]*.[0-9]* [0-9]*.[0-9]*

0 commit comments

Comments
 (0)