crabgrind
wraps various Valgrind macros in C functions, compiles and links against
the resulting binary, and exposes an unsafe interface to allow Rust programs running under Valgrind to
interact with the tools and environment.
This library is indeed a wrapper, the only thing it adds are the type conversions and some structure, all the real things are happening inside Valgrind.
- Supported tool-specific client request interface: valgrind, callgrind, memcheck, helgrind, massif
- Monitor commands interface
crabgrind
doesn't links against Valgrind, but reads it's header files, so they must be accessible
to build the project.
If headers resides at the /usr/include/valgrind
, cc
, the build
tool crabgrind
uses, will find them.
If you have installed Vallgrind manually, you can set DEP_VALGRIND
environment variable to the appropriate path,
its value, if specified, will be directly passed to cc::Build::include
.
env DEP_VALGRIND=/usr/include cargo build
Add the following to your Cargo.toml
file:
[dependencies]
crabgrind = "~0.1"
Next, use some of the Valgrind's API
use crabgrind as cg;
fn main() {
if matches!(cg::run_mode(), cg::RunMode::Native) {
println!("run me under Valgrind");
} else {
cg::println!("Hey, Valgrind!");
}
}
And run your application under Valgrind, either with handy cargo-valgrind
cargo valgrind run
or manually
cargo build
valgrind ./target/debug/appname
- Print current function stack-trace to the Valgrind log
- Exclude expensive initialization code from the measurements
- Run a closure on the real CPU while running under Valgrind
- Save current memory usage snapshot to a file
- Dump Callgrind counters on a function basis
from Valgrind docs
The code added to your binary has negligible performance impact: on x86, amd64, ppc32, ppc64 and ARM, the overhead is 6 simple integer instructions and is probably undetectable except in tight loops.
... the code does nothing when not run on Valgrind, so you are not forced to run your program under Valgrind just because you use the macros in this file.
however,
- wrapping each macros in a function implies function call overhead regardless of the run mode
- functions that returns
std::result::Result
involve branching - functions that takes strings as a parameters internally converts them to
std::ffi::CString
If you wish to compile out all (crab)Valgrind from the binary, you can wrap crabgrind
calls with
the feature-gate.
No
Tests must be run under Valgrind, as of now cargo-valgrind
fits nicely, it allows to compile and run tests under Valgrind in one command
cargo valgrind test
crabgrind
is distributed under the same license terms as the Valgrind
that is GPL version 2.