crabgrind 0.1.9

Rust bindings to "Valgrind Client Request" interface
Documentation

crates.io libs.rs documentation license

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.

Table of Contents

Valgrind 3 API coverage

Quickstart

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

Examples

Overhead

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.

Safety

No

Development

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

License

crabgrind is distributed under MIT license.

Valgrind itself is a GPL2, however valgrind/*.h headers are distributed under a BSD-style license, so we can use them without worrying about license conflicts.