Debugging Scripts
When writing complex bpftrace scripts, errors can arise from syntax issues, incorrect probe definitions, or type mismatches in eBPF programs. Debugging these requires leveraging verbose logging, type-checking, and validation tools provided by bpftrace. This section covers techniques to identify and resolve script errors effectively.
Verbose Logging¶
The -v (verbose) flag enables detailed logging during script execution, revealing internal processing steps, probe resolution, and potential issues. Use it to diagnose problems like missing probes, incorrect kernel versions, or unresolved symbols.
Example:
Output example:
INFO: bpftrace v0.14.0
INFO: Loading script.bt
INFO: Found 3 probes in script.bt
INFO: Could not resolve probe: kprobe:sys_open
ERROR: No such probe: kprobe:sys_open
If a probe is unresolved, verify its name using bpftrace --list-probes or check kernel version compatibility.
Log Level Control¶
The --log-level parameter allows fine-grained control over the verbosity of logs. Valid levels include:
- 0 (quiet): Minimal output
- 1 (info): Standard informational messages
- 2 (debug): Detailed debug logs
- 3 (trace): Full trace of internal operations
Use this to adjust logging granularity without disabling all verbose output. For example:
Type-Checking¶
eBPF programs require strict type-checking due to their safety constraints. Use the --type-check flag to validate scripts before execution, catching errors like invalid variable types or incorrect arithmetic operations.
Example:
Error example:
This indicates a script attempting to perform math with a string variable. Fix by ensuring variables are of numeric type.
Pass Mode¶
The --pass flag runs the script in "pass mode," validating syntax and probe existence without executing the eBPF program. This is ideal for pre-flight checks.
Example:
Success output:
Failure output:
Use this to catch syntax errors early, such as missing semicolons or mismatched braces.
Common Pitfalls¶
- Kernel version mismatches: Ensure the kernel supports the required eBPF features (e.g.,
CONFIG_BPF_SYSCALL). Useuname -rand check kernel documentation. - Probe syntax errors: Probes like
kprobe:__x64_sys_openrequire precise syntax. Usebpftrace --list-probesto verify valid names. - Resource limits: Some scripts may fail due to insufficient memory or CPU limits. Monitor with
dmesgorperf.
Key takeaways¶
- Use
-vand--log-levelto inspect detailed runtime logs and resolve probe resolution issues. - Leverage
--type-checkto catch eBPF type mismatches and invalid operations. - Run scripts in
--passmode to validate syntax and probe existence without execution. - Combine logging and type-checking to isolate errors in complex scripts.