Mark
Mark

Reputation: 4109

Intercepting sleep calls in C++

Is there a way to intercept calls to sleep and sleep-like functions in C++? I'd love to be able to replace the implementation with a no-op or in the alternative, multiply the sleep time. I imagine this would aid in determining correctness of concurrent programs as well as identifying a source of flakiness in tests.

I'm operating on a gigantic codebase, so using a wrapper function would be less satisfactory. Maybe there's a way to use ptrace or the same techniques that programs like valgrind use to intercept malloc?

Upvotes: 2

Views: 275

Answers (1)

Klaus
Klaus

Reputation: 25593

For gcc users there is a simple way to modify some calls to the libraries and link against own functions without changing the code itself.

if you have a snippet like:

... some stuff ...
AnyLibFunc();
... some stuff ...

you can advice the linker to use a wrapped method with the following line:

gcc program.c -Wl,-wrap,AnyLibFunc -o program

And you have to add your implementation of the wrapped func:

void __wrap_AnyLibFunc ()
{
    __real_AnyLibFunc(  ); // call the real function
}

In hope you are working on gcc environment!

Upvotes: 6

Related Questions